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
6512a712cc4e65e397d9a94e6068c6c02dd2b7cc
8b9f17008684d796c8022dab552e42f0cb6fb347
/tests/lean/hott/443_b.hlean
d78102bde9026b607a89923e32ba6b33f1cbd8d3
[ "Apache-2.0" ]
permissive
chubbymaggie/lean
0d06ae25f9dd396306fb02190e89422ea94afd7b
d2c7b5c31928c98f545b16420d37842c43b4ae9a
refs/heads/master
1,611,313,622,901
1,430,266,839,000
1,430,267,083,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
578
hlean
import algebra.groupoid algebra.group open eq sigma unit category path_algebra equiv set_option pp.implicit true set_option pp.universes true set_option pp.notation false section parameters {D₀ : Type} [C : precategory D₀] {D₂ : Π ⦃a b c d : D₀⦄ (f : hom a b) (g : hom c d) (h : hom a c) (i : hom b d), Type} include C structure my_structure1 : Type := (vo1 : D₀) (vo2 : D₀) check my_structure1 definition foo2 : Type := my_structure1 check foo2 end definition foo3 : Π {D₀ : Type} [C : precategory D₀], Type := @my_structure1
9769d44701973d407280c511e15eb4973fb795e5
900ff83b8a995f83b07c2fa0715d52fa265c4b9e
/library/init/data/int/basic.lean
4ffe5a658b2ef11b7c915c2f91c9d09336f052da
[ "Apache-2.0" ]
permissive
moritzbuhl/lean
b2ee98197f1c47255c647228c07c830b229ba766
89e98b56713c027afdf97ad96abd2d54b35a43d5
refs/heads/master
1,688,295,006,616
1,627,741,115,000
1,627,741,115,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
23,227
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad The integers, with addition, multiplication, and subtraction. -/ prelude import init.data.nat.lemmas init.data.nat.gcd open nat /- the type, coercions, and notation -/ @[derive decidable_eq] inductive int : Type | of_nat : nat → int | neg_succ_of_nat : nat → int notation `ℤ` := int instance : has_coe nat int := ⟨int.of_nat⟩ notation `-[1+ ` n `]` := int.neg_succ_of_nat n protected def int.repr : int → string | (int.of_nat n) := repr n | (int.neg_succ_of_nat n) := "-" ++ repr (succ n) instance : has_repr int := ⟨int.repr⟩ instance : has_to_string int := ⟨int.repr⟩ namespace int protected lemma coe_nat_eq (n : ℕ) : ↑n = int.of_nat n := rfl protected def zero : ℤ := of_nat 0 protected def one : ℤ := of_nat 1 instance : has_zero ℤ := ⟨int.zero⟩ instance : has_one ℤ := ⟨int.one⟩ lemma of_nat_zero : of_nat (0 : nat) = (0 : int) := rfl lemma of_nat_one : of_nat (1 : nat) = (1 : int) := rfl /- definitions of basic functions -/ def neg_of_nat : ℕ → ℤ | 0 := 0 | (succ m) := -[1+ m] def sub_nat_nat (m n : ℕ) : ℤ := match (n - m : nat) with | 0 := of_nat (m - n) -- m ≥ n | (succ k) := -[1+ k] -- m < n, and n - m = succ k end lemma sub_nat_nat_of_sub_eq_zero {m n : ℕ} (h : n - m = 0) : sub_nat_nat m n = of_nat (m - n) := begin unfold sub_nat_nat, rw h, unfold sub_nat_nat._match_1 end lemma sub_nat_nat_of_sub_eq_succ {m n k : ℕ} (h : n - m = succ k) : sub_nat_nat m n = -[1+ k] := begin unfold sub_nat_nat, rw h, unfold sub_nat_nat._match_1 end protected def neg : ℤ → ℤ | (of_nat n) := neg_of_nat n | -[1+ n] := succ n protected def add : ℤ → ℤ → ℤ | (of_nat m) (of_nat n) := of_nat (m + n) | (of_nat m) -[1+ n] := sub_nat_nat m (succ n) | -[1+ m] (of_nat n) := sub_nat_nat n (succ m) | -[1+ m] -[1+ n] := -[1+ succ (m + n)] protected def mul : ℤ → ℤ → ℤ | (of_nat m) (of_nat n) := of_nat (m * n) | (of_nat m) -[1+ n] := neg_of_nat (m * succ n) | -[1+ m] (of_nat n) := neg_of_nat (succ m * n) | -[1+ m] -[1+ n] := of_nat (succ m * succ n) instance : has_neg ℤ := ⟨int.neg⟩ instance : has_add ℤ := ⟨int.add⟩ instance : has_mul ℤ := ⟨int.mul⟩ -- defeq to algebra.sub which gives subtraction for arbitrary `add_group`s protected def sub : ℤ → ℤ → ℤ := λ m n, m + -n instance : has_sub ℤ := ⟨int.sub⟩ protected lemma neg_zero : -(0:ℤ) = 0 := rfl lemma of_nat_add (n m : ℕ) : of_nat (n + m) = of_nat n + of_nat m := rfl lemma of_nat_mul (n m : ℕ) : of_nat (n * m) = of_nat n * of_nat m := rfl lemma of_nat_succ (n : ℕ) : of_nat (succ n) = of_nat n + 1 := rfl lemma neg_of_nat_zero : -(of_nat 0) = 0 := rfl lemma neg_of_nat_of_succ (n : ℕ) : -(of_nat (succ n)) = -[1+ n] := rfl lemma neg_neg_of_nat_succ (n : ℕ) : -(-[1+ n]) = of_nat (succ n) := rfl lemma of_nat_eq_coe (n : ℕ) : of_nat n = ↑n := rfl lemma neg_succ_of_nat_coe (n : ℕ) : -[1+ n] = -↑(n + 1) := rfl protected lemma coe_nat_add (m n : ℕ) : (↑(m + n) : ℤ) = ↑m + ↑n := rfl protected lemma coe_nat_mul (m n : ℕ) : (↑(m * n) : ℤ) = ↑m * ↑n := rfl protected lemma coe_nat_zero : ↑(0 : ℕ) = (0 : ℤ) := rfl protected lemma coe_nat_one : ↑(1 : ℕ) = (1 : ℤ) := rfl protected lemma coe_nat_succ (n : ℕ) : (↑(succ n) : ℤ) = ↑n + 1 := rfl protected lemma coe_nat_add_out (m n : ℕ) : ↑m + ↑n = (m + n : ℤ) := rfl protected lemma coe_nat_mul_out (m n : ℕ) : ↑m * ↑n = (↑(m * n) : ℤ) := rfl protected lemma coe_nat_add_one_out (n : ℕ) : ↑n + (1 : ℤ) = ↑(succ n) := rfl /- these are only for internal use -/ lemma of_nat_add_of_nat (m n : nat) : of_nat m + of_nat n = of_nat (m + n) := rfl lemma of_nat_add_neg_succ_of_nat (m n : nat) : of_nat m + -[1+ n] = sub_nat_nat m (succ n) := rfl lemma neg_succ_of_nat_add_of_nat (m n : nat) : -[1+ m] + of_nat n = sub_nat_nat n (succ m) := rfl lemma neg_succ_of_nat_add_neg_succ_of_nat (m n : nat) : -[1+ m] + -[1+ n] = -[1+ succ (m + n)] := rfl lemma of_nat_mul_of_nat (m n : nat) : of_nat m * of_nat n = of_nat (m * n) := rfl lemma of_nat_mul_neg_succ_of_nat (m n : nat) : of_nat m * -[1+ n] = neg_of_nat (m * succ n) := rfl lemma neg_succ_of_nat_of_nat (m n : nat) : -[1+ m] * of_nat n = neg_of_nat (succ m * n) := rfl lemma mul_neg_succ_of_nat_neg_succ_of_nat (m n : nat) : -[1+ m] * -[1+ n] = of_nat (succ m * succ n) := rfl local attribute [simp] of_nat_add_of_nat of_nat_mul_of_nat neg_of_nat_zero neg_of_nat_of_succ neg_neg_of_nat_succ of_nat_add_neg_succ_of_nat neg_succ_of_nat_add_of_nat neg_succ_of_nat_add_neg_succ_of_nat of_nat_mul_neg_succ_of_nat neg_succ_of_nat_of_nat mul_neg_succ_of_nat_neg_succ_of_nat /- some basic functions and properties -/ protected lemma coe_nat_inj {m n : ℕ} (h : (↑m : ℤ) = ↑n) : m = n := int.of_nat.inj h lemma of_nat_eq_of_nat_iff (m n : ℕ) : of_nat m = of_nat n ↔ m = n := iff.intro int.of_nat.inj (congr_arg _) protected lemma coe_nat_eq_coe_nat_iff (m n : ℕ) : (↑m : ℤ) = ↑n ↔ m = n := of_nat_eq_of_nat_iff m n lemma neg_succ_of_nat_inj_iff {m n : ℕ} : neg_succ_of_nat m = neg_succ_of_nat n ↔ m = n := ⟨neg_succ_of_nat.inj, assume H, by simp [H]⟩ lemma neg_succ_of_nat_eq (n : ℕ) : -[1+ n] = -(n + 1) := rfl /- neg -/ protected lemma neg_neg : ∀ a : ℤ, -(-a) = a | (of_nat 0) := rfl | (of_nat (n+1)) := rfl | -[1+ n] := rfl protected lemma neg_inj {a b : ℤ} (h : -a = -b) : a = b := by rw [← int.neg_neg a, ← int.neg_neg b, h] protected lemma sub_eq_add_neg {a b : ℤ} : a - b = a + -b := rfl /- basic properties of sub_nat_nat -/ lemma sub_nat_nat_elim (m n : ℕ) (P : ℕ → ℕ → ℤ → Prop) (hp : ∀i n, P (n + i) n (of_nat i)) (hn : ∀i m, P m (m + i + 1) (-[1+ i])) : P m n (sub_nat_nat m n) := begin have H : ∀k, n - m = k → P m n (nat.cases_on k (of_nat (m - n)) (λa, -[1+ a])), { intro k, cases k, { intro e, cases (nat.le.dest (nat.le_of_sub_eq_zero e)) with k h, rw [h.symm, nat.add_sub_cancel_left], apply hp }, { intro heq, have h : m ≤ n, { exact nat.le_of_lt (nat.lt_of_sub_eq_succ heq) }, rw [nat.sub_eq_iff_eq_add h] at heq, rw [heq, nat.add_comm], apply hn } }, delta sub_nat_nat, exact H _ rfl end lemma sub_nat_nat_add_left {m n : ℕ} : sub_nat_nat (m + n) m = of_nat n := begin dunfold sub_nat_nat, rw [nat.sub_eq_zero_of_le], dunfold sub_nat_nat._match_1, rw [nat.add_sub_cancel_left], apply nat.le_add_right end lemma sub_nat_nat_add_right {m n : ℕ} : sub_nat_nat m (m + n + 1) = neg_succ_of_nat n := calc sub_nat_nat._match_1 m (m + n + 1) (m + n + 1 - m) = sub_nat_nat._match_1 m (m + n + 1) (m + (n + 1) - m) : by rw [nat.add_assoc] ... = sub_nat_nat._match_1 m (m + n + 1) (n + 1) : by rw [nat.add_sub_cancel_left] ... = neg_succ_of_nat n : rfl lemma sub_nat_nat_add_add (m n k : ℕ) : sub_nat_nat (m + k) (n + k) = sub_nat_nat m n := sub_nat_nat_elim m n (λm n i, sub_nat_nat (m + k) (n + k) = i) (assume i n, have n + i + k = (n + k) + i, by simp [nat.add_comm, nat.add_left_comm], begin rw [this], exact sub_nat_nat_add_left end) (assume i m, have m + i + 1 + k = (m + k) + i + 1, by simp [nat.add_comm, nat.add_left_comm], begin rw [this], exact sub_nat_nat_add_right end) lemma sub_nat_nat_of_le {m n : ℕ} (h : n ≤ m) : sub_nat_nat m n = of_nat (m - n) := sub_nat_nat_of_sub_eq_zero (nat.sub_eq_zero_of_le h) lemma sub_nat_nat_of_lt {m n : ℕ} (h : m < n) : sub_nat_nat m n = -[1+ pred (n - m)] := have n - m = succ (pred (n - m)), from eq.symm (succ_pred_eq_of_pos (nat.sub_pos_of_lt h)), by rewrite sub_nat_nat_of_sub_eq_succ this /- nat_abs -/ @[simp] def nat_abs : ℤ → ℕ | (of_nat m) := m | -[1+ m] := succ m lemma nat_abs_of_nat (n : ℕ) : nat_abs ↑n = n := rfl lemma eq_zero_of_nat_abs_eq_zero : Π {a : ℤ}, nat_abs a = 0 → a = 0 | (of_nat m) H := congr_arg of_nat H | -[1+ m'] H := absurd H (succ_ne_zero _) lemma nat_abs_pos_of_ne_zero {a : ℤ} (h : a ≠ 0) : 0 < nat_abs a := (nat.eq_zero_or_pos _).resolve_left $ mt eq_zero_of_nat_abs_eq_zero h lemma nat_abs_zero : nat_abs (0 : int) = (0 : nat) := rfl lemma nat_abs_one : nat_abs (1 : int) = (1 : nat) := rfl lemma nat_abs_mul_self : Π {a : ℤ}, ↑(nat_abs a * nat_abs a) = a * a | (of_nat m) := rfl | -[1+ m'] := rfl @[simp] lemma nat_abs_neg (a : ℤ) : nat_abs (-a) = nat_abs a := by {cases a with n n, cases n; refl, refl} lemma nat_abs_eq : Π (a : ℤ), a = nat_abs a ∨ a = -(nat_abs a) | (of_nat m) := or.inl rfl | -[1+ m'] := or.inr rfl lemma eq_coe_or_neg (a : ℤ) : ∃n : ℕ, a = n ∨ a = -n := ⟨_, nat_abs_eq a⟩ /- sign -/ def sign : ℤ → ℤ | (n+1:ℕ) := 1 | 0 := 0 | -[1+ n] := -1 @[simp] theorem sign_zero : sign 0 = 0 := rfl @[simp] theorem sign_one : sign 1 = 1 := rfl @[simp] theorem sign_neg_one : sign (-1) = -1 := rfl /- Quotient and remainder -/ -- There are three main conventions for integer division, -- referred here as the E, F, T rounding conventions. -- All three pairs satisfy the identity x % y + (x / y) * y = x -- unconditionally. -- E-rounding: This pair satisfies 0 ≤ mod x y < nat_abs y for y ≠ 0 protected def div : ℤ → ℤ → ℤ | (m : ℕ) (n : ℕ) := of_nat (m / n) | (m : ℕ) -[1+ n] := -of_nat (m / succ n) | -[1+ m] 0 := 0 | -[1+ m] (n+1:ℕ) := -[1+ m / succ n] | -[1+ m] -[1+ n] := of_nat (succ (m / succ n)) protected def mod : ℤ → ℤ → ℤ | (m : ℕ) n := (m % nat_abs n : ℕ) | -[1+ m] n := sub_nat_nat (nat_abs n) (succ (m % nat_abs n)) -- F-rounding: This pair satisfies fdiv x y = floor (x / y) def fdiv : ℤ → ℤ → ℤ | 0 _ := 0 | (m : ℕ) (n : ℕ) := of_nat (m / n) | (m+1:ℕ) -[1+ n] := -[1+ m / succ n] | -[1+ m] 0 := 0 | -[1+ m] (n+1:ℕ) := -[1+ m / succ n] | -[1+ m] -[1+ n] := of_nat (succ m / succ n) def fmod : ℤ → ℤ → ℤ | 0 _ := 0 | (m : ℕ) (n : ℕ) := of_nat (m % n) | (m+1:ℕ) -[1+ n] := sub_nat_nat (m % succ n) n | -[1+ m] (n : ℕ) := sub_nat_nat n (succ (m % n)) | -[1+ m] -[1+ n] := -of_nat (succ m % succ n) -- T-rounding: This pair satisfies quot x y = round_to_zero (x / y) def quot : ℤ → ℤ → ℤ | (of_nat m) (of_nat n) := of_nat (m / n) | (of_nat m) -[1+ n] := -of_nat (m / succ n) | -[1+ m] (of_nat n) := -of_nat (succ m / n) | -[1+ m] -[1+ n] := of_nat (succ m / succ n) def rem : ℤ → ℤ → ℤ | (of_nat m) (of_nat n) := of_nat (m % n) | (of_nat m) -[1+ n] := of_nat (m % succ n) | -[1+ m] (of_nat n) := -of_nat (succ m % n) | -[1+ m] -[1+ n] := -of_nat (succ m % succ n) instance : has_div ℤ := ⟨int.div⟩ instance : has_mod ℤ := ⟨int.mod⟩ /- gcd -/ def gcd (m n : ℤ) : ℕ := gcd (nat_abs m) (nat_abs n) /- int is a ring -/ /- addition -/ protected lemma add_comm : ∀ a b : ℤ, a + b = b + a | (of_nat n) (of_nat m) := by simp [nat.add_comm] | (of_nat n) -[1+ m] := rfl | -[1+ n] (of_nat m) := rfl | -[1+ n] -[1+m] := by simp [nat.add_comm] protected lemma add_zero : ∀ a : ℤ, a + 0 = a | (of_nat n) := rfl | -[1+ n] := rfl protected lemma zero_add (a : ℤ) : 0 + a = a := int.add_comm a 0 ▸ int.add_zero a lemma sub_nat_nat_sub {m n : ℕ} (h : n ≤ m) (k : ℕ) : sub_nat_nat (m - n) k = sub_nat_nat m (k + n) := calc sub_nat_nat (m - n) k = sub_nat_nat (m - n + n) (k + n) : by rewrite [sub_nat_nat_add_add] ... = sub_nat_nat m (k + n) : by rewrite [nat.sub_add_cancel h] lemma sub_nat_nat_add (m n k : ℕ) : sub_nat_nat (m + n) k = of_nat m + sub_nat_nat n k := begin have h := le_or_lt k n, cases h with h' h', { rw [sub_nat_nat_of_le h'], have h₂ : k ≤ m + n, exact (le_trans h' (nat.le_add_left _ _)), rw [sub_nat_nat_of_le h₂], simp, rw nat.add_sub_assoc h' }, rw [sub_nat_nat_of_lt h'], simp, rw [succ_pred_eq_of_pos (nat.sub_pos_of_lt h')], transitivity, rw [← nat.sub_add_cancel (le_of_lt h')], apply sub_nat_nat_add_add end lemma sub_nat_nat_add_neg_succ_of_nat (m n k : ℕ) : sub_nat_nat m n + -[1+ k] = sub_nat_nat m (n + succ k) := begin have h := le_or_lt n m, cases h with h' h', { rw [sub_nat_nat_of_le h'], simp, rw [sub_nat_nat_sub h', nat.add_comm] }, have h₂ : m < n + succ k, exact nat.lt_of_lt_of_le h' (nat.le_add_right _ _), have h₃ : m ≤ n + k, exact le_of_succ_le_succ h₂, rw [sub_nat_nat_of_lt h', sub_nat_nat_of_lt h₂], simp [nat.add_comm], rw [← add_succ, succ_pred_eq_of_pos (nat.sub_pos_of_lt h'), add_succ, succ_sub h₃, pred_succ], rw [nat.add_comm n, nat.add_sub_assoc (le_of_lt h')] end lemma add_assoc_aux1 (m n : ℕ) : ∀ c : ℤ, of_nat m + of_nat n + c = of_nat m + (of_nat n + c) | (of_nat k) := by simp [nat.add_assoc] | -[1+ k] := by simp [sub_nat_nat_add] lemma add_assoc_aux2 (m n k : ℕ) : -[1+ m] + -[1+ n] + of_nat k = -[1+ m] + (-[1+ n] + of_nat k) := begin simp [add_succ], rw [int.add_comm, sub_nat_nat_add_neg_succ_of_nat], simp [add_succ, succ_add, nat.add_comm] end protected lemma add_assoc : ∀ a b c : ℤ, a + b + c = a + (b + c) | (of_nat m) (of_nat n) c := add_assoc_aux1 _ _ _ | (of_nat m) b (of_nat k) := by rw [int.add_comm, ← add_assoc_aux1, int.add_comm (of_nat k), add_assoc_aux1, int.add_comm b] | a (of_nat n) (of_nat k) := by rw [int.add_comm, int.add_comm a, ← add_assoc_aux1, int.add_comm a, int.add_comm (of_nat k)] | -[1+ m] -[1+ n] (of_nat k) := add_assoc_aux2 _ _ _ | -[1+ m] (of_nat n) -[1+ k] := by rw [int.add_comm, ← add_assoc_aux2, int.add_comm (of_nat n), ← add_assoc_aux2, int.add_comm -[1+ m] ] | (of_nat m) -[1+ n] -[1+ k] := by rw [int.add_comm, int.add_comm (of_nat m), int.add_comm (of_nat m), ← add_assoc_aux2, int.add_comm -[1+ k] ] | -[1+ m] -[1+ n] -[1+ k] := by simp [add_succ, nat.add_comm, nat.add_left_comm, neg_of_nat_of_succ] /- negation -/ lemma sub_nat_self : ∀ n, sub_nat_nat n n = 0 | 0 := rfl | (succ m) := begin rw [sub_nat_nat_of_sub_eq_zero, nat.sub_self, of_nat_zero], rw nat.sub_self end local attribute [simp] sub_nat_self protected lemma add_left_neg : ∀ a : ℤ, -a + a = 0 | (of_nat 0) := rfl | (of_nat (succ m)) := by simp | -[1+ m] := by simp protected lemma add_right_neg (a : ℤ) : a + -a = 0 := by rw [int.add_comm, int.add_left_neg] /- multiplication -/ protected lemma mul_comm : ∀ a b : ℤ, a * b = b * a | (of_nat m) (of_nat n) := by simp [nat.mul_comm] | (of_nat m) -[1+ n] := by simp [nat.mul_comm] | -[1+ m] (of_nat n) := by simp [nat.mul_comm] | -[1+ m] -[1+ n] := by simp [nat.mul_comm] lemma of_nat_mul_neg_of_nat (m : ℕ) : ∀ n, of_nat m * neg_of_nat n = neg_of_nat (m * n) | 0 := rfl | (succ n) := begin unfold neg_of_nat, simp end lemma neg_of_nat_mul_of_nat (m n : ℕ) : neg_of_nat m * of_nat n = neg_of_nat (m * n) := begin rw int.mul_comm, simp [of_nat_mul_neg_of_nat, nat.mul_comm] end lemma neg_succ_of_nat_mul_neg_of_nat (m : ℕ) : ∀ n, -[1+ m] * neg_of_nat n = of_nat (succ m * n) | 0 := rfl | (succ n) := begin unfold neg_of_nat, simp end lemma neg_of_nat_mul_neg_succ_of_nat (m n : ℕ) : neg_of_nat n * -[1+ m] = of_nat (n * succ m) := begin rw int.mul_comm, simp [neg_succ_of_nat_mul_neg_of_nat, nat.mul_comm] end local attribute [simp] of_nat_mul_neg_of_nat neg_of_nat_mul_of_nat neg_succ_of_nat_mul_neg_of_nat neg_of_nat_mul_neg_succ_of_nat protected lemma mul_assoc : ∀ a b c : ℤ, a * b * c = a * (b * c) | (of_nat m) (of_nat n) (of_nat k) := by simp [nat.mul_assoc] | (of_nat m) (of_nat n) -[1+ k] := by simp [nat.mul_assoc] | (of_nat m) -[1+ n] (of_nat k) := by simp [nat.mul_assoc] | (of_nat m) -[1+ n] -[1+ k] := by simp [nat.mul_assoc] | -[1+ m] (of_nat n) (of_nat k) := by simp [nat.mul_assoc] | -[1+ m] (of_nat n) -[1+ k] := by simp [nat.mul_assoc] | -[1+ m] -[1+ n] (of_nat k) := by simp [nat.mul_assoc] | -[1+ m] -[1+ n] -[1+ k] := by simp [nat.mul_assoc] protected lemma mul_zero : ∀ (a : ℤ), a * 0 = 0 | (of_nat m) := rfl | -[1+ m] := rfl protected lemma zero_mul (a : ℤ) : 0 * a = 0 := int.mul_comm a 0 ▸ int.mul_zero a lemma neg_of_nat_eq_sub_nat_nat_zero : ∀ n, neg_of_nat n = sub_nat_nat 0 n | 0 := rfl | (succ n) := rfl lemma of_nat_mul_sub_nat_nat (m n k : ℕ) : of_nat m * sub_nat_nat n k = sub_nat_nat (m * n) (m * k) := begin have h₀ : m > 0 ∨ 0 = m, exact decidable.lt_or_eq_of_le m.zero_le, cases h₀ with h₀ h₀, { have h := nat.lt_or_ge n k, cases h with h h, { have h' : m * n < m * k, exact nat.mul_lt_mul_of_pos_left h h₀, rw [sub_nat_nat_of_lt h, sub_nat_nat_of_lt h'], simp, rw [succ_pred_eq_of_pos (nat.sub_pos_of_lt h)], rw [← neg_of_nat_of_succ, nat.mul_sub_left_distrib], rw [← succ_pred_eq_of_pos (nat.sub_pos_of_lt h')], reflexivity }, have h' : m * k ≤ m * n, exact nat.mul_le_mul_left _ h, rw [sub_nat_nat_of_le h, sub_nat_nat_of_le h'], simp, rw [nat.mul_sub_left_distrib] }, have h₂ : of_nat 0 = 0, exact rfl, subst h₀, simp [h₂, int.zero_mul, nat.zero_mul] end lemma neg_of_nat_add (m n : ℕ) : neg_of_nat m + neg_of_nat n = neg_of_nat (m + n) := begin cases m, { cases n, { simp, reflexivity }, simp [nat.zero_add], reflexivity }, cases n, { simp, reflexivity }, simp [nat.succ_add], reflexivity end lemma neg_succ_of_nat_mul_sub_nat_nat (m n k : ℕ) : -[1+ m] * sub_nat_nat n k = sub_nat_nat (succ m * k) (succ m * n) := begin have h := nat.lt_or_ge n k, cases h with h h, { have h' : succ m * n < succ m * k, exact nat.mul_lt_mul_of_pos_left h (nat.succ_pos m), rw [sub_nat_nat_of_lt h, sub_nat_nat_of_le (le_of_lt h')], simp [succ_pred_eq_of_pos (nat.sub_pos_of_lt h), nat.mul_sub_left_distrib]}, have h' : n > k ∨ k = n, exact decidable.lt_or_eq_of_le h, cases h' with h' h', { have h₁ : succ m * n > succ m * k, exact nat.mul_lt_mul_of_pos_left h' (nat.succ_pos m), rw [sub_nat_nat_of_le h, sub_nat_nat_of_lt h₁], simp [nat.mul_sub_left_distrib, nat.mul_comm], rw [nat.mul_comm k, nat.mul_comm n, ← succ_pred_eq_of_pos (nat.sub_pos_of_lt h₁), ← neg_of_nat_of_succ], reflexivity }, subst h', simp, reflexivity end local attribute [simp] of_nat_mul_sub_nat_nat neg_of_nat_add neg_succ_of_nat_mul_sub_nat_nat protected lemma distrib_left : ∀ a b c : ℤ, a * (b + c) = a * b + a * c | (of_nat m) (of_nat n) (of_nat k) := by simp [nat.left_distrib] | (of_nat m) (of_nat n) -[1+ k] := begin simp [neg_of_nat_eq_sub_nat_nat_zero], rw ← sub_nat_nat_add, reflexivity end | (of_nat m) -[1+ n] (of_nat k) := begin simp [neg_of_nat_eq_sub_nat_nat_zero], rw [int.add_comm, ← sub_nat_nat_add], reflexivity end | (of_nat m) -[1+ n] -[1+ k] := begin simp, rw [← nat.left_distrib, succ_add] end | -[1+ m] (of_nat n) (of_nat k) := begin simp [nat.mul_comm], rw [← nat.right_distrib, nat.mul_comm] end | -[1+ m] (of_nat n) -[1+ k] := begin simp [neg_of_nat_eq_sub_nat_nat_zero], rw [int.add_comm, ← sub_nat_nat_add], reflexivity end | -[1+ m] -[1+ n] (of_nat k) := begin simp [neg_of_nat_eq_sub_nat_nat_zero], rw [← sub_nat_nat_add], reflexivity end | -[1+ m] -[1+ n] -[1+ k] := begin simp, rw [← nat.left_distrib, succ_add] end protected lemma distrib_right (a b c : ℤ) : (a + b) * c = a * c + b * c := begin rw [int.mul_comm, int.distrib_left], simp [int.mul_comm] end protected lemma zero_ne_one : (0 : int) ≠ 1 := assume h : 0 = 1, succ_ne_zero _ (int.of_nat.inj h).symm lemma of_nat_sub {n m : ℕ} (h : m ≤ n) : of_nat (n - m) = of_nat n - of_nat m := show of_nat (n - m) = of_nat n + neg_of_nat m, from match m, h with | 0, h := rfl | succ m, h := show of_nat (n - succ m) = sub_nat_nat n (succ m), by delta sub_nat_nat; rw nat.sub_eq_zero_of_le h; refl end protected lemma add_left_comm (a b c : ℤ) : a + (b + c) = b + (a + c) := by rw [← int.add_assoc, int.add_comm a, int.add_assoc] protected lemma add_left_cancel {a b c : ℤ} (h : a + b = a + c) : b = c := have -a + (a + b) = -a + (a + c), by rw h, by rwa [← int.add_assoc, ← int.add_assoc, int.add_left_neg, int.zero_add, int.zero_add] at this protected lemma neg_add {a b : ℤ} : - (a + b) = -a + -b := calc - (a + b) = -(a + b) + (a + b) + -a + -b : begin rw [int.add_assoc, int.add_comm (-a), int.add_assoc, int.add_assoc, ← int.add_assoc b], rw [int.add_right_neg, int.zero_add, int.add_right_neg, int.add_zero], end ... = -a + -b : by { rw [int.add_left_neg, int.zero_add] } lemma neg_succ_of_nat_coe' (n : ℕ) : -[1+ n] = -↑n - 1 := by rw [int.sub_eq_add_neg, ← int.neg_add]; refl protected lemma coe_nat_sub {n m : ℕ} : n ≤ m → (↑(m - n) : ℤ) = ↑m - ↑n := of_nat_sub local attribute [simp] int.sub_eq_add_neg protected lemma sub_nat_nat_eq_coe {m n : ℕ} : sub_nat_nat m n = ↑m - ↑n := sub_nat_nat_elim m n (λm n i, i = ↑m - ↑n) (λi n, by { simp [int.coe_nat_add, int.add_left_comm, int.add_assoc, int.add_right_neg], refl }) (λi n, by { rw [int.coe_nat_add, int.coe_nat_add, int.coe_nat_one, int.neg_succ_of_nat_eq, int.sub_eq_add_neg, int.neg_add, int.neg_add, int.neg_add, ← int.add_assoc, ← int.add_assoc, int.add_right_neg, int.zero_add] }) def to_nat : ℤ → ℕ | (n : ℕ) := n | -[1+ n] := 0 theorem to_nat_sub (m n : ℕ) : to_nat (m - n) = m - n := by rw [← int.sub_nat_nat_eq_coe]; exact sub_nat_nat_elim m n (λm n i, to_nat i = m - n) (λi n, by rw [nat.add_sub_cancel_left]; refl) (λi n, by rw [nat.add_assoc, nat.sub_eq_zero_of_le (nat.le_add_right _ _)]; refl) -- Since mod x y is always nonnegative when y ≠ 0, we can make a nat version of it def nat_mod (m n : ℤ) : ℕ := (m % n).to_nat protected lemma one_mul : ∀ (a : ℤ), (1 : ℤ) * a = a | (of_nat n) := show of_nat (1 * n) = of_nat n, by rw nat.one_mul | -[1+ n] := show -[1+ (1 * n)] = -[1+ n], by rw nat.one_mul protected lemma mul_one (a : ℤ) : a * 1 = a := by rw [int.mul_comm, int.one_mul] protected lemma neg_eq_neg_one_mul : ∀ a : ℤ, -a = -1 * a | (of_nat 0) := rfl | (of_nat (n+1)) := show _ = -[1+ (1*n)+0], by { rw nat.one_mul, refl } | -[1+ n] := show _ = of_nat _, by { rw nat.one_mul, refl } theorem sign_mul_nat_abs : ∀ (a : ℤ), sign a * nat_abs a = a | (n+1:ℕ) := int.one_mul _ | 0 := rfl | -[1+ n] := (int.neg_eq_neg_one_mul _).symm end int
0cd93c572af2b385cf4570f1708e487d893e6401
1a61aba1b67cddccce19532a9596efe44be4285f
/tests/lean/fold.lean
4d88f2ea8246a8dc1da9404d8d91a654155d8b72
[ "Apache-2.0" ]
permissive
eigengrau/lean
07986a0f2548688c13ba36231f6cdbee82abf4c6
f8a773be1112015e2d232661ce616d23f12874d0
refs/heads/master
1,610,939,198,566
1,441,352,386,000
1,441,352,494,000
41,903,576
0
0
null
1,441,352,210,000
1,441,352,210,000
null
UTF-8
Lean
false
false
985
lean
prelude definition Prop := Type.{0} inductive true : Prop := intro : true inductive false : Prop constant num : Type inductive prod (A B : Type) := mk : A → B → prod A B infixl `×`:30 := prod variables a b c : num section local notation `(` t:(foldr `,` (e r, prod.mk e r)) `)` := t check (a, false, b, true, c) set_option pp.notation false check (a, false, b, true, c) end section local notation `(` t:(foldr `,` (e r, prod.mk r e)) `)` := t set_option pp.notation true check (a, false, b, true, c) set_option pp.notation false check (a, false, b, true, c) end section local notation `(` t:(foldl `,` (e r, prod.mk r e)) `)` := t set_option pp.notation true check (a, false, b, true, c) set_option pp.notation false check (a, false, b, true, c) end section local notation `(` t:(foldl `,` (e r, prod.mk e r)) `)` := t set_option pp.notation true check (a, false, b, true, c) set_option pp.notation false check (a, false, b, true, c) end
b27d81f52f56f92327d9bc743a0ee81b030e1bd7
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/or_shortcircuit.lean
9074bf76f326da01be773760089c7eba4a949539
[ "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
430
lean
@[noinline] def c1 (x : Nat) : Bool := dbg_trace "executed c1" x == 0 @[noinline] def c2 (x : Nat) : Bool := dbg_trace "executed c2" x == 0 @[noinline] def c3 (x : Nat) : Bool := dbg_trace "executed c3" x > 0 @[noinline] def f (x : Nat) := x + 1 def tst (x : Nat) : Nat := Id.run <| do let x := if !c1 x || (!c2 x && c3 x) then f x else f (x+2) match x with | 0 => f (x+1) | y+1 => f (y+3) #eval tst 10
b609c787a7ac6b80984cd372bd6881123de616e2
947b78d97130d56365ae2ec264df196ce769371a
/stage0/src/Lean/Elab/Alias.lean
3a682f5741f8375c1a8e1ea008aa0cac04a5eeee
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,534
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.Environment namespace Lean /-! We use aliases to implement the `export <id> (<id>+)` command. An `export A (x)` in the namespace `B` produces an alias `B.x ~> A.x`. -/ abbrev AliasState := SMap Name (List Name) abbrev AliasEntry := Name × Name def addAliasEntry (s : AliasState) (e : AliasEntry) : AliasState := match s.find? e.1 with | none => s.insert e.1 [e.2] | some es => if es.elem e.2 then s else s.insert e.1 (e.2 :: es) def mkAliasExtension : IO (SimplePersistentEnvExtension AliasEntry AliasState) := registerSimplePersistentEnvExtension { name := `aliasesExt, addEntryFn := addAliasEntry, addImportedFn := fun es => (mkStateFromImportedEntries addAliasEntry {} es).switch } @[init mkAliasExtension] constant aliasExtension : SimplePersistentEnvExtension AliasEntry AliasState := arbitrary _ /- Add alias `a` for `e` -/ @[export lean_add_alias] def addAlias (env : Environment) (a : Name) (e : Name) : Environment := aliasExtension.addEntry env (a, e) def getAliases (env : Environment) (a : Name) : List Name := match (aliasExtension.getState env).find? a with | none => [] | some es => es -- slower, but only used in the pretty printer def getRevAliases (env : Environment) (e : Name) : List Name := (aliasExtension.getState env).fold (fun as a es => if List.contains es e then a :: as else as) [] end Lean
ff8bba644c20d6fa83e7f81a94cb53cd7f95614a
4727251e0cd73359b15b664c3170e5d754078599
/src/geometry/manifold/derivation_bundle.lean
fadbf3a1350f78acce060c94abf001db7cf7289f
[ "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
6,338
lean
/- Copyright © 2020 Nicolò Cavalleri. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nicolò Cavalleri -/ import geometry.manifold.algebra.smooth_functions import ring_theory.derivation /-! # Derivation bundle In this file we define the derivations at a point of a manifold on the algebra of smooth fuctions. Moreover, we define the differential of a function in terms of derivations. The content of this file is not meant to be regarded as an alternative definition to the current tangent bundle but rather as a purely algebraic theory that provides a purely algebraic definition of the Lie algebra for a Lie group. -/ variables (𝕜 : Type*) [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) (M : Type*) [topological_space M] [charted_space H M] (n : with_top ℕ) open_locale manifold -- the following two instances prevent poorly understood type class inference timeout problems instance smooth_functions_algebra : algebra 𝕜 C^∞⟮I, M; 𝕜⟯ := by apply_instance instance smooth_functions_tower : is_scalar_tower 𝕜 C^∞⟮I, M; 𝕜⟯ C^∞⟮I, M; 𝕜⟯ := by apply_instance /-- Type synonym, introduced to put a different `has_scalar` action on `C^n⟮I, M; 𝕜⟯` which is defined as `f • r = f(x) * r`. -/ @[nolint unused_arguments] def pointed_smooth_map (x : M) := C^n⟮I, M; 𝕜⟯ localized "notation `C^` n `⟮` I `,` M `;` 𝕜 `⟯⟨` x `⟩` := pointed_smooth_map 𝕜 I M n x" in derivation variables {𝕜 M} namespace pointed_smooth_map instance {x : M} : has_coe_to_fun C^∞⟮I, M; 𝕜⟯⟨x⟩ (λ _, M → 𝕜) := cont_mdiff_map.has_coe_to_fun instance {x : M} : comm_ring C^∞⟮I, M; 𝕜⟯⟨x⟩ := smooth_map.comm_ring instance {x : M} : algebra 𝕜 C^∞⟮I, M; 𝕜⟯⟨x⟩ := smooth_map.algebra instance {x : M} : inhabited C^∞⟮I, M; 𝕜⟯⟨x⟩ := ⟨0⟩ instance {x : M} : algebra C^∞⟮I, M; 𝕜⟯⟨x⟩ C^∞⟮I, M; 𝕜⟯ := algebra.id C^∞⟮I, M; 𝕜⟯ instance {x : M} : is_scalar_tower 𝕜 C^∞⟮I, M; 𝕜⟯⟨x⟩ C^∞⟮I, M; 𝕜⟯ := is_scalar_tower.right variable {I} /-- `smooth_map.eval_ring_hom` gives rise to an algebra structure of `C^∞⟮I, M; 𝕜⟯` on `𝕜`. -/ instance eval_algebra {x : M} : algebra C^∞⟮I, M; 𝕜⟯⟨x⟩ 𝕜 := (smooth_map.eval_ring_hom x : C^∞⟮I, M; 𝕜⟯⟨x⟩ →+* 𝕜).to_algebra /-- With the `eval_algebra` algebra structure evaluation is actually an algebra morphism. -/ def eval (x : M) : C^∞⟮I, M; 𝕜⟯ →ₐ[C^∞⟮I, M; 𝕜⟯⟨x⟩] 𝕜 := algebra.of_id C^∞⟮I, M; 𝕜⟯⟨x⟩ 𝕜 lemma smul_def (x : M) (f : C^∞⟮I, M; 𝕜⟯⟨x⟩) (k : 𝕜) : f • k = f x * k := rfl instance (x : M) : is_scalar_tower 𝕜 C^∞⟮I, M; 𝕜⟯⟨x⟩ 𝕜 := { smul_assoc := λ k f h, by { simp only [smul_def, algebra.id.smul_eq_mul, smooth_map.coe_smul, pi.smul_apply, mul_assoc]} } end pointed_smooth_map open_locale derivation /-- The derivations at a point of a manifold. Some regard this as a possible definition of the tangent space -/ @[reducible] def point_derivation (x : M) := derivation 𝕜 (C^∞⟮I, M; 𝕜⟯⟨x⟩) 𝕜 section variables (I) {M} (X Y : derivation 𝕜 C^∞⟮I, M; 𝕜⟯ C^∞⟮I, M; 𝕜⟯) (f g : C^∞⟮I, M; 𝕜⟯) (r : 𝕜) /-- Evaluation at a point gives rise to a `C^∞⟮I, M; 𝕜⟯`-linear map between `C^∞⟮I, M; 𝕜⟯` and `𝕜`. -/ def smooth_function.eval_at (x : M) : C^∞⟮I, M; 𝕜⟯ →ₗ[C^∞⟮I, M; 𝕜⟯⟨x⟩] 𝕜 := (pointed_smooth_map.eval x).to_linear_map namespace derivation variable {I} /-- The evaluation at a point as a linear map. -/ def eval_at (x : M) : (derivation 𝕜 C^∞⟮I, M; 𝕜⟯ C^∞⟮I, M; 𝕜⟯) →ₗ[𝕜] point_derivation I x := (smooth_function.eval_at I x).comp_der lemma eval_at_apply (x : M) : eval_at x X f = (X f) x := rfl end derivation variables {I} {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'} {M' : Type*} [topological_space M'] [charted_space H' M'] /-- The heterogeneous differential as a linear map. Instead of taking a function as an argument this differential takes `h : f x = y`. It is particularly handy to deal with situations where the points on where it has to be evaluated are equal but not definitionally equal. -/ def hfdifferential {f : C^∞⟮I, M; I', M'⟯} {x : M} {y : M'} (h : f x = y) : point_derivation I x →ₗ[𝕜] point_derivation I' y := { to_fun := λ v, derivation.mk' { to_fun := λ g, v (g.comp f), map_add' := λ g g', by rw [smooth_map.add_comp, derivation.map_add], map_smul' := λ k g, by simp only [smooth_map.smul_comp, derivation.map_smul, ring_hom.id_apply], } (λ g g', by simp only [derivation.leibniz, smooth_map.mul_comp, linear_map.coe_mk, pointed_smooth_map.smul_def, cont_mdiff_map.comp_apply, h]), map_smul' := λ k v, rfl, map_add' := λ v w, rfl } /-- The homogeneous differential as a linear map. -/ def fdifferential (f : C^∞⟮I, M; I', M'⟯) (x : M) : point_derivation I x →ₗ[𝕜] point_derivation I' (f x) := hfdifferential (rfl : f x = f x) /- Standard notation for the differential. The abbreviation is `MId`. -/ localized "notation `𝒅` := fdifferential" in manifold /- Standard notation for the differential. The abbreviation is `MId`. -/ localized "notation `𝒅ₕ` := hfdifferential" in manifold @[simp] lemma apply_fdifferential (f : C^∞⟮I, M; I', M'⟯) {x : M} (v : point_derivation I x) (g : C^∞⟮I', M'; 𝕜⟯) : 𝒅f x v g = v (g.comp f) := rfl @[simp] lemma apply_hfdifferential {f : C^∞⟮I, M; I', M'⟯} {x : M} {y : M'} (h : f x = y) (v : point_derivation I x) (g : C^∞⟮I', M'; 𝕜⟯) : 𝒅ₕh v g = 𝒅f x v g := rfl variables {E'' : Type*} [normed_group E''] [normed_space 𝕜 E''] {H'' : Type*} [topological_space H''] {I'' : model_with_corners 𝕜 E'' H''} {M'' : Type*} [topological_space M''] [charted_space H'' M''] @[simp] lemma fdifferential_comp (g : C^∞⟮I', M'; I'', M''⟯) (f : C^∞⟮I, M; I', M'⟯) (x : M) : 𝒅(g.comp f) x = (𝒅g (f x)).comp (𝒅f x) := rfl end
c12a98e95f408ccc2030a5c5b9fdb90e796aad1c
4fa118f6209450d4e8d058790e2967337811b2b5
/src/for_mathlib/uniform_space/group_basis.lean
9867f02ddd2124febbb200fd1c54a77b6dac5582
[ "Apache-2.0" ]
permissive
leanprover-community/lean-perfectoid-spaces
16ab697a220ed3669bf76311daa8c466382207f7
95a6520ce578b30a80b4c36e36ab2d559a842690
refs/heads/master
1,639,557,829,139
1,638,797,866,000
1,638,797,866,000
135,769,296
96
10
Apache-2.0
1,638,797,866,000
1,527,892,754,000
Lean
UTF-8
Lean
false
false
2,000
lean
import topology.uniform_space.cauchy import topology.algebra.uniform_group import for_mathlib.topological_groups open filter set local infixr ` ×ᶠ `:51 := filter.prod local notation `𝓤` := uniformity local notation `𝓝` x:70 := nhds x section open tactic meta def clean_step : tactic unit := do tgt ← target, match tgt with | `(%%a → %%b) := `[intros] | `(%%a ↔ %%b) := match a with | `(%%c → %%d) := if c.has_var then `[apply imp_congr] else `[apply forall_congr] | `(Exists %%c) := `[apply exists_congr] | _ := `[exact iff.rfl] end | _ := fail "Goal is not a forall, implies or iff" end meta def tactic.interactive.clean_iff : tactic unit := do repeat clean_step end variables (α : Type*) [uniform_space α] [add_group α] [uniform_add_group α] lemma add_group_filter_basis.cauchy_iff {B : add_group_filter_basis α} (h : uniform_space.to_topological_space α = B.topology) {F : filter α} : cauchy F ↔ F ≠ ⊥ ∧ ∀ U ∈ B, ∃ M ∈ F, ∀ x y ∈ M, y - x ∈ U := begin suffices : F ×ᶠ F ≤ 𝓤 α ↔ ∀ U ∈ B, ∃ M ∈ F, ∀ x y ∈ M, y - x ∈ U, by split ; rintros ⟨h', h⟩ ; refine ⟨h', _⟩ ; [rwa ← this, rwa this], rw [uniformity_eq_comap_nhds_zero α, ← map_le_iff_le_comap], change tendsto _ _ _ ↔ _, rw [B.nhds_zero_eq h, filter_basis.tendsto_into], simp only [mem_prod_same_iff], clean_iff, rw [subset_def, prod.forall], clean_iff, rw [prod_mk_mem_set_prod_eq], tauto! end lemma test {α : Type*} [has_sub α] (S T : set $ set α) : (∀ {V : set α}, V ∈ S → (∃ (t : set α) (H : t ∈ T), set.prod t t ⊆ (λ (x : α × α), x.snd - x.fst) ⁻¹' V)) ↔ ∀ (U : set α), U ∈ S → (∃ (M : set α) (H : M ∈ T), ∀ (x y : α), x ∈ M → y ∈ M → y - x ∈ U) := begin clean_iff, rw [subset_def, prod.forall], clean_iff, rw [set.prod_mk_mem_set_prod_eq], tauto! end
9f3fa178cd307df8921de377666606402496253f
618003631150032a5676f229d13a079ac875ff77
/test/protec_proj.lean
5aa0b17890877276505be1bca1e4bdcf41faaad8
[ "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
501
lean
/- Copyright (c) 2020 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import tactic.protected open tactic @[protect_proj without baz bar] structure foo : Type := (bar : unit) (baz : unit) (qux : unit) open foo #check bar #check foo.qux run_cmd success_if_fail $ resolve_name `qux @[protect_proj] structure X : Type := (n : nat) (i : fin n) open X #check X.i #check X.n run_cmd success_if_fail $ resolve_name `n
5fb4c52285fc35cfa67625ca38f27372c30a33d9
63abd62053d479eae5abf4951554e1064a4c45b4
/src/category_theory/monad/limits.lean
fd9670ea2d02b830102b3c4b85717f8cd0ab5626
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
9,990
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Bhavik Mehta -/ import category_theory.monad.adjunction import category_theory.adjunction.limits namespace category_theory open category open category_theory.limits universes v₁ v₂ u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation namespace monad variables {C : Type u₁} [category.{v₁} C] variables {T : C ⥤ C} [monad T] variables {J : Type v₁} [small_category J] namespace forget_creates_limits variables (D : J ⥤ algebra T) (c : cone (D ⋙ forget T)) (t : is_limit c) /-- (Impl) The natural transformation used to define the new cone -/ @[simps] def γ : (D ⋙ forget T ⋙ T) ⟶ (D ⋙ forget T) := { app := λ j, (D.obj j).a } /-- (Impl) This new cone is used to construct the algebra structure -/ @[simps] def new_cone : cone (D ⋙ forget T) := { X := T.obj c.X, π := (functor.const_comp _ _ T).inv ≫ whisker_right c.π T ≫ (γ D) } /-- The algebra structure which will be the apex of the new limit cone for `D`. -/ @[simps] def cone_point : algebra T := { A := c.X, a := t.lift (new_cone D c), unit' := begin apply t.hom_ext, intro j, erw [category.assoc, t.fac (new_cone D c), id_comp], dsimp, erw [id_comp, ← category.assoc, ← (η_ T).naturality, functor.id_map, category.assoc, (D.obj j).unit, comp_id], end, assoc' := begin apply t.hom_ext, intro j, rw [category.assoc, category.assoc, t.fac (new_cone D c)], dsimp, erw id_comp, slice_lhs 1 2 {rw ← (μ_ T).naturality}, slice_lhs 2 3 {rw (D.obj j).assoc}, slice_rhs 1 2 {rw ← T.map_comp}, rw t.fac (new_cone D c), dsimp, erw [id_comp, T.map_comp, category.assoc] end } /-- (Impl) Construct the lifted cone in `algebra T` which will be limiting. -/ @[simps] def lifted_cone : cone D := { X := cone_point D c t, π := { app := λ j, { f := c.π.app j }, naturality' := λ X Y f, by { ext1, dsimp, erw c.w f, simp } } } /-- (Impl) Prove that the lifted cone is limiting. -/ @[simps] def lifted_cone_is_limit : is_limit (lifted_cone D c t) := { lift := λ s, { f := t.lift ((forget T).map_cone s), h' := begin apply t.hom_ext, intro j, have := t.fac ((forget T).map_cone s), slice_rhs 2 3 {rw t.fac ((forget T).map_cone s) j}, dsimp, slice_lhs 2 3 {rw t.fac (new_cone D c) j}, dsimp, rw category.id_comp, slice_lhs 1 2 {rw ← T.map_comp}, rw t.fac ((forget T).map_cone s) j, exact (s.π.app j).h end }, uniq' := λ s m J, begin ext1, apply t.hom_ext, intro j, simpa [t.fac (functor.map_cone (forget T) s) j] using congr_arg algebra.hom.f (J j), end } end forget_creates_limits -- Theorem 5.6.5 from [Riehl][riehl2017] /-- The forgetful functor from the Eilenberg-Moore category creates limits. -/ instance forget_creates_limits : creates_limits (forget T) := { creates_limits_of_shape := λ J 𝒥, by exactI { creates_limit := λ D, creates_limit_of_reflects_iso (λ c t, { lifted_cone := forget_creates_limits.lifted_cone D c t, valid_lift := cones.ext (iso.refl _) (λ j, (id_comp _).symm), makes_limit := forget_creates_limits.lifted_cone_is_limit _ _ _ } ) } } /-- `D ⋙ forget T` has a limit, then `D` has a limit. -/ lemma has_limit_of_comp_forget_has_limit (D : J ⥤ algebra T) [has_limit (D ⋙ forget T)] : has_limit D := has_limit_of_created D (forget T) namespace forget_creates_colimits -- Let's hide the implementation details in a namespace variables {D : J ⥤ algebra T} (c : cocone (D ⋙ forget T)) (t : is_colimit c) -- We have a diagram D of shape J in the category of algebras, and we assume that we are given a -- colimit for its image D ⋙ forget T under the forgetful functor, say its apex is L. -- We'll construct a colimiting coalgebra for D, whose carrier will also be L. -- To do this, we must find a map TL ⟶ L. Since T preserves colimits, TL is also a colimit. -- In particular, it is a colimit for the diagram `(D ⋙ forget T) ⋙ T` -- so to construct a map TL ⟶ L it suffices to show that L is the apex of a cocone for this diagram. -- In other words, we need a natural transformation from const L to `(D ⋙ forget T) ⋙ T`. -- But we already know that L is the apex of a cocone for the diagram `D ⋙ forget T`, so it -- suffices to give a natural transformation `((D ⋙ forget T) ⋙ T) ⟶ (D ⋙ forget T)`: /-- (Impl) The natural transformation given by the algebra structure maps, used to construct a cocone `c` with apex `colimit (D ⋙ forget T)`. -/ @[simps] def γ : ((D ⋙ forget T) ⋙ T) ⟶ (D ⋙ forget T) := { app := λ j, (D.obj j).a } /-- (Impl) A cocone for the diagram `(D ⋙ forget T) ⋙ T` found by composing the natural transformation `γ` with the colimiting cocone for `D ⋙ forget T`. -/ @[simps] def new_cocone : cocone ((D ⋙ forget T) ⋙ T) := { X := c.X, ι := γ ≫ c.ι } variable [preserves_colimits_of_shape J T] /-- (Impl) Define the map `λ : TL ⟶ L`, which will serve as the structure of the coalgebra on `L`, and we will show is the colimiting object. We use the cocone constructed by `c` and the fact that `T` preserves colimits to produce this morphism. -/ @[reducible] def lambda : (functor.map_cocone T c).X ⟶ c.X := (preserves_colimit.preserves t).desc (new_cocone c) /-- (Impl) The key property defining the map `λ : TL ⟶ L`. -/ lemma commuting (j : J) : T.map (c.ι.app j) ≫ lambda c t = (D.obj j).a ≫ c.ι.app j := is_colimit.fac (preserves_colimit.preserves t) (new_cocone c) j /-- (Impl) Construct the colimiting algebra from the map `λ : TL ⟶ L` given by `lambda`. We are required to show it satisfies the two algebra laws, which follow from the algebra laws for the image of `D` and our `commuting` lemma. -/ @[simps] def cocone_point : algebra T := { A := c.X, a := lambda c t, unit' := begin apply t.hom_ext, intro j, erw [comp_id, ← category.assoc, (η_ T).naturality, category.assoc, commuting, ← category.assoc], erw algebra.unit, apply id_comp end, assoc' := begin apply is_colimit.hom_ext (preserves_colimit.preserves (preserves_colimit.preserves t)), intro j, erw [← category.assoc, nat_trans.naturality (μ_ T), ← functor.map_cocone_ι, category.assoc, is_colimit.fac _ (new_cocone c) j], rw ← category.assoc, erw [← functor.map_comp, commuting], dsimp, erw [← category.assoc, algebra.assoc, category.assoc, functor.map_comp, category.assoc, commuting], apply_instance, apply_instance end } /-- (Impl) Construct the lifted cocone in `algebra T` which will be colimiting. -/ @[simps] def lifted_cocone : cocone D := { X := cocone_point c t, ι := { app := λ j, { f := c.ι.app j, h' := commuting _ _ _ }, naturality' := λ A B f, by { ext1, dsimp, erw [comp_id, c.w] } } } /-- (Impl) Prove that the lifted cocone is colimiting. -/ @[simps] def lifted_cocone_is_colimit : is_colimit (lifted_cocone c t) := { desc := λ s, { f := t.desc ((forget T).map_cocone s), h' := begin dsimp, apply is_colimit.hom_ext (preserves_colimit.preserves t), intro j, rw ← category.assoc, erw ← functor.map_comp, erw t.fac', rw ← category.assoc, erw forget_creates_colimits.commuting, rw category.assoc, rw t.fac', apply algebra.hom.h, apply_instance end }, uniq' := λ s m J, by { ext1, apply t.hom_ext, intro j, simpa using congr_arg algebra.hom.f (J j) } } end forget_creates_colimits open forget_creates_colimits -- TODO: the converse of this is true as well -- TODO: generalise to monadic functors, as for creating limits /-- The forgetful functor from the Eilenberg-Moore category for a monad creates any colimit which the monad itself preserves. -/ instance forget_creates_colimits [preserves_colimits_of_shape J T] : creates_colimits_of_shape J (forget T) := { creates_colimit := λ D, creates_colimit_of_reflects_iso $ λ c t, { lifted_cocone := { X := cocone_point c t, ι := { app := λ j, { f := c.ι.app j, h' := commuting _ _ _ }, naturality' := λ A B f, by { ext1, dsimp, erw [comp_id, c.w] } } }, valid_lift := cocones.ext (iso.refl _) (by tidy), makes_colimit := lifted_cocone_is_colimit _ _ } } /-- For `D : J ⥤ algebra T`, `D ⋙ forget T` has a colimit, then `D` has a colimit provided colimits of shape `J` are preserved by `T`. -/ lemma forget_creates_colimits_of_monad_preserves [preserves_colimits_of_shape J T] (D : J ⥤ algebra T) [has_colimit (D ⋙ forget T)] : has_colimit D := has_colimit_of_created D (forget T) end monad variables {C : Type u₁} [category.{v₁} C] {D : Type u₁} [category.{v₁} D] variables {J : Type v₁} [small_category J] instance comp_comparison_forget_has_limit (F : J ⥤ D) (R : D ⥤ C) [monadic_right_adjoint R] [has_limit (F ⋙ R)] : has_limit ((F ⋙ monad.comparison R) ⋙ monad.forget ((left_adjoint R) ⋙ R)) := (@has_limit_of_iso _ _ _ _ (F ⋙ R) _ _ (iso_whisker_left F (monad.comparison_forget R).symm)) instance comp_comparison_has_limit (F : J ⥤ D) (R : D ⥤ C) [monadic_right_adjoint R] [has_limit (F ⋙ R)] : has_limit (F ⋙ monad.comparison R) := monad.has_limit_of_comp_forget_has_limit (F ⋙ monad.comparison R) /-- Any monadic functor creates limits. -/ lemma monadic_creates_limits (F : J ⥤ D) (R : D ⥤ C) [monadic_right_adjoint R] [has_limit (F ⋙ R)] : has_limit F := adjunction.has_limit_of_comp_equivalence _ (monad.comparison R) section /-- If C has limits then any reflective subcategory has limits -/ lemma has_limits_of_reflective (R : D ⥤ C) [has_limits C] [reflective R] : has_limits D := { has_limits_of_shape := λ J 𝒥, by exactI { has_limit := λ F, monadic_creates_limits F R } } end end category_theory
d2065e7b071e6f4f9c712ba3a75dff5fd505a1e2
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/matchErrorLocation.lean
f188cbff11dbf22c5055e5571a061f4abce5aaf8
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
119
lean
new_frontend def head {α} (xs : List α) (h : xs = [] → False) : α := match he:xs with | [] => h he | x::_ => x
4f5bd499bdd1288d81ab6c3bff618f82ee9a8cf8
785b41b0993f39cbfa9b02fe0940ce3f2f51a57d
/conf/all6.lean
64f6081f1f8a10b3bc498768151f8c0dbf6b60b3
[ "MIT" ]
permissive
loso3000/OpenWrt-DIY-1
75b0d70314d703203508218a29acefc3b914d32d
5858be81ee44199908cbaa1a752b17505c9834e8
refs/heads/main
1,690,532,461,283
1,631,008,241,000
1,631,008,241,000
354,817,508
1
0
MIT
1,617,623,493,000
1,617,623,492,000
null
UTF-8
Lean
false
false
17,129
lean
CONFIG_TARGET_x86=y CONFIG_TARGET_x86_64=y CONFIG_TARGET_x86_64_DEVICE_generic=y # 设置固件大小 CONFIG_TARGET_KERNEL_PARTSIZE=64 CONFIG_TARGET_ROOTFS_PARTSIZE=1516 # EFI支持: CONFIG_GRUB_IMAGES=y CONFIG_EFI_IMAGES=y # CONFIG_VMDK_IMAGES is not set # 不压缩efi CONFIG_TARGET_ROOTFS_TARGZ=n # CONFIG_TARGET_IMAGES_GZIP is not set # Wireless # CONFIG_PACKAGE_wpad-basic-wolfssl=n #和easymesh冲突 #ipv6 CONFIG_PACKAGE_ipv6helper=y CONFIG_PACKAGE_dnsmasq_full_dhcpv6=y #添加SD卡支持 CONFIG_PACKAGE_kmod-mmc=y CONFIG_PACKAGE_kmod-sdhci=y #添加USB扩展支持 CONFIG_PACKAGE_block-mount=y CONFIG_PACKAGE_librt=y # x86 CONFIG_PACKAGE_kmod-usb-hid=y CONFIG_PACKAGE_qemu-ga=y CONFIG_PACKAGE_lm-sensors-detect=y CONFIG_PACKAGE_kmod-bonding=y CONFIG_PACKAGE_kmod-mmc-spi=y CONFIG_PACKAGE_ppp-mod-pptp=y #VPN客户端 CONFIG_PACKAGE_kmod-vmxnet3=y CONFIG_PACKAGE_kmod-igbvf=y CONFIG_PACKAGE_kmod-ixgbe=y CONFIG_PACKAGE_kmod-pcnet32=y CONFIG_PACKAGE_kmod-r8125=y CONFIG_PACKAGE_kmod-r8168=y CONFIG_PACKAGE_kmod-8139cp=y CONFIG_PACKAGE_kmod-8139too=y CONFIG_PACKAGE_kmod-rtl8xxxu=y CONFIG_PACKAGE_kmod-i40e=y CONFIG_PACKAGE_kmod-i40evf=y CONFIG_PACKAGE_kmod-ath5k=y CONFIG_PACKAGE_kmod-ath9k=y CONFIG_PACKAGE_kmod-ath9k-htc=y CONFIG_PACKAGE_kmod-ath10k=y CONFIG_PACKAGE_kmod-rt2800-usb=y CONFIG_PACKAGE_kmod-mlx4-core=y CONFIG_PACKAGE_kmod-mlx5-core=y CONFIG_PACKAGE_kmod-alx=y CONFIG_PACKAGE_kmod-tulip=y CONFIG_PACKAGE_kmod-tg3=y CONFIG_PACKAGE_ntfs-3g=y # CONFIG_PACKAGE_kmod-fs-antfs is not set # CONFIG_PACKAGE_kmod-fs-ntfs is not set CONFIG_PACKAGE_ath10k-firmware-qca9888=y CONFIG_PACKAGE_ath10k-firmware-qca988x=y CONFIG_PACKAGE_ath10k-firmware-qca9984=y CONFIG_PACKAGE_brcmfmac-firmware-43602a1-pcie=y CONFIG_PACKAGE_kmod-ac97=y CONFIG_PACKAGE_kmod-sound-via82xx=y CONFIG_PACKAGE_alsa-utils=y CONFIG_PACKAGE_kmod-iwlwifi=y #工具 CONFIG_PACKAGE_acpid=y CONFIG_PACKAGE_blkid=y CONFIG_PACKAGE_smartmontools=y CONFIG_PACKAGE_open-vm-tools=n #虚拟机支持管理性能更好 CONFIG_PACKAGE_ethtool=y #网卡工具 CONFIG_PACKAGE_iperf3=y #局域网测速 CONFIG_PACKAGE_snmpd=n #旁路由穿透显示真机器MAC #CONFIG_PACKAGE_parted=n #128个区分区工具z CONFIG_PACKAGE_fdisk=y #分区工具 CONFIG_PACKAGE_hdparm=y #移动硬盘设置 CONFIG_PACKAGE_curl=y # USB3.0支持: CONFIG_PACKAGE_kmod-usb2=y CONFIG_PACKAGE_kmod-usb2-pci=y CONFIG_PACKAGE_kmod-usb3=y CONFIG_PACKAGE_kmod-usb-audio=y CONFIG_PACKAGE_kmod-usb-printer=y #nfs CONFIG_PACKAGE_kmod-fs-nfsd=y CONFIG_PACKAGE_kmod-fs-nfs=y CONFIG_PACKAGE_kmod-fs-nfs-v4=y #Sound Support CONFIG_PACKAGE_kmod-sound-core=y CONFIG_PACKAGE_kmod-sound-hda-core=y CONFIG_PACKAGE_kmod-sound-hda-codec-realtek=y CONFIG_PACKAGE_kmod-sound-hda-codec-via=y CONFIG_PACKAGE_kmod-sound-hda-intel=y # CONFIG_PACKAGE_kmod-sound-hda-codec-hdmi=n #USB net driver CONFIG_PACKAGE_kmod-rtlwifi=y CONFIG_PACKAGE_kmod-rtlwifi-btcoexist=y CONFIG_PACKAGE_kmod-rtlwifi-usb=y CONFIG_PACKAGE_kmod-rtl8812au-ac=y CONFIG_PACKAGE_usb-modeswitch=y CONFIG_PACKAGE_kmod-rtl8192cu=y CONFIG_PACKAGE_kmod-rtl8821cu=y CONFIG_PACKAGE_kmod-mt76=y CONFIG_PACKAGE_kmod-mt76x2u=y CONFIG_PACKAGE_kmod-usb-net-asix=y CONFIG_PACKAGE_kmod-usb-net-asix-ax88179=y CONFIG_PACKAGE_kmod-usb-net-rtl8152-vendor=y CONFIG_PACKAGE_kmod-usb-net-rndis=y CONFIG_PACKAGE_kmod-usb-net-cdc-ether=y CONFIG_PACKAGE_kmod-usb-net-ipheth=y # L2TP CONFIG_PACKAGE_kmod-pppol2tp=y # pptp CONFIG_PACKAGE_kmod-pptp=n CONFIG_PACKAGE_kmod-gre=n CONFIG_PACKAGE_kmod-nf-nathelper-extra=n # ipsec-vpnd CONFIG_PACKAGE_kmod-crypto-authenc=y CONFIG_PACKAGE_kmod-ipsec=y CONFIG_PACKAGE_kmod-ipsec4=y CONFIG_PACKAGE_kmod-ipsec6=y CONFIG_PACKAGE_kmod-ipt-ipsec=y # cifsmount # CONFIG_PACKAGE_kmod-fs-cifs=y CONFIG_PACKAGE_kmod-nls-utf8=y CONFIG_PACKAGE_kmod-crypto-misc=y # eqos CONFIG_PACKAGE_kmod-ifb=y # map CONFIG_PACKAGE_kmod-ip6-tunnel=y CONFIG_PACKAGE_kmod-nat46=y # ebtables CONFIG_PACKAGE_kmod-ebtables=y CONFIG_PACKAGE_kmod-ebtables-ipv4=y CONFIG_PACKAGE_kmod-ebtables-ipv6=y # add upnp # CONFIG_PACKAGE_irqbalance=n # CONFIG_PACKAGE_miniupnpd=y CONFIG_PACKAGE_miniupnpd-igdv1=y # CONFIG_PACKAGE_luci-app-upnp=y # CONFIG_PACKAGE_luci-app-boostupnp=n # CONFIG_PACKAGE_luci-app-wol is not set CONFIG_PACKAGE_luci-app-wolplus=y #base插件 CONFIG_PACKAGE_ddns-scripts_cloudflare.com-v4=y CONFIG_PACKAGE_ddns-scripts=y CONFIG_PACKAGE_ddns-scripts_freedns_42_pl=y CONFIG_PACKAGE_ddns-scripts_godaddy.com-v1=y CONFIG_PACKAGE_ddns-scripts_no-ip_com=y CONFIG_PACKAGE_ddns-scripts_nsupdate=y CONFIG_PACKAGE_ddns-scripts_route53-v1=y # CONFIG_PACKAGE_autosamba is not set CONFIG_PACKAGE_autosamba-ksmbd=n CONFIG_PACKAGE_autosamba-samba4=y # CONFIG_PACKAGE_luci-app-accesscontrol is not set # CONFIG_PACKAGE_luci-app-adbyby-plus is not set CONFIG_PACKAGE_luci-app-adguardhome=y CONFIG_PACKAGE_luci-app-advanced=y CONFIG_PACKAGE_luci-app-autotimeset=n CONFIG_PACKAGE_luci-app-rebootschedule=y # CONFIG_PACKAGE_luci-app-autoreboot is not set CONFIG_PACKAGE_luci-app-control-timewol=y CONFIG_PACKAGE_luci-app-control-weburl=y CONFIG_PACKAGE_luci-app-control-webrestriction=n CONFIG_PACKAGE_luci-app-control-speedlimit=y CONFIG_PACKAGE_luci-app-webadmin=y CONFIG_PACKAGE_luci-app-cpulimit=y CONFIG_PACKAGE_luci-app-diskman=y CONFIG_PACKAGE_luci-app-diskman_INCLUDE_mdadm=y CONFIG_PACKAGE_luci-app-eqos=n # CONFIG_PACKAGE_luci-app-filetransfer is not set CONFIG_PACKAGE_luci-app-hd-idle=y CONFIG_PACKAGE_luci-app-jd-dailybonus=y CONFIG_PACKAGE_luci-app-koolproxyR=y CONFIG_PACKAGE_luci-app-netdata=y CONFIG_PACKAGE_luci-app-onliner=n CONFIG_PACKAGE_luci-app-openclash=y # CONFIG_PACKAGE_luci-app-samba is not set CONFIG_PACKAGE_luci-app-samba4=y CONFIG_PACKAGE_luci-app-serverchan=y # CONFIG_PACKAGE_luci-app-sfe is not set # CONFIG_PACKAGE_luci-app-flowoffload is not set CONFIG_PACKAGE_luci-app-smartdns=y CONFIG_PACKAGE_luci-app-passwall=y # CONFIG_PACKAGE_luci-app-ssr-plus is not set CONFIG_PACKAGE_luci-app-ssrpro=y CONFIG_PACKAGE_luci-app-ssrpro_INCLUDE_Kcptun=y CONFIG_PACKAGE_luci-app-ssrpro_INCLUDE_NaiveProxy=y CONFIG_PACKAGE_luci-app-ssrpro_INCLUDE_Redsocks2=y CONFIG_PACKAGE_luci-app-ssrpro_INCLUDE_Shadowsocks_Libev_Client=y CONFIG_PACKAGE_luci-app-ssrpro_INCLUDE_Shadowsocks_Libev_Server=y CONFIG_PACKAGE_luci-app-ssrpro_INCLUDE_ShadowsocksR_Libev_Client=y CONFIG_PACKAGE_luci-app-ssrpro_INCLUDE_ShadowsocksR_Libev_Server=y CONFIG_PACKAGE_luci-app-ssrpro_INCLUDE_Simple_Obfs=y CONFIG_PACKAGE_luci-app-ssrpro_INCLUDE_Trojan=y CONFIG_PACKAGE_luci-app-ssrpro_INCLUDE_V2ray_Plugin=y CONFIG_PACKAGE_luci-app-ssrpro_INCLUDE_Xray=y CONFIG_PACKAGE_luci-app-timecontrol=y CONFIG_PACKAGE_luci-app-ttyd=y CONFIG_PACKAGE_luci-app-turboacc=y # CONFIG_PACKAGE_luci-app-turboacc_INCLUDE_flow-offload=n # CONFIG_PACKAGE_luci-app-turboacc_INCLUDE_shortcut-fe=y CONFIG_PACKAGE_luci-app-vssr=y CONFIG_PACKAGE_luci-app-wrtbwmon=y CONFIG_PACKAGE_luci-app-nlbwmon=y CONFIG_PACKAGE_luci-app-netspeedtest=y CONFIG_PACKAGE_luci-app-dnsto=y CONFIG_PACKAGE_luci-app-bypass=n CONFIG_PACKAGE_luci-app-dnsfilter=y CONFIG_PACKAGE_luci-app-kodexplorer=y CONFIG_PACKAGE_luci-app-uhttpd=y CONFIG_PACKAGE_luci-app-mentohust=y # CONFIG_PACKAGE_luci-app-easymesh=n CONFIG_PACKAGE_luci-app-amule=n CONFIG_PACKAGE_luci-app-ttnode=n CONFIG_PACKAGE_luci-app-adblock-plus=n CONFIG_PACKAGE_luci-app-n2n_v2=y CONFIG_PACKAGE_luci-app-baidupcs-web=y #主题 CONFIG_PACKAGE_luci-theme-argon_new=y CONFIG_PACKAGE_luci-theme-btmod=n CONFIG_PACKAGE_luci-theme-opentomcat=n CONFIG_PACKAGE_luci-theme-opentopd=y #增加其它插件 CONFIG_PACKAGE_luci-app-ksmbd=n CONFIG_PACKAGE_luci-app-cifsd=n CONFIG_PACKAGE_luci-app-cifs-mount=y CONFIG_PACKAGE_luci-app-xlnetacc=y CONFIG_PACKAGE_luci-app-zerotier=y CONFIG_PACKAGE_luci-app-unblockneteasemusic=y CONFIG_PACKAGE_luci-app-unblockmusic=y CONFIG_UnblockNeteaseMusic_Go=y CONFIG_UnblockNeteaseMusic_NodeJS=y CONFIG_PACKAGE_luci-app-mwan3=y CONFIG_PACKAGE_luci-app-minidlna=y CONFIG_PACKAGE_luci-app-rclone=y CONFIG_PACKAGE_luci-app-rclone_INCLUDE_fuse-utils=y CONFIG_PACKAGE_luci-app-rclone_INCLUDE_rclone-ng=y CONFIG_PACKAGE_luci-app-rclone_INCLUDE_rclone-webui=y CONFIG_PACKAGE_luci-app-pptp-server=y CONFIG_PACKAGE_luci-app-pppoe-server=y CONFIG_PACKAGE_luci-app-ipsec-vpnd=y CONFIG_PACKAGE_luci-app-ipsec-serve=n # CONFIG_PACKAGE_luci-app-ipsec-vpnserver-manyusers is not set CONFIG_PACKAGE_luci-app-control-timewol=y CONFIG_PACKAGE_luci-app-docker=n CONFIG_PACKAGE_luci-app-dockerman=y CONFIG_PACKAGE_luci-app-koolddns=y CONFIG_PACKAGE_luci-app-syncdial=y CONFIG_PACKAGE_luci-app-softethervpn=y CONFIG_PACKAGE_luci-app-uugamebooster=y CONFIG_DEFAULT_luci-app-cpufreq=y CONFIG_PACKAGE_luci-app-udpxy=y CONFIG_PACKAGE_luci-app-socat=y CONFIG_PACKAGE_luci-app-oaf=y CONFIG_PACKAGE_luci-app-transmission=y CONFIG_PACKAGE_luci-app-usb-printer=y CONFIG_PACKAGE_luci-app-mwan3helper=y CONFIG_PACKAGE_luci-app-qbittorrent=y CONFIG_PACKAGE_luci-app-familycloud=y CONFIG_PACKAGE_luci-app-nps=y CONFIG_PACKAGE_luci-app-frpc=y #CONFIG_PACKAGE_luci-app-nfs=y CONFIG_PACKAGE_luci-app-openvpn-server=y CONFIG_PACKAGE_luci-app-aria2=y CONFIG_PACKAGE_luci-app-openvpn=y #network # CONFIG_PACKAGE_r8169-firmware=y CONFIG_PACKAGE_bnx2x-firmware=y CONFIG_PACKAGE_e100-firmware=y CONFIG_PACKAGE_kmod-3c59x=y CONFIG_PACKAGE_kmod-atl1=y CONFIG_PACKAGE_kmod-atl1c=y CONFIG_PACKAGE_kmod-atl1e=y CONFIG_PACKAGE_kmod-atl2=y CONFIG_PACKAGE_kmod-atm=y CONFIG_PACKAGE_kmod-b44=y CONFIG_PACKAGE_kmod-be2net=y CONFIG_PACKAGE_kmod-bnx2x=y CONFIG_PACKAGE_kmod-dm9000=y CONFIG_PACKAGE_kmod-dummy=y CONFIG_PACKAGE_kmod-e100=y CONFIG_PACKAGE_kmod-et131x=y CONFIG_PACKAGE_kmod-ethoc=y CONFIG_PACKAGE_kmod-hfcmulti=y CONFIG_PACKAGE_kmod-hfcpci=y CONFIG_PACKAGE_kmod-iavf=y CONFIG_PACKAGE_kmod-ixgbevf=y CONFIG_PACKAGE_kmod-lib-crc32c=y CONFIG_PACKAGE_kmod-mdio-gpio=y CONFIG_PACKAGE_kmod-misdn=y CONFIG_PACKAGE_kmod-natsemi=y CONFIG_PACKAGE_kmod-ne2k-pci=y CONFIG_PACKAGE_kmod-niu=y CONFIG_PACKAGE_kmod-of-mdio=y CONFIG_PACKAGE_kmod-phy-bcm84881=y CONFIG_PACKAGE_kmod-phy-broadcom=y CONFIG_PACKAGE_kmod-phy-realtek=y CONFIG_PACKAGE_kmod-phylib-broadcom=y CONFIG_PACKAGE_kmod-phylink=y CONFIG_PACKAGE_kmod-r8169=y CONFIG_PACKAGE_kmod-random-core=y CONFIG_PACKAGE_kmod-sfp=y CONFIG_PACKAGE_kmod-siit=y CONFIG_PACKAGE_kmod-sis190=y CONFIG_PACKAGE_kmod-sis900=y CONFIG_PACKAGE_kmod-skge=y CONFIG_PACKAGE_kmod-sky2=y CONFIG_PACKAGE_kmod-solos-pci=y CONFIG_PACKAGE_kmod-spi-ks8995=y CONFIG_PACKAGE_kmod-ssb=y CONFIG_PACKAGE_kmod-swconfig=y CONFIG_PACKAGE_kmod-switch-bcm53xx=y CONFIG_PACKAGE_kmod-switch-bcm53xx-mdio=y CONFIG_PACKAGE_kmod-switch-ip17xx=y CONFIG_PACKAGE_kmod-switch-mvsw61xx=y CONFIG_PACKAGE_kmod-switch-rtl8306=y CONFIG_PACKAGE_kmod-switch-rtl8366-smi=y # CONFIG_PACKAGE_kmod-switch-rtl8366rb=n CONFIG_PACKAGE_kmod-switch-rtl8366s=y CONFIG_PACKAGE_kmod-switch-rtl8367b=y CONFIG_PACKAGE_kmod-usb-atm=y CONFIG_PACKAGE_kmod-usb-atm-cxacru=y CONFIG_PACKAGE_kmod-usb-atm-speedtouch=y CONFIG_PACKAGE_kmod-usb-atm-ueagle=y CONFIG_PACKAGE_kmod-usb-cm109=y CONFIG_PACKAGE_kmod-usb-dwc2=y CONFIG_PACKAGE_kmod-usb-dwc3=y CONFIG_PACKAGE_kmod-usb-ehci=y CONFIG_PACKAGE_kmod-usb-ledtrig-usbport=y CONFIG_PACKAGE_kmod-usb-net-cdc-eem=y CONFIG_PACKAGE_kmod-usb-net-cdc-mbim=y CONFIG_PACKAGE_kmod-usb-net-cdc-ncm=y CONFIG_PACKAGE_kmod-usb-net-cdc-subset=y CONFIG_PACKAGE_kmod-usb-net-dm9601-ether=y CONFIG_PACKAGE_kmod-usb-net-hso=y CONFIG_PACKAGE_kmod-usb-net-huawei-cdc-ncm=y CONFIG_PACKAGE_kmod-usb-net-kalmia=y CONFIG_PACKAGE_kmod-usb-net-kaweth=y CONFIG_PACKAGE_kmod-usb-net-mcs7830=y CONFIG_PACKAGE_kmod-usb-net-pegasus=y CONFIG_PACKAGE_kmod-usb-net-pl=y CONFIG_PACKAGE_kmod-usb-net-qmi-wwan=y CONFIG_PACKAGE_kmod-usb-net-sierrawireless=y CONFIG_PACKAGE_kmod-usb-net-smsc95xx=y CONFIG_PACKAGE_kmod-usb-net-sr9700=y CONFIG_PACKAGE_kmod-usb-ohci=y CONFIG_PACKAGE_kmod-usb-ohci-pci=y CONFIG_PACKAGE_kmod-usb-uhci=y CONFIG_PACKAGE_kmod-usb-wdm=y CONFIG_PACKAGE_kmod-usb-yealink=y CONFIG_PACKAGE_kmod-usbip=y CONFIG_PACKAGE_kmod-usbip-client=y CONFIG_PACKAGE_kmod-usbip-server=y CONFIG_PACKAGE_kmod-usbmon=y CONFIG_PACKAGE_kmod-via-rhine=y CONFIG_PACKAGE_kmod-via-velocity=y #add drive CONFIG_PACKAGE_kmod-usb-net-rtl8150=y CONFIG_PACKAGE_kmod-usb-storage=y # Block Devices 挂载硬盘支持 CONFIG_PACKAGE_kmod-ata-core=y CONFIG_PACKAGE_kmod-block2mtd=y CONFIG_PACKAGE_kmod-scsi-core=y CONFIG_PACKAGE_kmod-scsi-generic=y CONFIG_PACKAGE_blockd=y CONFIG_PACKAGE_kmod-ata-ahci=y CONFIG_PACKAGE_kmod-ata-artop=y CONFIG_PACKAGE_kmod-ata-marvell-sata=y CONFIG_PACKAGE_kmod-ata-nvidia-sata=y CONFIG_PACKAGE_kmod-ata-pdc202xx-old=y CONFIG_PACKAGE_kmod-ata-piix=y CONFIG_PACKAGE_kmod-ata-sil=y CONFIG_PACKAGE_kmod-ata-sil24=y CONFIG_PACKAGE_kmod-ata-via-sata=y CONFIG_PACKAGE_kmod-dax=y CONFIG_PACKAGE_kmod-dm-raid=y CONFIG_PACKAGE_kmod-fs-autofs4=y CONFIG_PACKAGE_kmod-lib-crc32c=y CONFIG_PACKAGE_kmod-lib-raid6=y CONFIG_PACKAGE_kmod-lib-xor=y CONFIG_PACKAGE_kmod-md-mod=y CONFIG_PACKAGE_kmod-md-raid0=y CONFIG_PACKAGE_kmod-md-raid1=y CONFIG_PACKAGE_kmod-md-raid10=y CONFIG_PACKAGE_kmod-md-raid456=y #3G/4G Support CONFIG_PACKAGE_kmod-usb-serial=y CONFIG_PACKAGE_kmod-usb-serial-option=y CONFIG_PACKAGE_kmod-usb-serial-wwan=y CONFIG_PACKAGE_kmod-mii=y CONFIG_PACKAGE_kmod-usb-acm=y # docker CONFIG_PACKAGE_kmod-fs-btrfs=y CONFIG_PACKAGE_kmod-dm=y CONFIG_PACKAGE_kmod-br-netfilter=y CONFIG_PACKAGE_kmod-ikconfig=y CONFIG_PACKAGE_kmod-nf-conntrack-netlink=y CONFIG_PACKAGE_kmod-nf-ipvs=y CONFIG_PACKAGE_kmod-veth=y CONFIG_PACKAGE_kmod-dummy=y # n1 set CONFIG_PACKAGE_install-program=y CONFIG_BRCMSMAC_USE_FW_FROM_WL=y CONFIG_BTRFS_PROGS_ZSTD=y CONFIG_DRIVER_11N_SUPPORT=y CONFIG_PACKAGE_MAC80211_DEBUGFS=y CONFIG_PACKAGE_MAC80211_MESH=y CONFIG_PACKAGE_TAR_BZIP2=y CONFIG_PACKAGE_TAR_GZIP=y CONFIG_PACKAGE_TAR_XZ=y CONFIG_PACKAGE_TAR_ZSTD=y CONFIG_PACKAGE_attr=y CONFIG_PACKAGE_bash=y CONFIG_PACKAGE_blkid=y CONFIG_PACKAGE_btrfs-progs=y CONFIG_PACKAGE_bzip2=y CONFIG_PACKAGE_chattr=y CONFIG_PACKAGE_dosfstools=y CONFIG_PACKAGE_e2freefrag=y CONFIG_PACKAGE_f2fs-tools=y CONFIG_PACKAGE_f2fsck=y CONFIG_PACKAGE_fdisk=y CONFIG_PACKAGE_gawk=y CONFIG_PACKAGE_getopt=y CONFIG_PACKAGE_hostapd-common=y CONFIG_PACKAGE_iw=y CONFIG_PACKAGE_iwinfo=y CONFIG_PACKAGE_kmod-bcma=y CONFIG_PACKAGE_kmod-brcmsmac=y CONFIG_PACKAGE_kmod-brcmutil=y CONFIG_PACKAGE_kmod-cfg80211=y CONFIG_PACKAGE_kmod-crypto-acompress=y CONFIG_PACKAGE_kmod-crypto-ccm=y CONFIG_PACKAGE_kmod-crypto-cmac=y CONFIG_PACKAGE_kmod-crypto-crc32c=y CONFIG_PACKAGE_kmod-crypto-ctr=y CONFIG_PACKAGE_kmod-crypto-gcm=y CONFIG_PACKAGE_kmod-crypto-gf128=y CONFIG_PACKAGE_kmod-crypto-ghash=y CONFIG_PACKAGE_kmod-crypto-hmac=y CONFIG_PACKAGE_kmod-crypto-rng=y CONFIG_PACKAGE_kmod-crypto-seqiv=y CONFIG_PACKAGE_kmod-crypto-sha256=y CONFIG_PACKAGE_kmod-fs-btrfs=y CONFIG_PACKAGE_kmod-lib-cordic=y CONFIG_PACKAGE_kmod-lib-crc32c=y CONFIG_PACKAGE_kmod-lib-crc8=y CONFIG_PACKAGE_kmod-lib-lzo=y CONFIG_PACKAGE_kmod-lib-raid6=y CONFIG_PACKAGE_kmod-lib-xor=y CONFIG_PACKAGE_kmod-lib-zlib-deflate=y CONFIG_PACKAGE_kmod-lib-zlib-inflate=y CONFIG_PACKAGE_kmod-lib-zstd=y CONFIG_PACKAGE_kmod-mac80211=y CONFIG_PACKAGE_libattr=y CONFIG_PACKAGE_libbz2=y CONFIG_PACKAGE_libfdisk=y CONFIG_PACKAGE_liblzma=y CONFIG_PACKAGE_libmount=y CONFIG_PACKAGE_libncurses=y CONFIG_PACKAGE_libreadline=y CONFIG_PACKAGE_libsmartcols=y CONFIG_PACKAGE_libwolfssl=m CONFIG_PACKAGE_libzstd=y CONFIG_PACKAGE_losetup=y CONFIG_PACKAGE_lsattr=y CONFIG_PACKAGE_lsblk=y CONFIG_PACKAGE_partx-utils=y CONFIG_PACKAGE_perl=y CONFIG_PACKAGE_perl-http-date=y CONFIG_PACKAGE_perlbase-base=y CONFIG_PACKAGE_perlbase-bytes=y CONFIG_PACKAGE_perlbase-charnames=y CONFIG_PACKAGE_perlbase-class=y CONFIG_PACKAGE_perlbase-config=y CONFIG_PACKAGE_perlbase-cwd=y CONFIG_PACKAGE_perlbase-dynaloader=y CONFIG_PACKAGE_perlbase-errno=y CONFIG_PACKAGE_perlbase-essential=y CONFIG_PACKAGE_perlbase-fcntl=y CONFIG_PACKAGE_perlbase-file=y CONFIG_PACKAGE_perlbase-filehandle=y CONFIG_PACKAGE_perlbase-getopt=y CONFIG_PACKAGE_perlbase-i18n=y CONFIG_PACKAGE_perlbase-integer=y CONFIG_PACKAGE_perlbase-io=y CONFIG_PACKAGE_perlbase-list=y CONFIG_PACKAGE_perlbase-locale=y CONFIG_PACKAGE_perlbase-params=y CONFIG_PACKAGE_perlbase-posix=y CONFIG_PACKAGE_perlbase-re=y CONFIG_PACKAGE_perlbase-scalar=y CONFIG_PACKAGE_perlbase-selectsaver=y CONFIG_PACKAGE_perlbase-socket=y CONFIG_PACKAGE_perlbase-symbol=y CONFIG_PACKAGE_perlbase-tie=y CONFIG_PACKAGE_perlbase-time=y CONFIG_PACKAGE_perlbase-unicode=y CONFIG_PACKAGE_perlbase-unicore=y CONFIG_PACKAGE_perlbase-utf8=y CONFIG_PACKAGE_perlbase-xsloader=y CONFIG_PACKAGE_tar=y CONFIG_PACKAGE_terminfo=y CONFIG_PACKAGE_uuidgen=y CONFIG_PACKAGE_wireless-regdb=y CONFIG_PACKAGE_wpa-cli=y CONFIG_PACKAGE_wpad-basic=y CONFIG_PACKAGE_wpad-basic-wolfssl=m CONFIG_PACKAGE_xfs-fsck=y CONFIG_PACKAGE_xfs-mkfs=y CONFIG_PACKAGE_xz=y CONFIG_PACKAGE_xz-utils=y CONFIG_PERL_NOCOMMENT=y CONFIG_WOLFSSL_HAS_AES_CCM=y CONFIG_WOLFSSL_HAS_ARC4=y CONFIG_WOLFSSL_HAS_CERTGEN=y CONFIG_WOLFSSL_HAS_CHACHA_POLY=y CONFIG_WOLFSSL_HAS_DH=y CONFIG_WOLFSSL_HAS_NO_HW=y CONFIG_WOLFSSL_HAS_OCSP=y CONFIG_WOLFSSL_HAS_SESSION_TICKET=y CONFIG_WOLFSSL_HAS_TLSV10=y CONFIG_WOLFSSL_HAS_TLSV13=y CONFIG_WOLFSSL_HAS_WPAS=y CONFIG_WPA_MSG_MIN_PRIORITY=3 CONFIG_WPA_WOLFSSL=y CONFIG_ZSTD_OPTIMIZE_O3=y
940f5e70da2aeaa1c0cd967108cc2c8ac24ec0b0
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/control/traversable/default_auto.lean
b3248a5facd97afec3815d0e5b582a415bbcc503
[]
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
202
lean
import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.control.traversable.instances import Mathlib.control.traversable.lemmas import Mathlib.PostPort namespace Mathlib end Mathlib
ba1a79b014da0d64ac54c470d4016fbc7738208c
0c1546a496eccfb56620165cad015f88d56190c5
/library/init/data/nat/basic.lean
fb6d3dbec2dbf15ceb6be9caca27161011d74de2
[ "Apache-2.0" ]
permissive
Solertis/lean
491e0939957486f664498fbfb02546e042699958
84188c5aa1673fdf37a082b2de8562dddf53df3f
refs/heads/master
1,610,174,257,606
1,486,263,620,000
1,486,263,620,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,377
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura -/ prelude import init.logic init.data.num.basic notation `ℕ` := nat namespace nat inductive less_than_or_equal (a : ℕ) : ℕ → Prop | refl : less_than_or_equal a | step : Π {b}, less_than_or_equal b → less_than_or_equal (succ b) instance : has_le ℕ := ⟨nat.less_than_or_equal⟩ @[reducible] protected def le (n m : ℕ) := nat.less_than_or_equal n m @[reducible] protected def lt (n m : ℕ) := nat.less_than_or_equal (succ n) m instance : has_lt ℕ := ⟨nat.lt⟩ def pred : ℕ → ℕ | 0 := 0 | (a+1) := a protected def sub : ℕ → ℕ → ℕ | a 0 := a | a (b+1) := pred (sub a b) protected def mul : nat → nat → nat | a 0 := 0 | a (b+1) := (mul a b) + a instance : has_sub ℕ := ⟨nat.sub⟩ instance : has_mul ℕ := ⟨nat.mul⟩ instance : decidable_eq ℕ | zero zero := is_true rfl | (succ x) zero := is_false (λ h, nat.no_confusion h) | zero (succ y) := is_false (λ h, nat.no_confusion h) | (succ x) (succ y) := match decidable_eq x y with | is_true xeqy := is_true (xeqy ▸ eq.refl (succ x)) | is_false xney := is_false (λ h, nat.no_confusion h (λ xeqy, absurd xeqy xney)) end def {u} repeat {α : Type u} (f : ℕ → α → α) : ℕ → α → α | 0 a := a | (succ n) a := f n (repeat n a) instance : inhabited ℕ := ⟨nat.zero⟩ @[simp] lemma nat_zero_eq_zero : nat.zero = 0 := rfl /- properties of inequality -/ @[refl] protected def le_refl : ∀ a : ℕ, a ≤ a := less_than_or_equal.refl lemma le_succ (n : ℕ) : n ≤ succ n := less_than_or_equal.step (nat.le_refl n) lemma succ_le_succ {n m : ℕ} : n ≤ m → succ n ≤ succ m := λ h, less_than_or_equal.rec (nat.le_refl (succ n)) (λ a b, less_than_or_equal.step) h lemma zero_le : ∀ (n : ℕ), 0 ≤ n | 0 := nat.le_refl 0 | (n+1) := less_than_or_equal.step (zero_le n) lemma zero_lt_succ (n : ℕ) : 0 < succ n := succ_le_succ (zero_le n) def succ_pos := zero_lt_succ lemma not_succ_le_zero : ∀ (n : ℕ), succ n ≤ 0 → false . lemma not_lt_zero (a : ℕ) : ¬ a < 0 := not_succ_le_zero a lemma pred_le_pred {n m : ℕ} : n ≤ m → pred n ≤ pred m := λ h, less_than_or_equal.rec_on h (nat.le_refl (pred n)) (λ n, nat.rec (λ a b, b) (λ a b c, less_than_or_equal.step) n) lemma le_of_succ_le_succ {n m : ℕ} : succ n ≤ succ m → n ≤ m := pred_le_pred instance decidable_le : ∀ a b : ℕ, decidable (a ≤ b) | 0 b := is_true (zero_le b) | (a+1) 0 := is_false (not_succ_le_zero a) | (a+1) (b+1) := match decidable_le a b with | is_true h := is_true (succ_le_succ h) | is_false h := is_false (λ a, h (le_of_succ_le_succ a)) end instance decidable_lt : ∀ a b : ℕ, decidable (a < b) := λ a b, nat.decidable_le (succ a) b protected lemma eq_or_lt_of_le {a b : ℕ} (h : a ≤ b) : a = b ∨ a < b := less_than_or_equal.cases_on h (or.inl rfl) (λ n h, or.inr (succ_le_succ h)) lemma lt_succ_of_le {a b : ℕ} : a ≤ b → a < succ b := succ_le_succ @[simp] lemma succ_sub_succ_eq_sub (a b : ℕ) : succ a - succ b = a - b := nat.rec_on b (show succ a - succ zero = a - zero, from (eq.refl (succ a - succ zero))) (λ b, congr_arg pred) lemma not_succ_le_self : ∀ n : ℕ, ¬succ n ≤ n := λ n, nat.rec (not_succ_le_zero 0) (λ a b c, b (le_of_succ_le_succ c)) n protected lemma lt_irrefl (n : ℕ) : ¬n < n := not_succ_le_self n protected lemma le_trans {n m k : ℕ} (h1 : n ≤ m) : m ≤ k → n ≤ k := less_than_or_equal.rec h1 (λ p h2, less_than_or_equal.step) lemma pred_le : ∀ (n : ℕ), pred n ≤ n | 0 := less_than_or_equal.refl 0 | (succ a) := less_than_or_equal.step (less_than_or_equal.refl a) lemma sub_le (a b : ℕ) : a - b ≤ a := nat.rec_on b (nat.le_refl (a - 0)) (λ b₁, nat.le_trans (pred_le (a - b₁))) lemma sub_lt : ∀ {a b : ℕ}, 0 < a → 0 < b → a - b < a | 0 b h1 h2 := absurd h1 (nat.lt_irrefl 0) | (a+1) 0 h1 h2 := absurd h2 (nat.lt_irrefl 0) | (a+1) (b+1) h1 h2 := eq.symm (succ_sub_succ_eq_sub a b) ▸ show a - b < succ a, from lt_succ_of_le (sub_le a b) protected lemma lt_of_lt_of_le {n m k : ℕ} : n < m → m ≤ k → n < k := nat.le_trans end nat
a9a9ab99903559a52ead2f0c1958c1ffa6b67cd2
097294e9b80f0d9893ac160b9c7219aa135b51b9
/instructor/propositional_logic/satisfiability/propositional_logic_syntax_and_semantics.lean
5340f96e6212f09c692d57531136fd5333812829
[]
no_license
AbigailCastro17/CS2102-Discrete-Math
cf296251be9418ce90206f5e66bde9163e21abf9
d741e4d2d6a9b2e0c8380e51706218b8f608cee4
refs/heads/main
1,682,891,087,358
1,621,401,341,000
1,621,401,341,000
368,749,959
0
0
null
null
null
null
UTF-8
Lean
false
false
1,390
lean
/- Syntax, abstract and concrete, and semantics, of propositional logic. Import this file's definitions use this language and associated tools. -/ /- VARIABLES -/ inductive var : Type | mk : ℕ → var /- ABSTRACT SYNTAX -/ inductive pExp : Type | pTrue : pExp | pFalse : pExp | pVar : var → pExp | pNot : pExp → pExp | pAnd : pExp → pExp → pExp | pOr : pExp → pExp → pExp | pImp : pExp → pExp → pExp | pIff : pExp → pExp → pExp | pXor : pExp → pExp → pExp open pExp /- HELPER/UTILITY FUNCTIONS -/ def bimp : bool → bool → bool | tt tt := tt | tt ff := ff | ff tt := tt | ff ff := tt def biff : bool → bool → bool | tt tt := tt | tt ff := ff | ff tt := ff | ff ff := tt /- ABSTRACT SEMANTICS -/ def pEval : pExp → (var → bool) → bool | pTrue _ := tt | pFalse _ := ff | (pVar v) i := i v | (pNot e) i := bnot (pEval e i) | (pAnd e1 e2) i := band (pEval e1 i) (pEval e2 i) | (pOr e1 e2) i := bor (pEval e1 i) (pEval e2 i) | (pImp e1 e2) i := bimp (pEval e1 i) (pEval e2 i) | (pIff e1 e2) i := biff (pEval e1 i) (pEval e2 i) | (pXor e1 e2) i := xor (pEval e1 i) (pEval e2 i) /- CONCRETE SYNTAX -/ notation e1 ∧ e2 := pAnd e1 e2 notation e1 ∨ e2 := pOr e1 e2 notation ¬ e := pNot e -- notation e1 > e2 := pImp e1 e2 -- Bug infixr ` >> ` : 25 := pImp -- Fix notation e1 ↔ e2 := pIff e1 e2 notation e1 ⊕ e2 := pXor e1 e2
6194d0de4484ecb2054a61fd465465a4807577fa
82e44445c70db0f03e30d7be725775f122d72f3e
/src/ring_theory/subring.lean
37840f7ddbc87b94c0cd9740a776e34f2cd32bae
[ "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
36,046
lean
/- Copyright (c) 2020 Ashvni Narayanan. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ashvni Narayanan -/ import group_theory.subgroup import ring_theory.subsemiring /-! # Subrings Let `R` be a ring. This file defines the "bundled" subring type `subring R`, a type whose terms correspond to subrings of `R`. This is the preferred way to talk about subrings in mathlib. Unbundled subrings (`s : set R` and `is_subring s`) are not in this file, and they will ultimately be deprecated. We prove that subrings 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 `subring R`, sending a subset of `R` to the subring it generates, and prove that it is a Galois insertion. ## Main definitions Notation used here: `(R : Type u) [ring R] (S : Type u) [ring S] (f g : R →+* S)` `(A : subring R) (B : subring S) (s : set R)` * `subring R` : the type of subrings of a ring `R`. * `instance : complete_lattice (subring R)` : the complete lattice structure on the subrings. * `subring.closure` : subring closure of a set, i.e., the smallest subring that includes the set. * `subring.gi` : `closure : set M → subring M` and coercion `coe : subring M → set M` form a `galois_insertion`. * `comap f B : subring A` : the preimage of a subring `B` along the ring homomorphism `f` * `map f A : subring B` : the image of a subring `A` along the ring homomorphism `f`. * `prod A B : subring (R × S)` : the product of subrings * `f.range : subring B` : the range of the ring homomorphism `f`. * `eq_locus f g : subring R` : given ring homomorphisms `f g : R →+* S`, the subring of `R` where `f x = g x` ## Implementation notes A subring is implemented as a subsemiring which is also an additive subgroup. The initial PR was as a submonoid which is also an additive subgroup. Lattice inclusion (e.g. `≤` and `⊓`) is used rather than set notation (`⊆` and `∩`), although `∈` is defined as membership of a subring's underlying set. ## Tags subring, subrings -/ open_locale big_operators universes u v w variables {R : Type u} {S : Type v} {T : Type w} [ring R] [ring S] [ring T] set_option old_structure_cmd true /-- `subring R` is the type of subrings of `R`. A subring 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 subring (R : Type u) [ring R] extends subsemiring R, add_subgroup R /-- Reinterpret a `subring` as a `subsemiring`. -/ add_decl_doc subring.to_subsemiring /-- Reinterpret a `subring` as an `add_subgroup`. -/ add_decl_doc subring.to_add_subgroup namespace subring /-- The underlying submonoid of a subring. -/ def to_submonoid (s : subring R) : submonoid R := { carrier := s.carrier, ..s.to_subsemiring.to_submonoid } instance : set_like (subring R) R := ⟨subring.carrier, λ p q h, by cases p; cases q; congr'⟩ @[simp] lemma mem_carrier {s : subring R} {x : R} : x ∈ s.carrier ↔ x ∈ s := iff.rfl /-- Two subrings are equal if they have the same elements. -/ @[ext] theorem ext {S T : subring R} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := set_like.ext h /-- Copy of a subring with a new `carrier` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (S : subring R) (s : set R) (hs : s = ↑S) : subring R := { carrier := s, neg_mem' := hs.symm ▸ S.neg_mem', ..S.to_subsemiring.copy s hs } lemma to_subsemiring_injective : function.injective (to_subsemiring : subring R → subsemiring R) | r s h := ext (set_like.ext_iff.mp h : _) @[mono] lemma to_subsemiring_strict_mono : strict_mono (to_subsemiring : subring R → subsemiring R) := λ _ _, id @[mono] lemma to_subsemiring_mono : monotone (to_subsemiring : subring R → subsemiring R) := to_subsemiring_strict_mono.monotone lemma to_add_subgroup_injective : function.injective (to_add_subgroup : subring R → add_subgroup R) | r s h := ext (set_like.ext_iff.mp h : _) @[mono] lemma to_add_subgroup_strict_mono : strict_mono (to_add_subgroup : subring R → add_subgroup R) := λ _ _, id @[mono] lemma to_add_subgroup_mono : monotone (to_add_subgroup : subring R → add_subgroup R) := to_add_subgroup_strict_mono.monotone lemma to_submonoid_injective : function.injective (to_submonoid : subring R → submonoid R) | r s h := ext (set_like.ext_iff.mp h : _) @[mono] lemma to_submonoid_strict_mono : strict_mono (to_submonoid : subring R → submonoid R) := λ _ _, id @[mono] lemma to_submonoid_mono : monotone (to_submonoid : subring R → submonoid R) := to_submonoid_strict_mono.monotone /-- Construct a `subring R` from a set `s`, a submonoid `sm`, and an additive subgroup `sa` such that `x ∈ s ↔ x ∈ sm ↔ x ∈ sa`. -/ protected def mk' (s : set R) (sm : submonoid R) (sa : add_subgroup R) (hm : ↑sm = s) (ha : ↑sa = s) : subring R := { carrier := s, zero_mem' := ha ▸ sa.zero_mem, one_mem' := hm ▸ sm.one_mem, add_mem' := λ x y, by simpa only [← ha] using sa.add_mem, mul_mem' := λ x y, by simpa only [← hm] using sm.mul_mem, neg_mem' := λ x, by simpa only [← ha] using sa.neg_mem, } @[simp] lemma coe_mk' {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa = s) : (subring.mk' s sm sa hm ha : set R) = s := rfl @[simp] lemma mem_mk' {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa = s) {x : R} : x ∈ subring.mk' s sm sa hm ha ↔ x ∈ s := iff.rfl @[simp] lemma mk'_to_submonoid {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa = s) : (subring.mk' s sm sa hm ha).to_submonoid = sm := set_like.coe_injective hm.symm @[simp] lemma mk'_to_add_subgroup {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa =s) : (subring.mk' s sm sa hm ha).to_add_subgroup = sa := set_like.coe_injective ha.symm end subring /-- A `subsemiring` containing -1 is a `subring`. -/ def subsemiring.to_subring (s : subsemiring R) (hneg : (-1 : R) ∈ s) : subring R := { neg_mem' := by { rintros x, rw <-neg_one_mul, apply subsemiring.mul_mem, exact hneg, } ..s.to_submonoid, ..s.to_add_submonoid } namespace subring variables (s : subring R) /-- A subring contains the ring's 1. -/ theorem one_mem : (1 : R) ∈ s := s.one_mem' /-- A subring contains the ring's 0. -/ theorem zero_mem : (0 : R) ∈ s := s.zero_mem' /-- A subring is closed under multiplication. -/ theorem mul_mem : ∀ {x y : R}, x ∈ s → y ∈ s → x * y ∈ s := s.mul_mem' /-- A subring is closed under addition. -/ theorem add_mem : ∀ {x y : R}, x ∈ s → y ∈ s → x + y ∈ s := s.add_mem' /-- A subring is closed under negation. -/ theorem neg_mem : ∀ {x : R}, x ∈ s → -x ∈ s := s.neg_mem' /-- A subring is closed under subtraction -/ theorem sub_mem {x y : R} (hx : x ∈ s) (hy : y ∈ s) : x - y ∈ s := by { rw sub_eq_add_neg, exact s.add_mem hx (s.neg_mem hy) } /-- Product of a list of elements in a subring is in the subring. -/ lemma list_prod_mem {l : list R} : (∀x ∈ l, x ∈ s) → l.prod ∈ s := s.to_submonoid.list_prod_mem /-- Sum of a list of elements in a subring is in the subring. -/ lemma list_sum_mem {l : list R} : (∀x ∈ l, x ∈ s) → l.sum ∈ s := s.to_add_subgroup.list_sum_mem /-- Product of a multiset of elements in a subring of a `comm_ring` is in the subring. -/ lemma multiset_prod_mem {R} [comm_ring R] (s : subring R) (m : multiset R) : (∀a ∈ m, a ∈ s) → m.prod ∈ s := s.to_submonoid.multiset_prod_mem m /-- Sum of a multiset of elements in an `subring` of a `ring` is in the `subring`. -/ lemma multiset_sum_mem {R} [ring R] (s : subring R) (m : multiset R) : (∀a ∈ m, a ∈ s) → m.sum ∈ s := s.to_add_subgroup.multiset_sum_mem m /-- Product of elements of a subring of a `comm_ring` indexed by a `finset` is in the subring. -/ lemma prod_mem {R : Type*} [comm_ring R] (s : subring R) {ι : Type*} {t : finset ι} {f : ι → R} (h : ∀c ∈ t, f c ∈ s) : ∏ i in t, f i ∈ s := s.to_submonoid.prod_mem h /-- Sum of elements in a `subring` of a `ring` indexed by a `finset` is in the `subring`. -/ lemma sum_mem {R : Type*} [ring R] (s : subring R) {ι : Type*} {t : finset ι} {f : ι → R} (h : ∀c ∈ t, f c ∈ s) : ∑ i in t, f i ∈ s := s.to_add_subgroup.sum_mem h lemma pow_mem {x : R} (hx : x ∈ s) (n : ℕ) : x^n ∈ s := s.to_submonoid.pow_mem hx n lemma gsmul_mem {x : R} (hx : x ∈ s) (n : ℤ) : n • x ∈ s := s.to_add_subgroup.gsmul_mem hx n lemma coe_int_mem (n : ℤ) : (n : R) ∈ s := by simp only [← gsmul_one, gsmul_mem, one_mem] /-- A subring of a ring inherits a ring structure -/ instance to_ring : ring s := { right_distrib := λ x y z, subtype.eq $ right_distrib x y z, left_distrib := λ x y z, subtype.eq $ left_distrib x y z, .. s.to_submonoid.to_monoid, .. s.to_add_subgroup.to_add_comm_group } @[simp, norm_cast] lemma coe_add (x y : s) : (↑(x + y) : R) = ↑x + ↑y := rfl @[simp, norm_cast] lemma coe_neg (x : s) : (↑(-x) : R) = -↑x := rfl @[simp, norm_cast] lemma coe_mul (x y : s) : (↑(x * y) : R) = ↑x * ↑y := rfl @[simp, norm_cast] lemma coe_zero : ((0 : s) : R) = 0 := rfl @[simp, norm_cast] lemma coe_one : ((1 : s) : R) = 1 := rfl @[simp, norm_cast] lemma coe_pow (x : s) (n : ℕ) : (↑(x ^ n) : R) = x ^ n := s.to_submonoid.coe_pow x n @[simp] lemma coe_eq_zero_iff {x : s} : (x : R) = 0 ↔ x = 0 := ⟨λ h, subtype.ext (trans h s.coe_zero.symm), λ h, h.symm ▸ s.coe_zero⟩ /-- A subring of a `comm_ring` is a `comm_ring`. -/ instance to_comm_ring {R} [comm_ring R] (s : subring R) : comm_ring s := { mul_comm := λ _ _, subtype.eq $ mul_comm _ _, ..subring.to_ring s} /-- A subring of a non-trivial ring is non-trivial. -/ instance {R} [ring R] [nontrivial R] (s : subring R) : nontrivial s := s.to_subsemiring.nontrivial /-- A subring of a ring with no zero divisors has no zero divisors. -/ instance {R} [ring R] [no_zero_divisors R] (s : subring R) : no_zero_divisors s := s.to_subsemiring.no_zero_divisors /-- A subring of an integral domain is an integral domain. -/ instance {R} [integral_domain R] (s : subring R) : integral_domain s := { .. s.nontrivial, .. s.no_zero_divisors, .. s.to_comm_ring } /-- A subring of an `ordered_ring` is an `ordered_ring`. -/ instance to_ordered_ring {R} [ordered_ring R] (s : subring R) : ordered_ring s := subtype.coe_injective.ordered_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) /-- A subring of an `ordered_comm_ring` is an `ordered_comm_ring`. -/ instance to_ordered_comm_ring {R} [ordered_comm_ring R] (s : subring R) : ordered_comm_ring s := subtype.coe_injective.ordered_comm_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) /-- A subring of a `linear_ordered_ring` is a `linear_ordered_ring`. -/ instance to_linear_ordered_ring {R} [linear_ordered_ring R] (s : subring R) : linear_ordered_ring s := subtype.coe_injective.linear_ordered_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) /-- A subring of a `linear_ordered_comm_ring` is a `linear_ordered_comm_ring`. -/ instance to_linear_ordered_comm_ring {R} [linear_ordered_comm_ring R] (s : subring R) : linear_ordered_comm_ring s := subtype.coe_injective.linear_ordered_comm_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) /-- The natural ring hom from a subring of ring `R` to `R`. -/ def subtype (s : subring R) : s →+* R := { to_fun := coe, .. s.to_submonoid.subtype, .. s.to_add_subgroup.subtype } @[simp] theorem coe_subtype : ⇑s.subtype = coe := rfl @[simp, norm_cast] lemma coe_nat_cast (n : ℕ) : ((n : s) : R) = n := s.subtype.map_nat_cast n @[simp, norm_cast] lemma coe_int_cast (n : ℤ) : ((n : s) : R) = n := s.subtype.map_int_cast n /-! # Partial order -/ @[simp] lemma mem_to_submonoid {s : subring R} {x : R} : x ∈ s.to_submonoid ↔ x ∈ s := iff.rfl @[simp] lemma coe_to_submonoid (s : subring R) : (s.to_submonoid : set R) = s := rfl @[simp] lemma mem_to_add_subgroup {s : subring R} {x : R} : x ∈ s.to_add_subgroup ↔ x ∈ s := iff.rfl @[simp] lemma coe_to_add_subgroup (s : subring R) : (s.to_add_subgroup : set R) = s := rfl /-! # top -/ /-- The subring `R` of the ring `R`. -/ instance : has_top (subring R) := ⟨{ .. (⊤ : submonoid R), .. (⊤ : add_subgroup R) }⟩ @[simp] lemma mem_top (x : R) : x ∈ (⊤ : subring R) := set.mem_univ x @[simp] lemma coe_top : ((⊤ : subring R) : set R) = set.univ := rfl /-! # comap -/ /-- The preimage of a subring along a ring homomorphism is a subring. -/ def comap {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : subring S) : subring R := { carrier := f ⁻¹' s.carrier, .. s.to_submonoid.comap (f : R →* S), .. s.to_add_subgroup.comap (f : R →+ S) } @[simp] lemma coe_comap (s : subring S) (f : R →+* S) : (s.comap f : set R) = f ⁻¹' s := rfl @[simp] lemma mem_comap {s : subring S} {f : R →+* S} {x : R} : x ∈ s.comap f ↔ f x ∈ s := iff.rfl lemma comap_comap (s : subring T) (g : S →+* T) (f : R →+* S) : (s.comap g).comap f = s.comap (g.comp f) := rfl /-! # map -/ /-- The image of a subring along a ring homomorphism is a subring. -/ def map {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : subring R) : subring S := { carrier := f '' s.carrier, .. s.to_submonoid.map (f : R →* S), .. s.to_add_subgroup.map (f : R →+ S) } @[simp] lemma coe_map (f : R →+* S) (s : subring R) : (s.map f : set S) = f '' s := rfl @[simp] lemma mem_map {f : R →+* S} {s : subring R} {y : S} : y ∈ s.map f ↔ ∃ x ∈ s, f x = y := set.mem_image_iff_bex lemma map_map (g : S →+* T) (f : R →+* S) : (s.map f).map g = s.map (g.comp f) := set_like.coe_injective $ set.image_image _ _ _ lemma map_le_iff_le_comap {f : R →+* S} {s : subring R} {t : subring S} : s.map f ≤ t ↔ s ≤ t.comap f := set.image_subset_iff lemma gc_map_comap (f : R →+* S) : galois_connection (map f) (comap f) := λ S T, map_le_iff_le_comap /-- A subring is isomorphic to its image under an injective function -/ noncomputable def equiv_map_of_injective (f : R →+* S) (hf : function.injective f) : s ≃+* s.map f := { map_mul' := λ _ _, subtype.ext (f.map_mul _ _), map_add' := λ _ _, subtype.ext (f.map_add _ _), ..equiv.set.image f s hf } @[simp] lemma coe_equiv_map_of_injective_apply (f : R →+* S) (hf : function.injective f) (x : s) : (equiv_map_of_injective s f hf x : S) = f x := rfl end subring namespace ring_hom variables (g : S →+* T) (f : R →+* S) /-! # range -/ /-- The range of a ring homomorphism, as a subring of the target. See Note [range copy pattern]. -/ def range {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) : subring S := ((⊤ : subring R).map f).copy (set.range f) set.image_univ.symm @[simp] lemma coe_range : (f.range : set S) = set.range f := rfl @[simp] lemma mem_range {f : R →+* S} {y : S} : y ∈ f.range ↔ ∃ x, f x = y := iff.rfl lemma range_eq_map (f : R →+* S) : f.range = subring.map f ⊤ := by { ext, simp } lemma mem_range_self (f : R →+* S) (x : R) : f x ∈ f.range := mem_range.mpr ⟨x, rfl⟩ lemma map_range : f.range.map g = (g.comp f).range := by simpa only [range_eq_map] using (⊤ : subring R).map_map g f -- TODO -- rename to `cod_restrict` when is_ring_hom is deprecated /-- Restrict the codomain of a ring homomorphism to a subring that includes the range. -/ def cod_restrict' {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : subring S) (h : ∀ x, f x ∈ s) : R →+* s := { to_fun := λ x, ⟨f x, h x⟩, map_add' := λ x y, subtype.eq $ f.map_add x y, map_zero' := subtype.eq f.map_zero, map_mul' := λ x y, subtype.eq $ f.map_mul x y, map_one' := subtype.eq f.map_one } /-- The range of a ring homomorphism is a fintype, if the domain is a fintype. Note: this instance can form a diamond with `subtype.fintype` in the presence of `fintype S`. -/ instance fintype_range [fintype R] [decidable_eq S] (f : R →+* S) : fintype (range f) := set.fintype_range f end ring_hom namespace subring /-! # bot -/ instance : has_bot (subring R) := ⟨(int.cast_ring_hom R).range⟩ instance : inhabited (subring R) := ⟨⊥⟩ lemma coe_bot : ((⊥ : subring R) : set R) = set.range (coe : ℤ → R) := ring_hom.coe_range (int.cast_ring_hom R) lemma mem_bot {x : R} : x ∈ (⊥ : subring R) ↔ ∃ (n : ℤ), ↑n = x := ring_hom.mem_range /-! # inf -/ /-- The inf of two subrings is their intersection. -/ instance : has_inf (subring R) := ⟨λ s t, { carrier := s ∩ t, .. s.to_submonoid ⊓ t.to_submonoid, .. s.to_add_subgroup ⊓ t.to_add_subgroup }⟩ @[simp] lemma coe_inf (p p' : subring R) : ((p ⊓ p' : subring R) : set R) = p ∩ p' := rfl @[simp] lemma mem_inf {p p' : subring R} {x : R} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl instance : has_Inf (subring R) := ⟨λ s, subring.mk' (⋂ t ∈ s, ↑t) (⨅ t ∈ s, subring.to_submonoid t ) (⨅ t ∈ s, subring.to_add_subgroup t) (by simp) (by simp)⟩ @[simp, norm_cast] lemma coe_Inf (S : set (subring R)) : ((Inf S : subring R) : set R) = ⋂ s ∈ S, ↑s := rfl lemma mem_Inf {S : set (subring R)} {x : R} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_bInter_iff @[simp] lemma Inf_to_submonoid (s : set (subring R)) : (Inf s).to_submonoid = ⨅ t ∈ s, subring.to_submonoid t := mk'_to_submonoid _ _ @[simp] lemma Inf_to_add_subgroup (s : set (subring R)) : (Inf s).to_add_subgroup = ⨅ t ∈ s, subring.to_add_subgroup t := mk'_to_add_subgroup _ _ /-- Subrings of a ring form a complete lattice. -/ instance : complete_lattice (subring R) := { bot := (⊥), bot_le := λ s x hx, let ⟨n, hn⟩ := mem_bot.1 hx in hn ▸ s.coe_int_mem n, 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 (subring R) (λ s, is_glb.of_image (λ s t, show (s : set R) ≤ t ↔ s ≤ t, from set_like.coe_subset_coe) is_glb_binfi)} lemma eq_top_iff' (A : subring R) : A = ⊤ ↔ ∀ x : R, x ∈ A := eq_top_iff.trans ⟨λ h m, h $ mem_top m, λ h m _, h m⟩ /-! # subring closure of a subset -/ /-- The `subring` generated by a set. -/ def closure (s : set R) : subring R := Inf {S | s ⊆ S} lemma mem_closure {x : R} {s : set R} : x ∈ closure s ↔ ∀ S : subring R, s ⊆ S → x ∈ S := mem_Inf /-- The subring generated by a set includes the set. -/ @[simp] lemma subset_closure {s : set R} : s ⊆ closure s := λ x hx, mem_closure.2 $ λ S hS, hS hx /-- A subring `t` includes `closure s` if and only if it includes `s`. -/ @[simp] lemma closure_le {s : set R} {t : subring R} : closure s ≤ t ↔ s ⊆ t := ⟨set.subset.trans subset_closure, λ h, Inf_le h⟩ /-- Subring closure of a set is monotone in its argument: if `s ⊆ t`, then `closure s ≤ closure t`. -/ lemma closure_mono ⦃s t : set R⦄ (h : s ⊆ t) : closure s ≤ closure t := closure_le.2 $ set.subset.trans h subset_closure lemma closure_eq_of_le {s : set R} {t : subring R} (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 `0`, `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 R} {p : R → Prop} {x} (h : x ∈ closure s) (Hs : ∀ x ∈ s, p x) (H0 : p 0) (H1 : p 1) (Hadd : ∀ x y, p x → p y → p (x + y)) (Hneg : ∀ (x : R), p x → p (-x)) (Hmul : ∀ x y, p x → p y → p (x * y)) : p x := (@closure_le _ _ _ ⟨p, H1, Hmul, H0, Hadd, Hneg⟩).2 Hs h lemma mem_closure_iff {s : set R} {x} : x ∈ closure s ↔ x ∈ add_subgroup.closure (submonoid.closure s : set R) := ⟨ λ h, closure_induction h (λ x hx, add_subgroup.subset_closure $ submonoid.subset_closure hx ) (add_subgroup.zero_mem _) (add_subgroup.subset_closure ( submonoid.one_mem (submonoid.closure s)) ) (λ x y hx hy, add_subgroup.add_mem _ hx hy ) (λ x hx, add_subgroup.neg_mem _ hx ) ( λ x y hx hy, add_subgroup.closure_induction hy (λ q hq, add_subgroup.closure_induction hx ( λ p hp, add_subgroup.subset_closure ((submonoid.closure s).mul_mem hp hq) ) ( begin rw zero_mul q, apply add_subgroup.zero_mem _, end ) ( λ p₁ p₂ ihp₁ ihp₂, begin rw add_mul p₁ p₂ q, apply add_subgroup.add_mem _ ihp₁ ihp₂, end ) ( λ x hx, begin have f : -x * q = -(x*q) := by simp, rw f, apply add_subgroup.neg_mem _ hx, end ) ) ( begin rw mul_zero x, apply add_subgroup.zero_mem _, end ) ( λ q₁ q₂ ihq₁ ihq₂, begin rw mul_add x q₁ q₂, apply add_subgroup.add_mem _ ihq₁ ihq₂ end ) ( λ z hz, begin have f : x * -z = -(x*z) := by simp, rw f, apply add_subgroup.neg_mem _ hz, end ) ), λ h, add_subgroup.closure_induction h ( λ x hx, submonoid.closure_induction hx ( λ x hx, subset_closure hx ) ( one_mem _ ) ( λ x y hx hy, mul_mem _ hx hy ) ) ( zero_mem _ ) (λ x y hx hy, add_mem _ hx hy) ( λ x hx, neg_mem _ hx ) ⟩ theorem exists_list_of_mem_closure {s : set R} {x : R} (h : x ∈ closure s) : (∃ L : list (list R), (∀ t ∈ L, ∀ y ∈ t, y ∈ s ∨ y = (-1:R)) ∧ (L.map list.prod).sum = x) := add_subgroup.closure_induction (mem_closure_iff.1 h) (λ x hx, let ⟨l, hl, h⟩ :=submonoid.exists_list_of_mem_closure hx in ⟨[l], by simp [h]; clear_aux_decl; tauto!⟩) ⟨[], by simp⟩ (λ x y ⟨l, hl1, hl2⟩ ⟨m, hm1, hm2⟩, ⟨l ++ m, λ t ht, (list.mem_append.1 ht).elim (hl1 t) (hm1 t), by simp [hl2, hm2]⟩) (λ x ⟨L, hL⟩, ⟨L.map (list.cons (-1)), list.forall_mem_map_iff.2 $ λ j hj, list.forall_mem_cons.2 ⟨or.inr rfl, hL.1 j hj⟩, hL.2 ▸ list.rec_on L (by simp) (by simp [list.map_cons, add_comm] {contextual := tt})⟩) variable (R) /-- `closure` forms a Galois insertion with the coercion to set. -/ protected def gi : galois_insertion (@closure R _) coe := { choice := λ s _, closure s, gc := λ s t, closure_le, le_l_u := λ s, subset_closure, choice_eq := λ s h, rfl } variable {R} /-- Closure of a subring `S` equals `S`. -/ lemma closure_eq (s : subring R) : closure (s : set R) = s := (subring.gi R).l_u_eq s @[simp] lemma closure_empty : closure (∅ : set R) = ⊥ := (subring.gi R).gc.l_bot @[simp] lemma closure_univ : closure (set.univ : set R) = ⊤ := @coe_top R _ ▸ closure_eq ⊤ lemma closure_union (s t : set R) : closure (s ∪ t) = closure s ⊔ closure t := (subring.gi R).gc.l_sup lemma closure_Union {ι} (s : ι → set R) : closure (⋃ i, s i) = ⨆ i, closure (s i) := (subring.gi R).gc.l_supr lemma closure_sUnion (s : set (set R)) : closure (⋃₀ s) = ⨆ t ∈ s, closure t := (subring.gi R).gc.l_Sup lemma map_sup (s t : subring R) (f : R →+* S) : (s ⊔ t).map f = s.map f ⊔ t.map f := (gc_map_comap f).l_sup lemma map_supr {ι : Sort*} (f : R →+* S) (s : ι → subring R) : (supr s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_supr lemma comap_inf (s t : subring S) (f : R →+* S) : (s ⊓ t).comap f = s.comap f ⊓ t.comap f := (gc_map_comap f).u_inf lemma comap_infi {ι : Sort*} (f : R →+* S) (s : ι → subring S) : (infi s).comap f = ⨅ i, (s i).comap f := (gc_map_comap f).u_infi @[simp] lemma map_bot (f : R →+* S) : (⊥ : subring R).map f = ⊥ := (gc_map_comap f).l_bot @[simp] lemma comap_top (f : R →+* S) : (⊤ : subring S).comap f = ⊤ := (gc_map_comap f).u_top /-- Given `subring`s `s`, `t` of rings `R`, `S` respectively, `s.prod t` is `s × t` as a subring of `R × S`. -/ def prod (s : subring R) (t : subring S) : subring (R × S) := { carrier := (s : set R).prod t, .. s.to_submonoid.prod t.to_submonoid, .. s.to_add_subgroup.prod t.to_add_subgroup} @[norm_cast] lemma coe_prod (s : subring R) (t : subring S) : (s.prod t : set (R × S)) = (s : set R).prod (t : set S) := rfl lemma mem_prod {s : subring R} {t : subring S} {p : R × S} : p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl @[mono] lemma prod_mono ⦃s₁ s₂ : subring R⦄ (hs : s₁ ≤ s₂) ⦃t₁ t₂ : subring S⦄ (ht : t₁ ≤ t₂) : s₁.prod t₁ ≤ s₂.prod t₂ := set.prod_mono hs ht lemma prod_mono_right (s : subring R) : monotone (λ t : subring S, s.prod t) := prod_mono (le_refl s) lemma prod_mono_left (t : subring S) : monotone (λ s : subring R, s.prod t) := λ s₁ s₂ hs, prod_mono hs (le_refl t) lemma prod_top (s : subring R) : s.prod (⊤ : subring S) = s.comap (ring_hom.fst R S) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_fst] lemma top_prod (s : subring S) : (⊤ : subring R).prod s = s.comap (ring_hom.snd R S) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_snd] @[simp] lemma top_prod_top : (⊤ : subring R).prod (⊤ : subring S) = ⊤ := (top_prod _).trans $ comap_top _ /-- Product of subrings is isomorphic to their product as rings. -/ def prod_equiv (s : subring R) (t : subring S) : s.prod t ≃+* s × t := { map_mul' := λ x y, rfl, map_add' := λ x y, rfl, .. equiv.set.prod ↑s ↑t } /-- The underlying set of a non-empty directed Sup of subrings is just a union of the subrings. Note that this fails without the directedness assumption (the union of two subrings is typically not a subring) -/ lemma mem_supr_of_directed {ι} [hι : nonempty ι] {S : ι → subring R} (hS : directed (≤) S) {x : R} : x ∈ (⨆ i, S i) ↔ ∃ i, x ∈ S i := begin refine ⟨_, λ ⟨i, hi⟩, (set_like.le_def.1 $ le_supr S i) hi⟩, let U : subring R := subring.mk' (⋃ i, (S i : set R)) (⨆ i, (S i).to_submonoid) (⨆ i, (S i).to_add_subgroup) (submonoid.coe_supr_of_directed $ hS.mono_comp _ (λ _ _, id)) (add_subgroup.coe_supr_of_directed $ hS.mono_comp _ (λ _ _, id)), suffices : (⨆ i, S i) ≤ U, by simpa using @this x, exact supr_le (λ i x hx, set.mem_Union.2 ⟨i, hx⟩), end lemma coe_supr_of_directed {ι} [hι : nonempty ι] {S : ι → subring R} (hS : directed (≤) S) : ((⨆ i, S i : subring R) : set R) = ⋃ i, ↑(S i) := set.ext $ λ x, by simp [mem_supr_of_directed hS] lemma mem_Sup_of_directed_on {S : set (subring R)} (Sne : S.nonempty) (hS : directed_on (≤) S) {x : R} : 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 (subring R)} (Sne : S.nonempty) (hS : directed_on (≤) S) : (↑(Sup S) : set R) = ⋃ s ∈ S, ↑s := set.ext $ λ x, by simp [mem_Sup_of_directed_on Sne hS] lemma mem_map_equiv {f : R ≃+* S} {K : subring R} {x : S} : x ∈ K.map (f : R →+* S) ↔ f.symm x ∈ K := @set.mem_image_equiv _ _ ↑K f.to_equiv x lemma map_equiv_eq_comap_symm (f : R ≃+* S) (K : subring R) : K.map (f : R →+* S) = K.comap f.symm := set_like.coe_injective (f.to_equiv.image_eq_preimage K) lemma comap_equiv_eq_map_symm (f : R ≃+* S) (K : subring S) : K.comap (f : R →+* S) = K.map f.symm := (map_equiv_eq_comap_symm f.symm K).symm end subring namespace ring_hom variables [ring T] {s : subring R} open subring /-- Restriction of a ring homomorphism to a subring of the domain. -/ def restrict (f : R →+* S) (s : subring R) : s →+* S := f.comp s.subtype @[simp] lemma restrict_apply (f : R →+* S) (x : s) : f.restrict s x = f x := rfl /-- Restriction of a ring homomorphism to its range interpreted as a subsemiring. This is the bundled version of `set.range_factorization`. -/ def range_restrict (f : R →+* S) : R →+* f.range := f.cod_restrict' f.range $ λ x, ⟨x, rfl⟩ @[simp] lemma coe_range_restrict (f : R →+* S) (x : R) : (f.range_restrict x : S) = f x := rfl lemma range_restrict_surjective (f : R →+* S) : function.surjective f.range_restrict := λ ⟨y, hy⟩, let ⟨x, hx⟩ := mem_range.mp hy in ⟨x, subtype.ext hx⟩ lemma range_top_iff_surjective {f : R →+* S} : f.range = (⊤ : subring S) ↔ function.surjective f := set_like.ext'_iff.trans $ iff.trans (by rw [coe_range, coe_top]) set.range_iff_surjective /-- The range of a surjective ring homomorphism is the whole of the codomain. -/ lemma range_top_of_surjective (f : R →+* S) (hf : function.surjective f) : f.range = (⊤ : subring S) := range_top_iff_surjective.2 hf /-- The subring of elements `x : R` such that `f x = g x`, i.e., the equalizer of f and g as a subring of R -/ def eq_locus (f g : R →+* S) : subring R := { carrier := {x | f x = g x}, .. (f : R →* S).eq_mlocus g, .. (f : R →+ S).eq_locus g } /-- If two ring homomorphisms are equal on a set, then they are equal on its subring closure. -/ lemma eq_on_set_closure {f g : R →+* S} {s : set R} (h : set.eq_on f g s) : set.eq_on f g (closure s) := show closure s ≤ f.eq_locus g, from closure_le.2 h lemma eq_of_eq_on_set_top {f g : R →+* S} (h : set.eq_on f g (⊤ : subring R)) : f = g := ext $ λ x, h trivial lemma eq_of_eq_on_set_dense {s : set R} (hs : closure s = ⊤) {f g : R →+* S} (h : s.eq_on f g) : f = g := eq_of_eq_on_set_top $ hs ▸ eq_on_set_closure h lemma closure_preimage_le (f : R →+* S) (s : set S) : 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 subring generated by a set equals the subring generated by the image of the set. -/ lemma map_closure (f : R →+* S) (s : set R) : (closure s).map f = closure (f '' s) := le_antisymm (map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image _ _) (closure_preimage_le _ _)) (closure_le.2 $ set.image_subset _ subset_closure) end ring_hom namespace subring open ring_hom /-- The ring homomorphism associated to an inclusion of subrings. -/ def inclusion {S T : subring R} (h : S ≤ T) : S →* T := S.subtype.cod_restrict' _ (λ x, h x.2) @[simp] lemma range_subtype (s : subring R) : s.subtype.range = s := set_like.coe_injective $ (coe_srange _).trans subtype.range_coe @[simp] lemma range_fst : (fst R S).srange = ⊤ := (fst R S).srange_top_of_surjective $ prod.fst_surjective @[simp] lemma range_snd : (snd R S).srange = ⊤ := (snd R S).srange_top_of_surjective $ prod.snd_surjective @[simp] lemma prod_bot_sup_bot_prod (s : subring R) (t : subring S) : (s.prod ⊥) ⊔ (prod ⊥ t) = s.prod t := le_antisymm (sup_le (prod_mono_right s bot_le) (prod_mono_left t bot_le)) $ assume p hp, prod.fst_mul_snd p ▸ mul_mem _ ((le_sup_left : s.prod ⊥ ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨hp.1, set_like.mem_coe.2 $ one_mem ⊥⟩) ((le_sup_right : prod ⊥ t ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨set_like.mem_coe.2 $ one_mem ⊥, hp.2⟩) end subring namespace ring_equiv variables {s t : subring R} /-- Makes the identity isomorphism from a proof two subrings of a multiplicative monoid are equal. -/ def subring_congr (h : s = t) : s ≃+* t := { map_mul' := λ _ _, rfl, map_add' := λ _ _, rfl, ..equiv.set_congr $ congr_arg _ h } /-- Restrict a ring homomorphism with a left inverse to a ring isomorphism to its `ring_hom.range`. -/ def of_left_inverse {g : S → R} {f : R →+* S} (h : function.left_inverse g f) : R ≃+* f.range := { to_fun := λ x, f.range_restrict x, inv_fun := λ x, (g ∘ f.range.subtype) x, left_inv := h, right_inv := λ x, subtype.ext $ let ⟨x', hx'⟩ := ring_hom.mem_range.mp x.prop in show f (g x) = x, by rw [←hx', h x'], ..f.range_restrict } @[simp] lemma of_left_inverse_apply {g : S → R} {f : R →+* S} (h : function.left_inverse g f) (x : R) : ↑(of_left_inverse h x) = f x := rfl @[simp] lemma of_left_inverse_symm_apply {g : S → R} {f : R →+* S} (h : function.left_inverse g f) (x : f.range) : (of_left_inverse h).symm x = g x := rfl end ring_equiv namespace subring variables {s : set R} local attribute [reducible] closure @[elab_as_eliminator] protected theorem in_closure.rec_on {C : R → Prop} {x : R} (hx : x ∈ closure s) (h1 : C 1) (hneg1 : C (-1)) (hs : ∀ z ∈ s, ∀ n, C n → C (z * n)) (ha : ∀ {x y}, C x → C y → C (x + y)) : C x := begin have h0 : C 0 := add_neg_self (1:R) ▸ ha h1 hneg1, rcases exists_list_of_mem_closure hx with ⟨L, HL, rfl⟩, clear hx, induction L with hd tl ih, { exact h0 }, rw list.forall_mem_cons at HL, suffices : C (list.prod hd), { rw [list.map_cons, list.sum_cons], exact ha this (ih HL.2) }, replace HL := HL.1, clear ih tl, suffices : ∃ L : list R, (∀ x ∈ L, x ∈ s) ∧ (list.prod hd = list.prod L ∨ list.prod hd = -list.prod L), { rcases this with ⟨L, HL', HP | HP⟩, { rw HP, clear HP HL hd, induction L with hd tl ih, { exact h1 }, rw list.forall_mem_cons at HL', rw list.prod_cons, exact hs _ HL'.1 _ (ih HL'.2) }, rw HP, clear HP HL hd, induction L with hd tl ih, { exact hneg1 }, rw [list.prod_cons, neg_mul_eq_mul_neg], rw list.forall_mem_cons at HL', exact hs _ HL'.1 _ (ih HL'.2) }, induction hd with hd tl ih, { exact ⟨[], list.forall_mem_nil _, or.inl rfl⟩ }, rw list.forall_mem_cons at HL, rcases ih HL.2 with ⟨L, HL', HP | HP⟩; cases HL.1 with hhd hhd, { exact ⟨hd :: L, list.forall_mem_cons.2 ⟨hhd, HL'⟩, or.inl $ by rw [list.prod_cons, list.prod_cons, HP]⟩ }, { exact ⟨L, HL', or.inr $ by rw [list.prod_cons, hhd, neg_one_mul, HP]⟩ }, { exact ⟨hd :: L, list.forall_mem_cons.2 ⟨hhd, HL'⟩, or.inr $ by rw [list.prod_cons, list.prod_cons, HP, neg_mul_eq_mul_neg]⟩ }, { exact ⟨L, HL', or.inl $ by rw [list.prod_cons, hhd, HP, neg_one_mul, neg_neg]⟩ } end lemma closure_preimage_le (f : R →+* S) (s : set S) : closure (f ⁻¹' s) ≤ (closure s).comap f := closure_le.2 $ λ x hx, set_like.mem_coe.2 $ mem_comap.2 $ subset_closure hx end subring lemma add_subgroup.int_mul_mem {G : add_subgroup R} (k : ℤ) {g : R} (h : g ∈ G) : (k : R) * g ∈ G := by { convert add_subgroup.gsmul_mem G h k, simp } /-! ### Actions by `subring`s These are just copies of the definitions about `subsemiring` starting from `subsemiring.mul_action`. When `R` is commutative, `algebra.of_subring` provides a stronger result than those found in this file, which uses the same scalar action. -/ section actions namespace subring variables {α β : Type*} /-- The action by a subring is the action by the underlying ring. -/ instance [mul_action R α] (S : subring R) : mul_action S α := S.to_subsemiring.mul_action lemma smul_def [mul_action R α] {S : subring R} (g : S) (m : α) : g • m = (g : R) • m := rfl instance smul_comm_class_left [mul_action R β] [has_scalar α β] [smul_comm_class R α β] (S : subring R) : smul_comm_class S α β := S.to_subsemiring.smul_comm_class_left instance smul_comm_class_right [has_scalar α β] [mul_action R β] [smul_comm_class α R β] (S : subring R) : smul_comm_class α S β := S.to_subsemiring.smul_comm_class_right /-- Note that this provides `is_scalar_tower S R R` which is needed by `smul_mul_assoc`. -/ instance [has_scalar α β] [mul_action R α] [mul_action R β] [is_scalar_tower R α β] (S : subring R) : is_scalar_tower S α β := S.to_subsemiring.is_scalar_tower /-- The action by a subring is the action by the underlying ring. -/ instance [add_monoid α] [distrib_mul_action R α] (S : subring R) : distrib_mul_action S α := S.to_subsemiring.distrib_mul_action /-- The action by a subring is the action by the underlying ring. -/ instance [add_comm_monoid α] [module R α] (S : subring R) : module S α := S.to_subsemiring.module end subring end actions
c964f9ca6506e8719780921f49c9599fbc2f2b73
f68c8823d8ddc719de8a4513815174aa7408ac4a
/lean_resolutions/PUZ133=2.lean
df6246eb939c3cfe8a3b0ffdcc338d9c71777ef5
[]
no_license
FredsoNerd/tptp-lean-puzzles
e7ea66a0de9aa3cb7cc7480299f01adf885440a6
43d4d77524e797a4ac7a62b2cfaf8df08b409815
refs/heads/master
1,606,359,611,765
1,576,824,274,000
1,576,824,274,000
228,945,807
4
0
null
null
null
null
UTF-8
Lean
false
false
2,183
lean
-------------------------------------------------------------------------------- -- File : PUZ133=2 : TPTP v7.3.0. Released v5.0.0. -- Domain : Puzzles -- Problem : N queens problem has the variable symmetry property -- Version : Especial. -- English : -- Refs : [Bau08] Baumgartner (2008), Email to G. Sutcliffe -- : [BS09] Baumgartner & Slaney (2009), Constraint Modelling: A C -- Source : [TPTP] -------------------------------------------------------------------------------- ----queens_p = ---- forall (i in 1..n, j in i + 1..n) ( ---- p[i] != p[j] ---- /\ p[i] + i != p[j] + j ---- /\ p[i] - i != p[j] - j ---- ); ----... in terms of decision variables named p: variable queens_q: Prop variable queens_p: Prop variable q: int → int variable p: int → int variable n: int variable perm: int → int -- axioms variable queens_p_axiom: ( queens_p → ∀ (I: int)(J: int) , ( ( 1≤I) ∧ (I≤n) ∧ ((1+I)≤J) ∧ (J≤n) ) → ( p(I) ≠ p(J) ∧ (p(I)+I) ≠ (p(J)+J) ∧ (p(I)-I) ≠ (p(J)-J) ) ) -----The permutation function variable permutation: ∀ (I: int) , perm(I) = ((1+n)-I) -----... in terms of decision variables named q: variable queens_q_axiom: ( ∀ (I: int)(J: int) , ( ( (1≤I) ∧ (I≤n) ∧ ((1+I)≤J) ∧ (J≤n) ∧ ( ((1+I)≤J) ↔ ((1+perm(J))≤perm(I)) ) ) → ( q(I) ≤ q(J) ∧ (q(I)+I) ≠ (q(J)+J) ∧ (q(I)-I) ≠ (q(J)-J) ) ) → queens_q ) -----To prove: "queens_p /\ q is a permutation of p => queens_q" -- conjecture : queens_sym theorem queens_sym : ( ( queens_p ∧ ∀ (I: int) , q(I) = p(perm(I)) ) → queens_q ) := sorry -----Properties of permutations -----Permutation stays in range 1..n: variable permutation_range: ∀ (I: int) , ( ( (1≤I) ∧ (I≤n) ) → ( (1≤perm(I)) ∧ (perm(I)≤n) ) ) -----Lemma lemma permutation_another_one: ∀ (J: int)(I: int) , (I-J) = (perm(J)-perm(I)) := sorry
f20d9c56104d188cf455a23dafcf31fab5a572f3
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/stage0/src/Lean/Elab/Print.lean
addcb9355949d696517e6d3396bf849f13cdeb70
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
5,984
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Util.FoldConsts import Lean.Elab.Command namespace Lean.Elab.Command private def throwUnknownId (id : Name) : CommandElabM Unit := throwError "unknown identifier '{mkConst id}'" private def levelParamsToMessageData (levelParams : List Name) : MessageData := match levelParams with | [] => "" | u::us => Id.run do let mut m := m!".\{{u}" for u in us do m := m ++ ", " ++ toMessageData u return m ++ "}" private def mkHeader (kind : String) (id : Name) (levelParams : List Name) (type : Expr) (safety : DefinitionSafety) : CommandElabM MessageData := do let m : MessageData := match (← getReducibilityStatus id) with | ReducibilityStatus.irreducible => "@[irreducible] " | ReducibilityStatus.reducible => "@[reducible] " | ReducibilityStatus.semireducible => "" let m := m ++ match safety with | DefinitionSafety.unsafe => "unsafe " | DefinitionSafety.partial => "partial " | DefinitionSafety.safe => "" let m := if isProtected (← getEnv) id then m ++ "protected " else m let (m, id) := match privateToUserName? id with | some id => (m ++ "private ", id) | none => (m, id) let m := m ++ kind ++ " " ++ id ++ levelParamsToMessageData levelParams ++ " : " ++ type pure m private def mkHeader' (kind : String) (id : Name) (levelParams : List Name) (type : Expr) (isUnsafe : Bool) : CommandElabM MessageData := mkHeader kind id levelParams type (if isUnsafe then DefinitionSafety.unsafe else DefinitionSafety.safe) private def printDefLike (kind : String) (id : Name) (levelParams : List Name) (type : Expr) (value : Expr) (safety := DefinitionSafety.safe) : CommandElabM Unit := do let m ← mkHeader kind id levelParams type safety let m := m ++ " :=" ++ Format.line ++ value logInfo m private def printAxiomLike (kind : String) (id : Name) (levelParams : List Name) (type : Expr) (isUnsafe := false) : CommandElabM Unit := do logInfo (← mkHeader' kind id levelParams type isUnsafe) private def printQuot (id : Name) (levelParams : List Name) (type : Expr) : CommandElabM Unit := do printAxiomLike "Quotient primitive" id levelParams type private def printInduct (id : Name) (levelParams : List Name) (numParams : Nat) (type : Expr) (ctors : List Name) (isUnsafe : Bool) : CommandElabM Unit := do let mut m ← mkHeader' "inductive" id levelParams type isUnsafe m := m ++ Format.line ++ "number of parameters: " ++ toString numParams m := m ++ Format.line ++ "constructors:" for ctor in ctors do let cinfo ← getConstInfo ctor m := m ++ Format.line ++ ctor ++ " : " ++ cinfo.type logInfo m private def printIdCore (id : Name) : CommandElabM Unit := do match (← getEnv).find? id with | ConstantInfo.axiomInfo { levelParams := us, type := t, isUnsafe := u, .. } => printAxiomLike "axiom" id us t u | ConstantInfo.defnInfo { levelParams := us, type := t, value := v, safety := s, .. } => printDefLike "def" id us t v s | ConstantInfo.thmInfo { levelParams := us, type := t, value := v, .. } => printDefLike "theorem" id us t v | ConstantInfo.opaqueInfo { levelParams := us, type := t, isUnsafe := u, .. } => printAxiomLike "opaque" id us t u | ConstantInfo.quotInfo { levelParams := us, type := t, .. } => printQuot id us t | ConstantInfo.ctorInfo { levelParams := us, type := t, isUnsafe := u, .. } => printAxiomLike "constructor" id us t u | ConstantInfo.recInfo { levelParams := us, type := t, isUnsafe := u, .. } => printAxiomLike "recursor" id us t u | ConstantInfo.inductInfo { levelParams := us, numParams, type := t, ctors, isUnsafe := u, .. } => printInduct id us numParams t ctors u | none => throwUnknownId id private def printId (id : Syntax) : CommandElabM Unit := do addCompletionInfo <| CompletionInfo.id id id.getId (danglingDot := false) {} none let cs ← resolveGlobalConstWithInfos id cs.forM printIdCore @[builtinCommandElab «print»] def elabPrint : CommandElab | `(#print%$tk $id:ident) => withRef tk <| printId id | `(#print%$tk $s:str) => logInfoAt tk s.getString | _ => throwError "invalid #print command" namespace CollectAxioms structure State where visited : NameSet := {} axioms : Array Name := #[] abbrev M := ReaderT Environment $ StateM State partial def collect (c : Name) : M Unit := do let collectExpr (e : Expr) : M Unit := e.getUsedConstants.forM collect let s ← get unless s.visited.contains c do modify fun s => { s with visited := s.visited.insert c } let env ← read match env.find? c with | some (ConstantInfo.axiomInfo _) => modify fun s => { s with axioms := s.axioms.push c } | some (ConstantInfo.defnInfo v) => collectExpr v.type *> collectExpr v.value | some (ConstantInfo.thmInfo v) => collectExpr v.type *> collectExpr v.value | some (ConstantInfo.opaqueInfo v) => collectExpr v.type *> collectExpr v.value | some (ConstantInfo.quotInfo _) => pure () | some (ConstantInfo.ctorInfo v) => collectExpr v.type | some (ConstantInfo.recInfo v) => collectExpr v.type | some (ConstantInfo.inductInfo v) => collectExpr v.type *> v.ctors.forM collect | none => pure () end CollectAxioms private def printAxiomsOf (constName : Name) : CommandElabM Unit := do let env ← getEnv let (_, s) := ((CollectAxioms.collect constName).run env).run {} if s.axioms.isEmpty then logInfo m!"'{constName}' does not depend on any axioms" else logInfo m!"'{constName}' depends on axioms: {s.axioms.toList}" @[builtinCommandElab «printAxioms»] def elabPrintAxioms : CommandElab | `(#print%$tk axioms $id) => withRef tk do let cs ← resolveGlobalConstWithInfos id cs.forM printAxiomsOf | _ => throwUnsupportedSyntax end Lean.Elab.Command
d586ef6292e2cde2191dab284d30027984ac3965
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/category_theory/monoidal/category.lean
08f64db141167f26c4f5ce3855b97ad8710599a5
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
20,733
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, Bhavik Mehta, Jakob von Raumer -/ 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 (name := tensor_obj) ` ⊗ `: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 [reassoc] monoidal_category.pentagon attribute [simp, reassoc] monoidal_category.triangle open monoidal_category infixr (name := tensor_obj) ` ⊗ `:70 := tensor_obj infixr (name := tensor_hom) ` ⊗ `: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 (name := tensor_iso) ` ⊗ `: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} lemma tensor_dite {P : Prop} [decidable P] {W X Y Z : C} (f : W ⟶ X) (g : P → (Y ⟶ Z)) (g' : ¬P → (Y ⟶ Z)) : f ⊗ (if h : P then g h else g' h) = if h : P then f ⊗ g h else f ⊗ g' h := by { split_ifs; refl } lemma dite_tensor {P : Prop} [decidable P] {W X Y Z : C} (f : W ⟶ X) (g : P → (Y ⟶ Z)) (g' : ¬P → (Y ⟶ Z)) : (if h : P then g h else g' h) ⊗ f = if h : P then g h ⊗ f else g' h ⊗ f := by { split_ifs; refl } @[reassoc, simp] lemma comp_tensor_id (f : W ⟶ X) (g : X ⟶ Y) : (f ≫ g) ⊗ (𝟙 Z) = (f ⊗ (𝟙 Z)) ≫ (g ⊗ (𝟙 Z)) := by { rw ←tensor_comp, simp } @[reassoc, 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 } @[simp] lemma right_unitor_conjugation {X Y : C} (f : X ⟶ Y) : (f ⊗ (𝟙 (𝟙_ C))) = (ρ_ X).hom ≫ f ≫ (ρ_ Y).inv := by rw [←right_unitor_naturality_assoc, iso.hom_inv_id, category.comp_id] @[simp] lemma left_unitor_conjugation {X Y : C} (f : X ⟶ Y) : ((𝟙 (𝟙_ C)) ⊗ f) = (λ_ X).hom ≫ f ≫ (λ_ Y).inv := by rw [←left_unitor_naturality_assoc, iso.hom_inv_id, category.comp_id] @[reassoc] lemma left_unitor_inv_naturality {X X' : C} (f : X ⟶ X') : f ≫ (λ_ X').inv = (λ_ X).inv ≫ (𝟙 _ ⊗ f) := by simp @[reassoc] lemma right_unitor_inv_naturality {X X' : C} (f : X ⟶ X') : f ≫ (ρ_ X').inv = (ρ_ X).inv ≫ (f ⊗ 𝟙 _) := by simp lemma tensor_left_iff {X Y : C} (f g : X ⟶ Y) : ((𝟙 (𝟙_ C)) ⊗ f = (𝟙 (𝟙_ C)) ⊗ g) ↔ (f = g) := by simp lemma tensor_right_iff {X Y : C} (f g : X ⟶ Y) : (f ⊗ (𝟙 (𝟙_ C)) = g ⊗ (𝟙 (𝟙_ C))) ↔ (f = g) := by simp /-! The lemmas in the next section are true by coherence, but we prove them directly as they are used in proving the coherence theorem. -/ section @[reassoc] 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 := category_theory.eq_of_inv_eq_inv (by simp [pentagon]) @[reassoc, 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, ←cancel_mono (α_ X Y (𝟙_ C)).hom, assoc, associator_naturality, ←triangle_assoc, ←triangle, id_tensor_comp, pentagon_assoc, ←associator_naturality, tensor_id] @[reassoc, 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) @[simp, reassoc] lemma triangle_assoc_comp_right (X Y : C) : (α_ X (𝟙_ C) Y).inv ≫ ((ρ_ X).hom ⊗ 𝟙 Y) = ((𝟙 X) ⊗ (λ_ Y).hom) := by rw [←triangle, iso.inv_hom_id_assoc] @[simp, reassoc] 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 @[reassoc] 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) := by { rw [comp_inv_eq, assoc, associator_naturality], simp } @[reassoc, simp] lemma associator_conjugation {X X' Y Y' Z Z' : C} (f : X ⟶ X') (g : Y ⟶ Y') (h : Z ⟶ Z') : (f ⊗ g) ⊗ h = (α_ X Y Z).hom ≫ (f ⊗ (g ⊗ h)) ≫ (α_ X' Y' Z').inv := by rw [associator_inv_naturality, hom_inv_id_assoc] @[reassoc] lemma associator_inv_conjugation {X X' Y Y' Z Z' : C} (f : X ⟶ X') (g : Y ⟶ Y') (h : Z ⟶ Z') : f ⊗ g ⊗ h = (α_ X Y Z).inv ≫ ((f ⊗ g) ⊗ h) ≫ (α_ X' Y' Z').hom := by rw [associator_naturality, inv_hom_id_assoc] -- TODO these next two lemmas aren't so fundamental, and perhaps could be removed -- (replacing their usages by their proofs). @[reassoc] lemma id_tensor_associator_naturality {X Y Z Z' : C} (h : Z ⟶ Z') : (𝟙 (X ⊗ Y) ⊗ h) ≫ (α_ X Y Z').hom = (α_ X Y Z).hom ≫ (𝟙 X ⊗ (𝟙 Y ⊗ h)) := by { rw [←tensor_id, associator_naturality], } @[reassoc] lemma id_tensor_associator_inv_naturality {X Y Z X' : C} (f : X ⟶ X') : (f ⊗ 𝟙 (Y ⊗ Z)) ≫ (α_ X' Y Z).inv = (α_ X Y Z).inv ≫ ((f ⊗ 𝟙 Y) ⊗ 𝟙 Z) := by { rw [←tensor_id, associator_inv_naturality] } @[simp, reassoc] lemma hom_inv_id_tensor {V W X Y Z : C} (f : V ≅ W) (g : X ⟶ Y) (h : Y ⟶ Z) : (f.hom ⊗ g) ≫ (f.inv ⊗ h) = (𝟙 V ⊗ g) ≫ (𝟙 V ⊗ h) := by rw [←tensor_comp, f.hom_inv_id, id_tensor_comp] @[simp, reassoc] lemma inv_hom_id_tensor {V W X Y Z : C} (f : V ≅ W) (g : X ⟶ Y) (h : Y ⟶ Z) : (f.inv ⊗ g) ≫ (f.hom ⊗ h) = (𝟙 W ⊗ g) ≫ (𝟙 W ⊗ h) := by rw [←tensor_comp, f.inv_hom_id, id_tensor_comp] @[simp, reassoc] lemma tensor_hom_inv_id {V W X Y Z : C} (f : V ≅ W) (g : X ⟶ Y) (h : Y ⟶ Z) : (g ⊗ f.hom) ≫ (h ⊗ f.inv) = (g ⊗ 𝟙 V) ≫ (h ⊗ 𝟙 V) := by rw [←tensor_comp, f.hom_inv_id, comp_tensor_id] @[simp, reassoc] lemma tensor_inv_hom_id {V W X Y Z : C} (f : V ≅ W) (g : X ⟶ Y) (h : Y ⟶ Z) : (g ⊗ f.inv) ≫ (h ⊗ f.hom) = (g ⊗ 𝟙 W) ≫ (h ⊗ 𝟙 W) := by rw [←tensor_comp, f.inv_hom_id, comp_tensor_id] @[simp, reassoc] lemma hom_inv_id_tensor' {V W X Y Z : C} (f : V ⟶ W) [is_iso f] (g : X ⟶ Y) (h : Y ⟶ Z) : (f ⊗ g) ≫ (inv f ⊗ h) = (𝟙 V ⊗ g) ≫ (𝟙 V ⊗ h) := by rw [←tensor_comp, is_iso.hom_inv_id, id_tensor_comp] @[simp, reassoc] lemma inv_hom_id_tensor' {V W X Y Z : C} (f : V ⟶ W) [is_iso f] (g : X ⟶ Y) (h : Y ⟶ Z) : (inv f ⊗ g) ≫ (f ⊗ h) = (𝟙 W ⊗ g) ≫ (𝟙 W ⊗ h) := by rw [←tensor_comp, is_iso.inv_hom_id, id_tensor_comp] @[simp, reassoc] lemma tensor_hom_inv_id' {V W X Y Z : C} (f : V ⟶ W) [is_iso f] (g : X ⟶ Y) (h : Y ⟶ Z) : (g ⊗ f) ≫ (h ⊗ inv f) = (g ⊗ 𝟙 V) ≫ (h ⊗ 𝟙 V) := by rw [←tensor_comp, is_iso.hom_inv_id, comp_tensor_id] @[simp, reassoc] lemma tensor_inv_hom_id' {V W X Y Z : C} (f : V ⟶ W) [is_iso f] (g : X ⟶ Y) (h : Y ⟶ Z) : (g ⊗ inv f) ≫ (h ⊗ f) = (g ⊗ 𝟙 W) ≫ (h ⊗ 𝟙 W) := by rw [←tensor_comp, is_iso.inv_hom_id, comp_tensor_id] end section variables (C : Type u) [category.{v} C] [monoidal_category.{v} C] /-- The tensor product expressed as a functor. -/ @[simps] 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 left, as a functor from `C` into endofunctors of `C`. TODO: show this is a op-monoidal functor. -/ @[simps] def tensoring_left : C ⥤ C ⥤ C := { obj := tensor_left, map := λ X Y f, { app := λ Z, f ⊗ (𝟙 Z) } } instance : faithful (tensoring_left C) := { map_injective' := λ X Y f g h, begin injections with h, replace h := congr_fun h (𝟙_ C), simpa using h, end } /-- 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 section universes v₁ v₂ u₁ u₂ variables (C₁ : Type u₁) [category.{v₁} C₁] [monoidal_category.{v₁} C₁] variables (C₂ : Type u₂) [category.{v₂} C₂] [monoidal_category.{v₂} C₂] local attribute [simp] associator_naturality left_unitor_naturality right_unitor_naturality pentagon @[simps tensor_obj tensor_hom tensor_unit associator] instance prod_monoidal : monoidal_category (C₁ × C₂) := { tensor_obj := λ X Y, (X.1 ⊗ Y.1, X.2 ⊗ Y.2), tensor_hom := λ _ _ _ _ f g, (f.1 ⊗ g.1, f.2 ⊗ g.2), tensor_unit := (𝟙_ C₁, 𝟙_ C₂), associator := λ X Y Z, (α_ X.1 Y.1 Z.1).prod (α_ X.2 Y.2 Z.2), left_unitor := λ ⟨X₁, X₂⟩, (λ_ X₁).prod (λ_ X₂), right_unitor := λ ⟨X₁, X₂⟩, (ρ_ X₁).prod (ρ_ X₂) } @[simp] lemma prod_monoidal_left_unitor_hom_fst (X : C₁ × C₂) : ((λ_ X).hom : (𝟙_ _) ⊗ X ⟶ X).1 = (λ_ X.1).hom := by { cases X, refl } @[simp] lemma prod_monoidal_left_unitor_hom_snd (X : C₁ × C₂) : ((λ_ X).hom : (𝟙_ _) ⊗ X ⟶ X).2 = (λ_ X.2).hom := by { cases X, refl } @[simp] lemma prod_monoidal_left_unitor_inv_fst (X : C₁ × C₂) : ((λ_ X).inv : X ⟶ (𝟙_ _) ⊗ X).1 = (λ_ X.1).inv := by { cases X, refl } @[simp] lemma prod_monoidal_left_unitor_inv_snd (X : C₁ × C₂) : ((λ_ X).inv : X ⟶ (𝟙_ _) ⊗ X).2 = (λ_ X.2).inv := by { cases X, refl } @[simp] lemma prod_monoidal_right_unitor_hom_fst (X : C₁ × C₂) : ((ρ_ X).hom : X ⊗ (𝟙_ _) ⟶ X).1 = (ρ_ X.1).hom := by { cases X, refl } @[simp] lemma prod_monoidal_right_unitor_hom_snd (X : C₁ × C₂) : ((ρ_ X).hom : X ⊗ (𝟙_ _) ⟶ X).2 = (ρ_ X.2).hom := by { cases X, refl } @[simp] lemma prod_monoidal_right_unitor_inv_fst (X : C₁ × C₂) : ((ρ_ X).inv : X ⟶ X ⊗ (𝟙_ _)).1 = (ρ_ X.1).inv := by { cases X, refl } @[simp] lemma prod_monoidal_right_unitor_inv_snd (X : C₁ × C₂) : ((ρ_ X).inv : X ⟶ X ⊗ (𝟙_ _)).2 = (ρ_ X.2).inv := by { cases X, refl } end end monoidal_category end category_theory
411d41f94e6372423f8daab87bb8ab20933f0db4
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/stage0/src/Lean/PrettyPrinter/Delaborator/TopDownAnalyze.lean
f9398e7387bd47a8fc3705bb414e3b2e243d1bc7
[ "Apache-2.0" ]
permissive
subfish-zhou/leanprover-zh_CN.github.io
30b9fba9bd790720bd95764e61ae796697d2f603
8b2985d4a3d458ceda9361ac454c28168d920d3f
refs/heads/master
1,689,709,967,820
1,632,503,056,000
1,632,503,056,000
409,962,097
1
0
null
null
null
null
UTF-8
Lean
false
false
27,418
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Daniel Selsam -/ import Lean.Meta import Lean.Util.FindMVar import Lean.Util.FindLevelMVar import Lean.Util.CollectLevelParams import Lean.Util.ReplaceLevel import Lean.PrettyPrinter.Delaborator.Options import Lean.PrettyPrinter.Delaborator.SubExpr import Std.Data.RBMap /-! The top-down analyzer is an optional preprocessor to the delaborator that aims to determine the minimal annotations necessary to ensure that the delaborated expression can be re-elaborated correctly. Currently, the top-down analyzer is neither sound nor complete: there may be edge-cases in which the expression can still not be re-elaborated correctly, and it may also add many annotations that are not strictly necessary. -/ namespace Lean open Lean.Meta open Std (RBMap) register_builtin_option pp.analyze : Bool := { defValue := false group := "pp.analyze" descr := "(pretty printer analyzer) determine annotations sufficient to ensure round-tripping" } register_builtin_option pp.analyze.checkInstances : Bool := { -- TODO: It would be great to make this default to `true`, but currently, `MessageData` does not -- include the `LocalInstances`, so this will be very over-aggressive in inserting instances -- that would otherwise be easy to synthesize. We may consider threading the instances in the future, -- or at least tracking a bool for whether the instances have been lost. defValue := false group := "pp.analyze" descr := "(pretty printer analyzer) confirm that instances can be re-synthesized" } register_builtin_option pp.analyze.typeAscriptions : Bool := { defValue := true group := "pp.analyze" descr := "(pretty printer analyzer) add type ascriptions when deemed necessary" } register_builtin_option pp.analyze.trustSubst : Bool := { defValue := false group := "pp.analyze" descr := "(pretty printer analyzer) always 'pretend' applications that can delab to ▸ are 'regular'" } register_builtin_option pp.analyze.trustOfNat : Bool := { defValue := true group := "pp.analyze" descr := "(pretty printer analyzer) always 'pretend' `OfNat.ofNat` applications can elab bottom-up" } register_builtin_option pp.analyze.trustOfScientific : Bool := { defValue := true group := "pp.analyze" descr := "(pretty printer analyzer) always 'pretend' `OfScientific.ofScientific` applications can elab bottom-up" } register_builtin_option pp.analyze.trustCoe : Bool := { defValue := false group := "pp.analyze" descr := "(pretty printer analyzer) always assume a coercion can be correctly inserted" } -- TODO: this is an arbitrary special case of a more general principle. register_builtin_option pp.analyze.trustSubtypeMk : Bool := { defValue := true group := "pp.analyze" descr := "(pretty printer analyzer) assume the implicit arguments of Subtype.mk can be inferred" } register_builtin_option pp.analyze.trustId : Bool := { defValue := true group := "pp.analyze" descr := "(pretty printer analyzer) always assume an implicit `fun x => x` can be inferred" } register_builtin_option pp.analyze.trustKnownFOType2TypeHOFuns : Bool := { defValue := true group := "pp.analyze" descr := "(pretty printer analyzer) omit higher-order functions whose values seem to be knownType2Type" } register_builtin_option pp.analyze.omitMax : Bool := { defValue := true group := "pp.analyze" descr := "(pretty printer analyzer) omit universe `max` annotations (these constraints can actually hurt)" } register_builtin_option pp.analyze.knowsType : Bool := { defValue := true group := "pp.analyze" descr := "(pretty printer analyzer) assume the type of the original expression is known" } register_builtin_option pp.analyze.explicitHoles : Bool := { defValue := false group := "pp.analyze" descr := "(pretty printer analyzer) use `_` for explicit arguments that can be inferred" } def getPPAnalyze (o : Options) : Bool := o.get pp.analyze.name pp.analyze.defValue def getPPAnalyzeCheckInstances (o : Options) : Bool := o.get pp.analyze.checkInstances.name pp.analyze.checkInstances.defValue def getPPAnalyzeTypeAscriptions (o : Options) : Bool := o.get pp.analyze.typeAscriptions.name pp.analyze.typeAscriptions.defValue def getPPAnalyzeTrustSubst (o : Options) : Bool := o.get pp.analyze.trustSubst.name pp.analyze.trustSubst.defValue def getPPAnalyzeTrustOfNat (o : Options) : Bool := o.get pp.analyze.trustOfNat.name pp.analyze.trustOfNat.defValue def getPPAnalyzeTrustOfScientific (o : Options) : Bool := o.get pp.analyze.trustOfScientific.name pp.analyze.trustOfScientific.defValue def getPPAnalyzeTrustId (o : Options) : Bool := o.get pp.analyze.trustId.name pp.analyze.trustId.defValue def getPPAnalyzeTrustCoe (o : Options) : Bool := o.get pp.analyze.trustCoe.name pp.analyze.trustCoe.defValue def getPPAnalyzeTrustSubtypeMk (o : Options) : Bool := o.get pp.analyze.trustSubtypeMk.name pp.analyze.trustSubtypeMk.defValue def getPPAnalyzeTrustKnownFOType2TypeHOFuns (o : Options) : Bool := o.get pp.analyze.trustKnownFOType2TypeHOFuns.name pp.analyze.trustKnownFOType2TypeHOFuns.defValue def getPPAnalyzeOmitMax (o : Options) : Bool := o.get pp.analyze.omitMax.name pp.analyze.omitMax.defValue def getPPAnalyzeKnowsType (o : Options) : Bool := o.get pp.analyze.knowsType.name pp.analyze.knowsType.defValue def getPPAnalyzeExplicitHoles (o : Options) : Bool := o.get pp.analyze.explicitHoles.name pp.analyze.explicitHoles.defValue def getPPAnalysisSkip (o : Options) : Bool := o.get `pp.analysis.skip false def getPPAnalysisHole (o : Options) : Bool := o.get `pp.analysis.hole false def getPPAnalysisNamedArg (o : Options) : Bool := o.get `pp.analysis.namedArg false def getPPAnalysisLetVarType (o : Options) : Bool := o.get `pp.analysis.letVarType false def getPPAnalysisNeedsType (o : Options) : Bool := o.get `pp.analysis.needsType false def getPPAnalysisBlockImplicit (o : Options) : Bool := o.get `pp.analysis.blockImplicit false namespace PrettyPrinter.Delaborator def returnsPi (motive : Expr) : MetaM Bool := do lambdaTelescope motive fun xs b => b.isForall def isNonConstFun (motive : Expr) : MetaM Bool := do match motive with | Expr.lam name d b _ => isNonConstFun b | _ => motive.hasLooseBVars def isSimpleHOFun (motive : Expr) : MetaM Bool := do not (← returnsPi motive) && not (← isNonConstFun motive) def isType2Type (motive : Expr) : MetaM Bool := do match ← inferType motive with | Expr.forallE _ (Expr.sort ..) (Expr.sort ..) .. => true | _ => false def isFOLike (motive : Expr) : MetaM Bool := do let f := motive.getAppFn f.isFVar || f.isConst def isIdLike (arg : Expr) : Bool := do -- TODO: allow `id` constant as well? match arg with | Expr.lam _ _ (Expr.bvar ..) .. => true | _ => false def isCoe (e : Expr) : Bool := -- TODO: `coeSort? Builtins doesn't seem to render them anyway e.isAppOfArity `coe 4 || (e.isAppOf `coeFun && e.getAppNumArgs >= 4) || e.isAppOfArity `coeSort 4 def isStructureInstance (e : Expr) : MetaM Bool := do match e.isConstructorApp? (← getEnv) with | some s => isStructure (← getEnv) s.induct | none => false namespace TopDownAnalyze partial def hasMVarAtCurrDepth (e : Expr) : MetaM Bool := do let mctx ← getMCtx Option.isSome $ e.findMVar? fun mvarId => match mctx.findDecl? mvarId with | some mdecl => mdecl.depth == mctx.depth | _ => false partial def hasLevelMVarAtCurrDepth (e : Expr) : MetaM Bool := do let mctx ← getMCtx Option.isSome $ e.findLevelMVar? fun mvarId => mctx.findLevelDepth? mvarId == some mctx.depth private def valUnknown (e : Expr) : MetaM Bool := do hasMVarAtCurrDepth (← instantiateMVars e) private def typeUnknown (e : Expr) : MetaM Bool := do valUnknown (← inferType e) def isHBinOp (e : Expr) : Bool := do -- TODO: instead of tracking these explicitly, -- consider a more general solution that checks for defaultInstances if e.getAppNumArgs != 6 then return false let f := e.getAppFn if !f.isConst then return false -- Note: we leave out `HPow.hPow because we expect its homogeneous -- version will change soon let ops := #[ `HOr.hOr, `HXor.hXor, `HAnd.hAnd, `HAppend.hAppend, `HOrElse.hOrElse, `HAndThen.hAndThen, `HAdd.hAdd, `HSub.hSub, `HMul.hMul, `HDiv.hDiv, `HMod.hMod, `HShiftLeft.hShiftLeft, `HShiftRight] ops.any fun op => op == f.constName! def replaceLPsWithVars (e : Expr) : MetaM Expr := do if !e.hasLevelParam then return e let lps := collectLevelParams {} e |>.params let mut replaceMap : Std.HashMap Name Level := {} for lp in lps do replaceMap := replaceMap.insert lp (← mkFreshLevelMVar) return e.replaceLevel fun | Level.param n .. => replaceMap.find! n | l => if !l.hasParam then some l else none def isDefEqAssigning (t s : Expr) : MetaM Bool := do withReader (fun ctx => { ctx with config := { ctx.config with assignSyntheticOpaque := true }}) $ Meta.isDefEq t s def checkpointDefEq (t s : Expr) : MetaM Bool := do Meta.checkpointDefEq (mayPostpone := false) do isDefEqAssigning t s def isHigherOrder (type : Expr) : MetaM Bool := do forallTelescopeReducing type fun xs b => xs.size > 0 && b.isSort def isFunLike (e : Expr) : MetaM Bool := do forallTelescopeReducing (← inferType e) fun xs b => xs.size > 0 def isSubstLike (e : Expr) : Bool := e.isAppOfArity `Eq.ndrec 6 || e.isAppOfArity `Eq.rec 6 def nameNotRoundtrippable (n : Name) : Bool := n.hasMacroScopes || isPrivateName n || containsNum n where containsNum | Name.str p .. => containsNum p | Name.num .. => true | Name.anonymous => false def mvarName (mvar : Expr) : MetaM Name := do (← getMVarDecl mvar.mvarId!).userName def containsBadMax : Level → Bool | Level.succ u .. => containsBadMax u | Level.max u v .. => (u.hasParam && v.hasParam) || containsBadMax u || containsBadMax v | Level.imax u v .. => (u.hasParam && v.hasParam) || containsBadMax u || containsBadMax v | _ => false open SubExpr structure Context where knowsType : Bool knowsLevel : Bool -- only constants look at this inBottomUp : Bool := false parentIsApp : Bool := false subExpr : SubExpr deriving Inhabited structure State where annotations : RBMap Pos Options compare := {} postponed : Array (Expr × Expr) := #[] -- not currently used abbrev AnalyzeM := ReaderT Context (StateRefT State MetaM) instance (priority := low) : MonadReaderOf SubExpr AnalyzeM where read := Context.subExpr <$> read instance (priority := low) : MonadWithReaderOf SubExpr AnalyzeM where withReader f x := fun ctx => x { ctx with subExpr := f ctx.subExpr } def tryUnify (e₁ e₂ : Expr) : AnalyzeM Unit := do try let r ← isDefEqAssigning e₁ e₂ if !r then modify fun s => { s with postponed := s.postponed.push (e₁, e₂) } pure () catch ex => modify fun s => { s with postponed := s.postponed.push (e₁, e₂) } partial def inspectOutParams (arg mvar : Expr) : AnalyzeM Unit := do let argType ← inferType arg -- HAdd α α α let mvarType ← inferType mvar let fType ← inferType argType.getAppFn -- Type → Type → outParam Type let mType ← inferType mvarType.getAppFn inspectAux fType mType 0 argType.getAppArgs mvarType.getAppArgs where inspectAux (fType mType : Expr) (i : Nat) (args mvars : Array Expr) := do let fType ← whnf fType let mType ← whnf mType if not (i < args.size) then return () match fType, mType with | Expr.forallE _ fd fb _, Expr.forallE _ md mb _ => do -- TODO: do I need to check (← okBottomUp? args[i] mvars[i] fuel).isSafe here? -- if so, I'll need to take a callback if isOutParam fd then tryUnify (args[i]) (mvars[i]) inspectAux (fb.instantiate1 args[i]) (mb.instantiate1 mvars[i]) (i+1) args mvars | _, _ => return () partial def isTrivialBottomUp (e : Expr) : AnalyzeM Bool := do let opts ← getOptions return e.isFVar || e.isConst || e.isMVar || e.isNatLit || e.isStringLit || e.isSort || (getPPAnalyzeTrustOfNat opts && e.isAppOfArity `OfNat.ofNat 3) || (getPPAnalyzeTrustOfScientific opts && e.isAppOfArity `OfScientific.ofScientific 5) partial def canBottomUp (e : Expr) (mvar? : Option Expr := none) (fuel : Nat := 10) : AnalyzeM Bool := do -- Here we check if `e` can be safely elaborated without its expected type. -- These are incomplete (and possibly unsound) heuristics. -- TODO: do I need to snapshot the state before calling this? match fuel with | 0 => false | fuel + 1 => if ← isTrivialBottomUp e then return true let f := e.getAppFn if !f.isConst && !f.isFVar then return false let args := e.getAppArgs let fType ← replaceLPsWithVars (← inferType e.getAppFn) let (mvars, bInfos, resultType) ← forallMetaBoundedTelescope fType e.getAppArgs.size for i in [:mvars.size] do if bInfos[i] == BinderInfo.instImplicit then inspectOutParams args[i] mvars[i] else if ← bInfos[i] == BinderInfo.default then if ← isTrivialBottomUp args[i] then tryUnify args[i] mvars[i] else if ← typeUnknown mvars[i] <&&> canBottomUp args[i] mvars[i] fuel then tryUnify args[i] mvars[i] if ← (isHBinOp e <&&> (valUnknown mvars[0] <||> valUnknown mvars[1])) then tryUnify mvars[0] mvars[1] if mvar?.isSome then tryUnify resultType (← inferType mvar?.get!) return !(← valUnknown resultType) def withKnowing (knowsType knowsLevel : Bool) (x : AnalyzeM α) : AnalyzeM α := do withReader (fun ctx => { ctx with knowsType := knowsType, knowsLevel := knowsLevel }) x builtin_initialize analyzeFailureId : InternalExceptionId ← registerInternalExceptionId `analyzeFailure def checkKnowsType : AnalyzeM Unit := do if not (← read).knowsType then throw $ Exception.internal analyzeFailureId def annotateBoolAt (n : Name) (pos : Pos) : AnalyzeM Unit := do let opts := (← get).annotations.findD pos {} |>.setBool n true trace[pp.analyze.annotate] "{pos} {n}" modify fun s => { s with annotations := s.annotations.insert pos opts } def annotateBool (n : Name) : AnalyzeM Unit := do annotateBoolAt n (← getPos) structure App.Context where f : Expr fType : Expr args : Array Expr mvars : Array Expr bInfos : Array BinderInfo forceRegularApp : Bool structure App.State where bottomUps : Array Bool higherOrders : Array Bool funBinders : Array Bool provideds : Array Bool namedArgs : Array Name := #[] abbrev AnalyzeAppM := ReaderT App.Context (StateT App.State AnalyzeM) mutual partial def analyze (parentIsApp : Bool := false) : AnalyzeM Unit := do checkMaxHeartbeats "Delaborator.topDownAnalyze" trace[pp.analyze] "{(← read).knowsType}.{(← read).knowsLevel}" let e ← getExpr let opts ← getOptions if ← !e.isAtomic <&&> !(getPPProofs opts) <&&> (try Meta.isProof e catch ex => false) then if getPPProofsWithType opts then withType $ withKnowing true true $ analyze return () else withReader (fun ctx => { ctx with parentIsApp := parentIsApp }) do match (← getExpr) with | Expr.app .. => analyzeApp | Expr.forallE .. => analyzePi | Expr.lam .. => analyzeLam | Expr.const .. => analyzeConst | Expr.sort .. => analyzeSort | Expr.proj .. => analyzeProj | Expr.fvar .. => analyzeFVar | Expr.mdata .. => analyzeMData | Expr.letE .. => analyzeLet | Expr.lit .. => pure () | Expr.mvar .. => pure () | Expr.bvar .. => pure () where analyzeApp := do let mut willKnowType := (← read).knowsType if !(← read).knowsType && !(← canBottomUp (← getExpr)) then annotateBool `pp.analysis.needsType withType $ withKnowing true false $ analyze willKnowType := true else if ← (!(← read).knowsType <||> (← read).inBottomUp) <&&> isStructureInstance (← getExpr) then withType do annotateBool `pp.structureInstanceTypes withKnowing true false $ analyze willKnowType := true withKnowing willKnowType true $ analyzeAppStaged (← getExpr).getAppFn (← getExpr).getAppArgs analyzeAppStaged (f : Expr) (args : Array Expr) : AnalyzeM Unit := do let fType ← replaceLPsWithVars (← inferType f) let (mvars, bInfos, resultType) ← forallMetaBoundedTelescope fType args.size let rest := args.extract mvars.size args.size let args := args.shrink mvars.size -- Unify with the expected type if (← read).knowsType then tryUnify (← inferType (mkAppN f args)) resultType let forceRegularApp : Bool := (getPPAnalyzeTrustSubst (← getOptions) && isSubstLike (← getExpr)) || (getPPAnalyzeTrustCoe (← getOptions) && isCoe (← getExpr)) || (getPPAnalyzeTrustSubtypeMk (← getOptions) && (← getExpr).isAppOfArity `Subtype.mk 4) analyzeAppStagedCore { f, fType, args, mvars, bInfos, forceRegularApp } |>.run' { bottomUps := mkArray args.size false, higherOrders := mkArray args.size false, provideds := mkArray args.size false, funBinders := mkArray args.size false } if not rest.isEmpty then -- Note: this shouldn't happen for type-correct terms if !args.isEmpty then analyzeAppStaged (mkAppN f args) rest maybeAddBlockImplicit : AnalyzeM Unit := do -- See `MonadLift.noConfusion for an example where this is necessary. if !(← read).parentIsApp then let type ← inferType (← getExpr) if type.isForall && type.bindingInfo! == BinderInfo.implicit then annotateBool `pp.analysis.blockImplicit analyzeConst : AnalyzeM Unit := do let Expr.const n ls .. ← getExpr | unreachable! if !(← read).knowsLevel && !ls.isEmpty then -- TODO: this is a very crude heuristic, motivated by https://github.com/leanprover/lean4/issues/590 unless getPPAnalyzeOmitMax (← getOptions) && ls.any containsBadMax do annotateBool `pp.universes maybeAddBlockImplicit analyzePi : AnalyzeM Unit := do withBindingDomain $ withKnowing true false analyze withBindingBody Name.anonymous analyze analyzeLam : AnalyzeM Unit := do if !(← read).knowsType then annotateBool `pp.funBinderTypes withBindingDomain $ withKnowing true false analyze withBindingBody Name.anonymous analyze analyzeLet : AnalyzeM Unit := do let Expr.letE n t v body .. ← getExpr | unreachable! if !(← canBottomUp v) then annotateBool `pp.analysis.letVarType withLetVarType $ withKnowing true false analyze withLetValue $ withKnowing true true analyze else withReader (fun ctx => { ctx with inBottomUp := true }) do withLetValue $ withKnowing true true analyze withLetBody analyze analyzeSort : AnalyzeM Unit := pure () analyzeProj : AnalyzeM Unit := withProj analyze analyzeFVar : AnalyzeM Unit := maybeAddBlockImplicit analyzeMData : AnalyzeM Unit := withMDataExpr analyze partial def analyzeAppStagedCore : AnalyzeAppM Unit := do collectBottomUps checkOutParams collectHigherOrders hBinOpHeuristic collectTrivialBottomUps discard <| processPostponed (mayPostpone := true) applyFunBinderHeuristic analyzeFn for i in [:(← read).args.size] do analyzeArg i maybeSetExplicit where collectBottomUps := do let { args, mvars, bInfos, ..} ← read for target in [fun _ => none, fun i => some mvars[i]] do for i in [:args.size] do if bInfos[i] == BinderInfo.default then if ← typeUnknown mvars[i] <&&> canBottomUp args[i] (target i) then tryUnify args[i] mvars[i] modify fun s => { s with bottomUps := s.bottomUps.set! i true } checkOutParams := do let { args, mvars, bInfos, ..} ← read for i in [:args.size] do if bInfos[i] == BinderInfo.instImplicit then inspectOutParams args[i] mvars[i] collectHigherOrders := do let { args, mvars, bInfos, ..} ← read for i in [:args.size] do if not (bInfos[i] == BinderInfo.implicit || bInfos[i] == BinderInfo.strictImplicit) then continue if not (← isHigherOrder (← inferType args[i])) then continue if getPPAnalyzeTrustId (← getOptions) && isIdLike args[i] then continue if getPPAnalyzeTrustKnownFOType2TypeHOFuns (← getOptions) && not (← valUnknown mvars[i]) && (← isType2Type (args[i])) && (← isFOLike (args[i])) then continue tryUnify args[i] mvars[i] modify fun s => { s with higherOrders := s.higherOrders.set! i true } hBinOpHeuristic := do let { args, mvars, bInfos, ..} ← read if ← (isHBinOp (← getExpr) <&&> (valUnknown mvars[0] <||> valUnknown mvars[1])) then tryUnify mvars[0] mvars[1] collectTrivialBottomUps := do -- motivation: prevent levels from printing in -- Boo.mk : {α : Type u_1} → {β : Type u_2} → α → β → Boo.{u_1, u_2} α β let { args, mvars, bInfos, ..} ← read for i in [:args.size] do if bInfos[i] == BinderInfo.default then if ← valUnknown mvars[i] <&&> isTrivialBottomUp args[i] then tryUnify args[i] mvars[i] modify fun s => { s with bottomUps := s.bottomUps.set! i true } applyFunBinderHeuristic := do let { f, args, mvars, bInfos, .. } ← read let rec core (argIdx : Nat) (mvarType : Expr) : AnalyzeAppM Bool := do match ← getExpr, mvarType with | Expr.lam .., Expr.forallE n t b .. => let mut annotated := false for i in [:argIdx] do if ← bInfos[i] == BinderInfo.implicit <&&> valUnknown mvars[i] <&&> withNewMCtxDepth (checkpointDefEq t mvars[i]) then annotateBool `pp.funBinderTypes tryUnify args[i] mvars[i] -- Note: currently we always analyze the lambda binding domains in `analyzeLam` -- (so we don't need to analyze it again here) annotated := true break let annotatedBody ← withBindingBody Name.anonymous (core argIdx b) return annotated || annotatedBody | _, _ => return false for i in [:args.size] do if ← bInfos[i] == BinderInfo.default then let b ← withNaryArg i (core i (← inferType mvars[i])) if b then modify fun s => { s with funBinders := s.funBinders.set! i true } analyzeFn := do -- Now, if this is the first staging, analyze the n-ary function without expected type let {f, fType, forceRegularApp ..} ← read if !f.isApp then withKnowing false (forceRegularApp || !(← hasLevelMVarAtCurrDepth (← instantiateMVars fType))) $ withNaryFn (analyze (parentIsApp := true)) annotateNamedArg (n : Name) : AnalyzeAppM Unit := do annotateBool `pp.analysis.namedArg modify fun s => { s with namedArgs := s.namedArgs.push n } analyzeArg (i : Nat) := do let { f, args, mvars, bInfos, forceRegularApp ..} ← read let { bottomUps, higherOrders, funBinders, ..} ← get let arg := args[i] let argType ← inferType arg let processNaturalImplicit : AnalyzeAppM Unit := do if (← valUnknown mvars[i] <||> higherOrders[i]) && !forceRegularApp then annotateNamedArg (← mvarName mvars[i]) modify fun s => { s with provideds := s.provideds.set! i true } else annotateBool `pp.analysis.skip withNaryArg (f.getAppNumArgs + i) do withTheReader Context (fun ctx => { ctx with inBottomUp := ctx.inBottomUp || bottomUps[i] }) do match bInfos[i] with | BinderInfo.default => if ← getPPAnalyzeExplicitHoles (← getOptions) <&&> !(← valUnknown mvars[i]) <&&> !(← readThe Context).inBottomUp <&&> !(← isFunLike arg) <&&> !funBinders[i] <&&> checkpointDefEq mvars[i] arg then annotateBool `pp.analysis.hole else modify fun s => { s with provideds := s.provideds.set! i true } | BinderInfo.implicit => processNaturalImplicit | BinderInfo.strictImplicit => processNaturalImplicit | BinderInfo.instImplicit => -- Note: apparently checking valUnknown here is not sound, because the elaborator -- will not happily assign instImplicits that it cannot synthesize let mut provided := true if !getPPInstances (← getOptions) then annotateBool `pp.analysis.skip provided := false else if getPPAnalyzeCheckInstances (← getOptions) then let instResult ← try trySynthInstance argType catch _ => LOption.undef match instResult with | LOption.some inst => if ← checkpointDefEq inst arg then annotateBool `pp.analysis.skip; provided := false else annotateNamedArg (← mvarName mvars[i]) | _ => annotateNamedArg (← mvarName mvars[i]) else annotateBool `pp.analysis.skip; provided := false modify fun s => { s with provideds := s.provideds.set! i provided } | BinderInfo.auxDecl => pure () if (← get).provideds[i] then withKnowing (not (← typeUnknown mvars[i])) true analyze tryUnify mvars[i] args[i] maybeSetExplicit := do let { f, args, mvars, bInfos, forceRegularApp, ..} ← read if (← get).namedArgs.any nameNotRoundtrippable then annotateBool `pp.explicit for i in [:args.size] do if !(← get).provideds[i] then withNaryArg (f.getAppNumArgs + i) do annotateBool `pp.analysis.hole if bInfos[i] == BinderInfo.instImplicit && getPPInstanceTypes (← getOptions) then withType (withKnowing true false analyze) end end TopDownAnalyze open TopDownAnalyze SubExpr def topDownAnalyze (e : Expr) : MetaM OptionsPerPos := do let s₀ ← get traceCtx `pp.analyze do withReader (fun ctx => { ctx with config := Lean.Elab.Term.setElabConfig ctx.config }) do let ϕ : AnalyzeM OptionsPerPos := do withNewMCtxDepth analyze; (← get).annotations try let knowsType := getPPAnalyzeKnowsType (← getOptions) ϕ { knowsType := knowsType, knowsLevel := knowsType, subExpr := mkRoot e } |>.run' { : TopDownAnalyze.State } catch ex => trace[pp.analyze.error] "failed" pure {} finally set s₀ builtin_initialize registerTraceClass `pp.analyze registerTraceClass `pp.analyze.annotate registerTraceClass `pp.analyze.tryUnify registerTraceClass `pp.analyze.error end Lean.PrettyPrinter.Delaborator
6ae10d639001372d171fd26179397028e05d7fd1
491068d2ad28831e7dade8d6dff871c3e49d9431
/tests/lean/run/mul_zero.lean
57558b74b47c2cef304cabb78bad1a898696f2bd
[ "Apache-2.0" ]
permissive
davidmueller13/lean
65a3ed141b4088cd0a268e4de80eb6778b21a0e9
c626e2e3c6f3771e07c32e82ee5b9e030de5b050
refs/heads/master
1,611,278,313,401
1,444,021,177,000
1,444,021,177,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
85
lean
import data.nat open nat example (x : ℕ) : 0 = x * 0 := calc 0 = x * 0 : mul_zero
5c5efc7169e4805ad56a21f2b8c908ae9404b0d1
4fa161becb8ce7378a709f5992a594764699e268
/src/algebra/field_power.lean
f23da7af8c9fe51161e3de48d2f72664cdb9748a
[ "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
5,540
lean
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis Integer power operation on fields. -/ import algebra.group_with_zero_power import tactic.linarith universe u @[simp] lemma ring_hom.map_fpow {K L : Type*} [division_ring K] [division_ring L] (f : K →+* L) (a : K) : ∀ (n : ℤ), f (a ^ n) = f a ^ n | (n : ℕ) := f.map_pow a n | -[1+n] := by simp only [fpow_neg_succ_of_nat, f.map_pow, f.map_inv, f.map_one] namespace is_ring_hom lemma map_fpow {K L : Type*} [division_ring K] [division_ring L] (f : K → L) [is_ring_hom f] (a : K) : ∀ (n : ℤ), f (a ^ n) = f a ^ n := (ring_hom.of f).map_fpow a end is_ring_hom section ordered_field_power open int variables {K : Type u} [discrete_linear_ordered_field K] lemma fpow_nonneg_of_nonneg {a : K} (ha : 0 ≤ a) : ∀ (z : ℤ), 0 ≤ a ^ z | (of_nat n) := pow_nonneg ha _ | -[1+n] := inv_nonneg.2 $ pow_nonneg ha _ lemma fpow_pos_of_pos {a : K} (ha : 0 < a) : ∀ (z : ℤ), 0 < a ^ z | (of_nat n) := pow_pos ha _ | -[1+n] := inv_pos.2 $ pow_pos ha _ lemma fpow_le_of_le {x : K} (hx : 1 ≤ x) {a b : ℤ} (h : a ≤ b) : x ^ a ≤ x ^ b := begin induction a with a a; induction b with b b, { simp only [fpow_of_nat, of_nat_eq_coe], apply pow_le_pow hx, apply le_of_coe_nat_le_coe_nat h }, { apply absurd h, apply not_le_of_gt, exact lt_of_lt_of_le (neg_succ_lt_zero _) (of_nat_nonneg _) }, { simp only [fpow_neg_succ_of_nat, one_div_eq_inv], apply le_trans (inv_le_one _); apply one_le_pow_of_one_le hx }, { simp only [fpow_neg_succ_of_nat], apply (inv_le_inv _ _).2, { apply pow_le_pow hx, have : -(↑(a+1) : ℤ) ≤ -(↑(b+1) : ℤ), from h, have h' := le_of_neg_le_neg this, apply le_of_coe_nat_le_coe_nat h' }, repeat { apply pow_pos (lt_of_lt_of_le zero_lt_one hx) } } end lemma pow_le_max_of_min_le {x : K} (hx : 1 ≤ x) {a b c : ℤ} (h : min a b ≤ c) : x ^ (-c) ≤ max (x ^ (-a)) (x ^ (-b)) := begin wlog hle : a ≤ b, have hnle : -b ≤ -a, from neg_le_neg hle, have hfle : x ^ (-b) ≤ x ^ (-a), from fpow_le_of_le hx hnle, have : x ^ (-c) ≤ x ^ (-a), { apply fpow_le_of_le hx, simpa only [min_eq_left hle, neg_le_neg_iff] using h }, simpa only [max_eq_left hfle] end lemma fpow_le_one_of_nonpos {p : K} (hp : 1 ≤ p) {z : ℤ} (hz : z ≤ 0) : p ^ z ≤ 1 := calc p ^ z ≤ p ^ 0 : fpow_le_of_le hp hz ... = 1 : by simp lemma one_le_fpow_of_nonneg {p : K} (hp : 1 ≤ p) {z : ℤ} (hz : 0 ≤ z) : 1 ≤ p ^ z := calc p ^ z ≥ p ^ 0 : fpow_le_of_le hp hz ... = 1 : by simp end ordered_field_power lemma one_lt_pow {K} [linear_ordered_semiring K] {p : K} (hp : 1 < p) : ∀ {n : ℕ}, 1 ≤ n → 1 < p ^ n | 1 h := by simp; assumption | (k+2) h := begin rw ←one_mul (1 : K), apply mul_lt_mul, { assumption }, { apply le_of_lt, simpa using one_lt_pow (nat.le_add_left 1 k)}, { apply zero_lt_one }, { apply le_of_lt (lt_trans zero_lt_one hp) } end lemma one_lt_fpow {K} [discrete_linear_ordered_field K] {p : K} (hp : 1 < p) : ∀ z : ℤ, 0 < z → 1 < p ^ z | (int.of_nat n) h := one_lt_pow hp (nat.succ_le_of_lt (int.lt_of_coe_nat_lt_coe_nat h)) section ordered variables {K : Type*} [discrete_linear_ordered_field K] lemma nat.fpow_pos_of_pos {p : ℕ} (h : 0 < p) (n:ℤ) : 0 < (p:K)^n := by { apply fpow_pos_of_pos, exact_mod_cast h } lemma nat.fpow_ne_zero_of_pos {p : ℕ} (h : 0 < p) (n:ℤ) : (p:K)^n ≠ 0 := ne_of_gt (nat.fpow_pos_of_pos h n) lemma fpow_strict_mono {x : K} (hx : 1 < x) : strict_mono (λ n:ℤ, x ^ n) := λ m n h, show x ^ m < x ^ n, begin have xpos : 0 < x := by linarith, have h₀ : x ≠ 0 := by linarith, have hxm : 0 < x^m := fpow_pos_of_pos xpos m, have hxm₀ : x^m ≠ 0 := ne_of_gt hxm, suffices : 1 < x^(n-m), { replace := mul_lt_mul_of_pos_right this hxm, simp [sub_eq_add_neg] at this, simpa [*, fpow_add, mul_assoc, fpow_neg, inv_mul_cancel], }, apply one_lt_fpow hx, linarith, end @[simp] lemma fpow_lt_iff_lt {x : K} (hx : 1 < x) {m n : ℤ} : x ^ m < x ^ n ↔ m < n := (fpow_strict_mono hx).lt_iff_lt @[simp] lemma fpow_le_iff_le {x : K} (hx : 1 < x) {m n : ℤ} : x ^ m ≤ x ^ n ↔ m ≤ n := (fpow_strict_mono hx).le_iff_le @[simp] lemma pos_div_pow_pos {a b : K} (ha : 0 < a) (hb : 0 < b) (k : ℕ) : 0 < a/b^k := div_pos ha (pow_pos hb k) @[simp] lemma div_pow_le {a b : K} (ha : 0 < a) (hb : 1 ≤ b) (k : ℕ) : a/b^k ≤ a := (div_le_iff $ pow_pos (lt_of_lt_of_le zero_lt_one hb) k).mpr (calc a = a * 1 : (mul_one a).symm ... ≤ a*b^k : (mul_le_mul_left ha).mpr $ one_le_pow_of_one_le hb _) lemma fpow_injective {x : K} (h₀ : 0 < x) (h₁ : x ≠ 1) : function.injective ((^) x : ℤ → K) := begin intros m n h, rcases lt_trichotomy x 1 with H|rfl|H, { apply (fpow_strict_mono (one_lt_inv h₀ H)).injective, show x⁻¹ ^ m = x⁻¹ ^ n, rw [← fpow_neg_one, ← fpow_mul, ← fpow_mul, mul_comm _ m, mul_comm _ n, fpow_mul, fpow_mul, h], }, { contradiction }, { exact (fpow_strict_mono H).injective h, }, end @[simp] lemma fpow_inj {x : K} (h₀ : 0 < x) (h₁ : x ≠ 1) {m n : ℤ} : x ^ m = x ^ n ↔ m = n := (fpow_injective h₀ h₁).eq_iff end ordered section variables {K : Type*} [field K] @[simp, norm_cast] theorem rat.cast_fpow [char_zero K] (q : ℚ) (n : ℤ) : ((q ^ n : ℚ) : K) = q ^ n := (rat.cast_hom K).map_fpow q n end
feeaad1e0dc49aa3573b9dc48a103fa6c0ce789e
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/category_theory/sites/cover_lifting.lean
a8b08d32fd6783764748836c8f7b1226d9717595
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
13,244
lean
/- Copyright (c) 2021 Andrew Yang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andrew Yang -/ import category_theory.sites.sheaf import category_theory.limits.kan_extension import category_theory.sites.cover_preserving /-! # Cover-lifting functors between sites. We define cover-lifting functors between sites as functors that pull covering sieves back to covering sieves. This concept is also known as *cocontinuous functors* or *cover-reflecting functors*, but we have chosen this name following [MM92] in order to avoid potential naming collision or confusion with the general definition of cocontinuous functors between categories as functors preserving small colimits. The definition given here seems stronger than the definition found elsewhere, but they are actually equivalent via `category_theory.grothendieck_topology.superset_covering`. (The precise statement is not formalized, but follows from it quite trivially). ## Main definitions * `category_theory.sites.cover_lifting`: a functor between sites is cover-lifting if it pulls back covering sieves to covering sieves * `category_theory.sites.copullback`: A cover-lifting functor `G : (C, J) ⥤ (D, K)` induces a morphism of sites in the same direction as the functor. ## Main results * `category_theory.sites.Ran_is_sheaf_of_cover_lifting`: If `G : C ⥤ D` is cover_lifting, then `Ran G.op` (`ₚu`) as a functor `(Cᵒᵖ ⥤ A) ⥤ (Dᵒᵖ ⥤ A)` of presheaves maps sheaves to sheaves. * `category_theory.pullback_copullback_adjunction`: If `G : (C, J) ⥤ (D, K)` is cover-lifting, cover-preserving, and compatible-preserving, then `pullback G` and `copullback G` are adjoint. ## References * [Elephant]: *Sketches of an Elephant*, P. T. Johnstone: C2.3. * [S. MacLane, I. Moerdijk, *Sheaves in Geometry and Logic*][MM92] * https://stacks.math.columbia.edu/tag/00XI -/ universes w v v₁ v₂ v₃ u u₁ u₂ u₃ noncomputable theory open category_theory open opposite open category_theory.presieve.family_of_elements open category_theory.presieve open category_theory.limits namespace category_theory section cover_lifting variables {C : Type*} [category C] {D : Type*} [category D] {E : Type*} [category E] variables (J : grothendieck_topology C) (K : grothendieck_topology D) variables {L : grothendieck_topology E} /-- A functor `G : (C, J) ⥤ (D, K)` between sites is called to have the cover-lifting property if for all covering sieves `R` in `D`, `R.pullback G` is a covering sieve in `C`. -/ @[nolint has_inhabited_instance] structure cover_lifting (G : C ⥤ D) : Prop := (cover_lift : ∀ {U : C} {S : sieve (G.obj U)} (hS : S ∈ K (G.obj U)), S.functor_pullback G ∈ J U) /-- The identity functor on a site is cover-lifting. -/ lemma id_cover_lifting : cover_lifting J J (𝟭 _) := ⟨λ _ _ h, by simpa using h⟩ variables {J K} /-- The composition of two cover-lifting functors are cover-lifting -/ lemma comp_cover_lifting {F : C ⥤ D} (hu : cover_lifting J K F) {G : D ⥤ E} (hv : cover_lifting K L G) : cover_lifting J L (F ⋙ G) := ⟨λ _ S h, hu.cover_lift (hv.cover_lift h)⟩ end cover_lifting /-! We will now prove that `Ran G.op` (`ₚu`) maps sheaves to sheaves if `G` is cover-lifting. This can be found in https://stacks.math.columbia.edu/tag/00XK. However, the proof given here uses the amalgamation definition of sheaves, and thus does not require that `C` or `D` has categorical pullbacks. For the following proof sketch, `⊆` denotes the homs on `C` and `D` as in the topological analogy. By definition, the presheaf `𝒢 : Dᵒᵖ ⥤ A` is a sheaf if for every sieve `S` of `U : D`, and every compatible family of morphisms `X ⟶ 𝒢(V)` for each `V ⊆ U : S` with a fixed source `X`, we can glue them into a morphism `X ⟶ 𝒢(U)`. Since the presheaf `𝒢 := (Ran G.op).obj ℱ.val` is defined via `𝒢(U) = lim_{G(V) ⊆ U} ℱ(V)`, for gluing the family `x` into a `X ⟶ 𝒢(U)`, it suffices to provide a `X ⟶ ℱ(Y)` for each `G(Y) ⊆ U`. This can be done since `{ Y' ⊆ Y : G(Y') ⊆ U ∈ S}` is a covering sieve for `Y` on `C` (by the cover-lifting property of `G`). Thus the morphisms `X ⟶ 𝒢(G(Y')) ⟶ ℱ(Y')` can be glued into a morphism `X ⟶ ℱ(Y)`. This is done in `get_sections`. In `glued_limit_cone`, we verify these obtained sections are indeed compatible, and thus we obtain A `X ⟶ 𝒢(U)`. The remaining work is to verify that this is indeed the amalgamation and is unique. -/ variables {C D : Type u} [category.{v} C] [category.{v} D] variables {A : Type w} [category.{max u v} A] [has_limits A] variables {J : grothendieck_topology C} {K : grothendieck_topology D} namespace Ran_is_sheaf_of_cover_lifting variables {G : C ⥤ D} (hu : cover_lifting J K G) (ℱ : Sheaf J A) variables {X : A} {U : D} (S : sieve U) (hS : S ∈ K U) instance (X : Dᵒᵖ) : has_limits_of_shape (structured_arrow X G.op) A := begin haveI := limits.has_limits_of_size_shrink.{v (max u v) (max u v) (max u v)} A, exact has_limits_of_size.has_limits_of_shape _ end variables (x : S.arrows.family_of_elements ((Ran G.op).obj ℱ.val ⋙ coyoneda.obj (op X))) variables (hx : x.compatible) /-- The family of morphisms `X ⟶ 𝒢(G(Y')) ⟶ ℱ(Y')` defined on `{ Y' ⊆ Y : G(Y') ⊆ U ∈ S}`. -/ def pulledback_family (Y : structured_arrow (op U) G.op) := (((x.pullback Y.hom.unop).functor_pullback G).comp_presheaf_map (show _ ⟶ _, from whisker_right ((Ran.adjunction A G.op).counit.app ℱ.val) (coyoneda.obj (op X)))) @[simp] lemma pulledback_family_apply (Y : structured_arrow (op U) G.op) {W} {f : W ⟶ _} (Hf) : pulledback_family ℱ S x Y f Hf = x (G.map f ≫ Y.hom.unop) Hf ≫ ((Ran.adjunction A G.op).counit.app ℱ.val).app (op W) := rfl variables {x} {S} include hu hS hx /-- Given a `G(Y) ⊆ U`, we can find a unique section `X ⟶ ℱ(Y)` that agrees with `x`. -/ def get_section (Y : structured_arrow (op U) G.op) : X ⟶ ℱ.val.obj Y.right := begin let hom_sh := whisker_right ((Ran.adjunction A G.op).counit.app ℱ.val) (coyoneda.obj (op X)), have S' := (K.pullback_stable Y.hom.unop hS), have hs' := ((hx.pullback Y.3.unop).functor_pullback G).comp_presheaf_map hom_sh, exact (ℱ.2 X _ (hu.cover_lift S')).amalgamate _ hs' end lemma get_section_is_amalgamation (Y : structured_arrow (op U) G.op) : (pulledback_family ℱ S x Y).is_amalgamation (get_section hu ℱ hS hx Y) := is_sheaf_for.is_amalgamation _ _ lemma get_section_is_unique (Y : structured_arrow (op U) G.op) {y} (H : (pulledback_family ℱ S x Y).is_amalgamation y) : y = get_section hu ℱ hS hx Y := begin apply is_sheaf_for.is_separated_for _ (pulledback_family ℱ S x Y), { exact H }, { apply get_section_is_amalgamation }, { exact ℱ.2 X _ (hu.cover_lift (K.pullback_stable Y.hom.unop hS)) } end @[simp] lemma get_section_commute {Y Z : structured_arrow (op U) G.op} (f : Y ⟶ Z) : get_section hu ℱ hS hx Y ≫ ℱ.val.map f.right = get_section hu ℱ hS hx Z := begin apply get_section_is_unique, intros V' fV' hV', have eq : Z.hom = Y.hom ≫ (G.map f.right.unop).op, { convert f.w, erw category.id_comp }, rw eq at hV', convert get_section_is_amalgamation hu ℱ hS hx Y (fV' ≫ f.right.unop) _ using 1, { tidy }, { simp only [eq, quiver.hom.unop_op, pulledback_family_apply, functor.map_comp, unop_comp, category.assoc] }, { change S (G.map _ ≫ Y.hom.unop), simpa only [functor.map_comp, category.assoc] using hV' } end /-- The limit cone in order to glue the sections obtained via `get_section`. -/ def glued_limit_cone : limits.cone (Ran.diagram G.op ℱ.val (op U)) := { X := X, π := { app := λ Y, get_section hu ℱ hS hx Y, naturality' := λ Y Z f, by tidy } } @[simp] lemma glued_limit_cone_π_app (W) : (glued_limit_cone hu ℱ hS hx).π.app W = get_section hu ℱ hS hx W := rfl /-- The section obtained by passing `glued_limit_cone` into `category_theory.limits.limit.lift`. -/ def glued_section : X ⟶ ((Ran G.op).obj ℱ.val).obj (op U) := limit.lift _ (glued_limit_cone hu ℱ hS hx) /-- A helper lemma for the following two lemmas. Basically stating that if the section `y : X ⟶ 𝒢(V)` coincides with `x` on `G(V')` for all `G(V') ⊆ V ∈ S`, then `X ⟶ 𝒢(V) ⟶ ℱ(W)` is indeed the section obtained in `get_sections`. That said, this is littered with some more categorical jargon in order to be applied in the following lemmas easier. -/ lemma helper {V} (f : V ⟶ U) (y : X ⟶ ((Ran G.op).obj ℱ.val).obj (op V)) (W) (H : ∀ {V'} {fV : G.obj V' ⟶ V} (hV), y ≫ ((Ran G.op).obj ℱ.val).map fV.op = x (fV ≫ f) hV) : y ≫ limit.π (Ran.diagram G.op ℱ.val (op V)) W = (glued_limit_cone hu ℱ hS hx).π.app ((structured_arrow.map f.op).obj W) := begin dsimp only [glued_limit_cone_π_app], apply get_section_is_unique hu ℱ hS hx ((structured_arrow.map f.op).obj W), intros V' fV' hV', dsimp only [Ran.adjunction, Ran.equiv, pulledback_family_apply], erw [adjunction.adjunction_of_equiv_right_counit_app], have : y ≫ ((Ran G.op).obj ℱ.val).map (G.map fV' ≫ W.hom.unop).op = x (G.map fV' ≫ W.hom.unop ≫ f) (by simpa only using hV'), { convert H (show S ((G.map fV' ≫ W.hom.unop) ≫ f), by simpa only [category.assoc] using hV') using 2, simp only [category.assoc] }, simp only [quiver.hom.unop_op, equiv.symm_symm, structured_arrow.map_obj_hom, unop_comp, equiv.coe_fn_mk, functor.comp_map, coyoneda_obj_map, category.assoc, ← this, op_comp, Ran_obj_map, nat_trans.id_app], erw [category.id_comp, limit.pre_π], congr, convert limit.w (Ran.diagram G.op ℱ.val (op V)) (structured_arrow.hom_mk' W fV'.op), rw structured_arrow.map_mk, erw category.comp_id, simp only [quiver.hom.unop_op, functor.op_map, quiver.hom.op_unop] end /-- Verify that the `glued_section` is an amalgamation of `x`. -/ lemma glued_section_is_amalgamation : x.is_amalgamation (glued_section hu ℱ hS hx) := begin intros V fV hV, ext W, simp only [functor.comp_map, limit.lift_pre, coyoneda_obj_map, Ran_obj_map, glued_section], erw limit.lift_π, symmetry, convert helper hu ℱ hS hx _ (x fV hV) _ _ using 1, intros V' fV' hV', convert hx (fV') (𝟙 _) hV hV' (by rw category.id_comp), simp only [op_id, functor_to_types.map_id_apply] end /-- Verify that the amalgamation is indeed unique. -/ lemma glued_section_is_unique (y) (hy: x.is_amalgamation y) : y = glued_section hu ℱ hS hx := begin unfold glued_section limit.lift, ext W, erw limit.lift_π, convert helper hu ℱ hS hx (𝟙 _) y W _, { simp only [op_id, structured_arrow.map_id] }, { intros V' fV' hV', convert hy fV' (by simpa only [category.comp_id] using hV'), erw category.comp_id } end end Ran_is_sheaf_of_cover_lifting /-- If `G` is cover_lifting, then `Ran G.op` pushes sheaves to sheaves. This result is basically https://stacks.math.columbia.edu/tag/00XK, but without the condition that `C` or `D` has pullbacks. -/ theorem Ran_is_sheaf_of_cover_lifting {G : C ⥤ D} (hG : cover_lifting J K G) (ℱ : Sheaf J A) : presheaf.is_sheaf K ((Ran G.op).obj ℱ.val) := begin intros X U S hS x hx, split, swap, { apply Ran_is_sheaf_of_cover_lifting.glued_section hG ℱ hS hx }, split, { apply Ran_is_sheaf_of_cover_lifting.glued_section_is_amalgamation }, { apply Ran_is_sheaf_of_cover_lifting.glued_section_is_unique } end variable (A) /-- A cover-lifting functor induces a morphism of sites in the same direction as the functor. -/ def sites.copullback {G : C ⥤ D} (hG : cover_lifting J K G) : Sheaf J A ⥤ Sheaf K A := { obj := λ ℱ, ⟨(Ran G.op).obj ℱ.val, Ran_is_sheaf_of_cover_lifting hG ℱ⟩, map := λ _ _ f, ⟨(Ran G.op).map f.val⟩, map_id' := λ ℱ, Sheaf.hom.ext _ _ $ (Ran G.op).map_id ℱ.val, map_comp' := λ _ _ _ f g, Sheaf.hom.ext _ _ $ (Ran G.op).map_comp f.val g.val } /-- Given a functor between sites that is cover-preserving, cover-lifting, and compatible-preserving, the pullback and copullback along `G` are adjoint to each other -/ @[simps unit_app_val counit_app_val] noncomputable def sites.pullback_copullback_adjunction {G : C ⥤ D} (Hp : cover_preserving J K G) (Hl : cover_lifting J K G) (Hc : compatible_preserving K G) : sites.pullback A Hc Hp ⊣ sites.copullback A Hl := { hom_equiv := λ X Y, { to_fun := λ f, ⟨(Ran.adjunction A G.op).hom_equiv X.val Y.val f.val⟩, inv_fun := λ f, ⟨((Ran.adjunction A G.op).hom_equiv X.val Y.val).symm f.val⟩, left_inv := λ f, by { ext1, dsimp, rw [equiv.symm_apply_apply] }, right_inv := λ f, by { ext1, dsimp, rw [equiv.apply_symm_apply] } }, unit := { app := λ X, ⟨(Ran.adjunction A G.op).unit.app X.val⟩, naturality' := λ _ _ f, Sheaf.hom.ext _ _ $ (Ran.adjunction A G.op).unit.naturality f.val }, counit := { app := λ X, ⟨(Ran.adjunction A G.op).counit.app X.val⟩, naturality' := λ _ _ f, Sheaf.hom.ext _ _ $ (Ran.adjunction A G.op).counit.naturality f.val }, hom_equiv_unit' := λ X Y f, by { ext1, apply (Ran.adjunction A G.op).hom_equiv_unit }, hom_equiv_counit' := λ X Y f, by { ext1, apply (Ran.adjunction A G.op).hom_equiv_counit } } end category_theory
64e04ccd55754897f0aca82b67c8647c2ad13f84
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/local_extr.lean
4abf12035b58cc68b8912313039886783657e662
[ "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,920
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import order.filter.extr import topology.continuous_on /-! # Local extrema of functions on topological spaces > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. ## Main definitions This file defines special versions of `is_*_filter f a l`, `*=min/max/extr`, from `order/filter/extr` for two kinds of filters: `nhds_within` and `nhds`. These versions are called `is_local_*_on` and `is_local_*`, respectively. ## Main statements Many lemmas in this file restate those from `order/filter/extr`, and you can find a detailed documentation there. These convenience lemmas are provided only to make the dot notation return propositions of expected types, not just `is_*_filter`. Here is the list of statements specific to these two types of filters: * `is_local_*.on`, `is_local_*_on.on_subset`: restrict to a subset; * `is_local_*_on.inter` : intersect the set with another one; * `is_*_on.localize` : a global extremum is a local extremum too. * `is_[local_]*_on.is_local_*` : if we have `is_local_*_on f s a` and `s ∈ 𝓝 a`, then we have `is_local_* f a`. -/ universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} [topological_space α] open set filter open_locale topology filter section preorder variables [preorder β] [preorder γ] (f : α → β) (s : set α) (a : α) /-- `is_local_min_on f s a` means that `f a ≤ f x` for all `x ∈ s` in some neighborhood of `a`. -/ def is_local_min_on := is_min_filter f (𝓝[s] a) a /-- `is_local_max_on f s a` means that `f x ≤ f a` for all `x ∈ s` in some neighborhood of `a`. -/ def is_local_max_on := is_max_filter f (𝓝[s] a) a /-- `is_local_extr_on f s a` means `is_local_min_on f s a ∨ is_local_max_on f s a`. -/ def is_local_extr_on := is_extr_filter f (𝓝[s] a) a /-- `is_local_min f a` means that `f a ≤ f x` for all `x` in some neighborhood of `a`. -/ def is_local_min := is_min_filter f (𝓝 a) a /-- `is_local_max f a` means that `f x ≤ f a` for all `x ∈ s` in some neighborhood of `a`. -/ def is_local_max := is_max_filter f (𝓝 a) a /-- `is_local_extr_on f s a` means `is_local_min_on f s a ∨ is_local_max_on f s a`. -/ def is_local_extr := is_extr_filter f (𝓝 a) a variables {f s a} lemma is_local_extr_on.elim {p : Prop} : is_local_extr_on f s a → (is_local_min_on f s a → p) → (is_local_max_on f s a → p) → p := or.elim lemma is_local_extr.elim {p : Prop} : is_local_extr f a → (is_local_min f a → p) → (is_local_max f a → p) → p := or.elim /-! ### Restriction to (sub)sets -/ lemma is_local_min.on (h : is_local_min f a) (s) : is_local_min_on f s a := h.filter_inf _ lemma is_local_max.on (h : is_local_max f a) (s) : is_local_max_on f s a := h.filter_inf _ lemma is_local_extr.on (h : is_local_extr f a) (s) : is_local_extr_on f s a := h.filter_inf _ lemma is_local_min_on.on_subset {t : set α} (hf : is_local_min_on f t a) (h : s ⊆ t) : is_local_min_on f s a := hf.filter_mono $ nhds_within_mono a h lemma is_local_max_on.on_subset {t : set α} (hf : is_local_max_on f t a) (h : s ⊆ t) : is_local_max_on f s a := hf.filter_mono $ nhds_within_mono a h lemma is_local_extr_on.on_subset {t : set α} (hf : is_local_extr_on f t a) (h : s ⊆ t) : is_local_extr_on f s a := hf.filter_mono $ nhds_within_mono a h lemma is_local_min_on.inter (hf : is_local_min_on f s a) (t) : is_local_min_on f (s ∩ t) a := hf.on_subset (inter_subset_left s t) lemma is_local_max_on.inter (hf : is_local_max_on f s a) (t) : is_local_max_on f (s ∩ t) a := hf.on_subset (inter_subset_left s t) lemma is_local_extr_on.inter (hf : is_local_extr_on f s a) (t) : is_local_extr_on f (s ∩ t) a := hf.on_subset (inter_subset_left s t) lemma is_min_on.localize (hf : is_min_on f s a) : is_local_min_on f s a := hf.filter_mono $ inf_le_right lemma is_max_on.localize (hf : is_max_on f s a) : is_local_max_on f s a := hf.filter_mono $ inf_le_right lemma is_extr_on.localize (hf : is_extr_on f s a) : is_local_extr_on f s a := hf.filter_mono $ inf_le_right lemma is_local_min_on.is_local_min (hf : is_local_min_on f s a) (hs : s ∈ 𝓝 a) : is_local_min f a := have 𝓝 a ≤ 𝓟 s, from le_principal_iff.2 hs, hf.filter_mono $ le_inf le_rfl this lemma is_local_max_on.is_local_max (hf : is_local_max_on f s a) (hs : s ∈ 𝓝 a) : is_local_max f a := have 𝓝 a ≤ 𝓟 s, from le_principal_iff.2 hs, hf.filter_mono $ le_inf le_rfl this lemma is_local_extr_on.is_local_extr (hf : is_local_extr_on f s a) (hs : s ∈ 𝓝 a) : is_local_extr f a := hf.elim (λ hf, (hf.is_local_min hs).is_extr) (λ hf, (hf.is_local_max hs).is_extr) lemma is_min_on.is_local_min (hf : is_min_on f s a) (hs : s ∈ 𝓝 a) : is_local_min f a := hf.localize.is_local_min hs lemma is_max_on.is_local_max (hf : is_max_on f s a) (hs : s ∈ 𝓝 a) : is_local_max f a := hf.localize.is_local_max hs lemma is_extr_on.is_local_extr (hf : is_extr_on f s a) (hs : s ∈ 𝓝 a) : is_local_extr f a := hf.localize.is_local_extr hs lemma is_local_min_on.not_nhds_le_map [topological_space β] (hf : is_local_min_on f s a) [ne_bot (𝓝[<] (f a))] : ¬𝓝 (f a) ≤ map f (𝓝[s] a) := λ hle, have ∀ᶠ y in 𝓝[<] (f a), f a ≤ y, from (eventually_map.2 hf).filter_mono (inf_le_left.trans hle), let ⟨y, hy⟩ := (this.and self_mem_nhds_within).exists in hy.1.not_lt hy.2 lemma is_local_max_on.not_nhds_le_map [topological_space β] (hf : is_local_max_on f s a) [ne_bot (𝓝[>] (f a))] : ¬𝓝 (f a) ≤ map f (𝓝[s] a) := @is_local_min_on.not_nhds_le_map α βᵒᵈ _ _ _ _ _ ‹_› hf ‹_› lemma is_local_extr_on.not_nhds_le_map [topological_space β] (hf : is_local_extr_on f s a) [ne_bot (𝓝[<] (f a))] [ne_bot (𝓝[>] (f a))] : ¬𝓝 (f a) ≤ map f (𝓝[s] a) := hf.elim (λ h, h.not_nhds_le_map) (λ h, h.not_nhds_le_map) /-! ### Constant -/ lemma is_local_min_on_const {b : β} : is_local_min_on (λ _, b) s a := is_min_filter_const lemma is_local_max_on_const {b : β} : is_local_max_on (λ _, b) s a := is_max_filter_const lemma is_local_extr_on_const {b : β} : is_local_extr_on (λ _, b) s a := is_extr_filter_const lemma is_local_min_const {b : β} : is_local_min (λ _, b) a := is_min_filter_const lemma is_local_max_const {b : β} : is_local_max (λ _, b) a := is_max_filter_const lemma is_local_extr_const {b : β} : is_local_extr (λ _, b) a := is_extr_filter_const /-! ### Composition with (anti)monotone functions -/ lemma is_local_min.comp_mono (hf : is_local_min f a) {g : β → γ} (hg : monotone g) : is_local_min (g ∘ f) a := hf.comp_mono hg lemma is_local_max.comp_mono (hf : is_local_max f a) {g : β → γ} (hg : monotone g) : is_local_max (g ∘ f) a := hf.comp_mono hg lemma is_local_extr.comp_mono (hf : is_local_extr f a) {g : β → γ} (hg : monotone g) : is_local_extr (g ∘ f) a := hf.comp_mono hg lemma is_local_min.comp_antitone (hf : is_local_min f a) {g : β → γ} (hg : antitone g) : is_local_max (g ∘ f) a := hf.comp_antitone hg lemma is_local_max.comp_antitone (hf : is_local_max f a) {g : β → γ} (hg : antitone g) : is_local_min (g ∘ f) a := hf.comp_antitone hg lemma is_local_extr.comp_antitone (hf : is_local_extr f a) {g : β → γ} (hg : antitone g) : is_local_extr (g ∘ f) a := hf.comp_antitone hg lemma is_local_min_on.comp_mono (hf : is_local_min_on f s a) {g : β → γ} (hg : monotone g) : is_local_min_on (g ∘ f) s a := hf.comp_mono hg lemma is_local_max_on.comp_mono (hf : is_local_max_on f s a) {g : β → γ} (hg : monotone g) : is_local_max_on (g ∘ f) s a := hf.comp_mono hg lemma is_local_extr_on.comp_mono (hf : is_local_extr_on f s a) {g : β → γ} (hg : monotone g) : is_local_extr_on (g ∘ f) s a := hf.comp_mono hg lemma is_local_min_on.comp_antitone (hf : is_local_min_on f s a) {g : β → γ} (hg : antitone g) : is_local_max_on (g ∘ f) s a := hf.comp_antitone hg lemma is_local_max_on.comp_antitone (hf : is_local_max_on f s a) {g : β → γ} (hg : antitone g) : is_local_min_on (g ∘ f) s a := hf.comp_antitone hg lemma is_local_extr_on.comp_antitone (hf : is_local_extr_on f s a) {g : β → γ} (hg : antitone g) : is_local_extr_on (g ∘ f) s a := hf.comp_antitone hg lemma is_local_min.bicomp_mono [preorder δ] {op : β → γ → δ} (hop : ((≤) ⇒ (≤) ⇒ (≤)) op op) (hf : is_local_min f a) {g : α → γ} (hg : is_local_min g a) : is_local_min (λ x, op (f x) (g x)) a := hf.bicomp_mono hop hg lemma is_local_max.bicomp_mono [preorder δ] {op : β → γ → δ} (hop : ((≤) ⇒ (≤) ⇒ (≤)) op op) (hf : is_local_max f a) {g : α → γ} (hg : is_local_max g a) : is_local_max (λ x, op (f x) (g x)) a := hf.bicomp_mono hop hg -- No `extr` version because we need `hf` and `hg` to be of the same kind lemma is_local_min_on.bicomp_mono [preorder δ] {op : β → γ → δ} (hop : ((≤) ⇒ (≤) ⇒ (≤)) op op) (hf : is_local_min_on f s a) {g : α → γ} (hg : is_local_min_on g s a) : is_local_min_on (λ x, op (f x) (g x)) s a := hf.bicomp_mono hop hg lemma is_local_max_on.bicomp_mono [preorder δ] {op : β → γ → δ} (hop : ((≤) ⇒ (≤) ⇒ (≤)) op op) (hf : is_local_max_on f s a) {g : α → γ} (hg : is_local_max_on g s a) : is_local_max_on (λ x, op (f x) (g x)) s a := hf.bicomp_mono hop hg /-! ### Composition with `continuous_at` -/ lemma is_local_min.comp_continuous [topological_space δ] {g : δ → α} {b : δ} (hf : is_local_min f (g b)) (hg : continuous_at g b) : is_local_min (f ∘ g) b := hg hf lemma is_local_max.comp_continuous [topological_space δ] {g : δ → α} {b : δ} (hf : is_local_max f (g b)) (hg : continuous_at g b) : is_local_max (f ∘ g) b := hg hf lemma is_local_extr.comp_continuous [topological_space δ] {g : δ → α} {b : δ} (hf : is_local_extr f (g b)) (hg : continuous_at g b) : is_local_extr (f ∘ g) b := hf.comp_tendsto hg lemma is_local_min.comp_continuous_on [topological_space δ] {s : set δ} {g : δ → α} {b : δ} (hf : is_local_min f (g b)) (hg : continuous_on g s) (hb : b ∈ s) : is_local_min_on (f ∘ g) s b := hf.comp_tendsto (hg b hb) lemma is_local_max.comp_continuous_on [topological_space δ] {s : set δ} {g : δ → α} {b : δ} (hf : is_local_max f (g b)) (hg : continuous_on g s) (hb : b ∈ s) : is_local_max_on (f ∘ g) s b := hf.comp_tendsto (hg b hb) lemma is_local_extr.comp_continuous_on [topological_space δ] {s : set δ} (g : δ → α) {b : δ} (hf : is_local_extr f (g b)) (hg : continuous_on g s) (hb : b ∈ s) : is_local_extr_on (f ∘ g) s b := hf.elim (λ hf, (hf.comp_continuous_on hg hb).is_extr) (λ hf, (is_local_max.comp_continuous_on hf hg hb).is_extr) lemma is_local_min_on.comp_continuous_on [topological_space δ] {t : set α} {s : set δ} {g : δ → α} {b : δ} (hf : is_local_min_on f t (g b)) (hst : s ⊆ g ⁻¹' t) (hg : continuous_on g s) (hb : b ∈ s) : is_local_min_on (f ∘ g) s b := hf.comp_tendsto (tendsto_nhds_within_mono_right (image_subset_iff.mpr hst) (continuous_within_at.tendsto_nhds_within_image (hg b hb))) lemma is_local_max_on.comp_continuous_on [topological_space δ] {t : set α} {s : set δ} {g : δ → α} {b : δ} (hf : is_local_max_on f t (g b)) (hst : s ⊆ g ⁻¹' t) (hg : continuous_on g s) (hb : b ∈ s) : is_local_max_on (f ∘ g) s b := hf.comp_tendsto (tendsto_nhds_within_mono_right (image_subset_iff.mpr hst) (continuous_within_at.tendsto_nhds_within_image (hg b hb))) lemma is_local_extr_on.comp_continuous_on [topological_space δ] {t : set α} {s : set δ} (g : δ → α) {b : δ} (hf : is_local_extr_on f t (g b)) (hst : s ⊆ g ⁻¹' t) (hg : continuous_on g s) (hb : b ∈ s) : is_local_extr_on (f ∘ g) s b := hf.elim (λ hf, (hf.comp_continuous_on hst hg hb).is_extr) (λ hf, (is_local_max_on.comp_continuous_on hf hst hg hb).is_extr) end preorder /-! ### Pointwise addition -/ section ordered_add_comm_monoid variables [ordered_add_comm_monoid β] {f g : α → β} {a : α} {s : set α} {l : filter α} lemma is_local_min.add (hf : is_local_min f a) (hg : is_local_min g a) : is_local_min (λ x, f x + g x) a := hf.add hg lemma is_local_max.add (hf : is_local_max f a) (hg : is_local_max g a) : is_local_max (λ x, f x + g x) a := hf.add hg lemma is_local_min_on.add (hf : is_local_min_on f s a) (hg : is_local_min_on g s a) : is_local_min_on (λ x, f x + g x) s a := hf.add hg lemma is_local_max_on.add (hf : is_local_max_on f s a) (hg : is_local_max_on g s a) : is_local_max_on (λ x, f x + g x) s a := hf.add hg end ordered_add_comm_monoid /-! ### Pointwise negation and subtraction -/ section ordered_add_comm_group variables [ordered_add_comm_group β] {f g : α → β} {a : α} {s : set α} {l : filter α} lemma is_local_min.neg (hf : is_local_min f a) : is_local_max (λ x, -f x) a := hf.neg lemma is_local_max.neg (hf : is_local_max f a) : is_local_min (λ x, -f x) a := hf.neg lemma is_local_extr.neg (hf : is_local_extr f a) : is_local_extr (λ x, -f x) a := hf.neg lemma is_local_min_on.neg (hf : is_local_min_on f s a) : is_local_max_on (λ x, -f x) s a := hf.neg lemma is_local_max_on.neg (hf : is_local_max_on f s a) : is_local_min_on (λ x, -f x) s a := hf.neg lemma is_local_extr_on.neg (hf : is_local_extr_on f s a) : is_local_extr_on (λ x, -f x) s a := hf.neg lemma is_local_min.sub (hf : is_local_min f a) (hg : is_local_max g a) : is_local_min (λ x, f x - g x) a := hf.sub hg lemma is_local_max.sub (hf : is_local_max f a) (hg : is_local_min g a) : is_local_max (λ x, f x - g x) a := hf.sub hg lemma is_local_min_on.sub (hf : is_local_min_on f s a) (hg : is_local_max_on g s a) : is_local_min_on (λ x, f x - g x) s a := hf.sub hg lemma is_local_max_on.sub (hf : is_local_max_on f s a) (hg : is_local_min_on g s a) : is_local_max_on (λ x, f x - g x) s a := hf.sub hg end ordered_add_comm_group /-! ### Pointwise `sup`/`inf` -/ section semilattice_sup variables [semilattice_sup β] {f g : α → β} {a : α} {s : set α} {l : filter α} lemma is_local_min.sup (hf : is_local_min f a) (hg : is_local_min g a) : is_local_min (λ x, f x ⊔ g x) a := hf.sup hg lemma is_local_max.sup (hf : is_local_max f a) (hg : is_local_max g a) : is_local_max (λ x, f x ⊔ g x) a := hf.sup hg lemma is_local_min_on.sup (hf : is_local_min_on f s a) (hg : is_local_min_on g s a) : is_local_min_on (λ x, f x ⊔ g x) s a := hf.sup hg lemma is_local_max_on.sup (hf : is_local_max_on f s a) (hg : is_local_max_on g s a) : is_local_max_on (λ x, f x ⊔ g x) s a := hf.sup hg end semilattice_sup section semilattice_inf variables [semilattice_inf β] {f g : α → β} {a : α} {s : set α} {l : filter α} lemma is_local_min.inf (hf : is_local_min f a) (hg : is_local_min g a) : is_local_min (λ x, f x ⊓ g x) a := hf.inf hg lemma is_local_max.inf (hf : is_local_max f a) (hg : is_local_max g a) : is_local_max (λ x, f x ⊓ g x) a := hf.inf hg lemma is_local_min_on.inf (hf : is_local_min_on f s a) (hg : is_local_min_on g s a) : is_local_min_on (λ x, f x ⊓ g x) s a := hf.inf hg lemma is_local_max_on.inf (hf : is_local_max_on f s a) (hg : is_local_max_on g s a) : is_local_max_on (λ x, f x ⊓ g x) s a := hf.inf hg end semilattice_inf /-! ### Pointwise `min`/`max` -/ section linear_order variables [linear_order β] {f g : α → β} {a : α} {s : set α} {l : filter α} lemma is_local_min.min (hf : is_local_min f a) (hg : is_local_min g a) : is_local_min (λ x, min (f x) (g x)) a := hf.min hg lemma is_local_max.min (hf : is_local_max f a) (hg : is_local_max g a) : is_local_max (λ x, min (f x) (g x)) a := hf.min hg lemma is_local_min_on.min (hf : is_local_min_on f s a) (hg : is_local_min_on g s a) : is_local_min_on (λ x, min (f x) (g x)) s a := hf.min hg lemma is_local_max_on.min (hf : is_local_max_on f s a) (hg : is_local_max_on g s a) : is_local_max_on (λ x, min (f x) (g x)) s a := hf.min hg lemma is_local_min.max (hf : is_local_min f a) (hg : is_local_min g a) : is_local_min (λ x, max (f x) (g x)) a := hf.max hg lemma is_local_max.max (hf : is_local_max f a) (hg : is_local_max g a) : is_local_max (λ x, max (f x) (g x)) a := hf.max hg lemma is_local_min_on.max (hf : is_local_min_on f s a) (hg : is_local_min_on g s a) : is_local_min_on (λ x, max (f x) (g x)) s a := hf.max hg lemma is_local_max_on.max (hf : is_local_max_on f s a) (hg : is_local_max_on g s a) : is_local_max_on (λ x, max (f x) (g x)) s a := hf.max hg end linear_order section eventually /-! ### Relation with `eventually` comparisons of two functions -/ variables [preorder β] {s : set α} lemma filter.eventually_le.is_local_max_on {f g : α → β} {a : α} (hle : g ≤ᶠ[𝓝[s] a] f) (hfga : f a = g a) (h : is_local_max_on f s a) : is_local_max_on g s a := hle.is_max_filter hfga h lemma is_local_max_on.congr {f g : α → β} {a : α} (h : is_local_max_on f s a) (heq : f =ᶠ[𝓝[s] a] g) (hmem : a ∈ s) : is_local_max_on g s a := h.congr heq $ heq.eq_of_nhds_within hmem lemma filter.eventually_eq.is_local_max_on_iff {f g : α → β} {a : α} (heq : f =ᶠ[𝓝[s] a] g) (hmem : a ∈ s) : is_local_max_on f s a ↔ is_local_max_on g s a := heq.is_max_filter_iff $ heq.eq_of_nhds_within hmem lemma filter.eventually_le.is_local_min_on {f g : α → β} {a : α} (hle : f ≤ᶠ[𝓝[s] a] g) (hfga : f a = g a) (h : is_local_min_on f s a) : is_local_min_on g s a := hle.is_min_filter hfga h lemma is_local_min_on.congr {f g : α → β} {a : α} (h : is_local_min_on f s a) (heq : f =ᶠ[𝓝[s] a] g) (hmem : a ∈ s) : is_local_min_on g s a := h.congr heq $ heq.eq_of_nhds_within hmem lemma filter.eventually_eq.is_local_min_on_iff {f g : α → β} {a : α} (heq : f =ᶠ[𝓝[s] a] g) (hmem : a ∈ s) : is_local_min_on f s a ↔ is_local_min_on g s a := heq.is_min_filter_iff $ heq.eq_of_nhds_within hmem lemma is_local_extr_on.congr {f g : α → β} {a : α} (h : is_local_extr_on f s a) (heq : f =ᶠ[𝓝[s] a] g) (hmem : a ∈ s) : is_local_extr_on g s a := h.congr heq $ heq.eq_of_nhds_within hmem lemma filter.eventually_eq.is_local_extr_on_iff {f g : α → β} {a : α} (heq : f =ᶠ[𝓝[s] a] g) (hmem : a ∈ s) : is_local_extr_on f s a ↔ is_local_extr_on g s a := heq.is_extr_filter_iff $ heq.eq_of_nhds_within hmem lemma filter.eventually_le.is_local_max {f g : α → β} {a : α} (hle : g ≤ᶠ[𝓝 a] f) (hfga : f a = g a) (h : is_local_max f a) : is_local_max g a := hle.is_max_filter hfga h lemma is_local_max.congr {f g : α → β} {a : α} (h : is_local_max f a) (heq : f =ᶠ[𝓝 a] g) : is_local_max g a := h.congr heq heq.eq_of_nhds lemma filter.eventually_eq.is_local_max_iff {f g : α → β} {a : α} (heq : f =ᶠ[𝓝 a] g) : is_local_max f a ↔ is_local_max g a := heq.is_max_filter_iff heq.eq_of_nhds lemma filter.eventually_le.is_local_min {f g : α → β} {a : α} (hle : f ≤ᶠ[𝓝 a] g) (hfga : f a = g a) (h : is_local_min f a) : is_local_min g a := hle.is_min_filter hfga h lemma is_local_min.congr {f g : α → β} {a : α} (h : is_local_min f a) (heq : f =ᶠ[𝓝 a] g) : is_local_min g a := h.congr heq heq.eq_of_nhds lemma filter.eventually_eq.is_local_min_iff {f g : α → β} {a : α} (heq : f =ᶠ[𝓝 a] g) : is_local_min f a ↔ is_local_min g a := heq.is_min_filter_iff heq.eq_of_nhds lemma is_local_extr.congr {f g : α → β} {a : α} (h : is_local_extr f a) (heq : f =ᶠ[𝓝 a] g) : is_local_extr g a := h.congr heq heq.eq_of_nhds lemma filter.eventually_eq.is_local_extr_iff {f g : α → β} {a : α} (heq : f =ᶠ[𝓝 a] g) : is_local_extr f a ↔ is_local_extr g a := heq.is_extr_filter_iff heq.eq_of_nhds end eventually
e39c798ebccad52ae983a07dbb47f0041c9b885b
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/order/filter/at_top_bot.lean
aa1b323ad0e0678e803a7c4f8a884977d8864864
[ "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
63,978
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Jeremy Avigad, Yury Kudryashov, Patrick Massot -/ import order.filter.bases import data.finset.preimage /-! # `at_top` and `at_bot` filters on preorded sets, monoids and groups. In this file we define the filters * `at_top`: corresponds to `n → +∞`; * `at_bot`: corresponds to `n → -∞`. Then we prove many lemmas like “if `f → +∞`, then `f ± c → +∞`”. -/ variables {ι ι' α β γ : Type*} open set open_locale classical filter big_operators namespace filter /-- `at_top` is the filter representing the limit `→ ∞` on an ordered set. It is generated by the collection of up-sets `{b | a ≤ b}`. (The preorder need not have a top element for this to be well defined, and indeed is trivial when a top element exists.) -/ def at_top [preorder α] : filter α := ⨅ a, 𝓟 (Ici a) /-- `at_bot` is the filter representing the limit `→ -∞` on an ordered set. It is generated by the collection of down-sets `{b | b ≤ a}`. (The preorder need not have a bottom element for this to be well defined, and indeed is trivial when a bottom element exists.) -/ def at_bot [preorder α] : filter α := ⨅ a, 𝓟 (Iic a) lemma mem_at_top [preorder α] (a : α) : {b : α | a ≤ b} ∈ @at_top α _ := mem_infi_of_mem a $ subset.refl _ lemma Ici_mem_at_top [preorder α] (a : α) : Ici a ∈ (at_top : filter α) := mem_at_top a lemma Ioi_mem_at_top [preorder α] [no_max_order α] (x : α) : Ioi x ∈ (at_top : filter α) := let ⟨z, hz⟩ := exists_gt x in mem_of_superset (mem_at_top z) $ λ y h, lt_of_lt_of_le hz h lemma mem_at_bot [preorder α] (a : α) : {b : α | b ≤ a} ∈ @at_bot α _ := mem_infi_of_mem a $ subset.refl _ lemma Iio_mem_at_bot [preorder α] [no_min_order α] (x : α) : Iio x ∈ (at_bot : filter α) := let ⟨z, hz⟩ := exists_lt x in mem_of_superset (mem_at_bot z) $ λ y h, lt_of_le_of_lt h hz lemma at_top_basis [nonempty α] [semilattice_sup α] : (@at_top α _).has_basis (λ _, true) Ici := has_basis_infi_principal (directed_of_sup $ λ a b, Ici_subset_Ici.2) lemma at_top_basis' [semilattice_sup α] (a : α) : (@at_top α _).has_basis (λ x, a ≤ x) Ici := ⟨λ t, (@at_top_basis α ⟨a⟩ _).mem_iff.trans ⟨λ ⟨x, _, hx⟩, ⟨x ⊔ a, le_sup_right, λ y hy, hx (le_trans le_sup_left hy)⟩, λ ⟨x, _, hx⟩, ⟨x, trivial, hx⟩⟩⟩ lemma at_bot_basis [nonempty α] [semilattice_inf α] : (@at_bot α _).has_basis (λ _, true) Iic := @at_top_basis (order_dual α) _ _ lemma at_bot_basis' [semilattice_inf α] (a : α) : (@at_bot α _).has_basis (λ x, x ≤ a) Iic := @at_top_basis' (order_dual α) _ _ @[instance] lemma at_top_ne_bot [nonempty α] [semilattice_sup α] : ne_bot (at_top : filter α) := at_top_basis.ne_bot_iff.2 $ λ a _, nonempty_Ici @[instance] lemma at_bot_ne_bot [nonempty α] [semilattice_inf α] : ne_bot (at_bot : filter α) := @at_top_ne_bot (order_dual α) _ _ @[simp] lemma mem_at_top_sets [nonempty α] [semilattice_sup α] {s : set α} : s ∈ (at_top : filter α) ↔ ∃a:α, ∀b≥a, b ∈ s := at_top_basis.mem_iff.trans $ exists_congr $ λ _, exists_const _ @[simp] lemma mem_at_bot_sets [nonempty α] [semilattice_inf α] {s : set α} : s ∈ (at_bot : filter α) ↔ ∃a:α, ∀b≤a, b ∈ s := @mem_at_top_sets (order_dual α) _ _ _ @[simp] lemma eventually_at_top [semilattice_sup α] [nonempty α] {p : α → Prop} : (∀ᶠ x in at_top, p x) ↔ (∃ a, ∀ b ≥ a, p b) := mem_at_top_sets @[simp] lemma eventually_at_bot [semilattice_inf α] [nonempty α] {p : α → Prop} : (∀ᶠ x in at_bot, p x) ↔ (∃ a, ∀ b ≤ a, p b) := mem_at_bot_sets lemma eventually_ge_at_top [preorder α] (a : α) : ∀ᶠ x in at_top, a ≤ x := mem_at_top a lemma eventually_le_at_bot [preorder α] (a : α) : ∀ᶠ x in at_bot, x ≤ a := mem_at_bot a lemma eventually_gt_at_top [preorder α] [no_max_order α] (a : α) : ∀ᶠ x in at_top, a < x := Ioi_mem_at_top a lemma eventually_ne_at_top [preorder α] [no_max_order α] (a : α) : ∀ᶠ x in at_top, x ≠ a := (eventually_gt_at_top a).mono (λ x hx, hx.ne.symm) lemma eventually_lt_at_bot [preorder α] [no_min_order α] (a : α) : ∀ᶠ x in at_bot, x < a := Iio_mem_at_bot a lemma at_top_basis_Ioi [nonempty α] [semilattice_sup α] [no_max_order α] : (@at_top α _).has_basis (λ _, true) Ioi := at_top_basis.to_has_basis (λ a ha, ⟨a, ha, Ioi_subset_Ici_self⟩) $ λ a ha, (exists_gt a).imp $ λ b hb, ⟨ha, Ici_subset_Ioi.2 hb⟩ lemma at_top_countable_basis [nonempty α] [semilattice_sup α] [encodable α] : has_countable_basis (at_top : filter α) (λ _, true) Ici := { countable := countable_encodable _, .. at_top_basis } lemma at_bot_countable_basis [nonempty α] [semilattice_inf α] [encodable α] : has_countable_basis (at_bot : filter α) (λ _, true) Iic := { countable := countable_encodable _, .. at_bot_basis } @[priority 200] instance at_top.is_countably_generated [preorder α] [encodable α] : (at_top : filter $ α).is_countably_generated := is_countably_generated_seq _ @[priority 200] instance at_bot.is_countably_generated [preorder α] [encodable α] : (at_bot : filter $ α).is_countably_generated := is_countably_generated_seq _ lemma order_top.at_top_eq (α) [partial_order α] [order_top α] : (at_top : filter α) = pure ⊤ := le_antisymm (le_pure_iff.2 $ (eventually_ge_at_top ⊤).mono $ λ b, top_unique) (le_infi $ λ b, le_principal_iff.2 le_top) lemma order_bot.at_bot_eq (α) [partial_order α] [order_bot α] : (at_bot : filter α) = pure ⊥ := @order_top.at_top_eq (order_dual α) _ _ @[nontriviality] lemma subsingleton.at_top_eq (α) [subsingleton α] [preorder α] : (at_top : filter α) = ⊤ := begin refine top_unique (λ s hs x, _), letI : unique α := ⟨⟨x⟩, λ y, subsingleton.elim y x⟩, rw [at_top, infi_unique, unique.default_eq x, mem_principal] at hs, exact hs left_mem_Ici end @[nontriviality] lemma subsingleton.at_bot_eq (α) [subsingleton α] [preorder α] : (at_bot : filter α) = ⊤ := @subsingleton.at_top_eq (order_dual α) _ _ lemma tendsto_at_top_pure [partial_order α] [order_top α] (f : α → β) : tendsto f at_top (pure $ f ⊤) := (order_top.at_top_eq α).symm ▸ tendsto_pure_pure _ _ lemma tendsto_at_bot_pure [partial_order α] [order_bot α] (f : α → β) : tendsto f at_bot (pure $ f ⊥) := @tendsto_at_top_pure (order_dual α) _ _ _ _ lemma eventually.exists_forall_of_at_top [semilattice_sup α] [nonempty α] {p : α → Prop} (h : ∀ᶠ x in at_top, p x) : ∃ a, ∀ b ≥ a, p b := eventually_at_top.mp h lemma eventually.exists_forall_of_at_bot [semilattice_inf α] [nonempty α] {p : α → Prop} (h : ∀ᶠ x in at_bot, p x) : ∃ a, ∀ b ≤ a, p b := eventually_at_bot.mp h lemma frequently_at_top [semilattice_sup α] [nonempty α] {p : α → Prop} : (∃ᶠ x in at_top, p x) ↔ (∀ a, ∃ b ≥ a, p b) := by simp [at_top_basis.frequently_iff] lemma frequently_at_bot [semilattice_inf α] [nonempty α] {p : α → Prop} : (∃ᶠ x in at_bot, p x) ↔ (∀ a, ∃ b ≤ a, p b) := @frequently_at_top (order_dual α) _ _ _ lemma frequently_at_top' [semilattice_sup α] [nonempty α] [no_max_order α] {p : α → Prop} : (∃ᶠ x in at_top, p x) ↔ (∀ a, ∃ b > a, p b) := by simp [at_top_basis_Ioi.frequently_iff] lemma frequently_at_bot' [semilattice_inf α] [nonempty α] [no_min_order α] {p : α → Prop} : (∃ᶠ x in at_bot, p x) ↔ (∀ a, ∃ b < a, p b) := @frequently_at_top' (order_dual α) _ _ _ _ lemma frequently.forall_exists_of_at_top [semilattice_sup α] [nonempty α] {p : α → Prop} (h : ∃ᶠ x in at_top, p x) : ∀ a, ∃ b ≥ a, p b := frequently_at_top.mp h lemma frequently.forall_exists_of_at_bot [semilattice_inf α] [nonempty α] {p : α → Prop} (h : ∃ᶠ x in at_bot, p x) : ∀ a, ∃ b ≤ a, p b := frequently_at_bot.mp h lemma map_at_top_eq [nonempty α] [semilattice_sup α] {f : α → β} : at_top.map f = (⨅a, 𝓟 $ f '' {a' | a ≤ a'}) := (at_top_basis.map _).eq_infi lemma map_at_bot_eq [nonempty α] [semilattice_inf α] {f : α → β} : at_bot.map f = (⨅a, 𝓟 $ f '' {a' | a' ≤ a}) := @map_at_top_eq (order_dual α) _ _ _ _ lemma tendsto_at_top [preorder β] {m : α → β} {f : filter α} : tendsto m f at_top ↔ (∀b, ∀ᶠ a in f, b ≤ m a) := by simp only [at_top, tendsto_infi, tendsto_principal, mem_Ici] lemma tendsto_at_bot [preorder β] {m : α → β} {f : filter α} : tendsto m f at_bot ↔ (∀b, ∀ᶠ a in f, m a ≤ b) := @tendsto_at_top α (order_dual β) _ m f lemma tendsto_at_top_mono' [preorder β] (l : filter α) ⦃f₁ f₂ : α → β⦄ (h : f₁ ≤ᶠ[l] f₂) : tendsto f₁ l at_top → tendsto f₂ l at_top := assume h₁, tendsto_at_top.2 $ λ b, mp_mem (tendsto_at_top.1 h₁ b) (monotone_mem (λ a ha ha₁, le_trans ha₁ ha) h) lemma tendsto_at_bot_mono' [preorder β] (l : filter α) ⦃f₁ f₂ : α → β⦄ (h : f₁ ≤ᶠ[l] f₂) : tendsto f₂ l at_bot → tendsto f₁ l at_bot := @tendsto_at_top_mono' _ (order_dual β) _ _ _ _ h lemma tendsto_at_top_mono [preorder β] {l : filter α} {f g : α → β} (h : ∀ n, f n ≤ g n) : tendsto f l at_top → tendsto g l at_top := tendsto_at_top_mono' l $ eventually_of_forall h lemma tendsto_at_bot_mono [preorder β] {l : filter α} {f g : α → β} (h : ∀ n, f n ≤ g n) : tendsto g l at_bot → tendsto f l at_bot := @tendsto_at_top_mono _ (order_dual β) _ _ _ _ h /-! ### Sequences -/ lemma inf_map_at_top_ne_bot_iff [semilattice_sup α] [nonempty α] {F : filter β} {u : α → β} : ne_bot (F ⊓ (map u at_top)) ↔ ∀ U ∈ F, ∀ N, ∃ n ≥ N, u n ∈ U := by simp_rw [inf_ne_bot_iff_frequently_left, frequently_map, frequently_at_top]; refl lemma inf_map_at_bot_ne_bot_iff [semilattice_inf α] [nonempty α] {F : filter β} {u : α → β} : ne_bot (F ⊓ (map u at_bot)) ↔ ∀ U ∈ F, ∀ N, ∃ n ≤ N, u n ∈ U := @inf_map_at_top_ne_bot_iff (order_dual α) _ _ _ _ _ lemma extraction_of_frequently_at_top' {P : ℕ → Prop} (h : ∀ N, ∃ n > N, P n) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, P (φ n) := begin choose u hu using h, cases forall_and_distrib.mp hu with hu hu', exact ⟨u ∘ (nat.rec 0 (λ n v, u v)), strict_mono_nat_of_lt_succ (λ n, hu _), λ n, hu' _⟩, end lemma extraction_of_frequently_at_top {P : ℕ → Prop} (h : ∃ᶠ n in at_top, P n) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, P (φ n) := begin rw frequently_at_top' at h, exact extraction_of_frequently_at_top' h, end lemma extraction_of_eventually_at_top {P : ℕ → Prop} (h : ∀ᶠ n in at_top, P n) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, P (φ n) := extraction_of_frequently_at_top h.frequently lemma extraction_forall_of_frequently {P : ℕ → ℕ → Prop} (h : ∀ n, ∃ᶠ k in at_top, P n k) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, P n (φ n) := begin simp only [frequently_at_top'] at h, choose u hu hu' using h, use (λ n, nat.rec_on n (u 0 0) (λ n v, u (n+1) v) : ℕ → ℕ), split, { apply strict_mono_nat_of_lt_succ, intro n, apply hu }, { intros n, cases n ; simp [hu'] }, end lemma extraction_forall_of_eventually {P : ℕ → ℕ → Prop} (h : ∀ n, ∀ᶠ k in at_top, P n k) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, P n (φ n) := extraction_forall_of_frequently (λ n, (h n).frequently) lemma extraction_forall_of_eventually' {P : ℕ → ℕ → Prop} (h : ∀ n, ∃ N, ∀ k ≥ N, P n k) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, P n (φ n) := extraction_forall_of_eventually (by simp [eventually_at_top, h]) lemma exists_le_of_tendsto_at_top [semilattice_sup α] [preorder β] {u : α → β} (h : tendsto u at_top at_top) (a : α) (b : β) : ∃ a' ≥ a, b ≤ u a' := begin have : ∀ᶠ x in at_top, a ≤ x ∧ b ≤ u x := (eventually_ge_at_top a).and (h.eventually $ eventually_ge_at_top b), haveI : nonempty α := ⟨a⟩, rcases this.exists with ⟨a', ha, hb⟩, exact ⟨a', ha, hb⟩ end @[nolint ge_or_gt] -- see Note [nolint_ge] lemma exists_le_of_tendsto_at_bot [semilattice_sup α] [preorder β] {u : α → β} (h : tendsto u at_top at_bot) : ∀ a b, ∃ a' ≥ a, u a' ≤ b := @exists_le_of_tendsto_at_top _ (order_dual β) _ _ _ h lemma exists_lt_of_tendsto_at_top [semilattice_sup α] [preorder β] [no_max_order β] {u : α → β} (h : tendsto u at_top at_top) (a : α) (b : β) : ∃ a' ≥ a, b < u a' := begin cases exists_gt b with b' hb', rcases exists_le_of_tendsto_at_top h a b' with ⟨a', ha', ha''⟩, exact ⟨a', ha', lt_of_lt_of_le hb' ha''⟩ end @[nolint ge_or_gt] -- see Note [nolint_ge] lemma exists_lt_of_tendsto_at_bot [semilattice_sup α] [preorder β] [no_min_order β] {u : α → β} (h : tendsto u at_top at_bot) : ∀ a b, ∃ a' ≥ a, u a' < b := @exists_lt_of_tendsto_at_top _ (order_dual β) _ _ _ _ h /-- If `u` is a sequence which is unbounded above, then after any point, it reaches a value strictly greater than all previous values. -/ lemma high_scores [linear_order β] [no_max_order β] {u : ℕ → β} (hu : tendsto u at_top at_top) : ∀ N, ∃ n ≥ N, ∀ k < n, u k < u n := begin intros N, obtain ⟨k : ℕ, hkn : k ≤ N, hku : ∀ l ≤ N, u l ≤ u k⟩ : ∃ k ≤ N, ∀ l ≤ N, u l ≤ u k, from exists_max_image _ u (finite_le_nat N) ⟨N, le_refl N⟩, have ex : ∃ n ≥ N, u k < u n, from exists_lt_of_tendsto_at_top hu _ _, obtain ⟨n : ℕ, hnN : n ≥ N, hnk : u k < u n, hn_min : ∀ m, m < n → N ≤ m → u m ≤ u k⟩ : ∃ n ≥ N, u k < u n ∧ ∀ m, m < n → N ≤ m → u m ≤ u k, { rcases nat.find_x ex with ⟨n, ⟨hnN, hnk⟩, hn_min⟩, push_neg at hn_min, exact ⟨n, hnN, hnk, hn_min⟩ }, use [n, hnN], rintros (l : ℕ) (hl : l < n), have hlk : u l ≤ u k, { cases (le_total l N : l ≤ N ∨ N ≤ l) with H H, { exact hku l H }, { exact hn_min l hl H } }, calc u l ≤ u k : hlk ... < u n : hnk end /-- If `u` is a sequence which is unbounded below, then after any point, it reaches a value strictly smaller than all previous values. -/ @[nolint ge_or_gt] -- see Note [nolint_ge] lemma low_scores [linear_order β] [no_min_order β] {u : ℕ → β} (hu : tendsto u at_top at_bot) : ∀ N, ∃ n ≥ N, ∀ k < n, u n < u k := @high_scores (order_dual β) _ _ _ hu /-- If `u` is a sequence which is unbounded above, then it `frequently` reaches a value strictly greater than all previous values. -/ lemma frequently_high_scores [linear_order β] [no_max_order β] {u : ℕ → β} (hu : tendsto u at_top at_top) : ∃ᶠ n in at_top, ∀ k < n, u k < u n := by simpa [frequently_at_top] using high_scores hu /-- If `u` is a sequence which is unbounded below, then it `frequently` reaches a value strictly smaller than all previous values. -/ lemma frequently_low_scores [linear_order β] [no_min_order β] {u : ℕ → β} (hu : tendsto u at_top at_bot) : ∃ᶠ n in at_top, ∀ k < n, u n < u k := @frequently_high_scores (order_dual β) _ _ _ hu lemma strict_mono_subseq_of_tendsto_at_top {β : Type*} [linear_order β] [no_max_order β] {u : ℕ → β} (hu : tendsto u at_top at_top) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ strict_mono (u ∘ φ) := let ⟨φ, h, h'⟩ := extraction_of_frequently_at_top (frequently_high_scores hu) in ⟨φ, h, λ n m hnm, h' m _ (h hnm)⟩ lemma strict_mono_subseq_of_id_le {u : ℕ → ℕ} (hu : ∀ n, n ≤ u n) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ strict_mono (u ∘ φ) := strict_mono_subseq_of_tendsto_at_top (tendsto_at_top_mono hu tendsto_id) lemma _root_.strict_mono.tendsto_at_top {φ : ℕ → ℕ} (h : strict_mono φ) : tendsto φ at_top at_top := tendsto_at_top_mono h.id_le tendsto_id section ordered_add_comm_monoid variables [ordered_add_comm_monoid β] {l : filter α} {f g : α → β} lemma tendsto_at_top_add_nonneg_left' (hf : ∀ᶠ x in l, 0 ≤ f x) (hg : tendsto g l at_top) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_mono' l (hf.mono (λ x, le_add_of_nonneg_left)) hg lemma tendsto_at_bot_add_nonpos_left' (hf : ∀ᶠ x in l, f x ≤ 0) (hg : tendsto g l at_bot) : tendsto (λ x, f x + g x) l at_bot := @tendsto_at_top_add_nonneg_left' _ (order_dual β) _ _ _ _ hf hg lemma tendsto_at_top_add_nonneg_left (hf : ∀ x, 0 ≤ f x) (hg : tendsto g l at_top) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_add_nonneg_left' (eventually_of_forall hf) hg lemma tendsto_at_bot_add_nonpos_left (hf : ∀ x, f x ≤ 0) (hg : tendsto g l at_bot) : tendsto (λ x, f x + g x) l at_bot := @tendsto_at_top_add_nonneg_left _ (order_dual β) _ _ _ _ hf hg lemma tendsto_at_top_add_nonneg_right' (hf : tendsto f l at_top) (hg : ∀ᶠ x in l, 0 ≤ g x) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_mono' l (monotone_mem (λ x, le_add_of_nonneg_right) hg) hf lemma tendsto_at_bot_add_nonpos_right' (hf : tendsto f l at_bot) (hg : ∀ᶠ x in l, g x ≤ 0) : tendsto (λ x, f x + g x) l at_bot := @tendsto_at_top_add_nonneg_right' _ (order_dual β) _ _ _ _ hf hg lemma tendsto_at_top_add_nonneg_right (hf : tendsto f l at_top) (hg : ∀ x, 0 ≤ g x) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_add_nonneg_right' hf (eventually_of_forall hg) lemma tendsto_at_bot_add_nonpos_right (hf : tendsto f l at_bot) (hg : ∀ x, g x ≤ 0) : tendsto (λ x, f x + g x) l at_bot := @tendsto_at_top_add_nonneg_right _ (order_dual β) _ _ _ _ hf hg lemma tendsto_at_top_add (hf : tendsto f l at_top) (hg : tendsto g l at_top) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_add_nonneg_left' (tendsto_at_top.mp hf 0) hg lemma tendsto_at_bot_add (hf : tendsto f l at_bot) (hg : tendsto g l at_bot) : tendsto (λ x, f x + g x) l at_bot := @tendsto_at_top_add _ (order_dual β) _ _ _ _ hf hg lemma tendsto.nsmul_at_top (hf : tendsto f l at_top) {n : ℕ} (hn : 0 < n) : tendsto (λ x, n • f x) l at_top := tendsto_at_top.2 $ λ y, (tendsto_at_top.1 hf y).mp $ (tendsto_at_top.1 hf 0).mono $ λ x h₀ hy, calc y ≤ f x : hy ... = 1 • f x : (one_nsmul _).symm ... ≤ n • f x : nsmul_le_nsmul h₀ hn lemma tendsto.nsmul_at_bot (hf : tendsto f l at_bot) {n : ℕ} (hn : 0 < n) : tendsto (λ x, n • f x) l at_bot := @tendsto.nsmul_at_top α (order_dual β) _ l f hf n hn lemma tendsto_bit0_at_top : tendsto bit0 (at_top : filter β) at_top := tendsto_at_top_add tendsto_id tendsto_id lemma tendsto_bit0_at_bot : tendsto bit0 (at_bot : filter β) at_bot := tendsto_at_bot_add tendsto_id tendsto_id end ordered_add_comm_monoid section ordered_cancel_add_comm_monoid variables [ordered_cancel_add_comm_monoid β] {l : filter α} {f g : α → β} lemma tendsto_at_top_of_add_const_left (C : β) (hf : tendsto (λ x, C + f x) l at_top) : tendsto f l at_top := tendsto_at_top.2 $ assume b, (tendsto_at_top.1 hf (C + b)).mono (λ x, le_of_add_le_add_left) lemma tendsto_at_bot_of_add_const_left (C : β) (hf : tendsto (λ x, C + f x) l at_bot) : tendsto f l at_bot := @tendsto_at_top_of_add_const_left _ (order_dual β) _ _ _ C hf lemma tendsto_at_top_of_add_const_right (C : β) (hf : tendsto (λ x, f x + C) l at_top) : tendsto f l at_top := tendsto_at_top.2 $ assume b, (tendsto_at_top.1 hf (b + C)).mono (λ x, le_of_add_le_add_right) lemma tendsto_at_bot_of_add_const_right (C : β) (hf : tendsto (λ x, f x + C) l at_bot) : tendsto f l at_bot := @tendsto_at_top_of_add_const_right _ (order_dual β) _ _ _ C hf lemma tendsto_at_top_of_add_bdd_above_left' (C) (hC : ∀ᶠ x in l, f x ≤ C) (h : tendsto (λ x, f x + g x) l at_top) : tendsto g l at_top := tendsto_at_top_of_add_const_left C (tendsto_at_top_mono' l (hC.mono (λ x hx, add_le_add_right hx (g x))) h) lemma tendsto_at_bot_of_add_bdd_below_left' (C) (hC : ∀ᶠ x in l, C ≤ f x) (h : tendsto (λ x, f x + g x) l at_bot) : tendsto g l at_bot := @tendsto_at_top_of_add_bdd_above_left' _ (order_dual β) _ _ _ _ C hC h lemma tendsto_at_top_of_add_bdd_above_left (C) (hC : ∀ x, f x ≤ C) : tendsto (λ x, f x + g x) l at_top → tendsto g l at_top := tendsto_at_top_of_add_bdd_above_left' C (univ_mem' hC) lemma tendsto_at_bot_of_add_bdd_below_left (C) (hC : ∀ x, C ≤ f x) : tendsto (λ x, f x + g x) l at_bot → tendsto g l at_bot := @tendsto_at_top_of_add_bdd_above_left _ (order_dual β) _ _ _ _ C hC lemma tendsto_at_top_of_add_bdd_above_right' (C) (hC : ∀ᶠ x in l, g x ≤ C) (h : tendsto (λ x, f x + g x) l at_top) : tendsto f l at_top := tendsto_at_top_of_add_const_right C (tendsto_at_top_mono' l (hC.mono (λ x hx, add_le_add_left hx (f x))) h) lemma tendsto_at_bot_of_add_bdd_below_right' (C) (hC : ∀ᶠ x in l, C ≤ g x) (h : tendsto (λ x, f x + g x) l at_bot) : tendsto f l at_bot := @tendsto_at_top_of_add_bdd_above_right' _ (order_dual β) _ _ _ _ C hC h lemma tendsto_at_top_of_add_bdd_above_right (C) (hC : ∀ x, g x ≤ C) : tendsto (λ x, f x + g x) l at_top → tendsto f l at_top := tendsto_at_top_of_add_bdd_above_right' C (univ_mem' hC) lemma tendsto_at_bot_of_add_bdd_below_right (C) (hC : ∀ x, C ≤ g x) : tendsto (λ x, f x + g x) l at_bot → tendsto f l at_bot := @tendsto_at_top_of_add_bdd_above_right _ (order_dual β) _ _ _ _ C hC end ordered_cancel_add_comm_monoid section ordered_group variables [ordered_add_comm_group β] (l : filter α) {f g : α → β} lemma tendsto_at_top_add_left_of_le' (C : β) (hf : ∀ᶠ x in l, C ≤ f x) (hg : tendsto g l at_top) : tendsto (λ x, f x + g x) l at_top := @tendsto_at_top_of_add_bdd_above_left' _ _ _ l (λ x, -(f x)) (λ x, f x + g x) (-C) (by simpa) (by simpa) lemma tendsto_at_bot_add_left_of_ge' (C : β) (hf : ∀ᶠ x in l, f x ≤ C) (hg : tendsto g l at_bot) : tendsto (λ x, f x + g x) l at_bot := @tendsto_at_top_add_left_of_le' _ (order_dual β) _ _ _ _ C hf hg lemma tendsto_at_top_add_left_of_le (C : β) (hf : ∀ x, C ≤ f x) (hg : tendsto g l at_top) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_add_left_of_le' l C (univ_mem' hf) hg lemma tendsto_at_bot_add_left_of_ge (C : β) (hf : ∀ x, f x ≤ C) (hg : tendsto g l at_bot) : tendsto (λ x, f x + g x) l at_bot := @tendsto_at_top_add_left_of_le _ (order_dual β) _ _ _ _ C hf hg lemma tendsto_at_top_add_right_of_le' (C : β) (hf : tendsto f l at_top) (hg : ∀ᶠ x in l, C ≤ g x) : tendsto (λ x, f x + g x) l at_top := @tendsto_at_top_of_add_bdd_above_right' _ _ _ l (λ x, f x + g x) (λ x, -(g x)) (-C) (by simp [hg]) (by simp [hf]) lemma tendsto_at_bot_add_right_of_ge' (C : β) (hf : tendsto f l at_bot) (hg : ∀ᶠ x in l, g x ≤ C) : tendsto (λ x, f x + g x) l at_bot := @tendsto_at_top_add_right_of_le' _ (order_dual β) _ _ _ _ C hf hg lemma tendsto_at_top_add_right_of_le (C : β) (hf : tendsto f l at_top) (hg : ∀ x, C ≤ g x) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_add_right_of_le' l C hf (univ_mem' hg) lemma tendsto_at_bot_add_right_of_ge (C : β) (hf : tendsto f l at_bot) (hg : ∀ x, g x ≤ C) : tendsto (λ x, f x + g x) l at_bot := @tendsto_at_top_add_right_of_le _ (order_dual β) _ _ _ _ C hf hg lemma tendsto_at_top_add_const_left (C : β) (hf : tendsto f l at_top) : tendsto (λ x, C + f x) l at_top := tendsto_at_top_add_left_of_le' l C (univ_mem' $ λ _, le_refl C) hf lemma tendsto_at_bot_add_const_left (C : β) (hf : tendsto f l at_bot) : tendsto (λ x, C + f x) l at_bot := @tendsto_at_top_add_const_left _ (order_dual β) _ _ _ C hf lemma tendsto_at_top_add_const_right (C : β) (hf : tendsto f l at_top) : tendsto (λ x, f x + C) l at_top := tendsto_at_top_add_right_of_le' l C hf (univ_mem' $ λ _, le_refl C) lemma tendsto_at_bot_add_const_right (C : β) (hf : tendsto f l at_bot) : tendsto (λ x, f x + C) l at_bot := @tendsto_at_top_add_const_right _ (order_dual β) _ _ _ C hf lemma tendsto_neg_at_top_at_bot : tendsto (has_neg.neg : β → β) at_top at_bot := begin simp only [tendsto_at_bot, neg_le], exact λ b, eventually_ge_at_top _ end lemma tendsto_neg_at_bot_at_top : tendsto (has_neg.neg : β → β) at_bot at_top := @tendsto_neg_at_top_at_bot (order_dual β) _ end ordered_group section ordered_semiring variables [ordered_semiring α] {l : filter β} {f g : β → α} lemma tendsto_bit1_at_top : tendsto bit1 (at_top : filter α) at_top := tendsto_at_top_add_nonneg_right tendsto_bit0_at_top (λ _, zero_le_one) lemma tendsto.at_top_mul_at_top (hf : tendsto f l at_top) (hg : tendsto g l at_top) : tendsto (λ x, f x * g x) l at_top := begin refine tendsto_at_top_mono' _ _ hg, filter_upwards [hg.eventually (eventually_ge_at_top 0), hf.eventually (eventually_ge_at_top 1)] with _ using le_mul_of_one_le_left, end lemma tendsto_mul_self_at_top : tendsto (λ x : α, x * x) at_top at_top := tendsto_id.at_top_mul_at_top tendsto_id /-- The monomial function `x^n` tends to `+∞` at `+∞` for any positive natural `n`. A version for positive real powers exists as `tendsto_rpow_at_top`. -/ lemma tendsto_pow_at_top {n : ℕ} (hn : 1 ≤ n) : tendsto (λ x : α, x ^ n) at_top at_top := begin refine tendsto_at_top_mono' _ ((eventually_ge_at_top 1).mono $ λ x hx, _) tendsto_id, simpa only [pow_one] using pow_le_pow hx hn end lemma eventually_ne_of_tendsto_at_top [nontrivial α] (hf : tendsto f l at_top) (c : α) : ∀ᶠ x in l, f x ≠ c := (tendsto_at_top.1 hf $ (c + 1)).mono (λ x hx, ne_of_gt (lt_of_lt_of_le (lt_add_one c) hx)) end ordered_semiring lemma zero_pow_eventually_eq [monoid_with_zero α] : (λ n : ℕ, (0 : α) ^ n) =ᶠ[at_top] (λ n, 0) := eventually_at_top.2 ⟨1, λ n hn, zero_pow (zero_lt_one.trans_le hn)⟩ section ordered_ring variables [ordered_ring α] {l : filter β} {f g : β → α} lemma eventually_ne_of_tendsto_at_bot [nontrivial α] (hf : tendsto f l at_bot) (c : α) : ∀ᶠ x in l, f x ≠ c := (tendsto_at_bot.1 hf $ (c - 1)).mono (λ x hx, ne_of_lt (lt_of_le_of_lt hx ((sub_lt_self_iff c).2 zero_lt_one))) lemma tendsto.at_top_mul_at_bot (hf : tendsto f l at_top) (hg : tendsto g l at_bot) : tendsto (λ x, f x * g x) l at_bot := have _ := (hf.at_top_mul_at_top $ tendsto_neg_at_bot_at_top.comp hg), by simpa only [(∘), neg_mul_eq_mul_neg, neg_neg] using tendsto_neg_at_top_at_bot.comp this lemma tendsto.at_bot_mul_at_top (hf : tendsto f l at_bot) (hg : tendsto g l at_top) : tendsto (λ x, f x * g x) l at_bot := have tendsto (λ x, (-f x) * g x) l at_top := ( (tendsto_neg_at_bot_at_top.comp hf).at_top_mul_at_top hg), by simpa only [(∘), neg_mul_eq_neg_mul, neg_neg] using tendsto_neg_at_top_at_bot.comp this lemma tendsto.at_bot_mul_at_bot (hf : tendsto f l at_bot) (hg : tendsto g l at_bot) : tendsto (λ x, f x * g x) l at_top := have tendsto (λ x, (-f x) * (-g x)) l at_top := (tendsto_neg_at_bot_at_top.comp hf).at_top_mul_at_top (tendsto_neg_at_bot_at_top.comp hg), by simpa only [neg_mul_neg] using this end ordered_ring section linear_ordered_add_comm_group variables [linear_ordered_add_comm_group α] /-- $\lim_{x\to+\infty}|x|=+\infty$ -/ lemma tendsto_abs_at_top_at_top : tendsto (abs : α → α) at_top at_top := tendsto_at_top_mono le_abs_self tendsto_id /-- $\lim_{x\to-\infty}|x|=+\infty$ -/ lemma tendsto_abs_at_bot_at_top : tendsto (abs : α → α) at_bot at_top := tendsto_at_top_mono neg_le_abs_self tendsto_neg_at_bot_at_top @[simp] lemma comap_abs_at_top : comap (abs : α → α) at_top = at_bot ⊔ at_top := begin refine le_antisymm (((at_top_basis.comap _).le_basis_iff (at_bot_basis.sup at_top_basis)).2 _) (sup_le tendsto_abs_at_bot_at_top.le_comap tendsto_abs_at_top_at_top.le_comap), rintro ⟨a, b⟩ -, refine ⟨max (-a) b, trivial, λ x hx, _⟩, rw [mem_preimage, mem_Ici, le_abs', max_le_iff, ← min_neg_neg, le_min_iff, neg_neg] at hx, exact hx.imp and.left and.right end end linear_ordered_add_comm_group section linear_ordered_semiring variables [linear_ordered_semiring α] {l : filter β} {f : β → α} lemma tendsto.at_top_of_const_mul {c : α} (hc : 0 < c) (hf : tendsto (λ x, c * f x) l at_top) : tendsto f l at_top := tendsto_at_top.2 $ λ b, (tendsto_at_top.1 hf (c * b)).mono $ λ x hx, le_of_mul_le_mul_left hx hc lemma tendsto.at_top_of_mul_const {c : α} (hc : 0 < c) (hf : tendsto (λ x, f x * c) l at_top) : tendsto f l at_top := tendsto_at_top.2 $ λ b, (tendsto_at_top.1 hf (b * c)).mono $ λ x hx, le_of_mul_le_mul_right hx hc end linear_ordered_semiring lemma nonneg_of_eventually_pow_nonneg [linear_ordered_ring α] {a : α} (h : ∀ᶠ n in at_top, 0 ≤ a ^ (n : ℕ)) : 0 ≤ a := let ⟨n, hn⟩ := (tendsto_bit1_at_top.eventually h).exists in pow_bit1_nonneg_iff.1 hn section linear_ordered_field variables [linear_ordered_field α] {l : filter β} {f : β → α} {r : α} /-- If a function tends to infinity along a filter, then this function multiplied by a positive constant (on the left) also tends to infinity. For a version working in `ℕ` or `ℤ`, use `filter.tendsto.const_mul_at_top'` instead. -/ lemma tendsto.const_mul_at_top (hr : 0 < r) (hf : tendsto f l at_top) : tendsto (λx, r * f x) l at_top := tendsto.at_top_of_const_mul (inv_pos.2 hr) $ by simpa only [inv_mul_cancel_left₀ hr.ne'] /-- If a function tends to infinity along a filter, then this function multiplied by a positive constant (on the right) also tends to infinity. For a version working in `ℕ` or `ℤ`, use `filter.tendsto.at_top_mul_const'` instead. -/ lemma tendsto.at_top_mul_const (hr : 0 < r) (hf : tendsto f l at_top) : tendsto (λx, f x * r) l at_top := by simpa only [mul_comm] using hf.const_mul_at_top hr /-- If a function tends to infinity along a filter, then this function divided by a positive constant also tends to infinity. -/ lemma tendsto.at_top_div_const (hr : 0 < r) (hf : tendsto f l at_top) : tendsto (λx, f x / r) l at_top := by simpa only [div_eq_mul_inv] using hf.at_top_mul_const (inv_pos.2 hr) /-- If a function tends to infinity along a filter, then this function multiplied by a negative constant (on the left) tends to negative infinity. -/ lemma tendsto.neg_const_mul_at_top (hr : r < 0) (hf : tendsto f l at_top) : tendsto (λ x, r * f x) l at_bot := by simpa only [(∘), neg_mul_eq_neg_mul, neg_neg] using tendsto_neg_at_top_at_bot.comp (hf.const_mul_at_top (neg_pos.2 hr)) /-- If a function tends to infinity along a filter, then this function multiplied by a negative constant (on the right) tends to negative infinity. -/ lemma tendsto.at_top_mul_neg_const (hr : r < 0) (hf : tendsto f l at_top) : tendsto (λ x, f x * r) l at_bot := by simpa only [mul_comm] using hf.neg_const_mul_at_top hr /-- If a function tends to negative infinity along a filter, then this function multiplied by a positive constant (on the left) also tends to negative infinity. -/ lemma tendsto.const_mul_at_bot (hr : 0 < r) (hf : tendsto f l at_bot) : tendsto (λx, r * f x) l at_bot := by simpa only [(∘), neg_mul_eq_mul_neg, neg_neg] using tendsto_neg_at_top_at_bot.comp ((tendsto_neg_at_bot_at_top.comp hf).const_mul_at_top hr) /-- If a function tends to negative infinity along a filter, then this function multiplied by a positive constant (on the right) also tends to negative infinity. -/ lemma tendsto.at_bot_mul_const (hr : 0 < r) (hf : tendsto f l at_bot) : tendsto (λx, f x * r) l at_bot := by simpa only [mul_comm] using hf.const_mul_at_bot hr /-- If a function tends to negative infinity along a filter, then this function divided by a positive constant also tends to negative infinity. -/ lemma tendsto.at_bot_div_const (hr : 0 < r) (hf : tendsto f l at_bot) : tendsto (λx, f x / r) l at_bot := by simpa only [div_eq_mul_inv] using hf.at_bot_mul_const (inv_pos.2 hr) /-- If a function tends to negative infinity along a filter, then this function multiplied by a negative constant (on the left) tends to positive infinity. -/ lemma tendsto.neg_const_mul_at_bot (hr : r < 0) (hf : tendsto f l at_bot) : tendsto (λ x, r * f x) l at_top := by simpa only [(∘), neg_mul_eq_neg_mul, neg_neg] using tendsto_neg_at_bot_at_top.comp (hf.const_mul_at_bot (neg_pos.2 hr)) /-- If a function tends to negative infinity along a filter, then this function multiplied by a negative constant (on the right) tends to positive infinity. -/ lemma tendsto.at_bot_mul_neg_const (hr : r < 0) (hf : tendsto f l at_bot) : tendsto (λ x, f x * r) l at_top := by simpa only [mul_comm] using hf.neg_const_mul_at_bot hr lemma tendsto_const_mul_pow_at_top {c : α} {n : ℕ} (hn : 1 ≤ n) (hc : 0 < c) : tendsto (λ x, c * x^n) at_top at_top := tendsto.const_mul_at_top hc (tendsto_pow_at_top hn) lemma tendsto_const_mul_pow_at_top_iff (c : α) (n : ℕ) : tendsto (λ x, c * x^n) at_top at_top ↔ 1 ≤ n ∧ 0 < c := begin refine ⟨λ h, _, λ h, tendsto_const_mul_pow_at_top h.1 h.2⟩, simp only [tendsto_at_top, eventually_at_top] at h, have : 0 < c := let ⟨x, hx⟩ := h 1 in pos_of_mul_pos_right (lt_of_lt_of_le zero_lt_one (hx (max x 1) (le_max_left x 1))) (pow_nonneg (le_trans zero_le_one (le_max_right x 1)) n), refine ⟨nat.succ_le_iff.mp (lt_of_le_of_ne (zero_le n) (ne.symm (λ hn, _))), this⟩, obtain ⟨x, hx⟩ := h (c + 1), specialize hx x le_rfl, rw [hn, pow_zero, mul_one, add_le_iff_nonpos_right] at hx, exact absurd hx (not_le.mpr zero_lt_one), end lemma tendsto_neg_const_mul_pow_at_top {c : α} {n : ℕ} (hn : 1 ≤ n) (hc : c < 0) : tendsto (λ x, c * x^n) at_top at_bot := tendsto.neg_const_mul_at_top hc (tendsto_pow_at_top hn) lemma tendsto_neg_const_mul_pow_at_top_iff (c : α) (n : ℕ) : tendsto (λ x, c * x^n) at_top at_bot ↔ 1 ≤ n ∧ c < 0 := begin refine ⟨λ h, _, λ h, tendsto_neg_const_mul_pow_at_top h.1 h.2⟩, simp only [tendsto_at_bot, eventually_at_top] at h, have : c < 0 := let ⟨x, hx⟩ := h (-1) in neg_of_mul_neg_right (lt_of_le_of_lt (hx (max x 1) (le_max_left x 1)) (by simp [zero_lt_one])) (pow_nonneg (le_trans zero_le_one (le_max_right x 1)) n), refine ⟨nat.succ_le_iff.mp (lt_of_le_of_ne (zero_le n) (ne.symm (λ hn, _))), this⟩, obtain ⟨x, hx⟩ := h (c - 1), specialize hx x le_rfl, rw [hn, pow_zero, mul_one, le_sub, sub_self] at hx, exact absurd hx (not_le.mpr zero_lt_one), end end linear_ordered_field open_locale filter lemma tendsto_at_top' [nonempty α] [semilattice_sup α] {f : α → β} {l : filter β} : tendsto f at_top l ↔ (∀s ∈ l, ∃a, ∀b≥a, f b ∈ s) := by simp only [tendsto_def, mem_at_top_sets]; refl lemma tendsto_at_bot' [nonempty α] [semilattice_inf α] {f : α → β} {l : filter β} : tendsto f at_bot l ↔ (∀s ∈ l, ∃a, ∀b≤a, f b ∈ s) := @tendsto_at_top' (order_dual α) _ _ _ _ _ theorem tendsto_at_top_principal [nonempty β] [semilattice_sup β] {f : β → α} {s : set α} : tendsto f at_top (𝓟 s) ↔ ∃N, ∀n≥N, f n ∈ s := by rw [tendsto_iff_comap, comap_principal, le_principal_iff, mem_at_top_sets]; refl theorem tendsto_at_bot_principal [nonempty β] [semilattice_inf β] {f : β → α} {s : set α} : tendsto f at_bot (𝓟 s) ↔ ∃N, ∀n≤N, f n ∈ s := @tendsto_at_top_principal _ (order_dual β) _ _ _ _ /-- A function `f` grows to `+∞` independent of an order-preserving embedding `e`. -/ lemma tendsto_at_top_at_top [nonempty α] [semilattice_sup α] [preorder β] {f : α → β} : tendsto f at_top at_top ↔ ∀ b : β, ∃ i : α, ∀ a : α, i ≤ a → b ≤ f a := iff.trans tendsto_infi $ forall_congr $ assume b, tendsto_at_top_principal lemma tendsto_at_top_at_bot [nonempty α] [semilattice_sup α] [preorder β] {f : α → β} : tendsto f at_top at_bot ↔ ∀ (b : β), ∃ (i : α), ∀ (a : α), i ≤ a → f a ≤ b := @tendsto_at_top_at_top α (order_dual β) _ _ _ f lemma tendsto_at_bot_at_top [nonempty α] [semilattice_inf α] [preorder β] {f : α → β} : tendsto f at_bot at_top ↔ ∀ (b : β), ∃ (i : α), ∀ (a : α), a ≤ i → b ≤ f a := @tendsto_at_top_at_top (order_dual α) β _ _ _ f lemma tendsto_at_bot_at_bot [nonempty α] [semilattice_inf α] [preorder β] {f : α → β} : tendsto f at_bot at_bot ↔ ∀ (b : β), ∃ (i : α), ∀ (a : α), a ≤ i → f a ≤ b := @tendsto_at_top_at_top (order_dual α) (order_dual β) _ _ _ f lemma tendsto_at_top_at_top_of_monotone [preorder α] [preorder β] {f : α → β} (hf : monotone f) (h : ∀ b, ∃ a, b ≤ f a) : tendsto f at_top at_top := tendsto_infi.2 $ λ b, tendsto_principal.2 $ let ⟨a, ha⟩ := h b in mem_of_superset (mem_at_top a) $ λ a' ha', le_trans ha (hf ha') lemma tendsto_at_bot_at_bot_of_monotone [preorder α] [preorder β] {f : α → β} (hf : monotone f) (h : ∀ b, ∃ a, f a ≤ b) : tendsto f at_bot at_bot := tendsto_infi.2 $ λ b, tendsto_principal.2 $ let ⟨a, ha⟩ := h b in mem_of_superset (mem_at_bot a) $ λ a' ha', le_trans (hf ha') ha lemma tendsto_at_top_at_top_iff_of_monotone [nonempty α] [semilattice_sup α] [preorder β] {f : α → β} (hf : monotone f) : tendsto f at_top at_top ↔ ∀ b : β, ∃ a : α, b ≤ f a := tendsto_at_top_at_top.trans $ forall_congr $ λ b, exists_congr $ λ a, ⟨λ h, h a (le_refl a), λ h a' ha', le_trans h $ hf ha'⟩ lemma tendsto_at_bot_at_bot_iff_of_monotone [nonempty α] [semilattice_inf α] [preorder β] {f : α → β} (hf : monotone f) : tendsto f at_bot at_bot ↔ ∀ b : β, ∃ a : α, f a ≤ b := tendsto_at_bot_at_bot.trans $ forall_congr $ λ b, exists_congr $ λ a, ⟨λ h, h a (le_refl a), λ h a' ha', le_trans (hf ha') h⟩ alias tendsto_at_top_at_top_of_monotone ← monotone.tendsto_at_top_at_top alias tendsto_at_bot_at_bot_of_monotone ← monotone.tendsto_at_bot_at_bot alias tendsto_at_top_at_top_iff_of_monotone ← monotone.tendsto_at_top_at_top_iff alias tendsto_at_bot_at_bot_iff_of_monotone ← monotone.tendsto_at_bot_at_bot_iff lemma comap_embedding_at_top [preorder β] [preorder γ] {e : β → γ} (hm : ∀b₁ b₂, e b₁ ≤ e b₂ ↔ b₁ ≤ b₂) (hu : ∀c, ∃b, c ≤ e b) : comap e at_top = at_top := le_antisymm (le_infi $ λ b, le_principal_iff.2 $ mem_comap.2 ⟨Ici (e b), mem_at_top _, λ x, (hm _ _).1⟩) (tendsto_at_top_at_top_of_monotone (λ _ _, (hm _ _).2) hu).le_comap lemma comap_embedding_at_bot [preorder β] [preorder γ] {e : β → γ} (hm : ∀ b₁ b₂, e b₁ ≤ e b₂ ↔ b₁ ≤ b₂) (hu : ∀c, ∃b, e b ≤ c) : comap e at_bot = at_bot := @comap_embedding_at_top (order_dual β) (order_dual γ) _ _ e (function.swap hm) hu lemma tendsto_at_top_embedding [preorder β] [preorder γ] {f : α → β} {e : β → γ} {l : filter α} (hm : ∀b₁ b₂, e b₁ ≤ e b₂ ↔ b₁ ≤ b₂) (hu : ∀c, ∃b, c ≤ e b) : tendsto (e ∘ f) l at_top ↔ tendsto f l at_top := by rw [← comap_embedding_at_top hm hu, tendsto_comap_iff] /-- A function `f` goes to `-∞` independent of an order-preserving embedding `e`. -/ lemma tendsto_at_bot_embedding [preorder β] [preorder γ] {f : α → β} {e : β → γ} {l : filter α} (hm : ∀b₁ b₂, e b₁ ≤ e b₂ ↔ b₁ ≤ b₂) (hu : ∀c, ∃b, e b ≤ c) : tendsto (e ∘ f) l at_bot ↔ tendsto f l at_bot := @tendsto_at_top_embedding α (order_dual β) (order_dual γ) _ _ f e l (function.swap hm) hu lemma tendsto_finset_range : tendsto finset.range at_top at_top := finset.range_mono.tendsto_at_top_at_top finset.exists_nat_subset_range lemma at_top_finset_eq_infi : (at_top : filter $ finset α) = ⨅ x : α, 𝓟 (Ici {x}) := begin refine le_antisymm (le_infi (λ i, le_principal_iff.2 $ mem_at_top {i})) _, refine le_infi (λ s, le_principal_iff.2 $ mem_infi_of_Inter s.finite_to_set (λ i, mem_principal_self _) _), simp only [subset_def, mem_Inter, set_coe.forall, mem_Ici, finset.le_iff_subset, finset.mem_singleton, finset.subset_iff, forall_eq], dsimp, exact λ t, id end /-- If `f` is a monotone sequence of `finset`s and each `x` belongs to one of `f n`, then `tendsto f at_top at_top`. -/ lemma tendsto_at_top_finset_of_monotone [preorder β] {f : β → finset α} (h : monotone f) (h' : ∀ x : α, ∃ n, x ∈ f n) : tendsto f at_top at_top := begin simp only [at_top_finset_eq_infi, tendsto_infi, tendsto_principal], intro a, rcases h' a with ⟨b, hb⟩, exact eventually.mono (mem_at_top b) (λ b' hb', le_trans (finset.singleton_subset_iff.2 hb) (h hb')), end alias tendsto_at_top_finset_of_monotone ← monotone.tendsto_at_top_finset lemma tendsto_finset_image_at_top_at_top {i : β → γ} {j : γ → β} (h : function.left_inverse j i) : tendsto (finset.image j) at_top at_top := (finset.image_mono j).tendsto_at_top_finset $ assume a, ⟨{i a}, by simp only [finset.image_singleton, h a, finset.mem_singleton]⟩ lemma tendsto_finset_preimage_at_top_at_top {f : α → β} (hf : function.injective f) : tendsto (λ s : finset β, s.preimage f (hf.inj_on _)) at_top at_top := (finset.monotone_preimage hf).tendsto_at_top_finset $ λ x, ⟨{f x}, finset.mem_preimage.2 $ finset.mem_singleton_self _⟩ lemma prod_at_top_at_top_eq {β₁ β₂ : Type*} [semilattice_sup β₁] [semilattice_sup β₂] : (at_top : filter β₁) ×ᶠ (at_top : filter β₂) = (at_top : filter (β₁ × β₂)) := begin casesI (is_empty_or_nonempty β₁).symm, casesI (is_empty_or_nonempty β₂).symm, { simp [at_top, prod_infi_left, prod_infi_right, infi_prod], exact infi_comm, }, { simp only [at_top.filter_eq_bot_of_is_empty, prod_bot] }, { simp only [at_top.filter_eq_bot_of_is_empty, bot_prod] }, end lemma prod_at_bot_at_bot_eq {β₁ β₂ : Type*} [semilattice_inf β₁] [semilattice_inf β₂] : (at_bot : filter β₁) ×ᶠ (at_bot : filter β₂) = (at_bot : filter (β₁ × β₂)) := @prod_at_top_at_top_eq (order_dual β₁) (order_dual β₂) _ _ lemma prod_map_at_top_eq {α₁ α₂ β₁ β₂ : Type*} [semilattice_sup β₁] [semilattice_sup β₂] (u₁ : β₁ → α₁) (u₂ : β₂ → α₂) : (map u₁ at_top) ×ᶠ (map u₂ at_top) = map (prod.map u₁ u₂) at_top := by rw [prod_map_map_eq, prod_at_top_at_top_eq, prod.map_def] lemma prod_map_at_bot_eq {α₁ α₂ β₁ β₂ : Type*} [semilattice_inf β₁] [semilattice_inf β₂] (u₁ : β₁ → α₁) (u₂ : β₂ → α₂) : (map u₁ at_bot) ×ᶠ (map u₂ at_bot) = map (prod.map u₁ u₂) at_bot := @prod_map_at_top_eq _ _ (order_dual β₁) (order_dual β₂) _ _ _ _ lemma tendsto.subseq_mem {F : filter α} {V : ℕ → set α} (h : ∀ n, V n ∈ F) {u : ℕ → α} (hu : tendsto u at_top F) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, u (φ n) ∈ V n := extraction_forall_of_eventually' (λ n, tendsto_at_top'.mp hu _ (h n) : ∀ n, ∃ N, ∀ k ≥ N, u k ∈ V n) lemma tendsto_at_bot_diagonal [semilattice_inf α] : tendsto (λ a : α, (a, a)) at_bot at_bot := by { rw ← prod_at_bot_at_bot_eq, exact tendsto_id.prod_mk tendsto_id } lemma tendsto_at_top_diagonal [semilattice_sup α] : tendsto (λ a : α, (a, a)) at_top at_top := by { rw ← prod_at_top_at_top_eq, exact tendsto_id.prod_mk tendsto_id } lemma tendsto.prod_map_prod_at_bot [semilattice_inf γ] {F : filter α} {G : filter β} {f : α → γ} {g : β → γ} (hf : tendsto f F at_bot) (hg : tendsto g G at_bot) : tendsto (prod.map f g) (F ×ᶠ G) at_bot := by { rw ← prod_at_bot_at_bot_eq, exact hf.prod_map hg, } lemma tendsto.prod_map_prod_at_top [semilattice_sup γ] {F : filter α} {G : filter β} {f : α → γ} {g : β → γ} (hf : tendsto f F at_top) (hg : tendsto g G at_top) : tendsto (prod.map f g) (F ×ᶠ G) at_top := by { rw ← prod_at_top_at_top_eq, exact hf.prod_map hg, } lemma tendsto.prod_at_bot [semilattice_inf α] [semilattice_inf γ] {f g : α → γ} (hf : tendsto f at_bot at_bot) (hg : tendsto g at_bot at_bot) : tendsto (prod.map f g) at_bot at_bot := by { rw ← prod_at_bot_at_bot_eq, exact hf.prod_map_prod_at_bot hg, } lemma tendsto.prod_at_top [semilattice_sup α] [semilattice_sup γ] {f g : α → γ} (hf : tendsto f at_top at_top) (hg : tendsto g at_top at_top) : tendsto (prod.map f g) at_top at_top := by { rw ← prod_at_top_at_top_eq, exact hf.prod_map_prod_at_top hg, } lemma eventually_at_bot_prod_self [semilattice_inf α] [nonempty α] {p : α × α → Prop} : (∀ᶠ x in at_bot, p x) ↔ (∃ a, ∀ k l, k ≤ a → l ≤ a → p (k, l)) := by simp [← prod_at_bot_at_bot_eq, at_bot_basis.prod_self.eventually_iff] lemma eventually_at_top_prod_self [semilattice_sup α] [nonempty α] {p : α × α → Prop} : (∀ᶠ x in at_top, p x) ↔ (∃ a, ∀ k l, a ≤ k → a ≤ l → p (k, l)) := by simp [← prod_at_top_at_top_eq, at_top_basis.prod_self.eventually_iff] lemma eventually_at_bot_prod_self' [semilattice_inf α] [nonempty α] {p : α × α → Prop} : (∀ᶠ x in at_bot, p x) ↔ (∃ a, ∀ k ≤ a, ∀ l ≤ a, p (k, l)) := begin rw filter.eventually_at_bot_prod_self, apply exists_congr, tauto, end lemma eventually_at_top_prod_self' [semilattice_sup α] [nonempty α] {p : α × α → Prop} : (∀ᶠ x in at_top, p x) ↔ (∃ a, ∀ k ≥ a, ∀ l ≥ a, p (k, l)) := begin rw filter.eventually_at_top_prod_self, apply exists_congr, tauto, end lemma eventually_at_top_curry [semilattice_sup α] [semilattice_sup β] {p : α × β → Prop} (hp : ∀ᶠ (x : α × β) in filter.at_top, p x) : ∀ᶠ k in at_top, ∀ᶠ l in at_top, p (k, l) := begin rw ← prod_at_top_at_top_eq at hp, exact hp.curry, end lemma eventually_at_bot_curry [semilattice_inf α] [semilattice_inf β] {p : α × β → Prop} (hp : ∀ᶠ (x : α × β) in filter.at_bot, p x) : ∀ᶠ k in at_bot, ∀ᶠ l in at_bot, p (k, l) := @eventually_at_top_curry (order_dual α) (order_dual β) _ _ _ hp /-- A function `f` maps upwards closed sets (at_top sets) to upwards closed sets when it is a Galois insertion. The Galois "insertion" and "connection" is weakened to only require it to be an insertion and a connetion above `b'`. -/ lemma map_at_top_eq_of_gc [semilattice_sup α] [semilattice_sup β] {f : α → β} (g : β → α) (b' : β) (hf : monotone f) (gc : ∀a, ∀b≥b', f a ≤ b ↔ a ≤ g b) (hgi : ∀b≥b', b ≤ f (g b)) : map f at_top = at_top := begin refine le_antisymm (hf.tendsto_at_top_at_top $ λ b, ⟨g (b ⊔ b'), le_sup_left.trans $ hgi _ le_sup_right⟩) _, rw [@map_at_top_eq _ _ ⟨g b'⟩], refine le_infi (λ a, infi_le_of_le (f a ⊔ b') $ principal_mono.2 $ λ b hb, _), rw [mem_Ici, sup_le_iff] at hb, exact ⟨g b, (gc _ _ hb.2).1 hb.1, le_antisymm ((gc _ _ hb.2).2 le_rfl) (hgi _ hb.2)⟩ end lemma map_at_bot_eq_of_gc [semilattice_inf α] [semilattice_inf β] {f : α → β} (g : β → α) (b' : β) (hf : monotone f) (gc : ∀a, ∀b≤b', b ≤ f a ↔ g b ≤ a) (hgi : ∀b≤b', f (g b) ≤ b) : map f at_bot = at_bot := @map_at_top_eq_of_gc (order_dual α) (order_dual β) _ _ _ _ _ hf.dual gc hgi lemma map_coe_at_top_of_Ici_subset [semilattice_sup α] {a : α} {s : set α} (h : Ici a ⊆ s) : map (coe : s → α) at_top = at_top := begin have : directed (≥) (λ x : s, 𝓟 (Ici x)), { intros x y, use ⟨x ⊔ y ⊔ a, h le_sup_right⟩, simp only [ge_iff_le, principal_mono, Ici_subset_Ici, ← subtype.coe_le_coe, subtype.coe_mk], exact ⟨le_sup_left.trans le_sup_left, le_sup_right.trans le_sup_left⟩ }, haveI : nonempty s := ⟨⟨a, h le_rfl⟩⟩, simp only [le_antisymm_iff, at_top, le_infi_iff, le_principal_iff, mem_map, mem_set_of_eq, map_infi_eq this, map_principal], split, { intro x, refine mem_of_superset (mem_infi_of_mem ⟨x ⊔ a, h le_sup_right⟩ (mem_principal_self _)) _, rintro _ ⟨y, hy, rfl⟩, exact le_trans le_sup_left (subtype.coe_le_coe.2 hy) }, { intro x, filter_upwards [mem_at_top (↑x ⊔ a)] with b hb, exact ⟨⟨b, h $ le_sup_right.trans hb⟩, subtype.coe_le_coe.1 (le_sup_left.trans hb), rfl⟩, }, end /-- The image of the filter `at_top` on `Ici a` under the coercion equals `at_top`. -/ @[simp] lemma map_coe_Ici_at_top [semilattice_sup α] (a : α) : map (coe : Ici a → α) at_top = at_top := map_coe_at_top_of_Ici_subset (subset.refl _) /-- The image of the filter `at_top` on `Ioi a` under the coercion equals `at_top`. -/ @[simp] lemma map_coe_Ioi_at_top [semilattice_sup α] [no_max_order α] (a : α) : map (coe : Ioi a → α) at_top = at_top := let ⟨b, hb⟩ := exists_gt a in map_coe_at_top_of_Ici_subset $ Ici_subset_Ioi.2 hb /-- The `at_top` filter for an open interval `Ioi a` comes from the `at_top` filter in the ambient order. -/ lemma at_top_Ioi_eq [semilattice_sup α] (a : α) : at_top = comap (coe : Ioi a → α) at_top := begin nontriviality, rcases nontrivial_iff_nonempty.1 ‹_› with ⟨b, hb⟩, rw [← map_coe_at_top_of_Ici_subset (Ici_subset_Ioi.2 hb), comap_map subtype.coe_injective] end /-- The `at_top` filter for an open interval `Ici a` comes from the `at_top` filter in the ambient order. -/ lemma at_top_Ici_eq [semilattice_sup α] (a : α) : at_top = comap (coe : Ici a → α) at_top := by rw [← map_coe_Ici_at_top a, comap_map subtype.coe_injective] /-- The `at_bot` filter for an open interval `Iio a` comes from the `at_bot` filter in the ambient order. -/ @[simp] lemma map_coe_Iio_at_bot [semilattice_inf α] [no_min_order α] (a : α) : map (coe : Iio a → α) at_bot = at_bot := @map_coe_Ioi_at_top (order_dual α) _ _ _ /-- The `at_bot` filter for an open interval `Iio a` comes from the `at_bot` filter in the ambient order. -/ lemma at_bot_Iio_eq [semilattice_inf α] (a : α) : at_bot = comap (coe : Iio a → α) at_bot := @at_top_Ioi_eq (order_dual α) _ _ /-- The `at_bot` filter for an open interval `Iic a` comes from the `at_bot` filter in the ambient order. -/ @[simp] lemma map_coe_Iic_at_bot [semilattice_inf α] (a : α) : map (coe : Iic a → α) at_bot = at_bot := @map_coe_Ici_at_top (order_dual α) _ _ /-- The `at_bot` filter for an open interval `Iic a` comes from the `at_bot` filter in the ambient order. -/ lemma at_bot_Iic_eq [semilattice_inf α] (a : α) : at_bot = comap (coe : Iic a → α) at_bot := @at_top_Ici_eq (order_dual α) _ _ lemma tendsto_Ioi_at_top [semilattice_sup α] {a : α} {f : β → Ioi a} {l : filter β} : tendsto f l at_top ↔ tendsto (λ x, (f x : α)) l at_top := by rw [at_top_Ioi_eq, tendsto_comap_iff] lemma tendsto_Iio_at_bot [semilattice_inf α] {a : α} {f : β → Iio a} {l : filter β} : tendsto f l at_bot ↔ tendsto (λ x, (f x : α)) l at_bot := by rw [at_bot_Iio_eq, tendsto_comap_iff] lemma tendsto_Ici_at_top [semilattice_sup α] {a : α} {f : β → Ici a} {l : filter β} : tendsto f l at_top ↔ tendsto (λ x, (f x : α)) l at_top := by rw [at_top_Ici_eq, tendsto_comap_iff] lemma tendsto_Iic_at_bot [semilattice_inf α] {a : α} {f : β → Iic a} {l : filter β} : tendsto f l at_bot ↔ tendsto (λ x, (f x : α)) l at_bot := by rw [at_bot_Iic_eq, tendsto_comap_iff] @[simp] lemma tendsto_comp_coe_Ioi_at_top [semilattice_sup α] [no_max_order α] {a : α} {f : α → β} {l : filter β} : tendsto (λ x : Ioi a, f x) at_top l ↔ tendsto f at_top l := by rw [← map_coe_Ioi_at_top a, tendsto_map'_iff] @[simp] lemma tendsto_comp_coe_Ici_at_top [semilattice_sup α] {a : α} {f : α → β} {l : filter β} : tendsto (λ x : Ici a, f x) at_top l ↔ tendsto f at_top l := by rw [← map_coe_Ici_at_top a, tendsto_map'_iff] @[simp] lemma tendsto_comp_coe_Iio_at_bot [semilattice_inf α] [no_min_order α] {a : α} {f : α → β} {l : filter β} : tendsto (λ x : Iio a, f x) at_bot l ↔ tendsto f at_bot l := by rw [← map_coe_Iio_at_bot a, tendsto_map'_iff] @[simp] lemma tendsto_comp_coe_Iic_at_bot [semilattice_inf α] {a : α} {f : α → β} {l : filter β} : tendsto (λ x : Iic a, f x) at_bot l ↔ tendsto f at_bot l := by rw [← map_coe_Iic_at_bot a, tendsto_map'_iff] lemma map_add_at_top_eq_nat (k : ℕ) : map (λa, a + k) at_top = at_top := map_at_top_eq_of_gc (λa, a - k) k (assume a b h, add_le_add_right h k) (assume a b h, (le_tsub_iff_right h).symm) (assume a h, by rw [tsub_add_cancel_of_le h]) lemma map_sub_at_top_eq_nat (k : ℕ) : map (λa, a - k) at_top = at_top := map_at_top_eq_of_gc (λa, a + k) 0 (assume a b h, tsub_le_tsub_right h _) (assume a b _, tsub_le_iff_right) (assume b _, by rw [add_tsub_cancel_right]) lemma tendsto_add_at_top_nat (k : ℕ) : tendsto (λa, a + k) at_top at_top := le_of_eq (map_add_at_top_eq_nat k) lemma tendsto_sub_at_top_nat (k : ℕ) : tendsto (λa, a - k) at_top at_top := le_of_eq (map_sub_at_top_eq_nat k) lemma tendsto_add_at_top_iff_nat {f : ℕ → α} {l : filter α} (k : ℕ) : tendsto (λn, f (n + k)) at_top l ↔ tendsto f at_top l := show tendsto (f ∘ (λn, n + k)) at_top l ↔ tendsto f at_top l, by rw [← tendsto_map'_iff, map_add_at_top_eq_nat] lemma map_div_at_top_eq_nat (k : ℕ) (hk : 0 < k) : map (λa, a / k) at_top = at_top := map_at_top_eq_of_gc (λb, b * k + (k - 1)) 1 (assume a b h, nat.div_le_div_right h) (assume a b _, calc a / k ≤ b ↔ a / k < b + 1 : by rw [← nat.succ_eq_add_one, nat.lt_succ_iff] ... ↔ a < (b + 1) * k : nat.div_lt_iff_lt_mul _ _ hk ... ↔ _ : begin cases k, exact (lt_irrefl _ hk).elim, rw [add_mul, one_mul, nat.succ_sub_succ_eq_sub, tsub_zero, nat.add_succ, nat.lt_succ_iff], end) (assume b _, calc b = (b * k) / k : by rw [nat.mul_div_cancel b hk] ... ≤ (b * k + (k - 1)) / k : nat.div_le_div_right $ nat.le_add_right _ _) /-- If `u` is a monotone function with linear ordered codomain and the range of `u` is not bounded above, then `tendsto u at_top at_top`. -/ lemma tendsto_at_top_at_top_of_monotone' [preorder ι] [linear_order α] {u : ι → α} (h : monotone u) (H : ¬bdd_above (range u)) : tendsto u at_top at_top := begin apply h.tendsto_at_top_at_top, intro b, rcases not_bdd_above_iff.1 H b with ⟨_, ⟨N, rfl⟩, hN⟩, exact ⟨N, le_of_lt hN⟩, end /-- If `u` is a monotone function with linear ordered codomain and the range of `u` is not bounded below, then `tendsto u at_bot at_bot`. -/ lemma tendsto_at_bot_at_bot_of_monotone' [preorder ι] [linear_order α] {u : ι → α} (h : monotone u) (H : ¬bdd_below (range u)) : tendsto u at_bot at_bot := @tendsto_at_top_at_top_of_monotone' (order_dual ι) (order_dual α) _ _ _ h.dual H lemma unbounded_of_tendsto_at_top [nonempty α] [semilattice_sup α] [preorder β] [no_max_order β] {f : α → β} (h : tendsto f at_top at_top) : ¬ bdd_above (range f) := begin rintros ⟨M, hM⟩, cases mem_at_top_sets.mp (h $ Ioi_mem_at_top M) with a ha, apply lt_irrefl M, calc M < f a : ha a le_rfl ... ≤ M : hM (set.mem_range_self a) end lemma unbounded_of_tendsto_at_bot [nonempty α] [semilattice_sup α] [preorder β] [no_min_order β] {f : α → β} (h : tendsto f at_top at_bot) : ¬ bdd_below (range f) := @unbounded_of_tendsto_at_top _ (order_dual β) _ _ _ _ _ h lemma unbounded_of_tendsto_at_top' [nonempty α] [semilattice_inf α] [preorder β] [no_max_order β] {f : α → β} (h : tendsto f at_bot at_top) : ¬ bdd_above (range f) := @unbounded_of_tendsto_at_top (order_dual α) _ _ _ _ _ _ h lemma unbounded_of_tendsto_at_bot' [nonempty α] [semilattice_inf α] [preorder β] [no_min_order β] {f : α → β} (h : tendsto f at_bot at_bot) : ¬ bdd_below (range f) := @unbounded_of_tendsto_at_top (order_dual α) (order_dual β) _ _ _ _ _ h /-- If a monotone function `u : ι → α` tends to `at_top` along *some* non-trivial filter `l`, then it tends to `at_top` along `at_top`. -/ lemma tendsto_at_top_of_monotone_of_filter [preorder ι] [preorder α] {l : filter ι} {u : ι → α} (h : monotone u) [ne_bot l] (hu : tendsto u l at_top) : tendsto u at_top at_top := h.tendsto_at_top_at_top $ λ b, (hu.eventually (mem_at_top b)).exists /-- If a monotone function `u : ι → α` tends to `at_bot` along *some* non-trivial filter `l`, then it tends to `at_bot` along `at_bot`. -/ lemma tendsto_at_bot_of_monotone_of_filter [preorder ι] [preorder α] {l : filter ι} {u : ι → α} (h : monotone u) [ne_bot l] (hu : tendsto u l at_bot) : tendsto u at_bot at_bot := @tendsto_at_top_of_monotone_of_filter (order_dual ι) (order_dual α) _ _ _ _ h.dual _ hu lemma tendsto_at_top_of_monotone_of_subseq [preorder ι] [preorder α] {u : ι → α} {φ : ι' → ι} (h : monotone u) {l : filter ι'} [ne_bot l] (H : tendsto (u ∘ φ) l at_top) : tendsto u at_top at_top := tendsto_at_top_of_monotone_of_filter h (tendsto_map' H) lemma tendsto_at_bot_of_monotone_of_subseq [preorder ι] [preorder α] {u : ι → α} {φ : ι' → ι} (h : monotone u) {l : filter ι'} [ne_bot l] (H : tendsto (u ∘ φ) l at_bot) : tendsto u at_bot at_bot := tendsto_at_bot_of_monotone_of_filter h (tendsto_map' H) /-- Let `f` and `g` be two maps to the same commutative monoid. This lemma gives a sufficient condition for comparison of the filter `at_top.map (λ s, ∏ b in s, f b)` with `at_top.map (λ s, ∏ b in s, g b)`. This is useful to compare the set of limit points of `Π b in s, f b` as `s → at_top` with the similar set for `g`. -/ @[to_additive] lemma map_at_top_finset_prod_le_of_prod_eq [comm_monoid α] {f : β → α} {g : γ → α} (h_eq : ∀u:finset γ, ∃v:finset β, ∀v', v ⊆ v' → ∃u', u ⊆ u' ∧ ∏ x in u', g x = ∏ b in v', f b) : at_top.map (λs:finset β, ∏ b in s, f b) ≤ at_top.map (λs:finset γ, ∏ x in s, g x) := by rw [map_at_top_eq, map_at_top_eq]; from (le_infi $ assume b, let ⟨v, hv⟩ := h_eq b in infi_le_of_le v $ by simp [set.image_subset_iff]; exact hv) lemma has_antitone_basis.tendsto [semilattice_sup ι] [nonempty ι] {l : filter α} {s : ι → set α} (hl : l.has_antitone_basis s) {φ : ι → α} (h : ∀ i : ι, φ i ∈ s i) : tendsto φ at_top l := (at_top_basis.tendsto_iff hl.to_has_basis).2 $ assume i hi, ⟨i, trivial, λ j hij, hl.antitone hij (h _)⟩ /-- If `f` is a nontrivial countably generated filter, then there exists a sequence that converges to `f`. -/ lemma exists_seq_tendsto (f : filter α) [is_countably_generated f] [ne_bot f] : ∃ x : ℕ → α, tendsto x at_top f := begin obtain ⟨B, h⟩ := f.exists_antitone_basis, have := λ n, nonempty_of_mem (h.to_has_basis.mem_of_mem trivial : B n ∈ f), choose x hx, exact ⟨x, h.tendsto hx⟩ end /-- An abstract version of continuity of sequentially continuous functions on metric spaces: if a filter `k` is countably generated then `tendsto f k l` iff for every sequence `u` converging to `k`, `f ∘ u` tends to `l`. -/ lemma tendsto_iff_seq_tendsto {f : α → β} {k : filter α} {l : filter β} [k.is_countably_generated] : tendsto f k l ↔ (∀ x : ℕ → α, tendsto x at_top k → tendsto (f ∘ x) at_top l) := begin refine ⟨λ h x hx, h.comp hx, λ H s hs, _⟩, contrapose! H, haveI : ne_bot (k ⊓ 𝓟 (f ⁻¹' sᶜ)), by simpa [ne_bot_iff, inf_principal_eq_bot], rcases (k ⊓ 𝓟 (f ⁻¹' sᶜ)).exists_seq_tendsto with ⟨x, hx⟩, rw [tendsto_inf, tendsto_principal] at hx, refine ⟨x, hx.1, λ h, _⟩, rcases (hx.2.and (h hs)).exists with ⟨N, hnmem, hmem⟩, exact hnmem hmem end lemma tendsto_of_seq_tendsto {f : α → β} {k : filter α} {l : filter β} [k.is_countably_generated] : (∀ x : ℕ → α, tendsto x at_top k → tendsto (f ∘ x) at_top l) → tendsto f k l := tendsto_iff_seq_tendsto.2 lemma subseq_tendsto_of_ne_bot {f : filter α} [is_countably_generated f] {u : ℕ → α} (hx : ne_bot (f ⊓ map u at_top)) : ∃ (θ : ℕ → ℕ), (strict_mono θ) ∧ (tendsto (u ∘ θ) at_top f) := begin obtain ⟨B, h⟩ := f.exists_antitone_basis, have : ∀ N, ∃ n ≥ N, u n ∈ B N, from λ N, filter.inf_map_at_top_ne_bot_iff.mp hx _ (h.to_has_basis.mem_of_mem trivial) N, choose φ hφ using this, cases forall_and_distrib.mp hφ with φ_ge φ_in, have lim_uφ : tendsto (u ∘ φ) at_top f, from h.tendsto φ_in, have lim_φ : tendsto φ at_top at_top, from (tendsto_at_top_mono φ_ge tendsto_id), obtain ⟨ψ, hψ, hψφ⟩ : ∃ ψ : ℕ → ℕ, strict_mono ψ ∧ strict_mono (φ ∘ ψ), from strict_mono_subseq_of_tendsto_at_top lim_φ, exact ⟨φ ∘ ψ, hψφ, lim_uφ.comp hψ.tendsto_at_top⟩, end end filter open filter finset section variables {R : Type*} [linear_ordered_semiring R] lemma exists_lt_mul_self (a : R) : ∃ x ≥ 0, a < x * x := let ⟨x, hxa, hx0⟩ :=((tendsto_mul_self_at_top.eventually (eventually_gt_at_top a)).and (eventually_ge_at_top 0)).exists in ⟨x, hx0, hxa⟩ lemma exists_le_mul_self (a : R) : ∃ x ≥ 0, a ≤ x * x := let ⟨x, hx0, hxa⟩ := exists_lt_mul_self a in ⟨x, hx0, hxa.le⟩ end namespace order_iso variables [preorder α] [preorder β] @[simp] lemma comap_at_top (e : α ≃o β) : comap e at_top = at_top := by simp [at_top, ← e.surjective.infi_comp] @[simp] lemma comap_at_bot (e : α ≃o β) : comap e at_bot = at_bot := e.dual.comap_at_top @[simp] lemma map_at_top (e : α ≃o β) : map (e : α → β) at_top = at_top := by rw [← e.comap_at_top, map_comap_of_surjective e.surjective] @[simp] lemma map_at_bot (e : α ≃o β) : map (e : α → β) at_bot = at_bot := e.dual.map_at_top lemma tendsto_at_top (e : α ≃o β) : tendsto e at_top at_top := e.map_at_top.le lemma tendsto_at_bot (e : α ≃o β) : tendsto e at_bot at_bot := e.map_at_bot.le @[simp] lemma tendsto_at_top_iff {l : filter γ} {f : γ → α} (e : α ≃o β) : tendsto (λ x, e (f x)) l at_top ↔ tendsto f l at_top := by rw [← e.comap_at_top, tendsto_comap_iff] @[simp] lemma tendsto_at_bot_iff {l : filter γ} {f : γ → α} (e : α ≃o β) : tendsto (λ x, e (f x)) l at_bot ↔ tendsto f l at_bot := e.dual.tendsto_at_top_iff end order_iso /-- Let `g : γ → β` be an injective function and `f : β → α` be a function from the codomain of `g` to a commutative monoid. Suppose that `f x = 1` outside of the range of `g`. Then the filters `at_top.map (λ s, ∏ i in s, f (g i))` and `at_top.map (λ s, ∏ i in s, f i)` coincide. The additive version of this lemma is used to prove the equality `∑' x, f (g x) = ∑' y, f y` under the same assumptions.-/ @[to_additive] lemma function.injective.map_at_top_finset_prod_eq [comm_monoid α] {g : γ → β} (hg : function.injective g) {f : β → α} (hf : ∀ x ∉ set.range g, f x = 1) : map (λ s, ∏ i in s, f (g i)) at_top = map (λ s, ∏ i in s, f i) at_top := begin apply le_antisymm; refine map_at_top_finset_prod_le_of_prod_eq (λ s, _), { refine ⟨s.preimage g (hg.inj_on _), λ t ht, _⟩, refine ⟨t.image g ∪ s, finset.subset_union_right _ _, _⟩, rw [← finset.prod_image (hg.inj_on _)], refine (prod_subset (subset_union_left _ _) _).symm, simp only [finset.mem_union, finset.mem_image], refine λ y hy hyt, hf y (mt _ hyt), rintros ⟨x, rfl⟩, exact ⟨x, ht (finset.mem_preimage.2 $ hy.resolve_left hyt), rfl⟩ }, { refine ⟨s.image g, λ t ht, _⟩, simp only [← prod_preimage _ _ (hg.inj_on _) _ (λ x _, hf x)], exact ⟨_, (image_subset_iff_subset_preimage _).1 ht, rfl⟩ } end /-- Let `g : γ → β` be an injective function and `f : β → α` be a function from the codomain of `g` to an additive commutative monoid. Suppose that `f x = 0` outside of the range of `g`. Then the filters `at_top.map (λ s, ∑ i in s, f (g i))` and `at_top.map (λ s, ∑ i in s, f i)` coincide. This lemma is used to prove the equality `∑' x, f (g x) = ∑' y, f y` under the same assumptions.-/ add_decl_doc function.injective.map_at_top_finset_sum_eq
db0df227c86f250c928369ed4d65cd56b358c7e6
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/stage0/src/Init/Data/Range.lean
d1c9c982ac355529dc5839564c012c8792e892d7
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,006
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Meta import Init.Control.Foldable namespace Std -- We put `Range` in `Init` because we want the notation `[i:j]` without importing `Std` -- We don't put `Range` in the top-level namespace to avoid collisions with user defined types structure Range where start : Nat := 0 stop : Nat step : Nat := 1 namespace Range universes u v @[inline] protected def forIn {β : Type u} {m : Type u → Type v} [Monad m] (range : Range) (init : β) (f : Nat → β → m (ForInStep β)) : m β := let rec @[specialize] loop (i : Nat) (j : Nat) (b : β) : m β := do if j ≥ range.stop then pure b else match i with | 0 => pure b | i+1 => match ← f j b with | ForInStep.done b => pure b | ForInStep.yield b => loop i (j + range.step) b loop range.stop range.start init instance : ForIn m Range Nat where forIn := Range.forIn @[inline] protected def foldlM {β : Type u} {m : Type u → Type v} [Monad m] (f : β → Nat → m β) (init : β) (range : Range) : m β := let rec @[specialize] loop (i : Nat) (j : Nat) (b : β) : m β := do if j ≥ range.stop then pure b else match i with | 0 => pure b | i+1 => loop i (j + range.step) (← f b j) loop range.stop range.start init instance : Foldable m Range Nat where foldlM := Range.foldlM syntax:max "[" ":" term "]" : term syntax:max "[" term ":" term "]" : term syntax:max "[" ":" term ":" term "]" : term syntax:max "[" term ":" term ":" term "]" : term macro_rules | `([ : $stop]) => `({ stop := $stop : Range }) | `([ $start : $stop ]) => `({ start := $start, stop := $stop : Range }) | `([ $start : $stop : $step ]) => `({ start := $start, stop := $stop, step := $step : Range }) | `([ : $stop : $step ]) => `({ stop := $stop, step := $step : Range }) end Range end Std
96f7b2d8b2d94509ef26ad3ea1b9994098bacb03
491068d2ad28831e7dade8d6dff871c3e49d9431
/tests/lean/noncomp_error.lean
8f49f6777b8cd61bbd80294b7d5550d162d0ebf0
[ "Apache-2.0" ]
permissive
davidmueller13/lean
65a3ed141b4088cd0a268e4de80eb6778b21a0e9
c626e2e3c6f3771e07c32e82ee5b9e030de5b050
refs/heads/master
1,611,278,313,401
1,444,021,177,000
1,444,021,177,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
32
lean
noncomputable definition a := 2
4fc0964e86b53caa013444d0a8bd7d563bca9928
618003631150032a5676f229d13a079ac875ff77
/src/data/list/pairwise.lean
26e327684d71edde53c29332a892e7203f57d1c9
[ "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
14,243
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.list.basic open nat function universes u v variables {α : Type u} {β : Type v} namespace list /- pairwise relation (generalized no duplicate) -/ mk_iff_of_inductive_prop list.pairwise list.pairwise_iff variable {R : α → α → Prop} theorem rel_of_pairwise_cons {a : α} {l : list α} (p : pairwise R (a::l)) : ∀ {a'}, a' ∈ l → R a a' := (pairwise_cons.1 p).1 theorem pairwise_of_pairwise_cons {a : α} {l : list α} (p : pairwise R (a::l)) : pairwise R l := (pairwise_cons.1 p).2 theorem pairwise.imp_of_mem {S : α → α → Prop} {l : list α} (H : ∀ {a b}, a ∈ l → b ∈ l → R a b → S a b) (p : pairwise R l) : pairwise S l := begin induction p with a l r p IH generalizing H; constructor, { exact ball.imp_right (λ x h, H (mem_cons_self _ _) (mem_cons_of_mem _ h)) r }, { exact IH (λ a b m m', H (mem_cons_of_mem _ m) (mem_cons_of_mem _ m')) } end theorem pairwise.imp {S : α → α → Prop} (H : ∀ a b, R a b → S a b) {l : list α} : pairwise R l → pairwise S l := pairwise.imp_of_mem (λ a b _ _, H a b) theorem pairwise.and {S : α → α → Prop} {l : list α} : pairwise (λ a b, R a b ∧ S a b) l ↔ pairwise R l ∧ pairwise S l := ⟨λ h, ⟨h.imp (λ a b h, h.1), h.imp (λ a b h, h.2)⟩, λ ⟨hR, hS⟩, begin clear_, induction hR with a l R1 R2 IH; simp only [pairwise.nil, pairwise_cons] at *, exact ⟨λ b bl, ⟨R1 b bl, hS.1 b bl⟩, IH hS.2⟩ end⟩ theorem pairwise.imp₂ {S : α → α → Prop} {T : α → α → Prop} (H : ∀ a b, R a b → S a b → T a b) {l : list α} (hR : pairwise R l) (hS : pairwise S l) : pairwise T l := (pairwise.and.2 ⟨hR, hS⟩).imp $ λ a b, and.rec (H a b) theorem pairwise.iff_of_mem {S : α → α → Prop} {l : list α} (H : ∀ {a b}, a ∈ l → b ∈ l → (R a b ↔ S a b)) : pairwise R l ↔ pairwise S l := ⟨pairwise.imp_of_mem (λ a b m m', (H m m').1), pairwise.imp_of_mem (λ a b m m', (H m m').2)⟩ theorem pairwise.iff {S : α → α → Prop} (H : ∀ a b, R a b ↔ S a b) {l : list α} : pairwise R l ↔ pairwise S l := pairwise.iff_of_mem (λ a b _ _, H a b) theorem pairwise_of_forall {l : list α} (H : ∀ x y, R x y) : pairwise R l := by induction l; [exact pairwise.nil, simp only [*, pairwise_cons, forall_2_true_iff, and_true]] theorem pairwise.and_mem {l : list α} : pairwise R l ↔ pairwise (λ x y, x ∈ l ∧ y ∈ l ∧ R x y) l := pairwise.iff_of_mem (by simp only [true_and, iff_self, forall_2_true_iff] {contextual := tt}) theorem pairwise.imp_mem {l : list α} : pairwise R l ↔ pairwise (λ x y, x ∈ l → y ∈ l → R x y) l := pairwise.iff_of_mem (by simp only [forall_prop_of_true, iff_self, forall_2_true_iff] {contextual := tt}) theorem pairwise_of_sublist : Π {l₁ l₂ : list α}, l₁ <+ l₂ → pairwise R l₂ → pairwise R l₁ | ._ ._ sublist.slnil h := h | ._ ._ (sublist.cons l₁ l₂ a s) (pairwise.cons i n) := pairwise_of_sublist s n | ._ ._ (sublist.cons2 l₁ l₂ a s) (pairwise.cons i n) := (pairwise_of_sublist s n).cons (ball.imp_left s.subset i) theorem forall_of_forall_of_pairwise (H : symmetric R) {l : list α} (H₁ : ∀ x ∈ l, R x x) (H₂ : pairwise R l) : ∀ (x ∈ l) (y ∈ l), R x y := begin induction l with a l IH, { exact forall_mem_nil _ }, cases forall_mem_cons.1 H₁ with H₁₁ H₁₂, cases pairwise_cons.1 H₂ with H₂₁ H₂₂, rintro x (rfl | hx) y (rfl | hy), exacts [H₁₁, H₂₁ _ hy, H (H₂₁ _ hx), IH H₁₂ H₂₂ _ hx _ hy] end lemma forall_of_pairwise (H : symmetric R) {l : list α} (hl : pairwise R l) : (∀a∈l, ∀b∈l, a ≠ b → R a b) := forall_of_forall_of_pairwise (λ a b h hne, H (h hne.symm)) (λ _ _ h, (h rfl).elim) (pairwise.imp (λ _ _ h _, h) hl) theorem pairwise_singleton (R) (a : α) : pairwise R [a] := by simp only [pairwise_cons, mem_singleton, forall_prop_of_false (not_mem_nil _), forall_true_iff, pairwise.nil, and_true] theorem pairwise_pair {a b : α} : pairwise R [a, b] ↔ R a b := by simp only [pairwise_cons, mem_singleton, forall_eq, forall_prop_of_false (not_mem_nil _), forall_true_iff, pairwise.nil, and_true] theorem pairwise_append {l₁ l₂ : list α} : pairwise R (l₁++l₂) ↔ pairwise R l₁ ∧ pairwise R l₂ ∧ ∀ x ∈ l₁, ∀ y ∈ l₂, R x y := by induction l₁ with x l₁ IH; [simp only [list.pairwise.nil, forall_prop_of_false (not_mem_nil _), forall_true_iff, and_true, true_and, nil_append], simp only [cons_append, pairwise_cons, forall_mem_append, IH, forall_mem_cons, forall_and_distrib, and_assoc, and.left_comm]] theorem pairwise_append_comm (s : symmetric R) {l₁ l₂ : list α} : pairwise R (l₁++l₂) ↔ pairwise R (l₂++l₁) := have ∀ l₁ l₂ : list α, (∀ (x : α), x ∈ l₁ → ∀ (y : α), y ∈ l₂ → R x y) → (∀ (x : α), x ∈ l₂ → ∀ (y : α), y ∈ l₁ → R x y), from λ l₁ l₂ a x xm y ym, s (a y ym x xm), by simp only [pairwise_append, and.left_comm]; rw iff.intro (this l₁ l₂) (this l₂ l₁) theorem pairwise_middle (s : symmetric R) {a : α} {l₁ l₂ : list α} : pairwise R (l₁ ++ a::l₂) ↔ pairwise R (a::(l₁++l₂)) := show pairwise R (l₁ ++ ([a] ++ l₂)) ↔ pairwise R ([a] ++ l₁ ++ l₂), by rw [← append_assoc, pairwise_append, @pairwise_append _ _ ([a] ++ l₁), pairwise_append_comm s]; simp only [mem_append, or_comm] theorem pairwise_map (f : β → α) : ∀ {l : list β}, pairwise R (map f l) ↔ pairwise (λ a b : β, R (f a) (f b)) l | [] := by simp only [map, pairwise.nil] | (b::l) := have (∀ a b', b' ∈ l → f b' = a → R (f b) a) ↔ ∀ (b' : β), b' ∈ l → R (f b) (f b'), from forall_swap.trans $ forall_congr $ λ a, forall_swap.trans $ by simp only [forall_eq'], by simp only [map, pairwise_cons, mem_map, exists_imp_distrib, and_imp, this, pairwise_map] theorem pairwise_of_pairwise_map {S : β → β → Prop} (f : α → β) (H : ∀ a b : α, S (f a) (f b) → R a b) {l : list α} (p : pairwise S (map f l)) : pairwise R l := ((pairwise_map f).1 p).imp H theorem pairwise_map_of_pairwise {S : β → β → Prop} (f : α → β) (H : ∀ a b : α, R a b → S (f a) (f b)) {l : list α} (p : pairwise R l) : pairwise S (map f l) := (pairwise_map f).2 $ p.imp H theorem pairwise_filter_map (f : β → option α) {l : list β} : pairwise R (filter_map f l) ↔ pairwise (λ a a' : β, ∀ (b ∈ f a) (b' ∈ f a'), R b b') l := let S (a a' : β) := ∀ (b ∈ f a) (b' ∈ f a'), R b b' in begin simp only [option.mem_def], induction l with a l IH, { simp only [filter_map, pairwise.nil] }, cases e : f a with b, { rw [filter_map_cons_none _ _ e, IH, pairwise_cons], simp only [e, forall_prop_of_false not_false, forall_3_true_iff, true_and] }, rw [filter_map_cons_some _ _ _ e], simp only [pairwise_cons, mem_filter_map, exists_imp_distrib, and_imp, IH, e, forall_eq'], show (∀ (a' : α) (x : β), x ∈ l → f x = some a' → R b a') ∧ pairwise S l ↔ (∀ (a' : β), a' ∈ l → ∀ (b' : α), f a' = some b' → R b b') ∧ pairwise S l, from and_congr ⟨λ h b mb a ma, h a b mb ma, λ h a b mb ma, h b mb a ma⟩ iff.rfl end theorem pairwise_filter_map_of_pairwise {S : β → β → Prop} (f : α → option β) (H : ∀ (a a' : α), R a a' → ∀ (b ∈ f a) (b' ∈ f a'), S b b') {l : list α} (p : pairwise R l) : pairwise S (filter_map f l) := (pairwise_filter_map _).2 $ p.imp H theorem pairwise_filter (p : α → Prop) [decidable_pred p] {l : list α} : pairwise R (filter p l) ↔ pairwise (λ x y, p x → p y → R x y) l := begin rw [← filter_map_eq_filter, pairwise_filter_map], apply pairwise.iff, intros, simp only [option.mem_def, option.guard_eq_some, and_imp, forall_eq'], end theorem pairwise_filter_of_pairwise (p : α → Prop) [decidable_pred p] {l : list α} : pairwise R l → pairwise R (filter p l) := pairwise_of_sublist (filter_sublist _) theorem pairwise_join {L : list (list α)} : pairwise R (join L) ↔ (∀ l ∈ L, pairwise R l) ∧ pairwise (λ l₁ l₂, ∀ (x ∈ l₁) (y ∈ l₂), R x y) L := begin induction L with l L IH, {simp only [join, pairwise.nil, forall_prop_of_false (not_mem_nil _), forall_const, and_self]}, have : (∀ (x : α), x ∈ l → ∀ (y : α) (x_1 : list α), x_1 ∈ L → y ∈ x_1 → R x y) ↔ ∀ (a' : list α), a' ∈ L → ∀ (x : α), x ∈ l → ∀ (y : α), y ∈ a' → R x y := ⟨λ h a b c d e, h c d e a b, λ h c d e a b, h a b c d e⟩, simp only [join, pairwise_append, IH, mem_join, exists_imp_distrib, and_imp, this, forall_mem_cons, pairwise_cons], simp only [and_assoc, and_comm, and.left_comm], end @[simp] theorem pairwise_reverse : ∀ {R} {l : list α}, pairwise R (reverse l) ↔ pairwise (λ x y, R y x) l := suffices ∀ {R l}, @pairwise α R l → pairwise (λ x y, R y x) (reverse l), from λ R l, ⟨λ p, reverse_reverse l ▸ this p, this⟩, λ R l p, by induction p with a l h p IH; [apply pairwise.nil, simpa only [reverse_cons, pairwise_append, IH, pairwise_cons, forall_prop_of_false (not_mem_nil _), forall_true_iff, pairwise.nil, mem_reverse, mem_singleton, forall_eq, true_and] using h] theorem pairwise_iff_nth_le {R} : ∀ {l : list α}, pairwise R l ↔ ∀ i j (h₁ : j < length l) (h₂ : i < j), R (nth_le l i (lt_trans h₂ h₁)) (nth_le l j h₁) | [] := by simp only [pairwise.nil, true_iff]; exact λ i j h, (not_lt_zero j).elim h | (a::l) := begin rw [pairwise_cons, pairwise_iff_nth_le], refine ⟨λ H i j h₁ h₂, _, λ H, ⟨λ a' m, _, λ i j h₁ h₂, H _ _ (succ_lt_succ h₁) (succ_lt_succ h₂)⟩⟩, { cases j with j, {exact (not_lt_zero _).elim h₂}, cases i with i, { exact H.1 _ (nth_le_mem l _ _) }, { exact H.2 _ _ (lt_of_succ_lt_succ h₁) (lt_of_succ_lt_succ h₂) } }, { rcases nth_le_of_mem m with ⟨n, h, rfl⟩, exact H _ _ (succ_lt_succ h) (succ_pos _) } end theorem pairwise_sublists' {R} : ∀ {l : list α}, pairwise R l → pairwise (lex (swap R)) (sublists' l) | _ pairwise.nil := pairwise_singleton _ _ | _ (@pairwise.cons _ _ a l H₁ H₂) := begin simp only [sublists'_cons, pairwise_append, pairwise_map, mem_sublists', mem_map, exists_imp_distrib, and_imp], have IH := pairwise_sublists' H₂, refine ⟨IH, IH.imp (λ l₁ l₂, lex.cons), _⟩, intros l₁ sl₁ x l₂ sl₂ e, subst e, cases l₁ with b l₁, {constructor}, exact lex.rel (H₁ _ $ sl₁.subset $ mem_cons_self _ _) end theorem pairwise_sublists {R} {l : list α} (H : pairwise R l) : pairwise (λ l₁ l₂, lex R (reverse l₁) (reverse l₂)) (sublists l) := by have := pairwise_sublists' (pairwise_reverse.2 H); rwa [sublists'_reverse, pairwise_map] at this /- pairwise reduct -/ variable [decidable_rel R] @[simp] theorem pw_filter_nil : pw_filter R [] = [] := rfl @[simp] theorem pw_filter_cons_of_pos {a : α} {l : list α} (h : ∀ b ∈ pw_filter R l, R a b) : pw_filter R (a::l) = a :: pw_filter R l := if_pos h @[simp] theorem pw_filter_cons_of_neg {a : α} {l : list α} (h : ¬ ∀ b ∈ pw_filter R l, R a b) : pw_filter R (a::l) = pw_filter R l := if_neg h theorem pw_filter_map (f : β → α) : Π (l : list β), pw_filter R (map f l) = map f (pw_filter (λ x y, R (f x) (f y)) l) | [] := rfl | (x :: xs) := if h : ∀ b ∈ pw_filter R (map f xs), R (f x) b then have h' : ∀ (b : β), b ∈ pw_filter (λ (x y : β), R (f x) (f y)) xs → R (f x) (f b), from λ b hb, h _ (by rw [pw_filter_map]; apply mem_map_of_mem _ hb), by rw [map,pw_filter_cons_of_pos h,pw_filter_cons_of_pos h',pw_filter_map,map] else have h' : ¬∀ (b : β), b ∈ pw_filter (λ (x y : β), R (f x) (f y)) xs → R (f x) (f b), from λ hh, h $ λ a ha, by { rw [pw_filter_map,mem_map] at ha, rcases ha with ⟨b,hb₀,hb₁⟩, subst a, exact hh _ hb₀, }, by rw [map,pw_filter_cons_of_neg h,pw_filter_cons_of_neg h',pw_filter_map] theorem pw_filter_sublist : ∀ (l : list α), pw_filter R l <+ l | [] := nil_sublist _ | (x::l) := begin by_cases (∀ y ∈ pw_filter R l, R x y), { rw [pw_filter_cons_of_pos h], exact cons_sublist_cons _ (pw_filter_sublist l) }, { rw [pw_filter_cons_of_neg h], exact sublist_cons_of_sublist _ (pw_filter_sublist l) }, end theorem pw_filter_subset (l : list α) : pw_filter R l ⊆ l := (pw_filter_sublist _).subset theorem pairwise_pw_filter : ∀ (l : list α), pairwise R (pw_filter R l) | [] := pairwise.nil | (x::l) := begin by_cases (∀ y ∈ pw_filter R l, R x y), { rw [pw_filter_cons_of_pos h], exact pairwise_cons.2 ⟨h, pairwise_pw_filter l⟩ }, { rw [pw_filter_cons_of_neg h], exact pairwise_pw_filter l }, end theorem pw_filter_eq_self {l : list α} : pw_filter R l = l ↔ pairwise R l := ⟨λ e, e ▸ pairwise_pw_filter l, λ p, begin induction l with x l IH, {refl}, cases pairwise_cons.1 p with al p, rw [pw_filter_cons_of_pos (ball.imp_left (pw_filter_subset l) al), IH p], end⟩ @[simp] theorem pw_filter_idempotent {l : list α} : pw_filter R (pw_filter R l) = pw_filter R l := pw_filter_eq_self.mpr (pairwise_pw_filter l) theorem forall_mem_pw_filter (neg_trans : ∀ {x y z}, R x z → R x y ∨ R y z) (a : α) (l : list α) : (∀ b ∈ pw_filter R l, R a b) ↔ (∀ b ∈ l, R a b) := ⟨begin induction l with x l IH, { exact λ _ _, false.elim }, simp only [forall_mem_cons], by_cases (∀ y ∈ pw_filter R l, R x y); dsimp at h, { simp only [pw_filter_cons_of_pos h, forall_mem_cons, and_imp], exact λ r H, ⟨r, IH H⟩ }, { rw [pw_filter_cons_of_neg h], refine λ H, ⟨_, IH H⟩, cases e : find (λ y, ¬ R x y) (pw_filter R l) with k, { refine h.elim (ball.imp_right _ (find_eq_none.1 e)), exact λ y _, not_not.1 }, { have := find_some e, exact (neg_trans (H k (find_mem e))).resolve_right this } } end, ball.imp_left (pw_filter_subset l)⟩ end list
44ea61cfd1fb60f69fa6536dae1c92358b4cdfb0
35b83be3126daae10419b573c55e1fed009d3ae8
/src/Amy_Lean_Try/Amy_Complexes.lean
6b72114838e362866d5cbf47e47d218733e57ea4
[]
no_license
AHassan1024/Lean_Playground
ccb25b72029d199c0d23d002db2d32a9f2689ebc
a00b004c3a2eb9e3e863c361aa2b115260472414
refs/heads/master
1,586,221,905,125
1,544,951,310,000
1,544,951,310,000
157,934,290
0
0
null
null
null
null
UTF-8
Lean
false
false
2,883
lean
-- the result of trials by Ameena Hassan, 15th November 2018 import data.real.basic -- not import data.complex.basic import tactic.ring structure complex : Type := (re : ℝ) (im : ℝ) notation `∁` := complex definition z : ∁ := ⟨5, 6⟩ definition z2: ∁ := {re := 5, im := 6} --example : z = z2 := rfl namespace complex theorem ext (z w : ∁) : z.re = w.re ∧ z.im = w.im → z = w := begin cases z, cases w, intros, simp * at *, end instance : has_coe ℝ ∁ := ⟨λ x, {re := x, im := 0}⟩ definition I : ∁ := ⟨0, 1⟩ definition add : ∁ → ∁ → ∁ := λ z w, ⟨z.re + w.re, z.im + w.im⟩ instance : has_add ∁ := ⟨complex.add⟩ @[simp] theorem add_re (z w : ∁) : (z + w).re = z.re + w.re := rfl @[simp] theorem add_im (z w : ∁) : (z + w).im = z.im + w.im := rfl theorem add_comm : ∀ z w : ∁, z + w = w + z := begin intros, apply ext, split, all_goals {simp}, end theorem add_assoc : ∀ a b c : ∁, a + (b + c) = (a + b) + c := begin intros, apply ext, split, all_goals {simp}, end definition neg : ∁ → ∁ := λ z, ⟨-z.re, -z.im⟩ instance : has_neg ∁ := ⟨complex.neg⟩ @[simp] theorem neg_re (z : ∁) : (-z).re = -z.re := rfl @[simp] theorem neg_im (z : ∁) : (-z).im = -z.im := rfl definition mul : ∁ → ∁ → ∁ := λ z w, ⟨z.re * w.re - z.im * w.im, z.re * w.im + z.im * w.re⟩ instance : has_mul ∁ := ⟨complex.mul⟩ @[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 theorem mul_assoc : ∀ a b c : ∁, a * (b * c) = (a * b) * c := begin intros, apply ext, split, all_goals {simp}, all_goals {ring}, end meta def CORIP : tactic unit := do `[intros], `[apply ext], `[split], `[all_goals {simp}], `[all_goals {ring}] theorem mul_assoc' (a b c : ∁) : a * (b * c) = (a * b) * c := by CORIP theorem left_distrib (a b c : ∁) : a * (b + c) = a * b + a * c := by CORIP @[simp] definition one : ∁ := ⟨1, 0⟩ @[simp] definition zero : ∁ := ⟨0, 0⟩ theorem one_mul (a : ∁) : complex.one * a = a := by CORIP theorem zero_mul (a : ∁) : complex.zero * a = zero := by CORIP -- instance : comm_ring ∁ := -- by refine { mul := (*), -- add := (+), -- .. -- finilaize this part of mult one and mult zero -- } end complex -- theorem mul_assoc' (a b c : ∁) : a * (b*c) = (a*b)*c := by CORIP -- theorem left_distrib (a b c : ∁) : a *(b+c) = a*b + a*c := by CORIP -- @[simp] definition one : ∁ := (1, 0) -- @[simp] definition zero : ∁ := (0, 0) -- theorem one_mul (a : ∁) : complex.one * a = a := by CORIP -- theorem zero_mul (a: ∁) : complex.zero * a = 0 := by CORIP -- instance : comm_ring ∁ := -- by refine { mul := (*), -- add := (+), -- .. -- }; {by CORIP}
5685fb59de7ad84adfb5afc423d2a91b754d09ac
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/topology/separation.lean
e039edd7eb19cbc6b53495f1fde2c0e2080c48fa
[ "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
57,504
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.subset_properties import topology.connected /-! # Separation properties of topological spaces. This file defines the predicate `separated`, and common separation axioms (under the Kolmogorov classification). ## Main definitions * `separated`: Two `set`s are separated if they are contained in disjoint open sets. * `t0_space`: A T₀/Kolmogorov space is a space where, for every two points `x ≠ y`, there is an open set that contains one, but not the other. * `t1_space`: A T₁/Fréchet space is a space where every singleton set is closed. This is equivalent to, for every pair `x ≠ y`, there existing an open set containing `x` but not `y` (`t1_iff_exists_open` shows that these conditions are equivalent.) * `t2_space`: A T₂/Hausdorff space is a space where, for every two points `x ≠ y`, there is two disjoint open sets, one containing `x`, and the other `y`. * `t2_5_space`: A T₂.₅/Urysohn space is a space where, for every two points `x ≠ y`, there is two open sets, one containing `x`, and the other `y`, whose closures are disjoint. * `regular_space`: A T₃ space (sometimes referred to as regular, but authors vary on whether this includes T₂; `mathlib` does), is one where given any closed `C` and `x ∉ C`, there is disjoint open sets containing `x` and `C` respectively. In `mathlib`, T₃ implies T₂.₅. * `normal_space`: A T₄ space (sometimes referred to as normal, but authors vary on whether this includes T₂; `mathlib` does), is one where given two disjoint closed sets, we can find two open sets that separate them. In `mathlib`, T₄ implies T₃. ## Main results ### T₀ spaces * `is_closed.exists_closed_singleton` Given a closed set `S` in a compact T₀ space, there is some `x ∈ S` such that `{x}` is closed. * `exists_open_singleton_of_open_finset` Given an open `finset` `S` in a T₀ space, there is some `x ∈ S` such that `{x}` is open. ### T₁ spaces * `is_closed_map_const`: The constant map is a closed map. * `discrete_of_t1_of_finite`: A finite T₁ space must have the discrete topology. ### T₂ spaces * `t2_iff_nhds`: A space is T₂ iff the neighbourhoods of distinct points generate the bottom filter. * `t2_iff_is_closed_diagonal`: A space is T₂ iff the `diagonal` of `α` (that is, the set of all points of the form `(a, a) : α × α`) is closed under the product topology. * `finset_disjoing_finset_opens_of_t2`: Any two disjoint finsets are `separated`. * Most topological constructions preserve Hausdorffness; these results are part of the typeclass inference system (e.g. `embedding.t2_space`) * `set.eq_on.closure`: If two functions are equal on some set `s`, they are equal on its closure. * `is_compact.is_closed`: All compact sets are closed. * `locally_compact_of_compact_nhds`: If every point has a compact neighbourhood, then the space is locally compact. * `tot_sep_of_zero_dim`: If `α` has a clopen basis, it is a `totally_separated_space`. * `loc_compact_t2_tot_disc_iff_tot_sep`: A locally compact T₂ space is totally disconnected iff it is totally separated. If the space is also compact: * `normal_of_compact_t2`: A compact T₂ space is a `normal_space`. * `connected_components_eq_Inter_clopen`: The connected component of a point is the intersection of all its clopen neighbourhoods. * `compact_t2_tot_disc_iff_tot_sep`: Being a `totally_disconnected_space` is equivalent to being a `totally_separated_space`. * `connected_components.t2`: `connected_components α` is T₂ for `α` T₂ and compact. ### T₃ spaces * `disjoint_nested_nhds`: Given two points `x ≠ y`, we can find neighbourhoods `x ∈ V₁ ⊆ U₁` and `y ∈ V₂ ⊆ U₂`, with the `Vₖ` closed and the `Uₖ` open, such that the `Uₖ` are disjoint. ### Discrete spaces * `discrete_topology_iff_nhds`: Discrete topological spaces are those whose neighbourhood filters are the `pure` filter (which is the principal filter at a singleton). * `induced_bot`/`discrete_topology_induced`: The pullback of the discrete topology under an inclusion is the discrete topology. ## References https://en.wikipedia.org/wiki/Separation_axiom -/ open set filter open_locale topological_space filter classical universes u v variables {α : Type u} {β : Type v} [topological_space α] section separation /-- `separated` is a predicate on pairs of sub`set`s of a topological space. It holds if the two sub`set`s are contained in disjoint open sets. -/ def separated : set α → set α → Prop := λ (s t : set α), ∃ U V : (set α), (is_open U) ∧ is_open V ∧ (s ⊆ U) ∧ (t ⊆ V) ∧ disjoint U V namespace separated open separated @[symm] lemma symm {s t : set α} : separated s t → separated t s := λ ⟨U, V, oU, oV, aU, bV, UV⟩, ⟨V, U, oV, oU, bV, aU, disjoint.symm UV⟩ lemma comm (s t : set α) : separated s t ↔ separated t s := ⟨symm, symm⟩ lemma empty_right (a : set α) : separated a ∅ := ⟨_, _, is_open_univ, is_open_empty, λ a h, mem_univ a, λ a h, by cases h, disjoint_empty _⟩ lemma empty_left (a : set α) : separated ∅ a := (empty_right _).symm lemma union_left {a b c : set α} : separated a c → separated b c → separated (a ∪ b) c := λ ⟨U, V, oU, oV, aU, bV, UV⟩ ⟨W, X, oW, oX, aW, bX, WX⟩, ⟨U ∪ W, V ∩ X, is_open.union oU oW, is_open.inter oV oX, union_subset_union aU aW, subset_inter bV bX, set.disjoint_union_left.mpr ⟨disjoint_of_subset_right (inter_subset_left _ _) UV, disjoint_of_subset_right (inter_subset_right _ _) WX⟩⟩ lemma union_right {a b c : set α} (ab : separated a b) (ac : separated a c) : separated a (b ∪ c) := (ab.symm.union_left ac.symm).symm end separated /-- A T₀ space, also known as a Kolmogorov space, is a topological space where for every pair `x ≠ y`, there is an open set containing one but not the other. -/ class t0_space (α : Type u) [topological_space α] : Prop := (t0 : ∀ x y, x ≠ y → ∃ U:set α, is_open U ∧ (xor (x ∈ U) (y ∈ U))) /-- Given a closed set `S` in a compact T₀ space, there is some `x ∈ S` such that `{x}` is closed. -/ theorem is_closed.exists_closed_singleton {α : Type*} [topological_space α] [t0_space α] [compact_space α] {S : set α} (hS : is_closed S) (hne : S.nonempty) : ∃ (x : α), x ∈ S ∧ is_closed ({x} : set α) := begin obtain ⟨V, Vsub, Vne, Vcls, hV⟩ := hS.exists_minimal_nonempty_closed_subset hne, by_cases hnt : ∃ (x y : α) (hx : x ∈ V) (hy : y ∈ V), x ≠ y, { exfalso, obtain ⟨x, y, hx, hy, hne⟩ := hnt, obtain ⟨U, hU, hsep⟩ := t0_space.t0 _ _ hne, have : ∀ (z w : α) (hz : z ∈ V) (hw : w ∈ V) (hz' : z ∈ U) (hw' : ¬ w ∈ U), false, { intros z w hz hw hz' hw', have uvne : (V ∩ Uᶜ).nonempty, { use w, simp only [hw, hw', set.mem_inter_eq, not_false_iff, and_self, set.mem_compl_eq], }, specialize hV (V ∩ Uᶜ) (set.inter_subset_left _ _) uvne (is_closed.inter Vcls (is_closed_compl_iff.mpr hU)), have : V ⊆ Uᶜ, { rw ←hV, exact set.inter_subset_right _ _ }, exact this hz hz', }, cases hsep, { exact this x y hx hy hsep.1 hsep.2 }, { exact this y x hy hx hsep.1 hsep.2 } }, { push_neg at hnt, obtain ⟨z, hz⟩ := Vne, refine ⟨z, Vsub hz, _⟩, convert Vcls, ext, simp only [set.mem_singleton_iff, set.mem_compl_eq], split, { rintro rfl, exact hz, }, { exact λ hx, hnt x z hx hz, }, }, end /-- Given an open `finset` `S` in a T₀ space, there is some `x ∈ S` such that `{x}` is open. -/ theorem exists_open_singleton_of_open_finset [t0_space α] (s : finset α) (sne : s.nonempty) (hso : is_open (s : set α)) : ∃ x ∈ s, is_open ({x} : set α):= begin induction s using finset.strong_induction_on with s ihs, by_cases hs : set.subsingleton (s : set α), { rcases sne with ⟨x, hx⟩, refine ⟨x, hx, _⟩, have : (s : set α) = {x}, from hs.eq_singleton_of_mem hx, rwa this at hso }, { dunfold set.subsingleton at hs, push_neg at hs, rcases hs with ⟨x, hx, y, hy, hxy⟩, rcases t0_space.t0 x y hxy with ⟨U, hU, hxyU⟩, wlog H : x ∈ U ∧ y ∉ U := hxyU using [x y, y x], obtain ⟨z, hzs, hz⟩ : ∃ z ∈ s.filter (λ z, z ∈ U), is_open ({z} : set α), { refine ihs _ (finset.filter_ssubset.2 ⟨y, hy, H.2⟩) ⟨x, finset.mem_filter.2 ⟨hx, H.1⟩⟩ _, rw [finset.coe_filter], exact is_open.inter hso hU }, exact ⟨z, (finset.mem_filter.1 hzs).1, hz⟩ } end theorem exists_open_singleton_of_fintype [t0_space α] [f : fintype α] [ha : nonempty α] : ∃ x:α, is_open ({x}:set α) := begin refine ha.elim (λ x, _), have : is_open ((finset.univ : finset α) : set α), { simp }, rcases exists_open_singleton_of_open_finset _ ⟨x, finset.mem_univ x⟩ this with ⟨x, _, hx⟩, exact ⟨x, hx⟩ end instance subtype.t0_space [t0_space α] {p : α → Prop} : t0_space (subtype p) := ⟨λ x y hxy, let ⟨U, hU, hxyU⟩ := t0_space.t0 (x:α) y ((not_congr subtype.ext_iff_val).1 hxy) in ⟨(coe : subtype p → α) ⁻¹' U, is_open_induced hU, hxyU⟩⟩ /-- A T₁ space, also known as a Fréchet space, is a topological space where every singleton set is closed. Equivalently, for every pair `x ≠ y`, there is an open set containing `x` and not `y`. -/ class t1_space (α : Type u) [topological_space α] : Prop := (t1 : ∀x, is_closed ({x} : set α)) lemma is_closed_singleton [t1_space α] {x : α} : is_closed ({x} : set α) := t1_space.t1 x lemma is_open_compl_singleton [t1_space α] {x : α} : is_open ({x}ᶜ : set α) := is_closed_singleton.is_open_compl lemma is_open_ne [t1_space α] {x : α} : is_open {y | y ≠ x} := is_open_compl_singleton instance subtype.t1_space {α : Type u} [topological_space α] [t1_space α] {p : α → Prop} : t1_space (subtype p) := ⟨λ ⟨x, hx⟩, is_closed_induced_iff.2 $ ⟨{x}, is_closed_singleton, set.ext $ λ y, by simp [subtype.ext_iff_val]⟩⟩ @[priority 100] -- see Note [lower instance priority] instance t1_space.t0_space [t1_space α] : t0_space α := ⟨λ x y h, ⟨{z | z ≠ y}, is_open_ne, or.inl ⟨h, not_not_intro rfl⟩⟩⟩ lemma t1_iff_exists_open : t1_space α ↔ ∀ (x y), x ≠ y → (∃ (U : set α) (hU : is_open U), x ∈ U ∧ y ∉ U) := begin split, { introsI t1 x y hxy, exact ⟨{y}ᶜ, is_open_compl_iff.mpr (t1_space.t1 y), mem_compl_singleton_iff.mpr hxy, not_not.mpr rfl⟩}, { intro h, constructor, intro x, rw ← is_open_compl_iff, have p : ⋃₀ {U : set α | (x ∉ U) ∧ (is_open U)} = {x}ᶜ, { apply subset.antisymm; intros t ht, { rcases ht with ⟨A, ⟨hxA, hA⟩, htA⟩, rw [mem_compl_eq, mem_singleton_iff], rintro rfl, contradiction }, { obtain ⟨U, hU, hh⟩ := h t x (mem_compl_singleton_iff.mp ht), exact ⟨U, ⟨hh.2, hU⟩, hh.1⟩}}, rw ← p, exact is_open_sUnion (λ B hB, hB.2) } end lemma compl_singleton_mem_nhds [t1_space α] {x y : α} (h : y ≠ x) : {x}ᶜ ∈ 𝓝 y := is_open.mem_nhds is_open_compl_singleton $ by rwa [mem_compl_eq, mem_singleton_iff] @[simp] lemma closure_singleton [t1_space α] {a : α} : closure ({a} : set α) = {a} := is_closed_singleton.closure_eq lemma set.subsingleton.closure [t1_space α] {s : set α} (hs : s.subsingleton) : (closure s).subsingleton := hs.induction_on (by simp) $ λ x, by simp @[simp] lemma subsingleton_closure [t1_space α] {s : set α} : (closure s).subsingleton ↔ s.subsingleton := ⟨λ h, h.mono subset_closure, λ h, h.closure⟩ lemma is_closed_map_const {α β} [topological_space α] [topological_space β] [t1_space β] {y : β} : is_closed_map (function.const α y) := begin apply is_closed_map.of_nonempty, intros s hs h2s, simp_rw [h2s.image_const, is_closed_singleton] end lemma discrete_of_t1_of_finite {X : Type*} [topological_space X] [t1_space X] [fintype X] : discrete_topology X := begin apply singletons_open_iff_discrete.mp, intros x, rw [← is_closed_compl_iff, ← bUnion_of_singleton ({x} : set X)ᶜ], exact is_closed_bUnion (finite.of_fintype _) (λ y _, is_closed_singleton) end lemma singleton_mem_nhds_within_of_mem_discrete {s : set α} [discrete_topology s] {x : α} (hx : x ∈ s) : {x} ∈ 𝓝[s] x := begin have : ({⟨x, hx⟩} : set s) ∈ 𝓝 (⟨x, hx⟩ : s), by simp [nhds_discrete], simpa only [nhds_within_eq_map_subtype_coe hx, image_singleton] using @image_mem_map _ _ _ (coe : s → α) _ this end /-- The neighbourhoods filter of `x` within `s`, under the discrete topology, is equal to the pure `x` filter (which is the principal filter at the singleton `{x}`.) -/ lemma nhds_within_of_mem_discrete {s : set α} [discrete_topology s] {x : α} (hx : x ∈ s) : 𝓝[s] x = pure x := le_antisymm (le_pure_iff.2 $ singleton_mem_nhds_within_of_mem_discrete hx) (pure_le_nhds_within hx) lemma filter.has_basis.exists_inter_eq_singleton_of_mem_discrete {ι : Type*} {p : ι → Prop} {t : ι → set α} {s : set α} [discrete_topology s] {x : α} (hb : (𝓝 x).has_basis p t) (hx : x ∈ s) : ∃ i (hi : p i), t i ∩ s = {x} := begin rcases (nhds_within_has_basis hb s).mem_iff.1 (singleton_mem_nhds_within_of_mem_discrete hx) with ⟨i, hi, hix⟩, exact ⟨i, hi, subset.antisymm hix $ singleton_subset_iff.2 ⟨mem_of_mem_nhds $ hb.mem_of_mem hi, hx⟩⟩ end /-- A point `x` in a discrete subset `s` of a topological space admits a neighbourhood that only meets `s` at `x`. -/ lemma nhds_inter_eq_singleton_of_mem_discrete {s : set α} [discrete_topology s] {x : α} (hx : x ∈ s) : ∃ U ∈ 𝓝 x, U ∩ s = {x} := by simpa using (𝓝 x).basis_sets.exists_inter_eq_singleton_of_mem_discrete hx /-- For point `x` in a discrete subset `s` of a topological space, there is a set `U` such that 1. `U` is a punctured neighborhood of `x` (ie. `U ∪ {x}` is a neighbourhood of `x`), 2. `U` is disjoint from `s`. -/ lemma disjoint_nhds_within_of_mem_discrete {s : set α} [discrete_topology s] {x : α} (hx : x ∈ s) : ∃ U ∈ 𝓝[{x}ᶜ] x, disjoint U s := let ⟨V, h, h'⟩ := nhds_inter_eq_singleton_of_mem_discrete hx in ⟨{x}ᶜ ∩ V, inter_mem_nhds_within _ h, (disjoint_iff_inter_eq_empty.mpr (by { rw [inter_assoc, h', compl_inter_self] }))⟩ /-- Let `X` be a topological space and let `s, t ⊆ X` be two subsets. If there is an inclusion `t ⊆ s`, then the topological space structure on `t` induced by `X` is the same as the one obtained by the induced topological space structure on `s`. -/ lemma topological_space.subset_trans {X : Type*} [tX : topological_space X] {s t : set X} (ts : t ⊆ s) : (subtype.topological_space : topological_space t) = (subtype.topological_space : topological_space s).induced (set.inclusion ts) := begin change tX.induced ((coe : s → X) ∘ (set.inclusion ts)) = topological_space.induced (set.inclusion ts) (tX.induced _), rw ← induced_compose, end /-- This lemma characterizes discrete topological spaces as those whose singletons are neighbourhoods. -/ lemma discrete_topology_iff_nhds {X : Type*} [topological_space X] : discrete_topology X ↔ (nhds : X → filter X) = pure := begin split, { introI hX, exact nhds_discrete X }, { intro h, constructor, apply eq_of_nhds_eq_nhds, simp [h, nhds_bot] } end /-- The topology pulled-back under an inclusion `f : X → Y` from the discrete topology (`⊥`) is the discrete topology. This version does not assume the choice of a topology on either the source `X` nor the target `Y` of the inclusion `f`. -/ lemma induced_bot {X Y : Type*} {f : X → Y} (hf : function.injective f) : topological_space.induced f ⊥ = ⊥ := eq_of_nhds_eq_nhds (by simp [nhds_induced, ← set.image_singleton, hf.preimage_image, nhds_bot]) /-- The topology induced under an inclusion `f : X → Y` from the discrete topological space `Y` is the discrete topology on `X`. -/ lemma discrete_topology_induced {X Y : Type*} [tY : topological_space Y] [discrete_topology Y] {f : X → Y} (hf : function.injective f) : @discrete_topology X (topological_space.induced f tY) := begin constructor, rw discrete_topology.eq_bot Y, exact induced_bot hf end /-- Let `s, t ⊆ X` be two subsets of a topological space `X`. If `t ⊆ s` and the topology induced by `X`on `s` is discrete, then also the topology induces on `t` is discrete. -/ lemma discrete_topology.of_subset {X : Type*} [topological_space X] {s t : set X} (ds : discrete_topology s) (ts : t ⊆ s) : discrete_topology t := begin rw [topological_space.subset_trans ts, ds.eq_bot], exact {eq_bot := induced_bot (set.inclusion_injective ts)} end /-- A T₂ space, also known as a Hausdorff space, is one in which for every `x ≠ y` there exists disjoint open sets around `x` and `y`. This is the most widely used of the separation axioms. -/ class t2_space (α : Type u) [topological_space α] : Prop := (t2 : ∀x y, x ≠ y → ∃u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅) lemma t2_separation [t2_space α] {x y : α} (h : x ≠ y) : ∃u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅ := t2_space.t2 x y h @[priority 100] -- see Note [lower instance priority] instance t2_space.t1_space [t2_space α] : t1_space α := ⟨λ x, is_open_compl_iff.1 $ is_open_iff_forall_mem_open.2 $ λ y hxy, let ⟨u, v, hu, hv, hyu, hxv, huv⟩ := t2_separation (mt mem_singleton_of_eq hxy) in ⟨u, λ z hz1 hz2, (ext_iff.1 huv x).1 ⟨mem_singleton_iff.1 hz2 ▸ hz1, hxv⟩, hu, hyu⟩⟩ lemma eq_of_nhds_ne_bot [ht : t2_space α] {x y : α} (h : ne_bot (𝓝 x ⊓ 𝓝 y)) : x = y := classical.by_contradiction $ assume : x ≠ y, let ⟨u, v, hu, hv, hx, hy, huv⟩ := t2_space.t2 x y this in absurd huv $ (inf_ne_bot_iff.1 h (is_open.mem_nhds hu hx) (is_open.mem_nhds hv hy)).ne_empty /-- A space is T₂ iff the neighbourhoods of distinct points generate the bottom filter. -/ lemma t2_iff_nhds : t2_space α ↔ ∀ {x y : α}, ne_bot (𝓝 x ⊓ 𝓝 y) → x = y := ⟨assume h, by exactI λ x y, eq_of_nhds_ne_bot, assume h, ⟨assume x y xy, have 𝓝 x ⊓ 𝓝 y = ⊥ := not_ne_bot.1 $ mt h xy, let ⟨u', hu', v', hv', u'v'⟩ := empty_mem_iff_bot.mpr this, ⟨u, uu', uo, hu⟩ := mem_nhds_iff.mp hu', ⟨v, vv', vo, hv⟩ := mem_nhds_iff.mp hv' in ⟨u, v, uo, vo, hu, hv, by { rw [← subset_empty_iff, u'v'], exact inter_subset_inter uu' vv' }⟩⟩⟩ lemma t2_iff_ultrafilter : t2_space α ↔ ∀ {x y : α} (f : ultrafilter α), ↑f ≤ 𝓝 x → ↑f ≤ 𝓝 y → x = y := t2_iff_nhds.trans $ by simp only [←exists_ultrafilter_iff, and_imp, le_inf_iff, exists_imp_distrib] lemma is_closed_diagonal [t2_space α] : is_closed (diagonal α) := begin refine is_closed_iff_cluster_pt.mpr _, rintro ⟨a₁, a₂⟩ h, refine eq_of_nhds_ne_bot ⟨λ this : 𝓝 a₁ ⊓ 𝓝 a₂ = ⊥, h.ne _⟩, obtain ⟨t₁, (ht₁ : t₁ ∈ 𝓝 a₁), t₂, (ht₂ : t₂ ∈ 𝓝 a₂), (h' : t₁ ∩ t₂ = ∅)⟩ := inf_eq_bot_iff.1 this, rw [inf_principal_eq_bot, nhds_prod_eq], apply mem_of_superset (prod_mem_prod ht₁ ht₂), rintro ⟨x, y⟩ ⟨x_in, y_in⟩ (heq : x = y), rw ← heq at *, have : x ∈ t₁ ∩ t₂ := ⟨x_in, y_in⟩, rwa h' at this end lemma t2_iff_is_closed_diagonal : t2_space α ↔ is_closed (diagonal α) := begin split, { introI h, exact is_closed_diagonal }, { intro h, constructor, intros x y hxy, have : (x, y) ∈ (diagonal α)ᶜ, by rwa [mem_compl_iff], obtain ⟨t, t_sub, t_op, xyt⟩ : ∃ t ⊆ (diagonal α)ᶜ, is_open t ∧ (x, y) ∈ t := is_open_iff_forall_mem_open.mp h.is_open_compl _ this, rcases is_open_prod_iff.mp t_op x y xyt with ⟨U, V, U_op, V_op, xU, yV, H⟩, use [U, V, U_op, V_op, xU, yV], have := subset.trans H t_sub, rw eq_empty_iff_forall_not_mem, rintros z ⟨zU, zV⟩, have : ¬ (z, z) ∈ diagonal α := this (mk_mem_prod zU zV), exact this rfl }, end section separated open separated finset lemma finset_disjoint_finset_opens_of_t2 [t2_space α] : ∀ (s t : finset α), disjoint s t → separated (s : set α) t := begin refine induction_on_union _ (λ a b hi d, (hi d.symm).symm) (λ a d, empty_right a) (λ a b ab, _) _, { obtain ⟨U, V, oU, oV, aU, bV, UV⟩ := t2_separation (by { rw [ne.def, ← finset.mem_singleton], exact (disjoint_singleton.mp ab.symm) }), refine ⟨U, V, oU, oV, _, _, set.disjoint_iff_inter_eq_empty.mpr UV⟩; exact singleton_subset_set_iff.mpr ‹_› }, { intros a b c ac bc d, apply_mod_cast union_left (ac (disjoint_of_subset_left (a.subset_union_left b) d)) (bc _), exact disjoint_of_subset_left (a.subset_union_right b) d }, end lemma point_disjoint_finset_opens_of_t2 [t2_space α] {x : α} {s : finset α} (h : x ∉ s) : separated ({x} : set α) s := by exact_mod_cast finset_disjoint_finset_opens_of_t2 {x} s (singleton_disjoint.mpr h) end separated @[simp] lemma nhds_eq_nhds_iff {a b : α} [t2_space α] : 𝓝 a = 𝓝 b ↔ a = b := ⟨assume h, eq_of_nhds_ne_bot $ by rw [h, inf_idem]; exact nhds_ne_bot, assume h, h ▸ rfl⟩ @[simp] lemma nhds_le_nhds_iff {a b : α} [t2_space α] : 𝓝 a ≤ 𝓝 b ↔ a = b := ⟨assume h, eq_of_nhds_ne_bot $ by rw [inf_of_le_left h]; exact nhds_ne_bot, assume h, h ▸ le_refl _⟩ lemma tendsto_nhds_unique [t2_space α] {f : β → α} {l : filter β} {a b : α} [ne_bot l] (ha : tendsto f l (𝓝 a)) (hb : tendsto f l (𝓝 b)) : a = b := eq_of_nhds_ne_bot $ ne_bot_of_le $ le_inf ha hb lemma tendsto_nhds_unique' [t2_space α] {f : β → α} {l : filter β} {a b : α} (hl : ne_bot l) (ha : tendsto f l (𝓝 a)) (hb : tendsto f l (𝓝 b)) : a = b := eq_of_nhds_ne_bot $ ne_bot_of_le $ le_inf ha hb lemma tendsto_nhds_unique_of_eventually_eq [t2_space α] {f g : β → α} {l : filter β} {a b : α} [ne_bot l] (ha : tendsto f l (𝓝 a)) (hb : tendsto g l (𝓝 b)) (hfg : f =ᶠ[l] g) : a = b := tendsto_nhds_unique (ha.congr' hfg) hb lemma tendsto_const_nhds_iff [t2_space α] {l : filter α} [ne_bot l] {c d : α} : tendsto (λ x, c) l (𝓝 d) ↔ c = d := ⟨λ h, tendsto_nhds_unique (tendsto_const_nhds) h, λ h, h ▸ tendsto_const_nhds⟩ /-- A T₂.₅ space, also known as a Urysohn space, is a topological space where for every pair `x ≠ y`, there are two open sets, with the intersection of clousures empty, one containing `x` and the other `y` . -/ class t2_5_space (α : Type u) [topological_space α]: Prop := (t2_5 : ∀ x y (h : x ≠ y), ∃ (U V: set α), is_open U ∧ is_open V ∧ closure U ∩ closure V = ∅ ∧ x ∈ U ∧ y ∈ V) @[priority 100] -- see Note [lower instance priority] instance t2_5_space.t2_space [t2_5_space α] : t2_space α := ⟨λ x y hxy, let ⟨U, V, hU, hV, hUV, hh⟩ := t2_5_space.t2_5 x y hxy in ⟨U, V, hU, hV, hh.1, hh.2, subset_eq_empty (powerset_mono.mpr (closure_inter_subset_inter_closure U V) subset_closure) hUV⟩⟩ section lim variables [t2_space α] {f : filter α} /-! ### Properties of `Lim` and `lim` In this section we use explicit `nonempty α` instances for `Lim` and `lim`. This way the lemmas are useful without a `nonempty α` instance. -/ lemma Lim_eq {a : α} [ne_bot f] (h : f ≤ 𝓝 a) : @Lim _ _ ⟨a⟩ f = a := tendsto_nhds_unique (le_nhds_Lim ⟨a, h⟩) h lemma Lim_eq_iff [ne_bot f] (h : ∃ (a : α), f ≤ nhds a) {a} : @Lim _ _ ⟨a⟩ f = a ↔ f ≤ 𝓝 a := ⟨λ c, c ▸ le_nhds_Lim h, Lim_eq⟩ lemma ultrafilter.Lim_eq_iff_le_nhds [compact_space α] {x : α} {F : ultrafilter α} : F.Lim = x ↔ ↑F ≤ 𝓝 x := ⟨λ h, h ▸ F.le_nhds_Lim, Lim_eq⟩ lemma is_open_iff_ultrafilter' [compact_space α] (U : set α) : is_open U ↔ (∀ F : ultrafilter α, F.Lim ∈ U → U ∈ F.1) := begin rw is_open_iff_ultrafilter, refine ⟨λ h F hF, h F.Lim hF F F.le_nhds_Lim, _⟩, intros cond x hx f h, rw [← (ultrafilter.Lim_eq_iff_le_nhds.2 h)] at hx, exact cond _ hx end lemma filter.tendsto.lim_eq {a : α} {f : filter β} [ne_bot f] {g : β → α} (h : tendsto g f (𝓝 a)) : @lim _ _ _ ⟨a⟩ f g = a := Lim_eq h lemma filter.lim_eq_iff {f : filter β} [ne_bot f] {g : β → α} (h : ∃ a, tendsto g f (𝓝 a)) {a} : @lim _ _ _ ⟨a⟩ f g = a ↔ tendsto g f (𝓝 a) := ⟨λ c, c ▸ tendsto_nhds_lim h, filter.tendsto.lim_eq⟩ lemma continuous.lim_eq [topological_space β] {f : β → α} (h : continuous f) (a : β) : @lim _ _ _ ⟨f a⟩ (𝓝 a) f = f a := (h.tendsto a).lim_eq @[simp] lemma Lim_nhds (a : α) : @Lim _ _ ⟨a⟩ (𝓝 a) = a := Lim_eq (le_refl _) @[simp] lemma lim_nhds_id (a : α) : @lim _ _ _ ⟨a⟩ (𝓝 a) id = a := Lim_nhds a @[simp] lemma Lim_nhds_within {a : α} {s : set α} (h : a ∈ closure s) : @Lim _ _ ⟨a⟩ (𝓝[s] a) = a := by haveI : ne_bot (𝓝[s] a) := mem_closure_iff_cluster_pt.1 h; exact Lim_eq inf_le_left @[simp] lemma lim_nhds_within_id {a : α} {s : set α} (h : a ∈ closure s) : @lim _ _ _ ⟨a⟩ (𝓝[s] a) id = a := Lim_nhds_within h end lim /-! ### `t2_space` constructions We use two lemmas to prove that various standard constructions generate Hausdorff spaces from Hausdorff spaces: * `separated_by_continuous` says that two points `x y : α` can be separated by open neighborhoods provided that there exists a continuous map `f : α → β` with a Hausdorff codomain such that `f x ≠ f y`. We use this lemma to prove that topological spaces defined using `induced` are Hausdorff spaces. * `separated_by_open_embedding` says that for an open embedding `f : α → β` of a Hausdorff space `α`, the images of two distinct points `x y : α`, `x ≠ y` can be separated by open neighborhoods. We use this lemma to prove that topological spaces defined using `coinduced` are Hausdorff spaces. -/ @[priority 100] -- see Note [lower instance priority] instance t2_space_discrete {α : Type*} [topological_space α] [discrete_topology α] : t2_space α := { t2 := assume x y hxy, ⟨{x}, {y}, is_open_discrete _, is_open_discrete _, rfl, rfl, eq_empty_iff_forall_not_mem.2 $ by intros z hz; cases eq_of_mem_singleton hz.1; cases eq_of_mem_singleton hz.2; cc⟩ } lemma separated_by_continuous {α : Type*} {β : Type*} [topological_space α] [topological_space β] [t2_space β] {f : α → β} (hf : continuous f) {x y : α} (h : f x ≠ f y) : ∃u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅ := let ⟨u, v, uo, vo, xu, yv, uv⟩ := t2_separation h in ⟨f ⁻¹' u, f ⁻¹' v, uo.preimage hf, vo.preimage hf, xu, yv, by rw [←preimage_inter, uv, preimage_empty]⟩ lemma separated_by_open_embedding {α β : Type*} [topological_space α] [topological_space β] [t2_space α] {f : α → β} (hf : open_embedding f) {x y : α} (h : x ≠ y) : ∃ u v : set β, is_open u ∧ is_open v ∧ f x ∈ u ∧ f y ∈ v ∧ u ∩ v = ∅ := let ⟨u, v, uo, vo, xu, yv, uv⟩ := t2_separation h in ⟨f '' u, f '' v, hf.is_open_map _ uo, hf.is_open_map _ vo, mem_image_of_mem _ xu, mem_image_of_mem _ yv, by rw [image_inter hf.inj, uv, image_empty]⟩ instance {α : Type*} {p : α → Prop} [t : topological_space α] [t2_space α] : t2_space (subtype p) := ⟨assume x y h, separated_by_continuous continuous_subtype_val (mt subtype.eq h)⟩ instance {α : Type*} {β : Type*} [t₁ : topological_space α] [t2_space α] [t₂ : topological_space β] [t2_space β] : t2_space (α × β) := ⟨assume ⟨x₁,x₂⟩ ⟨y₁,y₂⟩ h, or.elim (not_and_distrib.mp (mt prod.ext_iff.mpr h)) (λ h₁, separated_by_continuous continuous_fst h₁) (λ h₂, separated_by_continuous continuous_snd h₂)⟩ lemma embedding.t2_space [topological_space β] [t2_space β] {f : α → β} (hf : embedding f) : t2_space α := ⟨λ x y h, separated_by_continuous hf.continuous (hf.inj.ne h)⟩ instance {α : Type*} {β : Type*} [t₁ : topological_space α] [t2_space α] [t₂ : topological_space β] [t2_space β] : t2_space (α ⊕ β) := begin constructor, rintros (x|x) (y|y) h, { replace h : x ≠ y := λ c, (c.subst h) rfl, exact separated_by_open_embedding open_embedding_inl h }, { exact ⟨_, _, is_open_range_inl, is_open_range_inr, ⟨x, rfl⟩, ⟨y, rfl⟩, range_inl_inter_range_inr⟩ }, { exact ⟨_, _, is_open_range_inr, is_open_range_inl, ⟨x, rfl⟩, ⟨y, rfl⟩, range_inr_inter_range_inl⟩ }, { replace h : x ≠ y := λ c, (c.subst h) rfl, exact separated_by_open_embedding open_embedding_inr h } end instance Pi.t2_space {α : Type*} {β : α → Type v} [t₂ : Πa, topological_space (β a)] [∀a, t2_space (β a)] : t2_space (Πa, β a) := ⟨assume x y h, let ⟨i, hi⟩ := not_forall.mp (mt funext h) in separated_by_continuous (continuous_apply i) hi⟩ instance sigma.t2_space {ι : Type*} {α : ι → Type*} [Πi, topological_space (α i)] [∀a, t2_space (α a)] : t2_space (Σi, α i) := begin constructor, rintros ⟨i, x⟩ ⟨j, y⟩ neq, rcases em (i = j) with (rfl|h), { replace neq : x ≠ y := λ c, (c.subst neq) rfl, exact separated_by_open_embedding open_embedding_sigma_mk neq }, { exact ⟨_, _, is_open_range_sigma_mk, is_open_range_sigma_mk, ⟨x, rfl⟩, ⟨y, rfl⟩, by tidy⟩ } end variables [topological_space β] lemma is_closed_eq [t2_space α] {f g : β → α} (hf : continuous f) (hg : continuous g) : is_closed {x:β | f x = g x} := continuous_iff_is_closed.mp (hf.prod_mk hg) _ is_closed_diagonal /-- If two continuous maps are equal on `s`, then they are equal on the closure of `s`. -/ lemma set.eq_on.closure [t2_space α] {s : set β} {f g : β → α} (h : eq_on f g s) (hf : continuous f) (hg : continuous g) : eq_on f g (closure s) := closure_minimal h (is_closed_eq hf hg) /-- If two continuous functions are equal on a dense set, then they are equal. -/ lemma continuous.ext_on [t2_space α] {s : set β} (hs : dense s) {f g : β → α} (hf : continuous f) (hg : continuous g) (h : eq_on f g s) : f = g := funext $ λ x, h.closure hf hg (hs x) lemma function.left_inverse.closed_range [t2_space α] {f : α → β} {g : β → α} (h : function.left_inverse f g) (hf : continuous f) (hg : continuous g) : is_closed (range g) := have eq_on (g ∘ f) id (closure $ range g), from h.right_inv_on_range.eq_on.closure (hg.comp hf) continuous_id, is_closed_of_closure_subset $ λ x hx, calc x = g (f x) : (this hx).symm ... ∈ _ : mem_range_self _ lemma function.left_inverse.closed_embedding [t2_space α] {f : α → β} {g : β → α} (h : function.left_inverse f g) (hf : continuous f) (hg : continuous g) : closed_embedding g := ⟨h.embedding hf hg, h.closed_range hf hg⟩ lemma diagonal_eq_range_diagonal_map {α : Type*} : {p:α×α | p.1 = p.2} = range (λx, (x,x)) := ext $ assume p, iff.intro (assume h, ⟨p.1, prod.ext_iff.2 ⟨rfl, h⟩⟩) (assume ⟨x, hx⟩, show p.1 = p.2, by rw ←hx) lemma prod_subset_compl_diagonal_iff_disjoint {α : Type*} {s t : set α} : set.prod s t ⊆ {p:α×α | p.1 = p.2}ᶜ ↔ s ∩ t = ∅ := by rw [eq_empty_iff_forall_not_mem, subset_compl_comm, diagonal_eq_range_diagonal_map, range_subset_iff]; simp lemma compact_compact_separated [t2_space α] {s t : set α} (hs : is_compact s) (ht : is_compact t) (hst : s ∩ t = ∅) : ∃u v : set α, is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ u ∩ v = ∅ := by simp only [prod_subset_compl_diagonal_iff_disjoint.symm] at ⊢ hst; exact generalized_tube_lemma hs ht is_closed_diagonal.is_open_compl hst /-- In a `t2_space`, every compact set is closed. -/ lemma is_compact.is_closed [t2_space α] {s : set α} (hs : is_compact s) : is_closed s := is_open_compl_iff.1 $ is_open_iff_forall_mem_open.mpr $ assume x hx, let ⟨u, v, uo, vo, su, xv, uv⟩ := compact_compact_separated hs (is_compact_singleton : is_compact {x}) (by rwa [inter_comm, ←subset_compl_iff_disjoint, singleton_subset_iff]) in have v ⊆ sᶜ, from subset_compl_comm.mp (subset.trans su (subset_compl_iff_disjoint.mpr uv)), ⟨v, this, vo, by simpa using xv⟩ /-- If `V : ι → set α` is a decreasing family of compact sets then any neighborhood of `⋂ i, V i` contains some `V i`. This is a version of `exists_subset_nhd_of_compact'` where we don't need to assume each `V i` closed because it follows from compactness since `α` is assumed to be Hausdorff. -/ lemma exists_subset_nhd_of_compact [t2_space α] {ι : Type*} [nonempty ι] {V : ι → set α} (hV : directed (⊇) V) (hV_cpct : ∀ i, is_compact (V i)) {U : set α} (hU : ∀ x ∈ ⋂ i, V i, U ∈ 𝓝 x) : ∃ i, V i ⊆ U := exists_subset_nhd_of_compact' hV hV_cpct (λ i, (hV_cpct i).is_closed) hU lemma compact_exhaustion.is_closed [t2_space α] (K : compact_exhaustion α) (n : ℕ) : is_closed (K n) := (K.is_compact n).is_closed lemma is_compact.inter [t2_space α] {s t : set α} (hs : is_compact s) (ht : is_compact t) : is_compact (s ∩ t) := hs.inter_right $ ht.is_closed lemma compact_closure_of_subset_compact [t2_space α] {s t : set α} (ht : is_compact t) (h : s ⊆ t) : is_compact (closure s) := compact_of_is_closed_subset ht is_closed_closure (closure_minimal h ht.is_closed) lemma image_closure_of_compact [t2_space β] {s : set α} (hs : is_compact (closure s)) {f : α → β} (hf : continuous_on f (closure s)) : f '' closure s = closure (f '' s) := subset.antisymm hf.image_closure $ closure_minimal (image_subset f subset_closure) (hs.image_of_continuous_on hf).is_closed /-- If a compact set is covered by two open sets, then we can cover it by two compact subsets. -/ lemma is_compact.binary_compact_cover [t2_space α] {K U V : set α} (hK : is_compact K) (hU : is_open U) (hV : is_open V) (h2K : K ⊆ U ∪ V) : ∃ K₁ K₂ : set α, is_compact K₁ ∧ is_compact K₂ ∧ K₁ ⊆ U ∧ K₂ ⊆ V ∧ K = K₁ ∪ K₂ := begin rcases compact_compact_separated (hK.diff hU) (hK.diff hV) (by rwa [diff_inter_diff, diff_eq_empty]) with ⟨O₁, O₂, h1O₁, h1O₂, h2O₁, h2O₂, hO⟩, refine ⟨_, _, hK.diff h1O₁, hK.diff h1O₂, by rwa [diff_subset_comm], by rwa [diff_subset_comm], by rw [← diff_inter, hO, diff_empty]⟩ end lemma continuous.is_closed_map [compact_space α] [t2_space β] {f : α → β} (h : continuous f) : is_closed_map f := λ s hs, (hs.is_compact.image h).is_closed lemma continuous.closed_embedding [compact_space α] [t2_space β] {f : α → β} (h : continuous f) (hf : function.injective f) : closed_embedding f := closed_embedding_of_continuous_injective_closed h hf h.is_closed_map section open finset function /-- For every finite open cover `Uᵢ` of a compact set, there exists a compact cover `Kᵢ ⊆ Uᵢ`. -/ lemma is_compact.finite_compact_cover [t2_space α] {s : set α} (hs : is_compact s) {ι} (t : finset ι) (U : ι → set α) (hU : ∀ i ∈ t, is_open (U i)) (hsC : s ⊆ ⋃ i ∈ t, U i) : ∃ K : ι → set α, (∀ i, is_compact (K i)) ∧ (∀i, K i ⊆ U i) ∧ s = ⋃ i ∈ t, K i := begin classical, induction t using finset.induction with x t hx ih generalizing U hU s hs hsC, { refine ⟨λ _, ∅, λ i, is_compact_empty, λ i, empty_subset _, _⟩, simpa only [subset_empty_iff, Union_false, Union_empty] using hsC }, simp only [finset.set_bUnion_insert] at hsC, simp only [finset.mem_insert] at hU, have hU' : ∀ i ∈ t, is_open (U i) := λ i hi, hU i (or.inr hi), rcases hs.binary_compact_cover (hU x (or.inl rfl)) (is_open_bUnion hU') hsC with ⟨K₁, K₂, h1K₁, h1K₂, h2K₁, h2K₂, hK⟩, rcases ih U hU' h1K₂ h2K₂ with ⟨K, h1K, h2K, h3K⟩, refine ⟨update K x K₁, _, _, _⟩, { intros i, by_cases hi : i = x, { simp only [update_same, hi, h1K₁] }, { rw [← ne.def] at hi, simp only [update_noteq hi, h1K] }}, { intros i, by_cases hi : i = x, { simp only [update_same, hi, h2K₁] }, { rw [← ne.def] at hi, simp only [update_noteq hi, h2K] }}, { simp only [set_bUnion_insert_update _ hx, hK, h3K] } end end lemma locally_compact_of_compact_nhds [t2_space α] (h : ∀ x : α, ∃ s, s ∈ 𝓝 x ∧ is_compact s) : locally_compact_space α := ⟨assume x n hn, let ⟨u, un, uo, xu⟩ := mem_nhds_iff.mp hn in let ⟨k, kx, kc⟩ := h x in -- K is compact but not necessarily contained in N. -- K \ U is again compact and doesn't contain x, so -- we may find open sets V, W separating x from K \ U. -- Then K \ W is a compact neighborhood of x contained in U. let ⟨v, w, vo, wo, xv, kuw, vw⟩ := compact_compact_separated is_compact_singleton (is_compact.diff kc uo) (by rw [singleton_inter_eq_empty]; exact λ h, h.2 xu) in have wn : wᶜ ∈ 𝓝 x, from mem_nhds_iff.mpr ⟨v, subset_compl_iff_disjoint.mpr vw, vo, singleton_subset_iff.mp xv⟩, ⟨k \ w, filter.inter_mem kx wn, subset.trans (diff_subset_comm.mp kuw) un, kc.diff wo⟩⟩ @[priority 100] -- see Note [lower instance priority] instance locally_compact_of_compact [t2_space α] [compact_space α] : locally_compact_space α := locally_compact_of_compact_nhds (assume x, ⟨univ, is_open_univ.mem_nhds trivial, compact_univ⟩) /-- In a locally compact T₂ space, every point has an open neighborhood with compact closure -/ lemma exists_open_with_compact_closure [locally_compact_space α] [t2_space α] (x : α) : ∃ (U : set α), is_open U ∧ x ∈ U ∧ is_compact (closure U) := begin rcases exists_compact_mem_nhds x with ⟨K, hKc, hxK⟩, rcases mem_nhds_iff.1 hxK with ⟨t, h1t, h2t, h3t⟩, exact ⟨t, h2t, h3t, compact_closure_of_subset_compact hKc h1t⟩ end end separation section regularity /-- A T₃ space, also known as a regular space (although this condition sometimes omits T₂), is one in which for every closed `C` and `x ∉ C`, there exist disjoint open sets containing `x` and `C` respectively. -/ class regular_space (α : Type u) [topological_space α] extends t0_space α : Prop := (regular : ∀{s:set α} {a}, is_closed s → a ∉ s → ∃t, is_open t ∧ s ⊆ t ∧ 𝓝[t] a = ⊥) @[priority 100] -- see Note [lower instance priority] instance regular_space.t1_space [regular_space α] : t1_space α := begin rw t1_iff_exists_open, intros x y hxy, obtain ⟨U, hU, h⟩ := t0_space.t0 x y hxy, cases h, { exact ⟨U, hU, h⟩ }, { obtain ⟨R, hR, hh⟩ := regular_space.regular (is_closed_compl_iff.mpr hU) (not_not.mpr h.1), obtain ⟨V, hV, hhh⟩ := mem_nhds_iff.1 (filter.inf_principal_eq_bot.1 hh.2), exact ⟨R, hR, hh.1 (mem_compl h.2), hV hhh.2⟩ } end lemma nhds_is_closed [regular_space α] {a : α} {s : set α} (h : s ∈ 𝓝 a) : ∃ t ∈ 𝓝 a, t ⊆ s ∧ is_closed t := let ⟨s', h₁, h₂, h₃⟩ := mem_nhds_iff.mp h in have ∃t, is_open t ∧ s'ᶜ ⊆ t ∧ 𝓝[t] a = ⊥, from regular_space.regular (is_closed_compl_iff.mpr h₂) (not_not_intro h₃), let ⟨t, ht₁, ht₂, ht₃⟩ := this in ⟨tᶜ, mem_of_eq_bot $ by rwa [compl_compl], subset.trans (compl_subset_comm.1 ht₂) h₁, is_closed_compl_iff.mpr ht₁⟩ lemma closed_nhds_basis [regular_space α] (a : α) : (𝓝 a).has_basis (λ s : set α, s ∈ 𝓝 a ∧ is_closed s) id := ⟨λ t, ⟨λ t_in, let ⟨s, s_in, h_st, h⟩ := nhds_is_closed t_in in ⟨s, ⟨s_in, h⟩, h_st⟩, λ ⟨s, ⟨s_in, hs⟩, hst⟩, mem_of_superset s_in hst⟩⟩ instance subtype.regular_space [regular_space α] {p : α → Prop} : regular_space (subtype p) := ⟨begin intros s a hs ha, rcases is_closed_induced_iff.1 hs with ⟨s, hs', rfl⟩, rcases regular_space.regular hs' ha with ⟨t, ht, hst, hat⟩, refine ⟨coe ⁻¹' t, is_open_induced ht, preimage_mono hst, _⟩, rw [nhds_within, nhds_induced, ← comap_principal, ← comap_inf, ← nhds_within, hat, comap_bot] end⟩ variable (α) @[priority 100] -- see Note [lower instance priority] instance regular_space.t2_space [regular_space α] : t2_space α := ⟨λ x y hxy, let ⟨s, hs, hys, hxs⟩ := regular_space.regular is_closed_singleton (mt mem_singleton_iff.1 hxy), ⟨t, hxt, u, hsu, htu⟩ := empty_mem_iff_bot.2 hxs, ⟨v, hvt, hv, hxv⟩ := mem_nhds_iff.1 hxt in ⟨v, s, hv, hs, hxv, singleton_subset_iff.1 hys, eq_empty_of_subset_empty $ λ z ⟨hzv, hzs⟩, by { rw htu, exact ⟨hvt hzv, hsu hzs⟩ }⟩⟩ @[priority 100] -- see Note [lower instance priority] instance regular_space.t2_5_space [regular_space α] : t2_5_space α := ⟨λ x y hxy, let ⟨U, V, hU, hV, hh_1, hh_2, hUV⟩ := t2_space.t2 x y hxy, hxcV := not_not.mpr ((interior_maximal (subset_compl_iff_disjoint.mpr hUV) hU) hh_1), ⟨R, hR, hh⟩ := regular_space.regular is_closed_closure (by rwa closure_eq_compl_interior_compl), ⟨A, hA, hhh⟩ := mem_nhds_iff.1 (filter.inf_principal_eq_bot.1 hh.2) in ⟨A, V, hhh.1, hV, subset_eq_empty ((closure V).inter_subset_inter_left (subset.trans (closure_minimal hA (is_closed_compl_iff.mpr hR)) (compl_subset_compl.mpr hh.1))) (compl_inter_self (closure V)), hhh.2, hh_2⟩⟩ variable {α} /-- Given two points `x ≠ y`, we can find neighbourhoods `x ∈ V₁ ⊆ U₁` and `y ∈ V₂ ⊆ U₂`, with the `Vₖ` closed and the `Uₖ` open, such that the `Uₖ` are disjoint. -/ lemma disjoint_nested_nhds [regular_space α] {x y : α} (h : x ≠ y) : ∃ (U₁ V₁ ∈ 𝓝 x) (U₂ V₂ ∈ 𝓝 y), is_closed V₁ ∧ is_closed V₂ ∧ is_open U₁ ∧ is_open U₂ ∧ V₁ ⊆ U₁ ∧ V₂ ⊆ U₂ ∧ U₁ ∩ U₂ = ∅ := begin rcases t2_separation h with ⟨U₁, U₂, U₁_op, U₂_op, x_in, y_in, H⟩, rcases nhds_is_closed (is_open.mem_nhds U₁_op x_in) with ⟨V₁, V₁_in, h₁, V₁_closed⟩, rcases nhds_is_closed (is_open.mem_nhds U₂_op y_in) with ⟨V₂, V₂_in, h₂, V₂_closed⟩, use [U₁, V₁, mem_of_superset V₁_in h₁, V₁_in, U₂, V₂, mem_of_superset V₂_in h₂, V₂_in], tauto end end regularity section normality /-- A T₄ space, also known as a normal space (although this condition sometimes omits T₂), is one in which for every pair of disjoint closed sets `C` and `D`, there exist disjoint open sets containing `C` and `D` respectively. -/ class normal_space (α : Type u) [topological_space α] extends t1_space α : Prop := (normal : ∀ s t : set α, is_closed s → is_closed t → disjoint s t → ∃ u v, is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ disjoint u v) theorem normal_separation [normal_space α] {s t : set α} (H1 : is_closed s) (H2 : is_closed t) (H3 : disjoint s t) : ∃ u v, is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ disjoint u v := normal_space.normal s t H1 H2 H3 theorem normal_exists_closure_subset [normal_space α] {s t : set α} (hs : is_closed s) (ht : is_open t) (hst : s ⊆ t) : ∃ u, is_open u ∧ s ⊆ u ∧ closure u ⊆ t := begin have : disjoint s tᶜ, from λ x ⟨hxs, hxt⟩, hxt (hst hxs), rcases normal_separation hs (is_closed_compl_iff.2 ht) this with ⟨s', t', hs', ht', hss', htt', hs't'⟩, refine ⟨s', hs', hss', subset.trans (closure_minimal _ (is_closed_compl_iff.2 ht')) (compl_subset_comm.1 htt')⟩, exact λ x hxs hxt, hs't' ⟨hxs, hxt⟩ end @[priority 100] -- see Note [lower instance priority] instance normal_space.regular_space [normal_space α] : regular_space α := { regular := λ s x hs hxs, let ⟨u, v, hu, hv, hsu, hxv, huv⟩ := normal_separation hs is_closed_singleton (λ _ ⟨hx, hy⟩, hxs $ mem_of_eq_of_mem (eq_of_mem_singleton hy).symm hx) in ⟨u, hu, hsu, filter.empty_mem_iff_bot.1 $ filter.mem_inf_iff.2 ⟨v, is_open.mem_nhds hv (singleton_subset_iff.1 hxv), u, filter.mem_principal_self u, by rwa [eq_comm, inter_comm, ← disjoint_iff_inter_eq_empty]⟩⟩ } -- We can't make this an instance because it could cause an instance loop. lemma normal_of_compact_t2 [compact_space α] [t2_space α] : normal_space α := begin refine ⟨assume s t hs ht st, _⟩, simp only [disjoint_iff], exact compact_compact_separated hs.is_compact ht.is_compact st.eq_bot end end normality /-- In a compact t2 space, the connected component of a point equals the intersection of all its clopen neighbourhoods. -/ lemma connected_component_eq_Inter_clopen [t2_space α] [compact_space α] {x : α} : connected_component x = ⋂ Z : {Z : set α // is_clopen Z ∧ x ∈ Z}, Z := begin apply eq_of_subset_of_subset connected_component_subset_Inter_clopen, -- Reduce to showing that the clopen intersection is connected. refine is_preconnected.subset_connected_component _ (mem_Inter.2 (λ Z, Z.2.2)), -- We do this by showing that any disjoint cover by two closed sets implies -- that one of these closed sets must contain our whole thing. -- To reduce to the case where the cover is disjoint on all of `α` we need that `s` is closed have hs : @is_closed _ _inst_1 (⋂ (Z : {Z : set α // is_clopen Z ∧ x ∈ Z}), Z) := is_closed_Inter (λ Z, Z.2.1.2), rw (is_preconnected_iff_subset_of_fully_disjoint_closed hs), intros a b ha hb hab ab_empty, haveI := @normal_of_compact_t2 α _ _ _, -- Since our space is normal, we get two larger disjoint open sets containing the disjoint -- closed sets. If we can show that our intersection is a subset of any of these we can then -- "descend" this to show that it is a subset of either a or b. rcases normal_separation ha hb (disjoint_iff.2 ab_empty) with ⟨u, v, hu, hv, hau, hbv, huv⟩, -- If we can find a clopen set around x, contained in u ∪ v, we get a disjoint decomposition -- Z = Z ∩ u ∪ Z ∩ v of clopen sets. The intersection of all clopen neighbourhoods will then lie -- in whichever of u or v x lies in and hence will be a subset of either a or b. suffices : ∃ (Z : set α), is_clopen Z ∧ x ∈ Z ∧ Z ⊆ u ∪ v, { cases this with Z H, rw [disjoint_iff_inter_eq_empty] at huv, have H1 := is_clopen_inter_of_disjoint_cover_clopen H.1 H.2.2 hu hv huv, rw [union_comm] at H, have H2 := is_clopen_inter_of_disjoint_cover_clopen H.1 H.2.2 hv hu (inter_comm u v ▸ huv), by_cases (x ∈ u), -- The x ∈ u case. { left, suffices : (⋂ (Z : {Z : set α // is_clopen Z ∧ x ∈ Z}), ↑Z) ⊆ u, { rw ←set.disjoint_iff_inter_eq_empty at huv, replace hab : (⋂ (Z : {Z // is_clopen Z ∧ x ∈ Z}), ↑Z) ≤ a ∪ b := hab, replace this : (⋂ (Z : {Z // is_clopen Z ∧ x ∈ Z}), ↑Z) ≤ u := this, exact disjoint.left_le_of_le_sup_right hab (huv.mono this hbv) }, { apply subset.trans _ (inter_subset_right Z u), apply Inter_subset (λ Z : {Z : set α // is_clopen Z ∧ x ∈ Z}, ↑Z) ⟨Z ∩ u, H1, mem_inter H.2.1 h⟩ } }, -- If x ∉ u, we get x ∈ v since x ∈ u ∪ v. The rest is then like the x ∈ u case. have h1 : x ∈ v, { cases (mem_union x u v).1 (mem_of_subset_of_mem (subset.trans hab (union_subset_union hau hbv)) (mem_Inter.2 (λ i, i.2.2))) with h1 h1, { exfalso, exact h h1}, { exact h1} }, right, suffices : (⋂ (Z : {Z : set α // is_clopen Z ∧ x ∈ Z}), ↑Z) ⊆ v, { rw [inter_comm, ←set.disjoint_iff_inter_eq_empty] at huv, replace hab : (⋂ (Z : {Z // is_clopen Z ∧ x ∈ Z}), ↑Z) ≤ a ∪ b := hab, replace this : (⋂ (Z : {Z // is_clopen Z ∧ x ∈ Z}), ↑Z) ≤ v := this, exact disjoint.left_le_of_le_sup_left hab (huv.mono this hau) }, { apply subset.trans _ (inter_subset_right Z v), apply Inter_subset (λ Z : {Z : set α // is_clopen Z ∧ x ∈ Z}, ↑Z) ⟨Z ∩ v, H2, mem_inter H.2.1 h1⟩ } }, -- Now we find the required Z. We utilize the fact that X \ u ∪ v will be compact, -- so there must be some finite intersection of clopen neighbourhoods of X disjoint to it, -- but a finite intersection of clopen sets is clopen so we let this be our Z. have H1 := ((is_closed_compl_iff.2 (hu.union hv)).is_compact.inter_Inter_nonempty (λ Z : {Z : set α // is_clopen Z ∧ x ∈ Z}, Z) (λ Z, Z.2.1.2)), rw [←not_imp_not, not_forall, not_nonempty_iff_eq_empty, inter_comm] at H1, have huv_union := subset.trans hab (union_subset_union hau hbv), rw [← compl_compl (u ∪ v), subset_compl_iff_disjoint] at huv_union, cases H1 huv_union with Zi H2, refine ⟨(⋂ (U ∈ Zi), subtype.val U), _, _, _⟩, { exact is_clopen_bInter (λ Z hZ, Z.2.1) }, { exact mem_bInter_iff.2 (λ Z hZ, Z.2.2) }, { rwa [not_nonempty_iff_eq_empty, inter_comm, ←subset_compl_iff_disjoint, compl_compl] at H2 } end section profinite open topological_space variables [t2_space α] /-- A Hausdorff space with a clopen basis is totally separated. -/ lemma tot_sep_of_zero_dim (h : is_topological_basis {s : set α | is_clopen s}) : totally_separated_space α := begin constructor, rintros x - y - hxy, obtain ⟨u, v, hu, hv, xu, yv, disj⟩ := t2_separation hxy, obtain ⟨w, hw : is_clopen w, xw, wu⟩ := (is_topological_basis.mem_nhds_iff h).1 (is_open.mem_nhds hu xu), refine ⟨w, wᶜ, hw.1, (is_clopen_compl_iff.2 hw).1, xw, _, _, set.inter_compl_self w⟩, { intro h, have : y ∈ u ∩ v := ⟨wu h, yv⟩, rwa disj at this }, rw set.union_compl_self, end variables [compact_space α] /-- A compact Hausdorff space is totally disconnected if and only if it is totally separated, this is also true for locally compact spaces. -/ theorem compact_t2_tot_disc_iff_tot_sep : totally_disconnected_space α ↔ totally_separated_space α := begin split, { intro h, constructor, rintros x - y -, contrapose!, intros hyp, suffices : x ∈ connected_component y, by simpa [totally_disconnected_space_iff_connected_component_singleton.1 h y, mem_singleton_iff], rw [connected_component_eq_Inter_clopen, mem_Inter], rintro ⟨w : set α, hw : is_clopen w, hy : y ∈ w⟩, by_contra hx, simpa using hyp wᶜ w (is_open_compl_iff.mpr hw.2) hw.1 hx hy }, apply totally_separated_space.totally_disconnected_space, end variables [totally_disconnected_space α] lemma nhds_basis_clopen (x : α) : (𝓝 x).has_basis (λ s : set α, x ∈ s ∧ is_clopen s) id := ⟨λ U, begin split, { have : connected_component x = {x}, from totally_disconnected_space_iff_connected_component_singleton.mp ‹_› x, rw connected_component_eq_Inter_clopen at this, intros hU, let N := {Z // is_clopen Z ∧ x ∈ Z}, suffices : ∃ Z : N, Z.val ⊆ U, { rcases this with ⟨⟨s, hs, hs'⟩, hs''⟩, exact ⟨s, ⟨hs', hs⟩, hs''⟩ }, haveI : nonempty N := ⟨⟨univ, is_clopen_univ, mem_univ x⟩⟩, have hNcl : ∀ Z : N, is_closed Z.val := (λ Z, Z.property.1.2), have hdir : directed superset (λ Z : N, Z.val), { rintros ⟨s, hs, hxs⟩ ⟨t, ht, hxt⟩, exact ⟨⟨s ∩ t, hs.inter ht, ⟨hxs, hxt⟩⟩, inter_subset_left s t, inter_subset_right s t⟩ }, have h_nhd: ∀ y ∈ (⋂ Z : N, Z.val), U ∈ 𝓝 y, { intros y y_in, erw [this, mem_singleton_iff] at y_in, rwa y_in }, exact exists_subset_nhd_of_compact_space hdir hNcl h_nhd }, { rintro ⟨V, ⟨hxV, V_op, -⟩, hUV : V ⊆ U⟩, rw mem_nhds_iff, exact ⟨V, hUV, V_op, hxV⟩ } end⟩ lemma is_topological_basis_clopen : is_topological_basis {s : set α | is_clopen s} := begin apply is_topological_basis_of_open_of_nhds (λ U (hU : is_clopen U), hU.1), intros x U hxU U_op, have : U ∈ 𝓝 x, from is_open.mem_nhds U_op hxU, rcases (nhds_basis_clopen x).mem_iff.mp this with ⟨V, ⟨hxV, hV⟩, hVU : V ⊆ U⟩, use V, tauto end /-- Every member of an open set in a compact Hausdorff totally disconnected space is contained in a clopen set contained in the open set. -/ lemma compact_exists_clopen_in_open {x : α} {U : set α} (is_open : is_open U) (memU : x ∈ U) : ∃ (V : set α) (hV : is_clopen V), x ∈ V ∧ V ⊆ U := (is_topological_basis.mem_nhds_iff is_topological_basis_clopen).1 (is_open.mem_nhds memU) end profinite section locally_compact open topological_space variables {H : Type*} [topological_space H] [locally_compact_space H] [t2_space H] /-- A locally compact Hausdorff totally disconnected space has a basis with clopen elements. -/ lemma loc_compact_Haus_tot_disc_of_zero_dim [totally_disconnected_space H] : is_topological_basis {s : set H | is_clopen s} := begin refine is_topological_basis_of_open_of_nhds (λ u hu, hu.1) _, rintros x U memU hU, obtain ⟨s, comp, xs, sU⟩ := exists_compact_subset hU memU, obtain ⟨t, h, ht, xt⟩ := mem_interior.1 xs, let u : set s := (coe : s → H)⁻¹' (interior s), have u_open_in_s : is_open u := is_open_interior.preimage continuous_subtype_coe, let X : s := ⟨x, h xt⟩, have Xu : X ∈ u := xs, haveI : compact_space s := is_compact_iff_compact_space.1 comp, obtain ⟨V : set s, clopen_in_s, Vx, V_sub⟩ := compact_exists_clopen_in_open u_open_in_s Xu, have V_clopen : is_clopen ((coe : s → H) '' V), { refine ⟨_, (comp.is_closed.closed_embedding_subtype_coe.closed_iff_image_closed).1 clopen_in_s.2⟩, let v : set u := (coe : u → s)⁻¹' V, have : (coe : u → H) = (coe : s → H) ∘ (coe : u → s) := rfl, have f0 : embedding (coe : u → H) := embedding_subtype_coe.comp embedding_subtype_coe, have f1 : open_embedding (coe : u → H), { refine ⟨f0, _⟩, { have : set.range (coe : u → H) = interior s, { rw [this, set.range_comp, subtype.range_coe, subtype.image_preimage_coe], apply set.inter_eq_self_of_subset_left interior_subset, }, rw this, apply is_open_interior } }, have f2 : is_open v := clopen_in_s.1.preimage continuous_subtype_coe, have f3 : (coe : s → H) '' V = (coe : u → H) '' v, { rw [this, image_comp coe coe, subtype.image_preimage_coe, inter_eq_self_of_subset_left V_sub] }, rw f3, apply f1.is_open_map v f2 }, refine ⟨coe '' V, V_clopen, by simp [Vx, h xt], _⟩, transitivity s, { simp }, assumption end /-- A locally compact Hausdorff space is totally disconnected if and only if it is totally separated. -/ theorem loc_compact_t2_tot_disc_iff_tot_sep : totally_disconnected_space H ↔ totally_separated_space H := begin split, { introI h, exact tot_sep_of_zero_dim loc_compact_Haus_tot_disc_of_zero_dim, }, apply totally_separated_space.totally_disconnected_space, end end locally_compact section connected_component_setoid local attribute [instance] connected_component_setoid /-- `connected_components α` is Hausdorff when `α` is Hausdorff and compact -/ instance connected_components.t2 [t2_space α] [compact_space α] : t2_space (connected_components α) := begin -- Proof follows that of: https://stacks.math.columbia.edu/tag/0900 -- Fix 2 distinct connected components, with points a and b refine ⟨λ x y, quotient.induction_on x (quotient.induction_on y (λ a b ne, _))⟩, rw connected_component_nrel_iff at ne, have h := connected_component_disjoint ne, -- write ⟦b⟧ as the intersection of all clopen subsets containing it rw [connected_component_eq_Inter_clopen, disjoint_iff_inter_eq_empty, inter_comm] at h, -- Now we show that this can be reduced to some clopen containing ⟦b⟧ being disjoint to ⟦a⟧ cases is_closed_connected_component.is_compact.elim_finite_subfamily_closed _ _ h with fin_a ha, swap, { exact λ Z, Z.2.1.2 }, set U : set α := (⋂ (i : {Z // is_clopen Z ∧ b ∈ Z}) (H : i ∈ fin_a), i) with hU, rw ←hU at ha, have hu_clopen : is_clopen U := is_clopen_bInter (λ i j, i.2.1), -- This clopen and its complement will separate the points corresponding to ⟦a⟧ and ⟦b⟧ use [quotient.mk '' U, quotient.mk '' Uᶜ], -- Using the fact that clopens are unions of connected components, we show that -- U and Uᶜ is the preimage of a clopen set in the quotient have hu : quotient.mk ⁻¹' (quotient.mk '' U) = U := (connected_components_preimage_image U ▸ eq.symm) hu_clopen.eq_union_connected_components, have huc : quotient.mk ⁻¹' (quotient.mk '' Uᶜ) = Uᶜ := (connected_components_preimage_image Uᶜ ▸ eq.symm) (is_clopen.compl hu_clopen).eq_union_connected_components, -- showing that U and Uᶜ are open and separates ⟦a⟧ and ⟦b⟧ refine ⟨_,_,_,_,_⟩, { rw [(quotient_map_iff.1 quotient_map_quotient_mk).2 _, hu], exact hu_clopen.1 }, { rw [(quotient_map_iff.1 quotient_map_quotient_mk).2 _, huc], exact is_open_compl_iff.2 hu_clopen.2 }, { exact mem_image_of_mem _ (mem_Inter.2 (λ Z, mem_Inter.2 (λ Zmem, Z.2.2))) }, { apply mem_image_of_mem, exact mem_of_subset_of_mem (subset_compl_iff_disjoint.2 ha) (@mem_connected_component _ _ a) }, apply preimage_injective.2 (@surjective_quotient_mk _ _), rw [preimage_inter, preimage_empty, hu, huc, inter_compl_self _], end end connected_component_setoid
c48a76dd7c25aa6d6f2d0dd4c66b4ec48d6e5662
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/measure_theory/outer_measure.lean
a229a20668fa3d0cbb0ea9acde189a680800204f
[]
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
37,330
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.analysis.specific_limits import Mathlib.measure_theory.measurable_space import Mathlib.topology.algebra.infinite_sum import Mathlib.PostPort universes u_1 l u_2 u_3 u namespace Mathlib /-! # Outer Measures An outer measure is a function `μ : set α → ennreal`, from the powerset of a type to the extended nonnegative real numbers that satisfies the following conditions: 1. `μ ∅ = 0`; 2. `μ` is monotone; 3. `μ` is countably subadditive. This means that the outer measure of a countable union is at most the sum of the outer measure on the individual sets. Note that we do not need `α` to be measurable to define an outer measure. The outer measures on a type `α` form a complete lattice. Given an arbitrary function `m : set α → ennreal` that sends `∅` to `0` we can define an outer measure on `α` that on `s` is defined to be the infimum of `∑ᵢ, m (sᵢ)` for all collections of sets `sᵢ` that cover `s`. This is the unique maximal outer measure that is at most the given function. We also define this for functions `m` defined on a subset of `set α`, by treating the function as having value `∞` outside its domain. Given an outer measure `m`, the Carathéodory-measurable sets are the sets `s` such that for all sets `t` we have `m t = m (t ∩ s) + m (t \ s)`. This forms a measurable space. ## Main definitions and statements * `outer_measure.bounded_by` is the greatest outer measure that is at most the given function. If you know that the given functions sends `∅` to `0`, then `outer_measure.of_function` is a special case. * `caratheodory` is the Carathéodory-measurable space of an outer measure. * `Inf_eq_of_function_Inf_gen` is a characterization of the infimum of outer measures. * `induced_outer_measure` is the measure induced by a function on a subset of `set α` ## References * <https://en.wikipedia.org/wiki/Outer_measure> * <https://en.wikipedia.org/wiki/Carath%C3%A9odory%27s_criterion> ## Tags outer measure, Carathéodory-measurable, Carathéodory's criterion -/ namespace measure_theory /-- An outer measure is a countably subadditive monotone function that sends `∅` to `0`. -/ structure outer_measure (α : Type u_1) where measure_of : set α → ennreal empty : measure_of ∅ = 0 mono : ∀ {s₁ s₂ : set α}, s₁ ⊆ s₂ → measure_of s₁ ≤ measure_of s₂ Union_nat : ∀ (s : ℕ → set α), measure_of (set.Union fun (i : ℕ) => s i) ≤ tsum fun (i : ℕ) => measure_of (s i) namespace outer_measure protected instance has_coe_to_fun {α : Type u_1} : has_coe_to_fun (outer_measure α) := has_coe_to_fun.mk (fun (m : outer_measure α) => set α → ennreal) fun (m : outer_measure α) => measure_of m @[simp] theorem measure_of_eq_coe {α : Type u_1} (m : outer_measure α) : measure_of m = ⇑m := rfl @[simp] theorem empty' {α : Type u_1} (m : outer_measure α) : coe_fn m ∅ = 0 := empty m theorem mono' {α : Type u_1} (m : outer_measure α) {s₁ : set α} {s₂ : set α} (h : s₁ ⊆ s₂) : coe_fn m s₁ ≤ coe_fn m s₂ := mono m h protected theorem Union {α : Type u_1} (m : outer_measure α) {β : Type u_2} [encodable β] (s : β → set α) : coe_fn m (set.Union fun (i : β) => s i) ≤ tsum fun (i : β) => coe_fn m (s i) := rel_supr_tsum (⇑m) (empty m) LessEq (Union_nat m) s theorem Union_null {α : Type u_1} (m : outer_measure α) {β : Type u_2} [encodable β] {s : β → set α} (h : ∀ (i : β), coe_fn m (s i) = 0) : coe_fn m (set.Union fun (i : β) => s i) = 0 := sorry protected theorem Union_finset {α : Type u_1} {β : Type u_2} (m : outer_measure α) (s : β → set α) (t : finset β) : coe_fn m (set.Union fun (i : β) => set.Union fun (H : i ∈ t) => s i) ≤ finset.sum t fun (i : β) => coe_fn m (s i) := rel_supr_sum (⇑m) (empty m) LessEq (Union_nat m) s t protected theorem union {α : Type u_1} (m : outer_measure α) (s₁ : set α) (s₂ : set α) : coe_fn m (s₁ ∪ s₂) ≤ coe_fn m s₁ + coe_fn m s₂ := rel_sup_add (⇑m) (empty m) LessEq (Union_nat m) s₁ s₂ theorem le_inter_add_diff {α : Type u_1} {m : outer_measure α} {t : set α} (s : set α) : coe_fn m t ≤ coe_fn m (t ∩ s) + coe_fn m (t \ s) := sorry theorem diff_null {α : Type u_1} (m : outer_measure α) (s : set α) {t : set α} (ht : coe_fn m t = 0) : coe_fn m (s \ t) = coe_fn m s := sorry theorem union_null {α : Type u_1} (m : outer_measure α) {s₁ : set α} {s₂ : set α} (h₁ : coe_fn m s₁ = 0) (h₂ : coe_fn m s₂ = 0) : coe_fn m (s₁ ∪ s₂) = 0 := sorry theorem injective_coe_fn {α : Type u_1} : function.injective fun (μ : outer_measure α) (s : set α) => coe_fn μ s := sorry theorem ext {α : Type u_1} {μ₁ : outer_measure α} {μ₂ : outer_measure α} (h : ∀ (s : set α), coe_fn μ₁ s = coe_fn μ₂ s) : μ₁ = μ₂ := injective_coe_fn (funext h) protected instance has_zero {α : Type u_1} : HasZero (outer_measure α) := { zero := mk (fun (_x : set α) => 0) sorry sorry sorry } @[simp] theorem coe_zero {α : Type u_1} : ⇑0 = 0 := rfl protected instance inhabited {α : Type u_1} : Inhabited (outer_measure α) := { default := 0 } protected instance has_add {α : Type u_1} : Add (outer_measure α) := { add := fun (m₁ m₂ : outer_measure α) => mk (fun (s : set α) => coe_fn m₁ s + coe_fn m₂ s) sorry sorry sorry } @[simp] theorem coe_add {α : Type u_1} (m₁ : outer_measure α) (m₂ : outer_measure α) : ⇑(m₁ + m₂) = ⇑m₁ + ⇑m₂ := rfl theorem add_apply {α : Type u_1} (m₁ : outer_measure α) (m₂ : outer_measure α) (s : set α) : coe_fn (m₁ + m₂) s = coe_fn m₁ s + coe_fn m₂ s := rfl protected instance add_comm_monoid {α : Type u_1} : add_comm_monoid (outer_measure α) := add_comm_monoid.mk Add.add sorry 0 sorry sorry sorry protected instance has_scalar {α : Type u_1} : has_scalar ennreal (outer_measure α) := has_scalar.mk fun (c : ennreal) (m : outer_measure α) => mk (fun (s : set α) => c * coe_fn m s) sorry sorry sorry @[simp] theorem coe_smul {α : Type u_1} (c : ennreal) (m : outer_measure α) : ⇑(c • m) = c • ⇑m := rfl theorem smul_apply {α : Type u_1} (c : ennreal) (m : outer_measure α) (s : set α) : coe_fn (c • m) s = c * coe_fn m s := rfl protected instance semimodule {α : Type u_1} : semimodule ennreal (outer_measure α) := semimodule.mk sorry sorry protected instance has_bot {α : Type u_1} : has_bot (outer_measure α) := has_bot.mk 0 protected instance outer_measure.order_bot {α : Type u_1} : order_bot (outer_measure α) := order_bot.mk 0 (fun (m₁ m₂ : outer_measure α) => ∀ (s : set α), coe_fn m₁ s ≤ coe_fn m₂ s) (partial_order.lt._default fun (m₁ m₂ : outer_measure α) => ∀ (s : set α), coe_fn m₁ s ≤ coe_fn m₂ s) sorry sorry sorry sorry protected instance has_Sup {α : Type u_1} : has_Sup (outer_measure α) := has_Sup.mk fun (ms : set (outer_measure α)) => mk (fun (s : set α) => supr fun (m : outer_measure α) => supr fun (H : m ∈ ms) => coe_fn m s) sorry sorry sorry protected instance complete_lattice {α : Type u_1} : complete_lattice (outer_measure α) := complete_lattice.mk complete_lattice.sup order_bot.le order_bot.lt sorry sorry sorry sorry sorry sorry complete_lattice.inf sorry sorry sorry complete_lattice.top sorry order_bot.bot sorry complete_lattice.Sup complete_lattice.Inf sorry sorry sorry sorry @[simp] theorem Sup_apply {α : Type u_1} (ms : set (outer_measure α)) (s : set α) : coe_fn (Sup ms) s = supr fun (m : outer_measure α) => supr fun (H : m ∈ ms) => coe_fn m s := rfl @[simp] theorem supr_apply {α : Type u_1} {ι : Sort u_2} (f : ι → outer_measure α) (s : set α) : coe_fn (supr fun (i : ι) => f i) s = supr fun (i : ι) => coe_fn (f i) s := sorry theorem coe_supr {α : Type u_1} {ι : Sort u_2} (f : ι → outer_measure α) : ⇑(supr fun (i : ι) => f i) = supr fun (i : ι) => ⇑(f i) := sorry @[simp] theorem sup_apply {α : Type u_1} (m₁ : outer_measure α) (m₂ : outer_measure α) (s : set α) : coe_fn (m₁ ⊔ m₂) s = coe_fn m₁ s ⊔ coe_fn m₂ s := sorry /-- The pushforward of `m` along `f`. The outer measure on `s` is defined to be `m (f ⁻¹' s)`. -/ def map {α : Type u_1} {β : Type u_2} (f : α → β) : linear_map ennreal (outer_measure α) (outer_measure β) := linear_map.mk (fun (m : outer_measure α) => mk (fun (s : set β) => coe_fn m (f ⁻¹' s)) (empty m) sorry sorry) sorry sorry @[simp] theorem map_apply {α : Type u_1} {β : Type u_2} (f : α → β) (m : outer_measure α) (s : set β) : coe_fn (coe_fn (map f) m) s = coe_fn m (f ⁻¹' s) := rfl @[simp] theorem map_id {α : Type u_1} (m : outer_measure α) : coe_fn (map id) m = m := ext fun (s : set α) => rfl @[simp] theorem map_map {α : Type u_1} {β : Type u_2} {γ : Type u_3} (f : α → β) (g : β → γ) (m : outer_measure α) : coe_fn (map g) (coe_fn (map f) m) = coe_fn (map (g ∘ f)) m := ext fun (s : set γ) => rfl protected instance functor : Functor outer_measure := { map := fun (α β : Type u_1) (f : α → β) => ⇑(map f), mapConst := fun (α β : Type u_1) => (fun (f : β → α) => ⇑(map f)) ∘ function.const β } protected instance is_lawful_functor : is_lawful_functor outer_measure := is_lawful_functor.mk (fun (α : Type u_1) => map_id) fun (α β γ : Type u_1) (f : α → β) (g : β → γ) (m : outer_measure α) => Eq.symm (map_map f g m) /-- The dirac outer measure. -/ def dirac {α : Type u_1} (a : α) : outer_measure α := mk (fun (s : set α) => set.indicator s (fun (_x : α) => 1) a) sorry sorry sorry @[simp] theorem dirac_apply {α : Type u_1} (a : α) (s : set α) : coe_fn (dirac a) s = set.indicator s (fun (_x : α) => 1) a := rfl /-- The sum of an (arbitrary) collection of outer measures. -/ def sum {α : Type u_1} {ι : Type u_2} (f : ι → outer_measure α) : outer_measure α := mk (fun (s : set α) => tsum fun (i : ι) => coe_fn (f i) s) sorry sorry sorry @[simp] theorem sum_apply {α : Type u_1} {ι : Type u_2} (f : ι → outer_measure α) (s : set α) : coe_fn (sum f) s = tsum fun (i : ι) => coe_fn (f i) s := rfl theorem smul_dirac_apply {α : Type u_1} (a : ennreal) (b : α) (s : set α) : coe_fn (a • dirac b) s = set.indicator s (fun (_x : α) => a) b := sorry /-- Pullback of an `outer_measure`: `comap f μ s = μ (f '' s)`. -/ def comap {α : Type u_1} {β : Type u_2} (f : α → β) : linear_map ennreal (outer_measure β) (outer_measure α) := linear_map.mk (fun (m : outer_measure β) => mk (fun (s : set α) => coe_fn m (f '' s)) sorry sorry sorry) sorry sorry @[simp] theorem comap_apply {α : Type u_1} {β : Type u_2} (f : α → β) (m : outer_measure β) (s : set α) : coe_fn (coe_fn (comap f) m) s = coe_fn m (f '' s) := rfl /-- Restrict an `outer_measure` to a set. -/ def restrict {α : Type u_1} (s : set α) : linear_map ennreal (outer_measure α) (outer_measure α) := linear_map.comp (map coe) (comap coe) @[simp] theorem restrict_apply {α : Type u_1} (s : set α) (t : set α) (m : outer_measure α) : coe_fn (coe_fn (restrict s) m) t = coe_fn m (t ∩ s) := sorry theorem top_apply {α : Type u_1} {s : set α} (h : set.nonempty s) : coe_fn ⊤ s = ⊤ := sorry /-- Given any function `m` assigning measures to sets satisying `m ∅ = 0`, there is a unique maximal outer measure `μ` satisfying `μ s ≤ m s` for all `s : set α`. -/ protected def of_function {α : Type u_1} (m : set α → ennreal) (m_empty : m ∅ = 0) : outer_measure α := let μ : set α → ennreal := fun (s : set α) => infi fun {f : ℕ → set α} => infi fun (h : s ⊆ set.Union fun (i : ℕ) => f i) => tsum fun (i : ℕ) => m (f i); mk μ sorry sorry sorry theorem of_function_apply {α : Type u_1} (m : set α → ennreal) (m_empty : m ∅ = 0) (s : set α) : coe_fn (outer_measure.of_function m m_empty) s = infi fun (t : ℕ → set α) => infi fun (h : s ⊆ set.Union t) => tsum fun (n : ℕ) => m (t n) := rfl theorem of_function_le {α : Type u_1} {m : set α → ennreal} {m_empty : m ∅ = 0} (s : set α) : coe_fn (outer_measure.of_function m m_empty) s ≤ m s := sorry theorem of_function_eq {α : Type u_1} {m : set α → ennreal} {m_empty : m ∅ = 0} (s : set α) (m_mono : ∀ {t : set α}, s ⊆ t → m s ≤ m t) (m_subadd : ∀ (s : ℕ → set α), m (set.Union fun (i : ℕ) => s i) ≤ tsum fun (i : ℕ) => m (s i)) : coe_fn (outer_measure.of_function m m_empty) s = m s := le_antisymm (of_function_le s) (le_infi fun (f : ℕ → set α) => le_infi fun (hf : s ⊆ set.Union fun (i : ℕ) => f i) => le_trans (m_mono hf) (m_subadd f)) theorem le_of_function {α : Type u_1} {m : set α → ennreal} {m_empty : m ∅ = 0} {μ : outer_measure α} : μ ≤ outer_measure.of_function m m_empty ↔ ∀ (s : set α), coe_fn μ s ≤ m s := sorry /-- Given any function `m` assigning measures to sets, there is a unique maximal outer measure `μ` satisfying `μ s ≤ m s` for all `s : set α`. This is the same as `outer_measure.of_function`, except that it doesn't require `m ∅ = 0`. -/ def bounded_by {α : Type u_1} (m : set α → ennreal) : outer_measure α := outer_measure.of_function (fun (s : set α) => supr fun (h : set.nonempty s) => m s) sorry theorem bounded_by_le {α : Type u_1} {m : set α → ennreal} (s : set α) : coe_fn (bounded_by m) s ≤ m s := has_le.le.trans (of_function_le s) supr_const_le theorem bounded_by_eq_of_function {α : Type u_1} {m : set α → ennreal} (m_empty : m ∅ = 0) (s : set α) : coe_fn (bounded_by m) s = coe_fn (outer_measure.of_function m m_empty) s := sorry theorem bounded_by_apply {α : Type u_1} {m : set α → ennreal} (s : set α) : coe_fn (bounded_by m) s = infi fun (t : ℕ → set α) => infi fun (h : s ⊆ set.Union t) => tsum fun (n : ℕ) => supr fun (h : set.nonempty (t n)) => m (t n) := sorry theorem bounded_by_eq {α : Type u_1} {m : set α → ennreal} (s : set α) (m_empty : m ∅ = 0) (m_mono : ∀ {t : set α}, s ⊆ t → m s ≤ m t) (m_subadd : ∀ (s : ℕ → set α), m (set.Union fun (i : ℕ) => s i) ≤ tsum fun (i : ℕ) => m (s i)) : coe_fn (bounded_by m) s = m s := sorry theorem le_bounded_by {α : Type u_1} {m : set α → ennreal} {μ : outer_measure α} : μ ≤ bounded_by m ↔ ∀ (s : set α), coe_fn μ s ≤ m s := sorry theorem le_bounded_by' {α : Type u_1} {m : set α → ennreal} {μ : outer_measure α} : μ ≤ bounded_by m ↔ ∀ (s : set α), set.nonempty s → coe_fn μ s ≤ m s := sorry /-- A set `s` is Carathéodory-measurable for an outer measure `m` if for all sets `t` we have `m t = m (t ∩ s) + m (t \ s)`. -/ def is_caratheodory {α : Type u} (m : outer_measure α) (s : set α) := ∀ (t : set α), coe_fn m t = coe_fn m (t ∩ s) + coe_fn m (t \ s) theorem is_caratheodory_iff_le' {α : Type u} (m : outer_measure α) {s : set α} : is_caratheodory m s ↔ ∀ (t : set α), coe_fn m (t ∩ s) + coe_fn m (t \ s) ≤ coe_fn m t := forall_congr fun (t : set α) => iff.trans le_antisymm_iff (and_iff_right (le_inter_add_diff s)) @[simp] theorem is_caratheodory_empty {α : Type u} (m : outer_measure α) : is_caratheodory m ∅ := sorry theorem is_caratheodory_compl {α : Type u} (m : outer_measure α) {s₁ : set α} : is_caratheodory m s₁ → is_caratheodory m (s₁ᶜ) := sorry @[simp] theorem is_caratheodory_compl_iff {α : Type u} (m : outer_measure α) {s : set α} : is_caratheodory m (sᶜ) ↔ is_caratheodory m s := sorry theorem is_caratheodory_union {α : Type u} (m : outer_measure α) {s₁ : set α} {s₂ : set α} (h₁ : is_caratheodory m s₁) (h₂ : is_caratheodory m s₂) : is_caratheodory m (s₁ ∪ s₂) := sorry theorem measure_inter_union {α : Type u} (m : outer_measure α) {s₁ : set α} {s₂ : set α} (h : s₁ ∩ s₂ ⊆ ∅) (h₁ : is_caratheodory m s₁) {t : set α} : coe_fn m (t ∩ (s₁ ∪ s₂)) = coe_fn m (t ∩ s₁) + coe_fn m (t ∩ s₂) := sorry theorem is_caratheodory_Union_lt {α : Type u} (m : outer_measure α) {s : ℕ → set α} {n : ℕ} : (∀ (i : ℕ), i < n → is_caratheodory m (s i)) → is_caratheodory m (set.Union fun (i : ℕ) => set.Union fun (H : i < n) => s i) := sorry theorem is_caratheodory_inter {α : Type u} (m : outer_measure α) {s₁ : set α} {s₂ : set α} (h₁ : is_caratheodory m s₁) (h₂ : is_caratheodory m s₂) : is_caratheodory m (s₁ ∩ s₂) := eq.mpr (id (Eq._oldrec (Eq.refl (is_caratheodory m (s₁ ∩ s₂))) (Eq.symm (propext (is_caratheodory_compl_iff m))))) (eq.mpr (id (Eq._oldrec (Eq.refl (is_caratheodory m (s₁ ∩ s₂ᶜ))) (set.compl_inter s₁ s₂))) (is_caratheodory_union m (is_caratheodory_compl m h₁) (is_caratheodory_compl m h₂))) theorem is_caratheodory_sum {α : Type u} (m : outer_measure α) {s : ℕ → set α} (h : ∀ (i : ℕ), is_caratheodory m (s i)) (hd : pairwise (disjoint on s)) {t : set α} {n : ℕ} : (finset.sum (finset.range n) fun (i : ℕ) => coe_fn m (t ∩ s i)) = coe_fn m (t ∩ set.Union fun (i : ℕ) => set.Union fun (H : i < n) => s i) := sorry theorem is_caratheodory_Union_nat {α : Type u} (m : outer_measure α) {s : ℕ → set α} (h : ∀ (i : ℕ), is_caratheodory m (s i)) (hd : pairwise (disjoint on s)) : is_caratheodory m (set.Union fun (i : ℕ) => s i) := sorry theorem f_Union {α : Type u} (m : outer_measure α) {s : ℕ → set α} (h : ∀ (i : ℕ), is_caratheodory m (s i)) (hd : pairwise (disjoint on s)) : coe_fn m (set.Union fun (i : ℕ) => s i) = tsum fun (i : ℕ) => coe_fn m (s i) := sorry /-- The Carathéodory-measurable sets for an outer measure `m` form a Dynkin system. -/ def caratheodory_dynkin {α : Type u} (m : outer_measure α) : measurable_space.dynkin_system α := measurable_space.dynkin_system.mk (is_caratheodory m) (is_caratheodory_empty m) sorry sorry /-- Given an outer measure `μ`, the Carathéodory-measurable space is defined such that `s` is measurable if `∀t, μ t = μ (t ∩ s) + μ (t \ s)`. -/ protected def caratheodory {α : Type u} (m : outer_measure α) : measurable_space α := measurable_space.dynkin_system.to_measurable_space (caratheodory_dynkin m) sorry theorem is_caratheodory_iff {α : Type u} (m : outer_measure α) {s : set α} : measurable_space.is_measurable' (outer_measure.caratheodory m) s ↔ ∀ (t : set α), coe_fn m t = coe_fn m (t ∩ s) + coe_fn m (t \ s) := iff.rfl theorem is_caratheodory_iff_le {α : Type u} (m : outer_measure α) {s : set α} : measurable_space.is_measurable' (outer_measure.caratheodory m) s ↔ ∀ (t : set α), coe_fn m (t ∩ s) + coe_fn m (t \ s) ≤ coe_fn m t := is_caratheodory_iff_le' m protected theorem Union_eq_of_caratheodory {α : Type u} (m : outer_measure α) {s : ℕ → set α} (h : ∀ (i : ℕ), measurable_space.is_measurable' (outer_measure.caratheodory m) (s i)) (hd : pairwise (disjoint on s)) : coe_fn m (set.Union fun (i : ℕ) => s i) = tsum fun (i : ℕ) => coe_fn m (s i) := f_Union m h hd theorem of_function_caratheodory {α : Type u_1} {m : set α → ennreal} {s : set α} {h₀ : m ∅ = 0} (hs : ∀ (t : set α), m (t ∩ s) + m (t \ s) ≤ m t) : measurable_space.is_measurable' (outer_measure.caratheodory (outer_measure.of_function m h₀)) s := sorry theorem bounded_by_caratheodory {α : Type u_1} {m : set α → ennreal} {s : set α} (hs : ∀ (t : set α), m (t ∩ s) + m (t \ s) ≤ m t) : measurable_space.is_measurable' (outer_measure.caratheodory (bounded_by m)) s := sorry @[simp] theorem zero_caratheodory {α : Type u_1} : outer_measure.caratheodory 0 = ⊤ := top_unique fun (s : set α) (_x : measurable_space.is_measurable' ⊤ s) (t : set α) => Eq.symm (add_zero (coe_fn 0 t)) theorem top_caratheodory {α : Type u_1} : outer_measure.caratheodory ⊤ = ⊤ := sorry theorem le_add_caratheodory {α : Type u_1} (m₁ : outer_measure α) (m₂ : outer_measure α) : outer_measure.caratheodory m₁ ⊓ outer_measure.caratheodory m₂ ≤ outer_measure.caratheodory (m₁ + m₂) := sorry theorem le_sum_caratheodory {α : Type u_1} {ι : Type u_2} (m : ι → outer_measure α) : (infi fun (i : ι) => outer_measure.caratheodory (m i)) ≤ outer_measure.caratheodory (sum m) := sorry theorem le_smul_caratheodory {α : Type u_1} (a : ennreal) (m : outer_measure α) : outer_measure.caratheodory m ≤ outer_measure.caratheodory (a • m) := sorry @[simp] theorem dirac_caratheodory {α : Type u_1} (a : α) : outer_measure.caratheodory (dirac a) = ⊤ := sorry /-- Given a set of outer measures, we define a new function that on a set `s` is defined to be the infimum of `μ(s)` for the outer measures `μ` in the collection. We ensure that this function is defined to be `0` on `∅`, even if the collection of outer measures is empty. The outer measure generated by this function is the infimum of the given outer measures. -/ def Inf_gen {α : Type u_1} (m : set (outer_measure α)) (s : set α) : ennreal := infi fun (μ : outer_measure α) => infi fun (h : μ ∈ m) => coe_fn μ s theorem Inf_gen_def {α : Type u_1} (m : set (outer_measure α)) (t : set α) : Inf_gen m t = infi fun (μ : outer_measure α) => infi fun (h : μ ∈ m) => coe_fn μ t := rfl theorem Inf_eq_bounded_by_Inf_gen {α : Type u_1} (m : set (outer_measure α)) : Inf m = bounded_by (Inf_gen m) := sorry theorem supr_Inf_gen_nonempty {α : Type u_1} {m : set (outer_measure α)} (h : set.nonempty m) (t : set α) : (supr fun (h : set.nonempty t) => Inf_gen m t) = infi fun (μ : outer_measure α) => infi fun (h : μ ∈ m) => coe_fn μ t := sorry /-- The value of the Infimum of a nonempty set of outer measures on a set is not simply the minimum value of a measure on that set: it is the infimum sum of measures of countable set of sets that covers that set, where a different measure can be used for each set in the cover. -/ theorem Inf_apply {α : Type u_1} {m : set (outer_measure α)} {s : set α} (h : set.nonempty m) : coe_fn (Inf m) s = infi fun (t : ℕ → set α) => infi fun (h2 : s ⊆ set.Union t) => tsum fun (n : ℕ) => infi fun (μ : outer_measure α) => infi fun (h3 : μ ∈ m) => coe_fn μ (t n) := sorry /-- This proves that Inf and restrict commute for outer measures, so long as the set of outer measures is nonempty. -/ theorem restrict_Inf_eq_Inf_restrict {α : Type u_1} (m : set (outer_measure α)) {s : set α} (hm : set.nonempty m) : coe_fn (restrict s) (Inf m) = Inf (⇑(restrict s) '' m) := sorry end outer_measure /-! ### Induced Outer Measure We can extend a function defined on a subset of `set α` to an outer measure. The underlying function is called `extend`, and the measure it induces is called `induced_outer_measure`. Some lemmas below are proven twice, once in the general case, and one where the function `m` is only defined on measurable sets (i.e. when `P = is_measurable`). In the latter cases, we can remove some hypotheses in the statement. The general version has the same name, but with a prime at the end. -/ /-- We can trivially extend a function defined on a subclass of objects (with codomain `ennreal`) to all objects by defining it to be `∞` on the objects not in the class. -/ def extend {α : Type u_1} {P : α → Prop} (m : (s : α) → P s → ennreal) (s : α) : ennreal := infi fun (h : P s) => m s h theorem extend_eq {α : Type u_1} {P : α → Prop} (m : (s : α) → P s → ennreal) {s : α} (h : P s) : extend m s = m s h := sorry theorem le_extend {α : Type u_1} {P : α → Prop} (m : (s : α) → P s → ennreal) {s : α} (h : P s) : m s h ≤ extend m s := sorry theorem extend_empty {α : Type u_1} {P : set α → Prop} {m : (s : set α) → P s → ennreal} (P0 : P ∅) (m0 : m ∅ P0 = 0) : extend m ∅ = 0 := Eq.trans (extend_eq m P0) m0 theorem extend_Union_nat {α : Type u_1} {P : set α → Prop} {m : (s : set α) → P s → ennreal} (PU : ∀ {f : ℕ → set α}, (∀ (i : ℕ), P (f i)) → P (set.Union fun (i : ℕ) => f i)) {f : ℕ → set α} (hm : ∀ (i : ℕ), P (f i)) (mU : m (set.Union fun (i : ℕ) => f i) (PU hm) = tsum fun (i : ℕ) => m (f i) (hm i)) : extend m (set.Union fun (i : ℕ) => f i) = tsum fun (i : ℕ) => extend m (f i) := sorry theorem extend_Union_le_tsum_nat' {α : Type u_1} {P : set α → Prop} {m : (s : set α) → P s → ennreal} (PU : ∀ {f : ℕ → set α}, (∀ (i : ℕ), P (f i)) → P (set.Union fun (i : ℕ) => f i)) (msU : ∀ {f : ℕ → set α} (hm : ∀ (i : ℕ), P (f i)), m (set.Union fun (i : ℕ) => f i) (PU hm) ≤ tsum fun (i : ℕ) => m (f i) (hm i)) (s : ℕ → set α) : extend m (set.Union fun (i : ℕ) => s i) ≤ tsum fun (i : ℕ) => extend m (s i) := sorry theorem extend_mono' {α : Type u_1} {P : set α → Prop} {m : (s : set α) → P s → ennreal} (m_mono : ∀ {s₁ s₂ : set α} (hs₁ : P s₁) (hs₂ : P s₂), s₁ ⊆ s₂ → m s₁ hs₁ ≤ m s₂ hs₂) {s₁ : set α} {s₂ : set α} (h₁ : P s₁) (hs : s₁ ⊆ s₂) : extend m s₁ ≤ extend m s₂ := le_infi fun (h₂ : P s₂) => eq.mpr (id (Eq._oldrec (Eq.refl (extend m s₁ ≤ m s₂ h₂)) (extend_eq m h₁))) (m_mono h₁ h₂ hs) theorem extend_Union {α : Type u_1} {P : set α → Prop} {m : (s : set α) → P s → ennreal} (P0 : P ∅) (m0 : m ∅ P0 = 0) (PU : ∀ {f : ℕ → set α}, (∀ (i : ℕ), P (f i)) → P (set.Union fun (i : ℕ) => f i)) (mU : ∀ {f : ℕ → set α} (hm : ∀ (i : ℕ), P (f i)), pairwise (disjoint on f) → m (set.Union fun (i : ℕ) => f i) (PU hm) = tsum fun (i : ℕ) => m (f i) (hm i)) {β : Type u_2} [encodable β] {f : β → set α} (hd : pairwise (disjoint on f)) (hm : ∀ (i : β), P (f i)) : extend m (set.Union fun (i : β) => f i) = tsum fun (i : β) => extend m (f i) := sorry theorem extend_union {α : Type u_1} {P : set α → Prop} {m : (s : set α) → P s → ennreal} (P0 : P ∅) (m0 : m ∅ P0 = 0) (PU : ∀ {f : ℕ → set α}, (∀ (i : ℕ), P (f i)) → P (set.Union fun (i : ℕ) => f i)) (mU : ∀ {f : ℕ → set α} (hm : ∀ (i : ℕ), P (f i)), pairwise (disjoint on f) → m (set.Union fun (i : ℕ) => f i) (PU hm) = tsum fun (i : ℕ) => m (f i) (hm i)) {s₁ : set α} {s₂ : set α} (hd : disjoint s₁ s₂) (h₁ : P s₁) (h₂ : P s₂) : extend m (s₁ ∪ s₂) = extend m s₁ + extend m s₂ := sorry /-- Given an arbitrary function on a subset of sets, we can define the outer measure corresponding to it (this is the unique maximal outer measure that is at most `m` on the domain of `m`). -/ def induced_outer_measure {α : Type u_1} {P : set α → Prop} (m : (s : set α) → P s → ennreal) (P0 : P ∅) (m0 : m ∅ P0 = 0) : outer_measure α := outer_measure.of_function (extend m) sorry theorem induced_outer_measure_eq_extend' {α : Type u_1} {P : set α → Prop} {m : (s : set α) → P s → ennreal} {P0 : P ∅} {m0 : m ∅ P0 = 0} (PU : ∀ {f : ℕ → set α}, (∀ (i : ℕ), P (f i)) → P (set.Union fun (i : ℕ) => f i)) (msU : ∀ {f : ℕ → set α} (hm : ∀ (i : ℕ), P (f i)), m (set.Union fun (i : ℕ) => f i) (PU hm) ≤ tsum fun (i : ℕ) => m (f i) (hm i)) (m_mono : ∀ {s₁ s₂ : set α} (hs₁ : P s₁) (hs₂ : P s₂), s₁ ⊆ s₂ → m s₁ hs₁ ≤ m s₂ hs₂) {s : set α} (hs : P s) : coe_fn (induced_outer_measure m P0 m0) s = extend m s := outer_measure.of_function_eq s (fun (t : set α) => extend_mono' m_mono hs) (extend_Union_le_tsum_nat' PU msU) theorem induced_outer_measure_eq' {α : Type u_1} {P : set α → Prop} {m : (s : set α) → P s → ennreal} {P0 : P ∅} {m0 : m ∅ P0 = 0} (PU : ∀ {f : ℕ → set α}, (∀ (i : ℕ), P (f i)) → P (set.Union fun (i : ℕ) => f i)) (msU : ∀ {f : ℕ → set α} (hm : ∀ (i : ℕ), P (f i)), m (set.Union fun (i : ℕ) => f i) (PU hm) ≤ tsum fun (i : ℕ) => m (f i) (hm i)) (m_mono : ∀ {s₁ s₂ : set α} (hs₁ : P s₁) (hs₂ : P s₂), s₁ ⊆ s₂ → m s₁ hs₁ ≤ m s₂ hs₂) {s : set α} (hs : P s) : coe_fn (induced_outer_measure m P0 m0) s = m s hs := Eq.trans (induced_outer_measure_eq_extend' PU msU m_mono hs) (extend_eq m hs) theorem induced_outer_measure_eq_infi {α : Type u_1} {P : set α → Prop} {m : (s : set α) → P s → ennreal} {P0 : P ∅} {m0 : m ∅ P0 = 0} (PU : ∀ {f : ℕ → set α}, (∀ (i : ℕ), P (f i)) → P (set.Union fun (i : ℕ) => f i)) (msU : ∀ {f : ℕ → set α} (hm : ∀ (i : ℕ), P (f i)), m (set.Union fun (i : ℕ) => f i) (PU hm) ≤ tsum fun (i : ℕ) => m (f i) (hm i)) (m_mono : ∀ {s₁ s₂ : set α} (hs₁ : P s₁) (hs₂ : P s₂), s₁ ⊆ s₂ → m s₁ hs₁ ≤ m s₂ hs₂) (s : set α) : coe_fn (induced_outer_measure m P0 m0) s = infi fun (t : set α) => infi fun (ht : P t) => infi fun (h : s ⊆ t) => m t ht := sorry theorem induced_outer_measure_preimage {α : Type u_1} {P : set α → Prop} {m : (s : set α) → P s → ennreal} {P0 : P ∅} {m0 : m ∅ P0 = 0} (PU : ∀ {f : ℕ → set α}, (∀ (i : ℕ), P (f i)) → P (set.Union fun (i : ℕ) => f i)) (msU : ∀ {f : ℕ → set α} (hm : ∀ (i : ℕ), P (f i)), m (set.Union fun (i : ℕ) => f i) (PU hm) ≤ tsum fun (i : ℕ) => m (f i) (hm i)) (m_mono : ∀ {s₁ s₂ : set α} (hs₁ : P s₁) (hs₂ : P s₂), s₁ ⊆ s₂ → m s₁ hs₁ ≤ m s₂ hs₂) (f : α ≃ α) (Pm : ∀ (s : set α), P (⇑f ⁻¹' s) ↔ P s) (mm : ∀ (s : set α) (hs : P s), m (⇑f ⁻¹' s) (iff.mpr (Pm s) hs) = m s hs) {A : set α} : coe_fn (induced_outer_measure m P0 m0) (⇑f ⁻¹' A) = coe_fn (induced_outer_measure m P0 m0) A := sorry theorem induced_outer_measure_exists_set {α : Type u_1} {P : set α → Prop} {m : (s : set α) → P s → ennreal} {P0 : P ∅} {m0 : m ∅ P0 = 0} (PU : ∀ {f : ℕ → set α}, (∀ (i : ℕ), P (f i)) → P (set.Union fun (i : ℕ) => f i)) (msU : ∀ {f : ℕ → set α} (hm : ∀ (i : ℕ), P (f i)), m (set.Union fun (i : ℕ) => f i) (PU hm) ≤ tsum fun (i : ℕ) => m (f i) (hm i)) (m_mono : ∀ {s₁ s₂ : set α} (hs₁ : P s₁) (hs₂ : P s₂), s₁ ⊆ s₂ → m s₁ hs₁ ≤ m s₂ hs₂) {s : set α} (hs : coe_fn (induced_outer_measure m P0 m0) s < ⊤) {ε : nnreal} (hε : 0 < ε) : ∃ (t : set α), ∃ (ht : P t), s ⊆ t ∧ coe_fn (induced_outer_measure m P0 m0) t ≤ coe_fn (induced_outer_measure m P0 m0) s + ↑ε := sorry /-- To test whether `s` is Carathéodory-measurable we only need to check the sets `t` for which `P t` holds. See `of_function_caratheodory` for another way to show the Carathéodory-measurability of `s`. -/ theorem induced_outer_measure_caratheodory {α : Type u_1} {P : set α → Prop} {m : (s : set α) → P s → ennreal} {P0 : P ∅} {m0 : m ∅ P0 = 0} (PU : ∀ {f : ℕ → set α}, (∀ (i : ℕ), P (f i)) → P (set.Union fun (i : ℕ) => f i)) (msU : ∀ {f : ℕ → set α} (hm : ∀ (i : ℕ), P (f i)), m (set.Union fun (i : ℕ) => f i) (PU hm) ≤ tsum fun (i : ℕ) => m (f i) (hm i)) (m_mono : ∀ {s₁ s₂ : set α} (hs₁ : P s₁) (hs₂ : P s₂), s₁ ⊆ s₂ → m s₁ hs₁ ≤ m s₂ hs₂) (s : set α) : measurable_space.is_measurable' (outer_measure.caratheodory (induced_outer_measure m P0 m0)) s ↔ ∀ (t : set α), P t → coe_fn (induced_outer_measure m P0 m0) (t ∩ s) + coe_fn (induced_outer_measure m P0 m0) (t \ s) ≤ coe_fn (induced_outer_measure m P0 m0) t := sorry /-! If `P` is `is_measurable` for some measurable space, then we can remove some hypotheses of the above lemmas. -/ theorem extend_mono {α : Type u_1} [measurable_space α] {m : (s : set α) → is_measurable s → ennreal} (m0 : m ∅ is_measurable.empty = 0) (mU : ∀ {f : ℕ → set α} (hm : ∀ (i : ℕ), is_measurable (f i)), pairwise (disjoint on f) → m (set.Union fun (i : ℕ) => f i) (is_measurable.Union hm) = tsum fun (i : ℕ) => m (f i) (hm i)) {s₁ : set α} {s₂ : set α} (h₁ : is_measurable s₁) (hs : s₁ ⊆ s₂) : extend m s₁ ≤ extend m s₂ := sorry theorem extend_Union_le_tsum_nat {α : Type u_1} [measurable_space α] {m : (s : set α) → is_measurable s → ennreal} (m0 : m ∅ is_measurable.empty = 0) (mU : ∀ {f : ℕ → set α} (hm : ∀ (i : ℕ), is_measurable (f i)), pairwise (disjoint on f) → m (set.Union fun (i : ℕ) => f i) (is_measurable.Union hm) = tsum fun (i : ℕ) => m (f i) (hm i)) (s : ℕ → set α) : extend m (set.Union fun (i : ℕ) => s i) ≤ tsum fun (i : ℕ) => extend m (s i) := sorry theorem induced_outer_measure_eq_extend {α : Type u_1} [measurable_space α] {m : (s : set α) → is_measurable s → ennreal} (m0 : m ∅ is_measurable.empty = 0) (mU : ∀ {f : ℕ → set α} (hm : ∀ (i : ℕ), is_measurable (f i)), pairwise (disjoint on f) → m (set.Union fun (i : ℕ) => f i) (is_measurable.Union hm) = tsum fun (i : ℕ) => m (f i) (hm i)) {s : set α} (hs : is_measurable s) : coe_fn (induced_outer_measure m is_measurable.empty m0) s = extend m s := outer_measure.of_function_eq s (fun (t : set α) => extend_mono m0 mU hs) (extend_Union_le_tsum_nat m0 mU) theorem induced_outer_measure_eq {α : Type u_1} [measurable_space α] {m : (s : set α) → is_measurable s → ennreal} (m0 : m ∅ is_measurable.empty = 0) (mU : ∀ {f : ℕ → set α} (hm : ∀ (i : ℕ), is_measurable (f i)), pairwise (disjoint on f) → m (set.Union fun (i : ℕ) => f i) (is_measurable.Union hm) = tsum fun (i : ℕ) => m (f i) (hm i)) {s : set α} (hs : is_measurable s) : coe_fn (induced_outer_measure m is_measurable.empty m0) s = m s hs := Eq.trans (induced_outer_measure_eq_extend m0 mU hs) (extend_eq m hs) namespace outer_measure /-- Given an outer measure `m` we can forget its value on non-measurable sets, and then consider `m.trim`, the unique maximal outer measure less than that function. -/ def trim {α : Type u_1} [measurable_space α] (m : outer_measure α) : outer_measure α := induced_outer_measure (fun (s : set α) (_x : is_measurable s) => coe_fn m s) is_measurable.empty (empty m) theorem le_trim {α : Type u_1} [measurable_space α] (m : outer_measure α) : m ≤ trim m := iff.mpr le_of_function fun (s : set α) => le_infi fun (_x : is_measurable s) => le_refl (coe_fn m s) theorem trim_eq {α : Type u_1} [measurable_space α] (m : outer_measure α) {s : set α} (hs : is_measurable s) : coe_fn (trim m) s = coe_fn m s := sorry theorem trim_congr {α : Type u_1} [measurable_space α] {m₁ : outer_measure α} {m₂ : outer_measure α} (H : ∀ {s : set α}, is_measurable s → coe_fn m₁ s = coe_fn m₂ s) : trim m₁ = trim m₂ := sorry theorem trim_le_trim {α : Type u_1} [measurable_space α] {m₁ : outer_measure α} {m₂ : outer_measure α} (H : m₁ ≤ m₂) : trim m₁ ≤ trim m₂ := sorry theorem le_trim_iff {α : Type u_1} [measurable_space α] {m₁ : outer_measure α} {m₂ : outer_measure α} : m₁ ≤ trim m₂ ↔ ∀ (s : set α), is_measurable s → coe_fn m₁ s ≤ coe_fn m₂ s := iff.trans le_of_function (forall_congr fun (s : set α) => le_infi_iff) theorem trim_eq_infi {α : Type u_1} [measurable_space α] (m : outer_measure α) (s : set α) : coe_fn (trim m) s = infi fun (t : set α) => infi fun (st : s ⊆ t) => infi fun (ht : is_measurable t) => coe_fn m t := sorry theorem trim_eq_infi' {α : Type u_1} [measurable_space α] (m : outer_measure α) (s : set α) : coe_fn (trim m) s = infi fun (t : Subtype fun (t : set α) => s ⊆ t ∧ is_measurable t) => coe_fn m ↑t := sorry theorem trim_trim {α : Type u_1} [measurable_space α] (m : outer_measure α) : trim (trim m) = trim m := sorry @[simp] theorem trim_zero {α : Type u_1} [measurable_space α] : trim 0 = 0 := sorry theorem trim_add {α : Type u_1} [measurable_space α] (m₁ : outer_measure α) (m₂ : outer_measure α) : trim (m₁ + m₂) = trim m₁ + trim m₂ := sorry theorem trim_sum_ge {α : Type u_1} [measurable_space α] {ι : Type u_2} (m : ι → outer_measure α) : (sum fun (i : ι) => trim (m i)) ≤ trim (sum m) := sorry theorem exists_is_measurable_superset_eq_trim {α : Type u_1} [measurable_space α] (m : outer_measure α) (s : set α) : ∃ (t : set α), s ⊆ t ∧ is_measurable t ∧ coe_fn m t = coe_fn (trim m) s := sorry theorem exists_is_measurable_superset_of_trim_eq_zero {α : Type u_1} [measurable_space α] {m : outer_measure α} {s : set α} (h : coe_fn (trim m) s = 0) : ∃ (t : set α), s ⊆ t ∧ is_measurable t ∧ coe_fn m t = 0 := sorry theorem trim_smul {α : Type u_1} [measurable_space α] (c : ennreal) (m : outer_measure α) : trim (c • m) = c • trim m := sorry /-- The trimmed property of a measure μ states that `μ.to_outer_measure.trim = μ.to_outer_measure`. This theorem shows that a restricted trimmed outer measure is a trimmed outer measure. -/ theorem restrict_trim {α : Type u_1} [measurable_space α] {μ : outer_measure α} {s : set α} (hs : is_measurable s) : trim (coe_fn (restrict s) μ) = coe_fn (restrict s) (trim μ) := sorry
7073460f919c28c5e7b6c23dd88ebfc4ad824b3f
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/topology/bases.lean
2cf4fef09493be3f1d764b118ba275a9131e4877
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
10,106
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro Bases of topologies. Countability axioms. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.topology.continuous_on import Mathlib.PostPort universes u l u_1 u_2 namespace Mathlib namespace topological_space /- countability axioms For our applications we are interested that there exists a countable basis, but we do not need the concrete basis itself. This allows us to declare these type classes as `Prop` to use them as mixins. -/ /-- A topological basis is one that satisfies the necessary conditions so that it suffices to take unions of the basis sets to get a topology (without taking finite intersections as well). -/ def is_topological_basis {α : Type u} [t : topological_space α] (s : set (set α)) := (∀ (t₁ : set α) (H : t₁ ∈ s) (t₂ : set α) (H : t₂ ∈ s) (x : α) (H : x ∈ t₁ ∩ t₂), ∃ (t₃ : set α), ∃ (H : t₃ ∈ s), x ∈ t₃ ∧ t₃ ⊆ t₁ ∩ t₂) ∧ ⋃₀s = set.univ ∧ t = generate_from s theorem is_topological_basis_of_subbasis {α : Type u} [t : topological_space α] {s : set (set α)} (hs : t = generate_from s) : is_topological_basis ((fun (f : set (set α)) => ⋂₀f) '' set_of fun (f : set (set α)) => set.finite f ∧ f ⊆ s ∧ set.nonempty (⋂₀f)) := sorry theorem is_topological_basis_of_open_of_nhds {α : Type u} [t : topological_space α] {s : set (set α)} (h_open : ∀ (u : set α), u ∈ s → is_open u) (h_nhds : ∀ (a : α) (u : set α), a ∈ u → is_open u → ∃ (v : set α), ∃ (H : v ∈ s), a ∈ v ∧ v ⊆ u) : is_topological_basis s := sorry theorem mem_nhds_of_is_topological_basis {α : Type u} [t : topological_space α] {a : α} {s : set α} {b : set (set α)} (hb : is_topological_basis b) : s ∈ nhds a ↔ ∃ (t : set α), ∃ (H : t ∈ b), a ∈ t ∧ t ⊆ s := sorry theorem is_topological_basis.nhds_has_basis {α : Type u} [t : topological_space α] {b : set (set α)} (hb : is_topological_basis b) {a : α} : filter.has_basis (nhds a) (fun (t : set α) => t ∈ b ∧ a ∈ t) fun (t : set α) => t := sorry theorem is_open_of_is_topological_basis {α : Type u} [t : topological_space α] {s : set α} {b : set (set α)} (hb : is_topological_basis b) (hs : s ∈ b) : is_open s := sorry theorem mem_basis_subset_of_mem_open {α : Type u} [t : topological_space α] {b : set (set α)} (hb : is_topological_basis b) {a : α} {u : set α} (au : a ∈ u) (ou : is_open u) : ∃ (v : set α), ∃ (H : v ∈ b), a ∈ v ∧ v ⊆ u := iff.mp (mem_nhds_of_is_topological_basis hb) (mem_nhds_sets ou au) theorem sUnion_basis_of_is_open {α : Type u} [t : topological_space α] {B : set (set α)} (hB : is_topological_basis B) {u : set α} (ou : is_open u) : ∃ (S : set (set α)), ∃ (H : S ⊆ B), u = ⋃₀S := sorry theorem Union_basis_of_is_open {α : Type u} [t : topological_space α] {B : set (set α)} (hB : is_topological_basis B) {u : set α} (ou : is_open u) : ∃ (β : Type u), ∃ (f : β → set α), (u = set.Union fun (i : β) => f i) ∧ ∀ (i : β), f i ∈ B := sorry /-- A separable space is one with a countable dense subset, available through `topological_space.exists_countable_dense`. If `α` is also known to be nonempty, then `topological_space.dense_seq` provides a sequence `ℕ → α` with dense range, see `topological_space.dense_range_dense_seq`. If `α` is a uniform space with countably generated uniformity filter (e.g., an `emetric_space`), then this condition is equivalent to `topological_space.second_countable_topology α`. In this case the latter should be used as a typeclass argument in theorems because Lean can automatically deduce `separable_space` from `second_countable_topology` but it can't deduce `second_countable_topology` and `emetric_space`. -/ class separable_space (α : Type u) [t : topological_space α] where exists_countable_dense : ∃ (s : set α), set.countable s ∧ dense s theorem exists_countable_dense (α : Type u) [t : topological_space α] [separable_space α] : ∃ (s : set α), set.countable s ∧ dense s := separable_space.exists_countable_dense /-- A nonempty separable space admits a sequence with dense range. Instead of running `cases` on the conclusion of this lemma, you might want to use `topological_space.dense_seq` and `topological_space.dense_range_dense_seq`. If `α` might be empty, then `exists_countable_dense` is the main way to use separability of `α`. -/ theorem exists_dense_seq (α : Type u) [t : topological_space α] [separable_space α] [Nonempty α] : ∃ (u : ℕ → α), dense_range u := sorry /-- A sequence dense in a non-empty separable topological space. If `α` might be empty, then `exists_countable_dense` is the main way to use separability of `α`. -/ def dense_seq (α : Type u) [t : topological_space α] [separable_space α] [Nonempty α] : ℕ → α := classical.some (exists_dense_seq α) /-- The sequence `dense_seq α` has dense range. -/ @[simp] theorem dense_range_dense_seq (α : Type u) [t : topological_space α] [separable_space α] [Nonempty α] : dense_range (dense_seq α) := classical.some_spec (exists_dense_seq α) end topological_space /-- If `α` is a separable space and `f : α → β` is a continuous map with dense range, then `β` is a separable space as well. E.g., the completion of a separable uniform space is separable. -/ protected theorem dense_range.separable_space {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space.separable_space α] [topological_space β] {f : α → β} (h : dense_range f) (h' : continuous f) : topological_space.separable_space β := sorry namespace topological_space /-- A first-countable space is one in which every point has a countable neighborhood basis. -/ class first_countable_topology (α : Type u) [t : topological_space α] where nhds_generated_countable : ∀ (a : α), filter.is_countably_generated (nhds a) namespace first_countable_topology theorem tendsto_subseq {α : Type u} [t : topological_space α] [first_countable_topology α] {u : ℕ → α} {x : α} (hx : map_cluster_pt x filter.at_top u) : ∃ (ψ : ℕ → ℕ), strict_mono ψ ∧ filter.tendsto (u ∘ ψ) filter.at_top (nhds x) := filter.is_countably_generated.subseq_tendsto (nhds_generated_countable x) hx end first_countable_topology theorem is_countably_generated_nhds {α : Type u} [t : topological_space α] [first_countable_topology α] (x : α) : filter.is_countably_generated (nhds x) := first_countable_topology.nhds_generated_countable x theorem is_countably_generated_nhds_within {α : Type u} [t : topological_space α] [first_countable_topology α] (x : α) (s : set α) : filter.is_countably_generated (nhds_within x s) := filter.is_countably_generated.inf_principal (is_countably_generated_nhds x) s /-- A second-countable space is one with a countable basis. -/ class second_countable_topology (α : Type u) [t : topological_space α] where is_open_generated_countable : ∃ (b : set (set α)), set.countable b ∧ t = generate_from b protected instance second_countable_topology.to_first_countable_topology (α : Type u) [t : topological_space α] [second_countable_topology α] : first_countable_topology α := sorry theorem second_countable_topology_induced (α : Type u) (β : Type u_1) [t : topological_space β] [second_countable_topology β] (f : α → β) : second_countable_topology α := sorry protected instance subtype.second_countable_topology (α : Type u) [t : topological_space α] (s : set α) [second_countable_topology α] : second_countable_topology ↥s := second_countable_topology_induced (↥s) α coe theorem is_open_generated_countable_inter (α : Type u) [t : topological_space α] [second_countable_topology α] : ∃ (b : set (set α)), set.countable b ∧ ¬∅ ∈ b ∧ is_topological_basis b := sorry /- TODO: more fine grained instances for first_countable_topology, separable_space, t2_space, ... -/ protected instance prod.second_countable_topology (α : Type u) [t : topological_space α] {β : Type u_1} [topological_space β] [second_countable_topology α] [second_countable_topology β] : second_countable_topology (α × β) := second_countable_topology.mk sorry protected instance second_countable_topology_fintype {ι : Type u_1} {π : ι → Type u_2} [fintype ι] [t : (a : ι) → topological_space (π a)] [sc : ∀ (a : ι), second_countable_topology (π a)] : second_countable_topology ((a : ι) → π a) := (fun (this : ∀ (i : ι), ∃ (b : set (set (π i))), set.countable b ∧ ¬∅ ∈ b ∧ is_topological_basis b) => sorry) fun (a : ι) => is_open_generated_countable_inter (π a) protected instance second_countable_topology.to_separable_space (α : Type u) [t : topological_space α] [second_countable_topology α] : separable_space α := sorry theorem is_open_Union_countable {α : Type u} [t : topological_space α] [second_countable_topology α] {ι : Type u_1} (s : ι → set α) (H : ∀ (i : ι), is_open (s i)) : ∃ (T : set ι), set.countable T ∧ (set.Union fun (i : ι) => set.Union fun (H : i ∈ T) => s i) = set.Union fun (i : ι) => s i := sorry theorem is_open_sUnion_countable {α : Type u} [t : topological_space α] [second_countable_topology α] (S : set (set α)) (H : ∀ (s : set α), s ∈ S → is_open s) : ∃ (T : set (set α)), set.countable T ∧ T ⊆ S ∧ ⋃₀T = ⋃₀S := sorry /-- In a topological space with second countable topology, if `f` is a function that sends each point `x` to a neighborhood of `x`, then for some countable set `s`, the neighborhoods `f x`, `x ∈ s`, cover the whole space. -/ theorem countable_cover_nhds {α : Type u} [t : topological_space α] [second_countable_topology α] {f : α → set α} (hf : ∀ (x : α), f x ∈ nhds x) : ∃ (s : set α), set.countable s ∧ (set.Union fun (x : α) => set.Union fun (H : x ∈ s) => f x) = set.univ := sorry
6b5c68e0c77b08dc307dca3260df3476df5a61a9
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/tactic/linarith/elimination.lean
300e836d268f43cf59fc569093400ed66546921f
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
13,999
lean
/- Copyright (c) 2020 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis -/ import tactic.linarith.datatypes /-! # The Fourier-Motzkin elimination procedure The Fourier-Motzkin procedure is a variable elimination method for linear inequalities. <https://en.wikipedia.org/wiki/Fourier%E2%80%93Motzkin_elimination> Given a set of linear inequalities `comps = {tᵢ Rᵢ 0}`, we aim to eliminate a single variable `a` from the set. We partition `comps` into `comps_pos`, `comps_neg`, and `comps_zero`, where `comps_pos` contains the comparisons `tᵢ Rᵢ 0` in which the coefficient of `a` in `tᵢ` is positive, and similar. For each pair of comparisons `tᵢ Rᵢ 0 ∈ comps_pos`, `tⱼ Rⱼ 0 ∈ comps_neg`, we compute coefficients `vᵢ, vⱼ ∈ ℕ` such that `vᵢ*tᵢ + vⱼ*tⱼ` cancels out `a`. We collect these sums `vᵢ*tᵢ + vⱼ*tⱼ R' 0` in a set `S` and set `comps' = S ∪ comps_zero`, a new set of comparisons in which `a` has been eliminated. Theorem: `comps` and `comps'` are equisatisfiable. We recursively eliminate all variables from the system. If we derive an empty clause `0 < 0`, we conclude that the original system was unsatisfiable. -/ open native namespace linarith /-! ### Datatypes The `comp_source` and `pcomp` datatypes are specific to the FM elimination routine; they are not shared with other components of `linarith`. -/ /-- `comp_source` tracks the source of a comparison. The atomic source of a comparison is an assumption, indexed by a natural number. Two comparisons can be added to produce a new comparison, and one comparison can be scaled by a natural number to produce a new comparison. -/ @[derive inhabited] inductive comp_source : Type | assump : ℕ → comp_source | add : comp_source → comp_source → comp_source | scale : ℕ → comp_source → comp_source /-- Given a `comp_source` `cs`, `cs.flatten` maps an assumption index to the number of copies of that assumption that appear in the history of `cs`. For example, suppose `cs` is produced by scaling assumption 2 by 5, and adding to that the sum of assumptions 1 and 2. `cs.flatten` maps `1 ↦ 1, 2 ↦ 6`. -/ meta def comp_source.flatten : comp_source → rb_map ℕ ℕ | (comp_source.assump n) := mk_rb_map.insert n 1 | (comp_source.add c1 c2) := (comp_source.flatten c1).add (comp_source.flatten c2) | (comp_source.scale n c) := (comp_source.flatten c).map (λ v, v * n) /-- Formats a `comp_source` for printing. -/ def comp_source.to_string : comp_source → string | (comp_source.assump e) := to_string e | (comp_source.add c1 c2) := comp_source.to_string c1 ++ " + " ++ comp_source.to_string c2 | (comp_source.scale n c) := to_string n ++ " * " ++ comp_source.to_string c meta instance comp_source.has_to_format : has_to_format comp_source := ⟨λ a, comp_source.to_string a⟩ /-- A `pcomp` stores a linear comparison `Σ cᵢ*xᵢ R 0`, along with information about how this comparison was derived. The original expressions fed into `linarith` are each assigned a unique natural number label. The *historical set* `pcomp.history` stores the labels of expressions that were used in deriving the current `pcomp`. Variables are also indexed by natural numbers. The sets `pcomp.effective`, `pcomp.implicit`, and `pcomp.vars` contain variable indices. * `pcomp.vars` contains the variables that appear in `pcomp.c`. We store them in `pcomp` to avoid recomputing the set, which requires folding over a list. (TODO: is this really needed?) * `pcomp.effective` contains the variables that have been effectively eliminated from `pcomp`. A variable `n` is said to be *effectively eliminated* in `pcomp` if the elimination of `n` produced at least one of the ancestors of `pcomp`. * `pcomp.implicit` contains the variables that have been implicitly eliminated from `pcomp`. A variable `n` is said to be *implicitly eliminated* in `pcomp` if it satisfies the following properties: - There is some `ancestor` of `pcomp` such that `n` appears in `ancestor.vars`. - `n` does not appear in `pcomp.vars`. - `n` was not effectively eliminated. We track these sets in order to compute whether the history of a `pcomp` is *minimal*. Checking this directly is expensive, but effective approximations can be defined in terms of these sets. During the variable elimination process, a `pcomp` with non-minimal history can be discarded. -/ meta structure pcomp : Type := (c : comp) (src : comp_source) (history : rb_set ℕ) (effective : rb_set ℕ) (implicit : rb_set ℕ) (vars : rb_set ℕ) /-- Any comparison whose history is not minimal is redundant, and need not be included in the new set of comparisons. `elimed_ge : ℕ` is a natural number such that all variables with index ≥ `elimed_ge` have been removed from the system. This test is an overapproximation to minimality. It gives necessary but not sufficient conditions. If the history of `c` is minimal, then `c.maybe_minimal` is true, but `c.maybe_minimal` may also be true for some `c` with minimal history. Thus, if `c.maybe_minimal` is false, `c` is known not to be minimal and must be redundant. See http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.51.493&rep=rep1&type=pdf p.13 (Theorem 7). The condition described there considers only implicitly eliminated variables that have been officially eliminated from the system. This is not the case for every implicitly eliminated variable. Consider eliminating `z` from `{x + y + z < 0, x - y - z < 0}`. The result is the set `{2*x < 0}`; `y` is implicitly but not officially eliminated. This implementation of Fourier-Motzkin elimination processes variables in decreasing order of indices. Immediately after a step that eliminates variable `k`, variable `k'` has been eliminated iff `k' ≥ k`. Thus we can compute the intersection of officially and implicitly eliminated variables by taking the set of implicitly eliminated variables with indices ≥ `elimed_ge`. -/ meta def pcomp.maybe_minimal (c : pcomp) (elimed_ge : ℕ) : bool := c.history.size ≤ 1 + ((c.implicit.filter (≥ elimed_ge)).union c.effective).size /-- The `comp_source` field is ignored when comparing `pcomp`s. Two `pcomp`s proving the same comparison, with different sources, are considered equivalent. -/ meta def pcomp.cmp (p1 p2 : pcomp) : ordering := p1.c.cmp p2.c /-- `pcomp.scale c n` scales the coefficients of `c` by `n` and notes this in the `comp_source`. -/ meta def pcomp.scale (c : pcomp) (n : ℕ) : pcomp := {c with c := c.c.scale n, src := c.src.scale n} /-- `pcomp.add c1 c2 elim_var` creates the result of summing the linear comparisons `c1` and `c2`, during the process of eliminating the variable `elim_var`. The computation assumes, but does not enforce, that `elim_var` appears in both `c1` and `c2` and does not appear in the sum. Computing the sum of the two comparisons is easy; the complicated details lie in tracking the additional fields of `pcomp`. * The historical set `pcomp.history` of `c1 + c2` is the union of the two historical sets. * We recompute the variables that appear in `c1 + c2` from the newly created `linexp`, since some may have been implicitly eliminated. * The effectively eliminated variables of `c1 + c2` are the union of the two effective sets, with `elim_var` inserted. * The implicitly eliminated variables of `c1 + c2` are those that appear in at least one of `c1.vars` and `c2.vars` but not in `(c1 + c2).vars`, excluding `elim_var`. -/ meta def pcomp.add (c1 c2 : pcomp) (elim_var : ℕ) : pcomp := let c := c1.c.add c2.c, src := c1.src.add c2.src, history := c1.history.union c2.history, vars := native.rb_set.of_list c.vars, effective := (c1.effective.union c2.effective).insert elim_var, implicit := ((c1.vars.union c2.vars).sdiff vars).erase elim_var in ⟨c, src, history, effective, implicit, vars⟩ /-- `pcomp.assump c n` creates a `pcomp` whose comparison is `c` and whose source is `comp_source.assump n`, that is, `c` is derived from the `n`th hypothesis. The history is the singleton set `{n}`. No variables have been eliminated (effectively or implicitly). -/ meta def pcomp.assump (c : comp) (n : ℕ) : pcomp := { c := c, src := comp_source.assump n, history := mk_rb_set.insert n, effective := mk_rb_set, implicit := mk_rb_set, vars := rb_set.of_list c.vars } meta instance pcomp.to_format : has_to_format pcomp := ⟨λ p, to_fmt p.c.coeffs ++ to_string p.c.str ++ "0"⟩ /-- Creates an empty set of `pcomp`s, sorted using `pcomp.cmp`. This should always be used instead of `mk_rb_map` for performance reasons. -/ meta def mk_pcomp_set : rb_set pcomp := rb_map.mk_core unit pcomp.cmp /-! ### Elimination procedure -/ /-- If `c1` and `c2` both contain variable `a` with opposite coefficients, produces `v1` and `v2` such that `a` has been cancelled in `v1*c1 + v2*c2`. -/ meta def elim_var (c1 c2 : comp) (a : ℕ) : option (ℕ × ℕ) := let v1 := c1.coeff_of a, v2 := c2.coeff_of a in if v1 * v2 < 0 then let vlcm := nat.lcm v1.nat_abs v2.nat_abs, v1' := vlcm / v1.nat_abs, v2' := vlcm / v2.nat_abs in some ⟨v1', v2'⟩ else none /-- `pelim_var p1 p2` calls `elim_var` on the `comp` components of `p1` and `p2`. If this returns `v1` and `v2`, it creates a new `pcomp` equal to `v1*p1 + v2*p2`, and tracks this in the `comp_source`. -/ meta def pelim_var (p1 p2 : pcomp) (a : ℕ) : option pcomp := do (n1, n2) ← elim_var p1.c p2.c a, return $ (p1.scale n1).add (p2.scale n2) a /-- A `pcomp` represents a contradiction if its `comp` field represents a contradiction. -/ meta def pcomp.is_contr (p : pcomp) : bool := p.c.is_contr /-- `elim_var_with_set a p comps` collects the result of calling `pelim_var p p' a` for every `p' ∈ comps`. -/ meta def elim_with_set (a : ℕ) (p : pcomp) (comps : rb_set pcomp) : rb_set pcomp := comps.fold mk_pcomp_set $ λ pc s, match pelim_var p pc a with | some pc := if pc.maybe_minimal a then s.insert pc else s | none := s end /-- The state for the elimination monad. * `max_var`: the largest variable index that has not been eliminated. * `comps`: a set of comparisons The elimination procedure proceeds by eliminating variable `v` from `comps` progressively in decreasing order. -/ meta structure linarith_structure : Type := (max_var : ℕ) (comps : rb_set pcomp) /-- The linarith monad extends an exceptional monad with a `linarith_structure` state. An exception produces a contradictory `pcomp`. -/ @[reducible, derive [monad, monad_except pcomp]] meta def linarith_monad : Type → Type := state_t linarith_structure (except_t pcomp id) /-- Returns the current max variable. -/ meta def get_max_var : linarith_monad ℕ := linarith_structure.max_var <$> get /-- Return the current comparison set. -/ meta def get_comps : linarith_monad (rb_set pcomp) := linarith_structure.comps <$> get /-- Throws an exception if a contradictory `pcomp` is contained in the current state. -/ meta def validate : linarith_monad unit := do ⟨_, comps⟩ ← get, match comps.to_list.find (λ p : pcomp, p.is_contr) with | none := return () | some c := throw c end /-- Updates the current state with a new max variable and comparisons, and calls `validate` to check for a contradiction. -/ meta def update (max_var : ℕ) (comps : rb_set pcomp) : linarith_monad unit := state_t.put ⟨max_var, comps⟩ >> validate /-- `split_set_by_var_sign a comps` partitions the set `comps` into three parts. * `pos` contains the elements of `comps` in which `a` has a positive coefficient. * `neg` contains the elements of `comps` in which `a` has a negative coefficient. * `not_present` contains the elements of `comps` in which `a` has coefficient 0. Returns `(pos, neg, not_present)`. -/ meta def split_set_by_var_sign (a : ℕ) (comps : rb_set pcomp) : rb_set pcomp × rb_set pcomp × rb_set pcomp := comps.fold ⟨mk_pcomp_set, mk_pcomp_set, mk_pcomp_set⟩ $ λ pc ⟨pos, neg, not_present⟩, let n := pc.c.coeff_of a in if n > 0 then ⟨pos.insert pc, neg, not_present⟩ else if n < 0 then ⟨pos, neg.insert pc, not_present⟩ else ⟨pos, neg, not_present.insert pc⟩ /-- `monad.elim_var a` performs one round of Fourier-Motzkin elimination, eliminating the variable `a` from the `linarith` state. -/ meta def monad.elim_var (a : ℕ) : linarith_monad unit := do vs ← get_max_var, when (a ≤ vs) $ do ⟨pos, neg, not_present⟩ ← split_set_by_var_sign a <$> get_comps, let cs' := pos.fold not_present (λ p s, s.union (elim_with_set a p neg)), update (vs - 1) cs' /-- `elim_all_vars` eliminates all variables from the linarith state, leaving it with a set of ground comparisons. If this succeeds without exception, the original `linarith` state is consistent. -/ meta def elim_all_vars : linarith_monad unit := do mv ← get_max_var, (list.range $ mv + 1).reverse.mmap' monad.elim_var /-- `mk_linarith_structure hyps vars` takes a list of hypotheses and the largest variable present in those hypotheses. It produces an initial state for the elimination monad. -/ meta def mk_linarith_structure (hyps : list comp) (max_var : ℕ) : linarith_structure := let pcomp_list : list pcomp := hyps.enum.map $ λ ⟨n, cmp⟩, pcomp.assump cmp n, pcomp_set := rb_set.of_list_core mk_pcomp_set pcomp_list in ⟨max_var, pcomp_set⟩ /-- `produce_certificate hyps vars` tries to derive a contradiction from the comparisons in `hyps` by eliminating all variables ≤ `max_var`. If successful, it returns a map `coeff : ℕ → ℕ` as a certificate. This map represents that we can find a contradiction by taking the sum `∑ (coeff i) * hyps[i]`. -/ meta def fourier_motzkin.produce_certificate : certificate_oracle := λ hyps max_var, let state := mk_linarith_structure hyps max_var in match except_t.run (state_t.run (validate >> elim_all_vars) state) with | (except.ok (a, _)) := tactic.failed | (except.error contr) := return contr.src.flatten end end linarith
08d48cb3da51b678b9bda91654ed076936b6051c
274261f7150b4ed5f1962f172c9357591be8a2b5
/src/iso_type.lean
0a1eae142bf66077b09919fb9689f70249a86918
[]
no_license
rspencer01/lean_representation_theory
219ea1edf4b9897b2997226b54473e44e1538b50
2eef2b4b39d99d7ce71bec7bbc3dcc2f7586fcb5
refs/heads/master
1,588,133,157,029
1,571,689,957,000
1,571,689,957,000
175,835,785
0
0
null
null
null
null
UTF-8
Lean
false
false
3,216
lean
import .Module import .lattices import logic.function import .module_trivialities import ring_theory.algebra variables {R : Type} [ring R] (M : Module R) (S₁ S₂ : submodule R M) @[simp] def is_simple := (¬ is_trivial M) ∧ (∀ (N : submodule R M), N = ⊤ ∨ N = ⊥) lemma intersection_of_simples_is_left_or_trivial : (is_simple (S₁ : Module R)) → (is_simple (S₂ : Module R)) → (is_trivial ((S₁ ⊓ S₂ : submodule R M) : Module R)) ∨ ((S₁ ⊓ S₂ : submodule R M) : Module R) = S₁ := begin intros, set S₃ : submodule R M := S₁ ⊓ S₂, set f := (submodule.map_subtype.order_iso S₁).symm, set f' := (submodule.map_subtype.order_iso S₁), have h : S₃ ≤ S₁ := by simp, set S₃' := f ⟨ S₃ , h ⟩ , cases a.elim_right S₃', have k : (⟨ S₃ , h ⟩ : {a // a ≤ S₁ }) = (⊤ : {a // a ≤ S₁}) := ( calc (⟨ S₃ , h ⟩ : {a // a ≤ S₁ }) = f' (f ⟨S₃, h⟩ ) : by rw order_iso.apply_symm_apply ... = f' S₃' : rfl ... = f' ⊤ : (by rw h_1; refl) ... = ⊤ : order_isom_preserves_top f'), have l : S₃ = S₁ := calc S₃ = (⟨ S₃, h⟩ : {a // a ≤ S₁}).val : by simp ... = (⊤ : {a // a ≤ S₁}).val : by rw k ... = S₁ : by refl, rw l, apply or.inr, refl, have i : (⟨ S₃ , h ⟩ : {a // a ≤ S₁ }) = (⊥ : {a // a ≤ S₁}) := ( calc (⟨ S₃ , h ⟩ : {a // a ≤ S₁ }) = f' (f ⟨S₃, h⟩ ) : by rw order_iso.apply_symm_apply ... = f' S₃' : rfl ... = f' ⊥ : (by rw h_1; refl) ... = ⊥ : order_isom_preserves_bot f'), have j : S₃ = ⊥ := calc S₃ = (⟨ S₃, h⟩ : {a // a ≤ S₁}).val : by simp ... = (⊥ : {a // a ≤ S₁}).val : by rw i ... = ⊥ : by refl, rw j, exact or.inl ⟨ bot_is_trivial _ ⟩ , end lemma intersection_of_simples_is_right_or_trivial (M : Module R) (S₁ S₂ : submodule R M) : (is_simple (S₁ : Module R)) → (is_simple (S₂ : Module R)) → (is_trivial ((S₁ ⊓ S₂ : submodule R M) : Module R)) ∨ ((S₁ ⊓ S₂ : submodule R M) : Module R) = S₂ := begin intros, rw lattice.inf_comm, exact intersection_of_simples_is_left_or_trivial M S₂ S₁ a_1 a end lemma intersection_of_simples_is_simple_or_trivial (M : Module R) (S₁ S₂ : submodule R M) : (is_simple (S₁ : Module R)) → (is_simple (S₂ : Module R)) → (is_trivial ((S₁ ⊓ S₂ : submodule R M) : Module R)) ∨ is_simple ((S₁ ⊓ S₂ : submodule R M) : Module R) := begin intros, cases intersection_of_simples_is_left_or_trivial M S₁ S₂ a a_1, exact or.inl h, rw h, exact or.inr a end theorem isom_is_equiv : equivalence (@are_isomorphic R _) := ⟨ λ x, ⟨ category_theory.iso.refl x ⟩, λ x y h, h.elim (λ f, ⟨ category_theory.iso.symm f ⟩), λ x y z h₁ h₂, h₁.elim (λ f, h₂.elim (λ g, ⟨category_theory.iso.trans f g⟩)) ⟩ def isomorphism_class_of_modules := quotient ⟨(@are_isomorphic R _), isom_is_equiv ⟩
cdc6619ddc2ed39a27f950894784485b239e52d7
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/mv_polynomial/supported.lean
6bdbf3475ffce3b423eefdbf28700f1892b736c7
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,778
lean
/- Copyright (c) 2021 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import ring_theory.adjoin.polynomial import data.mv_polynomial.variables /-! # Polynomials supported by a set of variables This file contains the definition and lemmas about `mv_polynomial.supported`. ## Main definitions * `mv_polynomial.supported` : Given a set `s : set σ`, `supported R s` is the subalgebra of `mv_polynomial σ R` consisting of polynomials whose set of variables is contained in `s`. This subalgebra is isomorphic to `mv_polynomial s R` ## Tags variables, polynomial, vars -/ universes u v w namespace mv_polynomial variables {σ τ : Type*} {R : Type u} {S : Type v} {r : R} {e : ℕ} {n m : σ} section comm_semiring variables [comm_semiring R] {p q : mv_polynomial σ R} variables (R) /-- The set of polynomials whose variables are contained in `s` as a `subalgebra` over `R`. -/ noncomputable def supported (s : set σ) : subalgebra R (mv_polynomial σ R) := algebra.adjoin R (X '' s) variables {σ R} open_locale classical open algebra lemma supported_eq_range_rename (s : set σ) : supported R s = (rename (coe : s → σ)).range := by rw [supported, set.image_eq_range, adjoin_range_eq_range_aeval, rename] /--The isomorphism between the subalgebra of polynomials supported by `s` and `mv_polynomial s R`-/ noncomputable def supported_equiv_mv_polynomial (s : set σ) : supported R s ≃ₐ[R] mv_polynomial s R := (subalgebra.equiv_of_eq _ _ (supported_eq_range_rename s)).trans (alg_equiv.of_injective (rename (coe : s → σ)) (rename_injective _ subtype.val_injective)).symm @[simp] lemma supported_equiv_mv_polynomial_symm_C (s : set σ) (x : R) : (supported_equiv_mv_polynomial s).symm (C x) = algebra_map R (supported R s) x := begin ext1, simp [supported_equiv_mv_polynomial, mv_polynomial.algebra_map_eq], end @[simp] lemma supported_equiv_mv_polynomial_symm_X (s : set σ) (i : s) : (↑((supported_equiv_mv_polynomial s).symm (X i : mv_polynomial s R)) : mv_polynomial σ R) = X i := by simp [supported_equiv_mv_polynomial] variables {s t : set σ} lemma mem_supported : p ∈ (supported R s) ↔ ↑p.vars ⊆ s := begin rw [supported_eq_range_rename, alg_hom.mem_range], split, { rintros ⟨p, rfl⟩, refine trans (finset.coe_subset.2 (vars_rename _ _)) _, simp }, { intros hs, exact exists_rename_eq_of_vars_subset_range p (coe : s → σ) subtype.val_injective (by simpa) } end lemma supported_eq_vars_subset : (supported R s : set (mv_polynomial σ R)) = {p | ↑p.vars ⊆ s} := set.ext $ λ _, mem_supported @[simp] lemma mem_supported_vars (p : mv_polynomial σ R) : p ∈ supported R (↑p.vars : set σ) := by rw [mem_supported] variable (s) lemma supported_eq_adjoin_X : supported R s = algebra.adjoin R (X '' s) := rfl @[simp] lemma supported_univ : supported R (set.univ : set σ) = ⊤ := by simp [algebra.eq_top_iff, mem_supported] @[simp] lemma supported_empty : supported R (∅ : set σ) = ⊥ := by simp [supported_eq_adjoin_X] variables {s} lemma supported_mono (st : s ⊆ t) : supported R s ≤ supported R t := algebra.adjoin_mono (set.image_subset _ st) @[simp] lemma X_mem_supported [nontrivial R] {i : σ} : (X i) ∈ supported R s ↔ i ∈ s := by simp [mem_supported] @[simp] lemma supported_le_supported_iff [nontrivial R] : supported R s ≤ supported R t ↔ s ⊆ t := begin split, { intros h i, simpa using @h (X i) }, { exact supported_mono } end lemma supported_strict_mono [nontrivial R] : strict_mono (supported R : set σ → subalgebra R (mv_polynomial σ R)) := strict_mono_of_le_iff_le (λ _ _, supported_le_supported_iff.symm) end comm_semiring end mv_polynomial
4935a2cb51423e2b68d7b5539fa985f0737e823a
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/analysis/calculus/times_cont_diff.lean
2677073d9472f2ef03c9db3087775f4221cb7cfe
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
142,906
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.calculus.mean_value import analysis.normed_space.multilinear import analysis.calculus.formal_multilinear_series /-! # Higher differentiability A function is `C^1` on a domain if it is differentiable there, and its derivative is continuous. By induction, it is `C^n` if it is `C^{n-1}` and its (n-1)-th derivative is `C^1` there or, equivalently, if it is `C^1` and its derivative is `C^{n-1}`. Finally, it is `C^∞` if it is `C^n` for all n. We formalize these notions by defining iteratively the `n+1`-th derivative of a function as the derivative of the `n`-th derivative. It is called `iterated_fderiv 𝕜 n f x` where `𝕜` is the field, `n` is the number of iterations, `f` is the function and `x` is the point, and it is given as an `n`-multilinear map. We also define a version `iterated_fderiv_within` relative to a domain, as well as predicates `times_cont_diff_within_at`, `times_cont_diff_at`, `times_cont_diff_on` and `times_cont_diff` saying that the function is `C^n` within a set at a point, at a point, on a set and on the whole space respectively. To avoid the issue of choice when choosing a derivative in sets where the derivative is not necessarily unique, `times_cont_diff_on` is not defined directly in terms of the regularity of the specific choice `iterated_fderiv_within 𝕜 n f s` inside `s`, but in terms of the existence of a nice sequence of derivatives, expressed with a predicate `has_ftaylor_series_up_to_on`. We prove basic properties of these notions. ## Main definitions and results Let `f : E → F` be a map between normed vector spaces over a nondiscrete normed field `𝕜`. * `has_ftaylor_series_up_to n f p`: expresses that the formal multilinear series `p` is a sequence of iterated derivatives of `f`, up to the `n`-th term (where `n` is a natural number or `∞`). * `has_ftaylor_series_up_to_on n f p s`: same thing, but inside a set `s`. The notion of derivative is now taken inside `s`. In particular, derivatives don't have to be unique. * `times_cont_diff 𝕜 n f`: expresses that `f` is `C^n`, i.e., it admits a Taylor series up to rank `n`. * `times_cont_diff_on 𝕜 n f s`: expresses that `f` is `C^n` in `s`. * `times_cont_diff_at 𝕜 n f x`: expresses that `f` is `C^n` around `x`. * `times_cont_diff_within_at 𝕜 n f s x`: expresses that `f` is `C^n` around `x` within the set `s`. * `iterated_fderiv_within 𝕜 n f s x` is an `n`-th derivative of `f` over the field `𝕜` on the set `s` at the point `x`. It is a continuous multilinear map from `E^n` to `F`, defined as a derivative within `s` of `iterated_fderiv_within 𝕜 (n-1) f s` if one exists, and `0` otherwise. * `iterated_fderiv 𝕜 n f x` is the `n`-th derivative of `f` over the field `𝕜` at the point `x`. It is a continuous multilinear map from `E^n` to `F`, defined as a derivative of `iterated_fderiv 𝕜 (n-1) f` if one exists, and `0` otherwise. In sets of unique differentiability, `times_cont_diff_on 𝕜 n f s` can be expressed in terms of the properties of `iterated_fderiv_within 𝕜 m f s` for `m ≤ n`. In the whole space, `times_cont_diff 𝕜 n f` can be expressed in terms of the properties of `iterated_fderiv 𝕜 m f` for `m ≤ n`. We also prove that the usual operations (addition, multiplication, difference, composition, and so on) preserve `C^n` functions. ## Implementation notes The definitions in this file are designed to work on any field `𝕜`. They are sometimes slightly more complicated than the naive definitions one would guess from the intuition over the real or complex numbers, but they are designed to circumvent the lack of gluing properties and partitions of unity in general. In the usual situations, they coincide with the usual definitions. ### Definition of `C^n` functions in domains One could define `C^n` functions in a domain `s` by fixing an arbitrary choice of derivatives (this is what we do with `iterated_fderiv_within`) and requiring that all these derivatives up to `n` are continuous. If the derivative is not unique, this could lead to strange behavior like two `C^n` functions `f` and `g` on `s` whose sum is not `C^n`. A better definition is thus to say that a function is `C^n` inside `s` if it admits a sequence of derivatives up to `n` inside `s`. This definition still has the problem that a function which is locally `C^n` would not need to be `C^n`, as different choices of sequences of derivatives around different points might possibly not be glued together to give a globally defined sequence of derivatives. (Note that this issue can not happen over reals, thanks to partition of unity, but the behavior over a general field is not so clear, and we want a definition for general fields). Also, there are locality problems for the order parameter: one could image a function which, for each `n`, has a nice sequence of derivatives up to order `n`, but they do not coincide for varying `n` and can therefore not be glued to give rise to an infinite sequence of derivatives. This would give a function which is `C^n` for all `n`, but not `C^∞`. We solve this issue by putting locality conditions in space and order in our definition of `times_cont_diff_within_at` and `times_cont_diff_on`. The resulting definition is slightly more complicated to work with (in fact not so much), but it gives rise to completely satisfactory theorems. For instance, with this definition, a real function which is `C^m` (but not better) on `(-1/m, 1/m)` for each natural `m` is by definition `C^∞` at `0`. There is another issue with the definition of `times_cont_diff_within_at 𝕜 n f s x`. We can require the existence and good behavior of derivatives up to order `n` on a neighborhood of `x` within `s`. However, this does not imply continuity or differentiability within `s` of the function at `x` when `x` does not belong to `s`. Therefore, we require such existence and good behavior on a neighborhood of `x` within `s ∪ {x}` (which appears as `insert x s` in this file). ### Side of the composition, and universe issues With a naïve direct definition, the `n`-th derivative of a function belongs to the space `E →L[𝕜] (E →L[𝕜] (E ... F)...)))` where there are n iterations of `E →L[𝕜]`. This space may also be seen as the space of continuous multilinear functions on `n` copies of `E` with values in `F`, by uncurrying. This is the point of view that is usually adopted in textbooks, and that we also use. This means that the definition and the first proofs are slightly involved, as one has to keep track of the uncurrying operation. The uncurrying can be done from the left or from the right, amounting to defining the `n+1`-th derivative either as the derivative of the `n`-th derivative, or as the `n`-th derivative of the derivative. For proofs, it would be more convenient to use the latter approach (from the right), as it means to prove things at the `n+1`-th step we only need to understand well enough the derivative in `E →L[𝕜] F` (contrary to the approach from the left, where one would need to know enough on the `n`-th derivative to deduce things on the `n+1`-th derivative). However, the definition from the right leads to a universe polymorphism problem: if we define `iterated_fderiv 𝕜 (n + 1) f x = iterated_fderiv 𝕜 n (fderiv 𝕜 f) x` by induction, we need to generalize over all spaces (as `f` and `fderiv 𝕜 f` don't take values in the same space). It is only possible to generalize over all spaces in some fixed universe in an inductive definition. For `f : E → F`, then `fderiv 𝕜 f` is a map `E → (E →L[𝕜] F)`. Therefore, the definition will only work if `F` and `E →L[𝕜] F` are in the same universe. This issue does not appear with the definition from the left, where one does not need to generalize over all spaces. Therefore, we use the definition from the left. This means some proofs later on become a little bit more complicated: to prove that a function is `C^n`, the most efficient approach is to exhibit a formula for its `n`-th derivative and prove it is continuous (contrary to the inductive approach where one would prove smoothness statements without giving a formula for the derivative). In the end, this approach is still satisfactory as it is good to have formulas for the iterated derivatives in various constructions. One point where we depart from this explicit approach is in the proof of smoothness of a composition: there is a formula for the `n`-th derivative of a composition (Faà di Bruno's formula), but it is very complicated and barely usable, while the inductive proof is very simple. Thus, we give the inductive proof. As explained above, it works by generalizing over the target space, hence it only works well if all spaces belong to the same universe. To get the general version, we lift things to a common universe using a trick. ### Variables management The textbook definitions and proofs use various identifications and abuse of notations, for instance when saying that the natural space in which the derivative lives, i.e., `E →L[𝕜] (E →L[𝕜] ( ... →L[𝕜] F))`, is the same as a space of multilinear maps. When doing things formally, we need to provide explicit maps for these identifications, and chase some diagrams to see everything is compatible with the identifications. In particular, one needs to check that taking the derivative and then doing the identification, or first doing the identification and then taking the derivative, gives the same result. The key point for this is that taking the derivative commutes with continuous linear equivalences. Therefore, we need to implement all our identifications with continuous linear equivs. ## Notations We use the notation `E [×n]→L[𝕜] F` for the space of continuous multilinear maps on `E^n` with values in `F`. This is the space in which the `n`-th derivative of a function from `E` to `F` lives. In this file, we denote `⊤ : with_top ℕ` with `∞`. ## Tags derivative, differentiability, higher derivative, `C^n`, multilinear, Taylor series, formal series -/ noncomputable theory open_locale classical big_operators nnreal local notation `∞` := (⊤ : with_top ℕ) universes u v w local attribute [instance, priority 1001] normed_group.to_add_comm_group normed_space.to_module add_comm_group.to_add_comm_monoid open set fin filter open_locale topological_space 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] {s s₁ t u : set E} {f f₁ : E → F} {g : F → G} {x : E} {c : F} {b : E × F → G} /-! ### Functions with a Taylor series on a domain -/ variable {p : E → formal_multilinear_series 𝕜 E F} /-- `has_ftaylor_series_up_to_on n f p s` registers the fact that `p 0 = f` and `p (m+1)` is a derivative of `p m` for `m < n`, and is continuous for `m ≤ n`. This is a predicate analogous to `has_fderiv_within_at` but for higher order derivatives. -/ structure has_ftaylor_series_up_to_on (n : with_top ℕ) (f : E → F) (p : E → formal_multilinear_series 𝕜 E F) (s : set E) : Prop := (zero_eq : ∀ x ∈ s, (p x 0).uncurry0 = f x) (fderiv_within : ∀ (m : ℕ) (hm : (m : with_top ℕ) < n), ∀ x ∈ s, has_fderiv_within_at (λ y, p y m) (p x m.succ).curry_left s x) (cont : ∀ (m : ℕ) (hm : (m : with_top ℕ) ≤ n), continuous_on (λ x, p x m) s) lemma has_ftaylor_series_up_to_on.zero_eq' {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) {x : E} (hx : x ∈ s) : p x 0 = (continuous_multilinear_curry_fin0 𝕜 E F).symm (f x) := by { rw ← h.zero_eq x hx, symmetry, exact continuous_multilinear_map.uncurry0_curry0 _ } /-- If two functions coincide on a set `s`, then a Taylor series for the first one is as well a Taylor series for the second one. -/ lemma has_ftaylor_series_up_to_on.congr {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) (h₁ : ∀ x ∈ s, f₁ x = f x) : has_ftaylor_series_up_to_on n f₁ p s := begin refine ⟨λ x hx, _, h.fderiv_within, h.cont⟩, rw h₁ x hx, exact h.zero_eq x hx end lemma has_ftaylor_series_up_to_on.mono {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) {t : set E} (hst : t ⊆ s) : has_ftaylor_series_up_to_on n f p t := ⟨λ x hx, h.zero_eq x (hst hx), λ m hm x hx, (h.fderiv_within m hm x (hst hx)).mono hst, λ m hm, (h.cont m hm).mono hst⟩ lemma has_ftaylor_series_up_to_on.of_le {m n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) (hmn : m ≤ n) : has_ftaylor_series_up_to_on m f p s := ⟨h.zero_eq, λ k hk x hx, h.fderiv_within k (lt_of_lt_of_le hk hmn) x hx, λ k hk, h.cont k (le_trans hk hmn)⟩ lemma has_ftaylor_series_up_to_on.continuous_on {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) : continuous_on f s := begin have := (h.cont 0 bot_le).congr (λ x hx, (h.zero_eq' hx).symm), rwa linear_isometry_equiv.comp_continuous_on_iff at this end lemma has_ftaylor_series_up_to_on_zero_iff : has_ftaylor_series_up_to_on 0 f p s ↔ continuous_on f s ∧ (∀ x ∈ s, (p x 0).uncurry0 = f x) := begin refine ⟨λ H, ⟨H.continuous_on, H.zero_eq⟩, λ H, ⟨H.2, λ m hm, false.elim (not_le.2 hm bot_le), _⟩⟩, assume m hm, obtain rfl : m = 0, by exact_mod_cast (hm.antisymm (zero_le _)), have : ∀ x ∈ s, p x 0 = (continuous_multilinear_curry_fin0 𝕜 E F).symm (f x), by { assume x hx, rw ← H.2 x hx, symmetry, exact continuous_multilinear_map.uncurry0_curry0 _ }, rw [continuous_on_congr this, linear_isometry_equiv.comp_continuous_on_iff], exact H.1 end lemma has_ftaylor_series_up_to_on_top_iff : (has_ftaylor_series_up_to_on ∞ f p s) ↔ (∀ (n : ℕ), has_ftaylor_series_up_to_on n f p s) := begin split, { assume H n, exact H.of_le le_top }, { assume H, split, { exact (H 0).zero_eq }, { assume m hm, apply (H m.succ).fderiv_within m (with_top.coe_lt_coe.2 (lt_add_one m)) }, { assume m hm, apply (H m).cont m (le_refl _) } } end /-- If a function has a Taylor series at order at least `1`, then the term of order `1` of this series is a derivative of `f`. -/ lemma has_ftaylor_series_up_to_on.has_fderiv_within_at {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) (hx : x ∈ s) : has_fderiv_within_at f (continuous_multilinear_curry_fin1 𝕜 E F (p x 1)) s x := begin have A : ∀ y ∈ s, f y = (continuous_multilinear_curry_fin0 𝕜 E F) (p y 0), { assume y hy, rw ← h.zero_eq y hy, refl }, suffices H : has_fderiv_within_at (λ y, continuous_multilinear_curry_fin0 𝕜 E F (p y 0)) (continuous_multilinear_curry_fin1 𝕜 E F (p x 1)) s x, by exact H.congr A (A x hx), rw linear_isometry_equiv.comp_has_fderiv_within_at_iff', have : ((0 : ℕ) : with_top ℕ) < n := lt_of_lt_of_le (with_top.coe_lt_coe.2 nat.zero_lt_one) hn, convert h.fderiv_within _ this x hx, ext y v, change (p x 1) (snoc 0 y) = (p x 1) (cons y v), unfold_coes, congr' with i, rw unique.eq_default i, refl end lemma has_ftaylor_series_up_to_on.differentiable_on {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) : differentiable_on 𝕜 f s := λ x hx, (h.has_fderiv_within_at hn hx).differentiable_within_at /-- If a function has a Taylor series at order at least `1` on a neighborhood of `x`, then the term of order `1` of this series is a derivative of `f` at `x`. -/ lemma has_ftaylor_series_up_to_on.has_fderiv_at {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) (hx : s ∈ 𝓝 x) : has_fderiv_at f (continuous_multilinear_curry_fin1 𝕜 E F (p x 1)) x := (h.has_fderiv_within_at hn (mem_of_mem_nhds hx)).has_fderiv_at hx /-- If a function has a Taylor series at order at least `1` on a neighborhood of `x`, then in a neighborhood of `x`, the term of order `1` of this series is a derivative of `f`. -/ lemma has_ftaylor_series_up_to_on.eventually_has_fderiv_at {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) (hx : s ∈ 𝓝 x) : ∀ᶠ y in 𝓝 x, has_fderiv_at f (continuous_multilinear_curry_fin1 𝕜 E F (p y 1)) y := (eventually_eventually_nhds.2 hx).mono $ λ y hy, h.has_fderiv_at hn hy /-- If a function has a Taylor series at order at least `1` on a neighborhood of `x`, then it is differentiable at `x`. -/ lemma has_ftaylor_series_up_to_on.differentiable_at {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) (hx : s ∈ 𝓝 x) : differentiable_at 𝕜 f x := (h.has_fderiv_at hn hx).differentiable_at /-- `p` is a Taylor series of `f` up to `n+1` if and only if `p` is a Taylor series up to `n`, and `p (n + 1)` is a derivative of `p n`. -/ theorem has_ftaylor_series_up_to_on_succ_iff_left {n : ℕ} : has_ftaylor_series_up_to_on (n + 1) f p s ↔ has_ftaylor_series_up_to_on n f p s ∧ (∀ x ∈ s, has_fderiv_within_at (λ y, p y n) (p x n.succ).curry_left s x) ∧ continuous_on (λ x, p x (n + 1)) s := begin split, { assume h, exact ⟨h.of_le (with_top.coe_le_coe.2 (nat.le_succ n)), h.fderiv_within _ (with_top.coe_lt_coe.2 (lt_add_one n)), h.cont (n + 1) (le_refl _)⟩ }, { assume h, split, { exact h.1.zero_eq }, { assume m hm, by_cases h' : m < n, { exact h.1.fderiv_within m (with_top.coe_lt_coe.2 h') }, { have : m = n := nat.eq_of_lt_succ_of_not_lt (with_top.coe_lt_coe.1 hm) h', rw this, exact h.2.1 } }, { assume m hm, by_cases h' : m ≤ n, { apply h.1.cont m (with_top.coe_le_coe.2 h') }, { have : m = (n + 1) := le_antisymm (with_top.coe_le_coe.1 hm) (not_le.1 h'), rw this, exact h.2.2 } } } end /-- `p` is a Taylor series of `f` up to `n+1` if and only if `p.shift` is a Taylor series up to `n` for `p 1`, which is a derivative of `f`. -/ theorem has_ftaylor_series_up_to_on_succ_iff_right {n : ℕ} : has_ftaylor_series_up_to_on ((n + 1) : ℕ) f p s ↔ (∀ x ∈ s, (p x 0).uncurry0 = f x) ∧ (∀ x ∈ s, has_fderiv_within_at (λ y, p y 0) (p x 1).curry_left s x) ∧ has_ftaylor_series_up_to_on n (λ x, continuous_multilinear_curry_fin1 𝕜 E F (p x 1)) (λ x, (p x).shift) s := begin split, { assume H, refine ⟨H.zero_eq, H.fderiv_within 0 (with_top.coe_lt_coe.2 (nat.succ_pos n)), _⟩, split, { assume x hx, refl }, { assume m (hm : (m : with_top ℕ) < n) x (hx : x ∈ s), have A : (m.succ : with_top ℕ) < n.succ, by { rw with_top.coe_lt_coe at ⊢ hm, exact nat.lt_succ_iff.mpr hm }, change has_fderiv_within_at ((continuous_multilinear_curry_right_equiv' 𝕜 m E F).symm ∘ (λ (y : E), p y m.succ)) (p x m.succ.succ).curry_right.curry_left s x, rw linear_isometry_equiv.comp_has_fderiv_within_at_iff', convert H.fderiv_within _ A x hx, ext y v, change (p x m.succ.succ) (snoc (cons y (init v)) (v (last _))) = (p x (nat.succ (nat.succ m))) (cons y v), rw [← cons_snoc_eq_snoc_cons, snoc_init_self] }, { assume m (hm : (m : with_top ℕ) ≤ n), have A : (m.succ : with_top ℕ) ≤ n.succ, by { rw with_top.coe_le_coe at ⊢ hm, exact nat.pred_le_iff.mp hm }, change continuous_on ((continuous_multilinear_curry_right_equiv' 𝕜 m E F).symm ∘ (λ (y : E), p y m.succ)) s, rw linear_isometry_equiv.comp_continuous_on_iff, exact H.cont _ A } }, { rintros ⟨Hzero_eq, Hfderiv_zero, Htaylor⟩, split, { exact Hzero_eq }, { assume m (hm : (m : with_top ℕ) < n.succ) x (hx : x ∈ s), cases m, { exact Hfderiv_zero x hx }, { have A : (m : with_top ℕ) < n, by { rw with_top.coe_lt_coe at hm ⊢, exact nat.lt_of_succ_lt_succ hm }, have : has_fderiv_within_at ((continuous_multilinear_curry_right_equiv' 𝕜 m E F).symm ∘ (λ (y : E), p y m.succ)) ((p x).shift m.succ).curry_left s x := Htaylor.fderiv_within _ A x hx, rw linear_isometry_equiv.comp_has_fderiv_within_at_iff' at this, convert this, ext y v, change (p x (nat.succ (nat.succ m))) (cons y v) = (p x m.succ.succ) (snoc (cons y (init v)) (v (last _))), rw [← cons_snoc_eq_snoc_cons, snoc_init_self] } }, { assume m (hm : (m : with_top ℕ) ≤ n.succ), cases m, { have : differentiable_on 𝕜 (λ x, p x 0) s := λ x hx, (Hfderiv_zero x hx).differentiable_within_at, exact this.continuous_on }, { have A : (m : with_top ℕ) ≤ n, by { rw with_top.coe_le_coe at hm ⊢, exact nat.lt_succ_iff.mp hm }, have : continuous_on ((continuous_multilinear_curry_right_equiv' 𝕜 m E F).symm ∘ (λ (y : E), p y m.succ)) s := Htaylor.cont _ A, rwa linear_isometry_equiv.comp_continuous_on_iff at this } } } end /-! ### Smooth functions within a set around a point -/ variable (𝕜) /-- A function is continuously differentiable up to order `n` within a set `s` at a point `x` if it admits continuous derivatives up to order `n` in a neighborhood of `x` in `s ∪ {x}`. For `n = ∞`, we only require that this holds up to any finite order (where the neighborhood may depend on the finite order we consider). For instance, a real function which is `C^m` on `(-1/m, 1/m)` for each natural `m`, but not better, is `C^∞` at `0` within `univ`. -/ def times_cont_diff_within_at (n : with_top ℕ) (f : E → F) (s : set E) (x : E) := ∀ (m : ℕ), (m : with_top ℕ) ≤ n → ∃ u ∈ 𝓝[insert x s] x, ∃ p : E → formal_multilinear_series 𝕜 E F, has_ftaylor_series_up_to_on m f p u variable {𝕜} lemma times_cont_diff_within_at_nat {n : ℕ} : times_cont_diff_within_at 𝕜 n f s x ↔ ∃ u ∈ 𝓝[insert x s] x, ∃ p : E → formal_multilinear_series 𝕜 E F, has_ftaylor_series_up_to_on n f p u := ⟨λ H, H n (le_refl _), λ ⟨u, hu, p, hp⟩ m hm, ⟨u, hu, p, hp.of_le hm⟩⟩ lemma times_cont_diff_within_at.of_le {m n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) (hmn : m ≤ n) : times_cont_diff_within_at 𝕜 m f s x := λ k hk, h k (le_trans hk hmn) lemma times_cont_diff_within_at_iff_forall_nat_le {n : with_top ℕ} : times_cont_diff_within_at 𝕜 n f s x ↔ ∀ m : ℕ, ↑m ≤ n → times_cont_diff_within_at 𝕜 m f s x := ⟨λ H m hm, H.of_le hm, λ H m hm, H m hm _ le_rfl⟩ lemma times_cont_diff_within_at_top : times_cont_diff_within_at 𝕜 ∞ f s x ↔ ∀ (n : ℕ), times_cont_diff_within_at 𝕜 n f s x := times_cont_diff_within_at_iff_forall_nat_le.trans $ by simp only [forall_prop_of_true, le_top] lemma times_cont_diff_within_at.continuous_within_at {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) : continuous_within_at f s x := begin rcases h 0 bot_le with ⟨u, hu, p, H⟩, rw [mem_nhds_within_insert] at hu, exact (H.continuous_on.continuous_within_at hu.1).mono_of_mem hu.2 end lemma times_cont_diff_within_at.congr_of_eventually_eq {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : times_cont_diff_within_at 𝕜 n f₁ s x := λ m hm, let ⟨u, hu, p, H⟩ := h m hm in ⟨{x ∈ u | f₁ x = f x}, filter.inter_mem hu (mem_nhds_within_insert.2 ⟨hx, h₁⟩), p, (H.mono (sep_subset _ _)).congr (λ _, and.right)⟩ lemma times_cont_diff_within_at.congr_of_eventually_eq' {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : x ∈ s) : times_cont_diff_within_at 𝕜 n f₁ s x := h.congr_of_eventually_eq h₁ $ h₁.self_of_nhds_within hx lemma filter.eventually_eq.times_cont_diff_within_at_iff {n : with_top ℕ} (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : times_cont_diff_within_at 𝕜 n f₁ s x ↔ times_cont_diff_within_at 𝕜 n f s x := ⟨λ H, times_cont_diff_within_at.congr_of_eventually_eq H h₁.symm hx.symm, λ H, H.congr_of_eventually_eq h₁ hx⟩ lemma times_cont_diff_within_at.congr {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) (h₁ : ∀ y ∈ s, f₁ y = f y) (hx : f₁ x = f x) : times_cont_diff_within_at 𝕜 n f₁ s x := h.congr_of_eventually_eq (filter.eventually_eq_of_mem self_mem_nhds_within h₁) hx lemma times_cont_diff_within_at.congr' {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) (h₁ : ∀ y ∈ s, f₁ y = f y) (hx : x ∈ s) : times_cont_diff_within_at 𝕜 n f₁ s x := h.congr h₁ (h₁ _ hx) lemma times_cont_diff_within_at.mono_of_mem {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) {t : set E} (hst : s ∈ 𝓝[t] x) : times_cont_diff_within_at 𝕜 n f t x := begin assume m hm, rcases h m hm with ⟨u, hu, p, H⟩, exact ⟨u, nhds_within_le_of_mem (insert_mem_nhds_within_insert hst) hu, p, H⟩ end lemma times_cont_diff_within_at.mono {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) {t : set E} (hst : t ⊆ s) : times_cont_diff_within_at 𝕜 n f t x := h.mono_of_mem $ filter.mem_of_superset self_mem_nhds_within hst lemma times_cont_diff_within_at.congr_nhds {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) {t : set E} (hst : 𝓝[s] x = 𝓝[t] x) : times_cont_diff_within_at 𝕜 n f t x := h.mono_of_mem $ hst ▸ self_mem_nhds_within lemma times_cont_diff_within_at_congr_nhds {n : with_top ℕ} {t : set E} (hst : 𝓝[s] x = 𝓝[t] x) : times_cont_diff_within_at 𝕜 n f s x ↔ times_cont_diff_within_at 𝕜 n f t x := ⟨λ h, h.congr_nhds hst, λ h, h.congr_nhds hst.symm⟩ lemma times_cont_diff_within_at_inter' {n : with_top ℕ} (h : t ∈ 𝓝[s] x) : times_cont_diff_within_at 𝕜 n f (s ∩ t) x ↔ times_cont_diff_within_at 𝕜 n f s x := times_cont_diff_within_at_congr_nhds $ eq.symm $ nhds_within_restrict'' _ h lemma times_cont_diff_within_at_inter {n : with_top ℕ} (h : t ∈ 𝓝 x) : times_cont_diff_within_at 𝕜 n f (s ∩ t) x ↔ times_cont_diff_within_at 𝕜 n f s x := times_cont_diff_within_at_inter' (mem_nhds_within_of_mem_nhds h) /-- If a function is `C^n` within a set at a point, with `n ≥ 1`, then it is differentiable within this set at this point. -/ lemma times_cont_diff_within_at.differentiable_within_at' {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) (hn : 1 ≤ n) : differentiable_within_at 𝕜 f (insert x s) x := begin rcases h 1 hn with ⟨u, hu, p, H⟩, rcases mem_nhds_within.1 hu with ⟨t, t_open, xt, tu⟩, rw inter_comm at tu, have := ((H.mono tu).differentiable_on (le_refl _)) x ⟨mem_insert x s, xt⟩, exact (differentiable_within_at_inter (is_open.mem_nhds t_open xt)).1 this, end lemma times_cont_diff_within_at.differentiable_within_at {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) (hn : 1 ≤ n) : differentiable_within_at 𝕜 f s x := (h.differentiable_within_at' hn).mono (subset_insert x s) /-- A function is `C^(n + 1)` on a domain iff locally, it has a derivative which is `C^n`. -/ theorem times_cont_diff_within_at_succ_iff_has_fderiv_within_at {n : ℕ} : times_cont_diff_within_at 𝕜 ((n + 1) : ℕ) f s x ↔ ∃ u ∈ 𝓝[insert x s] x, ∃ f' : E → (E →L[𝕜] F), (∀ x ∈ u, has_fderiv_within_at f (f' x) u x) ∧ (times_cont_diff_within_at 𝕜 n f' u x) := begin split, { assume h, rcases h n.succ (le_refl _) with ⟨u, hu, p, Hp⟩, refine ⟨u, hu, λ y, (continuous_multilinear_curry_fin1 𝕜 E F) (p y 1), λ y hy, Hp.has_fderiv_within_at (with_top.coe_le_coe.2 (nat.le_add_left 1 n)) hy, _⟩, assume m hm, refine ⟨u, _, λ (y : E), (p y).shift, _⟩, { convert self_mem_nhds_within, have : x ∈ insert x s, by simp, exact (insert_eq_of_mem (mem_of_mem_nhds_within this hu)) }, { rw has_ftaylor_series_up_to_on_succ_iff_right at Hp, exact Hp.2.2.of_le hm } }, { rintros ⟨u, hu, f', f'_eq_deriv, Hf'⟩, rw times_cont_diff_within_at_nat, rcases Hf' n (le_refl _) with ⟨v, hv, p', Hp'⟩, refine ⟨v ∩ u, _, λ x, (p' x).unshift (f x), _⟩, { apply filter.inter_mem _ hu, apply nhds_within_le_of_mem hu, exact nhds_within_mono _ (subset_insert x u) hv }, { rw has_ftaylor_series_up_to_on_succ_iff_right, refine ⟨λ y hy, rfl, λ y hy, _, _⟩, { change has_fderiv_within_at (λ z, (continuous_multilinear_curry_fin0 𝕜 E F).symm (f z)) ((formal_multilinear_series.unshift (p' y) (f y) 1).curry_left) (v ∩ u) y, rw linear_isometry_equiv.comp_has_fderiv_within_at_iff', convert (f'_eq_deriv y hy.2).mono (inter_subset_right v u), rw ← Hp'.zero_eq y hy.1, ext z, change ((p' y 0) (init (@cons 0 (λ i, E) z 0))) (@cons 0 (λ i, E) z 0 (last 0)) = ((p' y 0) 0) z, unfold_coes, congr }, { convert (Hp'.mono (inter_subset_left v u)).congr (λ x hx, Hp'.zero_eq x hx.1), { ext x y, change p' x 0 (init (@snoc 0 (λ i : fin 1, E) 0 y)) y = p' x 0 0 y, rw init_snoc }, { ext x k v y, change p' x k (init (@snoc k (λ i : fin k.succ, E) v y)) (@snoc k (λ i : fin k.succ, E) v y (last k)) = p' x k v y, rw [snoc_last, init_snoc] } } } } end /-! ### Smooth functions within a set -/ variable (𝕜) /-- A function is continuously differentiable up to `n` on `s` if, for any point `x` in `s`, it admits continuous derivatives up to order `n` on a neighborhood of `x` in `s`. For `n = ∞`, we only require that this holds up to any finite order (where the neighborhood may depend on the finite order we consider). -/ definition times_cont_diff_on (n : with_top ℕ) (f : E → F) (s : set E) := ∀ x ∈ s, times_cont_diff_within_at 𝕜 n f s x variable {𝕜} lemma times_cont_diff_on.times_cont_diff_within_at {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hx : x ∈ s) : times_cont_diff_within_at 𝕜 n f s x := h x hx lemma times_cont_diff_within_at.times_cont_diff_on {n : with_top ℕ} {m : ℕ} (hm : (m : with_top ℕ) ≤ n) (h : times_cont_diff_within_at 𝕜 n f s x) : ∃ u ∈ 𝓝[insert x s] x, u ⊆ insert x s ∧ times_cont_diff_on 𝕜 m f u := begin rcases h m hm with ⟨u, u_nhd, p, hp⟩, refine ⟨u ∩ insert x s, filter.inter_mem u_nhd self_mem_nhds_within, inter_subset_right _ _, _⟩, assume y hy m' hm', refine ⟨u ∩ insert x s, _, p, (hp.mono (inter_subset_left _ _)).of_le hm'⟩, convert self_mem_nhds_within, exact insert_eq_of_mem hy end protected lemma times_cont_diff_within_at.eventually {n : ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) : ∀ᶠ y in 𝓝[insert x s] x, times_cont_diff_within_at 𝕜 n f s y := begin rcases h.times_cont_diff_on le_rfl with ⟨u, hu, hu_sub, hd⟩, have : ∀ᶠ (y : E) in 𝓝[insert x s] x, u ∈ 𝓝[insert x s] y ∧ y ∈ u, from (eventually_nhds_within_nhds_within.2 hu).and hu, refine this.mono (λ y hy, (hd y hy.2).mono_of_mem _), exact nhds_within_mono y (subset_insert _ _) hy.1 end lemma times_cont_diff_on.of_le {m n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hmn : m ≤ n) : times_cont_diff_on 𝕜 m f s := λ x hx, (h x hx).of_le hmn lemma times_cont_diff_on_iff_forall_nat_le {n : with_top ℕ} : times_cont_diff_on 𝕜 n f s ↔ ∀ m : ℕ, ↑m ≤ n → times_cont_diff_on 𝕜 m f s := ⟨λ H m hm, H.of_le hm, λ H x hx m hm, H m hm x hx m le_rfl⟩ lemma times_cont_diff_on_top : times_cont_diff_on 𝕜 ∞ f s ↔ ∀ (n : ℕ), times_cont_diff_on 𝕜 n f s := times_cont_diff_on_iff_forall_nat_le.trans $ by simp only [le_top, forall_prop_of_true] lemma times_cont_diff_on_all_iff_nat : (∀ n, times_cont_diff_on 𝕜 n f s) ↔ (∀ n : ℕ, times_cont_diff_on 𝕜 n f s) := begin refine ⟨λ H n, H n, _⟩, rintro H (_|n), exacts [times_cont_diff_on_top.2 H, H n] end lemma times_cont_diff_on.continuous_on {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) : continuous_on f s := λ x hx, (h x hx).continuous_within_at lemma times_cont_diff_on.congr {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (h₁ : ∀ x ∈ s, f₁ x = f x) : times_cont_diff_on 𝕜 n f₁ s := λ x hx, (h x hx).congr h₁ (h₁ x hx) lemma times_cont_diff_on_congr {n : with_top ℕ} (h₁ : ∀ x ∈ s, f₁ x = f x) : times_cont_diff_on 𝕜 n f₁ s ↔ times_cont_diff_on 𝕜 n f s := ⟨λ H, H.congr (λ x hx, (h₁ x hx).symm), λ H, H.congr h₁⟩ lemma times_cont_diff_on.mono {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) {t : set E} (hst : t ⊆ s) : times_cont_diff_on 𝕜 n f t := λ x hx, (h x (hst hx)).mono hst lemma times_cont_diff_on.congr_mono {n : with_top ℕ} (hf : times_cont_diff_on 𝕜 n f s) (h₁ : ∀ x ∈ s₁, f₁ x = f x) (hs : s₁ ⊆ s) : times_cont_diff_on 𝕜 n f₁ s₁ := (hf.mono hs).congr h₁ /-- If a function is `C^n` on a set with `n ≥ 1`, then it is differentiable there. -/ lemma times_cont_diff_on.differentiable_on {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hn : 1 ≤ n) : differentiable_on 𝕜 f s := λ x hx, (h x hx).differentiable_within_at hn /-- If a function is `C^n` around each point in a set, then it is `C^n` on the set. -/ lemma times_cont_diff_on_of_locally_times_cont_diff_on {n : with_top ℕ} (h : ∀ x ∈ s, ∃u, is_open u ∧ x ∈ u ∧ times_cont_diff_on 𝕜 n f (s ∩ u)) : times_cont_diff_on 𝕜 n f s := begin assume x xs, rcases h x xs with ⟨u, u_open, xu, hu⟩, apply (times_cont_diff_within_at_inter _).1 (hu x ⟨xs, xu⟩), exact is_open.mem_nhds u_open xu end /-- A function is `C^(n + 1)` on a domain iff locally, it has a derivative which is `C^n`. -/ theorem times_cont_diff_on_succ_iff_has_fderiv_within_at {n : ℕ} : times_cont_diff_on 𝕜 ((n + 1) : ℕ) f s ↔ ∀ x ∈ s, ∃ u ∈ 𝓝[insert x s] x, ∃ f' : E → (E →L[𝕜] F), (∀ x ∈ u, has_fderiv_within_at f (f' x) u x) ∧ (times_cont_diff_on 𝕜 n f' u) := begin split, { assume h x hx, rcases (h x hx) n.succ (le_refl _) with ⟨u, hu, p, Hp⟩, refine ⟨u, hu, λ y, (continuous_multilinear_curry_fin1 𝕜 E F) (p y 1), λ y hy, Hp.has_fderiv_within_at (with_top.coe_le_coe.2 (nat.le_add_left 1 n)) hy, _⟩, rw has_ftaylor_series_up_to_on_succ_iff_right at Hp, assume z hz m hm, refine ⟨u, _, λ (x : E), (p x).shift, Hp.2.2.of_le hm⟩, convert self_mem_nhds_within, exact insert_eq_of_mem hz, }, { assume h x hx, rw times_cont_diff_within_at_succ_iff_has_fderiv_within_at, rcases h x hx with ⟨u, u_nhbd, f', hu, hf'⟩, have : x ∈ u := mem_of_mem_nhds_within (mem_insert _ _) u_nhbd, exact ⟨u, u_nhbd, f', hu, hf' x this⟩ } end /-! ### Iterated derivative within a set -/ variable (𝕜) /-- The `n`-th derivative of a function along a set, defined inductively by saying that the `n+1`-th derivative of `f` is the derivative of the `n`-th derivative of `f` along this set, together with an uncurrying step to see it as a multilinear map in `n+1` variables.. -/ noncomputable def iterated_fderiv_within (n : ℕ) (f : E → F) (s : set E) : E → (E [×n]→L[𝕜] F) := nat.rec_on n (λ x, continuous_multilinear_map.curry0 𝕜 E (f x)) (λ n rec x, continuous_linear_map.uncurry_left (fderiv_within 𝕜 rec s x)) /-- Formal Taylor series associated to a function within a set. -/ def ftaylor_series_within (f : E → F) (s : set E) (x : E) : formal_multilinear_series 𝕜 E F := λ n, iterated_fderiv_within 𝕜 n f s x variable {𝕜} @[simp] lemma iterated_fderiv_within_zero_apply (m : (fin 0) → E) : (iterated_fderiv_within 𝕜 0 f s x : ((fin 0) → E) → F) m = f x := rfl lemma iterated_fderiv_within_zero_eq_comp : iterated_fderiv_within 𝕜 0 f s = (continuous_multilinear_curry_fin0 𝕜 E F).symm ∘ f := rfl lemma iterated_fderiv_within_succ_apply_left {n : ℕ} (m : fin (n + 1) → E): (iterated_fderiv_within 𝕜 (n + 1) f s x : (fin (n + 1) → E) → F) m = (fderiv_within 𝕜 (iterated_fderiv_within 𝕜 n f s) s x : E → (E [×n]→L[𝕜] F)) (m 0) (tail m) := rfl /-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv, and the derivative of the `n`-th derivative. -/ lemma iterated_fderiv_within_succ_eq_comp_left {n : ℕ} : iterated_fderiv_within 𝕜 (n + 1) f s = (continuous_multilinear_curry_left_equiv 𝕜 (λ(i : fin (n + 1)), E) F) ∘ (fderiv_within 𝕜 (iterated_fderiv_within 𝕜 n f s) s) := rfl theorem iterated_fderiv_within_succ_apply_right {n : ℕ} (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) (m : fin (n + 1) → E) : (iterated_fderiv_within 𝕜 (n + 1) f s x : (fin (n + 1) → E) → F) m = iterated_fderiv_within 𝕜 n (λy, fderiv_within 𝕜 f s y) s x (init m) (m (last n)) := begin induction n with n IH generalizing x, { rw [iterated_fderiv_within_succ_eq_comp_left, iterated_fderiv_within_zero_eq_comp, iterated_fderiv_within_zero_apply, function.comp_apply, linear_isometry_equiv.comp_fderiv_within _ (hs x hx)], refl }, { let I := continuous_multilinear_curry_right_equiv' 𝕜 n E F, have A : ∀ y ∈ s, iterated_fderiv_within 𝕜 n.succ f s y = (I ∘ (iterated_fderiv_within 𝕜 n (λy, fderiv_within 𝕜 f s y) s)) y, by { assume y hy, ext m, rw @IH m y hy, refl }, calc (iterated_fderiv_within 𝕜 (n+2) f s x : (fin (n+2) → E) → F) m = (fderiv_within 𝕜 (iterated_fderiv_within 𝕜 n.succ f s) s x : E → (E [×(n + 1)]→L[𝕜] F)) (m 0) (tail m) : rfl ... = (fderiv_within 𝕜 (I ∘ (iterated_fderiv_within 𝕜 n (fderiv_within 𝕜 f s) s)) s x : E → (E [×(n + 1)]→L[𝕜] F)) (m 0) (tail m) : by rw fderiv_within_congr (hs x hx) A (A x hx) ... = (I ∘ fderiv_within 𝕜 ((iterated_fderiv_within 𝕜 n (fderiv_within 𝕜 f s) s)) s x : E → (E [×(n + 1)]→L[𝕜] F)) (m 0) (tail m) : by { rw linear_isometry_equiv.comp_fderiv_within _ (hs x hx), refl } ... = (fderiv_within 𝕜 ((iterated_fderiv_within 𝕜 n (λ y, fderiv_within 𝕜 f s y) s)) s x : E → (E [×n]→L[𝕜] (E →L[𝕜] F))) (m 0) (init (tail m)) ((tail m) (last n)) : rfl ... = iterated_fderiv_within 𝕜 (nat.succ n) (λ y, fderiv_within 𝕜 f s y) s x (init m) (m (last (n + 1))) : by { rw [iterated_fderiv_within_succ_apply_left, tail_init_eq_init_tail], refl } } end /-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv, and the `n`-th derivative of the derivative. -/ lemma iterated_fderiv_within_succ_eq_comp_right {n : ℕ} (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) : iterated_fderiv_within 𝕜 (n + 1) f s x = ((continuous_multilinear_curry_right_equiv' 𝕜 n E F) ∘ (iterated_fderiv_within 𝕜 n (λy, fderiv_within 𝕜 f s y) s)) x := by { ext m, rw iterated_fderiv_within_succ_apply_right hs hx, refl } @[simp] lemma iterated_fderiv_within_one_apply (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) (m : (fin 1) → E) : (iterated_fderiv_within 𝕜 1 f s x : ((fin 1) → E) → F) m = (fderiv_within 𝕜 f s x : E → F) (m 0) := by { rw [iterated_fderiv_within_succ_apply_right hs hx, iterated_fderiv_within_zero_apply], refl } /-- If two functions coincide on a set `s` of unique differentiability, then their iterated differentials within this set coincide. -/ lemma iterated_fderiv_within_congr {n : ℕ} (hs : unique_diff_on 𝕜 s) (hL : ∀y∈s, f₁ y = f y) (hx : x ∈ s) : iterated_fderiv_within 𝕜 n f₁ s x = iterated_fderiv_within 𝕜 n f s x := begin induction n with n IH generalizing x, { ext m, simp [hL x hx] }, { have : fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f₁ s y) s x = fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f s y) s x := fderiv_within_congr (hs x hx) (λ y hy, IH hy) (IH hx), ext m, rw [iterated_fderiv_within_succ_apply_left, iterated_fderiv_within_succ_apply_left, this] } end /-- The iterated differential within a set `s` at a point `x` is not modified if one intersects `s` with an open set containing `x`. -/ lemma iterated_fderiv_within_inter_open {n : ℕ} (hu : is_open u) (hs : unique_diff_on 𝕜 (s ∩ u)) (hx : x ∈ s ∩ u) : iterated_fderiv_within 𝕜 n f (s ∩ u) x = iterated_fderiv_within 𝕜 n f s x := begin induction n with n IH generalizing x, { ext m, simp }, { have A : fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f (s ∩ u) y) (s ∩ u) x = fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f s y) (s ∩ u) x := fderiv_within_congr (hs x hx) (λ y hy, IH hy) (IH hx), have B : fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f s y) (s ∩ u) x = fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f s y) s x := fderiv_within_inter (is_open.mem_nhds hu hx.2) ((unique_diff_within_at_inter (is_open.mem_nhds hu hx.2)).1 (hs x hx)), ext m, rw [iterated_fderiv_within_succ_apply_left, iterated_fderiv_within_succ_apply_left, A, B] } end /-- The iterated differential within a set `s` at a point `x` is not modified if one intersects `s` with a neighborhood of `x` within `s`. -/ lemma iterated_fderiv_within_inter' {n : ℕ} (hu : u ∈ 𝓝[s] x) (hs : unique_diff_on 𝕜 s) (xs : x ∈ s) : iterated_fderiv_within 𝕜 n f (s ∩ u) x = iterated_fderiv_within 𝕜 n f s x := begin obtain ⟨v, v_open, xv, vu⟩ : ∃ v, is_open v ∧ x ∈ v ∧ v ∩ s ⊆ u := mem_nhds_within.1 hu, have A : (s ∩ u) ∩ v = s ∩ v, { apply subset.antisymm (inter_subset_inter (inter_subset_left _ _) (subset.refl _)), exact λ y ⟨ys, yv⟩, ⟨⟨ys, vu ⟨yv, ys⟩⟩, yv⟩ }, have : iterated_fderiv_within 𝕜 n f (s ∩ v) x = iterated_fderiv_within 𝕜 n f s x := iterated_fderiv_within_inter_open v_open (hs.inter v_open) ⟨xs, xv⟩, rw ← this, have : iterated_fderiv_within 𝕜 n f ((s ∩ u) ∩ v) x = iterated_fderiv_within 𝕜 n f (s ∩ u) x, { refine iterated_fderiv_within_inter_open v_open _ ⟨⟨xs, vu ⟨xv, xs⟩⟩, xv⟩, rw A, exact hs.inter v_open }, rw A at this, rw ← this end /-- The iterated differential within a set `s` at a point `x` is not modified if one intersects `s` with a neighborhood of `x`. -/ lemma iterated_fderiv_within_inter {n : ℕ} (hu : u ∈ 𝓝 x) (hs : unique_diff_on 𝕜 s) (xs : x ∈ s) : iterated_fderiv_within 𝕜 n f (s ∩ u) x = iterated_fderiv_within 𝕜 n f s x := iterated_fderiv_within_inter' (mem_nhds_within_of_mem_nhds hu) hs xs @[simp] lemma times_cont_diff_on_zero : times_cont_diff_on 𝕜 0 f s ↔ continuous_on f s := begin refine ⟨λ H, H.continuous_on, λ H, _⟩, assume x hx m hm, have : (m : with_top ℕ) = 0 := le_antisymm hm bot_le, rw this, refine ⟨insert x s, self_mem_nhds_within, ftaylor_series_within 𝕜 f s, _⟩, rw has_ftaylor_series_up_to_on_zero_iff, exact ⟨by rwa insert_eq_of_mem hx, λ x hx, by simp [ftaylor_series_within]⟩ end lemma times_cont_diff_within_at_zero (hx : x ∈ s) : times_cont_diff_within_at 𝕜 0 f s x ↔ ∃ u ∈ 𝓝[s] x, continuous_on f (s ∩ u) := begin split, { intros h, obtain ⟨u, H, p, hp⟩ := h 0 (by norm_num), refine ⟨u, _, _⟩, { simpa [hx] using H }, { simp only [with_top.coe_zero, has_ftaylor_series_up_to_on_zero_iff] at hp, exact hp.1.mono (inter_subset_right s u) } }, { rintros ⟨u, H, hu⟩, rw ← times_cont_diff_within_at_inter' H, have h' : x ∈ s ∩ u := ⟨hx, mem_of_mem_nhds_within hx H⟩, exact (times_cont_diff_on_zero.mpr hu).times_cont_diff_within_at h' } end /-- On a set with unique differentiability, any choice of iterated differential has to coincide with the one we have chosen in `iterated_fderiv_within 𝕜 m f s`. -/ theorem has_ftaylor_series_up_to_on.eq_ftaylor_series_of_unique_diff_on {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) {m : ℕ} (hmn : (m : with_top ℕ) ≤ n) (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) : p x m = iterated_fderiv_within 𝕜 m f s x := begin induction m with m IH generalizing x, { rw [h.zero_eq' hx, iterated_fderiv_within_zero_eq_comp] }, { have A : (m : with_top ℕ) < n := lt_of_lt_of_le (with_top.coe_lt_coe.2 (lt_add_one m)) hmn, have : has_fderiv_within_at (λ (y : E), iterated_fderiv_within 𝕜 m f s y) (continuous_multilinear_map.curry_left (p x (nat.succ m))) s x := (h.fderiv_within m A x hx).congr (λ y hy, (IH (le_of_lt A) hy).symm) (IH (le_of_lt A) hx).symm, rw [iterated_fderiv_within_succ_eq_comp_left, function.comp_apply, this.fderiv_within (hs x hx)], exact (continuous_multilinear_map.uncurry_curry_left _).symm } end /-- When a function is `C^n` in a set `s` of unique differentiability, it admits `ftaylor_series_within 𝕜 f s` as a Taylor series up to order `n` in `s`. -/ theorem times_cont_diff_on.ftaylor_series_within {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) : has_ftaylor_series_up_to_on n f (ftaylor_series_within 𝕜 f s) s := begin split, { assume x hx, simp only [ftaylor_series_within, continuous_multilinear_map.uncurry0_apply, iterated_fderiv_within_zero_apply] }, { assume m hm x hx, rcases (h x hx) m.succ (with_top.add_one_le_of_lt hm) with ⟨u, hu, p, Hp⟩, rw insert_eq_of_mem hx at hu, rcases mem_nhds_within.1 hu with ⟨o, o_open, xo, ho⟩, rw inter_comm at ho, have : p x m.succ = ftaylor_series_within 𝕜 f s x m.succ, { change p x m.succ = iterated_fderiv_within 𝕜 m.succ f s x, rw ← iterated_fderiv_within_inter (is_open.mem_nhds o_open xo) hs hx, exact (Hp.mono ho).eq_ftaylor_series_of_unique_diff_on (le_refl _) (hs.inter o_open) ⟨hx, xo⟩ }, rw [← this, ← has_fderiv_within_at_inter (is_open.mem_nhds o_open xo)], have A : ∀ y ∈ s ∩ o, p y m = ftaylor_series_within 𝕜 f s y m, { rintros y ⟨hy, yo⟩, change p y m = iterated_fderiv_within 𝕜 m f s y, rw ← iterated_fderiv_within_inter (is_open.mem_nhds o_open yo) hs hy, exact (Hp.mono ho).eq_ftaylor_series_of_unique_diff_on (with_top.coe_le_coe.2 (nat.le_succ m)) (hs.inter o_open) ⟨hy, yo⟩ }, exact ((Hp.mono ho).fderiv_within m (with_top.coe_lt_coe.2 (lt_add_one m)) x ⟨hx, xo⟩).congr (λ y hy, (A y hy).symm) (A x ⟨hx, xo⟩).symm }, { assume m hm, apply continuous_on_of_locally_continuous_on, assume x hx, rcases h x hx m hm with ⟨u, hu, p, Hp⟩, rcases mem_nhds_within.1 hu with ⟨o, o_open, xo, ho⟩, rw insert_eq_of_mem hx at ho, rw inter_comm at ho, refine ⟨o, o_open, xo, _⟩, have A : ∀ y ∈ s ∩ o, p y m = ftaylor_series_within 𝕜 f s y m, { rintros y ⟨hy, yo⟩, change p y m = iterated_fderiv_within 𝕜 m f s y, rw ← iterated_fderiv_within_inter (is_open.mem_nhds o_open yo) hs hy, exact (Hp.mono ho).eq_ftaylor_series_of_unique_diff_on (le_refl _) (hs.inter o_open) ⟨hy, yo⟩ }, exact ((Hp.mono ho).cont m (le_refl _)).congr (λ y hy, (A y hy).symm) } end lemma times_cont_diff_on_of_continuous_on_differentiable_on {n : with_top ℕ} (Hcont : ∀ (m : ℕ), (m : with_top ℕ) ≤ n → continuous_on (λ x, iterated_fderiv_within 𝕜 m f s x) s) (Hdiff : ∀ (m : ℕ), (m : with_top ℕ) < n → differentiable_on 𝕜 (λ x, iterated_fderiv_within 𝕜 m f s x) s) : times_cont_diff_on 𝕜 n f s := begin assume x hx m hm, rw insert_eq_of_mem hx, refine ⟨s, self_mem_nhds_within, ftaylor_series_within 𝕜 f s, _⟩, split, { assume y hy, simp only [ftaylor_series_within, continuous_multilinear_map.uncurry0_apply, iterated_fderiv_within_zero_apply] }, { assume k hk y hy, convert (Hdiff k (lt_of_lt_of_le hk hm) y hy).has_fderiv_within_at, simp only [ftaylor_series_within, iterated_fderiv_within_succ_eq_comp_left, continuous_linear_equiv.coe_apply, function.comp_app, coe_fn_coe_base], exact continuous_linear_map.curry_uncurry_left _ }, { assume k hk, exact Hcont k (le_trans hk hm) } end lemma times_cont_diff_on_of_differentiable_on {n : with_top ℕ} (h : ∀(m : ℕ), (m : with_top ℕ) ≤ n → differentiable_on 𝕜 (iterated_fderiv_within 𝕜 m f s) s) : times_cont_diff_on 𝕜 n f s := times_cont_diff_on_of_continuous_on_differentiable_on (λ m hm, (h m hm).continuous_on) (λ m hm, (h m (le_of_lt hm))) lemma times_cont_diff_on.continuous_on_iterated_fderiv_within {n : with_top ℕ} {m : ℕ} (h : times_cont_diff_on 𝕜 n f s) (hmn : (m : with_top ℕ) ≤ n) (hs : unique_diff_on 𝕜 s) : continuous_on (iterated_fderiv_within 𝕜 m f s) s := (h.ftaylor_series_within hs).cont m hmn lemma times_cont_diff_on.differentiable_on_iterated_fderiv_within {n : with_top ℕ} {m : ℕ} (h : times_cont_diff_on 𝕜 n f s) (hmn : (m : with_top ℕ) < n) (hs : unique_diff_on 𝕜 s) : differentiable_on 𝕜 (iterated_fderiv_within 𝕜 m f s) s := λ x hx, ((h.ftaylor_series_within hs).fderiv_within m hmn x hx).differentiable_within_at lemma times_cont_diff_on_iff_continuous_on_differentiable_on {n : with_top ℕ} (hs : unique_diff_on 𝕜 s) : times_cont_diff_on 𝕜 n f s ↔ (∀ (m : ℕ), (m : with_top ℕ) ≤ n → continuous_on (λ x, iterated_fderiv_within 𝕜 m f s x) s) ∧ (∀ (m : ℕ), (m : with_top ℕ) < n → differentiable_on 𝕜 (λ x, iterated_fderiv_within 𝕜 m f s x) s) := begin split, { assume h, split, { assume m hm, exact h.continuous_on_iterated_fderiv_within hm hs }, { assume m hm, exact h.differentiable_on_iterated_fderiv_within hm hs } }, { assume h, exact times_cont_diff_on_of_continuous_on_differentiable_on h.1 h.2 } end /-- A function is `C^(n + 1)` on a domain with unique derivatives if and only if it is differentiable there, and its derivative (expressed with `fderiv_within`) is `C^n`. -/ theorem times_cont_diff_on_succ_iff_fderiv_within {n : ℕ} (hs : unique_diff_on 𝕜 s) : times_cont_diff_on 𝕜 ((n + 1) : ℕ) f s ↔ differentiable_on 𝕜 f s ∧ times_cont_diff_on 𝕜 n (λ y, fderiv_within 𝕜 f s y) s := begin split, { assume H, refine ⟨H.differentiable_on (with_top.coe_le_coe.2 (nat.le_add_left 1 n)), λ x hx, _⟩, rcases times_cont_diff_within_at_succ_iff_has_fderiv_within_at.1 (H x hx) with ⟨u, hu, f', hff', hf'⟩, rcases mem_nhds_within.1 hu with ⟨o, o_open, xo, ho⟩, rw [inter_comm, insert_eq_of_mem hx] at ho, have := hf'.mono ho, rw times_cont_diff_within_at_inter' (mem_nhds_within_of_mem_nhds (is_open.mem_nhds o_open xo)) at this, apply this.congr_of_eventually_eq' _ hx, have : o ∩ s ∈ 𝓝[s] x := mem_nhds_within.2 ⟨o, o_open, xo, subset.refl _⟩, rw inter_comm at this, apply filter.eventually_eq_of_mem this (λ y hy, _), have A : fderiv_within 𝕜 f (s ∩ o) y = f' y := ((hff' y (ho hy)).mono ho).fderiv_within (hs.inter o_open y hy), rwa fderiv_within_inter (is_open.mem_nhds o_open hy.2) (hs y hy.1) at A, }, { rintros ⟨hdiff, h⟩ x hx, rw [times_cont_diff_within_at_succ_iff_has_fderiv_within_at, insert_eq_of_mem hx], exact ⟨s, self_mem_nhds_within, fderiv_within 𝕜 f s, λ y hy, (hdiff y hy).has_fderiv_within_at, h x hx⟩ } end /-- A function is `C^(n + 1)` on an open domain if and only if it is differentiable there, and its derivative (expressed with `fderiv`) is `C^n`. -/ theorem times_cont_diff_on_succ_iff_fderiv_of_open {n : ℕ} (hs : is_open s) : times_cont_diff_on 𝕜 ((n + 1) : ℕ) f s ↔ differentiable_on 𝕜 f s ∧ times_cont_diff_on 𝕜 n (λ y, fderiv 𝕜 f y) s := begin rw times_cont_diff_on_succ_iff_fderiv_within hs.unique_diff_on, congr' 2, rw ← iff_iff_eq, apply times_cont_diff_on_congr, assume x hx, exact fderiv_within_of_open hs hx end /-- A function is `C^∞` on a domain with unique derivatives if and only if it is differentiable there, and its derivative (expressed with `fderiv_within`) is `C^∞`. -/ theorem times_cont_diff_on_top_iff_fderiv_within (hs : unique_diff_on 𝕜 s) : times_cont_diff_on 𝕜 ∞ f s ↔ differentiable_on 𝕜 f s ∧ times_cont_diff_on 𝕜 ∞ (λ y, fderiv_within 𝕜 f s y) s := begin split, { assume h, refine ⟨h.differentiable_on le_top, _⟩, apply times_cont_diff_on_top.2 (λ n, ((times_cont_diff_on_succ_iff_fderiv_within hs).1 _).2), exact h.of_le le_top }, { assume h, refine times_cont_diff_on_top.2 (λ n, _), have A : (n : with_top ℕ) ≤ ∞ := le_top, apply ((times_cont_diff_on_succ_iff_fderiv_within hs).2 ⟨h.1, h.2.of_le A⟩).of_le, exact with_top.coe_le_coe.2 (nat.le_succ n) } end /-- A function is `C^∞` on an open domain if and only if it is differentiable there, and its derivative (expressed with `fderiv`) is `C^∞`. -/ theorem times_cont_diff_on_top_iff_fderiv_of_open (hs : is_open s) : times_cont_diff_on 𝕜 ∞ f s ↔ differentiable_on 𝕜 f s ∧ times_cont_diff_on 𝕜 ∞ (λ y, fderiv 𝕜 f y) s := begin rw times_cont_diff_on_top_iff_fderiv_within hs.unique_diff_on, congr' 2, rw ← iff_iff_eq, apply times_cont_diff_on_congr, assume x hx, exact fderiv_within_of_open hs hx end lemma times_cont_diff_on.fderiv_within {m n : with_top ℕ} (hf : times_cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hmn : m + 1 ≤ n) : times_cont_diff_on 𝕜 m (λ y, fderiv_within 𝕜 f s y) s := begin cases m, { change ∞ + 1 ≤ n at hmn, have : n = ∞, by simpa using hmn, rw this at hf, exact ((times_cont_diff_on_top_iff_fderiv_within hs).1 hf).2 }, { change (m.succ : with_top ℕ) ≤ n at hmn, exact ((times_cont_diff_on_succ_iff_fderiv_within hs).1 (hf.of_le hmn)).2 } end lemma times_cont_diff_on.fderiv_of_open {m n : with_top ℕ} (hf : times_cont_diff_on 𝕜 n f s) (hs : is_open s) (hmn : m + 1 ≤ n) : times_cont_diff_on 𝕜 m (λ y, fderiv 𝕜 f y) s := (hf.fderiv_within hs.unique_diff_on hmn).congr (λ x hx, (fderiv_within_of_open hs hx).symm) lemma times_cont_diff_on.continuous_on_fderiv_within {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hn : 1 ≤ n) : continuous_on (λ x, fderiv_within 𝕜 f s x) s := ((times_cont_diff_on_succ_iff_fderiv_within hs).1 (h.of_le hn)).2.continuous_on lemma times_cont_diff_on.continuous_on_fderiv_of_open {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hs : is_open s) (hn : 1 ≤ n) : continuous_on (λ x, fderiv 𝕜 f x) s := ((times_cont_diff_on_succ_iff_fderiv_of_open hs).1 (h.of_le hn)).2.continuous_on /-- If a function is at least `C^1`, its bundled derivative (mapping `(x, v)` to `Df(x) v`) is continuous. -/ lemma times_cont_diff_on.continuous_on_fderiv_within_apply {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hn : 1 ≤ n) : continuous_on (λp : E × E, (fderiv_within 𝕜 f s p.1 : E → F) p.2) (set.prod s univ) := begin have A : continuous (λq : (E →L[𝕜] F) × E, q.1 q.2) := is_bounded_bilinear_map_apply.continuous, have B : continuous_on (λp : E × E, (fderiv_within 𝕜 f s p.1, p.2)) (set.prod s univ), { apply continuous_on.prod _ continuous_snd.continuous_on, exact continuous_on.comp (h.continuous_on_fderiv_within hs hn) continuous_fst.continuous_on (prod_subset_preimage_fst _ _) }, exact A.comp_continuous_on B end /-! ### Functions with a Taylor series on the whole space -/ /-- `has_ftaylor_series_up_to n f p` registers the fact that `p 0 = f` and `p (m+1)` is a derivative of `p m` for `m < n`, and is continuous for `m ≤ n`. This is a predicate analogous to `has_fderiv_at` but for higher order derivatives. -/ structure has_ftaylor_series_up_to (n : with_top ℕ) (f : E → F) (p : E → formal_multilinear_series 𝕜 E F) : Prop := (zero_eq : ∀ x, (p x 0).uncurry0 = f x) (fderiv : ∀ (m : ℕ) (hm : (m : with_top ℕ) < n), ∀ x, has_fderiv_at (λ y, p y m) (p x m.succ).curry_left x) (cont : ∀ (m : ℕ) (hm : (m : with_top ℕ) ≤ n), continuous (λ x, p x m)) lemma has_ftaylor_series_up_to.zero_eq' {n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) (x : E) : p x 0 = (continuous_multilinear_curry_fin0 𝕜 E F).symm (f x) := by { rw ← h.zero_eq x, symmetry, exact continuous_multilinear_map.uncurry0_curry0 _ } lemma has_ftaylor_series_up_to_on_univ_iff {n : with_top ℕ} : has_ftaylor_series_up_to_on n f p univ ↔ has_ftaylor_series_up_to n f p := begin split, { assume H, split, { exact λ x, H.zero_eq x (mem_univ x) }, { assume m hm x, rw ← has_fderiv_within_at_univ, exact H.fderiv_within m hm x (mem_univ x) }, { assume m hm, rw continuous_iff_continuous_on_univ, exact H.cont m hm } }, { assume H, split, { exact λ x hx, H.zero_eq x }, { assume m hm x hx, rw has_fderiv_within_at_univ, exact H.fderiv m hm x }, { assume m hm, rw ← continuous_iff_continuous_on_univ, exact H.cont m hm } } end lemma has_ftaylor_series_up_to.has_ftaylor_series_up_to_on {n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) (s : set E) : has_ftaylor_series_up_to_on n f p s := (has_ftaylor_series_up_to_on_univ_iff.2 h).mono (subset_univ _) lemma has_ftaylor_series_up_to.of_le {m n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) (hmn : m ≤ n) : has_ftaylor_series_up_to m f p := by { rw ← has_ftaylor_series_up_to_on_univ_iff at h ⊢, exact h.of_le hmn } lemma has_ftaylor_series_up_to.continuous {n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) : continuous f := begin rw ← has_ftaylor_series_up_to_on_univ_iff at h, rw continuous_iff_continuous_on_univ, exact h.continuous_on end lemma has_ftaylor_series_up_to_zero_iff : has_ftaylor_series_up_to 0 f p ↔ continuous f ∧ (∀ x, (p x 0).uncurry0 = f x) := by simp [has_ftaylor_series_up_to_on_univ_iff.symm, continuous_iff_continuous_on_univ, has_ftaylor_series_up_to_on_zero_iff] /-- If a function has a Taylor series at order at least `1`, then the term of order `1` of this series is a derivative of `f`. -/ lemma has_ftaylor_series_up_to.has_fderiv_at {n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) (hn : 1 ≤ n) (x : E) : has_fderiv_at f (continuous_multilinear_curry_fin1 𝕜 E F (p x 1)) x := begin rw [← has_fderiv_within_at_univ], exact (has_ftaylor_series_up_to_on_univ_iff.2 h).has_fderiv_within_at hn (mem_univ _) end lemma has_ftaylor_series_up_to.differentiable {n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) (hn : 1 ≤ n) : differentiable 𝕜 f := λ x, (h.has_fderiv_at hn x).differentiable_at /-- `p` is a Taylor series of `f` up to `n+1` if and only if `p.shift` is a Taylor series up to `n` for `p 1`, which is a derivative of `f`. -/ theorem has_ftaylor_series_up_to_succ_iff_right {n : ℕ} : has_ftaylor_series_up_to ((n + 1) : ℕ) f p ↔ (∀ x, (p x 0).uncurry0 = f x) ∧ (∀ x, has_fderiv_at (λ y, p y 0) (p x 1).curry_left x) ∧ has_ftaylor_series_up_to n (λ x, continuous_multilinear_curry_fin1 𝕜 E F (p x 1)) (λ x, (p x).shift) := by simp [has_ftaylor_series_up_to_on_succ_iff_right, has_ftaylor_series_up_to_on_univ_iff.symm, -add_comm, -with_zero.coe_add] /-! ### Smooth functions at a point -/ variable (𝕜) /-- A function is continuously differentiable up to `n` at a point `x` if, for any integer `k ≤ n`, there is a neighborhood of `x` where `f` admits derivatives up to order `n`, which are continuous. -/ def times_cont_diff_at (n : with_top ℕ) (f : E → F) (x : E) := times_cont_diff_within_at 𝕜 n f univ x variable {𝕜} theorem times_cont_diff_within_at_univ {n : with_top ℕ} : times_cont_diff_within_at 𝕜 n f univ x ↔ times_cont_diff_at 𝕜 n f x := iff.rfl lemma times_cont_diff_at_top : times_cont_diff_at 𝕜 ∞ f x ↔ ∀ (n : ℕ), times_cont_diff_at 𝕜 n f x := by simp [← times_cont_diff_within_at_univ, times_cont_diff_within_at_top] lemma times_cont_diff_at.times_cont_diff_within_at {n : with_top ℕ} (h : times_cont_diff_at 𝕜 n f x) : times_cont_diff_within_at 𝕜 n f s x := h.mono (subset_univ _) lemma times_cont_diff_within_at.times_cont_diff_at {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) (hx : s ∈ 𝓝 x) : times_cont_diff_at 𝕜 n f x := by rwa [times_cont_diff_at, ← times_cont_diff_within_at_inter hx, univ_inter] lemma times_cont_diff_at.congr_of_eventually_eq {n : with_top ℕ} (h : times_cont_diff_at 𝕜 n f x) (hg : f₁ =ᶠ[𝓝 x] f) : times_cont_diff_at 𝕜 n f₁ x := h.congr_of_eventually_eq' (by rwa nhds_within_univ) (mem_univ x) lemma times_cont_diff_at.of_le {m n : with_top ℕ} (h : times_cont_diff_at 𝕜 n f x) (hmn : m ≤ n) : times_cont_diff_at 𝕜 m f x := h.of_le hmn lemma times_cont_diff_at.continuous_at {n : with_top ℕ} (h : times_cont_diff_at 𝕜 n f x) : continuous_at f x := by simpa [continuous_within_at_univ] using h.continuous_within_at /-- If a function is `C^n` with `n ≥ 1` at a point, then it is differentiable there. -/ lemma times_cont_diff_at.differentiable_at {n : with_top ℕ} (h : times_cont_diff_at 𝕜 n f x) (hn : 1 ≤ n) : differentiable_at 𝕜 f x := by simpa [hn, differentiable_within_at_univ] using h.differentiable_within_at /-- A function is `C^(n + 1)` at a point iff locally, it has a derivative which is `C^n`. -/ theorem times_cont_diff_at_succ_iff_has_fderiv_at {n : ℕ} : times_cont_diff_at 𝕜 ((n + 1) : ℕ) f x ↔ (∃ f' : E → (E →L[𝕜] F), (∃ u ∈ 𝓝 x, (∀ x ∈ u, has_fderiv_at f (f' x) x)) ∧ (times_cont_diff_at 𝕜 n f' x)) := begin rw [← times_cont_diff_within_at_univ, times_cont_diff_within_at_succ_iff_has_fderiv_within_at], simp only [nhds_within_univ, exists_prop, mem_univ, insert_eq_of_mem], split, { rintros ⟨u, H, f', h_fderiv, h_times_cont_diff⟩, rcases mem_nhds_iff.mp H with ⟨t, htu, ht, hxt⟩, refine ⟨f', ⟨t, _⟩, h_times_cont_diff.times_cont_diff_at H⟩, refine ⟨mem_nhds_iff.mpr ⟨t, subset.rfl, ht, hxt⟩, _⟩, intros y hyt, refine (h_fderiv y (htu hyt)).has_fderiv_at _, exact mem_nhds_iff.mpr ⟨t, htu, ht, hyt⟩ }, { rintros ⟨f', ⟨u, H, h_fderiv⟩, h_times_cont_diff⟩, refine ⟨u, H, f', _, h_times_cont_diff.times_cont_diff_within_at⟩, intros x hxu, exact (h_fderiv x hxu).has_fderiv_within_at } end protected theorem times_cont_diff_at.eventually {n : ℕ} (h : times_cont_diff_at 𝕜 n f x) : ∀ᶠ y in 𝓝 x, times_cont_diff_at 𝕜 n f y := by simpa [nhds_within_univ] using h.eventually /-! ### Smooth functions -/ variable (𝕜) /-- A function is continuously differentiable up to `n` if it admits derivatives up to order `n`, which are continuous. Contrary to the case of definitions in domains (where derivatives might not be unique) we do not need to localize the definition in space or time. -/ definition times_cont_diff (n : with_top ℕ) (f : E → F) := ∃ p : E → formal_multilinear_series 𝕜 E F, has_ftaylor_series_up_to n f p variable {𝕜} theorem times_cont_diff_on_univ {n : with_top ℕ} : times_cont_diff_on 𝕜 n f univ ↔ times_cont_diff 𝕜 n f := begin split, { assume H, use ftaylor_series_within 𝕜 f univ, rw ← has_ftaylor_series_up_to_on_univ_iff, exact H.ftaylor_series_within unique_diff_on_univ }, { rintros ⟨p, hp⟩ x hx m hm, exact ⟨univ, filter.univ_sets _, p, (hp.has_ftaylor_series_up_to_on univ).of_le hm⟩ } end lemma times_cont_diff_iff_times_cont_diff_at {n : with_top ℕ} : times_cont_diff 𝕜 n f ↔ ∀ x, times_cont_diff_at 𝕜 n f x := by simp [← times_cont_diff_on_univ, times_cont_diff_on, times_cont_diff_at] lemma times_cont_diff.times_cont_diff_at {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) : times_cont_diff_at 𝕜 n f x := times_cont_diff_iff_times_cont_diff_at.1 h x lemma times_cont_diff.times_cont_diff_within_at {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) : times_cont_diff_within_at 𝕜 n f s x := h.times_cont_diff_at.times_cont_diff_within_at lemma times_cont_diff_top : times_cont_diff 𝕜 ∞ f ↔ ∀ (n : ℕ), times_cont_diff 𝕜 n f := by simp [times_cont_diff_on_univ.symm, times_cont_diff_on_top] lemma times_cont_diff_all_iff_nat : (∀ n, times_cont_diff 𝕜 n f) ↔ (∀ n : ℕ, times_cont_diff 𝕜 n f) := by simp only [← times_cont_diff_on_univ, times_cont_diff_on_all_iff_nat] lemma times_cont_diff.times_cont_diff_on {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) : times_cont_diff_on 𝕜 n f s := (times_cont_diff_on_univ.2 h).mono (subset_univ _) @[simp] lemma times_cont_diff_zero : times_cont_diff 𝕜 0 f ↔ continuous f := begin rw [← times_cont_diff_on_univ, continuous_iff_continuous_on_univ], exact times_cont_diff_on_zero end lemma times_cont_diff_at_zero : times_cont_diff_at 𝕜 0 f x ↔ ∃ u ∈ 𝓝 x, continuous_on f u := by { rw ← times_cont_diff_within_at_univ, simp [times_cont_diff_within_at_zero, nhds_within_univ] } lemma times_cont_diff.of_le {m n : with_top ℕ} (h : times_cont_diff 𝕜 n f) (hmn : m ≤ n) : times_cont_diff 𝕜 m f := times_cont_diff_on_univ.1 $ (times_cont_diff_on_univ.2 h).of_le hmn lemma times_cont_diff.continuous {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) : continuous f := times_cont_diff_zero.1 (h.of_le bot_le) /-- If a function is `C^n` with `n ≥ 1`, then it is differentiable. -/ lemma times_cont_diff.differentiable {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) (hn : 1 ≤ n) : differentiable 𝕜 f := differentiable_on_univ.1 $ (times_cont_diff_on_univ.2 h).differentiable_on hn /-! ### Iterated derivative -/ variable (𝕜) /-- The `n`-th derivative of a function, as a multilinear map, defined inductively. -/ noncomputable def iterated_fderiv (n : ℕ) (f : E → F) : E → (E [×n]→L[𝕜] F) := nat.rec_on n (λ x, continuous_multilinear_map.curry0 𝕜 E (f x)) (λ n rec x, continuous_linear_map.uncurry_left (fderiv 𝕜 rec x)) /-- Formal Taylor series associated to a function within a set. -/ def ftaylor_series (f : E → F) (x : E) : formal_multilinear_series 𝕜 E F := λ n, iterated_fderiv 𝕜 n f x variable {𝕜} @[simp] lemma iterated_fderiv_zero_apply (m : (fin 0) → E) : (iterated_fderiv 𝕜 0 f x : ((fin 0) → E) → F) m = f x := rfl lemma iterated_fderiv_zero_eq_comp : iterated_fderiv 𝕜 0 f = (continuous_multilinear_curry_fin0 𝕜 E F).symm ∘ f := rfl lemma iterated_fderiv_succ_apply_left {n : ℕ} (m : fin (n + 1) → E): (iterated_fderiv 𝕜 (n + 1) f x : (fin (n + 1) → E) → F) m = (fderiv 𝕜 (iterated_fderiv 𝕜 n f) x : E → (E [×n]→L[𝕜] F)) (m 0) (tail m) := rfl /-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv, and the derivative of the `n`-th derivative. -/ lemma iterated_fderiv_succ_eq_comp_left {n : ℕ} : iterated_fderiv 𝕜 (n + 1) f = (continuous_multilinear_curry_left_equiv 𝕜 (λ(i : fin (n + 1)), E) F) ∘ (fderiv 𝕜 (iterated_fderiv 𝕜 n f)) := rfl lemma iterated_fderiv_within_univ {n : ℕ} : iterated_fderiv_within 𝕜 n f univ = iterated_fderiv 𝕜 n f := begin induction n with n IH, { ext x, simp }, { ext x m, rw [iterated_fderiv_succ_apply_left, iterated_fderiv_within_succ_apply_left, IH, fderiv_within_univ] } end lemma ftaylor_series_within_univ : ftaylor_series_within 𝕜 f univ = ftaylor_series 𝕜 f := begin ext1 x, ext1 n, change iterated_fderiv_within 𝕜 n f univ x = iterated_fderiv 𝕜 n f x, rw iterated_fderiv_within_univ end theorem iterated_fderiv_succ_apply_right {n : ℕ} (m : fin (n + 1) → E) : (iterated_fderiv 𝕜 (n + 1) f x : (fin (n + 1) → E) → F) m = iterated_fderiv 𝕜 n (λy, fderiv 𝕜 f y) x (init m) (m (last n)) := begin rw [← iterated_fderiv_within_univ, ← iterated_fderiv_within_univ, ← fderiv_within_univ], exact iterated_fderiv_within_succ_apply_right unique_diff_on_univ (mem_univ _) _ end /-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv, and the `n`-th derivative of the derivative. -/ lemma iterated_fderiv_succ_eq_comp_right {n : ℕ} : iterated_fderiv 𝕜 (n + 1) f x = ((continuous_multilinear_curry_right_equiv' 𝕜 n E F) ∘ (iterated_fderiv 𝕜 n (λy, fderiv 𝕜 f y))) x := by { ext m, rw iterated_fderiv_succ_apply_right, refl } @[simp] lemma iterated_fderiv_one_apply (m : (fin 1) → E) : (iterated_fderiv 𝕜 1 f x : ((fin 1) → E) → F) m = (fderiv 𝕜 f x : E → F) (m 0) := by { rw [iterated_fderiv_succ_apply_right, iterated_fderiv_zero_apply], refl } /-- When a function is `C^n` in a set `s` of unique differentiability, it admits `ftaylor_series_within 𝕜 f s` as a Taylor series up to order `n` in `s`. -/ theorem times_cont_diff_on_iff_ftaylor_series {n : with_top ℕ} : times_cont_diff 𝕜 n f ↔ has_ftaylor_series_up_to n f (ftaylor_series 𝕜 f) := begin split, { rw [← times_cont_diff_on_univ, ← has_ftaylor_series_up_to_on_univ_iff, ← ftaylor_series_within_univ], exact λ h, times_cont_diff_on.ftaylor_series_within h unique_diff_on_univ }, { assume h, exact ⟨ftaylor_series 𝕜 f, h⟩ } end lemma times_cont_diff_iff_continuous_differentiable {n : with_top ℕ} : times_cont_diff 𝕜 n f ↔ (∀ (m : ℕ), (m : with_top ℕ) ≤ n → continuous (λ x, iterated_fderiv 𝕜 m f x)) ∧ (∀ (m : ℕ), (m : with_top ℕ) < n → differentiable 𝕜 (λ x, iterated_fderiv 𝕜 m f x)) := by simp [times_cont_diff_on_univ.symm, continuous_iff_continuous_on_univ, differentiable_on_univ.symm, iterated_fderiv_within_univ, times_cont_diff_on_iff_continuous_on_differentiable_on unique_diff_on_univ] lemma times_cont_diff_of_differentiable_iterated_fderiv {n : with_top ℕ} (h : ∀(m : ℕ), (m : with_top ℕ) ≤ n → differentiable 𝕜 (iterated_fderiv 𝕜 m f)) : times_cont_diff 𝕜 n f := times_cont_diff_iff_continuous_differentiable.2 ⟨λ m hm, (h m hm).continuous, λ m hm, (h m (le_of_lt hm))⟩ /-- A function is `C^(n + 1)` on a domain with unique derivatives if and only if it is differentiable there, and its derivative is `C^n`. -/ theorem times_cont_diff_succ_iff_fderiv {n : ℕ} : times_cont_diff 𝕜 ((n + 1) : ℕ) f ↔ differentiable 𝕜 f ∧ times_cont_diff 𝕜 n (λ y, fderiv 𝕜 f y) := by simp [times_cont_diff_on_univ.symm, differentiable_on_univ.symm, fderiv_within_univ.symm, - fderiv_within_univ, times_cont_diff_on_succ_iff_fderiv_within unique_diff_on_univ, -with_zero.coe_add, -add_comm] /-- A function is `C^∞` on a domain with unique derivatives if and only if it is differentiable there, and its derivative is `C^∞`. -/ theorem times_cont_diff_top_iff_fderiv : times_cont_diff 𝕜 ∞ f ↔ differentiable 𝕜 f ∧ times_cont_diff 𝕜 ∞ (λ y, fderiv 𝕜 f y) := begin simp [times_cont_diff_on_univ.symm, differentiable_on_univ.symm, fderiv_within_univ.symm, - fderiv_within_univ], rw times_cont_diff_on_top_iff_fderiv_within unique_diff_on_univ, end lemma times_cont_diff.continuous_fderiv {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) (hn : 1 ≤ n) : continuous (λ x, fderiv 𝕜 f x) := ((times_cont_diff_succ_iff_fderiv).1 (h.of_le hn)).2.continuous /-- If a function is at least `C^1`, its bundled derivative (mapping `(x, v)` to `Df(x) v`) is continuous. -/ lemma times_cont_diff.continuous_fderiv_apply {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) (hn : 1 ≤ n) : continuous (λp : E × E, (fderiv 𝕜 f p.1 : E → F) p.2) := begin have A : continuous (λq : (E →L[𝕜] F) × E, q.1 q.2) := is_bounded_bilinear_map_apply.continuous, have B : continuous (λp : E × E, (fderiv 𝕜 f p.1, p.2)), { apply continuous.prod_mk _ continuous_snd, exact continuous.comp (h.continuous_fderiv hn) continuous_fst }, exact A.comp B end /-! ### Constants -/ lemma iterated_fderiv_within_zero_fun {n : ℕ} : iterated_fderiv 𝕜 n (λ x : E, (0 : F)) = 0 := begin induction n with n IH, { ext m, simp }, { ext x m, rw [iterated_fderiv_succ_apply_left, IH], change (fderiv 𝕜 (λ (x : E), (0 : (E [×n]→L[𝕜] F))) x : E → (E [×n]→L[𝕜] F)) (m 0) (tail m) = _, rw fderiv_const, refl } end lemma times_cont_diff_zero_fun {n : with_top ℕ} : times_cont_diff 𝕜 n (λ x : E, (0 : F)) := begin apply times_cont_diff_of_differentiable_iterated_fderiv (λm hm, _), rw iterated_fderiv_within_zero_fun, apply differentiable_const (0 : (E [×m]→L[𝕜] F)) end /-- Constants are `C^∞`. -/ lemma times_cont_diff_const {n : with_top ℕ} {c : F} : times_cont_diff 𝕜 n (λx : E, c) := begin suffices h : times_cont_diff 𝕜 ∞ (λx : E, c), by exact h.of_le le_top, rw times_cont_diff_top_iff_fderiv, refine ⟨differentiable_const c, _⟩, rw fderiv_const, exact times_cont_diff_zero_fun end lemma times_cont_diff_on_const {n : with_top ℕ} {c : F} {s : set E} : times_cont_diff_on 𝕜 n (λx : E, c) s := times_cont_diff_const.times_cont_diff_on lemma times_cont_diff_at_const {n : with_top ℕ} {c : F} : times_cont_diff_at 𝕜 n (λx : E, c) x := times_cont_diff_const.times_cont_diff_at lemma times_cont_diff_within_at_const {n : with_top ℕ} {c : F} : times_cont_diff_within_at 𝕜 n (λx : E, c) s x := times_cont_diff_at_const.times_cont_diff_within_at @[nontriviality] lemma times_cont_diff_of_subsingleton [subsingleton F] {n : with_top ℕ} : times_cont_diff 𝕜 n f := by { rw [subsingleton.elim f (λ _, 0)], exact times_cont_diff_const } @[nontriviality] lemma times_cont_diff_at_of_subsingleton [subsingleton F] {n : with_top ℕ} : times_cont_diff_at 𝕜 n f x := by { rw [subsingleton.elim f (λ _, 0)], exact times_cont_diff_at_const } @[nontriviality] lemma times_cont_diff_within_at_of_subsingleton [subsingleton F] {n : with_top ℕ} : times_cont_diff_within_at 𝕜 n f s x := by { rw [subsingleton.elim f (λ _, 0)], exact times_cont_diff_within_at_const } @[nontriviality] lemma times_cont_diff_on_of_subsingleton [subsingleton F] {n : with_top ℕ} : times_cont_diff_on 𝕜 n f s := by { rw [subsingleton.elim f (λ _, 0)], exact times_cont_diff_on_const } /-! ### Linear functions -/ /-- Unbundled bounded linear functions are `C^∞`. -/ lemma is_bounded_linear_map.times_cont_diff {n : with_top ℕ} (hf : is_bounded_linear_map 𝕜 f) : times_cont_diff 𝕜 n f := begin suffices h : times_cont_diff 𝕜 ∞ f, by exact h.of_le le_top, rw times_cont_diff_top_iff_fderiv, refine ⟨hf.differentiable, _⟩, simp [hf.fderiv], exact times_cont_diff_const end lemma continuous_linear_map.times_cont_diff {n : with_top ℕ} (f : E →L[𝕜] F) : times_cont_diff 𝕜 n f := f.is_bounded_linear_map.times_cont_diff lemma continuous_linear_equiv.times_cont_diff {n : with_top ℕ} (f : E ≃L[𝕜] F) : times_cont_diff 𝕜 n f := (f : E →L[𝕜] F).times_cont_diff lemma linear_isometry_map.times_cont_diff {n : with_top ℕ} (f : E →ₗᵢ[𝕜] F) : times_cont_diff 𝕜 n f := f.to_continuous_linear_map.times_cont_diff lemma linear_isometry_equiv.times_cont_diff {n : with_top ℕ} (f : E ≃ₗᵢ[𝕜] F) : times_cont_diff 𝕜 n f := (f : E →L[𝕜] F).times_cont_diff /-- The first projection in a product is `C^∞`. -/ lemma times_cont_diff_fst {n : with_top ℕ} : times_cont_diff 𝕜 n (prod.fst : E × F → E) := is_bounded_linear_map.times_cont_diff is_bounded_linear_map.fst /-- The first projection on a domain in a product is `C^∞`. -/ lemma times_cont_diff_on_fst {s : set (E×F)} {n : with_top ℕ} : times_cont_diff_on 𝕜 n (prod.fst : E × F → E) s := times_cont_diff.times_cont_diff_on times_cont_diff_fst /-- The first projection at a point in a product is `C^∞`. -/ lemma times_cont_diff_at_fst {p : E × F} {n : with_top ℕ} : times_cont_diff_at 𝕜 n (prod.fst : E × F → E) p := times_cont_diff_fst.times_cont_diff_at /-- The first projection within a domain at a point in a product is `C^∞`. -/ lemma times_cont_diff_within_at_fst {s : set (E × F)} {p : E × F} {n : with_top ℕ} : times_cont_diff_within_at 𝕜 n (prod.fst : E × F → E) s p := times_cont_diff_fst.times_cont_diff_within_at /-- The second projection in a product is `C^∞`. -/ lemma times_cont_diff_snd {n : with_top ℕ} : times_cont_diff 𝕜 n (prod.snd : E × F → F) := is_bounded_linear_map.times_cont_diff is_bounded_linear_map.snd /-- The second projection on a domain in a product is `C^∞`. -/ lemma times_cont_diff_on_snd {s : set (E×F)} {n : with_top ℕ} : times_cont_diff_on 𝕜 n (prod.snd : E × F → F) s := times_cont_diff.times_cont_diff_on times_cont_diff_snd /-- The second projection at a point in a product is `C^∞`. -/ lemma times_cont_diff_at_snd {p : E × F} {n : with_top ℕ} : times_cont_diff_at 𝕜 n (prod.snd : E × F → F) p := times_cont_diff_snd.times_cont_diff_at /-- The second projection within a domain at a point in a product is `C^∞`. -/ lemma times_cont_diff_within_at_snd {s : set (E × F)} {p : E × F} {n : with_top ℕ} : times_cont_diff_within_at 𝕜 n (prod.snd : E × F → F) s p := times_cont_diff_snd.times_cont_diff_within_at /-- The identity is `C^∞`. -/ lemma times_cont_diff_id {n : with_top ℕ} : times_cont_diff 𝕜 n (id : E → E) := is_bounded_linear_map.id.times_cont_diff lemma times_cont_diff_within_at_id {n : with_top ℕ} {s x} : times_cont_diff_within_at 𝕜 n (id : E → E) s x := times_cont_diff_id.times_cont_diff_within_at lemma times_cont_diff_at_id {n : with_top ℕ} {x} : times_cont_diff_at 𝕜 n (id : E → E) x := times_cont_diff_id.times_cont_diff_at lemma times_cont_diff_on_id {n : with_top ℕ} {s} : times_cont_diff_on 𝕜 n (id : E → E) s := times_cont_diff_id.times_cont_diff_on /-- Bilinear functions are `C^∞`. -/ lemma is_bounded_bilinear_map.times_cont_diff {n : with_top ℕ} (hb : is_bounded_bilinear_map 𝕜 b) : times_cont_diff 𝕜 n b := begin suffices h : times_cont_diff 𝕜 ∞ b, by exact h.of_le le_top, rw times_cont_diff_top_iff_fderiv, refine ⟨hb.differentiable, _⟩, simp [hb.fderiv], exact hb.is_bounded_linear_map_deriv.times_cont_diff end /-- If `f` admits a Taylor series `p` in a set `s`, and `g` is linear, then `g ∘ f` admits a Taylor series whose `k`-th term is given by `g ∘ (p k)`. -/ lemma has_ftaylor_series_up_to_on.continuous_linear_map_comp {n : with_top ℕ} (g : F →L[𝕜] G) (hf : has_ftaylor_series_up_to_on n f p s) : has_ftaylor_series_up_to_on n (g ∘ f) (λ x k, g.comp_continuous_multilinear_map (p x k)) s := begin set L : Π m : ℕ, (E [×m]→L[𝕜] F) →L[𝕜] (E [×m]→L[𝕜] G) := λ m, continuous_linear_map.comp_continuous_multilinear_mapL g, split, { exact λ x hx, congr_arg g (hf.zero_eq x hx) }, { intros m hm x hx, convert (L m).has_fderiv_at.comp_has_fderiv_within_at x (hf.fderiv_within m hm x hx) }, { intros m hm, convert (L m).continuous.comp_continuous_on (hf.cont m hm) } end /-- Composition by continuous linear maps on the left preserves `C^n` functions in a domain at a point. -/ lemma times_cont_diff_within_at.continuous_linear_map_comp {n : with_top ℕ} (g : F →L[𝕜] G) (hf : times_cont_diff_within_at 𝕜 n f s x) : times_cont_diff_within_at 𝕜 n (g ∘ f) s x := begin assume m hm, rcases hf m hm with ⟨u, hu, p, hp⟩, exact ⟨u, hu, _, hp.continuous_linear_map_comp g⟩, end /-- Composition by continuous linear maps on the left preserves `C^n` functions in a domain at a point. -/ lemma times_cont_diff_at.continuous_linear_map_comp {n : with_top ℕ} (g : F →L[𝕜] G) (hf : times_cont_diff_at 𝕜 n f x) : times_cont_diff_at 𝕜 n (g ∘ f) x := times_cont_diff_within_at.continuous_linear_map_comp g hf /-- Composition by continuous linear maps on the left preserves `C^n` functions on domains. -/ lemma times_cont_diff_on.continuous_linear_map_comp {n : with_top ℕ} (g : F →L[𝕜] G) (hf : times_cont_diff_on 𝕜 n f s) : times_cont_diff_on 𝕜 n (g ∘ f) s := λ x hx, (hf x hx).continuous_linear_map_comp g /-- Composition by continuous linear maps on the left preserves `C^n` functions. -/ lemma times_cont_diff.continuous_linear_map_comp {n : with_top ℕ} {f : E → F} (g : F →L[𝕜] G) (hf : times_cont_diff 𝕜 n f) : times_cont_diff 𝕜 n (λx, g (f x)) := times_cont_diff_on_univ.1 $ times_cont_diff_on.continuous_linear_map_comp _ (times_cont_diff_on_univ.2 hf) /-- Composition by continuous linear equivs on the left respects higher differentiability on domains. -/ lemma continuous_linear_equiv.comp_times_cont_diff_within_at_iff {n : with_top ℕ} (e : F ≃L[𝕜] G) : times_cont_diff_within_at 𝕜 n (e ∘ f) s x ↔ times_cont_diff_within_at 𝕜 n f s x := ⟨λ H, by simpa only [(∘), e.symm.coe_coe, e.symm_apply_apply] using H.continuous_linear_map_comp (e.symm : G →L[𝕜] F), λ H, H.continuous_linear_map_comp (e : F →L[𝕜] G)⟩ /-- Composition by continuous linear equivs on the left respects higher differentiability on domains. -/ lemma continuous_linear_equiv.comp_times_cont_diff_on_iff {n : with_top ℕ} (e : F ≃L[𝕜] G) : times_cont_diff_on 𝕜 n (e ∘ f) s ↔ times_cont_diff_on 𝕜 n f s := by simp [times_cont_diff_on, e.comp_times_cont_diff_within_at_iff] /-- If `f` admits a Taylor series `p` in a set `s`, and `g` is linear, then `f ∘ g` admits a Taylor series in `g ⁻¹' s`, whose `k`-th term is given by `p k (g v₁, ..., g vₖ)` . -/ lemma has_ftaylor_series_up_to_on.comp_continuous_linear_map {n : with_top ℕ} (hf : has_ftaylor_series_up_to_on n f p s) (g : G →L[𝕜] E) : has_ftaylor_series_up_to_on n (f ∘ g) (λ x k, (p (g x) k).comp_continuous_linear_map (λ _, g)) (g ⁻¹' s) := begin let A : Π m : ℕ, (E [×m]→L[𝕜] F) → (G [×m]→L[𝕜] F) := λ m h, h.comp_continuous_linear_map (λ _, g), have hA : ∀ m, is_bounded_linear_map 𝕜 (A m) := λ m, is_bounded_linear_map_continuous_multilinear_map_comp_linear g, split, { assume x hx, simp only [(hf.zero_eq (g x) hx).symm, function.comp_app], change p (g x) 0 (λ (i : fin 0), g 0) = p (g x) 0 0, rw continuous_linear_map.map_zero, refl }, { assume m hm x hx, convert ((hA m).has_fderiv_at).comp_has_fderiv_within_at x ((hf.fderiv_within m hm (g x) hx).comp x (g.has_fderiv_within_at) (subset.refl _)), ext y v, change p (g x) (nat.succ m) (g ∘ (cons y v)) = p (g x) m.succ (cons (g y) (g ∘ v)), rw comp_cons }, { assume m hm, exact (hA m).continuous.comp_continuous_on ((hf.cont m hm).comp g.continuous.continuous_on (subset.refl _)) } end /-- Composition by continuous linear maps on the right preserves `C^n` functions at a point on a domain. -/ lemma times_cont_diff_within_at.comp_continuous_linear_map {n : with_top ℕ} {x : G} (g : G →L[𝕜] E) (hf : times_cont_diff_within_at 𝕜 n f s (g x)) : times_cont_diff_within_at 𝕜 n (f ∘ g) (g ⁻¹' s) x := begin assume m hm, rcases hf m hm with ⟨u, hu, p, hp⟩, refine ⟨g ⁻¹' u, _, _, hp.comp_continuous_linear_map g⟩, apply continuous_within_at.preimage_mem_nhds_within', { exact g.continuous.continuous_within_at }, { apply nhds_within_mono (g x) _ hu, rw image_insert_eq, exact insert_subset_insert (image_preimage_subset g s) } end /-- Composition by continuous linear maps on the right preserves `C^n` functions on domains. -/ lemma times_cont_diff_on.comp_continuous_linear_map {n : with_top ℕ} (hf : times_cont_diff_on 𝕜 n f s) (g : G →L[𝕜] E) : times_cont_diff_on 𝕜 n (f ∘ g) (g ⁻¹' s) := λ x hx, (hf (g x) hx).comp_continuous_linear_map g /-- Composition by continuous linear maps on the right preserves `C^n` functions. -/ lemma times_cont_diff.comp_continuous_linear_map {n : with_top ℕ} {f : E → F} {g : G →L[𝕜] E} (hf : times_cont_diff 𝕜 n f) : times_cont_diff 𝕜 n (f ∘ g) := times_cont_diff_on_univ.1 $ times_cont_diff_on.comp_continuous_linear_map (times_cont_diff_on_univ.2 hf) _ /-- Composition by continuous linear equivs on the right respects higher differentiability at a point in a domain. -/ lemma continuous_linear_equiv.times_cont_diff_within_at_comp_iff {n : with_top ℕ} (e : G ≃L[𝕜] E) : times_cont_diff_within_at 𝕜 n (f ∘ e) (e ⁻¹' s) (e.symm x) ↔ times_cont_diff_within_at 𝕜 n f s x := begin split, { assume H, simpa [← preimage_comp, (∘)] using H.comp_continuous_linear_map (e.symm : E →L[𝕜] G) }, { assume H, rw [← e.apply_symm_apply x, ← e.coe_coe] at H, exact H.comp_continuous_linear_map _ }, end /-- Composition by continuous linear equivs on the right respects higher differentiability on domains. -/ lemma continuous_linear_equiv.times_cont_diff_on_comp_iff {n : with_top ℕ} (e : G ≃L[𝕜] E) : times_cont_diff_on 𝕜 n (f ∘ e) (e ⁻¹' s) ↔ times_cont_diff_on 𝕜 n f s := begin refine ⟨λ H, _, λ H, H.comp_continuous_linear_map (e : G →L[𝕜] E)⟩, have A : f = (f ∘ e) ∘ e.symm, by { ext y, simp only [function.comp_app], rw e.apply_symm_apply y }, have B : e.symm ⁻¹' (e ⁻¹' s) = s, by { rw [← preimage_comp, e.self_comp_symm], refl }, rw [A, ← B], exact H.comp_continuous_linear_map (e.symm : E →L[𝕜] G) end /-- If two functions `f` and `g` admit Taylor series `p` and `q` in a set `s`, then the cartesian product of `f` and `g` admits the cartesian product of `p` and `q` as a Taylor series. -/ lemma has_ftaylor_series_up_to_on.prod {n : with_top ℕ} (hf : has_ftaylor_series_up_to_on n f p s) {g : E → G} {q : E → formal_multilinear_series 𝕜 E G} (hg : has_ftaylor_series_up_to_on n g q s) : has_ftaylor_series_up_to_on n (λ y, (f y, g y)) (λ y k, (p y k).prod (q y k)) s := begin set L := λ m, continuous_multilinear_map.prodL 𝕜 (λ i : fin m, E) F G, split, { assume x hx, rw [← hf.zero_eq x hx, ← hg.zero_eq x hx], refl }, { assume m hm x hx, convert (L m).has_fderiv_at.comp_has_fderiv_within_at x ((hf.fderiv_within m hm x hx).prod (hg.fderiv_within m hm x hx)) }, { assume m hm, exact (L m).continuous.comp_continuous_on ((hf.cont m hm).prod (hg.cont m hm)) } end /-- The cartesian product of `C^n` functions at a point in a domain is `C^n`. -/ lemma times_cont_diff_within_at.prod {n : with_top ℕ} {s : set E} {f : E → F} {g : E → G} (hf : times_cont_diff_within_at 𝕜 n f s x) (hg : times_cont_diff_within_at 𝕜 n g s x) : times_cont_diff_within_at 𝕜 n (λx:E, (f x, g x)) s x := begin assume m hm, rcases hf m hm with ⟨u, hu, p, hp⟩, rcases hg m hm with ⟨v, hv, q, hq⟩, exact ⟨u ∩ v, filter.inter_mem hu hv, _, (hp.mono (inter_subset_left u v)).prod (hq.mono (inter_subset_right u v))⟩ end /-- The cartesian product of `C^n` functions on domains is `C^n`. -/ lemma times_cont_diff_on.prod {n : with_top ℕ} {s : set E} {f : E → F} {g : E → G} (hf : times_cont_diff_on 𝕜 n f s) (hg : times_cont_diff_on 𝕜 n g s) : times_cont_diff_on 𝕜 n (λx:E, (f x, g x)) s := λ x hx, (hf x hx).prod (hg x hx) /-- The cartesian product of `C^n` functions at a point is `C^n`. -/ lemma times_cont_diff_at.prod {n : with_top ℕ} {f : E → F} {g : E → G} (hf : times_cont_diff_at 𝕜 n f x) (hg : times_cont_diff_at 𝕜 n g x) : times_cont_diff_at 𝕜 n (λx:E, (f x, g x)) x := times_cont_diff_within_at_univ.1 $ times_cont_diff_within_at.prod (times_cont_diff_within_at_univ.2 hf) (times_cont_diff_within_at_univ.2 hg) /-- The cartesian product of `C^n` functions is `C^n`. -/ lemma times_cont_diff.prod {n : with_top ℕ} {f : E → F} {g : E → G} (hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) : times_cont_diff 𝕜 n (λx:E, (f x, g x)) := times_cont_diff_on_univ.1 $ times_cont_diff_on.prod (times_cont_diff_on_univ.2 hf) (times_cont_diff_on_univ.2 hg) /-! ### Smoothness of functions `f : E → Π i, F' i` -/ section pi variables {ι : Type*} [fintype ι] {F' : ι → Type*} [Π i, normed_group (F' i)] [Π i, normed_space 𝕜 (F' i)] {φ : Π i, E → F' i} {p' : Π i, E → formal_multilinear_series 𝕜 E (F' i)} {Φ : E → Π i, F' i} {P' : E → formal_multilinear_series 𝕜 E (Π i, F' i)} {n : with_top ℕ} lemma has_ftaylor_series_up_to_on_pi : has_ftaylor_series_up_to_on n (λ x i, φ i x) (λ x m, continuous_multilinear_map.pi (λ i, p' i x m)) s ↔ ∀ i, has_ftaylor_series_up_to_on n (φ i) (p' i) s := begin set pr := @continuous_linear_map.proj 𝕜 _ ι F' _ _ _, letI : Π (m : ℕ) (i : ι), normed_space 𝕜 (E [×m]→L[𝕜] (F' i)) := λ m i, infer_instance, set L : Π m : ℕ, (Π i, E [×m]→L[𝕜] (F' i)) ≃ₗᵢ[𝕜] (E [×m]→L[𝕜] (Π i, F' i)) := λ m, continuous_multilinear_map.piₗᵢ _ _, refine ⟨λ h i, _, λ h, ⟨λ x hx, _, _, _⟩⟩, { convert h.continuous_linear_map_comp (pr i), ext, refl }, { ext1 i, exact (h i).zero_eq x hx }, { intros m hm x hx, have := has_fderiv_within_at_pi.2 (λ i, (h i).fderiv_within m hm x hx), convert (L m).has_fderiv_at.comp_has_fderiv_within_at x this }, { intros m hm, have := continuous_on_pi.2 (λ i, (h i).cont m hm), convert (L m).continuous.comp_continuous_on this } end @[simp] lemma has_ftaylor_series_up_to_on_pi' : has_ftaylor_series_up_to_on n Φ P' s ↔ ∀ i, has_ftaylor_series_up_to_on n (λ x, Φ x i) (λ x m, (@continuous_linear_map.proj 𝕜 _ ι F' _ _ _ i).comp_continuous_multilinear_map (P' x m)) s := by { convert has_ftaylor_series_up_to_on_pi, ext, refl } lemma times_cont_diff_within_at_pi : times_cont_diff_within_at 𝕜 n Φ s x ↔ ∀ i, times_cont_diff_within_at 𝕜 n (λ x, Φ x i) s x := begin set pr := @continuous_linear_map.proj 𝕜 _ ι F' _ _ _, refine ⟨λ h i, h.continuous_linear_map_comp (pr i), λ h m hm, _⟩, choose u hux p hp using λ i, h i m hm, exact ⟨⋂ i, u i, filter.Inter_mem.2 hux, _, has_ftaylor_series_up_to_on_pi.2 (λ i, (hp i).mono $ Inter_subset _ _)⟩, end lemma times_cont_diff_on_pi : times_cont_diff_on 𝕜 n Φ s ↔ ∀ i, times_cont_diff_on 𝕜 n (λ x, Φ x i) s := ⟨λ h i x hx, times_cont_diff_within_at_pi.1 (h x hx) _, λ h x hx, times_cont_diff_within_at_pi.2 (λ i, h i x hx)⟩ lemma times_cont_diff_at_pi : times_cont_diff_at 𝕜 n Φ x ↔ ∀ i, times_cont_diff_at 𝕜 n (λ x, Φ x i) x := times_cont_diff_within_at_pi lemma times_cont_diff_pi : times_cont_diff 𝕜 n Φ ↔ ∀ i, times_cont_diff 𝕜 n (λ x, Φ x i) := by simp only [← times_cont_diff_on_univ, times_cont_diff_on_pi] end pi /-! ### Composition of `C^n` functions We show that the composition of `C^n` functions is `C^n`. One way to prove it would be to write the `n`-th derivative of the composition (this is Faà di Bruno's formula) and check its continuity, but this is very painful. Instead, we go for a simple inductive proof. Assume it is done for `n`. Then, to check it for `n+1`, one needs to check that the derivative of `g ∘ f` is `C^n`, i.e., that `Dg(f x) ⬝ Df(x)` is `C^n`. The term `Dg (f x)` is the composition of two `C^n` functions, so it is `C^n` by the inductive assumption. The term `Df(x)` is also `C^n`. Then, the matrix multiplication is the application of a bilinear map (which is `C^∞`, and therefore `C^n`) to `x ↦ (Dg(f x), Df x)`. As the composition of two `C^n` maps, it is again `C^n`, and we are done. There is a subtlety in this argument: we apply the inductive assumption to functions on other Banach spaces. In maths, one would say: prove by induction over `n` that, for all `C^n` maps between all pairs of Banach spaces, their composition is `C^n`. In Lean, this is fine as long as the spaces stay in the same universe. This is not the case in the above argument: if `E` lives in universe `u` and `F` lives in universe `v`, then linear maps from `E` to `F` (to which the derivative of `f` belongs) is in universe `max u v`. If one could quantify over finitely many universes, the above proof would work fine, but this is not the case. One could still write the proof considering spaces in any universe in `u, v, w, max u v, max v w, max u v w`, but it would be extremely tedious and lead to a lot of duplication. Instead, we formulate the above proof when all spaces live in the same universe (where everything is fine), and then we deduce the general result by lifting all our spaces to a common universe. We use the trick that any space `H` is isomorphic through a continuous linear equiv to `continuous_multilinear_map (λ (i : fin 0), E × F × G) H` to change the universe level, and then argue that composing with such a linear equiv does not change the fact of being `C^n`, which we have already proved previously. -/ /-- Auxiliary lemma proving that the composition of `C^n` functions on domains is `C^n` when all spaces live in the same universe. Use instead `times_cont_diff_on.comp` which removes the universe assumption (but is deduced from this one). -/ private lemma times_cont_diff_on.comp_same_univ {Eu : Type u} [normed_group Eu] [normed_space 𝕜 Eu] {Fu : Type u} [normed_group Fu] [normed_space 𝕜 Fu] {Gu : Type u} [normed_group Gu] [normed_space 𝕜 Gu] {n : with_top ℕ} {s : set Eu} {t : set Fu} {g : Fu → Gu} {f : Eu → Fu} (hg : times_cont_diff_on 𝕜 n g t) (hf : times_cont_diff_on 𝕜 n f s) (st : s ⊆ f ⁻¹' t) : times_cont_diff_on 𝕜 n (g ∘ f) s := begin unfreezingI { induction n using with_top.nat_induction with n IH Itop generalizing Eu Fu Gu }, { rw times_cont_diff_on_zero at hf hg ⊢, exact continuous_on.comp hg hf st }, { rw times_cont_diff_on_succ_iff_has_fderiv_within_at at hg ⊢, assume x hx, rcases (times_cont_diff_on_succ_iff_has_fderiv_within_at.1 hf) x hx with ⟨u, hu, f', hf', f'_diff⟩, rcases hg (f x) (st hx) with ⟨v, hv, g', hg', g'_diff⟩, rw insert_eq_of_mem hx at hu ⊢, have xu : x ∈ u := mem_of_mem_nhds_within hx hu, let w := s ∩ (u ∩ f⁻¹' v), have wv : w ⊆ f ⁻¹' v := λ y hy, hy.2.2, have wu : w ⊆ u := λ y hy, hy.2.1, have ws : w ⊆ s := λ y hy, hy.1, refine ⟨w, _, λ y, (g' (f y)).comp (f' y), _, _⟩, show w ∈ 𝓝[s] x, { apply filter.inter_mem self_mem_nhds_within, apply filter.inter_mem hu, apply continuous_within_at.preimage_mem_nhds_within', { rw ← continuous_within_at_inter' hu, exact (hf' x xu).differentiable_within_at.continuous_within_at.mono (inter_subset_right _ _) }, { apply nhds_within_mono _ _ hv, exact subset.trans (image_subset_iff.mpr st) (subset_insert (f x) t) } }, show ∀ y ∈ w, has_fderiv_within_at (g ∘ f) ((g' (f y)).comp (f' y)) w y, { rintros y ⟨ys, yu, yv⟩, exact (hg' (f y) yv).comp y ((hf' y yu).mono wu) wv }, show times_cont_diff_on 𝕜 n (λ y, (g' (f y)).comp (f' y)) w, { have A : times_cont_diff_on 𝕜 n (λ y, g' (f y)) w := IH g'_diff ((hf.of_le (with_top.coe_le_coe.2 (nat.le_succ n))).mono ws) wv, have B : times_cont_diff_on 𝕜 n f' w := f'_diff.mono wu, have C : times_cont_diff_on 𝕜 n (λ y, (f' y, g' (f y))) w := times_cont_diff_on.prod B A, have D : times_cont_diff_on 𝕜 n (λ(p : (Eu →L[𝕜] Fu) × (Fu →L[𝕜] Gu)), p.2.comp p.1) univ := is_bounded_bilinear_map_comp.times_cont_diff.times_cont_diff_on, exact IH D C (subset_univ _) } }, { rw times_cont_diff_on_top at hf hg ⊢, assume n, apply Itop n (hg n) (hf n) st } end /-- The composition of `C^n` functions on domains is `C^n`. -/ lemma times_cont_diff_on.comp {n : with_top ℕ} {s : set E} {t : set F} {g : F → G} {f : E → F} (hg : times_cont_diff_on 𝕜 n g t) (hf : times_cont_diff_on 𝕜 n f s) (st : s ⊆ f ⁻¹' t) : times_cont_diff_on 𝕜 n (g ∘ f) s := begin /- we lift all the spaces to a common universe, as we have already proved the result in this situation. For the lift, we use the trick that `H` is isomorphic through a continuous linear equiv to `continuous_multilinear_map 𝕜 (λ (i : fin 0), (E × F × G)) H`, and continuous linear equivs respect smoothness classes. -/ let Eu := continuous_multilinear_map 𝕜 (λ (i : fin 0), (E × F × G)) E, letI : normed_group Eu := by apply_instance, letI : normed_space 𝕜 Eu := by apply_instance, let Fu := continuous_multilinear_map 𝕜 (λ (i : fin 0), (E × F × G)) F, letI : normed_group Fu := by apply_instance, letI : normed_space 𝕜 Fu := by apply_instance, let Gu := continuous_multilinear_map 𝕜 (λ (i : fin 0), (E × F × G)) G, letI : normed_group Gu := by apply_instance, letI : normed_space 𝕜 Gu := by apply_instance, -- declare the isomorphisms let isoE : Eu ≃L[𝕜] E := continuous_multilinear_curry_fin0 𝕜 (E × F × G) E, let isoF : Fu ≃L[𝕜] F := continuous_multilinear_curry_fin0 𝕜 (E × F × G) F, let isoG : Gu ≃L[𝕜] G := continuous_multilinear_curry_fin0 𝕜 (E × F × G) G, -- lift the functions to the new spaces, check smoothness there, and then go back. let fu : Eu → Fu := (isoF.symm ∘ f) ∘ isoE, have fu_diff : times_cont_diff_on 𝕜 n fu (isoE ⁻¹' s), by rwa [isoE.times_cont_diff_on_comp_iff, isoF.symm.comp_times_cont_diff_on_iff], let gu : Fu → Gu := (isoG.symm ∘ g) ∘ isoF, have gu_diff : times_cont_diff_on 𝕜 n gu (isoF ⁻¹' t), by rwa [isoF.times_cont_diff_on_comp_iff, isoG.symm.comp_times_cont_diff_on_iff], have main : times_cont_diff_on 𝕜 n (gu ∘ fu) (isoE ⁻¹' s), { apply times_cont_diff_on.comp_same_univ gu_diff fu_diff, assume y hy, simp only [fu, continuous_linear_equiv.coe_apply, function.comp_app, mem_preimage], rw isoF.apply_symm_apply (f (isoE y)), exact st hy }, have : gu ∘ fu = (isoG.symm ∘ (g ∘ f)) ∘ isoE, { ext y, simp only [function.comp_apply, gu, fu], rw isoF.apply_symm_apply (f (isoE y)) }, rwa [this, isoE.times_cont_diff_on_comp_iff, isoG.symm.comp_times_cont_diff_on_iff] at main end /-- The composition of `C^n` functions on domains is `C^n`. -/ lemma times_cont_diff_on.comp' {n : with_top ℕ} {s : set E} {t : set F} {g : F → G} {f : E → F} (hg : times_cont_diff_on 𝕜 n g t) (hf : times_cont_diff_on 𝕜 n f s) : times_cont_diff_on 𝕜 n (g ∘ f) (s ∩ f⁻¹' t) := hg.comp (hf.mono (inter_subset_left _ _)) (inter_subset_right _ _) /-- The composition of a `C^n` function on a domain with a `C^n` function is `C^n`. -/ lemma times_cont_diff.comp_times_cont_diff_on {n : with_top ℕ} {s : set E} {g : F → G} {f : E → F} (hg : times_cont_diff 𝕜 n g) (hf : times_cont_diff_on 𝕜 n f s) : times_cont_diff_on 𝕜 n (g ∘ f) s := (times_cont_diff_on_univ.2 hg).comp hf subset_preimage_univ /-- The composition of `C^n` functions is `C^n`. -/ lemma times_cont_diff.comp {n : with_top ℕ} {g : F → G} {f : E → F} (hg : times_cont_diff 𝕜 n g) (hf : times_cont_diff 𝕜 n f) : times_cont_diff 𝕜 n (g ∘ f) := times_cont_diff_on_univ.1 $ times_cont_diff_on.comp (times_cont_diff_on_univ.2 hg) (times_cont_diff_on_univ.2 hf) (subset_univ _) /-- The composition of `C^n` functions at points in domains is `C^n`. -/ lemma times_cont_diff_within_at.comp {n : with_top ℕ} {s : set E} {t : set F} {g : F → G} {f : E → F} (x : E) (hg : times_cont_diff_within_at 𝕜 n g t (f x)) (hf : times_cont_diff_within_at 𝕜 n f s x) (st : s ⊆ f ⁻¹' t) : times_cont_diff_within_at 𝕜 n (g ∘ f) s x := begin assume m hm, rcases hg.times_cont_diff_on hm with ⟨u, u_nhd, ut, hu⟩, rcases hf.times_cont_diff_on hm with ⟨v, v_nhd, vs, hv⟩, have xmem : x ∈ f ⁻¹' u ∩ v := ⟨(mem_of_mem_nhds_within (mem_insert (f x) _) u_nhd : _), mem_of_mem_nhds_within (mem_insert x s) v_nhd⟩, have : f ⁻¹' u ∈ 𝓝[insert x s] x, { apply hf.continuous_within_at.insert_self.preimage_mem_nhds_within', apply nhds_within_mono _ _ u_nhd, rw image_insert_eq, exact insert_subset_insert (image_subset_iff.mpr st) }, have Z := ((hu.comp (hv.mono (inter_subset_right (f ⁻¹' u) v)) (inter_subset_left _ _)) .times_cont_diff_within_at) xmem m (le_refl _), have : 𝓝[f ⁻¹' u ∩ v] x = 𝓝[insert x s] x, { have A : f ⁻¹' u ∩ v = (insert x s) ∩ (f ⁻¹' u ∩ v), { apply subset.antisymm _ (inter_subset_right _ _), rintros y ⟨hy1, hy2⟩, simp [hy1, hy2, vs hy2] }, rw [A, ← nhds_within_restrict''], exact filter.inter_mem this v_nhd }, rwa [insert_eq_of_mem xmem, this] at Z, end /-- The composition of `C^n` functions at points in domains is `C^n`. -/ lemma times_cont_diff_within_at.comp' {n : with_top ℕ} {s : set E} {t : set F} {g : F → G} {f : E → F} (x : E) (hg : times_cont_diff_within_at 𝕜 n g t (f x)) (hf : times_cont_diff_within_at 𝕜 n f s x) : times_cont_diff_within_at 𝕜 n (g ∘ f) (s ∩ f⁻¹' t) x := hg.comp x (hf.mono (inter_subset_left _ _)) (inter_subset_right _ _) lemma times_cont_diff_at.comp_times_cont_diff_within_at {n} (x : E) (hg : times_cont_diff_at 𝕜 n g (f x)) (hf : times_cont_diff_within_at 𝕜 n f s x) : times_cont_diff_within_at 𝕜 n (g ∘ f) s x := hg.comp x hf (maps_to_univ _ _) /-- The composition of `C^n` functions at points is `C^n`. -/ lemma times_cont_diff_at.comp {n : with_top ℕ} (x : E) (hg : times_cont_diff_at 𝕜 n g (f x)) (hf : times_cont_diff_at 𝕜 n f x) : times_cont_diff_at 𝕜 n (g ∘ f) x := hg.comp x hf subset_preimage_univ lemma times_cont_diff.comp_times_cont_diff_within_at {n : with_top ℕ} {g : F → G} {f : E → F} (h : times_cont_diff 𝕜 n g) (hf : times_cont_diff_within_at 𝕜 n f t x) : times_cont_diff_within_at 𝕜 n (g ∘ f) t x := begin have : times_cont_diff_within_at 𝕜 n g univ (f x) := h.times_cont_diff_at.times_cont_diff_within_at, exact this.comp x hf (subset_univ _), end lemma times_cont_diff.comp_times_cont_diff_at {n : with_top ℕ} {g : F → G} {f : E → F} (x : E) (hg : times_cont_diff 𝕜 n g) (hf : times_cont_diff_at 𝕜 n f x) : times_cont_diff_at 𝕜 n (g ∘ f) x := hg.comp_times_cont_diff_within_at hf /-- The bundled derivative of a `C^{n+1}` function is `C^n`. -/ lemma times_cont_diff_on_fderiv_within_apply {m n : with_top ℕ} {s : set E} {f : E → F} (hf : times_cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hmn : m + 1 ≤ n) : times_cont_diff_on 𝕜 m (λp : E × E, (fderiv_within 𝕜 f s p.1 : E →L[𝕜] F) p.2) (set.prod s (univ : set E)) := begin have A : times_cont_diff 𝕜 m (λp : (E →L[𝕜] F) × E, p.1 p.2), { apply is_bounded_bilinear_map.times_cont_diff, exact is_bounded_bilinear_map_apply }, have B : times_cont_diff_on 𝕜 m (λ (p : E × E), ((fderiv_within 𝕜 f s p.fst), p.snd)) (set.prod s univ), { apply times_cont_diff_on.prod _ _, { have I : times_cont_diff_on 𝕜 m (λ (x : E), fderiv_within 𝕜 f s x) s := hf.fderiv_within hs hmn, have J : times_cont_diff_on 𝕜 m (λ (x : E × E), x.1) (set.prod s univ) := times_cont_diff_fst.times_cont_diff_on, exact times_cont_diff_on.comp I J (prod_subset_preimage_fst _ _) }, { apply times_cont_diff.times_cont_diff_on _ , apply is_bounded_linear_map.snd.times_cont_diff } }, exact A.comp_times_cont_diff_on B end /-- The bundled derivative of a `C^{n+1}` function is `C^n`. -/ lemma times_cont_diff.times_cont_diff_fderiv_apply {n m : with_top ℕ} {f : E → F} (hf : times_cont_diff 𝕜 n f) (hmn : m + 1 ≤ n) : times_cont_diff 𝕜 m (λp : E × E, (fderiv 𝕜 f p.1 : E →L[𝕜] F) p.2) := begin rw ← times_cont_diff_on_univ at ⊢ hf, rw [← fderiv_within_univ, ← univ_prod_univ], exact times_cont_diff_on_fderiv_within_apply hf unique_diff_on_univ hmn end /-! ### Sum of two functions -/ /- The sum is smooth. -/ lemma times_cont_diff_add {n : with_top ℕ} : times_cont_diff 𝕜 n (λp : F × F, p.1 + p.2) := (is_bounded_linear_map.fst.add is_bounded_linear_map.snd).times_cont_diff /-- The sum of two `C^n` functions within a set at a point is `C^n` within this set at this point. -/ lemma times_cont_diff_within_at.add {n : with_top ℕ} {s : set E} {f g : E → F} (hf : times_cont_diff_within_at 𝕜 n f s x) (hg : times_cont_diff_within_at 𝕜 n g s x) : times_cont_diff_within_at 𝕜 n (λx, f x + g x) s x := times_cont_diff_add.times_cont_diff_within_at.comp x (hf.prod hg) subset_preimage_univ /-- The sum of two `C^n` functions at a point is `C^n` at this point. -/ lemma times_cont_diff_at.add {n : with_top ℕ} {f g : E → F} (hf : times_cont_diff_at 𝕜 n f x) (hg : times_cont_diff_at 𝕜 n g x) : times_cont_diff_at 𝕜 n (λx, f x + g x) x := by rw [← times_cont_diff_within_at_univ] at *; exact hf.add hg /-- The sum of two `C^n`functions is `C^n`. -/ lemma times_cont_diff.add {n : with_top ℕ} {f g : E → F} (hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) : times_cont_diff 𝕜 n (λx, f x + g x) := times_cont_diff_add.comp (hf.prod hg) /-- The sum of two `C^n` functions on a domain is `C^n`. -/ lemma times_cont_diff_on.add {n : with_top ℕ} {s : set E} {f g : E → F} (hf : times_cont_diff_on 𝕜 n f s) (hg : times_cont_diff_on 𝕜 n g s) : times_cont_diff_on 𝕜 n (λx, f x + g x) s := λ x hx, (hf x hx).add (hg x hx) /-! ### Negative -/ /- The negative is smooth. -/ lemma times_cont_diff_neg {n : with_top ℕ} : times_cont_diff 𝕜 n (λp : F, -p) := is_bounded_linear_map.id.neg.times_cont_diff /-- The negative of a `C^n` function within a domain at a point is `C^n` within this domain at this point. -/ lemma times_cont_diff_within_at.neg {n : with_top ℕ} {s : set E} {f : E → F} (hf : times_cont_diff_within_at 𝕜 n f s x) : times_cont_diff_within_at 𝕜 n (λx, -f x) s x := times_cont_diff_neg.times_cont_diff_within_at.comp x hf subset_preimage_univ /-- The negative of a `C^n` function at a point is `C^n` at this point. -/ lemma times_cont_diff_at.neg {n : with_top ℕ} {f : E → F} (hf : times_cont_diff_at 𝕜 n f x) : times_cont_diff_at 𝕜 n (λx, -f x) x := by rw ← times_cont_diff_within_at_univ at *; exact hf.neg /-- The negative of a `C^n`function is `C^n`. -/ lemma times_cont_diff.neg {n : with_top ℕ} {f : E → F} (hf : times_cont_diff 𝕜 n f) : times_cont_diff 𝕜 n (λx, -f x) := times_cont_diff_neg.comp hf /-- The negative of a `C^n` function on a domain is `C^n`. -/ lemma times_cont_diff_on.neg {n : with_top ℕ} {s : set E} {f : E → F} (hf : times_cont_diff_on 𝕜 n f s) : times_cont_diff_on 𝕜 n (λx, -f x) s := λ x hx, (hf x hx).neg /-! ### Subtraction -/ /-- The difference of two `C^n` functions within a set at a point is `C^n` within this set at this point. -/ lemma times_cont_diff_within_at.sub {n : with_top ℕ} {s : set E} {f g : E → F} (hf : times_cont_diff_within_at 𝕜 n f s x) (hg : times_cont_diff_within_at 𝕜 n g s x) : times_cont_diff_within_at 𝕜 n (λx, f x - g x) s x := by simpa only [sub_eq_add_neg] using hf.add hg.neg /-- The difference of two `C^n` functions at a point is `C^n` at this point. -/ lemma times_cont_diff_at.sub {n : with_top ℕ} {f g : E → F} (hf : times_cont_diff_at 𝕜 n f x) (hg : times_cont_diff_at 𝕜 n g x) : times_cont_diff_at 𝕜 n (λx, f x - g x) x := by simpa only [sub_eq_add_neg] using hf.add hg.neg /-- The difference of two `C^n` functions on a domain is `C^n`. -/ lemma times_cont_diff_on.sub {n : with_top ℕ} {s : set E} {f g : E → F} (hf : times_cont_diff_on 𝕜 n f s) (hg : times_cont_diff_on 𝕜 n g s) : times_cont_diff_on 𝕜 n (λx, f x - g x) s := by simpa only [sub_eq_add_neg] using hf.add hg.neg /-- The difference of two `C^n` functions is `C^n`. -/ lemma times_cont_diff.sub {n : with_top ℕ} {f g : E → F} (hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) : times_cont_diff 𝕜 n (λx, f x - g x) := by simpa only [sub_eq_add_neg] using hf.add hg.neg /-! ### Sum of finitely many functions -/ lemma times_cont_diff_within_at.sum {ι : Type*} {f : ι → E → F} {s : finset ι} {n : with_top ℕ} {t : set E} {x : E} (h : ∀ i ∈ s, times_cont_diff_within_at 𝕜 n (λ x, f i x) t x) : times_cont_diff_within_at 𝕜 n (λ x, (∑ i in s, f i x)) t x := begin classical, induction s using finset.induction_on with i s is IH, { simp [times_cont_diff_within_at_const] }, { simp only [is, finset.sum_insert, not_false_iff], exact (h _ (finset.mem_insert_self i s)).add (IH (λ j hj, h _ (finset.mem_insert_of_mem hj))) } end lemma times_cont_diff_at.sum {ι : Type*} {f : ι → E → F} {s : finset ι} {n : with_top ℕ} {x : E} (h : ∀ i ∈ s, times_cont_diff_at 𝕜 n (λ x, f i x) x) : times_cont_diff_at 𝕜 n (λ x, (∑ i in s, f i x)) x := by rw [← times_cont_diff_within_at_univ] at *; exact times_cont_diff_within_at.sum h lemma times_cont_diff_on.sum {ι : Type*} {f : ι → E → F} {s : finset ι} {n : with_top ℕ} {t : set E} (h : ∀ i ∈ s, times_cont_diff_on 𝕜 n (λ x, f i x) t) : times_cont_diff_on 𝕜 n (λ x, (∑ i in s, f i x)) t := λ x hx, times_cont_diff_within_at.sum (λ i hi, h i hi x hx) lemma times_cont_diff.sum {ι : Type*} {f : ι → E → F} {s : finset ι} {n : with_top ℕ} (h : ∀ i ∈ s, times_cont_diff 𝕜 n (λ x, f i x)) : times_cont_diff 𝕜 n (λ x, (∑ i in s, f i x)) := by simp [← times_cont_diff_on_univ] at *; exact times_cont_diff_on.sum h /-! ### Product of two functions -/ /- The product is smooth. -/ lemma times_cont_diff_mul {n : with_top ℕ} : times_cont_diff 𝕜 n (λ p : 𝕜 × 𝕜, p.1 * p.2) := is_bounded_bilinear_map_mul.times_cont_diff /-- The product of two `C^n` functions within a set at a point is `C^n` within this set at this point. -/ lemma times_cont_diff_within_at.mul {n : with_top ℕ} {s : set E} {f g : E → 𝕜} (hf : times_cont_diff_within_at 𝕜 n f s x) (hg : times_cont_diff_within_at 𝕜 n g s x) : times_cont_diff_within_at 𝕜 n (λ x, f x * g x) s x := times_cont_diff_mul.times_cont_diff_within_at.comp x (hf.prod hg) subset_preimage_univ /-- The product of two `C^n` functions at a point is `C^n` at this point. -/ lemma times_cont_diff_at.mul {n : with_top ℕ} {f g : E → 𝕜} (hf : times_cont_diff_at 𝕜 n f x) (hg : times_cont_diff_at 𝕜 n g x) : times_cont_diff_at 𝕜 n (λ x, f x * g x) x := by rw [← times_cont_diff_within_at_univ] at *; exact hf.mul hg /-- The product of two `C^n` functions on a domain is `C^n`. -/ lemma times_cont_diff_on.mul {n : with_top ℕ} {s : set E} {f g : E → 𝕜} (hf : times_cont_diff_on 𝕜 n f s) (hg : times_cont_diff_on 𝕜 n g s) : times_cont_diff_on 𝕜 n (λ x, f x * g x) s := λ x hx, (hf x hx).mul (hg x hx) /-- The product of two `C^n`functions is `C^n`. -/ lemma times_cont_diff.mul {n : with_top ℕ} {f g : E → 𝕜} (hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) : times_cont_diff 𝕜 n (λ x, f x * g x) := times_cont_diff_mul.comp (hf.prod hg) lemma times_cont_diff_within_at.div_const {f : E → 𝕜} {n} {c : 𝕜} (hf : times_cont_diff_within_at 𝕜 n f s x) : times_cont_diff_within_at 𝕜 n (λ x, f x / c) s x := by simpa only [div_eq_mul_inv] using hf.mul times_cont_diff_within_at_const lemma times_cont_diff_at.div_const {f : E → 𝕜} {n} {c : 𝕜} (hf : times_cont_diff_at 𝕜 n f x) : times_cont_diff_at 𝕜 n (λ x, f x / c) x := by simpa only [div_eq_mul_inv] using hf.mul times_cont_diff_at_const lemma times_cont_diff_on.div_const {f : E → 𝕜} {n} {c : 𝕜} (hf : times_cont_diff_on 𝕜 n f s) : times_cont_diff_on 𝕜 n (λ x, f x / c) s := by simpa only [div_eq_mul_inv] using hf.mul times_cont_diff_on_const lemma times_cont_diff.div_const {f : E → 𝕜} {n} {c : 𝕜} (hf : times_cont_diff 𝕜 n f) : times_cont_diff 𝕜 n (λ x, f x / c) := by simpa only [div_eq_mul_inv] using hf.mul times_cont_diff_const lemma times_cont_diff.pow {n : with_top ℕ} {f : E → 𝕜} (hf : times_cont_diff 𝕜 n f) : ∀ m : ℕ, times_cont_diff 𝕜 n (λ x, (f x) ^ m) | 0 := by simpa using times_cont_diff_const | (m + 1) := by simpa [pow_succ] using hf.mul (times_cont_diff.pow m) lemma times_cont_diff_at.pow {n : with_top ℕ} {f : E → 𝕜} (hf : times_cont_diff_at 𝕜 n f x) (m : ℕ) : times_cont_diff_at 𝕜 n (λ y, f y ^ m) x := (times_cont_diff_id.pow m).times_cont_diff_at.comp x hf lemma times_cont_diff_within_at.pow {n : with_top ℕ} {f : E → 𝕜} (hf : times_cont_diff_within_at 𝕜 n f s x) (m : ℕ) : times_cont_diff_within_at 𝕜 n (λ y, f y ^ m) s x := (times_cont_diff_id.pow m).times_cont_diff_at.comp_times_cont_diff_within_at x hf lemma times_cont_diff_on.pow {n : with_top ℕ} {f : E → 𝕜} (hf : times_cont_diff_on 𝕜 n f s) (m : ℕ) : times_cont_diff_on 𝕜 n (λ y, f y ^ m) s := λ y hy, (hf y hy).pow m /-! ### Scalar multiplication -/ /- The scalar multiplication is smooth. -/ lemma times_cont_diff_smul {n : with_top ℕ} : times_cont_diff 𝕜 n (λ p : 𝕜 × F, p.1 • p.2) := is_bounded_bilinear_map_smul.times_cont_diff /-- The scalar multiplication of two `C^n` functions within a set at a point is `C^n` within this set at this point. -/ lemma times_cont_diff_within_at.smul {n : with_top ℕ} {s : set E} {f : E → 𝕜} {g : E → F} (hf : times_cont_diff_within_at 𝕜 n f s x) (hg : times_cont_diff_within_at 𝕜 n g s x) : times_cont_diff_within_at 𝕜 n (λ x, f x • g x) s x := times_cont_diff_smul.times_cont_diff_within_at.comp x (hf.prod hg) subset_preimage_univ /-- The scalar multiplication of two `C^n` functions at a point is `C^n` at this point. -/ lemma times_cont_diff_at.smul {n : with_top ℕ} {f : E → 𝕜} {g : E → F} (hf : times_cont_diff_at 𝕜 n f x) (hg : times_cont_diff_at 𝕜 n g x) : times_cont_diff_at 𝕜 n (λ x, f x • g x) x := by rw [← times_cont_diff_within_at_univ] at *; exact hf.smul hg /-- The scalar multiplication of two `C^n` functions is `C^n`. -/ lemma times_cont_diff.smul {n : with_top ℕ} {f : E → 𝕜} {g : E → F} (hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) : times_cont_diff 𝕜 n (λ x, f x • g x) := times_cont_diff_smul.comp (hf.prod hg) /-- The scalar multiplication of two `C^n` functions on a domain is `C^n`. -/ lemma times_cont_diff_on.smul {n : with_top ℕ} {s : set E} {f : E → 𝕜} {g : E → F} (hf : times_cont_diff_on 𝕜 n f s) (hg : times_cont_diff_on 𝕜 n g s) : times_cont_diff_on 𝕜 n (λ x, f x • g x) s := λ x hx, (hf x hx).smul (hg x hx) /-! ### Cartesian product of two functions-/ section prod_map variables {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {F' : Type*} [normed_group F'] [normed_space 𝕜 F'] {n : with_top ℕ} /-- The product map of two `C^n` functions within a set at a point is `C^n` within the product set at the product point. -/ lemma times_cont_diff_within_at.prod_map' {s : set E} {t : set E'} {f : E → F} {g : E' → F'} {p : E × E'} (hf : times_cont_diff_within_at 𝕜 n f s p.1) (hg : times_cont_diff_within_at 𝕜 n g t p.2) : times_cont_diff_within_at 𝕜 n (prod.map f g) (set.prod s t) p := (hf.comp p times_cont_diff_within_at_fst (prod_subset_preimage_fst _ _)).prod (hg.comp p times_cont_diff_within_at_snd (prod_subset_preimage_snd _ _)) lemma times_cont_diff_within_at.prod_map {s : set E} {t : set E'} {f : E → F} {g : E' → F'} {x : E} {y : E'} (hf : times_cont_diff_within_at 𝕜 n f s x) (hg : times_cont_diff_within_at 𝕜 n g t y) : times_cont_diff_within_at 𝕜 n (prod.map f g) (set.prod s t) (x, y) := times_cont_diff_within_at.prod_map' hf hg /-- The product map of two `C^n` functions on a set is `C^n` on the product set. -/ lemma times_cont_diff_on.prod_map {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {F' : Type*} [normed_group F'] [normed_space 𝕜 F'] {s : set E} {t : set E'} {n : with_top ℕ} {f : E → F} {g : E' → F'} (hf : times_cont_diff_on 𝕜 n f s) (hg : times_cont_diff_on 𝕜 n g t) : times_cont_diff_on 𝕜 n (prod.map f g) (set.prod s t) := (hf.comp times_cont_diff_on_fst (prod_subset_preimage_fst _ _)).prod (hg.comp (times_cont_diff_on_snd) (prod_subset_preimage_snd _ _)) /-- The product map of two `C^n` functions within a set at a point is `C^n` within the product set at the product point. -/ lemma times_cont_diff_at.prod_map {f : E → F} {g : E' → F'} {x : E} {y : E'} (hf : times_cont_diff_at 𝕜 n f x) (hg : times_cont_diff_at 𝕜 n g y) : times_cont_diff_at 𝕜 n (prod.map f g) (x, y) := begin rw times_cont_diff_at at *, convert hf.prod_map hg, simp only [univ_prod_univ] end /-- The product map of two `C^n` functions within a set at a point is `C^n` within the product set at the product point. -/ lemma times_cont_diff_at.prod_map' {f : E → F} {g : E' → F'} {p : E × E'} (hf : times_cont_diff_at 𝕜 n f p.1) (hg : times_cont_diff_at 𝕜 n g p.2) : times_cont_diff_at 𝕜 n (prod.map f g) p := begin rcases p, exact times_cont_diff_at.prod_map hf hg end /-- The product map of two `C^n` functions is `C^n`. -/ lemma times_cont_diff.prod_map {f : E → F} {g : E' → F'} (hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) : times_cont_diff 𝕜 n (prod.map f g) := begin rw times_cont_diff_iff_times_cont_diff_at at *, exact λ ⟨x, y⟩, (hf x).prod_map (hg y) end end prod_map /-! ### Inversion in a complete normed algebra -/ section algebra_inverse variables (𝕜) {R : Type*} [normed_ring R] [normed_algebra 𝕜 R] open normed_ring continuous_linear_map ring /-- In a complete normed algebra, the operation of inversion is `C^n`, for all `n`, at each invertible element. The proof is by induction, bootstrapping using an identity expressing the derivative of inversion as a bilinear map of inversion itself. -/ lemma times_cont_diff_at_ring_inverse [complete_space R] {n : with_top ℕ} (x : units R) : times_cont_diff_at 𝕜 n ring.inverse (x : R) := begin induction n using with_top.nat_induction with n IH Itop, { intros m hm, refine ⟨{y : R | is_unit y}, _, _⟩, { simp [nhds_within_univ], exact x.nhds }, { use (ftaylor_series_within 𝕜 inverse univ), rw [le_antisymm hm bot_le, has_ftaylor_series_up_to_on_zero_iff], split, { rintros _ ⟨x', rfl⟩, exact (inverse_continuous_at x').continuous_within_at }, { simp [ftaylor_series_within] } } }, { apply times_cont_diff_at_succ_iff_has_fderiv_at.mpr, refine ⟨λ (x : R), - lmul_left_right 𝕜 R (inverse x) (inverse x), _, _⟩, { refine ⟨{y : R | is_unit y}, x.nhds, _⟩, rintros _ ⟨y, rfl⟩, rw [inverse_unit], exact has_fderiv_at_ring_inverse y }, { convert (lmul_left_right_is_bounded_bilinear 𝕜 R).times_cont_diff.neg.comp_times_cont_diff_at (x : R) (IH.prod IH) } }, { exact times_cont_diff_at_top.mpr Itop } end variables (𝕜) {𝕜' : Type*} [normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] [complete_space 𝕜'] lemma times_cont_diff_at_inv {x : 𝕜'} (hx : x ≠ 0) {n} : times_cont_diff_at 𝕜 n has_inv.inv x := by simpa only [inverse_eq_has_inv] using times_cont_diff_at_ring_inverse 𝕜 (units.mk0 x hx) lemma times_cont_diff_on_inv {n} : times_cont_diff_on 𝕜 n (has_inv.inv : 𝕜' → 𝕜') {0}ᶜ := λ x hx, (times_cont_diff_at_inv 𝕜 hx).times_cont_diff_within_at variable {𝕜} -- TODO: the next few lemmas don't need `𝕜` or `𝕜'` to be complete -- A good way to show this is to generalize `times_cont_diff_at_ring_inverse` to the setting -- of a function `f` such that `∀ᶠ x in 𝓝 a, x * f x = 1`. lemma times_cont_diff_within_at.inv {f : E → 𝕜'} {n} (hf : times_cont_diff_within_at 𝕜 n f s x) (hx : f x ≠ 0) : times_cont_diff_within_at 𝕜 n (λ x, (f x)⁻¹) s x := (times_cont_diff_at_inv 𝕜 hx).comp_times_cont_diff_within_at x hf lemma times_cont_diff_on.inv {f : E → 𝕜'} {n} (hf : times_cont_diff_on 𝕜 n f s) (h : ∀ x ∈ s, f x ≠ 0) : times_cont_diff_on 𝕜 n (λ x, (f x)⁻¹) s := λ x hx, (hf.times_cont_diff_within_at hx).inv (h x hx) lemma times_cont_diff_at.inv {f : E → 𝕜'} {n} (hf : times_cont_diff_at 𝕜 n f x) (hx : f x ≠ 0) : times_cont_diff_at 𝕜 n (λ x, (f x)⁻¹) x := hf.inv hx lemma times_cont_diff.inv {f : E → 𝕜'} {n} (hf : times_cont_diff 𝕜 n f) (h : ∀ x, f x ≠ 0) : times_cont_diff 𝕜 n (λ x, (f x)⁻¹) := by { rw times_cont_diff_iff_times_cont_diff_at, exact λ x, hf.times_cont_diff_at.inv (h x) } -- TODO: generalize to `f g : E → 𝕜'` lemma times_cont_diff_within_at.div [complete_space 𝕜] {f g : E → 𝕜} {n} (hf : times_cont_diff_within_at 𝕜 n f s x) (hg : times_cont_diff_within_at 𝕜 n g s x) (hx : g x ≠ 0) : times_cont_diff_within_at 𝕜 n (λ x, f x / g x) s x := by simpa only [div_eq_mul_inv] using hf.mul (hg.inv hx) lemma times_cont_diff_on.div [complete_space 𝕜] {f g : E → 𝕜} {n} (hf : times_cont_diff_on 𝕜 n f s) (hg : times_cont_diff_on 𝕜 n g s) (h₀ : ∀ x ∈ s, g x ≠ 0) : times_cont_diff_on 𝕜 n (f / g) s := λ x hx, (hf x hx).div (hg x hx) (h₀ x hx) lemma times_cont_diff_at.div [complete_space 𝕜] {f g : E → 𝕜} {n} (hf : times_cont_diff_at 𝕜 n f x) (hg : times_cont_diff_at 𝕜 n g x) (hx : g x ≠ 0) : times_cont_diff_at 𝕜 n (λ x, f x / g x) x := hf.div hg hx lemma times_cont_diff.div [complete_space 𝕜] {f g : E → 𝕜} {n} (hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) (h0 : ∀ x, g x ≠ 0) : times_cont_diff 𝕜 n (λ x, f x / g x) := begin simp only [times_cont_diff_iff_times_cont_diff_at] at *, exact λ x, (hf x).div (hg x) (h0 x) end end algebra_inverse /-! ### Inversion of continuous linear maps between Banach spaces -/ section map_inverse open continuous_linear_map /-- At a continuous linear equivalence `e : E ≃L[𝕜] F` between Banach spaces, the operation of inversion is `C^n`, for all `n`. -/ lemma times_cont_diff_at_map_inverse [complete_space E] {n : with_top ℕ} (e : E ≃L[𝕜] F) : times_cont_diff_at 𝕜 n inverse (e : E →L[𝕜] F) := begin nontriviality E, -- first, we use the lemma `to_ring_inverse` to rewrite in terms of `ring.inverse` in the ring -- `E →L[𝕜] E` let O₁ : (E →L[𝕜] E) → (F →L[𝕜] E) := λ f, f.comp (e.symm : (F →L[𝕜] E)), let O₂ : (E →L[𝕜] F) → (E →L[𝕜] E) := λ f, (e.symm : (F →L[𝕜] E)).comp f, have : continuous_linear_map.inverse = O₁ ∘ ring.inverse ∘ O₂ := funext (to_ring_inverse e), rw this, -- `O₁` and `O₂` are `times_cont_diff`, -- so we reduce to proving that `ring.inverse` is `times_cont_diff` have h₁ : times_cont_diff 𝕜 n O₁, from is_bounded_bilinear_map_comp.times_cont_diff.comp (times_cont_diff_const.prod times_cont_diff_id), have h₂ : times_cont_diff 𝕜 n O₂, from is_bounded_bilinear_map_comp.times_cont_diff.comp (times_cont_diff_id.prod times_cont_diff_const), refine h₁.times_cont_diff_at.comp _ (times_cont_diff_at.comp _ _ h₂.times_cont_diff_at), convert times_cont_diff_at_ring_inverse 𝕜 (1 : units (E →L[𝕜] E)), simp [O₂, one_def] end end map_inverse section function_inverse open continuous_linear_map /-- If `f` is a local homeomorphism and the point `a` is in its target, and if `f` is `n` times continuously differentiable at `f.symm a`, and if the derivative at `f.symm a` is a continuous linear equivalence, then `f.symm` is `n` times continuously differentiable at the point `a`. This is one of the easy parts of the inverse function theorem: it assumes that we already have an inverse function. -/ theorem local_homeomorph.times_cont_diff_at_symm [complete_space E] {n : with_top ℕ} (f : local_homeomorph E F) {f₀' : E ≃L[𝕜] F} {a : F} (ha : a ∈ f.target) (hf₀' : has_fderiv_at f (f₀' : E →L[𝕜] F) (f.symm a)) (hf : times_cont_diff_at 𝕜 n f (f.symm a)) : times_cont_diff_at 𝕜 n f.symm a := begin -- We prove this by induction on `n` induction n using with_top.nat_induction with n IH Itop, { rw times_cont_diff_at_zero, exact ⟨f.target, is_open.mem_nhds f.open_target ha, f.continuous_inv_fun⟩ }, { obtain ⟨f', ⟨u, hu, hff'⟩, hf'⟩ := times_cont_diff_at_succ_iff_has_fderiv_at.mp hf, apply times_cont_diff_at_succ_iff_has_fderiv_at.mpr, -- For showing `n.succ` times continuous differentiability (the main inductive step), it -- suffices to produce the derivative and show that it is `n` times continuously differentiable have eq_f₀' : f' (f.symm a) = f₀', { exact (hff' (f.symm a) (mem_of_mem_nhds hu)).unique hf₀' }, -- This follows by a bootstrapping formula expressing the derivative as a function of `f` itself refine ⟨inverse ∘ f' ∘ f.symm, _, _⟩, { -- We first check that the derivative of `f` is that formula have h_nhds : {y : E | ∃ (e : E ≃L[𝕜] F), ↑e = f' y} ∈ 𝓝 ((f.symm) a), { have hf₀' := f₀'.nhds, rw ← eq_f₀' at hf₀', exact hf'.continuous_at.preimage_mem_nhds hf₀' }, obtain ⟨t, htu, ht, htf⟩ := mem_nhds_iff.mp (filter.inter_mem hu h_nhds), use f.target ∩ (f.symm) ⁻¹' t, refine ⟨is_open.mem_nhds _ _, _⟩, { exact f.preimage_open_of_open_symm ht }, { exact mem_inter ha (mem_preimage.mpr htf) }, intros x hx, obtain ⟨hxu, e, he⟩ := htu hx.2, have h_deriv : has_fderiv_at f ↑e ((f.symm) x), { rw he, exact hff' (f.symm x) hxu }, convert f.has_fderiv_at_symm hx.1 h_deriv, simp [← he] }, { -- Then we check that the formula, being a composition of `times_cont_diff` pieces, is -- itself `times_cont_diff` have h_deriv₁ : times_cont_diff_at 𝕜 n inverse (f' (f.symm a)), { rw eq_f₀', exact times_cont_diff_at_map_inverse _ }, have h_deriv₂ : times_cont_diff_at 𝕜 n f.symm a, { refine IH (hf.of_le _), norm_cast, exact nat.le_succ n }, exact (h_deriv₁.comp _ hf').comp _ h_deriv₂ } }, { refine times_cont_diff_at_top.mpr _, intros n, exact Itop n (times_cont_diff_at_top.mp hf n) } end /-- Let `f` be a local homeomorphism of a nondiscrete normed field, let `a` be a point in its target. if `f` is `n` times continuously differentiable at `f.symm a`, and if the derivative at `f.symm a` is nonzero, then `f.symm` is `n` times continuously differentiable at the point `a`. This is one of the easy parts of the inverse function theorem: it assumes that we already have an inverse function. -/ theorem local_homeomorph.times_cont_diff_at_symm_deriv [complete_space 𝕜] {n : with_top ℕ} (f : local_homeomorph 𝕜 𝕜) {f₀' a : 𝕜} (h₀ : f₀' ≠ 0) (ha : a ∈ f.target) (hf₀' : has_deriv_at f f₀' (f.symm a)) (hf : times_cont_diff_at 𝕜 n f (f.symm a)) : times_cont_diff_at 𝕜 n f.symm a := f.times_cont_diff_at_symm ha (hf₀'.has_fderiv_at_equiv h₀) hf end function_inverse section real /-! ### Results over `ℝ` or `ℂ` The results in this section rely on the Mean Value Theorem, and therefore hold only over `ℝ` (and its extension fields such as `ℂ`). -/ variables {𝕂 : Type*} [is_R_or_C 𝕂] {E' : Type*} [normed_group E'] [normed_space 𝕂 E'] {F' : Type*} [normed_group F'] [normed_space 𝕂 F'] /-- If a function has a Taylor series at order at least 1, then at points in the interior of the domain of definition, the term of order 1 of this series is a strict derivative of `f`. -/ lemma has_ftaylor_series_up_to_on.has_strict_fderiv_at {s : set E'} {f : E' → F'} {x : E'} {p : E' → formal_multilinear_series 𝕂 E' F'} {n : with_top ℕ} (hf : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) (hs : s ∈ 𝓝 x) : has_strict_fderiv_at f ((continuous_multilinear_curry_fin1 𝕂 E' F') (p x 1)) x := has_strict_fderiv_at_of_has_fderiv_at_of_continuous_at (hf.eventually_has_fderiv_at hn hs) $ (continuous_multilinear_curry_fin1 𝕂 E' F').continuous_at.comp $ (hf.cont 1 hn).continuous_at hs /-- If a function is `C^n` with `1 ≤ n` around a point, and its derivative at that point is given to us as `f'`, then `f'` is also a strict derivative. -/ lemma times_cont_diff_at.has_strict_fderiv_at' {f : E' → F'} {f' : E' →L[𝕂] F'} {x : E'} {n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f x) (hf' : has_fderiv_at f f' x) (hn : 1 ≤ n) : has_strict_fderiv_at f f' x := begin rcases hf 1 hn with ⟨u, H, p, hp⟩, simp only [nhds_within_univ, mem_univ, insert_eq_of_mem] at H, have := hp.has_strict_fderiv_at le_rfl H, rwa hf'.unique this.has_fderiv_at end /-- If a function is `C^n` with `1 ≤ n` around a point, and its derivative at that point is given to us as `f'`, then `f'` is also a strict derivative. -/ lemma times_cont_diff_at.has_strict_deriv_at' {f : 𝕂 → F'} {f' : F'} {x : 𝕂} {n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f x) (hf' : has_deriv_at f f' x) (hn : 1 ≤ n) : has_strict_deriv_at f f' x := hf.has_strict_fderiv_at' hf' hn /-- If a function is `C^n` with `1 ≤ n` around a point, then the derivative of `f` at this point is also a strict derivative. -/ lemma times_cont_diff_at.has_strict_fderiv_at {f : E' → F'} {x : E'} {n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f x) (hn : 1 ≤ n) : has_strict_fderiv_at f (fderiv 𝕂 f x) x := hf.has_strict_fderiv_at' (hf.differentiable_at hn).has_fderiv_at hn /-- If a function is `C^n` with `1 ≤ n` around a point, then the derivative of `f` at this point is also a strict derivative. -/ lemma times_cont_diff_at.has_strict_deriv_at {f : 𝕂 → F'} {x : 𝕂} {n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f x) (hn : 1 ≤ n) : has_strict_deriv_at f (deriv f x) x := (hf.has_strict_fderiv_at hn).has_strict_deriv_at /-- If a function is `C^n` with `1 ≤ n`, then the derivative of `f` is also a strict derivative. -/ lemma times_cont_diff.has_strict_fderiv_at {f : E' → F'} {x : E'} {n : with_top ℕ} (hf : times_cont_diff 𝕂 n f) (hn : 1 ≤ n) : has_strict_fderiv_at f (fderiv 𝕂 f x) x := hf.times_cont_diff_at.has_strict_fderiv_at hn /-- If a function is `C^n` with `1 ≤ n`, then the derivative of `f` is also a strict derivative. -/ lemma times_cont_diff.has_strict_deriv_at {f : 𝕂 → F'} {x : 𝕂} {n : with_top ℕ} (hf : times_cont_diff 𝕂 n f) (hn : 1 ≤ n) : has_strict_deriv_at f (deriv f x) x := hf.times_cont_diff_at.has_strict_deriv_at hn /-- If `f` has a formal Taylor series `p` up to order `1` on `{x} ∪ s`, where `s` is a convex set, and `∥p x 1∥₊ < K`, then `f` is `K`-Lipschitz in a neighborhood of `x` within `s`. -/ lemma has_ftaylor_series_up_to_on.exists_lipschitz_on_with_of_nnnorm_lt {E F : Type*} [normed_group E] [normed_space ℝ E] [normed_group F] [normed_space ℝ F] {f : E → F} {p : E → formal_multilinear_series ℝ E F} {s : set E} {x : E} (hf : has_ftaylor_series_up_to_on 1 f p (insert x s)) (hs : convex ℝ s) (K : ℝ≥0) (hK : ∥p x 1∥₊ < K) : ∃ t ∈ 𝓝[s] x, lipschitz_on_with K f t := begin set f' := λ y, continuous_multilinear_curry_fin1 ℝ E F (p y 1), have hder : ∀ y ∈ s, has_fderiv_within_at f (f' y) s y, from λ y hy, (hf.has_fderiv_within_at le_rfl (subset_insert x s hy)).mono (subset_insert x s), have hcont : continuous_within_at f' s x, from (continuous_multilinear_curry_fin1 ℝ E F).continuous_at.comp_continuous_within_at ((hf.cont _ le_rfl _ (mem_insert _ _)).mono (subset_insert x s)), replace hK : ∥f' x∥₊ < K, by simpa only [linear_isometry_equiv.nnnorm_map], exact hs.exists_nhds_within_lipschitz_on_with_of_has_fderiv_within_at_of_nnnorm_lt (eventually_nhds_within_iff.2 $ eventually_of_forall hder) hcont K hK end /-- If `f` has a formal Taylor series `p` up to order `1` on `{x} ∪ s`, where `s` is a convex set, then `f` is Lipschitz in a neighborhood of `x` within `s`. -/ lemma has_ftaylor_series_up_to_on.exists_lipschitz_on_with {E F : Type*} [normed_group E] [normed_space ℝ E] [normed_group F] [normed_space ℝ F] {f : E → F} {p : E → formal_multilinear_series ℝ E F} {s : set E} {x : E} (hf : has_ftaylor_series_up_to_on 1 f p (insert x s)) (hs : convex ℝ s) : ∃ K (t ∈ 𝓝[s] x), lipschitz_on_with K f t := (no_top _).imp $ hf.exists_lipschitz_on_with_of_nnnorm_lt hs /-- If `f` is `C^1` within a conves set `s` at `x`, then it is Lipschitz on a neighborhood of `x` within `s`. -/ lemma times_cont_diff_within_at.exists_lipschitz_on_with {E F : Type*} [normed_group E] [normed_space ℝ E] [normed_group F] [normed_space ℝ F] {f : E → F} {s : set E} {x : E} (hf : times_cont_diff_within_at ℝ 1 f s x) (hs : convex ℝ s) : ∃ (K : ℝ≥0) (t ∈ 𝓝[s] x), lipschitz_on_with K f t := begin rcases hf 1 le_rfl with ⟨t, hst, p, hp⟩, rcases metric.mem_nhds_within_iff.mp hst with ⟨ε, ε0, hε⟩, replace hp : has_ftaylor_series_up_to_on 1 f p (metric.ball x ε ∩ insert x s) := hp.mono hε, clear hst hε t, rw [← insert_eq_of_mem (metric.mem_ball_self ε0), ← insert_inter] at hp, rcases hp.exists_lipschitz_on_with ((convex_ball _ _).inter hs) with ⟨K, t, hst, hft⟩, rw [inter_comm, ← nhds_within_restrict' _ (metric.ball_mem_nhds _ ε0)] at hst, exact ⟨K, t, hst, hft⟩ end /-- If `f` is `C^1` at `x` and `K > ∥fderiv 𝕂 f x∥`, then `f` is `K`-Lipschitz in a neighborhood of `x`. -/ lemma times_cont_diff_at.exists_lipschitz_on_with_of_nnnorm_lt {f : E' → F'} {x : E'} (hf : times_cont_diff_at 𝕂 1 f x) (K : ℝ≥0) (hK : ∥fderiv 𝕂 f x∥₊ < K) : ∃ t ∈ 𝓝 x, lipschitz_on_with K f t := (hf.has_strict_fderiv_at le_rfl).exists_lipschitz_on_with_of_nnnorm_lt K hK /-- If `f` is `C^1` at `x`, then `f` is Lipschitz in a neighborhood of `x`. -/ lemma times_cont_diff_at.exists_lipschitz_on_with {f : E' → F'} {x : E'} (hf : times_cont_diff_at 𝕂 1 f x) : ∃ K (t ∈ 𝓝 x), lipschitz_on_with K f t := (hf.has_strict_fderiv_at le_rfl).exists_lipschitz_on_with end real section deriv /-! ### One dimension All results up to now have been expressed in terms of the general Fréchet derivative `fderiv`. For maps defined on the field, the one-dimensional derivative `deriv` is often easier to use. In this paragraph, we reformulate some higher smoothness results in terms of `deriv`. -/ variables {f₂ : 𝕜 → F} {s₂ : set 𝕜} open continuous_linear_map (smul_right) /-- A function is `C^(n + 1)` on a domain with unique derivatives if and only if it is differentiable there, and its derivative (formulated with `deriv_within`) is `C^n`. -/ theorem times_cont_diff_on_succ_iff_deriv_within {n : ℕ} (hs : unique_diff_on 𝕜 s₂) : times_cont_diff_on 𝕜 ((n + 1) : ℕ) f₂ s₂ ↔ differentiable_on 𝕜 f₂ s₂ ∧ times_cont_diff_on 𝕜 n (deriv_within f₂ s₂) s₂ := begin rw times_cont_diff_on_succ_iff_fderiv_within hs, congr' 2, apply le_antisymm, { assume h, have : deriv_within f₂ s₂ = (λ u : 𝕜 →L[𝕜] F, u 1) ∘ (fderiv_within 𝕜 f₂ s₂), by { ext x, refl }, simp only [this], apply times_cont_diff.comp_times_cont_diff_on _ h, exact (is_bounded_bilinear_map_apply.is_bounded_linear_map_left _).times_cont_diff }, { assume h, have : fderiv_within 𝕜 f₂ s₂ = smul_right (1 : 𝕜 →L[𝕜] 𝕜) ∘ deriv_within f₂ s₂, by { ext x, simp [deriv_within] }, simp only [this], apply times_cont_diff.comp_times_cont_diff_on _ h, exact (is_bounded_bilinear_map_smul_right.is_bounded_linear_map_right _).times_cont_diff } end /-- A function is `C^(n + 1)` on an open domain if and only if it is differentiable there, and its derivative (formulated with `deriv`) is `C^n`. -/ theorem times_cont_diff_on_succ_iff_deriv_of_open {n : ℕ} (hs : is_open s₂) : times_cont_diff_on 𝕜 ((n + 1) : ℕ) f₂ s₂ ↔ differentiable_on 𝕜 f₂ s₂ ∧ times_cont_diff_on 𝕜 n (deriv f₂) s₂ := begin rw times_cont_diff_on_succ_iff_deriv_within hs.unique_diff_on, congr' 2, rw ← iff_iff_eq, apply times_cont_diff_on_congr, assume x hx, exact deriv_within_of_open hs hx end /-- A function is `C^∞` on a domain with unique derivatives if and only if it is differentiable there, and its derivative (formulated with `deriv_within`) is `C^∞`. -/ theorem times_cont_diff_on_top_iff_deriv_within (hs : unique_diff_on 𝕜 s₂) : times_cont_diff_on 𝕜 ∞ f₂ s₂ ↔ differentiable_on 𝕜 f₂ s₂ ∧ times_cont_diff_on 𝕜 ∞ (deriv_within f₂ s₂) s₂ := begin split, { assume h, refine ⟨h.differentiable_on le_top, _⟩, apply times_cont_diff_on_top.2 (λ n, ((times_cont_diff_on_succ_iff_deriv_within hs).1 _).2), exact h.of_le le_top }, { assume h, refine times_cont_diff_on_top.2 (λ n, _), have A : (n : with_top ℕ) ≤ ∞ := le_top, apply ((times_cont_diff_on_succ_iff_deriv_within hs).2 ⟨h.1, h.2.of_le A⟩).of_le, exact with_top.coe_le_coe.2 (nat.le_succ n) } end /-- A function is `C^∞` on an open domain if and only if it is differentiable there, and its derivative (formulated with `deriv`) is `C^∞`. -/ theorem times_cont_diff_on_top_iff_deriv_of_open (hs : is_open s₂) : times_cont_diff_on 𝕜 ∞ f₂ s₂ ↔ differentiable_on 𝕜 f₂ s₂ ∧ times_cont_diff_on 𝕜 ∞ (deriv f₂) s₂ := begin rw times_cont_diff_on_top_iff_deriv_within hs.unique_diff_on, congr' 2, rw ← iff_iff_eq, apply times_cont_diff_on_congr, assume x hx, exact deriv_within_of_open hs hx end lemma times_cont_diff_on.deriv_within {m n : with_top ℕ} (hf : times_cont_diff_on 𝕜 n f₂ s₂) (hs : unique_diff_on 𝕜 s₂) (hmn : m + 1 ≤ n) : times_cont_diff_on 𝕜 m (deriv_within f₂ s₂) s₂ := begin cases m, { change ∞ + 1 ≤ n at hmn, have : n = ∞, by simpa using hmn, rw this at hf, exact ((times_cont_diff_on_top_iff_deriv_within hs).1 hf).2 }, { change (m.succ : with_top ℕ) ≤ n at hmn, exact ((times_cont_diff_on_succ_iff_deriv_within hs).1 (hf.of_le hmn)).2 } end lemma times_cont_diff_on.deriv_of_open {m n : with_top ℕ} (hf : times_cont_diff_on 𝕜 n f₂ s₂) (hs : is_open s₂) (hmn : m + 1 ≤ n) : times_cont_diff_on 𝕜 m (deriv f₂) s₂ := (hf.deriv_within hs.unique_diff_on hmn).congr (λ x hx, (deriv_within_of_open hs hx).symm) lemma times_cont_diff_on.continuous_on_deriv_within {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f₂ s₂) (hs : unique_diff_on 𝕜 s₂) (hn : 1 ≤ n) : continuous_on (deriv_within f₂ s₂) s₂ := ((times_cont_diff_on_succ_iff_deriv_within hs).1 (h.of_le hn)).2.continuous_on lemma times_cont_diff_on.continuous_on_deriv_of_open {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f₂ s₂) (hs : is_open s₂) (hn : 1 ≤ n) : continuous_on (deriv f₂) s₂ := ((times_cont_diff_on_succ_iff_deriv_of_open hs).1 (h.of_le hn)).2.continuous_on /-- A function is `C^(n + 1)` on a domain with unique derivatives if and only if it is differentiable there, and its derivative is `C^n`. -/ theorem times_cont_diff_succ_iff_deriv {n : ℕ} : times_cont_diff 𝕜 ((n + 1) : ℕ) f₂ ↔ differentiable 𝕜 f₂ ∧ times_cont_diff 𝕜 n (deriv f₂) := by simp only [← times_cont_diff_on_univ, times_cont_diff_on_succ_iff_deriv_of_open, is_open_univ, differentiable_on_univ] end deriv section restrict_scalars /-! ### Restricting from `ℂ` to `ℝ`, or generally from `𝕜'` to `𝕜` If a function is `n` times continuously differentiable over `ℂ`, then it is `n` times continuously differentiable over `ℝ`. In this paragraph, we give variants of this statement, in the general situation where `ℂ` and `ℝ` are replaced respectively by `𝕜'` and `𝕜` where `𝕜'` is a normed algebra over `𝕜`. -/ variables (𝕜) {𝕜' : Type*} [nondiscrete_normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] variables [normed_space 𝕜' E] [is_scalar_tower 𝕜 𝕜' E] variables [normed_space 𝕜' F] [is_scalar_tower 𝕜 𝕜' F] variables {p' : E → formal_multilinear_series 𝕜' E F} {n : with_top ℕ} lemma has_ftaylor_series_up_to_on.restrict_scalars (h : has_ftaylor_series_up_to_on n f p' s) : has_ftaylor_series_up_to_on n f (λ x, (p' x).restrict_scalars 𝕜) s := { zero_eq := λ x hx, h.zero_eq x hx, fderiv_within := begin intros m hm x hx, convert ((continuous_multilinear_map.restrict_scalars_linear 𝕜).has_fderiv_at) .comp_has_fderiv_within_at _ ((h.fderiv_within m hm x hx).restrict_scalars 𝕜), end, cont := λ m hm, continuous_multilinear_map.continuous_restrict_scalars.comp_continuous_on (h.cont m hm) } lemma times_cont_diff_within_at.restrict_scalars (h : times_cont_diff_within_at 𝕜' n f s x) : times_cont_diff_within_at 𝕜 n f s x := begin intros m hm, rcases h m hm with ⟨u, u_mem, p', hp'⟩, exact ⟨u, u_mem, _, hp'.restrict_scalars _⟩ end lemma times_cont_diff_on.restrict_scalars (h : times_cont_diff_on 𝕜' n f s) : times_cont_diff_on 𝕜 n f s := λ x hx, (h x hx).restrict_scalars _ lemma times_cont_diff_at.restrict_scalars (h : times_cont_diff_at 𝕜' n f x) : times_cont_diff_at 𝕜 n f x := times_cont_diff_within_at_univ.1 $ h.times_cont_diff_within_at.restrict_scalars _ lemma times_cont_diff.restrict_scalars (h : times_cont_diff 𝕜' n f) : times_cont_diff 𝕜 n f := times_cont_diff_iff_times_cont_diff_at.2 $ λ x, h.times_cont_diff_at.restrict_scalars _ end restrict_scalars
49a543ef1d871917a8ae5d2f4167e8f20ad53350
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/bases.lean
81f4a211ad817615118a2780ec36493a65d260d0
[ "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
42,196
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.constructions import topology.continuous_on /-! # Bases of topologies. Countability axioms. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. A topological basis on a topological space `t` is a collection of sets, such that all open sets can be generated as unions of these sets, without the need to take finite intersections of them. This file introduces a framework for dealing with these collections, and also what more we can say under certain countability conditions on bases, which are referred to as first- and second-countable. We also briefly cover the theory of separable spaces, which are those with a countable, dense subset. If a space is second-countable, and also has a countably generated uniformity filter (for example, if `t` is a metric space), it will automatically be separable (and indeed, these conditions are equivalent in this case). ## Main definitions * `is_topological_basis s`: The topological space `t` has basis `s`. * `separable_space α`: The topological space `t` has a countable, dense subset. * `is_separable s`: The set `s` is contained in the closure of a countable set. * `first_countable_topology α`: A topology in which `𝓝 x` is countably generated for every `x`. * `second_countable_topology α`: A topology which has a topological basis which is countable. ## Main results * `first_countable_topology.tendsto_subseq`: In a first-countable space, cluster points are limits of subsequences. * `second_countable_topology.is_open_Union_countable`: In a second-countable space, the union of arbitrarily-many open sets is equal to a sub-union of only countably many of these sets. * `second_countable_topology.countable_cover_nhds`: Consider `f : α → set α` with the property that `f x ∈ 𝓝 x` for all `x`. Then there is some countable set `s` whose image covers the space. ## Implementation Notes For our applications we are interested that there exists a countable basis, but we do not need the concrete basis itself. This allows us to declare these type classes as `Prop` to use them as mixins. ### TODO: More fine grained instances for `first_countable_topology`, `separable_space`, `t2_space`, and more (see the comment below `subtype.second_countable_topology`.) -/ open set filter function open_locale topology filter noncomputable theory namespace topological_space universe u variables {α : Type u} [t : topological_space α] include t /-- A topological basis is one that satisfies the necessary conditions so that it suffices to take unions of the basis sets to get a topology (without taking finite intersections as well). -/ structure is_topological_basis (s : set (set α)) : Prop := (exists_subset_inter : ∀t₁∈s, ∀t₂∈s, ∀ x ∈ t₁ ∩ t₂, ∃ t₃∈s, x ∈ t₃ ∧ t₃ ⊆ t₁ ∩ t₂) (sUnion_eq : (⋃₀ s) = univ) (eq_generate_from : t = generate_from s) lemma is_topological_basis.insert_empty {s : set (set α)} (h : is_topological_basis s) : is_topological_basis (insert ∅ s) := begin refine ⟨_, by rw [sUnion_insert, empty_union, h.sUnion_eq], _⟩, { rintro t₁ (rfl|h₁) t₂ (rfl|h₂) x ⟨hx₁, hx₂⟩, {cases hx₁}, {cases hx₁}, {cases hx₂}, obtain ⟨t₃, h₃, hs⟩ := h.exists_subset_inter _ h₁ _ h₂ x ⟨hx₁, hx₂⟩, exact ⟨t₃, or.inr h₃, hs⟩ }, { rw h.eq_generate_from, refine le_antisymm (le_generate_from $ λ t, _) (generate_from_anti $ subset_insert ∅ s), rintro (rfl|ht), { convert is_open_empty }, { exact generate_open.basic t ht } }, end lemma is_topological_basis.diff_empty {s : set (set α)} (h : is_topological_basis s) : is_topological_basis (s \ {∅}) := begin refine ⟨_, by rw [sUnion_diff_singleton_empty, h.sUnion_eq], _⟩, { rintro t₁ ⟨h₁, -⟩ t₂ ⟨h₂, -⟩ x hx, obtain ⟨t₃, h₃, hs⟩ := h.exists_subset_inter _ h₁ _ h₂ x hx, exact ⟨t₃, ⟨h₃, nonempty.ne_empty ⟨x, hs.1⟩⟩, hs⟩ }, { rw h.eq_generate_from, refine le_antisymm (generate_from_anti $ diff_subset s _) (le_generate_from $ λ t ht, _), obtain rfl|he := eq_or_ne t ∅, { convert is_open_empty }, exact generate_open.basic t ⟨ht, he⟩ }, end /-- If a family of sets `s` generates the topology, then intersections of finite subcollections of `s` form a topological basis. -/ lemma is_topological_basis_of_subbasis {s : set (set α)} (hs : t = generate_from s) : is_topological_basis ((λ f, ⋂₀ f) '' {f : set (set α) | f.finite ∧ f ⊆ s}) := begin refine ⟨_, _, hs.trans (le_antisymm (le_generate_from _) $ generate_from_anti $ λ t ht, _)⟩, { rintro _ ⟨t₁, ⟨hft₁, ht₁b⟩, rfl⟩ _ ⟨t₂, ⟨hft₂, ht₂b⟩, rfl⟩ x h, exact ⟨_, ⟨_, ⟨hft₁.union hft₂, union_subset ht₁b ht₂b⟩, sInter_union t₁ t₂⟩, h, subset.rfl⟩ }, { rw [sUnion_image, Union₂_eq_univ_iff], exact λ x, ⟨∅, ⟨finite_empty, empty_subset _⟩, sInter_empty.substr $ mem_univ x⟩ }, { rintro _ ⟨t, ⟨hft, htb⟩, rfl⟩, apply is_open_sInter, exacts [hft, λ s hs, generate_open.basic _ $ htb hs] }, { rw ← sInter_singleton t, exact ⟨{t}, ⟨finite_singleton t, singleton_subset_iff.2 ht⟩, rfl⟩ }, end /-- If a family of open sets `s` is such that every open neighbourhood contains some member of `s`, then `s` is a topological basis. -/ lemma is_topological_basis_of_open_of_nhds {s : set (set α)} (h_open : ∀ u ∈ s, is_open u) (h_nhds : ∀(a:α) (u : set α), a ∈ u → is_open u → ∃v ∈ s, a ∈ v ∧ v ⊆ u) : is_topological_basis s := begin refine ⟨λ t₁ ht₁ t₂ ht₂ x hx, h_nhds _ _ hx (is_open.inter (h_open _ ht₁) (h_open _ ht₂)), _, _⟩, { refine sUnion_eq_univ_iff.2 (λ a, _), rcases h_nhds a univ trivial is_open_univ with ⟨u, h₁, h₂, -⟩, exact ⟨u, h₁, h₂⟩ }, { refine (le_generate_from h_open).antisymm (λ u hu, _), refine (@is_open_iff_nhds α (generate_from s) u).mpr (λ a ha, _), rcases h_nhds a u ha hu with ⟨v, hvs, hav, hvu⟩, rw nhds_generate_from, exact infi₂_le_of_le v ⟨hav, hvs⟩ (le_principal_iff.2 hvu) } end /-- A set `s` is in the neighbourhood of `a` iff there is some basis set `t`, which contains `a` and is itself contained in `s`. -/ lemma is_topological_basis.mem_nhds_iff {a : α} {s : set α} {b : set (set α)} (hb : is_topological_basis b) : s ∈ 𝓝 a ↔ ∃ t ∈ b, a ∈ t ∧ t ⊆ s := begin change s ∈ (𝓝 a).sets ↔ ∃ t ∈ b, a ∈ t ∧ t ⊆ s, rw [hb.eq_generate_from, nhds_generate_from, binfi_sets_eq], { simp [and_assoc, and.left_comm] }, { exact assume s ⟨hs₁, hs₂⟩ t ⟨ht₁, ht₂⟩, have a ∈ s ∩ t, from ⟨hs₁, ht₁⟩, let ⟨u, hu₁, hu₂, hu₃⟩ := hb.1 _ hs₂ _ ht₂ _ this in ⟨u, ⟨hu₂, hu₁⟩, le_principal_iff.2 (subset.trans hu₃ (inter_subset_left _ _)), le_principal_iff.2 (subset.trans hu₃ (inter_subset_right _ _))⟩ }, { rcases eq_univ_iff_forall.1 hb.sUnion_eq a with ⟨i, h1, h2⟩, exact ⟨i, h2, h1⟩ } end lemma is_topological_basis.is_open_iff {s : set α} {b : set (set α)} (hb : is_topological_basis b) : is_open s ↔ ∀ a ∈ s, ∃ t ∈ b, a ∈ t ∧ t ⊆ s := by simp [is_open_iff_mem_nhds, hb.mem_nhds_iff] lemma is_topological_basis.nhds_has_basis {b : set (set α)} (hb : is_topological_basis b) {a : α} : (𝓝 a).has_basis (λ t : set α, t ∈ b ∧ a ∈ t) (λ t, t) := ⟨λ s, hb.mem_nhds_iff.trans $ by simp only [exists_prop, and_assoc]⟩ protected lemma is_topological_basis.is_open {s : set α} {b : set (set α)} (hb : is_topological_basis b) (hs : s ∈ b) : is_open s := by { rw hb.eq_generate_from, exact generate_open.basic s hs } protected lemma is_topological_basis.mem_nhds {a : α} {s : set α} {b : set (set α)} (hb : is_topological_basis b) (hs : s ∈ b) (ha : a ∈ s) : s ∈ 𝓝 a := (hb.is_open hs).mem_nhds ha lemma is_topological_basis.exists_subset_of_mem_open {b : set (set α)} (hb : is_topological_basis b) {a:α} {u : set α} (au : a ∈ u) (ou : is_open u) : ∃v ∈ b, a ∈ v ∧ v ⊆ u := hb.mem_nhds_iff.1 $ is_open.mem_nhds ou au /-- Any open set is the union of the basis sets contained in it. -/ lemma is_topological_basis.open_eq_sUnion' {B : set (set α)} (hB : is_topological_basis B) {u : set α} (ou : is_open u) : u = ⋃₀ {s ∈ B | s ⊆ u} := ext $ λ a, ⟨λ ha, let ⟨b, hb, ab, bu⟩ := hB.exists_subset_of_mem_open ha ou in ⟨b, ⟨hb, bu⟩, ab⟩, λ ⟨b, ⟨hb, bu⟩, ab⟩, bu ab⟩ lemma is_topological_basis.open_eq_sUnion {B : set (set α)} (hB : is_topological_basis B) {u : set α} (ou : is_open u) : ∃ S ⊆ B, u = ⋃₀ S := ⟨{s ∈ B | s ⊆ u}, λ s h, h.1, hB.open_eq_sUnion' ou⟩ lemma is_topological_basis.open_iff_eq_sUnion {B : set (set α)} (hB : is_topological_basis B) {u : set α} : is_open u ↔ ∃ S ⊆ B, u = ⋃₀ S := ⟨hB.open_eq_sUnion, λ ⟨S, hSB, hu⟩, hu.symm ▸ is_open_sUnion (λ s hs, hB.is_open (hSB hs))⟩ lemma is_topological_basis.open_eq_Union {B : set (set α)} (hB : is_topological_basis B) {u : set α} (ou : is_open u) : ∃ (β : Type u) (f : β → set α), u = (⋃ i, f i) ∧ ∀ i, f i ∈ B := ⟨↥{s ∈ B | s ⊆ u}, coe, by { rw ← sUnion_eq_Union, apply hB.open_eq_sUnion' ou }, λ s, and.left s.2⟩ /-- A point `a` is in the closure of `s` iff all basis sets containing `a` intersect `s`. -/ lemma is_topological_basis.mem_closure_iff {b : set (set α)} (hb : is_topological_basis b) {s : set α} {a : α} : a ∈ closure s ↔ ∀ o ∈ b, a ∈ o → (o ∩ s).nonempty := (mem_closure_iff_nhds_basis' hb.nhds_has_basis).trans $ by simp only [and_imp] /-- A set is dense iff it has non-trivial intersection with all basis sets. -/ lemma is_topological_basis.dense_iff {b : set (set α)} (hb : is_topological_basis b) {s : set α} : dense s ↔ ∀ o ∈ b, set.nonempty o → (o ∩ s).nonempty := begin simp only [dense, hb.mem_closure_iff], exact ⟨λ h o hb ⟨a, ha⟩, h a o hb ha, λ h a o hb ha, h o hb ⟨a, ha⟩⟩ end lemma is_topological_basis.is_open_map_iff {β} [topological_space β] {B : set (set α)} (hB : is_topological_basis B) {f : α → β} : is_open_map f ↔ ∀ s ∈ B, is_open (f '' s) := begin refine ⟨λ H o ho, H _ (hB.is_open ho), λ hf o ho, _⟩, rw [hB.open_eq_sUnion' ho, sUnion_eq_Union, image_Union], exact is_open_Union (λ s, hf s s.2.1) end lemma is_topological_basis.exists_nonempty_subset {B : set (set α)} (hb : is_topological_basis B) {u : set α} (hu : u.nonempty) (ou : is_open u) : ∃ v ∈ B, set.nonempty v ∧ v ⊆ u := begin cases hu with x hx, rw [hb.open_eq_sUnion' ou, mem_sUnion] at hx, rcases hx with ⟨v, hv, hxv⟩, exact ⟨v, hv.1, ⟨x, hxv⟩, hv.2⟩ end lemma is_topological_basis_opens : is_topological_basis { U : set α | is_open U } := is_topological_basis_of_open_of_nhds (by tauto) (by tauto) protected lemma is_topological_basis.prod {β} [topological_space β] {B₁ : set (set α)} {B₂ : set (set β)} (h₁ : is_topological_basis B₁) (h₂ : is_topological_basis B₂) : is_topological_basis (image2 (×ˢ) B₁ B₂) := begin refine is_topological_basis_of_open_of_nhds _ _, { rintro _ ⟨u₁, u₂, hu₁, hu₂, rfl⟩, exact (h₁.is_open hu₁).prod (h₂.is_open hu₂) }, { rintro ⟨a, b⟩ u hu uo, rcases (h₁.nhds_has_basis.prod_nhds h₂.nhds_has_basis).mem_iff.1 (is_open.mem_nhds uo hu) with ⟨⟨s, t⟩, ⟨⟨hs, ha⟩, ht, hb⟩, hu⟩, exact ⟨s ×ˢ t, mem_image2_of_mem hs ht, ⟨ha, hb⟩, hu⟩ } end protected lemma is_topological_basis.inducing {β} [topological_space β] {f : α → β} {T : set (set β)} (hf : inducing f) (h : is_topological_basis T) : is_topological_basis (image (preimage f) T) := begin refine is_topological_basis_of_open_of_nhds _ _, { rintros _ ⟨V, hV, rfl⟩, rwa hf.is_open_iff, refine ⟨V, h.is_open hV, rfl⟩ }, { intros a U ha hU, rw hf.is_open_iff at hU, obtain ⟨V, hV, rfl⟩ := hU, obtain ⟨S, hS, rfl⟩ := h.open_eq_sUnion hV, obtain ⟨W, hW, ha⟩ := ha, refine ⟨f ⁻¹' W, ⟨_, hS hW, rfl⟩, ha, set.preimage_mono $ set.subset_sUnion_of_mem hW⟩ } end lemma is_topological_basis_of_cover {ι} {U : ι → set α} (Uo : ∀ i, is_open (U i)) (Uc : (⋃ i, U i) = univ) {b : Π i, set (set (U i))} (hb : ∀ i, is_topological_basis (b i)) : is_topological_basis (⋃ i : ι, image (coe : U i → α) '' (b i)) := begin refine is_topological_basis_of_open_of_nhds (λ u hu, _) _, { simp only [mem_Union, mem_image] at hu, rcases hu with ⟨i, s, sb, rfl⟩, exact (Uo i).is_open_map_subtype_coe _ ((hb i).is_open sb) }, { intros a u ha uo, rcases Union_eq_univ_iff.1 Uc a with ⟨i, hi⟩, lift a to ↥(U i) using hi, rcases (hb i).exists_subset_of_mem_open (by exact ha) (uo.preimage continuous_subtype_coe) with ⟨v, hvb, hav, hvu⟩, exact ⟨coe '' v, mem_Union.2 ⟨i, mem_image_of_mem _ hvb⟩, mem_image_of_mem _ hav, image_subset_iff.2 hvu⟩ } end protected lemma is_topological_basis.continuous {β : Type*} [topological_space β] {B : set (set β)} (hB : is_topological_basis B) (f : α → β) (hf : ∀ s ∈ B, is_open (f ⁻¹' s)) : continuous f := begin rw hB.eq_generate_from, exact continuous_generated_from hf end variables (α) /-- A separable space is one with a countable dense subset, available through `topological_space.exists_countable_dense`. If `α` is also known to be nonempty, then `topological_space.dense_seq` provides a sequence `ℕ → α` with dense range, see `topological_space.dense_range_dense_seq`. If `α` is a uniform space with countably generated uniformity filter (e.g., an `emetric_space`), then this condition is equivalent to `topological_space.second_countable_topology α`. In this case the latter should be used as a typeclass argument in theorems because Lean can automatically deduce `separable_space` from `second_countable_topology` but it can't deduce `second_countable_topology` and `emetric_space`. -/ class separable_space : Prop := (exists_countable_dense : ∃s:set α, s.countable ∧ dense s) lemma exists_countable_dense [separable_space α] : ∃ s : set α, s.countable ∧ dense s := separable_space.exists_countable_dense /-- A nonempty separable space admits a sequence with dense range. Instead of running `cases` on the conclusion of this lemma, you might want to use `topological_space.dense_seq` and `topological_space.dense_range_dense_seq`. If `α` might be empty, then `exists_countable_dense` is the main way to use separability of `α`. -/ lemma exists_dense_seq [separable_space α] [nonempty α] : ∃ u : ℕ → α, dense_range u := begin obtain ⟨s : set α, hs, s_dense⟩ := exists_countable_dense α, cases set.countable_iff_exists_subset_range.mp hs with u hu, exact ⟨u, s_dense.mono hu⟩, end /-- A dense sequence in a non-empty separable topological space. If `α` might be empty, then `exists_countable_dense` is the main way to use separability of `α`. -/ def dense_seq [separable_space α] [nonempty α] : ℕ → α := classical.some (exists_dense_seq α) /-- The sequence `dense_seq α` has dense range. -/ @[simp] lemma dense_range_dense_seq [separable_space α] [nonempty α] : dense_range (dense_seq α) := classical.some_spec (exists_dense_seq α) variable {α} @[priority 100] instance countable.to_separable_space [countable α] : separable_space α := { exists_countable_dense := ⟨set.univ, set.countable_univ, dense_univ⟩ } lemma separable_space_of_dense_range {ι : Type*} [countable ι] (u : ι → α) (hu : dense_range u) : separable_space α := ⟨⟨range u, countable_range u, hu⟩⟩ /-- In a separable space, a family of nonempty disjoint open sets is countable. -/ lemma _root_.set.pairwise_disjoint.countable_of_is_open [separable_space α] {ι : Type*} {s : ι → set α} {a : set ι} (h : a.pairwise_disjoint s) (ha : ∀ i ∈ a, is_open (s i)) (h'a : ∀ i ∈ a, (s i).nonempty) : a.countable := begin rcases exists_countable_dense α with ⟨u, ⟨u_encodable⟩, u_dense⟩, have : ∀ i : a, ∃ y, y ∈ s i ∩ u := λ i, dense_iff_inter_open.1 u_dense (s i) (ha i i.2) (h'a i i.2), choose f hfs hfu using this, lift f to a → u using hfu, have f_inj : injective f, { refine injective_iff_pairwise_ne.mpr ((h.subtype _ _).mono $ λ i j hij hfij, hij.le_bot ⟨hfs i, _⟩), simp only [congr_arg coe hfij, hfs j] }, exact ⟨@encodable.of_inj _ _ u_encodable f f_inj⟩ end /-- In a separable space, a family of disjoint sets with nonempty interiors is countable. -/ lemma _root_.set.pairwise_disjoint.countable_of_nonempty_interior [separable_space α] {ι : Type*} {s : ι → set α} {a : set ι} (h : a.pairwise_disjoint s) (ha : ∀ i ∈ a, (interior (s i)).nonempty) : a.countable := (h.mono $ λ i, interior_subset).countable_of_is_open (λ i hi, is_open_interior) ha /-- A set `s` in a topological space is separable if it is contained in the closure of a countable set `c`. Beware that this definition does not require that `c` is contained in `s` (to express the latter, use `separable_space s` or `is_separable (univ : set s))`. In metric spaces, the two definitions are equivalent, see `topological_space.is_separable.separable_space`. -/ def is_separable (s : set α) := ∃ c : set α, c.countable ∧ s ⊆ closure c lemma is_separable.mono {s u : set α} (hs : is_separable s) (hu : u ⊆ s) : is_separable u := begin rcases hs with ⟨c, c_count, hs⟩, exact ⟨c, c_count, hu.trans hs⟩ end lemma is_separable.union {s u : set α} (hs : is_separable s) (hu : is_separable u) : is_separable (s ∪ u) := begin rcases hs with ⟨cs, cs_count, hcs⟩, rcases hu with ⟨cu, cu_count, hcu⟩, refine ⟨cs ∪ cu, cs_count.union cu_count, _⟩, exact union_subset (hcs.trans (closure_mono (subset_union_left _ _))) (hcu.trans (closure_mono (subset_union_right _ _))) end lemma is_separable.closure {s : set α} (hs : is_separable s) : is_separable (closure s) := begin rcases hs with ⟨c, c_count, hs⟩, exact ⟨c, c_count, by simpa using closure_mono hs⟩, end lemma is_separable_Union {ι : Type*} [countable ι] {s : ι → set α} (hs : ∀ i, is_separable (s i)) : is_separable (⋃ i, s i) := begin choose c hc h'c using hs, refine ⟨⋃ i, c i, countable_Union hc, Union_subset_iff.2 (λ i, _)⟩, exact (h'c i).trans (closure_mono (subset_Union _ i)) end lemma _root_.set.countable.is_separable {s : set α} (hs : s.countable) : is_separable s := ⟨s, hs, subset_closure⟩ lemma _root_.set.finite.is_separable {s : set α} (hs : s.finite) : is_separable s := hs.countable.is_separable lemma is_separable_univ_iff : is_separable (univ : set α) ↔ separable_space α := begin split, { rintros ⟨c, c_count, hc⟩, refine ⟨⟨c, c_count, by rwa [dense_iff_closure_eq, ← univ_subset_iff]⟩⟩ }, { introsI h, rcases exists_countable_dense α with ⟨c, c_count, hc⟩, exact ⟨c, c_count, by rwa [univ_subset_iff, ← dense_iff_closure_eq]⟩ } end lemma is_separable_of_separable_space [h : separable_space α] (s : set α) : is_separable s := is_separable.mono (is_separable_univ_iff.2 h) (subset_univ _) lemma is_separable.image {β : Type*} [topological_space β] {s : set α} (hs : is_separable s) {f : α → β} (hf : continuous f) : is_separable (f '' s) := begin rcases hs with ⟨c, c_count, hc⟩, refine ⟨f '' c, c_count.image _, _⟩, rw image_subset_iff, exact hc.trans (closure_subset_preimage_closure_image hf) end lemma is_separable_of_separable_space_subtype (s : set α) [separable_space s] : is_separable s := begin have : is_separable ((coe : s → α) '' (univ : set s)) := (is_separable_of_separable_space _).image continuous_subtype_coe, simpa only [image_univ, subtype.range_coe_subtype], end end topological_space open topological_space lemma is_topological_basis_pi {ι : Type*} {X : ι → Type*} [∀ i, topological_space (X i)] {T : Π i, set (set (X i))} (cond : ∀ i, is_topological_basis (T i)) : is_topological_basis {S : set (Π i, X i) | ∃ (U : Π i, set (X i)) (F : finset ι), (∀ i, i ∈ F → (U i) ∈ T i) ∧ S = (F : set ι).pi U } := begin refine is_topological_basis_of_open_of_nhds _ _, { rintro _ ⟨U, F, h1, rfl⟩, apply is_open_set_pi F.finite_to_set, intros i hi, exact (cond i).is_open (h1 i hi) }, { intros a U ha hU, obtain ⟨I, t, hta, htU⟩ : ∃ (I : finset ι) (t : Π (i : ι), set (X i)), (∀ i, t i ∈ 𝓝 (a i)) ∧ set.pi ↑I t ⊆ U, { rw [← filter.mem_pi', ← nhds_pi], exact hU.mem_nhds ha }, have : ∀ i, ∃ V ∈ T i, a i ∈ V ∧ V ⊆ t i := λ i, (cond i).mem_nhds_iff.1 (hta i), choose V hVT haV hVt, exact ⟨_, ⟨V, I, λ i hi, hVT i, rfl⟩, λ i hi, haV i, (pi_mono $ λ i hi, hVt i).trans htU⟩ }, end lemma is_topological_basis_infi {β : Type*} {ι : Type*} {X : ι → Type*} [t : ∀ i, topological_space (X i)] {T : Π i, set (set (X i))} (cond : ∀ i, is_topological_basis (T i)) (f : Π i, β → X i) : @is_topological_basis β (⨅ i, induced (f i) (t i)) { S | ∃ (U : Π i, set (X i)) (F : finset ι), (∀ i, i ∈ F → U i ∈ T i) ∧ S = ⋂ i (hi : i ∈ F), (f i) ⁻¹' (U i) } := begin convert (is_topological_basis_pi cond).inducing (inducing_infi_to_pi _), ext V, split, { rintros ⟨U, F, h1, h2⟩, have : (F : set ι).pi U = (⋂ (i : ι) (hi : i ∈ F), (λ (z : Π j, X j), z i) ⁻¹' (U i)), by { ext, simp }, refine ⟨(F : set ι).pi U, ⟨U, F, h1, rfl⟩, _⟩, rw [this, h2, set.preimage_Inter], congr' 1, ext1, rw set.preimage_Inter, refl }, { rintros ⟨U, ⟨U, F, h1, rfl⟩, h⟩, refine ⟨U, F, h1, _⟩, have : (F : set ι).pi U = (⋂ (i : ι) (hi : i ∈ F), (λ (z : Π j, X j), z i) ⁻¹' (U i)), by { ext, simp }, rw [← h, this, set.preimage_Inter], congr' 1, ext1, rw set.preimage_Inter, refl } end lemma is_topological_basis_singletons (α : Type*) [topological_space α] [discrete_topology α] : is_topological_basis {s | ∃ (x : α), (s : set α) = {x}} := is_topological_basis_of_open_of_nhds (λ u hu, is_open_discrete _) $ λ x u hx u_open, ⟨{x}, ⟨x, rfl⟩, mem_singleton x, singleton_subset_iff.2 hx⟩ /-- If `α` is a separable space and `f : α → β` is a continuous map with dense range, then `β` is a separable space as well. E.g., the completion of a separable uniform space is separable. -/ protected lemma dense_range.separable_space {α β : Type*} [topological_space α] [separable_space α] [topological_space β] {f : α → β} (h : dense_range f) (h' : continuous f) : separable_space β := let ⟨s, s_cnt, s_dense⟩ := exists_countable_dense α in ⟨⟨f '' s, countable.image s_cnt f, h.dense_image h' s_dense⟩⟩ lemma dense.exists_countable_dense_subset {α : Type*} [topological_space α] {s : set α} [separable_space s] (hs : dense s) : ∃ t ⊆ s, t.countable ∧ dense t := let ⟨t, htc, htd⟩ := exists_countable_dense s in ⟨coe '' t, image_subset_iff.2 $ λ x _, mem_preimage.2 $ subtype.coe_prop _, htc.image coe, hs.dense_range_coe.dense_image continuous_subtype_val htd⟩ /-- Let `s` be a dense set in a topological space `α` with partial order structure. If `s` is a separable space (e.g., if `α` has a second countable topology), then there exists a countable dense subset `t ⊆ s` such that `t` contains bottom/top element of `α` when they exist and belong to `s`. For a dense subset containing neither bot nor top elements, see `dense.exists_countable_dense_subset_no_bot_top`. -/ lemma dense.exists_countable_dense_subset_bot_top {α : Type*} [topological_space α] [partial_order α] {s : set α} [separable_space s] (hs : dense s) : ∃ t ⊆ s, t.countable ∧ dense t ∧ (∀ x, is_bot x → x ∈ s → x ∈ t) ∧ (∀ x, is_top x → x ∈ s → x ∈ t) := begin rcases hs.exists_countable_dense_subset with ⟨t, hts, htc, htd⟩, refine ⟨(t ∪ ({x | is_bot x} ∪ {x | is_top x})) ∩ s, _, _, _, _, _⟩, exacts [inter_subset_right _ _, (htc.union ((countable_is_bot α).union (countable_is_top α))).mono (inter_subset_left _ _), htd.mono (subset_inter (subset_union_left _ _) hts), λ x hx hxs, ⟨or.inr $ or.inl hx, hxs⟩, λ x hx hxs, ⟨or.inr $ or.inr hx, hxs⟩] end instance separable_space_univ {α : Type*} [topological_space α] [separable_space α] : separable_space (univ : set α) := (equiv.set.univ α).symm.surjective.dense_range.separable_space (continuous_id.subtype_mk _) /-- If `α` is a separable topological space with a partial order, then there exists a countable dense set `s : set α` that contains those of both bottom and top elements of `α` that actually exist. For a dense set containing neither bot nor top elements, see `exists_countable_dense_no_bot_top`. -/ lemma exists_countable_dense_bot_top (α : Type*) [topological_space α] [separable_space α] [partial_order α] : ∃ s : set α, s.countable ∧ dense s ∧ (∀ x, is_bot x → x ∈ s) ∧ (∀ x, is_top x → x ∈ s) := by simpa using dense_univ.exists_countable_dense_subset_bot_top namespace topological_space universe u variables (α : Type u) [t : topological_space α] include t /-- A first-countable space is one in which every point has a countable neighborhood basis. -/ class first_countable_topology : Prop := (nhds_generated_countable : ∀a:α, (𝓝 a).is_countably_generated) attribute [instance] first_countable_topology.nhds_generated_countable namespace first_countable_topology variable {α} /-- In a first-countable space, a cluster point `x` of a sequence is the limit of some subsequence. -/ lemma tendsto_subseq [first_countable_topology α] {u : ℕ → α} {x : α} (hx : map_cluster_pt x at_top u) : ∃ (ψ : ℕ → ℕ), (strict_mono ψ) ∧ (tendsto (u ∘ ψ) at_top (𝓝 x)) := subseq_tendsto_of_ne_bot hx end first_countable_topology variables {α} instance {β} [topological_space β] [first_countable_topology α] [first_countable_topology β] : first_countable_topology (α × β) := ⟨λ ⟨x, y⟩, by { rw nhds_prod_eq, apply_instance }⟩ section pi omit t instance {ι : Type*} {π : ι → Type*} [countable ι] [Π i, topological_space (π i)] [∀ i, first_countable_topology (π i)] : first_countable_topology (Π i, π i) := ⟨λ f, by { rw nhds_pi, apply_instance }⟩ end pi instance is_countably_generated_nhds_within (x : α) [is_countably_generated (𝓝 x)] (s : set α) : is_countably_generated (𝓝[s] x) := inf.is_countably_generated _ _ variable (α) /-- A second-countable space is one with a countable basis. -/ class second_countable_topology : Prop := (is_open_generated_countable [] : ∃ b : set (set α), b.countable ∧ t = topological_space.generate_from b) variable {α} protected lemma is_topological_basis.second_countable_topology {b : set (set α)} (hb : is_topological_basis b) (hc : b.countable) : second_countable_topology α := ⟨⟨b, hc, hb.eq_generate_from⟩⟩ variable (α) lemma exists_countable_basis [second_countable_topology α] : ∃ b : set (set α), b.countable ∧ ∅ ∉ b ∧ is_topological_basis b := begin obtain ⟨b, hb₁, hb₂⟩ := second_countable_topology.is_open_generated_countable α, refine ⟨_, _, not_mem_diff_of_mem _, (is_topological_basis_of_subbasis hb₂).diff_empty⟩, exacts [((countable_set_of_finite_subset hb₁).image _).mono (diff_subset _ _), rfl], end /-- A countable topological basis of `α`. -/ def countable_basis [second_countable_topology α] : set (set α) := (exists_countable_basis α).some lemma countable_countable_basis [second_countable_topology α] : (countable_basis α).countable := (exists_countable_basis α).some_spec.1 instance encodable_countable_basis [second_countable_topology α] : encodable (countable_basis α) := (countable_countable_basis α).to_encodable lemma empty_nmem_countable_basis [second_countable_topology α] : ∅ ∉ countable_basis α := (exists_countable_basis α).some_spec.2.1 lemma is_basis_countable_basis [second_countable_topology α] : is_topological_basis (countable_basis α) := (exists_countable_basis α).some_spec.2.2 lemma eq_generate_from_countable_basis [second_countable_topology α] : ‹topological_space α› = generate_from (countable_basis α) := (is_basis_countable_basis α).eq_generate_from variable {α} lemma is_open_of_mem_countable_basis [second_countable_topology α] {s : set α} (hs : s ∈ countable_basis α) : is_open s := (is_basis_countable_basis α).is_open hs lemma nonempty_of_mem_countable_basis [second_countable_topology α] {s : set α} (hs : s ∈ countable_basis α) : s.nonempty := nonempty_iff_ne_empty.2 $ ne_of_mem_of_not_mem hs $ empty_nmem_countable_basis α variable (α) @[priority 100] -- see Note [lower instance priority] instance second_countable_topology.to_first_countable_topology [second_countable_topology α] : first_countable_topology α := ⟨λ x, has_countable_basis.is_countably_generated $ ⟨(is_basis_countable_basis α).nhds_has_basis, (countable_countable_basis α).mono $ inter_subset_left _ _⟩⟩ /-- If `β` is a second-countable space, then its induced topology via `f` on `α` is also second-countable. -/ lemma second_countable_topology_induced (β) [t : topological_space β] [second_countable_topology β] (f : α → β) : @second_countable_topology α (t.induced f) := begin rcases second_countable_topology.is_open_generated_countable β with ⟨b, hb, eq⟩, refine { is_open_generated_countable := ⟨preimage f '' b, hb.image _, _⟩ }, rw [eq, induced_generate_from_eq] end instance subtype.second_countable_topology (s : set α) [second_countable_topology α] : second_countable_topology s := second_countable_topology_induced s α coe /- TODO: more fine grained instances for first_countable_topology, separable_space, t2_space, ... -/ instance {β : Type*} [topological_space β] [second_countable_topology α] [second_countable_topology β] : second_countable_topology (α × β) := ((is_basis_countable_basis α).prod (is_basis_countable_basis β)).second_countable_topology $ (countable_countable_basis α).image2 (countable_countable_basis β) _ instance {ι : Type*} {π : ι → Type*} [countable ι] [t : ∀a, topological_space (π a)] [∀a, second_countable_topology (π a)] : second_countable_topology (∀a, π a) := begin have : t = (λa, generate_from (countable_basis (π a))), from funext (assume a, (is_basis_countable_basis (π a)).eq_generate_from), rw [this, pi_generate_from_eq], constructor, refine ⟨_, _, rfl⟩, have : set.countable {T : set (Π i, π i) | ∃ (I : finset ι) (s : Π i : I, set (π i)), (∀ i, s i ∈ countable_basis (π i)) ∧ T = {f | ∀ i : I, f i ∈ s i}}, { simp only [set_of_exists, ← exists_prop], refine countable_Union (λ I, countable.bUnion _ (λ _ _, countable_singleton _)), change set.countable {s : Π i : I, set (π i) | ∀ i, s i ∈ countable_basis (π i)}, exact countable_pi (λ i, countable_countable_basis _) }, convert this using 1, ext1 T, split, { rintro ⟨s, I, hs, rfl⟩, refine ⟨I, λ i, s i, λ i, hs i i.2, _⟩, simp only [set.pi, set_coe.forall'], refl }, { rintro ⟨I, s, hs, rfl⟩, rcases @subtype.surjective_restrict ι (λ i, set (π i)) _ (λ i, i ∈ I) s with ⟨s, rfl⟩, exact ⟨s, I, λ i hi, hs ⟨i, hi⟩, set.ext $ λ f, subtype.forall⟩ } end @[priority 100] -- see Note [lower instance priority] instance second_countable_topology.to_separable_space [second_countable_topology α] : separable_space α := begin choose p hp using λ s : countable_basis α, nonempty_of_mem_countable_basis s.2, exact ⟨⟨range p, countable_range _, (is_basis_countable_basis α).dense_iff.2 $ λ o ho _, ⟨p ⟨o, ho⟩, hp _, mem_range_self _⟩⟩⟩ end variables {α} /-- A countable open cover induces a second-countable topology if all open covers are themselves second countable. -/ lemma second_countable_topology_of_countable_cover {ι} [encodable ι] {U : ι → set α} [∀ i, second_countable_topology (U i)] (Uo : ∀ i, is_open (U i)) (hc : (⋃ i, U i) = univ) : second_countable_topology α := begin have : is_topological_basis (⋃ i, image (coe : U i → α) '' (countable_basis (U i))), from is_topological_basis_of_cover Uo hc (λ i, is_basis_countable_basis (U i)), exact this.second_countable_topology (countable_Union $ λ i, (countable_countable_basis _).image _) end /-- In a second-countable space, an open set, given as a union of open sets, is equal to the union of countably many of those sets. -/ lemma is_open_Union_countable [second_countable_topology α] {ι} (s : ι → set α) (H : ∀ i, is_open (s i)) : ∃ T : set ι, T.countable ∧ (⋃ i ∈ T, s i) = ⋃ i, s i := begin let B := {b ∈ countable_basis α | ∃ i, b ⊆ s i}, choose f hf using λ b : B, b.2.2, haveI : encodable B := ((countable_countable_basis α).mono (sep_subset _ _)).to_encodable, refine ⟨_, countable_range f, (Union₂_subset_Union _ _).antisymm (sUnion_subset _)⟩, rintro _ ⟨i, rfl⟩ x xs, rcases (is_basis_countable_basis α).exists_subset_of_mem_open xs (H _) with ⟨b, hb, xb, bs⟩, exact ⟨_, ⟨_, rfl⟩, _, ⟨⟨⟨_, hb, _, bs⟩, rfl⟩, rfl⟩, hf _ (by exact xb)⟩ end lemma is_open_sUnion_countable [second_countable_topology α] (S : set (set α)) (H : ∀ s ∈ S, is_open s) : ∃ T : set (set α), T.countable ∧ T ⊆ S ∧ ⋃₀ T = ⋃₀ S := let ⟨T, cT, hT⟩ := is_open_Union_countable (λ s:S, s.1) (λ s, H s.1 s.2) in ⟨subtype.val '' T, cT.image _, image_subset_iff.2 $ λ ⟨x, xs⟩ xt, xs, by rwa [sUnion_image, sUnion_eq_Union]⟩ /-- In a topological space with second countable topology, if `f` is a function that sends each point `x` to a neighborhood of `x`, then for some countable set `s`, the neighborhoods `f x`, `x ∈ s`, cover the whole space. -/ lemma countable_cover_nhds [second_countable_topology α] {f : α → set α} (hf : ∀ x, f x ∈ 𝓝 x) : ∃ s : set α, s.countable ∧ (⋃ x ∈ s, f x) = univ := begin rcases is_open_Union_countable (λ x, interior (f x)) (λ x, is_open_interior) with ⟨s, hsc, hsU⟩, suffices : (⋃ x ∈ s, interior (f x)) = univ, from ⟨s, hsc, flip eq_univ_of_subset this $ Union₂_mono $ λ _ _, interior_subset⟩, simp only [hsU, eq_univ_iff_forall, mem_Union], exact λ x, ⟨x, mem_interior_iff_mem_nhds.2 (hf x)⟩ end lemma countable_cover_nhds_within [second_countable_topology α] {f : α → set α} {s : set α} (hf : ∀ x ∈ s, f x ∈ 𝓝[s] x) : ∃ t ⊆ s, t.countable ∧ s ⊆ (⋃ x ∈ t, f x) := begin have : ∀ x : s, coe ⁻¹' (f x) ∈ 𝓝 x, from λ x, preimage_coe_mem_nhds_subtype.2 (hf x x.2), rcases countable_cover_nhds this with ⟨t, htc, htU⟩, refine ⟨coe '' t, subtype.coe_image_subset _ _, htc.image _, λ x hx, _⟩, simp only [bUnion_image, eq_univ_iff_forall, ← preimage_Union, mem_preimage] at htU ⊢, exact htU ⟨x, hx⟩ end section sigma variables {ι : Type*} {E : ι → Type*} [∀ i, topological_space (E i)] omit t /-- In a disjoint union space `Σ i, E i`, one can form a topological basis by taking the union of topological bases on each of the parts of the space. -/ lemma is_topological_basis.sigma {s : Π (i : ι), set (set (E i))} (hs : ∀ i, is_topological_basis (s i)) : is_topological_basis (⋃ (i : ι), (λ u, ((sigma.mk i) '' u : set (Σ i, E i))) '' (s i)) := begin apply is_topological_basis_of_open_of_nhds, { assume u hu, obtain ⟨i, t, ts, rfl⟩ : ∃ (i : ι) (t : set (E i)), t ∈ s i ∧ sigma.mk i '' t = u, by simpa only [mem_Union, mem_image] using hu, exact is_open_map_sigma_mk _ ((hs i).is_open ts) }, { rintros ⟨i, x⟩ u hxu u_open, have hx : x ∈ sigma.mk i ⁻¹' u := hxu, obtain ⟨v, vs, xv, hv⟩ : ∃ (v : set (E i)) (H : v ∈ s i), x ∈ v ∧ v ⊆ sigma.mk i ⁻¹' u := (hs i).exists_subset_of_mem_open hx (is_open_sigma_iff.1 u_open i), exact ⟨(sigma.mk i) '' v, mem_Union.2 ⟨i, mem_image_of_mem _ vs⟩, mem_image_of_mem _ xv, image_subset_iff.2 hv⟩ } end /-- A countable disjoint union of second countable spaces is second countable. -/ instance [countable ι] [∀ i, second_countable_topology (E i)] : second_countable_topology (Σ i, E i) := begin let b := (⋃ (i : ι), (λ u, ((sigma.mk i) '' u : set (Σ i, E i))) '' (countable_basis (E i))), have A : is_topological_basis b := is_topological_basis.sigma (λ i, is_basis_countable_basis _), have B : b.countable := countable_Union (λ i, countable.image (countable_countable_basis _) _), exact A.second_countable_topology B, end end sigma section sum omit t variables {β : Type*} [topological_space α] [topological_space β] /-- In a sum space `α ⊕ β`, one can form a topological basis by taking the union of topological bases on each of the two components. -/ lemma is_topological_basis.sum {s : set (set α)} (hs : is_topological_basis s) {t : set (set β)} (ht : is_topological_basis t) : is_topological_basis (((λ u, sum.inl '' u) '' s) ∪ ((λ u, sum.inr '' u) '' t)) := begin apply is_topological_basis_of_open_of_nhds, { assume u hu, cases hu, { rcases hu with ⟨w, hw, rfl⟩, exact open_embedding_inl.is_open_map w (hs.is_open hw) }, { rcases hu with ⟨w, hw, rfl⟩, exact open_embedding_inr.is_open_map w (ht.is_open hw) } }, { rintros x u hxu u_open, cases x, { have h'x : x ∈ sum.inl ⁻¹' u := hxu, obtain ⟨v, vs, xv, vu⟩ : ∃ (v : set α) (H : v ∈ s), x ∈ v ∧ v ⊆ sum.inl ⁻¹' u := hs.exists_subset_of_mem_open h'x (is_open_sum_iff.1 u_open).1, exact ⟨sum.inl '' v, mem_union_left _ (mem_image_of_mem _ vs), mem_image_of_mem _ xv, image_subset_iff.2 vu⟩ }, { have h'x : x ∈ sum.inr ⁻¹' u := hxu, obtain ⟨v, vs, xv, vu⟩ : ∃ (v : set β) (H : v ∈ t), x ∈ v ∧ v ⊆ sum.inr ⁻¹' u := ht.exists_subset_of_mem_open h'x (is_open_sum_iff.1 u_open).2, exact ⟨sum.inr '' v, mem_union_right _ (mem_image_of_mem _ vs), mem_image_of_mem _ xv, image_subset_iff.2 vu⟩ } } end /-- A sum type of two second countable spaces is second countable. -/ instance [second_countable_topology α] [second_countable_topology β] : second_countable_topology (α ⊕ β) := begin let b := (λ u, sum.inl '' u) '' (countable_basis α) ∪ (λ u, sum.inr '' u) '' (countable_basis β), have A : is_topological_basis b := (is_basis_countable_basis α).sum (is_basis_countable_basis β), have B : b.countable := (countable.image (countable_countable_basis _) _).union (countable.image (countable_countable_basis _) _), exact A.second_countable_topology B, end end sum section quotient variables {X : Type*} [topological_space X] {Y : Type*} [topological_space Y] {π : X → Y} omit t /-- The image of a topological basis under an open quotient map is a topological basis. -/ lemma is_topological_basis.quotient_map {V : set (set X)} (hV : is_topological_basis V) (h' : quotient_map π) (h : is_open_map π) : is_topological_basis (set.image π '' V) := begin apply is_topological_basis_of_open_of_nhds, { rintros - ⟨U, U_in_V, rfl⟩, apply h U (hV.is_open U_in_V), }, { intros y U y_in_U U_open, obtain ⟨x, rfl⟩ := h'.surjective y, let W := π ⁻¹' U, have x_in_W : x ∈ W := y_in_U, have W_open : is_open W := U_open.preimage h'.continuous, obtain ⟨Z, Z_in_V, x_in_Z, Z_in_W⟩ := hV.exists_subset_of_mem_open x_in_W W_open, have πZ_in_U : π '' Z ⊆ U := (set.image_subset _ Z_in_W).trans (image_preimage_subset π U), exact ⟨π '' Z, ⟨Z, Z_in_V, rfl⟩, ⟨x, x_in_Z, rfl⟩, πZ_in_U⟩, }, end /-- A second countable space is mapped by an open quotient map to a second countable space. -/ lemma quotient_map.second_countable_topology [second_countable_topology X] (h' : quotient_map π) (h : is_open_map π) : second_countable_topology Y := { is_open_generated_countable := begin obtain ⟨V, V_countable, V_no_empty, V_generates⟩ := exists_countable_basis X, exact ⟨set.image π '' V, V_countable.image (set.image π), (V_generates.quotient_map h' h).eq_generate_from⟩, end } variables {S : setoid X} /-- The image of a topological basis "downstairs" in an open quotient is a topological basis. -/ lemma is_topological_basis.quotient {V : set (set X)} (hV : is_topological_basis V) (h : is_open_map (quotient.mk : X → quotient S)) : is_topological_basis (set.image (quotient.mk : X → quotient S) '' V) := hV.quotient_map quotient_map_quotient_mk h /-- An open quotient of a second countable space is second countable. -/ lemma quotient.second_countable_topology [second_countable_topology X] (h : is_open_map (quotient.mk : X → quotient S)) : second_countable_topology (quotient S) := quotient_map_quotient_mk.second_countable_topology h end quotient end topological_space open topological_space variables {α β : Type*} [topological_space α] [topological_space β] {f : α → β} protected lemma inducing.second_countable_topology [second_countable_topology β] (hf : inducing f) : second_countable_topology α := by { rw hf.1, exact second_countable_topology_induced α β f } protected lemma embedding.second_countable_topology [second_countable_topology β] (hf : embedding f) : second_countable_topology α := hf.1.second_countable_topology
c498a487b8c562cf86f6c8b6c46c6ffe5f370b73
4727251e0cd73359b15b664c3170e5d754078599
/src/data/polynomial/expand.lean
c5d4f12bd692db16fe882d9241b0622b7ba6b0a0
[ "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
9,204
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import ring_theory.polynomial.basic import ring_theory.ideal.local_ring import tactic.ring_exp /-! # Expand a polynomial by a factor of p, so `∑ aₙ xⁿ` becomes `∑ aₙ xⁿᵖ`. -/ universes u v w open_locale classical big_operators polynomial open finset namespace polynomial section comm_semiring variables (R : Type u) [comm_semiring R] {S : Type v} [comm_semiring S] (p q : ℕ) /-- Expand the polynomial by a factor of p, so `∑ aₙ xⁿ` becomes `∑ aₙ xⁿᵖ`. -/ noncomputable def expand : R[X] →ₐ[R] R[X] := { commutes' := λ r, eval₂_C _ _, .. (eval₂_ring_hom C (X ^ p) : R[X] →+* R[X]) } lemma coe_expand : (expand R p : R[X] → R[X]) = eval₂ C (X ^ p) := rfl variables {R} lemma expand_eq_sum {f : R[X]} : expand R p f = f.sum (λ e a, C a * (X ^ p) ^ e) := by { dsimp [expand, eval₂], refl, } @[simp] lemma expand_C (r : R) : expand R p (C r) = C r := eval₂_C _ _ @[simp] lemma expand_X : expand R p X = X ^ p := eval₂_X _ _ @[simp] lemma expand_monomial (r : R) : expand R p (monomial q r) = monomial (q * p) r := by simp_rw [monomial_eq_smul_X, alg_hom.map_smul, alg_hom.map_pow, expand_X, mul_comm, pow_mul] theorem expand_expand (f : R[X]) : expand R p (expand R q f) = expand R (p * q) f := polynomial.induction_on f (λ r, by simp_rw expand_C) (λ f g ihf ihg, by simp_rw [alg_hom.map_add, ihf, ihg]) (λ n r ih, by simp_rw [alg_hom.map_mul, expand_C, alg_hom.map_pow, expand_X, alg_hom.map_pow, expand_X, pow_mul]) theorem expand_mul (f : R[X]) : expand R (p * q) f = expand R p (expand R q f) := (expand_expand p q f).symm @[simp] theorem expand_zero (f : R[X]) : expand R 0 f = C (eval 1 f) := by simp [expand] @[simp] theorem expand_one (f : R[X]) : expand R 1 f = f := polynomial.induction_on f (λ r, by rw expand_C) (λ f g ihf ihg, by rw [alg_hom.map_add, ihf, ihg]) (λ n r ih, by rw [alg_hom.map_mul, expand_C, alg_hom.map_pow, expand_X, pow_one]) theorem expand_pow (f : R[X]) : expand R (p ^ q) f = (expand R p ^[q] f) := nat.rec_on q (by rw [pow_zero, expand_one, function.iterate_zero, id]) $ λ n ih, by rw [function.iterate_succ_apply', pow_succ, expand_mul, ih] theorem derivative_expand (f : R[X]) : (expand R p f).derivative = expand R p f.derivative * (p * X ^ (p - 1)) := by rw [coe_expand, derivative_eval₂_C, derivative_pow, derivative_X, mul_one] theorem coeff_expand {p : ℕ} (hp : 0 < p) (f : R[X]) (n : ℕ) : (expand R p f).coeff n = if p ∣ n then f.coeff (n / p) else 0 := begin simp only [expand_eq_sum], simp_rw [coeff_sum, ← pow_mul, C_mul_X_pow_eq_monomial, coeff_monomial, sum], split_ifs with h, { rw [finset.sum_eq_single (n/p), nat.mul_div_cancel' h, if_pos rfl], { intros b hb1 hb2, rw if_neg, intro hb3, apply hb2, rw [← hb3, nat.mul_div_cancel_left b hp] }, { intro hn, rw not_mem_support_iff.1 hn, split_ifs; refl } }, { rw finset.sum_eq_zero, intros k hk, rw if_neg, exact λ hkn, h ⟨k, hkn.symm⟩, }, end @[simp] theorem coeff_expand_mul {p : ℕ} (hp : 0 < p) (f : R[X]) (n : ℕ) : (expand R p f).coeff (n * p) = f.coeff n := by rw [coeff_expand hp, if_pos (dvd_mul_left _ _), nat.mul_div_cancel _ hp] @[simp] theorem coeff_expand_mul' {p : ℕ} (hp : 0 < p) (f : R[X]) (n : ℕ) : (expand R p f).coeff (p * n) = f.coeff n := by rw [mul_comm, coeff_expand_mul hp] theorem expand_inj {p : ℕ} (hp : 0 < p) {f g : R[X]} : expand R p f = expand R p g ↔ f = g := ⟨λ H, ext $ λ n, by rw [← coeff_expand_mul hp, H, coeff_expand_mul hp], congr_arg _⟩ theorem expand_eq_zero {p : ℕ} (hp : 0 < p) {f : R[X]} : expand R p f = 0 ↔ f = 0 := by rw [← (expand R p).map_zero, expand_inj hp, alg_hom.map_zero] theorem expand_eq_C {p : ℕ} (hp : 0 < p) {f : R[X]} {r : R} : expand R p f = C r ↔ f = C r := by rw [← expand_C, expand_inj hp, expand_C] theorem nat_degree_expand (p : ℕ) (f : R[X]) : (expand R p f).nat_degree = f.nat_degree * p := begin cases p.eq_zero_or_pos with hp hp, { rw [hp, coe_expand, pow_zero, mul_zero, ← C_1, eval₂_hom, nat_degree_C] }, by_cases hf : f = 0, { rw [hf, alg_hom.map_zero, nat_degree_zero, zero_mul] }, have hf1 : expand R p f ≠ 0 := mt (expand_eq_zero hp).1 hf, rw [← with_bot.coe_eq_coe, ← degree_eq_nat_degree hf1], refine le_antisymm ((degree_le_iff_coeff_zero _ _).2 $ λ n hn, _) _, { rw coeff_expand hp, split_ifs with hpn, { rw coeff_eq_zero_of_nat_degree_lt, contrapose! hn, rw [with_bot.coe_le_coe, ← nat.div_mul_cancel hpn], exact nat.mul_le_mul_right p hn }, { refl } }, { refine le_degree_of_ne_zero _, rw [coeff_expand_mul hp, ← leading_coeff], exact mt leading_coeff_eq_zero.1 hf } end lemma monic.expand {p : ℕ} {f : R[X]} (hp : 0 < p) (h : f.monic) : (expand R p f).monic := begin rw [monic.def, leading_coeff, nat_degree_expand, coeff_expand hp], simp [hp, h], end theorem map_expand {p : ℕ} {f : R →+* S} {q : R[X]} : map f (expand R p q) = expand S p (map f q) := begin by_cases hp : p = 0, { simp [hp] }, ext, rw [coeff_map, coeff_expand (nat.pos_of_ne_zero hp), coeff_expand (nat.pos_of_ne_zero hp)], split_ifs; simp, end /-- Expansion is injective. -/ lemma expand_injective {n : ℕ} (hn : 0 < n) : function.injective (expand R n) := λ g g' h, begin ext, have h' : (expand R n g).coeff (n * n_1) = (expand R n g').coeff (n * n_1) := begin apply polynomial.ext_iff.1, exact h, end, rw [polynomial.coeff_expand hn g (n * n_1), polynomial.coeff_expand hn g' (n * n_1)] at h', simp only [if_true, dvd_mul_right] at h', rw (nat.mul_div_right n_1 hn) at h', exact h', end @[simp] lemma expand_eval (p : ℕ) (P : R[X]) (r : R) : eval r (expand R p P) = eval (r ^ p) P := begin refine polynomial.induction_on P (λ a, by simp) (λ f g hf hg, _) (λ n a h, by simp), rw [alg_hom.map_add, eval_add, eval_add, hf, hg] end /-- The opposite of `expand`: sends `∑ aₙ xⁿᵖ` to `∑ aₙ xⁿ`. -/ noncomputable def contract (p : ℕ) (f : R[X]) : R[X] := ∑ n in range (f.nat_degree + 1), monomial n (f.coeff (n * p)) theorem coeff_contract {p : ℕ} (hp : p ≠ 0) (f : R[X]) (n : ℕ) : (contract p f).coeff n = f.coeff (n * p) := begin simp only [contract, coeff_monomial, sum_ite_eq', finset_sum_coeff, mem_range, not_lt, ite_eq_left_iff], assume hn, apply (coeff_eq_zero_of_nat_degree_lt _).symm, calc f.nat_degree < f.nat_degree + 1 : nat.lt_succ_self _ ... ≤ n * 1 : by simpa only [mul_one] using hn ... ≤ n * p : mul_le_mul_of_nonneg_left (show 1 ≤ p, from hp.bot_lt) (zero_le n) end theorem contract_expand {f : R[X]} (hp : p ≠ 0) : contract p (expand R p f) = f := begin ext, simp [coeff_contract hp, coeff_expand hp.bot_lt, nat.mul_div_cancel _ hp.bot_lt] end section char_p variable [char_p R p] theorem expand_contract [no_zero_divisors R] {f : R[X]} (hf : f.derivative = 0) (hp : p ≠ 0) : expand R p (contract p f) = f := begin ext n, rw [coeff_expand hp.bot_lt, coeff_contract hp], split_ifs with h, { rw nat.div_mul_cancel h }, { cases n, { exact absurd (dvd_zero p) h }, have := coeff_derivative f n, rw [hf, coeff_zero, zero_eq_mul] at this, cases this, { rw this }, rw [← nat.cast_succ, char_p.cast_eq_zero_iff R p] at this, exact absurd this h } end variable [hp : fact p.prime] include hp theorem expand_char (f : R[X]) : map (frobenius R p) (expand R p f) = f ^ p := begin refine f.induction_on' (λ a b ha hb, _) (λ n a, _), { rw [alg_hom.map_add, polynomial.map_add, ha, hb, add_pow_char], }, { rw [expand_monomial, map_monomial, monomial_eq_C_mul_X, monomial_eq_C_mul_X, mul_pow, ← C.map_pow, frobenius_def], ring_exp } end theorem map_expand_pow_char (f : R[X]) (n : ℕ) : map ((frobenius R p) ^ n) (expand R (p ^ n) f) = f ^ (p ^ n) := begin induction n, { simp [ring_hom.one_def] }, symmetry, rw [pow_succ', pow_mul, ← n_ih, ← expand_char, pow_succ, ring_hom.mul_def, ← map_map, mul_comm, expand_mul, ← map_expand] end end char_p end comm_semiring section is_domain variables (R : Type u) [comm_ring R] [is_domain R] theorem is_local_ring_hom_expand {p : ℕ} (hp : 0 < p) : is_local_ring_hom (↑(expand R p) : R[X] →+* R[X]) := begin refine ⟨λ f hf1, _⟩, rw ← coe_fn_coe_base at hf1, have hf2 := eq_C_of_degree_eq_zero (degree_eq_zero_of_is_unit hf1), rw [coeff_expand hp, if_pos (dvd_zero _), p.zero_div] at hf2, rw [hf2, is_unit_C] at hf1, rw expand_eq_C hp at hf2, rwa [hf2, is_unit_C] end variable {R} theorem of_irreducible_expand {p : ℕ} (hp : p ≠ 0) {f : R[X]} (hf : irreducible (expand R p f)) : irreducible f := @@of_irreducible_map _ _ _ (is_local_ring_hom_expand R hp.bot_lt) hf theorem of_irreducible_expand_pow {p : ℕ} (hp : p ≠ 0) {f : R[X]} {n : ℕ} : irreducible (expand R (p ^ n) f) → irreducible f := nat.rec_on n (λ hf, by rwa [pow_zero, expand_one] at hf) $ λ n ih hf, ih $ of_irreducible_expand hp $ by { rw pow_succ at hf, rwa [expand_expand] } end is_domain end polynomial
646587b4084bad0c1ed54eff4179d300ddfa6958
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/section3.lean
0623525d24cf83a9d10a7fc7f5a3ce21de17e6ff
[ "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
107
lean
section parameter (A : Type) definition foo := A theorem bar {X : Type} {A : X} : foo := sorry end
5b9cb912d9f0ba7153a25a157884214fde8d8abc
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/cc_proj.lean
cf59ee3a4b9169af888d6b4c6943b3801238eae4
[ "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
340
lean
open tactic example (a b c d : nat) (f : nat → nat × nat) : (f d).1 ≠ a → f d = (b, c) → b = a → false := by cc def ex (a b c d : nat) (f : nat → nat × nat) : (f d).2 ≠ a → f d = (b, c) → c = a → false := by cc example (a b c : nat) (f : nat → nat) : (f b, c).1 ≠ f a → f b = f c → a = c → false := by cc
9748d9a559233bc6ee3445142ffc26e645b8eb31
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/category_theory/adjunction/basic.lean
cfe66ba338fb0cdbe7f6c02a70cf541b7fa8c999
[ "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
17,413
lean
/- Copyright (c) 2019 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Johan Commelin, Bhavik Mehta -/ import category_theory.equivalence import data.equiv.basic namespace category_theory open category universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation local attribute [elab_simple] whisker_left whisker_right variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] /-- `F ⊣ G` represents the data of an adjunction between two functors `F : C ⥤ D` and `G : D ⥤ C`. `F` is the left adjoint and `G` is the right adjoint. To construct an `adjunction` between two functors, it's often easier to instead use the constructors `mk_of_hom_equiv` or `mk_of_unit_counit`. To construct a left adjoint, there are also constructors `left_adjoint_of_equiv` and `adjunction_of_equiv_left` (as well as their duals) which can be simpler in practice. Uniqueness of adjoints is shown in `category_theory.adjunction.opposites`. -/ structure adjunction (F : C ⥤ D) (G : D ⥤ C) := (hom_equiv : Π (X Y), (F.obj X ⟶ Y) ≃ (X ⟶ G.obj Y)) (unit : 𝟭 C ⟶ F.comp G) (counit : G.comp F ⟶ 𝟭 D) (hom_equiv_unit' : Π {X Y f}, (hom_equiv X Y) f = (unit : _ ⟶ _).app X ≫ G.map f . obviously) (hom_equiv_counit' : Π {X Y g}, (hom_equiv X Y).symm g = F.map g ≫ counit.app Y . obviously) infix ` ⊣ `:15 := adjunction /-- A class giving a chosen right adjoint to the functor `left`. -/ class is_left_adjoint (left : C ⥤ D) := (right : D ⥤ C) (adj : left ⊣ right) /-- A class giving a chosen left adjoint to the functor `right`. -/ class is_right_adjoint (right : D ⥤ C) := (left : C ⥤ D) (adj : left ⊣ right) /-- Extract the left adjoint from the instance giving the chosen adjoint. -/ def left_adjoint (R : D ⥤ C) [is_right_adjoint R] : C ⥤ D := is_right_adjoint.left R /-- Extract the right adjoint from the instance giving the chosen adjoint. -/ def right_adjoint (L : C ⥤ D) [is_left_adjoint L] : D ⥤ C := is_left_adjoint.right L /-- The adjunction associated to a functor known to be a left adjoint. -/ def adjunction.of_left_adjoint (left : C ⥤ D) [is_left_adjoint left] : adjunction left (right_adjoint left) := is_left_adjoint.adj /-- The adjunction associated to a functor known to be a right adjoint. -/ def adjunction.of_right_adjoint (right : C ⥤ D) [is_right_adjoint right] : adjunction (left_adjoint right) right := is_right_adjoint.adj namespace adjunction restate_axiom hom_equiv_unit' restate_axiom hom_equiv_counit' attribute [simp, priority 10] hom_equiv_unit hom_equiv_counit section variables {F : C ⥤ D} {G : D ⥤ C} (adj : F ⊣ G) {X' X : C} {Y Y' : D} @[simp, priority 10] lemma hom_equiv_naturality_left_symm (f : X' ⟶ X) (g : X ⟶ G.obj Y) : (adj.hom_equiv X' Y).symm (f ≫ g) = F.map f ≫ (adj.hom_equiv X Y).symm g := by rw [hom_equiv_counit, F.map_comp, assoc, adj.hom_equiv_counit.symm] @[simp] lemma hom_equiv_naturality_left (f : X' ⟶ X) (g : F.obj X ⟶ Y) : (adj.hom_equiv X' Y) (F.map f ≫ g) = f ≫ (adj.hom_equiv X Y) g := by rw [← equiv.eq_symm_apply]; simp [-hom_equiv_unit] @[simp, priority 10] lemma hom_equiv_naturality_right (f : F.obj X ⟶ Y) (g : Y ⟶ Y') : (adj.hom_equiv X Y') (f ≫ g) = (adj.hom_equiv X Y) f ≫ G.map g := by rw [hom_equiv_unit, G.map_comp, ← assoc, ←hom_equiv_unit] @[simp] lemma hom_equiv_naturality_right_symm (f : X ⟶ G.obj Y) (g : Y ⟶ Y') : (adj.hom_equiv X Y').symm (f ≫ G.map g) = (adj.hom_equiv X Y).symm f ≫ g := by rw [equiv.symm_apply_eq]; simp [-hom_equiv_counit] @[simp] lemma left_triangle : (whisker_right adj.unit F) ≫ (whisker_left F adj.counit) = nat_trans.id _ := begin ext, dsimp, erw [← adj.hom_equiv_counit, equiv.symm_apply_eq, adj.hom_equiv_unit], simp end @[simp] lemma right_triangle : (whisker_left G adj.unit) ≫ (whisker_right adj.counit G) = nat_trans.id _ := begin ext, dsimp, erw [← adj.hom_equiv_unit, ← equiv.eq_symm_apply, adj.hom_equiv_counit], simp end @[simp, reassoc] lemma left_triangle_components : F.map (adj.unit.app X) ≫ adj.counit.app (F.obj X) = 𝟙 (F.obj X) := congr_arg (λ (t : nat_trans _ (𝟭 C ⋙ F)), t.app X) adj.left_triangle @[simp, reassoc] lemma right_triangle_components {Y : D} : adj.unit.app (G.obj Y) ≫ G.map (adj.counit.app Y) = 𝟙 (G.obj Y) := congr_arg (λ (t : nat_trans _ (G ⋙ 𝟭 C)), t.app Y) adj.right_triangle @[simp, reassoc] lemma counit_naturality {X Y : D} (f : X ⟶ Y) : F.map (G.map f) ≫ (adj.counit).app Y = (adj.counit).app X ≫ f := adj.counit.naturality f @[simp, reassoc] lemma unit_naturality {X Y : C} (f : X ⟶ Y) : (adj.unit).app X ≫ G.map (F.map f) = f ≫ (adj.unit).app Y := (adj.unit.naturality f).symm lemma hom_equiv_apply_eq {A : C} {B : D} (f : F.obj A ⟶ B) (g : A ⟶ G.obj B) : adj.hom_equiv A B f = g ↔ f = (adj.hom_equiv A B).symm g := ⟨λ h, by {cases h, simp}, λ h, by {cases h, simp}⟩ lemma eq_hom_equiv_apply {A : C} {B : D} (f : F.obj A ⟶ B) (g : A ⟶ G.obj B) : g = adj.hom_equiv A B f ↔ (adj.hom_equiv A B).symm g = f := ⟨λ h, by {cases h, simp}, λ h, by {cases h, simp}⟩ end end adjunction namespace adjunction structure core_hom_equiv (F : C ⥤ D) (G : D ⥤ C) := (hom_equiv : Π (X Y), (F.obj X ⟶ Y) ≃ (X ⟶ G.obj Y)) (hom_equiv_naturality_left_symm' : Π {X' X Y} (f : X' ⟶ X) (g : X ⟶ G.obj Y), (hom_equiv X' Y).symm (f ≫ g) = F.map f ≫ (hom_equiv X Y).symm g . obviously) (hom_equiv_naturality_right' : Π {X Y Y'} (f : F.obj X ⟶ Y) (g : Y ⟶ Y'), (hom_equiv X Y') (f ≫ g) = (hom_equiv X Y) f ≫ G.map g . obviously) namespace core_hom_equiv restate_axiom hom_equiv_naturality_left_symm' restate_axiom hom_equiv_naturality_right' attribute [simp, priority 10] hom_equiv_naturality_left_symm hom_equiv_naturality_right variables {F : C ⥤ D} {G : D ⥤ C} (adj : core_hom_equiv F G) {X' X : C} {Y Y' : D} @[simp] lemma hom_equiv_naturality_left (f : X' ⟶ X) (g : F.obj X ⟶ Y) : (adj.hom_equiv X' Y) (F.map f ≫ g) = f ≫ (adj.hom_equiv X Y) g := by rw [← equiv.eq_symm_apply]; simp @[simp] lemma hom_equiv_naturality_right_symm (f : X ⟶ G.obj Y) (g : Y ⟶ Y') : (adj.hom_equiv X Y').symm (f ≫ G.map g) = (adj.hom_equiv X Y).symm f ≫ g := by rw [equiv.symm_apply_eq]; simp end core_hom_equiv structure core_unit_counit (F : C ⥤ D) (G : D ⥤ C) := (unit : 𝟭 C ⟶ F.comp G) (counit : G.comp F ⟶ 𝟭 D) (left_triangle' : whisker_right unit F ≫ (functor.associator F G F).hom ≫ whisker_left F counit = nat_trans.id (𝟭 C ⋙ F) . obviously) (right_triangle' : whisker_left G unit ≫ (functor.associator G F G).inv ≫ whisker_right counit G = nat_trans.id (G ⋙ 𝟭 C) . obviously) namespace core_unit_counit restate_axiom left_triangle' restate_axiom right_triangle' attribute [simp] left_triangle right_triangle end core_unit_counit variables {F : C ⥤ D} {G : D ⥤ C} /-- Construct an adjunction between `F` and `G` out of a natural bijection between each `F.obj X ⟶ Y` and `X ⟶ G.obj Y`. -/ def mk_of_hom_equiv (adj : core_hom_equiv F G) : F ⊣ G := { unit := { app := λ X, (adj.hom_equiv X (F.obj X)) (𝟙 (F.obj X)), naturality' := begin intros, erw [← adj.hom_equiv_naturality_left, ← adj.hom_equiv_naturality_right], dsimp, simp -- See note [dsimp, simp]. end }, counit := { app := λ Y, (adj.hom_equiv _ _).inv_fun (𝟙 (G.obj Y)), naturality' := begin intros, erw [← adj.hom_equiv_naturality_left_symm, ← adj.hom_equiv_naturality_right_symm], dsimp, simp end }, hom_equiv_unit' := λ X Y f, by erw [← adj.hom_equiv_naturality_right]; simp, hom_equiv_counit' := λ X Y f, by erw [← adj.hom_equiv_naturality_left_symm]; simp, .. adj } /-- Construct an adjunction between functors `F` and `G` given a unit and counit for the adjunction satisfying the triangle identities. -/ def mk_of_unit_counit (adj : core_unit_counit F G) : F ⊣ G := { hom_equiv := λ X Y, { to_fun := λ f, adj.unit.app X ≫ G.map f, inv_fun := λ g, F.map g ≫ adj.counit.app Y, left_inv := λ f, begin change F.map (_ ≫ _) ≫ _ = _, rw [F.map_comp, assoc, ←functor.comp_map, adj.counit.naturality, ←assoc], convert id_comp f, have t := congr_arg (λ t : nat_trans _ _, t.app _) adj.left_triangle, dsimp at t, simp only [id_comp] at t, exact t, end, right_inv := λ g, begin change _ ≫ G.map (_ ≫ _) = _, rw [G.map_comp, ←assoc, ←functor.comp_map, ←adj.unit.naturality, assoc], convert comp_id g, have t := congr_arg (λ t : nat_trans _ _, t.app _) adj.right_triangle, dsimp at t, simp only [id_comp] at t, exact t, end }, .. adj } /-- The adjunction between the identity functor on a category and itself. -/ def id : 𝟭 C ⊣ 𝟭 C := { hom_equiv := λ X Y, equiv.refl _, unit := 𝟙 _, counit := 𝟙 _ } -- Satisfy the inhabited linter. instance : inhabited (adjunction (𝟭 C) (𝟭 C)) := ⟨id⟩ /-- If F and G are naturally isomorphic functors, establish an equivalence of hom-sets. -/ def equiv_homset_left_of_nat_iso {F F' : C ⥤ D} (iso : F ≅ F') {X : C} {Y : D} : (F.obj X ⟶ Y) ≃ (F'.obj X ⟶ Y) := { to_fun := λ f, iso.inv.app _ ≫ f, inv_fun := λ g, iso.hom.app _ ≫ g, left_inv := λ f, by simp, right_inv := λ g, by simp } @[simp] lemma equiv_homset_left_of_nat_iso_apply {F F' : C ⥤ D} (iso : F ≅ F') {X : C} {Y : D} (f : F.obj X ⟶ Y) : (equiv_homset_left_of_nat_iso iso) f = iso.inv.app _ ≫ f := rfl @[simp] lemma equiv_homset_left_of_nat_iso_symm_apply {F F' : C ⥤ D} (iso : F ≅ F') {X : C} {Y : D} (g : F'.obj X ⟶ Y) : (equiv_homset_left_of_nat_iso iso).symm g = iso.hom.app _ ≫ g := rfl /-- If G and H are naturally isomorphic functors, establish an equivalence of hom-sets. -/ def equiv_homset_right_of_nat_iso {G G' : D ⥤ C} (iso : G ≅ G') {X : C} {Y : D} : (X ⟶ G.obj Y) ≃ (X ⟶ G'.obj Y) := { to_fun := λ f, f ≫ iso.hom.app _, inv_fun := λ g, g ≫ iso.inv.app _, left_inv := λ f, by simp, right_inv := λ g, by simp } @[simp] lemma equiv_homset_right_of_nat_iso_apply {G G' : D ⥤ C} (iso : G ≅ G') {X : C} {Y : D} (f : X ⟶ G.obj Y) : (equiv_homset_right_of_nat_iso iso) f = f ≫ iso.hom.app _ := rfl @[simp] lemma equiv_homset_right_of_nat_iso_symm_apply {G G' : D ⥤ C} (iso : G ≅ G') {X : C} {Y : D} (g : X ⟶ G'.obj Y) : (equiv_homset_right_of_nat_iso iso).symm g = g ≫ iso.inv.app _ := rfl /-- Transport an adjunction along an natural isomorphism on the left. -/ def of_nat_iso_left {F G : C ⥤ D} {H : D ⥤ C} (adj : F ⊣ H) (iso : F ≅ G) : G ⊣ H := adjunction.mk_of_hom_equiv { hom_equiv := λ X Y, (equiv_homset_left_of_nat_iso iso.symm).trans (adj.hom_equiv X Y) } /-- Transport an adjunction along an natural isomorphism on the right. -/ def of_nat_iso_right {F : C ⥤ D} {G H : D ⥤ C} (adj : F ⊣ G) (iso : G ≅ H) : F ⊣ H := adjunction.mk_of_hom_equiv { hom_equiv := λ X Y, (adj.hom_equiv X Y).trans (equiv_homset_right_of_nat_iso iso) } /-- Transport being a right adjoint along a natural isomorphism. -/ def right_adjoint_of_nat_iso {F G : C ⥤ D} (h : F ≅ G) [r : is_right_adjoint F] : is_right_adjoint G := { left := r.left, adj := of_nat_iso_right r.adj h } /-- Transport being a left adjoint along a natural isomorphism. -/ def left_adjoint_of_nat_iso {F G : C ⥤ D} (h : F ≅ G) [r : is_left_adjoint F] : is_left_adjoint G := { right := r.right, adj := of_nat_iso_left r.adj h } section variables {E : Type u₃} [ℰ : category.{v₃} E] (H : D ⥤ E) (I : E ⥤ D) /-- Show that adjunctions can be composed. -/ def comp (adj₁ : F ⊣ G) (adj₂ : H ⊣ I) : F ⋙ H ⊣ I ⋙ G := { hom_equiv := λ X Z, equiv.trans (adj₂.hom_equiv _ _) (adj₁.hom_equiv _ _), unit := adj₁.unit ≫ (whisker_left F $ whisker_right adj₂.unit G) ≫ (functor.associator _ _ _).inv, counit := (functor.associator _ _ _).hom ≫ (whisker_left I $ whisker_right adj₁.counit H) ≫ adj₂.counit } /-- If `F` and `G` are left adjoints then `F ⋙ G` is a left adjoint too. -/ instance left_adjoint_of_comp {E : Type u₃} [ℰ : category.{v₃} E] (F : C ⥤ D) (G : D ⥤ E) [Fl : is_left_adjoint F] [Gl : is_left_adjoint G] : is_left_adjoint (F ⋙ G) := { right := Gl.right ⋙ Fl.right, adj := comp _ _ Fl.adj Gl.adj } /-- If `F` and `G` are right adjoints then `F ⋙ G` is a right adjoint too. -/ instance right_adjoint_of_comp {E : Type u₃} [ℰ : category.{v₃} E] {F : C ⥤ D} {G : D ⥤ E} [Fr : is_right_adjoint F] [Gr : is_right_adjoint G] : is_right_adjoint (F ⋙ G) := { left := Gr.left ⋙ Fr.left, adj := comp _ _ Gr.adj Fr.adj } end section construct_left -- Construction of a left adjoint. In order to construct a left -- adjoint to a functor G : D → C, it suffices to give the object part -- of a functor F : C → D together with isomorphisms Hom(FX, Y) ≃ -- Hom(X, GY) natural in Y. The action of F on morphisms can be -- constructed from this data. variables {F_obj : C → D} {G} variables (e : Π X Y, (F_obj X ⟶ Y) ≃ (X ⟶ G.obj Y)) variables (he : ∀ X Y Y' g h, e X Y' (h ≫ g) = e X Y h ≫ G.map g) include he private lemma he' {X Y Y'} (f g) : (e X Y').symm (f ≫ G.map g) = (e X Y).symm f ≫ g := by intros; rw [equiv.symm_apply_eq, he]; simp /-- Construct a left adjoint functor to `G`, given the functor's value on objects `F_obj` and a bijection `e` between `F_obj X ⟶ Y` and `X ⟶ G.obj Y` satisfying a naturality law `he : ∀ X Y Y' g h, e X Y' (h ≫ g) = e X Y h ≫ G.map g`. Dual to `right_adjoint_of_equiv`. -/ def left_adjoint_of_equiv : C ⥤ D := { obj := F_obj, map := λ X X' f, (e X (F_obj X')).symm (f ≫ e X' (F_obj X') (𝟙 _)), map_comp' := λ X X' X'' f f', begin rw [equiv.symm_apply_eq, he, equiv.apply_symm_apply], conv { to_rhs, rw [assoc, ←he, id_comp, equiv.apply_symm_apply] }, simp end } /-- Show that the functor given by `left_adjoint_of_equiv` is indeed left adjoint to `G`. Dual to `adjunction_of_equiv_right`. -/ def adjunction_of_equiv_left : left_adjoint_of_equiv e he ⊣ G := mk_of_hom_equiv { hom_equiv := e, hom_equiv_naturality_left_symm' := begin intros, erw [← he' e he, ← equiv.apply_eq_iff_eq], simp [(he _ _ _ _ _).symm] end } end construct_left section construct_right -- Construction of a right adjoint, analogous to the above. variables {F} {G_obj : D → C} variables (e : Π X Y, (F.obj X ⟶ Y) ≃ (X ⟶ G_obj Y)) variables (he : ∀ X' X Y f g, e X' Y (F.map f ≫ g) = f ≫ e X Y g) include he private lemma he' {X' X Y} (f g) : F.map f ≫ (e X Y).symm g = (e X' Y).symm (f ≫ g) := by intros; rw [equiv.eq_symm_apply, he]; simp /-- Construct a right adjoint functor to `F`, given the functor's value on objects `G_obj` and a bijection `e` between `F.obj X ⟶ Y` and `X ⟶ G_obj Y` satisfying a naturality law `he : ∀ X Y Y' g h, e X' Y (F.map f ≫ g) = f ≫ e X Y g`. Dual to `left_adjoint_of_equiv`. -/ def right_adjoint_of_equiv : D ⥤ C := { obj := G_obj, map := λ Y Y' g, (e (G_obj Y) Y') ((e (G_obj Y) Y).symm (𝟙 _) ≫ g), map_comp' := λ Y Y' Y'' g g', begin rw [← equiv.eq_symm_apply, ← he' e he, equiv.symm_apply_apply], conv { to_rhs, rw [← assoc, he' e he, comp_id, equiv.symm_apply_apply] }, simp end } /-- Show that the functor given by `right_adjoint_of_equiv` is indeed right adjoint to `F`. Dual to `adjunction_of_equiv_left`. -/ def adjunction_of_equiv_right : F ⊣ right_adjoint_of_equiv e he := mk_of_hom_equiv { hom_equiv := e, hom_equiv_naturality_left_symm' := by intros; rw [equiv.symm_apply_eq, he]; simp, hom_equiv_naturality_right' := begin intros X Y Y' g h, erw [←he, equiv.apply_eq_iff_eq, ←assoc, he' e he, comp_id, equiv.symm_apply_apply] end } end construct_right end adjunction open adjunction namespace equivalence /-- The adjunction given by an equivalence of categories. (To obtain the opposite adjunction, simply use `e.symm.to_adjunction`. -/ def to_adjunction (e : C ≌ D) : e.functor ⊣ e.inverse := mk_of_unit_counit ⟨e.unit, e.counit, by { ext, dsimp, simp only [id_comp], exact e.functor_unit_comp _, }, by { ext, dsimp, simp only [id_comp], exact e.unit_inverse_comp _, }⟩ end equivalence namespace functor /-- An equivalence `E` is left adjoint to its inverse. -/ def adjunction (E : C ⥤ D) [is_equivalence E] : E ⊣ E.inv := (E.as_equivalence).to_adjunction /-- If `F` is an equivalence, it's a left adjoint. -/ @[priority 10] instance left_adjoint_of_equivalence {F : C ⥤ D} [is_equivalence F] : is_left_adjoint F := { right := _, adj := functor.adjunction F } @[simp] lemma right_adjoint_of_is_equivalence {F : C ⥤ D} [is_equivalence F] : right_adjoint F = inv F := rfl /-- If `F` is an equivalence, it's a right adjoint. -/ @[priority 10] instance right_adjoint_of_equivalence {F : C ⥤ D} [is_equivalence F] : is_right_adjoint F := { left := _, adj := functor.adjunction F.inv } @[simp] lemma left_adjoint_of_is_equivalence {F : C ⥤ D} [is_equivalence F] : left_adjoint F = inv F := rfl end functor end category_theory
45005284bd70fbcdd40c0c261eebd6b35b527643
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/category_theory/limits/preserves.lean
98142ef0d23437bd158587d018fdf6171ae6caaa
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
11,075
lean
-- Copyright (c) 2018 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison, Reid Barton -- Preservation and reflection of (co)limits. import category_theory.limits.limits open category_theory namespace category_theory.limits universes v u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation variables {C : Sort u₁} [𝒞 : category.{v+1} C] variables {D : Sort u₂} [𝒟 : category.{v+1} D] include 𝒞 𝒟 variables {J : Type v} [small_category J] {K : J ⥤ C} /- Note on "preservation of (co)limits" There are various distinct notions of "preserving limits". The one we aim to capture here is: A functor F : C → D "preserves limits" if it sends every limit cone in C to a limit cone in D. Informally, F preserves all the limits which exist in C. Note that: * Of course, we do not want to require F to *strictly* take chosen limit cones of C to chosen limit cones of D. Indeed, the above definition makes no reference to a choice of limit cones so it makes sense without any conditions on C or D. * Some diagrams in C may have no limit. In this case, there is no condition on the behavior of F on such diagrams. There are other notions (such as "flat functor") which impose conditions also on diagrams in C with no limits, but these are not considered here. In order to be able to express the property of preserving limits of a certain form, we say that a functor F preserves the limit of a diagram K if F sends every limit cone on K to a limit cone. This is vacuously satisfied when K does not admit a limit, which is consistent with the above definition of "preserves limits". -/ class preserves_limit (K : J ⥤ C) (F : C ⥤ D) : Type (max u₁ u₂ v) := (preserves : Π {c : cone K}, is_limit c → is_limit (F.map_cone c)) class preserves_colimit (K : J ⥤ C) (F : C ⥤ D) : Type (max u₁ u₂ v) := (preserves : Π {c : cocone K}, is_colimit c → is_colimit (F.map_cocone c)) class preserves_limits_of_shape (J : Type v) [small_category J] (F : C ⥤ D) : Type (max u₁ u₂ v) := (preserves_limit : Π {K : J ⥤ C}, preserves_limit K F) class preserves_colimits_of_shape (J : Type v) [small_category J] (F : C ⥤ D) : Type (max u₁ u₂ v) := (preserves_colimit : Π {K : J ⥤ C}, preserves_colimit K F) class preserves_limits (F : C ⥤ D) : Type (max u₁ u₂ (v+1)) := (preserves_limits_of_shape : Π {J : Type v} {𝒥 : small_category J}, by exactI preserves_limits_of_shape J F) class preserves_colimits (F : C ⥤ D) : Type (max u₁ u₂ (v+1)) := (preserves_colimits_of_shape : Π {J : Type v} {𝒥 : small_category J}, by exactI preserves_colimits_of_shape J F) instance preserves_limit_subsingleton (K : J ⥤ C) (F : C ⥤ D) : subsingleton (preserves_limit K F) := by split; rintros ⟨a⟩ ⟨b⟩; congr instance preserves_colimit_subsingleton (K : J ⥤ C) (F : C ⥤ D) : subsingleton (preserves_colimit K F) := by split; rintros ⟨a⟩ ⟨b⟩; congr instance preserves_limits_of_shape_subsingleton (J : Type v) [small_category J] (F : C ⥤ D) : subsingleton (preserves_limits_of_shape J F) := by { split, intros, cases a, cases b, congr } instance preserves_colimits_of_shape_subsingleton (J : Type v) [small_category J] (F : C ⥤ D) : subsingleton (preserves_colimits_of_shape J F) := by { split, intros, cases a, cases b, congr } instance preserves_limits_subsingleton (F : C ⥤ D) : subsingleton (preserves_limits F) := by { split, intros, cases a, cases b, congr, funext J 𝒥, resetI, apply subsingleton.elim } instance preserves_colimits_subsingleton (F : C ⥤ D) : subsingleton (preserves_colimits F) := by { split, intros, cases a, cases b, congr, funext J 𝒥, resetI, apply subsingleton.elim } instance preserves_limit_of_preserves_limits_of_shape (F : C ⥤ D) [H : preserves_limits_of_shape J F] : preserves_limit K F := preserves_limits_of_shape.preserves_limit J F instance preserves_colimit_of_preserves_colimits_of_shape (F : C ⥤ D) [H : preserves_colimits_of_shape J F] : preserves_colimit K F := preserves_colimits_of_shape.preserves_colimit J F instance preserves_limits_of_shape_of_preserves_limits (F : C ⥤ D) [H : preserves_limits F] : preserves_limits_of_shape J F := preserves_limits.preserves_limits_of_shape F instance preserves_colimits_of_shape_of_preserves_colimits (F : C ⥤ D) [H : preserves_colimits F] : preserves_colimits_of_shape J F := preserves_colimits.preserves_colimits_of_shape F instance id_preserves_limits : preserves_limits (functor.id C) := { preserves_limits_of_shape := λ J 𝒥, { preserves_limit := λ K, by exactI ⟨λ c h, ⟨λ s, h.lift ⟨s.X, λ j, s.π.app j, λ j j' f, s.π.naturality f⟩, by cases K; rcases c with ⟨_, _, _⟩; intros s j; cases s; exact h.fac _ j, by cases K; rcases c with ⟨_, _, _⟩; intros s m w; rcases s with ⟨_, _, _⟩; exact h.uniq _ m w⟩⟩ } } instance id_preserves_colimits : preserves_colimits (functor.id C) := { preserves_colimits_of_shape := λ J 𝒥, { preserves_colimit := λ K, by exactI ⟨λ c h, ⟨λ s, h.desc ⟨s.X, λ j, s.ι.app j, λ j j' f, s.ι.naturality f⟩, by cases K; rcases c with ⟨_, _, _⟩; intros s j; cases s; exact h.fac _ j, by cases K; rcases c with ⟨_, _, _⟩; intros s m w; rcases s with ⟨_, _, _⟩; exact h.uniq _ m w⟩⟩ } } section variables {E : Sort u₃} [ℰ : category.{v+1} E] variables (F : C ⥤ D) (G : D ⥤ E) local attribute [elab_simple] preserves_limit.preserves preserves_colimit.preserves instance comp_preserves_limit [preserves_limit K F] [preserves_limit (K ⋙ F) G] : preserves_limit K (F ⋙ G) := ⟨λ c h, preserves_limit.preserves G (preserves_limit.preserves F h)⟩ instance comp_preserves_colimit [preserves_colimit K F] [preserves_colimit (K ⋙ F) G] : preserves_colimit K (F ⋙ G) := ⟨λ c h, preserves_colimit.preserves G (preserves_colimit.preserves F h)⟩ end /-- If F preserves one limit cone for the diagram K, then it preserves any limit cone for K. -/ def preserves_limit_of_preserves_limit_cone {F : C ⥤ D} {t : cone K} (h : is_limit t) (hF : is_limit (F.map_cone t)) : preserves_limit K F := ⟨λ t' h', is_limit.of_iso_limit hF (functor.map_iso _ (is_limit.unique h h'))⟩ /-- If F preserves one colimit cocone for the diagram K, then it preserves any colimit cocone for K. -/ def preserves_colimit_of_preserves_colimit_cocone {F : C ⥤ D} {t : cocone K} (h : is_colimit t) (hF : is_colimit (F.map_cocone t)) : preserves_colimit K F := ⟨λ t' h', is_colimit.of_iso_colimit hF (functor.map_iso _ (is_colimit.unique h h'))⟩ /- A functor F : C → D reflects limits if whenever the image of a cone under F is a limit cone in D, the cone was already a limit cone in C. Note that again we do not assume a priori that D actually has any limits. -/ class reflects_limit (K : J ⥤ C) (F : C ⥤ D) : Type (max u₁ u₂ v) := (reflects : Π {c : cone K}, is_limit (F.map_cone c) → is_limit c) class reflects_colimit (K : J ⥤ C) (F : C ⥤ D) : Type (max u₁ u₂ v) := (reflects : Π {c : cocone K}, is_colimit (F.map_cocone c) → is_colimit c) class reflects_limits_of_shape (J : Type v) [small_category J] (F : C ⥤ D) : Type (max u₁ u₂ v) := (reflects_limit : Π {K : J ⥤ C}, reflects_limit K F) class reflects_colimits_of_shape (J : Type v) [small_category J] (F : C ⥤ D) : Type (max u₁ u₂ v) := (reflects_colimit : Π {K : J ⥤ C}, reflects_colimit K F) class reflects_limits (F : C ⥤ D) : Type (max u₁ u₂ (v+1)) := (reflects_limits_of_shape : Π {J : Type v} {𝒥 : small_category J}, by exactI reflects_limits_of_shape J F) class reflects_colimits (F : C ⥤ D) : Type (max u₁ u₂ (v+1)) := (reflects_colimits_of_shape : Π {J : Type v} {𝒥 : small_category J}, by exactI reflects_colimits_of_shape J F) instance reflects_limit_subsingleton (K : J ⥤ C) (F : C ⥤ D) : subsingleton (reflects_limit K F) := by split; rintros ⟨a⟩ ⟨b⟩; congr instance reflects_colimit_subsingleton (K : J ⥤ C) (F : C ⥤ D) : subsingleton (reflects_colimit K F) := by split; rintros ⟨a⟩ ⟨b⟩; congr instance reflects_limits_of_shape_subsingleton (J : Type v) [small_category J] (F : C ⥤ D) : subsingleton (reflects_limits_of_shape J F) := by { split, intros, cases a, cases b, congr } instance reflects_colimits_of_shape_subsingleton (J : Type v) [small_category J] (F : C ⥤ D) : subsingleton (reflects_colimits_of_shape J F) := by { split, intros, cases a, cases b, congr } instance reflects_limits_subsingleton (F : C ⥤ D) : subsingleton (reflects_limits F) := by { split, intros, cases a, cases b, congr, funext J 𝒥, resetI, apply subsingleton.elim } instance reflects_colimits_subsingleton (F : C ⥤ D) : subsingleton (reflects_colimits F) := by { split, intros, cases a, cases b, congr, funext J 𝒥, resetI, apply subsingleton.elim } instance reflects_limit_of_reflects_limits_of_shape (K : J ⥤ C) (F : C ⥤ D) [H : reflects_limits_of_shape J F] : reflects_limit K F := reflects_limits_of_shape.reflects_limit J F instance reflects_colimit_of_reflects_colimits_of_shape (K : J ⥤ C) (F : C ⥤ D) [H : reflects_colimits_of_shape J F] : reflects_colimit K F := reflects_colimits_of_shape.reflects_colimit J F instance reflects_limits_of_shape_of_reflects_limits (F : C ⥤ D) [H : reflects_limits F] : reflects_limits_of_shape J F := reflects_limits.reflects_limits_of_shape F instance reflects_colimits_of_shape_of_reflects_colimits (F : C ⥤ D) [H : reflects_colimits F] : reflects_colimits_of_shape J F := reflects_colimits.reflects_colimits_of_shape F instance id_reflects_limits : reflects_limits (functor.id C) := { reflects_limits_of_shape := λ J 𝒥, { reflects_limit := λ K, by exactI ⟨λ c h, ⟨λ s, h.lift ⟨s.X, λ j, s.π.app j, λ j j' f, s.π.naturality f⟩, by cases K; rcases c with ⟨_, _, _⟩; intros s j; cases s; exact h.fac _ j, by cases K; rcases c with ⟨_, _, _⟩; intros s m w; rcases s with ⟨_, _, _⟩; exact h.uniq _ m w⟩⟩ } } instance id_reflects_colimits : reflects_colimits (functor.id C) := { reflects_colimits_of_shape := λ J 𝒥, { reflects_colimit := λ K, by exactI ⟨λ c h, ⟨λ s, h.desc ⟨s.X, λ j, s.ι.app j, λ j j' f, s.ι.naturality f⟩, by cases K; rcases c with ⟨_, _, _⟩; intros s j; cases s; exact h.fac _ j, by cases K; rcases c with ⟨_, _, _⟩; intros s m w; rcases s with ⟨_, _, _⟩; exact h.uniq _ m w⟩⟩ } } section variables {E : Sort u₃} [ℰ : category.{v+1} E] variables (F : C ⥤ D) (G : D ⥤ E) instance comp_reflects_limit [reflects_limit K F] [reflects_limit (K ⋙ F) G] : reflects_limit K (F ⋙ G) := ⟨λ c h, reflects_limit.reflects (reflects_limit.reflects h)⟩ instance comp_reflects_colimit [reflects_colimit K F] [reflects_colimit (K ⋙ F) G] : reflects_colimit K (F ⋙ G) := ⟨λ c h, reflects_colimit.reflects (reflects_colimit.reflects h)⟩ end end category_theory.limits
d0f0f5a2b88f71482401fb0b591ea2455758362b
853df553b1d6ca524e3f0a79aedd32dde5d27ec3
/src/measure_theory/measurable_space.lean
55b39cc91a8809d4a9bb195ac3ca744476c88d4c
[ "Apache-2.0" ]
permissive
DanielFabian/mathlib
efc3a50b5dde303c59eeb6353ef4c35a345d7112
f520d07eba0c852e96fe26da71d85bf6d40fcc2a
refs/heads/master
1,668,739,922,971
1,595,201,756,000
1,595,201,756,000
279,469,476
0
0
null
1,594,696,604,000
1,594,696,604,000
null
UTF-8
Lean
false
false
47,209
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 Measurable spaces -- σ-algberas -/ import data.set.disjointed import data.set.countable /-! # Measurable spaces and measurable functions This file defines measurable spaces and the functions and isomorphisms between them. A measurable space is a set equipped with a σ-algebra, a collection of subsets closed under complementation and countable union. A function between measurable spaces is measurable if the preimage of each measurable subset is measurable. σ-algebras on a fixed set `α` form a complete lattice. Here we order σ-algebras by writing `m₁ ≤ m₂` if every set which is `m₁`-measurable is also `m₂`-measurable (that is, `m₁` is a subset of `m₂`). In particular, any collection of subsets of `α` generates a smallest σ-algebra which contains all of them. A function `f : α → β` induces a Galois connection between the lattices of σ-algebras on `α` and `β`. A measurable equivalence between measurable spaces is an equivalence which respects the σ-algebras, that is, for which both directions of the equivalence are measurable functions. ## Main statements The main theorem of this file is Dynkin's π-λ theorem, which appears here as an induction principle `induction_on_inter`. Suppose `s` is a collection of subsets of `α` such that the intersection of two members of `s` belongs to `s` whenever it is nonempty. Let `m` be the σ-algebra generated by `s`. In order to check that a predicate `C` holds on every member of `m`, it suffices to check that `C` holds on the members of `s` and that `C` is preserved by complementation and *disjoint* countable unions. ## Implementation notes Measurability of a function `f : α → β` between measurable spaces is defined in terms of the Galois connection induced by f. ## References * <https://en.wikipedia.org/wiki/Measurable_space> * <https://en.wikipedia.org/wiki/Sigma-algebra> * <https://en.wikipedia.org/wiki/Dynkin_system> ## Tags measurable space, measurable function, dynkin system -/ local attribute [instance] classical.prop_decidable open set encodable open_locale classical universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} {ι : Sort x} {s t u : set α} /-- A measurable space is a space equipped with a σ-algebra. -/ structure measurable_space (α : Type u) := (is_measurable : set α → Prop) (is_measurable_empty : is_measurable ∅) (is_measurable_compl : ∀s, is_measurable s → is_measurable sᶜ) (is_measurable_Union : ∀f:ℕ → set α, (∀i, is_measurable (f i)) → is_measurable (⋃i, f i)) attribute [class] measurable_space section variable [measurable_space α] /-- `is_measurable s` means that `s` is measurable (in the ambient measure space on `α`) -/ def is_measurable : set α → Prop := ‹measurable_space α›.is_measurable lemma is_measurable.empty : is_measurable (∅ : set α) := ‹measurable_space α›.is_measurable_empty lemma is_measurable.compl : is_measurable s → is_measurable sᶜ := ‹measurable_space α›.is_measurable_compl s lemma is_measurable.of_compl (h : is_measurable sᶜ) : is_measurable s := s.compl_compl ▸ h.compl lemma is_measurable.compl_iff : is_measurable sᶜ ↔ is_measurable s := ⟨is_measurable.of_compl, is_measurable.compl⟩ lemma is_measurable.univ : is_measurable (univ : set α) := by simpa using (@is_measurable.empty α _).compl lemma subsingleton.is_measurable [subsingleton α] {s : set α} : is_measurable s := subsingleton.set_cases is_measurable.empty is_measurable.univ s lemma is_measurable.congr {s t : set α} (hs : is_measurable s) (h : s = t) : is_measurable t := by rwa ← h lemma encodable.Union_decode2 {α} [encodable β] (f : β → set α) : (⋃ b, f b) = ⋃ (i : ℕ) (b ∈ decode2 β i), f b := ext $ by simp [mem_decode2, exists_swap] @[elab_as_eliminator] lemma encodable.Union_decode2_cases {α} [encodable β] {f : β → set α} {C : set α → Prop} (H0 : C ∅) (H1 : ∀ b, C (f b)) {n} : C (⋃ b ∈ decode2 β n, f b) := match decode2 β n with | none := by simp; apply H0 | (some b) := by convert H1 b; simp [ext_iff] end lemma is_measurable.Union [encodable β] {f : β → set α} (h : ∀b, is_measurable (f b)) : is_measurable (⋃b, f b) := by rw encodable.Union_decode2; exact ‹measurable_space α›.is_measurable_Union (λ n, ⋃ b ∈ decode2 β n, f b) (λ n, encodable.Union_decode2_cases is_measurable.empty h) lemma is_measurable.bUnion {f : β → set α} {s : set β} (hs : countable s) (h : ∀b∈s, is_measurable (f b)) : is_measurable (⋃b∈s, f b) := begin rw bUnion_eq_Union, haveI := hs.to_encodable, exact is_measurable.Union (by simpa using h) end lemma is_measurable.sUnion {s : set (set α)} (hs : countable s) (h : ∀t∈s, is_measurable t) : is_measurable (⋃₀ s) := by rw sUnion_eq_bUnion; exact is_measurable.bUnion hs h lemma is_measurable.Union_Prop {p : Prop} {f : p → set α} (hf : ∀b, is_measurable (f b)) : is_measurable (⋃b, f b) := by by_cases p; simp [h, hf, is_measurable.empty] lemma is_measurable.Inter [encodable β] {f : β → set α} (h : ∀b, is_measurable (f b)) : is_measurable (⋂b, f b) := is_measurable.compl_iff.1 $ by rw compl_Inter; exact is_measurable.Union (λ b, (h b).compl) lemma is_measurable.bInter {f : β → set α} {s : set β} (hs : countable s) (h : ∀b∈s, is_measurable (f b)) : is_measurable (⋂b∈s, f b) := is_measurable.compl_iff.1 $ by rw compl_bInter; exact is_measurable.bUnion hs (λ b hb, (h b hb).compl) lemma is_measurable.sInter {s : set (set α)} (hs : countable s) (h : ∀t∈s, is_measurable t) : is_measurable (⋂₀ s) := by rw sInter_eq_bInter; exact is_measurable.bInter hs h lemma is_measurable.Inter_Prop {p : Prop} {f : p → set α} (hf : ∀b, is_measurable (f b)) : is_measurable (⋂b, f b) := by by_cases p; simp [h, hf, is_measurable.univ] lemma is_measurable.union {s₁ s₂ : set α} (h₁ : is_measurable s₁) (h₂ : is_measurable s₂) : is_measurable (s₁ ∪ s₂) := by rw union_eq_Union; exact is_measurable.Union (bool.forall_bool.2 ⟨h₂, h₁⟩) lemma is_measurable.inter {s₁ s₂ : set α} (h₁ : is_measurable s₁) (h₂ : is_measurable s₂) : is_measurable (s₁ ∩ s₂) := by rw inter_eq_compl_compl_union_compl; exact (h₁.compl.union h₂.compl).compl lemma is_measurable.diff {s₁ s₂ : set α} (h₁ : is_measurable s₁) (h₂ : is_measurable s₂) : is_measurable (s₁ \ s₂) := h₁.inter h₂.compl lemma is_measurable.disjointed {f : ℕ → set α} (h : ∀i, is_measurable (f i)) (n) : is_measurable (disjointed f n) := disjointed_induct (h n) (assume t i ht, is_measurable.diff ht $ h _) lemma is_measurable.const (p : Prop) : is_measurable {a : α | p} := by by_cases p; simp [h, is_measurable.empty]; apply is_measurable.univ end @[ext] lemma measurable_space.ext : ∀{m₁ m₂ : measurable_space α}, (∀s:set α, m₁.is_measurable s ↔ m₂.is_measurable s) → m₁ = m₂ | ⟨s₁, _, _, _⟩ ⟨s₂, _, _, _⟩ h := have s₁ = s₂, from funext $ assume x, propext $ h x, by subst this namespace measurable_space section complete_lattice instance : partial_order (measurable_space α) := { le := λm₁ m₂, m₁.is_measurable ≤ m₂.is_measurable, le_refl := assume a b, le_refl _, le_trans := assume a b c, le_trans, le_antisymm := assume a b h₁ h₂, measurable_space.ext $ assume s, ⟨h₁ s, h₂ s⟩ } /-- The smallest σ-algebra containing a collection `s` of basic sets -/ inductive generate_measurable (s : set (set α)) : set α → Prop | basic : ∀u∈s, generate_measurable u | empty : generate_measurable ∅ | compl : ∀s, generate_measurable s → generate_measurable sᶜ | union : ∀f:ℕ → set α, (∀n, generate_measurable (f n)) → generate_measurable (⋃i, f i) /-- Construct the smallest measure space containing a collection of basic sets -/ def generate_from (s : set (set α)) : measurable_space α := { is_measurable := generate_measurable s, is_measurable_empty := generate_measurable.empty, is_measurable_compl := generate_measurable.compl, is_measurable_Union := generate_measurable.union } lemma is_measurable_generate_from {s : set (set α)} {t : set α} (ht : t ∈ s) : (generate_from s).is_measurable t := generate_measurable.basic t ht lemma generate_from_le {s : set (set α)} {m : measurable_space α} (h : ∀t∈s, m.is_measurable t) : generate_from s ≤ m := assume t (ht : generate_measurable s t), ht.rec_on h (is_measurable_empty m) (assume s _ hs, is_measurable_compl m s hs) (assume f _ hf, is_measurable_Union m f hf) lemma generate_from_le_iff {s : set (set α)} {m : measurable_space α} : generate_from s ≤ m ↔ s ⊆ {t | m.is_measurable t} := iff.intro (assume h u hu, h _ $ is_measurable_generate_from hu) (assume h, generate_from_le h) /-- If `g` is a collection of subsets of `α` such that the `σ`-algebra generated from `g` contains the same sets as `g`, then `g` was already a `σ`-algebra. -/ protected def mk_of_closure (g : set (set α)) (hg : {t | (generate_from g).is_measurable t} = g) : measurable_space α := { is_measurable := λs, s ∈ g, is_measurable_empty := hg ▸ is_measurable_empty _, is_measurable_compl := hg ▸ is_measurable_compl _, is_measurable_Union := hg ▸ is_measurable_Union _ } lemma mk_of_closure_sets {s : set (set α)} {hs : {t | (generate_from s).is_measurable t} = s} : measurable_space.mk_of_closure s hs = generate_from s := measurable_space.ext $ assume t, show t ∈ s ↔ _, by rw [← hs] {occs := occurrences.pos [1] }; refl /-- We get a Galois insertion between `σ`-algebras on `α` and `set (set α)` by using `generate_from` on one side and the collection of measurable sets on the other side. -/ def gi_generate_from : galois_insertion (@generate_from α) (λm, {t | @is_measurable α m t}) := { gc := assume s m, generate_from_le_iff, le_l_u := assume m s, is_measurable_generate_from, choice := λg hg, measurable_space.mk_of_closure g $ le_antisymm hg $ generate_from_le_iff.1 $ le_refl _, choice_eq := assume g hg, mk_of_closure_sets } instance : complete_lattice (measurable_space α) := gi_generate_from.lift_complete_lattice instance : inhabited (measurable_space α) := ⟨⊤⟩ lemma is_measurable_bot_iff {s : set α} : @is_measurable α ⊥ s ↔ (s = ∅ ∨ s = univ) := let b : measurable_space α := { is_measurable := λs, s = ∅ ∨ s = univ, is_measurable_empty := or.inl rfl, is_measurable_compl := by simp [or_imp_distrib] {contextual := tt}, is_measurable_Union := assume f hf, classical.by_cases (assume h : ∃i, f i = univ, let ⟨i, hi⟩ := h in or.inr $ eq_univ_of_univ_subset $ hi ▸ le_supr f i) (assume h : ¬ ∃i, f i = univ, or.inl $ eq_empty_of_subset_empty $ Union_subset $ assume i, (hf i).elim (by simp {contextual := tt}) (assume hi, false.elim $ h ⟨i, hi⟩)) } in have b = ⊥, from bot_unique $ assume s hs, hs.elim (assume s, s.symm ▸ @is_measurable_empty _ ⊥) (assume s, s.symm ▸ @is_measurable.univ _ ⊥), this ▸ iff.rfl @[simp] theorem is_measurable_top {s : set α} : @is_measurable _ ⊤ s := trivial @[simp] theorem is_measurable_inf {m₁ m₂ : measurable_space α} {s : set α} : @is_measurable _ (m₁ ⊓ m₂) s ↔ @is_measurable _ m₁ s ∧ @is_measurable _ m₂ s := iff.rfl @[simp] theorem is_measurable_Inf {ms : set (measurable_space α)} {s : set α} : @is_measurable _ (Inf ms) s ↔ ∀ m ∈ ms, @is_measurable _ m s := show s ∈ (⋂m∈ms, {t | @is_measurable _ m t }) ↔ _, by simp @[simp] theorem is_measurable_infi {ι} {m : ι → measurable_space α} {s : set α} : @is_measurable _ (infi m) s ↔ ∀ i, @is_measurable _ (m i) s := show s ∈ (λm, {s | @is_measurable _ m s }) (infi m) ↔ _, by rw (@gi_generate_from α).gc.u_infi; simp; refl theorem is_measurable_sup {m₁ m₂ : measurable_space α} {s : set α} : @is_measurable _ (m₁ ⊔ m₂) s ↔ generate_measurable (m₁.is_measurable ∪ m₂.is_measurable) s := iff.refl _ theorem is_measurable_Sup {ms : set (measurable_space α)} {s : set α} : @is_measurable _ (Sup ms) s ↔ generate_measurable (⋃₀ (measurable_space.is_measurable '' ms)) s := begin change @is_measurable _ (generate_from _) _ ↔ _, dsimp [generate_from], rw (show (⨆ (b : measurable_space α) (H : b ∈ ms), set_of (is_measurable b)) = (⋃₀(is_measurable '' ms)), { ext, simp only [exists_prop, mem_Union, sUnion_image, mem_set_of_eq], refl, }) end theorem is_measurable_supr {ι} {m : ι → measurable_space α} {s : set α} : @is_measurable _ (supr m) s ↔ generate_measurable (⋃i, (m i).is_measurable) s := begin convert @is_measurable_Sup _ (range m) s, simp, end end complete_lattice section functors variables {m m₁ m₂ : measurable_space α} {m' : measurable_space β} {f : α → β} {g : β → α} /-- The forward image of a measure space under a function. `map f m` contains the sets `s : set β` whose preimage under `f` is measurable. -/ protected def map (f : α → β) (m : measurable_space α) : measurable_space β := { is_measurable := λs, m.is_measurable $ f ⁻¹' s, is_measurable_empty := m.is_measurable_empty, is_measurable_compl := assume s hs, m.is_measurable_compl _ hs, is_measurable_Union := assume f hf, by rw [preimage_Union]; exact m.is_measurable_Union _ hf } @[simp] lemma map_id : m.map id = m := measurable_space.ext $ assume s, iff.rfl @[simp] lemma map_comp {f : α → β} {g : β → γ} : (m.map f).map g = m.map (g ∘ f) := measurable_space.ext $ assume s, iff.rfl /-- The reverse image of a measure space under a function. `comap f m` contains the sets `s : set α` such that `s` is the `f`-preimage of a measurable set in `β`. -/ protected def comap (f : α → β) (m : measurable_space β) : measurable_space α := { is_measurable := λs, ∃s', m.is_measurable s' ∧ f ⁻¹' s' = s, is_measurable_empty := ⟨∅, m.is_measurable_empty, rfl⟩, is_measurable_compl := assume s ⟨s', h₁, h₂⟩, ⟨s'ᶜ, m.is_measurable_compl _ h₁, h₂ ▸ rfl⟩, is_measurable_Union := assume s hs, let ⟨s', hs'⟩ := classical.axiom_of_choice hs in ⟨⋃i, s' i, m.is_measurable_Union _ (λi, (hs' i).left), by simp [hs'] ⟩ } @[simp] lemma comap_id : m.comap id = m := measurable_space.ext $ assume s, ⟨assume ⟨s', hs', h⟩, h ▸ hs', assume h, ⟨s, h, rfl⟩⟩ @[simp] lemma comap_comp {f : β → α} {g : γ → β} : (m.comap f).comap g = m.comap (f ∘ g) := measurable_space.ext $ assume s, ⟨assume ⟨t, ⟨u, h, hu⟩, ht⟩, ⟨u, h, ht ▸ hu ▸ rfl⟩, assume ⟨t, h, ht⟩, ⟨f ⁻¹' t, ⟨_, h, rfl⟩, ht⟩⟩ lemma comap_le_iff_le_map {f : α → β} : m'.comap f ≤ m ↔ m' ≤ m.map f := ⟨assume h s hs, h _ ⟨_, hs, rfl⟩, assume h s ⟨t, ht, heq⟩, heq ▸ h _ ht⟩ lemma gc_comap_map (f : α → β) : galois_connection (measurable_space.comap f) (measurable_space.map f) := assume f g, comap_le_iff_le_map lemma map_mono (h : m₁ ≤ m₂) : m₁.map f ≤ m₂.map f := (gc_comap_map f).monotone_u h lemma monotone_map : monotone (measurable_space.map f) := assume a b h, map_mono h lemma comap_mono (h : m₁ ≤ m₂) : m₁.comap g ≤ m₂.comap g := (gc_comap_map g).monotone_l h lemma monotone_comap : monotone (measurable_space.comap g) := assume a b h, comap_mono h @[simp] lemma comap_bot : (⊥:measurable_space α).comap g = ⊥ := (gc_comap_map g).l_bot @[simp] lemma comap_sup : (m₁ ⊔ m₂).comap g = m₁.comap g ⊔ m₂.comap g := (gc_comap_map g).l_sup @[simp] lemma comap_supr {m : ι → measurable_space α} :(⨆i, m i).comap g = (⨆i, (m i).comap g) := (gc_comap_map g).l_supr @[simp] lemma map_top : (⊤:measurable_space α).map f = ⊤ := (gc_comap_map f).u_top @[simp] lemma map_inf : (m₁ ⊓ m₂).map f = m₁.map f ⊓ m₂.map f := (gc_comap_map f).u_inf @[simp] lemma map_infi {m : ι → measurable_space α} : (⨅i, m i).map f = (⨅i, (m i).map f) := (gc_comap_map f).u_infi lemma comap_map_le : (m.map f).comap f ≤ m := (gc_comap_map f).l_u_le _ lemma le_map_comap : m ≤ (m.comap g).map g := (gc_comap_map g).le_u_l _ end functors lemma generate_from_le_generate_from {s t : set (set α)} (h : s ⊆ t) : generate_from s ≤ generate_from t := gi_generate_from.gc.monotone_l h lemma generate_from_sup_generate_from {s t : set (set α)} : generate_from s ⊔ generate_from t = generate_from (s ∪ t) := (@gi_generate_from α).gc.l_sup.symm lemma comap_generate_from {f : α → β} {s : set (set β)} : (generate_from s).comap f = generate_from (preimage f '' s) := le_antisymm (comap_le_iff_le_map.2 $ generate_from_le $ assume t hts, generate_measurable.basic _ $ mem_image_of_mem _ $ hts) (generate_from_le $ assume t ⟨u, hu, eq⟩, eq ▸ ⟨u, generate_measurable.basic _ hu, rfl⟩) end measurable_space section measurable_functions open measurable_space /-- A function `f` between measurable spaces is measurable if the preimage of every measurable set is measurable. -/ def measurable [m₁ : measurable_space α] [m₂ : measurable_space β] (f : α → β) : Prop := m₂ ≤ m₁.map f lemma subsingleton.measurable [measurable_space α] [measurable_space β] [subsingleton α] {f : α → β} : measurable f := λ s hs, @subsingleton.is_measurable α _ _ _ lemma measurable_id [measurable_space α] : measurable (@id α) := le_refl _ lemma measurable.preimage [measurable_space α] [measurable_space β] {f : α → β} (hf : measurable f) {s : set β} : is_measurable s → is_measurable (f ⁻¹' s) := hf _ lemma measurable.comp [measurable_space α] [measurable_space β] [measurable_space γ] {g : β → γ} {f : α → β} (hg : measurable g) (hf : measurable f) : measurable (g ∘ f) := le_trans hg $ map_mono hf lemma measurable_from_top [measurable_space β] {f : α → β} : @measurable _ _ ⊤ _ f := λ s hs, trivial lemma measurable.mono {ma ma' : measurable_space α} {mb mb' : measurable_space β} {f : α → β} (hf : @measurable α β ma mb f) (ha : ma ≤ ma') (hb : mb' ≤ mb) : @measurable α β ma' mb' f := calc mb' ≤ mb : hb ... ≤ ma.map f : hf ... ≤ ma'.map f : map_mono ha lemma measurable_generate_from [measurable_space α] {s : set (set β)} {f : α → β} (h : ∀t∈s, is_measurable (f ⁻¹' t)) : @measurable _ _ _ (generate_from s) f := generate_from_le h lemma measurable.if [measurable_space α] [measurable_space β] {p : α → Prop} {h : decidable_pred p} {f g : α → β} (hp : is_measurable {a | p a}) (hf : measurable f) (hg : measurable g) : measurable (λa, if p a then f a else g a) := λ s hs, show is_measurable {a | (if p a then f a else g a) ∈ s}, begin convert (hp.inter $ hf s hs).union (hp.compl.inter $ hg s hs), exact ext (λ a, by by_cases p a ; { rw mem_def, simp [h] }) end lemma measurable_const {α β} [measurable_space α] [measurable_space β] {a : α} : measurable (λb:β, a) := assume s hs, show is_measurable {b : β | a ∈ s}, from classical.by_cases (assume h : a ∈ s, by simp [h]; from is_measurable.univ) (assume h : a ∉ s, by simp [h]; from is_measurable.empty) lemma measurable_zero {α β} [measurable_space α] [has_zero α] [measurable_space β] : measurable (λb:β, (0:α)) := measurable_const end measurable_functions section constructions instance : measurable_space empty := ⊤ instance : measurable_space unit := ⊤ instance : measurable_space bool := ⊤ instance : measurable_space ℕ := ⊤ instance : measurable_space ℤ := ⊤ instance : measurable_space ℚ := ⊤ lemma measurable_to_encodable [encodable α] [measurable_space α] [measurable_space β] {f : β → α} (h : ∀ y, is_measurable {x | f x = y}) : measurable f := begin assume s hs, show is_measurable {x | f x ∈ s}, have : {x | f x ∈ s} = ⋃ (n ∈ s), {x | f x = n}, { ext, simp }, rw this, simp [is_measurable.Union, is_measurable.Union_Prop, h] end lemma measurable_unit [measurable_space α] (f : unit → α) : measurable f := have f = (λu, f ()) := funext $ assume ⟨⟩, rfl, by rw this; exact measurable_const section nat lemma measurable_from_nat [measurable_space α] {f : ℕ → α} : measurable f := measurable_from_top lemma measurable_to_nat [measurable_space α] {f : α → ℕ} : (∀ k, is_measurable {x | f x = k}) → measurable f := measurable_to_encodable lemma measurable_find_greatest [measurable_space α] {p : ℕ → α → Prop} : ∀ {N}, (∀ k ≤ N, is_measurable {x | nat.find_greatest (λ n, p n x) N = k}) → measurable (λ x, nat.find_greatest (λ n, p n x) N) | 0 := assume h s hs, show is_measurable {x : α | (nat.find_greatest (λ n, p n x) 0) ∈ s}, begin by_cases h : 0 ∈ s, { convert is_measurable.univ, simp only [nat.find_greatest_zero, h] }, { convert is_measurable.empty, simp only [nat.find_greatest_zero, h], refl } end | (n + 1) := assume h, begin apply measurable_to_nat, assume k, by_cases hk : k ≤ n + 1, { exact h k hk }, { have := is_measurable.empty, rw ← set_of_false at this, convert this, funext, rw eq_false, assume h, rw ← h at hk, have := nat.find_greatest_le, contradiction } end end nat section subtype instance {p : α → Prop} [m : measurable_space α] : measurable_space (subtype p) := m.comap subtype.val lemma measurable.subtype_coe [measurable_space α] [measurable_space β] {p : β → Prop} {f : α → subtype p} (hf : measurable f) : measurable (λa:α, (f a : β)) := measurable.comp (measurable_space.comap_le_iff_le_map.mp (le_refl _)) hf lemma measurable.subtype_mk [measurable_space α] [measurable_space β] {p : β → Prop} {f : α → β} (hf : measurable f) {h : ∀ x, p (f x)} : measurable (λ x, (⟨f x, h x⟩ : subtype p)) := measurable_space.comap_le_iff_le_map.mpr $ by rw [measurable_space.map_comp]; exact hf lemma is_measurable_subtype_image [measurable_space α] {s : set α} {t : set s} (hs : is_measurable s) : is_measurable t → is_measurable ((coe : s → α) '' t) | ⟨u, (hu : is_measurable u), (eq : coe ⁻¹' u = t)⟩ := begin rw [← eq, image_preimage_eq_inter_range, subtype.range_coe], exact is_measurable.inter hu hs end lemma measurable_of_measurable_union_cover [measurable_space α] [measurable_space β] {f : α → β} (s t : set α) (hs : is_measurable s) (ht : is_measurable t) (h : univ ⊆ s ∪ t) (hc : measurable (λa:s, f a)) (hd : measurable (λa:t, f a)) : measurable f := assume u (hu : is_measurable u), show is_measurable (f ⁻¹' u), from begin rw show f ⁻¹' u = coe '' (coe ⁻¹' (f ⁻¹' u) : set s) ∪ coe '' (coe ⁻¹' (f ⁻¹' u) : set t), by rw [image_preimage_eq_inter_range, image_preimage_eq_inter_range, subtype.range_coe, subtype.range_coe, ← inter_distrib_left, univ_subset_iff.1 h, inter_univ], exact is_measurable.union (is_measurable_subtype_image hs (hc _ hu)) (is_measurable_subtype_image ht (hd _ hu)) end lemma measurable_of_measurable_on_compl_singleton [measurable_space α] [measurable_space β] {f : α → β} (a : α) (ha : is_measurable ({a} : set α)) (hf : measurable (set.restrict f {x | x ≠ a})) : measurable f := have ha : is_measurable {x | x = a}, from ha.congr $ set.ext $ λ x, mem_singleton_iff, measurable_of_measurable_union_cover _ _ ha ha.compl (λ x hx, classical.em _) (@subsingleton.measurable {x | x = a} _ _ _ ⟨λ x y, subtype.eq $ x.2.trans y.2.symm⟩ _) hf end subtype section prod instance [m₁ : measurable_space α] [m₂ : measurable_space β] : measurable_space (α × β) := m₁.comap prod.fst ⊔ m₂.comap prod.snd lemma measurable.fst [measurable_space α] [measurable_space β] [measurable_space γ] {f : α → β × γ} (hf : measurable f) : measurable (λa:α, (f a).1) := measurable.comp (measurable_space.comap_le_iff_le_map.mp le_sup_left) hf lemma measurable.snd [measurable_space α] [measurable_space β] [measurable_space γ] {f : α → β × γ} (hf : measurable f) : measurable (λa:α, (f a).2) := measurable.comp (measurable_space.comap_le_iff_le_map.mp le_sup_right) hf lemma measurable.prod [measurable_space α] [measurable_space β] [measurable_space γ] {f : α → β × γ} (hf₁ : measurable (λa, (f a).1)) (hf₂ : measurable (λa, (f a).2)) : measurable f := sup_le (by rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp]; exact hf₁) (by rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp]; exact hf₂) lemma measurable.prod_mk [measurable_space α] [measurable_space β] [measurable_space γ] {f : α → β} {g : α → γ} (hf : measurable f) (hg : measurable g) : measurable (λa:α, (f a, g a)) := measurable.prod hf hg lemma is_measurable.prod [measurable_space α] [measurable_space β] {s : set α} {t : set β} (hs : is_measurable s) (ht : is_measurable t) : is_measurable (set.prod s t) := is_measurable.inter (measurable_id.fst _ hs) (measurable_id.snd _ ht) end prod section pi instance measurable_space.pi {α : Type u} {β : α → Type v} [m : Πa, measurable_space (β a)] : measurable_space (Πa, β a) := ⨆a, (m a).comap (λb, b a) lemma measurable_pi_apply {α : Type u} {β : α → Type v} [Πa, measurable_space (β a)] (a : α) : measurable (λf:Πa, β a, f a) := measurable_space.comap_le_iff_le_map.1 $ le_supr _ a lemma measurable_pi_lambda {α : Type u} {β : α → Type v} {γ : Type w} [Πa, measurable_space (β a)] [measurable_space γ] (f : γ → Πa, β a) (hf : ∀a, measurable (λc, f c a)) : measurable f := supr_le $ assume a, measurable_space.comap_le_iff_le_map.2 (hf a) end pi instance [m₁ : measurable_space α] [m₂ : measurable_space β] : measurable_space (α ⊕ β) := m₁.map sum.inl ⊓ m₂.map sum.inr section sum variables [measurable_space α] [measurable_space β] [measurable_space γ] lemma measurable_inl : measurable (@sum.inl α β) := inf_le_left lemma measurable_inr : measurable (@sum.inr α β) := inf_le_right lemma measurable_sum {f : α ⊕ β → γ} (hl : measurable (f ∘ sum.inl)) (hr : measurable (f ∘ sum.inr)) : measurable f := measurable_space.comap_le_iff_le_map.1 $ le_inf (measurable_space.comap_le_iff_le_map.2 $ hl) (measurable_space.comap_le_iff_le_map.2 $ hr) lemma measurable_sum_rec {f : α → γ} {g : β → γ} (hf : measurable f) (hg : measurable g) : @measurable (α ⊕ β) γ _ _ (@sum.rec α β (λ_, γ) f g) := measurable_sum hf hg lemma is_measurable_inl_image {s : set α} (hs : is_measurable s) : is_measurable (sum.inl '' s : set (α ⊕ β)) := ⟨show is_measurable (sum.inl ⁻¹' _), by rwa [preimage_image_eq]; exact (assume a b, sum.inl.inj), have sum.inr ⁻¹' (sum.inl '' s : set (α ⊕ β)) = ∅ := eq_empty_of_subset_empty $ assume x ⟨y, hy, eq⟩, by contradiction, show is_measurable (sum.inr ⁻¹' _), by rw [this]; exact is_measurable.empty⟩ lemma is_measurable_range_inl : is_measurable (range sum.inl : set (α ⊕ β)) := by rw [← image_univ]; exact is_measurable_inl_image is_measurable.univ lemma is_measurable_inr_image {s : set β} (hs : is_measurable s) : is_measurable (sum.inr '' s : set (α ⊕ β)) := ⟨ have sum.inl ⁻¹' (sum.inr '' s : set (α ⊕ β)) = ∅ := eq_empty_of_subset_empty $ assume x ⟨y, hy, eq⟩, by contradiction, show is_measurable (sum.inl ⁻¹' _), by rw [this]; exact is_measurable.empty, show is_measurable (sum.inr ⁻¹' _), by rwa [preimage_image_eq]; exact (assume a b, sum.inr.inj)⟩ lemma is_measurable_range_inr : is_measurable (range sum.inr : set (α ⊕ β)) := by rw [← image_univ]; exact is_measurable_inr_image is_measurable.univ end sum instance {β : α → Type v} [m : Πa, measurable_space (β a)] : measurable_space (sigma β) := ⨅a, (m a).map (sigma.mk a) end constructions /-- Equivalences between measurable spaces. Main application is the simplification of measurability statements along measurable equivalences. -/ structure measurable_equiv (α β : Type*) [measurable_space α] [measurable_space β] extends α ≃ β := (measurable_to_fun : measurable to_fun) (measurable_inv_fun : measurable inv_fun) namespace measurable_equiv instance (α β) [measurable_space α] [measurable_space β] : has_coe_to_fun (measurable_equiv α β) := ⟨λ_, α → β, λe, e.to_equiv⟩ lemma coe_eq {α β} [measurable_space α] [measurable_space β] (e : measurable_equiv α β) : (e : α → β) = e.to_equiv := rfl /-- Any measurable space is equivalent to itself. -/ def refl (α : Type*) [measurable_space α] : measurable_equiv α α := { to_equiv := equiv.refl α, measurable_to_fun := measurable_id, measurable_inv_fun := measurable_id } /-- The composition of equivalences between measurable spaces. -/ def trans [measurable_space α] [measurable_space β] [measurable_space γ] (ab : measurable_equiv α β) (bc : measurable_equiv β γ) : measurable_equiv α γ := { to_equiv := ab.to_equiv.trans bc.to_equiv, measurable_to_fun := bc.measurable_to_fun.comp ab.measurable_to_fun, measurable_inv_fun := ab.measurable_inv_fun.comp bc.measurable_inv_fun } lemma trans_to_equiv {α β} [measurable_space α] [measurable_space β] [measurable_space γ] (e : measurable_equiv α β) (f : measurable_equiv β γ) : (e.trans f).to_equiv = e.to_equiv.trans f.to_equiv := rfl /-- The inverse of an equivalence between measurable spaces. -/ def symm [measurable_space α] [measurable_space β] (ab : measurable_equiv α β) : measurable_equiv β α := { to_equiv := ab.to_equiv.symm, measurable_to_fun := ab.measurable_inv_fun, measurable_inv_fun := ab.measurable_to_fun } lemma symm_to_equiv {α β} [measurable_space α] [measurable_space β] (e : measurable_equiv α β) : e.symm.to_equiv = e.to_equiv.symm := rfl /-- Equal measurable spaces are equivalent. -/ protected def cast {α β} [i₁ : measurable_space α] [i₂ : measurable_space β] (h : α = β) (hi : i₁ == i₂) : measurable_equiv α β := { to_equiv := equiv.cast h, measurable_to_fun := by substI h; substI hi; exact measurable_id, measurable_inv_fun := by substI h; substI hi; exact measurable_id } protected lemma measurable {α β} [measurable_space α] [measurable_space β] (e : measurable_equiv α β) : measurable (e : α → β) := e.measurable_to_fun protected lemma measurable_coe_iff {α β γ} [measurable_space α] [measurable_space β] [measurable_space γ] {f : β → γ} (e : measurable_equiv α β) : measurable (f ∘ e) ↔ measurable f := iff.intro (assume hfe, have measurable (f ∘ (e.symm.trans e).to_equiv) := hfe.comp e.symm.measurable, by rwa [trans_to_equiv, symm_to_equiv, equiv.symm_trans] at this) (λh, h.comp e.measurable) /-- Products of equivalent measurable spaces are equivalent. -/ def prod_congr [measurable_space α] [measurable_space β] [measurable_space γ] [measurable_space δ] (ab : measurable_equiv α β) (cd : measurable_equiv γ δ) : measurable_equiv (α × γ) (β × δ) := { to_equiv := equiv.prod_congr ab.to_equiv cd.to_equiv, measurable_to_fun := measurable.prod_mk (ab.measurable_to_fun.comp (measurable.fst measurable_id)) (cd.measurable_to_fun.comp (measurable.snd measurable_id)), measurable_inv_fun := measurable.prod_mk (ab.measurable_inv_fun.comp (measurable.fst measurable_id)) (cd.measurable_inv_fun.comp (measurable.snd measurable_id)) } /-- Products of measurable spaces are symmetric. -/ def prod_comm [measurable_space α] [measurable_space β] : measurable_equiv (α × β) (β × α) := { to_equiv := equiv.prod_comm α β, measurable_to_fun := measurable.prod_mk (measurable.snd measurable_id) (measurable.fst measurable_id), measurable_inv_fun := measurable.prod_mk (measurable.snd measurable_id) (measurable.fst measurable_id) } /-- Sums of measurable spaces are symmetric. -/ def sum_congr [measurable_space α] [measurable_space β] [measurable_space γ] [measurable_space δ] (ab : measurable_equiv α β) (cd : measurable_equiv γ δ) : measurable_equiv (α ⊕ γ) (β ⊕ δ) := { to_equiv := equiv.sum_congr ab.to_equiv cd.to_equiv, measurable_to_fun := begin cases ab with ab' abm, cases ab', cases cd with cd' cdm, cases cd', refine measurable_sum (measurable_inl.comp abm) (measurable_inr.comp cdm) end, measurable_inv_fun := begin cases ab with ab' _ abm, cases ab', cases cd with cd' _ cdm, cases cd', refine measurable_sum (measurable_inl.comp abm) (measurable_inr.comp cdm) end } /-- `set.prod s t ≃ (s × t)` as measurable spaces. -/ def set.prod [measurable_space α] [measurable_space β] (s : set α) (t : set β) : measurable_equiv (s.prod t) (s × t) := { to_equiv := equiv.set.prod s t, measurable_to_fun := measurable.prod_mk measurable_id.subtype_coe.fst.subtype_mk measurable_id.subtype_coe.snd.subtype_mk, measurable_inv_fun := measurable.subtype_mk $ measurable.prod_mk measurable_id.fst.subtype_coe measurable_id.snd.subtype_coe } /-- `univ α ≃ α` as measurable spaces. -/ def set.univ (α : Type*) [measurable_space α] : measurable_equiv (univ : set α) α := { to_equiv := equiv.set.univ α, measurable_to_fun := measurable_id.subtype_coe, measurable_inv_fun := measurable_id.subtype_mk } /-- `{a} ≃ unit` as measurable spaces. -/ def set.singleton [measurable_space α] (a:α) : measurable_equiv ({a} : set α) unit := { to_equiv := equiv.set.singleton a, measurable_to_fun := measurable_const, measurable_inv_fun := measurable_const } /-- A set is equivalent to its image under a function `f` as measurable spaces, if `f` is an injective measurable function that sends measurable sets to measurable sets. -/ noncomputable def set.image [measurable_space α] [measurable_space β] (f : α → β) (s : set α) (hf : function.injective f) (hfm : measurable f) (hfi : ∀s, is_measurable s → is_measurable (f '' s)) : measurable_equiv s (f '' s) := { to_equiv := equiv.set.image f s hf, measurable_to_fun := (hfm.comp measurable_id.subtype_coe).subtype_mk, measurable_inv_fun := assume t ⟨u, (hu : is_measurable u), eq⟩, begin clear_, subst eq, show is_measurable {x : f '' s | ((equiv.set.image f s hf).inv_fun x).val ∈ u}, have : ∀(a ∈ s) (h : ∃a', a' ∈ s ∧ a' = a), classical.some h = a := λa ha h, (classical.some_spec h).2, rw show {x:f '' s | ((equiv.set.image f s hf).inv_fun x).val ∈ u} = subtype.val ⁻¹' (f '' u), by ext ⟨b, a, hbs, rfl⟩; simp [equiv.set.image, equiv.set.image_of_inj_on, hf, this _ hbs], exact (measurable.subtype_coe measurable_id) (f '' u) (hfi u hu) end } /-- The domain of `f` is equivalent to its range as measurable spaces, if `f` is an injective measurable function that sends measurable sets to measurable sets. -/ noncomputable def set.range [measurable_space α] [measurable_space β] (f : α → β) (hf : function.injective f) (hfm : measurable f) (hfi : ∀s, is_measurable s → is_measurable (f '' s)) : measurable_equiv α (range f) := (measurable_equiv.set.univ _).symm.trans $ (measurable_equiv.set.image f univ hf hfm hfi).trans $ measurable_equiv.cast (by rw image_univ) (by rw image_univ) /-- `α` is equivalent to its image in `α ⊕ β` as measurable spaces. -/ def set.range_inl [measurable_space α] [measurable_space β] : measurable_equiv (range sum.inl : set (α ⊕ β)) α := { to_fun := λab, match ab with | ⟨sum.inl a, _⟩ := a | ⟨sum.inr b, p⟩ := have false, by cases p; contradiction, this.elim end, inv_fun := λa, ⟨sum.inl a, a, rfl⟩, left_inv := assume ⟨ab, a, eq⟩, by subst eq; refl, right_inv := assume a, rfl, measurable_to_fun := assume s (hs : is_measurable s), begin refine ⟨_, is_measurable_inl_image hs, set.ext _⟩, rintros ⟨ab, a, rfl⟩, simp [set.range_inl._match_1] end, measurable_inv_fun := measurable.subtype_mk measurable_inl } /-- `β` is equivalent to its image in `α ⊕ β` as measurable spaces. -/ def set.range_inr [measurable_space α] [measurable_space β] : measurable_equiv (range sum.inr : set (α ⊕ β)) β := { to_fun := λab, match ab with | ⟨sum.inr b, _⟩ := b | ⟨sum.inl a, p⟩ := have false, by cases p; contradiction, this.elim end, inv_fun := λb, ⟨sum.inr b, b, rfl⟩, left_inv := assume ⟨ab, b, eq⟩, by subst eq; refl, right_inv := assume b, rfl, measurable_to_fun := assume s (hs : is_measurable s), begin refine ⟨_, is_measurable_inr_image hs, set.ext _⟩, rintros ⟨ab, b, rfl⟩, simp [set.range_inr._match_1] end, measurable_inv_fun := measurable.subtype_mk measurable_inr } /-- Products distribute over sums (on the right) as measurable spaces. -/ def sum_prod_distrib (α β γ) [measurable_space α] [measurable_space β] [measurable_space γ] : measurable_equiv ((α ⊕ β) × γ) ((α × γ) ⊕ (β × γ)) := { to_equiv := equiv.sum_prod_distrib α β γ, measurable_to_fun := begin refine measurable_of_measurable_union_cover ((range sum.inl).prod univ) ((range sum.inr).prod univ) (is_measurable_range_inl.prod is_measurable.univ) (is_measurable_range_inr.prod is_measurable.univ) (assume ⟨ab, c⟩ s, by cases ab; simp [set.prod_eq]) _ _, { refine (set.prod (range sum.inl) univ).symm.measurable_coe_iff.1 _, refine (prod_congr set.range_inl (set.univ _)).symm.measurable_coe_iff.1 _, dsimp [(∘)], convert measurable_inl, ext ⟨a, c⟩, refl }, { refine (set.prod (range sum.inr) univ).symm.measurable_coe_iff.1 _, refine (prod_congr set.range_inr (set.univ _)).symm.measurable_coe_iff.1 _, dsimp [(∘)], convert measurable_inr, ext ⟨b, c⟩, refl } end, measurable_inv_fun := measurable_sum ((measurable_inl.comp (measurable.fst measurable_id)).prod_mk (measurable.snd measurable_id)) ((measurable_inr.comp (measurable.fst measurable_id)).prod_mk (measurable.snd measurable_id)) } /-- Products distribute over sums (on the left) as measurable spaces. -/ def prod_sum_distrib (α β γ) [measurable_space α] [measurable_space β] [measurable_space γ] : measurable_equiv (α × (β ⊕ γ)) ((α × β) ⊕ (α × γ)) := prod_comm.trans $ (sum_prod_distrib _ _ _).trans $ sum_congr prod_comm prod_comm /-- Products distribute over sums as measurable spaces. -/ def sum_prod_sum (α β γ δ) [measurable_space α] [measurable_space β] [measurable_space γ] [measurable_space δ] : measurable_equiv ((α ⊕ β) × (γ ⊕ δ)) (((α × γ) ⊕ (α × δ)) ⊕ ((β × γ) ⊕ (β × δ))) := (sum_prod_distrib _ _ _).trans $ sum_congr (prod_sum_distrib _ _ _) (prod_sum_distrib _ _ _) end measurable_equiv namespace measurable_equiv end measurable_equiv namespace measurable_space /-- A Dynkin system is a collection of subsets of a type `α` that contains the empty set, is closed under complementation and under countable union of pairwise disjoint sets. The disjointness condition is the only difference with `σ`-algebras. The main purpose of Dynkin systems is to provide a powerful induction rule for σ-algebras generated by intersection stable set systems. -/ structure dynkin_system (α : Type*) := (has : set α → Prop) (has_empty : has ∅) (has_compl : ∀{a}, has a → has aᶜ) (has_Union_nat : ∀{f:ℕ → set α}, pairwise (disjoint on f) → (∀i, has (f i)) → has (⋃i, f i)) theorem Union_decode2_disjoint_on {β} [encodable β] {f : β → set α} (hd : pairwise (disjoint on f)) : pairwise (disjoint on λ i, ⋃ b ∈ decode2 β i, f b) := begin rintro i j ij x ⟨h₁, h₂⟩, revert h₁ h₂, simp, intros b₁ e₁ h₁ b₂ e₂ h₂, refine hd _ _ _ ⟨h₁, h₂⟩, cases encodable.mem_decode2.1 e₁, cases encodable.mem_decode2.1 e₂, exact mt (congr_arg _) ij end namespace dynkin_system @[ext] lemma ext : ∀{d₁ d₂ : dynkin_system α}, (∀s:set α, d₁.has s ↔ d₂.has s) → d₁ = d₂ | ⟨s₁, _, _, _⟩ ⟨s₂, _, _, _⟩ h := have s₁ = s₂, from funext $ assume x, propext $ h x, by subst this variable (d : dynkin_system α) lemma has_compl_iff {a} : d.has aᶜ ↔ d.has a := ⟨λ h, by simpa using d.has_compl h, λ h, d.has_compl h⟩ lemma has_univ : d.has univ := by simpa using d.has_compl d.has_empty theorem has_Union {β} [encodable β] {f : β → set α} (hd : pairwise (disjoint on f)) (h : ∀i, d.has (f i)) : d.has (⋃i, f i) := by rw encodable.Union_decode2; exact d.has_Union_nat (Union_decode2_disjoint_on hd) (λ n, encodable.Union_decode2_cases d.has_empty h) theorem has_union {s₁ s₂ : set α} (h₁ : d.has s₁) (h₂ : d.has s₂) (h : s₁ ∩ s₂ ⊆ ∅) : d.has (s₁ ∪ s₂) := by rw union_eq_Union; exact d.has_Union (pairwise_disjoint_on_bool.2 h) (bool.forall_bool.2 ⟨h₂, h₁⟩) lemma has_diff {s₁ s₂ : set α} (h₁ : d.has s₁) (h₂ : d.has s₂) (h : s₂ ⊆ s₁) : d.has (s₁ \ s₂) := d.has_compl_iff.1 begin simp [diff_eq, compl_inter], exact d.has_union (d.has_compl h₁) h₂ (λ x ⟨h₁, h₂⟩, h₁ (h h₂)), end instance : partial_order (dynkin_system α) := { le := λm₁ m₂, m₁.has ≤ m₂.has, le_refl := assume a b, le_refl _, le_trans := assume a b c, le_trans, le_antisymm := assume a b h₁ h₂, ext $ assume s, ⟨h₁ s, h₂ s⟩ } /-- Every measurable space (σ-algebra) forms a Dynkin system -/ def of_measurable_space (m : measurable_space α) : dynkin_system α := { has := m.is_measurable, has_empty := m.is_measurable_empty, has_compl := m.is_measurable_compl, has_Union_nat := assume f _ hf, m.is_measurable_Union f hf } lemma of_measurable_space_le_of_measurable_space_iff {m₁ m₂ : measurable_space α} : of_measurable_space m₁ ≤ of_measurable_space m₂ ↔ m₁ ≤ m₂ := iff.rfl /-- The least Dynkin system containing a collection of basic sets. This inductive type gives the underlying collection of sets. -/ inductive generate_has (s : set (set α)) : set α → Prop | basic : ∀t∈s, generate_has t | empty : generate_has ∅ | compl : ∀{a}, generate_has a → generate_has aᶜ | Union : ∀{f:ℕ → set α}, pairwise (disjoint on f) → (∀i, generate_has (f i)) → generate_has (⋃i, f i) /-- The least Dynkin system containing a collection of basic sets. -/ def generate (s : set (set α)) : dynkin_system α := { has := generate_has s, has_empty := generate_has.empty, has_compl := assume a, generate_has.compl, has_Union_nat := assume f, generate_has.Union } instance : inhabited (dynkin_system α) := ⟨generate univ⟩ /-- If a Dynkin system is closed under binary intersection, then it forms a `σ`-algebra. -/ def to_measurable_space (h_inter : ∀s₁ s₂, d.has s₁ → d.has s₂ → d.has (s₁ ∩ s₂)) := { measurable_space . is_measurable := d.has, is_measurable_empty := d.has_empty, is_measurable_compl := assume s h, d.has_compl h, is_measurable_Union := assume f hf, have ∀n, d.has (disjointed f n), from assume n, disjointed_induct (hf n) (assume t i h, h_inter _ _ h $ d.has_compl $ hf i), have d.has (⋃n, disjointed f n), from d.has_Union disjoint_disjointed this, by rwa [Union_disjointed] at this } lemma of_measurable_space_to_measurable_space (h_inter : ∀s₁ s₂, d.has s₁ → d.has s₂ → d.has (s₁ ∩ s₂)) : of_measurable_space (d.to_measurable_space h_inter) = d := ext $ assume s, iff.rfl /-- If `s` is in a Dynkin system `d`, we can form the new Dynkin system `{s ∩ t | t ∈ d}`. -/ def restrict_on {s : set α} (h : d.has s) : dynkin_system α := { has := λt, d.has (t ∩ s), has_empty := by simp [d.has_empty], has_compl := assume t hts, have tᶜ ∩ s = ((t ∩ s)ᶜ) \ sᶜ, from set.ext $ assume x, by by_cases x ∈ s; simp [h], by rw [this]; from d.has_diff (d.has_compl hts) (d.has_compl h) (compl_subset_compl.mpr $ inter_subset_right _ _), has_Union_nat := assume f hd hf, begin rw [inter_comm, inter_Union], apply d.has_Union_nat, { exact λ i j h x ⟨⟨_, h₁⟩, _, h₂⟩, hd i j h ⟨h₁, h₂⟩ }, { simpa [inter_comm] using hf }, end } lemma generate_le {s : set (set α)} (h : ∀t∈s, d.has t) : generate s ≤ d := λ t ht, ht.rec_on h d.has_empty (assume a _ h, d.has_compl h) (assume f hd _ hf, d.has_Union hd hf) lemma generate_inter {s : set (set α)} (hs : ∀t₁ t₂ : set α, t₁ ∈ s → t₂ ∈ s → (t₁ ∩ t₂).nonempty → t₁ ∩ t₂ ∈ s) {t₁ t₂ : set α} (ht₁ : (generate s).has t₁) (ht₂ : (generate s).has t₂) : (generate s).has (t₁ ∩ t₂) := have generate s ≤ (generate s).restrict_on ht₂, from generate_le _ $ assume s₁ hs₁, have (generate s).has s₁, from generate_has.basic s₁ hs₁, have generate s ≤ (generate s).restrict_on this, from generate_le _ $ assume s₂ hs₂, show (generate s).has (s₂ ∩ s₁), from (s₂ ∩ s₁).eq_empty_or_nonempty.elim (λ h, h.symm ▸ generate_has.empty) (λ h, generate_has.basic _ (hs _ _ hs₂ hs₁ h)), have (generate s).has (t₂ ∩ s₁), from this _ ht₂, show (generate s).has (s₁ ∩ t₂), by rwa [inter_comm], this _ ht₁ lemma generate_from_eq {s : set (set α)} (hs : ∀t₁ t₂ : set α, t₁ ∈ s → t₂ ∈ s → (t₁ ∩ t₂).nonempty → t₁ ∩ t₂ ∈ s) : generate_from s = (generate s).to_measurable_space (assume t₁ t₂, generate_inter hs) := le_antisymm (generate_from_le $ assume t ht, generate_has.basic t ht) (of_measurable_space_le_of_measurable_space_iff.mp $ by rw [of_measurable_space_to_measurable_space]; from (generate_le _ $ assume t ht, is_measurable_generate_from ht)) end dynkin_system lemma induction_on_inter {C : set α → Prop} {s : set (set α)} {m : measurable_space α} (h_eq : m = generate_from s) (h_inter : ∀t₁ t₂ : set α, t₁ ∈ s → t₂ ∈ s → (t₁ ∩ t₂).nonempty → t₁ ∩ t₂ ∈ s) (h_empty : C ∅) (h_basic : ∀t∈s, C t) (h_compl : ∀t, m.is_measurable t → C t → C tᶜ) (h_union : ∀f:ℕ → set α, (∀i j, i ≠ j → f i ∩ f j ⊆ ∅) → (∀i, m.is_measurable (f i)) → (∀i, C (f i)) → C (⋃i, f i)) : ∀{t}, m.is_measurable t → C t := have eq : m.is_measurable = dynkin_system.generate_has s, by rw [h_eq, dynkin_system.generate_from_eq h_inter]; refl, assume t ht, have dynkin_system.generate_has s t, by rwa [eq] at ht, this.rec_on h_basic h_empty (assume t ht, h_compl t $ by rw [eq]; exact ht) (assume f hf ht, h_union f hf $ assume i, by rw [eq]; exact ht _) end measurable_space
92aeb1b2e8da11007102808e917672f3b6b0bc72
618003631150032a5676f229d13a079ac875ff77
/src/tactic/monotonicity/default.lean
bf46f602e73a8e0aab09bf93889947f7888345cb
[ "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
220
lean
/- Copyright (c) 2019 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Simon Hudon -/ import tactic.monotonicity.interactive import tactic.monotonicity.lemmas
9f2be6bcadf9d4e737e6c6777b0ead50413af521
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/tests/lean/run/doNotation5.lean
9735b3fbe9ca281b428a17b6fecf20d80e5d2fcd
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
527
lean
abbrev M := ExceptT String $ ExceptT Nat $ StateM Nat def inc (x : Nat) : M Unit := do if (← get) >= 100 then throwThe Nat ((← get) + x) modify (· + x) def dec (x : Nat) : M Unit := do if (← get) - x == 0 then throw "balance is zero" modify (· - x) def f (x y : Nat) : M Nat := do try inc x dec y get catch ex : String => dbgTrace! "string exception {ex}" pure 1000 catch ex : Nat => dbgTrace! "nat exception {ex}" pure ex #eval (f 10 20).run 1000 #eval (f 10 200).run 10 #eval (f 10 20).run 30
fcaa578d4611865b0a98e55190dba728af6c5b52
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/lint/default_auto.lean
a35ad8798a482188b5c3cebc1869c0c4f9efc99e
[]
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
445
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Robert Y. Lewis, Gabriel Ebner -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.lint.frontend import Mathlib.tactic.lint.simp import Mathlib.tactic.lint.type_classes import Mathlib.tactic.lint.misc import Mathlib.PostPort namespace Mathlib end Mathlib
03446107e27dcb5d8c67c6b48939a7a76c2c2168
c777c32c8e484e195053731103c5e52af26a25d1
/src/analysis/normed_space/exponential.lean
c75855885cbb512abfc611603ae8249c0ebed675
[ "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
27,193
lean
/- Copyright (c) 2021 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker, Eric Wieser -/ import analysis.analytic.basic import analysis.complex.basic import analysis.normed.field.infinite_sum import data.nat.choose.cast import data.finset.noncomm_prod import topology.algebra.algebra /-! # Exponential in a Banach algebra In this file, we define `exp 𝕂 : 𝔸 → 𝔸`, the exponential map in a topological algebra `𝔸` over a field `𝕂`. While for most interesting results we need `𝔸` to be normed algebra, we do not require this in the definition in order to make `exp` independent of a particular choice of norm. The definition also does not require that `𝔸` be complete, but we need to assume it for most results. We then prove some basic results, but we avoid importing derivatives here to minimize dependencies. Results involving derivatives and comparisons with `real.exp` and `complex.exp` can be found in `analysis/special_functions/exponential`. ## Main results We prove most result for an arbitrary field `𝕂`, and then specialize to `𝕂 = ℝ` or `𝕂 = ℂ`. ### General case - `exp_add_of_commute_of_mem_ball` : if `𝕂` has characteristic zero, then given two commuting elements `x` and `y` in the disk of convergence, we have `exp 𝕂 (x+y) = (exp 𝕂 x) * (exp 𝕂 y)` - `exp_add_of_mem_ball` : if `𝕂` has characteristic zero and `𝔸` is commutative, then given two elements `x` and `y` in the disk of convergence, we have `exp 𝕂 (x+y) = (exp 𝕂 x) * (exp 𝕂 y)` - `exp_neg_of_mem_ball` : if `𝕂` has characteristic zero and `𝔸` is a division ring, then given an element `x` in the disk of convergence, we have `exp 𝕂 (-x) = (exp 𝕂 x)⁻¹`. ### `𝕂 = ℝ` or `𝕂 = ℂ` - `exp_series_radius_eq_top` : the `formal_multilinear_series` defining `exp 𝕂` has infinite radius of convergence - `exp_add_of_commute` : given two commuting elements `x` and `y`, we have `exp 𝕂 (x+y) = (exp 𝕂 x) * (exp 𝕂 y)` - `exp_add` : if `𝔸` is commutative, then we have `exp 𝕂 (x+y) = (exp 𝕂 x) * (exp 𝕂 y)` for any `x` and `y` - `exp_neg` : if `𝔸` is a division ring, then we have `exp 𝕂 (-x) = (exp 𝕂 x)⁻¹`. - `exp_sum_of_commute` : the analogous result to `exp_add_of_commute` for `finset.sum`. - `exp_sum` : the analogous result to `exp_add` for `finset.sum`. - `exp_nsmul` : repeated addition in the domain corresponds to repeated multiplication in the codomain. - `exp_zsmul` : repeated addition in the domain corresponds to repeated multiplication in the codomain. ### Other useful compatibility results - `exp_eq_exp` : if `𝔸` is a normed algebra over two fields `𝕂` and `𝕂'`, then `exp 𝕂 = exp 𝕂' 𝔸` -/ open filter is_R_or_C continuous_multilinear_map normed_field asymptotics open_locale nat topology big_operators ennreal section topological_algebra variables (𝕂 𝔸 : Type*) [field 𝕂] [ring 𝔸] [algebra 𝕂 𝔸] [topological_space 𝔸] [topological_ring 𝔸] /-- `exp_series 𝕂 𝔸` is the `formal_multilinear_series` whose `n`-th term is the map `(xᵢ) : 𝔸ⁿ ↦ (1/n! : 𝕂) • ∏ xᵢ`. Its sum is the exponential map `exp 𝕂 : 𝔸 → 𝔸`. -/ def exp_series : formal_multilinear_series 𝕂 𝔸 𝔸 := λ n, (n!⁻¹ : 𝕂) • continuous_multilinear_map.mk_pi_algebra_fin 𝕂 n 𝔸 variables {𝔸} /-- `exp 𝕂 : 𝔸 → 𝔸` is the exponential map determined by the action of `𝕂` on `𝔸`. It is defined as the sum of the `formal_multilinear_series` `exp_series 𝕂 𝔸`. Note that when `𝔸 = matrix n n 𝕂`, this is the **Matrix Exponential**; see [`analysis.normed_space.matrix_exponential`](../matrix_exponential) for lemmas specific to that case. -/ noncomputable def exp (x : 𝔸) : 𝔸 := (exp_series 𝕂 𝔸).sum x variables {𝕂} lemma exp_series_apply_eq (x : 𝔸) (n : ℕ) : exp_series 𝕂 𝔸 n (λ _, x) = (n!⁻¹ : 𝕂) • x^n := by simp [exp_series] lemma exp_series_apply_eq' (x : 𝔸) : (λ n, exp_series 𝕂 𝔸 n (λ _, x)) = (λ n, (n!⁻¹ : 𝕂) • x^n) := funext (exp_series_apply_eq x) lemma exp_series_sum_eq (x : 𝔸) : (exp_series 𝕂 𝔸).sum x = ∑' (n : ℕ), (n!⁻¹ : 𝕂) • x^n := tsum_congr (λ n, exp_series_apply_eq x n) lemma exp_eq_tsum : exp 𝕂 = (λ x : 𝔸, ∑' (n : ℕ), (n!⁻¹ : 𝕂) • x^n) := funext exp_series_sum_eq lemma exp_series_apply_zero (n : ℕ) : exp_series 𝕂 𝔸 n (λ _, (0 : 𝔸)) = pi.single 0 1 n := begin rw exp_series_apply_eq, cases n, { rw [pow_zero, nat.factorial_zero, nat.cast_one, inv_one, one_smul, pi.single_eq_same], }, { rw [zero_pow (nat.succ_pos _), smul_zero, pi.single_eq_of_ne (n.succ_ne_zero)], }, end @[simp] lemma exp_zero [t2_space 𝔸] : exp 𝕂 (0 : 𝔸) = 1 := by simp_rw [exp_eq_tsum, ←exp_series_apply_eq, exp_series_apply_zero, tsum_pi_single] @[simp] lemma exp_op [t2_space 𝔸] (x : 𝔸) : exp 𝕂 (mul_opposite.op x) = mul_opposite.op (exp 𝕂 x) := by simp_rw [exp, exp_series_sum_eq, ←mul_opposite.op_pow, ←mul_opposite.op_smul, tsum_op] @[simp] lemma exp_unop [t2_space 𝔸] (x : 𝔸ᵐᵒᵖ) : exp 𝕂 (mul_opposite.unop x) = mul_opposite.unop (exp 𝕂 x) := by simp_rw [exp, exp_series_sum_eq, ←mul_opposite.unop_pow, ←mul_opposite.unop_smul, tsum_unop] lemma star_exp [t2_space 𝔸] [star_ring 𝔸] [has_continuous_star 𝔸] (x : 𝔸) : star (exp 𝕂 x) = exp 𝕂 (star x) := by simp_rw [exp_eq_tsum, ←star_pow, ←star_inv_nat_cast_smul, ←tsum_star] variables (𝕂) lemma is_self_adjoint.exp [t2_space 𝔸] [star_ring 𝔸] [has_continuous_star 𝔸] {x : 𝔸} (h : is_self_adjoint x) : is_self_adjoint (exp 𝕂 x) := (star_exp x).trans $ h.symm ▸ rfl lemma commute.exp_right [t2_space 𝔸] {x y : 𝔸} (h : commute x y) : commute x (exp 𝕂 y) := begin rw exp_eq_tsum, exact commute.tsum_right x (λ n, (h.pow_right n).smul_right _), end lemma commute.exp_left [t2_space 𝔸] {x y : 𝔸} (h : commute x y) : commute (exp 𝕂 x) y := (h.symm.exp_right 𝕂).symm lemma commute.exp [t2_space 𝔸] {x y : 𝔸} (h : commute x y) : commute (exp 𝕂 x) (exp 𝕂 y) := (h.exp_left _).exp_right _ end topological_algebra section topological_division_algebra variables {𝕂 𝔸 : Type*} [field 𝕂] [division_ring 𝔸] [algebra 𝕂 𝔸] [topological_space 𝔸] [topological_ring 𝔸] lemma exp_series_apply_eq_div (x : 𝔸) (n : ℕ) : exp_series 𝕂 𝔸 n (λ _, x) = x^n / n! := by rw [div_eq_mul_inv, ←(nat.cast_commute n! (x ^ n)).inv_left₀.eq, ←smul_eq_mul, exp_series_apply_eq, inv_nat_cast_smul_eq _ _ _ _] lemma exp_series_apply_eq_div' (x : 𝔸) : (λ n, exp_series 𝕂 𝔸 n (λ _, x)) = (λ n, x^n / n!) := funext (exp_series_apply_eq_div x) lemma exp_series_sum_eq_div (x : 𝔸) : (exp_series 𝕂 𝔸).sum x = ∑' (n : ℕ), x^n / n! := tsum_congr (exp_series_apply_eq_div x) lemma exp_eq_tsum_div : exp 𝕂 = (λ x : 𝔸, ∑' (n : ℕ), x^n / n!) := funext exp_series_sum_eq_div end topological_division_algebra section normed section any_field_any_algebra variables {𝕂 𝔸 𝔹 : Type*} [nontrivially_normed_field 𝕂] variables [normed_ring 𝔸] [normed_ring 𝔹] [normed_algebra 𝕂 𝔸] [normed_algebra 𝕂 𝔹] lemma norm_exp_series_summable_of_mem_ball (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : summable (λ n, ‖exp_series 𝕂 𝔸 n (λ _, x)‖) := (exp_series 𝕂 𝔸).summable_norm_apply hx lemma norm_exp_series_summable_of_mem_ball' (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : summable (λ n, ‖(n!⁻¹ : 𝕂) • x^n‖) := begin change summable (norm ∘ _), rw ← exp_series_apply_eq', exact norm_exp_series_summable_of_mem_ball x hx end section complete_algebra variables [complete_space 𝔸] lemma exp_series_summable_of_mem_ball (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : summable (λ n, exp_series 𝕂 𝔸 n (λ _, x)) := summable_of_summable_norm (norm_exp_series_summable_of_mem_ball x hx) lemma exp_series_summable_of_mem_ball' (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : summable (λ n, (n!⁻¹ : 𝕂) • x^n) := summable_of_summable_norm (norm_exp_series_summable_of_mem_ball' x hx) lemma exp_series_has_sum_exp_of_mem_ball (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : has_sum (λ n, exp_series 𝕂 𝔸 n (λ _, x)) (exp 𝕂 x) := formal_multilinear_series.has_sum (exp_series 𝕂 𝔸) hx lemma exp_series_has_sum_exp_of_mem_ball' (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : has_sum (λ n, (n!⁻¹ : 𝕂) • x^n) (exp 𝕂 x):= begin rw ← exp_series_apply_eq', exact exp_series_has_sum_exp_of_mem_ball x hx end lemma has_fpower_series_on_ball_exp_of_radius_pos (h : 0 < (exp_series 𝕂 𝔸).radius) : has_fpower_series_on_ball (exp 𝕂) (exp_series 𝕂 𝔸) 0 (exp_series 𝕂 𝔸).radius := (exp_series 𝕂 𝔸).has_fpower_series_on_ball h lemma has_fpower_series_at_exp_zero_of_radius_pos (h : 0 < (exp_series 𝕂 𝔸).radius) : has_fpower_series_at (exp 𝕂) (exp_series 𝕂 𝔸) 0 := (has_fpower_series_on_ball_exp_of_radius_pos h).has_fpower_series_at lemma continuous_on_exp : continuous_on (exp 𝕂 : 𝔸 → 𝔸) (emetric.ball 0 (exp_series 𝕂 𝔸).radius) := formal_multilinear_series.continuous_on lemma analytic_at_exp_of_mem_ball (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : analytic_at 𝕂 (exp 𝕂) x:= begin by_cases h : (exp_series 𝕂 𝔸).radius = 0, { rw h at hx, exact (ennreal.not_lt_zero hx).elim }, { have h := pos_iff_ne_zero.mpr h, exact (has_fpower_series_on_ball_exp_of_radius_pos h).analytic_at_of_mem hx } end /-- In a Banach-algebra `𝔸` over a normed field `𝕂` of characteristic zero, if `x` and `y` are in the disk of convergence and commute, then `exp 𝕂 (x + y) = (exp 𝕂 x) * (exp 𝕂 y)`. -/ lemma exp_add_of_commute_of_mem_ball [char_zero 𝕂] {x y : 𝔸} (hxy : commute x y) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) (hy : y ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : exp 𝕂 (x + y) = (exp 𝕂 x) * (exp 𝕂 y) := begin rw [exp_eq_tsum, tsum_mul_tsum_eq_tsum_sum_antidiagonal_of_summable_norm (norm_exp_series_summable_of_mem_ball' x hx) (norm_exp_series_summable_of_mem_ball' y hy)], dsimp only, conv_lhs {congr, funext, rw [hxy.add_pow' _, finset.smul_sum]}, refine tsum_congr (λ n, finset.sum_congr rfl $ λ kl hkl, _), rw [nsmul_eq_smul_cast 𝕂, smul_smul, smul_mul_smul, ← (finset.nat.mem_antidiagonal.mp hkl), nat.cast_add_choose, (finset.nat.mem_antidiagonal.mp hkl)], congr' 1, have : (n! : 𝕂) ≠ 0 := nat.cast_ne_zero.mpr n.factorial_ne_zero, field_simp [this] end /-- `exp 𝕂 x` has explicit two-sided inverse `exp 𝕂 (-x)`. -/ noncomputable def invertible_exp_of_mem_ball [char_zero 𝕂] {x : 𝔸} (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : invertible (exp 𝕂 x) := { inv_of := exp 𝕂 (-x), inv_of_mul_self := begin have hnx : -x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius, { rw [emetric.mem_ball, ←neg_zero, edist_neg_neg], exact hx }, rw [←exp_add_of_commute_of_mem_ball (commute.neg_left $ commute.refl x) hnx hx, neg_add_self, exp_zero], end, mul_inv_of_self := begin have hnx : -x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius, { rw [emetric.mem_ball, ←neg_zero, edist_neg_neg], exact hx }, rw [←exp_add_of_commute_of_mem_ball (commute.neg_right $ commute.refl x) hx hnx, add_neg_self, exp_zero], end } lemma is_unit_exp_of_mem_ball [char_zero 𝕂] {x : 𝔸} (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : is_unit (exp 𝕂 x) := @is_unit_of_invertible _ _ _ (invertible_exp_of_mem_ball hx) lemma inv_of_exp_of_mem_ball [char_zero 𝕂] {x : 𝔸} (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) [invertible (exp 𝕂 x)] : ⅟(exp 𝕂 x) = exp 𝕂 (-x) := by { letI := invertible_exp_of_mem_ball hx, convert (rfl : ⅟(exp 𝕂 x) = _) } /-- Any continuous ring homomorphism commutes with `exp`. -/ lemma map_exp_of_mem_ball {F} [ring_hom_class F 𝔸 𝔹] (f : F) (hf : continuous f) (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : f (exp 𝕂 x) = exp 𝕂 (f x) := begin rw [exp_eq_tsum, exp_eq_tsum], refine ((exp_series_summable_of_mem_ball' _ hx).has_sum.map f hf).tsum_eq.symm.trans _, dsimp only [function.comp], simp_rw [one_div, map_inv_nat_cast_smul f 𝕂 𝕂, map_pow], end end complete_algebra lemma algebra_map_exp_comm_of_mem_ball [complete_space 𝕂] (x : 𝕂) (hx : x ∈ emetric.ball (0 : 𝕂) (exp_series 𝕂 𝕂).radius) : algebra_map 𝕂 𝔸 (exp 𝕂 x) = exp 𝕂 (algebra_map 𝕂 𝔸 x) := map_exp_of_mem_ball _ (continuous_algebra_map 𝕂 𝔸) _ hx end any_field_any_algebra section any_field_division_algebra variables {𝕂 𝔸 : Type*} [nontrivially_normed_field 𝕂] [normed_division_ring 𝔸] [normed_algebra 𝕂 𝔸] variables (𝕂) lemma norm_exp_series_div_summable_of_mem_ball (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : summable (λ n, ‖x^n / n!‖) := begin change summable (norm ∘ _), rw ← exp_series_apply_eq_div' x, exact norm_exp_series_summable_of_mem_ball x hx end lemma exp_series_div_summable_of_mem_ball [complete_space 𝔸] (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : summable (λ n, x^n / n!) := summable_of_summable_norm (norm_exp_series_div_summable_of_mem_ball 𝕂 x hx) lemma exp_series_div_has_sum_exp_of_mem_ball [complete_space 𝔸] (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : has_sum (λ n, x^n / n!) (exp 𝕂 x) := begin rw ← exp_series_apply_eq_div' x, exact exp_series_has_sum_exp_of_mem_ball x hx end variables {𝕂} lemma exp_neg_of_mem_ball [char_zero 𝕂] [complete_space 𝔸] {x : 𝔸} (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : exp 𝕂 (-x) = (exp 𝕂 x)⁻¹ := begin letI := invertible_exp_of_mem_ball hx, exact inv_of_eq_inv (exp 𝕂 x), end end any_field_division_algebra section any_field_comm_algebra variables {𝕂 𝔸 : Type*} [nontrivially_normed_field 𝕂] [normed_comm_ring 𝔸] [normed_algebra 𝕂 𝔸] [complete_space 𝔸] /-- In a commutative Banach-algebra `𝔸` over a normed field `𝕂` of characteristic zero, `exp 𝕂 (x+y) = (exp 𝕂 x) * (exp 𝕂 y)` for all `x`, `y` in the disk of convergence. -/ lemma exp_add_of_mem_ball [char_zero 𝕂] {x y : 𝔸} (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) (hy : y ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : exp 𝕂 (x + y) = (exp 𝕂 x) * (exp 𝕂 y) := exp_add_of_commute_of_mem_ball (commute.all x y) hx hy end any_field_comm_algebra section is_R_or_C section any_algebra variables (𝕂 𝔸 𝔹 : Type*) [is_R_or_C 𝕂] [normed_ring 𝔸] [normed_algebra 𝕂 𝔸] variables [normed_ring 𝔹] [normed_algebra 𝕂 𝔹] /-- In a normed algebra `𝔸` over `𝕂 = ℝ` or `𝕂 = ℂ`, the series defining the exponential map has an infinite radius of convergence. -/ lemma exp_series_radius_eq_top : (exp_series 𝕂 𝔸).radius = ∞ := begin refine (exp_series 𝕂 𝔸).radius_eq_top_of_summable_norm (λ r, _), refine summable_of_norm_bounded_eventually _ (real.summable_pow_div_factorial r) _, filter_upwards [eventually_cofinite_ne 0] with n hn, rw [norm_mul, norm_norm (exp_series 𝕂 𝔸 n), exp_series, norm_smul, norm_inv, norm_pow, nnreal.norm_eq, norm_nat_cast, mul_comm, ←mul_assoc, ←div_eq_mul_inv], have : ‖continuous_multilinear_map.mk_pi_algebra_fin 𝕂 n 𝔸‖ ≤ 1 := norm_mk_pi_algebra_fin_le_of_pos (nat.pos_of_ne_zero hn), exact mul_le_of_le_one_right (div_nonneg (pow_nonneg r.coe_nonneg n) n!.cast_nonneg) this end lemma exp_series_radius_pos : 0 < (exp_series 𝕂 𝔸).radius := begin rw exp_series_radius_eq_top, exact with_top.zero_lt_top end variables {𝕂 𝔸 𝔹} lemma norm_exp_series_summable (x : 𝔸) : summable (λ n, ‖exp_series 𝕂 𝔸 n (λ _, x)‖) := norm_exp_series_summable_of_mem_ball x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) lemma norm_exp_series_summable' (x : 𝔸) : summable (λ n, ‖(n!⁻¹ : 𝕂) • x^n‖) := norm_exp_series_summable_of_mem_ball' x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) section complete_algebra variables [complete_space 𝔸] lemma exp_series_summable (x : 𝔸) : summable (λ n, exp_series 𝕂 𝔸 n (λ _, x)) := summable_of_summable_norm (norm_exp_series_summable x) lemma exp_series_summable' (x : 𝔸) : summable (λ n, (n!⁻¹ : 𝕂) • x^n) := summable_of_summable_norm (norm_exp_series_summable' x) lemma exp_series_has_sum_exp (x : 𝔸) : has_sum (λ n, exp_series 𝕂 𝔸 n (λ _, x)) (exp 𝕂 x) := exp_series_has_sum_exp_of_mem_ball x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) lemma exp_series_has_sum_exp' (x : 𝔸) : has_sum (λ n, (n!⁻¹ : 𝕂) • x^n) (exp 𝕂 x):= exp_series_has_sum_exp_of_mem_ball' x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) lemma exp_has_fpower_series_on_ball : has_fpower_series_on_ball (exp 𝕂) (exp_series 𝕂 𝔸) 0 ∞ := exp_series_radius_eq_top 𝕂 𝔸 ▸ has_fpower_series_on_ball_exp_of_radius_pos (exp_series_radius_pos _ _) lemma exp_has_fpower_series_at_zero : has_fpower_series_at (exp 𝕂) (exp_series 𝕂 𝔸) 0 := exp_has_fpower_series_on_ball.has_fpower_series_at lemma exp_continuous : continuous (exp 𝕂 : 𝔸 → 𝔸) := begin rw [continuous_iff_continuous_on_univ, ← metric.eball_top_eq_univ (0 : 𝔸), ← exp_series_radius_eq_top 𝕂 𝔸], exact continuous_on_exp end lemma exp_analytic (x : 𝔸) : analytic_at 𝕂 (exp 𝕂) x := analytic_at_exp_of_mem_ball x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) /-- In a Banach-algebra `𝔸` over `𝕂 = ℝ` or `𝕂 = ℂ`, if `x` and `y` commute, then `exp 𝕂 (x+y) = (exp 𝕂 x) * (exp 𝕂 y)`. -/ lemma exp_add_of_commute {x y : 𝔸} (hxy : commute x y) : exp 𝕂 (x + y) = (exp 𝕂 x) * (exp 𝕂 y) := exp_add_of_commute_of_mem_ball hxy ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) section variables (𝕂) /-- `exp 𝕂 x` has explicit two-sided inverse `exp 𝕂 (-x)`. -/ noncomputable def invertible_exp (x : 𝔸) : invertible (exp 𝕂 x) := invertible_exp_of_mem_ball $ (exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _ lemma is_unit_exp (x : 𝔸) : is_unit (exp 𝕂 x) := is_unit_exp_of_mem_ball $ (exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _ lemma inv_of_exp (x : 𝔸) [invertible (exp 𝕂 x)] : ⅟(exp 𝕂 x) = exp 𝕂 (-x) := inv_of_exp_of_mem_ball $ (exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _ lemma ring.inverse_exp (x : 𝔸) : ring.inverse (exp 𝕂 x) = exp 𝕂 (-x) := begin letI := invertible_exp 𝕂 x, exact ring.inverse_invertible _, end lemma exp_mem_unitary_of_mem_skew_adjoint [star_ring 𝔸] [has_continuous_star 𝔸] {x : 𝔸} (h : x ∈ skew_adjoint 𝔸) : exp 𝕂 x ∈ unitary 𝔸 := by rw [unitary.mem_iff, star_exp, skew_adjoint.mem_iff.mp h, ←exp_add_of_commute (commute.refl x).neg_left, ←exp_add_of_commute (commute.refl x).neg_right, add_left_neg, add_right_neg, exp_zero, and_self] end /-- In a Banach-algebra `𝔸` over `𝕂 = ℝ` or `𝕂 = ℂ`, if a family of elements `f i` mutually commute then `exp 𝕂 (∑ i, f i) = ∏ i, exp 𝕂 (f i)`. -/ lemma exp_sum_of_commute {ι} (s : finset ι) (f : ι → 𝔸) (h : (s : set ι).pairwise $ λ i j, commute (f i) (f j)) : exp 𝕂 (∑ i in s, f i) = s.noncomm_prod (λ i, exp 𝕂 (f i)) (λ i hi j hj _, (h.of_refl hi hj).exp 𝕂) := begin classical, induction s using finset.induction_on with a s ha ih, { simp }, rw [finset.noncomm_prod_insert_of_not_mem _ _ _ _ ha, finset.sum_insert ha, exp_add_of_commute, ih (h.mono $ finset.subset_insert _ _)], refine commute.sum_right _ _ _ (λ i hi, _), exact h.of_refl (finset.mem_insert_self _ _) (finset.mem_insert_of_mem hi), end lemma exp_nsmul (n : ℕ) (x : 𝔸) : exp 𝕂 (n • x) = exp 𝕂 x ^ n := begin induction n with n ih, { rw [zero_smul, pow_zero, exp_zero], }, { rw [succ_nsmul, pow_succ, exp_add_of_commute ((commute.refl x).smul_right n), ih] } end variables (𝕂) /-- Any continuous ring homomorphism commutes with `exp`. -/ lemma map_exp {F} [ring_hom_class F 𝔸 𝔹] (f : F) (hf : continuous f) (x : 𝔸) : f (exp 𝕂 x) = exp 𝕂 (f x) := map_exp_of_mem_ball f hf x $ (exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _ lemma exp_smul {G} [monoid G] [mul_semiring_action G 𝔸] [has_continuous_const_smul G 𝔸] (g : G) (x : 𝔸) : exp 𝕂 (g • x) = g • exp 𝕂 x := (map_exp 𝕂 (mul_semiring_action.to_ring_hom G 𝔸 g) (continuous_const_smul _) x).symm lemma exp_units_conj (y : 𝔸ˣ) (x : 𝔸) : exp 𝕂 (y * x * ↑(y⁻¹) : 𝔸) = y * exp 𝕂 x * ↑(y⁻¹) := exp_smul _ (conj_act.to_conj_act y) x lemma exp_units_conj' (y : 𝔸ˣ) (x : 𝔸) : exp 𝕂 (↑(y⁻¹) * x * y) = ↑(y⁻¹) * exp 𝕂 x * y := exp_units_conj _ _ _ @[simp] lemma prod.fst_exp [complete_space 𝔹] (x : 𝔸 × 𝔹) : (exp 𝕂 x).fst = exp 𝕂 x.fst := map_exp _ (ring_hom.fst 𝔸 𝔹) continuous_fst x @[simp] lemma prod.snd_exp [complete_space 𝔹] (x : 𝔸 × 𝔹) : (exp 𝕂 x).snd = exp 𝕂 x.snd := map_exp _ (ring_hom.snd 𝔸 𝔹) continuous_snd x @[simp] lemma pi.exp_apply {ι : Type*} {𝔸 : ι → Type*} [fintype ι] [Π i, normed_ring (𝔸 i)] [Π i, normed_algebra 𝕂 (𝔸 i)] [Π i, complete_space (𝔸 i)] (x : Π i, 𝔸 i) (i : ι) : exp 𝕂 x i = exp 𝕂 (x i) := begin -- Lean struggles to infer this instance due to it wanting `[Π i, semi_normed_ring (𝔸 i)]` letI : normed_algebra 𝕂 (Π i, 𝔸 i) := pi.normed_algebra _, exact map_exp _ (pi.eval_ring_hom 𝔸 i) (continuous_apply _) x end lemma pi.exp_def {ι : Type*} {𝔸 : ι → Type*} [fintype ι] [Π i, normed_ring (𝔸 i)] [Π i, normed_algebra 𝕂 (𝔸 i)] [Π i, complete_space (𝔸 i)] (x : Π i, 𝔸 i) : exp 𝕂 x = λ i, exp 𝕂 (x i) := funext $ pi.exp_apply 𝕂 x lemma function.update_exp {ι : Type*} {𝔸 : ι → Type*} [fintype ι] [decidable_eq ι] [Π i, normed_ring (𝔸 i)] [Π i, normed_algebra 𝕂 (𝔸 i)] [Π i, complete_space (𝔸 i)] (x : Π i, 𝔸 i) (j : ι) (xj : 𝔸 j) : function.update (exp 𝕂 x) j (exp 𝕂 xj) = exp 𝕂 (function.update x j xj) := begin ext i, simp_rw [pi.exp_def], exact (function.apply_update (λ i, exp 𝕂) x j xj i).symm, end end complete_algebra lemma algebra_map_exp_comm (x : 𝕂) : algebra_map 𝕂 𝔸 (exp 𝕂 x) = exp 𝕂 (algebra_map 𝕂 𝔸 x) := algebra_map_exp_comm_of_mem_ball x $ (exp_series_radius_eq_top 𝕂 𝕂).symm ▸ edist_lt_top _ _ end any_algebra section division_algebra variables {𝕂 𝔸 : Type*} [is_R_or_C 𝕂] [normed_division_ring 𝔸] [normed_algebra 𝕂 𝔸] variables (𝕂) lemma norm_exp_series_div_summable (x : 𝔸) : summable (λ n, ‖x^n / n!‖) := norm_exp_series_div_summable_of_mem_ball 𝕂 x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) variables [complete_space 𝔸] lemma exp_series_div_summable (x : 𝔸) : summable (λ n, x^n / n!) := summable_of_summable_norm (norm_exp_series_div_summable 𝕂 x) lemma exp_series_div_has_sum_exp (x : 𝔸) : has_sum (λ n, x^n / n!) (exp 𝕂 x):= exp_series_div_has_sum_exp_of_mem_ball 𝕂 x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) variables {𝕂} lemma exp_neg (x : 𝔸) : exp 𝕂 (-x) = (exp 𝕂 x)⁻¹ := exp_neg_of_mem_ball $ (exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _ lemma exp_zsmul (z : ℤ) (x : 𝔸) : exp 𝕂 (z • x) = (exp 𝕂 x) ^ z := begin obtain ⟨n, rfl | rfl⟩ := z.eq_coe_or_neg, { rw [zpow_coe_nat, coe_nat_zsmul, exp_nsmul] }, { rw [zpow_neg, zpow_coe_nat, neg_smul, exp_neg, coe_nat_zsmul, exp_nsmul] }, end lemma exp_conj (y : 𝔸) (x : 𝔸) (hy : y ≠ 0) : exp 𝕂 (y * x * y⁻¹) = y * exp 𝕂 x * y⁻¹ := exp_units_conj _ (units.mk0 y hy) x lemma exp_conj' (y : 𝔸) (x : 𝔸) (hy : y ≠ 0) : exp 𝕂 (y⁻¹ * x * y) = y⁻¹ * exp 𝕂 x * y := exp_units_conj' _ (units.mk0 y hy) x end division_algebra section comm_algebra variables {𝕂 𝔸 : Type*} [is_R_or_C 𝕂] [normed_comm_ring 𝔸] [normed_algebra 𝕂 𝔸] [complete_space 𝔸] /-- In a commutative Banach-algebra `𝔸` over `𝕂 = ℝ` or `𝕂 = ℂ`, `exp 𝕂 (x+y) = (exp 𝕂 x) * (exp 𝕂 y)`. -/ lemma exp_add {x y : 𝔸} : exp 𝕂 (x + y) = (exp 𝕂 x) * (exp 𝕂 y) := exp_add_of_mem_ball ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) /-- A version of `exp_sum_of_commute` for a commutative Banach-algebra. -/ lemma exp_sum {ι} (s : finset ι) (f : ι → 𝔸) : exp 𝕂 (∑ i in s, f i) = ∏ i in s, exp 𝕂 (f i) := begin rw [exp_sum_of_commute, finset.noncomm_prod_eq_prod], exact λ i hi j hj _, commute.all _ _, end end comm_algebra end is_R_or_C end normed section scalar_tower variables (𝕂 𝕂' 𝔸 : Type*) [field 𝕂] [field 𝕂'] [ring 𝔸] [algebra 𝕂 𝔸] [algebra 𝕂' 𝔸] [topological_space 𝔸] [topological_ring 𝔸] /-- If a normed ring `𝔸` is a normed algebra over two fields, then they define the same `exp_series` on `𝔸`. -/ lemma exp_series_eq_exp_series (n : ℕ) (x : 𝔸) : (exp_series 𝕂 𝔸 n (λ _, x)) = (exp_series 𝕂' 𝔸 n (λ _, x)) := by rw [exp_series_apply_eq, exp_series_apply_eq, inv_nat_cast_smul_eq 𝕂 𝕂'] /-- If a normed ring `𝔸` is a normed algebra over two fields, then they define the same exponential function on `𝔸`. -/ lemma exp_eq_exp : (exp 𝕂 : 𝔸 → 𝔸) = exp 𝕂' := begin ext, rw [exp, exp], refine tsum_congr (λ n, _), rw exp_series_eq_exp_series 𝕂 𝕂' 𝔸 n x end lemma exp_ℝ_ℂ_eq_exp_ℂ_ℂ : (exp ℝ : ℂ → ℂ) = exp ℂ := exp_eq_exp ℝ ℂ ℂ /-- A version of `complex.of_real_exp` for `exp` instead of `complex.exp` -/ @[simp, norm_cast] lemma of_real_exp_ℝ_ℝ (r : ℝ) : ↑(exp ℝ r) = exp ℂ (r : ℂ) := (map_exp ℝ (algebra_map ℝ ℂ) (continuous_algebra_map _ _) r).trans (congr_fun exp_ℝ_ℂ_eq_exp_ℂ_ℂ _) end scalar_tower
3e76d50ba3d0c44c19b446b8728437639b64b382
9e90bb7eb4d1bde1805f9eb6187c333fdf09588a
/src/stump/setup_properties.lean
43a71ff7fe564ad941b80ec6a60eb957d992e306
[ "Apache-2.0" ]
permissive
alexjbest/stump-learnable
6311d0c3a1a1a0e65ce83edcbb3b4b7cecabb851
f8fd812fc646d2ece312ff6ffc2a19848ac76032
refs/heads/master
1,659,486,805,691
1,590,454,024,000
1,590,454,024,000
266,173,720
0
0
Apache-2.0
1,590,169,884,000
1,590,169,883,000
null
UTF-8
Lean
false
false
3,580
lean
/- Copyright © 2019, Oracle and/or its affiliates. All rights reserved. -/ import .setup_definition import ..lib.util open set open measure_theory open probability_measure local attribute [instance] classical.prop_decidable namespace stump variables (μ: probability_measure ℍ) (target: ℍ) lemma label_correct: ∀ x, (label target x).snd = tt ↔ x ≤ target := begin intros, split; intro, { unfold label at a, simp at a, unfold rle at a, tidy, }, { unfold label, simp, unfold rle, tidy, } end lemma error_interval_1: ∀ h, h ≤ target → error μ target h = μ (Ioc h target) := begin intros, unfold error, have SETEQ: error_set h target = Ioc h target, { unfold error_set, unfold Ioc, unfold label, unfold rle, rw ext_iff, intro, simp at *, split; intro, { rw not_eq_prop at a_1, cases a_1; simp at a_1; cases a_1, { finish, }, { split, { exact lt_of_le_of_lt a a_1_right }, { transitivity h; assumption, }, }, }, { cases a_1, finish, }, }, exact congr_arg μ SETEQ, end lemma error_interval_2: ∀ h, target < h → error μ target h = μ (Ioc target h) := begin intros, unfold error, have SETEQ: error_set h target = Ioc target h, { unfold error_set, unfold Ioc, unfold label, unfold rle, rw ext_iff, intro, simp at *, split; intro, { rw not_eq_prop at a_1, cases a_1; simp at a_1; cases a_1, { split, { by_contradiction, have FOO: target < x, { transitivity h; try {assumption}, }, contradiction, }, { transitivity target; try {assumption}, have FOO: target < h, { exact mem_Ioi.mp a, }, exact le_of_lt a, }, }, { split, { exact mem_Ioi.mp a_1_right, }, { assumption, }, }, }, { cases a_1, finish, }, }, exact congr_arg μ SETEQ, end lemma error_mono: ∀ c₁, ∀ c₂, c₁ ≤ c₂ → c₂ ≤ target → error μ target c₂ ≤ error μ target c₁ := begin intros, rw error_interval_1, rw error_interval_1, apply probability_measure.prob_mono, exact Ioc_subset_Ioc_left a, transitivity c₂; assumption, assumption, end lemma error_mono_interval: ∀ c₁, ∀ c₂, c₁ ≤ c₂ → c₂ ≤ target → μ (Ioc c₂ target) ≤ μ (Ioc c₁ target) := begin intros, rw ← error_interval_1; try {assumption}, rw ← error_interval_1, apply error_mono; try {assumption}, transitivity c₂; try {assumption}, end lemma error_max: error μ target 0 = μ (Ioc 0 target) := begin apply error_interval_1, tidy, end end stump
8a5d1219e4462445c2813b33d5dbbbf72b803699
6b7c9c6393bac7cb1c64582a1c62597e24f5bb80
/src/tactic/gptf/gptf.lean
0e367dcd531ba836f5db67f45d3e0aa5e6a678cb
[ "Apache-2.0" ]
permissive
alreadydone/lean-gptf
56a7d9cbd9400af72fb143d60c8774b8cfbc09cb
b4ab1eb2da0178f3dcdc49771d9fed6b50e35d98
refs/heads/master
1,679,371,993,063
1,614,479,778,000
1,614,479,778,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,396
lean
import system.io import tactic import tactic.gptf.backends.openai /-! A tactic that will ring up GPT to ask for help solving a goal. Remember to set the `OPEN_AI_KEY` environment variable. Eg: ```sh # ~/.zshenv export OPEN_AI_KEY="<PUT YOUR SECRET KEY IN HERE>" ``` you may need to relogin to update. `n` is the number of iterations for the greedy search. `temperature` is a float between 0 and 1, and controls how deterministic the predictions are. -/ /- set to `some $KEY` if you don't want to mess with environment variables WARNING: do _not_ leave your key in committed source code -/ private meta def OPENAI_API_KEY : option string := none meta def try_lookup_key (env : environment) : tactic string := ↑OPENAI_API_KEY meta def get_openai_api_key : tactic string := do { env ← tactic.get_env, (try_lookup_key env <|> (tactic.unsafe_run_io $ io.env.get "OPENAI_API_KEY") >>= option.to_monad) <|> tactic.fail "[get_openai_api_key] ERROR: can't find an OpenAI API key" } section gptf namespace tactic namespace interactive setup_tactic_parser open openai meta structure GPTSuggestConfig : Type := (n : ℕ := 32) (temp : native.float := 1.0) (silent := ff) (engine_id : string := "formal-lean-wm-to-tt-m1-m2-v4-c4") (api_key : option string := none) (prompt_token := "PROOFSTEP") (pfx := "") (postprocess : option (string → string) := none) meta def gptf_core (cfg : GPTSuggestConfig := {}) : tactic (list string × list string) := do { tactic.success_if_fail done *> do { let req := { n := cfg.n, temperature := cfg.temp, prompt_token := cfg.prompt_token, prompt_prefix := cfg.pfx, replace_prefix := cfg.postprocess, .. default_partial_req }, api_key ← (cfg.api_key <|> get_openai_api_key), gptf_proof_search_step cfg.engine_id api_key req } } meta def gptf (cfg : GPTSuggestConfig := {}) : tactic unit := do { ⟨successes, predictions⟩ ← gptf_core cfg, if (successes.length > 0) then do { tactic.trace "\nSuccesses:\n----------", successes.mmap' tactic.trythis } else do { tactic.trace "no predictions succeeded" }, when (predictions.length > 0) $ tactic.trace "\nAll predictions: \n----------------" *> predictions.mmap' tactic.trythis } meta def neuro_eblast : tactic unit := gptf { pfx := "rw [", postprocess := λ x, "{[smt] eblast_using [" ++ (x) ++ "}" } end interactive end tactic end gptf
bc0b8b318a6d264ce469c892f424cec9c3c4a9e3
3bdd27ffdff3ffa22d4bb010eba695afcc96bc4a
/src/combinatorics/simplicial_complex/to_move/convex.lean
2854c85763d91aa56e3d30c29c174001c079ab10
[]
no_license
mmasdeu/brouwerfixedpoint
684d712c982c6a8b258b4e2c6b2eab923f2f1289
548270f79ecf12d7e20a256806ccb9fcf57b87e2
refs/heads/main
1,690,539,793,996
1,631,801,831,000
1,631,801,831,000
368,139,809
4
3
null
1,624,453,250,000
1,621,246,034,000
Lean
UTF-8
Lean
false
false
1,700
lean
/- Copyright (c) 2021 Yaël Dillies, Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies, Bhavik Mehta -/ import analysis.convex.topology import topology.basic import order.directed variables {E : Type*} [add_comm_group E] [module ℝ E] {s X Y : set E} open set --will be proven from the stuff about closure operators lemma convex_hull_convex_hull_union : convex_hull (convex_hull X ∪ Y) = convex_hull (X ∪ Y) := subset.antisymm (convex_hull_min (union_subset (convex_hull_mono (subset_union_left X Y)) (subset.trans (subset_convex_hull Y) (convex_hull_mono (subset_union_right X Y)))) (convex_convex_hull _)) (convex_hull_mono (union_subset_union_left _ (subset_convex_hull _))) --will be proven from the stuff about closure operators lemma convex_hull_self_union_convex_hull : convex_hull (X ∪ convex_hull Y) = convex_hull (X ∪ Y) := begin rw [union_comm, union_comm X Y], exact convex_hull_convex_hull_union, end lemma eq_left_or_right_or_mem_open_segment_of_mem_segment {x y z : E} (hz : z ∈ segment x y) : z = x ∨ z = y ∨ z ∈ open_segment x y := begin obtain ⟨a, b, ha, hb, hab, hz⟩ := hz, by_cases ha' : a = 0, swap, by_cases hb' : b = 0, swap, { right, right, exact ⟨a, b, ha.lt_of_ne (ne.symm ha'), hb.lt_of_ne (ne.symm hb'), hab, hz⟩ }, all_goals { simp only [*, add_zero, not_not, one_smul, zero_smul, zero_add, rfl] at *}, { left, refl }, right, left, refl, end lemma convex_hull_pair {a b : E} : convex_hull {a, b} = (segment a b) := sorry --TODO: Generalise to LCTVS variables [normed_group E] [normed_space ℝ E] {x : E} {A B : set E}
ac1e9cbf242f1d7a615b84d628e56aaf6164f883
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/deprecated/subring.lean
735a6901b603b137f7139dc1900e69b8a29b41b7
[]
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
5,516
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.deprecated.subgroup import Mathlib.deprecated.group import Mathlib.PostPort universes u l v u_1 namespace Mathlib /-- `S` is a subring: a set containing 1 and closed under multiplication, addition and and additive inverse. -/ class is_subring {R : Type u} [ring R] (S : set R) extends is_add_subgroup S, is_submonoid S where /-- The ring structure on a subring coerced to a type. -/ def subset.ring {R : Type u} [ring R] {S : set R} [is_subring S] : ring ↥S := ring.mk add_comm_group.add sorry add_comm_group.zero sorry sorry add_comm_group.neg add_comm_group.sub sorry sorry monoid.mul sorry monoid.one sorry sorry sorry sorry /-- The ring structure on a subring coerced to a type. -/ def subtype.ring {R : Type u} [ring R] {S : set R} [is_subring S] : ring (Subtype S) := subset.ring namespace ring_hom protected instance is_subring_preimage {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : set S) [is_subring s] : is_subring (⇑f ⁻¹' s) := is_subring.mk protected instance is_subring_image {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : set R) [is_subring s] : is_subring (⇑f '' s) := is_subring.mk protected instance is_subring_set_range {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) : is_subring (set.range ⇑f) := is_subring.mk end ring_hom /-- Restrict the codomain of a ring homomorphism to a subring that includes the range. -/ def ring_hom.cod_restrict {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : set S) [is_subring s] (h : ∀ (x : R), coe_fn f x ∈ s) : R →+* ↥s := ring_hom.mk (fun (x : R) => { val := coe_fn f x, property := h x }) sorry sorry sorry sorry /-- Coersion `S → R` as a ring homormorphism-/ def is_subring.subtype {R : Type u} [ring R] (S : set R) [is_subring S] : ↥S →+* R := ring_hom.mk coe sorry sorry sorry sorry @[simp] theorem is_subring.coe_subtype {R : Type u} [ring R] {S : set R} [is_subring S] : ⇑(is_subring.subtype S) = coe := rfl /-- The commutative ring structure on a subring coerced to a type. -/ def subset.comm_ring {cR : Type u} [comm_ring cR] {S : set cR} [is_subring S] : comm_ring ↥S := comm_ring.mk ring.add sorry ring.zero sorry sorry ring.neg ring.sub sorry sorry ring.mul sorry ring.one sorry sorry sorry sorry sorry /-- The commutative ring structure on a subring coerced to a type. -/ def subtype.comm_ring {cR : Type u} [comm_ring cR] {S : set cR} [is_subring S] : comm_ring (Subtype S) := subset.comm_ring /-- The integral domain structure on a subring of an integral domain coerced to a type. -/ def subring.domain {D : Type u_1} [integral_domain D] (S : set D) [is_subring S] : integral_domain ↥S := integral_domain.mk comm_ring.add sorry comm_ring.zero sorry sorry comm_ring.neg comm_ring.sub sorry sorry comm_ring.mul sorry comm_ring.one sorry sorry sorry sorry sorry sorry sorry protected instance is_subring.inter {R : Type u} [ring R] (S₁ : set R) (S₂ : set R) [is_subring S₁] [is_subring S₂] : is_subring (S₁ ∩ S₂) := is_subring.mk protected instance is_subring.Inter {R : Type u} [ring R] {ι : Sort u_1} (S : ι → set R) [h : ∀ (y : ι), is_subring (S y)] : is_subring (set.Inter S) := is_subring.mk theorem is_subring_Union_of_directed {R : Type u} [ring R] {ι : Type u_1} [hι : Nonempty ι] (s : ι → set R) [∀ (i : ι), is_subring (s i)] (directed : ∀ (i j : ι), ∃ (k : ι), s i ⊆ s k ∧ s j ⊆ s k) : is_subring (set.Union fun (i : ι) => s i) := is_subring.mk namespace ring def closure {R : Type u} [ring R] (s : set R) : set R := add_group.closure (monoid.closure s) theorem exists_list_of_mem_closure {R : Type u} [ring R] {s : set R} {a : R} (h : a ∈ closure s) : ∃ (L : List (List R)), (∀ (l : List R), l ∈ L → ∀ (x : R), x ∈ l → x ∈ s ∨ x = -1) ∧ list.sum (list.map list.prod L) = a := sorry protected theorem in_closure.rec_on {R : Type u} [ring R] {s : set R} {C : R → Prop} {x : R} (hx : x ∈ closure s) (h1 : C 1) (hneg1 : C (-1)) (hs : ∀ (z : R), z ∈ s → ∀ (n : R), C n → C (z * n)) (ha : ∀ {x y : R}, C x → C y → C (x + y)) : C x := sorry protected instance closure.is_subring {R : Type u} [ring R] {s : set R} : is_subring (closure s) := is_subring.mk theorem mem_closure {R : Type u} [ring R] {s : set R} {a : R} : a ∈ s → a ∈ closure s := add_group.mem_closure ∘ monoid.subset_closure theorem subset_closure {R : Type u} [ring R] {s : set R} : s ⊆ closure s := fun (_x : R) => mem_closure theorem closure_subset {R : Type u} [ring R] {s : set R} {t : set R} [is_subring t] : s ⊆ t → closure s ⊆ t := add_group.closure_subset ∘ monoid.closure_subset theorem closure_subset_iff {R : Type u} [ring R] (s : set R) (t : set R) [is_subring t] : closure s ⊆ t ↔ s ⊆ t := iff.trans (add_group.closure_subset_iff (monoid.closure s) t) { mp := set.subset.trans monoid.subset_closure, mpr := monoid.closure_subset } theorem closure_mono {R : Type u} [ring R] {s : set R} {t : set R} (H : s ⊆ t) : closure s ⊆ closure t := closure_subset (set.subset.trans H subset_closure) theorem image_closure {R : Type u} [ring R] {S : Type u_1} [ring S] (f : R →+* S) (s : set R) : ⇑f '' closure s = closure (⇑f '' s) := sorry
c7484698a6256c3816a7cd6f9daf9c7ec3e8c556
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/real/pi/bounds.lean
e6e665dd2687d6e562df88e3b41ec6b6d4f02734
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
7,787
lean
/- Copyright (c) 2019 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Mario Carneiro -/ import analysis.special_functions.trigonometric.bounds /-! # Pi This file contains lemmas which establish bounds on `real.pi`. Notably, these include `pi_gt_sqrt_two_add_series` and `pi_lt_sqrt_two_add_series`, which bound `π` using series; numerical bounds on `π` such as `pi_gt_314`and `pi_lt_315` (more precise versions are given, too). See also `data.real.pi.leibniz` and `data.real.pi.wallis` for infinite formulas for `π`. -/ open_locale real namespace real lemma pi_gt_sqrt_two_add_series (n : ℕ) : 2 ^ (n+1) * sqrt (2 - sqrt_two_add_series 0 n) < π := begin have : sqrt (2 - sqrt_two_add_series 0 n) / 2 * 2 ^ (n+2) < π, { rw [← lt_div_iff, ←sin_pi_over_two_pow_succ], apply sin_lt, apply div_pos pi_pos, all_goals { apply pow_pos, norm_num } }, apply lt_of_le_of_lt (le_of_eq _) this, rw [pow_succ _ (n+1), ←mul_assoc, div_mul_cancel, mul_comm], norm_num end lemma pi_lt_sqrt_two_add_series (n : ℕ) : π < 2 ^ (n+1) * sqrt (2 - sqrt_two_add_series 0 n) + 1 / 4 ^ n := begin have : π < (sqrt (2 - sqrt_two_add_series 0 n) / 2 + 1 / (2 ^ n) ^ 3 / 4) * 2 ^ (n+2), { rw [← div_lt_iff, ← sin_pi_over_two_pow_succ], refine lt_of_lt_of_le (lt_add_of_sub_right_lt (sin_gt_sub_cube _ _)) _, { apply div_pos pi_pos, apply pow_pos, norm_num }, { rw div_le_iff', { refine le_trans pi_le_four _, simp only [show ((4 : ℝ) = 2 ^ 2), by norm_num, mul_one], apply pow_le_pow, norm_num, apply le_add_of_nonneg_left, apply nat.zero_le }, { apply pow_pos, norm_num } }, apply add_le_add_left, rw div_le_div_right, rw [le_div_iff, ←mul_pow], refine le_trans _ (le_of_eq (one_pow 3)), apply pow_le_pow_of_le_left, { apply le_of_lt, apply mul_pos, apply div_pos pi_pos, apply pow_pos, norm_num, apply pow_pos, norm_num }, rw ← le_div_iff, refine le_trans ((div_le_div_right _).mpr pi_le_four) _, apply pow_pos, norm_num, rw [pow_succ, pow_succ, ←mul_assoc, ←div_div], convert le_rfl, all_goals { repeat {apply pow_pos}, norm_num }}, apply lt_of_lt_of_le this (le_of_eq _), rw [add_mul], congr' 1, { rw [pow_succ _ (n+1), ←mul_assoc, div_mul_cancel, mul_comm], norm_num }, rw [pow_succ, ←pow_mul, mul_comm n 2, pow_mul, show (2 : ℝ) ^ 2 = 4, by norm_num, pow_succ, pow_succ, ←mul_assoc (2 : ℝ), show (2 : ℝ) * 2 = 4, by norm_num, ←mul_assoc, div_mul_cancel, mul_comm ((2 : ℝ) ^ n), ←div_div, div_mul_cancel], apply pow_ne_zero, norm_num, norm_num end /-- From an upper bound on `sqrt_two_add_series 0 n = 2 cos (π / 2 ^ (n+1))` of the form `sqrt_two_add_series 0 n ≤ 2 - (a / 2 ^ (n + 1)) ^ 2)`, one can deduce the lower bound `a < π` thanks to basic trigonometric inequalities as expressed in `pi_gt_sqrt_two_add_series`. -/ theorem pi_lower_bound_start (n : ℕ) {a} (h : sqrt_two_add_series ((0:ℕ) / (1:ℕ)) n ≤ 2 - (a / 2 ^ (n + 1)) ^ 2) : a < π := begin refine lt_of_le_of_lt _ (pi_gt_sqrt_two_add_series n), rw [mul_comm], refine (div_le_iff (pow_pos (by norm_num) _ : (0 : ℝ) < _)).mp (le_sqrt_of_sq_le _), rwa [le_sub_comm, show (0:ℝ) = (0:ℕ)/(1:ℕ), by rw [nat.cast_zero, zero_div]], end lemma sqrt_two_add_series_step_up (c d : ℕ) {a b n : ℕ} {z : ℝ} (hz : sqrt_two_add_series (c/d) n ≤ z) (hb : 0 < b) (hd : 0 < d) (h : (2 * b + a) * d ^ 2 ≤ c ^ 2 * b) : sqrt_two_add_series (a/b) (n+1) ≤ z := begin refine le_trans _ hz, rw sqrt_two_add_series_succ, apply sqrt_two_add_series_monotone_left, have hb' : 0 < (b:ℝ) := nat.cast_pos.2 hb, have hd' : 0 < (d:ℝ) := nat.cast_pos.2 hd, rw [sqrt_le_left (div_nonneg c.cast_nonneg d.cast_nonneg), div_pow, add_div_eq_mul_add_div _ _ (ne_of_gt hb'), div_le_div_iff hb' (pow_pos hd' _)], exact_mod_cast h end /-- Create a proof of `a < π` for a fixed rational number `a`, given a witness, which is a sequence of rational numbers `sqrt 2 < r 1 < r 2 < ... < r n < 2` satisfying the property that `sqrt (2 + r i) ≤ r(i+1)`, where `r 0 = 0` and `sqrt (2 - r n) ≥ a/2^(n+1)`. -/ meta def pi_lower_bound (l : list ℚ) : tactic unit := do let n := l.length, tactic.apply `(@pi_lower_bound_start %%(reflect n)), l.mmap' (λ r, do let a := r.num.to_nat, let b := r.denom, (() <$ tactic.apply `(@sqrt_two_add_series_step_up %%(reflect a) %%(reflect b))); [tactic.skip, `[norm_num1], `[norm_num1], `[norm_num1]]), `[simp only [sqrt_two_add_series, nat.cast_bit0, nat.cast_bit1, nat.cast_one, nat.cast_zero]], `[norm_num1] /-- From a lower bound on `sqrt_two_add_series 0 n = 2 cos (π / 2 ^ (n+1))` of the form `2 - ((a - 1 / 4 ^ n) / 2 ^ (n + 1)) ^ 2 ≤ sqrt_two_add_series 0 n`, one can deduce the upper bound `π < a` thanks to basic trigonometric formulas as expressed in `pi_lt_sqrt_two_add_series`. -/ theorem pi_upper_bound_start (n : ℕ) {a} (h : 2 - ((a - 1 / 4 ^ n) / 2 ^ (n + 1)) ^ 2 ≤ sqrt_two_add_series ((0:ℕ) / (1:ℕ)) n) (h₂ : 1 / 4 ^ n ≤ a) : π < a := begin refine lt_of_lt_of_le (pi_lt_sqrt_two_add_series n) _, rw [← le_sub_iff_add_le, ← le_div_iff', sqrt_le_left, sub_le_comm], { rwa [nat.cast_zero, zero_div] at h }, { exact div_nonneg (sub_nonneg.2 h₂) (pow_nonneg (le_of_lt zero_lt_two) _) }, { exact pow_pos zero_lt_two _ } end lemma sqrt_two_add_series_step_down (a b : ℕ) {c d n : ℕ} {z : ℝ} (hz : z ≤ sqrt_two_add_series (a/b) n) (hb : 0 < b) (hd : 0 < d) (h : a ^ 2 * d ≤ (2 * d + c) * b ^ 2) : z ≤ sqrt_two_add_series (c/d) (n+1) := begin apply le_trans hz, rw sqrt_two_add_series_succ, apply sqrt_two_add_series_monotone_left, apply le_sqrt_of_sq_le, have hb' : 0 < (b:ℝ) := nat.cast_pos.2 hb, have hd' : 0 < (d:ℝ) := nat.cast_pos.2 hd, rw [div_pow, add_div_eq_mul_add_div _ _ (ne_of_gt hd'), div_le_div_iff (pow_pos hb' _) hd'], exact_mod_cast h end /-- Create a proof of `π < a` for a fixed rational number `a`, given a witness, which is a sequence of rational numbers `sqrt 2 < r 1 < r 2 < ... < r n < 2` satisfying the property that `sqrt (2 + r i) ≥ r(i+1)`, where `r 0 = 0` and `sqrt (2 - r n) ≥ (a - 1/4^n) / 2^(n+1)`. -/ meta def pi_upper_bound (l : list ℚ) : tactic unit := do let n := l.length, (() <$ tactic.apply `(@pi_upper_bound_start %%(reflect n))); [pure (), `[norm_num1]], l.mmap' (λ r, do let a := r.num.to_nat, let b := r.denom, (() <$ tactic.apply `(@sqrt_two_add_series_step_down %%(reflect a) %%(reflect b))); [pure (), `[norm_num1], `[norm_num1], `[norm_num1]]), `[simp only [sqrt_two_add_series, nat.cast_bit0, nat.cast_bit1, nat.cast_one, nat.cast_zero]], `[norm_num] lemma pi_gt_three : 3 < π := by pi_lower_bound [23/16] lemma pi_gt_314 : 3.14 < π := by pi_lower_bound [99/70, 874/473, 1940/989, 1447/727] lemma pi_lt_315 : π < 3.15 := by pi_upper_bound [140/99, 279/151, 51/26, 412/207] lemma pi_gt_31415 : 3.1415 < π := by pi_lower_bound [ 11482/8119, 5401/2923, 2348/1197, 11367/5711, 25705/12868, 23235/11621] lemma pi_lt_31416 : π < 3.1416 := by pi_upper_bound [ 4756/3363, 101211/54775, 505534/257719, 83289/41846, 411278/205887, 438142/219137, 451504/225769, 265603/132804, 849938/424971] lemma pi_gt_3141592 : 3.141592 < π := by pi_lower_bound [ 11482/8119, 7792/4217, 54055/27557, 949247/476920, 3310126/1657059, 2635492/1318143, 1580265/790192, 1221775/610899, 3612247/1806132, 849943/424972] lemma pi_lt_3141593 : π < 3.141593 := by pi_upper_bound [ 27720/19601, 56935/30813, 49359/25163, 258754/130003, 113599/56868, 1101994/551163, 8671537/4336095, 3877807/1938940, 52483813/26242030, 56946167/28473117, 23798415/11899211] end real
d55bee8f6a12f228907ade53607d8479113e32be
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/run/structInst3.lean
b80d12edc244d2382d14a24ced3a5e44f29665c8
[ "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
598
lean
universe u namespace Ex1 structure A (α : Type u) := (x : α) (f : α → α := λ x => x) structure B (α : Type u) extends A α := (y : α := f (f x)) (g : α → α → α := λ x y => f x) structure C (α : Type u) extends B α := (z : α := g x y) (x := f z) end Ex1 open Ex1 def c1 : C Nat := { x := 1 } #check { c1 with z := 2 } #check { c1 with z := 2 } theorem ex1 : { c1 with z := 2 }.z = 2 := rfl #check ex1 theorem ex2 : { c1 with z := 2 }.x = c1.x := rfl #check ex2 def c2 : C (Nat × Nat) := { z := (1, 1) } #check { c2 with x.fst := 2 } #check { c2 with x.1 := 3 }
342f90ae391d62360b4820a82e29cea067689ceb
30b012bb72d640ec30c8fdd4c45fdfa67beb012c
/number_theory/pell.lean
e1ec32e9337a819b3c2609fe17514b023965ea08
[ "Apache-2.0" ]
permissive
kckennylau/mathlib
21fb810b701b10d6606d9002a4004f7672262e83
47b3477e20ffb5a06588dd3abb01fe0fe3205646
refs/heads/master
1,634,976,409,281
1,542,042,832,000
1,542,319,733,000
109,560,458
0
0
Apache-2.0
1,542,369,208,000
1,509,867,494,000
Lean
UTF-8
Lean
false
false
62,357
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.int.basic data.nat.prime data.nat.modeq /-- The ring of integers adjoined with a square root of `d`. These have the form `a + b √d` where `a b : ℤ`. The components are called `re` and `im` by analogy to the negative `d` case, but of course both parts are real here since `d` is nonnegative. -/ structure zsqrtd (d : ℕ) := mk {} :: (re : ℤ) (im : ℤ) prefix `ℤ√`:100 := zsqrtd namespace zsqrtd section parameters {d : ℕ} instance : decidable_eq ℤ√d := by tactic.mk_dec_eq_instance theorem ext : ∀ {z w : ℤ√d}, z = w ↔ z.re = w.re ∧ z.im = w.im | ⟨x, y⟩ ⟨x', y'⟩ := ⟨λ h, by injection h; split; assumption, λ ⟨h₁, h₂⟩, by congr; assumption⟩ /-- Convert an integer to a `ℤ√d` -/ def of_int (n : ℤ) : ℤ√d := ⟨n, 0⟩ @[simp] theorem of_int_re (n : ℤ) : (of_int n).re = n := rfl @[simp] theorem of_int_im (n : ℤ) : (of_int n).im = 0 := rfl /-- The zero of the ring -/ def zero : ℤ√d := of_int 0 instance : has_zero ℤ√d := ⟨zsqrtd.zero⟩ @[simp] theorem zero_re : (0 : ℤ√d).re = 0 := rfl @[simp] theorem zero_im : (0 : ℤ√d).im = 0 := rfl /-- The one of the ring -/ def one : ℤ√d := of_int 1 instance : has_one ℤ√d := ⟨zsqrtd.one⟩ @[simp] theorem one_re : (1 : ℤ√d).re = 1 := rfl @[simp] theorem one_im : (1 : ℤ√d).im = 0 := rfl /-- The representative of `√d` in the ring -/ def sqrtd : ℤ√d := ⟨0, 1⟩ @[simp] theorem sqrtd_re : (sqrtd : ℤ√d).re = 0 := rfl @[simp] theorem sqrtd_im : (sqrtd : ℤ√d).im = 1 := rfl /-- Addition of elements of `ℤ√d` -/ def add : ℤ√d → ℤ√d → ℤ√d | ⟨x, y⟩ ⟨x', y'⟩ := ⟨x + x', y + y'⟩ instance : has_add ℤ√d := ⟨zsqrtd.add⟩ @[simp] theorem add_def (x y x' y' : ℤ) : (⟨x, y⟩ + ⟨x', y'⟩ : ℤ√d) = ⟨x + x', y + y'⟩ := rfl @[simp] theorem add_re : ∀ z w : ℤ√d, (z + w).re = z.re + w.re | ⟨x, y⟩ ⟨x', y'⟩ := rfl @[simp] theorem add_im : ∀ z w : ℤ√d, (z + w).im = z.im + w.im | ⟨x, y⟩ ⟨x', y'⟩ := rfl @[simp] theorem bit0_re (z) : (bit0 z : ℤ√d).re = bit0 z.re := add_re _ _ @[simp] theorem bit0_im (z) : (bit0 z : ℤ√d).im = bit0 z.im := add_im _ _ @[simp] theorem bit1_re (z) : (bit1 z : ℤ√d).re = bit1 z.re := by simp [bit1] @[simp] theorem bit1_im (z) : (bit1 z : ℤ√d).im = bit0 z.im := by simp [bit1] /-- Negation in `ℤ√d` -/ def neg : ℤ√d → ℤ√d | ⟨x, y⟩ := ⟨-x, -y⟩ instance : has_neg ℤ√d := ⟨zsqrtd.neg⟩ @[simp] theorem neg_re : ∀ z : ℤ√d, (-z).re = -z.re | ⟨x, y⟩ := rfl @[simp] theorem neg_im : ∀ z : ℤ√d, (-z).im = -z.im | ⟨x, y⟩ := rfl /-- Conjugation in `ℤ√d`. The conjugate of `a + b √d` is `a - b √d`. -/ def conj : ℤ√d → ℤ√d | ⟨x, y⟩ := ⟨x, -y⟩ @[simp] theorem conj_re : ∀ z : ℤ√d, (conj z).re = z.re | ⟨x, y⟩ := rfl @[simp] theorem conj_im : ∀ z : ℤ√d, (conj z).im = -z.im | ⟨x, y⟩ := rfl /-- Multiplication in `ℤ√d` -/ def mul : ℤ√d → ℤ√d → ℤ√d | ⟨x, y⟩ ⟨x', y'⟩ := ⟨x * x' + d * y * y', x * y' + y * x'⟩ instance : has_mul ℤ√d := ⟨zsqrtd.mul⟩ @[simp] theorem mul_re : ∀ z w : ℤ√d, (z * w).re = z.re * w.re + d * z.im * w.im | ⟨x, y⟩ ⟨x', y'⟩ := rfl @[simp] theorem mul_im : ∀ z w : ℤ√d, (z * w).im = z.re * w.im + z.im * w.re | ⟨x, y⟩ ⟨x', y'⟩ := rfl instance : comm_ring ℤ√d := by refine { add := (+), zero := 0, neg := has_neg.neg, mul := (*), one := 1, ..}; { intros, simp [ext, add_mul, mul_add, mul_comm, mul_left_comm] } instance : add_comm_monoid ℤ√d := by apply_instance instance : add_monoid ℤ√d := by apply_instance instance : monoid ℤ√d := by apply_instance instance : comm_monoid ℤ√d := by apply_instance instance : comm_semigroup ℤ√d := by apply_instance instance : semigroup ℤ√d := by apply_instance instance : add_comm_semigroup ℤ√d := by apply_instance instance : add_semigroup ℤ√d := by apply_instance instance : comm_semiring ℤ√d := by apply_instance instance : semiring ℤ√d := by apply_instance instance : ring ℤ√d := by apply_instance instance : distrib ℤ√d := by apply_instance instance : zero_ne_one_class ℤ√d := { zero := 0, one := 1, zero_ne_one := dec_trivial } @[simp] theorem coe_nat_re (n : ℕ) : (n : ℤ√d).re = n := by induction n; simp * @[simp] theorem coe_nat_im (n : ℕ) : (n : ℤ√d).im = 0 := by induction n; simp * theorem coe_nat_val (n : ℕ) : (n : ℤ√d) = ⟨n, 0⟩ := by simp [ext] @[simp] theorem coe_int_re (n : ℤ) : (n : ℤ√d).re = n := by cases n; simp [*, int.of_nat_eq_coe, int.neg_succ_of_nat_eq] @[simp] theorem coe_int_im (n : ℤ) : (n : ℤ√d).im = 0 := by cases n; simp * theorem coe_int_val (n : ℤ) : (n : ℤ√d) = ⟨n, 0⟩ := by simp [ext] @[simp] theorem of_int_eq_coe (n : ℤ) : (of_int n : ℤ√d) = n := by simp [ext] @[simp] theorem smul_val (n x y : ℤ) : (n : ℤ√d) * ⟨x, y⟩ = ⟨n * x, n * y⟩ := by simp [ext] @[simp] theorem muld_val (x y : ℤ) : sqrtd * ⟨x, y⟩ = ⟨d * y, x⟩ := by simp [ext] @[simp] theorem smuld_val (n x y : ℤ) : sqrtd * (n : ℤ√d) * ⟨x, y⟩ = ⟨d * n * y, n * x⟩ := by simp [ext] theorem decompose {x y : ℤ} : (⟨x, y⟩ : ℤ√d) = x + sqrtd * y := by simp [ext] theorem mul_conj {x y : ℤ} : (⟨x, y⟩ * conj ⟨x, y⟩ : ℤ√d) = x * x - d * y * y := by simp [ext, mul_comm] theorem conj_mul : Π {a b : ℤ√d}, conj (a * b) = conj a * conj b := by simp [ext] protected lemma coe_int_add (m n : ℤ) : (↑(m + n) : ℤ√d) = ↑m + ↑n := by simp [ext] protected lemma coe_int_sub (m n : ℤ) : (↑(m - n) : ℤ√d) = ↑m - ↑n := by simp [ext] protected lemma coe_int_mul (m n : ℤ) : (↑(m * n) : ℤ√d) = ↑m * ↑n := by simp [ext] protected lemma coe_int_inj {m n : ℤ} (h : (↑m : ℤ√d) = ↑n) : m = n := by simpa using congr_arg re h /-- Read `sq_le a c b d` as `a √c ≤ b √d` -/ def sq_le (a c b d : ℕ) : Prop := c*a*a ≤ d*b*b theorem sq_le_of_le {c d x y z w : ℕ} (xz : z ≤ x) (yw : y ≤ w) (xy : sq_le x c y d) : sq_le z c w d := le_trans (mul_le_mul (nat.mul_le_mul_left _ xz) xz (nat.zero_le _) (nat.zero_le _)) $ le_trans xy (mul_le_mul (nat.mul_le_mul_left _ yw) yw (nat.zero_le _) (nat.zero_le _)) theorem sq_le_add_mixed {c d x y z w : ℕ} (xy : sq_le x c y d) (zw : sq_le z c w d) : c * (x * z) ≤ d * (y * w) := nat.mul_self_le_mul_self_iff.2 $ by simpa [mul_comm, mul_left_comm] using mul_le_mul xy zw (nat.zero_le _) (nat.zero_le _) theorem sq_le_add {c d x y z w : ℕ} (xy : sq_le x c y d) (zw : sq_le z c w d) : sq_le (x + z) c (y + w) d := begin have xz := sq_le_add_mixed xy zw, simp [sq_le, mul_assoc] at xy zw, simp [sq_le, mul_add, mul_comm, mul_left_comm, add_le_add, *] end theorem sq_le_cancel {c d x y z w : ℕ} (zw : sq_le y d x c) (h : sq_le (x + z) c (y + w) d) : sq_le z c w d := begin apply le_of_not_gt, intro l, refine not_le_of_gt _ h, simp [sq_le, mul_add, mul_comm, mul_left_comm], have hm := sq_le_add_mixed zw (le_of_lt l), simp [sq_le, mul_assoc] at l zw, exact lt_of_le_of_lt (add_le_add_right zw _) (add_lt_add_left (add_lt_add_of_le_of_lt hm (add_lt_add_of_le_of_lt hm l)) _) end theorem sq_le_smul {c d x y : ℕ} (n : ℕ) (xy : sq_le x c y d) : sq_le (n * x) c (n * y) d := by simpa [sq_le, mul_left_comm, mul_assoc] using nat.mul_le_mul_left (n * n) xy theorem sq_le_mul {d x y z w : ℕ} : (sq_le x 1 y d → sq_le z 1 w d → sq_le (x * w + y * z) d (x * z + d * y * w) 1) ∧ (sq_le x 1 y d → sq_le w d z 1 → sq_le (x * z + d * y * w) 1 (x * w + y * z) d) ∧ (sq_le y d x 1 → sq_le z 1 w d → sq_le (x * z + d * y * w) 1 (x * w + y * z) d) ∧ (sq_le y d x 1 → sq_le w d z 1 → sq_le (x * w + y * z) d (x * z + d * y * w) 1) := by refine ⟨_, _, _, _⟩; { intros xy zw, have := int.mul_nonneg (sub_nonneg_of_le (int.coe_nat_le_coe_nat_of_le xy)) (sub_nonneg_of_le (int.coe_nat_le_coe_nat_of_le zw)), refine int.le_of_coe_nat_le_coe_nat (le_of_sub_nonneg _), simpa [mul_add, mul_left_comm, mul_comm] } /-- "Generalized" `nonneg`. `nonnegg c d x y` means `a √c + b √d ≥ 0`; we are interested in the case `c = 1` but this is more symmetric -/ def nonnegg (c d : ℕ) : ℤ → ℤ → Prop | (a : ℕ) (b : ℕ) := true | (a : ℕ) -[1+ b] := sq_le (b+1) c a d | -[1+ a] (b : ℕ) := sq_le (a+1) d b c | -[1+ a] -[1+ b] := false theorem nonnegg_comm {c d : ℕ} {x y : ℤ} : nonnegg c d x y = nonnegg d c y x := by induction x; induction y; refl theorem nonnegg_neg_pos {c d} : Π {a b : ℕ}, nonnegg c d (-a) b ↔ sq_le a d b c | 0 b := ⟨by simp [sq_le, nat.zero_le], λa, trivial⟩ | (a+1) b := by rw ← int.neg_succ_of_nat_coe; refl theorem nonnegg_pos_neg {c d} {a b : ℕ} : nonnegg c d a (-b) ↔ sq_le b c a d := by rw nonnegg_comm; exact nonnegg_neg_pos theorem nonnegg_cases_right {c d} {a : ℕ} : Π {b : ℤ}, (Π x : ℕ, b = -x → sq_le x c a d) → nonnegg c d a b | (b:nat) h := trivial | -[1+ b] h := h (b+1) rfl theorem nonnegg_cases_left {c d} {b : ℕ} {a : ℤ} (h : Π x : ℕ, a = -x → sq_le x d b c) : nonnegg c d a b := cast nonnegg_comm (nonnegg_cases_right h) /-- Nonnegativity of an element of `ℤ√d`. -/ def nonneg : ℤ√d → Prop | ⟨a, b⟩ := nonnegg d 1 a b protected def le (a b : ℤ√d) : Prop := nonneg (b - a) instance : has_le ℤ√d := ⟨zsqrtd.le⟩ protected def lt (a b : ℤ√d) : Prop := ¬(b ≤ a) instance : has_lt ℤ√d := ⟨zsqrtd.lt⟩ instance decidable_nonnegg (c d a b) : decidable (nonnegg c d a b) := by cases a; cases b; repeat {rw int.of_nat_eq_coe}; unfold nonnegg sq_le; apply_instance instance decidable_nonneg : Π (a : ℤ√d), decidable (nonneg a) | ⟨a, b⟩ := zsqrtd.decidable_nonnegg _ _ _ _ instance decidable_le (a b : ℤ√d) : decidable (a ≤ b) := decidable_nonneg _ theorem nonneg_cases : Π {a : ℤ√d}, nonneg a → ∃ x y : ℕ, a = ⟨x, y⟩ ∨ a = ⟨x, -y⟩ ∨ a = ⟨-x, y⟩ | ⟨(x : ℕ), (y : ℕ)⟩ h := ⟨x, y, or.inl rfl⟩ | ⟨(x : ℕ), -[1+ y]⟩ h := ⟨x, y+1, or.inr $ or.inl rfl⟩ | ⟨-[1+ x], (y : ℕ)⟩ h := ⟨x+1, y, or.inr $ or.inr rfl⟩ | ⟨-[1+ x], -[1+ y]⟩ h := false.elim h lemma nonneg_add_lem {x y z w : ℕ} (xy : nonneg ⟨x, -y⟩) (zw : nonneg ⟨-z, w⟩) : nonneg (⟨x, -y⟩ + ⟨-z, w⟩) := have nonneg ⟨int.sub_nat_nat x z, int.sub_nat_nat w y⟩, from int.sub_nat_nat_elim x z (λm n i, sq_le y d m 1 → sq_le n 1 w d → nonneg ⟨i, int.sub_nat_nat w y⟩) (λj k, int.sub_nat_nat_elim w y (λm n i, sq_le n d (k + j) 1 → sq_le k 1 m d → nonneg ⟨int.of_nat j, i⟩) (λm n xy zw, trivial) (λm n xy zw, sq_le_cancel zw xy)) (λj k, int.sub_nat_nat_elim w y (λm n i, sq_le n d k 1 → sq_le (k + j + 1) 1 m d → nonneg ⟨-[1+ j], i⟩) (λm n xy zw, sq_le_cancel xy zw) (λm n xy zw, let t := nat.le_trans zw (sq_le_of_le (nat.le_add_right n (m+1)) (le_refl _) xy) in have k + j + 1 ≤ k, from nat.mul_self_le_mul_self_iff.2 (by repeat{rw one_mul at t}; exact t), absurd this (not_le_of_gt $ nat.succ_le_succ $ nat.le_add_right _ _))) (nonnegg_pos_neg.1 xy) (nonnegg_neg_pos.1 zw), show nonneg ⟨_, _⟩, by rw [neg_add_eq_sub]; rwa [int.sub_nat_nat_eq_coe,int.sub_nat_nat_eq_coe] at this theorem nonneg_add {a b : ℤ√d} (ha : nonneg a) (hb : nonneg b) : nonneg (a + b) := begin rcases nonneg_cases ha with ⟨x, y, rfl|rfl|rfl⟩; rcases nonneg_cases hb with ⟨z, w, rfl|rfl|rfl⟩; dsimp [add, nonneg] at ha hb ⊢, { trivial }, { refine nonnegg_cases_right (λi h, sq_le_of_le _ _ (nonnegg_pos_neg.1 hb)), { exact int.coe_nat_le.1 (le_of_neg_le_neg (@int.le.intro _ _ y (by simp *))) }, { apply nat.le_add_left } }, { refine nonnegg_cases_left (λi h, sq_le_of_le _ _ (nonnegg_neg_pos.1 hb)), { exact int.coe_nat_le.1 (le_of_neg_le_neg (@int.le.intro _ _ x (by simp *))) }, { apply nat.le_add_left } }, { refine nonnegg_cases_right (λi h, sq_le_of_le _ _ (nonnegg_pos_neg.1 ha)), { exact int.coe_nat_le.1 (le_of_neg_le_neg (@int.le.intro _ _ w (by simp *))) }, { apply nat.le_add_right } }, { simpa using nonnegg_pos_neg.2 (sq_le_add (nonnegg_pos_neg.1 ha) (nonnegg_pos_neg.1 hb)) }, { exact nonneg_add_lem ha hb }, { refine nonnegg_cases_left (λi h, sq_le_of_le _ _ (nonnegg_neg_pos.1 ha)), { exact int.coe_nat_le.1 (le_of_neg_le_neg (@int.le.intro _ _ z (by simp *))) }, { apply nat.le_add_right } }, { rw [add_comm, add_comm ↑y], exact nonneg_add_lem hb ha }, { simpa using nonnegg_neg_pos.2 (sq_le_add (nonnegg_neg_pos.1 ha) (nonnegg_neg_pos.1 hb)) }, end theorem le_refl (a : ℤ√d) : a ≤ a := show nonneg (a - a), by simp protected theorem le_trans {a b c : ℤ√d} (ab : a ≤ b) (bc : b ≤ c) : a ≤ c := have nonneg (b - a + (c - b)), from nonneg_add ab bc, have nonneg (c - a + (b - b)), by simpa [-add_right_neg, add_left_comm], by simpa theorem nonneg_iff_zero_le {a : ℤ√d} : nonneg a ↔ 0 ≤ a := show _ ↔ nonneg _, by simp theorem le_of_le_le {x y z w : ℤ} (xz : x ≤ z) (yw : y ≤ w) : (⟨x, y⟩ : ℤ√d) ≤ ⟨z, w⟩ := show nonneg ⟨z - x, w - y⟩, from match z - x, w - y, int.le.dest_sub xz, int.le.dest_sub yw with ._, ._, ⟨a, rfl⟩, ⟨b, rfl⟩ := trivial end theorem le_arch (a : ℤ√d) : ∃n : ℕ, a ≤ n := let ⟨x, y, (h : a ≤ ⟨x, y⟩)⟩ := show ∃x y : ℕ, nonneg (⟨x, y⟩ + -a), from match -a with | ⟨int.of_nat x, int.of_nat y⟩ := ⟨0, 0, trivial⟩ | ⟨int.of_nat x, -[1+ y]⟩ := ⟨0, y+1, by simp [int.neg_succ_of_nat_coe]⟩ | ⟨-[1+ x], int.of_nat y⟩ := ⟨x+1, 0, by simp [int.neg_succ_of_nat_coe]⟩ | ⟨-[1+ x], -[1+ y]⟩ := ⟨x+1, y+1, by simp [int.neg_succ_of_nat_coe]⟩ end in begin refine ⟨x + d*y, zsqrtd.le_trans h _⟩, rw [← int.cast_coe_nat, ← of_int_eq_coe], change nonneg ⟨(↑x + d*y) - ↑x, 0-↑y⟩, cases y with y, { simp }, have h : ∀y, sq_le y d (d * y) 1 := λ y, by simpa [sq_le, mul_comm, mul_left_comm] using nat.mul_le_mul_right (y * y) (nat.le_mul_self d), rw [show (x:ℤ) + d * nat.succ y - x = d * nat.succ y, by simp], exact h (y+1) end protected theorem nonneg_total : Π (a : ℤ√d), nonneg a ∨ nonneg (-a) | ⟨(x : ℕ), (y : ℕ)⟩ := or.inl trivial | ⟨-[1+ x], -[1+ y]⟩ := or.inr trivial | ⟨0, -[1+ y]⟩ := or.inr trivial | ⟨-[1+ x], 0⟩ := or.inr trivial | ⟨(x+1:ℕ), -[1+ y]⟩ := nat.le_total | ⟨-[1+ x], (y+1:ℕ)⟩ := nat.le_total protected theorem le_total (a b : ℤ√d) : a ≤ b ∨ b ≤ a := let t := nonneg_total (b - a) in by rw [show -(b-a) = a-b, from neg_sub b a] at t; exact t instance : preorder ℤ√d := { le := zsqrtd.le, le_refl := zsqrtd.le_refl, le_trans := @zsqrtd.le_trans, lt := zsqrtd.lt, lt_iff_le_not_le := λ a b, (and_iff_right_of_imp (zsqrtd.le_total _ _).resolve_left).symm } protected theorem add_le_add_left (a b : ℤ√d) (ab : a ≤ b) (c : ℤ√d) : c + a ≤ c + b := show nonneg _, by rw add_sub_add_left_eq_sub; exact ab protected theorem le_of_add_le_add_left (a b c : ℤ√d) (h : c + a ≤ c + b) : a ≤ b := by simpa using zsqrtd.add_le_add_left _ _ h (-c) protected theorem add_lt_add_left (a b : ℤ√d) (h : a < b) (c) : c + a < c + b := λ h', h (zsqrtd.le_of_add_le_add_left _ _ _ h') theorem nonneg_smul {a : ℤ√d} {n : ℕ} (ha : nonneg a) : nonneg (n * a) := by rw ← int.cast_coe_nat; exact match a, nonneg_cases ha, ha with | ._, ⟨x, y, or.inl rfl⟩, ha := by rw smul_val; trivial | ._, ⟨x, y, or.inr $ or.inl rfl⟩, ha := by rw smul_val; simpa using nonnegg_pos_neg.2 (sq_le_smul n $ nonnegg_pos_neg.1 ha) | ._, ⟨x, y, or.inr $ or.inr rfl⟩, ha := by rw smul_val; simpa using nonnegg_neg_pos.2 (sq_le_smul n $ nonnegg_neg_pos.1 ha) end theorem nonneg_muld {a : ℤ√d} (ha : nonneg a) : nonneg (sqrtd * a) := by refine match a, nonneg_cases ha, ha with | ._, ⟨x, y, or.inl rfl⟩, ha := trivial | ._, ⟨x, y, or.inr $ or.inl rfl⟩, ha := by simp; apply nonnegg_neg_pos.2; simpa [sq_le, mul_comm, mul_left_comm] using nat.mul_le_mul_left d (nonnegg_pos_neg.1 ha) | ._, ⟨x, y, or.inr $ or.inr rfl⟩, ha := by simp; apply nonnegg_pos_neg.2; simpa [sq_le, mul_comm, mul_left_comm] using nat.mul_le_mul_left d (nonnegg_neg_pos.1 ha) end theorem nonneg_mul_lem {x y : ℕ} {a : ℤ√d} (ha : nonneg a) : nonneg (⟨x, y⟩ * a) := have (⟨x, y⟩ * a : ℤ√d) = x * a + sqrtd * (y * a), by rw [decompose, right_distrib, mul_assoc]; refl, by rw this; exact nonneg_add (nonneg_smul ha) (nonneg_muld $ nonneg_smul ha) theorem nonneg_mul {a b : ℤ√d} (ha : nonneg a) (hb : nonneg b) : nonneg (a * b) := match a, b, nonneg_cases ha, nonneg_cases hb, ha, hb with | ._, ._, ⟨x, y, or.inl rfl⟩, ⟨z, w, or.inl rfl⟩, ha, hb := trivial | ._, ._, ⟨x, y, or.inl rfl⟩, ⟨z, w, or.inr $ or.inr rfl⟩, ha, hb := nonneg_mul_lem hb | ._, ._, ⟨x, y, or.inl rfl⟩, ⟨z, w, or.inr $ or.inl rfl⟩, ha, hb := nonneg_mul_lem hb | ._, ._, ⟨x, y, or.inr $ or.inr rfl⟩, ⟨z, w, or.inl rfl⟩, ha, hb := by rw mul_comm; exact nonneg_mul_lem ha | ._, ._, ⟨x, y, or.inr $ or.inl rfl⟩, ⟨z, w, or.inl rfl⟩, ha, hb := by rw mul_comm; exact nonneg_mul_lem ha | ._, ._, ⟨x, y, or.inr $ or.inr rfl⟩, ⟨z, w, or.inr $ or.inr rfl⟩, ha, hb := by rw [calc (⟨-x, y⟩ * ⟨-z, w⟩ : ℤ√d) = ⟨_, _⟩ : rfl ... = ⟨x * z + d * y * w, -(x * w + y * z)⟩ : by simp]; exact nonnegg_pos_neg.2 (sq_le_mul.left (nonnegg_neg_pos.1 ha) (nonnegg_neg_pos.1 hb)) | ._, ._, ⟨x, y, or.inr $ or.inr rfl⟩, ⟨z, w, or.inr $ or.inl rfl⟩, ha, hb := by rw [calc (⟨-x, y⟩ * ⟨z, -w⟩ : ℤ√d) = ⟨_, _⟩ : rfl ... = ⟨-(x * z + d * y * w), x * w + y * z⟩ : by simp]; exact nonnegg_neg_pos.2 (sq_le_mul.right.left (nonnegg_neg_pos.1 ha) (nonnegg_pos_neg.1 hb)) | ._, ._, ⟨x, y, or.inr $ or.inl rfl⟩, ⟨z, w, or.inr $ or.inr rfl⟩, ha, hb := by rw [calc (⟨x, -y⟩ * ⟨-z, w⟩ : ℤ√d) = ⟨_, _⟩ : rfl ... = ⟨-(x * z + d * y * w), x * w + y * z⟩ : by simp]; exact nonnegg_neg_pos.2 (sq_le_mul.right.right.left (nonnegg_pos_neg.1 ha) (nonnegg_neg_pos.1 hb)) | ._, ._, ⟨x, y, or.inr $ or.inl rfl⟩, ⟨z, w, or.inr $ or.inl rfl⟩, ha, hb := by rw [calc (⟨x, -y⟩ * ⟨z, -w⟩ : ℤ√d) = ⟨_, _⟩ : rfl ... = ⟨x * z + d * y * w, -(x * w + y * z)⟩ : by simp]; exact nonnegg_pos_neg.2 (sq_le_mul.right.right.right (nonnegg_pos_neg.1 ha) (nonnegg_pos_neg.1 hb)) end protected theorem mul_nonneg (a b : ℤ√d) : 0 ≤ a → 0 ≤ b → 0 ≤ a * b := by repeat {rw ← nonneg_iff_zero_le}; exact nonneg_mul theorem not_sq_le_succ (c d y) (h : c > 0) : ¬sq_le (y + 1) c 0 d := not_le_of_gt $ mul_pos (mul_pos h $ nat.succ_pos _) $ nat.succ_pos _ /-- A nonsquare is a natural number that is not equal to the square of an integer. This is implemented as a typeclass because it's a necessary condition for much of the Pell equation theory. -/ class nonsquare (x : ℕ) : Prop := (ns : ∀n : ℕ, x ≠ n*n) parameter [dnsq : nonsquare d] include dnsq theorem d_pos : 0 < d := lt_of_le_of_ne (nat.zero_le _) $ ne.symm $ (nonsquare.ns d 0) theorem divides_sq_eq_zero {x y} (h : x * x = d * y * y) : x = 0 ∧ y = 0 := let g := x.gcd y in or.elim g.eq_zero_or_pos (λH, ⟨nat.eq_zero_of_gcd_eq_zero_left H, nat.eq_zero_of_gcd_eq_zero_right H⟩) (λgpos, false.elim $ let ⟨m, n, co, (hx : x = m * g), (hy : y = n * g)⟩ := nat.exists_coprime gpos in begin rw [hx, hy] at h, have : m * m = d * (n * n) := nat.eq_of_mul_eq_mul_left (mul_pos gpos gpos) (by simpa [mul_comm, mul_left_comm] using h), have co2 := let co1 := co.mul_right co in co1.mul co1, exact nonsquare.ns d m (nat.dvd_antisymm (by rw this; apply dvd_mul_right) $ co2.dvd_of_dvd_mul_right $ by simp [this]) end) theorem divides_sq_eq_zero_z {x y : ℤ} (h : x * x = d * y * y) : x = 0 ∧ y = 0 := by rw [mul_assoc, ← int.nat_abs_mul_self, ← int.nat_abs_mul_self, ← int.coe_nat_mul, ← mul_assoc] at h; exact let ⟨h1, h2⟩ := divides_sq_eq_zero (int.coe_nat_inj h) in ⟨int.eq_zero_of_nat_abs_eq_zero h1, int.eq_zero_of_nat_abs_eq_zero h2⟩ theorem not_divides_square (x y) : (x + 1) * (x + 1) ≠ d * (y + 1) * (y + 1) := λe, by have t := (divides_sq_eq_zero e).left; contradiction theorem nonneg_antisymm : Π {a : ℤ√d}, nonneg a → nonneg (-a) → a = 0 | ⟨0, 0⟩ xy yx := rfl | ⟨-[1+ x], -[1+ y]⟩ xy yx := false.elim xy | ⟨(x+1:nat), (y+1:nat)⟩ xy yx := false.elim yx | ⟨-[1+ x], 0⟩ xy yx := absurd xy (not_sq_le_succ _ _ _ dec_trivial) | ⟨(x+1:nat), 0⟩ xy yx := absurd yx (not_sq_le_succ _ _ _ dec_trivial) | ⟨0, -[1+ y]⟩ xy yx := absurd xy (not_sq_le_succ _ _ _ d_pos) | ⟨0, (y+1:nat)⟩ _ yx := absurd yx (not_sq_le_succ _ _ _ d_pos) | ⟨(x+1:nat), -[1+ y]⟩ (xy : sq_le _ _ _ _) (yx : sq_le _ _ _ _) := let t := le_antisymm yx xy in by rw[one_mul] at t; exact absurd t (not_divides_square _ _) | ⟨-[1+ x], (y+1:nat)⟩ (xy : sq_le _ _ _ _) (yx : sq_le _ _ _ _) := let t := le_antisymm xy yx in by rw[one_mul] at t; exact absurd t (not_divides_square _ _) theorem le_antisymm {a b : ℤ√d} (ab : a ≤ b) (ba : b ≤ a) : a = b := eq_of_sub_eq_zero $ nonneg_antisymm ba (by rw neg_sub; exact ab) instance : decidable_linear_order ℤ√d := { le_antisymm := @zsqrtd.le_antisymm, le_total := zsqrtd.le_total, decidable_le := zsqrtd.decidable_le, ..zsqrtd.preorder } protected theorem eq_zero_or_eq_zero_of_mul_eq_zero : Π {a b : ℤ√d}, a * b = 0 → a = 0 ∨ b = 0 | ⟨x, y⟩ ⟨z, w⟩ h := by injection h with h1 h2; exact have h1 : x*z = -(d*y*w), from eq_neg_of_add_eq_zero h1, have h2 : x*w = -(y*z), from eq_neg_of_add_eq_zero h2, have fin : x*x = d*y*y → (⟨x, y⟩:ℤ√d) = 0, from λe, match x, y, divides_sq_eq_zero_z e with ._, ._, ⟨rfl, rfl⟩ := rfl end, if z0 : z = 0 then if w0 : w = 0 then or.inr (match z, w, z0, w0 with ._, ._, rfl, rfl := rfl end) else or.inl $ fin $ eq_of_mul_eq_mul_right w0 $ calc x * x * w = -y * (x * z) : by simp [h2, mul_assoc, mul_left_comm] ... = d * y * y * w : by simp [h1, mul_assoc, mul_left_comm] else or.inl $ fin $ eq_of_mul_eq_mul_right z0 $ calc x * x * z = d * -y * (x * w) : by simp [h1, mul_assoc, mul_left_comm] ... = d * y * y * z : by simp [h2, mul_assoc, mul_left_comm] instance : integral_domain ℤ√d := { zero_ne_one := zero_ne_one, eq_zero_or_eq_zero_of_mul_eq_zero := @zsqrtd.eq_zero_or_eq_zero_of_mul_eq_zero, ..zsqrtd.comm_ring } protected theorem mul_pos (a b : ℤ√d) (a0 : 0 < a) (b0 : 0 < b) : 0 < a * b := λab, or.elim (eq_zero_or_eq_zero_of_mul_eq_zero (le_antisymm ab (mul_nonneg _ _ (le_of_lt a0) (le_of_lt b0)))) (λe, ne_of_gt a0 e) (λe, ne_of_gt b0 e) instance : decidable_linear_ordered_comm_ring ℤ√d := { add_le_add_left := @zsqrtd.add_le_add_left, add_lt_add_left := @zsqrtd.add_lt_add_left, zero_ne_one := zero_ne_one, mul_nonneg := @zsqrtd.mul_nonneg, mul_pos := @zsqrtd.mul_pos, zero_lt_one := dec_trivial, ..zsqrtd.comm_ring, ..zsqrtd.decidable_linear_order } instance : decidable_linear_ordered_semiring ℤ√d := by apply_instance instance : linear_ordered_semiring ℤ√d := by apply_instance instance : ordered_semiring ℤ√d := by apply_instance end end zsqrtd namespace pell open nat section parameters {a : ℕ} (a1 : a > 1) include a1 private def d := a*a - 1 @[simp] theorem d_pos : 0 < d := nat.sub_pos_of_lt (mul_lt_mul a1 (le_of_lt a1) dec_trivial dec_trivial : 1*1<a*a) /-- The Pell sequences, defined together in mutual recursion. -/ def pell : ℕ → ℕ × ℕ := λn, nat.rec_on n (1, 0) (λn xy, (xy.1*a + d*xy.2, xy.1 + xy.2*a)) /-- The Pell `x` sequence. -/ def xn (n : ℕ) : ℕ := (pell n).1 /-- The Pell `y` sequence. -/ def yn (n : ℕ) : ℕ := (pell n).2 @[simp] theorem pell_val (n : ℕ) : pell n = (xn n, yn n) := show pell n = ((pell n).1, (pell n).2), from match pell n with (a, b) := rfl end @[simp] theorem xn_zero : xn 0 = 1 := rfl @[simp] theorem yn_zero : yn 0 = 0 := rfl @[simp] theorem xn_succ (n : ℕ) : xn (n+1) = xn n * a + d * yn n := rfl @[simp] theorem yn_succ (n : ℕ) : yn (n+1) = xn n + yn n * a := rfl @[simp] theorem xn_one : xn 1 = a := by simp @[simp] theorem yn_one : yn 1 = 1 := by simp def xz (n : ℕ) : ℤ := xn n def yz (n : ℕ) : ℤ := yn n def az : ℤ := a theorem asq_pos : 0 < a*a := le_trans (le_of_lt a1) (by have := @nat.mul_le_mul_left 1 a a (le_of_lt a1); rwa mul_one at this) theorem dz_val : ↑d = az*az - 1 := have 1 ≤ a*a, from asq_pos, show ↑(a*a - 1) = _, by rw int.coe_nat_sub this; refl @[simp] theorem xz_succ (n : ℕ) : xz (n+1) = xz n * az + ↑d * yz n := rfl @[simp] theorem yz_succ (n : ℕ) : yz (n+1) = xz n + yz n * az := rfl /-- The Pell sequence can also be viewed as an element of `ℤ√d` -/ def pell_zd (n : ℕ) : ℤ√d := ⟨xn n, yn n⟩ @[simp] theorem pell_zd_re (n : ℕ) : (pell_zd n).re = xn n := rfl @[simp] theorem pell_zd_im (n : ℕ) : (pell_zd n).im = yn n := rfl /-- The property of being a solution to the Pell equation, expressed as a property of elements of `ℤ√d`. -/ def is_pell : ℤ√d → Prop | ⟨x, y⟩ := x*x - d*y*y = 1 theorem is_pell_nat {x y : ℕ} : is_pell ⟨x, y⟩ ↔ x*x - d*y*y = 1 := ⟨λh, int.coe_nat_inj (by rw int.coe_nat_sub (int.le_of_coe_nat_le_coe_nat $ int.le.intro_sub h); exact h), λh, show ((x*x : ℕ) - (d*y*y:ℕ) : ℤ) = 1, by rw [← int.coe_nat_sub $ le_of_lt $ nat.lt_of_sub_eq_succ h, h]; refl⟩ theorem is_pell_norm : Π {b : ℤ√d}, is_pell b ↔ b * b.conj = 1 | ⟨x, y⟩ := by simp [zsqrtd.ext, is_pell, mul_comm] theorem is_pell_mul {b c : ℤ√d} (hb : is_pell b) (hc : is_pell c) : is_pell (b * c) := is_pell_norm.2 (by simp [mul_comm, mul_left_comm, zsqrtd.conj_mul, pell.is_pell_norm.1 hb, pell.is_pell_norm.1 hc]) theorem is_pell_conj : ∀ {b : ℤ√d}, is_pell b ↔ is_pell b.conj | ⟨x, y⟩ := by simp [is_pell, zsqrtd.conj] @[simp] theorem pell_zd_succ (n : ℕ) : pell_zd (n+1) = pell_zd n * ⟨a, 1⟩ := by simp [zsqrtd.ext] theorem is_pell_one : is_pell ⟨a, 1⟩ := show az*az-d*1*1=1, by simp [dz_val] theorem is_pell_pell_zd : ∀ (n : ℕ), is_pell (pell_zd n) | 0 := rfl | (n+1) := let o := is_pell_one in by simp; exact pell.is_pell_mul (is_pell_pell_zd n) o @[simp] theorem pell_eqz (n : ℕ) : xz n * xz n - d * yz n * yz n = 1 := is_pell_pell_zd n @[simp] theorem pell_eq (n : ℕ) : xn n * xn n - d * yn n * yn n = 1 := let pn := pell_eqz n in have h : (↑(xn n * xn n) : ℤ) - ↑(d * yn n * yn n) = 1, by repeat {rw int.coe_nat_mul}; exact pn, have hl : d * yn n * yn n ≤ xn n * xn n, from int.le_of_coe_nat_le_coe_nat $ int.le.intro $ add_eq_of_eq_sub' $ eq.symm h, int.coe_nat_inj (by rw int.coe_nat_sub hl; exact h) instance dnsq : zsqrtd.nonsquare d := ⟨λn h, have n*n + 1 = a*a, by rw ← h; exact nat.succ_pred_eq_of_pos (asq_pos a1), have na : n < a, from nat.mul_self_lt_mul_self_iff.2 (by rw ← this; exact nat.lt_succ_self _), have (n+1)*(n+1) ≤ n*n + 1, by rw this; exact nat.mul_self_le_mul_self na, have n+n ≤ 0, from @nat.le_of_add_le_add_right (n*n + 1) _ _ (by simpa [mul_add, mul_comm, mul_left_comm]), ne_of_gt d_pos $ by rw nat.eq_zero_of_le_zero (le_trans (nat.le_add_left _ _) this) at h; exact h⟩ theorem xn_ge_a_pow : ∀ (n : ℕ), a^n ≤ xn n | 0 := le_refl 1 | (n+1) := by simp [nat.pow_succ]; exact le_trans (nat.mul_le_mul_right _ (xn_ge_a_pow n)) (nat.le_add_right _ _) theorem n_lt_a_pow : ∀ (n : ℕ), n < a^n | 0 := nat.le_refl 1 | (n+1) := begin have IH := n_lt_a_pow n, have : a^n + a^n ≤ a^n * a, { rw ← mul_two, exact nat.mul_le_mul_left _ a1 }, simp [nat.pow_succ], refine lt_of_lt_of_le _ this, exact add_lt_add_of_lt_of_le IH (lt_of_le_of_lt (nat.zero_le _) IH) end theorem n_lt_xn (n) : n < xn n := lt_of_lt_of_le (n_lt_a_pow n) (xn_ge_a_pow n) theorem x_pos (n) : xn n > 0 := lt_of_le_of_lt (nat.zero_le n) (n_lt_xn n) lemma eq_pell_lem : ∀n (b:ℤ√d), 1 ≤ b → is_pell b → pell_zd n ≥ b → ∃n, b = pell_zd n | 0 b := λh1 hp hl, ⟨0, @zsqrtd.le_antisymm _ dnsq _ _ hl h1⟩ | (n+1) b := λh1 hp h, have a1p : (0:ℤ√d) ≤ ⟨a, 1⟩, from trivial, have am1p : (0:ℤ√d) ≤ ⟨a, -1⟩, from show (_:nat) ≤ _, by simp; exact nat.pred_le _, have a1m : (⟨a, 1⟩ * ⟨a, -1⟩ : ℤ√d) = 1, from is_pell_norm.1 is_pell_one, if ha : b ≥ ⟨↑a, 1⟩ then let ⟨m, e⟩ := eq_pell_lem n (b * ⟨a, -1⟩) (by rw ← a1m; exact mul_le_mul_of_nonneg_right ha am1p) (is_pell_mul hp (is_pell_conj.1 is_pell_one)) (by have t := mul_le_mul_of_nonneg_right h am1p; rwa [pell_zd_succ, mul_assoc, a1m, mul_one] at t) in ⟨m+1, by rw [show b = b * ⟨a, -1⟩ * ⟨a, 1⟩, by rw [mul_assoc, eq.trans (mul_comm _ _) a1m]; simp, pell_zd_succ, e]⟩ else suffices ¬1 < b, from ⟨0, show b = 1, from (or.resolve_left (lt_or_eq_of_le h1) this).symm⟩, λh1l, by cases b with x y; exact have bm : (_*⟨_,_⟩ :ℤ√(d a1)) = 1, from pell.is_pell_norm.1 hp, have y0l : (0:ℤ√(d a1)) < ⟨x - x, y - -y⟩, from sub_lt_sub h1l $ λ(hn : (1:ℤ√(d a1)) ≤ ⟨x, -y⟩), by have t := mul_le_mul_of_nonneg_left hn (le_trans zero_le_one h1); rw [bm, mul_one] at t; exact h1l t, have yl2 : (⟨_, _⟩ : ℤ√_) < ⟨_, _⟩, from show (⟨x, y⟩ - ⟨x, -y⟩ : ℤ√(d a1)) < ⟨a, 1⟩ - ⟨a, -1⟩, from sub_lt_sub (by exact ha) $ λ(hn : (⟨x, -y⟩ : ℤ√(d a1)) ≤ ⟨a, -1⟩), by have t := mul_le_mul_of_nonneg_right (mul_le_mul_of_nonneg_left hn (le_trans zero_le_one h1)) a1p; rw [bm, one_mul, mul_assoc, eq.trans (mul_comm _ _) a1m, mul_one] at t; exact ha t, by simp at y0l; simp at yl2; exact match y, y0l, (yl2 : (⟨_, _⟩ : ℤ√_) < ⟨_, _⟩) with | 0, y0l, yl2 := y0l (le_refl 0) | (y+1 : ℕ), y0l, yl2 := yl2 (zsqrtd.le_of_le_le (le_refl 0) (let t := int.coe_nat_le_coe_nat_of_le (nat.succ_pos y) in add_le_add t t)) | -[1+y], y0l, yl2 := y0l trivial end theorem eq_pell_zd (b : ℤ√d) (b1 : 1 ≤ b) (hp : is_pell b) : ∃n, b = pell_zd n := let ⟨n, h⟩ := @zsqrtd.le_arch d b in eq_pell_lem n b b1 hp $ zsqrtd.le_trans h $ by rw zsqrtd.coe_nat_val; exact zsqrtd.le_of_le_le (int.coe_nat_le_coe_nat_of_le $ le_of_lt $ n_lt_xn _ _) (int.coe_zero_le _) theorem eq_pell {x y : ℕ} (hp : x*x - d*y*y = 1) : ∃n, x = xn n ∧ y = yn n := have (1:ℤ√d) ≤ ⟨x, y⟩, from match x, hp with | 0, (hp : 0 - _ = 1) := by rw nat.zero_sub at hp; contradiction | (x+1), hp := zsqrtd.le_of_le_le (int.coe_nat_le_coe_nat_of_le $ nat.succ_pos x) (int.coe_zero_le _) end, let ⟨m, e⟩ := eq_pell_zd ⟨x, y⟩ this (is_pell_nat.2 hp) in ⟨m, match x, y, e with ._, ._, rfl := ⟨rfl, rfl⟩ end⟩ theorem pell_zd_add (m) : ∀ n, pell_zd (m + n) = pell_zd m * pell_zd n | 0 := (mul_one _).symm | (n+1) := by rw[← add_assoc, pell_zd_succ, pell_zd_succ, pell_zd_add n, ← mul_assoc] theorem xn_add (m n) : xn (m + n) = xn m * xn n + d * yn m * yn n := by injection (pell_zd_add _ m n) with h _; repeat {rw ← int.coe_nat_add at h <|> rw ← int.coe_nat_mul at h}; exact int.coe_nat_inj h theorem yn_add (m n) : yn (m + n) = xn m * yn n + yn m * xn n := by injection (pell_zd_add _ m n) with _ h; repeat {rw ← int.coe_nat_add at h <|> rw ← int.coe_nat_mul at h}; exact int.coe_nat_inj h theorem pell_zd_sub {m n} (h : n ≤ m) : pell_zd (m - n) = pell_zd m * (pell_zd n).conj := let t := pell_zd_add n (m - n) in by rw [nat.add_sub_of_le h] at t; rw [t, mul_comm (pell_zd _ n) _, mul_assoc, (is_pell_norm _).1 (is_pell_pell_zd _ _), mul_one] theorem xz_sub {m n} (h : n ≤ m) : xz (m - n) = xz m * xz n - d * yz m * yz n := by injection (pell_zd_sub _ h) with h _; repeat {rw ← neg_mul_eq_mul_neg at h}; exact h theorem yz_sub {m n} (h : n ≤ m) : yz (m - n) = xz n * yz m - xz m * yz n := by injection (pell_zd_sub a1 h) with _ h; repeat {rw ← neg_mul_eq_mul_neg at h}; rw [add_comm, mul_comm] at h; exact h theorem xy_coprime (n) : (xn n).coprime (yn n) := nat.coprime_of_dvd' $ λk kx ky, let p := pell_eq n in by rw ← p; exact nat.dvd_sub (le_of_lt $ nat.lt_of_sub_eq_succ p) (dvd_mul_of_dvd_right kx _) (dvd_mul_of_dvd_right ky _) theorem y_increasing {m} : Π {n}, m < n → yn m < yn n | 0 h := absurd h $ nat.not_lt_zero _ | (n+1) h := have yn m ≤ yn n, from or.elim (lt_or_eq_of_le $ nat.le_of_succ_le_succ h) (λhl, le_of_lt $ y_increasing hl) (λe, by rw e), by simp; refine lt_of_le_of_lt _ (nat.lt_add_of_pos_left $ x_pos a1 n); rw ← mul_one (yn a1 m); exact mul_le_mul this (le_of_lt a1) (nat.zero_le _) (nat.zero_le _) theorem x_increasing {m} : Π {n}, m < n → xn m < xn n | 0 h := absurd h $ nat.not_lt_zero _ | (n+1) h := have xn m ≤ xn n, from or.elim (lt_or_eq_of_le $ nat.le_of_succ_le_succ h) (λhl, le_of_lt $ x_increasing hl) (λe, by rw e), by simp; refine lt_of_lt_of_le (lt_of_le_of_lt this _) (nat.le_add_right _ _); have t := nat.mul_lt_mul_of_pos_left a1 (x_pos a1 n); rwa mul_one at t theorem yn_ge_n : Π n, n ≤ yn n | 0 := nat.zero_le _ | (n+1) := show n < yn (n+1), from lt_of_le_of_lt (yn_ge_n n) (y_increasing $ nat.lt_succ_self n) theorem y_mul_dvd (n) : ∀k, yn n ∣ yn (n * k) | 0 := dvd_zero _ | (k+1) := by rw [nat.mul_succ, yn_add]; exact dvd_add (dvd_mul_left _ _) (dvd_mul_of_dvd_left (y_mul_dvd k) _) theorem y_dvd_iff (m n) : yn m ∣ yn n ↔ m ∣ n := ⟨λh, nat.dvd_of_mod_eq_zero $ (nat.eq_zero_or_pos _).resolve_right $ λhp, have co : nat.coprime (yn m) (xn (m * (n / m))), from nat.coprime.symm $ (xy_coprime _).coprime_dvd_right (y_mul_dvd m (n / m)), have m0 : m > 0, from m.eq_zero_or_pos.resolve_left $ λe, by rw [e, nat.mod_zero] at hp; rw [e] at h; exact have 0 < yn a1 n, from y_increasing _ hp, ne_of_lt (y_increasing a1 hp) (eq_zero_of_zero_dvd h).symm, by rw [← nat.mod_add_div n m, yn_add] at h; exact not_le_of_gt (y_increasing _ $ nat.mod_lt n m0) (nat.le_of_dvd (y_increasing _ hp) $ co.dvd_of_dvd_mul_right $ (nat.dvd_add_iff_right $ dvd_mul_of_dvd_right (y_mul_dvd _ _ _) _).2 h), λ⟨k, e⟩, by rw e; apply y_mul_dvd⟩ theorem xy_modeq_yn (n) : ∀k, xn (n * k) ≡ (xn n)^k [MOD (yn n)^2] ∧ yn (n * k) ≡ k * (xn n)^(k-1) * yn n [MOD (yn n)^3] | 0 := by constructor; simp | (k+1) := let ⟨hx, hy⟩ := xy_modeq_yn k in have L : xn (n * k) * xn n + d * yn (n * k) * yn n ≡ xn n^k * xn n + 0 [MOD yn n^2], from modeq.modeq_add (modeq.modeq_mul_right _ hx) $ modeq.modeq_zero_iff.2 $ by rw nat.pow_succ; exact mul_dvd_mul_right (dvd_mul_of_dvd_right (modeq.modeq_zero_iff.1 $ (hy.modeq_of_dvd_of_modeq $ by simp [nat.pow_succ]).trans $ modeq.modeq_zero_iff.2 $ by simp [-mul_comm, -mul_assoc]) _) _, have R : xn (n * k) * yn n + yn (n * k) * xn n ≡ xn n^k * yn n + k * xn n^k * yn n [MOD yn n^3], from modeq.modeq_add (by rw nat.pow_succ; exact modeq.modeq_mul_right' _ hx) $ have k * xn n^(k - 1) * yn n * xn n = k * xn n^k * yn n, by clear _let_match; cases k with k; simp [nat.pow_succ, mul_comm, mul_left_comm], by rw ← this; exact modeq.modeq_mul_right _ hy, by rw [nat.add_sub_cancel, nat.mul_succ, xn_add, yn_add, nat.pow_succ (xn _ n), nat.succ_mul, add_comm (k * xn _ n^k) (xn _ n^k), right_distrib]; exact ⟨L, R⟩ theorem ysq_dvd_yy (n) : yn n * yn n ∣ yn (n * yn n) := modeq.modeq_zero_iff.1 $ ((xy_modeq_yn n (yn n)).right.modeq_of_dvd_of_modeq $ by simp [nat.pow_succ]).trans (modeq.modeq_zero_iff.2 $ by simp [mul_dvd_mul_left, mul_assoc]) theorem dvd_of_ysq_dvd {n t} (h : yn n * yn n ∣ yn t) : yn n ∣ t := have nt : n ∣ t, from (y_dvd_iff n t).1 $ dvd_of_mul_left_dvd h, n.eq_zero_or_pos.elim (λn0, by rw n0; rw n0 at nt; exact nt) $ λ(n0l : n > 0), let ⟨k, ke⟩ := nt in have yn n ∣ k * (xn n)^(k-1), from nat.dvd_of_mul_dvd_mul_right (y_increasing n0l) $ modeq.modeq_zero_iff.1 $ by have xm := (xy_modeq_yn a1 n k).right; rw ← ke at xm; exact (xm.modeq_of_dvd_of_modeq $ by simp [nat.pow_succ]).symm.trans (modeq.modeq_zero_iff.2 h), by rw ke; exact dvd_mul_of_dvd_right (((xy_coprime _ _).pow_left _).symm.dvd_of_dvd_mul_right this) _ theorem pell_zd_succ_succ (n) : pell_zd (n + 2) + pell_zd n = (2 * a : ℕ) * pell_zd (n + 1) := have (1:ℤ√d) + ⟨a, 1⟩ * ⟨a, 1⟩ = ⟨a, 1⟩ * (2 * a), by rw zsqrtd.coe_nat_val; change (⟨_,_⟩:ℤ√(d a1))=⟨_,_⟩; rw dz_val; change az a1 with a; simp [mul_add, add_mul], by simpa [mul_add, mul_comm, mul_left_comm] using congr_arg (* pell_zd a1 n) this theorem xy_succ_succ (n) : xn (n + 2) + xn n = (2 * a) * xn (n + 1) ∧ yn (n + 2) + yn n = (2 * a) * yn (n + 1) := begin have := pell_zd_succ_succ a1 n, unfold pell_zd at this, rw [← int.cast_coe_nat, zsqrtd.smul_val] at this, injection this with h₁ h₂, split; apply int.coe_nat_inj; [simpa using h₁, simpa using h₂] end theorem xn_succ_succ (n) : xn (n + 2) + xn n = (2 * a) * xn (n + 1) := (xy_succ_succ n).1 theorem yn_succ_succ (n) : yn (n + 2) + yn n = (2 * a) * yn (n + 1) := (xy_succ_succ n).2 theorem xz_succ_succ (n) : xz (n + 2) = (2 * a : ℕ) * xz (n + 1) - xz n := eq_sub_of_add_eq $ by delta xz; rw [← int.coe_nat_add, ← int.coe_nat_mul, xn_succ_succ] theorem yz_succ_succ (n) : yz (n + 2) = (2 * a : ℕ) * yz (n + 1) - yz n := eq_sub_of_add_eq $ by delta yz; rw [← int.coe_nat_add, ← int.coe_nat_mul, yn_succ_succ] theorem yn_modeq_a_sub_one : ∀ n, yn n ≡ n [MOD a-1] | 0 := by simp | 1 := by simp | (n+2) := modeq.modeq_add_cancel_right (yn_modeq_a_sub_one n) $ have 2*(n+1) = n+2+n, by simp [two_mul], by rw [yn_succ_succ, ← this]; refine modeq.modeq_mul (modeq.modeq_mul_left 2 (_ : a ≡ 1 [MOD a-1])) (yn_modeq_a_sub_one (n+1)); exact (modeq.modeq_of_dvd $ by rw [int.coe_nat_sub $ le_of_lt a1]; apply dvd_refl).symm theorem yn_modeq_two : ∀ n, yn n ≡ n [MOD 2] | 0 := by simp | 1 := by simp | (n+2) := modeq.modeq_add_cancel_right (yn_modeq_two n) $ have 2*(n+1) = n+2+n, by simp [two_mul], by rw [yn_succ_succ, ← this]; refine modeq.modeq_mul _ (yn_modeq_two (n+1)); exact modeq.trans (modeq.modeq_zero_iff.2 $ by simp) (modeq.modeq_zero_iff.2 $ by simp).symm -- TODO(Mario): Hopefully a tactic will be able to dispense this lemma lemma x_sub_y_dvd_pow_lem (y2 y1 y0 yn1 yn0 xn1 xn0 ay a2 : ℤ) : (a2 * yn1 - yn0) * ay + y2 - (a2 * xn1 - xn0) = y2 - a2 * y1 + y0 + a2 * (yn1 * ay + y1 - xn1) - (yn0 * ay + y0 - xn0) := calc (a2 * yn1 - yn0) * ay + y2 - (a2 * xn1 - xn0) = a2 * yn1 * ay - yn0 * ay + y2 - (a2 * xn1 - xn0) : by rw [mul_sub_right_distrib] ... = y2 + a2 * (yn1 * ay) - a2 * xn1 - yn0 * ay + xn0 : by simp [mul_comm, mul_left_comm] ... = y2 + a2 * (yn1 * ay) - a2 * y1 + a2 * y1 - a2 * xn1 - yn0 * ay + y0 - y0 + xn0 : by rw [add_sub_cancel, sub_add_cancel] ... = y2 - a2 * y1 + y0 + a2 * (yn1 * ay + y1 - xn1) - (yn0 * ay + y0 - xn0) : by simp [mul_add] theorem x_sub_y_dvd_pow (y : ℕ) : ∀ n, (2*a*y - y*y - 1 : ℤ) ∣ yz n * (a - y) + ↑(y^n) - xz n | 0 := by simp [xz, yz, int.coe_nat_zero, int.coe_nat_one] | 1 := by simp [xz, yz, int.coe_nat_zero, int.coe_nat_one] | (n+2) := have (2*a*y - y*y - 1 : ℤ) ∣ ↑(y^(n + 2)) - ↑(2 * a) * ↑(y^(n + 1)) + ↑(y^n), from ⟨-↑(y^n), by simp [nat.pow_succ, mul_add, int.coe_nat_mul, show ((2:ℕ):ℤ) = 2, from rfl, mul_comm, mul_left_comm]⟩, by rw [xz_succ_succ, yz_succ_succ, x_sub_y_dvd_pow_lem a1 ↑(y^(n+2)) ↑(y^(n+1)) ↑(y^n)]; exact dvd_sub (dvd_add this $ dvd_mul_of_dvd_right (x_sub_y_dvd_pow (n+1)) _) (x_sub_y_dvd_pow n) theorem xn_modeq_x2n_add_lem (n j) : xn n ∣ d * yn n * (yn n * xn j) + xn j := have h1 : d * yn n * (yn n * xn j) + xn j = (d * yn n * yn n + 1) * xn j, by simp [add_mul, mul_assoc], have h2 : d * yn n * yn n + 1 = xn n * xn n, by apply int.coe_nat_inj; repeat {rw int.coe_nat_add <|> rw int.coe_nat_mul}; exact add_eq_of_eq_sub' (eq.symm $ pell_eqz _ _), by rw h2 at h1; rw [h1, mul_assoc]; exact dvd_mul_right _ _ theorem xn_modeq_x2n_add (n j) : xn (2 * n + j) + xn j ≡ 0 [MOD xn n] := by rw [two_mul, add_assoc, xn_add, add_assoc]; exact show _ ≡ 0+0 [MOD xn a1 n], from modeq.modeq_add (modeq.modeq_zero_iff.2 $ dvd_mul_right (xn a1 n) (xn a1 (n + j))) $ by rw [yn_add, left_distrib, add_assoc]; exact show _ ≡ 0+0 [MOD xn a1 n], from modeq.modeq_add (modeq.modeq_zero_iff.2 $ dvd_mul_of_dvd_right (dvd_mul_right _ _) _) $ modeq.modeq_zero_iff.2 $ xn_modeq_x2n_add_lem _ _ _ lemma xn_modeq_x2n_sub_lem {n j} (h : j ≤ n) : xn (2 * n - j) + xn j ≡ 0 [MOD xn n] := have h1 : xz n ∣ ↑d * yz n * yz (n - j) + xz j, by rw [yz_sub _ h, mul_sub_left_distrib, sub_add_eq_add_sub]; exact dvd_sub (by delta xz; delta yz; repeat {rw ← int.coe_nat_add <|> rw ← int.coe_nat_mul}; rw mul_comm (xn a1 j) (yn a1 n); exact int.coe_nat_dvd.2 (xn_modeq_x2n_add_lem _ _ _)) (dvd_mul_of_dvd_right (dvd_mul_right _ _) _), by rw [two_mul, nat.add_sub_assoc h, xn_add, add_assoc]; exact show _ ≡ 0+0 [MOD xn a1 n], from modeq.modeq_add (modeq.modeq_zero_iff.2 $ dvd_mul_right _ _) $ modeq.modeq_zero_iff.2 $ int.coe_nat_dvd.1 $ by simpa [xz, yz] using h1 theorem xn_modeq_x2n_sub {n j} (h : j ≤ 2 * n) : xn (2 * n - j) + xn j ≡ 0 [MOD xn n] := (le_total j n).elim xn_modeq_x2n_sub_lem (λjn, have 2 * n - j + j ≤ n + j, by rw [nat.sub_add_cancel h, two_mul]; exact nat.add_le_add_left jn _, let t := xn_modeq_x2n_sub_lem (nat.le_of_add_le_add_right this) in by rwa [nat.sub_sub_self h, add_comm] at t) theorem xn_modeq_x4n_add (n j) : xn (4 * n + j) ≡ xn j [MOD xn n] := modeq.modeq_add_cancel_right (modeq.refl $ xn (2 * n + j)) $ by refine @modeq.trans _ _ 0 _ _ (by rw add_comm; exact (xn_modeq_x2n_add _ _ _).symm); rw [show 4*n = 2*n + 2*n, from right_distrib 2 2 n, add_assoc]; apply xn_modeq_x2n_add theorem xn_modeq_x4n_sub {n j} (h : j ≤ 2 * n) : xn (4 * n - j) ≡ xn j [MOD xn n] := have h' : j ≤ 2*n, from le_trans h (by rw nat.succ_mul; apply nat.le_add_left), modeq.modeq_add_cancel_right (modeq.refl $ xn (2 * n - j)) $ by refine @modeq.trans _ _ 0 _ _ (by rw add_comm; exact (xn_modeq_x2n_sub _ h).symm); rw [show 4*n = 2*n + 2*n, from right_distrib 2 2 n, nat.add_sub_assoc h']; apply xn_modeq_x2n_add theorem eq_of_xn_modeq_lem1 {i n} (npos : n > 0) : Π {j}, i < j → j < n → xn i % xn n < xn j % xn n | 0 ij _ := absurd ij (nat.not_lt_zero _) | (j+1) ij jn := suffices xn j % xn n < xn (j + 1) % xn n, from (lt_or_eq_of_le (nat.le_of_succ_le_succ ij)).elim (λh, lt_trans (eq_of_xn_modeq_lem1 h (le_of_lt jn)) this) (λh, by rw h; exact this), by rw [nat.mod_eq_of_lt (x_increasing _ (nat.lt_of_succ_lt jn)), nat.mod_eq_of_lt (x_increasing _ jn)]; exact x_increasing _ (nat.lt_succ_self _) theorem eq_of_xn_modeq_lem2 {n} (h : 2 * xn n = xn (n + 1)) : a = 2 ∧ n = 0 := by rw [xn_succ, mul_comm] at h; exact have n = 0, from n.eq_zero_or_pos.resolve_right $ λnp, ne_of_lt (lt_of_le_of_lt (nat.mul_le_mul_left _ a1) (nat.lt_add_of_pos_right $ mul_pos (d_pos a1) (y_increasing a1 np))) h, by cases this; simp at h; exact ⟨h.symm, rfl⟩ theorem eq_of_xn_modeq_lem3 {i n} (npos : n > 0) : Π {j}, i < j → j ≤ 2 * n → j ≠ n → ¬(a = 2 ∧ n = 1 ∧ i = 0 ∧ j = 2) → xn i % xn n < xn j % xn n | 0 ij _ _ _ := absurd ij (nat.not_lt_zero _) | (j+1) ij j2n jnn ntriv := have lem2 : ∀k > n, k ≤ 2*n → (↑(xn k % xn n) : ℤ) = xn n - xn (2 * n - k), from λk kn k2n, let k2nl := lt_of_add_lt_add_right $ show 2*n-k+k < n+k, by {rw nat.sub_add_cancel, rw two_mul; exact (add_lt_add_left kn n), exact k2n } in have xle : xn (2 * n - k) ≤ xn n, from le_of_lt $ x_increasing k2nl, suffices xn k % xn n = xn n - xn (2 * n - k), by rw [this, int.coe_nat_sub xle], by { rw ← nat.mod_eq_of_lt (nat.sub_lt (x_pos a1 n) (x_pos a1 (2 * n - k))), apply modeq.modeq_add_cancel_right (modeq.refl (xn a1 (2 * n - k))), rw [nat.sub_add_cancel xle], have t := xn_modeq_x2n_sub_lem a1 (le_of_lt k2nl), rw nat.sub_sub_self k2n at t, exact t.trans (modeq.modeq_zero_iff.2 $ dvd_refl _).symm }, (lt_trichotomy j n).elim (λ (jn : j < n), eq_of_xn_modeq_lem1 npos ij (lt_of_le_of_ne jn jnn)) $ λo, o.elim (λ (jn : j = n), by { cases jn, apply int.lt_of_coe_nat_lt_coe_nat, rw [lem2 (n+1) (nat.lt_succ_self _) j2n, show 2 * n - (n + 1) = n - 1, by rw[two_mul, ← nat.sub_sub, nat.add_sub_cancel]], refine lt_sub_left_of_add_lt (int.coe_nat_lt_coe_nat_of_lt _), cases (lt_or_eq_of_le $ nat.le_of_succ_le_succ ij) with lin ein, { rw nat.mod_eq_of_lt (x_increasing _ lin), have ll : xn a1 (n-1) + xn a1 (n-1) ≤ xn a1 n, { rw [← two_mul, mul_comm, show xn a1 n = xn a1 (n-1+1), by rw [nat.sub_add_cancel npos], xn_succ], exact le_trans (nat.mul_le_mul_left _ a1) (nat.le_add_right _ _) }, have npm : (n-1).succ = n := nat.succ_pred_eq_of_pos npos, have il : i ≤ n - 1 := by apply nat.le_of_succ_le_succ; rw npm; exact lin, cases lt_or_eq_of_le il with ill ile, { exact lt_of_lt_of_le (nat.add_lt_add_left (x_increasing a1 ill) _) ll }, { rw ile, apply lt_of_le_of_ne ll, rw ← two_mul, exact λe, ntriv $ let ⟨a2, s1⟩ := @eq_of_xn_modeq_lem2 _ a1 (n-1) (by rw[nat.sub_add_cancel npos]; exact e) in have n1 : n = 1, from le_antisymm (nat.le_of_sub_eq_zero s1) npos, by rw [ile, a2, n1]; exact ⟨rfl, rfl, rfl, rfl⟩ } }, { rw [ein, nat.mod_self, add_zero], exact x_increasing _ (nat.pred_lt $ ne_of_gt npos) } }) (λ (jn : j > n), have lem1 : j ≠ n → xn j % xn n < xn (j + 1) % xn n → xn i % xn n < xn (j + 1) % xn n, from λjn s, (lt_or_eq_of_le (nat.le_of_succ_le_succ ij)).elim (λh, lt_trans (eq_of_xn_modeq_lem3 h (le_of_lt j2n) jn $ λ⟨a1, n1, i0, j2⟩, by rw [n1, j2] at j2n; exact absurd j2n dec_trivial) s) (λh, by rw h; exact s), lem1 (ne_of_gt jn) $ int.lt_of_coe_nat_lt_coe_nat $ by { rw [lem2 j jn (le_of_lt j2n), lem2 (j+1) (nat.le_succ_of_le jn) j2n], refine sub_lt_sub_left (int.coe_nat_lt_coe_nat_of_lt $ x_increasing _ _) _, rw [nat.sub_succ], exact nat.pred_lt (ne_of_gt $ nat.sub_pos_of_lt j2n) }) theorem eq_of_xn_modeq_le {i j n} (npos : n > 0) (ij : i ≤ j) (j2n : j ≤ 2 * n) (h : xn i ≡ xn j [MOD xn n]) (ntriv : ¬(a = 2 ∧ n = 1 ∧ i = 0 ∧ j = 2)) : i = j := (lt_or_eq_of_le ij).resolve_left $ λij', if jn : j = n then by { refine ne_of_gt _ h, rw [jn, nat.mod_self], have x0 : xn a1 0 % xn a1 n > 0 := by rw [nat.mod_eq_of_lt (x_increasing a1 npos)]; exact dec_trivial, cases i with i, exact x0, rw jn at ij', exact lt_trans x0 (eq_of_xn_modeq_lem3 _ npos (nat.succ_pos _) (le_trans ij j2n) (ne_of_lt ij') $ λ⟨a1, n1, _, i2⟩, by rw [n1, i2] at ij'; exact absurd ij' dec_trivial) } else ne_of_lt (eq_of_xn_modeq_lem3 npos ij' j2n jn ntriv) h theorem eq_of_xn_modeq {i j n} (npos : n > 0) (i2n : i ≤ 2 * n) (j2n : j ≤ 2 * n) (h : xn i ≡ xn j [MOD xn n]) (ntriv : a = 2 → n = 1 → (i = 0 → j ≠ 2) ∧ (i = 2 → j ≠ 0)) : i = j := (le_total i j).elim (λij, eq_of_xn_modeq_le npos ij j2n h $ λ⟨a2, n1, i0, j2⟩, (ntriv a2 n1).left i0 j2) (λij, (eq_of_xn_modeq_le npos ij i2n h.symm $ λ⟨a2, n1, j0, i2⟩, (ntriv a2 n1).right i2 j0).symm) theorem eq_of_xn_modeq' {i j n} (ipos : i > 0) (hin : i ≤ n) (j4n : j ≤ 4 * n) (h : xn j ≡ xn i [MOD xn n]) : j = i ∨ j + i = 4 * n := have i2n : i ≤ 2*n, by apply le_trans hin; rw two_mul; apply nat.le_add_left, have npos : n > 0, from lt_of_lt_of_le ipos hin, (le_or_gt j (2 * n)).imp (λj2n : j ≤ 2*n, eq_of_xn_modeq npos j2n i2n h $ λa2 n1, ⟨λj0 i2, by rw [n1, i2] at hin; exact absurd hin dec_trivial, λj2 i0, ne_of_gt ipos i0⟩) (λj2n : j > 2*n, suffices i = 4*n - j, by rw [this, nat.add_sub_of_le j4n], have j42n : 4*n - j ≤ 2*n, from @nat.le_of_add_le_add_right j _ _ $ by rw [nat.sub_add_cancel j4n, show 4*n = 2*n + 2*n, from right_distrib 2 2 n]; exact nat.add_le_add_left (le_of_lt j2n) _, eq_of_xn_modeq npos i2n j42n (h.symm.trans $ let t := xn_modeq_x4n_sub j42n in by rwa [nat.sub_sub_self j4n] at t) (λa2 n1, ⟨λi0, absurd i0 (ne_of_gt ipos), λi2, by rw[n1, i2] at hin; exact absurd hin dec_trivial⟩)) theorem modeq_of_xn_modeq {i j n} (ipos : i > 0) (hin : i ≤ n) (h : xn j ≡ xn i [MOD xn n]) : j ≡ i [MOD 4 * n] ∨ j + i ≡ 0 [MOD 4 * n] := let j' := j % (4 * n) in have n4 : 4 * n > 0, from mul_pos dec_trivial (lt_of_lt_of_le ipos hin), have jl : j' < 4 * n, from nat.mod_lt _ n4, have jj : j ≡ j' [MOD 4 * n], by delta modeq; rw nat.mod_eq_of_lt jl, have ∀j q, xn (j + 4 * n * q) ≡ xn j [MOD xn n], begin intros j q, induction q with q IH, { simp }, rw[nat.mul_succ, ← add_assoc, add_comm], exact modeq.trans (xn_modeq_x4n_add _ _ _) IH end, or.imp (λ(ji : j' = i), by rwa ← ji) (λ(ji : j' + i = 4 * n), (modeq.modeq_add jj (modeq.refl _)).trans $ by rw ji; exact modeq.modeq_zero_iff.2 (dvd_refl _)) (eq_of_xn_modeq' ipos hin (le_of_lt jl) $ (modeq.symm (by rw ← nat.mod_add_div j (4*n); exact this j' _)).trans h) end theorem xy_modeq_of_modeq {a b c} (a1 : a > 1) (b1 : b > 1) (h : a ≡ b [MOD c]) : ∀ n, xn a1 n ≡ xn b1 n [MOD c] ∧ yn a1 n ≡ yn b1 n [MOD c] | 0 := by constructor; refl | 1 := by simp; exact ⟨h, modeq.refl 1⟩ | (n+2) := ⟨ modeq.modeq_add_cancel_right (xy_modeq_of_modeq n).left $ by rw [xn_succ_succ a1, xn_succ_succ b1]; exact modeq.modeq_mul (modeq.modeq_mul_left _ h) (xy_modeq_of_modeq (n+1)).left, modeq.modeq_add_cancel_right (xy_modeq_of_modeq n).right $ by rw [yn_succ_succ a1, yn_succ_succ b1]; exact modeq.modeq_mul (modeq.modeq_mul_left _ h) (xy_modeq_of_modeq (n+1)).right⟩ theorem matiyasevic {a k x y} : (∃ a1 : a > 1, xn a1 k = x ∧ yn a1 k = y) ↔ a > 1 ∧ k ≤ y ∧ (x = 1 ∧ y = 0 ∨ ∃ (u v s t b : ℕ), x * x - (a * a - 1) * y * y = 1 ∧ u * u - (a * a - 1) * v * v = 1 ∧ s * s - (b * b - 1) * t * t = 1 ∧ b > 1 ∧ b ≡ 1 [MOD 4 * y] ∧ b ≡ a [MOD u] ∧ v > 0 ∧ y * y ∣ v ∧ s ≡ x [MOD u] ∧ t ≡ k [MOD 4 * y]) := ⟨λ⟨a1, hx, hy⟩, by rw [← hx, ← hy]; refine ⟨a1, (nat.eq_zero_or_pos k).elim (λk0, by rw k0; exact ⟨le_refl _, or.inl ⟨rfl, rfl⟩⟩) (λkpos, _)⟩; exact let x := xn a1 k, y := yn a1 k, m := 2 * (k * y), u := xn a1 m, v := yn a1 m in have ky : k ≤ y, from yn_ge_n a1 k, have yv : y * y ∣ v, from dvd_trans (ysq_dvd_yy a1 k) $ (y_dvd_iff _ _ _).2 $ dvd_mul_left _ _, have uco : nat.coprime u (4 * y), from have 2 ∣ v, from modeq.modeq_zero_iff.1 $ (yn_modeq_two _ _).trans $ modeq.modeq_zero_iff.2 (dvd_mul_right _ _), have nat.coprime u 2, from (xy_coprime a1 m).coprime_dvd_right this, (this.mul_right this).mul_right $ (xy_coprime _ _).coprime_dvd_right (dvd_of_mul_left_dvd yv), let ⟨b, ba, bm1⟩ := modeq.chinese_remainder uco a 1 in have m1 : 1 < m, from have 0 < k * y, from mul_pos kpos (y_increasing a1 kpos), nat.mul_le_mul_left 2 this, have vp : v > 0, from y_increasing a1 (lt_trans zero_lt_one m1), have b1 : b > 1, from have u > xn a1 1, from x_increasing a1 m1, have u > a, by simp at this; exact this, lt_of_lt_of_le a1 $ by delta modeq at ba; rw nat.mod_eq_of_lt this at ba; rw ← ba; apply nat.mod_le, let s := xn b1 k, t := yn b1 k in have sx : s ≡ x [MOD u], from (xy_modeq_of_modeq b1 a1 ba k).left, have tk : t ≡ k [MOD 4 * y], from have 4 * y ∣ b - 1, from int.coe_nat_dvd.1 $ by rw int.coe_nat_sub (le_of_lt b1); exact modeq.dvd_of_modeq bm1.symm, modeq.modeq_of_dvd_of_modeq this $ yn_modeq_a_sub_one _ _, ⟨ky, or.inr ⟨u, v, s, t, b, pell_eq _ _, pell_eq _ _, pell_eq _ _, b1, bm1, ba, vp, yv, sx, tk⟩⟩, λ⟨a1, ky, o⟩, ⟨a1, match o with | or.inl ⟨x1, y0⟩ := by rw y0 at ky; rw [nat.eq_zero_of_le_zero ky, x1, y0]; exact ⟨rfl, rfl⟩ | or.inr ⟨u, v, s, t, b, xy, uv, st, b1, rem⟩ := match x, y, eq_pell a1 xy, u, v, eq_pell a1 uv, s, t, eq_pell b1 st, rem, ky with | ._, ._, ⟨i, rfl, rfl⟩, ._, ._, ⟨n, rfl, rfl⟩, ._, ._, ⟨j, rfl, rfl⟩, ⟨(bm1 : b ≡ 1 [MOD 4 * yn a1 i]), (ba : b ≡ a [MOD xn a1 n]), (vp : yn a1 n > 0), (yv : yn a1 i * yn a1 i ∣ yn a1 n), (sx : xn b1 j ≡ xn a1 i [MOD xn a1 n]), (tk : yn b1 j ≡ k [MOD 4 * yn a1 i])⟩, (ky : k ≤ yn a1 i) := (nat.eq_zero_or_pos i).elim (λi0, by simp [i0] at ky; rw [i0, ky]; exact ⟨rfl, rfl⟩) $ λipos, suffices i = k, by rw this; exact ⟨rfl, rfl⟩, by clear _x o rem xy uv st _match _match _fun_match; exact have iln : i ≤ n, from le_of_not_gt $ λhin, not_lt_of_ge (nat.le_of_dvd vp (dvd_of_mul_left_dvd yv)) (y_increasing a1 hin), have yd : 4 * yn a1 i ∣ 4 * n, from mul_dvd_mul_left _ $ dvd_of_ysq_dvd a1 yv, have jk : j ≡ k [MOD 4 * yn a1 i], from have 4 * yn a1 i ∣ b - 1, from int.coe_nat_dvd.1 $ by rw int.coe_nat_sub (le_of_lt b1); exact modeq.dvd_of_modeq bm1.symm, (modeq.modeq_of_dvd_of_modeq this (yn_modeq_a_sub_one b1 _)).symm.trans tk, have ki : k + i < 4 * yn a1 i, from lt_of_le_of_lt (add_le_add ky (yn_ge_n a1 i)) $ by rw ← two_mul; exact nat.mul_lt_mul_of_pos_right dec_trivial (y_increasing a1 ipos), have ji : j ≡ i [MOD 4 * n], from have xn a1 j ≡ xn a1 i [MOD xn a1 n], from (xy_modeq_of_modeq b1 a1 ba j).left.symm.trans sx, (modeq_of_xn_modeq a1 ipos iln this).resolve_right $ λ (ji : j + i ≡ 0 [MOD 4 * n]), not_le_of_gt ki $ nat.le_of_dvd (lt_of_lt_of_le ipos $ nat.le_add_left _ _) $ modeq.modeq_zero_iff.1 $ (modeq.modeq_add jk.symm (modeq.refl i)).trans $ modeq.modeq_of_dvd_of_modeq yd ji, by have : i % (4 * yn a1 i) = k % (4 * yn a1 i) := (modeq.modeq_of_dvd_of_modeq yd ji).symm.trans jk; rwa [nat.mod_eq_of_lt (lt_of_le_of_lt (nat.le_add_left _ _) ki), nat.mod_eq_of_lt (lt_of_le_of_lt (nat.le_add_right _ _) ki)] at this end end⟩⟩ lemma eq_pow_of_pell_lem {a y k} (a1 : 1 < a) (ypos : y > 0) : k > 0 → a > y^k → (↑(y^k) : ℤ) < 2*a*y - y*y - 1 := have y < a → 2*a*y ≥ a + (y*y + 1), begin intro ya, induction y with y IH, exact absurd ypos (lt_irrefl _), cases nat.eq_zero_or_pos y with y0 ypos, { rw y0, simp [two_mul], apply add_le_add_left, exact a1 }, { rw [nat.mul_succ, nat.mul_succ, nat.succ_mul y], have : 2 * a ≥ y + nat.succ y, { change y + y < 2 * a, rw ← two_mul, exact mul_lt_mul_of_pos_left (nat.lt_of_succ_lt ya) dec_trivial }, have := add_le_add (IH ypos (nat.lt_of_succ_lt ya)) this, simp at this, simp, exact this } end, λk0 yak, lt_of_lt_of_le (int.coe_nat_lt_coe_nat_of_lt yak) $ by rw sub_sub; apply le_sub_right_of_add_le; apply int.coe_nat_le_coe_nat_of_le; have y1 := nat.pow_le_pow_of_le_right ypos k0; simp at y1; exact this (lt_of_le_of_lt y1 yak) theorem eq_pow_of_pell {m n k} : (n^k = m ↔ k = 0 ∧ m = 1 ∨ k > 0 ∧ (n = 0 ∧ m = 0 ∨ n > 0 ∧ ∃ (w a t z : ℕ) (a1 : a > 1), xn a1 k ≡ yn a1 k * (a - n) + m [MOD t] ∧ 2 * a * n = t + (n * n + 1) ∧ m < t ∧ n ≤ w ∧ k ≤ w ∧ a * a - ((w + 1) * (w + 1) - 1) * (w * z) * (w * z) = 1)) := ⟨λe, by rw ← e; refine (nat.eq_zero_or_pos k).elim (λk0, by rw k0; exact or.inl ⟨rfl, rfl⟩) (λkpos, or.inr ⟨kpos, _⟩); refine (nat.eq_zero_or_pos n).elim (λn0, by rw [n0, nat.zero_pow kpos]; exact or.inl ⟨rfl, rfl⟩) (λnpos, or.inr ⟨npos, _⟩); exact let w := _root_.max n k in have nw : n ≤ w, from le_max_left _ _, have kw : k ≤ w, from le_max_right _ _, have wpos : w > 0, from lt_of_lt_of_le npos nw, have w1 : w + 1 > 1, from nat.succ_lt_succ wpos, let a := xn w1 w in have a1 : a > 1, from x_increasing w1 wpos, let x := xn a1 k, y := yn a1 k in let ⟨z, ze⟩ := show w ∣ yn w1 w, from modeq.modeq_zero_iff.1 $ modeq.trans (yn_modeq_a_sub_one w1 w) (modeq.modeq_zero_iff.2 $ dvd_refl _) in have nt : (↑(n^k) : ℤ) < 2 * a * n - n * n - 1, from eq_pow_of_pell_lem a1 npos kpos $ calc n^k ≤ n^w : nat.pow_le_pow_of_le_right npos kw ... < (w + 1)^w : nat.pow_lt_pow_of_lt_left (nat.lt_succ_of_le nw) wpos ... ≤ a : xn_ge_a_pow w1 w, let ⟨t, te⟩ := int.eq_coe_of_zero_le $ le_trans (int.coe_zero_le _) $ le_of_lt nt in have na : n ≤ a, from le_trans nw $ le_of_lt $ n_lt_xn w1 w, have tm : x ≡ y * (a - n) + n^k [MOD t], begin apply modeq.modeq_of_dvd, rw [int.coe_nat_add, int.coe_nat_mul, int.coe_nat_sub na, ← te], exact x_sub_y_dvd_pow a1 n k end, have ta : 2 * a * n = t + (n * n + 1), from int.coe_nat_inj $ by rw [int.coe_nat_add, ← te, sub_sub]; repeat {rw int.coe_nat_add <|> rw int.coe_nat_mul}; rw [int.coe_nat_one, sub_add_cancel]; refl, have mt : n^k < t, from int.lt_of_coe_nat_lt_coe_nat $ by rw ← te; exact nt, have zp : a * a - ((w + 1) * (w + 1) - 1) * (w * z) * (w * z) = 1, by rw ← ze; exact pell_eq w1 w, ⟨w, a, t, z, a1, tm, ta, mt, nw, kw, zp⟩, λo, match o with | or.inl ⟨k0, m1⟩ := by rw [k0, m1]; refl | or.inr ⟨kpos, or.inl ⟨n0, m0⟩⟩ := by rw [n0, m0, nat.zero_pow kpos] | or.inr ⟨kpos, or.inr ⟨npos, w, a, t, z, (a1 : a > 1), (tm : xn a1 k ≡ yn a1 k * (a - n) + m [MOD t]), (ta : 2 * a * n = t + (n * n + 1)), (mt : m < t), (nw : n ≤ w), (kw : k ≤ w), (zp : a * a - ((w + 1) * (w + 1) - 1) * (w * z) * (w * z) = 1)⟩⟩ := have wpos : w > 0, from lt_of_lt_of_le npos nw, have w1 : w + 1 > 1, from nat.succ_lt_succ wpos, let ⟨j, xj, yj⟩ := eq_pell w1 zp in by clear _match o _let_match; exact have jpos : j > 0, from (nat.eq_zero_or_pos j).resolve_left $ λj0, have a1 : a = 1, by rw j0 at xj; exact xj, have 2 * n = t + (n * n + 1), by rw a1 at ta; exact ta, have n1 : n = 1, from have n * n < n * 2, by rw [mul_comm n 2, this]; apply nat.le_add_left, have n ≤ 1, from nat.le_of_lt_succ $ lt_of_mul_lt_mul_left this (nat.zero_le _), le_antisymm this npos, by rw n1 at this; rw ← @nat.add_right_cancel 0 2 t this at mt; exact nat.not_lt_zero _ mt, have wj : w ≤ j, from nat.le_of_dvd jpos $ modeq.modeq_zero_iff.1 $ (yn_modeq_a_sub_one w1 j).symm.trans $ modeq.modeq_zero_iff.2 ⟨z, yj.symm⟩, have nt : (↑(n^k) : ℤ) < 2 * a * n - n * n - 1, from eq_pow_of_pell_lem a1 npos kpos $ calc n^k ≤ n^j : nat.pow_le_pow_of_le_right npos (le_trans kw wj) ... < (w + 1)^j : nat.pow_lt_pow_of_lt_left (nat.lt_succ_of_le nw) jpos ... ≤ xn w1 j : xn_ge_a_pow w1 j ... = a : xj.symm, have na : n ≤ a, by rw xj; exact le_trans (le_trans nw wj) (le_of_lt $ n_lt_xn _ _), have te : (t : ℤ) = 2 * ↑a * ↑n - ↑n * ↑n - 1, by rw sub_sub; apply eq_sub_of_add_eq; apply (int.coe_nat_eq_coe_nat_iff _ _).2; exact ta.symm, have xn a1 k ≡ yn a1 k * (a - n) + n^k [MOD t], by have := x_sub_y_dvd_pow a1 n k; rw [← te, ← int.coe_nat_sub na] at this; exact modeq.modeq_of_dvd this, have n^k % t = m % t, from modeq.modeq_add_cancel_left (modeq.refl _) (this.symm.trans tm), by rw ← te at nt; rwa [nat.mod_eq_of_lt (int.lt_of_coe_nat_lt_coe_nat nt), nat.mod_eq_of_lt mt] at this end⟩ end pell
37a4542b822bac58de4c8e3c5c9789d72fa9a65a
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/group_theory/free_abelian_group_finsupp.lean
efc48541b31a21a8e455a6fc85177cff4a9285b7
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,464
lean
/- Copyright (c) 2021 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import group_theory.free_abelian_group import data.finsupp.basic /-! # Isomorphism between `free_abelian_group X` and `X →₀ ℤ` In this file we construct the canonical isomorphism between `free_abelian_group X` and `X →₀ ℤ`. We use this to transport the notion of `support` from `finsupp` to `free_abelian_group`. ## Main declarations - `free_abelian_group.equiv_finsupp`: group isomorphism between `free_abelian_group X` and `X →₀ ℤ` - `free_abelian_group.coeff`: the multiplicity of `x : X` in `a : free_abelian_group X` - `free_abelian_group.support`: the finset of `x : X` that occur in `a : free_abelian_group X` -/ noncomputable theory open_locale big_operators variables {X : Type*} /-- The group homomorphism `free_abelian_group X →+ (X →₀ ℤ)`. -/ def free_abelian_group.to_finsupp : free_abelian_group X →+ (X →₀ ℤ) := free_abelian_group.lift $ λ x, finsupp.single x (1 : ℤ) /-- The group homomorphism `(X →₀ ℤ) →+ free_abelian_group X`. -/ def finsupp.to_free_abelian_group : (X →₀ ℤ) →+ free_abelian_group X := finsupp.lift_add_hom $ λ x, (smul_add_hom ℤ (free_abelian_group X)).flip (free_abelian_group.of x) open finsupp free_abelian_group @[simp] lemma finsupp.to_free_abelian_group_comp_single_add_hom (x : X) : finsupp.to_free_abelian_group.comp (finsupp.single_add_hom x) = (smul_add_hom ℤ (free_abelian_group X)).flip (of x) := begin ext, simp only [add_monoid_hom.coe_comp, finsupp.single_add_hom_apply, function.comp_app, one_smul, to_free_abelian_group, finsupp.lift_add_hom_apply_single] end @[simp] lemma free_abelian_group.to_finsupp_comp_to_free_abelian_group : to_finsupp.comp to_free_abelian_group = add_monoid_hom.id (X →₀ ℤ) := begin ext x y, simp only [add_monoid_hom.id_comp], rw [add_monoid_hom.comp_assoc, finsupp.to_free_abelian_group_comp_single_add_hom], simp only [to_finsupp, add_monoid_hom.coe_comp, finsupp.single_add_hom_apply, function.comp_app, one_smul, lift.of, add_monoid_hom.flip_apply, smul_add_hom_apply, add_monoid_hom.id_apply], end @[simp] lemma finsupp.to_free_abelian_group_comp_to_finsupp : to_free_abelian_group.comp to_finsupp = add_monoid_hom.id (free_abelian_group X) := begin ext, rw [to_free_abelian_group, to_finsupp, add_monoid_hom.comp_apply, lift.of, lift_add_hom_apply_single, add_monoid_hom.flip_apply, smul_add_hom_apply, one_smul, add_monoid_hom.id_apply], end @[simp] lemma finsupp.to_free_abelian_group_to_finsupp {X} (x : free_abelian_group X) : x.to_finsupp.to_free_abelian_group = x := by rw [← add_monoid_hom.comp_apply, finsupp.to_free_abelian_group_comp_to_finsupp, add_monoid_hom.id_apply] namespace free_abelian_group open finsupp variable {X} @[simp] lemma to_finsupp_of (x : X) : to_finsupp (of x) = finsupp.single x 1 := by simp only [to_finsupp, lift.of] @[simp] lemma to_finsupp_to_free_abelian_group (f : X →₀ ℤ) : f.to_free_abelian_group.to_finsupp = f := by rw [← add_monoid_hom.comp_apply, to_finsupp_comp_to_free_abelian_group, add_monoid_hom.id_apply] variable (X) /-- The additive equivalence between `free_abelian_group X` and `(X →₀ ℤ)`. -/ @[simps] def equiv_finsupp : free_abelian_group X ≃+ (X →₀ ℤ) := { to_fun := to_finsupp, inv_fun := to_free_abelian_group, left_inv := to_free_abelian_group_to_finsupp, right_inv := to_finsupp_to_free_abelian_group, map_add' := to_finsupp.map_add } variable {X} /-- `coeff x` is the additive group homomorphism `free_abelian_group X →+ ℤ` that sends `a` to the multiplicity of `x : X` in `a`. -/ def coeff (x : X) : free_abelian_group X →+ ℤ := (finsupp.apply_add_hom x).comp to_finsupp /-- `support a` for `a : free_abelian_group X` is the finite set of `x : X` that occur in the formal sum `a`. -/ def support (a : free_abelian_group X) : finset X := a.to_finsupp.support lemma mem_support_iff (x : X) (a : free_abelian_group X) : x ∈ a.support ↔ coeff x a ≠ 0 := by { rw [support, finsupp.mem_support_iff], exact iff.rfl } lemma not_mem_support_iff (x : X) (a : free_abelian_group X) : x ∉ a.support ↔ coeff x a = 0 := by { rw [support, finsupp.not_mem_support_iff], exact iff.rfl } @[simp] lemma support_zero : support (0 : free_abelian_group X) = ∅ := by simp only [support, finsupp.support_zero, add_monoid_hom.map_zero] @[simp] lemma support_of (x : X) : support (of x) = {x} := by simp only [support, to_finsupp_of, finsupp.support_single_ne_zero (one_ne_zero)] @[simp] lemma support_neg (a : free_abelian_group X) : support (-a) = support a := by simp only [support, add_monoid_hom.map_neg, finsupp.support_neg] @[simp] lemma support_gsmul (k : ℤ) (h : k ≠ 0) (a : free_abelian_group X) : support (k • a) = support a := begin ext x, simp only [mem_support_iff, add_monoid_hom.map_gsmul], simp only [h, gsmul_int_int, false_or, ne.def, mul_eq_zero] end @[simp] lemma support_nsmul (k : ℕ) (h : k ≠ 0) (a : free_abelian_group X) : support (k • a) = support a := by { apply support_gsmul k _ a, exact_mod_cast h } open_locale classical lemma support_add (a b : free_abelian_group X) : (support (a + b)) ⊆ a.support ∪ b.support := begin simp only [support, add_monoid_hom.map_add], apply finsupp.support_add end end free_abelian_group
3f1d3e599b54092f45dc22a8875a21380c1c0f91
33340b3a23ca62ef3c8a7f6a2d4e14c07c6d3354
/lia/nnf.lean
78b5b4beacfe62eb1a125ce20d322b52314592a1
[]
no_license
lclem/cooper
79554e72ced343c64fed24b2d892d24bf9447dfe
812afc6b158821f2e7dac9c91d3b6123c7a19faf
refs/heads/master
1,607,554,257,488
1,578,694,133,000
1,578,694,133,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,746
lean
import ..logic .qfree .wf .remove_neg -- Requires : nqfree arg -- Ensures : nqfree ret def push_neg : formula → formula | ⊤' := ⊥' | ⊥' := ⊤' | (A' a) := A' (remove_neg a) | (¬' p) := ⊥' | (p ∨' q) := push_neg p ∧' push_neg q | (p ∧' q) := push_neg p ∨' push_neg q | (∃' p) := ⊥' lemma nqfree_push_neg : ∀ {p : formula}, nqfree p → nqfree (push_neg p) | ⊤' h := trivial | ⊥' h := trivial | (A' a) h := trivial | (¬' p) h := by cases h | (p ∨' q) h := begin cases h, constructor; apply nqfree_push_neg; assumption end | (p ∧' q) h := begin cases h, constructor; apply nqfree_push_neg; assumption end | (∃' p) h := by cases h -- Requires : qfree arg -- Ensures : nqfree ret def nnf : formula → formula | (formula.true) := ⊤' | (formula.false) := ⊥' | (formula.atom a) := A' a | (formula.not p) := push_neg (nnf p) | (formula.or p q) := formula.or (nnf p) (nnf q) | (formula.and p q) := formula.and (nnf p) (nnf q) | (formula.ex p) := ⊥' lemma nqfree_nnf : ∀ {p : formula}, qfree p → nqfree (nnf p) | ⊤' _ := trivial | ⊥' _ := trivial | (A' _) _ := trivial | (¬' p) h := begin apply nqfree_push_neg (nqfree_nnf _), apply h end | (p ∧' q) h := begin cases h, simp [nnf], constructor; apply nqfree_nnf; assumption end | (p ∨' q) h := begin cases h, simp [nnf], constructor; apply nqfree_nnf; assumption end lemma wf_push_neg : ∀ {p : formula}, nqfree p → p.wf → (push_neg p).wf | ⊤' h1 h2 := trivial | ⊥' h1 h2 := trivial | (A' a) h1 h2 := begin apply wf_remove_neg, apply h2 end | (¬' p) h1 h2 := by cases h1 | (p ∨' q) h1 h2 := begin cases h1, cases h2, constructor; apply wf_push_neg; assumption end | (p ∧' q) h1 h2 := begin cases h1, cases h2, constructor; apply wf_push_neg; assumption end | (∃' p) h1 h2 := by cases h1 lemma wf_nnf : ∀ {p : formula}, qfree p → p.wf → (nnf p).wf | ⊤' h1 h2 := trivial | ⊥' h1 h2 := trivial | (A' a) h1 h2 := h2 | (¬' p) h1 h2 := begin apply wf_push_neg (nqfree_nnf _), apply wf_nnf, apply h1, apply h2, apply h1 end | (p ∨' q) h1 h2 := begin cases h1, cases h2, constructor; apply wf_nnf; assumption end | (p ∧' q) h1 h2 := begin cases h1, cases h2, constructor; apply wf_nnf; assumption end | (∃' p) h1 h2 := by cases h1 lemma eval_push_neg : ∀ {p : formula}, nqfree p → ∀ {xs : list znum}, (push_neg p).eval xs ↔ ¬ (p.eval xs) | ⊤' h xs := begin simp [push_neg, formula.eval] end | ⊥' h xs := begin simp [push_neg, formula.eval] end | (A' a) h xs := begin simp [push_neg, formula.eval], apply eval_remove_neg, end | (p ∧' q) h xs := begin cases h, simp only [push_neg, formula.eval, eval_not_and], rw (@not_and_distrib _ _ (classical.dec _)), apply or_iff_or; apply eval_push_neg; assumption end | (p ∨' q) h xs := begin cases h, simp only [push_neg, formula.eval, not_or_distrib], apply and_iff_and; apply eval_push_neg; assumption end lemma eval_nnf : ∀ {p : formula}, qfree p → ∀ {xs : list znum}, ((nnf p).eval xs ↔ p.eval xs) | ⊤' hf xs := iff.refl _ | ⊥' hf xs := iff.refl _ | (A' a) h xs := iff.refl _ | (¬' p) h xs := begin simp [nnf, formula.eval], rw eval_push_neg (nqfree_nnf _), rw eval_nnf, apply h, apply h end | (p ∧' q) hf xs := begin cases hf, simp [nnf, formula.eval], apply and_iff_and; apply eval_nnf; assumption end | (p ∨' q) hf xs := begin cases hf, simp [nnf, formula.eval], apply or_iff_or; apply eval_nnf; assumption end #exit def nnf : formula → formula | (formula.true) := formula.true | (formula.false) := formula.false | (formula.atom a) := formula.atom a | (formula.not (formula.true)) := formula.false | (formula.not (formula.false)) := formula.true | (formula.not (formula.atom a)) := formula.atom (remove_neg a) | (formula.not (formula.not p)) := nnf p | (formula.not (formula.or p q)) := formula.and (nnf p.not) (nnf q.not) | (formula.not (formula.and p q)) := formula.or (nnf p.not) (nnf q.not) | (formula.not (formula.ex p)) := formula.false | (formula.or p q) := formula.or (nnf p) (nnf q) | (formula.and p q) := formula.and (nnf p) (nnf q) | (formula.ex p) := formula.false | (formula.true) _ _ := and.intro trivial trivial | (formula.false) _ _ := and.intro trivial trivial | (formula.atom a) _ h := begin simp [nnf, formula.wf] at *, apply and.intro h, apply wf_remove_neg h, end | (¬' p) hf hn := begin cases (@wf_nnf_core p hf hn), simp [nnf], constructor; assumption end | (formula.and p q) hf hn := by wf_nnf_core_tac | (formula.or p q) hf hn := by wf_nnf_core_tac #exit -- Requires : nqfree arg -- Ensures : nqfree ret meta def nqfree_nnf_core_aux := `[ cases h with hp hq, simp [nnf, nqfree], cases (nqfree_nnf_core hp), cases (nqfree_nnf_core hq), constructor; constructor; assumption ] lemma nqfree_nnf_core : ∀ {p : formula}, qfree p → (nqfree (nnf p) ∧ nqfree (nnf ¬' p)) | ⊤' _ := and.intro trivial trivial | ⊥' _ := and.intro trivial trivial | (A' _) _ := begin constructor; simp [nnf] end | (¬' p) h := begin cases (@nqfree_nnf_core p h), simp [nnf], constructor; assumption, end | (p ∧' q) h := by nqfree_nnf_core_aux | (p ∨' q) h := by nqfree_nnf_core_aux lemma nqfree_nnf : ∀ {p : formula}, qfree p → nqfree (nnf p) := λ p h, (nqfree_nnf_core h).left meta def wf_nnf_core_tac := `[ cases hf with hfp hfq, cases hn with hnp hnq, simp [formula.wf, nnf], cases (@wf_nnf_core p hfp hnp), cases (@wf_nnf_core q hfq hnq), constructor; constructor; assumption ] lemma wf_nnf_core : ∀ {p : formula}, qfree p → p.wf → (nnf p).wf ∧ (nnf ¬' p).wf | (formula.true) _ _ := and.intro trivial trivial | (formula.false) _ _ := and.intro trivial trivial | (formula.atom a) _ h := begin simp [nnf, formula.wf] at *, apply and.intro h, apply wf_remove_neg h, end | (¬' p) hf hn := begin cases (@wf_nnf_core p hf hn), simp [nnf], constructor; assumption end | (formula.and p q) hf hn := by wf_nnf_core_tac | (formula.or p q) hf hn := by wf_nnf_core_tac lemma wf_nnf {p : formula} (hf : qfree p) (hn : p.wf) : (nnf p).wf := (wf_nnf_core hf hn)^.elim_left open formula lemma eval_nnf_core : ∀ {p : formula}, qfree p → ∀ {xs : list znum}, (eval xs (nnf p) ↔ eval xs p) ∧ (eval xs (nnf (¬' p)) ↔ eval xs (¬' p)) | ⊤' hf xs := and.intro (by refl) (by {simp [nnf, formula.eval]}) | ⊥' hf xs := and.intro (by refl) (by {simp [nnf, formula.eval]}) | (A' a) hf xs := begin apply and.intro; simp [nnf, formula.eval], apply eval_remove_neg, end | (¬' p) hf xs := begin cases (@eval_nnf_core p hf xs), constructor, assumption, simp [nnf, eval_not, left, not_not_iff], end | (p ∧' q) hf xs := begin cases (@eval_nnf_core _ hf.left xs), cases (@eval_nnf_core _ hf.right xs), simp [nnf, eval_and], constructor; [ {apply and_iff_and; assumption}, { rw [eval_not, eval_and, @not_and_distrib _ _ (classical.dec _)], simp [eval_or], apply or_iff_or; assumption } ] end | (p ∨' q) hf xs := begin cases (@eval_nnf_core _ hf.left xs), cases (@eval_nnf_core _ hf.right xs), simp [nnf, eval_and], constructor; [ {apply or_iff_or; assumption}, { rw [eval_not, eval_or, not_or_distrib], apply and_iff_and; assumption } ] end lemma eval_nnf {p : formula} (h : qfree p) {xs : list znum} : (nnf p).eval xs ↔ p.eval xs := (eval_nnf_core h)^.elim_left
f23c90619427f520376940f72963121b125f2ddb
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/category_theory/simple.lean
dca21bd57d54f3c5b8fab84945ad628497590610
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,275
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel, Scott Morrison -/ import category_theory.limits.shapes.zero import category_theory.limits.shapes.kernels import category_theory.abelian.basic /-! # Simple objects We define simple objects in any category with zero morphisms. A simple object is an object `Y` such that any monomorphism `f : X ⟶ Y` is either an isomorphism or zero (but not both). This is formalized as a `Prop` valued typeclass `simple X`. If a morphism `f` out of a simple object is nonzero and has a kernel, then that kernel is zero. (We state this as `kernel.ι f = 0`, but should add `kernel f ≅ 0`.) When the category is abelian, being simple is the same as being cosimple (although we do not state a separate typeclass for this). As a consequence, any nonzero epimorphism out of a simple object is an isomorphism, and any nonzero morphism into a simple object has trivial cokernel. -/ noncomputable theory open category_theory.limits namespace category_theory universes v u variables {C : Type u} [category.{v} C] section variables [has_zero_morphisms C] /-- An object is simple if monomorphisms into it are (exclusively) either isomorphisms or zero. -/ class simple (X : C) : Prop := (mono_is_iso_iff_nonzero : ∀ {Y : C} (f : Y ⟶ X) [mono f], is_iso f ↔ (f ≠ 0)) /-- A nonzero monomorphism to a simple object is an isomorphism. -/ lemma is_iso_of_mono_of_nonzero {X Y : C} [simple Y] {f : X ⟶ Y} [mono f] (w : f ≠ 0) : is_iso f := (simple.mono_is_iso_iff_nonzero f).mpr w lemma kernel_zero_of_nonzero_from_simple {X Y : C} [simple X] {f : X ⟶ Y} [has_kernel f] (w : f ≠ 0) : kernel.ι f = 0 := begin classical, by_contradiction h, haveI := is_iso_of_mono_of_nonzero h, exact w (eq_zero_of_epi_kernel f), end lemma mono_to_simple_zero_of_not_iso {X Y : C} [simple Y] {f : X ⟶ Y} [mono f] (w : is_iso f → false) : f = 0 := begin classical, by_contradiction h, apply w, exact is_iso_of_mono_of_nonzero h, end lemma id_nonzero (X : C) [simple.{v} X] : 𝟙 X ≠ 0 := (simple.mono_is_iso_iff_nonzero (𝟙 X)).mp (by apply_instance) section variable [has_zero_object C] local attribute [instance] has_zero_object.has_zero /-- We don't want the definition of 'simple' to include the zero object, so we check that here. -/ lemma zero_not_simple [simple (0 : C)] : false := (simple.mono_is_iso_iff_nonzero (0 : (0 : C) ⟶ (0 : C))).mp ⟨⟨0, by tidy⟩⟩ rfl end end -- We next make the dual arguments, but for this we must be in an abelian category. section abelian variables [abelian C] /-- In an abelian category, an object satisfying the dual of the definition of a simple object is simple. -/ lemma simple_of_cosimple (X : C) (h : ∀ {Z : C} (f : X ⟶ Z) [epi f], is_iso f ↔ (f ≠ 0)) : simple X := ⟨λ Y f I, begin classical, fsplit, { introsI, have hx := cokernel.π_of_epi f, by_contradiction h, push_neg at h, substI h, exact (h _).mp (cokernel.π_of_zero _ _) hx }, { intro hf, suffices : epi f, { resetI, apply abelian.is_iso_of_mono_of_epi }, apply preadditive.epi_of_cokernel_zero, by_contradiction h', exact cokernel_not_iso_of_nonzero hf ((h _).mpr h') } end⟩ /-- A nonzero epimorphism from a simple object is an isomorphism. -/ lemma is_iso_of_epi_of_nonzero {X Y : C} [simple X] {f : X ⟶ Y} [epi f] (w : f ≠ 0) : is_iso f := begin -- `f ≠ 0` means that `kernel.ι f` is not an iso, and hence zero, and hence `f` is a mono. haveI : mono f := preadditive.mono_of_kernel_zero (mono_to_simple_zero_of_not_iso (kernel_not_iso_of_nonzero w)), exact abelian.is_iso_of_mono_of_epi f, end lemma cokernel_zero_of_nonzero_to_simple {X Y : C} [simple Y] {f : X ⟶ Y} [has_cokernel f] (w : f ≠ 0) : cokernel.π f = 0 := begin classical, by_contradiction h, haveI := is_iso_of_epi_of_nonzero h, exact w (eq_zero_of_mono_cokernel f), end lemma epi_from_simple_zero_of_not_iso {X Y : C} [simple X] {f : X ⟶ Y} [epi f] (w : is_iso f → false) : f = 0 := begin classical, by_contradiction h, apply w, exact is_iso_of_epi_of_nonzero h, end end abelian end category_theory
199b76866cb557c79d9e6c039d783c61782d207a
07c6143268cfb72beccd1cc35735d424ebcb187b
/src/measure_theory/set_integral.lean
dc105caec272b74659812172ed192d65e29e324c
[ "Apache-2.0" ]
permissive
khoek/mathlib
bc49a842910af13a3c372748310e86467d1dc766
aa55f8b50354b3e11ba64792dcb06cccb2d8ee28
refs/heads/master
1,588,232,063,837
1,587,304,803,000
1,587,304,803,000
176,688,517
0
0
Apache-2.0
1,553,070,585,000
1,553,070,585,000
null
UTF-8
Lean
false
false
14,974
lean
/- Copyright (c) 2020 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import measure_theory.bochner_integration import measure_theory.indicator_function import measure_theory.lebesgue_measure /-! # Set integral Integrate a function over a subset of a measure space. ## Main definitions `measurable_on`, `integrable_on`, `integral_on` ## Notation `∫ a in s, f a` is `measure_theory.integral (s.indicator f)` -/ noncomputable theory open set filter topological_space measure_theory measure_theory.simple_func open_locale classical topological_space interval universes u v w variables {α : Type u} {β : Type v} {γ : Type w} section measurable_on variables [measurable_space α] [measurable_space β] [has_zero β] {s : set α} {f : α → β} /-- `measurable_on s f` means `f` is measurable over the set `s`. -/ def measurable_on (s : set α) (f : α → β) : Prop := measurable (s.indicator f) @[simp] lemma measurable_on_empty (f : α → β) : measurable_on ∅ f := by { rw [measurable_on, indicator_empty], exact measurable_const } @[simp] lemma measurable.measurable_on_univ (hf : measurable f) : measurable_on univ f := hf.if is_measurable.univ measurable_const @[simp] lemma measurable_on_singleton {α} [topological_space α] [t1_space α] [measurable_space α] [opens_measurable_space α] {a : α} {f : α → β} : measurable_on {a} f := λ s hs, show is_measurable ((indicator {a} f)⁻¹' s), begin rw indicator_preimage, refine is_measurable.union _ (is_measurable_singleton.compl.inter $ measurable_const.preimage hs), by_cases h : a ∈ f⁻¹' s, { rw inter_eq_self_of_subset_left, { exact is_measurable_singleton }, rwa singleton_subset_iff }, rw [singleton_inter_eq_empty.2 h], exact is_measurable.empty end lemma is_measurable.inter_preimage {B : set β} (hs : is_measurable s) (hB : is_measurable B) (hf : measurable_on s f): is_measurable (s ∩ f ⁻¹' B) := begin replace hf : is_measurable ((indicator s f)⁻¹' B) := hf B hB, rw indicator_preimage at hf, replace hf := hf.diff _, rwa union_diff_cancel_right at hf, { assume a, simp {contextual := tt} }, exact hs.compl.inter (measurable_const.preimage hB) end lemma measurable.measurable_on (hs : is_measurable s) (hf : measurable f) : measurable_on s f := hf.if hs measurable_const lemma measurable_on.subset {t : set α} (hs : is_measurable s) (h : s ⊆ t) (hf : measurable_on t f) : measurable_on s f := begin have : measurable_on s (indicator t f) := measurable.measurable_on hs hf, simp only [measurable_on, indicator_indicator] at this, rwa [inter_eq_self_of_subset_left h] at this end lemma measurable_on.union {t : set α} {f : α → β} (hs : is_measurable s) (ht : is_measurable t) (hsm : measurable_on s f) (htm : measurable_on t f) : measurable_on (s ∪ t) f := begin assume B hB, show is_measurable ((indicator (s ∪ t) f)⁻¹' B), rw indicator_preimage, refine is_measurable.union _ ((hs.union ht).compl.inter (measurable_const.preimage hB)), simp only [union_inter_distrib_right], exact (hs.inter_preimage hB hsm).union (ht.inter_preimage hB htm) end end measurable_on section integrable_on variables [measure_space α] [normed_group β] {s t : set α} {f g : α → β} /-- `integrable_on s f` means `f` is integrable over the set `s`. -/ def integrable_on (s : set α) (f : α → β) : Prop := integrable (s.indicator f) lemma integrable_on_congr (h : ∀x, x ∈ s → f x = g x) : integrable_on s f ↔ integrable_on s g := by simp only [integrable_on, indicator_congr h] lemma integrable_on_congr_ae (h : ∀ₘ x, x ∈ s → f x = g x) : integrable_on s f ↔ integrable_on s g := by { apply integrable_congr_ae, exact indicator_congr_ae h } @[simp] lemma integrable_on_empty (f : α → β) : integrable_on ∅ f := by { simp only [integrable_on, indicator_empty], apply integrable_zero } lemma measure_theory.integrable.integrable_on (s : set α) (hf : integrable f) : integrable_on s f := by { refine integrable_of_le (λa, _) hf, apply norm_indicator_le_norm_self } lemma integrable_on.subset (h : s ⊆ t) : integrable_on t f → integrable_on s f := by { apply integrable_of_le_ae, filter_upwards [] norm_indicator_le_of_subset h _ } variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] lemma integrable_on.smul (s : set α) (c : 𝕜) {f : α → β} : integrable_on s f → integrable_on s (λa, c • f a) := by { simp only [integrable_on, indicator_smul], apply integrable.smul } lemma integrable_on.mul_left (s : set α) (r : ℝ) {f : α → ℝ} (hf : integrable_on s f) : integrable_on s (λa, r * f a) := by { simp only [smul_eq_mul.symm], exact hf.smul s r } lemma integrable_on.mul_right (s : set α) (r : ℝ) {f : α → ℝ} (hf : integrable_on s f) : integrable_on s (λa, f a * r) := by { simp only [mul_comm], exact hf.mul_left _ _ } lemma integrable_on.divide (s : set α) (r : ℝ) {f : α → ℝ} (hf : integrable_on s f) : integrable_on s (λa, f a / r) := by { simp only [div_eq_mul_inv], exact hf.mul_right _ _ } lemma integrable_on.add [measurable_space β] [opens_measurable_space β] (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) : integrable_on s (λa, f a + g a) := by { rw [integrable_on, indicator_add], exact hfi.add hfm hgm hgi } lemma integrable_on.neg (hf : integrable_on s f) : integrable_on s (λa, -f a) := by { rw [integrable_on, indicator_neg], exact hf.neg } lemma integrable_on.sub [measurable_space β] [opens_measurable_space β] (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) : integrable_on s (λa, f a - g a) := by { rw [integrable_on, indicator_sub], exact hfi.sub hfm hgm hgi } lemma integrable_on.union [measurable_space β] [opens_measurable_space β] (hs : is_measurable s) (ht : is_measurable t) (hsm : measurable_on s f) (hsi : integrable_on s f) (htm : measurable_on t f) (hti : integrable_on t f) : integrable_on (s ∪ t) f := begin rw ← union_diff_self, rw [integrable_on, indicator_union_of_disjoint], { refine integrable.add hsm hsi (htm.subset _ _) (hti.subset _), { exact ht.diff hs }, { exact diff_subset _ _ }, { exact diff_subset _ _ } }, exact disjoint_diff end lemma integrable_on_norm_iff (s : set α) (f : α → β) : integrable_on s (λa, ∥f a∥) ↔ integrable_on s f := begin simp only [integrable_on], convert ← integrable_norm_iff (indicator s f), funext, apply norm_indicator_eq_indicator_norm end end integrable_on section integral_on variables [measure_space α] [normed_group β] [second_countable_topology β] [normed_space ℝ β] [complete_space β] [measurable_space β] [borel_space β] {s t : set α} {f g : α → β} open set notation `∫` binders ` in ` s `, ` r:(scoped f, measure_theory.integral (set.indicator s f)) := r lemma integral_on_undef (h : ¬ (measurable_on s f ∧ integrable_on s f)) : (∫ a in s, f a) = 0 := integral_undef h lemma integral_on_non_measurable (h : ¬ measurable_on s f) : (∫ a in s, f a) = 0 := integral_non_measurable h lemma integral_on_non_integrable (h : ¬ integrable_on s f) : (∫ a in s, f a) = 0 := integral_non_integrable h variables (β) lemma integral_on_zero (s : set α) : (∫ a in s, (0:β)) = 0 := by simp variables {β} lemma integral_on_congr (h : ∀ a ∈ s, f a = g a) : (∫ a in s, f a) = (∫ a in s, g a) := by simp only [indicator_congr h] lemma integral_on_congr_of_ae_eq (hf : measurable_on s f) (hg : measurable_on s g) (h : ∀ₘ a, a ∈ s → f a = g a) : (∫ a in s, f a) = (∫ a in s, g a) := integral_congr_ae hf hg (indicator_congr_ae h) lemma integral_on_congr_of_set (hsm : measurable_on s f) (htm : measurable_on t f) (h : ∀ₘ a, a ∈ s ↔ a ∈ t) : (∫ a in s, f a) = (∫ a in t, f a) := integral_congr_ae hsm htm $ indicator_congr_of_set h variables (s t) lemma integral_on_smul (r : ℝ) (f : α → β) : (∫ a in s, r • (f a)) = r • (∫ a in s, f a) := by rw [← integral_smul, indicator_smul] lemma integral_on_mul_left (r : ℝ) (f : α → ℝ) : (∫ a in s, r * (f a)) = r * (∫ a in s, f a) := integral_on_smul s r f lemma integral_on_mul_right (r : ℝ) (f : α → ℝ) : (∫ a in s, (f a) * r) = (∫ a in s, f a) * r := by { simp only [mul_comm], exact integral_on_mul_left s r f } lemma integral_on_div (r : ℝ) (f : α → ℝ) : (∫ a in s, (f a) / r) = (∫ a in s, f a) / r := by { simp only [div_eq_mul_inv], apply integral_on_mul_right } lemma integral_on_neg (f : α → β) : (∫ a in s, -f a) = - (∫ a in s, f a) := by { simp only [indicator_neg], exact integral_neg _ } variables {s t} lemma integral_on_add {s : set α} (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) : (∫ a in s, f a + g a) = (∫ a in s, f a) + (∫ a in s, g a) := by { simp only [indicator_add], exact integral_add hfm hfi hgm hgi } lemma integral_on_sub (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) : (∫ a in s, f a - g a) = (∫ a in s, f a) - (∫ a in s, g a) := by { simp only [indicator_sub], exact integral_sub hfm hfi hgm hgi } lemma integral_on_le_integral_on_ae {f g : α → ℝ} (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) (h : ∀ₘ a, a ∈ s → f a ≤ g a) : (∫ a in s, f a) ≤ (∫ a in s, g a) := begin apply integral_le_integral_ae hfm hfi hgm hgi, apply indicator_le_indicator_ae, exact h end lemma integral_on_le_integral_on {f g : α → ℝ} (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) (h : ∀ a, a ∈ s → f a ≤ g a) : (∫ a in s, f a) ≤ (∫ a in s, g a) := integral_on_le_integral_on_ae hfm hfi hgm hgi $ by filter_upwards [] h lemma integral_on_union (hsm : measurable_on s f) (hsi : integrable_on s f) (htm : measurable_on t f) (hti : integrable_on t f) (h : disjoint s t) : (∫ a in (s ∪ t), f a) = (∫ a in s, f a) + (∫ a in t, f a) := by { rw [indicator_union_of_disjoint h, integral_add hsm hsi htm hti] } lemma integral_on_union_ae (hs : is_measurable s) (ht : is_measurable t) (hsm : measurable_on s f) (hsi : integrable_on s f) (htm : measurable_on t f) (hti : integrable_on t f) (h : ∀ₘ a, a ∉ s ∩ t) : (∫ a in (s ∪ t), f a) = (∫ a in s, f a) + (∫ a in t, f a) := begin have := integral_congr_ae _ _ (indicator_union_ae h f), rw [this, integral_add hsm hsi htm hti], { exact hsm.union hs ht htm }, { exact measurable.add hsm htm } end lemma integral_on_nonneg_of_ae {f : α → ℝ} (hf : ∀ₘ a, a ∈ s → 0 ≤ f a) : (0:ℝ) ≤ (∫ a in s, f a) := integral_nonneg_of_ae $ by { filter_upwards [hf] λ a h, indicator_nonneg' h } lemma integral_on_nonneg {f : α → ℝ} (hf : ∀ a, a ∈ s → 0 ≤ f a) : (0:ℝ) ≤ (∫ a in s, f a) := integral_on_nonneg_of_ae $ univ_mem_sets' hf lemma integral_on_nonpos_of_ae {f : α → ℝ} (hf : ∀ₘ a, a ∈ s → f a ≤ 0) : (∫ a in s, f a) ≤ 0 := integral_nonpos_of_nonpos_ae $ by { filter_upwards [hf] λ a h, indicator_nonpos' h } lemma integral_on_nonpos {f : α → ℝ} (hf : ∀ a, a ∈ s → f a ≤ 0) : (∫ a in s, f a) ≤ 0 := integral_on_nonpos_of_ae $ univ_mem_sets' hf lemma tendsto_integral_on_of_monotone {s : ℕ → set α} {f : α → β} (hsm : ∀i, is_measurable (s i)) (h_mono : monotone s) (hfm : measurable_on (Union s) f) (hfi : integrable_on (Union s) f) : tendsto (λi, ∫ a in (s i), f a) at_top (nhds (∫ a in (Union s), f a)) := let bound : α → ℝ := indicator (Union s) (λa, ∥f a∥) in begin apply tendsto_integral_of_dominated_convergence, { assume i, exact hfm.subset (hsm i) (subset_Union _ _) }, { assumption }, { show integrable_on (Union s) (λa, ∥f a∥), rwa integrable_on_norm_iff }, { assume i, apply all_ae_of_all, assume a, rw [norm_indicator_eq_indicator_norm], exact indicator_le_indicator_of_subset (subset_Union _ _) (λa, norm_nonneg _) _ }, { filter_upwards [] λa, le_trans (tendsto_indicator_of_monotone _ h_mono _ _) (pure_le_nhds _) } end lemma tendsto_integral_on_of_antimono (s : ℕ → set α) (f : α → β) (hsm : ∀i, is_measurable (s i)) (h_mono : ∀i j, i ≤ j → s j ⊆ s i) (hfm : measurable_on (s 0) f) (hfi : integrable_on (s 0) f) : tendsto (λi, ∫ a in (s i), f a) at_top (nhds (∫ a in (Inter s), f a)) := let bound : α → ℝ := indicator (s 0) (λa, ∥f a∥) in begin apply tendsto_integral_of_dominated_convergence, { assume i, refine hfm.subset (hsm i) (h_mono _ _ (zero_le _)) }, { exact hfm.subset (is_measurable.Inter hsm) (Inter_subset _ _) }, { show integrable_on (s 0) (λa, ∥f a∥), rwa integrable_on_norm_iff }, { assume i, apply all_ae_of_all, assume a, rw [norm_indicator_eq_indicator_norm], refine indicator_le_indicator_of_subset (h_mono _ _ (zero_le _)) (λa, norm_nonneg _) _ }, { filter_upwards [] λa, le_trans (tendsto_indicator_of_antimono _ h_mono _ _) (pure_le_nhds _) } end -- TODO : prove this for an encodable type -- by proving an encodable version of `filter.is_countably_generated_at_top_finset_nat ` lemma integral_on_Union (s : ℕ → set α) (f : α → β) (hm : ∀i, is_measurable (s i)) (hd : ∀ i j, i ≠ j → s i ∩ s j = ∅) (hfm : measurable_on (Union s) f) (hfi : integrable_on (Union s) f) : (∫ a in (Union s), f a) = ∑i, ∫ a in s i, f a := suffices h : tendsto (λn:finset ℕ, n.sum (λ i, ∫ a in s i, f a)) at_top (𝓝 $ (∫ a in (Union s), f a)), by { rwa tsum_eq_has_sum }, begin have : (λn:finset ℕ, n.sum (λ i, ∫ a in s i, f a)) = λn:finset ℕ, ∫ a in (⋃i∈n, s i), f a, { funext, rw [← integral_finset_sum, indicator_finset_bUnion], { assume i hi j hj hij, exact hd i j hij }, { assume i, refine hfm.subset (hm _) (subset_Union _ _) }, { assume i, refine hfi.subset (subset_Union _ _) } }, rw this, refine tendsto_integral_filter_of_dominated_convergence _ _ _ _ _ _ _, { exact indicator (Union s) (λ a, ∥f a∥) }, { exact is_countably_generated_at_top_finset_nat }, { refine univ_mem_sets' (λ n, _), simp only [mem_set_of_eq], refine hfm.subset (is_measurable.Union (λ i, is_measurable.Union_Prop (λh, hm _))) (bUnion_subset_Union _ _), }, { assumption }, { refine univ_mem_sets' (λ n, univ_mem_sets' $ _), simp only [mem_set_of_eq], assume a, rw ← norm_indicator_eq_indicator_norm, refine norm_indicator_le_of_subset (bUnion_subset_Union _ _) _ _ }, { rw [← integrable_on, integrable_on_norm_iff], assumption }, { filter_upwards [] λa, le_trans (tendsto_indicator_bUnion_finset _ _ _) (pure_le_nhds _) } end end integral_on
f98c5de278deaa591f2dc0daeb5ab686d4f6ab08
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/abelian/injective.lean
781dbd2ab645b63191256f2fc556bbaa800f33fa
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
1,614
lean
/- Copyright (c) 2022 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jakob von Raumer -/ import category_theory.abelian.exact import category_theory.preadditive.injective import category_theory.preadditive.yoneda.limits import category_theory.preadditive.yoneda.injective /-! # Injective objects in abelian categories > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. * Objects in an abelian categories are injective if and only if the preadditive Yoneda functor on them preserves finite colimits. -/ noncomputable theory open category_theory open category_theory.limits open category_theory.injective open opposite universes v u namespace category_theory variables {C : Type u} [category.{v} C] [abelian C] /-- The preadditive Yoneda functor on `J` preserves colimits if `J` is injective. -/ def preserves_finite_colimits_preadditive_yoneda_obj_of_injective (J : C) [hP : injective J] : preserves_finite_colimits (preadditive_yoneda_obj J) := begin letI := (injective_iff_preserves_epimorphisms_preadditive_yoneda_obj' J).mp hP, apply functor.preserves_finite_colimits_of_preserves_epis_and_kernels, end /-- An object is injective if its preadditive Yoneda functor preserves finite colimits. -/ lemma injective_of_preserves_finite_colimits_preadditive_yoneda_obj (J : C) [hP : preserves_finite_colimits (preadditive_yoneda_obj J)] : injective J := begin rw injective_iff_preserves_epimorphisms_preadditive_yoneda_obj', apply_instance end end category_theory
73dae586c89be090146b0cab6c90d5b999c50a00
5d166a16ae129621cb54ca9dde86c275d7d2b483
/tests/lean/run/u_eq_max_u_v.lean
80bf5e1ca4a7ce3df49af1665adce4bccc48bbd9
[ "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
3,039
lean
universe variables u v u1 u2 v1 v2 set_option pp.universes true open smt_tactic meta def blast : tactic unit := using_smt $ intros >> add_lemmas_from_facts >> repeat_at_most 3 ematch notation `♮` := by blast structure semigroup_morphism { α β : Type u } ( s : semigroup α ) ( t: semigroup β ) := (map: α → β) (multiplicative : ∀ x y : α, map(x * y) = map(x) * map(y)) attribute [simp] semigroup_morphism.multiplicative @[reducible] instance semigroup_morphism_to_map { α β : Type u } { s : semigroup α } { t: semigroup β } : has_coe_to_fun (semigroup_morphism s t) := { F := λ f, Π x : α, β, coe := semigroup_morphism.map } @[reducible] definition semigroup_identity { α : Type u } ( s: semigroup α ) : semigroup_morphism s s := ⟨ id, ♮ ⟩ @[reducible] definition semigroup_morphism_composition { α β γ : Type u } { s: semigroup α } { t: semigroup β } { u: semigroup γ} ( f: semigroup_morphism s t ) ( g: semigroup_morphism t u ) : semigroup_morphism s u := { map := λ x, g (f x), multiplicative := begin blast, simp end } @[reducible] definition semigroup_product { α β : Type u } ( s : semigroup α ) ( t: semigroup β ) : semigroup (α × β) := { mul := λ p q, (p^.fst * q^.fst, p^.snd * q^.snd), mul_assoc := begin intros, simp [@has_mul.mul (α × β)] end } definition semigroup_morphism_product { α β γ δ : Type u } { s_f : semigroup α } { s_g: semigroup β } { t_f : semigroup γ} { t_g: semigroup δ } ( f : semigroup_morphism s_f t_f ) ( g : semigroup_morphism s_g t_g ) : semigroup_morphism (semigroup_product s_f s_g) (semigroup_product t_f t_g) := { map := λ p, (f p.1, g p.2), multiplicative := begin -- cf https://groups.google.com/d/msg/lean-user/bVs5FdjClp4/tfHiVjLIBAAJ intros, unfold has_mul.mul, dsimp, simp end } structure Category := (Obj : Type u) (Hom : Obj → Obj → Type v) structure Functor (C : Category.{ u1 v1 }) (D : Category.{ u2 v2 }) := (onObjects : C^.Obj → D^.Obj) (onMorphisms : Π { X Y : C^.Obj }, C^.Hom X Y → D^.Hom (onObjects X) (onObjects Y)) @[reducible] definition ProductCategory (C : Category) (D : Category) : Category := { Obj := C^.Obj × D^.Obj, Hom := (λ X Y : C^.Obj × D^.Obj, C^.Hom (X^.fst) (Y^.fst) × D^.Hom (X^.snd) (Y^.snd)) } namespace ProductCategory notation C `×` D := ProductCategory C D end ProductCategory structure PreMonoidalCategory extends carrier : Category := (tensor : Functor (carrier × carrier) carrier) definition CategoryOfSemigroups : Category := { Obj := Σ α : Type u, semigroup α, Hom := λ s t, semigroup_morphism s.2 t.2 } definition PreMonoidalCategoryOfSemigroups : PreMonoidalCategory := { CategoryOfSemigroups with tensor := { onObjects := λ p, sigma.mk (p.1.1 × p.2.1) (semigroup_product p.1.2 p.2.2), onMorphisms := λ s t f, semigroup_morphism_product f.1 f.2 } }
0cbcc9f58f076b65430203918d30172085c170c5
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/ring_theory/hahn_series.lean
6e752eb90545c3c564e5ab18c8172796e0944fff
[ "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
56,961
lean
/- Copyright (c) 2021 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import order.well_founded_set import algebra.big_operators.finprod import ring_theory.valuation.basic import algebra.module.pi import ring_theory.power_series.basic /-! # Hahn Series If `Γ` is ordered and `R` has zero, then `hahn_series Γ R` consists of formal series over `Γ` with coefficients in `R`, whose supports are partially well-ordered. With further structure on `R` and `Γ`, we can add further structure on `hahn_series Γ R`, with the most studied case being when `Γ` is a linearly ordered abelian group and `R` is a field, in which case `hahn_series Γ R` is a valued field, with value group `Γ`. These generalize Laurent series (with value group `ℤ`), and Laurent series are implemented that way in the file `ring_theory/laurent_series`. ## Main Definitions * If `Γ` is ordered and `R` has zero, then `hahn_series Γ R` consists of formal series over `Γ` with coefficients in `R`, whose supports are partially well-ordered. * If `R` is a (commutative) additive monoid or group, then so is `hahn_series Γ R`. * If `R` is a (comm_)(semi)ring, then so is `hahn_series Γ R`. * `hahn_series.add_val Γ R` defines an `add_valuation` on `hahn_series Γ R` when `Γ` is linearly ordered. * A `hahn_series.summable_family` is a family of Hahn series such that the union of their supports is well-founded and only finitely many are nonzero at any given coefficient. They have a formal sum, `hahn_series.summable_family.hsum`, which can be bundled as a `linear_map` as `hahn_series.summable_family.lsum`. Note that this is different from `summable` in the valuation topology, because there are topologically summable families that do not satisfy the axioms of `hahn_series.summable_family`, and formally summable families whose sums do not converge topologically. * Laurent series over `R` are implemented as `hahn_series ℤ R` in the file `ring_theory/laurent_series`. ## TODO * Build an API for the variable `X` (defined to be `single 1 1 : hahn_series Γ R`) in analogy to `X : polynomial R` and `X : power_series R` ## References - [J. van der Hoeven, *Operators on Generalized Power Series*][van_der_hoeven] -/ open finset open_locale big_operators classical pointwise noncomputable theory /-- If `Γ` is linearly ordered and `R` has zero, then `hahn_series Γ R` consists of formal series over `Γ` with coefficients in `R`, whose supports are well-founded. -/ @[ext] structure hahn_series (Γ : Type*) (R : Type*) [partial_order Γ] [has_zero R] := (coeff : Γ → R) (is_pwo_support' : (function.support coeff).is_pwo) variables {Γ : Type*} {R : Type*} namespace hahn_series section zero variables [partial_order Γ] [has_zero R] /-- The support of a Hahn series is just the set of indices whose coefficients are nonzero. Notably, it is well-founded. -/ def support (x : hahn_series Γ R) : set Γ := function.support x.coeff @[simp] lemma is_pwo_support (x : hahn_series Γ R) : x.support.is_pwo := x.is_pwo_support' @[simp] lemma is_wf_support (x : hahn_series Γ R) : x.support.is_wf := x.is_pwo_support.is_wf @[simp] lemma mem_support (x : hahn_series Γ R) (a : Γ) : a ∈ x.support ↔ x.coeff a ≠ 0 := iff.refl _ instance : has_zero (hahn_series Γ R) := ⟨{ coeff := 0, is_pwo_support' := by simp }⟩ instance : inhabited (hahn_series Γ R) := ⟨0⟩ instance [subsingleton R] : subsingleton (hahn_series Γ R) := ⟨λ a b, a.ext b (subsingleton.elim _ _)⟩ @[simp] lemma zero_coeff {a : Γ} : (0 : hahn_series Γ R).coeff a = 0 := rfl lemma ne_zero_of_coeff_ne_zero {x : hahn_series Γ R} {g : Γ} (h : x.coeff g ≠ 0) : x ≠ 0 := mt (λ x0, (x0.symm ▸ zero_coeff : x.coeff g = 0)) h @[simp] lemma support_zero : support (0 : hahn_series Γ R) = ∅ := function.support_zero @[simp] lemma support_nonempty_iff {x : hahn_series Γ R} : x.support.nonempty ↔ x ≠ 0 := begin split, { rintro ⟨a, ha⟩ rfl, apply ha zero_coeff }, { contrapose!, rw set.not_nonempty_iff_eq_empty, intro h, ext a, have ha := set.not_mem_empty a, rw [← h, mem_support, not_not] at ha, rw [ha, zero_coeff] } end /-- `single a r` is the Hahn series which has coefficient `r` at `a` and zero otherwise. -/ def single (a : Γ) : zero_hom R (hahn_series Γ R) := { to_fun := λ r, { coeff := pi.single a r, is_pwo_support' := (set.is_pwo_singleton a).mono pi.support_single_subset }, map_zero' := ext _ _ (pi.single_zero _) } variables {a b : Γ} {r : R} @[simp] theorem single_coeff_same (a : Γ) (r : R) : (single a r).coeff a = r := pi.single_eq_same a r @[simp] theorem single_coeff_of_ne (h : b ≠ a) : (single a r).coeff b = 0 := pi.single_eq_of_ne h r theorem single_coeff : (single a r).coeff b = if (b = a) then r else 0 := by { split_ifs with h; simp [h] } @[simp] lemma support_single_of_ne (h : r ≠ 0) : support (single a r) = {a} := pi.support_single_of_ne h lemma support_single_subset : support (single a r) ⊆ {a} := pi.support_single_subset lemma eq_of_mem_support_single {b : Γ} (h : b ∈ support (single a r)) : b = a := support_single_subset h @[simp] lemma single_eq_zero : (single a (0 : R)) = 0 := (single a).map_zero lemma single_injective (a : Γ) : function.injective (single a : R → hahn_series Γ R) := λ r s rs, by rw [← single_coeff_same a r, ← single_coeff_same a s, rs] lemma single_ne_zero (h : r ≠ 0) : single a r ≠ 0 := λ con, h (single_injective a (con.trans single_eq_zero.symm)) instance [nonempty Γ] [nontrivial R] : nontrivial (hahn_series Γ R) := ⟨begin obtain ⟨r, s, rs⟩ := exists_pair_ne R, inhabit Γ, refine ⟨single (arbitrary Γ) r, single (arbitrary Γ) s, λ con, rs _⟩, rw [← single_coeff_same (arbitrary Γ) r, con, single_coeff_same], end⟩ section order variable [has_zero Γ] /-- The order of a nonzero Hahn series `x` is a minimal element of `Γ` where `x` has a nonzero coefficient, the order of 0 is 0. -/ def order (x : hahn_series Γ R) : Γ := if h : x = 0 then 0 else x.is_wf_support.min (support_nonempty_iff.2 h) @[simp] lemma order_zero : order (0 : hahn_series Γ R) = 0 := dif_pos rfl lemma order_of_ne {x : hahn_series Γ R} (hx : x ≠ 0) : order x = x.is_wf_support.min (support_nonempty_iff.2 hx) := dif_neg hx lemma coeff_order_ne_zero {x : hahn_series Γ R} (hx : x ≠ 0) : x.coeff x.order ≠ 0 := begin rw order_of_ne hx, exact x.is_wf_support.min_mem (support_nonempty_iff.2 hx) end lemma order_le_of_coeff_ne_zero {Γ} [linear_ordered_cancel_add_comm_monoid Γ] {x : hahn_series Γ R} {g : Γ} (h : x.coeff g ≠ 0) : x.order ≤ g := le_trans (le_of_eq (order_of_ne (ne_zero_of_coeff_ne_zero h))) (set.is_wf.min_le _ _ ((mem_support _ _).2 h)) @[simp] lemma order_single (h : r ≠ 0) : (single a r).order = a := (order_of_ne (single_ne_zero h)).trans (support_single_subset ((single a r).is_wf_support.min_mem (support_nonempty_iff.2 (single_ne_zero h)))) end order section domain variables {Γ' : Type*} [partial_order Γ'] /-- Extends the domain of a `hahn_series` by an `order_embedding`. -/ def emb_domain (f : Γ ↪o Γ') : hahn_series Γ R → hahn_series Γ' R := λ x, { coeff := λ (b : Γ'), if h : b ∈ f '' x.support then x.coeff (classical.some h) else 0, is_pwo_support' := (x.is_pwo_support.image_of_monotone f.monotone).mono (λ b hb, begin contrapose! hb, rw [function.mem_support, dif_neg hb, not_not], end) } @[simp] lemma emb_domain_coeff {f : Γ ↪o Γ'} {x : hahn_series Γ R} {a : Γ} : (emb_domain f x).coeff (f a) = x.coeff a := begin rw emb_domain, dsimp only, by_cases ha : a ∈ x.support, { rw dif_pos (set.mem_image_of_mem f ha), exact congr rfl (f.injective (classical.some_spec (set.mem_image_of_mem f ha)).2) }, { rw [dif_neg, not_not.1 (λ c, ha ((mem_support _ _).2 c))], contrapose! ha, obtain ⟨b, hb1, hb2⟩ := (set.mem_image _ _ _).1 ha, rwa f.injective hb2 at hb1 } end @[simp] lemma emb_domain_mk_coeff {f : Γ → Γ'} (hfi : function.injective f) (hf : ∀ g g' : Γ, f g ≤ f g' ↔ g ≤ g') {x : hahn_series Γ R} {a : Γ} : (emb_domain ⟨⟨f, hfi⟩, hf⟩ x).coeff (f a) = x.coeff a := emb_domain_coeff lemma emb_domain_notin_image_support {f : Γ ↪o Γ'} {x : hahn_series Γ R} {b : Γ'} (hb : b ∉ f '' x.support) : (emb_domain f x).coeff b = 0 := dif_neg hb lemma support_emb_domain_subset {f : Γ ↪o Γ'} {x : hahn_series Γ R} : support (emb_domain f x) ⊆ f '' x.support := begin intros g hg, contrapose! hg, rw [mem_support, emb_domain_notin_image_support hg, not_not], end lemma emb_domain_notin_range {f : Γ ↪o Γ'} {x : hahn_series Γ R} {b : Γ'} (hb : b ∉ set.range f) : (emb_domain f x).coeff b = 0 := emb_domain_notin_image_support (λ con, hb (set.image_subset_range _ _ con)) @[simp] lemma emb_domain_zero {f : Γ ↪o Γ'} : emb_domain f (0 : hahn_series Γ R) = 0 := by { ext, simp [emb_domain_notin_image_support] } @[simp] lemma emb_domain_single {f : Γ ↪o Γ'} {g : Γ} {r : R} : emb_domain f (single g r) = single (f g) r := begin ext g', by_cases h : g' = f g, { simp [h] }, rw [emb_domain_notin_image_support, single_coeff_of_ne h], by_cases hr : r = 0, { simp [hr] }, rwa [support_single_of_ne hr, set.image_singleton, set.mem_singleton_iff], end lemma emb_domain_injective {f : Γ ↪o Γ'} : function.injective (emb_domain f : hahn_series Γ R → hahn_series Γ' R) := λ x y xy, begin ext g, rw [ext_iff, function.funext_iff] at xy, have xyg := xy (f g), rwa [emb_domain_coeff, emb_domain_coeff] at xyg, end end domain end zero section addition variable [partial_order Γ] section add_monoid variable [add_monoid R] instance : has_add (hahn_series Γ R) := { add := λ x y, { coeff := x.coeff + y.coeff, is_pwo_support' := (x.is_pwo_support.union y.is_pwo_support).mono (function.support_add _ _) } } instance : add_monoid (hahn_series Γ R) := { zero := 0, add := (+), add_assoc := λ x y z, by { ext, apply add_assoc }, zero_add := λ x, by { ext, apply zero_add }, add_zero := λ x, by { ext, apply add_zero } } @[simp] lemma add_coeff' {x y : hahn_series Γ R} : (x + y).coeff = x.coeff + y.coeff := rfl lemma add_coeff {x y : hahn_series Γ R} {a : Γ} : (x + y).coeff a = x.coeff a + y.coeff a := rfl lemma support_add_subset {x y : hahn_series Γ R} : support (x + y) ⊆ support x ∪ support y := λ a ha, begin rw [mem_support, add_coeff] at ha, rw [set.mem_union, mem_support, mem_support], contrapose! ha, rw [ha.1, ha.2, add_zero], end lemma min_order_le_order_add {Γ} [linear_ordered_cancel_add_comm_monoid Γ] {x y : hahn_series Γ R} (hx : x ≠ 0) (hy : y ≠ 0) (hxy : x + y ≠ 0) : min x.order y.order ≤ (x + y).order := begin rw [order_of_ne hx, order_of_ne hy, order_of_ne hxy], refine le_trans _ (set.is_wf.min_le_min_of_subset support_add_subset), { exact x.is_wf_support.union y.is_wf_support }, { exact set.nonempty.mono (set.subset_union_left _ _) (support_nonempty_iff.2 hx) }, rw set.is_wf.min_union, end /-- `single` as an additive monoid/group homomorphism -/ @[simps] def single.add_monoid_hom (a : Γ) : R →+ (hahn_series Γ R) := { map_add' := λ x y, by { ext b, by_cases h : b = a; simp [h] }, ..single a } /-- `coeff g` as an additive monoid/group homomorphism -/ @[simps] def coeff.add_monoid_hom (g : Γ) : (hahn_series Γ R) →+ R := { to_fun := λ f, f.coeff g, map_zero' := zero_coeff, map_add' := λ x y, add_coeff } section domain variables {Γ' : Type*} [partial_order Γ'] lemma emb_domain_add (f : Γ ↪o Γ') (x y : hahn_series Γ R) : emb_domain f (x + y) = emb_domain f x + emb_domain f y := begin ext g, by_cases hg : g ∈ set.range f, { obtain ⟨a, rfl⟩ := hg, simp }, { simp [emb_domain_notin_range, hg] } end end domain end add_monoid instance [add_comm_monoid R] : add_comm_monoid (hahn_series Γ R) := { add_comm := λ x y, by { ext, apply add_comm } .. hahn_series.add_monoid } section add_group variable [add_group R] instance : add_group (hahn_series Γ R) := { neg := λ x, { coeff := λ a, - x.coeff a, is_pwo_support' := by { rw function.support_neg, exact x.is_pwo_support }, }, add_left_neg := λ x, by { ext, apply add_left_neg }, .. hahn_series.add_monoid } @[simp] lemma neg_coeff' {x : hahn_series Γ R} : (- x).coeff = - x.coeff := rfl lemma neg_coeff {x : hahn_series Γ R} {a : Γ} : (- x).coeff a = - x.coeff a := rfl @[simp] lemma support_neg {x : hahn_series Γ R} : (- x).support = x.support := by { ext, simp } @[simp] lemma sub_coeff' {x y : hahn_series Γ R} : (x - y).coeff = x.coeff - y.coeff := by { ext, simp [sub_eq_add_neg] } lemma sub_coeff {x y : hahn_series Γ R} {a : Γ} : (x - y).coeff a = x.coeff a - y.coeff a := by simp end add_group instance [add_comm_group R] : add_comm_group (hahn_series Γ R) := { .. hahn_series.add_comm_monoid, .. hahn_series.add_group } end addition section distrib_mul_action variables [partial_order Γ] {V : Type*} [monoid R] [add_monoid V] [distrib_mul_action R V] instance : has_scalar R (hahn_series Γ V) := ⟨λ r x, { coeff := r • x.coeff, is_pwo_support' := x.is_pwo_support.mono (function.support_smul_subset_right r x.coeff) }⟩ @[simp] lemma smul_coeff {r : R} {x : hahn_series Γ V} {a : Γ} : (r • x).coeff a = r • (x.coeff a) := rfl instance : distrib_mul_action R (hahn_series Γ V) := { smul := (•), one_smul := λ _, by { ext, simp }, smul_zero := λ _, by { ext, simp }, smul_add := λ _ _ _, by { ext, simp [smul_add] }, mul_smul := λ _ _ _, by { ext, simp [mul_smul] } } variables {S : Type*} [monoid S] [distrib_mul_action S V] instance [has_scalar R S] [is_scalar_tower R S V] : is_scalar_tower R S (hahn_series Γ V) := ⟨λ r s a, by { ext, simp }⟩ instance [smul_comm_class R S V] : smul_comm_class R S (hahn_series Γ V) := ⟨λ r s a, by { ext, simp [smul_comm] }⟩ end distrib_mul_action section module variables [partial_order Γ] [semiring R] {V : Type*} [add_comm_monoid V] [module R V] instance : module R (hahn_series Γ V) := { zero_smul := λ _, by { ext, simp }, add_smul := λ _ _ _, by { ext, simp [add_smul] }, .. hahn_series.distrib_mul_action } /-- `single` as a linear map -/ @[simps] def single.linear_map (a : Γ) : R →ₗ[R] (hahn_series Γ R) := { map_smul' := λ r s, by { ext b, by_cases h : b = a; simp [h] }, ..single.add_monoid_hom a } /-- `coeff g` as a linear map -/ @[simps] def coeff.linear_map (g : Γ) : (hahn_series Γ R) →ₗ[R] R := { map_smul' := λ r s, rfl, ..coeff.add_monoid_hom g } section domain variables {Γ' : Type*} [partial_order Γ'] lemma emb_domain_smul (f : Γ ↪o Γ') (r : R) (x : hahn_series Γ R) : emb_domain f (r • x) = r • emb_domain f x := begin ext g, by_cases hg : g ∈ set.range f, { obtain ⟨a, rfl⟩ := hg, simp }, { simp [emb_domain_notin_range, hg] } end /-- Extending the domain of Hahn series is a linear map. -/ @[simps] def emb_domain_linear_map (f : Γ ↪o Γ') : hahn_series Γ R →ₗ[R] hahn_series Γ' R := { to_fun := emb_domain f, map_add' := emb_domain_add f, map_smul' := emb_domain_smul f } end domain end module section multiplication variable [ordered_cancel_add_comm_monoid Γ] instance [has_zero R] [has_one R] : has_one (hahn_series Γ R) := ⟨single 0 1⟩ @[simp] lemma one_coeff [has_zero R] [has_one R] {a : Γ} : (1 : hahn_series Γ R).coeff a = if a = 0 then 1 else 0 := single_coeff @[simp] lemma single_zero_one [has_zero R] [has_one R] : (single 0 (1 : R)) = 1 := rfl @[simp] lemma support_one [mul_zero_one_class R] [nontrivial R] : support (1 : hahn_series Γ R) = {0} := support_single_of_ne one_ne_zero @[simp] lemma order_one [mul_zero_one_class R] : order (1 : hahn_series Γ R) = 0 := begin cases subsingleton_or_nontrivial R with h h; haveI := h, { rw [subsingleton.elim (1 : hahn_series Γ R) 0, order_zero] }, { exact order_single one_ne_zero } end instance [non_unital_non_assoc_semiring R] : has_mul (hahn_series Γ R) := { mul := λ x y, { coeff := λ a, ∑ ij in (add_antidiagonal x.is_pwo_support y.is_pwo_support a), x.coeff ij.fst * y.coeff ij.snd, is_pwo_support' := begin have h : {a : Γ | ∑ (ij : Γ × Γ) in add_antidiagonal x.is_pwo_support y.is_pwo_support a, x.coeff ij.fst * y.coeff ij.snd ≠ 0} ⊆ {a : Γ | (add_antidiagonal x.is_pwo_support y.is_pwo_support a).nonempty}, { intros a ha, contrapose! ha, simp [not_nonempty_iff_eq_empty.1 ha] }, exact is_pwo_support_add_antidiagonal.mono h, end, }, } @[simp] lemma mul_coeff [non_unital_non_assoc_semiring R] {x y : hahn_series Γ R} {a : Γ} : (x * y).coeff a = ∑ ij in (add_antidiagonal x.is_pwo_support y.is_pwo_support a), x.coeff ij.fst * y.coeff ij.snd := rfl lemma mul_coeff_right' [non_unital_non_assoc_semiring R] {x y : hahn_series Γ R} {a : Γ} {s : set Γ} (hs : s.is_pwo) (hys : y.support ⊆ s) : (x * y).coeff a = ∑ ij in (add_antidiagonal x.is_pwo_support hs a), x.coeff ij.fst * y.coeff ij.snd := begin rw mul_coeff, apply sum_subset_zero_on_sdiff (add_antidiagonal_mono_right hys) _ (λ _ _, rfl), intros b hb, simp only [not_and, not_not, mem_sdiff, mem_add_antidiagonal, ne.def, set.mem_set_of_eq, mem_support] at hb, rw [(hb.2 hb.1.1 hb.1.2.1), mul_zero] end lemma mul_coeff_left' [non_unital_non_assoc_semiring R] {x y : hahn_series Γ R} {a : Γ} {s : set Γ} (hs : s.is_pwo) (hxs : x.support ⊆ s) : (x * y).coeff a = ∑ ij in (add_antidiagonal hs y.is_pwo_support a), x.coeff ij.fst * y.coeff ij.snd := begin rw mul_coeff, apply sum_subset_zero_on_sdiff (add_antidiagonal_mono_left hxs) _ (λ _ _, rfl), intros b hb, simp only [not_and, not_not, mem_sdiff, mem_add_antidiagonal, ne.def, set.mem_set_of_eq, mem_support] at hb, rw [not_not.1 (λ con, hb.1.2.2 (hb.2 hb.1.1 con)), zero_mul], end instance [non_unital_non_assoc_semiring R] : distrib (hahn_series Γ R) := { left_distrib := λ x y z, begin ext a, have hwf := (y.is_pwo_support.union z.is_pwo_support), rw [mul_coeff_right' hwf, add_coeff, mul_coeff_right' hwf (set.subset_union_right _ _), mul_coeff_right' hwf (set.subset_union_left _ _)], { simp only [add_coeff, mul_add, sum_add_distrib] }, { intro b, simp only [add_coeff, ne.def, set.mem_union_eq, set.mem_set_of_eq, mem_support], contrapose!, intro h, rw [h.1, h.2, add_zero], } end, right_distrib := λ x y z, begin ext a, have hwf := (x.is_pwo_support.union y.is_pwo_support), rw [mul_coeff_left' hwf, add_coeff, mul_coeff_left' hwf (set.subset_union_right _ _), mul_coeff_left' hwf (set.subset_union_left _ _)], { simp only [add_coeff, add_mul, sum_add_distrib] }, { intro b, simp only [add_coeff, ne.def, set.mem_union_eq, set.mem_set_of_eq, mem_support], contrapose!, intro h, rw [h.1, h.2, add_zero], }, end, .. hahn_series.has_mul, .. hahn_series.has_add } lemma single_mul_coeff_add [non_unital_non_assoc_semiring R] {r : R} {x : hahn_series Γ R} {a : Γ} {b : Γ} : ((single b r) * x).coeff (a + b) = r * x.coeff a := begin by_cases hr : r = 0, { simp [hr] }, simp only [hr, smul_coeff, mul_coeff, support_single_of_ne, ne.def, not_false_iff, smul_eq_mul], by_cases hx : x.coeff a = 0, { simp only [hx, mul_zero], rw [sum_congr _ (λ _ _, rfl), sum_empty], ext ⟨a1, a2⟩, simp only [not_mem_empty, not_and, set.mem_singleton_iff, not_not, mem_add_antidiagonal, set.mem_set_of_eq, iff_false], rintro h1 rfl h2, rw add_comm at h1, rw ← add_right_cancel h1 at hx, exact h2 hx, }, transitivity ∑ (ij : Γ × Γ) in {(b, a)}, (single b r).coeff ij.fst * x.coeff ij.snd, { apply sum_congr _ (λ _ _, rfl), ext ⟨a1, a2⟩, simp only [set.mem_singleton_iff, prod.mk.inj_iff, mem_add_antidiagonal, mem_singleton, set.mem_set_of_eq], split, { rintro ⟨h1, rfl, h2⟩, rw add_comm at h1, refine ⟨rfl, add_right_cancel h1⟩ }, { rintro ⟨rfl, rfl⟩, refine ⟨add_comm _ _, _⟩, simp [hx] } }, { simp } end lemma mul_single_coeff_add [non_unital_non_assoc_semiring R] {r : R} {x : hahn_series Γ R} {a : Γ} {b : Γ} : (x * (single b r)).coeff (a + b) = x.coeff a * r := begin by_cases hr : r = 0, { simp [hr] }, simp only [hr, smul_coeff, mul_coeff, support_single_of_ne, ne.def, not_false_iff, smul_eq_mul], by_cases hx : x.coeff a = 0, { simp only [hx, zero_mul], rw [sum_congr _ (λ _ _, rfl), sum_empty], ext ⟨a1, a2⟩, simp only [not_mem_empty, not_and, set.mem_singleton_iff, not_not, mem_add_antidiagonal, set.mem_set_of_eq, iff_false], rintro h1 h2 rfl, rw ← add_right_cancel h1 at hx, exact h2 hx, }, transitivity ∑ (ij : Γ × Γ) in {(a,b)}, x.coeff ij.fst * (single b r).coeff ij.snd, { apply sum_congr _ (λ _ _, rfl), ext ⟨a1, a2⟩, simp only [set.mem_singleton_iff, prod.mk.inj_iff, mem_add_antidiagonal, mem_singleton, set.mem_set_of_eq], split, { rintro ⟨h1, h2, rfl⟩, refine ⟨add_right_cancel h1, rfl⟩ }, { rintro ⟨rfl, rfl⟩, simp [hx] } }, { simp } end @[simp] lemma mul_single_zero_coeff [non_unital_non_assoc_semiring R] {r : R} {x : hahn_series Γ R} {a : Γ} : (x * (single 0 r)).coeff a = x.coeff a * r := by rw [← add_zero a, mul_single_coeff_add, add_zero] lemma single_zero_mul_coeff [non_unital_non_assoc_semiring R] {r : R} {x : hahn_series Γ R} {a : Γ} : ((single 0 r) * x).coeff a = r * x.coeff a := by rw [← add_zero a, single_mul_coeff_add, add_zero] @[simp] lemma single_zero_mul_eq_smul [semiring R] {r : R} {x : hahn_series Γ R} : (single 0 r) * x = r • x := by { ext, exact single_zero_mul_coeff } theorem support_mul_subset_add_support [non_unital_non_assoc_semiring R] {x y : hahn_series Γ R} : support (x * y) ⊆ support x + support y := begin apply set.subset.trans (λ x hx, _) support_add_antidiagonal_subset_add, { exact x.is_pwo_support }, { exact y.is_pwo_support }, contrapose! hx, simp only [not_nonempty_iff_eq_empty, ne.def, set.mem_set_of_eq] at hx, simp [hx], end lemma mul_coeff_order_add_order {Γ} [linear_ordered_cancel_add_comm_monoid Γ] [non_unital_non_assoc_semiring R] {x y : hahn_series Γ R} (hx : x ≠ 0) (hy : y ≠ 0) : (x * y).coeff (x.order + y.order) = x.coeff x.order * y.coeff y.order := by rw [order_of_ne hx, order_of_ne hy, mul_coeff, finset.add_antidiagonal_min_add_min, finset.sum_singleton] private lemma mul_assoc' [non_unital_semiring R] (x y z : hahn_series Γ R) : x * y * z = x * (y * z) := begin ext b, rw [mul_coeff_left' (x.is_pwo_support.add y.is_pwo_support) support_mul_subset_add_support, mul_coeff_right' (y.is_pwo_support.add z.is_pwo_support) support_mul_subset_add_support], simp only [mul_coeff, add_coeff, sum_mul, mul_sum, sum_sigma'], refine sum_bij_ne_zero (λ a has ha0, ⟨⟨a.2.1, a.2.2 + a.1.2⟩, ⟨a.2.2, a.1.2⟩⟩) _ _ _ _, { rintros ⟨⟨i,j⟩, ⟨k,l⟩⟩ H1 H2, simp only [true_and, set.image2_add, eq_self_iff_true, mem_add_antidiagonal, ne.def, set.image_prod, mem_sigma, set.mem_set_of_eq] at H1 H2 ⊢, obtain ⟨⟨rfl, ⟨H3, nz⟩⟩, ⟨rfl, nx, ny⟩⟩ := H1, refine ⟨⟨(add_assoc _ _ _).symm, nx, set.add_mem_add ny nz⟩, ny, nz⟩ }, { rintros ⟨⟨i1,j1⟩, ⟨k1,l1⟩⟩ ⟨⟨i2,j2⟩, ⟨k2,l2⟩⟩ H1 H2 H3 H4 H5, simp only [set.image2_add, prod.mk.inj_iff, mem_add_antidiagonal, ne.def, set.image_prod, mem_sigma, set.mem_set_of_eq, heq_iff_eq] at H1 H3 H5, obtain ⟨⟨rfl, H⟩, rfl, rfl⟩ := H5, simp only [and_true, prod.mk.inj_iff, eq_self_iff_true, heq_iff_eq], exact add_right_cancel (H1.1.1.trans H3.1.1.symm) }, { rintros ⟨⟨i,j⟩, ⟨k,l⟩⟩ H1 H2, simp only [exists_prop, set.image2_add, prod.mk.inj_iff, mem_add_antidiagonal, sigma.exists, ne.def, set.image_prod, mem_sigma, set.mem_set_of_eq, heq_iff_eq, prod.exists] at H1 H2 ⊢, obtain ⟨⟨rfl, nx, H⟩, rfl, ny, nz⟩ := H1, exact ⟨i + k, l, i, k, ⟨⟨add_assoc _ _ _, set.add_mem_add nx ny, nz⟩, rfl, nx, ny⟩, λ con, H2 ((mul_assoc _ _ _).symm.trans con), ⟨rfl, rfl⟩, rfl, rfl⟩ }, { rintros ⟨⟨i,j⟩, ⟨k,l⟩⟩ H1 H2, simp [mul_assoc], } end instance [non_unital_non_assoc_semiring R] : non_unital_non_assoc_semiring (hahn_series Γ R) := { zero := 0, add := (+), mul := (*), zero_mul := λ _, by { ext, simp }, mul_zero := λ _, by { ext, simp }, .. hahn_series.add_comm_monoid, .. hahn_series.distrib } instance [non_unital_semiring R] : non_unital_semiring (hahn_series Γ R) := { zero := 0, add := (+), mul := (*), mul_assoc := mul_assoc', .. hahn_series.non_unital_non_assoc_semiring } instance [non_assoc_semiring R] : non_assoc_semiring (hahn_series Γ R) := { zero := 0, one := 1, add := (+), mul := (*), one_mul := λ x, by { ext, exact single_zero_mul_coeff.trans (one_mul _) }, mul_one := λ x, by { ext, exact mul_single_zero_coeff.trans (mul_one _) }, .. hahn_series.non_unital_non_assoc_semiring } instance [semiring R] : semiring (hahn_series Γ R) := { zero := 0, one := 1, add := (+), mul := (*), .. hahn_series.non_assoc_semiring, .. hahn_series.non_unital_semiring } instance [comm_semiring R] : comm_semiring (hahn_series Γ R) := { mul_comm := λ x y, begin ext, simp_rw [mul_coeff, mul_comm], refine sum_bij (λ a ha, ⟨a.2, a.1⟩) _ (λ a ha, by simp) _ _, { intros a ha, simp only [mem_add_antidiagonal, ne.def, set.mem_set_of_eq] at ha ⊢, obtain ⟨h1, h2, h3⟩ := ha, refine ⟨_, h3, h2⟩, rw [add_comm, h1], }, { rintros ⟨a1, a2⟩ ⟨b1, b2⟩ ha hb hab, rw prod.ext_iff at *, refine ⟨hab.2, hab.1⟩, }, { intros a ha, refine ⟨a.swap, _, by simp⟩, simp only [prod.fst_swap, mem_add_antidiagonal, prod.snd_swap, ne.def, set.mem_set_of_eq] at ha ⊢, exact ⟨(add_comm _ _).trans ha.1, ha.2.2, ha.2.1⟩ } end, .. hahn_series.semiring } instance [ring R] : ring (hahn_series Γ R) := { .. hahn_series.semiring, .. hahn_series.add_comm_group } instance [comm_ring R] : comm_ring (hahn_series Γ R) := { .. hahn_series.comm_semiring, .. hahn_series.ring } instance {Γ} [linear_ordered_cancel_add_comm_monoid Γ] [comm_ring R] [integral_domain R] : integral_domain (hahn_series Γ R) := { eq_zero_or_eq_zero_of_mul_eq_zero := λ x y xy, begin by_cases hx : x = 0, { left, exact hx }, right, contrapose! xy, rw [hahn_series.ext_iff, function.funext_iff, not_forall], refine ⟨x.order + y.order, _⟩, rw [mul_coeff_order_add_order hx xy, zero_coeff, mul_eq_zero], simp [coeff_order_ne_zero, hx, xy], end, .. hahn_series.nontrivial, .. hahn_series.comm_ring } @[simp] lemma order_mul {Γ} [linear_ordered_cancel_add_comm_monoid Γ] [comm_ring R] [integral_domain R] {x y : hahn_series Γ R} (hx : x ≠ 0) (hy : y ≠ 0) : (x * y).order = x.order + y.order := begin apply le_antisymm, { apply order_le_of_coeff_ne_zero, rw [mul_coeff_order_add_order hx hy], exact mul_ne_zero (coeff_order_ne_zero hx) (coeff_order_ne_zero hy) }, { rw [order_of_ne hx, order_of_ne hy, order_of_ne (mul_ne_zero hx hy), ← set.is_wf.min_add], exact set.is_wf.min_le_min_of_subset (support_mul_subset_add_support) }, end section non_unital_non_assoc_semiring variables [non_unital_non_assoc_semiring R] @[simp] lemma single_mul_single {a b : Γ} {r s : R} : single a r * single b s = single (a + b) (r * s) := begin ext x, by_cases h : x = a + b, { rw [h, mul_single_coeff_add], simp }, { rw [single_coeff_of_ne h, mul_coeff, sum_eq_zero], rintros ⟨y1, y2⟩ hy, obtain ⟨rfl, hy1, hy2⟩ := mem_add_antidiagonal.1 hy, rw [eq_of_mem_support_single hy1, eq_of_mem_support_single hy2] at h, exact (h rfl).elim } end end non_unital_non_assoc_semiring section non_assoc_semiring variables [non_assoc_semiring R] /-- `C a` is the constant Hahn Series `a`. `C` is provided as a ring homomorphism. -/ @[simps] def C : R →+* (hahn_series Γ R) := { to_fun := single 0, map_zero' := single_eq_zero, map_one' := rfl, map_add' := λ x y, by { ext a, by_cases h : a = 0; simp [h] }, map_mul' := λ x y, by rw [single_mul_single, zero_add] } @[simp] lemma C_zero : C (0 : R) = (0 : hahn_series Γ R) := C.map_zero @[simp] lemma C_one : C (1 : R) = (1 : hahn_series Γ R) := C.map_one lemma C_injective : function.injective (C : R → hahn_series Γ R) := begin intros r s rs, rw [ext_iff, function.funext_iff] at rs, have h := rs 0, rwa [C_apply, single_coeff_same, C_apply, single_coeff_same] at h, end lemma C_ne_zero {r : R} (h : r ≠ 0) : (C r : hahn_series Γ R) ≠ 0 := begin contrapose! h, rw ← C_zero at h, exact C_injective h, end lemma order_C {r : R} : order (C r : hahn_series Γ R) = 0 := begin by_cases h : r = 0, { rw [h, C_zero, order_zero] }, { exact order_single h } end end non_assoc_semiring section semiring variables [semiring R] lemma C_mul_eq_smul {r : R} {x : hahn_series Γ R} : C r * x = r • x := single_zero_mul_eq_smul end semiring section domain variables {Γ' : Type*} [ordered_cancel_add_comm_monoid Γ'] lemma emb_domain_mul [non_unital_non_assoc_semiring R] (f : Γ ↪o Γ') (hf : ∀ x y, f (x + y) = f x + f y) (x y : hahn_series Γ R) : emb_domain f (x * y) = emb_domain f x * emb_domain f y := begin ext g, by_cases hg : g ∈ set.range f, { obtain ⟨g, rfl⟩ := hg, simp only [mul_coeff, emb_domain_coeff], transitivity ∑ ij in (add_antidiagonal x.is_pwo_support y.is_pwo_support g).map (function.embedding.prod_map f.to_embedding f.to_embedding), (emb_domain f x).coeff (ij.1) * (emb_domain f y).coeff (ij.2), { simp }, apply sum_subset, { rintro ⟨i, j⟩ hij, simp only [exists_prop, mem_map, prod.mk.inj_iff, mem_add_antidiagonal, ne.def, function.embedding.coe_prod_map, mem_support, prod.exists] at hij, obtain ⟨i, j, ⟨rfl, hx, hy⟩, rfl, rfl⟩ := hij, simp [hx, hy, hf], }, { rintro ⟨_, _⟩ h1 h2, contrapose! h2, obtain ⟨i, hi, rfl⟩ := support_emb_domain_subset (ne_zero_and_ne_zero_of_mul h2).1, obtain ⟨j, hj, rfl⟩ := support_emb_domain_subset (ne_zero_and_ne_zero_of_mul h2).2, simp only [exists_prop, mem_map, prod.mk.inj_iff, mem_add_antidiagonal, ne.def, function.embedding.coe_prod_map, mem_support, prod.exists], simp only [mem_add_antidiagonal, emb_domain_coeff, ne.def, mem_support, ← hf] at h1, exact ⟨i, j, ⟨f.injective h1.1, h1.2⟩, rfl⟩, } }, { rw [emb_domain_notin_range hg, eq_comm], contrapose! hg, obtain ⟨_, _, hi, hj, rfl⟩ := support_mul_subset_add_support ((mem_support _ _).2 hg), obtain ⟨i, hi, rfl⟩ := support_emb_domain_subset hi, obtain ⟨j, hj, rfl⟩ := support_emb_domain_subset hj, refine ⟨i + j, hf i j⟩, } end lemma emb_domain_one [non_assoc_semiring R] (f : Γ ↪o Γ') (hf : f 0 = 0): emb_domain f (1 : hahn_series Γ R) = (1 : hahn_series Γ' R) := emb_domain_single.trans $ hf.symm ▸ rfl /-- Extending the domain of Hahn series is a ring homomorphism. -/ @[simps] def emb_domain_ring_hom [non_assoc_semiring R] (f : Γ →+ Γ') (hfi : function.injective f) (hf : ∀ g g' : Γ, f g ≤ f g' ↔ g ≤ g') : hahn_series Γ R →+* hahn_series Γ' R := { to_fun := emb_domain ⟨⟨f, hfi⟩, hf⟩, map_one' := emb_domain_one _ f.map_zero, map_mul' := emb_domain_mul _ f.map_add, map_zero' := emb_domain_zero, map_add' := emb_domain_add _} lemma emb_domain_ring_hom_C [non_assoc_semiring R] {f : Γ →+ Γ'} {hfi : function.injective f} {hf : ∀ g g' : Γ, f g ≤ f g' ↔ g ≤ g'} {r : R} : emb_domain_ring_hom f hfi hf (C r) = C r := emb_domain_single.trans (by simp) end domain section algebra variables [comm_semiring R] {A : Type*} [semiring A] [algebra R A] instance : algebra R (hahn_series Γ A) := { to_ring_hom := C.comp (algebra_map R A), smul_def' := λ r x, by { ext, simp }, commutes' := λ r x, by { ext, simp only [smul_coeff, single_zero_mul_eq_smul, ring_hom.coe_comp, ring_hom.to_fun_eq_coe, C_apply, function.comp_app, algebra_map_smul, mul_single_zero_coeff], rw [← algebra.commutes, algebra.smul_def], }, } theorem C_eq_algebra_map : C = (algebra_map R (hahn_series Γ R)) := rfl theorem algebra_map_apply {r : R} : algebra_map R (hahn_series Γ A) r = C (algebra_map R A r) := rfl instance [nontrivial Γ] [nontrivial R] : nontrivial (subalgebra R (hahn_series Γ R)) := ⟨⟨⊥, ⊤, begin rw [ne.def, set_like.ext_iff, not_forall], obtain ⟨a, ha⟩ := exists_ne (0 : Γ), refine ⟨single a 1, _⟩, simp only [algebra.mem_bot, not_exists, set.mem_range, iff_true, algebra.mem_top], intros x, rw [ext_iff, function.funext_iff, not_forall], refine ⟨a, _⟩, rw [single_coeff_same, algebra_map_apply, C_apply, single_coeff_of_ne ha], exact zero_ne_one end⟩⟩ section domain variables {Γ' : Type*} [ordered_cancel_add_comm_monoid Γ'] /-- Extending the domain of Hahn series is an algebra homomorphism. -/ @[simps] def emb_domain_alg_hom (f : Γ →+ Γ') (hfi : function.injective f) (hf : ∀ g g' : Γ, f g ≤ f g' ↔ g ≤ g') : hahn_series Γ A →ₐ[R] hahn_series Γ' A := { commutes' := λ r, emb_domain_ring_hom_C, .. emb_domain_ring_hom f hfi hf } end domain end algebra end multiplication section semiring variables [semiring R] /-- The ring `hahn_series ℕ R` is isomorphic to `power_series R`. -/ @[simps] def to_power_series : (hahn_series ℕ R) ≃+* power_series R := { to_fun := λ f, power_series.mk f.coeff, inv_fun := λ f, ⟨λ n, power_series.coeff R n f, (nat.lt_wf.is_wf _).is_pwo⟩, left_inv := λ f, by { ext, simp }, right_inv := λ f, by { ext, simp }, map_add' := λ f g, by { ext, simp }, map_mul' := λ f g, begin ext n, simp only [power_series.coeff_mul, power_series.coeff_mk, mul_coeff, is_pwo_support], classical, refine sum_filter_ne_zero.symm.trans ((sum_congr _ (λ _ _, rfl)).trans sum_filter_ne_zero), ext m, simp only [nat.mem_antidiagonal, and.congr_left_iff, mem_add_antidiagonal, ne.def, and_iff_left_iff_imp, mem_filter, mem_support], intros h1 h2, contrapose h1, rw ← decidable.or_iff_not_and_not at h1, cases h1; simp [h1] end } lemma coeff_to_power_series {f : hahn_series ℕ R} {n : ℕ} : power_series.coeff R n f.to_power_series = f.coeff n := power_series.coeff_mk _ _ lemma coeff_to_power_series_symm {f : power_series R} {n : ℕ} : (hahn_series.to_power_series.symm f).coeff n = power_series.coeff R n f := rfl variables (Γ) (R) [ordered_semiring Γ] [nontrivial Γ] /-- Casts a power series as a Hahn series with coefficients from an `ordered_semiring`. -/ def of_power_series : (power_series R) →+* hahn_series Γ R := (hahn_series.emb_domain_ring_hom (nat.cast_add_monoid_hom Γ) nat.strict_mono_cast.injective (λ _ _, nat.cast_le)).comp (ring_equiv.to_ring_hom to_power_series.symm) variables {Γ} {R} lemma of_power_series_injective : function.injective (of_power_series Γ R) := emb_domain_injective.comp to_power_series.symm.injective @[simp] lemma of_power_series_apply (x : power_series R) : of_power_series Γ R x = hahn_series.emb_domain ⟨⟨(coe : ℕ → Γ), nat.strict_mono_cast.injective⟩, λ a b, begin simp only [function.embedding.coe_fn_mk], exact nat.cast_le, end⟩ (to_power_series.symm x) := rfl lemma of_power_series_apply_coeff (x : power_series R) (n : ℕ) : (of_power_series Γ R x).coeff n = power_series.coeff R n x := by simp end semiring section algebra variables (R) [comm_semiring R] {A : Type*} [semiring A] [algebra R A] /-- The `R`-algebra `hahn_series ℕ A` is isomorphic to `power_series A`. -/ @[simps] def to_power_series_alg : (hahn_series ℕ A) ≃ₐ[R] power_series A := { commutes' := λ r, begin ext n, simp only [algebra_map_apply, power_series.algebra_map_apply, ring_equiv.to_fun_eq_coe, C_apply, coeff_to_power_series], cases n, { simp only [power_series.coeff_zero_eq_constant_coeff, single_coeff_same], refl }, { simp only [n.succ_ne_zero, ne.def, not_false_iff, single_coeff_of_ne], rw [power_series.coeff_C, if_neg n.succ_ne_zero] } end, .. to_power_series } variables (Γ) (R) [ordered_semiring Γ] [nontrivial Γ] /-- Casting a power series as a Hahn series with coefficients from an `ordered_semiring` is an algebra homomorphism. -/ @[simps] def of_power_series_alg : (power_series A) →ₐ[R] hahn_series Γ A := (hahn_series.emb_domain_alg_hom (nat.cast_add_monoid_hom Γ) nat.strict_mono_cast.injective (λ _ _, nat.cast_le)).comp (alg_equiv.to_alg_hom (to_power_series_alg R).symm) end algebra section valuation variables [linear_ordered_add_comm_group Γ] [comm_ring R] [integral_domain R] instance : linear_ordered_comm_group (multiplicative Γ) := { .. (infer_instance : linear_order (multiplicative Γ)), .. (infer_instance : ordered_comm_group (multiplicative Γ)) } instance : linear_ordered_comm_group_with_zero (with_zero (multiplicative Γ)) := { zero_le_one := with_zero.zero_le 1, .. (with_zero.ordered_comm_monoid), .. (infer_instance : linear_order (with_zero (multiplicative Γ))), .. (infer_instance : comm_group_with_zero (with_zero (multiplicative Γ))) } variables (Γ) (R) /-- The additive valuation on `hahn_series Γ R`, returning the smallest index at which a Hahn Series has a nonzero coefficient, or `⊤` for the 0 series. -/ def add_val : add_valuation (hahn_series Γ R) (with_top Γ) := add_valuation.of (λ x, if x = (0 : hahn_series Γ R) then (⊤ : with_top Γ) else x.order) (if_pos rfl) ((if_neg one_ne_zero).trans (by simp [order_of_ne])) (λ x y, begin by_cases hx : x = 0, { by_cases hy : y = 0; { simp [hx, hy] } }, { by_cases hy : y = 0, { simp [hx, hy] }, { simp only [hx, hy, support_nonempty_iff, if_neg, not_false_iff, is_wf_support], by_cases hxy : x + y = 0, { simp [hxy] }, rw [if_neg hxy, ← with_top.coe_min, with_top.coe_le_coe], exact min_order_le_order_add hx hy hxy } }, end) (λ x y, begin by_cases hx : x = 0, { simp [hx] }, by_cases hy : y = 0, { simp [hy] }, rw [if_neg hx, if_neg hy, if_neg (mul_ne_zero hx hy), ← with_top.coe_add, with_top.coe_eq_coe, order_mul hx hy], end) variables {Γ} {R} lemma add_val_apply {x : hahn_series Γ R} : add_val Γ R x = if x = (0 : hahn_series Γ R) then (⊤ : with_top Γ) else x.order := add_valuation.of_apply _ @[simp] lemma add_val_apply_of_ne {x : hahn_series Γ R} (hx : x ≠ 0) : add_val Γ R x = x.order := if_neg hx lemma add_val_le_of_coeff_ne_zero {x : hahn_series Γ R} {g : Γ} (h : x.coeff g ≠ 0) : add_val Γ R x ≤ g := begin rw [add_val_apply_of_ne (ne_zero_of_coeff_ne_zero h), with_top.coe_le_coe], exact order_le_of_coeff_ne_zero h end end valuation lemma is_pwo_Union_support_powers [linear_ordered_add_comm_group Γ] [comm_ring R] [integral_domain R] {x : hahn_series Γ R} (hx : 0 < add_val Γ R x) : (⋃ n : ℕ, (x ^ n).support).is_pwo := begin apply (x.is_wf_support.is_pwo.add_submonoid_closure (λ g hg, _)).mono _, { exact with_top.coe_le_coe.1 (le_trans (le_of_lt hx) (add_val_le_of_coeff_ne_zero hg)) }, refine set.Union_subset (λ n, _), induction n with n ih; intros g hn, { simp only [exists_prop, and_true, set.mem_singleton_iff, set.set_of_eq_eq_singleton, mem_support, ite_eq_right_iff, ne.def, not_false_iff, one_ne_zero, pow_zero, not_forall, one_coeff] at hn, rw [hn, set_like.mem_coe], exact add_submonoid.zero_mem _ }, { obtain ⟨i, j, hi, hj, rfl⟩ := support_mul_subset_add_support hn, exact set_like.mem_coe.2 (add_submonoid.add_mem _ (add_submonoid.subset_closure hi) (ih hj)) } end section variables (Γ) (R) [partial_order Γ] [add_comm_monoid R] /-- An infinite family of Hahn series which has a formal coefficient-wise sum. The requirements for this are that the union of the supports of the series is well-founded, and that only finitely many series are nonzero at any given coefficient. -/ structure summable_family (α : Type*) := (to_fun : α → hahn_series Γ R) (is_pwo_Union_support' : set.is_pwo (⋃ (a : α), (to_fun a).support)) (finite_co_support' : ∀ (g : Γ), ({a | (to_fun a).coeff g ≠ 0}).finite) end namespace summable_family section add_comm_monoid variables [partial_order Γ] [add_comm_monoid R] {α : Type*} instance : has_coe_to_fun (summable_family Γ R α) := ⟨λ _, (α → hahn_series Γ R), to_fun⟩ lemma is_pwo_Union_support (s : summable_family Γ R α) : set.is_pwo (⋃ (a : α), (s a).support) := s.is_pwo_Union_support' lemma finite_co_support (s : summable_family Γ R α) (g : Γ) : (function.support (λ a, (s a).coeff g)).finite := s.finite_co_support' g lemma coe_injective : @function.injective (summable_family Γ R α) (α → hahn_series Γ R) coe_fn | ⟨f1, hU1, hf1⟩ ⟨f2, hU2, hf2⟩ h := begin change f1 = f2 at h, subst h, end @[ext] lemma ext {s t : summable_family Γ R α} (h : ∀ (a : α), s a = t a) : s = t := coe_injective $ funext h instance : has_add (summable_family Γ R α) := ⟨λ x y, { to_fun := x + y, is_pwo_Union_support' := (x.is_pwo_Union_support.union y.is_pwo_Union_support).mono (begin rw ← set.Union_union_distrib, exact set.Union_subset_Union (λ a, support_add_subset) end), finite_co_support' := λ g, ((x.finite_co_support g).union (y.finite_co_support g)).subset begin intros a ha, change (x a).coeff g + (y a).coeff g ≠ 0 at ha, rw [set.mem_union, function.mem_support, function.mem_support], contrapose! ha, rw [ha.1, ha.2, add_zero] end }⟩ instance : has_zero (summable_family Γ R α) := ⟨⟨0, by simp, by simp⟩⟩ instance : inhabited (summable_family Γ R α) := ⟨0⟩ @[simp] lemma coe_add {s t : summable_family Γ R α} : ⇑(s + t) = s + t := rfl lemma add_apply {s t : summable_family Γ R α} {a : α} : (s + t) a = s a + t a := rfl @[simp] lemma coe_zero : ((0 : summable_family Γ R α) : α → hahn_series Γ R) = 0 := rfl lemma zero_apply {a : α} : (0 : summable_family Γ R α) a = 0 := rfl instance : add_comm_monoid (summable_family Γ R α) := { add := (+), zero := 0, zero_add := λ s, by { ext, apply zero_add }, add_zero := λ s, by { ext, apply add_zero }, add_comm := λ s t, by { ext, apply add_comm }, add_assoc := λ r s t, by { ext, apply add_assoc } } /-- The infinite sum of a `summable_family` of Hahn series. -/ def hsum (s : summable_family Γ R α) : hahn_series Γ R := { coeff := λ g, ∑ᶠ i, (s i).coeff g, is_pwo_support' := s.is_pwo_Union_support.mono (λ g, begin contrapose, rw [set.mem_Union, not_exists, function.mem_support, not_not], simp_rw [mem_support, not_not], intro h, rw [finsum_congr h, finsum_zero], end) } @[simp] lemma hsum_coeff {s : summable_family Γ R α} {g : Γ} : s.hsum.coeff g = ∑ᶠ i, (s i).coeff g := rfl lemma support_hsum_subset {s : summable_family Γ R α} : s.hsum.support ⊆ ⋃ (a : α), (s a).support := λ g hg, begin rw [mem_support, hsum_coeff, finsum_eq_sum _ (s.finite_co_support _)] at hg, obtain ⟨a, h1, h2⟩ := exists_ne_zero_of_sum_ne_zero hg, rw [set.mem_Union], exact ⟨a, h2⟩, end @[simp] lemma hsum_add {s t : summable_family Γ R α} : (s + t).hsum = s.hsum + t.hsum := begin ext g, simp only [hsum_coeff, add_coeff, add_apply], exact finsum_add_distrib (s.finite_co_support _) (t.finite_co_support _) end end add_comm_monoid section add_comm_group variables [partial_order Γ] [add_comm_group R] {α : Type*} {s t : summable_family Γ R α} {a : α} instance : add_comm_group (summable_family Γ R α) := { neg := λ s, { to_fun := λ a, - s a, is_pwo_Union_support' := by { simp_rw [support_neg], exact s.is_pwo_Union_support' }, finite_co_support' := λ g, by { simp only [neg_coeff', pi.neg_apply, ne.def, neg_eq_zero], exact s.finite_co_support g } }, add_left_neg := λ a, by { ext, apply add_left_neg }, .. summable_family.add_comm_monoid } @[simp] lemma coe_neg : ⇑(-s) = - s := rfl lemma neg_apply : (-s) a = - (s a) := rfl @[simp] lemma coe_sub : ⇑(s - t) = s - t := rfl lemma sub_apply : (s - t) a = s a - t a := rfl end add_comm_group section semiring variables [ordered_cancel_add_comm_monoid Γ] [semiring R] {α : Type*} instance : has_scalar (hahn_series Γ R) (summable_family Γ R α) := { smul := λ x s, { to_fun := λ a, x * (s a), is_pwo_Union_support' := begin apply (x.is_pwo_support.add s.is_pwo_Union_support).mono, refine set.subset.trans (set.Union_subset_Union (λ a, support_mul_subset_add_support)) _, intro g, simp only [set.mem_Union, exists_imp_distrib], exact λ a ha, (set.add_subset_add (set.subset.refl _) (set.subset_Union _ a)) ha, end, finite_co_support' := λ g, begin refine ((add_antidiagonal x.is_pwo_support s.is_pwo_Union_support g).finite_to_set.bUnion (λ ij hij, _)).subset (λ a ha, _), { exact λ ij hij, function.support (λ a, (s a).coeff ij.2) }, { apply s.finite_co_support }, { obtain ⟨i, j, hi, hj, rfl⟩ := support_mul_subset_add_support ha, simp only [exists_prop, set.mem_Union, mem_add_antidiagonal, mul_coeff, ne.def, mem_support, is_pwo_support, prod.exists], refine ⟨i, j, mem_coe.2 (mem_add_antidiagonal.2 ⟨rfl, hi, set.mem_Union.2 ⟨a, hj⟩⟩), hj⟩, } end } } @[simp] lemma smul_apply {x : hahn_series Γ R} {s : summable_family Γ R α} {a : α} : (x • s) a = x * (s a) := rfl instance : module (hahn_series Γ R) (summable_family Γ R α) := { smul := (•), smul_zero := λ x, ext (λ a, mul_zero _), zero_smul := λ x, ext (λ a, zero_mul _), one_smul := λ x, ext (λ a, one_mul _), add_smul := λ x y s, ext (λ a, add_mul _ _ _), smul_add := λ x s t, ext (λ a, mul_add _ _ _), mul_smul := λ x y s, ext (λ a, mul_assoc _ _ _) } @[simp] lemma hsum_smul {x : hahn_series Γ R} {s : summable_family Γ R α} : (x • s).hsum = x * s.hsum := begin ext g, simp only [mul_coeff, hsum_coeff, smul_apply], have h : ∀ i, (s i).support ⊆ ⋃ j, (s j).support := set.subset_Union _, refine (eq.trans (finsum_congr (λ a, _)) (finsum_sum_comm (add_antidiagonal x.is_pwo_support s.is_pwo_Union_support g) (λ i ij, x.coeff (prod.fst ij) * (s i).coeff ij.snd) _)).trans _, { refine sum_subset (add_antidiagonal_mono_right (set.subset_Union _ a)) _, rintro ⟨i, j⟩ hU ha, rw mem_add_antidiagonal at *, rw [not_not.1 (λ con, ha ⟨hU.1, hU.2.1, con⟩), mul_zero] }, { rintro ⟨i, j⟩ hij, refine (s.finite_co_support j).subset _, simp_rw [function.support_subset_iff', function.mem_support, not_not], intros a ha, rw [ha, mul_zero] }, { refine (sum_congr rfl _).trans (sum_subset (add_antidiagonal_mono_right _) _).symm, { rintro ⟨i, j⟩ hij, rw mul_finsum, apply s.finite_co_support, }, { intros x hx, simp only [set.mem_Union, ne.def, mem_support], contrapose! hx, simp [hx] }, { rintro ⟨i, j⟩ hU ha, rw mem_add_antidiagonal at *, rw [← hsum_coeff, not_not.1 (λ con, ha ⟨hU.1, hU.2.1, con⟩), mul_zero] } } end /-- The summation of a `summable_family` as a `linear_map`. -/ @[simps] def lsum : (summable_family Γ R α) →ₗ[hahn_series Γ R] (hahn_series Γ R) := { to_fun := hsum, map_add' := λ _ _, hsum_add, map_smul' := λ _ _, hsum_smul } @[simp] lemma hsum_sub {R : Type*} [ring R] {s t : summable_family Γ R α} : (s - t).hsum = s.hsum - t.hsum := by rw [← lsum_apply, linear_map.map_sub, lsum_apply, lsum_apply] end semiring section of_finsupp variables [partial_order Γ] [add_comm_monoid R] {α : Type*} /-- A family with only finitely many nonzero elements is summable. -/ def of_finsupp (f : α →₀ (hahn_series Γ R)) : summable_family Γ R α := { to_fun := f, is_pwo_Union_support' := begin apply (f.support.is_pwo_sup (λ a, (f a).support) (λ a ha, (f a).is_pwo_support)).mono, intros g hg, obtain ⟨a, ha⟩ := set.mem_Union.1 hg, have haf : a ∈ f.support, { rw finsupp.mem_support_iff, contrapose! ha, rw [ha, support_zero], exact set.not_mem_empty _ }, have h : (λ i, (f i).support) a ≤ _ := le_sup haf, exact h ha, end, finite_co_support' := λ g, begin refine f.support.finite_to_set.subset (λ a ha, _), simp only [coeff.add_monoid_hom_apply, mem_coe, finsupp.mem_support_iff, ne.def, function.mem_support], contrapose! ha, simp [ha] end } @[simp] lemma coe_of_finsupp {f : α →₀ (hahn_series Γ R)} : ⇑(summable_family.of_finsupp f) = f := rfl @[simp] lemma hsum_of_finsupp {f : α →₀ (hahn_series Γ R)} : (of_finsupp f).hsum = f.sum (λ a, id) := begin ext g, simp only [hsum_coeff, coe_of_finsupp, finsupp.sum, ne.def], simp_rw [← coeff.add_monoid_hom_apply, id.def], rw [add_monoid_hom.map_sum, finsum_eq_sum_of_support_subset], intros x h, simp only [coeff.add_monoid_hom_apply, mem_coe, finsupp.mem_support_iff, ne.def], contrapose! h, simp [h] end end of_finsupp section emb_domain variables [partial_order Γ] [add_comm_monoid R] {α β : Type*} /-- A summable family can be reindexed by an embedding without changing its sum. -/ def emb_domain (s : summable_family Γ R α) (f : α ↪ β) : summable_family Γ R β := { to_fun := λ b, if h : b ∈ set.range f then s (classical.some h) else 0, is_pwo_Union_support' := begin refine s.is_pwo_Union_support.mono (set.Union_subset (λ b g h, _)), by_cases hb : b ∈ set.range f, { rw dif_pos hb at h, exact set.mem_Union.2 ⟨classical.some hb, h⟩ }, { contrapose! h, simp [hb] } end, finite_co_support' := λ g, ((s.finite_co_support g).image f).subset begin intros b h, by_cases hb : b ∈ set.range f, { simp only [ne.def, set.mem_set_of_eq, dif_pos hb] at h, exact ⟨classical.some hb, h, classical.some_spec hb⟩ }, { contrapose! h, simp only [ne.def, set.mem_set_of_eq, dif_neg hb, not_not, zero_coeff] } end } variables (s : summable_family Γ R α) (f : α ↪ β) {a : α} {b : β} lemma emb_domain_apply : s.emb_domain f b = if h : b ∈ set.range f then s (classical.some h) else 0 := rfl @[simp] lemma emb_domain_image : s.emb_domain f (f a) = s a := begin rw [emb_domain_apply, dif_pos (set.mem_range_self a)], exact congr rfl (f.injective (classical.some_spec (set.mem_range_self a))) end @[simp] lemma emb_domain_notin_range (h : b ∉ set.range f) : s.emb_domain f b = 0 := by rw [emb_domain_apply, dif_neg h] @[simp] lemma hsum_emb_domain : (s.emb_domain f).hsum = s.hsum := begin ext g, simp only [hsum_coeff, emb_domain_apply, apply_dite hahn_series.coeff, dite_apply, zero_coeff], exact finsum_emb_domain f (λ a, (s a).coeff g) end end emb_domain section powers variables [linear_ordered_add_comm_group Γ] [comm_ring R] [integral_domain R] /-- The powers of an element of positive valuation form a summable family. -/ def powers (x : hahn_series Γ R) (hx : 0 < add_val Γ R x) : summable_family Γ R ℕ := { to_fun := λ n, x ^ n, is_pwo_Union_support' := is_pwo_Union_support_powers hx, finite_co_support' := λ g, begin have hpwo := (is_pwo_Union_support_powers hx), by_cases hg : g ∈ ⋃ n : ℕ, {g | (x ^ n).coeff g ≠ 0 }, swap, { exact set.finite_empty.subset (λ n hn, hg (set.mem_Union.2 ⟨n, hn⟩)) }, apply hpwo.is_wf.induction hg, intros y ys hy, refine ((((add_antidiagonal x.is_pwo_support hpwo y).finite_to_set.bUnion (λ ij hij, hy ij.snd _ _)).image nat.succ).union (set.finite_singleton 0)).subset _, { exact (mem_add_antidiagonal.1 (mem_coe.1 hij)).2.2 }, { obtain ⟨rfl, hi, hj⟩ := mem_add_antidiagonal.1 (mem_coe.1 hij), rw [← zero_add ij.snd, ← add_assoc, add_zero], exact add_lt_add_right (with_top.coe_lt_coe.1 (lt_of_lt_of_le hx (add_val_le_of_coeff_ne_zero hi))) _, }, { intros n hn, cases n, { exact set.mem_union_right _ (set.mem_singleton 0) }, { obtain ⟨i, j, hi, hj, rfl⟩ := support_mul_subset_add_support hn, refine set.mem_union_left _ ⟨n, set.mem_Union.2 ⟨⟨i, j⟩, set.mem_Union.2 ⟨_, hj⟩⟩, rfl⟩, simp only [true_and, set.mem_Union, mem_add_antidiagonal, mem_coe, eq_self_iff_true, ne.def, mem_support, set.mem_set_of_eq], exact ⟨hi, ⟨n, hj⟩⟩ } } end } variables {x : hahn_series Γ R} (hx : 0 < add_val Γ R x) @[simp] lemma coe_powers : ⇑(powers x hx) = pow x := rfl lemma emb_domain_succ_smul_powers : (x • powers x hx).emb_domain ⟨nat.succ, nat.succ_injective⟩ = powers x hx - of_finsupp (finsupp.single 0 1) := begin apply summable_family.ext (λ n, _), cases n, { rw [emb_domain_notin_range, sub_apply, coe_powers, pow_zero, coe_of_finsupp, finsupp.single_eq_same, sub_self], rw [set.mem_range, not_exists], exact nat.succ_ne_zero }, { refine eq.trans (emb_domain_image _ ⟨nat.succ, nat.succ_injective⟩) _, simp only [pow_succ, coe_powers, coe_sub, smul_apply, coe_of_finsupp, pi.sub_apply], rw [finsupp.single_eq_of_ne (n.succ_ne_zero).symm, sub_zero] } end lemma one_sub_self_mul_hsum_powers : (1 - x) * (powers x hx).hsum = 1 := begin rw [← hsum_smul, sub_smul, one_smul, hsum_sub, ← hsum_emb_domain (x • powers x hx) ⟨nat.succ, nat.succ_injective⟩, emb_domain_succ_smul_powers], simp, end end powers end summable_family section inversion variables [linear_ordered_add_comm_group Γ] section integral_domain variables [comm_ring R] [integral_domain R] lemma unit_aux (x : hahn_series Γ R) {r : R} (hr : r * x.coeff x.order = 1) : 0 < add_val Γ R (1 - C r * (single (- x.order) 1) * x) := begin have h10 : (1 : R) ≠ 0 := one_ne_zero, have x0 : x ≠ 0 := ne_zero_of_coeff_ne_zero (right_ne_zero_of_mul_eq_one hr), refine lt_of_le_of_ne ((add_val Γ R).map_le_sub (ge_of_eq (add_val Γ R).map_one) _) _, { simp only [add_valuation.map_mul], rw [add_val_apply_of_ne x0, add_val_apply_of_ne (single_ne_zero h10), add_val_apply_of_ne _, order_C, order_single h10, with_top.coe_zero, zero_add, ← with_top.coe_add, neg_add_self, with_top.coe_zero], { exact le_refl 0 }, { exact C_ne_zero (left_ne_zero_of_mul_eq_one hr) } }, { rw [add_val_apply, ← with_top.coe_zero], split_ifs, { apply with_top.coe_ne_top }, rw [ne.def, with_top.coe_eq_coe], intro con, apply coeff_order_ne_zero h, rw [← con, mul_assoc, sub_coeff, one_coeff, if_pos rfl, C_mul_eq_smul, smul_coeff, smul_eq_mul, ← add_neg_self x.order, single_mul_coeff_add, one_mul, hr, sub_self] } end lemma is_unit_iff {x : hahn_series Γ R} : is_unit x ↔ is_unit (x.coeff x.order) := begin split, { rintro ⟨⟨u, i, ui, iu⟩, rfl⟩, refine is_unit_of_mul_eq_one (u.coeff u.order) (i.coeff i.order) ((mul_coeff_order_add_order (left_ne_zero_of_mul_eq_one ui) (right_ne_zero_of_mul_eq_one ui)).symm.trans _), rw [ui, one_coeff, if_pos], rw [← order_mul (left_ne_zero_of_mul_eq_one ui) (right_ne_zero_of_mul_eq_one ui), ui, order_one] }, { rintro ⟨⟨u, i, ui, iu⟩, h⟩, rw [units.coe_mk] at h, rw h at iu, have h := summable_family.one_sub_self_mul_hsum_powers (unit_aux x iu), rw [sub_sub_cancel] at h, exact is_unit_of_mul_is_unit_right (is_unit_of_mul_eq_one _ _ h) }, end end integral_domain instance [field R] : field (hahn_series Γ R) := { inv := λ x, if x0 : x = 0 then 0 else (C (x.coeff x.order)⁻¹ * (single (-x.order)) 1 * (summable_family.powers _ (unit_aux x (inv_mul_cancel (coeff_order_ne_zero x0)))).hsum), inv_zero := dif_pos rfl, mul_inv_cancel := λ x x0, begin refine (congr rfl (dif_neg x0)).trans _, have h := summable_family.one_sub_self_mul_hsum_powers (unit_aux x (inv_mul_cancel (coeff_order_ne_zero x0))), rw [sub_sub_cancel] at h, rw [← mul_assoc, mul_comm x, h], end, .. hahn_series.integral_domain, .. hahn_series.comm_ring } end inversion end hahn_series
db890395bca3127264025ccee6415c396e2fe786
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/run/simp7.lean
d4a503f6db0e416f99069b3e263848c80bf94816
[ "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
1,421
lean
def f (x : α) := x theorem ex1 (a : α) (b : List α) : f (a::b = []) = False := by simp [f] def length : List α → Nat | [] => 0 | a::as => length as + 1 theorem ex2 (a b c : α) (as : List α) : length (a :: b :: as) > length as := by simp [length] apply Nat.lt.step apply Nat.lt_succ_self def fact : Nat → Nat | 0 => 1 | x+1 => (x+1) * fact x theorem ex3 : fact x > 0 := by induction x with | zero => decide | succ x ih => simp [fact] apply Nat.mul_pos apply Nat.zero_lt_succ apply ih def head [Inhabited α] : List α → α | [] => default | a::_ => a theorem ex4 [Inhabited α] (a : α) (as : List α) : head (a::as) = a := by simp [head] def foo := 10 theorem ex5 (x : Nat) : foo + x = 10 + x := by simp [foo] done def g (x : Nat) : Nat := Id.run <| do let x := x return x theorem ex6 : g x = x := by simp [g, bind, pure] rfl def f1 : StateM Nat Unit := do modify fun x => g x def f2 : StateM Nat Unit := do let s ← get set <| g s theorem ex7 : f1 = f2 := by simp [f1, f2, bind, StateT.bind, get, getThe, MonadStateOf.get, StateT.get, pure, set, StateT.set, modify, modifyGet, MonadStateOf.modifyGet, StateT.modifyGet] def h (x : Nat) : Sum (Nat × Nat) Nat := Sum.inl (x, x) def bla (x : Nat) := match h x with | Sum.inl (y, z) => y + z | Sum.inr _ => 0 theorem ex8 (x : Nat) : bla x = x + x := by simp [bla, h]
27caa44605f449d8844f0888da105c0d45e86d1d
7cdf3413c097e5d36492d12cdd07030eb991d394
/src/game/world2/level1.lean
37313679d6b18b81f014132723d7f3cb7d299edb
[]
no_license
alreadydone/natural_number_game
3135b9385a9f43e74cfbf79513fc37e69b99e0b3
1a39e693df4f4e871eb449890d3c7715a25c2ec9
refs/heads/master
1,599,387,390,105
1,573,200,587,000
1,573,200,691,000
220,397,084
0
0
null
1,573,192,734,000
1,573,192,733,000
null
UTF-8
Lean
false
false
7,961
lean
import mynat.definition -- Imports the natural numbers. import mynat.add -- imports addition. namespace mynat -- hide -- World name : Addition world /- Axiom : add_zero (a : mynat) : a + 0 = a -/ /- Axiom : add_succ (a b : mynat) : a + succ(b) = succ(a + b) -/ /- Tactic : induction If you have a natural number `n : mynat` in your context (above the `⊢`) then `induction n with d hd` turns your goal into two goals, a base case with `n = 0` and an inductive step where `hd` is a proof of the `n = d` case and your goal is the `n = succ(d)` case. ### Example: If this is our local context: ``` n : mynat ⊢ 2 * n = n + n ``` then `induction n with d hd` will give us two goals: ``` ⊢ 2 * 0 = 0 + 0 ``` and ``` d : mynat, hd : 2 * d = d + d ⊢ 2 * succ d = succ d + succ d ``` -/ /- # World 2 -- addition world. Welcome to World 2, addition world. If you've only proved *one* lemma from the tutorial world with `refl` and you've never heard of the `rw` tactic, then you probably just clicked the wrong button. Go back to world 1 using the "previous world" button in the top left and then click "next level" instead of "next world". If you've done all five levels in tutorial world, then you're in the right place! Here's a reminder of what you're now equipped with. ## Data: * a type called `mynat` * a term `0 : mynat`, interpreted as the number zero. * a function `succ : mynat → mynat`, with `succ n` interpreted as "the number after `n`". * Usual numerical notation 0,1,2 etc (although 2 onwards will be of no use to us ;-) ). * Addition (with notation `a + b`). ## Theorems: * `add_zero (a : mynat) : a + 0 = a` * `add_succ (a b : mynat) : a + succ(b) = succ(a + b)` * The principle of mathematical induction. Use with `induction` (see below) * A couple more theorems which we won't need for a while and I'll insert in the world 2 theorem box on the right when we do. ## Tactics: * `refl` -- proves goals of the form `X = X` * `rw h` -- if h is a proof of `A = B`, changes all A's in the goal to B's. * `induction n with d hd` -- we're going to learn this right now. # Important thing: This is a *really* good time to check you understand about the box on the left with the drop down menus. All the theorems and all the tactics above are documented there. Have a click around and check that you can find statements of the theorems above, and explanations of the tactics above. As we go through the game, these lists will grow. The box on the left will prove invaluable as the number of theorems we prove begins to grow. On the other hand, we only need to learn one more tactic to really start going places, so let's learn about that tactic right now. ## Level 1: the `induction` tactic. OK so let's see induction in action. We're going to prove `zero_add (n : mynat) : 0 + n = n`. That is: for all natural numbers $n$, $0+n=n$. Wait -- what is going on here? Didn't we already prove that adding zero to $n$ gave us $n$? No we didn't! We proved $n + 0 = n$ -- that was called `add_zero`. We're now trying to prove `zero_add`, which says $0 + n = n$. But aren't these two theorems the same? No they're not! It is *true* that `x + y = y + x`, but we haven't *proved* it yet, and in fact we will need both `add_zero` and `zero_add` in order to prove this. In fact `x + y = y + x` is the boss level for addition world. Now `add_zero` is one of Peano's axioms, so we don't need to prove it, we already have it (indeed, if you've opened the world 2 theorem statements on the left, you can even see it). To prove `zero_add` we need to use induction. While we're here, note that `zero_add` is about zero add something, and `add_zero` is about something add zero. The names tell you what the theorem is. Anyway, let's prove `zero_add`. Delete `sorry` and replace it with `induction n with d hd,` and **don't forget the comma**. Hit enter, wait for Lean to finish thinking, and let's see what we have. When Lean has finished thinking, we see that we now have *two goals*! The induction tactic has generated for us a base case with `n = 0` (the goal at the top) and an inductive step (the goal underneath). The golden rule: **Tactics operate on the first goal** -- the goal at the top. So let's just worry about that top goal now, the base case `⊢ 0 + 0 = 0`. Remember that `add_zero` (the theorem we have already) says that `x + 0 = x` (for any $x$) so we can try `rw add_zero,` . What do you think the goal will change to? Remember to just keep focussing on the top goal, ignore the other one for now, it's not changing and we're not working on it. You should be able to solve the top goal yourself now with `refl`. When you solved this base case goal, we are now be back down to one goal -- the inductive step. Take a look at the text below the lemma to see an explanation of this goal. -/ /- Lemma For all natual numbers $n$, we have $$0 + n = n.$$ -/ lemma zero_add (n : mynat) : 0 + n = n := begin [less_leaky] induction n with d hd, rw add_zero, refl, rw add_succ, rw hd, refl end /- We're in the successor case, and your top right box should look something like this (make sure you've solved the `0 + 0 = 0` goal or your tactics will be acting on that goal instead of the goal we're talking about here): ``` case mynat.succ d : mynat, hd : 0 + d = d ⊢ 0 + succ d = succ d ``` The first line just reminds us we're doing the inductive step. We have a fixed natural number `d`, and the inductive hypothesis `hd : 0 + d = d` saying that we have a proof of `0 + d = d`. Our goal is to prove `0 + succ d = succ d`. In words, we're showing that if the lemma is true for `d`, then it's also true for the number after `d`. That's the inductive step. Once we've proved this inductive step, we will have proved `zero_add` by the principle of mathematical induction. To prove our goal, we need to use `add_succ`. We know that `add_succ 0 d` is the result that `0 + succ d = succ (0 + d)`, so the first thing we need to do is to replace the left hand side `0 + succ d` of our goal with the right hand side. We do this with the `rw` command. You can write `rw add_succ,` (or even `rw add_succ 0 d,` if you want to give Lean all the inputs instead of making it figure them out itself). Don't forget the comma though. Hit enter. The goal should change to `⊢ succ (0 + d) = succ d` Now remember our inductive hypothesis `hd : 0 + d = d`. We need to rewrite this too! Type `rw hd,` (don't forget the comma). The goal will now change to `⊢ succ d = succ d` This goal can be solved with the `refl` tactic. After you apply it, Lean will inform you that there are no goals left. You are done! ## Now venture off on your own. Those three tactics -- * `induction n with d hd,` * `rw h,` * `refl,` will get you quite a long way through this game. Using only these tactics you can beat world 2 level 4, the boss level of addition world (although you'll need more tactics to do the bonus levels in world 2 after that) and also you will be able to beat all of multiplication world including the boss level `a * b = b * a`. If you're interested in seeing more tactics, or other ways of applying the tactics you know, take a look at the <a href="http://wwwf.imperial.ac.uk/~buzzard/xena/html/source/tactics/tacticindex.html" target="blank">tactic guide</a> in the book that one of us (Buzzard) is slowly writing (opens in new tab). Or just look in the sidebar on the left -- more tactics will appear there. But we're going to stop explaining stuff carefully now. If you get stuck or want to know more about Lean (e.g. how to do much harder maths in Lean), ask in `#new members` at <a href="https://leanprover.zulipchat.com" target="blank">the Lean chat</a> (login required, real name preferred). Kevin or Mohammad or one of the other people there might be able to help. Good luck! Click on "next level" to solve some levels on your own. -/ end mynat -- hide
d3655b7d34b05fd8a5de428861401981cb941bdd
bb31430994044506fa42fd667e2d556327e18dfe
/src/analysis/complex/removable_singularity.lean
8ce0db71f46c43216c3c51129d97b10ec8de0e74
[ "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
9,572
lean
/- Copyright (c) 2022 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import analysis.calculus.fderiv_analytic import analysis.asymptotics.specific_asymptotics import analysis.complex.cauchy_integral /-! # Removable singularity theorem In this file we prove Riemann's removable singularity theorem: if `f : ℂ → E` is complex differentiable in a punctured neighborhood of a point `c` and is bounded in a punctured neighborhood of `c` (or, more generally, $f(z) - f(c)=o((z-c)^{-1})$), then it has a limit at `c` and the function `function.update f c (lim (𝓝[≠] c) f)` is complex differentiable in a neighborhood of `c`. -/ open topological_space metric set filter asymptotics function open_locale topological_space filter nnreal real universe u variables {E : Type u} [normed_add_comm_group E] [normed_space ℂ E] [complete_space E] namespace complex /-- **Removable singularity** theorem, weak version. If `f : ℂ → E` is differentiable in a punctured neighborhood of a point and is continuous at this point, then it is analytic at this point. -/ lemma analytic_at_of_differentiable_on_punctured_nhds_of_continuous_at {f : ℂ → E} {c : ℂ} (hd : ∀ᶠ z in 𝓝[≠] c, differentiable_at ℂ f z) (hc : continuous_at f c) : analytic_at ℂ f c := begin rcases (nhds_within_has_basis nhds_basis_closed_ball _).mem_iff.1 hd with ⟨R, hR0, hRs⟩, lift R to ℝ≥0 using hR0.le, replace hc : continuous_on f (closed_ball c R), { refine λ z hz, continuous_at.continuous_within_at _, rcases eq_or_ne z c with rfl | hne, exacts [hc, (hRs ⟨hz, hne⟩).continuous_at] }, exact (has_fpower_series_on_ball_of_differentiable_off_countable (countable_singleton c) hc (λ z hz, hRs (diff_subset_diff_left ball_subset_closed_ball hz)) hR0).analytic_at end lemma differentiable_on_compl_singleton_and_continuous_at_iff {f : ℂ → E} {s : set ℂ} {c : ℂ} (hs : s ∈ 𝓝 c) : differentiable_on ℂ f (s \ {c}) ∧ continuous_at f c ↔ differentiable_on ℂ f s := begin refine ⟨_, λ hd, ⟨hd.mono (diff_subset _ _), (hd.differentiable_at hs).continuous_at⟩⟩, rintro ⟨hd, hc⟩ x hx, rcases eq_or_ne x c with rfl | hne, { refine (analytic_at_of_differentiable_on_punctured_nhds_of_continuous_at _ hc) .differentiable_at.differentiable_within_at, refine eventually_nhds_within_iff.2 ((eventually_mem_nhds.2 hs).mono $ λ z hz hzx, _), exact hd.differentiable_at (inter_mem hz (is_open_ne.mem_nhds hzx)) }, { simpa only [differentiable_within_at, has_fderiv_within_at, hne.nhds_within_diff_singleton] using hd x ⟨hx, hne⟩ } end lemma differentiable_on_dslope {f : ℂ → E} {s : set ℂ} {c : ℂ} (hc : s ∈ 𝓝 c) : differentiable_on ℂ (dslope f c) s ↔ differentiable_on ℂ f s := ⟨λ h, h.of_dslope, λ h, (differentiable_on_compl_singleton_and_continuous_at_iff hc).mp $ ⟨iff.mpr (differentiable_on_dslope_of_nmem $ λ h, h.2 rfl) (h.mono $ diff_subset _ _), continuous_at_dslope_same.2 $ h.differentiable_at hc⟩⟩ /-- **Removable singularity** theorem: if `s` is a neighborhood of `c : ℂ`, a function `f : ℂ → E` is complex differentiable on `s \ {c}`, and $f(z) - f(c)=o((z-c)^{-1})$, then `f` redefined to be equal to `lim (𝓝[≠] c) f` at `c` is complex differentiable on `s`. -/ lemma differentiable_on_update_lim_of_is_o {f : ℂ → E} {s : set ℂ} {c : ℂ} (hc : s ∈ 𝓝 c) (hd : differentiable_on ℂ f (s \ {c})) (ho : (λ z, f z - f c) =o[𝓝[≠] c] (λ z, (z - c)⁻¹)) : differentiable_on ℂ (update f c (lim (𝓝[≠] c) f)) s := begin set F : ℂ → E := λ z, (z - c) • f z with hF, suffices : differentiable_on ℂ F (s \ {c}) ∧ continuous_at F c, { rw [differentiable_on_compl_singleton_and_continuous_at_iff hc, ← differentiable_on_dslope hc, dslope_sub_smul] at this; try { apply_instance }, have hc : tendsto f (𝓝[≠] c) (𝓝 (deriv F c)), from continuous_at_update_same.mp (this.continuous_on.continuous_at hc), rwa hc.lim_eq }, refine ⟨(differentiable_on_id.sub_const _).smul hd, _⟩, rw ← continuous_within_at_compl_self, have H := ho.tendsto_inv_smul_nhds_zero, have H' : tendsto (λ z, (z - c) • f c) (𝓝[≠] c) (𝓝 (F c)), from (continuous_within_at_id.tendsto.sub tendsto_const_nhds).smul tendsto_const_nhds, simpa [← smul_add, continuous_within_at] using H.add H' end /-- **Removable singularity** theorem: if `s` is a punctured neighborhood of `c : ℂ`, a function `f : ℂ → E` is complex differentiable on `s`, and $f(z) - f(c)=o((z-c)^{-1})$, then `f` redefined to be equal to `lim (𝓝[≠] c) f` at `c` is complex differentiable on `{c} ∪ s`. -/ lemma differentiable_on_update_lim_insert_of_is_o {f : ℂ → E} {s : set ℂ} {c : ℂ} (hc : s ∈ 𝓝[≠] c) (hd : differentiable_on ℂ f s) (ho : (λ z, f z - f c) =o[𝓝[≠] c] (λ z, (z - c)⁻¹)) : differentiable_on ℂ (update f c (lim (𝓝[≠] c) f)) (insert c s) := differentiable_on_update_lim_of_is_o (insert_mem_nhds_iff.2 hc) (hd.mono $ λ z hz, hz.1.resolve_left hz.2) ho /-- **Removable singularity** theorem: if `s` is a neighborhood of `c : ℂ`, a function `f : ℂ → E` is complex differentiable and is bounded on `s \ {c}`, then `f` redefined to be equal to `lim (𝓝[≠] c) f` at `c` is complex differentiable on `s`. -/ lemma differentiable_on_update_lim_of_bdd_above {f : ℂ → E} {s : set ℂ} {c : ℂ} (hc : s ∈ 𝓝 c) (hd : differentiable_on ℂ f (s \ {c})) (hb : bdd_above (norm ∘ f '' (s \ {c}))) : differentiable_on ℂ (update f c (lim (𝓝[≠] c) f)) s := differentiable_on_update_lim_of_is_o hc hd $ is_bounded_under.is_o_sub_self_inv $ let ⟨C, hC⟩ := hb in ⟨C + ‖f c‖, eventually_map.2 $ mem_nhds_within_iff_exists_mem_nhds_inter.2 ⟨s, hc, λ z hz, norm_sub_le_of_le (hC $ mem_image_of_mem _ hz) le_rfl⟩⟩ /-- **Removable singularity** theorem: if a function `f : ℂ → E` is complex differentiable on a punctured neighborhood of `c` and $f(z) - f(c)=o((z-c)^{-1})$, then `f` has a limit at `c`. -/ lemma tendsto_lim_of_differentiable_on_punctured_nhds_of_is_o {f : ℂ → E} {c : ℂ} (hd : ∀ᶠ z in 𝓝[≠] c, differentiable_at ℂ f z) (ho : (λ z, f z - f c) =o[𝓝[≠] c] (λ z, (z - c)⁻¹)) : tendsto f (𝓝[≠] c) (𝓝 $ lim (𝓝[≠] c) f) := begin rw eventually_nhds_within_iff at hd, have : differentiable_on ℂ f ({z | z ≠ c → differentiable_at ℂ f z} \ {c}), from λ z hz, (hz.1 hz.2).differentiable_within_at, have H := differentiable_on_update_lim_of_is_o hd this ho, exact continuous_at_update_same.1 (H.differentiable_at hd).continuous_at end /-- **Removable singularity** theorem: if a function `f : ℂ → E` is complex differentiable and bounded on a punctured neighborhood of `c`, then `f` has a limit at `c`. -/ lemma tendsto_lim_of_differentiable_on_punctured_nhds_of_bounded_under {f : ℂ → E} {c : ℂ} (hd : ∀ᶠ z in 𝓝[≠] c, differentiable_at ℂ f z) (hb : is_bounded_under (≤) (𝓝[≠] c) (λ z, ‖f z - f c‖)) : tendsto f (𝓝[≠] c) (𝓝 $ lim (𝓝[≠] c) f) := tendsto_lim_of_differentiable_on_punctured_nhds_of_is_o hd hb.is_o_sub_self_inv /-- The Cauchy formula for the derivative of a holomorphic function. -/ lemma two_pi_I_inv_smul_circle_integral_sub_sq_inv_smul_of_differentiable {U : set ℂ} (hU : is_open U) {c w₀ : ℂ} {R : ℝ} {f : ℂ → E} (hc : closed_ball c R ⊆ U) (hf : differentiable_on ℂ f U) (hw₀ : w₀ ∈ ball c R) : (2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), ((z - w₀) ^ 2)⁻¹ • f z = deriv f w₀ := begin -- We apply the removable singularity theorem and the Cauchy formula to `dslope f w₀` have hR : 0 < R := not_le.mp (ball_eq_empty.not.mp (nonempty_of_mem hw₀).ne_empty), have hf' : differentiable_on ℂ (dslope f w₀) U, from (differentiable_on_dslope (hU.mem_nhds ((ball_subset_closed_ball.trans hc) hw₀))).mpr hf, have h0 := (hf'.diff_cont_on_cl_ball hc).two_pi_I_inv_smul_circle_integral_sub_inv_smul hw₀, rw [← dslope_same, ← h0], congr' 1, transitivity ∮ z in C(c, R), ((z - w₀) ^ 2)⁻¹ • (f z - f w₀), { have h1 : continuous_on (λ (z : ℂ), ((z - w₀) ^ 2)⁻¹) (sphere c R), { refine ((continuous_id'.sub continuous_const).pow 2).continuous_on.inv₀ (λ w hw h, _), exact sphere_disjoint_ball.ne_of_mem hw hw₀ (sub_eq_zero.mp (sq_eq_zero_iff.mp h)) }, have h2 : circle_integrable (λ (z : ℂ), ((z - w₀) ^ 2)⁻¹ • f z) c R, { refine continuous_on.circle_integrable (pos_of_mem_ball hw₀).le _, exact h1.smul (hf.continuous_on.mono (sphere_subset_closed_ball.trans hc)) }, have h3 : circle_integrable (λ (z : ℂ), ((z - w₀) ^ 2)⁻¹ • f w₀) c R, from continuous_on.circle_integrable (pos_of_mem_ball hw₀).le (h1.smul continuous_on_const), have h4 : ∮ (z : ℂ) in C(c, R), ((z - w₀) ^ 2)⁻¹ = 0, by simpa using circle_integral.integral_sub_zpow_of_ne (dec_trivial : (-2 : ℤ) ≠ -1) c w₀ R, simp only [smul_sub, circle_integral.integral_sub h2 h3, h4, circle_integral.integral_smul_const, zero_smul, sub_zero] }, { refine circle_integral.integral_congr (pos_of_mem_ball hw₀).le (λ z hz, _), simp only [dslope_of_ne, metric.sphere_disjoint_ball.ne_of_mem hz hw₀, slope, ← smul_assoc, sq, mul_inv, ne.def, not_false_iff, vsub_eq_sub, algebra.id.smul_eq_mul] } end end complex
d54be523afbc7e12380b4ba709cf2d6ec7f15694
e61a235b8468b03aee0120bf26ec615c045005d2
/tests/compiler/phashmap3.lean
efbeb6a76bcce42777a00f80af107f4d0d81f2c5
[ "Apache-2.0" ]
permissive
SCKelemen/lean4
140dc63a80539f7c61c8e43e1c174d8500ec3230
e10507e6615ddbef73d67b0b6c7f1e4cecdd82bc
refs/heads/master
1,660,973,595,917
1,590,278,033,000
1,590,278,033,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,813
lean
import Init.Data.PersistentHashMap import Init.Lean.Data.Format open Lean PersistentHashMap abbrev Map := PersistentHashMap Nat Nat partial def formatMap : Node Nat Nat → Format | Node.collision keys vals _ => Format.sbracket $ keys.size.fold (fun i fmt => let k := keys.get! i; let v := vals.get! i; let p := if i > 0 then fmt ++ format "," ++ Format.line else fmt; p ++ "c@" ++ Format.paren (format k ++ " => " ++ format v)) Format.nil | Node.entries entries => Format.sbracket $ entries.size.fold (fun i fmt => let entry := entries.get! i; let p := if i > 0 then fmt ++ format "," ++ Format.line else fmt; p ++ match entry with | Entry.null => "<null>" | Entry.ref node => formatMap node | Entry.entry k v => Format.paren (format k ++ " => " ++ format v)) Format.nil def checkState (m : Map) : IO Unit := do unless (m.stats.maxDepth == 1) (IO.println "unexpected max depth"); unless (m.stats.numCollisions == 0) (IO.println "unexpected number of collisions") def main : IO Unit := do let m : Map := PersistentHashMap.empty; let m := m.insert 1 1; let m := m.insert (32^5 + 1) 2; let max := PersistentHashMap.maxDepth.toNat; let m := m.insert (32^max + 1) 3; let m := m.insert (32^(max+1) + 1) 4; let m := m.insert (32^(max+2) + 1) 5; unless (m.stats.maxDepth == PersistentHashMap.maxDepth.toNat) (IO.println "unexpected max depth"); unless (m.stats.numCollisions == 3) (IO.println "unexpected number of collisions"); IO.println m.stats; let m := m.erase (32^(max+1) + 1); let m := m.erase (32^(max+2) + 1); let m := m.erase (32^max + 1); unless (m.stats.maxDepth == PersistentHashMap.maxDepth.toNat - 1) (IO.println "unexpected max depth"); let m := m.erase (32^5 + 1); checkState m; IO.println m.stats
74d0f289e3f6e6210d357ce4e8f04ecf09f630c4
5178376877966e906d8301a3169ebc83b8185ddb
/src/spec/spec.lean
7c73b5f56e715c338ff995d5cee7e3cb0fea06ca
[ "LicenseRef-scancode-generic-cla", "MIT" ]
permissive
aqjune/AliveInLean
8c436f1897774df37c532e6790c7e503712a7f00
7e92791c8bc316ef391c15a9e31dbd2355b6a4d4
refs/heads/master
1,584,465,770,747
1,549,480,763,000
1,549,480,763,000
133,785,889
0
0
null
1,526,547,050,000
1,526,547,049,000
null
UTF-8
Lean
false
false
12,428
lean
-- Copyright (c) Microsoft Corporation. All rights reserved. -- Licensed under the MIT license. import ..smtexpr import ..bitvector import ..irsem import ..irsem_exec import ..irsem_smt import ..freevar import ..verifyopt import .lemmas_basic import smt2.syntax import smt2.solvers.z3 import system.io namespace spec -- This inductive predicate is needed to formally define -- the relation between sbitvec ops and bitvector ops. -- This can be used to prove ex. ∀ s b, equiv s b → equiv (optimize s) b mutual inductive b_equiv, bv_equiv with b_equiv : sbool → bool → Prop | tt: b_equiv sbool.tt bool.tt | ff: b_equiv sbool.ff bool.ff | and1: ∀ (s1 s2:sbool) (b1 b2:bool), b_equiv s1 b1 → (b1 = tt → b_equiv s2 b2) → b_equiv (s1.and s2) (band b1 b2) | and2: ∀ (s1 s2:sbool) (b1 b2:bool), b_equiv s2 b2 → (b2 = tt → b_equiv s1 b1) → b_equiv (s1.and s2) (band b1 b2) | or1: ∀ (s1 s2:sbool) (b1 b2:bool), b_equiv s1 b1 → (b1 = ff → b_equiv s2 b2) → b_equiv (s1.or s2) (bor b1 b2) | or2: ∀ (s1 s2:sbool) (b1 b2:bool), b_equiv s2 b2 → (b2 = ff → b_equiv s1 b1) → b_equiv (s1.or s2) (bor b1 b2) | xor: ∀ (s1 s2:sbool) (b1 b2:bool), b_equiv s1 b1 → b_equiv s2 b2 → b_equiv (s1.xor s2) (bxor b1 b2) | not: ∀ (s:sbool) (b:bool), b_equiv s b → b_equiv (s.not) (bnot b) | ite: ∀ (sc s1 s2: sbool) (bc b1 b2:bool), b_equiv sc bc → b_equiv s1 b1 → b_equiv s2 b2 → b_equiv (sbool.ite sc s1 s2) (cond bc b1 b2) | eq: ∀ (sz:size) (s1 s2:sbitvec sz) (b1 b2:bitvector sz), bv_equiv s1 b1 → bv_equiv s2 b2 → b_equiv (sbool.eqbv s1 s2) (bitvector.eq b1 b2) | ne: ∀ (sz:size) (s1 s2:sbitvec sz) (b1 b2:bitvector sz), bv_equiv s1 b1 → bv_equiv s2 b2 → b_equiv (sbool.nebv s1 s2) (bitvector.ne b1 b2) | ult: ∀ (sz:size) (s1 s2:sbitvec sz) (b1 b2:bitvector sz), bv_equiv s1 b1 → bv_equiv s2 b2 → b_equiv (sbool.ult s1 s2) (bitvector.ult b1 b2) | ule: ∀ (sz:size) (s1 s2:sbitvec sz) (b1 b2:bitvector sz), bv_equiv s1 b1 → bv_equiv s2 b2 → b_equiv (sbool.ule s1 s2) (bitvector.ule b1 b2) | slt: ∀ (sz:size) (s1 s2:sbitvec sz) (b1 b2:bitvector sz), bv_equiv s1 b1 → bv_equiv s2 b2 → b_equiv (sbool.slt s1 s2) (bitvector.slt b1 b2) | sle: ∀ (sz:size) (s1 s2:sbitvec sz) (b1 b2:bitvector sz), bv_equiv s1 b1 → bv_equiv s2 b2 → b_equiv (sbool.sle s1 s2) (bitvector.sle b1 b2) with bv_equiv : Π {sz:size}, sbitvec sz → bitvector sz → Prop | const: ∀ {sz:size} (n:nat) (H:n < 2^sz.val), bv_equiv (sbitvec.const sz n) ⟨n, H⟩ | add: ∀ {sz:size} (s1 s2:sbitvec sz) (b1 b2:bitvector sz), bv_equiv s1 b1 → bv_equiv s2 b2 → bv_equiv (s1.add s2) (b1.add b2) | sub: ∀ {sz:size} (s1 s2:sbitvec sz) (b1 b2:bitvector sz), bv_equiv s1 b1 → bv_equiv s2 b2 → bv_equiv (s1.sub s2) (b1.sub b2) | mul: ∀ {sz:size} (s1 s2:sbitvec sz) (b1 b2:bitvector sz), bv_equiv s1 b1 → bv_equiv s2 b2 → bv_equiv (s1.mul s2) (b1.mul b2) | and: ∀ {sz:size} (s1 s2:sbitvec sz) (b1 b2:bitvector sz), bv_equiv s1 b1 → bv_equiv s2 b2 → bv_equiv (s1.and s2) (b1.bitwise_and b2) | or: ∀ {sz:size} (s1 s2:sbitvec sz) (b1 b2:bitvector sz), bv_equiv s1 b1 → bv_equiv s2 b2 → bv_equiv (s1.or s2) (b1.bitwise_or b2) | xor: ∀ {sz:size} (s1 s2:sbitvec sz) (b1 b2:bitvector sz), bv_equiv s1 b1 → bv_equiv s2 b2 → bv_equiv (s1.xor s2) (b1.bitwise_xor b2) | shl: ∀ {sz:size} (s1 s2:sbitvec sz) (b1 b2:bitvector sz), bv_equiv s1 b1 → bv_equiv s2 b2 → (bitvector.ult b2 (bitvector.of_int sz (int.of_nat sz.val)) = tt) → bv_equiv (s1.shl s2) (b1.shl b2) | lshr: ∀ {sz:size} (s1 s2:sbitvec sz) (b1 b2:bitvector sz), bv_equiv s1 b1 → bv_equiv s2 b2 → (bitvector.ult b2 (bitvector.of_int sz (int.of_nat sz.val)) = tt) → bv_equiv (s1.lshr s2) (b1.lshr b2) | ashr: ∀ {sz:size} (s1 s2:sbitvec sz) (b1 b2:bitvector sz), bv_equiv s1 b1 → bv_equiv s2 b2 → (bitvector.ult b2 (bitvector.of_int sz (int.of_nat sz.val)) = tt) → bv_equiv (s1.ashr s2) (b1.ashr b2) | udiv: ∀ {sz:size} (s1 s2:sbitvec sz) (b1 b2:bitvector sz), bv_equiv s1 b1 → bv_equiv s2 b2 → (bitvector.ne b2 (bitvector.zero sz) = tt) → -- Div by 0 is UB bv_equiv (s1.udiv s2) (b1.udiv b2) | urem: ∀ {sz:size} (s1 s2:sbitvec sz) (b1 b2:bitvector sz), bv_equiv s1 b1 → bv_equiv s2 b2 → (bitvector.ne b2 (bitvector.zero sz) = tt) → -- Div by 0 is UB bv_equiv (s1.urem s2) (b1.urem b2) | sdiv: ∀ {sz:size} (s1 s2:sbitvec sz) (b1 b2:bitvector sz), bv_equiv s1 b1 → bv_equiv s2 b2 → (bitvector.ne b2 (bitvector.zero sz) = tt) → -- Div by 0 is UB (bitvector.ne b1 (bitvector.int_min sz) = tt) ∨ (bitvector.ne b2 (bitvector.all_one sz) = tt) → -- (INT_MIN / -1 is UB) bv_equiv (s1.sdiv s2) (b1.sdiv b2) | srem: ∀ {sz:size} (s1 s2:sbitvec sz) (b1 b2:bitvector sz), bv_equiv s1 b1 → bv_equiv s2 b2 → (bitvector.ne b2 (bitvector.zero sz) = tt) → -- Div by 0 is UB (bitvector.ne b1 (bitvector.int_min sz) = tt) ∨ (bitvector.ne b2 (bitvector.all_one sz) = tt) → -- (INT_MIN / -1 is UB) bv_equiv (s1.srem s2) (b1.srem b2) | ite: ∀ {sz:size} (sc: sbool) (bc:bool) (s1 s2:sbitvec sz) (b1 b2:bitvector sz), b_equiv sc bc → (bc = tt → bv_equiv s1 b1) → (bc = ff → bv_equiv s2 b2) → bv_equiv (sbitvec.ite sc s1 s2) (cond bc b1 b2) | of_bool: ∀ (s:sbool) (b:bool), b_equiv s b → bv_equiv (sbitvec.of_bool s) (bitvector.of_bool b) | trunc: ∀ (fromsz tosz:size) (H:tosz.val < fromsz.val) (s:sbitvec fromsz) (b:bitvector fromsz), bv_equiv s b → bv_equiv (sbitvec.trunc tosz s) (bitvector.trunc tosz b) | zext: ∀ (fromsz tosz:size) (H:tosz.val > fromsz.val) (s:sbitvec fromsz) (b:bitvector fromsz), bv_equiv s b → bv_equiv (sbitvec.zext tosz s) (bitvector.zext tosz b) | sext: ∀ (fromsz tosz:size) (H:tosz.val > fromsz.val) (s:sbitvec fromsz) (b:bitvector fromsz), bv_equiv s b → bv_equiv (sbitvec.sext tosz s) (bitvector.sext tosz b) open irsem open freevar open opt @[reducible] def intty_smt := irsem_smt.intty @[reducible] def intty_exec := irsem_exec.intty @[reducible] def valty_smt := irsem_smt.valty @[reducible] def valty_exec := irsem_exec.valty @[reducible] def equals_size : valty_smt → valty_exec → bool | (irsem.valty.ival sz1 v1 p1) (irsem.valty.ival sz2 v2 p2) := sz1 = sz2 @[reducible] def poisonty_smt := irsem_smt.poisonty @[reducible] def poisonty_exec := irsem_exec.poisonty -- Equivalence of two poison values. inductive poison_equiv: irsem_smt.poisonty → irsem_exec.poisonty → Prop | intro: Π (ps:poisonty_smt) (pe:poisonty_exec), b_equiv ps pe → poison_equiv ps pe -- Equivalence of (concrete-value, poison) inductive val_equiv: valty_smt → valty_exec → Prop | poison_intty: Π (szs:size) (vs:intty_smt szs) (ps:poisonty_smt) (sze:size) (ve:intty_exec sze) (pe:poisonty_exec), poison_equiv ps pe → szs = sze → pe = irsem_exec.pbl.ff → val_equiv (irsem.valty.ival szs vs ps) (irsem.valty.ival sze ve pe) | concrete_intty: Π (sz:size) (vs:intty_smt sz) (ps:poisonty_smt) (ve:intty_exec sz) (pe:poisonty_exec), poison_equiv ps pe → bv_equiv vs ve → pe = irsem_exec.pbl.tt → val_equiv (irsem.valty.ival sz vs ps) (irsem.valty.ival sz ve pe) @[reducible] def regfile_smt := irsem_smt.regfile @[reducible] def regfile_exec := irsem_exec.regfile -- Equivalence of two Register files. inductive regfile_equiv: regfile_smt → regfile_exec → Prop | empty: ∀ rs re, rs = regfile.empty irsem_smt → re = regfile.empty irsem_exec → regfile_equiv rs re | update: ∀ rs re vs ve rname rs' re', regfile_equiv rs re → val_equiv vs ve → rs' = regfile.update irsem_smt rs rname vs → re' = regfile.update irsem_exec re rname ve → regfile_equiv rs' re' @[reducible] def none_or_some {α β:Type} (os: option α) (oe:option β) (P:α → β → Prop) := (os = none ∧ oe = none) ∨ (∃ s e, os = some s ∧ oe = some e ∧ (P s e)) def regfile_sametypes (rs:regfile_smt) (re:regfile_exec) := ∀ (rname:string) os oe, os = regfile.get irsem_smt rs rname ∧ oe = regfile.get irsem_exec re rname → none_or_some os oe (λ vs ve, equals_size vs ve = tt) def irstate_smt := irsem_smt.irstate def irstate_exec := irsem_exec.irstate @[reducible] def has_no_ub: irstate_exec → bool | (ubs, rs) := ubs -- Equivalence of two IR states. inductive irstate_equiv: irstate_smt → irstate_exec → Prop | noub: ∀ ubs ube rs re, regfile_equiv rs re → b_equiv ubs ube → ube = irsem_exec.bbl.tt → irstate_equiv (ubs, rs) (ube, re) | ub: ∀ ubs ube rs re, -- if UB, register files may differ. b_equiv ubs ube → ube = irsem_exec.bbl.ff → regfile_sametypes rs re → -- but they should have same type! irstate_equiv (ubs, rs) (ube, re) -- Smallstep preserves equivalence between two states. def step_both := ∀ {ss:irstate_smt} {se:irstate_exec} {i:instruction} {oss':option irstate_smt} {ose':option irstate_exec} (HSTEQ: irstate_equiv ss se) (HOSS': oss' = step irsem_smt ss i) (HOSE': ose' = step irsem_exec se i), none_or_some oss' ose' (λ ss' se', irstate_equiv ss' se') -- Bigstep on an open program def encode (ss:irstate_smt) (se:irstate_exec) (η:freevar.env) := irstate_equiv (η⟦ss⟧) se def bigstep_both:= ∀ ss se p oss' ose' η (HENC:encode ss se η) (HOSS': oss' = bigstep irsem_smt ss p) (HOSE': ose' = bigstep irsem_exec se p), none_or_some oss' ose' (λ ss' se', encode ss' se' η) def init_state_encode:= ∀ (freevars:list (string × ty)) (sg sg':std_gen) ise iss (HUNQ: list.unique $ freevars.map prod.fst) (HIE:(ise, sg') = create_init_state_exec freevars sg) (HIS:iss = create_init_state_smt freevars), ∃ η, encode iss ise η -- Refinement inductive val_refines : valty_exec → valty_exec → Prop | poison_intty: ∀ sz (isrc:intty_exec sz) psrc (itgt:intty_exec sz) ptgt, psrc = irsem_exec.pbl.ff → val_refines (valty.ival sz isrc psrc) (valty.ival sz itgt ptgt) | concrete_intty: ∀ sz (isrc:intty_exec sz) psrc (itgt:intty_exec sz) ptgt, psrc = irsem_exec.pbl.tt → ptgt = irsem_exec.pbl.tt → isrc = itgt → val_refines (valty.ival sz isrc psrc) (valty.ival sz itgt ptgt) def check_val_exec_spec:= ∀ vsrc vtgt (H:opt.check_val irsem_exec vsrc vtgt = some tt), val_refines vsrc vtgt inductive ub_refines: irstate_exec → irstate_exec → Prop | noub: ∀ src tgt, irstate.getub irsem_exec src = irsem_exec.bbl.tt → irstate.getub irsem_exec tgt = irsem_exec.bbl.tt → ub_refines src tgt | ub: ∀ src tgt, irstate.getub irsem_exec src = irsem_exec.bbl.ff → ub_refines src tgt def irstate_refines (se:irstate_exec) (se':irstate_exec): Prop := ub_refines se se' ∧ ∀ r, none_or_some (irstate.getreg irsem_exec se r) (irstate.getreg irsem_exec se' r) (λ v1 v2, val_refines v1 v2) def root_refines (ssrc:irstate_exec) (stgt:irstate_exec) (root:string): Prop := ub_refines ssrc stgt ∧ (irstate.getub irsem_exec ssrc = irsem_exec.bbl.tt → ∃ v1 v2, some v1 = irstate.getreg irsem_exec ssrc root ∧ some v2 = irstate.getreg irsem_exec stgt root ∧ val_refines v1 v2) def refines_finalstate (psrc ptgt:program) (se0:irstate_exec) := ∀ se se', some se = bigstep irsem_exec se0 psrc → some se' = bigstep irsem_exec se0 ptgt → irstate_refines se se' def root_refines_finalstate (psrc ptgt:program) (se0:irstate_exec) (root:string):= ∀ se se', some se = bigstep irsem_exec se0 psrc → some se' = bigstep irsem_exec se0 ptgt → root_refines se se' root def refines_smt (psrc ptgt:program) := ∀ (η:freevar.env) ss0 se0, encode ss0 se0 η → refines_finalstate psrc ptgt se0 def root_refines_smt (psrc ptgt:program) (ss0:irstate_smt) (root:string) := ∀ (η:freevar.env) se0, encode ss0 se0 η → root_refines_finalstate psrc ptgt se0 root def refines_single_reg_correct := ∀ (psrc ptgt:program) (root:string) (ss0:irstate_smt) sb (HSREF:some sb = check_single_reg0 irsem_smt psrc ptgt root ss0) (HEQ:∀ (η0:freevar.env) e, b_equiv (η0⟦sb⟧) e → e = tt), root_refines_smt psrc ptgt ss0 root end spec
c064cee8c2dc422e6be30703bacb1640bdeeb50a
94e33a31faa76775069b071adea97e86e218a8ee
/src/data/pfunctor/univariate/basic.lean
e4aeca9a721caca886b8663976b450460413921e
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
6,294
lean
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad -/ import data.W.basic /-! # Polynomial functors This file defines polynomial functors and the W-type construction as a polynomial functor. (For the M-type construction, see pfunctor/M.lean.) -/ universe u /-- A polynomial functor `P` is given by a type `A` and a family `B` of types over `A`. `P` maps any type `α` to a new type `P.obj α`, which is defined as the sigma type `Σ x, P.B x → α`. An element of `P.obj α` is a pair `⟨a, f⟩`, where `a` is an element of a type `A` and `f : B a → α`. Think of `a` as the shape of the object and `f` as an index to the relevant elements of `α`. -/ structure pfunctor := (A : Type u) (B : A → Type u) namespace pfunctor instance : inhabited pfunctor := ⟨⟨default, default⟩⟩ variables (P : pfunctor) {α β : Type u} /-- Applying `P` to an object of `Type` -/ def obj (α : Type*) := Σ x : P.A, P.B x → α /-- Applying `P` to a morphism of `Type` -/ def map {α β : Type*} (f : α → β) : P.obj α → P.obj β := λ ⟨a, g⟩, ⟨a, f ∘ g⟩ instance obj.inhabited [inhabited P.A] [inhabited α] : inhabited (P.obj α) := ⟨⟨default, default⟩⟩ instance : functor P.obj := {map := @map P} protected theorem map_eq {α β : Type*} (f : α → β) (a : P.A) (g : P.B a → α) : @functor.map P.obj _ _ _ f ⟨a, g⟩ = ⟨a, f ∘ g⟩ := rfl protected theorem id_map {α : Type*} : ∀ x : P.obj α, id <$> x = id x := λ ⟨a, b⟩, rfl protected theorem comp_map {α β γ : Type*} (f : α → β) (g : β → γ) : ∀ x : P.obj α, (g ∘ f) <$> x = g <$> (f <$> x) := λ ⟨a, b⟩, rfl instance : is_lawful_functor P.obj := {id_map := @pfunctor.id_map P, comp_map := @pfunctor.comp_map P} /-- re-export existing definition of W-types and adapt it to a packaged definition of polynomial functor -/ def W := _root_.W_type P.B /- inhabitants of W types is awkward to encode as an instance assumption because there needs to be a value `a : P.A` such that `P.B a` is empty to yield a finite tree -/ attribute [nolint has_inhabited_instance] W variables {P} /-- root element of a W tree -/ def W.head : W P → P.A | ⟨a, f⟩ := a /-- children of the root of a W tree -/ def W.children : Π x : W P, P.B (W.head x) → W P | ⟨a, f⟩ := f /-- destructor for W-types -/ def W.dest : W P → P.obj (W P) | ⟨a, f⟩ := ⟨a, f⟩ /-- constructor for W-types -/ def W.mk : P.obj (W P) → W P | ⟨a, f⟩ := ⟨a, f⟩ @[simp] theorem W.dest_mk (p : P.obj (W P)) : W.dest (W.mk p) = p := by cases p; reflexivity @[simp] theorem W.mk_dest (p : W P) : W.mk (W.dest p) = p := by cases p; reflexivity variables (P) /-- `Idx` identifies a location inside the application of a pfunctor. For `F : pfunctor`, `x : F.obj α` and `i : F.Idx`, `i` can designate one part of `x` or is invalid, if `i.1 ≠ x.1` -/ def Idx := Σ x : P.A, P.B x instance Idx.inhabited [inhabited P.A] [inhabited (P.B default)] : inhabited P.Idx := ⟨⟨default, default⟩⟩ variables {P} /-- `x.iget i` takes the component of `x` designated by `i` if any is or returns a default value -/ def obj.iget [decidable_eq P.A] {α} [inhabited α] (x : P.obj α) (i : P.Idx) : α := if h : i.1 = x.1 then x.2 (cast (congr_arg _ h) i.2) else default @[simp] lemma fst_map {α β : Type u} (x : P.obj α) (f : α → β) : (f <$> x).1 = x.1 := by { cases x; refl } @[simp] lemma iget_map [decidable_eq P.A] {α β : Type u} [inhabited α] [inhabited β] (x : P.obj α) (f : α → β) (i : P.Idx) (h : i.1 = x.1) : (f <$> x).iget i = f (x.iget i) := by { simp only [obj.iget, fst_map, *, dif_pos, eq_self_iff_true], cases x, refl } end pfunctor /- Composition of polynomial functors. -/ namespace pfunctor /-- functor composition for polynomial functors -/ def comp (P₂ P₁ : pfunctor.{u}) : pfunctor.{u} := ⟨ Σ a₂ : P₂.1, P₂.2 a₂ → P₁.1, λ a₂a₁, Σ u : P₂.2 a₂a₁.1, P₁.2 (a₂a₁.2 u) ⟩ /-- constructor for composition -/ def comp.mk (P₂ P₁ : pfunctor.{u}) {α : Type} (x : P₂.obj (P₁.obj α)) : (comp P₂ P₁).obj α := ⟨ ⟨ x.1, sigma.fst ∘ x.2 ⟩, λ a₂a₁, (x.2 a₂a₁.1).2 a₂a₁.2 ⟩ /-- destructor for composition -/ def comp.get (P₂ P₁ : pfunctor.{u}) {α : Type} (x : (comp P₂ P₁).obj α) : P₂.obj (P₁.obj α) := ⟨ x.1.1, λ a₂, ⟨x.1.2 a₂, λ a₁, x.2 ⟨a₂,a₁⟩ ⟩ ⟩ end pfunctor /- Lifting predicates and relations. -/ namespace pfunctor variables {P : pfunctor.{u}} open functor theorem liftp_iff {α : Type u} (p : α → Prop) (x : P.obj α) : liftp p x ↔ ∃ a f, x = ⟨a, f⟩ ∧ ∀ i, p (f i) := begin split, { rintros ⟨y, hy⟩, cases h : y with a f, refine ⟨a, λ i, (f i).val, _, λ i, (f i).property⟩, rw [←hy, h, pfunctor.map_eq] }, rintros ⟨a, f, xeq, pf⟩, use ⟨a, λ i, ⟨f i, pf i⟩⟩, rw [xeq], reflexivity end theorem liftp_iff' {α : Type u} (p : α → Prop) (a : P.A) (f : P.B a → α) : @liftp.{u} P.obj _ α p ⟨a,f⟩ ↔ ∀ i, p (f i) := begin simp only [liftp_iff, sigma.mk.inj_iff]; split; intro, { casesm* [Exists _, _ ∧ _], subst_vars, assumption }, repeat { constructor <|> assumption } end theorem liftr_iff {α : Type u} (r : α → α → Prop) (x y : P.obj α) : liftr r x y ↔ ∃ a f₀ f₁, x = ⟨a, f₀⟩ ∧ y = ⟨a, f₁⟩ ∧ ∀ i, r (f₀ i) (f₁ i) := begin split, { rintros ⟨u, xeq, yeq⟩, cases h : u with a f, use [a, λ i, (f i).val.fst, λ i, (f i).val.snd], split, { rw [←xeq, h], refl }, split, { rw [←yeq, h], refl }, intro i, exact (f i).property }, rintros ⟨a, f₀, f₁, xeq, yeq, h⟩, use ⟨a, λ i, ⟨(f₀ i, f₁ i), h i⟩⟩, split, { rw [xeq], refl }, rw [yeq], refl end open set theorem supp_eq {α : Type u} (a : P.A) (f : P.B a → α) : @supp.{u} P.obj _ α (⟨a,f⟩ : P.obj α) = f '' univ := begin ext, simp only [supp, image_univ, mem_range, mem_set_of_eq], split; intro h, { apply @h (λ x, ∃ (y : P.B a), f y = x), rw liftp_iff', intro, refine ⟨_,rfl⟩ }, { simp only [liftp_iff'], cases h, subst x, tauto } end end pfunctor
0c445da094a45942f434ef1cbc876fb2ca6bba54
9c1ad797ec8a5eddb37d34806c543602d9a6bf70
/examples/categories/cartesian_product_symmetry.lean
320fef39e62938a302fe90ef177b740d4662de34
[]
no_license
timjb/lean-category-theory
816eefc3a0582c22c05f4ee1c57ed04e57c0982f
12916cce261d08bb8740bc85e0175b75fb2a60f4
refs/heads/master
1,611,078,926,765
1,492,080,000,000
1,492,080,000,000
88,348,246
0
0
null
1,492,262,499,000
1,492,262,498,000
null
UTF-8
Lean
false
false
854
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Stephen Morgan, Scott Morrison import .cartesian_product open tqft.categories open tqft.categories.functor open tqft.categories.products open tqft.categories.natural_transformation namespace tqft.categories.monoidal_category definition SymmetryOnCartesianProductOfCategories : Symmetry CartesianProductOfCategories := { braiding := { morphism := { components := λ p, SwitchProductCategory p.1 p.2, naturality := ♮ }, inverse := { components := λ p, SwitchProductCategory p.2 p.1, naturality := ♮ }, witness_1 := ♯, witness_2 := ♯ }, hexagon_1 := ♯, hexagon_2 := ♯, symmetry := ♯ } end tqft.categories.monoidal_category
75731eff65dfc573d83bb8e4a04259d65f7a4cc7
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/star/subalgebra.lean
8e33aa409a99a1d3755ac01df91845df4ce04726
[ "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
26,476
lean
/- Copyright (c) 2022 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Jireh Loreaux -/ import algebra.star.star_alg_hom import algebra.algebra.subalgebra.basic import algebra.star.pointwise import algebra.star.module import ring_theory.adjoin.basic /-! # Star subalgebras A *-subalgebra is a subalgebra of a *-algebra which is closed under *. The centralizer of a *-closed set is a *-subalgebra. -/ universes u v set_option old_structure_cmd true /-- A *-subalgebra is a subalgebra of a *-algebra which is closed under *. -/ structure star_subalgebra (R : Type u) (A : Type v) [comm_semiring R] [star_ring R] [semiring A] [star_ring A] [algebra R A] [star_module R A] extends subalgebra R A : Type v := (star_mem' {a} : a ∈ carrier → star a ∈ carrier) namespace star_subalgebra /-- Forgetting that a *-subalgebra is closed under *. -/ add_decl_doc star_subalgebra.to_subalgebra variables {F R A B C : Type*} [comm_semiring R] [star_ring R] variables [semiring A] [star_ring A] [algebra R A] [star_module R A] variables [semiring B] [star_ring B] [algebra R B] [star_module R B] variables [semiring C] [star_ring C] [algebra R C] [star_module R C] instance : set_like (star_subalgebra R A) A := ⟨star_subalgebra.carrier, λ p q h, by cases p; cases q; congr'⟩ instance : star_mem_class (star_subalgebra R A) A := { star_mem := λ s a, s.star_mem' } instance : subsemiring_class (star_subalgebra R A) A := { add_mem := add_mem', mul_mem := mul_mem', one_mem := one_mem', zero_mem := zero_mem' } instance {R A} [comm_ring R] [star_ring R] [ring A] [star_ring A] [algebra R A] [star_module R A] : subring_class (star_subalgebra R A) A := { neg_mem := λ s a ha, show -a ∈ s.to_subalgebra, from neg_mem ha } -- this uses the `has_star` instance `s` inherits from `star_mem_class (star_subalgebra R A) A` instance (s : star_subalgebra R A) : star_ring s := { star := star, star_involutive := λ r, subtype.ext (star_star r), star_mul := λ r₁ r₂, subtype.ext (star_mul r₁ r₂), star_add := λ r₁ r₂, subtype.ext (star_add r₁ r₂) } instance (s : star_subalgebra R A) : algebra R s := s.to_subalgebra.algebra' instance (s : star_subalgebra R A) : star_module R s := { star_smul := λ r a, subtype.ext (star_smul r a) } @[simp] lemma mem_carrier {s : star_subalgebra R A} {x : A} : x ∈ s.carrier ↔ x ∈ s := iff.rfl @[ext] theorem ext {S T : star_subalgebra R A} (h : ∀ x : A, x ∈ S ↔ x ∈ T) : S = T := set_like.ext h @[simp] lemma mem_to_subalgebra {S : star_subalgebra R A} {x} : x ∈ S.to_subalgebra ↔ x ∈ S := iff.rfl @[simp] lemma coe_to_subalgebra (S : star_subalgebra R A) : (S.to_subalgebra : set A) = S := rfl theorem to_subalgebra_injective : function.injective (to_subalgebra : star_subalgebra R A → subalgebra R A) := λ S T h, ext $ λ x, by rw [← mem_to_subalgebra, ← mem_to_subalgebra, h] theorem to_subalgebra_inj {S U : star_subalgebra R A} : S.to_subalgebra = U.to_subalgebra ↔ S = U := to_subalgebra_injective.eq_iff lemma to_subalgebra_le_iff {S₁ S₂ : star_subalgebra R A} : S₁.to_subalgebra ≤ S₂.to_subalgebra ↔ S₁ ≤ S₂ := iff.rfl /-- Copy of a star subalgebra with a new `carrier` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (S : star_subalgebra R A) (s : set A) (hs : s = ↑S) : star_subalgebra R A := { carrier := s, add_mem' := λ _ _, hs.symm ▸ S.add_mem', mul_mem' := λ _ _, hs.symm ▸ S.mul_mem', algebra_map_mem' := hs.symm ▸ S.algebra_map_mem', star_mem' := λ _, hs.symm ▸ S.star_mem' } @[simp] lemma coe_copy (S : star_subalgebra R A) (s : set A) (hs : s = ↑S) : (S.copy s hs : set A) = s := rfl lemma copy_eq (S : star_subalgebra R A) (s : set A) (hs : s = ↑S) : S.copy s hs = S := set_like.coe_injective hs variables (S : star_subalgebra R A) theorem algebra_map_mem (r : R) : algebra_map R A r ∈ S := S.algebra_map_mem' r theorem srange_le : (algebra_map R A).srange ≤ S.to_subalgebra.to_subsemiring := λ x ⟨r, hr⟩, hr ▸ S.algebra_map_mem r theorem range_subset : set.range (algebra_map R A) ⊆ S := λ x ⟨r, hr⟩, hr ▸ S.algebra_map_mem r theorem range_le : set.range (algebra_map R A) ≤ S := S.range_subset protected theorem smul_mem {x : A} (hx : x ∈ S) (r : R) : r • x ∈ S := (algebra.smul_def r x).symm ▸ mul_mem (S.algebra_map_mem r) hx /-- Embedding of a subalgebra into the algebra. -/ def subtype : S →⋆ₐ[R] A := by refine_struct { to_fun := (coe : S → A) }; intros; refl @[simp] lemma coe_subtype : (S.subtype : S → A) = coe := rfl lemma subtype_apply (x : S) : S.subtype x = (x : A) := rfl @[simp] lemma to_subalgebra_subtype : S.to_subalgebra.val = S.subtype.to_alg_hom := rfl /-- The inclusion map between `star_subalgebra`s given by `subtype.map id` as a `star_alg_hom`. -/ @[simps] def inclusion {S₁ S₂ : star_subalgebra R A} (h : S₁ ≤ S₂) : S₁ →⋆ₐ[R] S₂ := { to_fun := subtype.map id h, map_one' := rfl, map_mul' := λ x y, rfl, map_zero' := rfl, map_add' := λ x y, rfl, commutes' := λ z, rfl, map_star' := λ x, rfl } lemma inclusion_injective {S₁ S₂ : star_subalgebra R A} (h : S₁ ≤ S₂) : function.injective $ inclusion h := set.inclusion_injective h @[simp] lemma subtype_comp_inclusion {S₁ S₂ : star_subalgebra R A} (h : S₁ ≤ S₂) : S₂.subtype.comp (inclusion h) = S₁.subtype := rfl section map /-- Transport a star subalgebra via a star algebra homomorphism. -/ def map (f : A →⋆ₐ[R] B) (S : star_subalgebra R A) : star_subalgebra R B := { star_mem' := begin rintro _ ⟨a, ha, rfl⟩, exact map_star f a ▸ set.mem_image_of_mem _ (S.star_mem' ha), end, .. S.to_subalgebra.map f.to_alg_hom } lemma map_mono {S₁ S₂ : star_subalgebra R A} {f : A →⋆ₐ[R] B} : S₁ ≤ S₂ → S₁.map f ≤ S₂.map f := set.image_subset f lemma map_injective {f : A →⋆ₐ[R] B} (hf : function.injective f) : function.injective (map f) := λ S₁ S₂ ih, ext $ set.ext_iff.1 $ set.image_injective.2 hf $ set.ext $ set_like.ext_iff.mp ih @[simp] lemma map_id (S : star_subalgebra R A) : S.map (star_alg_hom.id R A) = S := set_like.coe_injective $ set.image_id _ lemma map_map (S : star_subalgebra R A) (g : B →⋆ₐ[R] C) (f : A →⋆ₐ[R] B) : (S.map f).map g = S.map (g.comp f) := set_like.coe_injective $ set.image_image _ _ _ lemma mem_map {S : star_subalgebra R A} {f : A →⋆ₐ[R] B} {y : B} : y ∈ map f S ↔ ∃ x ∈ S, f x = y := subsemiring.mem_map lemma map_to_subalgebra {S : star_subalgebra R A} {f : A →⋆ₐ[R] B} : (S.map f).to_subalgebra = S.to_subalgebra.map f.to_alg_hom := set_like.coe_injective rfl @[simp] lemma coe_map (S : star_subalgebra R A) (f : A →⋆ₐ[R] B) : (S.map f : set B) = f '' S := rfl /-- Preimage of a star subalgebra under an star algebra homomorphism. -/ def comap (f : A →⋆ₐ[R] B) (S : star_subalgebra R B) : star_subalgebra R A := { star_mem' := λ a ha, show f (star a) ∈ S, from (map_star f a).symm ▸ star_mem ha, .. S.to_subalgebra.comap f.to_alg_hom } theorem map_le_iff_le_comap {S : star_subalgebra R A} {f : A →⋆ₐ[R] B} {U : star_subalgebra R B} : map f S ≤ U ↔ S ≤ comap f U := set.image_subset_iff lemma gc_map_comap (f : A →⋆ₐ[R] B) : galois_connection (map f) (comap f) := λ S U, map_le_iff_le_comap lemma comap_mono {S₁ S₂ : star_subalgebra R B} {f : A →⋆ₐ[R] B} : S₁ ≤ S₂ → S₁.comap f ≤ S₂.comap f := set.preimage_mono lemma comap_injective {f : A →⋆ₐ[R] B} (hf : function.surjective f) : function.injective (comap f) := λ S₁ S₂ h, ext $ λ b, let ⟨x, hx⟩ := hf b in let this := set_like.ext_iff.1 h x in hx ▸ this @[simp] lemma comap_id (S : star_subalgebra R A) : S.comap (star_alg_hom.id R A) = S := set_like.coe_injective $ set.preimage_id lemma comap_comap (S : star_subalgebra R C) (g : B →⋆ₐ[R] C) (f : A →⋆ₐ[R] B) : (S.comap g).comap f = S.comap (g.comp f) := set_like.coe_injective $ set.preimage_preimage @[simp] lemma mem_comap (S : star_subalgebra R B) (f : A →⋆ₐ[R] B) (x : A) : x ∈ S.comap f ↔ f x ∈ S := iff.rfl @[simp, norm_cast] lemma coe_comap (S : star_subalgebra R B) (f : A →⋆ₐ[R] B) : (S.comap f : set A) = f ⁻¹' (S : set B) := rfl end map section centralizer variables (R) {A} /-- The centralizer, or commutant, of a *-closed set as star subalgebra. -/ def centralizer (s : set A) (w : ∀ (a : A), a ∈ s → star a ∈ s) : star_subalgebra R A := { star_mem' := λ x xm y hy, by simpa using congr_arg star (xm _ (w _ hy)).symm, ..subalgebra.centralizer R s, } @[simp] lemma coe_centralizer (s : set A) (w : ∀ (a : A), a ∈ s → star a ∈ s) : (centralizer R s w : set A) = s.centralizer := rfl lemma mem_centralizer_iff {s : set A} {w} {z : A} : z ∈ centralizer R s w ↔ ∀ g ∈ s, g * z = z * g := iff.rfl lemma centralizer_le (s t : set A) (ws : ∀ (a : A), a ∈ s → star a ∈ s) (wt : ∀ (a : A), a ∈ t → star a ∈ t) (h : s ⊆ t) : centralizer R t wt ≤ centralizer R s ws := set.centralizer_subset h end centralizer end star_subalgebra /-! ### The star closure of a subalgebra -/ namespace subalgebra open_locale pointwise variables {F R A B : Type*} [comm_semiring R] [star_ring R] variables [semiring A] [algebra R A] [star_ring A] [star_module R A] variables [semiring B] [algebra R B] [star_ring B] [star_module R B] /-- The pointwise `star` of a subalgebra is a subalgebra. -/ instance : has_involutive_star (subalgebra R A) := { star := λ S, { carrier := star S.carrier, mul_mem' := λ x y hx hy, begin simp only [set.mem_star, subalgebra.mem_carrier] at *, exact (star_mul x y).symm ▸ mul_mem hy hx, end, one_mem' := set.mem_star.mp ((star_one A).symm ▸ one_mem S : star (1 : A) ∈ S), add_mem' := λ x y hx hy, begin simp only [set.mem_star, subalgebra.mem_carrier] at *, exact (star_add x y).symm ▸ add_mem hx hy, end, zero_mem' := set.mem_star.mp ((star_zero A).symm ▸ zero_mem S : star (0 : A) ∈ S), algebra_map_mem' := λ r, by simpa only [set.mem_star, subalgebra.mem_carrier, ←algebra_map_star_comm] using S.algebra_map_mem (star r) }, star_involutive := λ S, subalgebra.ext $ λ x, ⟨λ hx, (star_star x ▸ hx), λ hx, ((star_star x).symm ▸ hx : star (star x) ∈ S)⟩ } @[simp] lemma mem_star_iff (S : subalgebra R A) (x : A) : x ∈ star S ↔ star x ∈ S := iff.rfl @[simp] lemma star_mem_star_iff (S : subalgebra R A) (x : A) : star x ∈ star S ↔ x ∈ S := by simpa only [star_star] using mem_star_iff S (star x) @[simp] lemma coe_star (S : subalgebra R A) : ((star S : subalgebra R A) : set A) = star S := rfl lemma star_mono : monotone (star : subalgebra R A → subalgebra R A) := λ _ _ h _ hx, h hx variables (R) /-- The star operation on `subalgebra` commutes with `algebra.adjoin`. -/ lemma star_adjoin_comm (s : set A) : star (algebra.adjoin R s) = algebra.adjoin R (star s) := have this : ∀ t : set A, algebra.adjoin R (star t) ≤ star (algebra.adjoin R t), from λ t, algebra.adjoin_le (λ x hx, algebra.subset_adjoin hx), le_antisymm (by simpa only [star_star] using subalgebra.star_mono (this (star s))) (this s) variables {R} /-- The `star_subalgebra` obtained from `S : subalgebra R A` by taking the smallest subalgebra containing both `S` and `star S`. -/ @[simps] def star_closure (S : subalgebra R A) : star_subalgebra R A := { star_mem' := λ a ha, begin simp only [subalgebra.mem_carrier, ←(@algebra.gi R A _ _ _).l_sup_u _ _] at *, rw [←mem_star_iff _ a, star_adjoin_comm], convert ha, simp [set.union_comm], end, .. S ⊔ star S } lemma star_closure_le {S₁ : subalgebra R A} {S₂ : star_subalgebra R A} (h : S₁ ≤ S₂.to_subalgebra) : S₁.star_closure ≤ S₂ := star_subalgebra.to_subalgebra_le_iff.1 $ sup_le h $ λ x hx, (star_star x ▸ star_mem (show star x ∈ S₂, from h $ (S₁.mem_star_iff _).1 hx) : x ∈ S₂) lemma star_closure_le_iff {S₁ : subalgebra R A} {S₂ : star_subalgebra R A} : S₁.star_closure ≤ S₂ ↔ S₁ ≤ S₂.to_subalgebra := ⟨λ h, le_sup_left.trans h, star_closure_le⟩ end subalgebra /-! ### The star subalgebra generated by a set -/ namespace star_subalgebra variables {F R A B : Type*} [comm_semiring R] [star_ring R] variables [semiring A] [algebra R A] [star_ring A] [star_module R A] variables [semiring B] [algebra R B] [star_ring B] [star_module R B] variables (R) /-- The minimal star subalgebra that contains `s`. -/ @[simps] def adjoin (s : set A) : star_subalgebra R A := { star_mem' := λ x hx, by rwa [subalgebra.mem_carrier, ←subalgebra.mem_star_iff, subalgebra.star_adjoin_comm, set.union_star, star_star, set.union_comm], .. (algebra.adjoin R (s ∪ star s)) } lemma adjoin_eq_star_closure_adjoin (s : set A) : adjoin R s = (algebra.adjoin R s).star_closure := to_subalgebra_injective $ show algebra.adjoin R (s ∪ star s) = algebra.adjoin R s ⊔ star (algebra.adjoin R s), from (subalgebra.star_adjoin_comm R s).symm ▸ algebra.adjoin_union s (star s) lemma adjoin_to_subalgebra (s : set A) : (adjoin R s).to_subalgebra = (algebra.adjoin R (s ∪ star s)) := rfl lemma subset_adjoin (s : set A) : s ⊆ adjoin R s := (set.subset_union_left s (star s)).trans algebra.subset_adjoin lemma star_subset_adjoin (s : set A) : star s ⊆ adjoin R s := (set.subset_union_right s (star s)).trans algebra.subset_adjoin lemma self_mem_adjoin_singleton (x : A) : x ∈ adjoin R ({x} : set A) := algebra.subset_adjoin $ set.mem_union_left _ (set.mem_singleton x) lemma star_self_mem_adjoin_singleton (x : A) : star x ∈ adjoin R ({x} : set A) := star_mem $ self_mem_adjoin_singleton R x variables {R} protected lemma gc : galois_connection (adjoin R : set A → star_subalgebra R A) coe := begin intros s S, rw [←to_subalgebra_le_iff, adjoin_to_subalgebra, algebra.adjoin_le_iff, coe_to_subalgebra], exact ⟨λ h, (set.subset_union_left s _).trans h, λ h, set.union_subset h $ λ x hx, star_star x ▸ star_mem (show star x ∈ S, from h hx)⟩, end /-- Galois insertion between `adjoin` and `coe`. -/ protected def gi : galois_insertion (adjoin R : set A → star_subalgebra R A) coe := { choice := λ s hs, (adjoin R s).copy s $ le_antisymm (star_subalgebra.gc.le_u_l s) hs, gc := star_subalgebra.gc, le_l_u := λ S, (star_subalgebra.gc (S : set A) (adjoin R S)).1 $ le_rfl, choice_eq := λ _ _, star_subalgebra.copy_eq _ _ _ } lemma adjoin_le {S : star_subalgebra R A} {s : set A} (hs : s ⊆ S) : adjoin R s ≤ S := star_subalgebra.gc.l_le hs lemma adjoin_le_iff {S : star_subalgebra R A} {s : set A} : adjoin R s ≤ S ↔ s ⊆ S := star_subalgebra.gc _ _ lemma _root_.subalgebra.star_closure_eq_adjoin (S : subalgebra R A) : S.star_closure = adjoin R (S : set A) := le_antisymm (subalgebra.star_closure_le_iff.2 $ subset_adjoin R (S : set A)) (adjoin_le (le_sup_left : S ≤ S ⊔ star S)) /-- If some predicate holds for all `x ∈ (s : set A)` and this predicate is closed under the `algebra_map`, addition, multiplication and star operations, then it holds for `a ∈ adjoin R s`. -/ lemma adjoin_induction {s : set A} {p : A → Prop} {a : A} (h : a ∈ adjoin R s) (Hs : ∀ (x : A), x ∈ s → p x) (Halg : ∀ (r : R), p (algebra_map R A r)) (Hadd : ∀ (x y : A), p x → p y → p (x + y)) (Hmul : ∀ (x y : A), p x → p y → p (x * y)) (Hstar : ∀ (x : A), p x → p (star x)) : p a := algebra.adjoin_induction h (λ x hx, hx.elim (λ hx, Hs x hx) (λ hx, star_star x ▸ Hstar _ (Hs _ hx))) Halg Hadd Hmul lemma adjoin_induction₂ {s : set A} {p : A → A → Prop} {a b : A} (ha : a ∈ adjoin R s) (hb : b ∈ adjoin R s) (Hs : ∀ (x : A), x ∈ s → ∀ (y : A), y ∈ s → p x y) (Halg : ∀ (r₁ r₂ : R), p (algebra_map R A r₁) (algebra_map R A r₂)) (Halg_left : ∀ (r : R) (x : A), x ∈ s → p (algebra_map R A r) x) (Halg_right : ∀ (r : R) (x : A), x ∈ s → p x (algebra_map R A r)) (Hadd_left : ∀ (x₁ x₂ y : A), p x₁ y → p x₂ y → p (x₁ + x₂) y) (Hadd_right : ∀ (x y₁ y₂ : A), p x y₁ → p x y₂ → p x (y₁ + y₂)) (Hmul_left : ∀ (x₁ x₂ y : A), p x₁ y → p x₂ y → p (x₁ * x₂) y) (Hmul_right : ∀ (x y₁ y₂ : A), p x y₁ → p x y₂ → p x (y₁ * y₂)) (Hstar : ∀ (x y : A), p x y → p (star x) (star y)) (Hstar_left : ∀ (x y : A), p x y → p (star x) y) (Hstar_right : ∀ (x y : A), p x y → p x (star y)) : p a b := begin refine algebra.adjoin_induction₂ ha hb (λ x hx y hy, _) Halg (λ r x hx, _) (λ r x hx, _) Hadd_left Hadd_right Hmul_left Hmul_right, { cases hx; cases hy, exacts [Hs x hx y hy, star_star y ▸ Hstar_right _ _ (Hs _ hx _ hy), star_star x ▸ Hstar_left _ _ (Hs _ hx _ hy), star_star x ▸ star_star y ▸ Hstar _ _ (Hs _ hx _ hy)] }, { cases hx, exacts [Halg_left _ _ hx, star_star x ▸ Hstar_right _ _ (Halg_left r _ hx)] }, { cases hx, exacts [Halg_right _ _ hx, star_star x ▸ Hstar_left _ _ (Halg_right r _ hx)] }, end /-- The difference with `star_subalgebra.adjoin_induction` is that this acts on the subtype. -/ lemma adjoin_induction' {s : set A} {p : adjoin R s → Prop} (a : adjoin R s) (Hs : ∀ x (h : x ∈ s), p ⟨x, subset_adjoin R s h⟩) (Halg : ∀ r, p (algebra_map R _ r)) (Hadd : ∀ x y, p x → p y → p (x + y)) (Hmul : ∀ x y, p x → p y → p (x * y)) (Hstar : ∀ x, p x → p (star x)) : p a := subtype.rec_on a $ λ b hb, begin refine exists.elim _ (λ (hb : b ∈ adjoin R s) (hc : p ⟨b, hb⟩), hc), apply adjoin_induction hb, exacts [λ x hx, ⟨subset_adjoin R s hx, Hs x hx⟩, λ r, ⟨star_subalgebra.algebra_map_mem _ r, Halg r⟩, (λ x y hx hy, exists.elim hx $ λ hx' hx, exists.elim hy $ λ hy' hy, ⟨add_mem hx' hy', Hadd _ _ hx hy⟩), (λ x y hx hy, exists.elim hx $ λ hx' hx, exists.elim hy $ λ hy' hy, ⟨mul_mem hx' hy', Hmul _ _ hx hy⟩), λ x hx, exists.elim hx (λ hx' hx, ⟨star_mem hx', Hstar _ hx⟩)] end variables (R) /-- If all elements of `s : set A` commute pairwise and also commute pairwise with elements of `star s`, then `star_subalgebra.adjoin R s` is commutative. See note [reducible non-instances]. -/ @[reducible] def adjoin_comm_semiring_of_comm {s : set A} (hcomm : ∀ (a : A), a ∈ s → ∀ (b : A), b ∈ s → a * b = b * a) (hcomm_star : ∀ (a : A), a ∈ s → ∀ (b : A), b ∈ s → a * star b = star b * a) : comm_semiring (adjoin R s) := { mul_comm := begin rintro ⟨x, hx⟩ ⟨y, hy⟩, ext, simp only [set_like.coe_mk, mul_mem_class.mk_mul_mk], rw [←mem_to_subalgebra, adjoin_to_subalgebra] at hx hy, letI : comm_semiring (algebra.adjoin R (s ∪ star s)) := algebra.adjoin_comm_semiring_of_comm R begin intros a ha b hb, cases ha; cases hb, exacts [hcomm _ ha _ hb, star_star b ▸ hcomm_star _ ha _ hb, star_star a ▸ (hcomm_star _ hb _ ha).symm, by simpa only [star_mul, star_star] using congr_arg star (hcomm _ hb _ ha)], end, exact congr_arg coe (mul_comm (⟨x, hx⟩ : algebra.adjoin R (s ∪ star s)) ⟨y, hy⟩), end, ..(adjoin R s).to_subalgebra.to_semiring } /-- If all elements of `s : set A` commute pairwise and also commute pairwise with elements of `star s`, then `star_subalgebra.adjoin R s` is commutative. See note [reducible non-instances]. -/ @[reducible] def adjoin_comm_ring_of_comm (R : Type u) {A : Type v} [comm_ring R] [star_ring R] [ring A] [algebra R A] [star_ring A] [star_module R A] {s : set A} (hcomm : ∀ (a : A), a ∈ s → ∀ (b : A), b ∈ s → a * b = b * a) (hcomm_star : ∀ (a : A), a ∈ s → ∀ (b : A), b ∈ s → a * star b = star b * a) : comm_ring (adjoin R s) := { ..star_subalgebra.adjoin_comm_semiring_of_comm R hcomm hcomm_star, ..(adjoin R s).to_subalgebra.to_ring } /-- The star subalgebra `star_subalgebra.adjoin R {x}` generated by a single `x : A` is commutative if `x` is normal. -/ instance adjoin_comm_semiring_of_is_star_normal (x : A) [is_star_normal x] : comm_semiring (adjoin R ({x} : set A)) := adjoin_comm_semiring_of_comm R (λ a ha b hb, by { rw [set.mem_singleton_iff] at ha hb, rw [ha, hb] }) (λ a ha b hb, by { rw [set.mem_singleton_iff] at ha hb, simpa only [ha, hb] using (star_comm_self' x).symm }) /-- The star subalgebra `star_subalgebra.adjoin R {x}` generated by a single `x : A` is commutative if `x` is normal. -/ instance adjoin_comm_ring_of_is_star_normal (R : Type u) {A : Type v} [comm_ring R] [star_ring R] [ring A] [algebra R A] [star_ring A] [star_module R A] (x : A) [is_star_normal x] : comm_ring (adjoin R ({x} : set A)) := { mul_comm := mul_comm, ..(adjoin R ({x} : set A)).to_subalgebra.to_ring } /-! ### Complete lattice structure -/ variables {F R A B} instance : complete_lattice (star_subalgebra R A) := galois_insertion.lift_complete_lattice star_subalgebra.gi instance : inhabited (star_subalgebra R A) := ⟨⊤⟩ @[simp] lemma coe_top : (↑(⊤ : star_subalgebra R A) : set A) = set.univ := rfl @[simp] lemma mem_top {x : A} : x ∈ (⊤ : star_subalgebra R A) := set.mem_univ x @[simp] lemma top_to_subalgebra : (⊤ : star_subalgebra R A).to_subalgebra = ⊤ := rfl @[simp] lemma to_subalgebra_eq_top {S : star_subalgebra R A} : S.to_subalgebra = ⊤ ↔ S = ⊤ := star_subalgebra.to_subalgebra_injective.eq_iff' top_to_subalgebra lemma mem_sup_left {S T : star_subalgebra R A} : ∀ {x : A}, x ∈ S → x ∈ S ⊔ T := show S ≤ S ⊔ T, from le_sup_left lemma mem_sup_right {S T : star_subalgebra R A} : ∀ {x : A}, x ∈ T → x ∈ S ⊔ T := show T ≤ S ⊔ T, from le_sup_right lemma mul_mem_sup {S T : star_subalgebra R A} {x y : A} (hx : x ∈ S) (hy : y ∈ T) : x * y ∈ S ⊔ T := mul_mem (mem_sup_left hx) (mem_sup_right hy) lemma map_sup (f : A →⋆ₐ[R] B) (S T : star_subalgebra R A) : map f (S ⊔ T) = map f S ⊔ map f T := (star_subalgebra.gc_map_comap f).l_sup @[simp, norm_cast] lemma coe_inf (S T : star_subalgebra R A) : (↑(S ⊓ T) : set A) = S ∩ T := rfl @[simp] lemma mem_inf {S T : star_subalgebra R A} {x : A} : x ∈ S ⊓ T ↔ x ∈ S ∧ x ∈ T := iff.rfl @[simp] lemma inf_to_subalgebra (S T : star_subalgebra R A) : (S ⊓ T).to_subalgebra = S.to_subalgebra ⊓ T.to_subalgebra := rfl @[simp, norm_cast] lemma coe_Inf (S : set (star_subalgebra R A)) : (↑(Inf S) : set A) = ⋂ s ∈ S, ↑s := Inf_image lemma mem_Inf {S : set (star_subalgebra R A)} {x : A} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := by simp only [← set_like.mem_coe, coe_Inf, set.mem_Inter₂] @[simp] lemma Inf_to_subalgebra (S : set (star_subalgebra R A)) : (Inf S).to_subalgebra = Inf (star_subalgebra.to_subalgebra '' S) := set_like.coe_injective $ by simp @[simp, norm_cast] lemma coe_infi {ι : Sort*} {S : ι → star_subalgebra R A} : (↑(⨅ i, S i) : set A) = ⋂ i, S i := by simp [infi] lemma mem_infi {ι : Sort*} {S : ι → star_subalgebra R A} {x : A} : (x ∈ ⨅ i, S i) ↔ ∀ i, x ∈ S i := by simp only [infi, mem_Inf, set.forall_range_iff] @[simp] lemma infi_to_subalgebra {ι : Sort*} (S : ι → star_subalgebra R A) : (⨅ i, S i).to_subalgebra = ⨅ i, (S i).to_subalgebra := set_like.coe_injective $ by simp lemma bot_to_subalgebra : (⊥ : star_subalgebra R A).to_subalgebra = ⊥ := by { change algebra.adjoin R (∅ ∪ star ∅) = algebra.adjoin R ∅, simp } theorem mem_bot {x : A} : x ∈ (⊥ : star_subalgebra R A) ↔ x ∈ set.range (algebra_map R A) := by rw [←mem_to_subalgebra, bot_to_subalgebra, algebra.mem_bot] @[simp] theorem coe_bot : ((⊥ : star_subalgebra R A) : set A) = set.range (algebra_map R A) := by simp [set.ext_iff, mem_bot] theorem eq_top_iff {S : star_subalgebra R A} : S = ⊤ ↔ ∀ x : A, x ∈ S := ⟨λ h x, by rw h; exact mem_top, λ h, by ext x; exact ⟨λ _, mem_top, λ _, h x⟩⟩ end star_subalgebra namespace star_alg_hom open star_subalgebra variables {F R A B : Type*} [comm_semiring R] [star_ring R] variables [semiring A] [algebra R A] [star_ring A] [star_module R A] variables [semiring B] [algebra R B] [star_ring B] variables [hF : star_alg_hom_class F R A B] (f g : F) include hF /-- The equalizer of two star `R`-algebra homomorphisms. -/ def equalizer : star_subalgebra R A := { carrier := {a | f a = g a}, mul_mem' := λ a b (ha : f a = g a) (hb : f b = g b), by rw [set.mem_set_of_eq, map_mul f, map_mul g, ha, hb], add_mem' := λ a b (ha : f a = g a) (hb : f b = g b), by rw [set.mem_set_of_eq, map_add f, map_add g, ha, hb], algebra_map_mem' := λ r, by simp only [set.mem_set_of_eq, alg_hom_class.commutes], star_mem' := λ a (ha : f a = g a), by rw [set.mem_set_of_eq, map_star f, map_star g, ha] } @[simp] lemma mem_equalizer (x : A) : x ∈ star_alg_hom.equalizer f g ↔ f x = g x := iff.rfl lemma adjoin_le_equalizer {s : set A} (h : s.eq_on f g) : adjoin R s ≤ star_alg_hom.equalizer f g := adjoin_le h lemma ext_of_adjoin_eq_top {s : set A} (h : adjoin R s = ⊤) ⦃f g : F⦄ (hs : s.eq_on f g) : f = g := fun_like.ext f g $ λ x, star_alg_hom.adjoin_le_equalizer f g hs $ h.symm ▸ trivial omit hF lemma map_adjoin [star_module R B] (f : A →⋆ₐ[R] B) (s : set A) : map f (adjoin R s) = adjoin R (f '' s) := galois_connection.l_comm_of_u_comm set.image_preimage (gc_map_comap f) star_subalgebra.gc star_subalgebra.gc (λ _, rfl) lemma ext_adjoin {s : set A} [star_alg_hom_class F R (adjoin R s) B] {f g : F} (h : ∀ x : adjoin R s, (x : A) ∈ s → f x = g x) : f = g := begin refine fun_like.ext f g (λ a, adjoin_induction' a (λ x hx, _) (λ r, _) (λ x y hx hy, _) (λ x y hx hy, _) (λ x hx, _)), { exact h ⟨x, subset_adjoin R s hx⟩ hx }, { simp only [alg_hom_class.commutes] }, { rw [map_add, map_add, hx, hy] }, { rw [map_mul, map_mul, hx, hy] }, { rw [map_star, map_star, hx] }, end lemma ext_adjoin_singleton {a : A} [star_alg_hom_class F R (adjoin R ({a} : set A)) B] {f g : F} (h : f ⟨a, self_mem_adjoin_singleton R a⟩ = g ⟨a, self_mem_adjoin_singleton R a⟩) : f = g := ext_adjoin $ λ x hx, (show x = ⟨a, self_mem_adjoin_singleton R a⟩, from subtype.ext $ set.mem_singleton_iff.mp hx).symm ▸ h end star_alg_hom
d65db6484f0f3cff4f7a86e4ffc848db15270ad6
0845ae2ca02071debcfd4ac24be871236c01784f
/library/init/lean/compiler/ir/basic.lean
a5ae79bdba560fcc26b02e6aaed82c2cee67ca1d
[ "Apache-2.0" ]
permissive
GaloisInc/lean4
74c267eb0e900bfaa23df8de86039483ecbd60b7
228ddd5fdcd98dd4e9c009f425284e86917938aa
refs/heads/master
1,643,131,356,301
1,562,715,572,000
1,562,715,572,000
192,390,898
0
0
null
1,560,792,750,000
1,560,792,749,000
null
UTF-8
Lean
false
false
23,789
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.data.array import init.lean.name import init.lean.kvmap import init.lean.format import init.lean.compiler.externattr /- Implements (extended) λPure and λRc proposed in the article "Counting Immutable Beans", Sebastian Ullrich and Leonardo de Moura. The Lean to IR transformation produces λPure code, and this part is implemented in C++. The procedures described in the paper above are implemented in Lean. -/ namespace Lean namespace IR /- Function identifier -/ abbrev FunId := Name abbrev Index := Nat /- Variable identifier -/ structure VarId := (idx : Index) /- Join point identifier -/ structure JoinPointId := (idx : Index) abbrev Index.lt (a b : Index) : Bool := a < b namespace VarId instance : HasBeq VarId := ⟨fun a b => a.idx == b.idx⟩ instance : HasToString VarId := ⟨fun a => "x_" ++ toString a.idx⟩ instance : HasFormat VarId := ⟨fun a => toString a⟩ instance : Hashable VarId := ⟨fun a => hash a.idx⟩ end VarId namespace JoinPointId instance : HasBeq JoinPointId := ⟨fun a b => a.idx == b.idx⟩ instance : HasToString JoinPointId := ⟨fun a => "block_" ++ toString a.idx⟩ instance : HasFormat JoinPointId := ⟨fun a => toString a⟩ instance : Hashable JoinPointId := ⟨fun a => hash a.idx⟩ end JoinPointId abbrev MData := KVMap namespace MData abbrev empty : MData := {KVMap .} instance : HasEmptyc MData := ⟨empty⟩ end MData /- Low Level IR types. Most are self explanatory. - `usize` represents the C++ `size_t` Type. We have it here because it is 32-bit in 32-bit machines, and 64-bit in 64-bit machines, and we want the C++ backend for our Compiler to generate platform independent code. - `irrelevant` for Lean types, propositions and proofs. - `object` a pointer to a value in the heap. - `tobject` a pointer to a value in the heap or tagged pointer (i.e., the least significant bit is 1) storing a scalar value. Remark: the RC operations for `tobject` are slightly more expensive because we first need to test whether the `tobject` is really a pointer or not. Remark: the Lean runtime assumes that sizeof(void*) == sizeof(sizeT). Lean cannot be compiled on old platforms where this is not True. -/ inductive IRType | float | uint8 | uint16 | uint32 | uint64 | usize | irrelevant | object | tobject def IRType.beq : IRType → IRType → Bool | IRType.float IRType.float := true | IRType.uint8 IRType.uint8 := true | IRType.uint16 IRType.uint16 := true | IRType.uint32 IRType.uint32 := true | IRType.uint64 IRType.uint64 := true | IRType.usize IRType.usize := true | IRType.irrelevant IRType.irrelevant := true | IRType.object IRType.object := true | IRType.tobject IRType.tobject := true | _ _ := false instance IRType.HasBeq : HasBeq IRType := ⟨IRType.beq⟩ def IRType.isScalar : IRType → Bool | IRType.float := true | IRType.uint8 := true | IRType.uint16 := true | IRType.uint32 := true | IRType.uint64 := true | IRType.usize := true | _ := false def IRType.isObj : IRType → Bool | IRType.object := true | IRType.tobject := true | _ := false def IRType.isIrrelevant : IRType → Bool | IRType.irrelevant := true | _ := false /- Arguments to applications, constructors, etc. We use `irrelevant` for Lean types, propositions and proofs that have been erased. Recall that for a Function `f`, we also generate `f._rarg` which does not take `irrelevant` arguments. However, `f._rarg` is only safe to be used in full applications. -/ inductive Arg | var (id : VarId) | irrelevant namespace Arg protected def beq : Arg → Arg → Bool | (var x) (var y) := x == y | irrelevant irrelevant := true | _ _ := false instance : HasBeq Arg := ⟨Arg.beq⟩ instance : Inhabited Arg := ⟨irrelevant⟩ end Arg @[export lean.ir.mk_var_arg_core] def mkVarArg (id : VarId) : Arg := Arg.var id @[export lean.ir.mk_irrelevant_arg_core] def mkIrrelevantArg : Arg := Arg.irrelevant inductive LitVal | num (v : Nat) | str (v : String) def LitVal.beq : LitVal → LitVal → Bool | (LitVal.num v₁) (LitVal.num v₂) := v₁ == v₂ | (LitVal.str v₁) (LitVal.str v₂) := v₁ == v₂ | _ _ := false instance LitVal.HasBeq : HasBeq LitVal := ⟨LitVal.beq⟩ /- Constructor information. - `name` is the Name of the Constructor in Lean. - `cidx` is the Constructor index (aka tag). - `size` is the number of arguments of type `object/tobject`. - `usize` is the number of arguments of type `usize`. - `ssize` is the number of bytes used to store scalar values. Recall that a Constructor object contains a header, then a sequence of pointers to other Lean objects, a sequence of `Usize` (i.e., `sizeT`) scalar values, and a sequence of other scalar values. -/ structure CtorInfo := (name : Name) (cidx : Nat) (size : Nat) (usize : Nat) (ssize : Nat) def CtorInfo.beq : CtorInfo → CtorInfo → Bool | ⟨n₁, cidx₁, size₁, usize₁, ssize₁⟩ ⟨n₂, cidx₂, size₂, usize₂, ssize₂⟩ := n₁ == n₂ && cidx₁ == cidx₂ && size₁ == size₂ && usize₁ == usize₂ && ssize₁ == ssize₂ instance CtorInfo.HasBeq : HasBeq CtorInfo := ⟨CtorInfo.beq⟩ def CtorInfo.isRef (info : CtorInfo) : Bool := info.size > 0 || info.usize > 0 || info.ssize > 0 def CtorInfo.isScalar (info : CtorInfo) : Bool := !info.isRef inductive Expr | ctor (i : CtorInfo) (ys : Array Arg) | reset (n : Nat) (x : VarId) /- `reuse x in ctor_i ys` instruction in the paper. -/ | reuse (x : VarId) (i : CtorInfo) (updtHeader : Bool) (ys : Array Arg) /- Extract the `tobject` value at Position `sizeof(void*)*i` from `x`. -/ | proj (i : Nat) (x : VarId) /- Extract the `Usize` value at Position `sizeof(void*)*i` from `x`. -/ | uproj (i : Nat) (x : VarId) /- Extract the scalar value at Position `sizeof(void*)*n + offset` from `x`. -/ | sproj (n : Nat) (offset : Nat) (x : VarId) /- Full application. -/ | fap (c : FunId) (ys : Array Arg) /- Partial application that creates a `pap` value (aka closure in our nonstandard terminology). -/ | pap (c : FunId) (ys : Array Arg) /- Application. `x` must be a `pap` value. -/ | ap (x : VarId) (ys : Array Arg) /- Given `x : ty` where `ty` is a scalar type, this operation returns a value of Type `tobject`. For small scalar values, the Result is a tagged pointer, and no memory allocation is performed. -/ | box (ty : IRType) (x : VarId) /- Given `x : [t]object`, obtain the scalar value. -/ | unbox (x : VarId) | lit (v : LitVal) /- Return `1 : uint8` Iff `RC(x) > 1` -/ | isShared (x : VarId) /- Return `1 : uint8` Iff `x : tobject` is a tagged pointer (storing a scalar value). -/ | isTaggedPtr (x : VarId) @[export lean.ir.mk_ctor_expr_core] def mkCtorExpr (n : Name) (cidx : Nat) (size : Nat) (usize : Nat) (ssize : Nat) (ys : Array Arg) : Expr := Expr.ctor ⟨n, cidx, size, usize, ssize⟩ ys @[export lean.ir.mk_proj_expr_core] def mkProjExpr (i : Nat) (x : VarId) : Expr := Expr.proj i x @[export lean.ir.mk_uproj_expr_core] def mkUProjExpr (i : Nat) (x : VarId) : Expr := Expr.uproj i x @[export lean.ir.mk_sproj_expr_core] def mkSProjExpr (n : Nat) (offset : Nat) (x : VarId) : Expr := Expr.sproj n offset x @[export lean.ir.mk_fapp_expr_core] def mkFAppExpr (c : FunId) (ys : Array Arg) : Expr := Expr.fap c ys @[export lean.ir.mk_papp_expr_core] def mkPAppExpr (c : FunId) (ys : Array Arg) : Expr := Expr.pap c ys @[export lean.ir.mk_app_expr_core] def mkAppExpr (x : VarId) (ys : Array Arg) : Expr := Expr.ap x ys @[export lean.ir.mk_num_expr_core] def mkNumExpr (v : Nat) : Expr := Expr.lit (LitVal.num v) @[export lean.ir.mk_str_expr_core] def mkStrExpr (v : String) : Expr := Expr.lit (LitVal.str v) structure Param := (x : VarId) (borrow : Bool) (ty : IRType) instance paramInh : Inhabited Param := ⟨{ x := { idx := 0 }, borrow := false, ty := IRType.object }⟩ @[export lean.ir.mk_param_core] def mkParam (x : VarId) (borrow : Bool) (ty : IRType) : Param := ⟨x, borrow, ty⟩ inductive AltCore (FnBody : Type) : Type | ctor (info : CtorInfo) (b : FnBody) : AltCore | default (b : FnBody) : AltCore inductive FnBody /- `let x : ty := e; b` -/ | vdecl (x : VarId) (ty : IRType) (e : Expr) (b : FnBody) /- Join point Declaration `block_j (xs) := e; b` -/ | jdecl (j : JoinPointId) (xs : Array Param) (v : FnBody) (b : FnBody) /- Store `y` at Position `sizeof(void*)*i` in `x`. `x` must be a Constructor object and `RC(x)` must be 1. This operation is not part of λPure is only used during optimization. -/ | set (x : VarId) (i : Nat) (y : Arg) (b : FnBody) | setTag (x : VarId) (cidx : Nat) (b : FnBody) /- Store `y : Usize` at Position `sizeof(void*)*i` in `x`. `x` must be a Constructor object and `RC(x)` must be 1. -/ | uset (x : VarId) (i : Nat) (y : VarId) (b : FnBody) /- Store `y : ty` at Position `sizeof(void*)*i + offset` in `x`. `x` must be a Constructor object and `RC(x)` must be 1. `ty` must not be `object`, `tobject`, `irrelevant` nor `Usize`. -/ | sset (x : VarId) (i : Nat) (offset : Nat) (y : VarId) (ty : IRType) (b : FnBody) /- RC increment for `object`. If c == `true`, then `inc` must check whether `x` is a tagged pointer or not. -/ | inc (x : VarId) (n : Nat) (c : Bool) (b : FnBody) /- RC decrement for `object`. If c == `true`, then `inc` must check whether `x` is a tagged pointer or not. -/ | dec (x : VarId) (n : Nat) (c : Bool) (b : FnBody) | del (x : VarId) (b : FnBody) | mdata (d : MData) (b : FnBody) | case (tid : Name) (x : VarId) (cs : Array (AltCore FnBody)) | ret (x : Arg) /- Jump to join point `j` -/ | jmp (j : JoinPointId) (ys : Array Arg) | unreachable instance : Inhabited FnBody := ⟨FnBody.unreachable⟩ abbrev FnBody.nil := FnBody.unreachable @[export lean.ir.mk_vdecl_core] def mkVDecl (x : VarId) (ty : IRType) (e : Expr) (b : FnBody) : FnBody := FnBody.vdecl x ty e b @[export lean.ir.mk_jdecl_core] def mkJDecl (j : JoinPointId) (xs : Array Param) (v : FnBody) (b : FnBody) : FnBody := FnBody.jdecl j xs v b @[export lean.ir.mk_uset_core] def mkUSet (x : VarId) (i : Nat) (y : VarId) (b : FnBody) : FnBody := FnBody.uset x i y b @[export lean.ir.mk_sset_core] def mkSSet (x : VarId) (i : Nat) (offset : Nat) (y : VarId) (ty : IRType) (b : FnBody) : FnBody := FnBody.sset x i offset y ty b @[export lean.ir.mk_case_core] def mkCase (tid : Name) (x : VarId) (cs : Array (AltCore FnBody)) : FnBody := FnBody.case tid x cs @[export lean.ir.mk_ret_core] def mkRet (x : Arg) : FnBody := FnBody.ret x @[export lean.ir.mk_jmp_core] def mkJmp (j : JoinPointId) (ys : Array Arg) : FnBody := FnBody.jmp j ys @[export lean.ir.mk_unreachable_core] def mkUnreachable : FnBody := FnBody.unreachable abbrev Alt := AltCore FnBody @[matchPattern] abbrev Alt.ctor := @AltCore.ctor FnBody @[matchPattern] abbrev Alt.default := @AltCore.default FnBody instance altInh : Inhabited Alt := ⟨Alt.default (default _)⟩ def FnBody.isTerminal : FnBody → Bool | (FnBody.case _ _ _) := true | (FnBody.ret _) := true | (FnBody.jmp _ _) := true | FnBody.unreachable := true | _ := false def FnBody.body : FnBody → FnBody | (FnBody.vdecl _ _ _ b) := b | (FnBody.jdecl _ _ _ b) := b | (FnBody.set _ _ _ b) := b | (FnBody.uset _ _ _ b) := b | (FnBody.sset _ _ _ _ _ b) := b | (FnBody.setTag _ _ b) := b | (FnBody.inc _ _ _ b) := b | (FnBody.dec _ _ _ b) := b | (FnBody.del _ b) := b | (FnBody.mdata _ b) := b | other := other def FnBody.setBody : FnBody → FnBody → FnBody | (FnBody.vdecl x t v _) b := FnBody.vdecl x t v b | (FnBody.jdecl j xs v _) b := FnBody.jdecl j xs v b | (FnBody.set x i y _) b := FnBody.set x i y b | (FnBody.uset x i y _) b := FnBody.uset x i y b | (FnBody.sset x i o y t _) b := FnBody.sset x i o y t b | (FnBody.setTag x i _) b := FnBody.setTag x i b | (FnBody.inc x n c _) b := FnBody.inc x n c b | (FnBody.dec x n c _) b := FnBody.dec x n c b | (FnBody.del x _) b := FnBody.del x b | (FnBody.mdata d _) b := FnBody.mdata d b | other b := other @[inline] def FnBody.resetBody (b : FnBody) : FnBody := b.setBody FnBody.nil /- If b is a non terminal, then return a pair `(c, b')` s.t. `b == c <;> b'`, and c.body == FnBody.nil -/ @[inline] def FnBody.split (b : FnBody) : FnBody × FnBody := let b' := b.body; let c := b.resetBody; (c, b') def AltCore.body : Alt → FnBody | (Alt.ctor _ b) := b | (Alt.default b) := b def AltCore.setBody : Alt → FnBody → Alt | (Alt.ctor c _) b := Alt.ctor c b | (Alt.default _) b := Alt.default b @[inline] def AltCore.modifyBody (f : FnBody → FnBody) : AltCore FnBody → Alt | (Alt.ctor c b) := Alt.ctor c (f b) | (Alt.default b) := Alt.default (f b) @[inline] def AltCore.mmodifyBody {m : Type → Type} [Monad m] (f : FnBody → m FnBody) : AltCore FnBody → m Alt | (Alt.ctor c b) := Alt.ctor c <$> f b | (Alt.default b) := Alt.default <$> f b def Alt.isDefault : Alt → Bool | (Alt.ctor _ _) := false | (Alt.default _) := true def push (bs : Array FnBody) (b : FnBody) : Array FnBody := let b := b.resetBody; bs.push b partial def flattenAux : FnBody → Array FnBody → (Array FnBody) × FnBody | b r := if b.isTerminal then (r, b) else flattenAux b.body (push r b) def FnBody.flatten (b : FnBody) : (Array FnBody) × FnBody := flattenAux b Array.empty partial def reshapeAux : Array FnBody → Nat → FnBody → FnBody | a i b := if i == 0 then b else let i := i - 1; let (curr, a) := a.swapAt i (default _); let b := curr.setBody b; reshapeAux a i b def reshape (bs : Array FnBody) (term : FnBody) : FnBody := reshapeAux bs bs.size term @[inline] def modifyJPs (bs : Array FnBody) (f : FnBody → FnBody) : Array FnBody := bs.map $ fun b => match b with | FnBody.jdecl j xs v k => FnBody.jdecl j xs (f v) k | other => other @[inline] def mmodifyJPs {m : Type → Type} [Monad m] (bs : Array FnBody) (f : FnBody → m FnBody) : m (Array FnBody) := bs.mmap $ fun b => match b with | FnBody.jdecl j xs v k => do v ← f v; pure $ FnBody.jdecl j xs v k | other => pure other @[export lean.ir.mk_alt_core] def mkAlt (n : Name) (cidx : Nat) (size : Nat) (usize : Nat) (ssize : Nat) (b : FnBody) : Alt := Alt.ctor ⟨n, cidx, size, usize, ssize⟩ b inductive Decl | fdecl (f : FunId) (xs : Array Param) (ty : IRType) (b : FnBody) | extern (f : FunId) (xs : Array Param) (ty : IRType) (ext : ExternAttrData) namespace Decl instance : Inhabited Decl := ⟨fdecl (default _) (default _) IRType.irrelevant (default _)⟩ def name : Decl → FunId | (Decl.fdecl f _ _ _) := f | (Decl.extern f _ _ _) := f def params : Decl → Array Param | (Decl.fdecl _ xs _ _) := xs | (Decl.extern _ xs _ _) := xs def resultType : Decl → IRType | (Decl.fdecl _ _ t _) := t | (Decl.extern _ _ t _) := t end Decl @[export lean.ir.mk_decl_core] def mkDecl (f : FunId) (xs : Array Param) (ty : IRType) (b : FnBody) : Decl := Decl.fdecl f xs ty b @[export lean.ir.mk_extern_decl_core] def mkExternDecl (f : FunId) (xs : Array Param) (ty : IRType) (e : ExternAttrData) : Decl := Decl.extern f xs ty e /-- Set of variable and join point names -/ abbrev IndexSet := RBTree Index Index.lt instance vsetInh : Inhabited IndexSet := ⟨{}⟩ inductive LocalContextEntry | param : IRType → LocalContextEntry | localVar : IRType → Expr → LocalContextEntry | joinPoint : Array Param → FnBody → LocalContextEntry abbrev LocalContext := RBMap Index LocalContextEntry Index.lt def LocalContext.addLocal (ctx : LocalContext) (x : VarId) (t : IRType) (v : Expr) : LocalContext := ctx.insert x.idx (LocalContextEntry.localVar t v) def LocalContext.addJP (ctx : LocalContext) (j : JoinPointId) (xs : Array Param) (b : FnBody) : LocalContext := ctx.insert j.idx (LocalContextEntry.joinPoint xs b) def LocalContext.addParam (ctx : LocalContext) (p : Param) : LocalContext := ctx.insert p.x.idx (LocalContextEntry.param p.ty) def LocalContext.addParams (ctx : LocalContext) (ps : Array Param) : LocalContext := ps.foldl LocalContext.addParam ctx def LocalContext.isJP (ctx : LocalContext) (idx : Index) : Bool := match ctx.find idx with | some (LocalContextEntry.joinPoint _ _) => true | other => false def LocalContext.getJPBody (ctx : LocalContext) (j : JoinPointId) : Option FnBody := match ctx.find j.idx with | some (LocalContextEntry.joinPoint _ b) => some b | other => none def LocalContext.getJPParams (ctx : LocalContext) (j : JoinPointId) : Option (Array Param) := match ctx.find j.idx with | some (LocalContextEntry.joinPoint ys _) => some ys | other => none def LocalContext.isParam (ctx : LocalContext) (idx : Index) : Bool := match ctx.find idx with | some (LocalContextEntry.param _) => true | other => false def LocalContext.isLocalVar (ctx : LocalContext) (idx : Index) : Bool := match ctx.find idx with | some (LocalContextEntry.localVar _ _) => true | other => false def LocalContext.contains (ctx : LocalContext) (idx : Index) : Bool := ctx.contains idx def LocalContext.eraseJoinPointDecl (ctx : LocalContext) (j : JoinPointId) : LocalContext := ctx.erase j.idx def LocalContext.getType (ctx : LocalContext) (x : VarId) : Option IRType := match ctx.find x.idx with | some (LocalContextEntry.param t) => some t | some (LocalContextEntry.localVar t _) => some t | other => none abbrev IndexRenaming := RBMap Index Index Index.lt class HasAlphaEqv (α : Type) := (aeqv : IndexRenaming → α → α → Bool) export HasAlphaEqv (aeqv) def VarId.alphaEqv (ρ : IndexRenaming) (v₁ v₂ : VarId) : Bool := match ρ.find v₁.idx with | some v => v == v₂.idx | none => v₁ == v₂ instance VarId.hasAeqv : HasAlphaEqv VarId := ⟨VarId.alphaEqv⟩ def Arg.alphaEqv (ρ : IndexRenaming) : Arg → Arg → Bool | (Arg.var v₁) (Arg.var v₂) := aeqv ρ v₁ v₂ | Arg.irrelevant Arg.irrelevant := true | _ _ := false instance Arg.hasAeqv : HasAlphaEqv Arg := ⟨Arg.alphaEqv⟩ def args.alphaEqv (ρ : IndexRenaming) (args₁ args₂ : Array Arg) : Bool := Array.isEqv args₁ args₂ (fun a b => aeqv ρ a b) instance args.hasAeqv : HasAlphaEqv (Array Arg) := ⟨args.alphaEqv⟩ def Expr.alphaEqv (ρ : IndexRenaming) : Expr → Expr → Bool | (Expr.ctor i₁ ys₁) (Expr.ctor i₂ ys₂) := i₁ == i₂ && aeqv ρ ys₁ ys₂ | (Expr.reset n₁ x₁) (Expr.reset n₂ x₂) := n₁ == n₂ && aeqv ρ x₁ x₂ | (Expr.reuse x₁ i₁ u₁ ys₁) (Expr.reuse x₂ i₂ u₂ ys₂) := aeqv ρ x₁ x₂ && i₁ == i₂ && u₁ == u₂ && aeqv ρ ys₁ ys₂ | (Expr.proj i₁ x₁) (Expr.proj i₂ x₂) := i₁ == i₂ && aeqv ρ x₁ x₂ | (Expr.uproj i₁ x₁) (Expr.uproj i₂ x₂) := i₁ == i₂ && aeqv ρ x₁ x₂ | (Expr.sproj n₁ o₁ x₁) (Expr.sproj n₂ o₂ x₂) := n₁ == n₂ && o₁ == o₂ && aeqv ρ x₁ x₂ | (Expr.fap c₁ ys₁) (Expr.fap c₂ ys₂) := c₁ == c₂ && aeqv ρ ys₁ ys₂ | (Expr.pap c₁ ys₁) (Expr.pap c₂ ys₂) := c₁ == c₂ && aeqv ρ ys₂ ys₂ | (Expr.ap x₁ ys₁) (Expr.ap x₂ ys₂) := aeqv ρ x₁ x₂ && aeqv ρ ys₁ ys₂ | (Expr.box ty₁ x₁) (Expr.box ty₂ x₂) := ty₁ == ty₂ && aeqv ρ x₁ x₂ | (Expr.unbox x₁) (Expr.unbox x₂) := aeqv ρ x₁ x₂ | (Expr.lit v₁) (Expr.lit v₂) := v₁ == v₂ | (Expr.isShared x₁) (Expr.isShared x₂) := aeqv ρ x₁ x₂ | (Expr.isTaggedPtr x₁) (Expr.isTaggedPtr x₂) := aeqv ρ x₁ x₂ | _ _ := false instance Expr.hasAeqv : HasAlphaEqv Expr:= ⟨Expr.alphaEqv⟩ def addVarRename (ρ : IndexRenaming) (x₁ x₂ : Nat) := if x₁ == x₂ then ρ else ρ.insert x₁ x₂ def addParamRename (ρ : IndexRenaming) (p₁ p₂ : Param) : Option IndexRenaming := if p₁.ty == p₂.ty && p₁.borrow = p₂.borrow then some (addVarRename ρ p₁.x.idx p₂.x.idx) else none def addParamsRename (ρ : IndexRenaming) (ps₁ ps₂ : Array Param) : Option IndexRenaming := if ps₁.size != ps₂.size then none else Array.foldl₂ (fun ρ p₁ p₂ => do ρ ← ρ; addParamRename ρ p₁ p₂) (some ρ) ps₁ ps₂ partial def FnBody.alphaEqv : IndexRenaming → FnBody → FnBody → Bool | ρ (FnBody.vdecl x₁ t₁ v₁ b₁) (FnBody.vdecl x₂ t₂ v₂ b₂) := t₁ == t₂ && aeqv ρ v₁ v₂ && FnBody.alphaEqv (addVarRename ρ x₁.idx x₂.idx) b₁ b₂ | ρ (FnBody.jdecl j₁ ys₁ v₁ b₁) (FnBody.jdecl j₂ ys₂ v₂ b₂) := match addParamsRename ρ ys₁ ys₂ with | some ρ' => FnBody.alphaEqv ρ' v₁ v₂ && FnBody.alphaEqv (addVarRename ρ j₁.idx j₂.idx) b₁ b₂ | none => false | ρ (FnBody.set x₁ i₁ y₁ b₁) (FnBody.set x₂ i₂ y₂ b₂) := aeqv ρ x₁ x₂ && i₁ == i₂ && aeqv ρ y₁ y₂ && FnBody.alphaEqv ρ b₁ b₂ | ρ (FnBody.uset x₁ i₁ y₁ b₁) (FnBody.uset x₂ i₂ y₂ b₂) := aeqv ρ x₁ x₂ && i₁ == i₂ && aeqv ρ y₁ y₂ && FnBody.alphaEqv ρ b₁ b₂ | ρ (FnBody.sset x₁ i₁ o₁ y₁ t₁ b₁) (FnBody.sset x₂ i₂ o₂ y₂ t₂ b₂) := aeqv ρ x₁ x₂ && i₁ = i₂ && o₁ = o₂ && aeqv ρ y₁ y₂ && t₁ == t₂ && FnBody.alphaEqv ρ b₁ b₂ | ρ (FnBody.setTag x₁ i₁ b₁) (FnBody.setTag x₂ i₂ b₂) := aeqv ρ x₁ x₂ && i₁ == i₂ && FnBody.alphaEqv ρ b₁ b₂ | ρ (FnBody.inc x₁ n₁ c₁ b₁) (FnBody.inc x₂ n₂ c₂ b₂) := aeqv ρ x₁ x₂ && n₁ == n₂ && c₁ == c₂ && FnBody.alphaEqv ρ b₁ b₂ | ρ (FnBody.dec x₁ n₁ c₁ b₁) (FnBody.dec x₂ n₂ c₂ b₂) := aeqv ρ x₁ x₂ && n₁ == n₂ && c₁ == c₂ && FnBody.alphaEqv ρ b₁ b₂ | ρ (FnBody.del x₁ b₁) (FnBody.del x₂ b₂) := aeqv ρ x₁ x₂ && FnBody.alphaEqv ρ b₁ b₂ | ρ (FnBody.mdata m₁ b₁) (FnBody.mdata m₂ b₂) := m₁ == m₂ && FnBody.alphaEqv ρ b₁ b₂ | ρ (FnBody.case n₁ x₁ alts₁) (FnBody.case n₂ x₂ alts₂) := n₁ == n₂ && aeqv ρ x₁ x₂ && Array.isEqv alts₁ alts₂ (fun alt₁ alt₂ => match alt₁, alt₂ with | Alt.ctor i₁ b₁, Alt.ctor i₂ b₂ => i₁ == i₂ && FnBody.alphaEqv ρ b₁ b₂ | Alt.default b₁, Alt.default b₂ => FnBody.alphaEqv ρ b₁ b₂ | _, _ => false) | ρ (FnBody.jmp j₁ ys₁) (FnBody.jmp j₂ ys₂) := j₁ == j₂ && aeqv ρ ys₁ ys₂ | ρ (FnBody.ret x₁) (FnBody.ret x₂) := aeqv ρ x₁ x₂ | _ FnBody.unreachable FnBody.unreachable := true | _ _ _ := false def FnBody.beq (b₁ b₂ : FnBody) : Bool := FnBody.alphaEqv ∅ b₁ b₂ instance FnBody.HasBeq : HasBeq FnBody := ⟨FnBody.beq⟩ abbrev VarIdSet := RBTree VarId (fun x y => x.idx < y.idx) namespace VarIdSet instance : Inhabited VarIdSet := ⟨{}⟩ end VarIdSet def mkIf (x : VarId) (t e : FnBody) : FnBody := FnBody.case `Bool x [ Alt.ctor {name := `Bool.false, cidx := 0, size := 0, usize := 0, ssize := 0} e, Alt.ctor {name := `Bool.true, cidx := 1, size := 0, usize := 0, ssize := 0} t ].toArray end IR end Lean
52ac3993a25d68750ae2300a4109ad73f2cc08dd
42c01158c2730cc6ac3e058c1339c18cb90366e2
/M1F/2017-18/Example_Sheet_01/Questions_02_to_4/M1F_sheet01_questions02_to_04.lean
f438abbae8f69828c3b1bdd29f430510ade16381
[]
no_license
ChrisHughes24/xena
c80d94355d0c2ae8deddda9d01e6d31bc21c30ae
337a0d7c9f0e255e08d6d0a383e303c080c6ec0c
refs/heads/master
1,631,059,898,392
1,511,200,551,000
1,511,200,551,000
111,468,589
1
0
null
null
null
null
UTF-8
Lean
false
false
1,697
lean
/- M1F 2017-18 Sheet 1 Question 1 Author : Kevin Buzzard This file should work with any version of lean -- whether you installed it yourself or are running the version on https://leanprover.github.io/live/latest/ -/ -- We probably need the "law of the excluded middle" for this question -- every -- proposition is either true or false! Don't even ask me to explain what the -- other options are, but Lean does not come with this axiom by default (blame -- the computer scientists) and mathematicians have to add it themselves. -- It's easy to add though. "em" for excluded middle. axiom em (X : Prop) : X ∨ ¬ X variables P Q R S : Prop -- A "Prop" is a proposition, that is, a true/false statement. -- Sheet 1 Q2. Prove one result and delete the other. theorem m1f_sheet01_q02_is_T (HQP : Q → P) (HnQnR : ¬ Q → ¬ R) : R → P := sorry theorem m1f_sheet01_q02_is_F (HQP : Q → P) (HnQnR : ¬ Q → ¬ R) : ¬ (R → P) := sorry -- Sheet 1 Q3. Prove one result and delete the other. theorem m1f_sheet01_q03_is_T (HP : P) (HnQ : ¬ Q) (HnR : ¬ R) (HS : S) : (R → S) → (P → Q) := sorry theorem m1f_sheet01_q03_is_F (HP : P) (HnQ : ¬ Q) (HnR : ¬ R) (HS : S) : ¬ ((R → S) → (P → Q)) := sorry -- Sheet 1 Q4. **Edit the question** until it corresponds to what you think the -- answer is, and then prove it. -- For example if you think that the answer is that either P and Q are both true -- or P,Q,R are all false, then change the end of the question (after the iff) to -- ((P ∧ Q) ∨ (¬ P ∧ ¬ Q ∧ ¬ R)) theorem m1f_sheet01_q04 : (P → (Q ∨ R)) ∧ (¬ Q → (R ∨ ¬ P)) ∧ ((Q ∧ R) → ¬ P) ↔ ((P ∧ Q ∧ R) ∨ (P ∧ ¬ Q ∧ ¬ R)) := sorry
df884b76f6e772621fd574fa01e3a2aae00dc0a2
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/tests/lean/run/noindexAnnotation.lean
d8b4692ea0f664e61328fe82d0490f7f37f5ea50
[ "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
321
lean
structure Fin2 (n : Nat) := (val : Nat) (isLt : Less val n) protected def Fin2.ofNat {n : Nat} (a : Nat) : Fin2 (Nat.succ n) := ⟨a % Nat.succ n, Nat.mod_lt _ (Nat.zeroLtSucc _)⟩ instance : OfNat (Fin2 (no_index (n+1))) i where ofNat := Fin2.ofNat i def ex1 : Fin2 (9 + 1) := 0 def ex2 : Fin2 10 := 0
e0af3b20351eb5a3733fd517124ad41c283ab842
8e6cad62ec62c6c348e5faaa3c3f2079012bdd69
/src/ring_theory/multiplicity.lean
ee414847997790074ecc1fa437f63b860e6fd0a1
[ "Apache-2.0" ]
permissive
benjamindavidson/mathlib
8cc81c865aa8e7cf4462245f58d35ae9a56b150d
fad44b9f670670d87c8e25ff9cdf63af87ad731e
refs/heads/master
1,679,545,578,362
1,615,343,014,000
1,615,343,014,000
312,926,983
0
0
Apache-2.0
1,615,360,301,000
1,605,399,418,000
Lean
UTF-8
Lean
false
false
17,448
lean
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis, Chris Hughes -/ import algebra.associated import algebra.big_operators.basic import data.nat.enat /-! # Multiplicity of a divisor For a commutative monoid, this file introduces the notion of multiplicity of a divisor and proves several basic results on it. ## Main definitions * `multiplicity a b`: for two elements `a` and `b` of a commutative monoid returns the largest number `n` such that `a ^ n ∣ b` or infinity, written `⊤`, if `a ^ n ∣ b` for all natural numbers `n`. * `multiplicity.finite a b`: a predicate denoting that the multiplicity of `a` in `b` is finite. -/ variables {α : Type*} open nat roption open_locale big_operators /-- `multiplicity a b` returns the largest natural number `n` such that `a ^ n ∣ b`, as an `enat` or natural with infinity. If `∀ n, a ^ n ∣ b`, then it returns `⊤`-/ def multiplicity [comm_monoid α] [decidable_rel ((∣) : α → α → Prop)] (a b : α) : enat := enat.find $ λ n, ¬a ^ (n + 1) ∣ b namespace multiplicity section comm_monoid variables [comm_monoid α] /-- `multiplicity.finite a b` indicates that the multiplicity of `a` in `b` is finite. -/ @[reducible] def finite (a b : α) : Prop := ∃ n : ℕ, ¬a ^ (n + 1) ∣ b lemma finite_iff_dom [decidable_rel ((∣) : α → α → Prop)] {a b : α} : finite a b ↔ (multiplicity a b).dom := iff.rfl lemma finite_def {a b : α} : finite a b ↔ ∃ n : ℕ, ¬a ^ (n + 1) ∣ b := iff.rfl @[norm_cast] theorem int.coe_nat_multiplicity (a b : ℕ) : multiplicity (a : ℤ) (b : ℤ) = multiplicity a b := begin apply roption.ext', { repeat {rw [← finite_iff_dom, finite_def]}, norm_cast }, { intros h1 h2, apply _root_.le_antisymm; { apply nat.find_le, norm_cast, simp }} end lemma not_finite_iff_forall {a b : α} : (¬ finite a b) ↔ ∀ n : ℕ, a ^ n ∣ b := ⟨λ h n, nat.cases_on n (one_dvd _) (by simpa [finite, not_not] using h), by simp [finite, multiplicity, not_not]; tauto⟩ lemma not_unit_of_finite {a b : α} (h : finite a b) : ¬is_unit a := let ⟨n, hn⟩ := h in mt (is_unit_iff_forall_dvd.1 ∘ is_unit.pow (n + 1)) $ λ h, hn (h b) lemma finite_of_finite_mul_left {a b c : α} : finite a (b * c) → finite a c := λ ⟨n, hn⟩, ⟨n, λ h, hn (dvd.trans h (by simp [mul_pow]))⟩ lemma finite_of_finite_mul_right {a b c : α} : finite a (b * c) → finite a b := by rw mul_comm; exact finite_of_finite_mul_left variable [decidable_rel ((∣) : α → α → Prop)] lemma pow_dvd_of_le_multiplicity {a b : α} {k : ℕ} : (k : enat) ≤ multiplicity a b → a ^ k ∣ b := nat.cases_on k (λ _, one_dvd _) (λ k ⟨h₁, h₂⟩, by_contradiction (λ hk, (nat.find_min _ (lt_of_succ_le (h₂ ⟨k, hk⟩)) hk))) lemma pow_multiplicity_dvd {a b : α} (h : finite a b) : a ^ get (multiplicity a b) h ∣ b := pow_dvd_of_le_multiplicity (by rw enat.coe_get) lemma is_greatest {a b : α} {m : ℕ} (hm : multiplicity a b < m) : ¬a ^ m ∣ b := λ h, by rw [enat.lt_coe_iff] at hm; exact nat.find_spec hm.fst (dvd.trans (pow_dvd_pow _ hm.snd) h) lemma is_greatest' {a b : α} {m : ℕ} (h : finite a b) (hm : get (multiplicity a b) h < m) : ¬a ^ m ∣ b := is_greatest (by rwa [← enat.coe_lt_coe, enat.coe_get] at hm) lemma unique {a b : α} {k : ℕ} (hk : a ^ k ∣ b) (hsucc : ¬a ^ (k + 1) ∣ b) : (k : enat) = multiplicity a b := le_antisymm (le_of_not_gt (λ hk', is_greatest hk' hk)) $ have finite a b, from ⟨k, hsucc⟩, by { rw [enat.le_coe_iff], exact ⟨this, nat.find_min' _ hsucc⟩ } lemma unique' {a b : α} {k : ℕ} (hk : a ^ k ∣ b) (hsucc : ¬ a ^ (k + 1) ∣ b) : k = get (multiplicity a b) ⟨k, hsucc⟩ := by rw [← enat.coe_inj, enat.coe_get, unique hk hsucc] lemma le_multiplicity_of_pow_dvd {a b : α} {k : ℕ} (hk : a ^ k ∣ b) : (k : enat) ≤ multiplicity a b := le_of_not_gt $ λ hk', is_greatest hk' hk lemma pow_dvd_iff_le_multiplicity {a b : α} {k : ℕ} : a ^ k ∣ b ↔ (k : enat) ≤ multiplicity a b := ⟨le_multiplicity_of_pow_dvd, pow_dvd_of_le_multiplicity⟩ lemma multiplicity_lt_iff_neg_dvd {a b : α} {k : ℕ} : multiplicity a b < (k : enat) ↔ ¬ a ^ k ∣ b := by { rw [pow_dvd_iff_le_multiplicity, not_le] } lemma eq_some_iff {a b : α} {n : ℕ} : multiplicity a b = (n : enat) ↔ a ^ n ∣ b ∧ ¬a ^ (n + 1) ∣ b := ⟨λ h, let ⟨h₁, h₂⟩ := eq_some_iff.1 h in h₂ ▸ ⟨pow_multiplicity_dvd _, is_greatest (by { rw [enat.lt_coe_iff], exact ⟨h₁, lt_succ_self _⟩ })⟩, λ h, eq_some_iff.2 ⟨⟨n, h.2⟩, eq.symm $ unique' h.1 h.2⟩⟩ lemma eq_top_iff {a b : α} : multiplicity a b = ⊤ ↔ ∀ n : ℕ, a ^ n ∣ b := (enat.find_eq_top_iff _).trans $ by { simp only [not_not], exact ⟨λ h n, nat.cases_on n (one_dvd _) (λ n, h _), λ h n, h _⟩ } lemma one_right {a : α} (ha : ¬is_unit a) : multiplicity a 1 = 0 := eq_some_iff.2 ⟨dvd_refl _, mt is_unit_iff_dvd_one.2 $ by simpa⟩ @[simp] lemma get_one_right {a : α} (ha : finite a 1) : get (multiplicity a 1) ha = 0 := get_eq_iff_eq_some.2 (eq_some_iff.2 ⟨dvd_refl _, by simpa [is_unit_iff_dvd_one.symm] using not_unit_of_finite ha⟩) @[simp] lemma multiplicity_unit {a : α} (b : α) (ha : is_unit a) : multiplicity a b = ⊤ := eq_top_iff.2 (λ _, is_unit_iff_forall_dvd.1 (ha.pow _) _) @[simp] lemma one_left (b : α) : multiplicity 1 b = ⊤ := by simp [eq_top_iff] lemma multiplicity_eq_zero_of_not_dvd {a b : α} (ha : ¬a ∣ b) : multiplicity a b = 0 := eq_some_iff.2 (by simpa) lemma eq_top_iff_not_finite {a b : α} : multiplicity a b = ⊤ ↔ ¬ finite a b := roption.eq_none_iff' open_locale classical lemma multiplicity_le_multiplicity_iff {a b c d : α} : multiplicity a b ≤ multiplicity c d ↔ (∀ n : ℕ, a ^ n ∣ b → c ^ n ∣ d) := ⟨λ h n hab, (pow_dvd_of_le_multiplicity (le_trans (le_multiplicity_of_pow_dvd hab) h)), λ h, if hab : finite a b then by rw [← enat.coe_get (finite_iff_dom.1 hab)]; exact le_multiplicity_of_pow_dvd (h _ (pow_multiplicity_dvd _)) else have ∀ n : ℕ, c ^ n ∣ d, from λ n, h n (not_finite_iff_forall.1 hab _), by rw [eq_top_iff_not_finite.2 hab, eq_top_iff_not_finite.2 (not_finite_iff_forall.2 this)]⟩ lemma multiplicity_le_multiplicity_of_dvd {a b c : α} (hdvd : a ∣ b) : multiplicity b c ≤ multiplicity a c := multiplicity_le_multiplicity_iff.2 $ λ n h, dvd_trans (pow_dvd_pow_of_dvd hdvd n) h lemma dvd_of_multiplicity_pos {a b : α} (h : (0 : enat) < multiplicity a b) : a ∣ b := by rw [← pow_one a]; exact pow_dvd_of_le_multiplicity (enat.pos_iff_one_le.1 h) lemma dvd_iff_multiplicity_pos {a b : α} : (0 : enat) < multiplicity a b ↔ a ∣ b := ⟨dvd_of_multiplicity_pos, λ hdvd, lt_of_le_of_ne (zero_le _) (λ heq, is_greatest (show multiplicity a b < 1, from heq ▸ enat.coe_lt_coe.mpr zero_lt_one) (by rwa pow_one a))⟩ lemma finite_nat_iff {a b : ℕ} : finite a b ↔ (a ≠ 1 ∧ 0 < b) := begin rw [← not_iff_not, not_finite_iff_forall, not_and_distrib, ne.def, not_not, not_lt, nat.le_zero_iff], exact ⟨λ h, or_iff_not_imp_right.2 (λ hb, have ha : a ≠ 0, from λ ha, by simpa [ha] using h 1, by_contradiction (λ ha1 : a ≠ 1, have ha_gt_one : 1 < a, from lt_of_not_ge (λ ha', by { clear h, revert ha ha1, dec_trivial! }), not_lt_of_ge (le_of_dvd (nat.pos_of_ne_zero hb) (h b)) (lt_pow_self ha_gt_one b))), λ h, by cases h; simp *⟩ end end comm_monoid section comm_monoid_with_zero variable [comm_monoid_with_zero α] lemma ne_zero_of_finite {a b : α} (h : finite a b) : b ≠ 0 := let ⟨n, hn⟩ := h in λ hb, by simpa [hb] using hn variable [decidable_rel ((∣) : α → α → Prop)] @[simp] protected lemma zero (a : α) : multiplicity a 0 = ⊤ := roption.eq_none_iff.2 (λ n ⟨⟨k, hk⟩, _⟩, hk (dvd_zero _)) @[simp] lemma multiplicity_zero_eq_zero_of_ne_zero (a : α) (ha : a ≠ 0) : multiplicity 0 a = 0 := begin apply multiplicity.multiplicity_eq_zero_of_not_dvd, rwa zero_dvd_iff, end end comm_monoid_with_zero section comm_semiring variables [comm_semiring α] [decidable_rel ((∣) : α → α → Prop)] lemma min_le_multiplicity_add {p a b : α} : min (multiplicity p a) (multiplicity p b) ≤ multiplicity p (a + b) := (le_total (multiplicity p a) (multiplicity p b)).elim (λ h, by rw [min_eq_left h, multiplicity_le_multiplicity_iff]; exact λ n hn, dvd_add hn (multiplicity_le_multiplicity_iff.1 h n hn)) (λ h, by rw [min_eq_right h, multiplicity_le_multiplicity_iff]; exact λ n hn, dvd_add (multiplicity_le_multiplicity_iff.1 h n hn) hn) end comm_semiring section comm_ring variables [comm_ring α] [decidable_rel ((∣) : α → α → Prop)] open_locale classical @[simp] protected lemma neg (a b : α) : multiplicity a (-b) = multiplicity a b := roption.ext' (by simp only [multiplicity, enat.find, dvd_neg]) (λ h₁ h₂, enat.coe_inj.1 (by rw [enat.coe_get]; exact eq.symm (unique ((dvd_neg _ _).2 (pow_multiplicity_dvd _)) (mt (dvd_neg _ _).1 (is_greatest' _ (lt_succ_self _)))))) lemma multiplicity_add_of_gt {p a b : α} (h : multiplicity p b < multiplicity p a) : multiplicity p (a + b) = multiplicity p b := begin apply le_antisymm, { apply enat.le_of_lt_add_one, cases enat.ne_top_iff.mp (enat.ne_top_of_lt h) with k hk, rw [hk], rw_mod_cast [multiplicity_lt_iff_neg_dvd], intro h_dvd, rw [← dvd_add_iff_right] at h_dvd, apply multiplicity.is_greatest _ h_dvd, rw [hk], apply_mod_cast nat.lt_succ_self, rw [pow_dvd_iff_le_multiplicity, enat.coe_add, ← hk], exact enat.add_one_le_of_lt h }, { convert min_le_multiplicity_add, rw [min_eq_right (le_of_lt h)] } end lemma multiplicity_sub_of_gt {p a b : α} (h : multiplicity p b < multiplicity p a) : multiplicity p (a - b) = multiplicity p b := by { rw [sub_eq_add_neg, multiplicity_add_of_gt]; rwa [multiplicity.neg] } lemma multiplicity_add_eq_min {p a b : α} (h : multiplicity p a ≠ multiplicity p b) : multiplicity p (a + b) = min (multiplicity p a) (multiplicity p b) := begin rcases lt_trichotomy (multiplicity p a) (multiplicity p b) with hab|hab|hab, { rw [add_comm, multiplicity_add_of_gt hab, min_eq_left], exact le_of_lt hab }, { contradiction }, { rw [multiplicity_add_of_gt hab, min_eq_right], exact le_of_lt hab}, end end comm_ring section comm_cancel_monoid_with_zero variables [comm_cancel_monoid_with_zero α] lemma finite_mul_aux {p : α} (hp : prime p) : ∀ {n m : ℕ} {a b : α}, ¬p ^ (n + 1) ∣ a → ¬p ^ (m + 1) ∣ b → ¬p ^ (n + m + 1) ∣ a * b | n m := λ a b ha hb ⟨s, hs⟩, have p ∣ a * b, from ⟨p ^ (n + m) * s, by simp [hs, pow_add, mul_comm, mul_assoc, mul_left_comm]⟩, (hp.2.2 a b this).elim (λ ⟨x, hx⟩, have hn0 : 0 < n, from nat.pos_of_ne_zero (λ hn0, by clear _fun_match _fun_match; simpa [hx, hn0] using ha), have wf : (n - 1) < n, from nat.sub_lt_self hn0 dec_trivial, have hpx : ¬ p ^ (n - 1 + 1) ∣ x, from λ ⟨y, hy⟩, ha (hx.symm ▸ ⟨y, mul_right_cancel' hp.1 $ by rw [nat.sub_add_cancel hn0] at hy; simp [hy, pow_add, mul_comm, mul_assoc, mul_left_comm]⟩), have 1 ≤ n + m, from le_trans hn0 (le_add_right n m), finite_mul_aux hpx hb ⟨s, mul_right_cancel' hp.1 begin rw [← nat.sub_add_comm hn0, nat.sub_add_cancel this], clear _fun_match _fun_match finite_mul_aux, simp [*, mul_comm, mul_assoc, mul_left_comm, pow_add] at * end⟩) (λ ⟨x, hx⟩, have hm0 : 0 < m, from nat.pos_of_ne_zero (λ hm0, by clear _fun_match _fun_match; simpa [hx, hm0] using hb), have wf : (m - 1) < m, from nat.sub_lt_self hm0 dec_trivial, have hpx : ¬ p ^ (m - 1 + 1) ∣ x, from λ ⟨y, hy⟩, hb (hx.symm ▸ ⟨y, mul_right_cancel' hp.1 $ by rw [nat.sub_add_cancel hm0] at hy; simp [hy, pow_add, mul_comm, mul_assoc, mul_left_comm]⟩), finite_mul_aux ha hpx ⟨s, mul_right_cancel' hp.1 begin rw [add_assoc, nat.sub_add_cancel hm0], clear _fun_match _fun_match finite_mul_aux, simp [*, mul_comm, mul_assoc, mul_left_comm, pow_add] at * end⟩) lemma finite_mul {p a b : α} (hp : prime p) : finite p a → finite p b → finite p (a * b) := λ ⟨n, hn⟩ ⟨m, hm⟩, ⟨n + m, finite_mul_aux hp hn hm⟩ lemma finite_mul_iff {p a b : α} (hp : prime p) : finite p (a * b) ↔ finite p a ∧ finite p b := ⟨λ h, ⟨finite_of_finite_mul_right h, finite_of_finite_mul_left h⟩, λ h, finite_mul hp h.1 h.2⟩ lemma finite_pow {p a : α} (hp : prime p) : Π {k : ℕ} (ha : finite p a), finite p (a ^ k) | 0 ha := ⟨0, by simp [mt is_unit_iff_dvd_one.2 hp.2.1]⟩ | (k+1) ha := by rw [pow_succ]; exact finite_mul hp ha (finite_pow ha) variable [decidable_rel ((∣) : α → α → Prop)] @[simp] lemma multiplicity_self {a : α} (ha : ¬is_unit a) (ha0 : a ≠ 0) : multiplicity a a = 1 := eq_some_iff.2 ⟨by simp, λ ⟨b, hb⟩, ha (is_unit_iff_dvd_one.2 ⟨b, mul_left_cancel' ha0 $ by clear _fun_match; simpa [pow_succ, mul_assoc] using hb⟩)⟩ @[simp] lemma get_multiplicity_self {a : α} (ha : finite a a) : get (multiplicity a a) ha = 1 := roption.get_eq_iff_eq_some.2 (eq_some_iff.2 ⟨by simp, λ ⟨b, hb⟩, by rw [← mul_one a, pow_add, pow_one, mul_assoc, mul_assoc, mul_right_inj' (ne_zero_of_finite ha)] at hb; exact mt is_unit_iff_dvd_one.2 (not_unit_of_finite ha) ⟨b, by clear _fun_match; simp * at *⟩⟩) protected lemma mul' {p a b : α} (hp : prime p) (h : (multiplicity p (a * b)).dom) : get (multiplicity p (a * b)) h = get (multiplicity p a) ((finite_mul_iff hp).1 h).1 + get (multiplicity p b) ((finite_mul_iff hp).1 h).2 := have hdiva : p ^ get (multiplicity p a) ((finite_mul_iff hp).1 h).1 ∣ a, from pow_multiplicity_dvd _, have hdivb : p ^ get (multiplicity p b) ((finite_mul_iff hp).1 h).2 ∣ b, from pow_multiplicity_dvd _, have hpoweq : p ^ (get (multiplicity p a) ((finite_mul_iff hp).1 h).1 + get (multiplicity p b) ((finite_mul_iff hp).1 h).2) = p ^ get (multiplicity p a) ((finite_mul_iff hp).1 h).1 * p ^ get (multiplicity p b) ((finite_mul_iff hp).1 h).2, by simp [pow_add], have hdiv : p ^ (get (multiplicity p a) ((finite_mul_iff hp).1 h).1 + get (multiplicity p b) ((finite_mul_iff hp).1 h).2) ∣ a * b, by rw [hpoweq]; apply mul_dvd_mul; assumption, have hsucc : ¬p ^ ((get (multiplicity p a) ((finite_mul_iff hp).1 h).1 + get (multiplicity p b) ((finite_mul_iff hp).1 h).2) + 1) ∣ a * b, from λ h, not_or (is_greatest' _ (lt_succ_self _)) (is_greatest' _ (lt_succ_self _)) (by exact succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul hp hdiva hdivb h), by rw [← enat.coe_inj, enat.coe_get, eq_some_iff]; exact ⟨hdiv, hsucc⟩ open_locale classical protected lemma mul {p a b : α} (hp : prime p) : multiplicity p (a * b) = multiplicity p a + multiplicity p b := if h : finite p a ∧ finite p b then by rw [← enat.coe_get (finite_iff_dom.1 h.1), ← enat.coe_get (finite_iff_dom.1 h.2), ← enat.coe_get (finite_iff_dom.1 (finite_mul hp h.1 h.2)), ← enat.coe_add, enat.coe_inj, multiplicity.mul' hp]; refl else begin rw [eq_top_iff_not_finite.2 (mt (finite_mul_iff hp).1 h)], cases not_and_distrib.1 h with h h; simp [eq_top_iff_not_finite.2 h] end lemma finset.prod {β : Type*} {p : α} (hp : prime p) (s : finset β) (f : β → α) : multiplicity p (∏ x in s, f x) = ∑ x in s, multiplicity p (f x) := begin classical, induction s using finset.induction with a s has ih h, { simp only [finset.sum_empty, finset.prod_empty], convert one_right hp.not_unit }, { simp [has, ← ih], convert multiplicity.mul hp } end protected lemma pow' {p a : α} (hp : prime p) (ha : finite p a) : ∀ {k : ℕ}, get (multiplicity p (a ^ k)) (finite_pow hp ha) = k * get (multiplicity p a) ha | 0 := by dsimp [pow_zero]; simp [one_right hp.not_unit]; refl | (k+1) := by dsimp only [pow_succ]; erw [multiplicity.mul' hp, pow', add_mul, one_mul, add_comm] lemma pow {p a : α} (hp : prime p) : ∀ {k : ℕ}, multiplicity p (a ^ k) = k •ℕ (multiplicity p a) | 0 := by simp [one_right hp.not_unit] | (succ k) := by simp [pow_succ, succ_nsmul, pow, multiplicity.mul hp] lemma multiplicity_pow_self {p : α} (h0 : p ≠ 0) (hu : ¬ is_unit p) (n : ℕ) : multiplicity p (p ^ n) = n := by { rw [eq_some_iff], use dvd_refl _, rw [pow_dvd_pow_iff h0 hu], apply nat.not_succ_le_self } lemma multiplicity_pow_self_of_prime {p : α} (hp : prime p) (n : ℕ) : multiplicity p (p ^ n) = n := multiplicity_pow_self hp.ne_zero hp.not_unit n end comm_cancel_monoid_with_zero end multiplicity section nat open multiplicity lemma multiplicity_eq_zero_of_coprime {p a b : ℕ} (hp : p ≠ 1) (hle : multiplicity p a ≤ multiplicity p b) (hab : nat.coprime a b) : multiplicity p a = 0 := begin rw [multiplicity_le_multiplicity_iff] at hle, rw [← nonpos_iff_eq_zero, ← not_lt, enat.pos_iff_one_le, ← enat.coe_one, ← pow_dvd_iff_le_multiplicity], assume h, have := nat.dvd_gcd h (hle _ h), rw [coprime.gcd_eq_one hab, nat.dvd_one, pow_one] at this, exact hp this end end nat
11ebd74af5b4b2b4a7297d1d79bc6ba35991bfc3
97f752b44fd85ec3f635078a2dd125ddae7a82b6
/library/data/set/filter.lean
d7621eb6823d8892523f9c46d8844b12ff048f28
[ "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
21,894
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Jacob Gross Filters, following Hölzl, Immler, and Huffman, "Type classes and filters for mathematical analysis in Isabelle/HOL". -/ import data.set.function logic.identities algebra.complete_lattice namespace set open classical structure filter (A : Type) := (sets : set (set A)) (univ_mem_sets : univ ∈ sets) (inter_closed : ∀ {a b}, a ∈ sets → b ∈ sets → a ∩ b ∈ sets) (is_mono : ∀ {a b}, a ⊆ b → a ∈ sets → b ∈ sets) attribute filter.sets [coercion] namespace filter -- i.e. set.filter variable {A : Type} variables {P Q : A → Prop} variables {F₁ : filter A} {F₂ : filter A} {F : filter A} definition eventually (P : A → Prop) (F : filter A) : Prop := P ∈ F -- TODO: notation for eventually? -- notation `forallf` binders `∈` F `,` r:(scoped:1 P, P) := eventually r F -- notation `'∀f` binders `∈` F `,` r:(scoped:1 P, P) := eventually r F theorem eventually_true (F : filter A) : eventually (λx, true) F := !filter.univ_mem_sets theorem eventually_of_forall (F : filter A) (H : ∀ x, P x) : eventually P F := by rewrite [eq_univ_of_forall H]; apply eventually_true theorem eventually_mono (H₁ : eventually P F) (H₂ : ∀x, P x → Q x) : eventually Q F := !filter.is_mono H₂ H₁ theorem eventually_congr (H : ∀ x, P x ↔ Q x) (H' : eventually P F) : eventually Q F := have P = Q, from ext H, using this, by rewrite -this; exact H' theorem eventually_and (H₁ : eventually P F) (H₂ : eventually Q F) : eventually (λ x, P x ∧ Q x) F := !filter.inter_closed H₁ H₂ theorem eventually_of_eventually_and_left {P Q : A → Prop} {F : filter A} (H : eventually (λ x, P x ∧ Q x) F) : eventually P F := !filter.is_mono (λ x HPQ, and.elim_left HPQ) H theorem eventually_of_eventually_and_right {P Q : A → Prop} {F : filter A} (H : eventually (λ x, P x ∧ Q x) F) : eventually Q F := !filter.is_mono (λ x HPQ, and.elim_right HPQ) H theorem eventually_mp (H₁ : eventually (λx, P x → Q x) F) (H₂ : eventually P F) : eventually Q F := have ∀ x, (P x → Q x) ∧ P x → Q x, from take x, assume H, and.left H (and.right H), eventually_mono (eventually_and H₁ H₂) this theorem eventually_mpr (H₁ : eventually P F) (H₂ : eventually (λx, P x → Q x) F) : eventually Q F := eventually_mp H₂ H₁ variables (P Q F) theorem eventually_and_iff : eventually (λ x, P x ∧ Q x) F ↔ eventually P F ∧ eventually Q F := iff.intro (assume H, and.intro (eventually_mpr H (eventually_of_forall F (take x, and.left))) (eventually_mpr H (eventually_of_forall F (take x, and.right)))) (assume H, eventually_and (and.left H) (and.right H)) variables {P Q F} -- TODO: port eventually_ball_finite_distrib, etc. theorem eventually_choice {B : Type} [nonemptyB : nonempty B] {R : A → B → Prop} {F : filter A} (H : eventually (λ x, ∃ y, R x y) F) : ∃ f, eventually (λ x, R x (f x)) F := let f := λ x, epsilon (λ y, R x y) in exists.intro f (eventually_mono H (take x, suppose ∃ y, R x y, show R x (f x), from epsilon_spec this)) theorem exists_not_of_not_eventually (H : ¬ eventually P F) : ∃ x, ¬ P x := exists_not_of_not_forall (assume H', H (eventually_of_forall F H')) theorem eventually_iff_mp (H₁ : eventually (λ x, P x ↔ Q x) F) (H₂ : eventually P F) : eventually Q F := eventually_mono (eventually_and H₁ H₂) (λ x H, iff.mp (and.left H) (and.right H)) theorem eventually_iff_mpr (H₁ : eventually (λ x, P x ↔ Q x) F) (H₂ : eventually Q F) : eventually P F := eventually_mono (eventually_and H₁ H₂) (λ x H, iff.mpr (and.left H) (and.right H)) theorem eventually_iff_iff (H : eventually (λ x, P x ↔ Q x) F) : eventually P F ↔ eventually Q F := iff.intro (eventually_iff_mp H) (eventually_iff_mpr H) /- frequently -/ definition frequently (P : A → Prop) (F : filter A) : Prop := ¬ eventually (λ x, ¬ P x) F theorem not_frequently_of_eventually : eventually (λ x, ¬ P x) F → ¬ frequently P F := not_not_intro theorem frequently_mono (H₁ : frequently P F) (H₂ : ∀ x, P x → Q x) : frequently Q F := not.mto (λ H, eventually_mono H ( λ x, not.mto (H₂ x))) H₁ theorem frequently_mp (ev : eventually (λ x, P x → Q x) F) : frequently P F → frequently Q F := not.mto (λ H, eventually_mp (eventually_mono ev (λ x HPQ, not.mto HPQ)) H) theorem not_frequently_false : ¬ frequently (λ x, false) F := begin apply not_not_intro, apply eventually_congr, intro x, apply iff.symm not_false_iff, exact eventually_true F end section open classical theorem not_frequently_iff : ¬ frequently P F ↔ eventually (λ x, ¬ P x) F := by unfold frequently; rewrite not_not_iff theorem exists_of_frequently : frequently P F → ∃ x, P x := assume H, obtain x Hx, from !exists_not_of_not_eventually H, show _, from exists.intro x (not_not_elim Hx) theorem frequently_inl (H : frequently P F) : frequently (λx, P x ∨ Q x) F := assume H' : eventually (λx, ¬ (P x ∨ Q x)) F, have (λx, ¬ (P x ∨ Q x)) = λ x, ¬ P x ∧ ¬ Q x, by apply funext; intro x; rewrite not_or_iff_not_and_not, show false, using this, by rewrite this at H'; exact H (eventually_of_eventually_and_left H') theorem frequently_inr (H : frequently Q F) : frequently (λx, P x ∨ Q x) F := begin apply frequently_mp, apply eventually_of_forall, intro x, apply or.swap, exact frequently_inl H end end /- filters form a lattice under ⊇ -/ protected theorem eq : sets F₁ = sets F₂ → F₁ = F₂ := begin cases F₁ with s₁ u₁ i₁ m₁, cases F₂ with s₂ u₂ i₂ m₂, esimp, intro eqs₁s₂, revert [u₁, i₁, m₁, u₂, i₂, m₂], subst s₁, intros, exact rfl end namespace complete_lattice definition le [reducible] (F₁ F₂ : filter A) := F₁ ⊇ F₂ local infix `≤`:50 := le definition ge [reducible] (F₁ F₂ : filter A) := F₁ ⊆ F₂ local infix `≥`:50 := ge theorem le_refl (F : filter A) : F ≤ F := subset.refl _ theorem le_trans {F₁ F₂ F₃ : filter A} (H₁ : F₁ ≤ F₂) (H₂ : F₂ ≤ F₃) : F₁ ≤ F₃ := subset.trans H₂ H₁ theorem le_antisymm (H₁ : F₁ ≤ F₂) (H₂ : F₂ ≤ F₁) : F₁ = F₂ := filter.eq (eq_of_subset_of_subset H₂ H₁) definition sup (F₁ F₂ : filter A) : filter A := ⦃ filter, sets := F₁ ∩ F₂, univ_mem_sets := and.intro (filter.univ_mem_sets F₁) (filter.univ_mem_sets F₂), inter_closed := abstract λ a b Ha Hb, and.intro (filter.inter_closed F₁ (and.left Ha) (and.left Hb)) (filter.inter_closed F₂ (and.right Ha) (and.right Hb)) end, is_mono := abstract λ a b Hsub Ha, and.intro (filter.is_mono F₁ Hsub (and.left Ha)) (filter.is_mono F₂ Hsub (and.right Ha)) end ⦄ local infix ` ⊔ `:65 := sup definition inf (F₁ F₂ : filter A) : filter A := ⦃ filter, sets := {r | ∃₀ s ∈ F₁, ∃₀ t ∈ F₂, r ⊇ s ∩ t}, univ_mem_sets := abstract bounded_exists.intro (univ_mem_sets F₁) (bounded_exists.intro (univ_mem_sets F₂) (by rewrite univ_inter; apply subset.refl)) end, inter_closed := abstract λ a b Ha Hb, obtain a₁ [a₁F₁ [a₂ [a₂F₂ (Ha' : a ⊇ a₁ ∩ a₂)]]], from Ha, obtain b₁ [b₁F₁ [b₂ [b₂F₂ (Hb' : b ⊇ b₁ ∩ b₂)]]], from Hb, assert a₁ ∩ b₁ ∩ (a₂ ∩ b₂) = a₁ ∩ a₂ ∩ (b₁ ∩ b₂), by rewrite [*inter_assoc, inter_left_comm b₁], have a ∩ b ⊇ a₁ ∩ b₁ ∩ (a₂ ∩ b₂), begin rewrite this, apply subset_inter, {apply subset.trans, apply inter_subset_left, exact Ha'}, apply subset.trans, apply inter_subset_right, exact Hb' end, bounded_exists.intro (inter_closed F₁ a₁F₁ b₁F₁) (bounded_exists.intro (inter_closed F₂ a₂F₂ b₂F₂) this) end, is_mono := abstract λ a b Hsub Ha, obtain a₁ [a₁F₁ [a₂ [a₂F₂ (Ha' : a ⊇ a₁ ∩ a₂)]]], from Ha, bounded_exists.intro a₁F₁ (bounded_exists.intro a₂F₂ (subset.trans Ha' Hsub)) end ⦄ infix `⊓`:70 := inf definition Sup (S : set (filter A)) : filter A := ⦃ filter, sets := ⋂ F ∈ S, sets F, univ_mem_sets := λ F FS, univ_mem_sets F, inter_closed := abstract λ a b Ha Hb F FS, inter_closed F (Ha F FS) (Hb F FS) end, is_mono := abstract λ a b asubb Ha F FS, is_mono F asubb (Ha F FS) end ⦄ local prefix `⨆ `:65 := Sup definition Inf (S : set (filter A)) : filter A := Sup {F | ∀ G, G ∈ S → G ≥ F} local prefix `⨅ `:70 := Inf theorem le_sup_left (F₁ F₂ : filter A) : F₁ ≤ F₁ ⊔ F₂ := inter_subset_left _ _ theorem le_sup_right (F₁ F₂ : filter A) : F₂ ≤ F₁ ⊔ F₂ := inter_subset_right _ _ theorem sup_le (H₁ : F₁ ≤ F) (H₂ : F₂ ≤ F) : F₁ ⊔ F₂ ≤ F := subset_inter H₁ H₂ theorem inf_le_left (F₁ F₂ : filter A) : F₁ ⊓ F₂ ≤ F₁ := take s, suppose s ∈ F₁, bounded_exists.intro `s ∈ F₁` (bounded_exists.intro (univ_mem_sets F₂) (by rewrite inter_univ; apply subset.refl)) theorem inf_le_right (F₁ F₂ : filter A) : F₁ ⊓ F₂ ≤ F₂ := take s, suppose s ∈ F₂, bounded_exists.intro (univ_mem_sets F₁) (bounded_exists.intro `s ∈ F₂` (by rewrite univ_inter; apply subset.refl)) theorem le_inf (H₁ : F ≤ F₁) (H₂ : F ≤ F₂) : F ≤ F₁ ⊓ F₂ := take s : set A, suppose (s ∈ (F₁ ⊓ F₂ : set (set A))), obtain a₁ [a₁F₁ [a₂ [a₂F₂ (Hsub : s ⊇ a₁ ∩ a₂)]]], from this, have a₁ ∈ F, from H₁ a₁F₁, have a₂ ∈ F, from H₂ a₂F₂, show s ∈ F, from is_mono F Hsub (inter_closed F `a₁ ∈ F` `a₂ ∈ F`) theorem Sup_le {F : filter A} {S : set (filter A)} (H : ∀₀ G ∈ S, G ≤ F) : ⨆ S ≤ F := λ s Fs G GS, H GS Fs theorem le_Sup {F : filter A} {S : set (filter A)} (FS : F ∈ S) : F ≤ ⨆ S := λ s sInfS, sInfS F FS theorem le_Inf {F : filter A} {S : set (filter A)} (H : ∀₀ G ∈ S, F ≤ G) : F ≤ ⨅ S := le_Sup H theorem Inf_le {F : filter A} {S : set (filter A)} (FS : F ∈ S) : ⨅ S ≤ F := Sup_le (λ G GS, GS F FS) end complete_lattice protected definition complete_lattice [reducible] [trans_instance] : complete_lattice (filter A) := ⦃ complete_lattice, le := complete_lattice.le, le_refl := complete_lattice.le_refl, le_trans := @complete_lattice.le_trans A, le_antisymm := @complete_lattice.le_antisymm A, inf := complete_lattice.inf, le_inf := @complete_lattice.le_inf A, inf_le_left := @complete_lattice.inf_le_left A, inf_le_right := @complete_lattice.inf_le_right A, sup := complete_lattice.sup, sup_le := @complete_lattice.sup_le A, le_sup_left := complete_lattice.le_sup_left, le_sup_right := complete_lattice.le_sup_right, Inf := complete_lattice.Inf, Inf_le := @complete_lattice.Inf_le A, le_Inf := @complete_lattice.le_Inf A, Sup := complete_lattice.Sup, Sup_le := @complete_lattice.Sup_le A, le_Sup := @complete_lattice.le_Sup A ⦄ protected theorem subset_of_le {F₁ F₂ : filter A} (H : F₁ ≤ F₂) : sets F₂ ⊆ sets F₁ := H protected theorem le_of_subset {F₁ F₂ : filter A} (H : sets F₂ ⊆ sets F₁) : F₁ ≤ F₂ := H theorem sets_Sup (S : set (filter A)) : sets (⨆ S) = ⋂ F ∈ S, sets F := rfl theorem sets_sup (F₁ F₂ : filter A) : sets (F₁ ⊔ F₂) = sets F₁ ∩ sets F₂ := rfl theorem sets_inf (F₁ F₂ : filter A) : sets (F₁ ⊓ F₂) = {r | ∃₀ s ∈ F₁, ∃₀ t ∈ F₂, r ⊇ s ∩ t} := rfl /- eventually and lattice operations -/ theorem eventually_of_le (H₁ : eventually P F₁) (H₂ : F₂ ≤ F₁) : eventually P F₂ := filter.subset_of_le H₂ H₁ theorem le_of_forall_eventually (H : ∀ P, eventually P F₁ → eventually P F₂) : F₂ ≤ F₁ := H theorem eventually_Sup_iff (P : A → Prop) (S : set (filter A)) : eventually P (⨆ S) ↔ ∀₀ F ∈ S, eventually P F := by rewrite [↑eventually, sets_Sup] theorem eventually_Sup {P : A → Prop} {S : set (filter A)} (H : ∀₀ F ∈ S, eventually P F) : eventually P (⨆ S) := iff.mpr (eventually_Sup_iff P S) H theorem eventually_of_eventually_Sup {P : A → Prop} {S : set (filter A)} (H : eventually P (⨆ S)) {F : filter A} (FS : F ∈ S) : eventually P F := iff.mp (eventually_Sup_iff P S) H F FS theorem eventually_sup_iff (P : A → Prop) (F₁ F₂ : filter A) : eventually P (F₁ ⊔ F₂) ↔ eventually P F₁ ∧ eventually P F₂ := by rewrite [↑eventually, sets_sup] theorem eventually_sup {P : A → Prop} {F₁ F₂ : filter A} (H₁ : eventually P F₁) (H₂ : eventually P F₂) : eventually P (F₁ ⊔ F₂) := iff.mpr (eventually_sup_iff P F₁ F₂) (and.intro H₁ H₂) theorem eventually_of_eventually_sup_left {P : A → Prop} {F₁ F₂ : filter A} (H : eventually P (F₁ ⊔ F₂)) : eventually P F₁ := and.left (iff.mp (eventually_sup_iff P F₁ F₂) H) theorem eventually_of_eventually_sup_right {P : A → Prop} {F₁ F₂ : filter A} (H : eventually P (F₁ ⊔ F₂)) : eventually P F₂ := and.right (iff.mp (eventually_sup_iff P F₁ F₂) H) theorem eventually_inf_iff (P : A → Prop) (F₁ F₂ : filter A) : eventually P (F₁ ⊓ F₂) ↔ (∃ S, eventually S F₁ ∧ ∃ T, eventually T F₂ ∧ (P ⊇ S ∩ T)) := by rewrite [↑eventually, sets_inf] theorem eventually_inf {P : A → Prop} {F₁ F₂ : filter A} {S : A → Prop} (HS : eventually S F₁) (T : A → Prop) (HT : eventually T F₂) (HP : P ⊇ S ∩ T) : eventually P (F₁ ⊓ F₂) := iff.mpr (eventually_inf_iff P F₁ F₂) (exists.intro S (and.intro HS (exists.intro T (and.intro HT HP)))) theorem exists_of_eventually_inf {P : A → Prop} {F₁ F₂ : filter A} (H : eventually P (F₁ ⊓ F₂)) : ∃ S, eventually S F₁ ∧ ∃ T, eventually T F₂ ∧ (P ⊇ S ∩ T) := iff.mp (eventually_inf_iff P F₁ F₂) H /- top and bot -/ protected definition bot (A : Type) : filter A := ⦃ filter, sets := univ, univ_mem_sets := trivial, inter_closed := λ a b Ha Hb, trivial, is_mono := λ a b Ha Hsub, trivial ⦄ protected definition top (A : Type) : filter A := ⦃ filter, sets := '{univ}, univ_mem_sets := !or.inl rfl, inter_closed := abstract λ a b Ha Hb, by rewrite [*!mem_singleton_iff at *]; substvars; exact !inter_univ end, is_mono := abstract λ a b Hsub Ha, begin rewrite [mem_singleton_iff at Ha], subst [Ha], exact or.inl (eq_univ_of_univ_subset Hsub) end end ⦄ protected theorem bot_eq : ⊥ = filter.bot A := le.antisymm !bot_le begin apply le_of_forall_eventually, intro P H, apply mem_univ end protected theorem top_eq : ⊤ = filter.top A := le.antisymm (le_of_forall_eventually (λ P H, have P = univ, from eq_of_mem_singleton H, by+ rewrite this; apply eventually_true ⊤)) !le_top theorem sets_bot_eq : sets ⊥ = (univ : set (set A)) := by rewrite filter.bot_eq theorem sets_top_eq : sets ⊤ = ('{univ} : set (set A)) := by rewrite filter.top_eq theorem eventually_bot (P : A → Prop) : eventually P ⊥ := by rewrite [↑eventually, sets_bot_eq]; apply mem_univ theorem eventually_top_of_forall (H : ∀ x, P x) : eventually P ⊤ := by rewrite [↑eventually, sets_top_eq, mem_singleton_iff]; exact eq_univ_of_forall H theorem forall_of_eventually_top : eventually P ⊤ → ∀ x, P x := by rewrite [↑eventually, sets_top_eq, mem_singleton_iff]; intro H x; rewrite H; exact trivial theorem eventually_top_iff (P : A → Prop) : eventually P top ↔ ∀ x, P x := iff.intro forall_of_eventually_top eventually_top_of_forall /- filter generated by a collection of sets -/ inductive sets_generated_by {A : Type} (B : set (set A)) : set A → Prop := | generators_mem : ∀ ⦃s : set A⦄, s ∈ B → sets_generated_by B s | univ_mem : sets_generated_by B univ | inter_mem : ∀ {a b}, sets_generated_by B a → sets_generated_by B b → sets_generated_by B (a ∩ b) | mono : ∀ {a b}, a ⊆ b → sets_generated_by B a → sets_generated_by B b definition filter_generated_by [reducible] {A : Type} (B : set (set A)) : filter A := ⦃filter, sets := sets_generated_by B, univ_mem_sets := sets_generated_by.univ_mem B, inter_closed := @sets_generated_by.inter_mem A B, is_mono := @sets_generated_by.mono A B ⦄ theorem generators_subset_generated_by (B : set (set A)) : B ⊆ filter_generated_by B := λ s H, sets_generated_by.generators_mem H theorem sets_generated_by_initial {B : set (set A)} {F : filter A} (H : B ⊆ sets F) : sets_generated_by B ⊆ F := begin intro s Hs, induction Hs with s sB s t os ot soA toA S SB SOA, {exact H sB}, {exact filter.univ_mem_sets F}, {exact filter.inter_closed F soA toA}, exact filter.is_mono F SOA v_0 end theorem le_filter_generated_by {B : set (set A)} {F : filter A} (H : B ⊆ sets F) : F ≤ filter_generated_by B := sets_generated_by_initial H /- principal filter -/ definition principal (s : set A) : filter A := filter_generated_by '{s} theorem mem_principal (s : set A) : s ∈ principal s := !generators_subset_generated_by !mem_singleton theorem eventually_principal {s : set A} {P : A → Prop} (H : s ⊆ P) : eventually P (principal s) := !filter.is_mono H !mem_principal theorem subset_of_eventually_principal {s : set A} {P : A → Prop} (H : eventually P (principal s)) : s ⊆ P := begin induction H with s' Ps' a b Ha Hb ssuba ssubb a b asubb Ha ssuba, {rewrite [eq_of_mem_singleton Ps']; exact subset.refl s}, {exact subset_univ s}, {exact subset_inter ssuba ssubb}, {exact subset.trans ssuba asubb} end theorem eventually_principal_iff (s P : set A) : eventually P (principal s) ↔ (s ⊆ P) := iff.intro subset_of_eventually_principal eventually_principal theorem sets_principal (s : set A) : sets (principal s) = { t | s ⊆ t } := ext (take P, eventually_principal_iff s P) theorem principal_empty : principal (∅ : set A) = ⊥ := begin apply filter.eq, rewrite [sets_principal, sets_bot_eq], show { t | ∅ ⊆ t} = univ, from ext (take x, iff.intro (assume H, trivial) (assume H, empty_subset x)) end theorem principal_univ_eq : principal (@univ A) = ⊤ := begin apply filter.eq, rewrite [sets_principal, sets_top_eq], show { t | univ ⊆ t} = '{univ}, from ext (take x, iff.intro (assume H : univ ⊆ x, mem_singleton_of_eq (!eq_univ_of_univ_subset H)) (assume H : x ∈ '{univ}, by rewrite [eq_of_mem_singleton H]; apply subset.refl)) end theorem principal_le_principal {s t : set A} (H : s ⊆ t) : principal s ≤ principal t := begin apply filter.le_of_subset, rewrite *sets_principal, show { u | t ⊆ u } ⊆ { u | s ⊆ u }, from take u, suppose t ⊆ u, subset.trans H this end theorem subset_of_principal_le_principal {s t : set A} (H : principal s ≤ principal t) : s ⊆ t := begin note H' := filter.subset_of_le H, revert H', rewrite *sets_principal, intro H', exact H' (subset.refl t) end theorem principal_refines_principal_iff {s t : set A} : principal s ≤ principal t ↔ s ⊆ t := iff.intro subset_of_principal_le_principal principal_le_principal theorem principal_sup_principal {s t : set A} : principal s ⊔ principal t = principal (s ∪ t) := begin apply filter.eq, rewrite [sets_sup, *sets_principal], apply ext, intro u, apply iff.intro, exact (suppose s ⊆ u ∧ t ⊆ u, show s ∪ t ⊆ u, from union_subset (and.left this) (and.right this)), exact suppose s ∪ t ⊆ u, and.intro (subset.trans (subset_union_left _ _) this) (subset.trans (subset_union_right _ _) this) end theorem principal_inf_principal {s t : set A} : principal s ⊓ principal t = principal (s ∩ t) := begin apply filter.eq, rewrite [sets_inf, *sets_principal], apply ext, intro u, apply iff.intro, {intro H, show s ∩ t ⊆ u, from obtain s' [(ss' : s ⊆ s') [t' [(tt' : t ⊆ t') (Hu : s' ∩ t' ⊆ u)]]], from H, take x, assume H', Hu (and.intro (ss' (and.left H')) (tt' (and.right H')))}, {intro H', exact exists.intro s (and.intro (subset.refl s) (exists.intro t (and.intro (subset.refl t) H')))} end end filter end set
2ed5e3c2f0d8fc9fbf82b124500e4cd0f6712c50
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/set_attr1.lean
0b4e2b7fc8c8ec1a513196042c2173dee0c16d65
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
391
lean
open tactic constant f : nat → nat constant foo : ∀ n, f n = n + 1 constant add_zero : ∀ n, n + 0 = n definition ex1 (n : nat) : f n + 0 = n + 1 := by do set_basic_attribute `simp `foo, set_basic_attribute `simp `add_zero, simp definition ex2 (n : nat) : f n + 0 = n + 1 := by do unset_attribute `simp `foo, simp -- should fail since we remove [simp] attribute from `foo`
f3e7c78a9b1881caf427873f5577a1c8d833bc0d
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/polynomial/degree/trailing_degree_auto.lean
8ee4a397cce13c6c25ebcccd1d22d8a7c4a5d1c2
[]
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,584
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.polynomial.degree.definitions import Mathlib.PostPort universes u v namespace Mathlib /-! # Trailing degree of univariate polynomials ## Main definitions * `trailing_degree p`: the multiplicity of `X` in the polynomial `p` * `nat_trailing_degree`: a variant of `trailing_degree` that takes values in the natural numbers * `trailing_coeff`: the coefficient at index `nat_trailing_degree p` Converts most results about `degree`, `nat_degree` and `leading_coeff` to results about the bottom end of a polynomial -/ namespace polynomial /-- `trailing_degree p` is the multiplicity of `x` in the polynomial `p`, i.e. the smallest `X`-exponent in `p`. `trailing_degree p = some n` when `p ≠ 0` and `n` is the smallest power of `X` that appears in `p`, otherwise `trailing_degree 0 = ⊤`. -/ def trailing_degree {R : Type u} [semiring R] (p : polynomial R) : with_top ℕ := finset.inf (finsupp.support p) some theorem trailing_degree_lt_wf {R : Type u} [semiring R] : well_founded fun (p q : polynomial R) => trailing_degree p < trailing_degree q := inv_image.wf trailing_degree (with_top.well_founded_lt nat.lt_wf) /-- `nat_trailing_degree p` forces `trailing_degree p` to `ℕ`, by defining `nat_trailing_degree ⊤ = 0`. -/ def nat_trailing_degree {R : Type u} [semiring R] (p : polynomial R) : ℕ := option.get_or_else (trailing_degree p) 0 /-- `trailing_coeff p` gives the coefficient of the smallest power of `X` in `p`-/ def trailing_coeff {R : Type u} [semiring R] (p : polynomial R) : R := coeff p (nat_trailing_degree p) /-- a polynomial is `monic_at` if its trailing coefficient is 1 -/ def trailing_monic {R : Type u} [semiring R] (p : polynomial R) := trailing_coeff p = 1 theorem trailing_monic.def {R : Type u} [semiring R] {p : polynomial R} : trailing_monic p ↔ trailing_coeff p = 1 := iff.rfl protected instance trailing_monic.decidable {R : Type u} [semiring R] {p : polynomial R} [DecidableEq R] : Decidable (trailing_monic p) := eq.mpr sorry (_inst_2 (trailing_coeff p) 1) @[simp] theorem trailing_monic.trailing_coeff {R : Type u} [semiring R] {p : polynomial R} (hp : trailing_monic p) : trailing_coeff p = 1 := hp @[simp] theorem trailing_degree_zero {R : Type u} [semiring R] : trailing_degree 0 = ⊤ := rfl @[simp] theorem nat_trailing_degree_zero {R : Type u} [semiring R] : nat_trailing_degree 0 = 0 := rfl theorem trailing_degree_eq_top {R : Type u} [semiring R] {p : polynomial R} : trailing_degree p = ⊤ ↔ p = 0 := sorry theorem trailing_degree_eq_nat_trailing_degree {R : Type u} [semiring R] {p : polynomial R} (hp : p ≠ 0) : trailing_degree p = ↑(nat_trailing_degree p) := sorry theorem trailing_degree_eq_iff_nat_trailing_degree_eq {R : Type u} [semiring R] {p : polynomial R} {n : ℕ} (hp : p ≠ 0) : trailing_degree p = ↑n ↔ nat_trailing_degree p = n := sorry theorem trailing_degree_eq_iff_nat_trailing_degree_eq_of_pos {R : Type u} [semiring R] {p : polynomial R} {n : ℕ} (hn : 0 < n) : trailing_degree p = ↑n ↔ nat_trailing_degree p = n := sorry theorem nat_trailing_degree_eq_of_trailing_degree_eq_some {R : Type u} [semiring R] {p : polynomial R} {n : ℕ} (h : trailing_degree p = ↑n) : nat_trailing_degree p = n := sorry @[simp] theorem nat_trailing_degree_le_trailing_degree {R : Type u} [semiring R] {p : polynomial R} : ↑(nat_trailing_degree p) ≤ trailing_degree p := sorry theorem nat_trailing_degree_eq_of_trailing_degree_eq {R : Type u} {S : Type v} [semiring R] {p : polynomial R} [semiring S] {q : polynomial S} (h : trailing_degree p = trailing_degree q) : nat_trailing_degree p = nat_trailing_degree q := sorry theorem le_trailing_degree_of_ne_zero {R : Type u} {n : ℕ} [semiring R] {p : polynomial R} (h : coeff p n ≠ 0) : trailing_degree p ≤ ↑n := (fun (this : finset.inf (finsupp.support p) some ≤ some n) => this) (finset.inf_le (iff.mpr finsupp.mem_support_iff h)) theorem nat_trailing_degree_le_of_ne_zero {R : Type u} {n : ℕ} [semiring R] {p : polynomial R} (h : coeff p n ≠ 0) : nat_trailing_degree p ≤ n := sorry theorem trailing_degree_le_trailing_degree {R : Type u} [semiring R] {p : polynomial R} {q : polynomial R} (h : coeff q (nat_trailing_degree p) ≠ 0) : trailing_degree q ≤ trailing_degree p := sorry theorem trailing_degree_ne_of_nat_trailing_degree_ne {R : Type u} [semiring R] {p : polynomial R} {n : ℕ} : nat_trailing_degree p ≠ n → trailing_degree p ≠ ↑n := sorry theorem nat_trailing_degree_le_of_trailing_degree_le {R : Type u} [semiring R] {p : polynomial R} {n : ℕ} {hp : p ≠ 0} (H : ↑n ≤ trailing_degree p) : n ≤ nat_trailing_degree p := iff.mp with_top.coe_le_coe (eq.mp (Eq._oldrec (Eq.refl (↑n ≤ trailing_degree p)) (trailing_degree_eq_nat_trailing_degree hp)) H) theorem nat_trailing_degree_le_nat_trailing_degree {R : Type u} [semiring R] {p : polynomial R} {q : polynomial R} {hq : q ≠ 0} (hpq : trailing_degree p ≤ trailing_degree q) : nat_trailing_degree p ≤ nat_trailing_degree q := sorry @[simp] theorem trailing_degree_monomial {R : Type u} {a : R} {n : ℕ} [semiring R] (ha : a ≠ 0) : trailing_degree (coe_fn (monomial n) a) = ↑n := sorry theorem nat_trailing_degree_monomial {R : Type u} {a : R} {n : ℕ} [semiring R] (ha : a ≠ 0) : nat_trailing_degree (coe_fn (monomial n) a) = n := sorry theorem nat_trailing_degree_monomial_le {R : Type u} {a : R} {n : ℕ} [semiring R] : nat_trailing_degree (coe_fn (monomial n) a) ≤ n := sorry theorem le_trailing_degree_monomial {R : Type u} {a : R} {n : ℕ} [semiring R] : ↑n ≤ trailing_degree (coe_fn (monomial n) a) := sorry @[simp] theorem trailing_degree_C {R : Type u} {a : R} [semiring R] (ha : a ≠ 0) : trailing_degree (coe_fn C a) = 0 := trailing_degree_monomial ha theorem le_trailing_degree_C {R : Type u} {a : R} [semiring R] : 0 ≤ trailing_degree (coe_fn C a) := le_trailing_degree_monomial theorem trailing_degree_one_le {R : Type u} [semiring R] : 0 ≤ trailing_degree 1 := eq.mpr (id (Eq._oldrec (Eq.refl (0 ≤ trailing_degree 1)) (Eq.symm C_1))) le_trailing_degree_C @[simp] theorem nat_trailing_degree_C {R : Type u} [semiring R] (a : R) : nat_trailing_degree (coe_fn C a) = 0 := iff.mp nonpos_iff_eq_zero nat_trailing_degree_monomial_le @[simp] theorem nat_trailing_degree_one {R : Type u} [semiring R] : nat_trailing_degree 1 = 0 := nat_trailing_degree_C 1 @[simp] theorem nat_trailing_degree_nat_cast {R : Type u} [semiring R] (n : ℕ) : nat_trailing_degree ↑n = 0 := sorry @[simp] theorem trailing_degree_C_mul_X_pow {R : Type u} {a : R} [semiring R] (n : ℕ) (ha : a ≠ 0) : trailing_degree (coe_fn C a * X ^ n) = ↑n := eq.mpr (id (Eq._oldrec (Eq.refl (trailing_degree (coe_fn C a * X ^ n) = ↑n)) (C_mul_X_pow_eq_monomial a n))) (eq.mpr (id (Eq._oldrec (Eq.refl (trailing_degree (coe_fn (monomial n) a) = ↑n)) (trailing_degree_monomial ha))) (Eq.refl ↑n)) theorem le_trailing_degree_C_mul_X_pow {R : Type u} [semiring R] (n : ℕ) (a : R) : ↑n ≤ trailing_degree (coe_fn C a * X ^ n) := eq.mpr (id (Eq._oldrec (Eq.refl (↑n ≤ trailing_degree (coe_fn C a * X ^ n))) (C_mul_X_pow_eq_monomial a n))) le_trailing_degree_monomial theorem coeff_eq_zero_of_trailing_degree_lt {R : Type u} {n : ℕ} [semiring R] {p : polynomial R} (h : ↑n < trailing_degree p) : coeff p n = 0 := iff.mp not_not (mt le_trailing_degree_of_ne_zero (not_le_of_gt h)) theorem coeff_eq_zero_of_lt_nat_trailing_degree {R : Type u} [semiring R] {p : polynomial R} {n : ℕ} (h : n < nat_trailing_degree p) : coeff p n = 0 := sorry @[simp] theorem coeff_nat_trailing_degree_pred_eq_zero {R : Type u} [semiring R] {p : polynomial R} {hp : 0 < ↑(nat_trailing_degree p)} : coeff p (nat_trailing_degree p - 1) = 0 := coeff_eq_zero_of_lt_nat_trailing_degree (nat.sub_lt (iff.mp (with_top.zero_lt_coe (nat_trailing_degree p)) hp) nat.one_pos) theorem le_trailing_degree_X_pow {R : Type u} [semiring R] (n : ℕ) : ↑n ≤ trailing_degree (X ^ n) := sorry theorem le_trailing_degree_X {R : Type u} [semiring R] : 1 ≤ trailing_degree X := le_trailing_degree_monomial theorem nat_trailing_degree_X_le {R : Type u} [semiring R] : nat_trailing_degree X ≤ 1 := nat_trailing_degree_monomial_le @[simp] theorem trailing_coeff_eq_zero {R : Type u} [semiring R] {p : polynomial R} : trailing_coeff p = 0 ↔ p = 0 := sorry theorem trailing_coeff_nonzero_iff_nonzero {R : Type u} [semiring R] {p : polynomial R} : trailing_coeff p ≠ 0 ↔ p ≠ 0 := not_congr trailing_coeff_eq_zero theorem nat_trailing_degree_mem_support_of_nonzero {R : Type u} [semiring R] {p : polynomial R} : p ≠ 0 → nat_trailing_degree p ∈ finsupp.support p := iff.mpr mem_support_iff_coeff_ne_zero ∘ iff.mpr trailing_coeff_nonzero_iff_nonzero theorem nat_trailing_degree_le_of_mem_supp {R : Type u} [semiring R] {p : polynomial R} (a : ℕ) : a ∈ finsupp.support p → nat_trailing_degree p ≤ a := nat_trailing_degree_le_of_ne_zero ∘ iff.mp mem_support_iff_coeff_ne_zero theorem nat_trailing_degree_eq_support_min' {R : Type u} [semiring R] {p : polynomial R} (h : p ≠ 0) : nat_trailing_degree p = finset.min' (finsupp.support p) (iff.mpr nonempty_support_iff h) := sorry @[simp] theorem trailing_degree_one {R : Type u} [semiring R] [nontrivial R] : trailing_degree 1 = 0 := trailing_degree_C one_ne_zero @[simp] theorem trailing_degree_X {R : Type u} [semiring R] [nontrivial R] : trailing_degree X = 1 := trailing_degree_monomial one_ne_zero @[simp] theorem nat_trailing_degree_X {R : Type u} [semiring R] [nontrivial R] : nat_trailing_degree X = 1 := nat_trailing_degree_monomial one_ne_zero @[simp] theorem trailing_degree_neg {R : Type u} [ring R] (p : polynomial R) : trailing_degree (-p) = trailing_degree p := sorry @[simp] theorem nat_trailing_degree_neg {R : Type u} [ring R] (p : polynomial R) : nat_trailing_degree (-p) = nat_trailing_degree p := sorry @[simp] theorem nat_trailing_degree_int_cast {R : Type u} [ring R] (n : ℤ) : nat_trailing_degree ↑n = 0 := sorry /-- The second-lowest coefficient, or 0 for constants -/ def next_coeff_up {R : Type u} [semiring R] (p : polynomial R) : R := ite (nat_trailing_degree p = 0) 0 (coeff p (nat_trailing_degree p + 1)) @[simp] theorem next_coeff_up_C_eq_zero {R : Type u} [semiring R] (c : R) : next_coeff_up (coe_fn C c) = 0 := sorry theorem next_coeff_up_of_pos_nat_trailing_degree {R : Type u} [semiring R] (p : polynomial R) (hp : 0 < nat_trailing_degree p) : next_coeff_up p = coeff p (nat_trailing_degree p + 1) := sorry theorem coeff_nat_trailing_degree_eq_zero_of_trailing_degree_lt {R : Type u} [semiring R] {p : polynomial R} {q : polynomial R} (h : trailing_degree p < trailing_degree q) : coeff q (nat_trailing_degree p) = 0 := coeff_eq_zero_of_trailing_degree_lt (has_le.le.trans_lt nat_trailing_degree_le_trailing_degree h) theorem ne_zero_of_trailing_degree_lt {R : Type u} [semiring R] {p : polynomial R} {n : with_top ℕ} (h : trailing_degree p < n) : p ≠ 0 := sorry end Mathlib
07b10f7bd17903ab54a3124564e364a46764410e
d642a6b1261b2cbe691e53561ac777b924751b63
/src/measure_theory/decomposition.lean
7dd48fafb7ca885666d47e9d9a73514a0ae900b6
[ "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
8,677
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl Hahn decomposition theorem TODO: * introduce finite measures (into nnreal) * show general for signed measures (into ℝ) -/ import measure_theory.measure_space open set lattice filter open_locale classical namespace measure_theory variables {α : Type*} [measurable_space α] {μ ν : measure α} -- suddenly this is necessary?! private lemma aux {m : ℕ} {γ d : ℝ} (h : γ - (1 / 2) ^ m < d) : γ - 2 * (1 / 2) ^ m + (1 / 2) ^ m ≤ d := by linarith lemma hahn_decomposition (hμ : μ univ < ⊤) (hν : ν univ < ⊤) : ∃s, is_measurable s ∧ (∀t, is_measurable t → t ⊆ s → ν t ≤ μ t) ∧ (∀t, is_measurable t → t ⊆ - s → μ t ≤ ν t) := begin let d : set α → ℝ := λs, ((μ s).to_nnreal : ℝ) - (ν s).to_nnreal, let c : set ℝ := d '' {s | is_measurable s }, let γ : ℝ := Sup c, have hμ : ∀s, μ s < ⊤ := assume s, lt_of_le_of_lt (measure_mono $ subset_univ _) hμ, have hν : ∀s, ν s < ⊤ := assume s, lt_of_le_of_lt (measure_mono $ subset_univ _) hν, have to_nnreal_μ : ∀s, ((μ s).to_nnreal : ennreal) = μ s := (assume s, ennreal.coe_to_nnreal $ ne_top_of_lt $ hμ _), have to_nnreal_ν : ∀s, ((ν s).to_nnreal : ennreal) = ν s := (assume s, ennreal.coe_to_nnreal $ ne_top_of_lt $ hν _), have d_empty : d ∅ = 0, { simp [d], rw [measure_empty, measure_empty], simp }, have d_split : ∀s t, is_measurable s → is_measurable t → d s = d (s \ t) + d (s ∩ t), { assume s t hs ht, simp only [d], rw [measure_eq_inter_diff hs ht, measure_eq_inter_diff hs ht, ennreal.to_nnreal_add (hμ _) (hμ _), ennreal.to_nnreal_add (hν _) (hν _), nnreal.coe_add, nnreal.coe_add], simp only [sub_eq_add_neg, neg_add], ac_refl }, have d_Union : ∀(s : ℕ → set α), (∀n, is_measurable (s n)) → monotone s → tendsto (λn, d (s n)) at_top (nhds (d (⋃n, s n))), { assume s hs hm, refine tendsto_sub _ _; refine (nnreal.tendsto_coe.2 $ (ennreal.tendsto_to_nnreal $ @ne_top_of_lt _ _ _ ⊤ _).comp $ tendsto_measure_Union hs hm), exact hμ _, exact hν _ }, have d_Inter : ∀(s : ℕ → set α), (∀n, is_measurable (s n)) → (∀n m, n ≤ m → s m ⊆ s n) → tendsto (λn, d (s n)) at_top (nhds (d (⋂n, s n))), { assume s hs hm, refine tendsto_sub _ _; refine (nnreal.tendsto_coe.2 $ (ennreal.tendsto_to_nnreal $ @ne_top_of_lt _ _ _ ⊤ _).comp $ tendsto_measure_Inter hs hm _), exact hμ _, exact ⟨0, hμ _⟩, exact hν _, exact ⟨0, hν _⟩ }, have bdd_c : bdd_above c, { use (μ univ).to_nnreal, rintros r ⟨s, hs, rfl⟩, refine le_trans (sub_le_self _ $ nnreal.coe_nonneg _) _, rw [← nnreal.coe_le, ← ennreal.coe_le_coe, to_nnreal_μ, to_nnreal_μ], exact measure_mono (subset_univ _) }, have c_nonempty : c ≠ ∅ := ne_empty_of_mem (mem_image_of_mem _ is_measurable.empty), have d_le_γ : ∀s, is_measurable s → d s ≤ γ := assume s hs, le_cSup bdd_c ⟨s, hs, rfl⟩, have : ∀n:ℕ, ∃s : set α, is_measurable s ∧ γ - (1/2)^n < d s, { assume n, have : γ - (1/2)^n < γ := sub_lt_self γ (pow_pos (half_pos zero_lt_one) n), rcases exists_lt_of_lt_cSup c_nonempty this with ⟨r, ⟨s, hs, rfl⟩, hlt⟩, exact ⟨s, hs, hlt⟩ }, rcases classical.axiom_of_choice this with ⟨e, he⟩, change ℕ → set α at e, have he₁ : ∀n, is_measurable (e n) := assume n, (he n).1, have he₂ : ∀n, γ - (1/2)^n < d (e n) := assume n, (he n).2, let f : ℕ → ℕ → set α := λn m, (finset.Ico n (m + 1)).inf e, have hf : ∀n m, is_measurable (f n m), { assume n m, simp only [f, finset.inf_eq_infi], exact is_measurable.bInter (countable_encodable _) (assume i _, he₁ _) }, have f_subset_f : ∀{a b c d}, a ≤ b → c ≤ d → f a d ⊆ f b c, { assume a b c d hab hcd, dsimp only [f], rw [finset.inf_eq_infi, finset.inf_eq_infi], refine bInter_subset_bInter_left _, simp, rintros j ⟨hbj, hjc⟩, exact ⟨le_trans hab hbj, lt_of_lt_of_le hjc $ add_le_add_right hcd 1⟩ }, have f_succ : ∀n m, n ≤ m → f n (m + 1) = f n m ∩ e (m + 1), { assume n m hnm, have : n ≤ m + 1 := le_of_lt (nat.succ_le_succ hnm), simp only [f], rw [finset.Ico.succ_top this, finset.inf_insert, set.inter_comm], refl }, have le_d_f : ∀n m, m ≤ n → γ - 2 * ((1 / 2) ^ m) + (1 / 2) ^ n ≤ d (f m n), { assume n m h, refine nat.le_induction _ _ n h, { have := he₂ m, simp only [f], rw [finset.Ico.succ_singleton, finset.inf_singleton], exact aux this }, { assume n (hmn : m ≤ n) ih, have : γ + (γ - 2 * (1 / 2)^m + (1 / 2) ^ (n + 1)) ≤ γ + d (f m (n + 1)), { calc γ + (γ - 2 * (1 / 2)^m + (1 / 2) ^ (n+1)) ≤ γ + (γ - 2 * (1 / 2)^m + ((1 / 2) ^ n - (1/2)^(n+1))) : begin refine add_le_add_left (add_le_add_left _ _) γ, simp only [pow_add, pow_one, le_sub_iff_add_le], linarith end ... = (γ - (1 / 2)^(n+1)) + (γ - 2 * (1 / 2)^m + (1 / 2)^n) : by simp only [sub_eq_add_neg]; ac_refl ... ≤ d (e (n + 1)) + d (f m n) : add_le_add (le_of_lt $ he₂ _) ih ... ≤ d (e (n + 1)) + d (f m n \ e (n + 1)) + d (f m (n + 1)) : by rw [f_succ _ _ hmn, d_split (f m n) (e (n + 1)) (hf _ _) (he₁ _), add_assoc] ... = d (e (n + 1) ∪ f m n) + d (f m (n + 1)) : begin rw [d_split (e (n + 1) ∪ f m n) (e (n + 1)), union_diff_left, union_inter_cancel_left], ac_refl, exact (he₁ _).union (hf _ _), exact (he₁ _) end ... ≤ γ + d (f m (n + 1)) : add_le_add_right (d_le_γ _ $ (he₁ _).union (hf _ _)) _ }, exact (add_le_add_iff_left γ).1 this } }, let s := ⋃ m, ⋂n, f m n, have γ_le_d_s : γ ≤ d s, { have hγ : tendsto (λm:ℕ, γ - 2 * (1/2)^m) at_top (nhds γ), { suffices : tendsto (λm:ℕ, γ - 2 * (1/2)^m) at_top (nhds (γ - 2 * 0)), { simpa }, exact (tendsto_sub tendsto_const_nhds $ tendsto_mul tendsto_const_nhds $ tendsto_pow_at_top_nhds_0_of_lt_1 (le_of_lt $ half_pos $ zero_lt_one) (half_lt_self zero_lt_one)) }, have hd : tendsto (λm, d (⋂n, f m n)) at_top (nhds (d (⋃ m, ⋂ n, f m n))), { refine d_Union _ _ _, { assume n, exact is_measurable.Inter (assume m, hf _ _) }, { exact assume n m hnm, subset_Inter (assume i, subset.trans (Inter_subset (f n) i) $ f_subset_f hnm $ le_refl _) } }, refine le_of_tendsto_of_tendsto (@at_top_ne_bot ℕ _ _) hγ hd (univ_mem_sets' $ assume m, _), change γ - 2 * (1 / 2) ^ m ≤ d (⋂ (n : ℕ), f m n), have : tendsto (λn, d (f m n)) at_top (nhds (d (⋂ n, f m n))), { refine d_Inter _ _ _, { assume n, exact hf _ _ }, { assume n m hnm, exact f_subset_f (le_refl _) hnm } }, refine ge_of_tendsto (@at_top_ne_bot ℕ _ _) this (mem_at_top_sets.2 ⟨m, assume n hmn, _⟩), change γ - 2 * (1 / 2) ^ m ≤ d (f m n), refine le_trans _ (le_d_f _ _ hmn), exact le_add_of_le_of_nonneg (le_refl _) (pow_nonneg (le_of_lt $ half_pos $ zero_lt_one) _) }, have hs : is_measurable s := is_measurable.Union (assume n, is_measurable.Inter (assume m, hf _ _)), refine ⟨s, hs, _, _⟩, { assume t ht hts, have : 0 ≤ d t := ((add_le_add_iff_left γ).1 $ calc γ + 0 ≤ d s : by rw [add_zero]; exact γ_le_d_s ... = d (s \ t) + d t : by rw [d_split _ _ hs ht, inter_eq_self_of_subset_right hts] ... ≤ γ + d t : add_le_add (d_le_γ _ (hs.diff ht)) (le_refl _)), rw [← to_nnreal_μ, ← to_nnreal_ν, ennreal.coe_le_coe, nnreal.coe_le], simpa only [d, le_sub_iff_add_le, zero_add] using this }, { assume t ht hts, have : d t ≤ 0, exact ((add_le_add_iff_left γ).1 $ calc γ + d t ≤ d s + d t : add_le_add γ_le_d_s (le_refl _) ... = d (s ∪ t) : begin rw [d_split _ _ (hs.union ht) ht, union_diff_right, union_inter_cancel_right, diff_eq_self.2], exact assume a ⟨hat, has⟩, hts hat has end ... ≤ γ + 0 : by rw [add_zero]; exact d_le_γ _ (hs.union ht)), rw [← to_nnreal_μ, ← to_nnreal_ν, ennreal.coe_le_coe, nnreal.coe_le], simpa only [d, sub_le_iff_le_add, zero_add] using this } end end measure_theory
8659658fe8463d7cf47262166750c28f81141d55
5756a081670ba9c1d1d3fca7bd47cb4e31beae66
/Mathport/Syntax/Data4.lean
abb776609be02082ee5f645d1f564e11e745ed12
[ "Apache-2.0" ]
permissive
leanprover-community/mathport
2c9bdc8292168febf59799efdc5451dbf0450d4a
13051f68064f7638970d39a8fecaede68ffbf9e1
refs/heads/master
1,693,841,364,079
1,693,813,111,000
1,693,813,111,000
379,357,010
27
10
Apache-2.0
1,691,309,132,000
1,624,384,521,000
Lean
UTF-8
Lean
false
false
337
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Daniel Selsam -/ import Mathport.Util.Misc namespace Mathport open Lean structure Data4 where fmt : Format exprs : HashMap Position Expr deriving Inhabited end Mathport
03e26c58b3a5a6a9f6d70f005eceab423eccc4a9
36938939954e91f23dec66a02728db08a7acfcf9
/lean/elf.lean
5fefa802411f5b08bfaeff413db9675024687376
[ "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
16,582
lean
/- This file contains the start of an Elf parser for Lean. It currently has facilities for parsing Elf -/ import system.io import init.category.reader import init.category.state import x86_semantics.machine_memory import x86_semantics.buffer_map import .file_input def repeat {α : Type} {m : Type → Type} [applicative m] : ℕ → m α → m (list α) | 0 m := pure [] | (nat.succ n) m := list.cons <$> m <*> repeat n m def forM' {m : Type → Type} [monad m] {α : Type} {β:Type} (l:list α) (f:α → m β) : m unit := list.mmap' f l def bracket {α β:Type} (c:io α) (d:α → io unit) (f:α → io β) := do x ← c, io.finally (f x) (d x) @[reducible] def uint8 := fin (nat.succ 0xff) @[reducible] def uint16 := fin (nat.succ 0xffff) @[reducible] def uint32 := fin (nat.succ 0xffffffff) @[reducible] def uint64 := fin (nat.succ 0xffffffffffffffff) namespace buffer @[reducible] def reader := except_t string (state char_buffer) namespace reader protected def read_chars (n:ℕ) : reader (list char) := do s ← get, when (s.size < n) (throw "read_chars eof"), put $ s.drop n, return ((s.take n).to_list) def skip (n:ℕ) : reader unit := do s ← get, when (s.size < n) (throw "skip eof"), put $ s.drop n def read_uint8 : reader uint8 := do l ← reader.read_chars 1, match l with | [h] := do return $ fin.of_nat h.val | _ := throw "internal: read_uint8" end def from_handle {α} (h:io.handle) (n:ℕ) (r:reader α) : io α := do b ← io.fs.read h n, match r.run.run b with | ⟨except.ok r,b'⟩ := do pure r | ⟨except.error e, b'⟩ := do io.fail e end end reader end buffer namespace elf def magic : list char := ['\x7f', 'E', 'L', 'F' ] inductive elf_class | ELF32 : elf_class | ELF64 : elf_class namespace elf_class protected def repr : elf_class → string | ELF32 := "ELF32" | ELF64 := "ELF64" protected def bytes : elf_class → ℕ | ELF32 := 4 | ELF64 := 8 protected def bits : elf_class → ℕ | ELF32 := 32 | ELF64 := 64 end elf_class inductive elf_data | ELFDATA2LSB : elf_data | ELFDATA2MSB : elf_data namespace elf_data protected def repr : elf_data → string | ELFDATA2LSB := "ELFDATA2LSB" | ELFDATA2MSB := "ELFDATA2MSB" end elf_data open elf_class open elf_data structure osabi := (val : uint8) namespace osabi protected def repr (o:osabi) : string := match o.val.val with | 0 := "UNIX - System V" | v := v.repr end end osabi -- Information from first 16-bytes of Elf file (allows reading rest of file). structure info := (elf_class : elf_class) (elf_data : elf_data) (osabi : osabi) (abi_version : uint8) namespace info protected def pp (e:info) : string := "Class: " ++ e.elf_class.repr ++ "\n" ++ "Data: " ++ e.elf_data.repr ++ "\n" ++ "OS/ABI: " ++ e.osabi.repr ++ "\n" ++ "ABI Version: " ++ e.abi_version.val.repr ++ "\n" open buffer def read : reader info := do b ← reader.read_chars 4, when (b ≠ magic) (throw "Not an elf file."), cl_val ← reader.read_uint8, cl ← match cl_val.val with | 1 := pure ELF32 | 2 := pure ELF64 | _ := throw "Invalid elf data." end, dt_val ← reader.read_uint8, dt ← match dt_val.val with | 1 := pure ELFDATA2LSB | 2 := pure ELFDATA2MSB | _ := throw "Invalid elf data." end, elf_version ← reader.read_uint8, when (elf_version ≠ 1) (throw "Mismatched elf version."), osabi ← reader.read_uint8, osabi_ver ← reader.read_uint8, pure { elf_class := cl , elf_data := dt , osabi := ⟨osabi⟩ , abi_version := osabi_ver } end info -- A reader for elf files @[reducible] def file_reader := reader_t info buffer.reader namespace file_reader protected def from_handle {α:Type} (i:info) (h:io.handle) (n:ℕ) (r:file_reader α) : io α := do b ← io.fs.read h n, match (r.run i).run.run b with | ⟨except.ok r, b'⟩ := do pure r | ⟨except.error e, b'⟩ := do io.fail e end def read_u8 : file_reader uint8 := reader_t.lift $ buffer.reader.read_uint8 protected def read_chars_lsb (w:ℕ) : file_reader (list char) := do i ← read, let f (l:list char) : list char := match i.elf_data with | ELFDATA2LSB := l | ELFDATA2MSB := l.reverse end in do reader_t.lift $ f <$> buffer.reader.read_chars w def read_u16 : file_reader uint16 := do l ← file_reader.read_chars_lsb 2, match l with | [x0,x1] := return $ fin.of_nat $ nat.shiftl x1.val 8 + x0.val | _ := throw "internal: read_uint16" end def read_u32 : file_reader uint32 := do l ← file_reader.read_chars_lsb 4, match l with | [x0,x1,x2,x3] := return $ fin.of_nat $ nat.shiftl x3.val 24 + nat.shiftl x2.val 16 + nat.shiftl x1.val 8 + x0.val | _ := throw "internal: read_uint32" end def read_u64 : file_reader uint64 := do l ← file_reader.read_chars_lsb 8, match l with | [x0,x1,x2,x3, x4, x5, x6, x7] := return $ fin.of_nat $ nat.shiftl x7.val 56 + nat.shiftl x6.val 48 + nat.shiftl x5.val 40 + nat.shiftl x4.val 32 + nat.shiftl x3.val 24 + nat.shiftl x2.val 16 + nat.shiftl x1.val 8 + x0.val | _ := throw "internal: read_uint64" end end file_reader class elf_file_data (α:Type) := (read {} : file_reader α) instance uint16_si_elf_file_data : elf_file_data uint16 := { read := file_reader.read_u16 } /- Elf defines a set of types depending on the class, e.g. Elf32_Word, Elf32_Addr, etc. Some of these are independent of the class, e.g. Elf32_Word = Elf64_Word = uint32. -/ -- A 32 or 64-bit word dependent on the class. def word (c:elf_class) := fin (nat.succ (2^c.bits-1)) namespace word protected def to_hex_with_leading_zeros : list char → ℕ → ℕ → string | prev 0 _ := prev.as_string | prev (nat.succ w) x := let c := (nat.land x 0xf).digit_char in to_hex_with_leading_zeros (c::prev) w (nat.shiftr x 4) protected def to_hex' : list char → ℕ → ℕ → string | prev 0 _ := prev.as_string | prev w 0 := prev.as_string | prev (nat.succ w) x := let c := (nat.land x 0xf).digit_char in to_hex' (c::prev) w (nat.shiftr x 4) -- Print word as decinal protected def pp_dec : Π{c:elf_class}, word c → string | ELF32 v := v.val.repr | ELF64 v := v.val.repr --- Print word as hex protected def pp_hex : Π{c:elf_class}, word c → string | ELF32 v := "0x" ++ word.to_hex' [] 8 v.val | ELF64 v := "0x" ++ word.to_hex' [] 16 v.val protected def size : elf_class → ℕ := elf_class.bytes theorem dbl_is_bit0 (x y:ℕ) : bit0 x*y = bit0 (x*y) := begin simp [bit0, nat.right_distrib], end --- `bit1_dec d x` is a utility function that denotes `(d+1)*x-1` -- It is primarily used def bit1_dec : ℕ → ℕ → ℕ | d x := (d+1)*x-1 theorem bit0_sub_1_congr (x:ℕ) : bit0 x - 1 = bit1_dec 1 x := begin simp [bit1_dec, bit1, bit0, nat.add_succ, nat.succ_mul], end theorem bit1_dec_bit0 (d x:ℕ) : bit1_dec d (bit0 x) = bit1_dec (bit1 d) x := begin simp [bit1_dec, bit1, bit0, nat.add_succ, nat.succ_mul, nat.left_distrib, nat.right_distrib], end theorem bit1_dec_one (d:ℕ) : bit1_dec d 1 = d := begin simp [bit1_dec], end theorem word32_is_uint32 : uint32 = word ELF32 := begin simp [uint32, word, elf_class.bits, has_pow.pow, nat.pow, dbl_is_bit0, bit0_sub_1_congr , bit1_dec_bit0, bit1_dec_one], end theorem word64_is_uint64 : uint64 = word ELF64 := begin simp [uint32, word, elf_class.bits, has_pow.pow, nat.pow, dbl_is_bit0, bit0_sub_1_congr , bit1_dec_bit0, bit1_dec_one], end instance (c:elf_class) : elf_file_data (word c) := { read := do match c with | ELF32 := eq.mp word32_is_uint32 <$> file_reader.read_u32 | ELF64 := eq.mp word64_is_uint64 <$> file_reader.read_u64 end } end word ------------------------------------------------------------------------ -- phdr structure phdr_type := (val : uint32) namespace phdr_type instance : elf_file_data phdr_type := { read := mk <$> file_reader.read_u32 } def repr (pht : phdr_type) : string := match pht.val.val with | 0 := "PT_NULL" | 1 := "PT_LOAD" | 2 := "PT_DYNAMIC" | 3 := "PT_INTERP" | 4 := "PT_NOTE" | 5 := "PT_SHLIB" | 6 := "PT_PHDR" | _ := "PT_PROC " ++ pht.val.val.repr end instance : has_repr phdr_type := ⟨repr⟩ instance : decidable_eq phdr_type := by tactic.mk_dec_eq_instance def PT_LOAD : phdr_type := mk 1 end phdr_type /-- Flags for a program header. -/ structure phdr_flags := (val : uint32) namespace phdr_flags instance : elf_file_data phdr_flags := { read := mk <$> file_reader.read_u32 } def repr (phfs : phdr_flags) : string := phfs.val.val.repr instance : has_repr phdr_flags := ⟨repr⟩ def has_X (f : phdr_flags) : bool := f.val.val.test_bit 0 def has_W (f : phdr_flags) : bool := f.val.val.test_bit 1 def has_R (f : phdr_flags) : bool := f.val.val.test_bit 2 end phdr_flags structure phdr (c : elf_class) := (phdr_type : phdr_type) (flags : phdr_flags) (offset : word c) (vaddr : word c) (paddr : word c) (filesz : word c) (memsz : word c) (align : word c) namespace phdr def size (c:elf_class) : ℕ := 8 + 6 * word.size c protected def pp {c:elf_class} (x:phdr c) := "Type: " ++ x.phdr_type.repr ++ "\n" ++ "Flags: " ++ x.flags.repr ++ "\n" ++ "File Offset: " ++ x.offset.pp_dec ++ "\n" ++ "Virtual Address: " ++ x.vaddr.pp_hex ++ "\n" ++ "Physical Address: " ++ x.paddr.pp_hex ++ "\n" ++ "File Size: " ++ x.filesz.pp_dec ++ "\n" ++ "Memory Size: " ++ x.memsz.pp_dec ++ "\n" ++ "Alignment: " ++ x.memsz.pp_hex ++ "\n" end phdr def read_phdr : Π(c:elf_class), file_reader (phdr c) | ELF32 := do tp ← elf_file_data.read, offset ← elf_file_data.read, vaddr ← elf_file_data.read, paddr ← elf_file_data.read, filesz ← elf_file_data.read, memsz ← elf_file_data.read, flags ← elf_file_data.read, align ← elf_file_data.read, pure { phdr_type := tp , flags := flags , offset := offset , vaddr := vaddr , paddr := paddr , filesz := filesz , memsz := memsz , align := align } | ELF64 := do tp ← elf_file_data.read, flags ← elf_file_data.read, offset ← elf_file_data.read, vaddr ← elf_file_data.read, paddr ← elf_file_data.read, filesz ← elf_file_data.read, memsz ← elf_file_data.read, align ← elf_file_data.read, pure { phdr_type := tp , flags := flags , offset := offset , vaddr := vaddr , paddr := paddr , filesz := filesz , memsz := memsz , align := align } ------------------------------------------------------------------------ -- ehdr /-- Elf file type -/ structure elf_type := (val : uint16) namespace elf_type instance : elf_file_data elf_type := { read := elf_type.mk <$> file_reader.read_u16 } end elf_type /-- Elf header machine type. -/ structure machine := (val : uint16) namespace machine instance : elf_file_data machine := { read := machine.mk <$> file_reader.read_u16 } end machine /-- Flags for a elf header. -/ structure ehdr_flags := (val : uint32) namespace ehdr_flags instance : elf_file_data ehdr_flags := { read := ehdr_flags.mk <$> file_reader.read_u32 } end ehdr_flags /-- The elf header information -/ structure ehdr := (info : info) (elf_type : elf_type) (machine : machine) (entry : word info.elf_class) (phoff : word info.elf_class) (shoff : word info.elf_class) (flags : ehdr_flags) (phnum : uint16) (shnum : uint16) (shstrndx : uint16) namespace ehdr -- Number of bytes in the ehdr after the 16 byte info. protected def size (c:elf_class) : ℕ := 16 + 24 + 3 * c.bytes protected def elf_class (e:ehdr) := e.info.elf_class protected def pp (e:ehdr) := e.info.pp ++ "Type: " ++ e.elf_type.val.val.repr ++ "\n" ++ "Machine: " ++ e.machine.val.val.repr ++ "\n" ++ "Entry point address: " ++ e.entry.pp_hex ++ "\n" ++ "Start of program headers: " ++ e.phoff.val.repr ++ " (bytes into file)\n" ++ "Start of section headers: " ++ e.shoff.val.repr ++ " (bytes into file)\n" ++ "Flags: " ++ e.flags.val.val.repr ++ "\n" ++ "Number of program headers: " ++ e.phnum.val.repr ++ "\n" ++ "Number of section headers: " ++ e.shnum.val.repr ++ "\n" ++ "Section header string table index: " ++ e.shstrndx.val.repr ++ "\n" end ehdr -- Read the remainder of the elf header after the first 16 bytes for the info. def read_ehdr_remainder : file_reader ehdr := do i ← read, tp ← elf_file_data.read, mach ← elf_file_data.read, ver ← file_reader.read_u32, when (ver ≠ 1) (throw $ "Unexpected version: " ++ ver.val.repr), entry ← elf_file_data.read, phoff ← elf_file_data.read, shoff ← elf_file_data.read, flags ← elf_file_data.read, _ehsize ← file_reader.read_u16, _phentsize ← file_reader.read_u16, phnum ← elf_file_data.read, _shentsize ← file_reader.read_u16, shnum ← elf_file_data.read, shstrndx ← elf_file_data.read, pure { info := i , elf_type := tp , machine := mach , entry := entry , phoff := phoff , shoff := shoff , flags := flags , phnum := phnum , shnum := shnum , shstrndx := shstrndx } -- The the elf header from the handle (which should be at the -- beginning of the file. def read_ehdr_from_handle (h:io.handle) : io ehdr := do i ← buffer.reader.from_handle h 16 info.read, let ehdr_size := ehdr.size i.elf_class in do file_reader.from_handle i h (ehdr_size - 16) read_ehdr_remainder -- Return size of phdr table def phdr_table_size (e:ehdr) : ℕ := phdr.size e.elf_class * e.phnum.val def read_phdrs_from_handle (e:ehdr) (h:io.handle) : io (list (phdr e.elf_class)) := file_reader.from_handle (e.info) h (phdr_table_size e) ( repeat (e.phnum.val) $ read_phdr e.elf_class) -- Move from current offset to target offset. def move_to_target (h:io.handle) (cur_off:ℕ) (target_off:ℕ) : io unit := if target_off < cur_off then do io.fail "Unexpected phdr offset" else do _ ← io.fs.read h (target_off - cur_off), pure () def pp_phdrs {c:elf_class} (phdrs:list (phdr c)) : io unit := do forM' (list.zip (list.range phdrs.length) phdrs) (λphdr, do let idx := phdr.fst in do io.put_str_ln $ "Index: " ++ idx.repr, io.put_str_ln phdr.snd.pp) -- A region is the contents of memory as it appears at load time. @[reducible] def region := char_buffer -- An elfmem represents the load-time address space represented by -- the elf file. It is indexed by virtual address @[reducible] def elfmem : Type := init_memory -- Helper for read_elfmem: add the region corresponding to the phdr in ph def read_one_elfmem {c : elf_class} (m : elfmem) (ph : phdr c) : file_input elfmem := if ph.phdr_type = phdr_type.PT_LOAD then do file_input.seek ph.offset.val, fbs <- file_input.read (min ph.filesz.val ph.memsz.val), monad_lift (io.put_str_ln ("read_one_elfmem: read " ++ fbs.size.repr ++ " bytes at " ++ repr ph.offset.val)), let fbs' := memory.char_buffer_to_region fbs in let zeroes : memory.region := array.to_buffer (mk_array (ph.memsz.val - ph.filesz.val) 0) in pure $ m.insert ph.vaddr.val (buffer.append fbs' zeroes) else pure m def read_elfmem {c : elf_class} (path : string) (phdrs : list (phdr c)) : io elfmem := do r <- file_input.run_file_input path $ list.mfoldl read_one_elfmem buffer_map.empty phdrs, match r with | except.error s := io.fail s | except.ok r := return r end /--- Read the elf file from the given path, and print out ehdr and program headers. -/ def read_info_from_file (path : string) : io ((Σ(e : ehdr), list (phdr e.elf_class)) × elfmem) := do bracket (io.mk_file_handle path io.mode.read tt) io.fs.close $ λh, do e ← read_ehdr_from_handle h, let i := e.info in do let ehdr_size := ehdr.size i.elf_class in do move_to_target h ehdr_size e.phoff.val, phdrs ← read_phdrs_from_handle e h, mem <- read_elfmem path phdrs, return ((sigma.mk e phdrs), mem) -- pp_phdrs phdrs end elf
020d6b0bab425a6a18fad06986c4986346f66490
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/omega/int/form.lean
b75f3a22fb737c96be1ebf105031dd58184297d1
[]
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,454
lean
/- Copyright (c) 2019 Seul Baek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Seul Baek -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.omega.int.preterm import Mathlib.PostPort universes l namespace Mathlib /- Linear integer arithmetic formulas in pre-normalized form. -/ namespace omega namespace int /-- Intermediate shadow syntax for LNA formulas that includes unreified exprs -/ /-- Intermediate shadow syntax for LIA formulas that includes non-canonical terms -/ inductive preform where | eq : preterm → preterm → preform | le : preterm → preterm → preform | not : preform → preform | or : preform → preform → preform | and : preform → preform → preform namespace preform /-- Evaluate a preform into prop using the valuation v. -/ @[simp] def holds (v : ℕ → ℤ) : preform → Prop := sorry end preform /-- univ_close p n := p closed by prepending n universal quantifiers -/ @[simp] def univ_close (p : preform) : (ℕ → ℤ) → ℕ → Prop := sorry namespace preform /-- Fresh de Brujin index not used by any variable in argument -/ def fresh_index : preform → ℕ := sorry /-- All valuations satisfy argument -/ def valid (p : preform) := ∀ (v : ℕ → ℤ), holds v p /-- There exists some valuation that satisfies argument -/ def sat (p : preform) := ∃ (v : ℕ → ℤ), holds v p /-- implies p q := under any valuation, q holds if p holds -/ def implies (p : preform) (q : preform) := ∀ (v : ℕ → ℤ), holds v p → holds v q /-- equiv p q := under any valuation, p holds iff q holds -/ def equiv (p : preform) (q : preform) := ∀ (v : ℕ → ℤ), holds v p ↔ holds v q theorem sat_of_implies_of_sat {p : preform} {q : preform} : implies p q → sat p → sat q := fun (h1 : implies p q) (h2 : sat p) => exists_imp_exists h1 h2 theorem sat_or {p : preform} {q : preform} : sat (or p q) ↔ sat p ∨ sat q := sorry /-- There does not exist any valuation that satisfies argument -/ def unsat (p : preform) := ¬sat p def repr : preform → string := sorry protected instance has_repr : has_repr preform := has_repr.mk repr end preform theorem univ_close_of_valid {p : preform} {m : ℕ} {v : ℕ → ℤ} : preform.valid p → univ_close p v m := sorry theorem valid_of_unsat_not {p : preform} : preform.unsat (preform.not p) → preform.valid p := sorry
36a8f951eb7f28249faffaecf18c730c967c4373
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/run/psum_wf_rec.lean
05bebd8f21f931774ea22af794f123dfc33898e5
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
715
lean
def psum.alt.sizeof {α β} [has_sizeof α] [has_sizeof β] : psum α β → nat | (psum.inl a) := 2*sizeof a + 2 | (psum.inr b) := 2*sizeof b + 1 def sum_has_sizeof_2 {α β} [has_sizeof α] [has_sizeof β] : has_sizeof (psum α β) := ⟨psum.alt.sizeof⟩ local attribute [instance] sum_has_sizeof_2 mutual def f, g with f : ℕ → ℕ | n := g n + 1 with g : ℕ → ℕ | 0 := 0 | (n+1) := /- The following is a hint for the equation compiler. We will be able to delete it as soon as we have decision procedures for arithmetic -/ have 2 + n * 2 < 1 + 2 * (n + 1), from begin rw [left_distrib], simp, well_founded_tactics.cancel_nat_add_lt, tactic.comp_val end, f n
4b7927621c8fb959966744c9857e31d6be2829f9
35b83be3126daae10419b573c55e1fed009d3ae8
/_target/deps/mathlib/analysis/real.lean
32c9273d787f66630d7020fa1280adf1dddd854c
[]
no_license
AHassan1024/Lean_Playground
ccb25b72029d199c0d23d002db2d32a9f2689ebc
a00b004c3a2eb9e3e863c361aa2b115260472414
refs/heads/master
1,586,221,905,125
1,544,951,310,000
1,544,951,310,000
157,934,290
0
0
null
null
null
null
UTF-8
Lean
false
false
17,414
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 The real numbers ℝ. They are constructed as the topological completion of ℚ. With the following steps: (1) prove that ℚ forms a uniform space. (2) subtraction and addition are uniform continuous functions in this space (3) for multiplication and inverse this only holds on bounded subsets (4) ℝ is defined as separated Cauchy filters over ℚ (the separation requires a quotient construction) (5) extend the uniform continuous functions along the completion (6) proof field properties using the principle of extension of identities TODO generalizations: * topological groups & rings * order topologies * Archimedean fields -/ import logic.function analysis.metric_space tactic.linarith noncomputable theory open classical set lattice filter topological_space local attribute [instance] prop_decidable 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 instance : metric_space ℤ := begin letI M := metric_space.induced coe int.cast_injective real.metric_space, refine @metric_space.replace_uniformity _ int.uniform_space M (le_antisymm refl_le_uniformity $ λ r ru, mem_uniformity_dist.2 ⟨1, zero_lt_one, λ a b h, mem_principal_sets.1 ru $ dist_le_zero.1 (_ : (abs (a - b) : ℝ) ≤ 0)⟩), simpa using (@int.cast_le ℝ _ _ 0).2 (int.lt_add_one_iff.1 $ (@int.cast_lt ℝ _ (abs (a - b)) 1).1 $ by simpa using h) end theorem uniform_continuous_of_rat : uniform_continuous (coe : ℚ → ℝ) := uniform_continuous_comap theorem uniform_embedding_of_rat : uniform_embedding (coe : ℚ → ℝ) := metric_space.induced_uniform_embedding _ _ _ 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ε⟩ := mem_nhds_iff_metric.1 ht in let ⟨q, h⟩ := exists_rat_near x ε0 in ne_empty_iff_exists_mem.2 ⟨_, hε (mem_ball'.2 h), q, rfl⟩ theorem embedding_of_rat : embedding (coe : ℚ → ℝ) := dense_embedding_of_rat.embedding theorem continuous_of_rat : continuous (coe : ℚ → ℝ) := uniform_continuous_of_rat.continuous theorem real.uniform_continuous_add : uniform_continuous (λp : ℝ × ℝ, p.1 + p.2) := uniform_continuous_of_metric.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.uniform_continuous_iff.2 $ by simp [(∘)]; exact ((uniform_continuous_fst.comp uniform_continuous_of_rat).prod_mk (uniform_continuous_snd.comp uniform_continuous_of_rat)).comp real.uniform_continuous_add theorem real.uniform_continuous_neg : uniform_continuous (@has_neg.neg ℝ _) := uniform_continuous_of_metric.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 ℚ _) := uniform_continuous_of_metric.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 instance : topological_add_group ℝ := by apply_instance instance : topological_add_group ℚ := by apply_instance instance : orderable_topology ℚ := induced_orderable_topology _ (λ x y, rat.cast_lt) (@exists_rat_btwn _ _ _) 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_unbounded (no_top _) (no_bot _)).mp (mem_nhds_sets hv hav), ⟨q, hlq, hqa⟩ := exists_rat_btwn hl, ⟨p, hap, hpu⟩ := exists_rat_btwn hu in ⟨Ioo q p, by simp; exact ⟨q, p, rat.cast_lt.1 $ lt_trans hqa hap, rfl⟩, ⟨hqa, hap⟩, assume a' ⟨hqa', ha'p⟩, h _ (lt_trans hlq hqa') (lt_trans ha'p hpu)⟩) instance : second_countable_topology ℝ := ⟨⟨(⋃(a b : ℚ) (h : a < b), {Ioo a b}), by simp [countable_Union, countable_Union_Prop], real.is_topological_basis_Ioo_rat.2.2⟩⟩ /- 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.uniform_continuous_inv (s : set ℝ) {r : ℝ} (r0 : 0 < r) (H : ∀ x ∈ s, r ≤ abs x) : uniform_continuous (λp:s, p.1⁻¹) := uniform_continuous_of_metric.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 : ℝ → ℝ) := uniform_continuous_of_metric.2 $ λ ε ε0, ⟨ε, ε0, λ a b, lt_of_le_of_lt (abs_abs_sub_abs_le_abs_sub _ _)⟩ lemma real.continuous_abs : continuous (abs : ℝ → ℝ) := real.uniform_continuous_abs.continuous lemma rat.uniform_continuous_abs : uniform_continuous (abs : ℚ → ℚ) := uniform_continuous_of_metric.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 rat.continuous_abs : continuous (abs : ℚ → ℚ) := rat.uniform_continuous_abs.continuous lemma real.tendsto_inv {r : ℝ} (r0 : r ≠ 0) : tendsto (λq, q⁻¹) (nhds r) (nhds r⁻¹) := by rw ← abs_pos_iff 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)) (mem_nhds_sets (real.continuous_abs _ $ is_open_lt' (abs r / 2)) (half_lt_self r0)) lemma real.continuous_inv' : continuous (λa:{r:ℝ // r ≠ 0}, a.val⁻¹) := continuous_iff_tendsto.mpr $ assume ⟨r, hr⟩, (continuous_iff_tendsto.mp continuous_subtype_val _).comp (real.tendsto_inv hr) 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 (continuous_subtype_mk _ hf).comp real.continuous_inv' lemma real.uniform_continuous_mul_const {x : ℝ} : uniform_continuous ((*) x) := uniform_continuous_of_metric.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₂ : ℝ} (r₁0 : 0 < r₁) (r₂0 : 0 < r₂) (H : ∀ x ∈ s, abs (x : ℝ × ℝ).1 < r₁ ∧ abs x.2 < r₂) : uniform_continuous (λp:s, p.1.1 * p.1.2) := uniform_continuous_of_metric.2 $ λ ε ε0, let ⟨δ, δ0, Hδ⟩ := rat_mul_continuous_lemma abs ε0 r₁0 r₂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_tendsto.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}) (lt_of_le_of_lt (abs_nonneg _) (lt_add_one _)) (lt_of_le_of_lt (abs_nonneg _) (lt_add_one _)) (λ x, id)) (mem_nhds_sets (is_open_prod (real.continuous_abs _ $ is_open_gt' _) (real.continuous_abs _ $ is_open_gt' _)) ⟨lt_add_one (abs a₁), lt_add_one (abs a₂)⟩) instance : topological_ring ℝ := { continuous_mul := real.continuous_mul, ..real.topological_add_group } instance : topological_semiring ℝ := by apply_instance lemma rat.continuous_mul : continuous (λp : ℚ × ℚ, p.1 * p.2) := embedding_of_rat.continuous_iff.2 $ by simp [(∘)]; exact ((continuous_fst.comp continuous_of_rat).prod_mk (continuous_snd.comp continuous_of_rat)).comp real.continuous_mul 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] lemma real.totally_bounded_Ioo (a b : ℝ) : totally_bounded (Ioo a b) := totally_bounded_of_metric.2 $ λ ε ε0, begin rcases exists_nat_gt ((b - a) / ε) with ⟨n, ba⟩, rw [div_lt_iff' ε0, sub_lt_iff_lt_add'] at ba, let s := (λ i:ℕ, a + ε * i) '' {i:ℕ | i < n}, refine ⟨s, finite_image _ ⟨set.fintype_lt_nat _⟩, λ x h, _⟩, rcases h with ⟨ax, xb⟩, let i : ℕ := ⌊(x - a) / ε⌋.to_nat, have : (i : ℤ) = ⌊(x - a) / ε⌋ := int.to_nat_of_nonneg (floor_nonneg.2 $ le_of_lt (div_pos (sub_pos.2 ax) ε0)), simp, refine ⟨_, ⟨i, _, rfl⟩, _⟩, { rw [← int.coe_nat_lt, this], refine int.cast_lt.1 (lt_of_le_of_lt (floor_le _) _), rw [int.cast_coe_nat, div_lt_iff' ε0, sub_lt_iff_lt_add'], exact lt_trans xb ba }, { rw [real.dist_eq, ← int.cast_coe_nat, this, abs_of_nonneg, ← sub_sub, sub_lt_iff_lt_add'], { have := lt_floor_add_one ((x - a) / ε), rwa [div_lt_iff' ε0, mul_add, mul_one] at this }, { have := floor_le ((x - a) / ε), rwa [ge, sub_nonneg, ← le_sub_iff_add_le', ← le_div_iff' ε0] } } end lemma real.totally_bounded_ball (x ε : ℝ) : totally_bounded (ball x ε) := by rw real.ball_eq_Ioo; apply real.totally_bounded_Ioo lemma real.totally_bounded_Ico (a b : ℝ) : totally_bounded (Ico a b) := let ⟨c, ac⟩ := no_bot a in totally_bounded_subset (by exact λ x ⟨h₁, h₂⟩, ⟨lt_of_lt_of_le ac h₁, h₂⟩) (real.totally_bounded_Ioo c b) lemma real.totally_bounded_Icc (a b : ℝ) : totally_bounded (Icc a b) := let ⟨c, bc⟩ := no_top b in totally_bounded_subset (by exact λ x ⟨h₁, h₂⟩, ⟨h₁, lt_of_le_of_lt h₂ bc⟩) (real.totally_bounded_Ico a c) lemma rat.totally_bounded_Icc (a b : ℚ) : totally_bounded (Icc a b) := begin have := totally_bounded_preimage uniform_embedding_of_rat (real.totally_bounded_Icc a b), rwa (set.ext (λ q, _) : Icc _ _ = _), simp end -- TODO(Mario): Generalize to first-countable uniform spaces? instance : complete_space ℝ := ⟨λ f cf, begin let g : ℕ → {ε:ℝ//ε>0} := λ n, ⟨n.to_pnat'⁻¹, inv_pos (nat.cast_pos.2 n.to_pnat'.pos)⟩, choose S hS hS_dist using show ∀n:ℕ, ∃t ∈ f.sets, ∀ x y ∈ t, dist x y < g n, from assume n, let ⟨t, tf, h⟩ := (cauchy_of_metric.1 cf).2 (g n).1 (g n).2 in ⟨t, tf, h⟩, let F : ℕ → set ℝ := λn, ⋂i≤n, S i, have hF : ∀n, F n ∈ f.sets := assume n, Inter_mem_sets (finite_le_nat n) (λ i _, hS i), have hF_dist : ∀n, ∀ x y ∈ F n, dist x y < g n := assume n x y hx hy, have F n ⊆ S n := bInter_subset_of_mem (le_refl n), (hS_dist n) _ _ (this hx) (this hy), choose G hG using assume n:ℕ, inhabited_of_mem_sets cf.1 (hF n), have hg : ∀ ε > 0, ∃ n, ∀ j ≥ n, (g j : ℝ) < ε, { intros ε ε0, cases exists_nat_gt ε⁻¹ with n hn, refine ⟨n, λ j nj, _⟩, have hj := lt_of_lt_of_le hn (nat.cast_le.2 nj), have j0 := lt_trans (inv_pos ε0) hj, have jε := (inv_lt j0 ε0).2 hj, rwa ← pnat.to_pnat'_coe (nat.cast_pos.1 j0) at jε }, let c : cau_seq ℝ abs, { refine ⟨λ n, G n, λ ε ε0, _⟩, cases hg _ ε0 with n hn, refine ⟨n, λ j jn, _⟩, have : F j ⊆ F n := bInter_subset_bInter_left (λ i h, @le_trans _ _ i n j h jn), exact lt_trans (hF_dist n _ _ (this (hG j)) (hG n)) (hn _ $ le_refl _) }, refine ⟨real.lim c, λ s h, _⟩, rcases mem_nhds_iff_metric.1 h with ⟨ε, ε0, hε⟩, cases exists_forall_ge_and (hg _ $ half_pos ε0) (real.equiv_lim c _ $ half_pos ε0) with n hn, cases hn _ (le_refl _) with h₁ h₂, refine sets_of_superset _ (hF n) (subset.trans _ $ subset.trans (ball_half_subset (G n) h₂) hε), exact λ x h, lt_trans ((hF_dist n) x (G n) h (hG n)) h₁ end⟩ lemma tendsto_of_nat_at_top_at_top : tendsto (coe : ℕ → ℝ) at_top at_top := tendsto_infi.2 $ assume r, tendsto_principal.2 $ let ⟨n, hn⟩ := exists_nat_gt r in mem_at_top_sets.2 ⟨n, λ m h, le_trans (le_of_lt hn) (nat.cast_le.2 h)⟩ section lemma closure_of_rat_image_lt {q : ℚ} : closure ((coe:ℚ → ℝ) '' {x | q < x}) = {r | ↑q ≤ r} := subset.antisymm ((closure_subset_iff_subset_of_is_closed (is_closed_ge' _)).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ε⟩ := mem_nhds_iff_metric.1 ht in let ⟨p, h₁, h₂⟩ := exists_rat_btwn ((lt_add_iff_pos_right x).2 ε0) in ne_empty_iff_exists_mem.2 ⟨_, 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 compact_Icc {a b : ℝ} : compact (Icc a b) := compact_of_totally_bounded_is_closed (real.totally_bounded_Icc a b) (is_closed_inter (is_closed_ge' a) (is_closed_le' b)) open real lemma real.intermediate_value {f : ℝ → ℝ} {a b t : ℝ} (hf : ∀ x, a ≤ x → x ≤ b → tendsto f (nhds x) (nhds (f x))) (ha : f a ≤ t) (hb : t ≤ f b) (hab : a ≤ b) : ∃ x : ℝ, a ≤ x ∧ x ≤ b ∧ f x = t := let x := real.Sup {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} in have hx₁ : ∃ y, ∀ g ∈ {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b}, g ≤ y := ⟨b, λ _ h, h.2.2⟩, have hx₂ : ∃ y, y ∈ {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} := ⟨a, ha, le_refl _, hab⟩, have hax : a ≤ x, from le_Sup _ hx₁ ⟨ha, le_refl _, hab⟩, have hxb : x ≤ b, from (Sup_le _ hx₂ hx₁).2 (λ _ h, h.2.2), ⟨x, hax, hxb, eq_of_forall_dist_le $ λ ε ε0, let ⟨δ, hδ0, hδ⟩ := tendsto_nhds_of_metric.1 (hf _ hax hxb) ε ε0 in (le_total t (f x)).elim (λ h, le_of_not_gt $ λ hfε, begin rw [dist_eq, abs_of_nonneg (sub_nonneg.2 h)] at hfε, refine mt (Sup_le {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} hx₂ hx₁).2 (not_le_of_gt (sub_lt_self x (half_pos hδ0))) (λ g hg, le_of_not_gt (λ hgδ, not_lt_of_ge hg.1 (lt_trans (lt_sub.1 hfε) (sub_lt_of_sub_lt (lt_of_le_of_lt (le_abs_self _) _))))), rw abs_sub, exact hδ (abs_sub_lt_iff.2 ⟨lt_of_le_of_lt (sub_nonpos.2 (le_Sup _ hx₁ hg)) hδ0, by simp only [x] at *; linarith⟩) end) (λ h, le_of_not_gt $ λ hfε, begin rw [dist_eq, abs_of_nonpos (sub_nonpos.2 h)] at hfε, exact mt (le_Sup {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b}) (λ h : ∀ k, k ∈ {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} → k ≤ x, not_le_of_gt ((lt_add_iff_pos_left x).2 (half_pos hδ0)) (h _ ⟨le_trans (le_sub_iff_add_le.2 (le_trans (le_abs_self _) (le_of_lt (hδ $ by rw [dist_eq, add_sub_cancel, abs_of_nonneg (le_of_lt (half_pos hδ0))]; exact half_lt_self hδ0)))) (by linarith), le_trans hax (le_of_lt ((lt_add_iff_pos_left _).2 (half_pos hδ0))), le_of_not_gt (λ hδy, not_lt_of_ge hb (lt_of_le_of_lt (show f b ≤ f b - f x - ε + t, by linarith) (add_lt_of_neg_of_le (sub_neg_of_lt (lt_of_le_of_lt (le_abs_self _) (@hδ b (abs_sub_lt_iff.2 ⟨by simp only [x] at *; linarith, by linarith⟩)))) (le_refl _))))⟩)) hx₁ end)⟩ lemma real.intermediate_value' {f : ℝ → ℝ} {a b t : ℝ} (hf : ∀ x, a ≤ x → x ≤ b → tendsto f (nhds x) (nhds (f x))) (ha : t ≤ f a) (hb : f b ≤ t) (hab : a ≤ b) : ∃ x : ℝ, a ≤ x ∧ x ≤ b ∧ f x = t := let ⟨x, hx₁, hx₂, hx₃⟩ := @real.intermediate_value (λ x, - f x) a b (-t) (λ x hax hxb, tendsto_neg (hf x hax hxb)) (neg_le_neg ha) (neg_le_neg hb) hab in ⟨x, hx₁, hx₂, neg_inj hx₃⟩ end
7ac1c205073d0d91e7a5c0e35342361be4bce386
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/finsupp/fin.lean
a8d468850acddd70ab9fd8335ebf32f4dafbc125
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
2,396
lean
/- Copyright (c) 2021 Ivan Sadofschi Costa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ivan Sadofschi Costa -/ import data.finsupp.defs /-! # `cons` and `tail` for maps `fin n →₀ M` We interpret maps `fin n →₀ M` as `n`-tuples of elements of `M`, We define the following operations: * `finsupp.tail` : the tail of a map `fin (n + 1) →₀ M`, i.e., its last `n` entries; * `finsupp.cons` : adding an element at the beginning of an `n`-tuple, to get an `n + 1`-tuple; In this context, we prove some usual properties of `tail` and `cons`, analogous to those of `data.fin.tuple.basic`. -/ noncomputable theory namespace finsupp variables {n : ℕ} (i : fin n) {M : Type*} [has_zero M] (y : M) (t : fin (n + 1) →₀ M) (s : fin n →₀ M) /-- `tail` for maps `fin (n + 1) →₀ M`. See `fin.tail` for more details. -/ def tail (s : fin (n + 1) →₀ M) : fin n →₀ M := finsupp.equiv_fun_on_finite.symm (fin.tail s) /-- `cons` for maps `fin n →₀ M`. See `fin.cons` for more details. -/ def cons (y : M) (s : fin n →₀ M) : fin (n + 1) →₀ M := finsupp.equiv_fun_on_finite.symm (fin.cons y s : fin (n + 1) → M) lemma tail_apply : tail t i = t i.succ := rfl @[simp] lemma cons_zero : cons y s 0 = y := rfl @[simp] lemma cons_succ : cons y s i.succ = s i := fin.cons_succ _ _ _ @[simp] lemma tail_cons : tail (cons y s) = s := ext $ λ k, by simp only [tail_apply, cons_succ] @[simp] lemma cons_tail : cons (t 0) (tail t) = t := begin ext, by_cases c_a : a = 0, { rw [c_a, cons_zero] }, { rw [←fin.succ_pred a c_a, cons_succ, ←tail_apply] }, end @[simp] lemma cons_zero_zero : cons 0 (0 : fin n →₀ M) = 0 := begin ext, by_cases c : a = 0, { simp [c] }, { rw [←fin.succ_pred a c, cons_succ], simp }, end variables {s} {y} lemma cons_ne_zero_of_left (h : y ≠ 0) : cons y s ≠ 0 := begin contrapose! h with c, rw [←cons_zero y s, c, finsupp.coe_zero, pi.zero_apply], end lemma cons_ne_zero_of_right (h : s ≠ 0) : cons y s ≠ 0 := begin contrapose! h with c, ext, simp [ ← cons_succ a y s, c], end lemma cons_ne_zero_iff : cons y s ≠ 0 ↔ y ≠ 0 ∨ s ≠ 0 := begin refine ⟨λ h, _, λ h, h.cases_on cons_ne_zero_of_left cons_ne_zero_of_right⟩, refine imp_iff_not_or.1 (λ h' c, h _), rw [h', c, finsupp.cons_zero_zero], end end finsupp
2bbd0c847e51cc77469531cac9a818696d238678
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/bad_open.lean
b846d30f5d0227fe8876d579f0b96a94e2a123e0
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
11
lean
open "nat"
bf08e3aca70ea126ff50894e968285bc4d0c841e
9ad8d18fbe5f120c22b5e035bc240f711d2cbd7e
/src/exercises/tactics_list.lean
4113804009cc3d67f80baed33cdaab553ebae644
[]
no_license
agusakov/lean_lib
c0e9cc29fc7d2518004e224376adeb5e69b5cc1a
f88d162da2f990b87c4d34f5f46bbca2bbc5948e
refs/heads/master
1,642,141,461,087
1,557,395,798,000
1,557,395,798,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,987
lean
import tactic.interactive import tactic.monotonicity import data.finset /- To do: also document derive_handlers -/ #check tactic.interactive.ac_mono -- Deduce inequalities using known monotonicity properties of functions #check tactic.interactive.abstract -- Tactical to record the argument used as a lemma, as well as solving the goal #check tactic.interactive.ac_reflexivity -- Proves an equation using commutativity and associativity #check tactic.interactive.admit -- Sorry #check tactic.interactive.all_goals -- Apply a tactic to all goals #check tactic.interactive.any_goals -- Apply a tactic to any goal where it works #check tactic.interactive.apply -- #check tactic.interactive.apply_assumption -- Like apply, but looks in the local context for applicable things #check tactic.interactive.apply_auto_param -- Not used directly in mathlib, but used indirectly by the `tidy` tactic. #check tactic.interactive.apply_field -- #check tactic.interactive.apply_instance -- Use type class resolution to close the main goal. For example, if we alread -- have group structures on G and H, and the goal is to give a group structure -- on G × (ℕ → H), then `apply_instance` will close the goal. #check tactic.interactive.apply_opt_param -- Not used directly in mathlib #check tactic.interactive.apply_rules -- This is like `apply`, except that you supply a list of rules instead of -- a single rule, and Lean applies any of the rules that are applicable. #check tactic.interactive.apply_with -- Like apply, but with configurable options #check tactic.interactive.assume -- Like intro, but type can be specified #check tactic.interactive.assumption -- #check tactic.interactive.assumption' -- Try to apply `assumption` to all goals #check tactic.interactive.async -- #check tactic.interactive.by_cases -- #check tactic.interactive.by_contra -- Same as by_contradiction #check tactic.interactive.by_contradiction -- #check tactic.interactive.case #check tactic.interactive.cases #check tactic.interactive.cases_matching #check tactic.interactive.cases_type #check tactic.interactive.casesm #check tactic.interactive.change #check tactic.interactive.choose #check tactic.interactive.clarify #check tactic.interactive.classical #check tactic.interactive.clean #check tactic.interactive.clean_ids #check tactic.interactive.clear #check tactic.interactive.coinduction #check tactic.interactive.comp_val #check tactic.interactive.congr #check tactic.interactive.congr' #check tactic.interactive.constructor #check tactic.interactive.constructor_matching #check tactic.interactive.continue #check tactic.interactive.contradiction #check tactic.interactive.conv #check tactic.interactive.convert #check tactic.interactive.decidable_eq #check tactic.interactive.delete_expr #check tactic.interactive.delta #check tactic.interactive.derive_functor #check tactic.interactive.derive_lawful_functor #check tactic.interactive.derive_lawful_traversable #check tactic.interactive.derive_map_equations #check tactic.interactive.derive_traverse #check tactic.interactive.derive_traverse_equations #check tactic.interactive.destruct #check tactic.interactive.done #check tactic.interactive.dsimp #check tactic.interactive.dunfold #check tactic.interactive.eapply #check tactic.interactive.erewrite #check tactic.interactive.exact #check tactic.interactive.exactI #check tactic.interactive.exacts #check tactic.interactive.exfalso #check tactic.interactive.existsi #check tactic.interactive.ext #check tactic.interactive.ext1 #check tactic.interactive.fail_if_success #check tactic.interactive.fapply #check tactic.interactive.field -- No docstring #check tactic.interactive.filter_instances -- No docstring #check tactic.interactive.find_lemma -- No docstring #check tactic.interactive.find_one_difference -- No docstring #check tactic.interactive.find_rule -- No docstring #check tactic.interactive.finish -- No docstring #check tactic.interactive.focus -- #check tactic.interactive.fold_assoc -- No docstring #check tactic.interactive.fold_assoc1 -- No docstring #check tactic.interactive.from -- Same as `exact` #check tactic.interactive.fsplit -- No docstring #check tactic.interactive.functor_derive_handler -- #check tactic.interactive.functor_derive_handler' -- #check tactic.interactive.funext -- #check tactic.interactive.generalize -- Make goal more general #check tactic.interactive.generalize_a_aux -- #check tactic.interactive.generalize_hyp -- Similar to generalize #check tactic.interactive.generalize_proofs -- Similar to generalize? #check tactic.interactive.get_current_field -- No docstring #check tactic.interactive.get_equations_of -- No docstring #check tactic.interactive.get_monotonicity_lemmas -- No docstring #check tactic.interactive.get_operator -- No docstring #check tactic.interactive.get_rule_eqn_lemmas -- No docstring #check tactic.interactive.guard_class -- No docstring #check tactic.interactive.guard_expr_eq -- No docstring #check tactic.interactive.guard_expr_eq' -- No docstring #check tactic.interactive.guard_hyp -- Used for writing tests #check tactic.interactive.guard_hyp' -- Used for writing tests #check tactic.interactive.guard_hyp_nums -- No docstring #check tactic.interactive.guard_tags -- No docstring #check tactic.interactive.guard_target -- Used for writing tests #check tactic.interactive.guard_target' -- Used for writing tests #check tactic.interactive.h_generalize -- For dealing with casts and heterogenous equality #check tactic.interactive.has_to_format -- No docstring #check tactic.interactive.has_to_tactic_format_mono_ctx -- No docstring #check tactic.interactive.has_to_tactic_format_mono_function -- No docstring #check tactic.interactive.has_to_tactic_format_mono_law -- No docstring #check tactic.interactive.have -- #check tactic.interactive.haveI -- #check tactic.interactive.have_field -- #check tactic.interactive.hide_meta_vars' -- No docstring #check tactic.interactive.iclarify -- No docstring #check tactic.interactive.induction -- #check tactic.interactive.injection -- Apply the fact that constructors are injective. #check tactic.interactive.injections -- Apply the fact that constructors are injective, repeatedly #check tactic.interactive.injections_and_clear -- No docstring #check tactic.interactive.intro -- #check tactic.interactive.introI -- #check tactic.interactive.intros -- #check tactic.interactive.introsI -- #check tactic.interactive.introv -- Like intros, but with slightly different behaviour wrt naming -- of introduced variables #check tactic.interactive.isafe -- No docstring #check tactic.interactive.iterate -- Apply a tactic repeatedly #check tactic.interactive.left -- #check tactic.interactive.let -- #check tactic.interactive.letI #check tactic.interactive.list.minimum_on -- No docstring #check tactic.interactive.list_cast_of -- No docstring #check tactic.interactive.map_constructor -- #check tactic.interactive.map_field -- #check tactic.interactive.mapply -- #check tactic.interactive.match_ac -- No docstring #check tactic.interactive.match_ac' -- No docstring #check tactic.interactive.match_ac'._main -- No docstring #check tactic.interactive.match_assoc -- #check tactic.interactive.match_chaining_rules -- No docstring #check tactic.interactive.match_imp -- No docstring #check tactic.interactive.match_prefix -- No docstring #check tactic.interactive.match_rule -- No docstring #check tactic.interactive.match_target -- Fail if type of target is not as specified #check tactic.interactive.min_tac -- No docstring #check tactic.interactive.mk_assumption_set -- No docstring #check tactic.interactive.mk_congr_args -- No docstring #check tactic.interactive.mk_congr_law -- No docstring #check tactic.interactive.mk_fun_app -- No docstring #check tactic.interactive.mk_map -- Helps to define functors. You can just specify the effect on -- objects and mk_map will try to work out the effect on maps (?) #check tactic.interactive.mk_mapp' -- No docstring #check tactic.interactive.mk_one_instance -- No docstring #check tactic.interactive.mk_pattern -- No docstring #check tactic.interactive.mk_rel -- No docstring #check tactic.interactive.mk_traverse -- Helps to define traversable functors #check tactic.interactive.mono -- Applies monotonicity rules #check tactic.interactive.nested_map -- #check tactic.interactive.nested_traverse -- #check tactic.interactive.one_line -- No docstring #check tactic.interactive.rcases -- #check tactic.interactive.refine -- Like exact, but allows holes #check tactic.interactive.refine_struct -- When defining a structure, this tactic gives a goal for each required field -- (Compare with hole commands?) #check tactic.interactive.refl -- #check tactic.interactive.reflexivity -- Same as refl #check tactic.interactive.rename -- #check tactic.interactive.repeat -- #check tactic.interactive.repeat_or_not -- #check tactic.interactive.repeat_until -- #check tactic.interactive.repeat_until_or_at_most -- #check tactic.interactive.replace -- #check tactic.interactive.resetI -- #check tactic.interactive.revert -- Reverse of intro #check tactic.interactive.revert_all -- #check tactic.interactive.rewrite -- same as rw #check tactic.interactive.right -- If the goal is P ∨ Q, change it to Q. Also works with other types with -- precisely two constructors #check tactic.interactive.rintro -- #check tactic.interactive.rintros -- Same as rintro #check tactic.interactive.rsimp -- No docstring #check tactic.interactive.rw -- #check tactic.interactive.rwa -- rw followed by assumption #check tactic.interactive.safe -- #check tactic.interactive.same_function -- No docstring #check tactic.interactive.same_operator -- No docstring #check tactic.interactive.show -- Similar to change, but can alo select a goal other than the first one #check tactic.interactive.simp -- #check tactic.interactive.simp_core -- Is this the same as simp only[] ? #check tactic.interactive.simp_functor -- No docstring #check tactic.interactive.simp_intros #check tactic.interactive.simpa -- Like intros, but with simplification #check tactic.interactive.skip -- Does nothing; only useful in combinators #check tactic.interactive.solve1 -- #check tactic.interactive.solve_by_elim -- #check tactic.interactive.solve_mvar #check tactic.interactive.sorry #check tactic.interactive.source_fields #check tactic.interactive.specialize #check tactic.interactive.split #check tactic.interactive.split_ifs #check tactic.interactive.squeeze_simpa #check tactic.interactive.subst #check tactic.interactive.subst_vars #check tactic.interactive.substs #check tactic.interactive.success_if_fail #check tactic.interactive.suffices #check tactic.interactive.swap #check tactic.interactive.symmetry #check tactic.interactive.tauto #check tactic.interactive.tautology #check tactic.interactive.to_expr' #check tactic.interactive.trace #check tactic.interactive.trace_simp_set #check tactic.interactive.trace_state #check tactic.interactive.transitivity #check tactic.interactive.traversable_derive_handler #check tactic.interactive.traversable_derive_handler' #check tactic.interactive.traversable_law_starter #check tactic.interactive.traverse_constructor #check tactic.interactive.traverse_field #check tactic.interactive.triv #check tactic.interactive.trivial #check tactic.interactive.try #check tactic.interactive.try_for #check tactic.interactive.type_check #check tactic.interactive.unfold #check tactic.interactive.unfold1 #check tactic.interactive.unfold_aux #check tactic.interactive.unfold_coes #check tactic.interactive.unfold_projs #check tactic.interactive.unfreezeI #check tactic.interactive.unify_with_instance #check tactic.interactive.use #check tactic.interactive.with_cases #check tactic.interactive.with_prefix #check tactic.interactive.wlog
d8746d0ed724d5e07ba93e6a34c9a8653640470c
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/finset/card.lean
98cf0399b0e584af4580ee3f8a88ef813cb7f0f6
[ "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
24,649
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad -/ import data.finset.image import tactic.by_contra /-! # Cardinality of a finite set > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This defines the cardinality of a `finset` and provides induction principles for finsets. ## Main declarations * `finset.card`: `s.card : ℕ` returns the cardinality of `s : finset α`. ### Induction principles * `finset.strong_induction`: Strong induction * `finset.strong_induction_on` * `finset.strong_downward_induction` * `finset.strong_downward_induction_on` * `finset.case_strong_induction_on` ## TODO Should we add a noncomputable version? -/ open function multiset nat variables {α β : Type*} namespace finset variables {s t : finset α} {a b : α} /-- `s.card` is the number of elements of `s`, aka its cardinality. -/ def card (s : finset α) : ℕ := s.1.card lemma card_def (s : finset α) : s.card = s.1.card := rfl @[simp] lemma card_mk {m nodup} : (⟨m, nodup⟩ : finset α).card = m.card := rfl @[simp] lemma card_empty : card (∅ : finset α) = 0 := rfl lemma card_le_of_subset : s ⊆ t → s.card ≤ t.card := multiset.card_le_of_le ∘ val_le_iff.mpr @[mono] lemma card_mono : monotone (@card α) := by apply card_le_of_subset @[simp] lemma card_eq_zero : s.card = 0 ↔ s = ∅ := card_eq_zero.trans val_eq_zero lemma card_pos : 0 < s.card ↔ s.nonempty := pos_iff_ne_zero.trans $ (not_congr card_eq_zero).trans nonempty_iff_ne_empty.symm alias card_pos ↔ _ nonempty.card_pos lemma card_ne_zero_of_mem (h : a ∈ s) : s.card ≠ 0 := (not_congr card_eq_zero).2 $ ne_empty_of_mem h @[simp] lemma card_singleton (a : α) : card ({a} : finset α) = 1 := card_singleton _ lemma card_singleton_inter [decidable_eq α] : ({a} ∩ s).card ≤ 1 := begin cases (finset.decidable_mem a s), { simp [finset.singleton_inter_of_not_mem h] }, { simp [finset.singleton_inter_of_mem h] } end @[simp] lemma card_cons (h : a ∉ s) : (s.cons a h).card = s.card + 1 := card_cons _ _ section insert_erase variables [decidable_eq α] @[simp] lemma card_insert_of_not_mem (h : a ∉ s) : (insert a s).card = s.card + 1 := by rw [←cons_eq_insert _ _ h, card_cons] lemma card_insert_of_mem (h : a ∈ s) : card (insert a s) = s.card := by rw insert_eq_of_mem h lemma card_insert_le (a : α) (s : finset α) : card (insert a s) ≤ s.card + 1 := by by_cases a ∈ s; [{rw insert_eq_of_mem h, exact nat.le_succ _ }, rw card_insert_of_not_mem h] /-- If `a ∈ s` is known, see also `finset.card_insert_of_mem` and `finset.card_insert_of_not_mem`. -/ lemma card_insert_eq_ite : card (insert a s) = if a ∈ s then s.card else s.card + 1 := begin by_cases h : a ∈ s, { rw [card_insert_of_mem h, if_pos h] }, { rw [card_insert_of_not_mem h, if_neg h] } end @[simp] lemma card_doubleton (h : a ≠ b) : ({a, b} : finset α).card = 2 := by rw [card_insert_of_not_mem (not_mem_singleton.2 h), card_singleton] @[simp] lemma card_erase_of_mem : a ∈ s → (s.erase a).card = s.card - 1 := card_erase_of_mem @[simp] lemma card_erase_add_one : a ∈ s → (s.erase a).card + 1 = s.card := card_erase_add_one lemma card_erase_lt_of_mem : a ∈ s → (s.erase a).card < s.card := card_erase_lt_of_mem lemma card_erase_le : (s.erase a).card ≤ s.card := card_erase_le lemma pred_card_le_card_erase : s.card - 1 ≤ (s.erase a).card := begin by_cases h : a ∈ s, { exact (card_erase_of_mem h).ge }, { rw erase_eq_of_not_mem h, exact nat.sub_le _ _ } end /-- If `a ∈ s` is known, see also `finset.card_erase_of_mem` and `finset.erase_eq_of_not_mem`. -/ lemma card_erase_eq_ite : (s.erase a).card = if a ∈ s then s.card - 1 else s.card := card_erase_eq_ite end insert_erase @[simp] lemma card_range (n : ℕ) : (range n).card = n := card_range n @[simp] lemma card_attach : s.attach.card = s.card := multiset.card_attach end finset section to_list_multiset variables [decidable_eq α] (m : multiset α) (l : list α) lemma multiset.card_to_finset : m.to_finset.card = m.dedup.card := rfl lemma multiset.to_finset_card_le : m.to_finset.card ≤ m.card := card_le_of_le $ dedup_le _ lemma multiset.to_finset_card_of_nodup {m : multiset α} (h : m.nodup) : m.to_finset.card = m.card := congr_arg card $ multiset.dedup_eq_self.mpr h lemma list.card_to_finset : l.to_finset.card = l.dedup.length := rfl lemma list.to_finset_card_le : l.to_finset.card ≤ l.length := multiset.to_finset_card_le ⟦l⟧ lemma list.to_finset_card_of_nodup {l : list α} (h : l.nodup) : l.to_finset.card = l.length := multiset.to_finset_card_of_nodup h end to_list_multiset namespace finset variables {s t : finset α} {f : α → β} {n : ℕ} @[simp] lemma length_to_list (s : finset α) : s.to_list.length = s.card := by { rw [to_list, ←multiset.coe_card, multiset.coe_to_list], refl } lemma card_image_le [decidable_eq β] : (s.image f).card ≤ s.card := by simpa only [card_map] using (s.1.map f).to_finset_card_le lemma card_image_of_inj_on [decidable_eq β] (H : set.inj_on f s) : (s.image f).card = s.card := by simp only [card, image_val_of_inj_on H, card_map] lemma inj_on_of_card_image_eq [decidable_eq β] (H : (s.image f).card = s.card) : set.inj_on f s := begin change (s.1.map f).dedup.card = s.1.card at H, have : (s.1.map f).dedup = s.1.map f, { refine multiset.eq_of_le_of_card_le (multiset.dedup_le _) _, rw H, simp only [multiset.card_map] }, rw multiset.dedup_eq_self at this, exact inj_on_of_nodup_map this, end lemma card_image_iff [decidable_eq β] : (s.image f).card = s.card ↔ set.inj_on f s := ⟨inj_on_of_card_image_eq, card_image_of_inj_on⟩ lemma card_image_of_injective [decidable_eq β] (s : finset α) (H : injective f) : (s.image f).card = s.card := card_image_of_inj_on $ λ x _ y _ h, H h lemma fiber_card_ne_zero_iff_mem_image (s : finset α) (f : α → β) [decidable_eq β] (y : β) : (s.filter (λ x, f x = y)).card ≠ 0 ↔ y ∈ s.image f := by { rw [←pos_iff_ne_zero, card_pos, fiber_nonempty_iff_mem_image] } @[simp] lemma card_map (f : α ↪ β) : (s.map f).card = s.card := multiset.card_map _ _ @[simp] lemma card_subtype (p : α → Prop) [decidable_pred p] (s : finset α) : (s.subtype p).card = (s.filter p).card := by simp [finset.subtype] lemma card_filter_le (s : finset α) (p : α → Prop) [decidable_pred p] : (s.filter p).card ≤ s.card := card_le_of_subset $ filter_subset _ _ lemma eq_of_subset_of_card_le {s t : finset α} (h : s ⊆ t) (h₂ : t.card ≤ s.card) : s = t := eq_of_veq $ multiset.eq_of_le_of_card_le (val_le_iff.mpr h) h₂ lemma eq_of_superset_of_card_ge (hst : s ⊆ t) (hts : t.card ≤ s.card) : t = s := (eq_of_subset_of_card_le hst hts).symm lemma subset_iff_eq_of_card_le (h : t.card ≤ s.card) : s ⊆ t ↔ s = t := ⟨λ hst, eq_of_subset_of_card_le hst h, eq.subset'⟩ lemma map_eq_of_subset {f : α ↪ α} (hs : s.map f ⊆ s) : s.map f = s := eq_of_subset_of_card_le hs (card_map _).ge lemma filter_card_eq {p : α → Prop} [decidable_pred p] (h : (s.filter p).card = s.card) (x : α) (hx : x ∈ s) : p x := begin rw [←eq_of_subset_of_card_le (s.filter_subset p) h.ge, mem_filter] at hx, exact hx.2, end lemma card_lt_card (h : s ⊂ t) : s.card < t.card := card_lt_of_lt $ val_lt_iff.2 h lemma card_eq_of_bijective (f : ∀ i, i < n → α) (hf : ∀ a ∈ s, ∃ i, ∃ h : i < n, f i h = a) (hf' : ∀ i (h : i < n), f i h ∈ s) (f_inj : ∀ i j (hi : i < n) (hj : j < n), f i hi = f j hj → i = j) : s.card = n := begin classical, have : ∀ (a : α), a ∈ s ↔ ∃ i (hi : i ∈ range n), f i (mem_range.1 hi) = a, from λ a, ⟨λ ha, let ⟨i, hi, eq⟩ := hf a ha in ⟨i, mem_range.2 hi, eq⟩, λ ⟨i, hi, eq⟩, eq ▸ hf' i (mem_range.1 hi)⟩, have : s = ((range n).attach.image $ λi, f i.1 (mem_range.1 i.2)), by simpa only [ext_iff, mem_image, exists_prop, subtype.exists, mem_attach, true_and], calc s.card = card ((range n).attach.image $ λ i, f i.1 (mem_range.1 i.2)) : by rw this ... = card ((range n).attach) : card_image_of_injective _ $ λ ⟨i, hi⟩ ⟨j, hj⟩ eq, subtype.eq $ f_inj i j (mem_range.1 hi) (mem_range.1 hj) eq ... = card (range n) : card_attach ... = n : card_range n end lemma card_congr {t : finset β} (f : Π a ∈ s, β) (h₁ : ∀ a ha, f a ha ∈ t) (h₂ : ∀ a b ha hb, f a ha = f b hb → a = b) (h₃ : ∀ b ∈ t, ∃ a ha, f a ha = b) : s.card = t.card := by classical; calc s.card = s.attach.card : card_attach.symm ... = (s.attach.image (λ (a : {a // a ∈ s}), f a.1 a.2)).card : eq.symm (card_image_of_injective _ $ λ a b h, subtype.eq $ h₂ _ _ _ _ h) ... = t.card : congr_arg card (finset.ext $ λ b, ⟨λ h, let ⟨a, ha₁, ha₂⟩ := mem_image.1 h in ha₂ ▸ h₁ _ _, λ h, let ⟨a, ha₁, ha₂⟩ := h₃ b h in mem_image.2 ⟨⟨a, ha₁⟩, by simp [ha₂]⟩⟩) lemma card_le_card_of_inj_on {t : finset β} (f : α → β) (hf : ∀ a ∈ s, f a ∈ t) (f_inj : ∀ a₁ ∈ s, ∀ a₂ ∈ s, f a₁ = f a₂ → a₁ = a₂) : s.card ≤ t.card := by classical; calc s.card = (s.image f).card : (card_image_of_inj_on f_inj).symm ... ≤ t.card : card_le_of_subset $ image_subset_iff.2 hf /-- If there are more pigeons than pigeonholes, then there are two pigeons in the same pigeonhole. -/ lemma exists_ne_map_eq_of_card_lt_of_maps_to {t : finset β} (hc : t.card < s.card) {f : α → β} (hf : ∀ a ∈ s, f a ∈ t) : ∃ (x ∈ s) (y ∈ s), x ≠ y ∧ f x = f y := begin classical, by_contra' hz, refine hc.not_le (card_le_card_of_inj_on f hf _), intros x hx y hy, contrapose, exact hz x hx y hy, end lemma le_card_of_inj_on_range (f : ℕ → α) (hf : ∀ i < n, f i ∈ s) (f_inj : ∀ (i < n) (j < n), f i = f j → i = j) : n ≤ s.card := calc n = card (range n) : (card_range n).symm ... ≤ s.card : card_le_card_of_inj_on f (by simpa only [mem_range]) (by simpa only [mem_range]) lemma surj_on_of_inj_on_of_card_le {t : finset β} (f : Π a ∈ s, β) (hf : ∀ a ha, f a ha ∈ t) (hinj : ∀ a₁ a₂ ha₁ ha₂, f a₁ ha₁ = f a₂ ha₂ → a₁ = a₂) (hst : t.card ≤ s.card) : ∀ b ∈ t, ∃ a ha, b = f a ha := begin classical, intros b hb, have h : (s.attach.image $ λ (a : {a // a ∈ s}), f a a.prop).card = s.card, { exact @card_attach _ s ▸ card_image_of_injective _ (λ ⟨a₁, ha₁⟩ ⟨a₂, ha₂⟩ h, subtype.eq $ hinj _ _ _ _ h) }, have h' : image (λ a : {a // a ∈ s}, f a a.prop) s.attach = t, { exact eq_of_subset_of_card_le (λ b h, let ⟨a, ha₁, ha₂⟩ := mem_image.1 h in ha₂ ▸ hf _ _) (by simp [hst, h]) }, rw ←h' at hb, obtain ⟨a, ha₁, ha₂⟩ := mem_image.1 hb, exact ⟨a, a.2, ha₂.symm⟩, end lemma inj_on_of_surj_on_of_card_le {t : finset β} (f : Π a ∈ s, β) (hf : ∀ a ha, f a ha ∈ t) (hsurj : ∀ b ∈ t, ∃ a ha, b = f a ha) (hst : s.card ≤ t.card) ⦃a₁ a₂⦄ (ha₁ : a₁ ∈ s) (ha₂ : a₂ ∈ s) (ha₁a₂: f a₁ ha₁ = f a₂ ha₂) : a₁ = a₂ := by haveI : inhabited {x // x ∈ s} := ⟨⟨a₁, ha₁⟩⟩; exact let f' : {x // x ∈ s} → {x // x ∈ t} := λ x, ⟨f x.1 x.2, hf x.1 x.2⟩ in let g : {x // x ∈ t} → {x // x ∈ s} := @surj_inv _ _ f' (λ x, let ⟨y, hy₁, hy₂⟩ := hsurj x.1 x.2 in ⟨⟨y, hy₁⟩, subtype.eq hy₂.symm⟩) in have hg : injective g, from injective_surj_inv _, have hsg : surjective g, from λ x, let ⟨y, hy⟩ := surj_on_of_inj_on_of_card_le (λ (x : {x // x ∈ t}) (hx : x ∈ t.attach), g x) (λ x _, show (g x) ∈ s.attach, from mem_attach _ _) (λ x y _ _ hxy, hg hxy) (by simpa) x (mem_attach _ _) in ⟨y, hy.snd.symm⟩, have hif : injective f', from (left_inverse_of_surjective_of_right_inverse hsg (right_inverse_surj_inv _)).injective, subtype.ext_iff_val.1 (@hif ⟨a₁, ha₁⟩ ⟨a₂, ha₂⟩ (subtype.eq ha₁a₂)) @[simp] lemma card_disj_union (s t : finset α) (h) : (s.disj_union t h).card = s.card + t.card := multiset.card_add _ _ /-! ### Lattice structure -/ section lattice variables [decidable_eq α] lemma card_union_add_card_inter (s t : finset α) : (s ∪ t).card + (s ∩ t).card = s.card + t.card := finset.induction_on t (by simp) $ λ a r har, by by_cases a ∈ s; simp *; cc lemma card_inter_add_card_union (s t : finset α) : (s ∩ t).card + (s ∪ t).card = s.card + t.card := by rw [add_comm, card_union_add_card_inter] lemma card_union_le (s t : finset α) : (s ∪ t).card ≤ s.card + t.card := card_union_add_card_inter s t ▸ nat.le_add_right _ _ lemma card_union_eq (h : disjoint s t) : (s ∪ t).card = s.card + t.card := by rw [←disj_union_eq_union s t h, card_disj_union _ _ _] @[simp] lemma card_disjoint_union (h : disjoint s t) : card (s ∪ t) = s.card + t.card := card_union_eq h lemma card_sdiff (h : s ⊆ t) : card (t \ s) = t.card - s.card := suffices card (t \ s) = card ((t \ s) ∪ s) - s.card, by rwa sdiff_union_of_subset h at this, by rw [card_disjoint_union sdiff_disjoint, add_tsub_cancel_right] lemma card_sdiff_add_card_eq_card {s t : finset α} (h : s ⊆ t) : card (t \ s) + card s = card t := ((nat.sub_eq_iff_eq_add (card_le_of_subset h)).mp (card_sdiff h).symm).symm lemma le_card_sdiff (s t : finset α) : t.card - s.card ≤ card (t \ s) := calc card t - card s ≤ card t - card (s ∩ t) : tsub_le_tsub_left (card_le_of_subset (inter_subset_left s t)) _ ... = card (t \ (s ∩ t)) : (card_sdiff (inter_subset_right s t)).symm ... ≤ card (t \ s) : by rw sdiff_inter_self_right t s lemma card_le_card_sdiff_add_card : s.card ≤ (s \ t).card + t.card := tsub_le_iff_right.1 $ le_card_sdiff _ _ lemma card_sdiff_add_card : (s \ t).card + t.card = (s ∪ t).card := by rw [←card_disjoint_union sdiff_disjoint, sdiff_union_self_eq_union] end lattice lemma filter_card_add_filter_neg_card_eq_card (p : α → Prop) [decidable_pred p] : (s.filter p).card + (s.filter (not ∘ p)).card = s.card := by { classical, simp [←card_union_eq, filter_union_filter_neg_eq, disjoint_filter] } /-- Given a set `A` and a set `B` inside it, we can shrink `A` to any appropriate size, and keep `B` inside it. -/ lemma exists_intermediate_set {A B : finset α} (i : ℕ) (h₁ : i + card B ≤ card A) (h₂ : B ⊆ A) : ∃ (C : finset α), B ⊆ C ∧ C ⊆ A ∧ card C = i + card B := begin classical, rcases nat.le.dest h₁ with ⟨k, _⟩, clear h₁, induction k with k ih generalizing A, { exact ⟨A, h₂, subset.refl _, h.symm⟩ }, have : (A \ B).nonempty, { rw [←card_pos, card_sdiff h₂, ←h, nat.add_right_comm, add_tsub_cancel_right, nat.add_succ], apply nat.succ_pos }, rcases this with ⟨a, ha⟩, have z : i + card B + k = card (erase A a), { rw [card_erase_of_mem (mem_sdiff.1 ha).1, ←h], refl }, rcases ih _ z with ⟨B', hB', B'subA', cards⟩, { exact ⟨B', hB', trans B'subA' (erase_subset _ _), cards⟩ }, { rintro t th, apply mem_erase_of_ne_of_mem _ (h₂ th), rintro rfl, exact not_mem_sdiff_of_mem_right th ha } end /-- We can shrink `A` to any smaller size. -/ lemma exists_smaller_set (A : finset α) (i : ℕ) (h₁ : i ≤ card A) : ∃ (B : finset α), B ⊆ A ∧ card B = i := let ⟨B, _, x₁, x₂⟩ := exists_intermediate_set i (by simpa) (empty_subset A) in ⟨B, x₁, x₂⟩ lemma exists_subset_or_subset_of_two_mul_lt_card [decidable_eq α] {X Y : finset α} {n : ℕ} (hXY : 2 * n < (X ∪ Y).card) : ∃ C : finset α, n < C.card ∧ (C ⊆ X ∨ C ⊆ Y) := begin have h₁ : (X ∩ (Y \ X)).card = 0 := finset.card_eq_zero.mpr (finset.inter_sdiff_self X Y), have h₂ : (X ∪ Y).card = X.card + (Y \ X).card, { rw [←card_union_add_card_inter X (Y \ X), finset.union_sdiff_self_eq_union, h₁, add_zero] }, rw [h₂, two_mul] at hXY, rcases lt_or_lt_of_add_lt_add hXY with h|h, { exact ⟨X, h, or.inl (finset.subset.refl X)⟩ }, { exact ⟨Y \ X, h, or.inr (finset.sdiff_subset Y X)⟩ } end /-! ### Explicit description of a finset from its card -/ lemma card_eq_one : s.card = 1 ↔ ∃ a, s = {a} := by cases s; simp only [multiset.card_eq_one, finset.card, ←val_inj, singleton_val] lemma exists_eq_insert_iff [decidable_eq α] {s t : finset α} : (∃ a ∉ s, insert a s = t) ↔ s ⊆ t ∧ s.card + 1 = t.card := begin split, { rintro ⟨a, ha, rfl⟩, exact ⟨subset_insert _ _, (card_insert_of_not_mem ha).symm⟩ }, { rintro ⟨hst, h⟩, obtain ⟨a, ha⟩ : ∃ a, t \ s = {a}, { exact card_eq_one.1 (by rw [card_sdiff hst, ←h, add_tsub_cancel_left]) }, refine ⟨a, λ hs, (_ : a ∉ {a}) $ mem_singleton_self _, by rw [insert_eq, ←ha, sdiff_union_of_subset hst]⟩, rw ←ha, exact not_mem_sdiff_of_mem_right hs } end lemma card_le_one : s.card ≤ 1 ↔ ∀ (a ∈ s) (b ∈ s), a = b := begin obtain rfl | ⟨x, hx⟩ := s.eq_empty_or_nonempty, { simp }, refine (nat.succ_le_of_lt (card_pos.2 ⟨x, hx⟩)).le_iff_eq.trans (card_eq_one.trans ⟨_, _⟩), { rintro ⟨y, rfl⟩, simp }, { exact λ h, ⟨x, eq_singleton_iff_unique_mem.2 ⟨hx, λ y hy, h _ hy _ hx⟩⟩ } end lemma card_le_one_iff : s.card ≤ 1 ↔ ∀ {a b}, a ∈ s → b ∈ s → a = b := by { rw card_le_one, tauto } lemma card_le_one_iff_subset_singleton [nonempty α] : s.card ≤ 1 ↔ ∃ (x : α), s ⊆ {x} := begin refine ⟨λ H, _, _⟩, { obtain rfl | ⟨x, hx⟩ := s.eq_empty_or_nonempty, { exact ⟨classical.arbitrary α, empty_subset _⟩ }, { exact ⟨x, λ y hy, by rw [card_le_one.1 H y hy x hx, mem_singleton]⟩ } }, { rintro ⟨x, hx⟩, rw ←card_singleton x, exact card_le_of_subset hx } end /-- A `finset` of a subsingleton type has cardinality at most one. -/ lemma card_le_one_of_subsingleton [subsingleton α] (s : finset α) : s.card ≤ 1 := finset.card_le_one_iff.2 $ λ _ _ _ _, subsingleton.elim _ _ lemma one_lt_card : 1 < s.card ↔ ∃ (a ∈ s) (b ∈ s), a ≠ b := by { rw ←not_iff_not, push_neg, exact card_le_one } lemma one_lt_card_iff : 1 < s.card ↔ ∃ a b, a ∈ s ∧ b ∈ s ∧ a ≠ b := by { rw one_lt_card, simp only [exists_prop, exists_and_distrib_left] } lemma two_lt_card_iff : 2 < s.card ↔ ∃ a b c, a ∈ s ∧ b ∈ s ∧ c ∈ s ∧ a ≠ b ∧ a ≠ c ∧ b ≠ c := begin classical, refine ⟨λ h, _, _⟩, { obtain ⟨c, hc⟩ := card_pos.mp (zero_lt_two.trans h), have : 1 < (s.erase c).card := by rwa [←add_lt_add_iff_right 1, card_erase_add_one hc], obtain ⟨a, b, ha, hb, hab⟩ := one_lt_card_iff.mp this, exact ⟨a, b, c, mem_of_mem_erase ha, mem_of_mem_erase hb, hc, hab, ne_of_mem_erase ha, ne_of_mem_erase hb⟩ }, { rintros ⟨a, b, c, ha, hb, hc, hab, hac, hbc⟩, rw [←card_erase_add_one hc, ←card_erase_add_one (mem_erase_of_ne_of_mem hbc hb), ←card_erase_add_one (mem_erase_of_ne_of_mem hab (mem_erase_of_ne_of_mem hac ha))], apply nat.le_add_left }, end lemma two_lt_card : 2 < s.card ↔ ∃ (a ∈ s) (b ∈ s) (c ∈ s), a ≠ b ∧ a ≠ c ∧ b ≠ c := by simp_rw [two_lt_card_iff, exists_prop, exists_and_distrib_left] lemma exists_ne_of_one_lt_card (hs : 1 < s.card) (a : α) : ∃ b, b ∈ s ∧ b ≠ a := begin obtain ⟨x, hx, y, hy, hxy⟩ := finset.one_lt_card.mp hs, by_cases ha : y = a, { exact ⟨x, hx, ne_of_ne_of_eq hxy ha⟩ }, { exact ⟨y, hy, ha⟩ } end lemma card_eq_succ [decidable_eq α] : s.card = n + 1 ↔ ∃ a t, a ∉ t ∧ insert a t = s ∧ t.card = n := ⟨λ h, let ⟨a, has⟩ := card_pos.mp (h.symm ▸ nat.zero_lt_succ _ : 0 < s.card) in ⟨a, s.erase a, s.not_mem_erase a, insert_erase has, by simp only [h, card_erase_of_mem has, add_tsub_cancel_right]⟩, λ ⟨a, t, hat, s_eq, n_eq⟩, s_eq ▸ n_eq ▸ card_insert_of_not_mem hat⟩ lemma card_eq_two [decidable_eq α] : s.card = 2 ↔ ∃ x y, x ≠ y ∧ s = {x, y} := begin split, { rw card_eq_succ, simp_rw [card_eq_one], rintro ⟨a, _, hab, rfl, b, rfl⟩, exact ⟨a, b, not_mem_singleton.1 hab, rfl⟩ }, { rintro ⟨x, y, h, rfl⟩, exact card_doubleton h } end lemma card_eq_three [decidable_eq α] : s.card = 3 ↔ ∃ x y z, x ≠ y ∧ x ≠ z ∧ y ≠ z ∧ s = {x, y, z} := begin split, { rw card_eq_succ, simp_rw [card_eq_two], rintro ⟨a, _, abc, rfl, b, c, bc, rfl⟩, rw [mem_insert, mem_singleton, not_or_distrib] at abc, exact ⟨a, b, c, abc.1, abc.2, bc, rfl⟩ }, { rintro ⟨x, y, z, xy, xz, yz, rfl⟩, simp only [xy, xz, yz, mem_insert, card_insert_of_not_mem, not_false_iff, mem_singleton, or_self, card_singleton] } end /-! ### Inductions -/ /-- Suppose that, given objects defined on all strict subsets of any finset `s`, one knows how to define an object on `s`. Then one can inductively define an object on all finsets, starting from the empty set and iterating. This can be used either to define data, or to prove properties. -/ def strong_induction {p : finset α → Sort*} (H : ∀ s, (∀ t ⊂ s, p t) → p s) : ∀ (s : finset α), p s | s := H s (λ t h, have t.card < s.card, from card_lt_card h, strong_induction t) using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf card⟩]} lemma strong_induction_eq {p : finset α → Sort*} (H : ∀ s, (∀ t ⊂ s, p t) → p s) (s : finset α) : strong_induction H s = H s (λ t h, strong_induction H t) := by rw strong_induction /-- Analogue of `strong_induction` with order of arguments swapped. -/ @[elab_as_eliminator] def strong_induction_on {p : finset α → Sort*} (s : finset α) : (∀ s, (∀ t ⊂ s, p t) → p s) → p s := λ H, strong_induction H s lemma strong_induction_on_eq {p : finset α → Sort*} (s : finset α) (H : ∀ s, (∀ t ⊂ s, p t) → p s) : s.strong_induction_on H = H s (λ t h, t.strong_induction_on H) := by { dunfold strong_induction_on, rw strong_induction } @[elab_as_eliminator] lemma case_strong_induction_on [decidable_eq α] {p : finset α → Prop} (s : finset α) (h₀ : p ∅) (h₁ : ∀ a s, a ∉ s → (∀ t ⊆ s, p t) → p (insert a s)) : p s := finset.strong_induction_on s $ λ s, finset.induction_on s (λ _, h₀) $ λ a s n _ ih, h₁ a s n $ λ t ss, ih _ (lt_of_le_of_lt ss (ssubset_insert n) : t < _) /-- Suppose that, given that `p t` can be defined on all supersets of `s` of cardinality less than `n`, one knows how to define `p s`. Then one can inductively define `p s` for all finsets `s` of cardinality less than `n`, starting from finsets of card `n` and iterating. This can be used either to define data, or to prove properties. -/ def strong_downward_induction {p : finset α → Sort*} {n : ℕ} (H : ∀ t₁, (∀ {t₂ : finset α}, t₂.card ≤ n → t₁ ⊂ t₂ → p t₂) → t₁.card ≤ n → p t₁) : ∀ (s : finset α), s.card ≤ n → p s | s := H s (λ t ht h, have n - t.card < n - s.card, from (tsub_lt_tsub_iff_left_of_le ht).2 (finset.card_lt_card h), strong_downward_induction t ht) using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ (t : finset α), n - t.card)⟩]} lemma strong_downward_induction_eq {p : finset α → Sort*} (H : ∀ t₁, (∀ {t₂ : finset α}, t₂.card ≤ n → t₁ ⊂ t₂ → p t₂) → t₁.card ≤ n → p t₁) (s : finset α) : strong_downward_induction H s = H s (λ t ht hst, strong_downward_induction H t ht) := by rw strong_downward_induction /-- Analogue of `strong_downward_induction` with order of arguments swapped. -/ @[elab_as_eliminator] def strong_downward_induction_on {p : finset α → Sort*} (s : finset α) (H : ∀ t₁, (∀ {t₂ : finset α}, t₂.card ≤ n → t₁ ⊂ t₂ → p t₂) → t₁.card ≤ n → p t₁) : s.card ≤ n → p s := strong_downward_induction H s lemma strong_downward_induction_on_eq {p : finset α → Sort*} (s : finset α) (H : ∀ t₁, (∀ {t₂ : finset α}, t₂.card ≤ n → t₁ ⊂ t₂ → p t₂) → t₁.card ≤ n → p t₁) : s.strong_downward_induction_on H = H s (λ t ht h, t.strong_downward_induction_on H ht) := by { dunfold strong_downward_induction_on, rw strong_downward_induction } lemma lt_wf {α} : well_founded (@has_lt.lt (finset α) _) := have H : subrelation (@has_lt.lt (finset α) _) (inv_image ( < ) card), from λ x y hxy, card_lt_card hxy, subrelation.wf H $ inv_image.wf _ $ nat.lt_wf end finset
c5d088d1345a7268749b4357b4b40b4406a4f582
fef48cac17c73db8662678da38fd75888db97560
/src/point9.lean
0d4b349e944ebc404309c123140d1c2725ed48bc
[]
no_license
kbuzzard/lean-squares-in-fibonacci
6c0d924f799d6751e19798bb2530ee602ec7087e
8cea20e5ce88ab7d17b020932d84d316532a84a8
refs/heads/master
1,584,524,504,815
1,582,387,156,000
1,582,387,156,000
134,576,655
3
1
null
1,541,538,497,000
1,527,083,406,000
Lean
UTF-8
Lean
false
false
205
lean
-- 9) Deduce that if m > 2 and u m is a square, then m is a multiple of 12. import definitions theorem twelve_dvd_of_fib_square (m : ℕ) : m > 2 → (∃ d : ℕ, d ^ 2 = fib m) → 12 ∣ m := sorry
001d008363f6f7e5fcccce2cb94cf41fd31a8b0f
569919c7071b4209bdf17284c1b44d9b93c3b136
/src/solutions/00_first_proofs.lean
1145309c4671855790aaada3535c682e4d7c4ad2
[ "Apache-2.0" ]
permissive
dimpase/tutorials
74f149c4d5b945cd7cd677e4423d644265a7e3bf
cc7d1b2bff39b36907b3a56b446e123d0762a63b
refs/heads/master
1,668,521,216,072
1,593,390,403,000
1,593,390,403,000
276,760,422
0
0
Apache-2.0
1,593,730,650,000
1,593,730,649,000
null
UTF-8
Lean
false
false
18,035
lean
/- This file is intended for Lean beginners. The goal is to demonstrate what it feels like to prove things using Lean and mathlib. Complicated definitions and theory building are not covered. Everything is covered again more slowly and with exercises in the next files. -/ -- We want real numbers and their basic properties import data.real.basic -- We want to be able to define functions using the law of excluded middle noncomputable theory open_locale classical /- Our first goal is to define the set of upper bounds of a set of real numbers. This is already defined in mathlib (in a more general context), but we repeat it for the sake of exposition. Right-click "upper_bounds" below to get offered to jump to mathlib's version -/ #check upper_bounds /-- The set of upper bounds of a set of real numbers ℝ -/ def up_bounds (A : set ℝ) := { x : ℝ | ∀ a ∈ A, a ≤ x} /-- Predicate `is_max a A` means `a` is a maximum of `A` -/ def is_max (a : ℝ) (A : set ℝ) := a ∈ A ∧ a ∈ up_bounds A /- In the above definition, the symbol `∧` means "and". We also see the most visible difference between set theoretic foundations and type theoretic ones (used by almost all proof assistants). In set theory, everything is a set, and the only relation you get from foundations are `=` and `∈`. In type theory, there is a meta-theoretic relation of "typing": `a : ℝ` reads "`a` is a real number" or, more precisely, "the type of `a` is `ℝ`". Here "meta-theoretic" means this is not a statement you can prove or disprove inside the theory, it's a fact that is true or not. Here we impose this fact, in other circumstances, it would be checked by the Lean kernel. By contrast, `a ∈ A` is a statement inside the theory. Here it's part of the definition, in other circumstances it could be something proven inside Lean. -/ /- For illustrative purposes, we now define an infix version of the above predicate. It will allow us to write `a is_a_max_of A`, which is closer to a sentence. -/ infix `is_a_max_of`:55 := is_max /- Let's prove something now! A set of real numbers has at most one maximum. Here everything left of the final `:` is introducing the objects and assumption. The equality `x = y` right of the colon is the conclusion. -/ lemma unique_max (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := begin -- We first break our assumptions in their two constituent pieces. -- We are free to choose the name following `with` cases hx with x_in x_up, cases hy with y_in y_up, -- Assumption `x_up` means x isn't less than elements of A, let's apply this to y specialize x_up y, -- Assumption `x_up` now needs the information that `y` is indeed in `A`. specialize x_up y_in, -- Let's do this quicker with roles swapped specialize y_up x x_in, -- We explained to Lean the idea of this proof. -- Now we know `x ≤ y` and `y ≤ x`, and Lean shouldn't need more help. -- `linarith` proves equalities and inequalities that follow linearly from -- the assumption we have. linarith, end /- The above proof is too long, even if you remove comments. We don't really need the unpacking steps at the beginning; we can access both parts of the assumption `hx : x is_a_max_of A` using shortcuts `h.1` and `h.2`. We can also improve readability without assistance from the tactic state display, clearly announcing intermediate goals using `have`. This way we get to the following version of the same proof. -/ example (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := begin have : x ≤ y, from hy.2 x hx.1, have : y ≤ x, from hx.2 y hy.1, linarith, end /- Notice how mathematics based on type theory treats the assumption `∀ a ∈ A, a ≤ y` as a function turning an element `a` of `A` into the statement `a ≤ y`. More precisely, this assumption is the abbreviation of `∀ a : ℝ, a ∈ A → a ≤ y`. The expression `hy.2 x` appearing in the above proof is then the statement `x ∈ A → x ≤ y`, which itself is a function turning a statement `x ∈ A` into `x ≤ y` so that the full expression `hy.2 x hx.1` is indeed a proof of `x ≤ y`. One could argue a three-line-long proof of this lemma is still two lines too long. This is debatable, but mathlib's style is to write very short proofs for trivial lemmas. Those proofs are not easy to read but they are meant to indicate that the proof is probably not worth reading. In order to reach this stage, we need to know what `linarith` did for us. It invoked the lemma `le_antisymm` which says: `x ≤ y → y ≤ x → x = y`. This arrow, which is used both for function and implication, is right associative. So the statement is `x ≤ y → (y ≤ x → x = y)` which reads: I will send a proof `p` of `x ≤ y` to a function sending a proof `q'` of `y ≤ x` to a proof of `x = y`. Hence `le_antisymm p q'` is a proof of `x = y`. Using this we can get our one-line proof: -/ example (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := le_antisymm (hy.2 x hx.1) (hx.2 y hy.1) /- Such a proof is called a proof term (or a "term mode" proof). Notice it has no `begin` and `end`. It is directly the kind of low level proof that the Lean kernel is consuming. Commands like `cases`, `specialize` or `linarith` are called tactics, they help users constructing proof terms that could be very tedious to write directly. The most efficient proof style combines tactics with proof terms like our previous `have : x ≤ y, from hy.2 x hx.1` where `hy.2 x hx.1` is a proof term embeded inside a tactic mode proof. In the remaining of this file, we'll be characterizing infima of sets of real numbers in term of sequences. -/ /-- The set of lower bounds of a set of real numbers ℝ -/ def low_bounds (A : set ℝ) := { x : ℝ | ∀ a ∈ A, x ≤ a} /- We now define `a` is an infimum of `A`. Again there is already a more general version in mathlib. -/ def is_inf (x : ℝ) (A : set ℝ) := x is_a_max_of (low_bounds A) infix ` is_an_inf_of `:55 := is_inf /- We need to prove that any number which is greater than the infimum of A is greater than some element of A. -/ lemma inf_lt {A : set ℝ} {x : ℝ} (hx : x is_an_inf_of A) : ∀ y, x < y → ∃ a ∈ A, a < y := begin -- Let `y` be any real number. intro y, -- Let's prove the contrapositive contrapose, -- The symbol `¬` means negation. Let's ask Lean to rewrite the goal without negation, -- pushing negation through quantifiers and inequalities push_neg, -- Let's assume the premise, calling the assumption `h` intro h, -- `h` is exactly saying `y` is a lower bound of `A` so the second part of -- the infimum assumption `hx` applied to `y` and `h` is exactly what we want. exact hx.2 y h end /- In the above proof, the sequence `contrapose, push_neg` is so common that it can be abbreviated to `contrapose!`. With these commands, we enter the gray zone between proof checking and proof finding. Practical computer proof checking crucially needs the computer to handle tedious proof steps. In the next proof, we'll start using `linarith` a bit more seriously, going one step further into automation. Our next real goal is to prove inequalities for limits of sequences. We extract the following lemma: if `y ≤ x + ε` for all positive `ε` then `y ≤ x`. -/ lemma le_of_le_add_eps {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := begin -- Let's prove the contrapositive, asking Lean to push negations right away. contrapose!, -- Assume `h : x < y`. intro h, -- We need to find `ε` such that `ε` is positive and `x + ε < y`. -- Let's use `(y-x)/2` use ((y-x)/2), -- we now have two properties to prove. Let's do both in turn, using `linarith` split, linarith, linarith, end /- Note how `linarith` was used for both sub-goals at the end of the above proof. We could have shortened that using the semi-colon combinator instead of comma, writing `split ; linarith`. Next we will study a compressed version of that proof: -/ example {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := begin contrapose!, exact assume h, ⟨(y-x)/2, by linarith, by linarith⟩, end /- The angle brackets `⟨` and `⟩` introduce compound data or proofs. A proof of a `∃ z, P z` statemement is composed of a witness `z₀` and a proof `h` of `P z₀`. The compound is denoted by `⟨z₀, h⟩`. In the example above, the predicate is itself compound, it is a conjunction `P z ∧ Q z`. So the proof term should read `⟨z₀, ⟨h₁, h₂⟩⟩` where `h₁` (resp. `h₂`) is a proof of `P z₀` (resp. `Q z₀`). But these so-called "anonymous constructor" brackets are right-associative, so we can get rid of the nested brackets. The keyword `by` introduces tactic mode inside term mode, it is a shorter version of the `begin`/`end` pair, which is more convenient for single tactic blocks. In this example, `begin` enters tactic mode, `exact` leaves it, `by` re-enters it. Going all the way to a proof term would make the proof much longer, because we crucially use automation with `contrapose!` and `linarith`. We can still get a one-line proof using curly braces to gather several tactic invocations, and the `by` abbreviation instead of `begin`/`end`: -/ example {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := by { contrapose!, exact assume h, ⟨(y-x)/2, by linarith, by linarith⟩ } /- One could argue that the above proof is a bit too terse, and we are relying too much on linarith. Let's have more `linarith` calls for smaller steps. For the sake of (tiny) variation, we will also assume the premise and argue by contradiction instead of contraposing. -/ example {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := begin intro h, -- Assume the conclusion is false, and call this assumption H. by_contradiction H, push_neg at H, -- Now let's compute. have key := calc -- Each line must end with a colon followed by a proof term -- We want to specialize our assumption `h` to `ε = (y-x)/2` but this is long to -- type, so let's put a hole `_` that Lean will fill in by comparing the -- statement we want to prove and our proof term with a hole. As usual, -- positivity of `(y-x)/2` is proved by `linarith` y ≤ x + (y-x)/2 : h _ (by linarith) ... = x/2 + y/2 : by ring ... < y : by linarith, -- our key now says `y < y` (notice how the sequence `≤`, `=`, `<` was correctly -- merged into a `<`). Let `linarith` find the desired contradiction now. linarith, -- alternatively, we could have provided the proof term -- `exact lt_irrefl y key` end /- Now we are ready for some analysis. Let's set up notation for absolute value -/ local notation `|`x`|` := abs x /- And let's define convergence of sequences of real numbers (of course there is a much more general definition in mathlib). -/ /-- The sequence `u` tends to `l` -/ def limit (u : ℕ → ℝ) (l : ℝ) := ∀ ε > 0, ∃ N, ∀ n ≥ N, |u n - l| ≤ ε /- In the above definition, `u n` denotes the n-th term of the sequence. We can add parentheses to get `u(n)` but we try to avoid parentheses because they pile up very quickly -/ -- If y ≤ u n for all n and u n goes to x then y ≤ x lemma le_lim {x y : ℝ} {u : ℕ → ℝ} (hu : limit u x) (ineq : ∀ n, y ≤ u n) : y ≤ x := begin -- Let's apply our previous lemma apply le_of_le_add_eps, -- We need to prove y ≤ x + ε for all positive ε. -- Let ε be any positive real intros ε ε_pos, -- we now specialize our limit assumption to this `ε`, and immediately -- fix a `N` as promised by the definition. cases hu ε ε_pos with N HN, -- Now we only need to compute until reaching the conclusion calc y ≤ u N : ineq N ... = x + (u N - x) : by linarith -- We'll need `add_le_add` which says `a ≤ b` and `c ≤ d` implies `a + c ≤ b + d` -- We need a lemma saying `z ≤ |z|`. Because we don't know the name of this lemma, -- let's use `library_search`. Because searching thourgh the library is slow, -- Lean will write what it found in the Lean message window when cursor is on -- that line, so that we can replace it by the lemma. We see `le_max_left` which -- says `a ≤ max a b`. Actually there is a more specific lemma `le_abs_self` ... ≤ x + |u N - x| : add_le_add (by linarith) (by library_search) ... ≤ x + ε : add_le_add (by linarith) (HN N (by linarith)), end /- The next lemma has been extracted from the main proof in order to discuss numbers. In ordinary maths, we know that ℕ is *not* contained in `ℝ`, whatever the construction of real numbers that we use. For instance a natural number is not an equivalence class of Cauchy sequences. But it's very easy to pretend otherwise. Formal maths requires slightly more care. In the statement below, the "type ascription" `(n + 1 : ℝ)` forces Lean to convert the natural number `n+1` into a real number. The "inclusion" map will be displayed in tactic state as `↑`. There are various lemmas asserting this map is compatible with addition and monotone, but we don't want to bother writing their names. The `norm_cast` tactic is designed to wisely apply those lemmas for us. -/ lemma inv_succ_pos : ∀ n : ℕ, 1/(n+1 : ℝ) > 0 := begin -- Let `n` be any integer intro n, -- Since we don't know the name of the relevant lemma, asserting that the inverse of -- a positive number is positive, let's state that is suffices -- to prove that `n+1`, seen as a real number, is positive, and ask `library_search` suffices : (n + 1 : ℝ) > 0, { library_search }, -- Now we want to reduce to a statement about natural numbers, not real numbers -- coming from natural numbers. norm_cast, -- and then get the usual help from `linarith` linarith, end /- That was a pretty long proof for an obvious fact. And stating it as a lemma feels stupid, so let's find a way to write it on one line in case we want to include it in some other proof without stating a lemma. First the `library_search` call above displays the name of the relevant lemma: `one_div_pos_of_pos`. We can also replace the `linarith` call on the last line by `library_search` to learn the name of the lemma `nat.succ_pos` asserting that the successor of a natural number is positive. There is also a variant on `norm_cast` that combines it with `exact`. The term mode analogue of `intro` is `λ`. We get down to: -/ example : ∀ n : ℕ, 1/(n+1 : ℝ) > 0 := λ n, one_div_pos_of_pos (by exact_mod_cast nat.succ_pos n) /- The next proof uses mostly known things, so we will commment only new aspects. -/ lemma limit_inv_succ : ∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, 1/(n + 1 : ℝ) ≤ ε := begin intros ε ε_pos, suffices : ∃ N : ℕ, 1/ε ≤ N, { -- Because we didn't provide a name for the above statement, Lean called it `this`. -- Let's fix an `N` that works. cases this with N HN, use N, intros n Hn, -- Now we want to rewrite the goal using lemmas -- `div_le_iff' : 0 < b → (a / b ≤ c ↔ a ≤ b * c)` -- `div_le_iff : 0 < b → (a / b ≤ c ↔ a ≤ c * b)` -- the second one will be rewritten from right to left, as indicated by `←`. -- Lean will create a side goal for the required positivity assumption that -- we don't provide for `div_le_iff'`. rw [div_le_iff', ← div_le_iff ε_pos], -- We want to replace assumption `Hn` by its real counter-part so that -- linarith can find what it needs. replace Hn : (N : ℝ) ≤ n, exact_mod_cast Hn, linarith, -- we are still left with the positivity assumption, but already discussed -- how to prove it in the preceding lemma exact_mod_cast nat.succ_pos n }, -- Now we need to prove that sufficient statement. -- We want to use that `ℝ` is archimedean. So we start typing -- `exact archimedean_` and hit Ctrl-space to see what completion Lean proposes -- the lemma `archimedean_iff_nat_le` sounds promising. We select the left to -- right implication using `.1`. This a generic lemma for fields equiped with -- a linear (ie total) order. We need to provide a proof that `ℝ` is indeed -- archimedean. This is done using the `apply_instance` tactic that will be -- covered elsewhere. exact archimedean_iff_nat_le.1 (by apply_instance) (1/ε), end /- We can now put all pieces together, with almost no new things to explain. -/ lemma inf_seq (A : set ℝ) (x : ℝ) : (x is_an_inf_of A) ↔ (x ∈ low_bounds A ∧ ∃ u : ℕ → ℝ, limit u x ∧ ∀ n, u n ∈ A ) := begin split, { intro h, split, { exact h.1 }, -- On the next line, we don't need to tell Lean to treat `n+1` as a real number because -- we add `x` to it, so Lean knows there is only one way to make sense of this expression. have key : ∀ n : ℕ, ∃ a ∈ A, a < x + 1/(n+1), { intro n, -- we can use the lemma we proved above apply inf_lt h, -- and another one we proved! have : 0 < 1/(n+1 : ℝ), from inv_succ_pos n, linarith }, -- Now we need to use axiom of (countable) choice choose u hu using key, use u, split, { intros ε ε_pos, -- again we use a lemma we proved, specializing it to our fixed `ε`, and fixing a `N` cases limit_inv_succ ε ε_pos with N H, use N, intros n hn, have : x ≤ u n, from h.1 _ (hu n).1, have := calc u n < x + 1/(n + 1) : (hu n).2 ... ≤ x + ε : add_le_add (le_refl x) (H n hn), rw abs_of_nonneg ; linarith }, { intro n, exact (hu n).1 } }, { intro h, -- Assumption `h` is made of nested compound statements. We can use the -- recursive version of `cases` to unpack it in one go. rcases h with ⟨x_min, u, lim, huA⟩, split, exact x_min, intros y y_mino, apply le_lim lim, intro n, exact y_mino (u n) (huA n) }, end
389a1c88ab4a78b136d51f94eb213776add0d044
206422fb9edabf63def0ed2aa3f489150fb09ccb
/src/measure_theory/bochner_integration.lean
6deaa5c483febc47226103698561af6cba2f6826
[ "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
68,602
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Yury Kudryashov -/ import measure_theory.simple_func_dense import analysis.normed_space.bounded_linear_maps import topology.sequences /-! # Bochner integral The Bochner integral extends the definition of the Lebesgue integral to functions that map from a measure space into a Banach space (complete normed vector space). It is constructed here by extending the integral on simple functions. ## Main definitions The Bochner integral is defined following these steps: 1. Define the integral on simple functions of the type `simple_func α E` (notation : `α →ₛ E`) where `E` is a real normed space. (See `simple_func.bintegral` and section `bintegral` for details. Also see `simple_func.integral` for the integral on simple functions of the type `simple_func α ennreal`.) 2. Use `α →ₛ E` to cut out the simple functions from L1 functions, and define integral on these. The type of simple functions in L1 space is written as `α →₁ₛ[μ] E`. 3. Show that the embedding of `α →₁ₛ[μ] E` into L1 is a dense and uniform one. 4. Show that the integral defined on `α →₁ₛ[μ] E` is a continuous linear map. 5. Define the Bochner integral on L1 functions by extending the integral on integrable simple functions `α →₁ₛ[μ] E` using `continuous_linear_map.extend`. Define the Bochner integral on functions as the Bochner integral of its equivalence class in L1 space. ## Main statements 1. Basic properties of the Bochner integral on functions of type `α → E`, where `α` is a measure space and `E` is a real normed space. * `integral_zero` : `∫ 0 ∂μ = 0` * `integral_add` : `∫ x, f x + g x ∂μ = ∫ x, f ∂μ + ∫ x, g x ∂μ` * `integral_neg` : `∫ x, - f x ∂μ = - ∫ x, f x ∂μ` * `integral_sub` : `∫ x, f x - g x ∂μ = ∫ x, f x ∂μ - ∫ x, g x ∂μ` * `integral_smul` : `∫ x, r • f x ∂μ = r • ∫ x, f x ∂μ` * `integral_congr_ae` : `f =ᵐ[μ] g → ∫ x, f x ∂μ = ∫ x, g x ∂μ` * `norm_integral_le_integral_norm` : `∥∫ x, f x ∂μ∥ ≤ ∫ x, ∥f x∥ ∂μ` 2. Basic properties of the Bochner integral on functions of type `α → ℝ`, where `α` is a measure space. * `integral_nonneg_of_ae` : `0 ≤ᵐ[μ] f → 0 ≤ ∫ x, f x ∂μ` * `integral_nonpos_of_ae` : `f ≤ᵐ[μ] 0 → ∫ x, f x ∂μ ≤ 0` * `integral_mono_ae` : `f ≤ᵐ[μ] g → ∫ x, f x ∂μ ≤ ∫ x, g x ∂μ` * `integral_nonneg` : `0 ≤ f → 0 ≤ ∫ x, f x ∂μ` * `integral_nonpos` : `f ≤ 0 → ∫ x, f x ∂μ ≤ 0` * `integral_mono` : `f ≤ᵐ[μ] g → ∫ x, f x ∂μ ≤ ∫ x, g x ∂μ` 3. Propositions connecting the Bochner integral with the integral on `ennreal`-valued functions, which is called `lintegral` and has the notation `∫⁻`. * `integral_eq_lintegral_max_sub_lintegral_min` : `∫ x, f x ∂μ = ∫⁻ x, f⁺ x ∂μ - ∫⁻ x, f⁻ x ∂μ`, where `f⁺` is the positive part of `f` and `f⁻` is the negative part of `f`. * `integral_eq_lintegral_of_nonneg_ae` : `0 ≤ᵐ[μ] f → ∫ x, f x ∂μ = ∫⁻ x, f x ∂μ` 4. `tendsto_integral_of_dominated_convergence` : the Lebesgue dominated convergence theorem ## Notes Some tips on how to prove a proposition if the API for the Bochner integral is not enough so that you need to unfold the definition of the Bochner integral and go back to simple functions. One method is to use the theorem `integrable.induction` in the file `set_integral`, which allows you to prove something for an arbitrary measurable + integrable function. Another method is using the following steps. See `integral_eq_lintegral_max_sub_lintegral_min` for a complicated example, which proves that `∫ f = ∫⁻ f⁺ - ∫⁻ f⁻`, with the first integral sign being the Bochner integral of a real-valued function `f : α → ℝ`, and second and third integral sign being the integral on ennreal-valued functions (called `lintegral`). The proof of `integral_eq_lintegral_max_sub_lintegral_min` is scattered in sections with the name `pos_part`. Here are the usual steps of proving that a property `p`, say `∫ f = ∫⁻ f⁺ - ∫⁻ f⁻`, holds for all functions : 1. First go to the `L¹` space. For example, if you see `ennreal.to_real (∫⁻ a, ennreal.of_real $ ∥f a∥)`, that is the norm of `f` in `L¹` space. Rewrite using `l1.norm_of_fun_eq_lintegral_norm`. 2. Show that the set `{f ∈ L¹ | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻}` is closed in `L¹` using `is_closed_eq`. 3. Show that the property holds for all simple functions `s` in `L¹` space. Typically, you need to convert various notions to their `simple_func` counterpart, using lemmas like `l1.integral_coe_eq_integral`. 4. Since simple functions are dense in `L¹`, ``` univ = closure {s simple} = closure {s simple | ∫ s = ∫⁻ s⁺ - ∫⁻ s⁻} : the property holds for all simple functions ⊆ closure {f | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻} = {f | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻} : closure of a closed set is itself ``` Use `is_closed_property` or `dense_range.induction_on` for this argument. ## Notations * `α →ₛ E` : simple functions (defined in `measure_theory/integration`) * `α →₁[μ] E` : functions in L1 space, i.e., equivalence classes of integrable functions (defined in `measure_theory/l1_space`) * `α →₁ₛ[μ] E` : simple functions in L1 space, i.e., equivalence classes of integrable simple functions Note : `ₛ` is typed using `\_s`. Sometimes it shows as a box if font is missing. ## Tags Bochner integral, simple function, function space, Lebesgue dominated convergence theorem -/ noncomputable theory open_locale classical topological_space big_operators nnreal namespace measure_theory variables {α E : Type*} [measurable_space α] [linear_order E] [has_zero E] local infixr ` →ₛ `:25 := simple_func namespace simple_func section pos_part /-- Positive part of a simple function. -/ def pos_part (f : α →ₛ E) : α →ₛ E := f.map (λb, max b 0) /-- Negative part of a simple function. -/ def neg_part [has_neg E] (f : α →ₛ E) : α →ₛ E := pos_part (-f) lemma pos_part_map_norm (f : α →ₛ ℝ) : (pos_part f).map norm = pos_part f := begin ext, rw [map_apply, real.norm_eq_abs, abs_of_nonneg], rw [pos_part, map_apply], exact le_max_right _ _ end lemma neg_part_map_norm (f : α →ₛ ℝ) : (neg_part f).map norm = neg_part f := by { rw neg_part, exact pos_part_map_norm _ } lemma pos_part_sub_neg_part (f : α →ₛ ℝ) : f.pos_part - f.neg_part = f := begin simp only [pos_part, neg_part], ext a, rw coe_sub, exact max_zero_sub_eq_self (f a) end end pos_part end simple_func end measure_theory namespace measure_theory open set filter topological_space ennreal emetric variables {α E F : Type*} [measurable_space α] local infixr ` →ₛ `:25 := simple_func namespace simple_func section integral /-! ### The Bochner integral of simple functions Define the Bochner integral of simple functions of the type `α →ₛ β` where `β` is a normed group, and prove basic property of this integral. -/ open finset variables [normed_group E] [measurable_space E] [normed_group F] variables {μ : measure α} /-- For simple functions with a `normed_group` as codomain, being integrable is the same as having finite volume support. -/ lemma integrable_iff_fin_meas_supp {f : α →ₛ E} {μ : measure α} : integrable f μ ↔ f.fin_meas_supp μ := calc integrable f μ ↔ ∫⁻ x, f.map (coe ∘ nnnorm : E → ennreal) x ∂μ < ⊤ : and_iff_right f.ae_measurable ... ↔ (f.map (coe ∘ nnnorm : E → ennreal)).lintegral μ < ⊤ : by rw lintegral_eq_lintegral ... ↔ (f.map (coe ∘ nnnorm : E → ennreal)).fin_meas_supp μ : iff.symm $ fin_meas_supp.iff_lintegral_lt_top $ eventually_of_forall $ λ x, coe_lt_top ... ↔ _ : fin_meas_supp.map_iff $ λ b, coe_eq_zero.trans nnnorm_eq_zero lemma fin_meas_supp.integrable {f : α →ₛ E} (h : f.fin_meas_supp μ) : integrable f μ := integrable_iff_fin_meas_supp.2 h lemma integrable_pair [measurable_space F] {f : α →ₛ E} {g : α →ₛ F} : integrable f μ → integrable g μ → integrable (pair f g) μ := by simpa only [integrable_iff_fin_meas_supp] using fin_meas_supp.pair variables [normed_space ℝ F] /-- Bochner integral of simple functions whose codomain is a real `normed_space`. -/ def integral (μ : measure α) (f : α →ₛ F) : F := ∑ x in f.range, (ennreal.to_real (μ (f ⁻¹' {x}))) • x lemma integral_eq_sum_filter (f : α →ₛ F) (μ) : f.integral μ = ∑ x in f.range.filter (λ x, x ≠ 0), (ennreal.to_real (μ (f ⁻¹' {x}))) • x := eq.symm $ sum_filter_of_ne $ λ x _, mt $ λ h0, h0.symm ▸ smul_zero _ /-- The Bochner integral is equal to a sum over any set that includes `f.range` (except `0`). -/ lemma integral_eq_sum_of_subset {f : α →ₛ F} {μ : measure α} {s : finset F} (hs : f.range.filter (λ x, x ≠ 0) ⊆ s) : f.integral μ = ∑ x in s, (μ (f ⁻¹' {x})).to_real • x := begin rw [simple_func.integral_eq_sum_filter, finset.sum_subset hs], rintro x - hx, rw [finset.mem_filter, not_and_distrib, ne.def, not_not] at hx, rcases hx with hx|rfl; [skip, simp], rw [simple_func.mem_range] at hx, rw [preimage_eq_empty]; simp [disjoint_singleton_left, hx] end /-- Calculate the integral of `g ∘ f : α →ₛ F`, where `f` is an integrable function from `α` to `E` and `g` is a function from `E` to `F`. We require `g 0 = 0` so that `g ∘ f` is integrable. -/ lemma map_integral (f : α →ₛ E) (g : E → F) (hf : integrable f μ) (hg : g 0 = 0) : (f.map g).integral μ = ∑ x in f.range, (ennreal.to_real (μ (f ⁻¹' {x}))) • (g x) := begin -- We start as in the proof of `map_lintegral` simp only [integral, range_map], refine finset.sum_image' _ (assume b hb, _), rcases mem_range.1 hb with ⟨a, rfl⟩, rw [map_preimage_singleton, ← sum_measure_preimage_singleton _ (λ _ _, f.is_measurable_preimage _)], -- Now we use `hf : integrable f μ` to show that `ennreal.to_real` is additive. by_cases ha : g (f a) = 0, { simp only [ha, smul_zero], refine (sum_eq_zero $ λ x hx, _).symm, simp only [mem_filter] at hx, simp [hx.2] }, { rw [to_real_sum, sum_smul], { refine sum_congr rfl (λ x hx, _), simp only [mem_filter] at hx, rw [hx.2] }, { intros x hx, simp only [mem_filter] at hx, refine (integrable_iff_fin_meas_supp.1 hf).meas_preimage_singleton_ne_zero _, exact λ h0, ha (hx.2 ▸ h0.symm ▸ hg) } }, end /-- `simple_func.integral` and `simple_func.lintegral` agree when the integrand has type `α →ₛ ennreal`. But since `ennreal` is not a `normed_space`, we need some form of coercion. See `integral_eq_lintegral` for a simpler version. -/ lemma integral_eq_lintegral' {f : α →ₛ E} {g : E → ennreal} (hf : integrable f μ) (hg0 : g 0 = 0) (hgt : ∀b, g b < ⊤): (f.map (ennreal.to_real ∘ g)).integral μ = ennreal.to_real (∫⁻ a, g (f a) ∂μ) := begin have hf' : f.fin_meas_supp μ := integrable_iff_fin_meas_supp.1 hf, simp only [← map_apply g f, lintegral_eq_lintegral], rw [map_integral f _ hf, map_lintegral, ennreal.to_real_sum], { refine finset.sum_congr rfl (λb hb, _), rw [smul_eq_mul, to_real_mul, mul_comm] }, { assume a ha, by_cases a0 : a = 0, { rw [a0, hg0, zero_mul], exact with_top.zero_lt_top }, { apply mul_lt_top (hgt a) (hf'.meas_preimage_singleton_ne_zero a0) } }, { simp [hg0] } end variables [normed_space ℝ E] lemma integral_congr {f g : α →ₛ E} (hf : integrable f μ) (h : f =ᵐ[μ] g): f.integral μ = g.integral μ := show ((pair f g).map prod.fst).integral μ = ((pair f g).map prod.snd).integral μ, from begin have inte := integrable_pair hf (hf.congr h), rw [map_integral (pair f g) _ inte prod.fst_zero, map_integral (pair f g) _ inte prod.snd_zero], refine finset.sum_congr rfl (assume p hp, _), rcases mem_range.1 hp with ⟨a, rfl⟩, by_cases eq : f a = g a, { dsimp only [pair_apply], rw eq }, { have : μ ((pair f g) ⁻¹' {(f a, g a)}) = 0, { refine measure_mono_null (assume a' ha', _) h, simp only [set.mem_preimage, mem_singleton_iff, pair_apply, prod.mk.inj_iff] at ha', show f a' ≠ g a', rwa [ha'.1, ha'.2] }, simp only [this, pair_apply, zero_smul, ennreal.zero_to_real] }, end /-- `simple_func.bintegral` and `simple_func.integral` agree when the integrand has type `α →ₛ ennreal`. But since `ennreal` is not a `normed_space`, we need some form of coercion. -/ lemma integral_eq_lintegral {f : α →ₛ ℝ} (hf : integrable f μ) (h_pos : 0 ≤ᵐ[μ] f) : f.integral μ = ennreal.to_real (∫⁻ a, ennreal.of_real (f a) ∂μ) := begin have : f =ᵐ[μ] f.map (ennreal.to_real ∘ ennreal.of_real) := h_pos.mono (λ a h, (ennreal.to_real_of_real h).symm), rw [← integral_eq_lintegral' hf], { exact integral_congr hf this }, { exact ennreal.of_real_zero }, { assume b, rw ennreal.lt_top_iff_ne_top, exact ennreal.of_real_ne_top } end lemma integral_add {f g : α →ₛ E} (hf : integrable f μ) (hg : integrable g μ) : integral μ (f + g) = integral μ f + integral μ g := calc integral μ (f + g) = ∑ x in (pair f g).range, ennreal.to_real (μ ((pair f g) ⁻¹' {x})) • (x.fst + x.snd) : begin rw [add_eq_map₂, map_integral (pair f g)], { exact integrable_pair hf hg }, { simp only [add_zero, prod.fst_zero, prod.snd_zero] } end ... = ∑ x in (pair f g).range, (ennreal.to_real (μ ((pair f g) ⁻¹' {x})) • x.fst + ennreal.to_real (μ ((pair f g) ⁻¹' {x})) • x.snd) : finset.sum_congr rfl $ assume a ha, smul_add _ _ _ ... = ∑ x in (pair f g).range, ennreal.to_real (μ ((pair f g) ⁻¹' {x})) • x.fst + ∑ x in (pair f g).range, ennreal.to_real (μ ((pair f g) ⁻¹' {x})) • x.snd : by rw finset.sum_add_distrib ... = ((pair f g).map prod.fst).integral μ + ((pair f g).map prod.snd).integral μ : begin rw [map_integral (pair f g), map_integral (pair f g)], { exact integrable_pair hf hg }, { refl }, { exact integrable_pair hf hg }, { refl } end ... = integral μ f + integral μ g : rfl lemma integral_neg {f : α →ₛ E} (hf : integrable f μ) : integral μ (-f) = - integral μ f := calc integral μ (-f) = integral μ (f.map (has_neg.neg)) : rfl ... = - integral μ f : begin rw [map_integral f _ hf neg_zero, integral, ← sum_neg_distrib], refine finset.sum_congr rfl (λx h, smul_neg _ _), end lemma integral_sub [borel_space E] {f g : α →ₛ E} (hf : integrable f μ) (hg : integrable g μ) : integral μ (f - g) = integral μ f - integral μ g := begin rw [sub_eq_add_neg, integral_add hf, integral_neg hg, sub_eq_add_neg], exact hg.neg end lemma integral_smul (r : ℝ) {f : α →ₛ E} (hf : integrable f μ) : integral μ (r • f) = r • integral μ f := calc integral μ (r • f) = ∑ x in f.range, ennreal.to_real (μ (f ⁻¹' {x})) • r • x : by rw [smul_eq_map r f, map_integral f _ hf (smul_zero _)] ... = ∑ x in f.range, ((ennreal.to_real (μ (f ⁻¹' {x}))) * r) • x : finset.sum_congr rfl $ λb hb, by apply smul_smul ... = r • integral μ f : by simp only [integral, smul_sum, smul_smul, mul_comm] lemma norm_integral_le_integral_norm (f : α →ₛ E) (hf : integrable f μ) : ∥f.integral μ∥ ≤ (f.map norm).integral μ := begin rw [map_integral f norm hf norm_zero, integral], calc ∥∑ x in f.range, ennreal.to_real (μ (f ⁻¹' {x})) • x∥ ≤ ∑ x in f.range, ∥ennreal.to_real (μ (f ⁻¹' {x})) • x∥ : norm_sum_le _ _ ... = ∑ x in f.range, ennreal.to_real (μ (f ⁻¹' {x})) • ∥x∥ : begin refine finset.sum_congr rfl (λb hb, _), rw [norm_smul, smul_eq_mul, real.norm_eq_abs, abs_of_nonneg to_real_nonneg] end end lemma integral_add_measure {ν} (f : α →ₛ E) (hf : integrable f (μ + ν)) : f.integral (μ + ν) = f.integral μ + f.integral ν := begin simp only [integral_eq_sum_filter, ← sum_add_distrib, ← add_smul, measure.add_apply], refine sum_congr rfl (λ x hx, _), rw [to_real_add]; refine ne_of_lt ((integrable_iff_fin_meas_supp.1 _).meas_preimage_singleton_ne_zero (mem_filter.1 hx).2), exacts [hf.left_of_add_measure, hf.right_of_add_measure] end end integral end simple_func namespace l1 open ae_eq_fun variables [normed_group E] [second_countable_topology E] [measurable_space E] [borel_space E] [normed_group F] [second_countable_topology F] [measurable_space F] [borel_space F] {μ : measure α} variables (α E μ) -- We use `Type*` instead of `add_subgroup` because otherwise we loose dot notation. /-- `l1.simple_func` is a subspace of L1 consisting of equivalence classes of an integrable simple function. -/ def simple_func : Type* := ↥({ carrier := {f : α →₁[μ] E | ∃ (s : α →ₛ E), (ae_eq_fun.mk s s.ae_measurable : α →ₘ[μ] E) = f}, zero_mem' := ⟨0, rfl⟩, add_mem' := λ f g ⟨s, hs⟩ ⟨t, ht⟩, ⟨s + t, by simp only [coe_add, ← hs, ← ht, mk_add_mk, ← simple_func.coe_add]⟩, neg_mem' := λ f ⟨s, hs⟩, ⟨-s, by simp only [coe_neg, ← hs, neg_mk, ← simple_func.coe_neg]⟩ } : add_subgroup (α →₁[μ] E)) variables {α E μ} notation α ` →₁ₛ[`:25 μ `] ` E := measure_theory.l1.simple_func α E μ namespace simple_func section instances /-! Simple functions in L1 space form a `normed_space`. -/ instance : has_coe (α →₁ₛ[μ] E) (α →₁[μ] E) := coe_subtype instance : has_coe_to_fun (α →₁ₛ[μ] E) := ⟨λ f, α → E, λ f, ⇑(f : α →₁[μ] E)⟩ @[simp, norm_cast] lemma coe_coe (f : α →₁ₛ[μ] E) : ⇑(f : α →₁[μ] E) = f := rfl protected lemma eq {f g : α →₁ₛ[μ] E} : (f : α →₁[μ] E) = (g : α →₁[μ] E) → f = g := subtype.eq protected lemma eq' {f g : α →₁ₛ[μ] E} : (f : α →ₘ[μ] E) = (g : α →ₘ[μ] E) → f = g := subtype.eq ∘ subtype.eq @[norm_cast] protected lemma eq_iff {f g : α →₁ₛ[μ] E} : (f : α →₁[μ] E) = g ↔ f = g := subtype.ext_iff.symm @[norm_cast] protected lemma eq_iff' {f g : α →₁ₛ[μ] E} : (f : α →ₘ[μ] E) = g ↔ f = g := iff.intro (simple_func.eq') (congr_arg _) /-- L1 simple functions forms a `emetric_space`, with the emetric being inherited from L1 space, i.e., `edist f g = ∫⁻ a, edist (f a) (g a)`. Not declared as an instance as `α →₁ₛ[μ] β` will only be useful in the construction of the Bochner integral. -/ protected def emetric_space : emetric_space (α →₁ₛ[μ] E) := subtype.emetric_space /-- L1 simple functions forms a `metric_space`, with the metric being inherited from L1 space, i.e., `dist f g = ennreal.to_real (∫⁻ a, edist (f a) (g a)`). Not declared as an instance as `α →₁ₛ[μ] β` will only be useful in the construction of the Bochner integral. -/ protected def metric_space : metric_space (α →₁ₛ[μ] E) := subtype.metric_space local attribute [instance] simple_func.metric_space simple_func.emetric_space /-- Functions `α →₁ₛ[μ] E` form an additive commutative group. -/ local attribute [instance, priority 10000] protected def add_comm_group : add_comm_group (α →₁ₛ[μ] E) := add_subgroup.to_add_comm_group _ instance : inhabited (α →₁ₛ[μ] E) := ⟨0⟩ @[simp, norm_cast] lemma coe_zero : ((0 : α →₁ₛ[μ] E) : α →₁[μ] E) = 0 := rfl @[simp, norm_cast] lemma coe_add (f g : α →₁ₛ[μ] E) : ((f + g : α →₁ₛ[μ] E) : α →₁[μ] E) = f + g := rfl @[simp, norm_cast] lemma coe_neg (f : α →₁ₛ[μ] E) : ((-f : α →₁ₛ[μ] E) : α →₁[μ] E) = -f := rfl @[simp, norm_cast] lemma coe_sub (f g : α →₁ₛ[μ] E) : ((f - g : α →₁ₛ[μ] E) : α →₁[μ] E) = f - g := rfl @[simp] lemma edist_eq (f g : α →₁ₛ[μ] E) : edist f g = edist (f : α →₁[μ] E) (g : α →₁[μ] E) := rfl @[simp] lemma dist_eq (f g : α →₁ₛ[μ] E) : dist f g = dist (f : α →₁[μ] E) (g : α →₁[μ] E) := rfl /-- The norm on `α →₁ₛ[μ] E` is inherited from L1 space. That is, `∥f∥ = ∫⁻ a, edist (f a) 0`. Not declared as an instance as `α →₁ₛ[μ] E` will only be useful in the construction of the Bochner integral. -/ protected def has_norm : has_norm (α →₁ₛ[μ] E) := ⟨λf, ∥(f : α →₁[μ] E)∥⟩ local attribute [instance] simple_func.has_norm lemma norm_eq (f : α →₁ₛ[μ] E) : ∥f∥ = ∥(f : α →₁[μ] E)∥ := rfl lemma norm_eq' (f : α →₁ₛ[μ] E) : ∥f∥ = ennreal.to_real (edist (f : α →ₘ[μ] E) 0) := rfl /-- Not declared as an instance as `α →₁ₛ[μ] E` will only be useful in the construction of the Bochner integral. -/ protected def normed_group : normed_group (α →₁ₛ[μ] E) := normed_group.of_add_dist (λ x, rfl) $ by { intros, simp only [dist_eq, coe_add, l1.dist_eq, l1.coe_add], rw edist_add_right } variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 E] /-- Not declared as an instance as `α →₁ₛ[μ] E` will only be useful in the construction of the Bochner integral. -/ protected def has_scalar : has_scalar 𝕜 (α →₁ₛ[μ] E) := ⟨λk f, ⟨k • f, begin rcases f with ⟨f, ⟨s, hs⟩⟩, use k • s, rw [coe_smul, subtype.coe_mk, ← hs], refl end ⟩⟩ local attribute [instance, priority 10000] simple_func.has_scalar @[simp, norm_cast] lemma coe_smul (c : 𝕜) (f : α →₁ₛ[μ] E) : ((c • f : α →₁ₛ[μ] E) : α →₁[μ] E) = c • (f : α →₁[μ] E) := rfl /-- Not declared as an instance as `α →₁ₛ[μ] E` will only be useful in the construction of the Bochner integral. -/ protected def semimodule : semimodule 𝕜 (α →₁ₛ[μ] E) := { one_smul := λf, simple_func.eq (by { simp only [coe_smul], exact one_smul _ _ }), mul_smul := λx y f, simple_func.eq (by { simp only [coe_smul], exact mul_smul _ _ _ }), smul_add := λx f g, simple_func.eq (by { simp only [coe_smul, coe_add], exact smul_add _ _ _ }), smul_zero := λx, simple_func.eq (by { simp only [coe_zero, coe_smul], exact smul_zero _ }), add_smul := λx y f, simple_func.eq (by { simp only [coe_smul], exact add_smul _ _ _ }), zero_smul := λf, simple_func.eq (by { simp only [coe_smul], exact zero_smul _ _ }) } local attribute [instance] simple_func.normed_group simple_func.semimodule /-- Not declared as an instance as `α →₁ₛ[μ] E` will only be useful in the construction of the Bochner integral. -/ protected def normed_space : normed_space 𝕜 (α →₁ₛ[μ] E) := ⟨ λc f, by { rw [norm_eq, norm_eq, coe_smul, norm_smul] } ⟩ end instances local attribute [instance] simple_func.normed_group simple_func.normed_space section of_simple_func /-- Construct the equivalence class `[f]` of an integrable simple function `f`. -/ @[reducible] def of_simple_func (f : α →ₛ E) (hf : integrable f μ) : (α →₁ₛ[μ] E) := ⟨l1.of_fun f hf, ⟨f, rfl⟩⟩ lemma of_simple_func_eq_of_fun (f : α →ₛ E) (hf : integrable f μ) : (of_simple_func f hf : α →₁[μ] E) = l1.of_fun f hf := rfl lemma of_simple_func_eq_mk (f : α →ₛ E) (hf : integrable f μ) : (of_simple_func f hf : α →ₘ[μ] E) = ae_eq_fun.mk f f.ae_measurable := rfl lemma of_simple_func_zero : of_simple_func (0 : α →ₛ E) (integrable_zero α E μ) = 0 := rfl lemma of_simple_func_add (f g : α →ₛ E) (hf : integrable f μ) (hg : integrable g μ) : of_simple_func (f + g) (hf.add hg) = of_simple_func f hf + of_simple_func g hg := rfl lemma of_simple_func_neg (f : α →ₛ E) (hf : integrable f μ) : of_simple_func (-f) hf.neg = -of_simple_func f hf := rfl lemma of_simple_func_sub (f g : α →ₛ E) (hf : integrable f μ) (hg : integrable g μ) : of_simple_func (f - g) (hf.sub hg) = of_simple_func f hf - of_simple_func g hg := by { simp only [sub_eq_add_neg, ← of_simple_func_neg, ← of_simple_func_add], refl } variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 E] lemma of_simple_func_smul (f : α →ₛ E) (hf : integrable f μ) (c : 𝕜) : of_simple_func (c • f) (hf.smul c) = c • of_simple_func f hf := rfl lemma norm_of_simple_func (f : α →ₛ E) (hf : integrable f μ) : ∥of_simple_func f hf∥ = ennreal.to_real (∫⁻ a, edist (f a) 0 ∂μ) := rfl end of_simple_func section to_simple_func /-- Find a representative of a `l1.simple_func`. -/ def to_simple_func (f : α →₁ₛ[μ] E) : α →ₛ E := classical.some f.2 /-- `f.to_simple_func` is measurable. -/ protected lemma measurable (f : α →₁ₛ[μ] E) : measurable f.to_simple_func := f.to_simple_func.measurable protected lemma ae_measurable (f : α →₁ₛ[μ] E) : ae_measurable f.to_simple_func μ := f.measurable.ae_measurable /-- `f.to_simple_func` is integrable. -/ protected lemma integrable (f : α →₁ₛ[μ] E) : integrable f.to_simple_func μ := let h := classical.some_spec f.2 in (integrable_mk f.ae_measurable).1 $ h.symm ▸ (f : α →₁[μ] E).2 lemma of_simple_func_to_simple_func (f : α →₁ₛ[μ] E) : of_simple_func (f.to_simple_func) f.integrable = f := by { rw ← simple_func.eq_iff', exact classical.some_spec f.2 } lemma to_simple_func_of_simple_func (f : α →ₛ E) (hfi : integrable f μ) : (of_simple_func f hfi).to_simple_func =ᵐ[μ] f := by { rw ← mk_eq_mk, exact classical.some_spec (of_simple_func f hfi).2 } lemma to_simple_func_eq_to_fun (f : α →₁ₛ[μ] E) : f.to_simple_func =ᵐ[μ] f := begin rw [← of_fun_eq_of_fun f.to_simple_func f f.integrable (f : α →₁[μ] E).integrable, ← l1.eq_iff], simp only [of_fun_eq_mk, ← coe_coe, mk_to_fun], exact classical.some_spec f.coe_prop end variables (α E) lemma zero_to_simple_func : (0 : α →₁ₛ[μ] E).to_simple_func =ᵐ[μ] 0 := begin filter_upwards [to_simple_func_eq_to_fun (0 : α →₁ₛ[μ] E), l1.zero_to_fun α E], assume a h₁ h₂, rwa h₁, end variables {α E} lemma add_to_simple_func (f g : α →₁ₛ[μ] E) : (f + g).to_simple_func =ᵐ[μ] f.to_simple_func + g.to_simple_func := begin filter_upwards [to_simple_func_eq_to_fun (f + g), to_simple_func_eq_to_fun f, to_simple_func_eq_to_fun g, l1.add_to_fun (f : α →₁[μ] E) g], assume a, simp only [← coe_coe, coe_add, pi.add_apply], iterate 4 { assume h, rw h } end lemma neg_to_simple_func (f : α →₁ₛ[μ] E) : (-f).to_simple_func =ᵐ[μ] - f.to_simple_func := begin filter_upwards [to_simple_func_eq_to_fun (-f), to_simple_func_eq_to_fun f, l1.neg_to_fun (f : α →₁[μ] E)], assume a, simp only [pi.neg_apply, coe_neg, ← coe_coe], repeat { assume h, rw h } end lemma sub_to_simple_func (f g : α →₁ₛ[μ] E) : (f - g).to_simple_func =ᵐ[μ] f.to_simple_func - g.to_simple_func := begin filter_upwards [to_simple_func_eq_to_fun (f - g), to_simple_func_eq_to_fun f, to_simple_func_eq_to_fun g, l1.sub_to_fun (f : α →₁[μ] E) g], assume a, simp only [coe_sub, pi.sub_apply, ← coe_coe], repeat { assume h, rw h } end variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 E] lemma smul_to_simple_func (k : 𝕜) (f : α →₁ₛ[μ] E) : (k • f).to_simple_func =ᵐ[μ] k • f.to_simple_func := begin filter_upwards [to_simple_func_eq_to_fun (k • f), to_simple_func_eq_to_fun f, l1.smul_to_fun k (f : α →₁[μ] E)], assume a, simp only [pi.smul_apply, coe_smul, ← coe_coe], repeat { assume h, rw h } end lemma lintegral_edist_to_simple_func_lt_top (f g : α →₁ₛ[μ] E) : ∫⁻ (x : α), edist ((to_simple_func f) x) ((to_simple_func g) x) ∂μ < ⊤ := begin rw lintegral_rw₂ (to_simple_func_eq_to_fun f) (to_simple_func_eq_to_fun g), exact lintegral_edist_to_fun_lt_top _ _ end lemma dist_to_simple_func (f g : α →₁ₛ[μ] E) : dist f g = ennreal.to_real (∫⁻ x, edist (f.to_simple_func x) (g.to_simple_func x) ∂μ) := begin rw [dist_eq, l1.dist_to_fun, ennreal.to_real_eq_to_real], { rw lintegral_rw₂, repeat { exact ae_eq_symm (to_simple_func_eq_to_fun _) } }, { exact l1.lintegral_edist_to_fun_lt_top _ _ }, { exact lintegral_edist_to_simple_func_lt_top _ _ } end lemma norm_to_simple_func (f : α →₁ₛ[μ] E) : ∥f∥ = ennreal.to_real (∫⁻ (a : α), nnnorm ((to_simple_func f) a) ∂μ) := calc ∥f∥ = ennreal.to_real (∫⁻x, edist (f.to_simple_func x) ((0 : α →₁ₛ[μ] E).to_simple_func x) ∂μ) : begin rw [← dist_zero_right, dist_to_simple_func] end ... = ennreal.to_real (∫⁻ (x : α), (coe ∘ nnnorm) (f.to_simple_func x) ∂μ) : begin rw lintegral_nnnorm_eq_lintegral_edist, have : ∫⁻ x, edist ((to_simple_func f) x) ((to_simple_func (0 : α →₁ₛ[μ] E)) x) ∂μ = ∫⁻ x, edist ((to_simple_func f) x) 0 ∂μ, { refine lintegral_congr_ae ((zero_to_simple_func α E).mono (λ a h, _)), rw [h, pi.zero_apply] }, rw [ennreal.to_real_eq_to_real], { exact this }, { exact lintegral_edist_to_simple_func_lt_top _ _ }, { rw ← this, exact lintegral_edist_to_simple_func_lt_top _ _ } end lemma norm_eq_integral (f : α →₁ₛ[μ] E) : ∥f∥ = (f.to_simple_func.map norm).integral μ := -- calc ∥f∥ = ennreal.to_real (∫⁻ (x : α), (coe ∘ nnnorm) (f.to_simple_func x) ∂μ) : -- by { rw norm_to_simple_func } -- ... = (f.to_simple_func.map norm).integral μ : begin rw [norm_to_simple_func, simple_func.integral_eq_lintegral], { simp only [simple_func.map_apply, of_real_norm_eq_coe_nnnorm] }, { exact f.integrable.norm }, { exact eventually_of_forall (λ x, norm_nonneg _) } end end to_simple_func section coe_to_l1 protected lemma uniform_continuous : uniform_continuous (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) := uniform_continuous_comap protected lemma uniform_embedding : uniform_embedding (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) := uniform_embedding_comap subtype.val_injective protected lemma uniform_inducing : uniform_inducing (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) := simple_func.uniform_embedding.to_uniform_inducing protected lemma dense_embedding : dense_embedding (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) := begin apply simple_func.uniform_embedding.dense_embedding, rintros ⟨f, hfi⟩, have A : ae_eq_fun.mk f f.ae_measurable = f := mk_coe_fn _, rw mem_closure_iff_seq_limit, have hfi' := integrable_coe_fn.2 hfi, refine ⟨λ n, ↑(of_simple_func (simple_func.approx_on f f.measurable univ 0 trivial n) (simple_func.integrable_approx_on_univ f.measurable hfi' n)), λ n, mem_range_self _, _⟩, simp only [tendsto_iff_edist_tendsto_0, of_fun_eq_mk, subtype.coe_mk, edist_eq], dsimp, conv in (edist _ _) { congr, skip, rw ← A }, simpa only [edist_mk_mk] using simple_func.tendsto_approx_on_univ_l1_edist f.measurable hfi' end protected lemma dense_inducing : dense_inducing (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) := simple_func.dense_embedding.to_dense_inducing protected lemma dense_range : dense_range (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) := simple_func.dense_inducing.dense variables (𝕜 : Type*) [normed_field 𝕜] [normed_space 𝕜 E] variables (α E) /-- The uniform and dense embedding of L1 simple functions into L1 functions. -/ def coe_to_l1 : (α →₁ₛ[μ] E) →L[𝕜] (α →₁[μ] E) := { to_fun := (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)), map_add' := λf g, rfl, map_smul' := λk f, rfl, cont := l1.simple_func.uniform_continuous.continuous, } variables {α E 𝕜} end coe_to_l1 section pos_part /-- Positive part of a simple function in L1 space. -/ def pos_part (f : α →₁ₛ[μ] ℝ) : α →₁ₛ[μ] ℝ := ⟨l1.pos_part (f : α →₁[μ] ℝ), begin rcases f with ⟨f, s, hsf⟩, use s.pos_part, simp only [subtype.coe_mk, l1.coe_pos_part, ← hsf, ae_eq_fun.pos_part_mk, simple_func.pos_part, simple_func.coe_map] end ⟩ /-- Negative part of a simple function in L1 space. -/ def neg_part (f : α →₁ₛ[μ] ℝ) : α →₁ₛ[μ] ℝ := pos_part (-f) @[norm_cast] lemma coe_pos_part (f : α →₁ₛ[μ] ℝ) : (f.pos_part : α →₁[μ] ℝ) = (f : α →₁[μ] ℝ).pos_part := rfl @[norm_cast] lemma coe_neg_part (f : α →₁ₛ[μ] ℝ) : (f.neg_part : α →₁[μ] ℝ) = (f : α →₁[μ] ℝ).neg_part := rfl end pos_part section simple_func_integral /-! Define the Bochner integral on `α →₁ₛ[μ] E` and prove basic properties of this integral. -/ variables [normed_space ℝ E] /-- The Bochner integral over simple functions in l1 space. -/ def integral (f : α →₁ₛ[μ] E) : E := (f.to_simple_func).integral μ lemma integral_eq_integral (f : α →₁ₛ[μ] E) : integral f = (f.to_simple_func).integral μ := rfl lemma integral_eq_lintegral {f : α →₁ₛ[μ] ℝ} (h_pos : 0 ≤ᵐ[μ] f.to_simple_func) : integral f = ennreal.to_real (∫⁻ a, ennreal.of_real (f.to_simple_func a) ∂μ) := by rw [integral, simple_func.integral_eq_lintegral f.integrable h_pos] lemma integral_congr {f g : α →₁ₛ[μ] E} (h : f.to_simple_func =ᵐ[μ] g.to_simple_func) : integral f = integral g := simple_func.integral_congr f.integrable h lemma integral_add (f g : α →₁ₛ[μ] E) : integral (f + g) = integral f + integral g := begin simp only [integral], rw ← simple_func.integral_add f.integrable g.integrable, apply measure_theory.simple_func.integral_congr (f + g).integrable, apply add_to_simple_func end lemma integral_smul (r : ℝ) (f : α →₁ₛ[μ] E) : integral (r • f) = r • integral f := begin simp only [integral], rw ← simple_func.integral_smul _ f.integrable, apply measure_theory.simple_func.integral_congr (r • f).integrable, apply smul_to_simple_func end lemma norm_integral_le_norm (f : α →₁ₛ[μ] E) : ∥ integral f ∥ ≤ ∥f∥ := begin rw [integral, norm_eq_integral], exact f.to_simple_func.norm_integral_le_integral_norm f.integrable end /-- The Bochner integral over simple functions in l1 space as a continuous linear map. -/ def integral_clm : (α →₁ₛ[μ] E) →L[ℝ] E := linear_map.mk_continuous ⟨integral, integral_add, integral_smul⟩ 1 (λf, le_trans (norm_integral_le_norm _) $ by rw one_mul) local notation `Integral` := @integral_clm α E _ _ _ _ _ μ _ open continuous_linear_map lemma norm_Integral_le_one : ∥Integral∥ ≤ 1 := linear_map.mk_continuous_norm_le _ (zero_le_one) _ section pos_part lemma pos_part_to_simple_func (f : α →₁ₛ[μ] ℝ) : f.pos_part.to_simple_func =ᵐ[μ] f.to_simple_func.pos_part := begin have eq : ∀ a, f.to_simple_func.pos_part a = max (f.to_simple_func a) 0 := λa, rfl, have ae_eq : ∀ᵐ a ∂μ, f.pos_part.to_simple_func a = max (f.to_simple_func a) 0, { filter_upwards [to_simple_func_eq_to_fun f.pos_part, pos_part_to_fun (f : α →₁[μ] ℝ), to_simple_func_eq_to_fun f], assume a h₁ h₂ h₃, rw [h₁, ← coe_coe, coe_pos_part, h₂, coe_coe, ← h₃] }, refine ae_eq.mono (assume a h, _), rw [h, eq] end lemma neg_part_to_simple_func (f : α →₁ₛ[μ] ℝ) : f.neg_part.to_simple_func =ᵐ[μ] f.to_simple_func.neg_part := begin rw [simple_func.neg_part, measure_theory.simple_func.neg_part], filter_upwards [pos_part_to_simple_func (-f), neg_to_simple_func f], assume a h₁ h₂, rw h₁, show max _ _ = max _ _, rw h₂, refl end lemma integral_eq_norm_pos_part_sub (f : α →₁ₛ[μ] ℝ) : f.integral = ∥f.pos_part∥ - ∥f.neg_part∥ := begin -- Convert things in `L¹` to their `simple_func` counterpart have ae_eq₁ : f.to_simple_func.pos_part =ᵐ[μ] (f.pos_part).to_simple_func.map norm, { filter_upwards [pos_part_to_simple_func f], assume a h, rw [simple_func.map_apply, h], conv_lhs { rw [← simple_func.pos_part_map_norm, simple_func.map_apply] } }, -- Convert things in `L¹` to their `simple_func` counterpart have ae_eq₂ : f.to_simple_func.neg_part =ᵐ[μ] (f.neg_part).to_simple_func.map norm, { filter_upwards [neg_part_to_simple_func f], assume a h, rw [simple_func.map_apply, h], conv_lhs { rw [← simple_func.neg_part_map_norm, simple_func.map_apply] } }, -- Convert things in `L¹` to their `simple_func` counterpart have ae_eq : ∀ᵐ a ∂μ, f.to_simple_func.pos_part a - f.to_simple_func.neg_part a = (f.pos_part).to_simple_func.map norm a - (f.neg_part).to_simple_func.map norm a, { filter_upwards [ae_eq₁, ae_eq₂], assume a h₁ h₂, rw [h₁, h₂] }, rw [integral, norm_eq_integral, norm_eq_integral, ← simple_func.integral_sub], { show f.to_simple_func.integral μ = ((f.pos_part.to_simple_func).map norm - f.neg_part.to_simple_func.map norm).integral μ, apply measure_theory.simple_func.integral_congr f.integrable, filter_upwards [ae_eq₁, ae_eq₂], assume a h₁ h₂, show _ = _ - _, rw [← h₁, ← h₂], have := f.to_simple_func.pos_part_sub_neg_part, conv_lhs {rw ← this}, refl }, { exact f.integrable.max_zero.congr ae_eq₁ }, { exact f.integrable.neg.max_zero.congr ae_eq₂ } end end pos_part end simple_func_integral end simple_func open simple_func variables [normed_space ℝ E] [normed_space ℝ F] [complete_space E] section integration_in_l1 local notation `to_l1` := coe_to_l1 α E ℝ local attribute [instance] simple_func.normed_group simple_func.normed_space open continuous_linear_map /-- The Bochner integral in l1 space as a continuous linear map. -/ def integral_clm : (α →₁[μ] E) →L[ℝ] E := integral_clm.extend to_l1 simple_func.dense_range simple_func.uniform_inducing /-- The Bochner integral in l1 space -/ def integral (f : α →₁[μ] E) : E := integral_clm f lemma integral_eq (f : α →₁[μ] E) : integral f = integral_clm f := rfl @[norm_cast] lemma simple_func.integral_l1_eq_integral (f : α →₁ₛ[μ] E) : integral (f : α →₁[μ] E) = f.integral := uniformly_extend_of_ind simple_func.uniform_inducing simple_func.dense_range simple_func.integral_clm.uniform_continuous _ variables (α E) @[simp] lemma integral_zero : integral (0 : α →₁[μ] E) = 0 := map_zero integral_clm variables {α E} lemma integral_add (f g : α →₁[μ] E) : integral (f + g) = integral f + integral g := map_add integral_clm f g lemma integral_neg (f : α →₁[μ] E) : integral (-f) = - integral f := map_neg integral_clm f lemma integral_sub (f g : α →₁[μ] E) : integral (f - g) = integral f - integral g := map_sub integral_clm f g lemma integral_smul (r : ℝ) (f : α →₁[μ] E) : integral (r • f) = r • integral f := map_smul r integral_clm f local notation `Integral` := @integral_clm α E _ _ _ _ _ μ _ _ local notation `sIntegral` := @simple_func.integral_clm α E _ _ _ _ _ μ _ lemma norm_Integral_le_one : ∥Integral∥ ≤ 1 := calc ∥Integral∥ ≤ (1 : ℝ≥0) * ∥sIntegral∥ : op_norm_extend_le _ _ _ $ λs, by {rw [nnreal.coe_one, one_mul], refl} ... = ∥sIntegral∥ : one_mul _ ... ≤ 1 : norm_Integral_le_one lemma norm_integral_le (f : α →₁[μ] E) : ∥integral f∥ ≤ ∥f∥ := calc ∥integral f∥ = ∥Integral f∥ : rfl ... ≤ ∥Integral∥ * ∥f∥ : le_op_norm _ _ ... ≤ 1 * ∥f∥ : mul_le_mul_of_nonneg_right norm_Integral_le_one $ norm_nonneg _ ... = ∥f∥ : one_mul _ @[continuity] lemma continuous_integral : continuous (λ (f : α →₁[μ] E), f.integral) := by simp [l1.integral, l1.integral_clm.continuous] section pos_part lemma integral_eq_norm_pos_part_sub (f : α →₁[μ] ℝ) : integral f = ∥pos_part f∥ - ∥neg_part f∥ := begin -- Use `is_closed_property` and `is_closed_eq` refine @is_closed_property _ _ _ (coe : (α →₁ₛ[μ] ℝ) → (α →₁[μ] ℝ)) (λ f : α →₁[μ] ℝ, integral f = ∥pos_part f∥ - ∥neg_part f∥) l1.simple_func.dense_range (is_closed_eq _ _) _ f, { exact cont _ }, { refine continuous.sub (continuous_norm.comp l1.continuous_pos_part) (continuous_norm.comp l1.continuous_neg_part) }, -- Show that the property holds for all simple functions in the `L¹` space. { assume s, norm_cast, rw [← simple_func.norm_eq, ← simple_func.norm_eq], exact simple_func.integral_eq_norm_pos_part_sub _} end end pos_part end integration_in_l1 end l1 variables [normed_group E] [second_countable_topology E] [normed_space ℝ E] [complete_space E] [measurable_space E] [borel_space E] [normed_group F] [second_countable_topology F] [normed_space ℝ F] [complete_space F] [measurable_space F] [borel_space F] /-- The Bochner integral -/ def integral (μ : measure α) (f : α → E) : E := if hf : integrable f μ then (l1.of_fun f hf).integral else 0 /-! In the notation for integrals, an expression like `∫ x, g ∥x∥ ∂μ` will not be parsed correctly, and needs parentheses. We do not set the binding power of `r` to `0`, because then `∫ x, f x = 0` will be parsed incorrectly. -/ notation `∫` binders `, ` r:(scoped:60 f, f) ` ∂` μ:70 := integral μ r notation `∫` binders `, ` r:(scoped:60 f, integral volume f) := r notation `∫` binders ` in ` s `, ` r:(scoped:60 f, f) ` ∂` μ:70 := integral (measure.restrict μ s) r notation `∫` binders ` in ` s `, ` r:(scoped:60 f, integral (measure.restrict volume s) f) := r section properties open continuous_linear_map measure_theory.simple_func variables {f g : α → E} {μ : measure α} lemma integral_eq (f : α → E) (hf : integrable f μ) : ∫ a, f a ∂μ = (l1.of_fun f hf).integral := dif_pos hf lemma l1.integral_eq_integral (f : α →₁[μ] E) : f.integral = ∫ a, f a ∂μ := by rw [integral_eq, l1.of_fun_to_fun] lemma integral_undef (h : ¬ integrable f μ) : ∫ a, f a ∂μ = 0 := dif_neg h lemma integral_non_ae_measurable (h : ¬ ae_measurable f μ) : ∫ a, f a ∂μ = 0 := integral_undef $ not_and_of_not_left _ h variables (α E) lemma integral_zero : ∫ a : α, (0:E) ∂μ = 0 := by rw [integral_eq, l1.of_fun_zero, l1.integral_zero] @[simp] lemma integral_zero' : integral μ (0 : α → E) = 0 := integral_zero α E variables {α E} lemma integral_add (hf : integrable f μ) (hg : integrable g μ) : ∫ a, f a + g a ∂μ = ∫ a, f a ∂μ + ∫ a, g a ∂μ := by { rw [integral_eq, integral_eq f hf, integral_eq g hg, ← l1.integral_add, ← l1.of_fun_add], refl } lemma integral_add' (hf : integrable f μ) (hg : integrable g μ) : ∫ a, (f + g) a ∂μ = ∫ a, f a ∂μ + ∫ a, g a ∂μ := integral_add hf hg lemma integral_neg (f : α → E) : ∫ a, -f a ∂μ = - ∫ a, f a ∂μ := begin by_cases hf : integrable f μ, { rw [integral_eq f hf, integral_eq (λa, - f a) hf.neg, ← l1.integral_neg, ← l1.of_fun_neg], refl }, { rw [integral_undef hf, integral_undef, neg_zero], rwa [← integrable_neg_iff] at hf } end lemma integral_neg' (f : α → E) : ∫ a, (-f) a ∂μ = - ∫ a, f a ∂μ := integral_neg f lemma integral_sub (hf : integrable f μ) (hg : integrable g μ) : ∫ a, f a - g a ∂μ = ∫ a, f a ∂μ - ∫ a, g a ∂μ := by { simp only [sub_eq_add_neg, ← integral_neg], exact integral_add hf hg.neg } lemma integral_sub' (hf : integrable f μ) (hg : integrable g μ) : ∫ a, (f - g) a ∂μ = ∫ a, f a ∂μ - ∫ a, g a ∂μ := integral_sub hf hg lemma integral_smul (r : ℝ) (f : α → E) : ∫ a, r • (f a) ∂μ = r • ∫ a, f a ∂μ := begin by_cases hf : integrable f μ, { rw [integral_eq f hf, integral_eq (λa, r • (f a)), l1.of_fun_smul, l1.integral_smul] }, { by_cases hr : r = 0, { simp only [hr, measure_theory.integral_zero, zero_smul] }, have hf' : ¬ integrable (λ x, r • f x) μ, { change ¬ integrable (r • f) μ, rwa [integrable_smul_iff hr f] }, rw [integral_undef hf, integral_undef hf', smul_zero] } end lemma integral_mul_left (r : ℝ) (f : α → ℝ) : ∫ a, r * (f a) ∂μ = r * ∫ a, f a ∂μ := integral_smul r f lemma integral_mul_right (r : ℝ) (f : α → ℝ) : ∫ a, (f a) * r ∂μ = ∫ a, f a ∂μ * r := by { simp only [mul_comm], exact integral_mul_left r f } lemma integral_div (r : ℝ) (f : α → ℝ) : ∫ a, (f a) / r ∂μ = ∫ a, f a ∂μ / r := integral_mul_right r⁻¹ f lemma integral_congr_ae (h : f =ᵐ[μ] g) : ∫ a, f a ∂μ = ∫ a, g a ∂μ := begin by_cases hfi : integrable f μ, { have hgi : integrable g μ := hfi.congr h, rw [integral_eq f hfi, integral_eq g hgi, (l1.of_fun_eq_of_fun f g hfi hgi).2 h] }, { have hgi : ¬ integrable g μ, { rw integrable_congr h at hfi, exact hfi }, rw [integral_undef hfi, integral_undef hgi] }, end @[simp] lemma l1.integral_of_fun_eq_integral {f : α → E} (hf : integrable f μ) : ∫ a, (l1.of_fun f hf) a ∂μ = ∫ a, f a ∂μ := integral_congr_ae (l1.to_fun_of_fun f hf) @[continuity] lemma continuous_integral : continuous (λ (f : α →₁[μ] E), ∫ a, f a ∂μ) := by { simp only [← l1.integral_eq_integral], exact l1.continuous_integral } lemma norm_integral_le_lintegral_norm (f : α → E) : ∥∫ a, f a ∂μ∥ ≤ ennreal.to_real (∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ) := begin by_cases hf : integrable f μ, { rw [integral_eq f hf, ← l1.norm_of_fun_eq_lintegral_norm f hf], exact l1.norm_integral_le _ }, { rw [integral_undef hf, norm_zero], exact to_real_nonneg } end lemma ennnorm_integral_le_lintegral_ennnorm (f : α → E) : (nnnorm (∫ a, f a ∂μ) : ennreal) ≤ ∫⁻ a, (nnnorm (f a)) ∂μ := by { simp_rw [← of_real_norm_eq_coe_nnnorm], apply ennreal.of_real_le_of_le_to_real, exact norm_integral_le_lintegral_norm f } lemma integral_eq_zero_of_ae {f : α → E} (hf : f =ᵐ[μ] 0) : ∫ a, f a ∂μ = 0 := if hfm : ae_measurable f μ then by simp [integral_congr_ae hf, integral_zero] else integral_non_ae_measurable hfm /-- If `f` has finite integral, then `∫ x in s, f x ∂μ` is absolutely continuous in `s`: it tends to zero as `μ s` tends to zero. -/ lemma has_finite_integral.tendsto_set_integral_nhds_zero {ι} {f : α → E} (hf : has_finite_integral f μ) {l : filter ι} {s : ι → set α} (hs : tendsto (μ ∘ s) l (𝓝 0)) : tendsto (λ i, ∫ x in s i, f x ∂μ) l (𝓝 0) := begin rw [tendsto_zero_iff_norm_tendsto_zero], simp_rw [← coe_nnnorm, ← nnreal.coe_zero, nnreal.tendsto_coe, ← ennreal.tendsto_coe, ennreal.coe_zero], exact tendsto_of_tendsto_of_tendsto_of_le_of_le tendsto_const_nhds (tendsto_set_lintegral_zero hf hs) (λ i, zero_le _) (λ i, ennnorm_integral_le_lintegral_ennnorm _) end /-- If `f` is integrable, then `∫ x in s, f x ∂μ` is absolutely continuous in `s`: it tends to zero as `μ s` tends to zero. -/ lemma integrable.tendsto_set_integral_nhds_zero {ι} {f : α → E} (hf : integrable f μ) {l : filter ι} {s : ι → set α} (hs : tendsto (μ ∘ s) l (𝓝 0)) : tendsto (λ i, ∫ x in s i, f x ∂μ) l (𝓝 0) := hf.2.tendsto_set_integral_nhds_zero hs /-- If `F i → f` in `L1`, then `∫ x, F i x ∂μ → ∫ x, f x∂μ`. -/ lemma tendsto_integral_of_l1 {ι} (f : α → E) (hfi : integrable f μ) {F : ι → α → E} {l : filter ι} (hFi : ∀ᶠ i in l, integrable (F i) μ) (hF : tendsto (λ i, ∫⁻ x, edist (F i x) (f x) ∂μ) l (𝓝 0)) : tendsto (λ i, ∫ x, F i x ∂μ) l (𝓝 $ ∫ x, f x ∂μ) := begin rw [tendsto_iff_norm_tendsto_zero], replace hF : tendsto (λ i, ennreal.to_real $ ∫⁻ x, edist (F i x) (f x) ∂μ) l (𝓝 0) := (ennreal.tendsto_to_real zero_ne_top).comp hF, refine squeeze_zero_norm' (hFi.mp $ hFi.mono $ λ i hFi hFm, _) hF, simp only [norm_norm, ← integral_sub hFi hfi, edist_dist, dist_eq_norm], apply norm_integral_le_lintegral_norm end /-- Lebesgue dominated convergence theorem provides sufficient conditions under which almost everywhere convergence of a sequence of functions implies the convergence of their integrals. -/ theorem tendsto_integral_of_dominated_convergence {F : ℕ → α → E} {f : α → E} (bound : α → ℝ) (F_measurable : ∀ n, ae_measurable (F n) μ) (f_measurable : ae_measurable f μ) (bound_integrable : integrable bound μ) (h_bound : ∀ n, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) : tendsto (λn, ∫ a, F n a ∂μ) at_top (𝓝 $ ∫ a, f a ∂μ) := begin /- To show `(∫ a, F n a) --> (∫ f)`, suffices to show `∥∫ a, F n a - ∫ f∥ --> 0` -/ rw tendsto_iff_norm_tendsto_zero, /- But `0 ≤ ∥∫ a, F n a - ∫ f∥ = ∥∫ a, (F n a - f a) ∥ ≤ ∫ a, ∥F n a - f a∥, and thus we apply the sandwich theorem and prove that `∫ a, ∥F n a - f a∥ --> 0` -/ have lintegral_norm_tendsto_zero : tendsto (λn, ennreal.to_real $ ∫⁻ a, (ennreal.of_real ∥F n a - f a∥) ∂μ) at_top (𝓝 0) := (tendsto_to_real zero_ne_top).comp (tendsto_lintegral_norm_of_dominated_convergence F_measurable f_measurable bound_integrable.has_finite_integral h_bound h_lim), -- Use the sandwich theorem refine squeeze_zero (λ n, norm_nonneg _) _ lintegral_norm_tendsto_zero, -- Show `∥∫ a, F n a - ∫ f∥ ≤ ∫ a, ∥F n a - f a∥` for all `n` { assume n, have h₁ : integrable (F n) μ := bound_integrable.mono' (F_measurable n) (h_bound _), have h₂ : integrable f μ := ⟨f_measurable, has_finite_integral_of_dominated_convergence bound_integrable.has_finite_integral h_bound h_lim⟩, rw ← integral_sub h₁ h₂, exact norm_integral_le_lintegral_norm _ } end /-- Lebesgue dominated convergence theorem for filters with a countable basis -/ lemma tendsto_integral_filter_of_dominated_convergence {ι} {l : filter ι} {F : ι → α → E} {f : α → E} (bound : α → ℝ) (hl_cb : l.is_countably_generated) (hF_meas : ∀ᶠ n in l, ae_measurable (F n) μ) (f_measurable : ae_measurable f μ) (h_bound : ∀ᶠ n in l, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a) (bound_integrable : integrable bound μ) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) l (𝓝 (f a))) : tendsto (λn, ∫ a, F n a ∂μ) l (𝓝 $ ∫ a, f a ∂μ) := begin rw hl_cb.tendsto_iff_seq_tendsto, { intros x xl, have hxl, { rw tendsto_at_top' at xl, exact xl }, have h := inter_mem_sets hF_meas h_bound, replace h := hxl _ h, rcases h with ⟨k, h⟩, rw ← tendsto_add_at_top_iff_nat k, refine tendsto_integral_of_dominated_convergence _ _ _ _ _ _, { exact bound }, { intro, refine (h _ _).1, exact nat.le_add_left _ _ }, { assumption }, { assumption }, { intro, refine (h _ _).2, exact nat.le_add_left _ _ }, { filter_upwards [h_lim], assume a h_lim, apply @tendsto.comp _ _ _ (λn, x (n + k)) (λn, F n a), { assumption }, rw tendsto_add_at_top_iff_nat, assumption } }, end /-- The Bochner integral of a real-valued function `f : α → ℝ` is the difference between the integral of the positive part of `f` and the integral of the negative part of `f`. -/ lemma integral_eq_lintegral_max_sub_lintegral_min {f : α → ℝ} (hf : integrable f μ) : ∫ a, f a ∂μ = ennreal.to_real (∫⁻ a, (ennreal.of_real $ max (f a) 0) ∂μ) - ennreal.to_real (∫⁻ a, (ennreal.of_real $ - min (f a) 0) ∂μ) := let f₁ : α →₁[μ] ℝ := l1.of_fun f hf in -- Go to the `L¹` space have eq₁ : ennreal.to_real (∫⁻ a, (ennreal.of_real $ max (f a) 0) ∂μ) = ∥l1.pos_part f₁∥ := begin rw l1.norm_eq_norm_to_fun, congr' 1, apply lintegral_congr_ae, filter_upwards [l1.pos_part_to_fun f₁, l1.to_fun_of_fun f hf], assume a h₁ h₂, rw [h₁, h₂, real.norm_eq_abs, abs_of_nonneg], exact le_max_right _ _ end, -- Go to the `L¹` space have eq₂ : ennreal.to_real (∫⁻ a, (ennreal.of_real $ -min (f a) 0) ∂μ) = ∥l1.neg_part f₁∥ := begin rw l1.norm_eq_norm_to_fun, congr' 1, apply lintegral_congr_ae, filter_upwards [l1.neg_part_to_fun_eq_min f₁, l1.to_fun_of_fun f hf], assume a h₁ h₂, rw [h₁, h₂, real.norm_eq_abs, abs_of_nonneg], rw [neg_nonneg], exact min_le_right _ _ end, begin rw [eq₁, eq₂, integral, dif_pos], exact l1.integral_eq_norm_pos_part_sub _ end lemma integral_eq_lintegral_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hfm : ae_measurable f μ) : ∫ a, f a ∂μ = ennreal.to_real (∫⁻ a, (ennreal.of_real $ f a) ∂μ) := begin by_cases hfi : integrable f μ, { rw integral_eq_lintegral_max_sub_lintegral_min hfi, have h_min : ∫⁻ a, ennreal.of_real (-min (f a) 0) ∂μ = 0, { rw lintegral_eq_zero_iff', { refine hf.mono _, simp only [pi.zero_apply], assume a h, simp only [min_eq_right h, neg_zero, ennreal.of_real_zero] }, { exact measurable_of_real.comp_ae_measurable (measurable_id.neg.comp_ae_measurable $ hfm.min ae_measurable_const) } }, have h_max : ∫⁻ a, ennreal.of_real (max (f a) 0) ∂μ = ∫⁻ a, ennreal.of_real (f a) ∂μ, { refine lintegral_congr_ae (hf.mono (λ a h, _)), rw [pi.zero_apply] at h, rw max_eq_left h }, rw [h_min, h_max, zero_to_real, _root_.sub_zero] }, { rw integral_undef hfi, simp_rw [integrable, hfm, has_finite_integral_iff_norm, lt_top_iff_ne_top, ne.def, true_and, not_not] at hfi, have : ∫⁻ (a : α), ennreal.of_real (f a) ∂μ = ∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ, { refine lintegral_congr_ae (hf.mono $ assume a h, _), rw [real.norm_eq_abs, abs_of_nonneg h] }, rw [this, hfi], refl } end lemma integral_nonneg_of_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) : 0 ≤ ∫ a, f a ∂μ := begin by_cases hfm : ae_measurable f μ, { rw integral_eq_lintegral_of_nonneg_ae hf hfm, exact to_real_nonneg }, { rw integral_non_ae_measurable hfm } end lemma lintegral_coe_eq_integral (f : α → ℝ≥0) (hfi : integrable (λ x, (f x : real)) μ) : ∫⁻ a, f a ∂μ = ennreal.of_real ∫ a, f a ∂μ := begin simp_rw [integral_eq_lintegral_of_nonneg_ae (eventually_of_forall (λ x, (f x).coe_nonneg)) hfi.ae_measurable, ← ennreal.coe_nnreal_eq], rw [ennreal.of_real_to_real], rw [← lt_top_iff_ne_top], convert hfi.has_finite_integral, ext1 x, rw [real.nnnorm_coe_eq_self] end lemma integral_to_real {f : α → ennreal} (hfm : ae_measurable f μ) (hf : ∀ᵐ x ∂μ, f x < ⊤) : ∫ a, (f a).to_real ∂μ = (∫⁻ a, f a ∂μ).to_real := begin rw [integral_eq_lintegral_of_nonneg_ae _ hfm.to_real], { rw lintegral_congr_ae, refine hf.mp (eventually_of_forall _), intros x hx, rw [lt_top_iff_ne_top] at hx, simp [hx] }, { exact (eventually_of_forall $ λ x, ennreal.to_real_nonneg) } end lemma integral_nonneg {f : α → ℝ} (hf : 0 ≤ f) : 0 ≤ ∫ a, f a ∂μ := integral_nonneg_of_ae $ eventually_of_forall hf lemma integral_nonpos_of_ae {f : α → ℝ} (hf : f ≤ᵐ[μ] 0) : ∫ a, f a ∂μ ≤ 0 := begin have hf : 0 ≤ᵐ[μ] (-f) := hf.mono (assume a h, by rwa [pi.neg_apply, pi.zero_apply, neg_nonneg]), have : 0 ≤ ∫ a, -f a ∂μ := integral_nonneg_of_ae hf, rwa [integral_neg, neg_nonneg] at this, end lemma integral_nonpos {f : α → ℝ} (hf : f ≤ 0) : ∫ a, f a ∂μ ≤ 0 := integral_nonpos_of_ae $ eventually_of_forall hf lemma integral_eq_zero_iff_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hfi : integrable f μ) : ∫ x, f x ∂μ = 0 ↔ f =ᵐ[μ] 0 := by simp_rw [integral_eq_lintegral_of_nonneg_ae hf hfi.1, ennreal.to_real_eq_zero_iff, lintegral_eq_zero_iff' (ennreal.measurable_of_real.comp_ae_measurable hfi.1), ← ennreal.not_lt_top, ← has_finite_integral_iff_of_real hf, hfi.2, not_true, or_false, ← hf.le_iff_eq, filter.eventually_eq, filter.eventually_le, (∘), pi.zero_apply, ennreal.of_real_eq_zero] lemma integral_eq_zero_iff_of_nonneg {f : α → ℝ} (hf : 0 ≤ f) (hfi : integrable f μ) : ∫ x, f x ∂μ = 0 ↔ f =ᵐ[μ] 0 := integral_eq_zero_iff_of_nonneg_ae (eventually_of_forall hf) hfi lemma integral_pos_iff_support_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hfi : integrable f μ) : (0 < ∫ x, f x ∂μ) ↔ 0 < μ (function.support f) := by simp_rw [(integral_nonneg_of_ae hf).lt_iff_ne, pos_iff_ne_zero, ne.def, @eq_comm ℝ 0, integral_eq_zero_iff_of_nonneg_ae hf hfi, filter.eventually_eq, ae_iff, pi.zero_apply, function.support] lemma integral_pos_iff_support_of_nonneg {f : α → ℝ} (hf : 0 ≤ f) (hfi : integrable f μ) : (0 < ∫ x, f x ∂μ) ↔ 0 < μ (function.support f) := integral_pos_iff_support_of_nonneg_ae (eventually_of_forall hf) hfi section normed_group variables {H : Type*} [normed_group H] [second_countable_topology H] [measurable_space H] [borel_space H] lemma l1.norm_eq_integral_norm (f : α →₁[μ] H) : ∥f∥ = ∫ a, ∥f a∥ ∂μ := by rw [l1.norm_eq_norm_to_fun, integral_eq_lintegral_of_nonneg_ae (eventually_of_forall $ by simp [norm_nonneg]) (continuous_norm.measurable.comp_ae_measurable f.ae_measurable)] lemma l1.norm_of_fun_eq_integral_norm {f : α → H} (hf : integrable f μ) : ∥ l1.of_fun f hf ∥ = ∫ a, ∥f a∥ ∂μ := begin rw l1.norm_eq_integral_norm, refine integral_congr_ae _, apply (l1.to_fun_of_fun f hf).mono, intros a ha, simp [ha] end end normed_group lemma integral_mono_ae {f g : α → ℝ} (hf : integrable f μ) (hg : integrable g μ) (h : f ≤ᵐ[μ] g) : ∫ a, f a ∂μ ≤ ∫ a, g a ∂μ := le_of_sub_nonneg $ integral_sub hg hf ▸ integral_nonneg_of_ae $ h.mono (λ a, sub_nonneg_of_le) lemma integral_mono {f g : α → ℝ} (hf : integrable f μ) (hg : integrable g μ) (h : f ≤ g) : ∫ a, f a ∂μ ≤ ∫ a, g a ∂μ := integral_mono_ae hf hg $ eventually_of_forall h lemma integral_mono_of_nonneg {f g : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hgi : integrable g μ) (h : f ≤ᵐ[μ] g) : ∫ a, f a ∂μ ≤ ∫ a, g a ∂μ := begin by_cases hfm : ae_measurable f μ, { refine integral_mono_ae ⟨hfm, _⟩ hgi h, refine (hgi.has_finite_integral.mono $ h.mp $ hf.mono $ λ x hf hfg, _), simpa [real.norm_eq_abs, abs_of_nonneg hf, abs_of_nonneg (le_trans hf hfg)] }, { rw [integral_non_ae_measurable hfm], exact integral_nonneg_of_ae (hf.trans h) } end lemma norm_integral_le_integral_norm (f : α → E) : ∥(∫ a, f a ∂μ)∥ ≤ ∫ a, ∥f a∥ ∂μ := have le_ae : ∀ᵐ a ∂μ, 0 ≤ ∥f a∥ := eventually_of_forall (λa, norm_nonneg _), classical.by_cases ( λh : ae_measurable f μ, calc ∥∫ a, f a ∂μ∥ ≤ ennreal.to_real (∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ) : norm_integral_le_lintegral_norm _ ... = ∫ a, ∥f a∥ ∂μ : (integral_eq_lintegral_of_nonneg_ae le_ae $ ae_measurable.norm h).symm ) ( λh : ¬ae_measurable f μ, begin rw [integral_non_ae_measurable h, norm_zero], exact integral_nonneg_of_ae le_ae end ) lemma norm_integral_le_of_norm_le {f : α → E} {g : α → ℝ} (hg : integrable g μ) (h : ∀ᵐ x ∂μ, ∥f x∥ ≤ g x) : ∥∫ x, f x ∂μ∥ ≤ ∫ x, g x ∂μ := calc ∥∫ x, f x ∂μ∥ ≤ ∫ x, ∥f x∥ ∂μ : norm_integral_le_integral_norm f ... ≤ ∫ x, g x ∂μ : integral_mono_of_nonneg (eventually_of_forall $ λ x, norm_nonneg _) hg h lemma integral_finset_sum {ι} (s : finset ι) {f : ι → α → E} (hf : ∀ i, integrable (f i) μ) : ∫ a, ∑ i in s, f i a ∂μ = ∑ i in s, ∫ a, f i a ∂μ := begin refine finset.induction_on s _ _, { simp only [integral_zero, finset.sum_empty] }, { assume i s his ih, simp only [his, finset.sum_insert, not_false_iff], rw [integral_add (hf _) (integrable_finset_sum s hf), ih] } end lemma simple_func.integral_eq_integral (f : α →ₛ E) (hfi : integrable f μ) : f.integral μ = ∫ x, f x ∂μ := begin rw [integral_eq f hfi, ← l1.simple_func.of_simple_func_eq_of_fun, l1.simple_func.integral_l1_eq_integral, l1.simple_func.integral_eq_integral], exact simple_func.integral_congr hfi (l1.simple_func.to_simple_func_of_simple_func _ _).symm end @[simp] lemma integral_const (c : E) : ∫ x : α, c ∂μ = (μ univ).to_real • c := begin by_cases hμ : μ univ < ⊤, { haveI : finite_measure μ := ⟨hμ⟩, calc ∫ x : α, c ∂μ = (simple_func.const α c).integral μ : ((simple_func.const α c).integral_eq_integral (integrable_const _)).symm ... = _ : _, rw [simple_func.integral], by_cases ha : nonempty α, { resetI, simp [preimage_const_of_mem] }, { simp [μ.eq_zero_of_not_nonempty ha] } }, { by_cases hc : c = 0, { simp [hc, integral_zero] }, { have : ¬integrable (λ x : α, c) μ, { simp only [integrable_const_iff, not_or_distrib], exact ⟨hc, hμ⟩ }, simp only [not_lt, top_le_iff] at hμ, simp [integral_undef, *] } } end lemma norm_integral_le_of_norm_le_const [finite_measure μ] {f : α → E} {C : ℝ} (h : ∀ᵐ x ∂μ, ∥f x∥ ≤ C) : ∥∫ x, f x ∂μ∥ ≤ C * (μ univ).to_real := calc ∥∫ x, f x ∂μ∥ ≤ ∫ x, C ∂μ : norm_integral_le_of_norm_le (integrable_const C) h ... = C * (μ univ).to_real : by rw [integral_const, smul_eq_mul, mul_comm] lemma tendsto_integral_approx_on_univ_of_measurable {f : α → E} (fmeas : measurable f) (hf : integrable f μ) : tendsto (λ n, (simple_func.approx_on f fmeas univ 0 trivial n).integral μ) at_top (𝓝 $ ∫ x, f x ∂μ) := begin have : tendsto (λ n, ∫ x, simple_func.approx_on f fmeas univ 0 trivial n x ∂μ) at_top (𝓝 $ ∫ x, f x ∂μ) := tendsto_integral_of_l1 _ hf (eventually_of_forall $ simple_func.integrable_approx_on_univ fmeas hf) (simple_func.tendsto_approx_on_univ_l1_edist fmeas hf), simpa only [simple_func.integral_eq_integral, simple_func.integrable_approx_on_univ fmeas hf] end variable {ν : measure α} private lemma integral_add_measure_of_measurable {f : α → E} (fmeas : measurable f) (hμ : integrable f μ) (hν : integrable f ν) : ∫ x, f x ∂(μ + ν) = ∫ x, f x ∂μ + ∫ x, f x ∂ν := begin have hfi := hμ.add_measure hν, refine tendsto_nhds_unique (tendsto_integral_approx_on_univ_of_measurable fmeas hfi) _, simpa only [simple_func.integral_add_measure _ (simple_func.integrable_approx_on_univ fmeas hfi _)] using (tendsto_integral_approx_on_univ_of_measurable fmeas hμ).add (tendsto_integral_approx_on_univ_of_measurable fmeas hν) end lemma integral_add_measure {f : α → E} (hμ : integrable f μ) (hν : integrable f ν) : ∫ x, f x ∂(μ + ν) = ∫ x, f x ∂μ + ∫ x, f x ∂ν := begin have h : ae_measurable f (μ + ν) := hμ.ae_measurable.add_measure hν.ae_measurable, let g := h.mk f, have A : f =ᵐ[μ + ν] g := h.ae_eq_mk, have B : f =ᵐ[μ] g := A.filter_mono (ae_mono (measure.le_add_right (le_refl μ))), have C : f =ᵐ[ν] g := A.filter_mono (ae_mono (measure.le_add_left (le_refl ν))), calc ∫ x, f x ∂(μ + ν) = ∫ x, g x ∂(μ + ν) : integral_congr_ae A ... = ∫ x, g x ∂μ + ∫ x, g x ∂ν : integral_add_measure_of_measurable h.measurable_mk ((integrable_congr B).1 hμ) ((integrable_congr C).1 hν) ... = ∫ x, f x ∂μ + ∫ x, f x ∂ν : by { congr' 1, { exact integral_congr_ae B.symm }, { exact integral_congr_ae C.symm } } end @[simp] lemma integral_zero_measure (f : α → E) : ∫ x, f x ∂0 = 0 := norm_le_zero_iff.1 $ le_trans (norm_integral_le_lintegral_norm f) $ by simp private lemma integral_smul_measure_aux {f : α → E} {c : ennreal} (h0 : 0 < c) (hc : c < ⊤) (fmeas : measurable f) (hfi : integrable f μ) : ∫ x, f x ∂(c • μ) = c.to_real • ∫ x, f x ∂μ := begin refine tendsto_nhds_unique _ (tendsto_const_nhds.smul (tendsto_integral_approx_on_univ_of_measurable fmeas hfi)), convert tendsto_integral_approx_on_univ_of_measurable fmeas (hfi.smul_measure hc), simp only [simple_func.integral, measure.smul_apply, finset.smul_sum, smul_smul, ennreal.to_real_mul] end @[simp] lemma integral_smul_measure (f : α → E) (c : ennreal) : ∫ x, f x ∂(c • μ) = c.to_real • ∫ x, f x ∂μ := begin -- First we consider “degenerate” cases: -- `c = 0` rcases (zero_le c).eq_or_lt with rfl|h0, { simp }, -- `f` is not almost everywhere measurable by_cases hfm : ae_measurable f μ, swap, { have : ¬ (ae_measurable f (c • μ)), by simpa [ne_of_gt h0] using hfm, simp [integral_non_ae_measurable, hfm, this] }, -- `c = ⊤` rcases (le_top : c ≤ ⊤).eq_or_lt with rfl|hc, { rw [ennreal.top_to_real, zero_smul], by_cases hf : f =ᵐ[μ] 0, { have : f =ᵐ[⊤ • μ] 0 := ae_smul_measure hf ⊤, exact integral_eq_zero_of_ae this }, { apply integral_undef, rw [integrable, has_finite_integral, iff_true_intro (hfm.smul_measure ⊤), true_and, lintegral_smul_measure, top_mul, if_neg], { apply lt_irrefl }, { rw [lintegral_eq_zero_iff' hfm.ennnorm], refine λ h, hf (h.mono $ λ x, _), simp } } }, -- `f` is not integrable and `0 < c < ⊤` by_cases hfi : integrable f μ, swap, { rw [integral_undef hfi, smul_zero], refine integral_undef (mt (λ h, _) hfi), convert h.smul_measure (ennreal.inv_lt_top.2 h0), rw [smul_smul, ennreal.inv_mul_cancel (ne_of_gt h0) (ne_of_lt hc), one_smul] }, -- Main case: `0 < c < ⊤`, `f` is almost everywhere measurable and integrable let g := hfm.mk f, calc ∫ x, f x ∂(c • μ) = ∫ x, g x ∂(c • μ) : integral_congr_ae $ ae_smul_measure hfm.ae_eq_mk c ... = c.to_real • ∫ x, g x ∂μ : integral_smul_measure_aux h0 hc hfm.measurable_mk $ hfi.congr hfm.ae_eq_mk ... = c.to_real • ∫ x, f x ∂μ : by { congr' 1, exact integral_congr_ae (hfm.ae_eq_mk.symm) } end lemma integral_map_of_measurable {β} [measurable_space β] {φ : α → β} (hφ : measurable φ) {f : β → E} (hfm : measurable f) : ∫ y, f y ∂(measure.map φ μ) = ∫ x, f (φ x) ∂μ := begin by_cases hfi : integrable f (measure.map φ μ), swap, { rw [integral_undef hfi, integral_undef], rwa [← integrable_map_measure hfm.ae_measurable hφ] }, refine tendsto_nhds_unique (tendsto_integral_approx_on_univ_of_measurable hfm hfi) _, convert tendsto_integral_approx_on_univ_of_measurable (hfm.comp hφ) ((integrable_map_measure hfm.ae_measurable hφ).1 hfi), ext1 i, simp only [simple_func.approx_on_comp, simple_func.integral, measure.map_apply, hφ, simple_func.is_measurable_preimage, ← preimage_comp, simple_func.coe_comp], refine (finset.sum_subset (simple_func.range_comp_subset_range _ hφ) (λ y _ hy, _)).symm, rw [simple_func.mem_range, ← set.preimage_singleton_eq_empty, simple_func.coe_comp] at hy, simp [hy] end lemma integral_map {β} [measurable_space β] {φ : α → β} (hφ : measurable φ) {f : β → E} (hfm : ae_measurable f (measure.map φ μ)) : ∫ y, f y ∂(measure.map φ μ) = ∫ x, f (φ x) ∂μ := let g := hfm.mk f in calc ∫ y, f y ∂(measure.map φ μ) = ∫ y, g y ∂(measure.map φ μ) : integral_congr_ae hfm.ae_eq_mk ... = ∫ x, g (φ x) ∂μ : integral_map_of_measurable hφ hfm.measurable_mk ... = ∫ x, f (φ x) ∂μ : integral_congr_ae $ ae_eq_comp hφ (hfm.ae_eq_mk).symm lemma integral_dirac' (f : α → E) (a : α) (hfm : measurable f) : ∫ x, f x ∂(measure.dirac a) = f a := calc ∫ x, f x ∂(measure.dirac a) = ∫ x, f a ∂(measure.dirac a) : integral_congr_ae $ ae_eq_dirac' hfm ... = f a : by simp [measure.dirac_apply_of_mem] lemma integral_dirac [measurable_singleton_class α] (f : α → E) (a : α) : ∫ x, f x ∂(measure.dirac a) = f a := calc ∫ x, f x ∂(measure.dirac a) = ∫ x, f a ∂(measure.dirac a) : integral_congr_ae $ ae_eq_dirac f ... = f a : by simp [measure.dirac_apply_of_mem] end properties mk_simp_attribute integral_simps "Simp set for integral rules." attribute [integral_simps] integral_neg integral_smul l1.integral_add l1.integral_sub l1.integral_smul l1.integral_neg attribute [irreducible] integral l1.integral end measure_theory
39da077eeb00144e6045b99d3c5dc3eabb2e9a3c
2a70b774d16dbdf5a533432ee0ebab6838df0948
/_target/deps/mathlib/src/linear_algebra/determinant.lean
ce9642b3c4dc1caddef5b9f4655fd0b6de355b92
[ "Apache-2.0" ]
permissive
hjvromen/lewis
40b035973df7c77ebf927afab7878c76d05ff758
105b675f73630f028ad5d890897a51b3c1146fb0
refs/heads/master
1,677,944,636,343
1,676,555,301,000
1,676,555,301,000
327,553,599
0
0
null
null
null
null
UTF-8
Lean
false
false
12,655
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Chris Hughes, Tim Baanen -/ import data.matrix.pequiv import data.fintype.card import group_theory.perm.sign import algebra.algebra.basic import tactic.ring import linear_algebra.alternating universes u v w z open equiv equiv.perm finset function namespace matrix open_locale matrix big_operators variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [comm_ring R] local notation `ε` σ:max := ((sign σ : ℤ ) : R) /-- The determinant of a matrix given by the Leibniz formula. -/ definition det (M : matrix n n R) : R := ∑ σ : perm n, ε σ * ∏ i, M (σ i) i @[simp] lemma det_diagonal {d : n → R} : det (diagonal d) = ∏ i, d i := begin refine (finset.sum_eq_single 1 _ _).trans _, { intros σ h1 h2, cases not_forall.1 (mt equiv.ext h2) with x h3, convert mul_zero _, apply finset.prod_eq_zero, { change x ∈ _, simp }, exact if_neg h3 }, { simp }, { simp } end @[simp] lemma det_zero (h : nonempty n) : det (0 : matrix n n R) = 0 := by rw [← diagonal_zero, det_diagonal, finset.prod_const, ← fintype.card, zero_pow (fintype.card_pos_iff.2 h)] @[simp] lemma det_one : det (1 : matrix n n R) = 1 := by rw [← diagonal_one]; simp [-diagonal_one] lemma det_eq_one_of_card_eq_zero {A : matrix n n R} (h : fintype.card n = 0) : det A = 1 := begin have perm_eq : (univ : finset (perm n)) = {1} := univ_eq_singleton_of_card_one (1 : perm n) (by simp [card_univ, fintype.card_perm, h]), simp [det, card_eq_zero.mp h, perm_eq], end lemma det_mul_aux {M N : matrix n n R} {p : n → n} (H : ¬bijective p) : ∑ σ : perm n, (ε σ) * ∏ x, (M (σ x) (p x) * N (p x) x) = 0 := begin obtain ⟨i, j, hpij, hij⟩ : ∃ i j, p i = p j ∧ i ≠ j, { rw [← fintype.injective_iff_bijective, injective] at H, push_neg at H, exact H }, exact sum_involution (λ σ _, σ * swap i j) (λ σ _, have ∏ x, M (σ x) (p x) = ∏ x, M ((σ * swap i j) x) (p x), from prod_bij (λ a _, swap i j a) (λ _ _, mem_univ _) (by simp [apply_swap_eq_self hpij]) (λ _ _ _ _ h, (swap i j).injective h) (λ b _, ⟨swap i j b, mem_univ _, by simp⟩), by simp [this, sign_swap hij, prod_mul_distrib]) (λ σ _ _, (not_congr mul_swap_eq_iff).mpr hij) (λ _ _, mem_univ _) (λ σ _, mul_swap_involutive i j σ) end @[simp] lemma det_mul (M N : matrix n n R) : det (M ⬝ N) = det M * det N := calc det (M ⬝ N) = ∑ p : n → n, ∑ σ : perm n, ε σ * ∏ i, (M (σ i) (p i) * N (p i) i) : by simp only [det, mul_apply, prod_univ_sum, mul_sum, fintype.pi_finset_univ]; rw [finset.sum_comm] ... = ∑ p in (@univ (n → n) _).filter bijective, ∑ σ : perm n, ε σ * ∏ i, (M (σ i) (p i) * N (p i) i) : eq.symm $ sum_subset (filter_subset _ _) (λ f _ hbij, det_mul_aux $ by simpa using hbij) ... = ∑ τ : perm n, ∑ σ : perm n, ε σ * ∏ i, (M (σ i) (τ i) * N (τ i) i) : sum_bij (λ p h, equiv.of_bijective p (mem_filter.1 h).2) (λ _ _, mem_univ _) (λ _ _, rfl) (λ _ _ _ _ h, by injection h) (λ b _, ⟨b, mem_filter.2 ⟨mem_univ _, b.bijective⟩, injective_coe_fn rfl⟩) ... = ∑ σ : perm n, ∑ τ : perm n, (∏ i, N (σ i) i) * ε τ * (∏ j, M (τ j) (σ j)) : by simp [mul_sum, det, mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc] ... = ∑ σ : perm n, ∑ τ : perm n, (((∏ i, N (σ i) i) * (ε σ * ε τ)) * ∏ i, M (τ i) i) : sum_congr rfl (λ σ _, sum_bij (λ τ _, τ * σ⁻¹) (λ _ _, mem_univ _) (λ τ _, have ∏ j, M (τ j) (σ j) = ∏ j, M ((τ * σ⁻¹) j) j, by rw ← σ⁻¹.prod_comp; simp [mul_apply], have h : ε σ * ε (τ * σ⁻¹) = ε τ := calc ε σ * ε (τ * σ⁻¹) = ε ((τ * σ⁻¹) * σ) : by rw [mul_comm, sign_mul (τ * σ⁻¹)]; simp ... = ε τ : by simp, by rw h; simp [this, mul_comm, mul_assoc, mul_left_comm]) (λ _ _ _ _, mul_right_cancel) (λ τ _, ⟨τ * σ, by simp⟩)) ... = det M * det N : by simp [det, mul_assoc, mul_sum, mul_comm, mul_left_comm] instance : is_monoid_hom (det : matrix n n R → R) := { map_one := det_one, map_mul := det_mul } /-- Transposing a matrix preserves the determinant. -/ @[simp] lemma det_transpose (M : matrix n n R) : Mᵀ.det = M.det := begin apply sum_bij (λ σ _, σ⁻¹), { intros σ _, apply mem_univ }, { intros σ _, rw [sign_inv], congr' 1, apply prod_bij (λ i _, σ i), { intros i _, apply mem_univ }, { intros i _, simp }, { intros i j _ _ h, simp at h, assumption }, { intros i _, use σ⁻¹ i, finish } }, { intros σ σ' _ _ h, simp at h, assumption }, { intros σ _, use σ⁻¹, finish } end /-- The determinant of a permutation matrix equals its sign. -/ @[simp] lemma det_permutation (σ : perm n) : matrix.det (σ.to_pequiv.to_matrix : matrix n n R) = σ.sign := begin suffices : matrix.det (σ.to_pequiv.to_matrix) = ↑σ.sign * det (1 : matrix n n R), { simp [this] }, unfold det, rw mul_sum, apply sum_bij (λ τ _, σ * τ), { intros τ _, apply mem_univ }, { intros τ _, rw [←mul_assoc, sign_mul, coe_coe, ←int.cast_mul, ←units.coe_mul, ←mul_assoc, int.units_mul_self, one_mul], congr, ext i, apply pequiv.equiv_to_pequiv_to_matrix }, { intros τ τ' _ _, exact (mul_right_inj σ).mp }, { intros τ _, use σ⁻¹ * τ, use (mem_univ _), exact (mul_inv_cancel_left _ _).symm } end /-- Permuting the columns changes the sign of the determinant. -/ lemma det_permute (σ : perm n) (M : matrix n n R) : matrix.det (λ i, M (σ i)) = σ.sign * M.det := by rw [←det_permutation, ←det_mul, pequiv.to_pequiv_mul_matrix] @[simp] lemma det_smul {A : matrix n n R} {c : R} : det (c • A) = c ^ fintype.card n * det A := calc det (c • A) = det (matrix.mul (diagonal (λ _, c)) A) : by rw [smul_eq_diagonal_mul] ... = det (diagonal (λ _, c)) * det A : det_mul _ _ ... = c ^ fintype.card n * det A : by simp [card_univ] section hom_map variables {S : Type w} [comm_ring S] lemma ring_hom.map_det {M : matrix n n R} {f : R →+* S} : f M.det = matrix.det (f.map_matrix M) := by simp [matrix.det, f.map_sum, f.map_prod] lemma alg_hom.map_det [algebra R S] {T : Type z} [comm_ring T] [algebra R T] {M : matrix n n S} {f : S →ₐ[R] T} : f M.det = matrix.det ((f : S →+* T).map_matrix M) := by rw [← alg_hom.coe_to_ring_hom, ring_hom.map_det] end hom_map section det_zero /-! ### `det_zero` section Prove that a matrix with a repeated column has determinant equal to zero. -/ lemma det_eq_zero_of_row_eq_zero {A : matrix n n R} (i : n) (h : ∀ j, A i j = 0) : det A = 0 := begin rw [←det_transpose, det], convert @sum_const_zero _ _ (univ : finset (perm n)) _, ext σ, convert mul_zero ↑(sign σ), apply prod_eq_zero (mem_univ i), rw [transpose_apply], apply h end lemma det_eq_zero_of_column_eq_zero {A : matrix n n R} (j : n) (h : ∀ i, A i j = 0) : det A = 0 := by { rw ← det_transpose, exact det_eq_zero_of_row_eq_zero j h, } variables {M : matrix n n R} {i j : n} /-- If a matrix has a repeated row, the determinant will be zero. -/ theorem det_zero_of_row_eq (i_ne_j : i ≠ j) (hij : M i = M j) : M.det = 0 := begin apply finset.sum_involution (λ σ _, swap i j * σ) (λ σ _, _) (λ σ _ _, (not_congr swap_mul_eq_iff).mpr i_ne_j) (λ σ _, finset.mem_univ _) (λ σ _, swap_mul_involutive i j σ), convert add_right_neg (↑↑(sign σ) * ∏ i, M (σ i) i), rw neg_mul_eq_neg_mul, congr, { rw [sign_mul, sign_swap i_ne_j], norm_num }, { ext j, rw [perm.mul_apply, apply_swap_eq_self hij], } end end det_zero lemma det_update_column_add (M : matrix n n R) (j : n) (u v : n → R) : det (update_column M j $ u + v) = det (update_column M j u) + det (update_column M j v) := begin simp only [det], have : ∀ σ : perm n, ∏ i, M.update_column j (u + v) (σ i) i = ∏ i, M.update_column j u (σ i) i + ∏ i, M.update_column j v (σ i) i, { intros σ, simp only [update_column_apply, prod_ite, filter_eq', finset.prod_singleton, finset.mem_univ, if_true, pi.add_apply, add_mul] }, rw [← sum_add_distrib], apply sum_congr rfl, intros x _, rw [this, mul_add] end lemma det_update_row_add (M : matrix n n R) (j : n) (u v : n → R) : det (update_row M j $ u + v) = det (update_row M j u) + det (update_row M j v) := begin rw [← det_transpose, ← update_column_transpose, det_update_column_add], simp [update_column_transpose, det_transpose] end lemma det_update_column_smul (M : matrix n n R) (j : n) (s : R) (u : n → R) : det (update_column M j $ s • u) = s * det (update_column M j u) := begin simp only [det], have : ∀ σ : perm n, ∏ i, M.update_column j (s • u) (σ i) i = s * ∏ i, M.update_column j u (σ i) i, { intros σ, simp only [update_column_apply, prod_ite, filter_eq', finset.prod_singleton, finset.mem_univ, if_true, algebra.id.smul_eq_mul, pi.smul_apply], ring }, rw mul_sum, apply sum_congr rfl, intros x _, rw this, ring end lemma det_update_row_smul (M : matrix n n R) (j : n) (s : R) (u : n → R) : det (update_row M j $ s • u) = s * det (update_row M j u) := begin rw [← det_transpose, ← update_column_transpose, det_update_column_smul], simp [update_column_transpose, det_transpose] end /-- `det` is an alternating multilinear map over the rows of the matrix. See also `is_basis.det`. -/ @[simps apply] def det_row_multilinear : alternating_map R (n → R) R n:= { to_fun := det, map_add' := det_update_row_add, map_smul' := det_update_row_smul, map_eq_zero_of_eq' := λ M i j h hij, det_zero_of_row_eq hij h } @[simp] lemma det_block_diagonal {o : Type*} [fintype o] [decidable_eq o] (M : o → matrix n n R) : (block_diagonal M).det = ∏ k, (M k).det := begin -- Rewrite the determinants as a sum over permutations. unfold det, -- The right hand side is a product of sums, rewrite it as a sum of products. rw finset.prod_sum, simp_rw [finset.mem_univ, finset.prod_attach_univ, finset.univ_pi_univ], -- We claim that the only permutations contributing to the sum are those that -- preserve their second component. let preserving_snd : finset (equiv.perm (n × o)) := finset.univ.filter (λ σ, ∀ x, (σ x).snd = x.snd), have mem_preserving_snd : ∀ {σ : equiv.perm (n × o)}, σ ∈ preserving_snd ↔ ∀ x, (σ x).snd = x.snd := λ σ, finset.mem_filter.trans ⟨λ h, h.2, λ h, ⟨finset.mem_univ _, h⟩⟩, rw ← finset.sum_subset (finset.subset_univ preserving_snd) _, -- And that these are in bijection with `o → equiv.perm m`. rw (finset.sum_bij (λ (σ : ∀ (k : o), k ∈ finset.univ → equiv.perm n) _, prod_congr_left (λ k, σ k (finset.mem_univ k))) _ _ _ _).symm, { intros σ _, rw mem_preserving_snd, rintros ⟨k, x⟩, simp }, { intros σ _, rw [finset.prod_mul_distrib, ←finset.univ_product_univ, finset.prod_product, finset.prod_comm], simp [sign_prod_congr_left] }, { intros σ σ' _ _ eq, ext x hx k, simp only at eq, have : ∀ k x, prod_congr_left (λ k, σ k (finset.mem_univ _)) (k, x) = prod_congr_left (λ k, σ' k (finset.mem_univ _)) (k, x) := λ k x, by rw eq, simp only [prod_congr_left_apply, prod.mk.inj_iff] at this, exact (this k x).1 }, { intros σ hσ, rw mem_preserving_snd at hσ, have hσ' : ∀ x, (σ⁻¹ x).snd = x.snd, { intro x, conv_rhs { rw [← perm.apply_inv_self σ x, hσ] } }, have mk_apply_eq : ∀ k x, ((σ (x, k)).fst, k) = σ (x, k), { intros k x, ext; simp [hσ] }, have mk_inv_apply_eq : ∀ k x, ((σ⁻¹ (x, k)).fst, k) = σ⁻¹ (x, k), { intros k x, conv_lhs { rw ← perm.apply_inv_self σ (x, k) }, ext; simp [hσ'] }, refine ⟨λ k _, ⟨λ x, (σ (x, k)).fst, λ x, (σ⁻¹ (x, k)).fst, _, _⟩, _, _⟩, { intro x, simp [mk_apply_eq, mk_inv_apply_eq] }, { intro x, simp [mk_apply_eq, mk_inv_apply_eq] }, { apply finset.mem_univ }, { ext ⟨k, x⟩; simp [hσ] } }, { intros σ _ hσ, rw mem_preserving_snd at hσ, obtain ⟨⟨k, x⟩, hkx⟩ := not_forall.mp hσ, rw [finset.prod_eq_zero (finset.mem_univ (k, x)), mul_zero], rw [← @prod.mk.eta _ _ (σ (k, x)), block_diagonal_apply_ne], exact hkx } end end matrix
d57c34e9284536535c79c94bb1bfe5495a2ec12d
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/172.lean
5024fc714b42cfea3fe3b294ceb7a512015c5d57
[ "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
267
lean
-- see issue: https://github.com/leanprover-community/lean/issues/172 instance its_a_monad : monad option := { pure := λ _, some , bind := λ α β x f, option.rec_on x option.none f} #eval @has_bind.bind option (@monad.to_has_bind _ its_a_monad) _ _ (some 4) some
258e26ae5d583d4ff89ca711ebe8523cc03f190e
367134ba5a65885e863bdc4507601606690974c1
/src/analysis/normed_space/dual.lean
40a99fa39322a1dbcb2fd9ce425241bdf1de7cf9
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
11,850
lean
/- Copyright (c) 2020 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth, Frédéric Dupuis -/ import analysis.normed_space.hahn_banach import analysis.normed_space.inner_product /-! # The topological dual of a normed space In this file we define the topological dual of a normed space, and the bounded linear map from a normed space into its double dual. We also prove that, for base field `𝕜` with `[is_R_or_C 𝕜]`, this map is an isometry. We then consider inner product spaces, with base field over `ℝ` (the corresponding results for `ℂ` will require the definition of conjugate-linear maps). We define `to_dual_map`, a continuous linear map from `E` to its dual, which maps an element `x` of the space to `λ y, ⟪x, y⟫`. We check (`to_dual_map_isometry`) that this map is an isometry onto its image, and particular is injective. We also define `to_dual'` as the function taking taking a vector to its dual for a base field `𝕜` with `[is_R_or_C 𝕜]`; this is a function and not a linear map. Finally, under the hypothesis of completeness (i.e., for Hilbert spaces), we prove the Fréchet-Riesz representation (`to_dual_map_eq_top`), which states the surjectivity: every element of the dual of a Hilbert space `E` has the form `λ u, ⟪x, u⟫` for some `x : E`. This permits the map `to_dual_map` to be upgraded to an (isometric) continuous linear equivalence, `to_dual`, between a Hilbert space and its dual. ## References * [M. Einsiedler and T. Ward, *Functional Analysis, Spectral Theory, and Applications*] [EinsiedlerWard2017] ## Tags dual, Fréchet-Riesz -/ noncomputable theory open_locale classical universes u v namespace normed_space section general variables (𝕜 : Type*) [nondiscrete_normed_field 𝕜] variables (E : Type*) [normed_group E] [normed_space 𝕜 E] /-- The topological dual of a normed space `E`. -/ @[derive [has_coe_to_fun, normed_group, normed_space 𝕜]] def dual := E →L[𝕜] 𝕜 instance : inhabited (dual 𝕜 E) := ⟨0⟩ /-- The inclusion of a normed space in its double (topological) dual. -/ def inclusion_in_double_dual' (x : E) : (dual 𝕜 (dual 𝕜 E)) := linear_map.mk_continuous { to_fun := λ f, f x, map_add' := by simp, map_smul' := by simp } ∥x∥ (λ f, by { rw mul_comm, exact f.le_op_norm x } ) @[simp] lemma dual_def (x : E) (f : dual 𝕜 E) : ((inclusion_in_double_dual' 𝕜 E) x) f = f x := rfl lemma double_dual_bound (x : E) : ∥(inclusion_in_double_dual' 𝕜 E) x∥ ≤ ∥x∥ := begin apply continuous_linear_map.op_norm_le_bound, { simp }, { intros f, rw mul_comm, exact f.le_op_norm x, } end /-- The inclusion of a normed space in its double (topological) dual, considered as a bounded linear map. -/ def inclusion_in_double_dual : E →L[𝕜] (dual 𝕜 (dual 𝕜 E)) := linear_map.mk_continuous { to_fun := λ (x : E), (inclusion_in_double_dual' 𝕜 E) x, map_add' := λ x y, by { ext, simp }, map_smul' := λ (c : 𝕜) x, by { ext, simp } } 1 (λ x, by { convert double_dual_bound _ _ _, simp } ) end general section bidual_isometry variables {𝕜 : Type v} [is_R_or_C 𝕜] {E : Type u} [normed_group E] [normed_space 𝕜 E] /-- If one controls the norm of every `f x`, then one controls the norm of `x`. Compare `continuous_linear_map.op_norm_le_bound`. -/ lemma norm_le_dual_bound (x : E) {M : ℝ} (hMp: 0 ≤ M) (hM : ∀ (f : dual 𝕜 E), ∥f x∥ ≤ M * ∥f∥) : ∥x∥ ≤ M := begin classical, by_cases h : x = 0, { simp only [h, hMp, norm_zero] }, { obtain ⟨f, hf⟩ : ∃ g : E →L[𝕜] 𝕜, _ := exists_dual_vector x h, calc ∥x∥ = ∥norm' 𝕜 x∥ : (norm_norm' _ _ _).symm ... = ∥f x∥ : by rw hf.2 ... ≤ M * ∥f∥ : hM f ... = M : by rw [hf.1, mul_one] } end /-- The inclusion of a normed space in its double dual is an isometry onto its image.-/ lemma inclusion_in_double_dual_isometry (x : E) : ∥inclusion_in_double_dual 𝕜 E x∥ = ∥x∥ := begin apply le_antisymm, { exact double_dual_bound 𝕜 E x }, { rw continuous_linear_map.norm_def, apply real.lb_le_Inf _ continuous_linear_map.bounds_nonempty, rintros c ⟨hc1, hc2⟩, exact norm_le_dual_bound x hc1 hc2 }, end end bidual_isometry end normed_space namespace inner_product_space open is_R_or_C continuous_linear_map section is_R_or_C variables (𝕜 : Type*) variables {E : Type*} [is_R_or_C 𝕜] [inner_product_space 𝕜 E] local notation `⟪`x`, `y`⟫` := @inner 𝕜 E _ x y local postfix `†`:90 := @is_R_or_C.conj 𝕜 _ /-- Given some `x` in an inner product space, we can define its dual as the continuous linear map `λ y, ⟪x, y⟫`. Consider using `to_dual` or `to_dual_map` instead in the real case. -/ def to_dual' : E →+ normed_space.dual 𝕜 E := { to_fun := λ x, linear_map.mk_continuous { to_fun := λ y, ⟪x, y⟫, map_add' := λ _ _, inner_add_right, map_smul' := λ _ _, inner_smul_right } ∥x∥ (λ y, by { rw [is_R_or_C.norm_eq_abs], exact abs_inner_le_norm _ _ }), map_zero' := by { ext z, simp }, map_add' := λ x y, by { ext z, simp [inner_add_left] } } @[simp] lemma to_dual'_apply {x y : E} : to_dual' 𝕜 x y = ⟪x, y⟫ := rfl /-- In an inner product space, the norm of the dual of a vector `x` is `∥x∥` -/ @[simp] lemma norm_to_dual'_apply (x : E) : ∥to_dual' 𝕜 x∥ = ∥x∥ := begin refine le_antisymm _ _, { exact linear_map.mk_continuous_norm_le _ (norm_nonneg _) _ }, { cases eq_or_lt_of_le (norm_nonneg x) with h h, { have : x = 0 := norm_eq_zero.mp (eq.symm h), simp [this] }, { refine (mul_le_mul_right h).mp _, calc ∥x∥ * ∥x∥ = ∥x∥ ^ 2 : by ring ... = re ⟪x, x⟫ : norm_sq_eq_inner _ ... ≤ abs ⟪x, x⟫ : re_le_abs _ ... = ∥to_dual' 𝕜 x x∥ : by simp [norm_eq_abs] ... ≤ ∥to_dual' 𝕜 x∥ * ∥x∥ : le_op_norm (to_dual' 𝕜 x) x } } end variables (E) lemma to_dual'_isometry : isometry (@to_dual' 𝕜 E _ _) := add_monoid_hom.isometry_of_norm _ (norm_to_dual'_apply 𝕜) /-- Fréchet-Riesz representation: any `ℓ` in the dual of a Hilbert space `E` is of the form `λ u, ⟪y, u⟫` for some `y : E`, i.e. `to_dual'` is surjective. -/ lemma to_dual'_surjective [complete_space E] : function.surjective (@to_dual' 𝕜 E _ _) := begin intros ℓ, set Y := ker ℓ with hY, by_cases htriv : Y = ⊤, { have hℓ : ℓ = 0, { have h' := linear_map.ker_eq_top.mp htriv, rw [←coe_zero] at h', apply coe_injective, exact h' }, exact ⟨0, by simp [hℓ]⟩ }, { have Ycomplete := is_complete_ker ℓ, rw [← submodule.orthogonal_eq_bot_iff Ycomplete, ←hY] at htriv, change Yᗮ ≠ ⊥ at htriv, rw [submodule.ne_bot_iff] at htriv, obtain ⟨z : E, hz : z ∈ Yᗮ, z_ne_0 : z ≠ 0⟩ := htriv, refine ⟨((ℓ z)† / ⟪z, z⟫) • z, _⟩, ext x, have h₁ : (ℓ z) • x - (ℓ x) • z ∈ Y, { rw [mem_ker, map_sub, map_smul, map_smul, algebra.id.smul_eq_mul, algebra.id.smul_eq_mul, mul_comm], exact sub_self (ℓ x * ℓ z) }, have h₂ : (ℓ z) * ⟪z, x⟫ = (ℓ x) * ⟪z, z⟫, { have h₃ := calc 0 = ⟪z, (ℓ z) • x - (ℓ x) • z⟫ : by { rw [(Y.mem_orthogonal' z).mp hz], exact h₁ } ... = ⟪z, (ℓ z) • x⟫ - ⟪z, (ℓ x) • z⟫ : by rw [inner_sub_right] ... = (ℓ z) * ⟪z, x⟫ - (ℓ x) * ⟪z, z⟫ : by simp [inner_smul_right], exact sub_eq_zero.mp (eq.symm h₃) }, have h₄ := calc ⟪((ℓ z)† / ⟪z, z⟫) • z, x⟫ = (ℓ z) / ⟪z, z⟫ * ⟪z, x⟫ : by simp [inner_smul_left, conj_div, conj_conj] ... = (ℓ z) * ⟪z, x⟫ / ⟪z, z⟫ : by rw [←div_mul_eq_mul_div] ... = (ℓ x) * ⟪z, z⟫ / ⟪z, z⟫ : by rw [h₂] ... = ℓ x : begin have : ⟪z, z⟫ ≠ 0, { change z = 0 → false at z_ne_0, rwa ←inner_self_eq_zero at z_ne_0 }, field_simp [this] end, exact h₄ } end end is_R_or_C section real variables {F : Type*} [inner_product_space ℝ F] /-- In a real inner product space `F`, the function that takes a vector `x` in `F` to its dual `λ y, ⟪x, y⟫` is a continuous linear map. If the space is complete (i.e. is a Hilbert space), consider using `to_dual` instead. -/ -- TODO extend to `is_R_or_C` (requires a definition of conjugate linear maps) def to_dual_map : F →L[ℝ] (normed_space.dual ℝ F) := linear_map.mk_continuous { to_fun := to_dual' ℝ, map_add' := λ x y, by { ext, simp [inner_add_left] }, map_smul' := λ c x, by { ext, simp [inner_smul_left] } } 1 (λ x, by simp only [norm_to_dual'_apply, one_mul, linear_map.coe_mk]) @[simp] lemma to_dual_map_apply {x y : F} : to_dual_map x y = ⟪x, y⟫_ℝ := rfl /-- In an inner product space, the norm of the dual of a vector `x` is `∥x∥` -/ @[simp] lemma norm_to_dual_map_apply (x : F) : ∥to_dual_map x∥ = ∥x∥ := norm_to_dual'_apply _ _ lemma to_dual_map_isometry : isometry (@to_dual_map F _) := add_monoid_hom.isometry_of_norm _ norm_to_dual_map_apply lemma to_dual_map_injective : function.injective (@to_dual_map F _) := to_dual_map_isometry.injective @[simp] lemma ker_to_dual_map : (@to_dual_map F _).ker = ⊥ := linear_map.ker_eq_bot.mpr to_dual_map_injective @[simp] lemma to_dual_map_eq_iff_eq {x y : F} : to_dual_map x = to_dual_map y ↔ x = y := ((linear_map.ker_eq_bot).mp (@ker_to_dual_map F _)).eq_iff variables [complete_space F] /-- Fréchet-Riesz representation: any `ℓ` in the dual of a real Hilbert space `F` is of the form `λ u, ⟪y, u⟫` for some `y` in `F`. See `inner_product_space.to_dual` for the continuous linear equivalence thus induced. -/ -- TODO extend to `is_R_or_C` (requires a definition of conjugate linear maps) lemma range_to_dual_map : (@to_dual_map F _).range = ⊤ := linear_map.range_eq_top.mpr (to_dual'_surjective ℝ F) /-- Fréchet-Riesz representation: If `F` is a Hilbert space, the function that takes a vector in `F` to its dual is a continuous linear equivalence. -/ def to_dual : F ≃L[ℝ] (normed_space.dual ℝ F) := continuous_linear_equiv.of_isometry to_dual_map.to_linear_map to_dual_map_isometry range_to_dual_map /-- Fréchet-Riesz representation: If `F` is a Hilbert space, the function that takes a vector in `F` to its dual is an isometry. -/ def isometric.to_dual : F ≃ᵢ normed_space.dual ℝ F := { to_equiv := to_dual.to_linear_equiv.to_equiv, isometry_to_fun := to_dual'_isometry ℝ F} @[simp] lemma to_dual_apply {x y : F} : to_dual x y = ⟪x, y⟫_ℝ := rfl @[simp] lemma to_dual_eq_iff_eq {x y : F} : to_dual x = to_dual y ↔ x = y := (@to_dual F _ _).injective.eq_iff lemma to_dual_eq_iff_eq' {x x' : F} : (∀ y : F, ⟪x, y⟫_ℝ = ⟪x', y⟫_ℝ) ↔ x = x' := begin split, { intros h, have : to_dual x = to_dual x' → x = x' := to_dual_eq_iff_eq.mp, apply this, simp_rw [←to_dual_apply] at h, ext z, exact h z }, { rintros rfl y, refl } end @[simp] lemma norm_to_dual_apply (x : F) : ∥to_dual x∥ = ∥x∥ := norm_to_dual_map_apply x /-- In a Hilbert space, the norm of a vector in the dual space is the norm of its corresponding primal vector. -/ lemma norm_to_dual_symm_apply (ℓ : normed_space.dual ℝ F) : ∥to_dual.symm ℓ∥ = ∥ℓ∥ := begin have : ℓ = to_dual (to_dual.symm ℓ) := by simp only [continuous_linear_equiv.apply_symm_apply], conv_rhs { rw [this] }, refine eq.symm (norm_to_dual_apply _), end end real end inner_product_space
c54f3d8d40c891ae3370b4cc310d8acee252440b
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/data/nat/cast.lean
a368404b6bbfa48a8a90e4caee98f077dbe5a50e
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
4,099
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 tactic.interactive algebra.order algebra.ordered_group algebra.ring 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 @[priority 0] instance cast_coe : has_coe ℕ α := ⟨nat.cast⟩ @[simp] theorem cast_zero : ((0 : ℕ) : α) = 0 := rfl theorem cast_add_one (n : ℕ) : ((n + 1 : ℕ) : α) = n + 1 := rfl @[simp] theorem cast_succ (n : ℕ) : ((succ n : ℕ) : α) = n + 1 := rfl end @[simp] theorem cast_one [add_monoid α] [has_one α] : ((1 : ℕ) : α) = 1 := zero_add _ @[simp] 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] instance [add_monoid α] [has_one α] : is_add_monoid_hom (coe : ℕ → α) := { map_zero := cast_zero, map_add := cast_add } @[simp] theorem cast_bit0 [add_monoid α] [has_one α] (n : ℕ) : ((bit0 n : ℕ) : α) = bit0 n := cast_add _ _ @[simp] 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*} [semiring α] : ((2 : ℕ) : α) = 2 := by simp @[simp] theorem cast_pred [add_group α] [has_one α] : ∀ {n}, n > 0 → ((n - 1 : ℕ) : α) = n - 1 | (n+1) h := (add_sub_cancel (n:α) 1).symm @[simp] 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] 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] instance [semiring α] : is_semiring_hom (coe : ℕ → α) := by refine_struct {..}; simp theorem mul_cast_comm [semiring α] (a : α) (n : ℕ) : a * n = n * a := by induction n; simp [left_distrib, right_distrib, *] @[simp] theorem cast_nonneg [linear_ordered_semiring α] : ∀ n : ℕ, 0 ≤ (n : α) | 0 := le_refl _ | (n+1) := add_nonneg (cast_nonneg n) zero_le_one @[simp] theorem cast_le [linear_ordered_semiring α] : ∀ {m n : ℕ}, (m : α) ≤ n ↔ m ≤ n | 0 n := by simp [zero_le] | (m+1) 0 := by simpa [not_succ_le_zero] using lt_add_of_lt_of_nonneg zero_lt_one (@cast_nonneg α _ m) | (m+1) (n+1) := (add_le_add_iff_right 1).trans $ (@cast_le m n).trans $ (add_le_add_iff_right 1).symm @[simp] theorem cast_lt [linear_ordered_semiring α] {m n : ℕ} : (m : α) < n ↔ m < n := by simpa [-cast_le] using not_congr (@cast_le α _ n m) @[simp] theorem cast_pos [linear_ordered_semiring α] {n : ℕ} : (0 : α) < n ↔ 0 < n := by rw [← cast_zero, cast_lt] theorem eq_cast [add_monoid α] [has_one α] (f : ℕ → α) (H0 : f 0 = 0) (H1 : f 1 = 1) (Hadd : ∀ x y, f (x + y) = f x + f y) : ∀ n : ℕ, f n = n | 0 := H0 | (n+1) := by rw [Hadd, H1, eq_cast]; refl theorem eq_cast' [add_group α] [has_one α] (f : ℕ → α) (H1 : f 1 = 1) (Hadd : ∀ x y, f (x + y) = f x + f y) : ∀ n : ℕ, f n = n := eq_cast _ (by rw [← add_left_inj (f 0), add_zero, ← Hadd]) H1 Hadd @[simp] theorem cast_id (n : ℕ) : ↑n = n := (eq_cast id rfl rfl (λ _ _, rfl) n).symm @[simp] theorem cast_min [decidable_linear_ordered_semiring α] {a b : ℕ} : (↑(min a b) : α) = min a b := by by_cases a ≤ b; simp [h, min] @[simp] theorem cast_max [decidable_linear_ordered_semiring α] {a b : ℕ} : (↑(max a b) : α) = max a b := by by_cases a ≤ b; simp [h, max] @[simp] theorem abs_cast [decidable_linear_ordered_comm_ring α] (a : ℕ) : abs (a : α) = a := abs_of_nonneg (cast_nonneg a) end nat
0f08bbba416c953f80e316608e2ae86794005687
491068d2ad28831e7dade8d6dff871c3e49d9431
/tests/lean/run/ppbeta.lean
1f10bacbcc66b9c3c1f6c9a2cee759b8caabd6c1
[ "Apache-2.0" ]
permissive
davidmueller13/lean
65a3ed141b4088cd0a268e4de80eb6778b21a0e9
c626e2e3c6f3771e07c32e82ee5b9e030de5b050
refs/heads/master
1,611,278,313,401
1,444,021,177,000
1,444,021,177,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
278
lean
import data.int open [coercions] int open [coercions] nat definition lt (a b : int) := int.le (int.add a 1) b infix `<` := lt infixl `+` := int.add theorem lt_add_succ2 (a : int) (n : nat) : a < a + nat.succ n := int.le.intro (show a + 1 + n = a + nat.succ n, from sorry)
914b46834aba149ad3197cae064dff51d721773b
4d3f29a7b2eff44af8fd0d3176232e039acb9ee3
/LAMR/Util/Propositional/SatSolver.lean
dbfac152cf6a5a250675dd33a99983032330eb0c
[]
no_license
marijnheule/lamr
5fc5d69d326ff92e321242cfd7f72e78d7f99d7e
28cc4114c7361059bb54f407fa312bf38b48728b
refs/heads/main
1,689,338,013,620
1,630,359,632,000
1,630,359,632,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,480
lean
import LAMR.Util.Propositional.Transformations import LAMR.Util.Propositional.DRAT import Std open Std open Lean open List open Nat open HashMap def SNH := HashMap String Nat def NSH := HashMap Nat String def HH := SNH × NSH def litVar : Lit → String | Lit.pos s => s | Lit.neg s => s def addLiteral : HH → Lit → HH | (sn, ns), l => let s := litVar l if sn.contains s then (sn, ns) else let k := sn.size + 1 (sn.insert s k, ns.insert k s) def addCla : HH → Clause → HH | h, c => foldl addLiteral h c def addCnfForm : CnfForm → HH | c => foldl addCla (empty, empty) c def charToNat : Char → Option Nat | '0' => some 0 | '1' => some 1 | '2' => some 2 | '3' => some 3 | '4' => some 4 | '5' => some 5 | '6' => some 6 | '7' => some 7 | '8' => some 8 | '9' => some 9 | _ => none protected partial def String.toNatCore : Nat → String → Option Nat | k, ⟨[]⟩ => some k | k, ⟨c :: s⟩ => match charToNat c with | some m => String.toNatCore ((k * 10) + m) ⟨s⟩ | none => none protected def String.toNat (s : String) : Option Nat := String.toNatCore 0 s def Lit.toDimacs (h : SNH) : Lit → String | pos s => toString (h.findD s 0) | neg s => "-" ++ toString (h.findD s 0) protected def Clause.toDimacs (h : SNH) : Clause → String | [] => "0" | [l] => l.toDimacs h ++ " 0" | l0 :: l1 :: ls => l0.toDimacs h ++ " " ++ Clause.toDimacs h (l1 :: ls) protected def CnfForm.toDimacsCore (h : SNH) : CnfForm → String | [] => "" | [c] => c.toDimacs h | c0 :: c1 :: cs => c0.toDimacs h ++ "\n" ++ CnfForm.toDimacsCore h (c1 :: cs) protected def CnfForm.toDimacs (c : CnfForm) : SNH × NSH × String := let (sn, ns) := addCnfForm c (sn, ns, "p cnf " ++ toString sn.size ++ " " ++ toString c.length ++ "\n" ++ c.toDimacsCore sn) def runCadical : IO.Process.SpawnArgs := {cmd := "LAMR/bin/cadical", args := #["LAMR/bin/temp.cnf", "LAMR/bin/temp.drat"]} -- Same as IO.Process.run, but does not require exitcode = 0 def run' (args : IO.Process.SpawnArgs) : IO String := do let out ← IO.Process.output args pure out.stdout inductive SatResult | Sat : List Lit → SatResult | Unsat : CnfForm → SatResult open SatResult def String.toLit (h : NSH) : String → Option Lit | ⟨'-' :: cs⟩ => match String.toNat ⟨cs⟩ with | some n => Lit.neg (h.findD n "ERROR") | none => none | s => match String.toNat s with | some n => Lit.pos (h.findD n "ERROR") | none => none /-- Given a list of solver output lines containing a model, returns the model. -/ private def parseModel (h : NSH) (lns : List String) : Except String (List Lit) := go [] lns |>.map List.reverse where -- called on every line go (acc : List Lit) : List String → Except String (List Lit) | ln :: lns => match ln.splitOn " " with | "v" :: litSs => do let (done, lits) ← goLits acc litSs if done then lits else go lits lns | _ => go acc lns | [] => if acc.length > 0 then throw s!"model does not end with \"0\", literals so far:\n{acc}" else throw "expected model, got no literals" -- called on every literal in a line goLits (acc : List Lit) : List String → Except String (Bool × List Lit) | litS :: litSs => if litS == "0" then (true, acc) -- model ends at "0" else do let some lit ← String.toLit h litS | throw s!"expected literal, got '{litS}'" goLits (lit :: acc) litSs | [] => (false, acc) def formatLit : Lit → String | Lit.neg s => "-" ++ s | Lit.pos s => s def formatCla : Clause → String | ls => "[" ++ (" ".intercalate $ ls.map formatLit) ++ "]" def formatCnf : CnfForm → String | ls => "\n".intercalate $ ls.map formatCla def formatResult : SatResult → String | Sat ls => "Satisfying assignment : " ++ formatCla ls | Unsat m => "Unsat proof : \n" ++ formatCnf m def is_odd : Nat → Bool | k + 2 => is_odd k | 1 => Bool.true | 0 => Bool.false def natToLit (h : NSH) (k : Nat) : Option Lit := if is_odd k then match k with | k' + 1 => let k'' := k' / 2 some $ Lit.neg (h.findD k'' $ toString k'') | _ => none else some $ Lit.pos (h.findD (k / 2) $ toString (k / 2)) def natsToLits (h : NSH) : List Nat → Option (List Lit) | [] => some [] | k :: ks => match natToLit h k, natsToLits h ks with | some l, some ls => some (l :: ls) | _, _ => none -- https://satcompetition.github.io/2021/certificates.html def decode : Nat → Nat → List Nat → Option (List Nat) | 0, 0, (0 :: ns) => match decode 0 0 ns with | some ks => some (0 :: ks) | none => none | k, o, (n :: ns) => if n < 128 then match decode 0 0 ns with | some ks => some $ (((2 ^ o) * n) + k) :: ks | none => none else decode (((2 ^ o) * (n - 128)) + k) (o + 1) ns | 0, 0, [] => some [] | _, _, _ => none def clausify (h : NSH) : Clause → List Nat → Option CnfForm | _, [] => some [] | c, 0 :: ns => match clausify h [] ns with | some m => some (c.reverse :: m) | none => none | c, n :: ns => do match natToLit h n with | some l => clausify h (l :: c) ns | none => none /-- Parses the output of a SAT solver given as a list of lines. Returns `some lits` if SATISFIABLE, `none` if UNSATISFIABLE, otherwise describes a parsing error. See http://www.satcompetition.org/2004/format-solvers2004.html -/ private def parseSatOutputAux (h : NSH) : List String → Except String (Option (List Lit)) | ("s SATISFIABLE" :: lns) => do let lits ← parseModel h lns some lits | ("s UNSATISFIABLE" :: lns) => none | ("s UNKNOWN" :: lns) => throw "solver returned 'UNKNOWN' satisfiability" | _ :: lns => parseSatOutputAux h lns | [] => throw "expected satisfiability status, saw none" def parseSatOutput (h : NSH) (ss : List String) : IO SatResult := match parseSatOutputAux h ss with | Except.error e => throwThe IO.Error e | Except.ok (some lits) => SatResult.Sat lits | Except.ok none => do let bs ← IO.FS.readBinFile "LAMR/bin/temp.drat" match DRAT.decodeDrat bs with | Except.error e => throwThe IO.Error s!"failed decoding DRAT proof:\n{e}" | Except.ok pf => -- TODO convert DRAT ints to `Lit`s via NSH SatResult.Unsat [] def callCadical (cnf : CnfForm) : IO (String × SatResult) := do let (sn, ns, cnfs) ← CnfForm.toDimacs cnf IO.FS.writeFile "LAMR/bin/temp.cnf" cnfs let s ← run' runCadical let ss ← String.splitOn s "\n" let rst ← parseSatOutput ns ss pure (s, rst)
3e8f96eb84dd3bd45a7b3478e3c91b2d03e5ad9b
968e2f50b755d3048175f176376eff7139e9df70
/examples/prop_logic_lean_summary/unnamed_407.lean
5d7456e50d998ed8ec75bb5af5bbd9311ba35bba
[]
no_license
gihanmarasingha/mth1001_sphinx
190a003269ba5e54717b448302a27ca26e31d491
05126586cbf5786e521be1ea2ef5b4ba3c44e74a
refs/heads/master
1,672,913,933,677
1,604,516,583,000
1,604,516,583,000
309,245,750
1
0
null
null
null
null
UTF-8
Lean
false
false
257
lean
variables p q r : Prop -- BEGIN example (h₁ : (p ∧ r) ∨ (r ∧ q)) : r := or.elim h₁ (assume h₂ : p ∧ r, h₂.right) -- A term-style proof of `p ∧ r → r` (assume h₂ : r ∧ q, h₂.left) -- A term-style proof of `r ∧ q → r` -- END
2231af6dd231d2cb6766d2e2c6ca2cdf365d2610
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/tests/lean/have1.lean
30444a01652a3bc3de33fcbd9d0cd64c5818d1eb
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
440
lean
import logic open bool eq.ops tactic eq constants a b c : bool axiom H1 : a = b axiom H2 : b = c check have e1 [visible] : a = b, from H1, have e2 : a = c, by apply trans; apply e1; apply H2, have e3 : c = a, from e2⁻¹, have e4 [visible] : b = a, from e1⁻¹, have e5 : b = c, from e4 ⬝ e2, have e6 : a = a, from H1 ⬝ H2 ⬝ H2⁻¹ ⬝ H1⁻¹ ⬝ H1 ⬝ H2 ⬝ H2⁻¹ ⬝ H1⁻¹, e3 ⬝ e2
36a99a7a2ea033241ee09beb46ea50711ce3eb1a
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/stage0/src/Lean/Elab/Term.lean
0b48144ca334fd711f45153523c8b87363a65630
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
65,971
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.ResolveName import Lean.Util.Sorry import Lean.Util.ReplaceExpr import Lean.Structure import Lean.Meta.ExprDefEq import Lean.Meta.AppBuilder import Lean.Meta.SynthInstance import Lean.Meta.CollectMVars import Lean.Meta.Coe import Lean.Meta.Tactic.Util import Lean.Hygiene import Lean.Util.RecDepth import Lean.Elab.Log import Lean.Elab.Level import Lean.Elab.Attributes import Lean.Elab.AutoBound import Lean.Elab.InfoTree import Lean.Elab.Open import Lean.Elab.SetOption namespace Lean.Elab.Term /- Set isDefEq configuration for the elaborator. Note that we enable all approximations but `quasiPatternApprox` In Lean3 and Lean 4, we used to use the quasi-pattern approximation during elaboration. The example: ``` def ex : StateT δ (StateT σ Id) σ := monadLift (get : StateT σ Id σ) ``` demonstrates why it produces counterintuitive behavior. We have the `Monad-lift` application: ``` @monadLift ?m ?n ?c ?α (get : StateT σ id σ) : ?n ?α ``` It produces the following unification problem when we process the expected type: ``` ?n ?α =?= StateT δ (StateT σ id) σ ==> (approximate using first-order unification) ?n := StateT δ (StateT σ id) ?α := σ ``` Then, we need to solve: ``` ?m ?α =?= StateT σ id σ ==> instantiate metavars ?m σ =?= StateT σ id σ ==> (approximate since it is a quasi-pattern unification constraint) ?m := fun σ => StateT σ id σ ``` Note that the constraint is not a Milner pattern because σ is in the local context of `?m`. We are ignoring the other possible solutions: ``` ?m := fun σ' => StateT σ id σ ?m := fun σ' => StateT σ' id σ ?m := fun σ' => StateT σ id σ' ``` We need the quasi-pattern approximation for elaborating recursor-like expressions (e.g., dependent `match with` expressions). If we had use first-order unification, then we would have produced the right answer: `?m := StateT σ id` Haskell would work on this example since it always uses first-order unification. -/ def setElabConfig (cfg : Meta.Config) : Meta.Config := { cfg with foApprox := true, ctxApprox := true, constApprox := false, quasiPatternApprox := false } structure Context where fileName : String fileMap : FileMap declName? : Option Name := none macroStack : MacroStack := [] currMacroScope : MacroScope := firstFrontendMacroScope /- When `mayPostpone == true`, an elaboration function may interrupt its execution by throwing `Exception.postpone`. The function `elabTerm` catches this exception and creates fresh synthetic metavariable `?m`, stores `?m` in the list of pending synthetic metavariables, and returns `?m`. -/ mayPostpone : Bool := true /- When `errToSorry` is set to true, the method `elabTerm` catches exceptions and converts them into synthetic `sorry`s. The implementation of choice nodes and overloaded symbols rely on the fact that when `errToSorry` is set to false for an elaboration function `F`, then `errToSorry` remains `false` for all elaboration functions invoked by `F`. That is, it is safe to transition `errToSorry` from `true` to `false`, but we must not set `errToSorry` to `true` when it is currently set to `false`. -/ errToSorry : Bool := true /- When `autoBoundImplicit` is set to true, instead of producing an "unknown identifier" error for unbound variables, we generate an internal exception. This exception is caught at `elabBinders` and `elabTypeWithUnboldImplicit`. Both methods add implicit declarations for the unbound variable and try again. -/ autoBoundImplicit : Bool := false autoBoundImplicits : Std.PArray Expr := {} /-- Map from user name to internal unique name -/ sectionVars : NameMap Name := {} /-- Map from internal name to fvar -/ sectionFVars : NameMap Expr := {} /-- Enable/disable implicit lambdas feature. -/ implicitLambda : Bool := true /-- Saved context for postponed terms and tactics to be executed. -/ structure SavedContext where declName? : Option Name options : Options openDecls : List OpenDecl macroStack : MacroStack errToSorry : Bool /-- We use synthetic metavariables as placeholders for pending elaboration steps. -/ inductive SyntheticMVarKind where -- typeclass instance search | typeClass /- Similar to typeClass, but error messages are different. if `f?` is `some f`, we produce an application type mismatch error message. Otherwise, if `header?` is `some header`, we generate the error `(header ++ "has type" ++ eType ++ "but it is expected to have type" ++ expectedType)` Otherwise, we generate the error `("type mismatch" ++ e ++ "has type" ++ eType ++ "but it is expected to have type" ++ expectedType)` -/ | coe (header? : Option String) (eNew : Expr) (expectedType : Expr) (eType : Expr) (e : Expr) (f? : Option Expr) -- tactic block execution | tactic (tacticCode : Syntax) (ctx : SavedContext) -- `elabTerm` call that threw `Exception.postpone` (input is stored at `SyntheticMVarDecl.ref`) | postponed (ctx : SavedContext) instance : ToString SyntheticMVarKind where toString | SyntheticMVarKind.typeClass => "typeclass" | SyntheticMVarKind.coe .. => "coe" | SyntheticMVarKind.tactic .. => "tactic" | SyntheticMVarKind.postponed .. => "postponed" structure SyntheticMVarDecl where mvarId : MVarId stx : Syntax kind : SyntheticMVarKind inductive MVarErrorKind where | implicitArg (ctx : Expr) | hole | custom (msgData : MessageData) instance : ToString MVarErrorKind where toString | MVarErrorKind.implicitArg ctx => "implicitArg" | MVarErrorKind.hole => "hole" | MVarErrorKind.custom msg => "custom" structure MVarErrorInfo where mvarId : MVarId ref : Syntax kind : MVarErrorKind structure LetRecToLift where ref : Syntax fvarId : FVarId attrs : Array Attribute shortDeclName : Name declName : Name lctx : LocalContext localInstances : LocalInstances type : Expr val : Expr mvarId : MVarId structure State where levelNames : List Name := [] syntheticMVars : List SyntheticMVarDecl := [] mvarErrorInfos : List MVarErrorInfo := [] messages : MessageLog := {} letRecsToLift : List LetRecToLift := [] infoState : InfoState := {} deriving Inhabited abbrev TermElabM := ReaderT Context $ StateRefT State MetaM abbrev TermElab := Syntax → Option Expr → TermElabM Expr -- Make the compiler generate specialized `pure`/`bind` so we do not have to optimize through the -- whole monad stack at every use site. May eventually be covered by `deriving`. instance : Monad TermElabM := { inferInstanceAs (Monad TermElabM) with } open Meta instance : Inhabited (TermElabM α) where default := throw arbitrary structure SavedState where meta : Meta.SavedState «elab» : State deriving Inhabited protected def saveState : TermElabM SavedState := do pure { meta := (← Meta.saveState), «elab» := (← get) } def SavedState.restore (s : SavedState) (restoreInfo : Bool := false) : TermElabM Unit := do let traceState ← getTraceState -- We never backtrack trace message let infoState := (← get).infoState -- We also do not backtrack the info nodes when `restoreInfo == false` s.meta.restore set s.elab setTraceState traceState unless restoreInfo do modify fun s => { s with infoState := infoState } instance : MonadBacktrack SavedState TermElabM where saveState := Term.saveState restoreState b := b.restore abbrev TermElabResult (α : Type) := EStateM.Result Exception SavedState α instance [Inhabited α] : Inhabited (TermElabResult α) where default := EStateM.Result.ok arbitrary arbitrary def setMessageLog (messages : MessageLog) : TermElabM Unit := modify fun s => { s with messages := messages } def resetMessageLog : TermElabM Unit := setMessageLog {} def getMessageLog : TermElabM MessageLog := return (← get).messages /-- Execute `x`, save resulting expression and new state. We remove any `Info` created by `x`. The info nodes are committed when we execute `applyResult`. We use `observing` to implement overloaded notation and decls. We want to save `Info` nodes for the chosen alternative. -/ def observing (x : TermElabM α) : TermElabM (TermElabResult α) := do let s ← saveState try let e ← x let sNew ← saveState s.restore (restoreInfo := true) pure (EStateM.Result.ok e sNew) catch | ex@(Exception.error _ _) => let sNew ← saveState s.restore (restoreInfo := true) pure (EStateM.Result.error ex sNew) | ex@(Exception.internal id _) => if id == postponeExceptionId then s.restore (restoreInfo := true) throw ex /-- Apply the result/exception and state captured with `observing`. We use this method to implement overloaded notation and symbols. -/ def applyResult (result : TermElabResult α) : TermElabM α := match result with | EStateM.Result.ok a r => do r.restore (restoreInfo := true); pure a | EStateM.Result.error ex r => do r.restore (restoreInfo := true); throw ex /-- Execute `x`, but keep state modifications only if `x` did not postpone. This method is useful to implement elaboration functions that cannot decide whether they need to postpone or not without updating the state. -/ def commitIfDidNotPostpone (x : TermElabM α) : TermElabM α := do -- We just reuse the implementation of `observing` and `applyResult`. let r ← observing x applyResult r def getLevelNames : TermElabM (List Name) := return (← get).levelNames def getFVarLocalDecl! (fvar : Expr) : TermElabM LocalDecl := do match (← getLCtx).find? fvar.fvarId! with | some d => pure d | none => unreachable! instance : AddErrorMessageContext TermElabM where add ref msg := do let ctx ← read let ref := getBetterRef ref ctx.macroStack let msg ← addMessageContext msg let msg ← addMacroStack msg ctx.macroStack pure (ref, msg) instance : MonadLog TermElabM where getRef := getRef getFileMap := return (← read).fileMap getFileName := return (← read).fileName logMessage msg := do let ctx ← readThe Core.Context let msg := { msg with data := MessageData.withNamingContext { currNamespace := ctx.currNamespace, openDecls := ctx.openDecls } msg.data }; modify fun s => { s with messages := s.messages.add msg } protected def getCurrMacroScope : TermElabM MacroScope := do pure (← read).currMacroScope protected def getMainModule : TermElabM Name := do pure (← getEnv).mainModule protected def withFreshMacroScope (x : TermElabM α) : TermElabM α := do let fresh ← modifyGetThe Core.State (fun st => (st.nextMacroScope, { st with nextMacroScope := st.nextMacroScope + 1 })) withReader (fun ctx => { ctx with currMacroScope := fresh }) x instance : MonadQuotation TermElabM where getCurrMacroScope := Term.getCurrMacroScope getMainModule := Term.getMainModule withFreshMacroScope := Term.withFreshMacroScope instance : MonadInfoTree TermElabM where getInfoState := return (← get).infoState modifyInfoState f := modify fun s => { s with infoState := f s.infoState } /-- Execute `x` but discard changes performed at `Term.State` and `Meta.State`. Recall that the environment is at `Core.State`. Thus, any updates to it will be preserved. This method is useful for performing computations where all metavariable must be resolved or discarded. The info trees are not discarded, however, and wrapped in `InfoTree.Context` to store their metavariable context. -/ def withoutModifyingElabMetaStateWithInfo (x : TermElabM α) : TermElabM α := do let s ← get let sMeta ← getThe Meta.State try withSaveInfoContext x finally modify ({ s with infoState := ·.infoState }) set sMeta unsafe def mkTermElabAttributeUnsafe : IO (KeyedDeclsAttribute TermElab) := mkElabAttribute TermElab `Lean.Elab.Term.termElabAttribute `builtinTermElab `termElab `Lean.Parser.Term `Lean.Elab.Term.TermElab "term" @[implementedBy mkTermElabAttributeUnsafe] constant mkTermElabAttribute : IO (KeyedDeclsAttribute TermElab) builtin_initialize termElabAttribute : KeyedDeclsAttribute TermElab ← mkTermElabAttribute /-- Auxiliary datatatype for presenting a Lean lvalue modifier. We represent a unelaborated lvalue as a `Syntax` (or `Expr`) and `List LVal`. Example: `a.foo[i].1` is represented as the `Syntax` `a` and the list `[LVal.fieldName "foo", LVal.getOp i, LVal.fieldIdx 1]`. Recall that the notation `a[i]` is not just for accessing arrays in Lean. -/ inductive LVal where | fieldIdx (ref : Syntax) (i : Nat) /- Field `suffix?` is for producing better error messages because `x.y` may be a field access or a hierachical/composite name. `ref` is the syntax object representing the field. `targetStx` is the target object being accessed. -/ | fieldName (ref : Syntax) (name : String) (suffix? : Option Name) (targetStx : Syntax) | getOp (ref : Syntax) (idx : Syntax) def LVal.getRef : LVal → Syntax | LVal.fieldIdx ref _ => ref | LVal.fieldName ref .. => ref | LVal.getOp ref _ => ref def LVal.isFieldName : LVal → Bool | LVal.fieldName .. => true | _ => false instance : ToString LVal where toString | LVal.fieldIdx _ i => toString i | LVal.fieldName _ n .. => n | LVal.getOp _ idx => "[" ++ toString idx ++ "]" def getDeclName? : TermElabM (Option Name) := return (← read).declName? def getLetRecsToLift : TermElabM (List LetRecToLift) := return (← get).letRecsToLift def isExprMVarAssigned (mvarId : MVarId) : TermElabM Bool := return (← getMCtx).isExprAssigned mvarId def getMVarDecl (mvarId : MVarId) : TermElabM MetavarDecl := return (← getMCtx).getDecl mvarId def assignLevelMVar (mvarId : MVarId) (val : Level) : TermElabM Unit := modifyThe Meta.State fun s => { s with mctx := s.mctx.assignLevel mvarId val } def withDeclName (name : Name) (x : TermElabM α) : TermElabM α := withReader (fun ctx => { ctx with declName? := name }) x def setLevelNames (levelNames : List Name) : TermElabM Unit := modify fun s => { s with levelNames := levelNames } def withLevelNames (levelNames : List Name) (x : TermElabM α) : TermElabM α := do let levelNamesSaved ← getLevelNames setLevelNames levelNames try x finally setLevelNames levelNamesSaved def withoutErrToSorry (x : TermElabM α) : TermElabM α := withReader (fun ctx => { ctx with errToSorry := false }) x /-- For testing `TermElabM` methods. The #eval command will sign the error. -/ def throwErrorIfErrors : TermElabM Unit := do if (← get).messages.hasErrors then throwError "Error(s)" def traceAtCmdPos (cls : Name) (msg : Unit → MessageData) : TermElabM Unit := withRef Syntax.missing $ trace cls msg def ppGoal (mvarId : MVarId) : TermElabM Format := Meta.ppGoal mvarId open Level (LevelElabM) def liftLevelM (x : LevelElabM α) : TermElabM α := do let ctx ← read let mctx ← getMCtx let ngen ← getNGen let lvlCtx : Level.Context := { options := (← getOptions), ref := (← getRef), autoBoundImplicit := ctx.autoBoundImplicit } match (x lvlCtx).run { ngen := ngen, mctx := mctx, levelNames := (← getLevelNames) } with | EStateM.Result.ok a newS => setMCtx newS.mctx; setNGen newS.ngen; setLevelNames newS.levelNames; pure a | EStateM.Result.error ex _ => throw ex def elabLevel (stx : Syntax) : TermElabM Level := liftLevelM $ Level.elabLevel stx /- Elaborate `x` with `stx` on the macro stack -/ def withMacroExpansion (beforeStx afterStx : Syntax) (x : TermElabM α) : TermElabM α := withMacroExpansionInfo beforeStx afterStx do withReader (fun ctx => { ctx with macroStack := { before := beforeStx, after := afterStx } :: ctx.macroStack }) x /- Add the given metavariable to the list of pending synthetic metavariables. The method `synthesizeSyntheticMVars` is used to process the metavariables on this list. -/ def registerSyntheticMVar (stx : Syntax) (mvarId : MVarId) (kind : SyntheticMVarKind) : TermElabM Unit := do modify fun s => { s with syntheticMVars := { mvarId := mvarId, stx := stx, kind := kind } :: s.syntheticMVars } def registerSyntheticMVarWithCurrRef (mvarId : MVarId) (kind : SyntheticMVarKind) : TermElabM Unit := do registerSyntheticMVar (← getRef) mvarId kind def registerMVarErrorHoleInfo (mvarId : MVarId) (ref : Syntax) : TermElabM Unit := do modify fun s => { s with mvarErrorInfos := { mvarId := mvarId, ref := ref, kind := MVarErrorKind.hole } :: s.mvarErrorInfos } def registerMVarErrorImplicitArgInfo (mvarId : MVarId) (ref : Syntax) (app : Expr) : TermElabM Unit := do modify fun s => { s with mvarErrorInfos := { mvarId := mvarId, ref := ref, kind := MVarErrorKind.implicitArg app } :: s.mvarErrorInfos } def registerMVarErrorCustomInfo (mvarId : MVarId) (ref : Syntax) (msgData : MessageData) : TermElabM Unit := do modify fun s => { s with mvarErrorInfos := { mvarId := mvarId, ref := ref, kind := MVarErrorKind.custom msgData } :: s.mvarErrorInfos } def registerCustomErrorIfMVar (e : Expr) (ref : Syntax) (msgData : MessageData) : TermElabM Unit := match e.getAppFn with | Expr.mvar mvarId _ => registerMVarErrorCustomInfo mvarId ref msgData | _ => pure () /- Auxiliary method for reporting errors of the form "... contains metavariables ...". This kind of error is thrown, for example, at `Match.lean` where elaboration cannot continue if there are metavariables in patterns. We only want to log it if we haven't logged any error so far. -/ def throwMVarError (m : MessageData) : TermElabM α := do if (← get).messages.hasErrors then throwAbortTerm else throwError m def MVarErrorInfo.logError (mvarErrorInfo : MVarErrorInfo) (extraMsg? : Option MessageData) : TermElabM Unit := do match mvarErrorInfo.kind with | MVarErrorKind.implicitArg app => do let app ← instantiateMVars app let msg : MessageData := m!"don't know how to synthesize implicit argument{indentExpr app.setAppPPExplicitForExposingMVars}" let msg := msg ++ Format.line ++ "context:" ++ Format.line ++ MessageData.ofGoal mvarErrorInfo.mvarId logErrorAt mvarErrorInfo.ref (appendExtra msg) | MVarErrorKind.hole => do let msg : MessageData := "don't know how to synthesize placeholder" let msg := msg ++ Format.line ++ "context:" ++ Format.line ++ MessageData.ofGoal mvarErrorInfo.mvarId logErrorAt mvarErrorInfo.ref (MessageData.tagged `Elab.synthPlaceholder <| appendExtra msg) | MVarErrorKind.custom msg => logErrorAt mvarErrorInfo.ref (appendExtra msg) where appendExtra (msg : MessageData) : MessageData := match extraMsg? with | none => msg | some extraMsg => msg ++ extraMsg /-- Try to log errors for the unassigned metavariables `pendingMVarIds`. Return `true` if there were "unfilled holes", and we should "abort" declaration. TODO: try to fill "all" holes using synthetic "sorry's" Remark: We only log the "unfilled holes" as new errors if no error has been logged so far. -/ def logUnassignedUsingErrorInfos (pendingMVarIds : Array MVarId) (extraMsg? : Option MessageData := none) : TermElabM Bool := do let s ← get let hasOtherErrors := s.messages.hasErrors let mut hasNewErrors := false let mut alreadyVisited : NameSet := {} for mvarErrorInfo in s.mvarErrorInfos do let mvarId := mvarErrorInfo.mvarId unless alreadyVisited.contains mvarId do alreadyVisited := alreadyVisited.insert mvarId let foundError ← withMVarContext mvarId do /- The metavariable `mvarErrorInfo.mvarId` may have been assigned or delayed assigned to another metavariable that is unassigned. -/ let mvarDeps ← getMVars (mkMVar mvarId) if mvarDeps.any pendingMVarIds.contains then do unless hasOtherErrors do mvarErrorInfo.logError extraMsg? pure true else pure false if foundError then hasNewErrors := true return hasNewErrors /-- Ensure metavariables registered using `registerMVarErrorInfos` (and used in the given declaration) have been assigned. -/ def ensureNoUnassignedMVars (decl : Declaration) : TermElabM Unit := do let pendingMVarIds ← getMVarsAtDecl decl if (← logUnassignedUsingErrorInfos pendingMVarIds) then throwAbortCommand /- Execute `x` without allowing it to postpone elaboration tasks. That is, `tryPostpone` is a noop. -/ def withoutPostponing (x : TermElabM α) : TermElabM α := withReader (fun ctx => { ctx with mayPostpone := false }) x /-- Creates syntax for `(` <ident> `:` <type> `)` -/ def mkExplicitBinder (ident : Syntax) (type : Syntax) : Syntax := mkNode ``Lean.Parser.Term.explicitBinder #[mkAtom "(", mkNullNode #[ident], mkNullNode #[mkAtom ":", type], mkNullNode, mkAtom ")"] /-- Convert unassigned universe level metavariables into parameters. The new parameter names are of the form `u_i` where `i >= nextParamIdx`. The method returns the updated expression and new `nextParamIdx`. Remark: we make sure the generated parameter names do not clash with the universe at `ctx.levelNames`. -/ def levelMVarToParam (e : Expr) (nextParamIdx : Nat := 1) : TermElabM (Expr × Nat) := do let mctx ← getMCtx let levelNames ← getLevelNames let r := mctx.levelMVarToParam (fun n => levelNames.elem n) e `u nextParamIdx setMCtx r.mctx pure (r.expr, r.nextParamIdx) /-- Variant of `levelMVarToParam` where `nextParamIdx` is stored in a state monad. -/ def levelMVarToParam' (e : Expr) : StateRefT Nat TermElabM Expr := do let nextParamIdx ← get let (e, nextParamIdx) ← levelMVarToParam e nextParamIdx set nextParamIdx pure e /-- Auxiliary method for creating fresh binder names. Do not confuse with the method for creating fresh free/meta variable ids. -/ def mkFreshBinderName [Monad m] [MonadQuotation m] : m Name := withFreshMacroScope $ MonadQuotation.addMacroScope `x /-- Auxiliary method for creating a `Syntax.ident` containing a fresh name. This method is intended for creating fresh binder names. It is just a thin layer on top of `mkFreshUserName`. -/ def mkFreshIdent [Monad m] [MonadQuotation m] (ref : Syntax) : m Syntax := return mkIdentFrom ref (← mkFreshBinderName) private def applyAttributesCore (declName : Name) (attrs : Array Attribute) (applicationTime? : Option AttributeApplicationTime) : TermElabM Unit := for attr in attrs do let env ← getEnv match getAttributeImpl env attr.name with | Except.error errMsg => throwError errMsg | Except.ok attrImpl => match applicationTime? with | none => attrImpl.add declName attr.stx attr.kind | some applicationTime => if applicationTime == attrImpl.applicationTime then attrImpl.add declName attr.stx attr.kind /-- Apply given attributes **at** a given application time -/ def applyAttributesAt (declName : Name) (attrs : Array Attribute) (applicationTime : AttributeApplicationTime) : TermElabM Unit := applyAttributesCore declName attrs applicationTime def applyAttributes (declName : Name) (attrs : Array Attribute) : TermElabM Unit := applyAttributesCore declName attrs none def mkTypeMismatchError (header? : Option String) (e : Expr) (eType : Expr) (expectedType : Expr) : TermElabM MessageData := do let header : MessageData := match header? with | some header => m!"{header} " | none => m!"type mismatch{indentExpr e}\n" return m!"{header}{← mkHasTypeButIsExpectedMsg eType expectedType}" def throwTypeMismatchError (header? : Option String) (expectedType : Expr) (eType : Expr) (e : Expr) (f? : Option Expr := none) (extraMsg? : Option MessageData := none) : TermElabM α := do /- We ignore `extraMsg?` for now. In all our tests, it contained no useful information. It was always of the form: ``` failed to synthesize instance CoeT <eType> <e> <expectedType> ``` We should revisit this decision in the future and decide whether it may contain useful information or not. -/ let extraMsg := Format.nil /- let extraMsg : MessageData := match extraMsg? with | none => Format.nil | some extraMsg => Format.line ++ extraMsg; -/ match f? with | none => throwError "{← mkTypeMismatchError header? e eType expectedType}{extraMsg}" | some f => Meta.throwAppTypeMismatch f e extraMsg def withoutMacroStackAtErr (x : TermElabM α) : TermElabM α := withTheReader Core.Context (fun (ctx : Core.Context) => { ctx with options := pp.macroStack.set ctx.options false }) x /- Try to synthesize metavariable using type class resolution. This method assumes the local context and local instances of `instMVar` coincide with the current local context and local instances. Return `true` if the instance was synthesized successfully, and `false` if the instance contains unassigned metavariables that are blocking the type class resolution procedure. Throw an exception if resolution or assignment irrevocably fails. -/ def synthesizeInstMVarCore (instMVar : MVarId) (maxResultSize? : Option Nat := none) : TermElabM Bool := do let instMVarDecl ← getMVarDecl instMVar let type := instMVarDecl.type let type ← instantiateMVars type let result ← trySynthInstance type maxResultSize? match result with | LOption.some val => if (← isExprMVarAssigned instMVar) then let oldVal ← instantiateMVars (mkMVar instMVar) unless (← isDefEq oldVal val) do let oldValType ← inferType oldVal let valType ← inferType val unless (← isDefEq oldValType valType) do throwError "synthesized type class instance type is not definitionally equal to expected type, synthesized{indentExpr val}\nhas type{indentExpr valType}\nexpected{indentExpr oldValType}" throwError "synthesized type class instance is not definitionally equal to expression inferred by typing rules, synthesized{indentExpr val}\ninferred{indentExpr oldVal}" else unless (← isDefEq (mkMVar instMVar) val) do throwError "failed to assign synthesized type class instance{indentExpr val}" pure true | LOption.undef => pure false -- we will try later | LOption.none => throwError "failed to synthesize instance{indentExpr type}" register_builtin_option autoLift : Bool := { defValue := true descr := "insert monadic lifts (i.e., `liftM` and `liftCoeM`) when needed" } register_builtin_option maxCoeSize : Nat := { defValue := 16 descr := "maximum number of instances used to construct an automatic coercion" } def synthesizeCoeInstMVarCore (instMVar : MVarId) : TermElabM Bool := do synthesizeInstMVarCore instMVar (some (maxCoeSize.get (← getOptions))) /- The coercion from `α` to `Thunk α` cannot be implemented using an instance because it would eagerly evaluate `e` -/ def tryCoeThunk? (expectedType : Expr) (eType : Expr) (e : Expr) : TermElabM (Option Expr) := do match expectedType with | Expr.app (Expr.const ``Thunk u _) arg _ => if (← isDefEq eType arg) then pure (some (mkApp2 (mkConst ``Thunk.mk u) arg (mkSimpleThunk e))) else pure none | _ => pure none /-- Try to apply coercion to make sure `e` has type `expectedType`. Relevant definitions: ``` class CoeT (α : Sort u) (a : α) (β : Sort v) abbrev coe {α : Sort u} {β : Sort v} (a : α) [CoeT α a β] : β ``` -/ private def tryCoe (errorMsgHeader? : Option String) (expectedType : Expr) (eType : Expr) (e : Expr) (f? : Option Expr) : TermElabM Expr := do if (← isDefEq expectedType eType) then return e else match (← tryCoeThunk? expectedType eType e) with | some r => return r | none => let u ← getLevel eType let v ← getLevel expectedType let coeTInstType := mkAppN (mkConst ``CoeT [u, v]) #[eType, e, expectedType] let mvar ← mkFreshExprMVar coeTInstType MetavarKind.synthetic let eNew := mkAppN (mkConst ``coe [u, v]) #[eType, expectedType, e, mvar] let mvarId := mvar.mvarId! try withoutMacroStackAtErr do if (← synthesizeCoeInstMVarCore mvarId) then expandCoe eNew else -- We create an auxiliary metavariable to represent the result, because we need to execute `expandCoe` -- after we syntheze `mvar` let mvarAux ← mkFreshExprMVar expectedType MetavarKind.syntheticOpaque registerSyntheticMVarWithCurrRef mvarAux.mvarId! (SyntheticMVarKind.coe errorMsgHeader? eNew expectedType eType e f?) return mvarAux catch | Exception.error _ msg => throwTypeMismatchError errorMsgHeader? expectedType eType e f? msg | _ => throwTypeMismatchError errorMsgHeader? expectedType eType e f? def isTypeApp? (type : Expr) : TermElabM (Option (Expr × Expr)) := do let type ← withReducible $ whnf type match type with | Expr.app m α _ => pure (some ((← instantiateMVars m), (← instantiateMVars α))) | _ => pure none def synthesizeInst (type : Expr) : TermElabM Expr := do let type ← instantiateMVars type match (← trySynthInstance type) with | LOption.some val => pure val | LOption.undef => throwError "failed to synthesize instance{indentExpr type}" | LOption.none => throwError "failed to synthesize instance{indentExpr type}" def isMonadApp (type : Expr) : TermElabM Bool := do let some (m, _) ← isTypeApp? type | pure false return (← isMonad? m) |>.isSome /-- Try to coerce `a : α` into `m β` by first coercing `a : α` into ‵β`, and then using `pure`. The method is only applied if `α` is not monadic (e.g., `Nat → IO Unit`), and the head symbol of the resulting type is not a metavariable (e.g., `?m Unit` or `Bool → ?m Nat`). The main limitation of the approach above is polymorphic code. As usual, coercions and polymorphism do not interact well. In the example above, the lift is successfully applied to `true`, `false` and `!y` since none of them is polymorphic ``` def f (x : Bool) : IO Bool := do let y ← if x == 0 then IO.println "hello"; true else false; !y ``` On the other hand, the following fails since `+` is polymorphic ``` def f (x : Bool) : IO Nat := do IO.prinln x x + x -- Error: failed to synthesize `Add (IO Nat)` ``` -/ private def tryPureCoe? (errorMsgHeader? : Option String) (m β α a : Expr) : TermElabM (Option Expr) := commitWhenSome? do let doIt : TermElabM (Option Expr) := do try let aNew ← tryCoe errorMsgHeader? β α a none let aNew ← mkPure m aNew pure (some aNew) catch _ => pure none forallTelescope α fun _ α => do if (← isMonadApp α) then pure none else if !α.getAppFn.isMVar then doIt else pure none /- Try coercions and monad lifts to make sure `e` has type `expectedType`. If `expectedType` is of the form `n β`, we try monad lifts and other extensions. Otherwise, we just use the basic `tryCoe`. Extensions for monads. Given an expected type of the form `n β`, if `eType` is of the form `α`, but not `m α` 1 - Try to coerce ‵α` into ‵β`, and use `pure` to lift it to `n α`. It only works if `n` implements `Pure` If `eType` is of the form `m α`. We use the following approaches. 1- Try to unify `n` and `m`. If it succeeds, then we use ``` coeM {m : Type u → Type v} {α β : Type u} [∀ a, CoeT α a β] [Monad m] (x : m α) : m β ``` `n` must be a `Monad` to use this one. 2- If there is monad lift from `m` to `n` and we can unify `α` and `β`, we use ``` liftM : ∀ {m : Type u_1 → Type u_2} {n : Type u_1 → Type u_3} [self : MonadLiftT m n] {α : Type u_1}, m α → n α ``` Note that `n` may not be a `Monad` in this case. This happens quite a bit in code such as ``` def g (x : Nat) : IO Nat := do IO.println x pure x def f {m} [MonadLiftT IO m] : m Nat := g 10 ``` 3- If there is a monad lif from `m` to `n` and a coercion from `α` to `β`, we use ``` liftCoeM {m : Type u → Type v} {n : Type u → Type w} {α β : Type u} [MonadLiftT m n] [∀ a, CoeT α a β] [Monad n] (x : m α) : n β ``` Note that approach 3 does not subsume 1 because it is only applicable if there is a coercion from `α` to `β` for all values in `α`. This is not the case for example for `pure $ x > 0` when the expected type is `IO Bool`. The given type is `IO Prop`, and we only have a coercion from decidable propositions. Approach 1 works because it constructs the coercion `CoeT (m Prop) (pure $ x > 0) (m Bool)` using the instance `pureCoeDepProp`. Note that, approach 2 is more powerful than `tryCoe`. Recall that type class resolution never assigns metavariables created by other modules. Now, consider the following scenario ```lean def g (x : Nat) : IO Nat := ... deg h (x : Nat) : StateT Nat IO Nat := do v ← g x; IO.Println v; ... ``` Let's assume there is no other occurrence of `v` in `h`. Thus, we have that the expected of `g x` is `StateT Nat IO ?α`, and the given type is `IO Nat`. So, even if we add a coercion. ``` instance {α m n} [MonadLiftT m n] {α} : Coe (m α) (n α) := ... ``` It is not applicable because TC would have to assign `?α := Nat`. On the other hand, TC can easily solve `[MonadLiftT IO (StateT Nat IO)]` since this goal does not contain any metavariables. And then, we convert `g x` into `liftM $ g x`. -/ private def tryLiftAndCoe (errorMsgHeader? : Option String) (expectedType : Expr) (eType : Expr) (e : Expr) (f? : Option Expr) : TermElabM Expr := do let expectedType ← instantiateMVars expectedType let eType ← instantiateMVars eType let throwMismatch {α} : TermElabM α := throwTypeMismatchError errorMsgHeader? expectedType eType e f? let tryCoeSimple : TermElabM Expr := tryCoe errorMsgHeader? expectedType eType e f? let some (n, β) ← isTypeApp? expectedType | tryCoeSimple let tryPureCoeAndSimple : TermElabM Expr := do if autoLift.get (← getOptions) then match (← tryPureCoe? errorMsgHeader? n β eType e) with | some eNew => pure eNew | none => tryCoeSimple else tryCoeSimple let some (m, α) ← isTypeApp? eType | tryPureCoeAndSimple if (← isDefEq m n) then let some monadInst ← isMonad? n | tryCoeSimple try expandCoe (← mkAppOptM ``coeM #[m, α, β, none, monadInst, e]) catch _ => throwMismatch else if autoLift.get (← getOptions) then try -- Construct lift from `m` to `n` let monadLiftType ← mkAppM ``MonadLiftT #[m, n] let monadLiftVal ← synthesizeInst monadLiftType let u_1 ← getDecLevel α let u_2 ← getDecLevel eType let u_3 ← getDecLevel expectedType let eNew := mkAppN (Lean.mkConst ``liftM [u_1, u_2, u_3]) #[m, n, monadLiftVal, α, e] let eNewType ← inferType eNew if (← isDefEq expectedType eNewType) then return eNew -- approach 2 worked else let some monadInst ← isMonad? n | tryCoeSimple let u ← getLevel α let v ← getLevel β let coeTInstType := Lean.mkForall `a BinderInfo.default α $ mkAppN (mkConst ``CoeT [u, v]) #[α, mkBVar 0, β] let coeTInstVal ← synthesizeInst coeTInstType let eNew ← expandCoe (← mkAppN (Lean.mkConst ``liftCoeM [u_1, u_2, u_3]) #[m, n, α, β, monadLiftVal, coeTInstVal, monadInst, e]) let eNewType ← inferType eNew unless (← isDefEq expectedType eNewType) do throwMismatch return eNew -- approach 3 worked catch _ => /- If `m` is not a monad, then we try to use `tryPureCoe?` and then `tryCoe?`. Otherwise, we just try `tryCoe?`. -/ match (← isMonad? m) with | none => tryPureCoeAndSimple | some _ => tryCoeSimple else tryCoeSimple /-- If `expectedType?` is `some t`, then ensure `t` and `eType` are definitionally equal. If they are not, then try coercions. Argument `f?` is used only for generating error messages. -/ def ensureHasTypeAux (expectedType? : Option Expr) (eType : Expr) (e : Expr) (f? : Option Expr := none) (errorMsgHeader? : Option String := none) : TermElabM Expr := do match expectedType? with | none => pure e | some expectedType => if (← isDefEq eType expectedType) then pure e else tryLiftAndCoe errorMsgHeader? expectedType eType e f? /-- If `expectedType?` is `some t`, then ensure `t` and type of `e` are definitionally equal. If they are not, then try coercions. -/ def ensureHasType (expectedType? : Option Expr) (e : Expr) (errorMsgHeader? : Option String := none) : TermElabM Expr := match expectedType? with | none => pure e | _ => do let eType ← inferType e ensureHasTypeAux expectedType? eType e none errorMsgHeader? private def mkSyntheticSorryFor (expectedType? : Option Expr) : TermElabM Expr := do let expectedType ← match expectedType? with | none => mkFreshTypeMVar | some expectedType => pure expectedType mkSyntheticSorry expectedType private def exceptionToSorry (ex : Exception) (expectedType? : Option Expr) : TermElabM Expr := do let syntheticSorry ← mkSyntheticSorryFor expectedType? logException ex pure syntheticSorry /-- If `mayPostpone == true`, throw `Expection.postpone`. -/ def tryPostpone : TermElabM Unit := do if (← read).mayPostpone then throwPostpone /-- If `mayPostpone == true` and `e`'s head is a metavariable, throw `Exception.postpone`. -/ def tryPostponeIfMVar (e : Expr) : TermElabM Unit := do if e.getAppFn.isMVar then let e ← instantiateMVars e if e.getAppFn.isMVar then tryPostpone def tryPostponeIfNoneOrMVar (e? : Option Expr) : TermElabM Unit := match e? with | some e => tryPostponeIfMVar e | none => tryPostpone def tryPostponeIfHasMVars (expectedType? : Option Expr) (msg : String) : TermElabM Expr := do tryPostponeIfNoneOrMVar expectedType? let some expectedType ← pure expectedType? | throwError "{msg}, expected type must be known" let expectedType ← instantiateMVars expectedType if expectedType.hasExprMVar then tryPostpone throwError "{msg}, expected type contains metavariables{indentExpr expectedType}" pure expectedType def saveContext : TermElabM SavedContext := return { macroStack := (← read).macroStack declName? := (← read).declName? options := (← getOptions) openDecls := (← getOpenDecls) errToSorry := (← read).errToSorry } def withSavedContext (savedCtx : SavedContext) (x : TermElabM α) : TermElabM α := do withReader (fun ctx => { ctx with declName? := savedCtx.declName?, macroStack := savedCtx.macroStack, errToSorry := savedCtx.errToSorry }) <| withTheReader Core.Context (fun ctx => { ctx with options := savedCtx.options, openDecls := savedCtx.openDecls }) x private def postponeElabTerm (stx : Syntax) (expectedType? : Option Expr) : TermElabM Expr := do trace[Elab.postpone] "{stx} : {expectedType?}" let mvar ← mkFreshExprMVar expectedType? MetavarKind.syntheticOpaque let ctx ← read registerSyntheticMVar stx mvar.mvarId! (SyntheticMVarKind.postponed (← saveContext)) pure mvar def getSyntheticMVarDecl? (mvarId : MVarId) : TermElabM (Option SyntheticMVarDecl) := return (← get).syntheticMVars.find? fun d => d.mvarId == mvarId def mkTermInfo (elaborator : Name) (stx : Syntax) (e : Expr) (expectedType? : Option Expr := none) (lctx? : Option LocalContext := none) : TermElabM (Sum Info MVarId) := do let isHole? : TermElabM (Option MVarId) := do match e with | Expr.mvar mvarId _ => match (← getSyntheticMVarDecl? mvarId) with | some { kind := SyntheticMVarKind.tactic .., .. } => return mvarId | some { kind := SyntheticMVarKind.postponed .., .. } => return mvarId | _ => return none | _ => pure none match (← isHole?) with | none => return Sum.inl <| Info.ofTermInfo { elaborator, lctx := lctx?.getD (← getLCtx), expr := e, stx, expectedType? } | some mvarId => return Sum.inr mvarId def addTermInfo (stx : Syntax) (e : Expr) (expectedType? : Option Expr := none) (lctx? : Option LocalContext := none) (elaborator := Name.anonymous) : TermElabM Unit := do withInfoContext' (pure ()) (fun _ => mkTermInfo elaborator stx e expectedType? lctx?) |> discard /- Helper function for `elabTerm` is tries the registered elaboration functions for `stxNode` kind until it finds one that supports the syntax or an error is found. -/ private def elabUsingElabFnsAux (s : SavedState) (stx : Syntax) (expectedType? : Option Expr) (catchExPostpone : Bool) : List (KeyedDeclsAttribute.AttributeEntry TermElab) → TermElabM Expr | [] => do throwError "unexpected syntax{indentD stx}" | (elabFn::elabFns) => try -- record elaborator in info tree, but only when not backtracking to other elaborators (outer `try`) withInfoContext' (mkInfo := mkTermInfo elabFn.decl (expectedType? := expectedType?) stx) (try elabFn.value stx expectedType? catch ex => match ex with | Exception.error ref msg => if (← read).errToSorry then exceptionToSorry ex expectedType? else throw ex | Exception.internal id _ => if (← read).errToSorry && id == abortTermExceptionId then exceptionToSorry ex expectedType? else if id == unsupportedSyntaxExceptionId then throw ex -- to outer try else if catchExPostpone && id == postponeExceptionId then /- If `elab` threw `Exception.postpone`, we reset any state modifications. For example, we want to make sure pending synthetic metavariables created by `elab` before it threw `Exception.postpone` are discarded. Note that we are also discarding the messages created by `elab`. For example, consider the expression. `((f.x a1).x a2).x a3` Now, suppose the elaboration of `f.x a1` produces an `Exception.postpone`. Then, a new metavariable `?m` is created. Then, `?m.x a2` also throws `Exception.postpone` because the type of `?m` is not yet known. Then another, metavariable `?n` is created, and finally `?n.x a3` also throws `Exception.postpone`. If we did not restore the state, we would keep "dead" metavariables `?m` and `?n` on the pending synthetic metavariable list. This is wasteful because when we resume the elaboration of `((f.x a1).x a2).x a3`, we start it from scratch and new metavariables are created for the nested functions. -/ s.restore postponeElabTerm stx expectedType? else throw ex) catch ex => match ex with | Exception.internal id _ => if id == unsupportedSyntaxExceptionId then s.restore -- also removes the info tree created above elabUsingElabFnsAux s stx expectedType? catchExPostpone elabFns else throw ex | _ => throw ex private def elabUsingElabFns (stx : Syntax) (expectedType? : Option Expr) (catchExPostpone : Bool) : TermElabM Expr := do let s ← saveState let k := stx.getKind match termElabAttribute.getEntries (← getEnv) k with | [] => throwError "elaboration function for '{k}' has not been implemented{indentD stx}" | elabFns => elabUsingElabFnsAux s stx expectedType? catchExPostpone elabFns instance : MonadMacroAdapter TermElabM where getCurrMacroScope := getCurrMacroScope getNextMacroScope := return (← getThe Core.State).nextMacroScope setNextMacroScope next := modifyThe Core.State fun s => { s with nextMacroScope := next } private def isExplicit (stx : Syntax) : Bool := match stx with | `(@$f) => true | _ => false private def isExplicitApp (stx : Syntax) : Bool := stx.getKind == ``Lean.Parser.Term.app && isExplicit stx[0] /-- Return true if `stx` if a lambda abstraction containing a `{}` or `[]` binder annotation. Example: `fun {α} (a : α) => a` -/ private def isLambdaWithImplicit (stx : Syntax) : Bool := match stx with | `(fun $binders* => $body) => binders.any fun b => b.isOfKind ``Lean.Parser.Term.implicitBinder || b.isOfKind `Lean.Parser.Term.instBinder | _ => false private partial def dropTermParens : Syntax → Syntax := fun stx => match stx with | `(($stx)) => dropTermParens stx | _ => stx private def isHole (stx : Syntax) : Bool := match stx with | `(_) => true | `(? _) => true | `(? $x:ident) => true | _ => false private def isTacticBlock (stx : Syntax) : Bool := match stx with | `(by $x:tacticSeq) => true | _ => false private def isNoImplicitLambda (stx : Syntax) : Bool := match stx with | `(noImplicitLambda% $x:term) => true | _ => false private def isTypeAscription (stx : Syntax) : Bool := match stx with | `(($e : $type)) => true | _ => false def mkNoImplicitLambdaAnnotation (type : Expr) : Expr := mkAnnotation `noImplicitLambda type def hasNoImplicitLambdaAnnotation (type : Expr) : Bool := annotation? `noImplicitLambda type |>.isSome /-- Block usage of implicit lambdas if `stx` is `@f` or `@f arg1 ...` or `fun` with an implicit binder annotation. -/ def blockImplicitLambda (stx : Syntax) : Bool := let stx := dropTermParens stx -- TODO: make it extensible isExplicit stx || isExplicitApp stx || isLambdaWithImplicit stx || isHole stx || isTacticBlock stx || isNoImplicitLambda stx || isTypeAscription stx /-- Return normalized expected type if it is of the form `{a : α} → β` or `[a : α] → β` and `blockImplicitLambda stx` is not true, else return `none`. -/ private def useImplicitLambda? (stx : Syntax) (expectedType? : Option Expr) : TermElabM (Option Expr) := if blockImplicitLambda stx then pure none else match expectedType? with | some expectedType => do if hasNoImplicitLambdaAnnotation expectedType then pure none else let expectedType ← whnfForall expectedType match expectedType with | Expr.forallE _ _ _ c => if c.binderInfo.isExplicit then pure none else pure $ some expectedType | _ => pure none | _ => pure none private def decorateErrorMessageWithLambdaImplicitVars (ex : Exception) (impFVars : Array Expr) : TermElabM Exception := do match ex with | Exception.error ref msg => if impFVars.isEmpty then return Exception.error ref msg else let mut msg := m!"{msg}\nthe following variables have been introduced by the implicit lamda feature" for impFVar in impFVars do let auxMsg := m!"{impFVar} : {← inferType impFVar}" let auxMsg ← addMessageContext auxMsg msg := m!"{msg}{indentD auxMsg}" msg := m!"{msg}\nyou can disable implict lambdas using `@` or writing a lambda expression with `\{}` or `[]` binder annotations." return Exception.error ref msg | _ => return ex private def elabImplicitLambdaAux (stx : Syntax) (catchExPostpone : Bool) (expectedType : Expr) (impFVars : Array Expr) : TermElabM Expr := do let body ← elabUsingElabFns stx expectedType catchExPostpone try let body ← ensureHasType expectedType body let r ← mkLambdaFVars impFVars body trace[Elab.implicitForall] r pure r catch ex => throw (← decorateErrorMessageWithLambdaImplicitVars ex impFVars) private partial def elabImplicitLambda (stx : Syntax) (catchExPostpone : Bool) (type : Expr) : TermElabM Expr := loop type #[] where loop | type@(Expr.forallE n d b c), fvars => if c.binderInfo.isExplicit then elabImplicitLambdaAux stx catchExPostpone type fvars else withFreshMacroScope do let n ← MonadQuotation.addMacroScope n withLocalDecl n c.binderInfo d fun fvar => do let type ← whnfForall (b.instantiate1 fvar) loop type (fvars.push fvar) | type, fvars => elabImplicitLambdaAux stx catchExPostpone type fvars /- Main loop for `elabTerm` -/ private partial def elabTermAux (expectedType? : Option Expr) (catchExPostpone : Bool) (implicitLambda : Bool) : Syntax → TermElabM Expr | Syntax.missing => mkSyntheticSorryFor expectedType? | stx => withFreshMacroScope <| withIncRecDepth do trace[Elab.step] "expected type: {expectedType?}, term\n{stx}" checkMaxHeartbeats "elaborator" withNestedTraces do let env ← getEnv match (← liftMacroM (expandMacroImpl? env stx)) with | some (decl, stxNew) => withInfoContext' (mkInfo := mkTermInfo decl (expectedType? := expectedType?) stx) <| withMacroExpansion stx stxNew <| withRef stxNew <| elabTermAux expectedType? catchExPostpone implicitLambda stxNew | _ => let implicit? ← if implicitLambda && (← read).implicitLambda then useImplicitLambda? stx expectedType? else pure none match implicit? with | some expectedType => elabImplicitLambda stx catchExPostpone expectedType | none => elabUsingElabFns stx expectedType? catchExPostpone /-- Store in the `InfoTree` that `e` is a "dot"-completion target. -/ def addDotCompletionInfo (stx : Syntax) (e : Expr) (expectedType? : Option Expr) (field? : Option Syntax := none) : TermElabM Unit := do addCompletionInfo <| CompletionInfo.dot { expr := e, stx, lctx := (← getLCtx), elaborator := Name.anonymous, expectedType? } (field? := field?) (expectedType? := expectedType?) /-- Main function for elaborating terms. It extracts the elaboration methods from the environment using the node kind. Recall that the environment has a mapping from `SyntaxNodeKind` to `TermElab` methods. It creates a fresh macro scope for executing the elaboration method. All unlogged trace messages produced by the elaboration method are logged using the position information at `stx`. If the elaboration method throws an `Exception.error` and `errToSorry == true`, the error is logged and a synthetic sorry expression is returned. If the elaboration throws `Exception.postpone` and `catchExPostpone == true`, a new synthetic metavariable of kind `SyntheticMVarKind.postponed` is created, registered, and returned. The option `catchExPostpone == false` is used to implement `resumeElabTerm` to prevent the creation of another synthetic metavariable when resuming the elaboration. If `implicitLambda == true`, then disable implicit lambdas feature for the given syntax, but not for its subterms. We use this flag to implement, for example, the `@` modifier. If `Context.implicitLambda == false`, then this parameter has no effect. -/ def elabTerm (stx : Syntax) (expectedType? : Option Expr) (catchExPostpone := true) (implicitLambda := true) : TermElabM Expr := withRef stx <| elabTermAux expectedType? catchExPostpone implicitLambda stx def elabTermEnsuringType (stx : Syntax) (expectedType? : Option Expr) (catchExPostpone := true) (implicitLambda := true) (errorMsgHeader? : Option String := none) : TermElabM Expr := do let e ← elabTerm stx expectedType? catchExPostpone implicitLambda withRef stx <| ensureHasType expectedType? e errorMsgHeader? /-- Execute `x` and return `some` if no new errors were recorded or exceptions was thrown. Otherwise, return `none` -/ def commitIfNoErrors? (x : TermElabM α) : TermElabM (Option α) := do let saved ← saveState modify fun s => { s with messages := {} } try let a ← x if (← get).messages.hasErrors then restoreState saved return none else modify fun s => { s with messages := saved.elab.messages ++ s.messages } return a catch _ => restoreState saved return none /-- Adapt a syntax transformation to a regular, term-producing elaborator. -/ def adaptExpander (exp : Syntax → TermElabM Syntax) : TermElab := fun stx expectedType? => do let stx' ← exp stx withMacroExpansion stx stx' $ elabTerm stx' expectedType? def mkInstMVar (type : Expr) : TermElabM Expr := do let mvar ← mkFreshExprMVar type MetavarKind.synthetic let mvarId := mvar.mvarId! unless (← synthesizeInstMVarCore mvarId) do registerSyntheticMVarWithCurrRef mvarId SyntheticMVarKind.typeClass pure mvar /- Relevant definitions: ``` class CoeSort (α : Sort u) (β : outParam (Sort v)) abbrev coeSort {α : Sort u} {β : Sort v} (a : α) [CoeSort α β] : β ``` -/ private def tryCoeSort (α : Expr) (a : Expr) : TermElabM Expr := do let β ← mkFreshTypeMVar let u ← getLevel α let v ← getLevel β let coeSortInstType := mkAppN (Lean.mkConst ``CoeSort [u, v]) #[α, β] let mvar ← mkFreshExprMVar coeSortInstType MetavarKind.synthetic let mvarId := mvar.mvarId! try withoutMacroStackAtErr do if (← synthesizeCoeInstMVarCore mvarId) then expandCoe <| mkAppN (Lean.mkConst ``coeSort [u, v]) #[α, β, a, mvar] else throwError "type expected" catch | Exception.error _ msg => throwError "type expected\n{msg}" | _ => throwError "type expected" /-- Make sure `e` is a type by inferring its type and making sure it is a `Expr.sort` or is unifiable with `Expr.sort`, or can be coerced into one. -/ def ensureType (e : Expr) : TermElabM Expr := do if (← isType e) then pure e else let eType ← inferType e let u ← mkFreshLevelMVar if (← isDefEq eType (mkSort u)) then pure e else tryCoeSort eType e /-- Elaborate `stx` and ensure result is a type. -/ def elabType (stx : Syntax) : TermElabM Expr := do let u ← mkFreshLevelMVar let type ← elabTerm stx (mkSort u) withRef stx $ ensureType type /-- Enable auto-bound implicits, and execute `k` while catching auto bound implicit exceptions. When an exception is caught, a new local declaration is created, registered, and `k` is tried to be executed again. -/ partial def withAutoBoundImplicit (k : TermElabM α) : TermElabM α := do let flag := autoBoundImplicitLocal.get (← getOptions) if flag then withReader (fun ctx => { ctx with autoBoundImplicit := flag, autoBoundImplicits := {} }) do let rec loop (s : SavedState) : TermElabM α := do try k catch | ex => match isAutoBoundImplicitLocalException? ex with | some n => -- Restore state, declare `n`, and try again s.restore withLocalDecl n BinderInfo.implicit (← mkFreshTypeMVar) fun x => withReader (fun ctx => { ctx with autoBoundImplicits := ctx.autoBoundImplicits.push x } ) do loop (← saveState) | none => throw ex loop (← saveState) else k def withoutAutoBoundImplicit (k : TermElabM α) : TermElabM α := do withReader (fun ctx => { ctx with autoBoundImplicit := false, autoBoundImplicits := {} }) k /-- Return `autoBoundImplicits ++ xs. This methoid throws an error if a variable in `autoBoundImplicits` depends on some `x` in `xs` -/ def addAutoBoundImplicits (xs : Array Expr) : TermElabM (Array Expr) := do let autoBoundImplicits := (← read).autoBoundImplicits for auto in autoBoundImplicits do let localDecl ← getLocalDecl auto.fvarId! for x in xs do if (← getMCtx).localDeclDependsOn localDecl x.fvarId! then throwError "invalid auto implicit argument '{auto}', it depends on explicitly provided argument '{x}'" return autoBoundImplicits.toArray ++ xs def mkAuxName (suffix : Name) : TermElabM Name := do match (← read).declName? with | none => throwError "auxiliary declaration cannot be created when declaration name is not available" | some declName => Lean.mkAuxName (declName ++ suffix) 1 builtin_initialize registerTraceClass `Elab.letrec /- Return true if mvarId is an auxiliary metavariable created for compiling `let rec` or it is delayed assigned to one. -/ def isLetRecAuxMVar (mvarId : MVarId) : TermElabM Bool := do trace[Elab.letrec] "mvarId: {mkMVar mvarId} letrecMVars: {(← get).letRecsToLift.map (mkMVar $ ·.mvarId)}" let mvarId := (← getMCtx).getDelayedRoot mvarId trace[Elab.letrec] "mvarId root: {mkMVar mvarId}" return (← get).letRecsToLift.any (·.mvarId == mvarId) def resolveLocalName (n : Name) : TermElabM (Option (Expr × List String)) := do let lctx ← getLCtx let view := extractMacroScopes n let rec loop (n : Name) (projs : List String) := match lctx.findFromUserName? { view with name := n }.review with | some decl => some (decl.toExpr, projs) | none => match n with | Name.str pre s _ => loop pre (s::projs) | _ => none return loop view.name [] /- Return true iff `stx` is a `Syntax.ident`, and it is a local variable. -/ def isLocalIdent? (stx : Syntax) : TermElabM (Option Expr) := match stx with | Syntax.ident _ _ val _ => do let r? ← resolveLocalName val match r? with | some (fvar, []) => pure (some fvar) | _ => pure none | _ => pure none /-- Create an `Expr.const` using the given name and explicit levels. Remark: fresh universe metavariables are created if the constant has more universe parameters than `explicitLevels`. -/ def mkConst (constName : Name) (explicitLevels : List Level := []) : TermElabM Expr := do let cinfo ← getConstInfo constName if explicitLevels.length > cinfo.levelParams.length then throwError "too many explicit universe levels" else let numMissingLevels := cinfo.levelParams.length - explicitLevels.length let us ← mkFreshLevelMVars numMissingLevels pure $ Lean.mkConst constName (explicitLevels ++ us) private def mkConsts (candidates : List (Name × List String)) (explicitLevels : List Level) : TermElabM (List (Expr × List String)) := do candidates.foldlM (init := []) fun result (constName, projs) => do -- TODO: better suppor for `mkConst` failure. We may want to cache the failures, and report them if all candidates fail. let const ← mkConst constName explicitLevels return (const, projs) :: result def resolveName (stx : Syntax) (n : Name) (preresolved : List (Name × List String)) (explicitLevels : List Level) (expectedType? : Option Expr := none) : TermElabM (List (Expr × List String)) := do try if let some (e, projs) ← resolveLocalName n then unless explicitLevels.isEmpty do throwError "invalid use of explicit universe parameters, '{e}' is a local" return [(e, projs)] -- check for section variable capture by a quotation let ctx ← read if let some (e, projs) := preresolved.findSome? fun (n, projs) => ctx.sectionFVars.find? n |>.map (·, projs) then return [(e, projs)] -- section variables should shadow global decls if preresolved.isEmpty then process (← resolveGlobalName n) else process preresolved catch ex => if preresolved.isEmpty && explicitLevels.isEmpty then addCompletionInfo <| CompletionInfo.id stx stx.getId (danglingDot := false) (← getLCtx) expectedType? throw ex where process (candidates : List (Name × List String)) : TermElabM (List (Expr × List String)) := do if candidates.isEmpty then if (← read).autoBoundImplicit && isValidAutoBoundImplicitName n then throwAutoBoundImplicitLocal n else throwError "unknown identifier '{Lean.mkConst n}'" if preresolved.isEmpty && explicitLevels.isEmpty then addCompletionInfo <| CompletionInfo.id stx stx.getId (danglingDot := false) (← getLCtx) expectedType? mkConsts candidates explicitLevels /-- Similar to `resolveName`, but creates identifiers for the main part and each projection with position information derived from `ident`. Example: Assume resolveName `v.head.bla.boo` produces `(v.head, ["bla", "boo"])`, then this method produces `(v.head, id, [f₁, f₂])` where `id` is an identifier for `v.head`, and `f₁` and `f₂` are identifiers for fields `"bla"` and `"boo"`. -/ def resolveName' (ident : Syntax) (explicitLevels : List Level) (expectedType? : Option Expr := none) : TermElabM (List (Expr × Syntax × List Syntax)) := do match ident with | Syntax.ident info rawStr n preresolved => let r ← resolveName ident n preresolved explicitLevels expectedType? r.mapM fun (c, fields) => do let (cSstr, fields) := fields.foldr (init := (rawStr, [])) fun field (restSstr, fs) => let fieldSstr := restSstr.takeRightWhile (· ≠ '.') ({ restSstr with stopPos := restSstr.stopPos - (fieldSstr.bsize + 1) }, (field, fieldSstr) :: fs) let mkIdentFromPos pos rawVal val := let info := match info with | SourceInfo.original .. => SourceInfo.original "".toSubstring pos "".toSubstring (pos + rawVal.bsize) | _ => SourceInfo.synthetic pos (pos + rawVal.bsize) Syntax.ident info rawVal val [] let id := match c with | Expr.const id _ _ => id | Expr.fvar id _ => id | _ => unreachable! let id := mkIdentFromPos (ident.getPos?.getD 0) cSstr id match info.getPos? with | none => return (c, id, fields.map fun (field, _) => mkIdentFrom ident (Name.mkSimple field)) | some pos => let mut pos := pos + cSstr.bsize + 1 let mut newFields := #[] for (field, fieldSstr) in fields do newFields := newFields.push <| mkIdentFromPos pos fieldSstr (Name.mkSimple field) pos := pos + fieldSstr.bsize + 1 return (c, id, newFields.toList) | _ => throwError "identifier expected" def resolveId? (stx : Syntax) (kind := "term") (withInfo := false) : TermElabM (Option Expr) := match stx with | Syntax.ident _ _ val preresolved => do let rs ← try resolveName stx val preresolved [] catch _ => pure [] let rs := rs.filter fun ⟨f, projs⟩ => projs.isEmpty let fs := rs.map fun (f, _) => f match fs with | [] => pure none | [f] => if withInfo then addTermInfo stx f pure (some f) | _ => throwError "ambiguous {kind}, use fully qualified name, possible interpretations {fs}" | _ => throwError "identifier expected" private def mkSomeContext : Context := { fileName := "<TermElabM>" fileMap := arbitrary } def TermElabM.run (x : TermElabM α) (ctx : Context := mkSomeContext) (s : State := {}) : MetaM (α × State) := withConfig setElabConfig (x ctx |>.run s) @[inline] def TermElabM.run' (x : TermElabM α) (ctx : Context := mkSomeContext) (s : State := {}) : MetaM α := (·.1) <$> x.run ctx s def TermElabM.toIO (x : TermElabM α) (ctxCore : Core.Context) (sCore : Core.State) (ctxMeta : Meta.Context) (sMeta : Meta.State) (ctx : Context) (s : State) : IO (α × Core.State × Meta.State × State) := do let ((a, s), sCore, sMeta) ← (x.run ctx s).toIO ctxCore sCore ctxMeta sMeta pure (a, sCore, sMeta, s) instance [MetaEval α] : MetaEval (TermElabM α) where eval env opts x _ := let x : TermElabM α := do try x finally let s ← get s.messages.forM fun msg => do IO.println (← msg.toString) MetaEval.eval env opts (hideUnit := true) $ x.run' mkSomeContext unsafe def evalExpr (α) (typeName : Name) (value : Expr) : TermElabM α := withoutModifyingEnv do let name ← mkFreshUserName `_tmp let type ← inferType value let type ← whnfD type unless type.isConstOf typeName do throwError "unexpected type at evalExpr{indentExpr type}" let decl := Declaration.defnDecl { name := name, levelParams := [], type := type, value := value, hints := ReducibilityHints.opaque, safety := DefinitionSafety.unsafe } ensureNoUnassignedMVars decl addAndCompile decl evalConst α name private def throwStuckAtUniverseCnstr : TermElabM Unit := do -- This code assumes `entries` is not empty. Note that `processPostponed` uses `exceptionOnFailure` to guarantee this property let entries ← getPostponed let mut found : Std.HashSet (Level × Level) := {} let mut uniqueEntries := #[] for entry in entries do let mut lhs := entry.lhs let mut rhs := entry.rhs if Level.normLt rhs lhs then (lhs, rhs) := (rhs, lhs) unless found.contains (lhs, rhs) do found := found.insert (lhs, rhs) uniqueEntries := uniqueEntries.push entry for i in [1:uniqueEntries.size] do logErrorAt uniqueEntries[i].ref (← mkLevelStuckErrorMessage uniqueEntries[i]) throwErrorAt uniqueEntries[0].ref (← mkLevelStuckErrorMessage uniqueEntries[0]) def withoutPostponingUniverseConstraints (x : TermElabM α) : TermElabM α := do let postponed ← getResetPostponed try let a ← x unless (← processPostponed (mayPostpone := false) (exceptionOnFailure := true)) do throwStuckAtUniverseCnstr setPostponed postponed return a catch ex => setPostponed postponed throw ex end Term builtin_initialize registerTraceClass `Elab.postpone registerTraceClass `Elab.coe registerTraceClass `Elab.debug export Term (TermElabM) end Lean.Elab
f6eb1c60a68632fce07e81583ca9494da568b404
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/group_theory/specific_groups/quaternion.lean
cc2948abfd190148b8d0794b8b2330b74cb5110b
[ "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
8,154
lean
/- Copyright (c) 2021 Julian Kuelshammer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Julian Kuelshammer -/ import data.zmod.basic import group_theory.order_of_element import data.nat.basic import tactic.interval_cases import group_theory.specific_groups.dihedral import group_theory.specific_groups.cyclic /-! # Quaternion Groups We define the (generalised) quaternion groups `quaternion_group n` of order `4n`, also known as dicyclic groups, with elements `a i` and `xa i` for `i : zmod n`. The (generalised) quaternion groups can be defined by the presentation $\langle a, x | a^{2n} = 1, x^2 = a^n, x^{-1}ax=a^{-1}\rangle$. We write `a i` for $a^i$ and `xa i` for $x * a^i$. For `n=2` the quaternion group `quaternion_group 2` is isomorphic to the unit integral quaternions `units (quaternion ℤ)`. ## Main definition `quaternion_group n`: The (generalised) quaternion group of order `4n`. ## Implementation notes This file is heavily based on `dihedral_group` by Shing Tak Lam. In mathematics, the name "quaternion group" is reserved for the cases `n ≥ 2`. Since it would be inconvenient to carry around this condition we define `quaternion_group` also for `n = 0` and `n = 1`. `quaternion_group 0` is isomorphic to the infinite dihedral group, while `quaternion_group 1` is isomorphic to a cyclic group of order `4`. ## References * https://en.wikipedia.org/wiki/Dicyclic_group * https://en.wikipedia.org/wiki/Quaternion_group ## TODO Show that `quaternion_group 2 ≃* units (quaternion ℤ)`. -/ /-- The (generalised) quaternion group `quaternion_group n` of order `4n`. It can be defined by the presentation $\langle a, x | a^{2n} = 1, x^2 = a^n, x^{-1}ax=a^{-1}\rangle$. We write `a i` for $a^i$ and `xa i` for $x * a^i$. -/ @[derive decidable_eq] inductive quaternion_group (n : ℕ) : Type | a : zmod (2 * n) → quaternion_group | xa : zmod (2 * n) → quaternion_group namespace quaternion_group variables {n : ℕ} /-- Multiplication of the dihedral group. -/ private def mul : quaternion_group n → quaternion_group n → quaternion_group n | (a i) (a j) := a (i + j) | (a i) (xa j) := xa (j - i) | (xa i) (a j) := xa (i + j) | (xa i) (xa j) := a (n + j - i) /-- The identity `1` is given by `aⁱ`. -/ private def one : quaternion_group n := a 0 instance : inhabited (quaternion_group n) := ⟨one⟩ /-- The inverse of an element of the quaternion group. -/ private def inv : quaternion_group n → quaternion_group n | (a i) := a (-i) | (xa i) := xa (n + i) /-- The group structure on `quaternion_group n`. -/ instance : group (quaternion_group n) := { mul := mul, mul_assoc := begin rintros (i | i) (j | j) (k | k); simp only [mul]; abel, simp only [neg_mul_eq_neg_mul_symm, one_mul, int.cast_one, zsmul_eq_mul, int.cast_neg, add_right_inj], calc -(n : zmod (2 * n)) = 0 - n : by rw zero_sub ... = 2 * n - n : by { norm_cast, simp, } ... = n : by ring end, one := one, one_mul := begin rintros (i | i), { exact congr_arg a (zero_add i) }, { exact congr_arg xa (sub_zero i) }, end, mul_one := begin rintros (i | i), { exact congr_arg a (add_zero i) }, { exact congr_arg xa (add_zero i) }, end, inv := inv, mul_left_inv := begin rintros (i | i), { exact congr_arg a (neg_add_self i) }, { exact congr_arg a (sub_self (n + i)) }, end } variable {n} @[simp] lemma a_mul_a (i j : zmod (2 * n)) : a i * a j = a (i + j) := rfl @[simp] lemma a_mul_xa (i j : zmod (2 * n)) : a i * xa j = xa (j - i) := rfl @[simp] lemma xa_mul_a (i j : zmod (2 * n)) : xa i * a j = xa (i + j) := rfl @[simp] lemma xa_mul_xa (i j : zmod (2 * n)) : xa i * xa j = a (n + j - i) := rfl lemma one_def : (1 : quaternion_group n) = a 0 := rfl private def fintype_helper : (zmod (2 * n) ⊕ zmod (2 * n)) ≃ quaternion_group n := { inv_fun := λ i, match i with | (a j) := sum.inl j | (xa j) := sum.inr j end, to_fun := λ i, match i with | (sum.inl j) := a j | (sum.inr j) := xa j end, left_inv := by rintro (x | x); refl, right_inv := by rintro (x | x); refl } /-- The special case that more or less by definition `quaternion_group 0` is isomorphic to the infinite dihedral group. -/ def quaternion_group_zero_equiv_dihedral_group_zero : quaternion_group 0 ≃* dihedral_group 0 := { to_fun := λ i, quaternion_group.rec_on i dihedral_group.r dihedral_group.sr, inv_fun := λ i, match i with | (dihedral_group.r j) := a j | (dihedral_group.sr j) := xa j end, left_inv := by rintro (k | k); refl, right_inv := by rintro (k | k); refl, map_mul' := by { rintros (k | k) (l | l); { dsimp, simp, }, } } /-- Some of the lemmas on `zmod m` require that `m` is positive, as `m = 2 * n` is the case relevant in this file but we don't want to write `[fact (0 < 2 * n)]` we make this lemma a local instance. -/ private lemma succ_mul_pos_fact {m : ℕ} [hn : fact (0 < n)] : fact (0 < (nat.succ m) * n) := ⟨nat.succ_mul_pos m hn.1⟩ local attribute [instance] succ_mul_pos_fact /-- If `0 < n`, then `quaternion_group n` is a finite group. -/ instance [fact (0 < n)] : fintype (quaternion_group n) := fintype.of_equiv _ fintype_helper instance : nontrivial (quaternion_group n) := ⟨⟨a 0, xa 0, dec_trivial⟩⟩ /-- If `0 < n`, then `quaternion_group n` has `4n` elements. -/ lemma card [fact (0 < n)] : fintype.card (quaternion_group n) = 4 * n := begin rw [← fintype.card_eq.mpr ⟨fintype_helper⟩, fintype.card_sum, zmod.card, two_mul], ring end @[simp] lemma a_one_pow (k : ℕ) : (a 1 : quaternion_group n) ^ k = a k := begin induction k with k IH, { refl }, { rw [pow_succ, IH, a_mul_a], congr' 1, norm_cast, rw nat.one_add } end @[simp] lemma a_one_pow_n : (a 1 : quaternion_group n)^(2 * n) = 1 := begin cases n, { simp_rw [mul_zero, pow_zero] }, { rw [a_one_pow, one_def], congr' 1, exact zmod.nat_cast_self _ } end @[simp] lemma xa_sq (i : zmod (2 * n)) : xa i ^ 2 = a n := begin simp [sq] end @[simp] lemma xa_pow_four (i : zmod (2 * n)) : xa i ^ 4 = 1 := begin simp only [pow_succ, sq, xa_mul_xa, xa_mul_a, add_sub_cancel, add_sub_assoc, add_sub_cancel', sub_self, add_zero], norm_cast, rw ← two_mul, simp [one_def], end /-- If `0 < n`, then `xa i` has order 4. -/ @[simp] lemma order_of_xa [hpos : fact (0 < n)] (i : zmod (2 * n)) : order_of (xa i) = 4 := begin change _ = 2^2, haveI : fact(nat.prime 2) := fact.mk (nat.prime_two), apply order_of_eq_prime_pow, { intro h, simp only [pow_one, xa_sq] at h, injection h with h', apply_fun zmod.val at h', apply_fun ( / n) at h', simp only [zmod.val_nat_cast, zmod.val_zero, nat.zero_div, nat.mod_mul_left_div_self, nat.div_self hpos.1] at h', norm_num at h' }, { norm_num } end /-- In the special case `n = 1`, `quaternion 1` is a cyclic group (of order `4`). -/ lemma quaternion_group_one_is_cyclic : is_cyclic (quaternion_group 1) := begin apply is_cyclic_of_order_of_eq_card, rw [card, mul_one], exact order_of_xa 0 end /-- If `0 < n`, then `a 1` has order `2 * n`. -/ @[simp] lemma order_of_a_one [hn : fact (0 < n)] : order_of (a 1 : quaternion_group n) = 2 * n := begin cases (nat.le_of_dvd (nat.succ_mul_pos _ hn.1) (order_of_dvd_of_pow_eq_one (@a_one_pow_n n))).lt_or_eq with h h, { have h1 : (a 1 : quaternion_group n)^(order_of (a 1)) = 1 := pow_order_of_eq_one _, rw a_one_pow at h1, injection h1 with h2, rw [← zmod.val_eq_zero, zmod.val_nat_cast, nat.mod_eq_of_lt h] at h2, exact absurd h2.symm (order_of_pos _).ne }, { exact h } end /-- If `0 < n`, then `a i` has order `(2 * n) / gcd (2 * n) i`. -/ lemma order_of_a [fact (0 < n)] (i : zmod (2 * n)) : order_of (a i) = (2 * n) / nat.gcd (2 * n) i.val := begin conv_lhs { rw ← zmod.nat_cast_zmod_val i }, rw [← a_one_pow, order_of_pow, order_of_a_one] end end quaternion_group
176dc9d06f3a2a78abc42e606e8dfd0876d80e09
d1a52c3f208fa42c41df8278c3d280f075eb020c
/tests/lean/run/balg.lean
03913e650e267946bd6c9e12c9dc204cdca4f0a8
[ "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
2,467
lean
import Lean universe u structure Magma where α : Type u mul : α → α → α instance : CoeSort Magma (Type u) where coe s := s.α abbrev mul {M : Magma} (a b : M) : M := M.mul a b set_option pp.all true infixl:70 (priority := high) "*" => mul structure Semigroup extends Magma where mul_assoc (a b c : α) : a * b * c = a * (b * c) instance : CoeSort Semigroup (Type u) where coe s := s.α structure CommSemigroup extends Semigroup where mul_comm (a b : α) : a * b = b * a structure Monoid extends Semigroup where one : α one_mul (a : α) : one * a = a mul_one (a : α) : a * one = a instance : CoeSort Monoid (Type u) where coe s := s.α structure CommMonoid extends Monoid where mul_comm (a b : α) : a * b = b * a instance : Coe CommMonoid CommSemigroup where coe s := { α := s.α mul := s.mul mul_assoc := s.mul_assoc mul_comm := s.mul_comm } structure Group extends Monoid where inv : α → α mul_left_inv (a : α) : (inv a) * a = one instance : CoeSort Group (Type u) where coe s := s.α abbrev inv {G : Group} (a : G) : G := G.inv a postfix:max "⁻¹" => inv instance (G : Group) : OfNat (coeSort G.toMagma) (nat_lit 1) where ofNat := G.one instance (G : Group) : OfNat (G.toMagma.α) (nat_lit 1) where ofNat := G.one structure CommGroup extends Group where mul_comm (a b : α) : a * b = b * a instance : CoeSort CommGroup (Type u) where coe s := s.α theorem inv_mul_cancel_left {G : Group} (a b : G) : a⁻¹ * (a * b) = b := by rw [← G.mul_assoc, G.mul_left_inv, G.one_mul] theorem toMonoidOneEq {G : Group} : G.toMonoid.one = 1 := rfl theorem inv_eq_of_mul_eq_one {G : Group} {a b : G} (h : a * b = 1) : a⁻¹ = b := by rw [← G.mul_one a⁻¹, toMonoidOneEq, ←h, ← G.mul_assoc, G.mul_left_inv, G.one_mul] theorem inv_inv {G : Group} (a : G) : (a⁻¹)⁻¹ = a := inv_eq_of_mul_eq_one (G.mul_left_inv a) theorem mul_right_inv {G : Group} (a : G) : a * a⁻¹ = 1 := by have : a⁻¹⁻¹ * a⁻¹ = 1 := by rw [G.mul_left_inv]; rfl rw [inv_inv] at this assumption unif_hint (G : Group) where |- G.toMonoid.one =?= 1 theorem mul_inv_rev {G : Group} (a b : G) : (a * b)⁻¹ = b⁻¹ * a⁻¹ := by apply inv_eq_of_mul_eq_one rw [G.mul_assoc, ← G.mul_assoc b, mul_right_inv, G.one_mul, mul_right_inv] theorem mul_inv {G : CommGroup} (a b : G) : (a * b)⁻¹ = a⁻¹ * b⁻¹ := by rw [mul_inv_rev, G.mul_comm]
b6402c4f9820cc826a743c9ccebe821ea6c57612
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/category_theory/category/Cat.lean
ac6e85b33dc6a17d93348236deb6a977288d2243
[ "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
2,517
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import category_theory.concrete_category import category_theory.discrete_category import category_theory.eq_to_hom /-! # Category of categories This file contains the definition of the category `Cat` of all categories. In this category objects are categories and morphisms are functors between these categories. ## Implementation notes Though `Cat` is not a concrete category, we use `bundled` to define its carrier type. -/ universes v u namespace category_theory /-- Category of categories. -/ @[nolint check_univs] -- intended to be used with explicit universe parameters def Cat := bundled category.{v u} namespace Cat instance : inhabited Cat := ⟨⟨Type u, category_theory.types⟩⟩ instance : has_coe_to_sort Cat := { S := Type u, coe := bundled.α } instance str (C : Cat.{v u}) : category.{v u} C := C.str /-- Construct a bundled `Cat` from the underlying type and the typeclass. -/ def of (C : Type u) [category.{v} C] : Cat.{v u} := bundled.of C /-- Category structure on `Cat` -/ instance category : large_category.{max v u} Cat.{v u} := { hom := λ C D, C ⥤ D, id := λ C, 𝟭 C, comp := λ C D E F G, F ⋙ G, id_comp' := λ C D F, by cases F; refl, comp_id' := λ C D F, by cases F; refl, assoc' := by intros; refl } /-- Functor that gets the set of objects of a category. It is not called `forget`, because it is not a faithful functor. -/ def objects : Cat.{v u} ⥤ Type u := { obj := λ C, C, map := λ C D F, F.obj } /-- Any isomorphism in `Cat` induces an equivalence of the underlying categories. -/ def equiv_of_iso {C D : Cat} (γ : C ≅ D) : C ≌ D := { functor := γ.hom, inverse := γ.inv, unit_iso := eq_to_iso $ eq.symm γ.hom_inv_id, counit_iso := eq_to_iso γ.inv_hom_id } end Cat /-- Embedding `Type` into `Cat` as discrete categories. This ought to be modelled as a 2-functor! -/ @[simps] def Type_to_Cat : Type u ⥤ Cat := { obj := λ X, Cat.of (discrete X), map := λ X Y f, discrete.functor f, map_id' := λ X, begin apply functor.ext, tidy, end, map_comp' := λ X Y Z f g, begin apply functor.ext, tidy, end } instance : faithful Type_to_Cat.{u} := {} instance : full Type_to_Cat.{u} := { preimage := λ X Y F, F.obj, witness' := begin intros X Y F, apply functor.ext, { intros x y f, dsimp, ext, }, { intros x, refl, } end } end category_theory