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
196af943446dfc0bec791d8152c0ef072cea958d
9dc8cecdf3c4634764a18254e94d43da07142918
/src/algebra/order/sub.lean
bf808bdee5ceaec37c738fdf49dbeae8953824dc
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
31,754
lean
/- Copyright (c) 2021 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import algebra.order.monoid /-! # Ordered Subtraction This file proves lemmas relating (truncated) subtraction with an order. We provide a class `has_ordered_sub` stating that `a - b ≤ c ↔ a ≤ c + b`. The subtraction discussed here could both be normal subtraction in an additive group or truncated subtraction on a canonically ordered monoid (`ℕ`, `multiset`, `part_enat`, `ennreal`, ...) ## Implementation details `has_ordered_sub` is a mixin type-class, so that we can use the results in this file even in cases where we don't have a `canonically_ordered_add_monoid` instance (even though that is our main focus). Conversely, this means we can use `canonically_ordered_add_monoid` without necessarily having to define a subtraction. The results in this file are ordered by the type-class assumption needed to prove it. This means that similar results might not be close to each other. Furthermore, we don't prove implications if a bi-implication can be proven under the same assumptions. Lemmas using this class are named using `tsub` instead of `sub` (short for "truncated subtraction"). This is to avoid naming conflicts with similar lemmas about ordered groups. We provide a second version of most results that require `[contravariant_class α α (+) (≤)]`. In the second version we replace this type-class assumption by explicit `add_le_cancellable` assumptions. TODO: maybe we should make a multiplicative version of this, so that we can replace some identical lemmas about subtraction/division in `ordered_[add_]comm_group` with these. TODO: generalize `nat.le_of_le_of_sub_le_sub_right`, `nat.sub_le_sub_right_iff`, `nat.mul_self_sub_mul_self_eq` -/ variables {α β : Type*} /-- `has_ordered_sub α` means that `α` has a subtraction characterized by `a - b ≤ c ↔ a ≤ c + b`. In other words, `a - b` is the least `c` such that `a ≤ b + c`. This is satisfied both by the subtraction in additive ordered groups and by truncated subtraction in canonically ordered monoids on many specific types. -/ class has_ordered_sub (α : Type*) [has_le α] [has_add α] [has_sub α] := (tsub_le_iff_right : ∀ a b c : α, a - b ≤ c ↔ a ≤ c + b) section has_add variables [preorder α] [has_add α] [has_sub α] [has_ordered_sub α] {a b c d : α} @[simp] lemma tsub_le_iff_right : a - b ≤ c ↔ a ≤ c + b := has_ordered_sub.tsub_le_iff_right a b c /-- See `add_tsub_cancel_right` for the equality if `contravariant_class α α (+) (≤)`. -/ lemma add_tsub_le_right : a + b - b ≤ a := tsub_le_iff_right.mpr le_rfl lemma le_tsub_add : b ≤ (b - a) + a := tsub_le_iff_right.mp le_rfl lemma add_hom.le_map_tsub [preorder β] [has_add β] [has_sub β] [has_ordered_sub β] (f : add_hom α β) (hf : monotone f) (a b : α) : f a - f b ≤ f (a - b) := by { rw [tsub_le_iff_right, ← f.map_add], exact hf le_tsub_add } lemma le_mul_tsub {R : Type*} [distrib R] [preorder R] [has_sub R] [has_ordered_sub R] [covariant_class R R (*) (≤)] {a b c : R} : a * b - a * c ≤ a * (b - c) := (add_hom.mul_left a).le_map_tsub (monotone_id.const_mul' a) _ _ lemma le_tsub_mul {R : Type*} [comm_semiring R] [preorder R] [has_sub R] [has_ordered_sub R] [covariant_class R R (*) (≤)] {a b c : R} : a * c - b * c ≤ (a - b) * c := by simpa only [mul_comm _ c] using le_mul_tsub end has_add /-- An order isomorphism between types with ordered subtraction preserves subtraction provided that it preserves addition. -/ lemma order_iso.map_tsub {M N : Type*} [preorder M] [has_add M] [has_sub M] [has_ordered_sub M] [partial_order N] [has_add N] [has_sub N] [has_ordered_sub N] (e : M ≃o N) (h_add : ∀ a b, e (a + b) = e a + e b) (a b : M) : e (a - b) = e a - e b := begin set e_add : M ≃+ N := { map_add' := h_add, .. e }, refine le_antisymm _ (e_add.to_add_hom.le_map_tsub e.monotone a b), suffices : e (e.symm (e a) - e.symm (e b)) ≤ e (e.symm (e a - e b)), by simpa, exact e.monotone (e_add.symm.to_add_hom.le_map_tsub e.symm.monotone _ _) end /-! ### Preorder -/ section ordered_add_comm_semigroup section preorder variables [preorder α] section add_comm_semigroup variables [add_comm_semigroup α] [has_sub α] [has_ordered_sub α] {a b c d : α} lemma tsub_le_iff_left : a - b ≤ c ↔ a ≤ b + c := by rw [tsub_le_iff_right, add_comm] lemma le_add_tsub : a ≤ b + (a - b) := tsub_le_iff_left.mp le_rfl /-- See `add_tsub_cancel_left` for the equality if `contravariant_class α α (+) (≤)`. -/ lemma add_tsub_le_left : a + b - a ≤ b := tsub_le_iff_left.mpr le_rfl lemma tsub_le_tsub_right (h : a ≤ b) (c : α) : a - c ≤ b - c := tsub_le_iff_left.mpr $ h.trans le_add_tsub lemma tsub_le_iff_tsub_le : a - b ≤ c ↔ a - c ≤ b := by rw [tsub_le_iff_left, tsub_le_iff_right] /-- See `tsub_tsub_cancel_of_le` for the equality. -/ lemma tsub_tsub_le : b - (b - a) ≤ a := tsub_le_iff_right.mpr le_add_tsub section cov variable [covariant_class α α (+) (≤)] lemma tsub_le_tsub_left (h : a ≤ b) (c : α) : c - b ≤ c - a := tsub_le_iff_left.mpr $ le_add_tsub.trans $ add_le_add_right h _ lemma tsub_le_tsub (hab : a ≤ b) (hcd : c ≤ d) : a - d ≤ b - c := (tsub_le_tsub_right hab _).trans $ tsub_le_tsub_left hcd _ lemma antitone_const_tsub : antitone (λ x, c - x) := λ x y hxy, tsub_le_tsub rfl.le hxy /-- See `add_tsub_assoc_of_le` for the equality. -/ lemma add_tsub_le_assoc : a + b - c ≤ a + (b - c) := by { rw [tsub_le_iff_left, add_left_comm], exact add_le_add_left le_add_tsub a } /-- See `tsub_add_eq_add_tsub` for the equality. -/ lemma add_tsub_le_tsub_add : a + b - c ≤ a - c + b := by { rw [add_comm, add_comm _ b], exact add_tsub_le_assoc } lemma add_le_add_add_tsub : a + b ≤ (a + c) + (b - c) := by { rw [add_assoc], exact add_le_add_left le_add_tsub a } lemma le_tsub_add_add : a + b ≤ (a - c) + (b + c) := by { rw [add_comm a, add_comm (a - c)], exact add_le_add_add_tsub } lemma tsub_le_tsub_add_tsub : a - c ≤ (a - b) + (b - c) := begin rw [tsub_le_iff_left, ← add_assoc, add_right_comm], exact le_add_tsub.trans (add_le_add_right le_add_tsub _), end lemma tsub_tsub_tsub_le_tsub : (c - a) - (c - b) ≤ b - a := begin rw [tsub_le_iff_left, tsub_le_iff_left, add_left_comm], exact le_tsub_add.trans (add_le_add_left le_add_tsub _), end lemma tsub_tsub_le_tsub_add {a b c : α} : a - (b - c) ≤ a - b + c := tsub_le_iff_right.2 $ calc a ≤ a - b + b : le_tsub_add ... ≤ a - b + (c + (b - c)) : add_le_add_left le_add_tsub _ ... = a - b + c + (b - c) : (add_assoc _ _ _).symm /-- See `tsub_add_tsub_comm` for the equality. -/ lemma add_tsub_add_le_tsub_add_tsub : a + b - (c + d) ≤ a - c + (b - d) := begin rw [add_comm c, tsub_le_iff_left, add_assoc, ←tsub_le_iff_left, ←tsub_le_iff_left], refine (tsub_le_tsub_right add_tsub_le_assoc c).trans _, rw [add_comm a, add_comm (a - c)], exact add_tsub_le_assoc, end /-- See `add_tsub_add_eq_tsub_left` for the equality. -/ lemma add_tsub_add_le_tsub_left : a + b - (a + c) ≤ b - c := by { rw [tsub_le_iff_left, add_assoc], exact add_le_add_left le_add_tsub _ } /-- See `add_tsub_add_eq_tsub_right` for the equality. -/ lemma add_tsub_add_le_tsub_right : a + c - (b + c) ≤ a - b := by { rw [tsub_le_iff_left, add_right_comm], exact add_le_add_right le_add_tsub c } end cov /-! #### Lemmas that assume that an element is `add_le_cancellable` -/ namespace add_le_cancellable protected lemma le_add_tsub_swap (hb : add_le_cancellable b) : a ≤ b + a - b := hb le_add_tsub protected lemma le_add_tsub (hb : add_le_cancellable b) : a ≤ a + b - b := by { rw add_comm, exact hb.le_add_tsub_swap } protected lemma le_tsub_of_add_le_left (ha : add_le_cancellable a) (h : a + b ≤ c) : b ≤ c - a := ha $ h.trans le_add_tsub protected lemma le_tsub_of_add_le_right (hb : add_le_cancellable b) (h : a + b ≤ c) : a ≤ c - b := hb.le_tsub_of_add_le_left $ by rwa add_comm end add_le_cancellable /-! ### Lemmas where addition is order-reflecting -/ section contra variable [contravariant_class α α (+) (≤)] lemma le_add_tsub_swap : a ≤ b + a - b := contravariant.add_le_cancellable.le_add_tsub_swap lemma le_add_tsub' : a ≤ a + b - b := contravariant.add_le_cancellable.le_add_tsub lemma le_tsub_of_add_le_left (h : a + b ≤ c) : b ≤ c - a := contravariant.add_le_cancellable.le_tsub_of_add_le_left h lemma le_tsub_of_add_le_right (h : a + b ≤ c) : a ≤ c - b := contravariant.add_le_cancellable.le_tsub_of_add_le_right h end contra end add_comm_semigroup variables [add_comm_monoid α] [has_sub α] [has_ordered_sub α] {a b c d : α} lemma tsub_nonpos : a - b ≤ 0 ↔ a ≤ b := by rw [tsub_le_iff_left, add_zero] alias tsub_nonpos ↔ _ tsub_nonpos_of_le lemma add_monoid_hom.le_map_tsub [preorder β] [add_comm_monoid β] [has_sub β] [has_ordered_sub β] (f : α →+ β) (hf : monotone f) (a b : α) : f a - f b ≤ f (a - b) := f.to_add_hom.le_map_tsub hf a b end preorder /-! ### Partial order -/ variables [partial_order α] [add_comm_semigroup α] [has_sub α] [has_ordered_sub α] {a b c d : α} lemma tsub_tsub (b a c : α) : b - a - c = b - (a + c) := begin apply le_antisymm, { rw [tsub_le_iff_left, tsub_le_iff_left, ← add_assoc, ← tsub_le_iff_left] }, { rw [tsub_le_iff_left, add_assoc, ← tsub_le_iff_left, ← tsub_le_iff_left] } end lemma tsub_add_eq_tsub_tsub (a b c : α) : a - (b + c) = a - b - c := (tsub_tsub _ _ _).symm lemma tsub_add_eq_tsub_tsub_swap (a b c : α) : a - (b + c) = a - c - b := by { rw [add_comm], apply tsub_add_eq_tsub_tsub } lemma tsub_right_comm : a - b - c = a - c - b := by simp_rw [← tsub_add_eq_tsub_tsub, add_comm] /-! ### Lemmas that assume that an element is `add_le_cancellable`. -/ namespace add_le_cancellable protected lemma tsub_eq_of_eq_add (hb : add_le_cancellable b) (h : a = c + b) : a - b = c := le_antisymm (tsub_le_iff_right.mpr h.le) $ by { rw h, exact hb.le_add_tsub } protected lemma eq_tsub_of_add_eq (hc : add_le_cancellable c) (h : a + c = b) : a = b - c := (hc.tsub_eq_of_eq_add h.symm).symm protected theorem tsub_eq_of_eq_add_rev (hb : add_le_cancellable b) (h : a = b + c) : a - b = c := hb.tsub_eq_of_eq_add $ by rw [add_comm, h] @[simp] protected lemma add_tsub_cancel_right (hb : add_le_cancellable b) : a + b - b = a := hb.tsub_eq_of_eq_add $ by rw [add_comm] @[simp] protected lemma add_tsub_cancel_left (ha : add_le_cancellable a) : a + b - a = b := ha.tsub_eq_of_eq_add $ add_comm a b protected lemma lt_add_of_tsub_lt_left (hb : add_le_cancellable b) (h : a - b < c) : a < b + c := begin rw [lt_iff_le_and_ne, ← tsub_le_iff_left], refine ⟨h.le, _⟩, rintro rfl, simpa [hb] using h, end protected lemma lt_add_of_tsub_lt_right (hc : add_le_cancellable c) (h : a - c < b) : a < b + c := begin rw [lt_iff_le_and_ne, ← tsub_le_iff_right], refine ⟨h.le, _⟩, rintro rfl, simpa [hc] using h, end protected lemma lt_tsub_of_add_lt_right (hc : add_le_cancellable c) (h : a + c < b) : a < b - c := (hc.le_tsub_of_add_le_right h.le).lt_of_ne $ by { rintro rfl, exact h.not_le le_tsub_add } protected lemma lt_tsub_of_add_lt_left (ha : add_le_cancellable a) (h : a + c < b) : c < b - a := ha.lt_tsub_of_add_lt_right $ by rwa add_comm end add_le_cancellable /-! #### Lemmas where addition is order-reflecting. -/ section contra variable [contravariant_class α α (+) (≤)] lemma tsub_eq_of_eq_add (h : a = c + b) : a - b = c := contravariant.add_le_cancellable.tsub_eq_of_eq_add h lemma eq_tsub_of_add_eq (h : a + c = b) : a = b - c := contravariant.add_le_cancellable.eq_tsub_of_add_eq h lemma tsub_eq_of_eq_add_rev (h : a = b + c) : a - b = c := contravariant.add_le_cancellable.tsub_eq_of_eq_add_rev h @[simp] lemma add_tsub_cancel_right (a b : α) : a + b - b = a := contravariant.add_le_cancellable.add_tsub_cancel_right @[simp] lemma add_tsub_cancel_left (a b : α) : a + b - a = b := contravariant.add_le_cancellable.add_tsub_cancel_left lemma lt_add_of_tsub_lt_left (h : a - b < c) : a < b + c := contravariant.add_le_cancellable.lt_add_of_tsub_lt_left h lemma lt_add_of_tsub_lt_right (h : a - c < b) : a < b + c := contravariant.add_le_cancellable.lt_add_of_tsub_lt_right h /-- This lemma (and some of its corollaries) also holds for `ennreal`, but this proof doesn't work for it. Maybe we should add this lemma as field to `has_ordered_sub`? -/ lemma lt_tsub_of_add_lt_left : a + c < b → c < b - a := contravariant.add_le_cancellable.lt_tsub_of_add_lt_left lemma lt_tsub_of_add_lt_right : a + c < b → a < b - c := contravariant.add_le_cancellable.lt_tsub_of_add_lt_right end contra section both variables [covariant_class α α (+) (≤)] [contravariant_class α α (+) (≤)] lemma add_tsub_add_eq_tsub_right (a c b : α) : (a + c) - (b + c) = a - b := begin refine add_tsub_add_le_tsub_right.antisymm (tsub_le_iff_right.2 $ le_of_add_le_add_right _), swap, rw add_assoc, exact le_tsub_add, end lemma add_tsub_add_eq_tsub_left (a b c : α) : (a + b) - (a + c) = b - c := by rw [add_comm a b, add_comm a c, add_tsub_add_eq_tsub_right] end both end ordered_add_comm_semigroup /-! ### Lemmas in a linearly ordered monoid. -/ section linear_order variables {a b c d : α} [linear_order α] [add_comm_semigroup α] [has_sub α] [has_ordered_sub α] /-- See `lt_of_tsub_lt_tsub_right_of_le` for a weaker statement in a partial order. -/ lemma lt_of_tsub_lt_tsub_right (h : a - c < b - c) : a < b := lt_imp_lt_of_le_imp_le (λ h, tsub_le_tsub_right h c) h /-- See `lt_tsub_iff_right_of_le` for a weaker statement in a partial order. -/ lemma lt_tsub_iff_right : a < b - c ↔ a + c < b := lt_iff_lt_of_le_iff_le tsub_le_iff_right /-- See `lt_tsub_iff_left_of_le` for a weaker statement in a partial order. -/ lemma lt_tsub_iff_left : a < b - c ↔ c + a < b := lt_iff_lt_of_le_iff_le tsub_le_iff_left lemma lt_tsub_comm : a < b - c ↔ c < b - a := lt_tsub_iff_left.trans lt_tsub_iff_right.symm section cov variable [covariant_class α α (+) (≤)] /-- See `lt_of_tsub_lt_tsub_left_of_le` for a weaker statement in a partial order. -/ lemma lt_of_tsub_lt_tsub_left (h : a - b < a - c) : c < b := lt_imp_lt_of_le_imp_le (λ h, tsub_le_tsub_left h a) h end cov end linear_order section ordered_add_comm_monoid variables [partial_order α] [add_comm_monoid α] [has_sub α] [has_ordered_sub α] @[simp] lemma tsub_zero (a : α) : a - 0 = a := add_le_cancellable.tsub_eq_of_eq_add add_le_cancellable_zero (add_zero _).symm end ordered_add_comm_monoid section has_exists_add_of_le variables [add_comm_semigroup α] [partial_order α] [has_exists_add_of_le α] [covariant_class α α (+) (≤)] [has_sub α] [has_ordered_sub α] {a b c d : α} @[simp] lemma add_tsub_cancel_of_le (h : a ≤ b) : a + (b - a) = b := begin refine le_antisymm _ le_add_tsub, obtain ⟨c, rfl⟩ := exists_add_of_le h, exact add_le_add_left add_tsub_le_left a, end lemma tsub_add_cancel_of_le (h : a ≤ b) : b - a + a = b := by { rw [add_comm], exact add_tsub_cancel_of_le h } lemma add_le_of_le_tsub_right_of_le (h : b ≤ c) (h2 : a ≤ c - b) : a + b ≤ c := (add_le_add_right h2 b).trans_eq $ tsub_add_cancel_of_le h lemma add_le_of_le_tsub_left_of_le (h : a ≤ c) (h2 : b ≤ c - a) : a + b ≤ c := (add_le_add_left h2 a).trans_eq $ add_tsub_cancel_of_le h lemma tsub_le_tsub_iff_right (h : c ≤ b) : a - c ≤ b - c ↔ a ≤ b := by rw [tsub_le_iff_right, tsub_add_cancel_of_le h] lemma tsub_left_inj (h1 : c ≤ a) (h2 : c ≤ b) : a - c = b - c ↔ a = b := by simp_rw [le_antisymm_iff, tsub_le_tsub_iff_right h1, tsub_le_tsub_iff_right h2] lemma tsub_inj_left (h₁ : a ≤ b) (h₂ : a ≤ c) : b - a = c - a → b = c := (tsub_left_inj h₁ h₂).1 /-- See `lt_of_tsub_lt_tsub_right` for a stronger statement in a linear order. -/ lemma lt_of_tsub_lt_tsub_right_of_le (h : c ≤ b) (h2 : a - c < b - c) : a < b := by { refine ((tsub_le_tsub_iff_right h).mp h2.le).lt_of_ne _, rintro rfl, exact h2.false } lemma tsub_add_tsub_cancel (hab : b ≤ a) (hcb : c ≤ b) : (a - b) + (b - c) = a - c := begin convert tsub_add_cancel_of_le (tsub_le_tsub_right hab c) using 2, rw [tsub_tsub, add_tsub_cancel_of_le hcb], end lemma tsub_tsub_tsub_cancel_right (h : c ≤ b) : (a - c) - (b - c) = a - b := by rw [tsub_tsub, add_tsub_cancel_of_le h] /-! #### Lemmas that assume that an element is `add_le_cancellable`. -/ namespace add_le_cancellable protected lemma eq_tsub_iff_add_eq_of_le (hc : add_le_cancellable c) (h : c ≤ b) : a = b - c ↔ a + c = b := ⟨by { rintro rfl, exact tsub_add_cancel_of_le h }, hc.eq_tsub_of_add_eq⟩ protected lemma tsub_eq_iff_eq_add_of_le (hb : add_le_cancellable b) (h : b ≤ a) : a - b = c ↔ a = c + b := by rw [eq_comm, hb.eq_tsub_iff_add_eq_of_le h, eq_comm] protected lemma add_tsub_assoc_of_le (hc : add_le_cancellable c) (h : c ≤ b) (a : α) : a + b - c = a + (b - c) := by conv_lhs { rw [← add_tsub_cancel_of_le h, add_comm c, ← add_assoc, hc.add_tsub_cancel_right] } protected lemma tsub_add_eq_add_tsub (hb : add_le_cancellable b) (h : b ≤ a) : a - b + c = a + c - b := by rw [add_comm a, hb.add_tsub_assoc_of_le h, add_comm] protected lemma tsub_tsub_assoc (hbc : add_le_cancellable (b - c)) (h₁ : b ≤ a) (h₂ : c ≤ b) : a - (b - c) = a - b + c := hbc.tsub_eq_of_eq_add $ by rw [add_assoc, add_tsub_cancel_of_le h₂, tsub_add_cancel_of_le h₁] protected lemma tsub_add_tsub_comm (hb : add_le_cancellable b) (hd : add_le_cancellable d) (hba : b ≤ a) (hdc : d ≤ c) : a - b + (c - d) = a + c - (b + d) := by rw [hb.tsub_add_eq_add_tsub hba, ←hd.add_tsub_assoc_of_le hdc, tsub_tsub, add_comm d] protected lemma le_tsub_iff_left (ha : add_le_cancellable a) (h : a ≤ c) : b ≤ c - a ↔ a + b ≤ c := ⟨add_le_of_le_tsub_left_of_le h, ha.le_tsub_of_add_le_left⟩ protected lemma le_tsub_iff_right (ha : add_le_cancellable a) (h : a ≤ c) : b ≤ c - a ↔ b + a ≤ c := by { rw [add_comm], exact ha.le_tsub_iff_left h } protected lemma tsub_lt_iff_left (hb : add_le_cancellable b) (hba : b ≤ a) : a - b < c ↔ a < b + c := begin refine ⟨hb.lt_add_of_tsub_lt_left, _⟩, intro h, refine (tsub_le_iff_left.mpr h.le).lt_of_ne _, rintro rfl, exact h.ne' (add_tsub_cancel_of_le hba) end protected lemma tsub_lt_iff_right (hb : add_le_cancellable b) (hba : b ≤ a) : a - b < c ↔ a < c + b := by { rw [add_comm], exact hb.tsub_lt_iff_left hba } protected lemma tsub_lt_iff_tsub_lt (hb : add_le_cancellable b) (hc : add_le_cancellable c) (h₁ : b ≤ a) (h₂ : c ≤ a) : a - b < c ↔ a - c < b := by rw [hb.tsub_lt_iff_left h₁, hc.tsub_lt_iff_right h₂] protected lemma le_tsub_iff_le_tsub (ha : add_le_cancellable a) (hc : add_le_cancellable c) (h₁ : a ≤ b) (h₂ : c ≤ b) : a ≤ b - c ↔ c ≤ b - a := by rw [ha.le_tsub_iff_left h₁, hc.le_tsub_iff_right h₂] protected lemma lt_tsub_iff_right_of_le (hc : add_le_cancellable c) (h : c ≤ b) : a < b - c ↔ a + c < b := begin refine ⟨λ h', (add_le_of_le_tsub_right_of_le h h'.le).lt_of_ne _, hc.lt_tsub_of_add_lt_right⟩, rintro rfl, exact h'.ne' hc.add_tsub_cancel_right, end protected lemma lt_tsub_iff_left_of_le (hc : add_le_cancellable c) (h : c ≤ b) : a < b - c ↔ c + a < b := by { rw [add_comm], exact hc.lt_tsub_iff_right_of_le h } protected lemma lt_of_tsub_lt_tsub_left_of_le [contravariant_class α α (+) (<)] (hb : add_le_cancellable b) (hca : c ≤ a) (h : a - b < a - c) : c < b := begin conv_lhs at h { rw [← tsub_add_cancel_of_le hca] }, exact lt_of_add_lt_add_left (hb.lt_add_of_tsub_lt_right h), end protected lemma tsub_lt_tsub_right_of_le (hc : add_le_cancellable c) (h : c ≤ a) (h2 : a < b) : a - c < b - c := by { apply hc.lt_tsub_of_add_lt_left, rwa [add_tsub_cancel_of_le h] } protected lemma tsub_inj_right (hab : add_le_cancellable (a - b)) (h₁ : b ≤ a) (h₂ : c ≤ a) (h₃ : a - b = a - c) : b = c := by { rw ← hab.inj, rw [tsub_add_cancel_of_le h₁, h₃, tsub_add_cancel_of_le h₂] } protected lemma tsub_lt_tsub_iff_left_of_le_of_le [contravariant_class α α (+) (<)] (hb : add_le_cancellable b) (hab : add_le_cancellable (a - b)) (h₁ : b ≤ a) (h₂ : c ≤ a) : a - b < a - c ↔ c < b := begin refine ⟨hb.lt_of_tsub_lt_tsub_left_of_le h₂, _⟩, intro h, refine (tsub_le_tsub_left h.le _).lt_of_ne _, rintro h2, exact h.ne' (hab.tsub_inj_right h₁ h₂ h2) end @[simp] protected lemma add_tsub_tsub_cancel (hac : add_le_cancellable (a - c)) (h : c ≤ a) : (a + b) - (a - c) = b + c := hac.tsub_eq_of_eq_add $ by rw [add_assoc, add_tsub_cancel_of_le h, add_comm] protected lemma tsub_tsub_cancel_of_le (hba : add_le_cancellable (b - a)) (h : a ≤ b) : b - (b - a) = a := hba.tsub_eq_of_eq_add (add_tsub_cancel_of_le h).symm end add_le_cancellable section contra /-! ### Lemmas where addition is order-reflecting. -/ variable [contravariant_class α α (+) (≤)] lemma eq_tsub_iff_add_eq_of_le (h : c ≤ b) : a = b - c ↔ a + c = b := contravariant.add_le_cancellable.eq_tsub_iff_add_eq_of_le h lemma tsub_eq_iff_eq_add_of_le (h : b ≤ a) : a - b = c ↔ a = c + b := contravariant.add_le_cancellable.tsub_eq_iff_eq_add_of_le h /-- See `add_tsub_le_assoc` for an inequality. -/ lemma add_tsub_assoc_of_le (h : c ≤ b) (a : α) : a + b - c = a + (b - c) := contravariant.add_le_cancellable.add_tsub_assoc_of_le h a lemma tsub_add_eq_add_tsub (h : b ≤ a) : a - b + c = a + c - b := contravariant.add_le_cancellable.tsub_add_eq_add_tsub h lemma tsub_tsub_assoc (h₁ : b ≤ a) (h₂ : c ≤ b) : a - (b - c) = a - b + c := contravariant.add_le_cancellable.tsub_tsub_assoc h₁ h₂ lemma tsub_add_tsub_comm (hba : b ≤ a) (hdc : d ≤ c) : a - b + (c - d) = a + c - (b + d) := contravariant.add_le_cancellable.tsub_add_tsub_comm contravariant.add_le_cancellable hba hdc lemma le_tsub_iff_left (h : a ≤ c) : b ≤ c - a ↔ a + b ≤ c := contravariant.add_le_cancellable.le_tsub_iff_left h lemma le_tsub_iff_right (h : a ≤ c) : b ≤ c - a ↔ b + a ≤ c := contravariant.add_le_cancellable.le_tsub_iff_right h lemma tsub_lt_iff_left (hbc : b ≤ a) : a - b < c ↔ a < b + c := contravariant.add_le_cancellable.tsub_lt_iff_left hbc lemma tsub_lt_iff_right (hbc : b ≤ a) : a - b < c ↔ a < c + b := contravariant.add_le_cancellable.tsub_lt_iff_right hbc lemma tsub_lt_iff_tsub_lt (h₁ : b ≤ a) (h₂ : c ≤ a) : a - b < c ↔ a - c < b := contravariant.add_le_cancellable.tsub_lt_iff_tsub_lt contravariant.add_le_cancellable h₁ h₂ lemma le_tsub_iff_le_tsub (h₁ : a ≤ b) (h₂ : c ≤ b) : a ≤ b - c ↔ c ≤ b - a := contravariant.add_le_cancellable.le_tsub_iff_le_tsub contravariant.add_le_cancellable h₁ h₂ /-- See `lt_tsub_iff_right` for a stronger statement in a linear order. -/ lemma lt_tsub_iff_right_of_le (h : c ≤ b) : a < b - c ↔ a + c < b := contravariant.add_le_cancellable.lt_tsub_iff_right_of_le h /-- See `lt_tsub_iff_left` for a stronger statement in a linear order. -/ lemma lt_tsub_iff_left_of_le (h : c ≤ b) : a < b - c ↔ c + a < b := contravariant.add_le_cancellable.lt_tsub_iff_left_of_le h /-- See `lt_of_tsub_lt_tsub_left` for a stronger statement in a linear order. -/ lemma lt_of_tsub_lt_tsub_left_of_le [contravariant_class α α (+) (<)] (hca : c ≤ a) (h : a - b < a - c) : c < b := contravariant.add_le_cancellable.lt_of_tsub_lt_tsub_left_of_le hca h lemma tsub_lt_tsub_right_of_le (h : c ≤ a) (h2 : a < b) : a - c < b - c := contravariant.add_le_cancellable.tsub_lt_tsub_right_of_le h h2 lemma tsub_inj_right (h₁ : b ≤ a) (h₂ : c ≤ a) (h₃ : a - b = a - c) : b = c := contravariant.add_le_cancellable.tsub_inj_right h₁ h₂ h₃ /-- See `tsub_lt_tsub_iff_left_of_le` for a stronger statement in a linear order. -/ lemma tsub_lt_tsub_iff_left_of_le_of_le [contravariant_class α α (+) (<)] (h₁ : b ≤ a) (h₂ : c ≤ a) : a - b < a - c ↔ c < b := contravariant.add_le_cancellable.tsub_lt_tsub_iff_left_of_le_of_le contravariant.add_le_cancellable h₁ h₂ @[simp] lemma add_tsub_tsub_cancel (h : c ≤ a) : (a + b) - (a - c) = b + c := contravariant.add_le_cancellable.add_tsub_tsub_cancel h /-- See `tsub_tsub_le` for an inequality. -/ lemma tsub_tsub_cancel_of_le (h : a ≤ b) : b - (b - a) = a := contravariant.add_le_cancellable.tsub_tsub_cancel_of_le h end contra end has_exists_add_of_le /-! ### Lemmas in a canonically ordered monoid. -/ section canonically_ordered_add_monoid variables [canonically_ordered_add_monoid α] [has_sub α] [has_ordered_sub α] {a b c d : α} lemma add_tsub_cancel_iff_le : a + (b - a) = b ↔ a ≤ b := ⟨λ h, le_iff_exists_add.mpr ⟨b - a, h.symm⟩, add_tsub_cancel_of_le⟩ lemma tsub_add_cancel_iff_le : b - a + a = b ↔ a ≤ b := by { rw [add_comm], exact add_tsub_cancel_iff_le } @[simp] lemma tsub_eq_zero_iff_le : a - b = 0 ↔ a ≤ b := by rw [← nonpos_iff_eq_zero, tsub_le_iff_left, add_zero] alias tsub_eq_zero_iff_le ↔ _ tsub_eq_zero_of_le attribute [simp] tsub_eq_zero_of_le @[simp] lemma tsub_self (a : α) : a - a = 0 := tsub_eq_zero_of_le le_rfl @[simp] lemma tsub_le_self : a - b ≤ a := tsub_le_iff_left.mpr $ le_add_left le_rfl @[simp] lemma zero_tsub (a : α) : 0 - a = 0 := tsub_eq_zero_of_le $ zero_le a lemma tsub_self_add (a b : α) : a - (a + b) = 0 := tsub_eq_zero_of_le $ self_le_add_right _ _ lemma tsub_pos_iff_not_le : 0 < a - b ↔ ¬ a ≤ b := by rw [pos_iff_ne_zero, ne.def, tsub_eq_zero_iff_le] lemma tsub_pos_of_lt (h : a < b) : 0 < b - a := tsub_pos_iff_not_le.mpr h.not_le lemma tsub_lt_of_lt (h : a < b) : a - c < b := lt_of_le_of_lt tsub_le_self h namespace add_le_cancellable protected lemma tsub_le_tsub_iff_left (ha : add_le_cancellable a) (hc : add_le_cancellable c) (h : c ≤ a) : a - b ≤ a - c ↔ c ≤ b := begin refine ⟨_, λ h, tsub_le_tsub_left h a⟩, rw [tsub_le_iff_left, ← hc.add_tsub_assoc_of_le h, hc.le_tsub_iff_right (h.trans le_add_self), add_comm b], apply ha, end protected lemma tsub_right_inj (ha : add_le_cancellable a) (hb : add_le_cancellable b) (hc : add_le_cancellable c) (hba : b ≤ a) (hca : c ≤ a) : a - b = a - c ↔ b = c := by simp_rw [le_antisymm_iff, ha.tsub_le_tsub_iff_left hb hba, ha.tsub_le_tsub_iff_left hc hca, and_comm] end add_le_cancellable /-! #### Lemmas where addition is order-reflecting. -/ section contra variable [contravariant_class α α (+) (≤)] lemma tsub_le_tsub_iff_left (h : c ≤ a) : a - b ≤ a - c ↔ c ≤ b := contravariant.add_le_cancellable.tsub_le_tsub_iff_left contravariant.add_le_cancellable h lemma tsub_right_inj (hba : b ≤ a) (hca : c ≤ a) : a - b = a - c ↔ b = c := contravariant.add_le_cancellable.tsub_right_inj contravariant.add_le_cancellable contravariant.add_le_cancellable hba hca variables (α) /-- A `canonically_ordered_add_monoid` with ordered subtraction and order-reflecting addition is cancellative. This is not an instance at it would form a typeclass loop. See note [reducible non-instances]. -/ @[reducible] def canonically_ordered_add_monoid.to_add_cancel_comm_monoid : add_cancel_comm_monoid α := { add_left_cancel := λ a b c h, by simpa only [add_tsub_cancel_left] using congr_arg (λ x, x - a) h, ..(by apply_instance : add_comm_monoid α) } end contra end canonically_ordered_add_monoid /-! ### Lemmas in a linearly canonically ordered monoid. -/ section canonically_linear_ordered_add_monoid variables [canonically_linear_ordered_add_monoid α] [has_sub α] [has_ordered_sub α] {a b c d : α} @[simp] lemma tsub_pos_iff_lt : 0 < a - b ↔ b < a := by rw [tsub_pos_iff_not_le, not_le] lemma tsub_eq_tsub_min (a b : α) : a - b = a - min a b := begin cases le_total a b with h h, { rw [min_eq_left h, tsub_self, tsub_eq_zero_of_le h] }, { rw [min_eq_right h] }, end namespace add_le_cancellable protected lemma lt_tsub_iff_right (hc : add_le_cancellable c) : a < b - c ↔ a + c < b := ⟨lt_imp_lt_of_le_imp_le tsub_le_iff_right.mpr, hc.lt_tsub_of_add_lt_right⟩ protected lemma lt_tsub_iff_left (hc : add_le_cancellable c) : a < b - c ↔ c + a < b := ⟨lt_imp_lt_of_le_imp_le tsub_le_iff_left.mpr, hc.lt_tsub_of_add_lt_left⟩ protected lemma tsub_lt_tsub_iff_right (hc : add_le_cancellable c) (h : c ≤ a) : a - c < b - c ↔ a < b := by rw [hc.lt_tsub_iff_left, add_tsub_cancel_of_le h] protected lemma tsub_lt_self (ha : add_le_cancellable a) (h₁ : 0 < a) (h₂ : 0 < b) : a - b < a := begin refine tsub_le_self.lt_of_ne (λ h, _), rw [← h, tsub_pos_iff_lt] at h₁, exact h₂.not_le (ha.add_le_iff_nonpos_left.1 $ add_le_of_le_tsub_left_of_le h₁.le h.ge), end protected lemma tsub_lt_self_iff (ha : add_le_cancellable a) : a - b < a ↔ 0 < a ∧ 0 < b := begin refine ⟨λ h, ⟨(zero_le _).trans_lt h, (zero_le b).lt_of_ne _⟩, λ h, ha.tsub_lt_self h.1 h.2⟩, rintro rfl, rw [tsub_zero] at h, exact h.false end /-- See `lt_tsub_iff_left_of_le_of_le` for a weaker statement in a partial order. -/ protected lemma tsub_lt_tsub_iff_left_of_le (ha : add_le_cancellable a) (hb : add_le_cancellable b) (h : b ≤ a) : a - b < a - c ↔ c < b := lt_iff_lt_of_le_iff_le $ ha.tsub_le_tsub_iff_left hb h end add_le_cancellable section contra variable [contravariant_class α α (+) (≤)] /-- This lemma also holds for `ennreal`, but we need a different proof for that. -/ lemma tsub_lt_tsub_iff_right (h : c ≤ a) : a - c < b - c ↔ a < b := contravariant.add_le_cancellable.tsub_lt_tsub_iff_right h lemma tsub_lt_self : 0 < a → 0 < b → a - b < a := contravariant.add_le_cancellable.tsub_lt_self lemma tsub_lt_self_iff : a - b < a ↔ 0 < a ∧ 0 < b := contravariant.add_le_cancellable.tsub_lt_self_iff /-- See `lt_tsub_iff_left_of_le_of_le` for a weaker statement in a partial order. -/ lemma tsub_lt_tsub_iff_left_of_le (h : b ≤ a) : a - b < a - c ↔ c < b := contravariant.add_le_cancellable.tsub_lt_tsub_iff_left_of_le contravariant.add_le_cancellable h end contra /-! ### Lemmas about `max` and `min`. -/ lemma tsub_add_eq_max : a - b + b = max a b := begin cases le_total a b with h h, { rw [max_eq_right h, tsub_eq_zero_of_le h, zero_add] }, { rw [max_eq_left h, tsub_add_cancel_of_le h] } end lemma add_tsub_eq_max : a + (b - a) = max a b := by rw [add_comm, max_comm, tsub_add_eq_max] lemma tsub_min : a - min a b = a - b := begin cases le_total a b with h h, { rw [min_eq_left h, tsub_self, tsub_eq_zero_of_le h] }, { rw [min_eq_right h] } end lemma tsub_add_min : a - b + min a b = a := by { rw [← tsub_min, tsub_add_cancel_of_le], apply min_le_left } end canonically_linear_ordered_add_monoid namespace with_top section variables [has_sub α] [has_zero α] /-- If `α` has subtraction and `0`, we can extend the subtraction to `with_top α`. -/ protected def sub : Π (a b : with_top α), with_top α | _ ⊤ := 0 | ⊤ (x : α) := ⊤ | (x : α) (y : α) := (x - y : α) instance : has_sub (with_top α) := ⟨with_top.sub⟩ @[simp, norm_cast] lemma coe_sub {a b : α} : (↑(a - b) : with_top α) = ↑a - ↑b := rfl @[simp] lemma top_sub_coe {a : α} : (⊤ : with_top α) - a = ⊤ := rfl @[simp] lemma sub_top {a : with_top α} : a - ⊤ = 0 := by { cases a; refl } end variables [canonically_ordered_add_monoid α] [has_sub α] [has_ordered_sub α] instance : has_ordered_sub (with_top α) := begin constructor, rintro x y z, induction y using with_top.rec_top_coe, { simp }, induction x using with_top.rec_top_coe, { simp }, induction z using with_top.rec_top_coe, { simp }, norm_cast, exact tsub_le_iff_right end end with_top
fe077ae87454f7dc7bd28af7a1c8b403ec3ed5cd
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/linear_algebra/matrix/pos_def.lean
aa243db83e8a881d1c841d267a3ffc589bda24bb
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
5,852
lean
/- Copyright (c) 2022 Alexander Bentkamp. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp -/ import linear_algebra.matrix.spectrum import linear_algebra.quadratic_form.basic /-! # Positive Definite Matrices This file defines positive (semi)definite matrices and connects the notion to positive definiteness of quadratic forms. ## Main definition * `matrix.pos_def` : a matrix `M : matrix n n 𝕜` is positive definite if it is hermitian and `xᴴMx` is greater than zero for all nonzero `x`. * `matrix.pos_semidef` : a matrix `M : matrix n n 𝕜` is positive semidefinite if it is hermitian and `xᴴMx` is nonnegative for all `x`. -/ namespace matrix variables {𝕜 : Type*} [is_R_or_C 𝕜] {m n : Type*} [fintype m] [fintype n] open_locale matrix /-- A matrix `M : matrix n n 𝕜` is positive definite if it is hermitian and `xᴴMx` is greater than zero for all nonzero `x`. -/ def pos_def (M : matrix n n 𝕜) := M.is_hermitian ∧ ∀ x : n → 𝕜, x ≠ 0 → 0 < is_R_or_C.re (dot_product (star x) (M.mul_vec x)) lemma pos_def.is_hermitian {M : matrix n n 𝕜} (hM : M.pos_def) : M.is_hermitian := hM.1 /-- A matrix `M : matrix n n 𝕜` is positive semidefinite if it is hermitian and `xᴴMx` is nonnegative for all `x`. -/ def pos_semidef (M : matrix n n 𝕜) := M.is_hermitian ∧ ∀ x : n → 𝕜, 0 ≤ is_R_or_C.re (dot_product (star x) (M.mul_vec x)) lemma pos_def.pos_semidef {M : matrix n n 𝕜} (hM : M.pos_def) : M.pos_semidef := begin refine ⟨hM.1, _⟩, intros x, by_cases hx : x = 0, { simp only [hx, zero_dot_product, star_zero, is_R_or_C.zero_re'] }, { exact le_of_lt (hM.2 x hx) } end lemma pos_semidef.submatrix {M : matrix n n 𝕜} (hM : M.pos_semidef) (e : m ≃ n): (M.submatrix e e).pos_semidef := begin refine ⟨hM.1.submatrix e, λ x, _⟩, have : (M.submatrix ⇑e ⇑e).mul_vec x = M.mul_vec (λ (i : n), x (e.symm i)) ∘ e, { ext i, dsimp only [(∘), mul_vec, dot_product], rw finset.sum_bij' (λ i _, e i) _ _ (λ i _, e.symm i); simp only [eq_self_iff_true, implies_true_iff, equiv.symm_apply_apply, finset.mem_univ, submatrix_apply, equiv.apply_symm_apply] }, rw this, convert hM.2 (λ i, x (e.symm i)) using 3, unfold dot_product, rw [finset.sum_bij' (λ i _, e i) _ _ (λ i _, e.symm i)]; simp only [eq_self_iff_true, implies_true_iff, equiv.symm_apply_apply, finset.mem_univ, submatrix_apply, equiv.apply_symm_apply, pi.star_apply], end @[simp] lemma pos_semidef_submatrix_equiv {M : matrix n n 𝕜} (e : m ≃ n) : (M.submatrix e e).pos_semidef ↔ M.pos_semidef := ⟨λ h, by simpa using h.submatrix e.symm, λ h, h.submatrix _⟩ lemma pos_def.transpose {M : matrix n n 𝕜} (hM : M.pos_def) : Mᵀ.pos_def := begin refine ⟨is_hermitian.transpose hM.1, λ x hx, _⟩, convert hM.2 (star x) (star_ne_zero.2 hx) using 2, rw [mul_vec_transpose, matrix.dot_product_mul_vec, star_star, dot_product_comm] end lemma pos_def_of_to_quadratic_form' [decidable_eq n] {M : matrix n n ℝ} (hM : M.is_symm) (hMq : M.to_quadratic_form'.pos_def) : M.pos_def := begin refine ⟨hM, λ x hx, _⟩, simp only [to_quadratic_form', quadratic_form.pos_def, bilin_form.to_quadratic_form_apply, matrix.to_bilin'_apply'] at hMq, apply hMq x hx, end lemma pos_def_to_quadratic_form' [decidable_eq n] {M : matrix n n ℝ} (hM : M.pos_def) : M.to_quadratic_form'.pos_def := begin intros x hx, simp only [to_quadratic_form', bilin_form.to_quadratic_form_apply, matrix.to_bilin'_apply'], apply hM.2 x hx, end namespace pos_def variables {M : matrix n n ℝ} (hM : M.pos_def) include hM lemma det_pos [decidable_eq n] : 0 < det M := begin rw hM.is_hermitian.det_eq_prod_eigenvalues, apply finset.prod_pos, intros i _, rw hM.is_hermitian.eigenvalues_eq, apply hM.2 _ (λ h, _), have h_det : (hM.is_hermitian.eigenvector_matrix)ᵀ.det = 0, from matrix.det_eq_zero_of_row_eq_zero i (λ j, congr_fun h j), simpa only [h_det, not_is_unit_zero] using is_unit_det_of_invertible hM.is_hermitian.eigenvector_matrixᵀ, end end pos_def end matrix namespace quadratic_form variables {n : Type*} [fintype n] lemma pos_def_of_to_matrix' [decidable_eq n] {Q : quadratic_form ℝ (n → ℝ)} (hQ : Q.to_matrix'.pos_def) : Q.pos_def := begin rw [←to_quadratic_form_associated ℝ Q, ←bilin_form.to_matrix'.left_inv ((associated_hom _) Q)], apply matrix.pos_def_to_quadratic_form' hQ end lemma pos_def_to_matrix' [decidable_eq n] {Q : quadratic_form ℝ (n → ℝ)} (hQ : Q.pos_def) : Q.to_matrix'.pos_def := begin rw [←to_quadratic_form_associated ℝ Q, ←bilin_form.to_matrix'.left_inv ((associated_hom _) Q)] at hQ, apply matrix.pos_def_of_to_quadratic_form' (is_symm_to_matrix' Q) hQ, end end quadratic_form namespace matrix variables {𝕜 : Type*} [is_R_or_C 𝕜] {n : Type*} [fintype n] /-- A positive definite matrix `M` induces an inner product `⟪x, y⟫ = xᴴMy`. -/ noncomputable def inner_product_space.of_matrix {M : matrix n n 𝕜} (hM : M.pos_def) : inner_product_space 𝕜 (n → 𝕜) := inner_product_space.of_core { inner := λ x y, dot_product (star x) (M.mul_vec y), conj_sym := λ x y, by rw [star_dot_product, star_ring_end_apply, star_star, star_mul_vec, dot_product_mul_vec, hM.is_hermitian.eq], nonneg_re := λ x, begin by_cases h : x = 0, { simp [h] }, { exact le_of_lt (hM.2 x h) } end, definite := λ x hx, begin by_contra' h, simpa [hx, lt_self_iff_false] using hM.2 x h, end, add_left := by simp only [star_add, add_dot_product, eq_self_iff_true, forall_const], smul_left := λ x y r, by rw [← smul_eq_mul, ←smul_dot_product, star_ring_end_apply, ← star_smul] } end matrix
3c060beaa73063b0f1e3476ca5c6c2f482f9f4dc
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/data/equiv/functor.lean
a840b8da80b7a8487ae02d1e1b1cdb08f8938331
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
2,301
lean
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Simon Hudon, Scott Morrison -/ import data.equiv.basic import control.bifunctor /-! # Functor and bifunctors can be applied to `equiv`s. We define ```lean def functor.map_equiv (f : Type u → Type v) [functor f] [is_lawful_functor f] : α ≃ β → f α ≃ f β ``` and ```lean def bifunctor.map_equiv (F : Type u → Type v → Type w) [bifunctor F] [is_lawful_bifunctor F] : α ≃ β → α' ≃ β' → F α α' ≃ F β β' ``` -/ universes u v w variables {α β : Type u} open equiv namespace functor variables (f : Type u → Type v) [functor f] [is_lawful_functor f] /-- Apply a functor to an `equiv`. -/ def map_equiv (h : α ≃ β) : f α ≃ f β := { to_fun := map h, inv_fun := map h.symm, left_inv := λ x, by simp [map_map], right_inv := λ x, by simp [map_map] } @[simp] lemma map_equiv_apply (h : α ≃ β) (x : f α) : (map_equiv f h : f α ≃ f β) x = map h x := rfl @[simp] lemma map_equiv_symm_apply (h : α ≃ β) (y : f β) : (map_equiv f h : f α ≃ f β).symm y = map h.symm y := rfl @[simp] lemma map_equiv_refl : map_equiv f (equiv.refl α) = equiv.refl (f α) := begin ext x, simp only [map_equiv_apply, refl_apply], exact is_lawful_functor.id_map x, end end functor namespace bifunctor variables {α' β' : Type v} (F : Type u → Type v → Type w) [bifunctor F] [is_lawful_bifunctor F] /-- Apply a bifunctor to a pair of `equiv`s. -/ def map_equiv (h : α ≃ β) (h' : α' ≃ β') : F α α' ≃ F β β' := { to_fun := bimap h h', inv_fun := bimap h.symm h'.symm, left_inv := λ x, by simp [bimap_bimap, id_bimap], right_inv := λ x, by simp [bimap_bimap, id_bimap] } @[simp] lemma map_equiv_apply (h : α ≃ β) (h' : α' ≃ β') (x : F α α') : (map_equiv F h h' : F α α' ≃ F β β') x = bimap h h' x := rfl @[simp] lemma map_equiv_symm_apply (h : α ≃ β) (h' : α' ≃ β') (y : F β β') : (map_equiv F h h' : F α α' ≃ F β β').symm y = bimap h.symm h'.symm y := rfl @[simp] lemma map_equiv_refl_refl : map_equiv F (equiv.refl α) (equiv.refl α') = equiv.refl (F α α') := begin ext x, simp [id_bimap] end end bifunctor
86c7d7e9cf6a993d6d2b0945f3a4984f68d737f6
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/order/bounded_order.lean
92f3ecad3af545f02b9b19456331939e45937bdf
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
70,275
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import data.option.basic import logic.nontrivial import order.lattice import order.max import tactic.pi_instances /-! # ⊤ and ⊥, bounded lattices and variants This file defines top and bottom elements (greatest and least elements) of a type, the bounded variants of different kinds of lattices, sets up the typeclass hierarchy between them and provides instances for `Prop` and `fun`. ## Main declarations * `has_<top/bot> α`: Typeclasses to declare the `⊤`/`⊥` notation. * `order_<top/bot> α`: Order with a top/bottom element. * `bounded_order α`: Order with a top and bottom element. * `with_<top/bot> α`: Equips `option α` with the order on `α` plus `none` as the top/bottom element. * `is_compl x y`: In a bounded lattice, predicate for "`x` is a complement of `y`". Note that in a non distributive lattice, an element can have several complements. * `is_complemented α`: Typeclass stating that any element of a lattice has a complement. ## Common lattices * Distributive lattices with a bottom element. Notated by `[distrib_lattice α] [order_bot α]` It captures the properties of `disjoint` that are common to `generalized_boolean_algebra` and `distrib_lattice` when `order_bot`. * Bounded and distributive lattice. Notated by `[distrib_lattice α] [bounded_order α]`. Typical examples include `Prop` and `set α`. -/ open order_dual set_option old_structure_cmd true universes u v variables {α : Type u} {β : Type v} /-! ### Top, bottom element -/ /-- Typeclass for the `⊤` (`\top`) notation -/ @[notation_class] class has_top (α : Type u) := (top : α) /-- Typeclass for the `⊥` (`\bot`) notation -/ @[notation_class] class has_bot (α : Type u) := (bot : α) notation `⊤` := has_top.top notation `⊥` := has_bot.bot @[priority 100] instance has_top_nonempty (α : Type u) [has_top α] : nonempty α := ⟨⊤⟩ @[priority 100] instance has_bot_nonempty (α : Type u) [has_bot α] : nonempty α := ⟨⊥⟩ attribute [pattern] has_bot.bot has_top.top /-- An order is an `order_top` if it has a greatest element. We state this using a data mixin, holding the value of `⊤` and the greatest element constraint. -/ @[ancestor has_top] class order_top (α : Type u) [has_le α] extends has_top α := (le_top : ∀ a : α, a ≤ ⊤) section order_top /-- An order is (noncomputably) either an `order_top` or a `no_order_top`. Use as `casesI bot_order_or_no_bot_order α`. -/ noncomputable def top_order_or_no_top_order (α : Type*) [has_le α] : psum (order_top α) (no_top_order α) := begin by_cases H : ∀ a : α, ∃ b, ¬ b ≤ a, { exact psum.inr ⟨H⟩ }, { push_neg at H, exact psum.inl ⟨_, classical.some_spec H⟩ } end section has_le variables [has_le α] [order_top α] {a : α} @[simp] lemma le_top : a ≤ ⊤ := order_top.le_top a @[simp] lemma is_top_top : is_top (⊤ : α) := λ _, le_top end has_le section preorder variables [preorder α] [order_top α] {a b : α} @[simp] lemma is_max_top : is_max (⊤ : α) := is_top_top.is_max @[simp] lemma not_top_lt : ¬ ⊤ < a := is_max_top.not_lt lemma ne_top_of_lt (h : a < b) : a ≠ ⊤ := (h.trans_le le_top).ne alias ne_top_of_lt ← has_lt.lt.ne_top end preorder variables [partial_order α] [order_top α] [preorder β] {f : α → β} {a b : α} @[simp] lemma is_max_iff_eq_top : is_max a ↔ a = ⊤ := ⟨λ h, h.eq_of_le le_top, λ h b _, h.symm ▸ le_top⟩ @[simp] lemma is_top_iff_eq_top : is_top a ↔ a = ⊤ := ⟨λ h, h.is_max.eq_of_le le_top, λ h b, h.symm ▸ le_top⟩ lemma not_is_max_iff_ne_top : ¬ is_max a ↔ a ≠ ⊤ := is_max_iff_eq_top.not lemma not_is_top_iff_ne_top : ¬ is_top a ↔ a ≠ ⊤ := is_top_iff_eq_top.not alias is_max_iff_eq_top ↔ is_max.eq_top _ alias is_top_iff_eq_top ↔ is_top.eq_top _ @[simp] lemma top_le_iff : ⊤ ≤ a ↔ a = ⊤ := le_top.le_iff_eq.trans eq_comm lemma top_unique (h : ⊤ ≤ a) : a = ⊤ := le_top.antisymm h lemma eq_top_iff : a = ⊤ ↔ ⊤ ≤ a := top_le_iff.symm lemma eq_top_mono (h : a ≤ b) (h₂ : a = ⊤) : b = ⊤ := top_unique $ h₂ ▸ h lemma lt_top_iff_ne_top : a < ⊤ ↔ a ≠ ⊤ := le_top.lt_iff_ne @[simp] lemma not_lt_top_iff : ¬ a < ⊤ ↔ a = ⊤ := lt_top_iff_ne_top.not_left lemma eq_top_or_lt_top (a : α) : a = ⊤ ∨ a < ⊤ := le_top.eq_or_lt lemma ne.lt_top (h : a ≠ ⊤) : a < ⊤ := lt_top_iff_ne_top.mpr h lemma ne.lt_top' (h : ⊤ ≠ a) : a < ⊤ := h.symm.lt_top lemma ne_top_of_le_ne_top (hb : b ≠ ⊤) (hab : a ≤ b) : a ≠ ⊤ := (hab.trans_lt hb.lt_top).ne lemma strict_mono.apply_eq_top_iff (hf : strict_mono f) : f a = f ⊤ ↔ a = ⊤ := ⟨λ h, not_lt_top_iff.1 $ λ ha, (hf ha).ne h, congr_arg _⟩ lemma strict_anti.apply_eq_top_iff (hf : strict_anti f) : f a = f ⊤ ↔ a = ⊤ := ⟨λ h, not_lt_top_iff.1 $ λ ha, (hf ha).ne' h, congr_arg _⟩ variables [nontrivial α] lemma not_is_min_top : ¬ is_min (⊤ : α) := λ h, let ⟨a, ha⟩ := exists_ne (⊤ : α) in ha $ top_le_iff.1 $ h le_top end order_top lemma strict_mono.maximal_preimage_top [linear_order α] [preorder β] [order_top β] {f : α → β} (H : strict_mono f) {a} (h_top : f a = ⊤) (x : α) : x ≤ a := H.maximal_of_maximal_image (λ p, by { rw h_top, exact le_top }) x theorem order_top.ext_top {α} {hA : partial_order α} (A : order_top α) {hB : partial_order α} (B : order_top α) (H : ∀ x y : α, (by haveI := hA; exact x ≤ y) ↔ x ≤ y) : (by haveI := A; exact ⊤ : α) = ⊤ := top_unique $ by rw ← H; apply le_top theorem order_top.ext {α} [partial_order α] {A B : order_top α} : A = B := begin have tt := order_top.ext_top A B (λ _ _, iff.rfl), casesI A with _ ha, casesI B with _ hb, congr, exact le_antisymm (hb _) (ha _) end /-- An order is an `order_bot` if it has a least element. We state this using a data mixin, holding the value of `⊥` and the least element constraint. -/ @[ancestor has_bot] class order_bot (α : Type u) [has_le α] extends has_bot α := (bot_le : ∀ a : α, ⊥ ≤ a) section order_bot /-- An order is (noncomputably) either an `order_bot` or a `no_order_bot`. Use as `casesI bot_order_or_no_bot_order α`. -/ noncomputable def bot_order_or_no_bot_order (α : Type*) [has_le α] : psum (order_bot α) (no_bot_order α) := begin by_cases H : ∀ a : α, ∃ b, ¬ a ≤ b, { exact psum.inr ⟨H⟩ }, { push_neg at H, exact psum.inl ⟨_, classical.some_spec H⟩ } end section has_le variables [has_le α] [order_bot α] {a : α} @[simp] lemma bot_le : ⊥ ≤ a := order_bot.bot_le a @[simp] lemma is_bot_bot : is_bot (⊥ : α) := λ _, bot_le end has_le namespace order_dual variable (α) instance [has_bot α] : has_top αᵒᵈ := ⟨(⊥ : α)⟩ instance [has_top α] : has_bot αᵒᵈ := ⟨(⊤ : α)⟩ instance [has_le α] [order_bot α] : order_top αᵒᵈ := { le_top := @bot_le α _ _, .. order_dual.has_top α } instance [has_le α] [order_top α] : order_bot αᵒᵈ := { bot_le := @le_top α _ _, .. order_dual.has_bot α } @[simp] lemma of_dual_bot [has_top α] : of_dual ⊥ = (⊤ : α) := rfl @[simp] lemma of_dual_top [has_bot α] : of_dual ⊤ = (⊥ : α) := rfl @[simp] lemma to_dual_bot [has_bot α] : to_dual (⊥ : α) = ⊤ := rfl @[simp] lemma to_dual_top [has_top α] : to_dual (⊤ : α) = ⊥ := rfl end order_dual section preorder variables [preorder α] [order_bot α] {a b : α} @[simp] lemma is_min_bot : is_min (⊥ : α) := is_bot_bot.is_min @[simp] lemma not_lt_bot : ¬ a < ⊥ := is_min_bot.not_lt lemma ne_bot_of_gt (h : a < b) : b ≠ ⊥ := (bot_le.trans_lt h).ne' alias ne_bot_of_gt ← has_lt.lt.ne_bot end preorder variables [partial_order α] [order_bot α] [preorder β] {f : α → β} {a b : α} @[simp] lemma is_min_iff_eq_bot : is_min a ↔ a = ⊥ := ⟨λ h, h.eq_of_ge bot_le, λ h b _, h.symm ▸ bot_le⟩ @[simp] lemma is_bot_iff_eq_bot : is_bot a ↔ a = ⊥ := ⟨λ h, h.is_min.eq_of_ge bot_le, λ h b, h.symm ▸ bot_le⟩ lemma not_is_min_iff_ne_bot : ¬ is_min a ↔ a ≠ ⊥ := is_min_iff_eq_bot.not lemma not_is_bot_iff_ne_bot : ¬ is_bot a ↔ a ≠ ⊥ := is_bot_iff_eq_bot.not alias is_min_iff_eq_bot ↔ is_min.eq_bot _ alias is_bot_iff_eq_bot ↔ is_bot.eq_bot _ @[simp] lemma le_bot_iff : a ≤ ⊥ ↔ a = ⊥ := bot_le.le_iff_eq lemma bot_unique (h : a ≤ ⊥) : a = ⊥ := h.antisymm bot_le lemma eq_bot_iff : a = ⊥ ↔ a ≤ ⊥ := le_bot_iff.symm lemma eq_bot_mono (h : a ≤ b) (h₂ : b = ⊥) : a = ⊥ := bot_unique $ h₂ ▸ h lemma bot_lt_iff_ne_bot : ⊥ < a ↔ a ≠ ⊥ := bot_le.lt_iff_ne.trans ne_comm @[simp] lemma not_bot_lt_iff : ¬ ⊥ < a ↔ a = ⊥ := bot_lt_iff_ne_bot.not_left lemma eq_bot_or_bot_lt (a : α) : a = ⊥ ∨ ⊥ < a := bot_le.eq_or_gt lemma eq_bot_of_minimal (h : ∀ b, ¬ b < a) : a = ⊥ := (eq_bot_or_bot_lt a).resolve_right (h ⊥) lemma ne.bot_lt (h : a ≠ ⊥) : ⊥ < a := bot_lt_iff_ne_bot.mpr h lemma ne.bot_lt' (h : ⊥ ≠ a) : ⊥ < a := h.symm.bot_lt lemma ne_bot_of_le_ne_bot (hb : b ≠ ⊥) (hab : b ≤ a) : a ≠ ⊥ := (hb.bot_lt.trans_le hab).ne' lemma strict_mono.apply_eq_bot_iff (hf : strict_mono f) : f a = f ⊥ ↔ a = ⊥ := hf.dual.apply_eq_top_iff lemma strict_anti.apply_eq_bot_iff (hf : strict_anti f) : f a = f ⊥ ↔ a = ⊥ := hf.dual.apply_eq_top_iff variables [nontrivial α] lemma not_is_max_bot : ¬ is_max (⊥ : α) := @not_is_min_top αᵒᵈ _ _ _ end order_bot lemma strict_mono.minimal_preimage_bot [linear_order α] [partial_order β] [order_bot β] {f : α → β} (H : strict_mono f) {a} (h_bot : f a = ⊥) (x : α) : a ≤ x := H.minimal_of_minimal_image (λ p, by { rw h_bot, exact bot_le }) x theorem order_bot.ext_bot {α} {hA : partial_order α} (A : order_bot α) {hB : partial_order α} (B : order_bot α) (H : ∀ x y : α, (by haveI := hA; exact x ≤ y) ↔ x ≤ y) : (by haveI := A; exact ⊥ : α) = ⊥ := bot_unique $ by rw ← H; apply bot_le theorem order_bot.ext {α} [partial_order α] {A B : order_bot α} : A = B := begin have tt := order_bot.ext_bot A B (λ _ _, iff.rfl), casesI A with a ha, casesI B with b hb, congr, exact le_antisymm (ha _) (hb _) end section semilattice_sup_top variables [semilattice_sup α] [order_top α] {a : α} @[simp] theorem top_sup_eq : ⊤ ⊔ a = ⊤ := sup_of_le_left le_top @[simp] theorem sup_top_eq : a ⊔ ⊤ = ⊤ := sup_of_le_right le_top end semilattice_sup_top section semilattice_sup_bot variables [semilattice_sup α] [order_bot α] {a b : α} @[simp] theorem bot_sup_eq : ⊥ ⊔ a = a := sup_of_le_right bot_le @[simp] theorem sup_bot_eq : a ⊔ ⊥ = a := sup_of_le_left bot_le @[simp] theorem sup_eq_bot_iff : a ⊔ b = ⊥ ↔ (a = ⊥ ∧ b = ⊥) := by rw [eq_bot_iff, sup_le_iff]; simp end semilattice_sup_bot section semilattice_inf_top variables [semilattice_inf α] [order_top α] {a b : α} @[simp] theorem top_inf_eq : ⊤ ⊓ a = a := inf_of_le_right le_top @[simp] theorem inf_top_eq : a ⊓ ⊤ = a := inf_of_le_left le_top @[simp] theorem inf_eq_top_iff : a ⊓ b = ⊤ ↔ (a = ⊤ ∧ b = ⊤) := @sup_eq_bot_iff αᵒᵈ _ _ _ _ end semilattice_inf_top section semilattice_inf_bot variables [semilattice_inf α] [order_bot α] {a : α} @[simp] theorem bot_inf_eq : ⊥ ⊓ a = ⊥ := inf_of_le_left bot_le @[simp] theorem inf_bot_eq : a ⊓ ⊥ = ⊥ := inf_of_le_right bot_le end semilattice_inf_bot /-! ### Bounded order -/ /-- A bounded order describes an order `(≤)` with a top and bottom element, denoted `⊤` and `⊥` respectively. -/ @[ancestor order_top order_bot] class bounded_order (α : Type u) [has_le α] extends order_top α, order_bot α. instance (α : Type u) [has_le α] [bounded_order α] : bounded_order αᵒᵈ := { .. order_dual.order_top α, .. order_dual.order_bot α } theorem bounded_order.ext {α} [partial_order α] {A B : bounded_order α} : A = B := begin have ht : @bounded_order.to_order_top α _ A = @bounded_order.to_order_top α _ B := order_top.ext, have hb : @bounded_order.to_order_bot α _ A = @bounded_order.to_order_bot α _ B := order_bot.ext, casesI A, casesI B, injection ht with h, injection hb with h', convert rfl, { exact h.symm }, { exact h'.symm } end /-- Propositions form a distributive lattice. -/ instance Prop.distrib_lattice : distrib_lattice Prop := { sup := or, le_sup_left := @or.inl, le_sup_right := @or.inr, sup_le := λ a b c, or.rec, inf := and, inf_le_left := @and.left, inf_le_right := @and.right, le_inf := λ a b c Hab Hac Ha, and.intro (Hab Ha) (Hac Ha), le_sup_inf := λ a b c H, or_iff_not_imp_left.2 $ λ Ha, ⟨H.1.resolve_left Ha, H.2.resolve_left Ha⟩, ..Prop.partial_order } /-- Propositions form a bounded order. -/ instance Prop.bounded_order : bounded_order Prop := { top := true, le_top := λ a Ha, true.intro, bot := false, bot_le := @false.elim } instance Prop.le_is_total : is_total Prop (≤) := ⟨λ p q, by { change (p → q) ∨ (q → p), tauto! }⟩ noncomputable instance Prop.linear_order : linear_order Prop := by classical; exact lattice.to_linear_order Prop @[simp] lemma sup_Prop_eq : (⊔) = (∨) := rfl @[simp] lemma inf_Prop_eq : (⊓) = (∧) := rfl section logic /-! #### In this section we prove some properties about monotone and antitone operations on `Prop` -/ section preorder variable [preorder α] theorem monotone_and {p q : α → Prop} (m_p : monotone p) (m_q : monotone q) : monotone (λ x, p x ∧ q x) := λ a b h, and.imp (m_p h) (m_q h) -- Note: by finish [monotone] doesn't work theorem monotone_or {p q : α → Prop} (m_p : monotone p) (m_q : monotone q) : monotone (λ x, p x ∨ q x) := λ a b h, or.imp (m_p h) (m_q h) lemma monotone_le {x : α}: monotone ((≤) x) := λ y z h' h, h.trans h' lemma monotone_lt {x : α}: monotone ((<) x) := λ y z h' h, h.trans_le h' lemma antitone_le {x : α}: antitone (≤ x) := λ y z h' h, h'.trans h lemma antitone_lt {x : α}: antitone (< x) := λ y z h' h, h'.trans_lt h lemma monotone.forall {P : β → α → Prop} (hP : ∀ x, monotone (P x)) : monotone (λ y, ∀ x, P x y) := λ y y' hy h x, hP x hy $ h x lemma antitone.forall {P : β → α → Prop} (hP : ∀ x, antitone (P x)) : antitone (λ y, ∀ x, P x y) := λ y y' hy h x, hP x hy (h x) lemma monotone.ball {P : β → α → Prop} {s : set β} (hP : ∀ x ∈ s, monotone (P x)) : monotone (λ y, ∀ x ∈ s, P x y) := λ y y' hy h x hx, hP x hx hy (h x hx) lemma antitone.ball {P : β → α → Prop} {s : set β} (hP : ∀ x ∈ s, antitone (P x)) : antitone (λ y, ∀ x ∈ s, P x y) := λ y y' hy h x hx, hP x hx hy (h x hx) end preorder section semilattice_sup variables [semilattice_sup α] lemma exists_ge_and_iff_exists {P : α → Prop} {x₀ : α} (hP : monotone P) : (∃ x, x₀ ≤ x ∧ P x) ↔ ∃ x, P x := ⟨λ h, h.imp $ λ x h, h.2, λ ⟨x, hx⟩, ⟨x ⊔ x₀, le_sup_right, hP le_sup_left hx⟩⟩ end semilattice_sup section semilattice_inf variables [semilattice_inf α] lemma exists_le_and_iff_exists {P : α → Prop} {x₀ : α} (hP : antitone P) : (∃ x, x ≤ x₀ ∧ P x) ↔ ∃ x, P x := exists_ge_and_iff_exists hP.dual_left end semilattice_inf end logic /-! ### Function lattices -/ namespace pi variables {ι : Type*} {α' : ι → Type*} instance [Π i, has_bot (α' i)] : has_bot (Π i, α' i) := ⟨λ i, ⊥⟩ @[simp] lemma bot_apply [Π i, has_bot (α' i)] (i : ι) : (⊥ : Π i, α' i) i = ⊥ := rfl lemma bot_def [Π i, has_bot (α' i)] : (⊥ : Π i, α' i) = λ i, ⊥ := rfl instance [Π i, has_top (α' i)] : has_top (Π i, α' i) := ⟨λ i, ⊤⟩ @[simp] lemma top_apply [Π i, has_top (α' i)] (i : ι) : (⊤ : Π i, α' i) i = ⊤ := rfl lemma top_def [Π i, has_top (α' i)] : (⊤ : Π i, α' i) = λ i, ⊤ := rfl instance [Π i, has_le (α' i)] [Π i, order_top (α' i)] : order_top (Π i, α' i) := { le_top := λ _ _, le_top, ..pi.has_top } instance [Π i, has_le (α' i)] [Π i, order_bot (α' i)] : order_bot (Π i, α' i) := { bot_le := λ _ _, bot_le, ..pi.has_bot } instance [Π i, has_le (α' i)] [Π i, bounded_order (α' i)] : bounded_order (Π i, α' i) := { ..pi.order_top, ..pi.order_bot } end pi section subsingleton variables [partial_order α] [bounded_order α] lemma eq_bot_of_bot_eq_top (hα : (⊥ : α) = ⊤) (x : α) : x = (⊥ : α) := eq_bot_mono le_top (eq.symm hα) lemma eq_top_of_bot_eq_top (hα : (⊥ : α) = ⊤) (x : α) : x = (⊤ : α) := eq_top_mono bot_le hα lemma subsingleton_of_top_le_bot (h : (⊤ : α) ≤ (⊥ : α)) : subsingleton α := ⟨λ a b, le_antisymm (le_trans le_top $ le_trans h bot_le) (le_trans le_top $ le_trans h bot_le)⟩ lemma subsingleton_of_bot_eq_top (hα : (⊥ : α) = (⊤ : α)) : subsingleton α := subsingleton_of_top_le_bot (ge_of_eq hα) lemma subsingleton_iff_bot_eq_top : (⊥ : α) = (⊤ : α) ↔ subsingleton α := ⟨subsingleton_of_bot_eq_top, λ h, by exactI subsingleton.elim ⊥ ⊤⟩ end subsingleton section lift /-- Pullback an `order_top`. -/ @[reducible] -- See note [reducible non-instances] def order_top.lift [has_le α] [has_top α] [has_le β] [order_top β] (f : α → β) (map_le : ∀ a b, f a ≤ f b → a ≤ b) (map_top : f ⊤ = ⊤) : order_top α := ⟨⊤, λ a, map_le _ _ $ by { rw map_top, exact le_top }⟩ /-- Pullback an `order_bot`. -/ @[reducible] -- See note [reducible non-instances] def order_bot.lift [has_le α] [has_bot α] [has_le β] [order_bot β] (f : α → β) (map_le : ∀ a b, f a ≤ f b → a ≤ b) (map_bot : f ⊥ = ⊥) : order_bot α := ⟨⊥, λ a, map_le _ _ $ by { rw map_bot, exact bot_le }⟩ /-- Pullback a `bounded_order`. -/ @[reducible] -- See note [reducible non-instances] def bounded_order.lift [has_le α] [has_top α] [has_bot α] [has_le β] [bounded_order β] (f : α → β) (map_le : ∀ a b, f a ≤ f b → a ≤ b) (map_top : f ⊤ = ⊤) (map_bot : f ⊥ = ⊥) : bounded_order α := { ..order_top.lift f map_le map_top, ..order_bot.lift f map_le map_bot } end lift /-! ### `with_bot`, `with_top` -/ /-- Attach `⊥` to a type. -/ def with_bot (α : Type*) := option α namespace with_bot variables {a b : α} meta instance [has_to_format α] : has_to_format (with_bot α) := { to_format := λ x, match x with | none := "⊥" | (some x) := to_fmt x end } instance [has_repr α] : has_repr (with_bot α) := ⟨λ o, match o with | none := "⊥" | (some a) := "↑" ++ repr a end⟩ instance : has_coe_t α (with_bot α) := ⟨some⟩ instance : has_bot (with_bot α) := ⟨none⟩ meta instance {α : Type} [reflected _ α] [has_reflect α] : has_reflect (with_bot α) | ⊥ := `(⊥) | (a : α) := `(coe : α → with_bot α).subst `(a) instance : inhabited (with_bot α) := ⟨⊥⟩ lemma none_eq_bot : (none : with_bot α) = (⊥ : with_bot α) := rfl lemma some_eq_coe (a : α) : (some a : with_bot α) = (↑a : with_bot α) := rfl @[simp] lemma bot_ne_coe : ⊥ ≠ (a : with_bot α) . @[simp] lemma coe_ne_bot : (a : with_bot α) ≠ ⊥ . /-- Recursor for `with_bot` using the preferred forms `⊥` and `↑a`. -/ @[elab_as_eliminator] def rec_bot_coe {C : with_bot α → Sort*} (h₁ : C ⊥) (h₂ : Π (a : α), C a) : Π (n : with_bot α), C n := option.rec h₁ h₂ @[simp] lemma rec_bot_coe_bot {C : with_bot α → Sort*} (d : C ⊥) (f : Π (a : α), C a) : @rec_bot_coe _ C d f ⊥ = d := rfl @[simp] lemma rec_bot_coe_coe {C : with_bot α → Sort*} (d : C ⊥) (f : Π (a : α), C a) (x : α) : @rec_bot_coe _ C d f ↑x = f x := rfl /-- Specialization of `option.get_or_else` to values in `with_bot α` that respects API boundaries. -/ def unbot' (d : α) (x : with_bot α) : α := rec_bot_coe d id x @[simp] lemma unbot'_bot {α} (d : α) : unbot' d ⊥ = d := rfl @[simp] lemma unbot'_coe {α} (d x : α) : unbot' d x = x := rfl @[norm_cast] lemma coe_eq_coe : (a : with_bot α) = b ↔ a = b := option.some_inj /-- Lift a map `f : α → β` to `with_bot α → with_bot β`. Implemented using `option.map`. -/ def map (f : α → β) : with_bot α → with_bot β := option.map f @[simp] lemma map_bot (f : α → β) : map f ⊥ = ⊥ := rfl @[simp] lemma map_coe (f : α → β) (a : α) : map f a = f a := rfl lemma ne_bot_iff_exists {x : with_bot α} : x ≠ ⊥ ↔ ∃ (a : α), ↑a = x := option.ne_none_iff_exists /-- Deconstruct a `x : with_bot α` to the underlying value in `α`, given a proof that `x ≠ ⊥`. -/ def unbot : Π (x : with_bot α), x ≠ ⊥ → α | ⊥ h := absurd rfl h | (some x) h := x @[simp] lemma coe_unbot (x : with_bot α) (h : x ≠ ⊥) : (x.unbot h : with_bot α) = x := by { cases x, simpa using h, refl, } @[simp] lemma unbot_coe (x : α) (h : (x : with_bot α) ≠ ⊥ := coe_ne_bot) : (x : with_bot α).unbot h = x := rfl instance : can_lift (with_bot α) α := { coe := coe, cond := λ r, r ≠ ⊥, prf := λ x h, ⟨x.unbot h, coe_unbot _ _⟩ } section has_le variables [has_le α] @[priority 10] instance : has_le (with_bot α) := ⟨λ o₁ o₂ : option α, ∀ a ∈ o₁, ∃ b ∈ o₂, a ≤ b⟩ @[simp] lemma some_le_some : @has_le.le (with_bot α) _ (some a) (some b) ↔ a ≤ b := by simp [(≤)] @[simp, norm_cast] lemma coe_le_coe : (a : with_bot α) ≤ b ↔ a ≤ b := some_le_some @[simp] lemma none_le {a : with_bot α} : @has_le.le (with_bot α) _ none a := λ b h, option.no_confusion h instance : order_bot (with_bot α) := { bot_le := λ a, none_le, ..with_bot.has_bot } instance [order_top α] : order_top (with_bot α) := { top := some ⊤, le_top := λ o a ha, by cases ha; exact ⟨_, rfl, le_top⟩ } instance [order_top α] : bounded_order (with_bot α) := { ..with_bot.order_top, ..with_bot.order_bot } lemma not_coe_le_bot (a : α) : ¬ (a : with_bot α) ≤ ⊥ := λ h, let ⟨b, hb, _⟩ := h _ rfl in option.not_mem_none _ hb lemma coe_le : ∀ {o : option α}, b ∈ o → ((a : with_bot α) ≤ o ↔ a ≤ b) | _ rfl := coe_le_coe lemma coe_le_iff : ∀ {x : with_bot α}, ↑a ≤ x ↔ ∃ b : α, x = b ∧ a ≤ b | (some a) := by simp [some_eq_coe, coe_eq_coe] | none := iff_of_false (not_coe_le_bot _) $ by simp [none_eq_bot] lemma le_coe_iff : ∀ {x : with_bot α}, x ≤ b ↔ ∀ a, x = ↑a → a ≤ b | (some b) := by simp [some_eq_coe, coe_eq_coe] | none := by simp [none_eq_bot] protected lemma _root_.is_max.with_bot (h : is_max a) : is_max (a : with_bot α) | none _ := bot_le | (some b) hb := some_le_some.2 $ h $ some_le_some.1 hb end has_le section has_lt variables [has_lt α] @[priority 10] instance : has_lt (with_bot α) := ⟨λ o₁ o₂ : option α, ∃ b ∈ o₂, ∀ a ∈ o₁, a < b⟩ @[simp] lemma some_lt_some : @has_lt.lt (with_bot α) _ (some a) (some b) ↔ a < b := by simp [(<)] @[simp, norm_cast] lemma coe_lt_coe : (a : with_bot α) < b ↔ a < b := some_lt_some @[simp] lemma none_lt_some (a : α) : @has_lt.lt (with_bot α) _ none (some a) := ⟨a, rfl, λ b hb, (option.not_mem_none _ hb).elim⟩ lemma bot_lt_coe (a : α) : (⊥ : with_bot α) < a := none_lt_some a @[simp] lemma not_lt_none (a : with_bot α) : ¬ @has_lt.lt (with_bot α) _ a none := λ ⟨_, h, _⟩, option.not_mem_none _ h lemma lt_iff_exists_coe : ∀ {a b : with_bot α}, a < b ↔ ∃ p : α, b = p ∧ a < p | a (some b) := by simp [some_eq_coe, coe_eq_coe] | a none := iff_of_false (not_lt_none _) $ by simp [none_eq_bot] lemma lt_coe_iff : ∀ {x : with_bot α}, x < b ↔ ∀ a, x = ↑a → a < b | (some b) := by simp [some_eq_coe, coe_eq_coe, coe_lt_coe] | none := by simp [none_eq_bot, bot_lt_coe] end has_lt instance [preorder α] : preorder (with_bot α) := { le := (≤), lt := (<), lt_iff_le_not_le := by { intros, cases a; cases b; simp [lt_iff_le_not_le]; simp [(<), (≤)] }, le_refl := λ o a ha, ⟨a, ha, le_rfl⟩, le_trans := λ o₁ o₂ o₃ h₁ h₂ a ha, let ⟨b, hb, ab⟩ := h₁ a ha, ⟨c, hc, bc⟩ := h₂ b hb in ⟨c, hc, le_trans ab bc⟩ } instance [partial_order α] : partial_order (with_bot α) := { le_antisymm := λ o₁ o₂ h₁ h₂, begin cases o₁ with a, { cases o₂ with b, {refl}, rcases h₂ b rfl with ⟨_, ⟨⟩, _⟩ }, { rcases h₁ a rfl with ⟨b, ⟨⟩, h₁'⟩, rcases h₂ b rfl with ⟨_, ⟨⟩, h₂'⟩, rw le_antisymm h₁' h₂' } end, .. with_bot.preorder } lemma map_le_iff [preorder α] [preorder β] (f : α → β) (mono_iff : ∀ {a b}, f a ≤ f b ↔ a ≤ b) : ∀ (a b : with_bot α), a.map f ≤ b.map f ↔ a ≤ b | ⊥ _ := by simp only [map_bot, bot_le] | (a : α) ⊥ := by simp only [map_coe, map_bot, coe_ne_bot, not_coe_le_bot _] | (a : α) (b : α) := by simpa using mono_iff lemma le_coe_get_or_else [preorder α] : ∀ (a : with_bot α) (b : α), a ≤ a.get_or_else b | (some a) b := le_refl a | none b := λ _ h, option.no_confusion h @[simp] lemma get_or_else_bot (a : α) : option.get_or_else (⊥ : with_bot α) a = a := rfl lemma get_or_else_bot_le_iff [has_le α] [order_bot α] {a : with_bot α} {b : α} : a.get_or_else ⊥ ≤ b ↔ a ≤ b := by cases a; simp [none_eq_bot, some_eq_coe] lemma get_or_else_bot_lt_iff [partial_order α] [order_bot α] {a : with_bot α} {b : α} (ha : a ≠ ⊥) : a.get_or_else ⊥ < b ↔ a < b := begin obtain ⟨a, rfl⟩ := ne_bot_iff_exists.mp ha, simp only [lt_iff_le_and_ne, get_or_else_bot_le_iff, and.congr_right_iff], intro h, apply iff.not, simp only [with_bot.coe_eq_coe, option.get_or_else_coe, iff_self], end instance [semilattice_sup α] : semilattice_sup (with_bot α) := { sup := option.lift_or_get (⊔), le_sup_left := λ o₁ o₂ a ha, by cases ha; cases o₂; simp [option.lift_or_get], le_sup_right := λ o₁ o₂ a ha, by cases ha; cases o₁; simp [option.lift_or_get], sup_le := λ o₁ o₂ o₃ h₁ h₂ a ha, begin cases o₁ with b; cases o₂ with c; cases ha, { exact h₂ a rfl }, { exact h₁ a rfl }, { rcases h₁ b rfl with ⟨d, ⟨⟩, h₁'⟩, simp at h₂, exact ⟨d, rfl, sup_le h₁' h₂⟩ } end, ..with_bot.order_bot, ..with_bot.partial_order } lemma coe_sup [semilattice_sup α] (a b : α) : ((a ⊔ b : α) : with_bot α) = a ⊔ b := rfl instance [semilattice_inf α] : semilattice_inf (with_bot α) := { inf := λ o₁ o₂, o₁.bind (λ a, o₂.map (λ b, a ⊓ b)), inf_le_left := λ o₁ o₂ a ha, begin simp [map] at ha, rcases ha with ⟨b, rfl, c, rfl, rfl⟩, exact ⟨_, rfl, inf_le_left⟩ end, inf_le_right := λ o₁ o₂ a ha, begin simp [map] at ha, rcases ha with ⟨b, rfl, c, rfl, rfl⟩, exact ⟨_, rfl, inf_le_right⟩ end, le_inf := λ o₁ o₂ o₃ h₁ h₂ a ha, begin cases ha, rcases h₁ a rfl with ⟨b, ⟨⟩, ab⟩, rcases h₂ a rfl with ⟨c, ⟨⟩, ac⟩, exact ⟨_, rfl, le_inf ab ac⟩ end, ..with_bot.order_bot, ..with_bot.partial_order } lemma coe_inf [semilattice_inf α] (a b : α) : ((a ⊓ b : α) : with_bot α) = a ⊓ b := rfl instance [lattice α] : lattice (with_bot α) := { ..with_bot.semilattice_sup, ..with_bot.semilattice_inf } instance decidable_le [has_le α] [@decidable_rel α (≤)] : @decidable_rel (with_bot α) (≤) | none x := is_true $ λ a h, option.no_confusion h | (some x) (some y) := if h : x ≤ y then is_true (some_le_some.2 h) else is_false $ by simp * | (some x) none := is_false $ λ h, by rcases h x rfl with ⟨y, ⟨_⟩, _⟩ instance decidable_lt [has_lt α] [@decidable_rel α (<)] : @decidable_rel (with_bot α) (<) | none (some x) := is_true $ by existsi [x,rfl]; rintros _ ⟨⟩ | (some x) (some y) := if h : x < y then is_true $ by simp * else is_false $ by simp * | x none := is_false $ by rintro ⟨a,⟨⟨⟩⟩⟩ instance is_total_le [has_le α] [is_total α (≤)] : is_total (with_bot α) (≤) := ⟨λ a b, match a, b with | none , _ := or.inl bot_le | _ , none := or.inr bot_le | some x, some y := (total_of (≤) x y).imp some_le_some.2 some_le_some.2 end⟩ instance [linear_order α] : linear_order (with_bot α) := lattice.to_linear_order _ @[norm_cast] -- this is not marked simp because the corresponding with_top lemmas are used lemma coe_min [linear_order α] (x y : α) : ((min x y : α) : with_bot α) = min x y := rfl @[norm_cast] -- this is not marked simp because the corresponding with_top lemmas are used lemma coe_max [linear_order α] (x y : α) : ((max x y : α) : with_bot α) = max x y := rfl lemma well_founded_lt [preorder α] (h : @well_founded α (<)) : @well_founded (with_bot α) (<) := have acc_bot : acc ((<) : with_bot α → with_bot α → Prop) ⊥ := acc.intro _ (λ a ha, (not_le_of_gt ha bot_le).elim), ⟨λ a, option.rec_on a acc_bot (λ a, acc.intro _ (λ b, option.rec_on b (λ _, acc_bot) (λ b, well_founded.induction h b (show ∀ b : α, (∀ c, c < b → (c : with_bot α) < a → acc ((<) : with_bot α → with_bot α → Prop) c) → (b : with_bot α) < a → acc ((<) : with_bot α → with_bot α → Prop) b, from λ b ih hba, acc.intro _ (λ c, option.rec_on c (λ _, acc_bot) (λ c hc, ih _ (some_lt_some.1 hc) (lt_trans hc hba)))))))⟩ instance [has_lt α] [densely_ordered α] [no_min_order α] : densely_ordered (with_bot α) := ⟨ λ a b, match a, b with | a, none := λ h : a < ⊥, (not_lt_none _ h).elim | none, some b := λ h, let ⟨a, ha⟩ := exists_lt b in ⟨a, bot_lt_coe a, coe_lt_coe.2 ha⟩ | some a, some b := λ h, let ⟨a, ha₁, ha₂⟩ := exists_between (coe_lt_coe.1 h) in ⟨a, coe_lt_coe.2 ha₁, coe_lt_coe.2 ha₂⟩ end⟩ lemma lt_iff_exists_coe_btwn [preorder α] [densely_ordered α] [no_min_order α] {a b : with_bot α} : a < b ↔ ∃ x : α, a < ↑x ∧ ↑x < b := ⟨λ h, let ⟨y, hy⟩ := exists_between h, ⟨x, hx⟩ := lt_iff_exists_coe.1 hy.1 in ⟨x, hx.1 ▸ hy⟩, λ ⟨x, hx⟩, lt_trans hx.1 hx.2⟩ instance [has_le α] [no_top_order α] [nonempty α] : no_top_order (with_bot α) := ⟨begin apply rec_bot_coe, { exact ‹nonempty α›.elim (λ a, ⟨a, not_coe_le_bot a⟩) }, { intro a, obtain ⟨b, h⟩ := exists_not_le a, exact ⟨b, by rwa coe_le_coe⟩ } end⟩ instance [has_lt α] [no_max_order α] [nonempty α] : no_max_order (with_bot α) := ⟨begin apply with_bot.rec_bot_coe, { apply ‹nonempty α›.elim, exact λ a, ⟨a, with_bot.bot_lt_coe a⟩, }, { intro a, obtain ⟨b, ha⟩ := exists_gt a, exact ⟨b, with_bot.coe_lt_coe.mpr ha⟩, } end⟩ end with_bot --TODO(Mario): Construct using order dual on with_bot /-- Attach `⊤` to a type. -/ def with_top (α : Type*) := option α namespace with_top variables {a b : α} meta instance [has_to_format α] : has_to_format (with_top α) := { to_format := λ x, match x with | none := "⊤" | (some x) := to_fmt x end } instance [has_repr α] : has_repr (with_top α) := ⟨λ o, match o with | none := "⊤" | (some a) := "↑" ++ repr a end⟩ instance : has_coe_t α (with_top α) := ⟨some⟩ instance : has_top (with_top α) := ⟨none⟩ meta instance {α : Type} [reflected _ α] [has_reflect α] : has_reflect (with_top α) | ⊤ := `(⊤) | (a : α) := `(coe : α → with_top α).subst `(a) instance : inhabited (with_top α) := ⟨⊤⟩ lemma none_eq_top : (none : with_top α) = (⊤ : with_top α) := rfl lemma some_eq_coe (a : α) : (some a : with_top α) = (↑a : with_top α) := rfl @[simp] lemma top_ne_coe : ⊤ ≠ (a : with_top α) . @[simp] lemma coe_ne_top : (a : with_top α) ≠ ⊤ . /-- Recursor for `with_top` using the preferred forms `⊤` and `↑a`. -/ @[elab_as_eliminator] def rec_top_coe {C : with_top α → Sort*} (h₁ : C ⊤) (h₂ : Π (a : α), C a) : Π (n : with_top α), C n := option.rec h₁ h₂ @[simp] lemma rec_top_coe_top {C : with_top α → Sort*} (d : C ⊤) (f : Π (a : α), C a) : @rec_top_coe _ C d f ⊤ = d := rfl @[simp] lemma rec_top_coe_coe {C : with_top α → Sort*} (d : C ⊤) (f : Π (a : α), C a) (x : α) : @rec_top_coe _ C d f ↑x = f x := rfl /-- `with_top.to_dual` is the equivalence sending `⊤` to `⊥` and any `a : α` to `to_dual a : αᵒᵈ`. See `with_top.to_dual_bot_equiv` for the related order-iso. -/ protected def to_dual : with_top α ≃ with_bot αᵒᵈ := equiv.refl _ /-- `with_top.of_dual` is the equivalence sending `⊤` to `⊥` and any `a : αᵒᵈ` to `of_dual a : α`. See `with_top.to_dual_bot_equiv` for the related order-iso. -/ protected def of_dual : with_top αᵒᵈ ≃ with_bot α := equiv.refl _ /-- `with_bot.to_dual` is the equivalence sending `⊥` to `⊤` and any `a : α` to `to_dual a : αᵒᵈ`. See `with_bot.to_dual_top_equiv` for the related order-iso. -/ protected def _root_.with_bot.to_dual : with_bot α ≃ with_top αᵒᵈ := equiv.refl _ /-- `with_bot.of_dual` is the equivalence sending `⊥` to `⊤` and any `a : αᵒᵈ` to `of_dual a : α`. See `with_bot.to_dual_top_equiv` for the related order-iso. -/ protected def _root_.with_bot.of_dual : with_bot αᵒᵈ ≃ with_top α := equiv.refl _ @[simp] lemma to_dual_symm_apply (a : with_bot αᵒᵈ) : with_top.to_dual.symm a = a.of_dual := rfl @[simp] lemma of_dual_symm_apply (a : with_bot α) : with_top.of_dual.symm a = a.to_dual := rfl @[simp] lemma to_dual_apply_top : with_top.to_dual (⊤ : with_top α) = ⊥ := rfl @[simp] lemma of_dual_apply_top : with_top.of_dual (⊤ : with_top α) = ⊥ := rfl @[simp] lemma to_dual_apply_coe (a : α) : with_top.to_dual (a : with_top α) = to_dual a := rfl @[simp] lemma of_dual_apply_coe (a : αᵒᵈ) : with_top.of_dual (a : with_top αᵒᵈ) = of_dual a := rfl /-- Specialization of `option.get_or_else` to values in `with_top α` that respects API boundaries. -/ def untop' (d : α) (x : with_top α) : α := rec_top_coe d id x @[simp] lemma untop'_top {α} (d : α) : untop' d ⊤ = d := rfl @[simp] lemma untop'_coe {α} (d x : α) : untop' d x = x := rfl @[norm_cast] lemma coe_eq_coe : (a : with_top α) = b ↔ a = b := option.some_inj /-- Lift a map `f : α → β` to `with_top α → with_top β`. Implemented using `option.map`. -/ def map (f : α → β) : with_top α → with_top β := option.map f @[simp] lemma map_top (f : α → β) : map f ⊤ = ⊤ := rfl @[simp] lemma map_coe (f : α → β) (a : α) : map f a = f a := rfl lemma map_to_dual (f : αᵒᵈ → βᵒᵈ) (a : with_bot α) : map f (with_bot.to_dual a) = a.map (to_dual ∘ f) := rfl lemma map_of_dual (f : α → β) (a : with_bot αᵒᵈ) : map f (with_bot.of_dual a) = a.map (of_dual ∘ f) := rfl lemma to_dual_map (f : α → β) (a : with_top α) : with_top.to_dual (map f a) = with_bot.map (to_dual ∘ f ∘ of_dual) a.to_dual := rfl lemma of_dual_map (f : αᵒᵈ → βᵒᵈ) (a : with_top αᵒᵈ) : with_top.of_dual (map f a) = with_bot.map (of_dual ∘ f ∘ to_dual) a.of_dual := rfl lemma ne_top_iff_exists {x : with_top α} : x ≠ ⊤ ↔ ∃ (a : α), ↑a = x := option.ne_none_iff_exists /-- Deconstruct a `x : with_top α` to the underlying value in `α`, given a proof that `x ≠ ⊤`. -/ def untop : Π (x : with_top α), x ≠ ⊤ → α := with_bot.unbot @[simp] lemma coe_untop (x : with_top α) (h : x ≠ ⊤) : (x.untop h : with_top α) = x := with_bot.coe_unbot x h @[simp] lemma untop_coe (x : α) (h : (x : with_top α) ≠ ⊤ := coe_ne_top) : (x : with_top α).untop h = x := rfl instance : can_lift (with_top α) α := { coe := coe, cond := λ r, r ≠ ⊤, prf := λ x h, ⟨x.untop h, coe_untop _ _⟩ } section has_le variables [has_le α] @[priority 10] instance : has_le (with_top α) := ⟨λ o₁ o₂ : option α, ∀ a ∈ o₂, ∃ b ∈ o₁, b ≤ a⟩ lemma to_dual_le_iff {a : with_top α} {b : with_bot αᵒᵈ} : with_top.to_dual a ≤ b ↔ with_bot.of_dual b ≤ a := iff.rfl lemma le_to_dual_iff {a : with_bot αᵒᵈ} {b : with_top α} : a ≤ with_top.to_dual b ↔ b ≤ with_bot.of_dual a := iff.rfl @[simp] lemma to_dual_le_to_dual_iff {a b : with_top α} : with_top.to_dual a ≤ with_top.to_dual b ↔ b ≤ a := iff.rfl lemma of_dual_le_iff {a : with_top αᵒᵈ} {b : with_bot α} : with_top.of_dual a ≤ b ↔ with_bot.to_dual b ≤ a := iff.rfl lemma le_of_dual_iff {a : with_bot α} {b : with_top αᵒᵈ} : a ≤ with_top.of_dual b ↔ b ≤ with_bot.to_dual a := iff.rfl @[simp] lemma of_dual_le_of_dual_iff {a b : with_top αᵒᵈ} : with_top.of_dual a ≤ with_top.of_dual b ↔ b ≤ a := iff.rfl @[simp, norm_cast] lemma coe_le_coe : (a : with_top α) ≤ b ↔ a ≤ b := by simp only [←to_dual_le_to_dual_iff, to_dual_apply_coe, with_bot.coe_le_coe, to_dual_le_to_dual] @[simp] lemma some_le_some : @has_le.le (with_top α) _ (some a) (some b) ↔ a ≤ b := coe_le_coe @[simp] lemma le_none {a : with_top α} : @has_le.le (with_top α) _ a none := to_dual_le_to_dual_iff.mp with_bot.none_le instance : order_top (with_top α) := { le_top := λ a, le_none, .. with_top.has_top } instance [order_bot α] : order_bot (with_top α) := { bot := some ⊥, bot_le := λ o a ha, by cases ha; exact ⟨_, rfl, bot_le⟩ } instance [order_bot α] : bounded_order (with_top α) := { ..with_top.order_top, ..with_top.order_bot } lemma not_top_le_coe (a : α) : ¬ (⊤ : with_top α) ≤ ↑a := with_bot.not_coe_le_bot (to_dual a) lemma le_coe : ∀ {o : option α}, a ∈ o → (@has_le.le (with_top α) _ o b ↔ a ≤ b) | _ rfl := coe_le_coe lemma le_coe_iff {x : with_top α} : x ≤ b ↔ ∃ a : α, x = a ∧ a ≤ b := by simpa [←to_dual_le_to_dual_iff, with_bot.coe_le_iff] lemma coe_le_iff {x : with_top α} : ↑a ≤ x ↔ ∀ b, x = ↑b → a ≤ b := begin simp only [←to_dual_le_to_dual_iff, to_dual_apply_coe, with_bot.le_coe_iff, order_dual.forall, to_dual_le_to_dual], exact forall₂_congr (λ _ _, iff.rfl) end protected lemma _root_.is_min.with_top (h : is_min a) : is_min (a : with_top α) := begin -- defeq to is_max_to_dual_iff.mp (is_max.with_bot _), but that breaks API boundary intros _ hb, rw ←to_dual_le_to_dual_iff at hb, simpa [to_dual_le_iff] using (is_max.with_bot h : is_max (to_dual a : with_bot αᵒᵈ)) hb end end has_le section has_lt variables [has_lt α] @[priority 10] instance : has_lt (with_top α) := ⟨λ o₁ o₂ : option α, ∃ b ∈ o₁, ∀ a ∈ o₂, b < a⟩ lemma to_dual_lt_iff {a : with_top α} {b : with_bot αᵒᵈ} : with_top.to_dual a < b ↔ with_bot.of_dual b < a := iff.rfl lemma lt_to_dual_iff {a : with_bot αᵒᵈ} {b : with_top α} : a < with_top.to_dual b ↔ b < with_bot.of_dual a := iff.rfl @[simp] lemma to_dual_lt_to_dual_iff {a b : with_top α} : with_top.to_dual a < with_top.to_dual b ↔ b < a := iff.rfl lemma of_dual_lt_iff {a : with_top αᵒᵈ} {b : with_bot α} : with_top.of_dual a < b ↔ with_bot.to_dual b < a := iff.rfl lemma lt_of_dual_iff {a : with_bot α} {b : with_top αᵒᵈ} : a < with_top.of_dual b ↔ b < with_bot.to_dual a := iff.rfl @[simp] lemma of_dual_lt_of_dual_iff {a b : with_top αᵒᵈ} : with_top.of_dual a < with_top.of_dual b ↔ b < a := iff.rfl end has_lt end with_top namespace with_bot @[simp] lemma to_dual_symm_apply (a : with_top αᵒᵈ) : with_bot.to_dual.symm a = a.of_dual := rfl @[simp] lemma of_dual_symm_apply (a : with_top α) : with_bot.of_dual.symm a = a.to_dual := rfl @[simp] lemma to_dual_apply_bot : with_bot.to_dual (⊥ : with_bot α) = ⊤ := rfl @[simp] lemma of_dual_apply_bot : with_bot.of_dual (⊥ : with_bot α) = ⊤ := rfl @[simp] lemma to_dual_apply_coe (a : α) : with_bot.to_dual (a : with_bot α) = to_dual a := rfl @[simp] lemma of_dual_apply_coe (a : αᵒᵈ) : with_bot.of_dual (a : with_bot αᵒᵈ) = of_dual a := rfl lemma map_to_dual (f : αᵒᵈ → βᵒᵈ) (a : with_top α) : with_bot.map f (with_top.to_dual a) = a.map (to_dual ∘ f) := rfl lemma map_of_dual (f : α → β) (a : with_top αᵒᵈ) : with_bot.map f (with_top.of_dual a) = a.map (of_dual ∘ f) := rfl lemma to_dual_map (f : α → β) (a : with_bot α) : with_bot.to_dual (with_bot.map f a) = map (to_dual ∘ f ∘ of_dual) a.to_dual := rfl lemma of_dual_map (f : αᵒᵈ → βᵒᵈ) (a : with_bot αᵒᵈ) : with_bot.of_dual (with_bot.map f a) = map (of_dual ∘ f ∘ to_dual) a.of_dual := rfl section has_le variables [has_le α] {a b : α} lemma to_dual_le_iff {a : with_bot α} {b : with_top αᵒᵈ} : with_bot.to_dual a ≤ b ↔ with_top.of_dual b ≤ a := iff.rfl lemma le_to_dual_iff {a : with_top αᵒᵈ} {b : with_bot α} : a ≤ with_bot.to_dual b ↔ b ≤ with_top.of_dual a := iff.rfl @[simp] lemma to_dual_le_to_dual_iff {a b : with_bot α} : with_bot.to_dual a ≤ with_bot.to_dual b ↔ b ≤ a := iff.rfl lemma of_dual_le_iff {a : with_bot αᵒᵈ} {b : with_top α} : with_bot.of_dual a ≤ b ↔ with_top.to_dual b ≤ a := iff.rfl lemma le_of_dual_iff {a : with_top α} {b : with_bot αᵒᵈ} : a ≤ with_bot.of_dual b ↔ b ≤ with_top.to_dual a := iff.rfl @[simp] lemma of_dual_le_of_dual_iff {a b : with_bot αᵒᵈ} : with_bot.of_dual a ≤ with_bot.of_dual b ↔ b ≤ a := iff.rfl end has_le section has_lt variables [has_lt α] {a b : α} lemma to_dual_lt_iff {a : with_bot α} {b : with_top αᵒᵈ} : with_bot.to_dual a < b ↔ with_top.of_dual b < a := iff.rfl lemma lt_to_dual_iff {a : with_top αᵒᵈ} {b : with_bot α} : a < with_bot.to_dual b ↔ b < with_top.of_dual a := iff.rfl @[simp] lemma to_dual_lt_to_dual_iff {a b : with_bot α} : with_bot.to_dual a < with_bot.to_dual b ↔ b < a := iff.rfl lemma of_dual_lt_iff {a : with_bot αᵒᵈ} {b : with_top α} : with_bot.of_dual a < b ↔ with_top.to_dual b < a := iff.rfl lemma lt_of_dual_iff {a : with_top α} {b : with_bot αᵒᵈ} : a < with_bot.of_dual b ↔ b < with_top.to_dual a := iff.rfl @[simp] lemma of_dual_lt_of_dual_iff {a b : with_bot αᵒᵈ} : with_bot.of_dual a < with_bot.of_dual b ↔ b < a := iff.rfl end has_lt end with_bot namespace with_top section has_lt variables [has_lt α] {a b : α} @[simp, norm_cast] lemma coe_lt_coe : (a : with_top α) < b ↔ a < b := by simp only [←to_dual_lt_to_dual_iff, to_dual_apply_coe, with_bot.coe_lt_coe, to_dual_lt_to_dual] @[simp] lemma some_lt_some : @has_lt.lt (with_top α) _ (some a) (some b) ↔ a < b := coe_lt_coe lemma coe_lt_top (a : α) : (a : with_top α) < ⊤ := by simpa [←to_dual_lt_to_dual_iff] using with_bot.bot_lt_coe _ @[simp] lemma some_lt_none (a : α) : @has_lt.lt (with_top α) _ (some a) none := coe_lt_top a @[simp] lemma not_none_lt (a : with_top α) : ¬ @has_lt.lt (with_top α) _ none a := begin rw [←to_dual_lt_to_dual_iff], exact with_bot.not_lt_none _ end lemma lt_iff_exists_coe {a b : with_top α} : a < b ↔ ∃ p : α, a = p ∧ ↑p < b := begin rw [←to_dual_lt_to_dual_iff, with_bot.lt_iff_exists_coe, order_dual.exists], exact exists_congr (λ _, and_congr_left' iff.rfl) end lemma coe_lt_iff {x : with_top α} : ↑a < x ↔ ∀ b, x = ↑b → a < b := begin simp only [←to_dual_lt_to_dual_iff, with_bot.lt_coe_iff, to_dual_apply_coe, order_dual.forall, to_dual_lt_to_dual], exact forall₂_congr (λ _ _, iff.rfl) end end has_lt instance [preorder α] : preorder (with_top α) := { le := (≤), lt := (<), lt_iff_le_not_le := by simp [←to_dual_lt_to_dual_iff, lt_iff_le_not_le], le_refl := λ _, to_dual_le_to_dual_iff.mp le_rfl, le_trans := λ _ _ _, by { simp_rw [←to_dual_le_to_dual_iff], exact function.swap le_trans } } instance [partial_order α] : partial_order (with_top α) := { le_antisymm := λ _ _, by { simp_rw [←to_dual_le_to_dual_iff], exact function.swap le_antisymm }, .. with_top.preorder } lemma map_le_iff [preorder α] [preorder β] (f : α → β) (a b : with_top α) (mono_iff : ∀ {a b}, f a ≤ f b ↔ a ≤ b) : a.map f ≤ b.map f ↔ a ≤ b := begin rw [←to_dual_le_to_dual_iff, to_dual_map, to_dual_map, with_bot.map_le_iff, to_dual_le_to_dual_iff], simp [mono_iff] end instance [semilattice_inf α] : semilattice_inf (with_top α) := { inf := option.lift_or_get (⊓), inf_le_left := λ o₁ o₂ a ha, by cases ha; cases o₂; simp [option.lift_or_get], inf_le_right := λ o₁ o₂ a ha, by cases ha; cases o₁; simp [option.lift_or_get], le_inf := λ o₁ o₂ o₃ h₁ h₂ a ha, begin cases o₂ with b; cases o₃ with c; cases ha, { exact h₂ a rfl }, { exact h₁ a rfl }, { rcases h₁ b rfl with ⟨d, ⟨⟩, h₁'⟩, simp at h₂, exact ⟨d, rfl, le_inf h₁' h₂⟩ } end, ..with_top.partial_order } lemma coe_inf [semilattice_inf α] (a b : α) : ((a ⊓ b : α) : with_top α) = a ⊓ b := rfl instance [semilattice_sup α] : semilattice_sup (with_top α) := { sup := λ o₁ o₂, o₁.bind (λ a, o₂.map (λ b, a ⊔ b)), le_sup_left := λ o₁ o₂ a ha, begin simp [map] at ha, rcases ha with ⟨b, rfl, c, rfl, rfl⟩, exact ⟨_, rfl, le_sup_left⟩ end, le_sup_right := λ o₁ o₂ a ha, begin simp [map] at ha, rcases ha with ⟨b, rfl, c, rfl, rfl⟩, exact ⟨_, rfl, le_sup_right⟩ end, sup_le := λ o₁ o₂ o₃ h₁ h₂ a ha, begin cases ha, rcases h₁ a rfl with ⟨b, ⟨⟩, ab⟩, rcases h₂ a rfl with ⟨c, ⟨⟩, ac⟩, exact ⟨_, rfl, sup_le ab ac⟩ end, ..with_top.partial_order } lemma coe_sup [semilattice_sup α] (a b : α) : ((a ⊔ b : α) : with_top α) = a ⊔ b := rfl instance [lattice α] : lattice (with_top α) := { ..with_top.semilattice_sup, ..with_top.semilattice_inf } instance decidable_le [has_le α] [@decidable_rel α (≤)] : @decidable_rel (with_top α) (≤) := λ _ _, decidable_of_decidable_of_iff (with_bot.decidable_le _ _) (to_dual_le_to_dual_iff) instance decidable_lt [has_lt α] [@decidable_rel α (<)] : @decidable_rel (with_top α) (<) := λ _ _, decidable_of_decidable_of_iff (with_bot.decidable_lt _ _) (to_dual_lt_to_dual_iff) instance is_total_le [has_le α] [is_total α (≤)] : is_total (with_top α) (≤) := ⟨λ _ _, by { simp_rw ←to_dual_le_to_dual_iff, exact total_of _ _ _ }⟩ instance [linear_order α] : linear_order (with_top α) := lattice.to_linear_order _ @[simp, norm_cast] lemma coe_min [linear_order α] (x y : α) : (↑(min x y) : with_top α) = min x y := rfl @[simp, norm_cast] lemma coe_max [linear_order α] (x y : α) : (↑(max x y) : with_top α) = max x y := rfl lemma well_founded_lt [preorder α] (h : @well_founded α (<)) : @well_founded (with_top α) (<) := have acc_some : ∀ a : α, acc ((<) : with_top α → with_top α → Prop) (some a) := λ a, acc.intro _ (well_founded.induction h a (show ∀ b, (∀ c, c < b → ∀ d : with_top α, d < some c → acc (<) d) → ∀ y : with_top α, y < some b → acc (<) y, from λ b ih c, option.rec_on c (λ hc, (not_lt_of_ge le_top hc).elim) (λ c hc, acc.intro _ (ih _ (some_lt_some.1 hc))))), ⟨λ a, option.rec_on a (acc.intro _ (λ y, option.rec_on y (λ h, (lt_irrefl _ h).elim) (λ _ _, acc_some _))) acc_some⟩ lemma well_founded_gt [preorder α] (h : @well_founded α (>)) : @well_founded (with_top α) (>) := ⟨λ a, begin -- ideally, use rel_hom_class.acc, but that is defined later have : acc (<) a.to_dual := well_founded.apply (with_bot.well_founded_lt h) _, revert this, generalize ha : a.to_dual = b, intro ac, induction ac with _ H IH generalizing a, subst ha, exact ⟨_, λ a' h, IH (a'.to_dual) (to_dual_lt_to_dual.mpr h) _ rfl⟩ end⟩ lemma _root_.with_bot.well_founded_gt [preorder α] (h : @well_founded α (>)) : @well_founded (with_bot α) (>) := ⟨λ a, begin -- ideally, use rel_hom_class.acc, but that is defined later have : acc (<) a.to_dual := well_founded.apply (with_top.well_founded_lt h) _, revert this, generalize ha : a.to_dual = b, intro ac, induction ac with _ H IH generalizing a, subst ha, exact ⟨_, λ a' h, IH (a'.to_dual) (to_dual_lt_to_dual.mpr h) _ rfl⟩ end⟩ instance trichotomous.lt [preorder α] [is_trichotomous α (<)] : is_trichotomous (with_top α) (<) := ⟨begin rintro (a | _) (b | _), iterate 3 { simp }, simpa [option.some_inj] using @trichotomous _ (<) _ a b end⟩ instance is_well_order.lt [preorder α] [h : is_well_order α (<)] : is_well_order (with_top α) (<) := { wf := well_founded_lt h.wf } instance trichotomous.gt [preorder α] [is_trichotomous α (>)] : is_trichotomous (with_top α) (>) := ⟨begin rintro (a | _) (b | _), iterate 3 { simp }, simpa [option.some_inj] using @trichotomous _ (>) _ a b end⟩ instance is_well_order.gt [preorder α] [h : is_well_order α (>)] : is_well_order (with_top α) (>) := { wf := well_founded_gt h.wf } instance _root_.with_bot.trichotomous.lt [preorder α] [h : is_trichotomous α (<)] : is_trichotomous (with_bot α) (<) := @with_top.trichotomous.gt αᵒᵈ _ h instance _root_.with_bot.is_well_order.lt [preorder α] [h : is_well_order α (<)] : is_well_order (with_bot α) (<) := @with_top.is_well_order.gt αᵒᵈ _ h instance _root_.with_bot.trichotomous.gt [preorder α] [h : is_trichotomous α (>)] : is_trichotomous (with_bot α) (>) := @with_top.trichotomous.lt αᵒᵈ _ h instance _root_.with_bot.is_well_order.gt [preorder α] [h : is_well_order α (>)] : is_well_order (with_bot α) (>) := @with_top.is_well_order.lt αᵒᵈ _ h instance [has_lt α] [densely_ordered α] [no_max_order α] : densely_ordered (with_top α) := order_dual.densely_ordered (with_bot αᵒᵈ) lemma lt_iff_exists_coe_btwn [preorder α] [densely_ordered α] [no_max_order α] {a b : with_top α} : a < b ↔ ∃ x : α, a < ↑x ∧ ↑x < b := ⟨λ h, let ⟨y, hy⟩ := exists_between h, ⟨x, hx⟩ := lt_iff_exists_coe.1 hy.2 in ⟨x, hx.1 ▸ hy⟩, λ ⟨x, hx⟩, lt_trans hx.1 hx.2⟩ instance [has_le α] [no_bot_order α] [nonempty α] : no_bot_order (with_top α) := order_dual.no_bot_order (with_bot αᵒᵈ) instance [has_lt α] [no_min_order α] [nonempty α] : no_min_order (with_top α) := order_dual.no_min_order (with_bot αᵒᵈ) end with_top section mono variables [preorder α] [preorder β] {f : α → β} protected lemma monotone.with_bot_map (hf : monotone f) : monotone (with_bot.map f) | ⊥ _ h := bot_le | (a : α) ⊥ h := (with_bot.not_coe_le_bot _ h).elim | (a : α) (b : α) h := with_bot.coe_le_coe.2 (hf (with_bot.coe_le_coe.1 h)) protected lemma monotone.with_top_map (hf : monotone f) : monotone (with_top.map f) := hf.dual.with_bot_map.dual protected lemma strict_mono.with_bot_map (hf : strict_mono f) : strict_mono (with_bot.map f) | ⊥ (a : α) h := with_bot.bot_lt_coe _ | (a : α) (b : α) h := with_bot.coe_lt_coe.mpr (hf $ with_bot.coe_lt_coe.mp h) protected lemma strict_mono.with_top_map (hf : strict_mono f) : strict_mono (with_top.map f) := hf.dual.with_bot_map.dual end mono /-! ### Subtype, order dual, product lattices -/ namespace subtype variables {p : α → Prop} /-- A subtype remains a `⊥`-order if the property holds at `⊥`. -/ @[reducible] -- See note [reducible non-instances] protected def order_bot [has_le α] [order_bot α] (hbot : p ⊥) : order_bot {x : α // p x} := { bot := ⟨⊥, hbot⟩, bot_le := λ _, bot_le } /-- A subtype remains a `⊤`-order if the property holds at `⊤`. -/ @[reducible] -- See note [reducible non-instances] protected def order_top [has_le α] [order_top α] (htop : p ⊤) : order_top {x : α // p x} := { top := ⟨⊤, htop⟩, le_top := λ _, le_top } /-- A subtype remains a bounded order if the property holds at `⊥` and `⊤`. -/ @[reducible] -- See note [reducible non-instances] protected def bounded_order [has_le α] [bounded_order α] (hbot : p ⊥) (htop : p ⊤) : bounded_order (subtype p) := { ..subtype.order_top htop, ..subtype.order_bot hbot } variables [partial_order α] @[simp] lemma mk_bot [order_bot α] [order_bot (subtype p)] (hbot : p ⊥) : mk ⊥ hbot = ⊥ := le_bot_iff.1 $ coe_le_coe.1 bot_le @[simp] lemma mk_top [order_top α] [order_top (subtype p)] (htop : p ⊤) : mk ⊤ htop = ⊤ := top_le_iff.1 $ coe_le_coe.1 le_top lemma coe_bot [order_bot α] [order_bot (subtype p)] (hbot : p ⊥) : ((⊥ : subtype p) : α) = ⊥ := congr_arg coe (mk_bot hbot).symm lemma coe_top [order_top α] [order_top (subtype p)] (htop : p ⊤) : ((⊤ : subtype p) : α) = ⊤ := congr_arg coe (mk_top htop).symm @[simp] lemma coe_eq_bot_iff [order_bot α] [order_bot (subtype p)] (hbot : p ⊥) {x : {x // p x}} : (x : α) = ⊥ ↔ x = ⊥ := by rw [←coe_bot hbot, ext_iff] @[simp] lemma coe_eq_top_iff [order_top α] [order_top (subtype p)] (htop : p ⊤) {x : {x // p x}} : (x : α) = ⊤ ↔ x = ⊤ := by rw [←coe_top htop, ext_iff] @[simp] lemma mk_eq_bot_iff [order_bot α] [order_bot (subtype p)] (hbot : p ⊥) {x : α} (hx : p x) : (⟨x, hx⟩ : subtype p) = ⊥ ↔ x = ⊥ := (coe_eq_bot_iff hbot).symm @[simp] lemma mk_eq_top_iff [order_top α] [order_top (subtype p)] (htop : p ⊤) {x : α} (hx : p x) : (⟨x, hx⟩ : subtype p) = ⊤ ↔ x = ⊤ := (coe_eq_top_iff htop).symm end subtype namespace prod variables (α β) instance [has_top α] [has_top β] : has_top (α × β) := ⟨⟨⊤, ⊤⟩⟩ instance [has_bot α] [has_bot β] : has_bot (α × β) := ⟨⟨⊥, ⊥⟩⟩ instance [has_le α] [has_le β] [order_top α] [order_top β] : order_top (α × β) := { le_top := λ a, ⟨le_top, le_top⟩, .. prod.has_top α β } instance [has_le α] [has_le β] [order_bot α] [order_bot β] : order_bot (α × β) := { bot_le := λ a, ⟨bot_le, bot_le⟩, .. prod.has_bot α β } instance [has_le α] [has_le β] [bounded_order α] [bounded_order β] : bounded_order (α × β) := { .. prod.order_top α β, .. prod.order_bot α β } end prod section linear_order variables [linear_order α] -- `simp` can prove these, so they shouldn't be simp-lemmas. lemma min_bot_left [order_bot α] (a : α) : min ⊥ a = ⊥ := bot_inf_eq lemma max_top_left [order_top α] (a : α) : max ⊤ a = ⊤ := top_sup_eq lemma min_top_left [order_top α] (a : α) : min ⊤ a = a := top_inf_eq lemma max_bot_left [order_bot α] (a : α) : max ⊥ a = a := bot_sup_eq lemma min_top_right [order_top α] (a : α) : min a ⊤ = a := inf_top_eq lemma max_bot_right [order_bot α] (a : α) : max a ⊥ = a := sup_bot_eq lemma min_bot_right [order_bot α] (a : α) : min a ⊥ = ⊥ := inf_bot_eq lemma max_top_right [order_top α] (a : α) : max a ⊤ = ⊤ := sup_top_eq @[simp] lemma min_eq_bot [order_bot α] {a b : α} : min a b = ⊥ ↔ a = ⊥ ∨ b = ⊥ := by simp only [←inf_eq_min, ←le_bot_iff, inf_le_iff] @[simp] lemma max_eq_top [order_top α] {a b : α} : max a b = ⊤ ↔ a = ⊤ ∨ b = ⊤ := @min_eq_bot αᵒᵈ _ _ a b @[simp] lemma max_eq_bot [order_bot α] {a b : α} : max a b = ⊥ ↔ a = ⊥ ∧ b = ⊥ := sup_eq_bot_iff @[simp] lemma min_eq_top [order_top α] {a b : α} : min a b = ⊤ ↔ a = ⊤ ∧ b = ⊤ := inf_eq_top_iff end linear_order /-! ### Disjointness and complements -/ section disjoint section semilattice_inf_bot variables [semilattice_inf α] [order_bot α] {a b c d : α} /-- Two elements of a lattice are disjoint if their inf is the bottom element. (This generalizes disjoint sets, viewed as members of the subset lattice.) -/ def disjoint (a b : α) : Prop := a ⊓ b ≤ ⊥ lemma disjoint_iff : disjoint a b ↔ a ⊓ b = ⊥ := le_bot_iff lemma disjoint.eq_bot : disjoint a b → a ⊓ b = ⊥ := bot_unique lemma disjoint.comm : disjoint a b ↔ disjoint b a := by rw [disjoint, disjoint, inf_comm] @[symm] lemma disjoint.symm ⦃a b : α⦄ : disjoint a b → disjoint b a := disjoint.comm.1 lemma symmetric_disjoint : symmetric (disjoint : α → α → Prop) := disjoint.symm lemma disjoint_assoc : disjoint (a ⊓ b) c ↔ disjoint a (b ⊓ c) := by rw [disjoint, disjoint, inf_assoc] lemma disjoint_left_comm : disjoint a (b ⊓ c) ↔ disjoint b (a ⊓ c) := by simp_rw [disjoint, inf_left_comm] lemma disjoint_right_comm : disjoint (a ⊓ b) c ↔ disjoint (a ⊓ c) b := by simp_rw [disjoint, inf_right_comm] @[simp] lemma disjoint_bot_left : disjoint ⊥ a := inf_le_left @[simp] lemma disjoint_bot_right : disjoint a ⊥ := inf_le_right lemma disjoint.mono (h₁ : a ≤ b) (h₂ : c ≤ d) : disjoint b d → disjoint a c := le_trans $ inf_le_inf h₁ h₂ lemma disjoint.mono_left (h : a ≤ b) : disjoint b c → disjoint a c := disjoint.mono h le_rfl lemma disjoint.mono_right : b ≤ c → disjoint a c → disjoint a b := disjoint.mono le_rfl variables (c) lemma disjoint.inf_left (h : disjoint a b) : disjoint (a ⊓ c) b := h.mono_left inf_le_left lemma disjoint.inf_left' (h : disjoint a b) : disjoint (c ⊓ a) b := h.mono_left inf_le_right lemma disjoint.inf_right (h : disjoint a b) : disjoint a (b ⊓ c) := h.mono_right inf_le_left lemma disjoint.inf_right' (h : disjoint a b) : disjoint a (c ⊓ b) := h.mono_right inf_le_right variables {c} @[simp] lemma disjoint_self : disjoint a a ↔ a = ⊥ := by simp [disjoint] /- TODO: Rename `disjoint.eq_bot` to `disjoint.inf_eq` and `disjoint.eq_bot_of_self` to `disjoint.eq_bot` -/ alias disjoint_self ↔ disjoint.eq_bot_of_self _ lemma disjoint.ne (ha : a ≠ ⊥) (hab : disjoint a b) : a ≠ b := λ h, ha $ disjoint_self.1 $ by rwa ←h at hab lemma disjoint.eq_bot_of_le (hab : disjoint a b) (h : a ≤ b) : a = ⊥ := eq_bot_iff.2 (by rwa ←inf_eq_left.2 h) lemma disjoint.eq_bot_of_ge (hab : disjoint a b) : b ≤ a → b = ⊥ := hab.symm.eq_bot_of_le lemma disjoint.of_disjoint_inf_of_le (h : disjoint (a ⊓ b) c) (hle : a ≤ c) : disjoint a b := disjoint_iff.2 $ h.eq_bot_of_le $ inf_le_of_left_le hle lemma disjoint.of_disjoint_inf_of_le' (h : disjoint (a ⊓ b) c) (hle : b ≤ c) : disjoint a b := disjoint_iff.2 $ h.eq_bot_of_le $ inf_le_of_right_le hle end semilattice_inf_bot section lattice variables [lattice α] [bounded_order α] {a : α} @[simp] theorem disjoint_top : disjoint a ⊤ ↔ a = ⊥ := by simp [disjoint_iff] @[simp] theorem top_disjoint : disjoint ⊤ a ↔ a = ⊥ := by simp [disjoint_iff] end lattice section distrib_lattice_bot variables [distrib_lattice α] [order_bot α] {a b c : α} @[simp] lemma disjoint_sup_left : disjoint (a ⊔ b) c ↔ disjoint a c ∧ disjoint b c := by simp only [disjoint_iff, inf_sup_right, sup_eq_bot_iff] @[simp] lemma disjoint_sup_right : disjoint a (b ⊔ c) ↔ disjoint a b ∧ disjoint a c := by simp only [disjoint_iff, inf_sup_left, sup_eq_bot_iff] lemma disjoint.sup_left (ha : disjoint a c) (hb : disjoint b c) : disjoint (a ⊔ b) c := disjoint_sup_left.2 ⟨ha, hb⟩ lemma disjoint.sup_right (hb : disjoint a b) (hc : disjoint a c) : disjoint a (b ⊔ c) := disjoint_sup_right.2 ⟨hb, hc⟩ lemma disjoint.left_le_of_le_sup_right (h : a ≤ b ⊔ c) (hd : disjoint a c) : a ≤ b := le_of_inf_le_sup_le (le_trans hd bot_le) $ sup_le h le_sup_right lemma disjoint.left_le_of_le_sup_left (h : a ≤ c ⊔ b) (hd : disjoint a c) : a ≤ b := hd.left_le_of_le_sup_right $ by rwa sup_comm end distrib_lattice_bot end disjoint section codisjoint section semilattice_sup_top variables [semilattice_sup α] [order_top α] {a b c d : α} /-- Two elements of a lattice are codisjoint if their sup is the top element. -/ def codisjoint (a b : α) : Prop := ⊤ ≤ a ⊔ b lemma codisjoint_iff : codisjoint a b ↔ a ⊔ b = ⊤ := top_le_iff lemma codisjoint.eq_top : codisjoint a b → a ⊔ b = ⊤ := top_unique lemma codisjoint.comm : codisjoint a b ↔ codisjoint b a := by rw [codisjoint, codisjoint, sup_comm] @[symm] lemma codisjoint.symm ⦃a b : α⦄ : codisjoint a b → codisjoint b a := codisjoint.comm.1 lemma symmetric_codisjoint : symmetric (codisjoint : α → α → Prop) := codisjoint.symm lemma codisjoint_assoc : codisjoint (a ⊔ b) c ↔ codisjoint a (b ⊔ c) := by rw [codisjoint, codisjoint, sup_assoc] lemma codisjoint_left_comm : codisjoint a (b ⊔ c) ↔ codisjoint b (a ⊔ c) := by simp_rw [codisjoint, sup_left_comm] lemma codisjoint_right_comm : codisjoint (a ⊔ b) c ↔ codisjoint (a ⊔ c) b := by simp_rw [codisjoint, sup_right_comm] @[simp] lemma codisjoint_top_left : codisjoint ⊤ a := le_sup_left @[simp] lemma codisjoint_top_right : codisjoint a ⊤ := le_sup_right lemma codisjoint.mono (h₁ : a ≤ b) (h₂ : c ≤ d) : codisjoint a c → codisjoint b d := le_trans' $ sup_le_sup h₁ h₂ lemma codisjoint.mono_left (h : a ≤ b) : codisjoint a c → codisjoint b c := codisjoint.mono h le_rfl lemma codisjoint.mono_right : b ≤ c → codisjoint a b → codisjoint a c := codisjoint.mono le_rfl variables (c) lemma codisjoint.sup_left (h : codisjoint a b) : codisjoint (a ⊔ c) b := h.mono_left le_sup_left lemma codisjoint.sup_left' (h : codisjoint a b) : codisjoint (c ⊔ a) b := h.mono_left le_sup_right lemma codisjoint.sup_right (h : codisjoint a b) : codisjoint a (b ⊔ c) := h.mono_right le_sup_left lemma codisjoint.sup_right' (h : codisjoint a b) : codisjoint a (c ⊔ b) := h.mono_right le_sup_right variables {c} @[simp] lemma codisjoint_self : codisjoint a a ↔ a = ⊤ := by simp [codisjoint] /- TODO: Rename `codisjoint.eq_top` to `codisjoint.sup_eq` and `codisjoint.eq_top_of_self` to `codisjoint.eq_top` -/ alias codisjoint_self ↔ codisjoint.eq_top_of_self _ lemma codisjoint.ne (ha : a ≠ ⊤) (hab : codisjoint a b) : a ≠ b := λ h, ha $ codisjoint_self.1 $ by rwa ←h at hab lemma codisjoint.eq_top_of_ge (hab : codisjoint a b) (h : b ≤ a) : a = ⊤ := eq_top_iff.2 $ by rwa ←sup_eq_left.2 h lemma codisjoint.eq_top_of_le (hab : codisjoint a b) : a ≤ b → b = ⊤ := hab.symm.eq_top_of_ge lemma codisjoint.of_codisjoint_sup_of_le (h : codisjoint (a ⊔ b) c) (hle : c ≤ a) : codisjoint a b := codisjoint_iff.2 $ h.eq_top_of_ge $ le_sup_of_le_left hle lemma codisjoint.of_codisjoint_sup_of_le' (h : codisjoint (a ⊔ b) c) (hle : c ≤ b) : codisjoint a b := codisjoint_iff.2 $ h.eq_top_of_ge $ le_sup_of_le_right hle end semilattice_sup_top section lattice variables [lattice α] [bounded_order α] {a : α} @[simp] lemma codisjoint_bot : codisjoint a ⊥ ↔ a = ⊤ := by simp [codisjoint_iff] @[simp] lemma bot_codisjoint : codisjoint ⊥ a ↔ a = ⊤ := by simp [codisjoint_iff] end lattice section distrib_lattice_top variables [distrib_lattice α] [order_top α] {a b c : α} @[simp] lemma codisjoint_inf_left : codisjoint (a ⊓ b) c ↔ codisjoint a c ∧ codisjoint b c := by simp only [codisjoint_iff, sup_inf_right, inf_eq_top_iff] @[simp] lemma codisjoint_inf_right : codisjoint a (b ⊓ c) ↔ codisjoint a b ∧ codisjoint a c := by simp only [codisjoint_iff, sup_inf_left, inf_eq_top_iff] lemma codisjoint.inf_left (ha : codisjoint a c) (hb : codisjoint b c) : codisjoint (a ⊓ b) c := codisjoint_inf_left.2 ⟨ha, hb⟩ lemma codisjoint.inf_right (hb : codisjoint a b) (hc : codisjoint a c) : codisjoint a (b ⊓ c) := codisjoint_inf_right.2 ⟨hb, hc⟩ lemma codisjoint.left_le_of_le_inf_right (h : a ⊓ b ≤ c) (hd : codisjoint b c) : a ≤ c := le_of_inf_le_sup_le (le_inf h inf_le_right) $ le_top.trans hd.symm lemma codisjoint.left_le_of_le_inf_left (h : b ⊓ a ≤ c) (hd : codisjoint b c) : a ≤ c := hd.left_le_of_le_inf_right $ by rwa inf_comm end distrib_lattice_top end codisjoint lemma disjoint.dual [semilattice_inf α] [order_bot α] {a b : α} : disjoint a b → codisjoint (to_dual a) (to_dual b) := id lemma codisjoint.dual [semilattice_sup α] [order_top α] {a b : α} : codisjoint a b → disjoint (to_dual a) (to_dual b) := id @[simp] lemma disjoint_to_dual_iff [semilattice_sup α] [order_top α] {a b : α} : disjoint (to_dual a) (to_dual b) ↔ codisjoint a b := iff.rfl @[simp] lemma disjoint_of_dual_iff [semilattice_inf α] [order_bot α] {a b : αᵒᵈ} : disjoint (of_dual a) (of_dual b) ↔ codisjoint a b := iff.rfl @[simp] lemma codisjoint_to_dual_iff [semilattice_inf α] [order_bot α] {a b : α} : codisjoint (to_dual a) (to_dual b) ↔ disjoint a b := iff.rfl @[simp] lemma codisjoint_of_dual_iff [semilattice_sup α] [order_top α] {a b : αᵒᵈ} : codisjoint (of_dual a) (of_dual b) ↔ disjoint a b := iff.rfl section distrib_lattice variables [distrib_lattice α] [bounded_order α] {a b c : α} lemma disjoint.le_of_codisjoint (hab : disjoint a b) (hbc : codisjoint b c) : a ≤ c := begin rw [←@inf_top_eq _ _ _ a, ←@bot_sup_eq _ _ _ c, ←hab.eq_bot, ←hbc.eq_top, sup_inf_right], exact inf_le_inf_right _ le_sup_left, end end distrib_lattice section is_compl /-- Two elements `x` and `y` are complements of each other if `x ⊔ y = ⊤` and `x ⊓ y = ⊥`. -/ @[protect_proj] structure is_compl [lattice α] [bounded_order α] (x y : α) : Prop := (disjoint : disjoint x y) (codisjoint : codisjoint x y) namespace is_compl section bounded_order variables [lattice α] [bounded_order α] {x y z : α} @[symm] protected lemma symm (h : is_compl x y) : is_compl y x := ⟨h.1.symm, h.2.symm⟩ lemma of_eq (h₁ : x ⊓ y = ⊥) (h₂ : x ⊔ y = ⊤) : is_compl x y := ⟨le_of_eq h₁, ge_of_eq h₂⟩ lemma inf_eq_bot (h : is_compl x y) : x ⊓ y = ⊥ := h.disjoint.eq_bot lemma sup_eq_top (h : is_compl x y) : x ⊔ y = ⊤ := h.codisjoint.eq_top lemma dual (h : is_compl x y) : is_compl (to_dual x) (to_dual y) := ⟨h.2, h.1⟩ lemma of_dual {a b : αᵒᵈ} (h : is_compl a b) : is_compl (of_dual a) (of_dual b) := ⟨h.2, h.1⟩ end bounded_order variables [distrib_lattice α] [bounded_order α] {a b x y z : α} lemma inf_left_le_of_le_sup_right (h : is_compl x y) (hle : a ≤ b ⊔ y) : a ⊓ x ≤ b := calc a ⊓ x ≤ (b ⊔ y) ⊓ x : inf_le_inf hle le_rfl ... = (b ⊓ x) ⊔ (y ⊓ x) : inf_sup_right ... = b ⊓ x : by rw [h.symm.inf_eq_bot, sup_bot_eq] ... ≤ b : inf_le_left lemma le_sup_right_iff_inf_left_le {a b} (h : is_compl x y) : a ≤ b ⊔ y ↔ a ⊓ x ≤ b := ⟨h.inf_left_le_of_le_sup_right, h.symm.dual.inf_left_le_of_le_sup_right⟩ lemma inf_left_eq_bot_iff (h : is_compl y z) : x ⊓ y = ⊥ ↔ x ≤ z := by rw [← le_bot_iff, ← h.le_sup_right_iff_inf_left_le, bot_sup_eq] lemma inf_right_eq_bot_iff (h : is_compl y z) : x ⊓ z = ⊥ ↔ x ≤ y := h.symm.inf_left_eq_bot_iff lemma disjoint_left_iff (h : is_compl y z) : disjoint x y ↔ x ≤ z := by { rw disjoint_iff, exact h.inf_left_eq_bot_iff } lemma disjoint_right_iff (h : is_compl y z) : disjoint x z ↔ x ≤ y := h.symm.disjoint_left_iff lemma le_left_iff (h : is_compl x y) : z ≤ x ↔ disjoint z y := h.disjoint_right_iff.symm lemma le_right_iff (h : is_compl x y) : z ≤ y ↔ disjoint z x := h.symm.le_left_iff lemma left_le_iff (h : is_compl x y) : x ≤ z ↔ ⊤ ≤ z ⊔ y := h.dual.le_left_iff lemma right_le_iff (h : is_compl x y) : y ≤ z ↔ ⊤ ≤ z ⊔ x := h.symm.left_le_iff protected lemma antitone {x' y'} (h : is_compl x y) (h' : is_compl x' y') (hx : x ≤ x') : y' ≤ y := h'.right_le_iff.2 $ le_trans h.symm.codisjoint (sup_le_sup_left hx _) lemma right_unique (hxy : is_compl x y) (hxz : is_compl x z) : y = z := le_antisymm (hxz.antitone hxy $ le_refl x) (hxy.antitone hxz $ le_refl x) lemma left_unique (hxz : is_compl x z) (hyz : is_compl y z) : x = y := hxz.symm.right_unique hyz.symm lemma sup_inf {x' y'} (h : is_compl x y) (h' : is_compl x' y') : is_compl (x ⊔ x') (y ⊓ y') := of_eq (by rw [inf_sup_right, ← inf_assoc, h.inf_eq_bot, bot_inf_eq, bot_sup_eq, inf_left_comm, h'.inf_eq_bot, inf_bot_eq]) (by rw [sup_inf_left, @sup_comm _ _ x, sup_assoc, h.sup_eq_top, sup_top_eq, top_inf_eq, sup_assoc, sup_left_comm, h'.sup_eq_top, sup_top_eq]) lemma inf_sup {x' y'} (h : is_compl x y) (h' : is_compl x' y') : is_compl (x ⊓ x') (y ⊔ y') := (h.symm.sup_inf h'.symm).symm end is_compl section variables [lattice α] [bounded_order α] {a b x : α} @[simp] lemma is_compl_to_dual_iff : is_compl (to_dual a) (to_dual b) ↔ is_compl a b := ⟨is_compl.of_dual, is_compl.dual⟩ @[simp] lemma is_compl_of_dual_iff {a b : αᵒᵈ} : is_compl (of_dual a) (of_dual b) ↔ is_compl a b := ⟨is_compl.dual, is_compl.of_dual⟩ lemma is_compl_bot_top : is_compl (⊥ : α) ⊤ := is_compl.of_eq bot_inf_eq sup_top_eq lemma is_compl_top_bot : is_compl (⊤ : α) ⊥ := is_compl.of_eq inf_bot_eq top_sup_eq lemma eq_top_of_is_compl_bot (h : is_compl x ⊥) : x = ⊤ := sup_bot_eq.symm.trans h.sup_eq_top lemma eq_top_of_bot_is_compl (h : is_compl ⊥ x) : x = ⊤ := eq_top_of_is_compl_bot h.symm lemma eq_bot_of_is_compl_top (h : is_compl x ⊤) : x = ⊥ := eq_top_of_is_compl_bot h.dual lemma eq_bot_of_top_is_compl (h : is_compl ⊤ x) : x = ⊥ := eq_top_of_bot_is_compl h.dual end /-- A complemented bounded lattice is one where every element has a (not necessarily unique) complement. -/ class is_complemented (α) [lattice α] [bounded_order α] : Prop := (exists_is_compl : ∀ (a : α), ∃ (b : α), is_compl a b) export is_complemented (exists_is_compl) namespace is_complemented variables [lattice α] [bounded_order α] [is_complemented α] instance : is_complemented αᵒᵈ := ⟨λ a, let ⟨b, hb⟩ := exists_is_compl (show α, from a) in ⟨b, hb.dual⟩⟩ end is_complemented end is_compl section nontrivial variables [partial_order α] [bounded_order α] [nontrivial α] lemma bot_ne_top : (⊥ : α) ≠ ⊤ := λ H, not_nontrivial_iff_subsingleton.mpr (subsingleton_of_bot_eq_top H) ‹_› lemma top_ne_bot : (⊤ : α) ≠ ⊥ := bot_ne_top.symm lemma bot_lt_top : (⊥ : α) < ⊤ := lt_top_iff_ne_top.2 bot_ne_top end nontrivial section bool open bool instance : bounded_order bool := { top := tt, le_top := λ x, le_tt, bot := ff, bot_le := λ x, ff_le } @[simp] lemma top_eq_tt : ⊤ = tt := rfl @[simp] lemma bot_eq_ff : ⊥ = ff := rfl end bool
e022716aea7633fe0bd022744597215f6d17fd06
02fbe05a45fda5abde7583464416db4366eedfbf
/tests/lean/list_local_vars.lean
3364da072e0a54fb96752ccac144afd997c8cbbb
[ "Apache-2.0" ]
permissive
jasonrute/lean
cc12807e11f9ac6b01b8951a8bfb9c2eb35a0154
4be962c167ca442a0ec5e84472d7ff9f5302788f
refs/heads/master
1,672,036,664,637
1,601,642,826,000
1,601,642,826,000
260,777,966
0
0
Apache-2.0
1,588,454,819,000
1,588,454,818,000
null
UTF-8
Lean
false
false
1,992
lean
-- #exit open interactive tactic.interactive namespace tactic.interactive open lean.parser meta def tac (ls : parse lean.parser.list_include_var_names) : tactic unit := trace ls open interactive.types @[user_command] meta def tac_cmd (_ : parse $ tk "stuff") : lean.parser unit := with_local_scope $ do ls ← lean.parser.list_available_include_vars, trace ls, ls ← lean.parser.list_include_var_names, (n,t) ← brackets "(" ")" (prod.mk <$> (ident <* tk ":") <*> texpr), t ← tactic.to_expr t, v ← tactic.mk_local_def n t, lean.parser.add_local v, (n',t') ← brackets "(" ")" (prod.mk <$> (ident <* tk ":") <*> texpr), t' ← tactic.to_expr t', v ← tactic.mk_local_def n' t', lean.parser.add_local v, include_var v.local_pp_name, trace ls, ls ← tactic.local_context, trace ls, brackets "(" ")" texpr, ↑(ls.mmap' $ trace ∘ to_string) @[user_command] meta def add_var_cmd (_ : parse $ tk "add_var") : lean.parser unit := do (n,t) ← brackets "(" ")" (prod.mk <$> (ident <* tk ":") <*> texpr), t ← tactic.to_expr t, v ← tactic.mk_local_def n t, add_local_level `u, lean.parser.add_local v, include_var v.local_pp_name, (n',t') ← brackets "(" ")" (prod.mk <$> (ident <* tk ":") <*> texpr), t' ← tactic.to_expr t', v ← tactic.mk_local_def n' t', lean.parser.add_local v, include_var v.local_pp_name, omit_var v.local_pp_name, ls ← lean.parser.list_include_var_names, trace ls, trace_state end tactic.interactive variables (a b c : ℕ) include a b variables {α : Type} section stuff (β : Type) (γ : Type) (α × β × γ) def x := β def y := γ #check x section add_var (β : Type) (γ : Type u) def x' : β → β := id def y' : γ → γ := id end def x'' := β def y'' := γ #check x' #check y' section parameter δ : Type #print δ #print β end #print γ end example : c ≤ 3 := begin (do v ← tactic.get_local `a, trace $ v.local_pp_name ), tac end
66576dd1c2b85031af86344c603c5d278b30a355
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/tactic/default.lean
b5754b54ac5a6f97697490974ecc9041a0c4df21
[ "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
1,137
lean
/- This file imports many useful tactics ("the kitchen sink"). You can use `import tactic` at the beginning of your file to get everything. (Although you may want to strip things down when you're polishing.) Because this file imports some complicated tactics, it has many transitive dependencies (which of course may not use `import tactic`, and must import selectively). As (non-exhaustive) examples, these includes things like: * algebra.group_power * algebra.order.ring * data.rat * data.nat.prime * data.list.perm * data.set.lattice * data.equiv.encodable.basic * order.complete_lattice -/ import tactic.basic -- ensure basic tactics are available import tactic.abel import tactic.ring_exp import tactic.noncomm_ring import tactic.linarith import tactic.omega import tactic.tfae import tactic.apply_fun import tactic.interval_cases import tactic.reassoc_axiom -- most likely useful only for category_theory import tactic.slice import tactic.subtype_instance import tactic.derive_fintype import tactic.group import tactic.cancel_denoms import tactic.zify import tactic.transport import tactic.unfold_cases import tactic.field_simp
8b39e263c3026ac58ab8ef57ac0615b8f092a49c
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/group_theory/order_of_element_auto.lean
e11ec2f74fdd0350db1bdd47804ca28941ed653b
[]
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,901
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.big_operators.order import Mathlib.group_theory.coset import Mathlib.data.nat.totient import Mathlib.data.int.gcd import Mathlib.data.set.finite import Mathlib.PostPort universes u_1 u_2 l namespace Mathlib -- TODO mem_range_iff_mem_finset_range_of_mod_eq should be moved elsewhere. namespace finset theorem mem_range_iff_mem_finset_range_of_mod_eq {α : Type u_1} [DecidableEq α] {f : ℤ → α} {a : α} {n : ℕ} (hn : 0 < n) (h : ∀ (i : ℤ), f (i % ↑n) = f i) : a ∈ set.range f ↔ a ∈ image (fun (i : ℕ) => f ↑i) (range n) := sorry end finset theorem mem_normalizer_fintype {α : Type u_1} [group α] {s : set α} [fintype ↥s] {x : α} (h : ∀ (n : α), n ∈ s → x * n * (x⁻¹) ∈ s) : x ∈ subgroup.set_normalizer s := sorry protected instance fintype_bot {α : Type u_1} [group α] : fintype ↥⊥ := fintype.mk (singleton 1) sorry @[simp] theorem card_trivial {α : Type u_1} [group α] : fintype.card ↥⊥ = 1 := sorry theorem card_eq_card_quotient_mul_card_subgroup {α : Type u_1} [group α] [fintype α] (s : subgroup α) [fintype ↥s] [decidable_pred fun (a : α) => a ∈ s] : fintype.card α = fintype.card (quotient_group.quotient s) * fintype.card ↥s := sorry theorem card_subgroup_dvd_card {α : Type u_1} [group α] [fintype α] (s : subgroup α) [fintype ↥s] : fintype.card ↥s ∣ fintype.card α := sorry theorem card_quotient_dvd_card {α : Type u_1} [group α] [fintype α] (s : subgroup α) [decidable_pred fun (a : α) => a ∈ s] [fintype ↥s] : fintype.card (quotient_group.quotient s) ∣ fintype.card α := sorry theorem exists_gpow_eq_one {α : Type u_1} [group α] [fintype α] (a : α) : ∃ (i : ℤ), ∃ (H : i ≠ 0), a ^ i = 1 := sorry theorem exists_pow_eq_one {α : Type u_1} [group α] [fintype α] (a : α) : ∃ (i : ℕ), ∃ (H : i > 0), a ^ i = 1 := sorry /-- `order_of a` is the order of the element `a`, i.e. the `n ≥ 1`, s.t. `a ^ n = 1` -/ def order_of {α : Type u_1} [group α] [fintype α] [dec : DecidableEq α] (a : α) : ℕ := nat.find (exists_pow_eq_one a) theorem pow_order_of_eq_one {α : Type u_1} [group α] [fintype α] [dec : DecidableEq α] (a : α) : a ^ order_of a = 1 := sorry theorem order_of_pos {α : Type u_1} [group α] [fintype α] [dec : DecidableEq α] (a : α) : 0 < order_of a := sorry theorem pow_injective_of_lt_order_of {α : Type u_1} [group α] [fintype α] [dec : DecidableEq α] {n : ℕ} {m : ℕ} (a : α) (hn : n < order_of a) (hm : m < order_of a) (eq : a ^ n = a ^ m) : n = m := or.elim (le_total n m) (fun (h : n ≤ m) => pow_injective_aux a h hn hm eq) fun (h : m ≤ n) => Eq.symm (pow_injective_aux a h hm hn (Eq.symm eq)) theorem order_of_le_card_univ {α : Type u_1} {a : α} [group α] [fintype α] [dec : DecidableEq α] : order_of a ≤ fintype.card α := finset.card_le_of_inj_on (pow a) (fun (n : ℕ) (_x : n < order_of a) => fintype.complete (a ^ n)) fun (i j : ℕ) => pow_injective_of_lt_order_of a theorem pow_eq_mod_order_of {α : Type u_1} {a : α} [group α] [fintype α] [dec : DecidableEq α] {n : ℕ} : a ^ n = a ^ (n % order_of a) := sorry theorem gpow_eq_mod_order_of {α : Type u_1} {a : α} [group α] [fintype α] [dec : DecidableEq α] {i : ℤ} : a ^ i = a ^ (i % ↑(order_of a)) := sorry theorem mem_gpowers_iff_mem_range_order_of {α : Type u_1} [group α] [fintype α] [dec : DecidableEq α] {a : α} {a' : α} : a' ∈ subgroup.gpowers a ↔ a' ∈ finset.image (pow a) (finset.range (order_of a)) := finset.mem_range_iff_mem_finset_range_of_mod_eq (order_of_pos a) fun (i : ℤ) => Eq.symm gpow_eq_mod_order_of protected instance decidable_gpowers {α : Type u_1} {a : α} [group α] [fintype α] [dec : DecidableEq α] : decidable_pred ↑(subgroup.gpowers a) := fun (a' : α) => decidable_of_iff' (a' ∈ finset.image (pow a) (finset.range (order_of a))) sorry theorem order_of_dvd_of_pow_eq_one {α : Type u_1} {a : α} [group α] [fintype α] [dec : DecidableEq α] {n : ℕ} (h : a ^ n = 1) : order_of a ∣ n := sorry theorem order_of_dvd_iff_pow_eq_one {α : Type u_1} {a : α} [group α] [fintype α] [dec : DecidableEq α] {n : ℕ} : order_of a ∣ n ↔ a ^ n = 1 := sorry theorem order_of_le_of_pow_eq_one {α : Type u_1} {a : α} [group α] [fintype α] [dec : DecidableEq α] {n : ℕ} (hn : 0 < n) (h : a ^ n = 1) : order_of a ≤ n := nat.find_min' (exists_pow_eq_one a) (Exists.intro hn h) theorem sum_card_order_of_eq_card_pow_eq_one {α : Type u_1} [group α] [fintype α] [dec : DecidableEq α] {n : ℕ} (hn : 0 < n) : (finset.sum (finset.filter (fun (_x : ℕ) => _x ∣ n) (finset.range (Nat.succ n))) fun (m : ℕ) => finset.card (finset.filter (fun (a : α) => order_of a = m) finset.univ)) = finset.card (finset.filter (fun (a : α) => a ^ n = 1) finset.univ) := sorry theorem order_eq_card_gpowers {α : Type u_1} {a : α} [group α] [fintype α] [dec : DecidableEq α] : order_of a = fintype.card ↥↑(subgroup.gpowers a) := sorry @[simp] theorem order_of_one {α : Type u_1} [group α] [fintype α] [dec : DecidableEq α] : order_of 1 = 1 := sorry @[simp] theorem order_of_eq_one_iff {α : Type u_1} {a : α} [group α] [fintype α] [dec : DecidableEq α] : order_of a = 1 ↔ a = 1 := sorry theorem order_of_eq_prime {α : Type u_1} {a : α} [group α] [fintype α] [dec : DecidableEq α] {p : ℕ} [hp : fact (nat.prime p)] (hg : a ^ p = 1) (hg1 : a ≠ 1) : order_of a = p := or.resolve_left (and.right hp (order_of a) (order_of_dvd_of_pow_eq_one hg)) (mt (iff.mp order_of_eq_one_iff) hg1) /- TODO: use cardinal theory, introduce `card : set α → ℕ`, or setup decidability for cosets -/ theorem order_of_dvd_card_univ {α : Type u_1} {a : α} [group α] [fintype α] [dec : DecidableEq α] : order_of a ∣ fintype.card α := sorry @[simp] theorem pow_card_eq_one {α : Type u_1} [group α] [fintype α] (a : α) : a ^ fintype.card α = 1 := sorry theorem mem_powers_iff_mem_gpowers {α : Type u_1} [group α] [fintype α] {a : α} {x : α} : x ∈ submonoid.powers a ↔ x ∈ subgroup.gpowers a := sorry theorem powers_eq_gpowers {α : Type u_1} [group α] [fintype α] (a : α) : ↑(submonoid.powers a) = ↑(subgroup.gpowers a) := set.ext fun (x : α) => mem_powers_iff_mem_gpowers theorem order_of_pow {α : Type u_1} [group α] [fintype α] [dec : DecidableEq α] (a : α) (n : ℕ) : order_of (a ^ n) = order_of a / nat.gcd (order_of a) n := sorry theorem image_range_order_of {α : Type u_1} [group α] [fintype α] [dec : DecidableEq α] (a : α) : finset.image (fun (i : ℕ) => a ^ i) (finset.range (order_of a)) = set.to_finset ↑(subgroup.gpowers a) := sorry theorem pow_gcd_card_eq_one_iff {α : Type u_1} [group α] [fintype α] {n : ℕ} {a : α} : a ^ n = 1 ↔ a ^ nat.gcd n (fintype.card α) = 1 := sorry /-- A group is called *cyclic* if it is generated by a single element. -/ class is_cyclic (α : Type u_2) [group α] where exists_generator : ∃ (g : α), ∀ (x : α), x ∈ subgroup.gpowers g /-- A cyclic group is always commutative. This is not an `instance` because often we have a better proof of `comm_group`. -/ def is_cyclic.comm_group {α : Type u_1} [hg : group α] [is_cyclic α] : comm_group α := comm_group.mk group.mul group.mul_assoc group.one group.one_mul group.mul_one group.inv group.div group.mul_left_inv sorry theorem is_cyclic_of_order_of_eq_card {α : Type u_1} [group α] [DecidableEq α] [fintype α] (x : α) (hx : order_of x = fintype.card α) : is_cyclic α := sorry theorem is_cyclic_of_prime_card {α : Type u_1} [group α] [fintype α] {p : ℕ} [hp : fact (nat.prime p)] (h : fintype.card α = p) : is_cyclic α := sorry theorem order_of_eq_card_of_forall_mem_gpowers {α : Type u_1} [group α] [DecidableEq α] [fintype α] {g : α} (hx : ∀ (x : α), x ∈ subgroup.gpowers g) : order_of g = fintype.card α := sorry protected instance bot.is_cyclic {α : Type u_1} [group α] : is_cyclic ↥⊥ := is_cyclic.mk (Exists.intro 1 fun (x : ↥⊥) => Exists.intro 0 (subtype.eq (Eq.symm (iff.mp subgroup.mem_bot (subtype.property x))))) protected instance subgroup.is_cyclic {α : Type u_1} [group α] [is_cyclic α] (H : subgroup α) : is_cyclic ↥H := sorry theorem is_cyclic.card_pow_eq_one_le {α : Type u_1} [group α] [DecidableEq α] [fintype α] [is_cyclic α] {n : ℕ} (hn0 : 0 < n) : finset.card (finset.filter (fun (a : α) => a ^ n = 1) finset.univ) ≤ n := sorry theorem is_cyclic.exists_monoid_generator (α : Type u_1) [group α] [fintype α] [is_cyclic α] : ∃ (x : α), ∀ (y : α), y ∈ submonoid.powers x := sorry theorem is_cyclic.image_range_order_of {α : Type u_1} {a : α} [group α] [DecidableEq α] [fintype α] (ha : ∀ (x : α), x ∈ subgroup.gpowers a) : finset.image (fun (i : ℕ) => a ^ i) (finset.range (order_of a)) = finset.univ := sorry theorem is_cyclic.image_range_card {α : Type u_1} {a : α} [group α] [DecidableEq α] [fintype α] (ha : ∀ (x : α), x ∈ subgroup.gpowers a) : finset.image (fun (i : ℕ) => a ^ i) (finset.range (fintype.card α)) = finset.univ := sorry theorem card_pow_eq_one_eq_order_of_aux {α : Type u_1} [group α] [DecidableEq α] [fintype α] (hn : ∀ (n : ℕ), 0 < n → finset.card (finset.filter (fun (a : α) => a ^ n = 1) finset.univ) ≤ n) (a : α) : finset.card (finset.filter (fun (b : α) => b ^ order_of a = 1) finset.univ) = order_of a := sorry theorem card_order_of_eq_totient_aux₂ {α : Type u_1} [group α] [DecidableEq α] [fintype α] (hn : ∀ (n : ℕ), 0 < n → finset.card (finset.filter (fun (a : α) => a ^ n = 1) finset.univ) ≤ n) {d : ℕ} (hd : d ∣ fintype.card α) : finset.card (finset.filter (fun (a : α) => order_of a = d) finset.univ) = nat.totient d := sorry theorem is_cyclic_of_card_pow_eq_one_le {α : Type u_1} [group α] [DecidableEq α] [fintype α] (hn : ∀ (n : ℕ), 0 < n → finset.card (finset.filter (fun (a : α) => a ^ n = 1) finset.univ) ≤ n) : is_cyclic α := sorry theorem is_cyclic.card_order_of_eq_totient {α : Type u_1} [group α] [is_cyclic α] [DecidableEq α] [fintype α] {d : ℕ} (hd : d ∣ fintype.card α) : finset.card (finset.filter (fun (a : α) => order_of a = d) finset.univ) = nat.totient d := card_order_of_eq_totient_aux₂ (fun (n : ℕ) => is_cyclic.card_pow_eq_one_le) hd end Mathlib
d1d60205de66bc582b0948b704b1e8687ccfba9e
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/category_theory/monoidal/functorial.lean
ce7ff0860b281fdc24befee092adef3a3e65ce60
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
3,804
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.monoidal.functor import category_theory.functorial /-! # Unbundled lax monoidal functors ## Design considerations The essential problem I've encountered that requires unbundled functors is having an existing (non-monoidal) functor `F : C ⥤ D` between monoidal categories, and wanting to assert that it has an extension to a lax monoidal functor. The two options seem to be 1. Construct a separate `F' : lax_monoidal_functor C D`, and assert `F'.to_functor ≅ F`. 2. Introduce unbundled functors and unbundled lax monoidal functors, and construct `lax_monoidal F.obj`, then construct `F' := lax_monoidal_functor.of F.obj`. Both have costs, but as for option 2. the cost is in library design, while in option 1. the cost is users having to carry around additional isomorphisms forever, I wanted to introduce unbundled functors. TODO: later, we may want to do this for strong monoidal functors as well, but the immediate application, for enriched categories, only requires this notion. -/ open category_theory universes v₁ v₂ v₃ u₁ u₂ u₃ open category_theory.category open category_theory.functor namespace category_theory open monoidal_category variables {C : Type u₁} [category.{v₁} C] [𝒞 : monoidal_category.{v₁} C] {D : Type u₂} [category.{v₂} D] [𝒟 : monoidal_category.{v₂} D] include 𝒞 𝒟 /-- An unbundled description of lax monoidal functors. -/ -- Perhaps in the future we'll redefine `lax_monoidal_functor` in terms of this, -- but that isn't the immediate plan. class lax_monoidal (F : C → D) [functorial.{v₁ v₂} F] := -- unit morphism (ε : 𝟙_ D ⟶ F (𝟙_ C)) -- tensorator (μ : Π X Y : C, (F X) ⊗ (F Y) ⟶ F (X ⊗ Y)) (μ_natural' : ∀ {X Y X' Y' : C} (f : X ⟶ Y) (g : X' ⟶ Y'), ((map F f) ⊗ (map F g)) ≫ μ Y Y' = μ X X' ≫ map F (f ⊗ g) . obviously) -- associativity of the tensorator (associativity' : ∀ (X Y Z : C), (μ X Y ⊗ 𝟙 (F Z)) ≫ μ (X ⊗ Y) Z ≫ map F (α_ X Y Z).hom = (α_ (F X) (F Y) (F Z)).hom ≫ (𝟙 (F X) ⊗ μ Y Z) ≫ μ X (Y ⊗ Z) . obviously) -- unitality (left_unitality' : ∀ X : C, (λ_ (F X)).hom = (ε ⊗ 𝟙 (F X)) ≫ μ (𝟙_ C) X ≫ map F (λ_ X).hom . obviously) (right_unitality' : ∀ X : C, (ρ_ (F X)).hom = (𝟙 (F X) ⊗ ε) ≫ μ X (𝟙_ C) ≫ map F (ρ_ X).hom . obviously) restate_axiom lax_monoidal.μ_natural' attribute [simp] lax_monoidal.μ_natural restate_axiom lax_monoidal.left_unitality' restate_axiom lax_monoidal.right_unitality' -- The unitality axioms cannot be used as simp lemmas because they require -- higher-order matching to figure out the `F` and `X` from `F X`. restate_axiom lax_monoidal.associativity' attribute [simp] lax_monoidal.associativity namespace lax_monoidal_functor /-- Construct a bundled `lax_monoidal_functor` from the object level function and `functorial` and `lax_monoidal` typeclasses. -/ def of (F : C → D) [I₁ : functorial.{v₁ v₂} F] [I₂ : lax_monoidal.{v₁ v₂} F] : lax_monoidal_functor.{v₁ v₂} C D := { obj := F, ..I₁, ..I₂ } end lax_monoidal_functor instance (F : lax_monoidal_functor.{v₁ v₂} C D) : lax_monoidal.{v₁ v₂} (F.obj) := { .. F } section omit 𝒟 instance lax_monoidal_id : lax_monoidal.{v₁ v₁} (id : C → C) := { ε := 𝟙 _, μ := λ X Y, 𝟙 _ } end -- TODO instances for composition, as required -- TODO `strong_monoidal`, as well as `lax_monoidal` -- (... but it seems for enriched categories I'll only need unbundled lax monoidal functors at first) end category_theory
0167ff383e001eb6e98eea500383221027ca6a0b
b00eb947a9c4141624aa8919e94ce6dcd249ed70
/stage0/src/Lean/Server/Utils.lean
6259c5e4ee7533bd3c8720d3006b43a5bb6c4558
[ "Apache-2.0" ]
permissive
gebner/lean4-old
a4129a041af2d4d12afb3a8d4deedabde727719b
ee51cdfaf63ee313c914d83264f91f414a0e3b6e
refs/heads/master
1,683,628,606,745
1,622,651,300,000
1,622,654,405,000
142,608,821
1
0
null
null
null
null
UTF-8
Lean
false
false
4,870
lean
/- Copyright (c) 2020 Wojciech Nawrocki. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wojciech Nawrocki, Marc Huisinga -/ import Lean.Data.Position import Lean.Data.Lsp import Init.System.FilePath namespace IO def throwServerError (err : String) : IO α := throw (userError err) namespace FS.Stream /-- Chains two streams by creating a new stream s.t. writing to it just writes to `a` but reading from it also duplicates the read output into `b`, c.f. `a | tee b` on Unix. NB: if `a` is written to but this stream is never read from, the output will *not* be duplicated. Use this if you only care about the data that was actually read. -/ def chainRight (a : Stream) (b : Stream) (flushEagerly : Bool := false) : Stream := { a with flush := a.flush *> b.flush read := fun sz => do let bs ← a.read sz b.write bs if flushEagerly then b.flush pure bs getLine := do let ln ← a.getLine b.putStr ln if flushEagerly then b.flush pure ln } /-- Like `tee a | b` on Unix. See `chainOut`. -/ def chainLeft (a : Stream) (b : Stream) (flushEagerly : Bool := false) : Stream := { b with flush := a.flush *> b.flush write := fun bs => do a.write bs if flushEagerly then a.flush b.write bs putStr := fun s => do a.putStr s if flushEagerly then a.flush b.putStr s } /-- Prefixes all written outputs with `pre`. -/ def withPrefix (a : Stream) (pre : String) : Stream := { a with write := fun bs => do a.putStr pre a.write bs putStr := fun s => a.putStr (pre ++ s) } end FS.Stream end IO namespace Lean.Server structure DocumentMeta where uri : Lsp.DocumentUri version : Nat text : FileMap deriving Inhabited def replaceLspRange (text : FileMap) (r : Lsp.Range) (newText : String) : FileMap := let start := text.lspPosToUtf8Pos r.start let «end» := text.lspPosToUtf8Pos r.«end» let pre := text.source.extract 0 start let post := text.source.extract «end» text.source.bsize (pre ++ newText ++ post).toFileMap open IO /-- Duplicates an I/O stream to a log file `fName` in LEAN_SERVER_LOG_DIR if that envvar is set. -/ def maybeTee (fName : String) (isOut : Bool) (h : FS.Stream) : IO FS.Stream := do match (← IO.getEnv "LEAN_SERVER_LOG_DIR") with | none => pure h | some logDir => let hTee ← FS.Handle.mk (System.mkFilePath [logDir, fName]) FS.Mode.write true let hTee := FS.Stream.ofHandle hTee pure $ if isOut then hTee.chainLeft h true else h.chainRight hTee true /-- Transform the given path to a file:// URI. -/ def toFileUri (fname : String) : Lsp.DocumentUri := let fname := System.FilePath.normalizePath fname let fname := if System.Platform.isWindows then fname.map fun c => if c == '\\' then '/' else c else fname -- TODO(WN): URL-encode special characters -- Three slashes denote localhost. "file:///" ++ fname.dropWhile (· == '/') open Lsp /-- Returns the document contents with all changes applied, together with the position of the change which lands earliest in the file. Panics if there are no changes. -/ def foldDocumentChanges (changes : @& Array Lsp.TextDocumentContentChangeEvent) (oldText : FileMap) : FileMap × String.Pos := if changes.isEmpty then panic! "Lean.Server.foldDocumentChanges: empty change array" else let accumulateChanges : FileMap × String.Pos → TextDocumentContentChangeEvent → FileMap × String.Pos := fun ⟨newDocText, minStartOff⟩ change => match change with | TextDocumentContentChangeEvent.rangeChange (range : Range) (newText : String) => let startOff := oldText.lspPosToUtf8Pos range.start let newDocText := replaceLspRange newDocText range newText let minStartOff := minStartOff.min startOff ⟨newDocText, minStartOff⟩ | TextDocumentContentChangeEvent.fullChange (newText : String) => ⟨newText.toFileMap, 0⟩ -- NOTE: We assume Lean files are below 16 EiB. changes.foldl accumulateChanges (oldText, 0xffffffff) def publishDiagnostics (m : DocumentMeta) (diagnostics : Array Lsp.Diagnostic) (hOut : FS.Stream) : IO Unit := hOut.writeLspNotification { method := "textDocument/publishDiagnostics" param := { uri := m.uri version? := m.version diagnostics := diagnostics : PublishDiagnosticsParams } } def publishMessages (m : DocumentMeta) (msgLog : MessageLog) (hOut : FS.Stream) : IO Unit := do let diagnostics ← msgLog.msgs.mapM (msgToDiagnostic m.text) publishDiagnostics m diagnostics.toArray hOut end Lean.Server namespace List universe u def takeWhile (p : α → Bool) : List α → List α | [] => [] | hd :: tl => if p hd then hd :: takeWhile p tl else [] end List
58e9a271d3f61475d8658c28c7796f27e5b928e3
cf39355caa609c0f33405126beee2739aa3cb77e
/library/init/meta/feature_search.lean
cddf5600e5990a1b3915c2ff3e8b55920f8b0c81
[ "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
3,776
lean
/- Copyright (c) 2019 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Gabriel Ebner -/ prelude import init.meta.tactic init.meta.derive init.meta.mk_dec_eq_instance init.meta.float namespace feature_search open tactic native structure feature_cfg := (ignore_tc := tt) (ignore_pi_domain := tt) (ignore_type_args := tt) (ignore_decidable := tt) (ignore_conns := tt) @[derive decidable_eq] meta inductive feature | const (n : name) | arg (p : name) (c : name) namespace feature protected meta def to_string : feature → string | (const n) := n.to_string | (arg p c) := p.to_string ++ "→" ++ c.to_string protected meta def repr : feature → string | (const n) := "(const `" ++ n.to_string ++ ")" | (arg p c) := "(arg `" ++ p.to_string ++ " `" ++ c.to_string ++ ")" meta instance : has_to_string feature := ⟨feature.to_string⟩ meta instance : has_repr feature := ⟨feature.repr⟩ meta instance : has_to_tactic_format feature := ⟨λ f, pure f.to_string⟩ meta instance : has_to_format feature := ⟨λ f, f.to_string⟩ end feature meta constant feature_vec : Type namespace feature_vec meta constant of_expr (env : environment) (e : expr) (cfg : feature_cfg := {}) : feature_vec meta constant of_exprs (env : environment) (es : list expr) (cfg : feature_cfg := {}) : list feature_vec protected meta constant union (a b : feature_vec) : feature_vec meta instance : has_union feature_vec := ⟨feature_vec.union⟩ protected meta constant isect (a b : feature_vec) : feature_vec meta instance : has_inter feature_vec := ⟨feature_vec.isect⟩ meta def of_proof (prf : expr) (cfg : feature_cfg := {}) : tactic feature_vec := do ty ← infer_type prf, env ← get_env, pure $ of_expr env ty cfg meta def of_thm (n : name) (cfg : feature_cfg := {}) : tactic feature_vec := do decl ← get_decl n, env ← get_env, pure $ of_expr env decl.type cfg protected meta constant to_list (fv : feature_vec) : list feature meta instance : has_to_string feature_vec := ⟨to_string ∘ feature_vec.to_list⟩ meta instance : has_repr feature_vec := ⟨repr ∘ feature_vec.to_list⟩ meta instance : has_to_tactic_format feature_vec := ⟨pp ∘ feature_vec.to_list⟩ meta instance : has_to_format feature_vec := ⟨to_fmt ∘ feature_vec.to_list⟩ end feature_vec meta constant feature_stats : Type namespace feature_stats meta constant of_feature_vecs (fvs : list feature_vec) : feature_stats meta constant idf (fs : feature_stats) (f : feature) : float meta constant norm (fs : feature_stats) (fv : feature_vec) : float meta constant dotp (fs : feature_stats) (fv1 fv2 : feature_vec) : float meta constant cosine_similarity (fs : feature_stats) (fv1 fv2 : feature_vec) : float meta constant features (fs : feature_stats) : list feature meta def features_with_idf (fs : feature_stats) : list (feature × float) := fs.features.map (λ f, (f, fs.idf f)) meta instance : has_to_string feature_stats := ⟨to_string ∘ feature_stats.features_with_idf⟩ meta instance : has_repr feature_stats := ⟨repr ∘ feature_stats.features_with_idf⟩ meta instance : has_to_tactic_format feature_stats := ⟨pp ∘ feature_stats.features_with_idf⟩ meta instance : has_to_format feature_stats := ⟨to_fmt ∘ feature_stats.features_with_idf⟩ end feature_stats meta constant predictor : Type meta constant predictor.predict (p : predictor) (goal : feature_vec) (max_results : ℕ) : list (name × float) @[derive decidable_eq] inductive predictor_type | knn | mepo | bayes structure predictor_cfg extends feature_cfg := (type := predictor_type.bayes) end feature_search open feature_search meta constant environment.mk_predictor (env : environment) (cfg : predictor_cfg := {}) : predictor
70a2f83e4f79181ab83762c0e704cbb689a7e30e
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/PrettyPrinter/Delaborator/Basic.lean
2e1ebeb6ada3a68f1ff2ca672c0925d0d75dd875
[ "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
13,012
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ import Lean.Elab.Term import Lean.PrettyPrinter.Delaborator.Options import Lean.PrettyPrinter.Delaborator.SubExpr import Lean.PrettyPrinter.Delaborator.TopDownAnalyze /-! The delaborator is the first stage of the pretty printer, and the inverse of the elaborator: it turns fully elaborated `Expr` core terms back into surface-level `Syntax`, omitting some implicit information again and using higher-level syntax abstractions like notations where possible. The exact behavior can be customized using pretty printer options; activating `pp.all` should guarantee that the delaborator is injective and that re-elaborating the resulting `Syntax` round-trips. Pretty printer options can be given not only for the whole term, but also specific subterms. This is used both when automatically refining pp options until round-trip and when interactively selecting pp options for a subterm (both TBD). The association of options to subterms is done by assigning a unique, synthetic Nat position to each subterm derived from its position in the full term. This position is added to the corresponding Syntax object so that elaboration errors and interactions with the pretty printer output can be traced back to the subterm. The delaborator is extensible via the `[delab]` attribute. -/ namespace Lean.PrettyPrinter.Delaborator open Lean.Meta Lean.SubExpr SubExpr open Lean.Elab (Info TermInfo Info.ofTermInfo) structure Context where optionsPerPos : OptionsPerPos currNamespace : Name openDecls : List OpenDecl inPattern : Bool := false -- true when delaborating `match` patterns subExpr : SubExpr structure State where /-- We attach `Elab.Info` at various locations in the `Syntax` output in order to convey its semantics. While the elaborator emits `InfoTree`s, here we have no real text location tree to traverse, so we use a flattened map. -/ infos : PosMap Info := {} /-- See `SubExpr.nextExtraPos`. -/ holeIter : SubExpr.HoleIterator := {} -- Exceptions from delaborators are not expected. We use an internal exception to signal whether -- the delaborator was able to produce a Syntax object. builtin_initialize delabFailureId : InternalExceptionId ← registerInternalExceptionId `delabFailure abbrev DelabM := ReaderT Context (StateRefT State MetaM) abbrev Delab := DelabM Term instance : Inhabited (DelabM α) where default := throw default @[inline] protected def orElse (d₁ : DelabM α) (d₂ : Unit → DelabM α) : DelabM α := do catchInternalId delabFailureId d₁ fun _ => d₂ () protected def failure : DelabM α := throw $ Exception.internal delabFailureId instance : Alternative DelabM where orElse := Delaborator.orElse failure := Delaborator.failure -- HACK: necessary since it would otherwise prefer the instance from MonadExcept instance {α} : OrElse (DelabM α) := ⟨Delaborator.orElse⟩ -- Low priority instances so `read`/`get`/etc default to the whole `Context`/`State` instance (priority := low) : MonadReaderOf SubExpr DelabM where read := Context.subExpr <$> read instance (priority := low) : MonadWithReaderOf SubExpr DelabM where withReader f x := fun ctx => x { ctx with subExpr := f ctx.subExpr } instance (priority := low) : MonadStateOf SubExpr.HoleIterator DelabM where get := State.holeIter <$> get set iter := modify fun ⟨infos, _⟩ => ⟨infos, iter⟩ modifyGet f := modifyGet fun ⟨infos, iter⟩ => let (ret, iter') := f iter; (ret, ⟨infos, iter'⟩) -- Macro scopes in the delaborator output are ultimately ignored by the pretty printer, -- so give a trivial implementation. instance : MonadQuotation DelabM := { getCurrMacroScope := pure default getMainModule := pure default withFreshMacroScope := fun x => x } unsafe def mkDelabAttribute : IO (KeyedDeclsAttribute Delab) := KeyedDeclsAttribute.init { builtinName := `builtin_delab, name := `delab, descr := "Register a delaborator. [delab k] registers a declaration of type `Lean.PrettyPrinter.Delaborator.Delab` for the `Lean.Expr` constructor `k`. Multiple delaborators for a single constructor are tried in turn until the first success. If the term to be delaborated is an application of a constant `c`, elaborators for `app.c` are tried first; this is also done for `Expr.const`s (\"nullary applications\") to reduce special casing. If the term is an `Expr.mdata` with a single key `k`, `mdata.k` is tried first.", valueTypeName := `Lean.PrettyPrinter.Delaborator.Delab evalKey := fun _ stx => do let stx ← Attribute.Builtin.getIdent stx let kind := stx.getId if (← Elab.getInfoState).enabled && kind.getRoot == `app then let c := kind.replacePrefix `app .anonymous if (← getEnv).contains c then Elab.addConstInfo stx c none pure kind } `Lean.PrettyPrinter.Delaborator.delabAttribute @[builtin_init mkDelabAttribute] opaque delabAttribute : KeyedDeclsAttribute Delab def getExprKind : DelabM Name := do let e ← getExpr pure $ match e with | Expr.bvar _ => `bvar | Expr.fvar _ => `fvar | Expr.mvar _ => `mvar | Expr.sort _ => `sort | Expr.const c _ => -- we identify constants as "nullary applications" to reduce special casing `app ++ c | Expr.app fn _ => match fn.getAppFn with | Expr.const c _ => `app ++ c | _ => `app | Expr.lam _ _ _ _ => `lam | Expr.forallE _ _ _ _ => `forallE | Expr.letE _ _ _ _ _ => `letE | Expr.lit _ => `lit | Expr.mdata m _ => match m.entries with | [(key, _)] => `mdata ++ key | _ => `mdata | Expr.proj _ _ _ => `proj def getOptionsAtCurrPos : DelabM Options := do let ctx ← read let mut opts ← getOptions if let some opts' := ctx.optionsPerPos.find? (← getPos) then for (k, v) in opts' do opts := opts.insert k v return opts /-- Evaluate option accessor, using subterm-specific options if set. -/ def getPPOption (opt : Options → Bool) : DelabM Bool := do return opt (← getOptionsAtCurrPos) def whenPPOption (opt : Options → Bool) (d : Delab) : Delab := do let b ← getPPOption opt if b then d else failure def whenNotPPOption (opt : Options → Bool) (d : Delab) : Delab := do let b ← getPPOption opt if b then failure else d /-- Set the given option at the current position and execute `x` in this context. -/ def withOptionAtCurrPos (k : Name) (v : DataValue) (x : DelabM α) : DelabM α := do let pos ← getPos withReader (fun ctx => let opts' := ctx.optionsPerPos.find? pos |>.getD {} |>.insert k v { ctx with optionsPerPos := ctx.optionsPerPos.insert pos opts' }) x def annotatePos (pos : Pos) (stx : Term) : Term := ⟨stx.raw.setInfo (SourceInfo.synthetic ⟨pos⟩ ⟨pos⟩)⟩ def annotateCurPos (stx : Term) : Delab := return annotatePos (← getPos) stx def getUnusedName (suggestion : Name) (body : Expr) : DelabM Name := do -- Use a nicer binder name than `[anonymous]`. We probably shouldn't do this in all LocalContext use cases, so do it here. let suggestion := if suggestion.isAnonymous then `a else suggestion -- We use this small hack to convert identifiers created using `mkAuxFunDiscr` to simple names let suggestion := suggestion.eraseMacroScopes let lctx ← getLCtx if !lctx.usesUserName suggestion then return suggestion else if (← getPPOption getPPSafeShadowing) && !bodyUsesSuggestion lctx suggestion then return suggestion else return lctx.getUnusedName suggestion where bodyUsesSuggestion (lctx : LocalContext) (suggestion' : Name) : Bool := Option.isSome <| body.find? fun | Expr.fvar fvarId => match lctx.find? fvarId with | none => false | some decl => decl.userName == suggestion' | _ => false def withBindingBodyUnusedName {α} (d : Syntax → DelabM α) : DelabM α := do let n ← getUnusedName (← getExpr).bindingName! (← getExpr).bindingBody! let stxN ← annotateCurPos (mkIdent n) withBindingBody n $ d stxN @[inline] def liftMetaM {α} (x : MetaM α) : DelabM α := liftM x def addTermInfo (pos : Pos) (stx : Syntax) (e : Expr) (isBinder : Bool := false) : DelabM Unit := do let info ← mkTermInfo stx e isBinder modify fun s => { s with infos := s.infos.insert pos info } where mkTermInfo stx e isBinder := return Info.ofTermInfo { elaborator := `Delab, stx := stx, lctx := (← getLCtx), expectedType? := none, expr := e, isBinder := isBinder } def addFieldInfo (pos : Pos) (projName fieldName : Name) (stx : Syntax) (val : Expr) : DelabM Unit := do let info ← mkFieldInfo projName fieldName stx val modify fun s => { s with infos := s.infos.insert pos info } where mkFieldInfo projName fieldName stx val := return Info.ofFieldInfo { projName := projName, fieldName := fieldName, lctx := (← getLCtx), val := val, stx := stx } def annotateTermInfo (stx : Term) : Delab := do let stx ← annotateCurPos stx addTermInfo (← getPos) stx (← getExpr) pure stx partial def delabFor : Name → Delab | Name.anonymous => failure | k => (do annotateTermInfo (← (delabAttribute.getValues (← getEnv) k).firstM id)) -- have `app.Option.some` fall back to `app` etc. <|> if k.isAtomic then failure else delabFor k.getRoot partial def delab : Delab := do checkMaxHeartbeats "delab" let e ← getExpr -- no need to hide atomic proofs if ← pure !e.isAtomic <&&> pure !(← getPPOption getPPProofs) <&&> (try Meta.isProof e catch _ => pure false) then if ← getPPOption getPPProofsWithType then let stx ← withType delab return ← annotateTermInfo (← `((_ : $stx))) else return ← annotateTermInfo (← ``(_)) let k ← getExprKind let stx ← delabFor k <|> (liftM $ show MetaM _ from throwError "don't know how to delaborate '{k}'") if ← getPPOption getPPAnalyzeTypeAscriptions <&&> getPPOption getPPAnalysisNeedsType <&&> pure !e.isMData then let typeStx ← withType delab `(($stx : $typeStx)) >>= annotateCurPos else return stx unsafe def mkAppUnexpanderAttribute : IO (KeyedDeclsAttribute Unexpander) := KeyedDeclsAttribute.init { name := `app_unexpander, descr := "Register an unexpander for applications of a given constant. [app_unexpander c] registers a `Lean.PrettyPrinter.Unexpander` for applications of the constant `c`. The unexpander is passed the result of pre-pretty printing the application *without* implicitly passed arguments. If `pp.explicit` is set to true or `pp.notation` is set to false, it will not be called at all.", valueTypeName := `Lean.PrettyPrinter.Unexpander evalKey := fun _ stx => do Elab.resolveGlobalConstNoOverloadWithInfo (← Attribute.Builtin.getIdent stx) } `Lean.PrettyPrinter.Delaborator.appUnexpanderAttribute @[builtin_init mkAppUnexpanderAttribute] opaque appUnexpanderAttribute : KeyedDeclsAttribute Unexpander end Delaborator open SubExpr (Pos PosMap) open Delaborator (OptionsPerPos topDownAnalyze) def delabCore (e : Expr) (optionsPerPos : OptionsPerPos := {}) (delab := Delaborator.delab) : MetaM (Term × PosMap Elab.Info) := do /- Using `erasePatternAnnotations` here is a bit hackish, but we do it `Expr.mdata` affects the delaborator. TODO: should we fix that? -/ let e ← Meta.erasePatternRefAnnotations e trace[PrettyPrinter.delab.input] "{Std.format e}" let mut opts ← getOptions -- default `pp.proofs` to `true` if `e` is a proof if pp.proofs.get? opts == none && -- necessary for `delabConstWithSignature`, and harmless otherwise !e.isConst then try if ← Meta.isProof e then opts := pp.proofs.set opts true catch _ => pure () withOptions (fun _ => opts) do let e ← if getPPInstantiateMVars opts then instantiateMVars e else pure e let optionsPerPos ← if !getPPAll opts && getPPAnalyze opts && optionsPerPos.isEmpty then topDownAnalyze e else pure optionsPerPos let (stx, {infos := infos, ..}) ← catchInternalId Delaborator.delabFailureId (delab { optionsPerPos := optionsPerPos currNamespace := (← getCurrNamespace) openDecls := (← getOpenDecls) subExpr := SubExpr.mkRoot e inPattern := opts.getInPattern } |>.run { : Delaborator.State }) (fun _ => unreachable!) return (stx, infos) /-- "Delaborate" the given term into surface-level syntax using the default and given subterm-specific options. -/ def delab (e : Expr) (optionsPerPos : OptionsPerPos := {}) : MetaM Term := do let (stx, _) ← delabCore e optionsPerPos return stx builtin_initialize registerTraceClass `PrettyPrinter.delab end Lean.PrettyPrinter
edaf23d3f5f62222e150c49990d307c6263d2f86
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/run/empty_set_inside_quotations.lean
22c935037ac1aa09b9ddfc24c92e1a9571035114
[ "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
404
lean
constant union_is_assoc {α} : is_associative (set α) (∪) attribute [instance] union_is_assoc #check ({} : set nat) open tactic expr meta def is_assoc_bin_app : expr → tactic (expr × expr) | (app (app op a1) a2) := do h ← to_expr ``(is_associative.assoc %%op), return (op, h) | _ := failed run_cmd to_expr ``(({} : set nat) ∪ {}) >>= is_assoc_bin_app >>= λ p, trace p.2
bdd91db13901c68a3c8f17ede767ed1715b7e2f9
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/ppExpr.lean
6385fb6d658f7e42b2ece1fa7900772a3897ef08
[ "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
638
lean
import Lean /-! Pretty printing tests for `Expr`s that cannot be generated by parsing+elaborating. -/ open Lean def test (e : Expr) : MetaM Unit := do IO.println (← PrettyPrinter.ppExpr e) -- loose bound variable #eval test (mkBVar 0) -- anonymous binder #eval test (mkLambda Name.anonymous BinderInfo.default (mkSort levelZero) (mkBVar 0)) -- pp annotations #eval test $ mkAppN (mkConst `id [levelOne]) #[ mkConst `Nat, mkMData (KVMap.empty.set `pp.explicit true) $ mkAppN (mkConst `id [levelOne]) #[ mkConst `Nat, mkAppN (mkConst `id [levelOne]) #[ mkConst `Nat, mkConst `Nat.zero ]]]
de296d2d16777049caf3c17960ee248144bf6d04
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/stage0/src/Init/Data/UInt.lean
0cecf19817c51eb94eea30dd1180d14a947c57c4
[ "Apache-2.0" ]
permissive
banksonian/lean4
3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc
78da6b3aa2840693eea354a41e89fc5b212a5011
refs/heads/master
1,673,703,624,165
1,605,123,551,000
1,605,123,551,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,952
lean
/- Copyright (c) 2018 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.Fin.Basic import Init.System.Platform open Nat @[extern "lean_uint8_of_nat"] def UInt8.ofNat (n : @& Nat) : UInt8 := ⟨Fin.ofNat n⟩ abbrev Nat.toUInt8 := UInt8.ofNat @[extern "lean_uint8_to_nat"] def UInt8.toNat (n : UInt8) : Nat := n.val.val @[extern c inline "#1 + #2"] def UInt8.add (a b : UInt8) : UInt8 := ⟨a.val + b.val⟩ @[extern c inline "#1 - #2"] def UInt8.sub (a b : UInt8) : UInt8 := ⟨a.val - b.val⟩ @[extern c inline "#1 * #2"] def UInt8.mul (a b : UInt8) : UInt8 := ⟨a.val * b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 / #2"] def UInt8.div (a b : UInt8) : UInt8 := ⟨a.val / b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 % #2"] def UInt8.mod (a b : UInt8) : UInt8 := ⟨a.val % b.val⟩ @[extern "lean_uint8_modn"] def UInt8.modn (a : UInt8) (n : @& Nat) : UInt8 := ⟨a.val %ₙ n⟩ @[extern c inline "#1 & #2"] def UInt8.land (a b : UInt8) : UInt8 := ⟨Fin.land a.val b.val⟩ @[extern c inline "#1 | #2"] def UInt8.lor (a b : UInt8) : UInt8 := ⟨Fin.lor a.val b.val⟩ def UInt8.lt (a b : UInt8) : Prop := a.val < b.val def UInt8.le (a b : UInt8) : Prop := a.val ≤ b.val instance : OfNat UInt8 := ⟨UInt8.ofNat⟩ instance : Add UInt8 := ⟨UInt8.add⟩ instance : Sub UInt8 := ⟨UInt8.sub⟩ instance : Mul UInt8 := ⟨UInt8.mul⟩ instance : Mod UInt8 := ⟨UInt8.mod⟩ instance : ModN UInt8 := ⟨UInt8.modn⟩ instance : Div UInt8 := ⟨UInt8.div⟩ instance : HasLess UInt8 := ⟨UInt8.lt⟩ instance : HasLessEq UInt8 := ⟨UInt8.le⟩ set_option bootstrap.gen_matcher_code false in @[extern c inline "#1 < #2"] def UInt8.decLt (a b : UInt8) : Decidable (a < b) := match a, b with | ⟨n⟩, ⟨m⟩ => inferInstanceAs (Decidable (n < m)) set_option bootstrap.gen_matcher_code false in @[extern c inline "#1 <= #2"] def UInt8.decLe (a b : UInt8) : Decidable (a ≤ b) := match a, b with | ⟨n⟩, ⟨m⟩ => inferInstanceAs (Decidable (n <= m)) instance (a b : UInt8) : Decidable (a < b) := UInt8.decLt a b instance (a b : UInt8) : Decidable (a ≤ b) := UInt8.decLe a b @[extern "lean_uint16_of_nat"] def UInt16.ofNat (n : @& Nat) : UInt16 := ⟨Fin.ofNat n⟩ abbrev Nat.toUInt16 := UInt16.ofNat @[extern "lean_uint16_to_nat"] def UInt16.toNat (n : UInt16) : Nat := n.val.val @[extern c inline "#1 + #2"] def UInt16.add (a b : UInt16) : UInt16 := ⟨a.val + b.val⟩ @[extern c inline "#1 - #2"] def UInt16.sub (a b : UInt16) : UInt16 := ⟨a.val - b.val⟩ @[extern c inline "#1 * #2"] def UInt16.mul (a b : UInt16) : UInt16 := ⟨a.val * b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 / #2"] def UInt16.div (a b : UInt16) : UInt16 := ⟨a.val / b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 % #2"] def UInt16.mod (a b : UInt16) : UInt16 := ⟨a.val % b.val⟩ @[extern "lean_uint16_modn"] def UInt16.modn (a : UInt16) (n : @& Nat) : UInt16 := ⟨a.val %ₙ n⟩ @[extern c inline "#1 & #2"] def UInt16.land (a b : UInt16) : UInt16 := ⟨Fin.land a.val b.val⟩ @[extern c inline "#1 | #2"] def UInt16.lor (a b : UInt16) : UInt16 := ⟨Fin.lor a.val b.val⟩ def UInt16.lt (a b : UInt16) : Prop := a.val < b.val def UInt16.le (a b : UInt16) : Prop := a.val ≤ b.val instance : OfNat UInt16 := ⟨UInt16.ofNat⟩ instance : Add UInt16 := ⟨UInt16.add⟩ instance : Sub UInt16 := ⟨UInt16.sub⟩ instance : Mul UInt16 := ⟨UInt16.mul⟩ instance : Mod UInt16 := ⟨UInt16.mod⟩ instance : ModN UInt16 := ⟨UInt16.modn⟩ instance : Div UInt16 := ⟨UInt16.div⟩ instance : HasLess UInt16 := ⟨UInt16.lt⟩ instance : HasLessEq UInt16 := ⟨UInt16.le⟩ set_option bootstrap.gen_matcher_code false in @[extern c inline "#1 < #2"] def UInt16.decLt (a b : UInt16) : Decidable (a < b) := match a, b with | ⟨n⟩, ⟨m⟩ => inferInstanceAs (Decidable (n < m)) set_option bootstrap.gen_matcher_code false in @[extern c inline "#1 <= #2"] def UInt16.decLe (a b : UInt16) : Decidable (a ≤ b) := match a, b with | ⟨n⟩, ⟨m⟩ => inferInstanceAs (Decidable (n <= m)) instance (a b : UInt16) : Decidable (a < b) := UInt16.decLt a b instance (a b : UInt16) : Decidable (a ≤ b) := UInt16.decLe a b @[extern "lean_uint32_of_nat"] def UInt32.ofNat (n : @& Nat) : UInt32 := ⟨Fin.ofNat n⟩ @[extern "lean_uint32_of_nat"] def UInt32.ofNat' (n : Nat) (h : n < uint32Sz) : UInt32 := ⟨⟨n, h⟩⟩ abbrev Nat.toUInt32 := UInt32.ofNat @[extern c inline "#1 + #2"] def UInt32.add (a b : UInt32) : UInt32 := ⟨a.val + b.val⟩ @[extern c inline "#1 - #2"] def UInt32.sub (a b : UInt32) : UInt32 := ⟨a.val - b.val⟩ @[extern c inline "#1 * #2"] def UInt32.mul (a b : UInt32) : UInt32 := ⟨a.val * b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 / #2"] def UInt32.div (a b : UInt32) : UInt32 := ⟨a.val / b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 % #2"] def UInt32.mod (a b : UInt32) : UInt32 := ⟨a.val % b.val⟩ @[extern "lean_uint32_modn"] def UInt32.modn (a : UInt32) (n : @& Nat) : UInt32 := ⟨a.val %ₙ n⟩ @[extern c inline "#1 & #2"] def UInt32.land (a b : UInt32) : UInt32 := ⟨Fin.land a.val b.val⟩ @[extern c inline "#1 | #2"] def UInt32.lor (a b : UInt32) : UInt32 := ⟨Fin.lor a.val b.val⟩ @[extern c inline "((uint8_t)#1)"] def UInt32.toUInt8 (a : UInt32) : UInt8 := a.toNat.toUInt8 @[extern c inline "((uint16_t)#1)"] def UInt32.toUInt16 (a : UInt32) : UInt16 := a.toNat.toUInt16 @[extern c inline "((uint32_t)#1)"] def UInt8.toUInt32 (a : UInt8) : UInt32 := a.toNat.toUInt32 instance : OfNat UInt32 := ⟨UInt32.ofNat⟩ instance : Add UInt32 := ⟨UInt32.add⟩ instance : Sub UInt32 := ⟨UInt32.sub⟩ instance : Mul UInt32 := ⟨UInt32.mul⟩ instance : Mod UInt32 := ⟨UInt32.mod⟩ instance : ModN UInt32 := ⟨UInt32.modn⟩ instance : Div UInt32 := ⟨UInt32.div⟩ @[extern c inline "#1 << #2"] constant UInt32.shiftLeft (a b : UInt32) : UInt32 := (arbitrary Nat).toUInt32 @[extern c inline "#1 >> #2"] constant UInt32.shiftRight (a b : UInt32) : UInt32 := (arbitrary Nat).toUInt32 @[extern "lean_uint64_of_nat"] def UInt64.ofNat (n : @& Nat) : UInt64 := ⟨Fin.ofNat n⟩ abbrev Nat.toUInt64 := UInt64.ofNat @[extern "lean_uint64_to_nat"] def UInt64.toNat (n : UInt64) : Nat := n.val.val @[extern c inline "#1 + #2"] def UInt64.add (a b : UInt64) : UInt64 := ⟨a.val + b.val⟩ @[extern c inline "#1 - #2"] def UInt64.sub (a b : UInt64) : UInt64 := ⟨a.val - b.val⟩ @[extern c inline "#1 * #2"] def UInt64.mul (a b : UInt64) : UInt64 := ⟨a.val * b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 / #2"] def UInt64.div (a b : UInt64) : UInt64 := ⟨a.val / b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 % #2"] def UInt64.mod (a b : UInt64) : UInt64 := ⟨a.val % b.val⟩ @[extern "lean_uint64_modn"] def UInt64.modn (a : UInt64) (n : @& Nat) : UInt64 := ⟨a.val %ₙ n⟩ @[extern c inline "#1 & #2"] def UInt64.land (a b : UInt64) : UInt64 := ⟨Fin.land a.val b.val⟩ @[extern c inline "#1 | #2"] def UInt64.lor (a b : UInt64) : UInt64 := ⟨Fin.lor a.val b.val⟩ def UInt64.lt (a b : UInt64) : Prop := a.val < b.val def UInt64.le (a b : UInt64) : Prop := a.val ≤ b.val @[extern c inline "((uint8_t)#1)"] def UInt64.toUInt8 (a : UInt64) : UInt8 := a.toNat.toUInt8 @[extern c inline "((uint16_t)#1)"] def UInt64.toUInt16 (a : UInt64) : UInt16 := a.toNat.toUInt16 @[extern c inline "((uint32_t)#1)"] def UInt64.toUInt32 (a : UInt64) : UInt32 := a.toNat.toUInt32 @[extern c inline "((uint64_t)#1)"] def UInt32.toUInt64 (a : UInt32) : UInt64 := a.toNat.toUInt64 -- TODO(Leo): give reference implementation for shiftLeft and shiftRight, and define them for other UInt types @[extern c inline "#1 << #2"] constant UInt64.shiftLeft (a b : UInt64) : UInt64 := (arbitrary Nat).toUInt64 @[extern c inline "#1 >> #2"] constant UInt64.shiftRight (a b : UInt64) : UInt64 := (arbitrary Nat).toUInt64 instance : OfNat UInt64 := ⟨UInt64.ofNat⟩ instance : Add UInt64 := ⟨UInt64.add⟩ instance : Sub UInt64 := ⟨UInt64.sub⟩ instance : Mul UInt64 := ⟨UInt64.mul⟩ instance : Mod UInt64 := ⟨UInt64.mod⟩ instance : ModN UInt64 := ⟨UInt64.modn⟩ instance : Div UInt64 := ⟨UInt64.div⟩ instance : HasLess UInt64 := ⟨UInt64.lt⟩ instance : HasLessEq UInt64 := ⟨UInt64.le⟩ @[extern c inline "(uint64_t)#1"] def Bool.toUInt64 (b : Bool) : UInt64 := if b then 1 else 0 set_option bootstrap.gen_matcher_code false in @[extern c inline "#1 < #2"] def UInt64.decLt (a b : UInt64) : Decidable (a < b) := match a, b with | ⟨n⟩, ⟨m⟩ => inferInstanceAs (Decidable (n < m)) set_option bootstrap.gen_matcher_code false in @[extern c inline "#1 <= #2"] def UInt64.decLe (a b : UInt64) : Decidable (a ≤ b) := match a, b with | ⟨n⟩, ⟨m⟩ => inferInstanceAs (Decidable (n <= m)) instance (a b : UInt64) : Decidable (a < b) := UInt64.decLt a b instance (a b : UInt64) : Decidable (a ≤ b) := UInt64.decLe a b theorem usizeSzGt0 : usizeSz > 0 := Nat.posPowOfPos System.Platform.numBits (Nat.zeroLtSucc _) @[extern "lean_usize_of_nat"] def USize.ofNat (n : @& Nat) : USize := ⟨Fin.ofNat' n usizeSzGt0⟩ abbrev Nat.toUSize := USize.ofNat @[extern "lean_usize_to_nat"] def USize.toNat (n : USize) : Nat := n.val.val @[extern c inline "#1 + #2"] def USize.add (a b : USize) : USize := ⟨a.val + b.val⟩ @[extern c inline "#1 - #2"] def USize.sub (a b : USize) : USize := ⟨a.val - b.val⟩ @[extern c inline "#1 * #2"] def USize.mul (a b : USize) : USize := ⟨a.val * b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 / #2"] def USize.div (a b : USize) : USize := ⟨a.val / b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 % #2"] def USize.mod (a b : USize) : USize := ⟨a.val % b.val⟩ @[extern "lean_usize_modn"] def USize.modn (a : USize) (n : @& Nat) : USize := ⟨a.val %ₙ n⟩ @[extern c inline "#1 & #2"] def USize.land (a b : USize) : USize := ⟨Fin.land a.val b.val⟩ @[extern c inline "#1 | #2"] def USize.lor (a b : USize) : USize := ⟨Fin.lor a.val b.val⟩ @[extern c inline "#1"] def UInt32.toUSize (a : UInt32) : USize := a.toNat.toUSize @[extern c inline "((size_t)#1)"] def UInt64.toUSize (a : UInt64) : USize := a.toNat.toUSize @[extern c inline "(uint32_t)#1"] def USize.toUInt32 (a : USize) : UInt32 := a.toNat.toUInt32 -- TODO(Leo): give reference implementation for shiftLeft and shiftRight, and define them for other UInt types @[extern c inline "#1 << #2"] constant USize.shiftLeft (a b : USize) : USize := (arbitrary Nat).toUSize @[extern c inline "#1 >> #2"] constant USize.shiftRight (a b : USize) : USize := (arbitrary Nat).toUSize def USize.lt (a b : USize) : Prop := a.val < b.val def USize.le (a b : USize) : Prop := a.val ≤ b.val instance : OfNat USize := ⟨USize.ofNat⟩ instance : Add USize := ⟨USize.add⟩ instance : Sub USize := ⟨USize.sub⟩ instance : Mul USize := ⟨USize.mul⟩ instance : Mod USize := ⟨USize.mod⟩ instance : ModN USize := ⟨USize.modn⟩ instance : Div USize := ⟨USize.div⟩ instance : HasLess USize := ⟨USize.lt⟩ instance : HasLessEq USize := ⟨USize.le⟩ set_option bootstrap.gen_matcher_code false in @[extern c inline "#1 < #2"] def USize.decLt (a b : USize) : Decidable (a < b) := match a, b with | ⟨n⟩, ⟨m⟩ => inferInstanceAs (Decidable (n < m)) set_option bootstrap.gen_matcher_code false in @[extern c inline "#1 <= #2"] def USize.decLe (a b : USize) : Decidable (a ≤ b) := match a, b with | ⟨n⟩, ⟨m⟩ => inferInstanceAs (Decidable (n <= m)) instance (a b : USize) : Decidable (a < b) := USize.decLt a b instance (a b : USize) : Decidable (a ≤ b) := USize.decLe a b theorem USize.modnLt {m : Nat} : ∀ (u : USize), m > 0 → USize.toNat (u %ₙ m) < m | ⟨u⟩, h => Fin.modnLt u h
a47c0f85757af2ba6130005f6f1c520ec83c097c
4919181312c130f03eed71db8c80aa1cbd140256
/src/fol.lean
54baa53aff09be7a8ffff995e6c4e2ba6bca4e7f
[]
no_license
williamdemeo/flypitch
62143800b790f0a0ffcd338c82b6bf3cd401552b
f73127c3ad36e6c2f074a26518dc333f07c1ab85
refs/heads/master
1,587,042,220,139
1,547,238,998,000
1,547,238,998,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
121,741
lean
/- A development of first-order logic in Lean. * The object theory uses classical logic * We use de Bruijn variables. * We use a deep embedding of the logic, i.e. the type of terms and formulas is inductively defined. * There is no well-formedness predicate; all elements of type "term" are well-formed. -/ import .to_mathlib open nat set universe variables u v local notation h :: t := dvector.cons h t local notation `[` l:(foldr `, ` (h t, dvector.cons h t) dvector.nil `]`:0) := l namespace fol /- realizers of variables are just maps ℕ → S. We need some operations on them -/ /-- Given a valuation v, a nat n, and an x : S, return v truncated to its first n values, with the rest of the values replaced by x. --/ def subst_realize {S : Type u} (v : ℕ → S) (x : S) (n k : ℕ) : S := if k < n then v k else if n < k then v (k - 1) else x notation v `[`:95 x ` // `:95 n `]`:0 := fol.subst_realize v x n /-- --/ @[simp] lemma subst_realize_lt {S : Type u} (v : ℕ → S) (x : S) {n k : ℕ} (H : k < n) : v[x // n] k = v k := by simp only [H, subst_realize, if_true, eq_self_iff_true] @[simp] lemma subst_realize_gt {S : Type u} (v : ℕ → S) (x : S) {n k : ℕ} (H : n < k) : v[x // n] k = v (k-1) := have h : ¬(k < n), from lt_asymm H, by simp only [*, subst_realize, if_true, eq_self_iff_true, if_false] @[simp] lemma subst_realize_var_eq {S : Type u} (v : ℕ → S) (x : S) (n : ℕ) : v[x // n] n = x := by simp only [subst_realize, lt_irrefl, eq_self_iff_true, if_false] lemma subst_realize_congr {S : Type u} {v v' : ℕ → S} (hv : ∀k, v k = v' k) (x : S) (n k : ℕ) : v [x // n] k = v' [x // n] k := by apply lt_by_cases k n; intro h; simp only [*, subst_realize_lt, subst_realize_gt, subst_realize_var_eq, eq_self_iff_true] lemma subst_realize2 {S : Type u} (v : ℕ → S) (x x' : S) (n₁ n₂ k : ℕ) : v [x' // n₁ + n₂] [x // n₁] k = v [x // n₁] [x' // n₁ + n₂ + 1] k := begin apply lt_by_cases k n₁; intro h, { have : k < n₁ + n₂, from lt_of_le_of_lt (k.le_add_right n₂) (add_lt_add_right h n₂), have : k < n₁ + n₂ + 1, from lt.step this, simp only [*, fol.subst_realize_lt, eq_self_iff_true] }, { have : k < n₂ + (k + 1), from nat.lt_add_left _ _ n₂ (lt.base k), subst h, simp [*, -add_comm] }, apply lt_by_cases k (n₁ + n₂ + 1); intro h', { have : k - 1 < n₁ + n₂, from (nat.sub_lt_right_iff_lt_add (one_le_of_lt h)).2 h', simp [*, -add_comm, -add_assoc] }, { subst h', simp [h, -add_comm, -add_assoc] }, { have : n₁ + n₂ < k - 1, from nat.lt_sub_right_of_add_lt h', have : n₁ < k - 1, from lt_of_le_of_lt (n₁.le_add_right n₂) this, simp only [*, fol.subst_realize_gt, eq_self_iff_true] } end lemma subst_realize2_0 {S : Type u} (v : ℕ → S) (x x' : S) (n k : ℕ) : v [x' // n] [x // 0] k = v [x // 0] [x' // n + 1] k := let h := subst_realize2 v x x' 0 n k in by simp only [zero_add] at h; exact h lemma subst_realize_irrel {S : Type u} {v₁ v₂ : ℕ → S} {n : ℕ} (hv : ∀k < n, v₁ k = v₂ k) (x : S) {k : ℕ} (hk : k < n + 1) : v₁[x // 0] k = v₂[x // 0] k := begin cases k, refl, have h : 0 < succ k, from zero_lt_succ k, simp [h, hv k (lt_of_succ_lt_succ hk)] end lemma lift_subst_realize_cancel {S : Type u} (v : ℕ → S) (k : ℕ) : (λn, v (n + 1))[v 0 // 0] k = v k := begin cases k, refl, have h : 0 < succ k, from zero_lt_succ k, simp [h], end lemma subst_fin_realize_eq {S : Type u} {n} {v₁ : dvector S n} {v₂ : ℕ → S} (hv : ∀k (hk : k < n), v₁.nth k hk = v₂ k) (x : S) (k : ℕ) (hk : k < n+1) : (x::v₁).nth k hk = v₂[x // 0] k := begin cases k, refl, have h : 0 < succ k, from zero_lt_succ k, have h' : (0 : fin (n+1)).val < (fin.mk (succ k) hk).val, from h, rw [subst_realize_gt v₂ x h, dvector.nth], apply hv end structure Language : Type (u+1) := (functions : ℕ → Type u) (relations : ℕ → Type u) def Language.constants (L : Language) := L.functions 0 variable (L : Language.{u}) /- preterm L l is a partially applied term. if applied to n terms, it becomes a term. * Every element of preterm L 0 is a well-formed term. * We use this encoding to avoid mutual or nested inductive types, since those are not too convenient to work with in Lean. -/ inductive preterm : ℕ → Type u | var {} : ∀ (k : ℕ), preterm 0 | func : ∀ {l : ℕ} (f : L.functions l), preterm l | app : ∀ {l : ℕ} (t : preterm (l + 1)) (s : preterm 0), preterm l export preterm @[reducible] def term := preterm L 0 variable {L} prefix `&`:max := fol.preterm.var @[simp] def apps : ∀{l}, preterm L l → dvector (term L) l → term L | _ t [] := t | _ t (t'::ts) := apps (app t t') ts -- @[simp] def apps' : ∀{l l'}, preterm L (l'+l) → dvector (term L) l → preterm L l' -- | _ _ t [] := t -- | _ _ t (t'::ts) := apps' (app t t') ts -- @[simp] def rev_apps : ∀{l l'}, preterm L (l+l) → dvector (term L) l' → preterm L l -- | _ _ t [] := sorry -- | l _ t (@dvector.cons _ l' t' ts) := app (@rev_apps (l+1) l' t ts) t' @[simp] lemma apps_zero (t : term L) (ts : dvector (term L) 0) : apps t ts = t := by cases ts; refl lemma apps_eq_app {l} (t : preterm L (l+1)) (s : term L) (ts : dvector (term L) l) : ∃t' s', apps t (s::ts) = app t' s' := begin induction ts generalizing s, exact ⟨t, s, rfl⟩, exact ts_ih (app t s) ts_x end namespace preterm @[simp] def change_arity' : ∀{l l'} (h : l = l') (t : preterm L l), preterm L l' | _ _ h &k := by induction h; exact &k | _ _ h (func f) := func (by induction h; exact f) | _ _ h (app t₁ t₂) := app (change_arity' (congr_arg succ h) t₁) t₂ @[simp] lemma change_arity'_rfl : ∀{l} (t : preterm L l), change_arity' rfl t = t | _ &k := by refl | _ (func f) := by refl | _ (app t₁ t₂) := by dsimp; simp* end preterm -- lemma apps'_concat {l l'} (t : preterm L (l'+(l+1))) (s : term L) (ts : dvector (term L) l) : -- apps' t (ts.concat s) = app (apps' (t.change_arity' (by simp)) ts) s := -- begin -- induction ts generalizing s, -- { simp }, -- { apply ts_ih (app t ts_x) s } -- end lemma apps_ne_var {l} {f : L.functions l} {ts : dvector (term L) l} {k : ℕ} : apps (func f) ts ≠ &k := begin intro h, cases ts, injection h, rcases apps_eq_app (func f) ts_x ts_xs with ⟨t, s, h'⟩, cases h.symm.trans h' end lemma apps_inj' {l} {t t' : preterm L l} {ts ts' : dvector (term L) l} (h : apps t ts = apps t' ts') : t = t' ∧ ts = ts' := begin induction ts; cases ts', { exact ⟨h, rfl⟩ }, { rcases ts_ih h with ⟨⟨rfl, rfl⟩, rfl⟩, exact ⟨rfl, rfl⟩ } end -- lemma apps_inj_length {l l'} {f : L.functions l} {f' : L.functions l'} -- {ts : dvector (term L) l} {ts' : dvector (term L) l'} -- (h : apps (func f) ts = apps (func f') ts') : l = l' := -- begin -- sorry -- end -- lemma apps'_inj_length {l₁ l₂ l'} {f : L.functions (l' + l₁)} {f' : L.functions (l' + l₂)} -- {ts : dvector (term L) l₁} {ts' : dvector (term L) l₂} -- (h : apps' (func f) ts = apps' (func f') ts') : l₁ = l₂ := -- begin -- sorry -- -- induction ts generalizing l'; cases ts', -- -- { refl }, -- -- { rcases apps'_eq_app (func f') ts'_x ts'_xs with ⟨t, s, h'⟩, cases h.trans h' }, -- -- { rcases apps'_eq_app (func f) ts_x ts_xs with ⟨t, s, h'⟩, cases h.symm.trans h' }, -- -- { rcases apps'_eq_app (func f) ts_x ts_xs with ⟨t₁, s₁, h₁⟩, -- -- rcases apps'_eq_app (func f') ts'_x ts'_xs with ⟨t₂, s₂, h₂⟩, -- -- } -- end lemma apps_inj {l} {f f' : L.functions l} {ts ts' : dvector (term L) l} (h : apps (func f) ts = apps (func f') ts') : f = f' ∧ ts = ts' := by rcases apps_inj' h with ⟨h', rfl⟩; cases h'; exact ⟨rfl, rfl⟩ def term_of_function {l} (f : L.functions l) : arity' (term L) (term L) l := arity'.of_dvector_map $ apps (func f) @[elab_as_eliminator] def term.rec {C : term L → Sort v} (hvar : ∀(k : ℕ), C &k) (hfunc : Π {l} (f : L.functions l) (ts : dvector (term L) l) (ih_ts : ∀t, ts.pmem t → C t), C (apps (func f) ts)) : ∀(t : term L), C t := have h : ∀{l} (t : preterm L l) (ts : dvector (term L) l) (ih_ts : ∀s, ts.pmem s → C s), C (apps t ts), begin intros, induction t; try {rw ts.zero_eq}, { apply hvar }, { apply hfunc t_f ts ih_ts }, { apply t_ih_t (t_s::ts), intros t ht, cases ht, { induction ht, apply t_ih_s ([]), intros s hs, cases hs }, { exact ih_ts t ht }}, end, λt, h t ([]) (by intros s hs; cases hs) @[elab_as_eliminator] def term.elim' {C : Type v} (hvar : ∀(k : ℕ), C) (hfunc : Π {{l}} (f : L.functions l) (ts : dvector (term L) l) (ih_ts : dvector C l), C) : ∀{l} (t : preterm L l) (ts : dvector (term L) l) (ih_ts : dvector C l), C | _ &k ts ih_ts := hvar k | _ (func f) ts ih_ts := hfunc f ts ih_ts | _ (app t s) ts ih_ts := term.elim' t (s::ts) (term.elim' s ([]) ([])::ih_ts) @[elab_as_eliminator] def term.elim {C : Type v} (hvar : ∀(k : ℕ), C) (hfunc : Π {{l}} (f : L.functions l) (ts : dvector (term L) l) (ih_ts : dvector C l), C) : ∀(t : term L), C := λt, term.elim' hvar hfunc t ([]) ([]) lemma term.elim'_apps {C : Type v} (hvar : ∀(k : ℕ), C) (hfunc : Π {{l}} (f : L.functions l) (ts : dvector (term L) l) (ih_ts : dvector C l), C) {l} (t : preterm L l) (ts : dvector (term L) l) : @term.elim' L C hvar hfunc 0 (apps t ts) ([]) ([]) = @term.elim' L C hvar hfunc l t ts (ts.map $ term.elim hvar hfunc) := begin induction ts, { refl }, { dsimp only [dvector.map, apps], rw [ts_ih], refl } end lemma term.elim_apps {C : Type v} (hvar : ∀(k : ℕ), C) (hfunc : Π {{l}} (f : L.functions l) (ts : dvector (term L) l) (ih_ts : dvector C l), C) {l} (f : L.functions l) (ts : dvector (term L) l) : @term.elim L C hvar hfunc (apps (func f) ts) = hfunc f ts (ts.map $ @term.elim L C hvar hfunc) := by dsimp only [term.elim]; rw term.elim'_apps; refl /- lift_term_at _ t n m raises variables in t which are at least m by n -/ @[simp] def lift_term_at : ∀ {l}, preterm L l → ℕ → ℕ → preterm L l | _ &k n m := &(if m ≤ k then k+n else k) | _ (func f) n m := func f | _ (app t₁ t₂) n m := app (lift_term_at t₁ n m) (lift_term_at t₂ n m) notation t ` ↑' `:90 n ` # `:90 m:90 := fol.lift_term_at t n m -- input ↑ with \u or \upa -- @[simp] lemma lift_term_var_le {k n m} (h : m ≤ k) : &k ↑' n # m = (&(k+n) : term L) := dif_pos h -- @[simp] lemma lift_term_var_gt {k n m} (h : ¬(m ≤ k)) : &k ↑' n # m = (&k : term L) := dif_neg h -- @[simp] lemma lift_term_at_func {l} (f : L.functions l) (n m) : func f ↑' n # m = func f := by refl -- @[simp] lemma lift_term_at_app {l} (t : preterm L (l+1)) (s : preterm L 0) (n m) : -- app t s ↑' n # m = app (t ↑' n # m) (s ↑' n # m) := by refl @[reducible] def lift_term {l} (t : preterm L l) (n : ℕ) : preterm L l := t ↑' n # 0 infix ` ↑ `:100 := fol.lift_term -- input ↑' with \u or \upa @[reducible, simp] def lift_term1 {l} (t : preterm L l) : preterm L l := t ↑ 1 @[simp] lemma lift_term_def {l} (t : preterm L l) (n : ℕ) : t ↑' n # 0 = t ↑ n := by refl lemma injective_lift_term_at : ∀ {l} {n m : ℕ}, function.injective (λ(t : preterm L l), lift_term_at t n m) | _ n m &k &k' h := by by_cases h₁ : m ≤ k; by_cases h₂ : m ≤ k'; simp [h₁, h₂] at h; congr;[assumption, skip, skip, assumption]; exfalso; try {apply h₁}; try {apply h₂}; subst h; apply le_trans (by assumption) (le_add_left _ _) | _ n m &k (func f') h := by cases h | _ n m &k (app t₁' t₂') h := by cases h | _ n m (func f) &k' h := by cases h | _ n m (func f) (func f') h := h | _ n m (func f) (app t₁' t₂') h := by cases h | _ n m (app t₁ t₂) &k' h := by cases h | _ n m (app t₁ t₂) (func f') h := by cases h | _ n m (app t₁ t₂) (app t₁' t₂') h := begin injection h, congr; apply injective_lift_term_at; assumption end @[simp] lemma lift_term_at_zero : ∀ {l} (t : preterm L l) (m : ℕ), t ↑' 0 # m = t | _ &k m := by simp [lift_term_at] | _ (func f) m := by refl | _ (app t₁ t₂) m := by dsimp; congr; apply lift_term_at_zero @[simp] lemma lift_term_zero {l} (t : preterm L l) : t ↑ 0 = t := lift_term_at_zero t 0 /- the following lemmas simplify iterated lifts, depending on the size of m' -/ lemma lift_term_at2_small : ∀ {l} (t : preterm L l) (n n') {m m'}, m' ≤ m → (t ↑' n # m) ↑' n' # m' = (t ↑' n' # m') ↑' n # (m + n') | _ &k n n' m m' H := begin by_cases h : m ≤ k, { have h₁ : m' ≤ k := le_trans H h, have h₂ : m' ≤ k + n, from le_trans h₁ (k.le_add_right n), simp [*, -add_assoc, -add_comm], simp }, { have h₁ : ¬m + n' ≤ k + n', from λ h', h (le_of_add_le_add_right h'), have h₂ : ¬m + n' ≤ k, from λ h', h₁ (le_trans h' (k.le_add_right n')), by_cases h' : m' ≤ k; simp [*, -add_comm, -add_assoc] } end | _ (func f) n n' m m' H := by refl | _ (app t₁ t₂) n n' m m' H := begin dsimp; congr1; apply lift_term_at2_small; assumption end lemma lift_term_at2_medium : ∀ {l} (t : preterm L l) {n} (n') {m m'}, m ≤ m' → m' ≤ m+n → (t ↑' n # m) ↑' n' # m' = t ↑' (n+n') # m | _ &k n n' m m' H₁ H₂ := begin by_cases h : m ≤ k, { have h₁ : m' ≤ k + n, from le_trans H₂ (add_le_add_right h n), simp [*, -add_comm], }, { have h₁ : ¬m' ≤ k, from λ h', h (le_trans H₁ h'), simp [*, -add_comm, -add_assoc] } end | _ (func f) n n' m m' H₁ H₂ := by refl | _ (app t₁ t₂) n n' m m' H₁ H₂ := begin dsimp; congr1; apply lift_term_at2_medium; assumption end lemma lift_term2_medium {l} (t : preterm L l) {n} (n') {m'} (h : m' ≤ n) : (t ↑ n) ↑' n' # m' = t ↑ (n+n') := lift_term_at2_medium t n' m'.zero_le (by simp*) lemma lift_term2 {l} (t : preterm L l) (n n') : (t ↑ n) ↑ n' = t ↑ (n+n') := lift_term2_medium t n' n.zero_le lemma lift_term_at2_eq {l} (t : preterm L l) (n n' m : ℕ) : (t ↑' n # m) ↑' n' # (m+n) = t ↑' (n+n') # m := lift_term_at2_medium t n' (m.le_add_right n) (le_refl _) lemma lift_term_at2_large {l} (t : preterm L l) {n} (n') {m m'} (H : m + n ≤ m') : (t ↑' n # m) ↑' n' # m' = (t ↑' n' # (m'-n)) ↑' n # m := have H₁ : n ≤ m', from le_trans (n.le_add_left m) H, have H₂ : m ≤ m' - n, from nat.le_sub_right_of_add_le H, begin rw fol.lift_term_at2_small t n' n H₂, rw [nat.sub_add_cancel], exact H₁ end @[simp] lemma lift_term_var0 (n : ℕ) : &0 ↑ n = (&n : term L) := by have h : 0 ≤ 0 := le_refl 0; rw [←lift_term_def]; simp [h, -lift_term_def] @[simp] lemma lift_term_at_apps {l} (t : preterm L l) (ts : dvector (term L) l) (n m : ℕ) : (apps t ts) ↑' n # m = apps (t ↑' n # m) (ts.map $ λx, x ↑' n # m) := by induction ts generalizing t;[refl, apply ts_ih (app t ts_x)] @[simp] lemma lift_term_apps {l} (t : preterm L l) (ts : dvector (term L) l) (n : ℕ) : (apps t ts) ↑ n = apps (t ↑ n) (ts.map $ λx, x ↑ n) := lift_term_at_apps t ts n 0 /- subst_term t s n substitutes s for (&n) and reduces the level of all variables above n by 1 -/ def subst_term : ∀ {l}, preterm L l → term L → ℕ → preterm L l | _ &k s n := subst_realize var (s ↑ n) n k | _ (func f) s n := func f | _ (app t₁ t₂) s n := app (subst_term t₁ s n) (subst_term t₂ s n) notation t `[`:max s ` // `:95 n `]`:0 := fol.subst_term t s n @[simp] lemma subst_term_var_lt (s : term L) {k n : ℕ} (H : k < n) : &k[s // n] = &k := by simp only [H, fol.subst_term, fol.subst_realize_lt, eq_self_iff_true] @[simp] lemma subst_term_var_gt (s : term L) {k n : ℕ} (H : n < k) : &k[s // n] = &(k-1) := by simp only [H, fol.subst_term, fol.subst_realize_gt, eq_self_iff_true] @[simp] lemma subst_term_var_eq (s : term L) (n : ℕ) : &n[s // n] = s ↑' n # 0 := by simp [subst_term] lemma subst_term_var0 (s : term L) : &0[s // 0] = s := by simp @[simp] lemma subst_term_func {l} (f : L.functions l) (s : term L) (n : ℕ) : (func f)[s // n] = func f := by refl @[simp] lemma subst_term_app {l} (t₁ : preterm L (l+1)) (t₂ s : term L) (n : ℕ) : (app t₁ t₂)[s // n] = app (t₁[s // n]) (t₂[s // n]) := by refl @[simp] lemma subst_term_apps {l} (t : preterm L l) (ts : dvector (term L) l) (s : term L) (n : ℕ) : (apps t ts)[s // n] = apps (t[s // n]) (ts.map $ λx, x[s // n]) := by induction ts generalizing t;[refl, apply ts_ih (app t ts_x)] /- the following lemmas simplify first lifting and then substituting, depending on the size of the substituted variable -/ lemma lift_at_subst_term_large : ∀{l} (t : preterm L l) (s : term L) {n₁} (n₂) {m}, m ≤ n₁ → (t ↑' n₂ # m)[s // n₁+n₂] = (t [s // n₁]) ↑' n₂ # m | _ &k s n₁ n₂ m h := begin apply lt_by_cases k n₁; intro h₂, { have : k < n₁ + n₂, from lt_of_le_of_lt (k.le_add_right n₂) (by simp*), by_cases m ≤ k; simp* }, { subst h₂, simp [*, lift_term2_medium] }, { have h₂ : m < k, by apply lt_of_le_of_lt; assumption, have : m ≤ k - 1, from nat.le_sub_right_of_add_le (succ_le_of_lt h₂), have : m ≤ k, from le_of_lt h₂, have : 1 ≤ k, from one_le_of_lt h₂, simp [*, nat.add_sub_swap this n₂, -add_assoc, -add_comm] } end | _ (func f) s n₁ n₂ m h := rfl | _ (app t₁ t₂) s n₁ n₂ m h := by simp* lemma lift_subst_term_large {l} (t : preterm L l) (s : term L) (n₁ n₂) : (t ↑ n₂)[s // n₁+n₂] = (t [s // n₁]) ↑ n₂ := lift_at_subst_term_large t s n₂ n₁.zero_le lemma lift_subst_term_large' {l} (t : preterm L l) (s : term L) (n₁ n₂) : (t ↑ n₂)[s // n₂+n₁] = (t [s // n₁]) ↑ n₂ := by rw [add_comm]; apply lift_subst_term_large lemma lift_at_subst_term_medium : ∀{l} (t : preterm L l) (s : term L) {n₁ n₂ m}, m ≤ n₂ → n₂ ≤ m + n₁ → (t ↑' n₁+1 # m)[s // n₂] = t ↑' n₁ # m | _ &k s n₁ n₂ m h₁ h₂ := begin by_cases h : m ≤ k, { have h₃ : n₂ < k + (n₁ + 1), from lt_succ_of_le (le_trans h₂ (add_le_add_right h _)), simp [*, add_sub_cancel_right] }, { have h₃ : k < n₂, from lt_of_lt_of_le (lt_of_not_ge h) h₁, simp* } end | _ (func f) s n₁ n₂ m h₁ h₂ := rfl | _ (app t₁ t₂) s n₁ n₂ m h₁ h₂ := by simp* lemma lift_subst_term_medium {l} (t : preterm L l) (s : term L) (n₁ n₂) : (t ↑ ((n₁ + n₂) + 1))[s // n₁] = t ↑ (n₁ + n₂) := lift_at_subst_term_medium t s n₁.zero_le (by rw [zero_add]; exact n₁.le_add_right n₂) lemma lift_at_subst_term_eq {l} (t : preterm L l) (s : term L) (n : ℕ) : (t ↑' 1 # n)[s // n] = t := begin rw [lift_at_subst_term_medium t s, lift_term_at_zero]; refl end @[simp] lemma lift_term1_subst_term {l} (t : preterm L l) (s : term L) : (t ↑ 1)[s // 0] = t := lift_at_subst_term_eq t s 0 lemma lift_at_subst_term_small : ∀{l} (t : preterm L l) (s : term L) (n₁ n₂ m), (t ↑' n₁ # (m + n₂ + 1))[s ↑' n₁ # m // n₂] = (t [s // n₂]) ↑' n₁ # (m + n₂) | _ &k s n₁ n₂ m := begin by_cases h : m + n₂ + 1 ≤ k, { change m + n₂ + 1 ≤ k at h, have h₂ : n₂ < k := lt_of_le_of_lt (le_add_left n₂ m) (lt_of_succ_le h), have h₃ : n₂ < k + n₁ := by apply nat.lt_add_right; exact h₂, have h₄ : m + n₂ ≤ k - 1 := nat.le_sub_right_of_add_le h, simp [*, -add_comm, -add_assoc, nat.add_sub_swap (one_le_of_lt h₂)] }, { change ¬(m + n₂ + 1 ≤ k) at h, apply lt_by_cases k n₂; intro h₂, { have h₃ : ¬(m + n₂ ≤ k) := λh', not_le_of_gt h₂ (le_trans (le_add_left n₂ m) h'), simp [h, h₂, h₃, -add_comm, -add_assoc] }, { subst h₂, have h₃ : ¬(k + m + 1 ≤ k) := by rw [add_comm k m]; exact h, simp [h, h₃, -add_comm, -add_assoc], exact lift_term_at2_small _ _ _ m.zero_le }, { have h₃ : ¬(m + n₂ ≤ k - 1) := λh', h $ (nat.le_sub_right_iff_add_le $ one_le_of_lt h₂).mp h', simp [h, h₂, h₃, -add_comm, -add_assoc] }} end | _ (func f) s n₁ n₂ m := rfl | _ (app t₁ t₂) s n₁ n₂ m := by simp [*, -add_assoc, -add_comm] lemma subst_term2 : ∀{l} (t : preterm L l) (s₁ s₂ : term L) (n₁ n₂), t [s₁ // n₁] [s₂ // n₁ + n₂] = t [s₂ // n₁ + n₂ + 1] [s₁[s₂ // n₂] // n₁] | _ &k s₁ s₂ n₁ n₂ := begin -- can we use subst_realize2 here? apply lt_by_cases k n₁; intro h, { have : k < n₁ + n₂, from lt_of_le_of_lt (k.le_add_right n₂) (by simp*), have : k < n₁ + n₂ + 1, from lt.step this, simp only [*, eq_self_iff_true, fol.subst_term_var_lt] }, { have : k < k + (n₂ + 1), from lt_succ_of_le (le_add_right _ n₂), subst h, simp [*, lift_subst_term_large', -add_comm] }, apply lt_by_cases k (n₁ + n₂ + 1); intro h', { have : k - 1 < n₁ + n₂, from (nat.sub_lt_right_iff_lt_add (one_le_of_lt h)).2 h', simp [*, -add_comm, -add_assoc] }, { subst h', simp [h, lift_subst_term_medium, -add_comm, -add_assoc] }, { have : n₁ + n₂ < k - 1, from nat.lt_sub_right_of_add_lt h', have : n₁ < k - 1, from lt_of_le_of_lt (n₁.le_add_right n₂) this, simp only [*, eq_self_iff_true, fol.subst_term_var_gt] } end | _ (func f) s₁ s₂ n₁ n₂ := rfl | _ (app t₁ t₂) s₁ s₂ n₁ n₂ := by simp* lemma subst_term2_0 {l} (t : preterm L l) (s₁ s₂ : term L) (n) : t [s₁ // 0] [s₂ // n] = t [s₂ // n + 1] [s₁[s₂ // n] // 0] := let h := subst_term2 t s₁ s₂ 0 n in by simp only [zero_add] at h; exact h lemma lift_subst_term_cancel : ∀{l} (t : preterm L l) (n : ℕ), (t ↑' 1 # (n+1))[&0 // n] = t | _ &k n := begin apply lt_by_cases n k; intro h, { change n+1 ≤ k at h, have h' : n < k+1, from lt.step (lt_of_succ_le h), simp [h, h'] }, { have h' : ¬(k+1 ≤ k), from not_succ_le_self k, simp [h, h'] }, { have h' : ¬(n+1 ≤ k) := not_le_of_lt (lt.step h), simp [h, h'] } end | _ (func f) n := rfl | _ (app t₁ t₂) n := by dsimp; simp [*] /- Probably useful facts about substitution which we should add when needed: (forall M N i j k, ( M [ j ← N] ) ↑' k # (j+i) = (M ↑' k # (S (j+i))) [ j ← (N ↑' k # i ) ]) subst_travers : (forall M N P n, (M [← N]) [n ← P] = (M [n+1 ← P])[← N[n← P]]) erasure_lem3 : (forall n m t, m>n->#m = (#m ↑' 1 # (S n)) [n ← t]). lift_is_lift_sublemma : forall j v, j<v->exists w,#v=w↑1#j. lift_is_lift : (forall N A n i j,N ↑' i # n=A ↑' 1 # j -> j<n -> exists M,N=M ↑' 1 # j) subst_is_lift : (forall N T A n j, N [n ← T]=A↑' 1#j->j<n->exists M,N=M↑' 1#j) -/ /- preformula l is a partially applied formula. if applied to n terms, it becomes a formula. * We only have implication as binary connective. Since we use classical logic, we can define the other connectives from implication and falsum. * Similarly, universal quantification is our only quantifier. * We could make `falsum` and `equal` into elements of rel. However, if we do that, then we cannot make the interpretation of them in a model definitionally what we want. -/ variable (L) inductive preformula : ℕ → Type u | falsum {} : preformula 0 | equal (t₁ t₂ : term L) : preformula 0 | rel {l : ℕ} (R : L.relations l) : preformula l | apprel {l : ℕ} (f : preformula (l + 1)) (t : term L) : preformula l | imp (f₁ f₂ : preformula 0) : preformula 0 | all (f : preformula 0) : preformula 0 export preformula @[reducible] def formula := preformula L 0 variable {L} notation `⊥` := fol.preformula.falsum -- input: \bot infix ` ≃ `:88 := fol.preformula.equal -- input \~- or \simeq infix ` ⟹ `:62 := fol.preformula.imp -- input \==> prefix `∀'`:110 := fol.preformula.all def not (f : formula L) : formula L := f ⟹ ⊥ prefix `∼`:max := fol.not -- input \~, the ASCII character ~ has too low precedence notation `⊤` := ∼⊥ -- input: \top def and (f₁ f₂ : formula L) : formula L := ∼(f₁ ⟹ ∼f₂) infixr ` ⊓ ` := fol.and -- input: \sqcap def or (f₁ f₂ : formula L) : formula L := ∼f₁ ⟹ f₂ infixr ` ⊔ ` := fol.or -- input: \sqcup def biimp (f₁ f₂ : formula L) : formula L := (f₁ ⟹ f₂) ⊓ (f₂ ⟹ f₁) infix ` ⇔ `:61 := fol.biimp -- input \<=> def ex (f : formula L) : formula L := ∼ ∀' ∼f prefix `∃'`:110 := fol.ex -- input \ex @[simp] def apps_rel : ∀{l} (f : preformula L l) (ts : dvector (term L) l), formula L | 0 f [] := f | (n+1) f (t::ts) := apps_rel (apprel f t) ts @[simp] lemma apps_rel_zero (f : formula L) (ts : dvector (term L) 0) : apps_rel f ts = f := by cases ts; refl -- lemma apps_rel_ne_falsum {l} {R : L.relations l} {ts : dvector (term L) l} : -- apps_rel (rel R) ts ≠ ⊥ := -- by induction l; cases ts; [{cases ts_xs, intro h, injection h}, apply l_ih] -- lemma apps_rel_ne_falsum {l} {f : preformula L (l+1)} {ts : dvector (term L) (l+1)} : -- apps_rel f ts ≠ ⊥ := -- by induction l; cases ts; [{cases ts_xs, intro h, injection h}, apply l_ih] -- lemma apps_rel_ne_equal {l} {f : preformula L (l+1)} {ts : dvector (term L) (l+1)} -- {t₁ t₂ : term L} : apps_rel f ts ≠ t₁ ≃ t₂ := -- by induction l; cases ts; [{cases ts_xs, intro h, injection h}, apply l_ih] -- lemma apps_rel_ne_imp {l} {f : preformula L (l+1)} {ts : dvector (term L) (l+1)} -- {f₁ f₂ : formula L} : apps_rel f ts ≠ f₁ ⟹ f₂ := -- by induction l; cases ts; [{cases ts_xs, intro h, injection h}, apply l_ih] -- lemma apps_rel_ne_all {l} {f : preformula L (l+1)} {ts : dvector (term L) (l+1)} -- {f' : formula L} : apps_rel f ts ≠ ∀' f' := -- by induction l; cases ts; [{cases ts_xs, intro h, injection h}, apply l_ih] def formula_of_relation {l} (R : L.relations l) : arity' (term L) (formula L) l := arity'.of_dvector_map $ apps_rel (rel R) @[elab_as_eliminator] def formula.rec' {C : formula L → Sort v} (hfalsum : C ⊥) (hequal : Π (t₁ t₂ : term L), C (t₁ ≃ t₂)) (hrel : Π {{l}} (R : L.relations l) (ts : dvector (term L) l), C (apps_rel (rel R) ts)) (himp : Π {{f₁ f₂ : formula L}} (ih₁ : C f₁) (ih₂ : C f₂), C (f₁ ⟹ f₂)) (hall : Π {{f : formula L}} (ih : C f), C (∀' f)) : ∀{l} (f : preformula L l) (ts : dvector (term L) l), C (apps_rel f ts) | _ falsum ts := by cases ts; exact hfalsum | _ (t₁ ≃ t₂) ts := by cases ts; apply hequal | _ (rel R) ts := by apply hrel | _ (apprel f t) ts := by apply formula.rec' f (t::ts) | _ (f₁ ⟹ f₂) ts := by cases ts; exact himp (formula.rec' f₁ ([])) (formula.rec' f₂ ([])) | _ (∀' f) ts := by cases ts; exact hall (formula.rec' f ([])) @[elab_as_eliminator] def formula.rec {C : formula L → Sort v} (hfalsum : C ⊥) (hequal : Π (t₁ t₂ : term L), C (t₁ ≃ t₂)) (hrel : Π {{l}} (R : L.relations l) (ts : dvector (term L) l), C (apps_rel (rel R) ts)) (himp : Π {{f₁ f₂ : formula L}} (ih₁ : C f₁) (ih₂ : C f₂), C (f₁ ⟹ f₂)) (hall : Π {{f : formula L}} (ih : C f), C (∀' f)) : ∀f, C f := λf, formula.rec' hfalsum hequal hrel himp hall f ([]) @[simp] def formula.rec'_apps_rel {C : formula L → Sort v} (hfalsum : C ⊥) (hequal : Π (t₁ t₂ : term L), C (t₁ ≃ t₂)) (hrel : Π {{l}} (R : L.relations l) (ts : dvector (term L) l), C (apps_rel (rel R) ts)) (himp : Π {{f₁ f₂ : formula L}} (ih₁ : C f₁) (ih₂ : C f₂), C (f₁ ⟹ f₂)) (hall : Π {{f : formula L}} (ih : C f), C (∀' f)) {l} (f : preformula L l) (ts : dvector (term L) l) : @formula.rec' L C hfalsum hequal hrel himp hall 0 (apps_rel f ts) ([]) = @formula.rec' L C hfalsum hequal hrel himp hall l f ts := begin induction ts, { refl }, { dsimp only [dvector.map, apps_rel], rw [ts_ih], refl } end @[simp] def formula.rec_apps_rel {C : formula L → Sort v} (hfalsum : C ⊥) (hequal : Π (t₁ t₂ : term L), C (t₁ ≃ t₂)) (hrel : Π {{l}} (R : L.relations l) (ts : dvector (term L) l), C (apps_rel (rel R) ts)) (himp : Π {{f₁ f₂ : formula L}} (ih₁ : C f₁) (ih₂ : C f₂), C (f₁ ⟹ f₂)) (hall : Π {{f : formula L}} (ih : C f), C (∀' f)) {l} (R : L.relations l) (ts : dvector (term L) l) : @formula.rec L C hfalsum hequal hrel himp hall (apps_rel (rel R) ts) = hrel R ts := by dsimp only [formula.rec]; rw formula.rec'_apps_rel; refl @[simp] def lift_formula_at : ∀ {l}, preformula L l → ℕ → ℕ → preformula L l | _ falsum n m := falsum | _ (t₁ ≃ t₂) n m := lift_term_at t₁ n m ≃ lift_term_at t₂ n m | _ (rel R) n m := rel R | _ (apprel f t) n m := apprel (lift_formula_at f n m) (lift_term_at t n m) | _ (f₁ ⟹ f₂) n m := lift_formula_at f₁ n m ⟹ lift_formula_at f₂ n m | _ (∀' f) n m := ∀' lift_formula_at f n (m+1) notation f ` ↑' `:90 n ` # `:90 m:90 := fol.lift_formula_at f n m -- input ↑' with \upa @[reducible] def lift_formula {l} (f : preformula L l) (n : ℕ) : preformula L l := f ↑' n # 0 infix ` ↑ `:100 := fol.lift_formula -- input ↑' with \upa @[reducible, simp] def lift_formula1 {l} (f : preformula L l) : preformula L l := f ↑ 1 @[simp] lemma lift_formula_def {l} (f : preformula L l) (n : ℕ) : f ↑' n # 0 = f ↑ n := by refl @[simp] lemma lift_formula1_not (n : ℕ) (f : formula L) : ∼f ↑ n = ∼(f ↑ n) := by refl lemma injective_lift_formula_at {l} {n m : ℕ} : function.injective (λ (f : preformula L l), lift_formula_at f n m) := begin intros f f' H, induction f generalizing m; cases f'; injection H, { simp only [injective_lift_term_at h_1, injective_lift_term_at h_2, eq_self_iff_true, and_self] }, { simp only [f_ih h_1, injective_lift_term_at h_2, eq_self_iff_true, and_self] }, { simp only [f_ih_f₁ h_1, f_ih_f₂ h_2, eq_self_iff_true, and_self] }, { simp only [f_ih h_1, eq_self_iff_true] } end @[simp] lemma lift_formula_at_zero : ∀ {l} (f : preformula L l) (m : ℕ), f ↑' 0 # m = f | _ falsum m := by refl | _ (t₁ ≃ t₂) m := by simp | _ (rel R) m := by refl | _ (apprel f t) m := by simp; apply lift_formula_at_zero | _ (f₁ ⟹ f₂) m := by dsimp; congr1; apply lift_formula_at_zero | _ (∀' f) m := by simp; apply lift_formula_at_zero /- the following lemmas simplify iterated lifts, depending on the size of m' -/ lemma lift_formula_at2_small : ∀ {l} (f : preformula L l) (n n') {m m'}, m' ≤ m → (f ↑' n # m) ↑' n' # m' = (f ↑' n' # m') ↑' n # (m + n') | _ falsum n n' m m' H := by refl | _ (t₁ ≃ t₂) n n' m m' H := by simp [lift_term_at2_small, H] | _ (rel R) n n' m m' H := by refl | _ (apprel f t) n n' m m' H := by simp [lift_term_at2_small, H, -add_comm]; apply lift_formula_at2_small; assumption | _ (f₁ ⟹ f₂) n n' m m' H := by dsimp; congr1; apply lift_formula_at2_small; assumption | _ (∀' f) n n' m m' H := by simp [lift_term_at2_small, H, lift_formula_at2_small f n n' (add_le_add_right H 1)] lemma lift_formula_at2_medium : ∀ {l} (f : preformula L l) (n n') {m m'}, m ≤ m' → m' ≤ m+n → (f ↑' n # m) ↑' n' # m' = f ↑' (n+n') # m | _ falsum n n' m m' H₁ H₂ := by refl | _ (t₁ ≃ t₂) n n' m m' H₁ H₂ := by simp [*, lift_term_at2_medium] | _ (rel R) n n' m m' H₁ H₂ := by refl | _ (apprel f t) n n' m m' H₁ H₂ := by simp [*, lift_term_at2_medium, -add_comm] | _ (f₁ ⟹ f₂) n n' m m' H₁ H₂ := by simp* | _ (∀' f) n n' m m' H₁ H₂ := have m' + 1 ≤ (m + 1) + n, from le_trans (add_le_add_right H₂ 1) (by simp), by simp* lemma lift_formula_at2_eq {l} (f : preformula L l) (n n' m : ℕ) : (f ↑' n # m) ↑' n' # (m+n) = f ↑' (n+n') # m := lift_formula_at2_medium f n n' (m.le_add_right n) (le_refl _) lemma lift_formula_at2_large {l} (f : preformula L l) (n n') {m m'} (H : m + n ≤ m') : (f ↑' n # m) ↑' n' # m' = (f ↑' n' # (m'-n)) ↑' n # m := have H₁ : n ≤ m', from le_trans (n.le_add_left m) H, have H₂ : m ≤ m' - n, from nat.le_sub_right_of_add_le H, begin rw lift_formula_at2_small f n' n H₂, rw [nat.sub_add_cancel], exact H₁ end @[simp] lemma lift_formula_at_apps_rel {l} (f : preformula L l) (ts : dvector (term L) l) (n m : ℕ) : (apps_rel f ts) ↑' n # m = apps_rel (f ↑' n # m) (ts.map $ λx, x ↑' n # m) := by induction ts generalizing f;[refl, apply ts_ih (apprel f ts_x)] @[simp] lemma lift_formula_apps_rel {l} (f : preformula L l) (ts : dvector (term L) l) (n : ℕ) : (apps_rel f ts) ↑ n = apps_rel (f ↑ n) (ts.map $ λx, x ↑ n) := lift_formula_at_apps_rel f ts n 0 @[simp] def subst_formula : ∀ {l}, preformula L l → term L → ℕ → preformula L l | _ falsum s n := falsum | _ (t₁ ≃ t₂) s n := subst_term t₁ s n ≃ subst_term t₂ s n | _ (rel R) s n := rel R | _ (apprel f t) s n := apprel (subst_formula f s n) (subst_term t s n) | _ (f₁ ⟹ f₂) s n := subst_formula f₁ s n ⟹ subst_formula f₂ s n | _ (∀' f) s n := ∀' subst_formula f s (n+1) notation f `[`:95 s ` // `:95 n `]`:0 := fol.subst_formula f s n lemma subst_formula_equal (t₁ t₂ s : term L) (n : ℕ) : (t₁ ≃ t₂)[s // n] = t₁[s // n] ≃ (t₂[s // n]) := by refl @[simp] lemma subst_formula_biimp (f₁ f₂ : formula L) (s : term L) (n : ℕ) : (f₁ ⇔ f₂)[s // n] = f₁[s // n] ⇔ (f₂[s // n]) := by refl lemma lift_at_subst_formula_large : ∀{l} (f : preformula L l) (s : term L) {n₁} (n₂) {m}, m ≤ n₁ → (f ↑' n₂ # m)[s // n₁+n₂] = (f [s // n₁]) ↑' n₂ # m | _ falsum s n₁ n₂ m h := by refl | _ (t₁ ≃ t₂) s n₁ n₂ m h := by simp [*, lift_at_subst_term_large] | _ (rel R) s n₁ n₂ m h := by refl | _ (apprel f t) s n₁ n₂ m h := by simp [*, lift_at_subst_term_large] | _ (f₁ ⟹ f₂) s n₁ n₂ m h := by simp* | _ (∀' f) s n₁ n₂ m h := by have := lift_at_subst_formula_large f s n₂ (add_le_add_right h 1); simp at this; simp* lemma lift_subst_formula_large {l} (f : preformula L l) (s : term L) {n₁ n₂} : (f ↑ n₂)[s // n₁+n₂] = (f [s // n₁]) ↑ n₂ := lift_at_subst_formula_large f s n₂ n₁.zero_le lemma lift_subst_formula_large' {l} (f : preformula L l) (s : term L) {n₁ n₂} : (f ↑ n₂)[s // n₂+n₁] = (f [s // n₁]) ↑ n₂ := by rw [add_comm]; apply lift_subst_formula_large lemma lift_at_subst_formula_medium : ∀{l} (f : preformula L l) (s : term L) {n₁ n₂ m}, m ≤ n₂ → n₂ ≤ m + n₁ → (f ↑' n₁+1 # m)[s // n₂] = f ↑' n₁ # m | _ falsum s n₁ n₂ m h₁ h₂ := by refl | _ (t₁ ≃ t₂) s n₁ n₂ m h₁ h₂ := by simp [*, lift_at_subst_term_medium] | _ (rel R) s n₁ n₂ m h₁ h₂ := by refl | _ (apprel f t) s n₁ n₂ m h₁ h₂ := by simp [*, lift_at_subst_term_medium] | _ (f₁ ⟹ f₂) s n₁ n₂ m h₁ h₂ := by simp* | _ (∀' f) s n₁ n₂ m h₁ h₂ := begin have h : n₂ + 1 ≤ (m + 1) + n₁, from le_trans (add_le_add_right h₂ 1) (by simp), have := lift_at_subst_formula_medium f s (add_le_add_right h₁ 1) h, simp only [fol.subst_formula, fol.lift_formula_at] at this, simp* end lemma lift_subst_formula_medium {l} (f : preformula L l) (s : term L) (n₁ n₂) : (f ↑ ((n₁ + n₂) + 1))[s // n₁] = f ↑ (n₁ + n₂) := lift_at_subst_formula_medium f s n₁.zero_le (by rw [zero_add]; exact n₁.le_add_right n₂) lemma lift_at_subst_formula_eq {l} (f : preformula L l) (s : term L) (n : ℕ) : (f ↑' 1 # n)[s // n] = f := begin rw [lift_at_subst_formula_medium f s, lift_formula_at_zero]; refl end @[simp] lemma lift_formula1_subst {l} (f : preformula L l) (s : term L) : (f ↑ 1)[s // 0] = f := lift_at_subst_formula_eq f s 0 lemma lift_at_subst_formula_small : ∀{l} (f : preformula L l) (s : term L) (n₁ n₂ m), (f ↑' n₁ # (m + n₂ + 1))[s ↑' n₁ # m // n₂] = (f [s // n₂]) ↑' n₁ # (m + n₂) | _ falsum s n₁ n₂ m := by refl | _ (t₁ ≃ t₂) s n₁ n₂ m := by dsimp; simp only [lift_at_subst_term_small, eq_self_iff_true, and_self] | _ (rel R) s n₁ n₂ m := by refl | _ (apprel f t) s n₁ n₂ m := by dsimp; simp only [*, lift_at_subst_term_small, eq_self_iff_true, and_self] | _ (f₁ ⟹ f₂) s n₁ n₂ m := by dsimp; simp only [*, lift_at_subst_term_small, eq_self_iff_true, and_self] | _ (∀' f) s n₁ n₂ m := by have := lift_at_subst_formula_small f s n₁ (n₂+1) m; dsimp; simp at this ⊢; exact this lemma lift_at_subst_formula_small0 {l} (f : preformula L l) (s : term L) (n₁ m) : (f ↑' n₁ # (m + 1))[s ↑' n₁ # m // 0] = (f [s // 0]) ↑' n₁ # m := lift_at_subst_formula_small f s n₁ 0 m lemma subst_formula2 : ∀{l} (f : preformula L l) (s₁ s₂ : term L) (n₁ n₂), f [s₁ // n₁] [s₂ // n₁ + n₂] = f [s₂ // n₁ + n₂ + 1] [s₁[s₂ // n₂] // n₁] | _ falsum s₁ s₂ n₁ n₂ := by refl | _ (t₁ ≃ t₂) s₁ s₂ n₁ n₂ := by simp [*, subst_term2] | _ (rel R) s₁ s₂ n₁ n₂ := by refl | _ (apprel f t) s₁ s₂ n₁ n₂ := by simp [*, subst_term2] | _ (f₁ ⟹ f₂) s₁ s₂ n₁ n₂ := by simp* | _ (∀' f) s₁ s₂ n₁ n₂ := by simp*; rw [add_comm n₂ 1, ←add_assoc, subst_formula2 f s₁ s₂ (n₁ + 1) n₂]; simp lemma subst_formula2_zero {l} (f : preformula L l) (s₁ s₂ : term L) (n) : f [s₁ // 0] [s₂ // n] = f [s₂ // n + 1] [s₁[s₂ // n] // 0] := let h := subst_formula2 f s₁ s₂ 0 n in by simp only [fol.subst_formula, zero_add] at h; exact h lemma lift_subst_formula_cancel : ∀{l} (f : preformula L l) (n : ℕ), (f ↑' 1 # (n+1))[&0 // n] = f | _ falsum n := by refl | _ (t₁ ≃ t₂) n := by simp [*, lift_subst_term_cancel] | _ (rel R) n := by refl | _ (apprel f t) n := by simp [*, lift_subst_term_cancel] | _ (f₁ ⟹ f₂) n := by simp* | _ (∀' f) n := by simp* @[simp] lemma subst_formula_apps_rel {l} (f : preformula L l) (ts : dvector (term L) l) (s : term L) (n : ℕ): (apps_rel f ts)[s // n] = apps_rel (f[s // n]) (ts.map $ λx, x[s // n]) := by induction ts generalizing f;[refl, apply ts_ih (apprel f ts_x)] @[simp] def count_quantifiers : ∀ {l}, preformula L l → ℕ | _ falsum := 0 | _ (t₁ ≃ t₂) := 0 | _ (rel R) := 0 | _ (apprel f t) := 0 | _ (f₁ ⟹ f₂) := count_quantifiers f₁ + count_quantifiers f₂ | _ (∀' f) := count_quantifiers f + 1 @[simp] def count_quantifiers_succ {l} (f : preformula L (l+1)) : count_quantifiers f = 0 := by cases f; refl @[simp] lemma count_quantifiers_subst : ∀ {l} (f : preformula L l) (s : term L) (n : ℕ), count_quantifiers (f[s // n]) = count_quantifiers f | _ falsum s n := by refl | _ (t₁ ≃ t₂) s n := by refl | _ (rel R) s n := by refl | _ (apprel f t) s n := by refl | _ (f₁ ⟹ f₂) s n := by simp* | _ (∀' f) s n := by simp* def quantifier_free {l} : preformula L l → Prop := λ f, count_quantifiers f = 0 /- Provability * to decide: should Γ be a list or a set (or finset)? * We use natural deduction as our deduction system, since that is most convenient to work with. * All rules are motivated to work well with backwards reasoning. -/ inductive prf : set (formula L) → formula L → Type u | axm {Γ A} (h : A ∈ Γ) : prf Γ A | impI {Γ : set $ formula L} {A B} (h : prf (insert A Γ) B) : prf Γ (A ⟹ B) | impE {Γ} (A) {B} (h₁ : prf Γ (A ⟹ B)) (h₂ : prf Γ A) : prf Γ B | falsumE {Γ : set $ formula L} {A} (h : prf (insert ∼A Γ) ⊥) : prf Γ A | allI {Γ A} (h : prf (lift_formula1 '' Γ) A) : prf Γ (∀' A) | allE₂ {Γ} A t (h : prf Γ (∀' A)) : prf Γ (A[t // 0]) | ref (Γ t) : prf Γ (t ≃ t) | subst₂ {Γ} (s t f) (h₁ : prf Γ (s ≃ t)) (h₂ : prf Γ (f[s // 0])) : prf Γ (f[t // 0]) export prf infix ` ⊢ `:51 := fol.prf -- input: \|- or \vdash def provable (T : set $ formula L) (f : formula L) := nonempty (T ⊢ f) infix ` ⊢' `:51 := fol.provable -- input: \|- or \vdash def allE {Γ} (A : formula L) (t) {B} (H₁ : Γ ⊢ ∀' A) (H₂ : A[t // 0] = B) : Γ ⊢ B := by induction H₂; exact allE₂ A t H₁ def subst {Γ} {s t} (f₁ : formula L) {f₂} (H₁ : Γ ⊢ s ≃ t) (H₂ : Γ ⊢ f₁[s // 0]) (H₃ : f₁[t // 0] = f₂) : Γ ⊢ f₂ := by induction H₃; exact subst₂ s t f₁ H₁ H₂ def axm1 {Γ : set (formula L)} {A : formula L} : insert A Γ ⊢ A := by apply axm; left; refl def axm2 {Γ : set (formula L)} {A B : formula L} : insert A (insert B Γ) ⊢ B := by apply axm; right; left; refl def weakening {Γ Δ} {f : formula L} (H₁ : Γ ⊆ Δ) (H₂ : Γ ⊢ f) : Δ ⊢ f := begin induction H₂ generalizing Δ, { apply axm, exact H₁ H₂_h, }, { apply impI, apply H₂_ih, apply insert_subset_insert, apply H₁ }, { apply impE, apply H₂_ih_h₁, assumption, apply H₂_ih_h₂, assumption }, { apply falsumE, apply H₂_ih, apply insert_subset_insert, apply H₁ }, { apply allI, apply H₂_ih, apply image_subset _ H₁ }, { apply allE₂, apply H₂_ih, assumption }, { apply ref }, { apply subst₂, apply H₂_ih_h₁, assumption, apply H₂_ih_h₂, assumption }, end def prf_lift {Γ} {f : formula L} (n m : ℕ) (H : Γ ⊢ f) : (λf', f' ↑' n # m) '' Γ ⊢ f ↑' n # m := begin induction H generalizing m, { apply axm, apply mem_image_of_mem _ H_h }, { apply impI, have h := @H_ih m, rw [image_insert_eq] at h, exact h }, { apply impE, apply H_ih_h₁, apply H_ih_h₂ }, { apply falsumE, have h := @H_ih m, rw [image_insert_eq] at h, exact h }, { apply allI, rw [image_image], have h := @H_ih (m+1), rw [image_image] at h, apply cast _ h, congr1, apply image_congr', intro f', symmetry, exact lift_formula_at2_small f' _ _ m.zero_le }, { apply allE _ _ (H_ih m), apply lift_at_subst_formula_small0 }, { apply ref }, { apply subst _ (H_ih_h₁ m), { have h := @H_ih_h₂ m, rw [←lift_at_subst_formula_small0] at h, exact h}, rw [lift_at_subst_formula_small0] }, end def substitution {Γ} {f : formula L} (t n) (H : Γ ⊢ f) : (λx, x[t // n]) '' Γ ⊢ f[t // n] := begin induction H generalizing n, { apply axm, apply mem_image_of_mem _ H_h }, { apply impI, have h := H_ih n, rw [image_insert_eq] at h, exact h }, { apply impE, apply H_ih_h₁, apply H_ih_h₂ }, { apply falsumE, have h := H_ih n, rw [image_insert_eq] at h, exact h }, { apply allI, rw [image_image], have h := @H_ih (n+1), rw [image_image] at h, apply cast _ h, congr1, apply image_congr', intro, apply lift_subst_formula_large }, { apply allE _ _ (H_ih n), symmetry, apply subst_formula2_zero }, { apply ref }, { apply subst _ (H_ih_h₁ n), { have h := @H_ih_h₂ n, rw [subst_formula2_zero] at h, exact h}, rw [subst_formula2_zero] }, end def reflect_prf_lift1 {Γ} {f : formula L} (h : lift_formula1 '' Γ ⊢ f ↑ 1) : Γ ⊢ f := begin have := substitution &0 0 h, simp [image_image] at this, exact this end -- def reflect_prf_lift {Γ} {f : formula L} (n m : ℕ) : -- (λf' : formula L, f' ↑' n # m) '' Γ ⊢ f ↑' n # m → Γ ⊢ f := -- begin -- induction n, -- { rw [lift_zero] }, -- { } -- end def weakening1 {Γ} {f₁ f₂ : formula L} (H : Γ ⊢ f₂) : insert f₁ Γ ⊢ f₂ := weakening (subset_insert f₁ Γ) H def weakening2 {Γ} {f₁ f₂ f₃ : formula L} (H : insert f₁ Γ ⊢ f₂) : insert f₁ (insert f₃ Γ) ⊢ f₂ := weakening (insert_subset_insert (subset_insert _ Γ)) H def deduction {Γ} {A B : formula L} (H : Γ ⊢ A ⟹ B) : insert A Γ ⊢ B := impE A (weakening1 H) axm1 def exfalso {Γ} {A : formula L} (H : Γ ⊢ falsum) : Γ ⊢ A := falsumE (weakening1 H) def exfalso' {Γ} {A : formula L} (H : Γ ⊢' falsum) : Γ ⊢' A := by {fapply nonempty.map, exact Γ ⊢ falsum, exact exfalso, exact H} def notI {Γ} {A : formula L} (H : Γ ⊢ A ⟹ falsum) : Γ ⊢ ∼ A := by {rw[not], assumption} def andI {Γ} {f₁ f₂ : formula L} (H₁ : Γ ⊢ f₁) (H₂ : Γ ⊢ f₂) : Γ ⊢ f₁ ⊓ f₂ := begin apply impI, apply impE f₂, { apply impE f₁, apply axm1, exact weakening1 H₁ }, { exact weakening1 H₂ } end def andE1 {Γ f₁} (f₂ : formula L) (H : Γ ⊢ f₁ ⊓ f₂) : Γ ⊢ f₁ := begin apply falsumE, apply impE _ (weakening1 H), apply impI, apply exfalso, apply impE f₁; [apply axm2, apply axm1] end def andE2 {Γ} (f₁ : formula L) {f₂} (H : Γ ⊢ f₁ ⊓ f₂) : Γ ⊢ f₂ := begin apply falsumE, apply impE _ (weakening1 H), apply impI, apply axm2 end def orI1 {Γ} {A B : formula L} (H : Γ ⊢ A) : Γ ⊢ A ⊔ B := begin apply impI, apply exfalso, refine impE _ _ (weakening1 H), apply axm1 end def orI2 {Γ} {A B : formula L} (H : Γ ⊢ B) : Γ ⊢ A ⊔ B := impI $ weakening1 H def orE {Γ} {A B C : formula L} (H₁ : Γ ⊢ A ⊔ B) (H₂ : insert A Γ ⊢ C) (H₃ : insert B Γ ⊢ C) : Γ ⊢ C := begin apply falsumE, apply impE C, { apply axm1 }, apply impE B, { apply impI, exact weakening2 H₃ }, apply impE _ (weakening1 H₁), apply impI (impE _ axm2 (weakening2 H₂)) end def biimpI {Γ} {f₁ f₂ : formula L} (H₁ : insert f₁ Γ ⊢ f₂) (H₂ : insert f₂ Γ ⊢ f₁) : Γ ⊢ f₁ ⇔ f₂ := by apply andI; apply impI; assumption def biimpE1 {Γ} {f₁ f₂ : formula L} (H : Γ ⊢ f₁ ⇔ f₂) : insert f₁ Γ ⊢ f₂ := deduction (andE1 _ H) def biimpE2 {Γ} {f₁ f₂ : formula L} (H : Γ ⊢ f₁ ⇔ f₂) : insert f₂ Γ ⊢ f₁ := deduction (andE2 _ H) def exI {Γ f} (t : term L) (H : Γ ⊢ f [t // 0]) : Γ ⊢ ∃' f := begin apply impI, apply impE (f[t // 0]) _ (weakening1 H), apply allE₂ ∼f t axm1, end def exE {Γ} {f₁ f₂ : formula L} (H₁ : Γ ⊢ ∃' f₁) (H₂ : insert f₁ (lift_formula1 '' Γ) ⊢ lift_formula1 f₂) : Γ ⊢ f₂ := begin apply falsumE, apply impE _ (weakening1 H₁), apply allI, apply impI, rw [image_insert_eq], apply impE _ axm2, apply weakening2 H₂ end def ex_not_of_not_all {Γ} {f : formula L} (H : Γ ⊢ ∼ ∀' f) : Γ ⊢ ∃' ∼ f := begin apply falsumE, apply impE _ (weakening1 H), apply allI, apply falsumE, rw [image_insert_eq], apply impE _ axm2, apply exI &0, rw [lift_subst_formula_cancel], exact axm1 end def not_and_self {Γ : set (formula L)} {f : formula L} (H : Γ ⊢ f ⊓ ∼f) : Γ ⊢ ⊥ := impE f (andE2 f H) (andE1 ∼f H) -- def andE1 {Γ f₁} (f₂ : formula L) (H : Γ ⊢ f₁ ⊓ f₂) : Γ ⊢ f₁ := def symm {Γ} {s t : term L} (H : Γ ⊢ s ≃ t) : Γ ⊢ t ≃ s := begin apply subst (&0 ≃ s ↑ 1) H; rw [subst_formula_equal, lift_term1_subst_term, subst_term_var0], apply ref end def trans {Γ} {t₁ t₂ t₃ : term L} (H : Γ ⊢ t₁ ≃ t₂) (H' : Γ ⊢ t₂ ≃ t₃) : Γ ⊢ t₁ ≃ t₃ := begin apply subst (t₁ ↑ 1 ≃ &0) H'; rw [subst_formula_equal, lift_term1_subst_term, subst_term_var0], exact H end def congr {Γ} {t₁ t₂ : term L} (s : term L) (H : Γ ⊢ t₁ ≃ t₂) : Γ ⊢ s[t₁ // 0] ≃ s[t₂ // 0] := begin apply subst (s[t₁ // 0] ↑ 1 ≃ s) H, { rw [subst_formula_equal, lift_term1_subst_term], apply ref }, { rw [subst_formula_equal, lift_term1_subst_term] } end def app_congr {Γ} {t₁ t₂ : term L} (s : preterm L 1) (H : Γ ⊢ t₁ ≃ t₂) : Γ ⊢ app s t₁ ≃ app s t₂ := begin have h := congr (app (s ↑ 1) &0) H, simp at h, exact h end def apprel_congr {Γ} {t₁ t₂ : term L} (f : preformula L 1) (H : Γ ⊢ t₁ ≃ t₂) (H₂ : Γ ⊢ apprel f t₁) : Γ ⊢ apprel f t₂ := begin apply subst (apprel (f ↑ 1) &0) H; simp, exact H₂ end def imp_trans {Γ} {f₁ f₂ f₃ : formula L} (H₁ : Γ ⊢ f₁ ⟹ f₂) (H₂ : Γ ⊢ f₂ ⟹ f₃) : Γ ⊢ f₁ ⟹ f₃ := begin apply impI, apply impE _ (weakening1 H₂), apply impE _ (weakening1 H₁) axm1 end def biimp_refl (Γ : set (formula L)) (f : formula L) : Γ ⊢ f ⇔ f := by apply biimpI; apply axm1 def biimp_trans {Γ} {f₁ f₂ f₃ : formula L} (H₁ : Γ ⊢ f₁ ⇔ f₂) (H₂ : Γ ⊢ f₂ ⇔ f₃) : Γ ⊢ f₁ ⇔ f₃ := begin apply andI; apply imp_trans, apply andE1 _ H₁, apply andE1 _ H₂, apply andE2 _ H₂, apply andE2 _ H₁ end def equal_preterms (T : set (formula L)) {l} (t₁ t₂ : preterm L l) : Type u := ∀(ts : dvector (term L) l), T ⊢ apps t₁ ts ≃ apps t₂ ts def equal_preterms_app {T : set (formula L)} {l} {t t' : preterm L (l+1)} {s s' : term L} (Ht : equal_preterms T t t') (Hs : T ⊢ s ≃ s') : equal_preterms T (app t s) (app t' s') := begin intro xs, apply trans (Ht (xs.cons s)), have h := congr (apps (t' ↑ 1) (&0 :: xs.map lift_term1)) Hs, simp [dvector.map_congr (λt, lift_term1_subst_term t s')] at h, exact h end @[refl] def equal_preterms_refl (T : set (formula L)) {l} (t : preterm L l) : equal_preterms T t t := λxs, ref T (apps t xs) def equiv_preformulae (T : set (formula L)) {l} (f₁ f₂ : preformula L l) : Type u := ∀(ts : dvector (term L) l), T ⊢ apps_rel f₁ ts ⇔ apps_rel f₂ ts def equiv_preformulae_apprel {T : set (formula L)} {l} {f f' : preformula L (l+1)} {s s' : term L} (Ht : equiv_preformulae T f f') (Hs : T ⊢ s ≃ s') : equiv_preformulae T (apprel f s) (apprel f' s') := begin intro xs, apply biimp_trans (Ht (xs.cons s)), apply subst (apps_rel (f' ↑ 1) ((s :: xs).map lift_term1) ⇔ apps_rel (f' ↑ 1) (&0 :: xs.map lift_term1)) Hs; simp [dvector.map_congr (λt, lift_term1_subst_term t s')], apply biimp_refl end @[refl] def equiv_preformulae_refl (T : set (formula L)) {l} (f : preformula L l) : equiv_preformulae T f f := λxs, biimp_refl T (apps_rel f xs) def impI' {Γ : set $ formula L} {A B} (h : insert A Γ ⊢' B) : Γ ⊢' (A ⟹ B) := h.map impI def impE' {Γ} (A : formula L) {B} (h₁ : Γ ⊢' A ⟹ B) (h₂ : Γ ⊢' A) : Γ ⊢' B := h₁.map2 (impE _) h₂ def falsumE' {Γ : set $ formula L} {A} (h : insert ∼A Γ ⊢' ⊥ ) : Γ ⊢' A := h.map falsumE def allI' {Γ} {A : formula L} (h : lift_formula1 '' Γ ⊢' A) : Γ ⊢' ∀' A := h.map allI def allE' {Γ} (A : formula L) (t) {B} (H₁ : Γ ⊢' ∀' A) (H₂ : A[t // 0] = B) : Γ ⊢' B := H₁.map (λx, allE _ _ x H₂) def allE₂' {Γ} {A} {t : term L} (h : Γ ⊢' ∀' A) : Γ ⊢' A[t // 0] := h.map (λx, allE _ _ x rfl) def ref' (Γ) (t : term L) : Γ ⊢' (t ≃ t) := ⟨ref Γ t⟩ def subst' {Γ} {s t} (f₁ : formula L) {f₂} (H₁ : Γ ⊢' s ≃ t) (H₂ : Γ ⊢' f₁[s // 0]) (H₃ : f₁[t // 0] = f₂) : Γ ⊢' f₂ := H₁.map2 (λx y, subst _ x y H₃) H₂ def subst₂' {Γ} (s t) (f : formula L) (h₁ : Γ ⊢' s ≃ t) (h₂ : Γ ⊢' f[s // 0]) : Γ ⊢' f[t // 0] := h₁.map2 (subst₂ _ _ _) h₂ def weakening' {Γ Δ} {f : formula L} (H₁ : Γ ⊆ Δ) (H₂ : Γ ⊢' f) : Δ ⊢' f := H₂.map $ weakening H₁ def weakening1' {Γ} {f₁ f₂ : formula L} (H : Γ ⊢' f₂) : insert f₁ Γ ⊢' f₂ := H.map weakening1 def weakening2' {Γ} {f₁ f₂ f₃ : formula L} (H : insert f₁ Γ ⊢' f₂) : insert f₁ (insert f₃ Γ) ⊢' f₂ := H.map weakening2 lemma apprel_congr' {Γ} {t₁ t₂ : term L} (f : preformula L 1) (H : Γ ⊢ t₁ ≃ t₂) : Γ ⊢' apprel f t₁ ↔ Γ ⊢' apprel f t₂ := ⟨nonempty.map $ apprel_congr f H, nonempty.map $ apprel_congr f $ symm H⟩ lemma prf_all_iff {Γ : set (formula L)} {f} : Γ ⊢' ∀' f ↔ lift_formula1 '' Γ ⊢' f := begin split, { intro H, rw [←lift_subst_formula_cancel f 0], apply allE₂', apply H.map (prf_lift 1 0) }, { exact allI' } end lemma iff_of_biimp {Γ} {f₁ f₂ : formula L} (H : Γ ⊢' f₁ ⇔ f₂) : Γ ⊢' f₁ ↔ Γ ⊢' f₂ := ⟨impE' _ $ H.map (andE1 _), impE' _ $ H.map (andE2 _)⟩ lemma prf_by_cases {Γ} (f₁) {f₂ : formula L} (H₁ : insert f₁ Γ ⊢' f₂) (H₂ : insert ∼f₁ Γ ⊢' f₂) : Γ ⊢' f₂ := begin apply falsumE', apply impE' _ ⟨axm1⟩, refine impE' _ (impI' (weakening2' H₁)) _, apply falsumE', apply impE' _ ⟨axm2⟩, apply weakening2' H₂ end /- model theory -/ /- an L-structure is a type S with interpretations of the functions and relations on S -/ variable (L) structure Structure := (carrier : Type u) (fun_map : ∀{n}, L.functions n → dvector carrier n → carrier) (rel_map : ∀{n}, L.relations n → dvector carrier n → Prop) variable {L} instance has_coe_Structure : has_coe_to_sort (@fol.Structure L) := ⟨Type u, Structure.carrier⟩ /- realization of terms -/ @[simp] def realize_term {S : Structure L} (v : ℕ → S) : ∀{l} (t : preterm L l) (xs : dvector S l), S.carrier | _ &k xs := v k | _ (func f) xs := S.fun_map f xs | _ (app t₁ t₂) xs := realize_term t₁ $ realize_term t₂ ([])::xs lemma realize_term_congr {S : Structure L} {v v' : ℕ → S} (h : ∀n, v n = v' n) : ∀{l} (t : preterm L l) (xs : dvector S l), realize_term v t xs = realize_term v' t xs | _ &k xs := h k | _ (func f) xs := by refl | _ (app t₁ t₂) xs := by dsimp; rw [realize_term_congr t₁, realize_term_congr t₂] lemma realize_term_subst {S : Structure L} (v : ℕ → S) : ∀{l} (n : ℕ) (t : preterm L l) (s : term L) (xs : dvector S l), realize_term (v[realize_term v (s ↑ n) ([]) // n]) t xs = realize_term v (t[s // n]) xs | _ n &k s [] := by apply lt_by_cases k n; intro h;[simp [h], {subst h; simp}, simp [h]] | _ n (func f) s xs := by refl | _ n (app t₁ t₂) s xs := by dsimp; simp* lemma realize_term_subst_lift {S : Structure L} (v : ℕ → S) (x : S) (m : ℕ) : ∀{l} (t : preterm L l) (xs : dvector S l), realize_term (v [x // m]) (t ↑' 1 # m) xs = realize_term v t xs | _ &k [] := begin by_cases h : m ≤ k, { have : m < k + 1, from lt_succ_of_le h, simp* }, { have : k < m, from lt_of_not_ge h, simp* } end | _ (func f) xs := by refl | _ (app t₁ t₂) xs := by simp* /- realization of formulas -/ @[simp] def realize_formula {S : Structure L} : ∀{l}, (ℕ → S) → preformula L l → dvector S l → Prop | _ v falsum xs := false | _ v (t₁ ≃ t₂) xs := realize_term v t₁ xs = realize_term v t₂ xs | _ v (rel R) xs := S.rel_map R xs | _ v (apprel f t) xs := realize_formula v f $ realize_term v t ([])::xs | _ v (f₁ ⟹ f₂) xs := realize_formula v f₁ xs → realize_formula v f₂ xs | _ v (∀' f) xs := ∀(x : S), realize_formula (v [x // 0]) f xs lemma realize_formula_congr {S : Structure L} : ∀{l} {v v' : ℕ → S} (h : ∀n, v n = v' n) (f : preformula L l) (xs : dvector S l), realize_formula v f xs ↔ realize_formula v' f xs | _ v v' h falsum xs := by refl | _ v v' h (t₁ ≃ t₂) xs := by simp [realize_term_congr h] | _ v v' h (rel R) xs := by refl | _ v v' h (apprel f t) xs := by simp [realize_term_congr h]; rw [realize_formula_congr h] | _ v v' h (f₁ ⟹ f₂) xs := by dsimp; rw [realize_formula_congr h, realize_formula_congr h] | _ v v' h (∀' f) xs := by apply forall_congr; intro x; apply realize_formula_congr; intro n; apply subst_realize_congr h lemma realize_formula_subst {S : Structure L} : ∀{l} (v : ℕ → S) (n : ℕ) (f : preformula L l) (s : term L) (xs : dvector S l), realize_formula (v[realize_term v (s ↑ n) ([]) // n]) f xs ↔ realize_formula v (f[s // n]) xs | _ v n falsum s xs := by refl | _ v n (t₁ ≃ t₂) s xs := by simp [realize_term_subst] | _ v n (rel R) s xs := by refl | _ v n (apprel f t) s xs := by simp [realize_term_subst]; rw realize_formula_subst | _ v n (f₁ ⟹ f₂) s xs := by apply imp_congr; apply realize_formula_subst | _ v n (∀' f) s xs := begin apply forall_congr, intro x, rw [←realize_formula_subst], apply realize_formula_congr, intro k, rw [subst_realize2_0, ←realize_term_subst_lift v x 0, lift_term_def, lift_term2] end lemma realize_formula_subst0 {S : Structure L} {l} (v : ℕ → S) (f : preformula L l) (s : term L) (xs : dvector S l) : realize_formula (v[realize_term v s ([]) // 0]) f xs ↔ realize_formula v (f[s // 0]) xs := by have h := realize_formula_subst v 0 f s; simp at h; exact h xs lemma realize_formula_subst_lift {S : Structure L} : ∀{l} (v : ℕ → S) (x : S) (m : ℕ) (f : preformula L l) (xs : dvector S l), realize_formula (v [x // m]) (f ↑' 1 # m) xs = realize_formula v f xs | _ v x m falsum xs := by refl | _ v x m (t₁ ≃ t₂) xs := by simp [realize_term_subst_lift] | _ v x m (rel R) xs := by refl | _ v x m (apprel f t) xs := by simp [realize_term_subst_lift]; rw realize_formula_subst_lift | _ v x m (f₁ ⟹ f₂) xs := by apply imp_eq_congr; apply realize_formula_subst_lift | _ v x m (∀' f) xs := begin apply forall_eq_congr, intro x', rw [realize_formula_congr (subst_realize2_0 _ _ _ _), realize_formula_subst_lift] end /- the following definitions of provability and satisfiability are not exactly how you normally define them, since we define it for formulae instead of sentences. If all the formulae happen to be sentences, then these definitions are equivalent to the normal definitions (the realization of closed terms and sentences are independent of the realizer v). -/ def all_prf (T T' : set (formula L)) := ∀{{f}}, f ∈ T' → T ⊢ f infix ` ⊢ `:51 := fol.all_prf -- input: |- or \vdash def satisfied_in (S : Structure L) (f : formula L) := ∀(v : ℕ → S), realize_formula v f ([]) infix ` ⊨ `:51 := fol.satisfied_in -- input using \|= or \vDash, but not using \models def all_satisfied_in (S : Structure L) (T : set (formula L)) := ∀{{f}}, f ∈ T → S ⊨ f infix ` ⊨ `:51 := fol.all_satisfied_in -- input using \|= or \vDash, but not using \models def satisfied (T : set (formula L)) (f : formula L) := ∀(S : Structure L) (v : ℕ → S), (∀f' ∈ T, realize_formula v (f' : formula L) ([])) → realize_formula v f ([]) infix ` ⊨ `:51 := fol.satisfied -- input using \|= or \vDash, but not using \models def all_satisfied (T T' : set (formula L)) := ∀{{f}}, f ∈ T' → T ⊨ f infix ` ⊨ `:51 := fol.all_satisfied -- input using \|= or \vDash, but not using \models def satisfied_in_trans {S : Structure L} {T : set (formula L)} {f : formula L} (H' : S ⊨ T) (H : T ⊨ f) : S ⊨ f := λv, H S v $ λf' hf', H' hf' v def all_satisfied_in_trans {S : Structure L} {T T' : set (formula L)} (H' : S ⊨ T) (H : T ⊨ T') : S ⊨ T' := λf hf, satisfied_in_trans H' $ H hf def satisfied_of_mem {T : set (formula L)} {f : formula L} (hf : f ∈ T) : T ⊨ f := λS v h, h f hf def all_satisfied_of_subset {T T' : set (formula L)} (h : T' ⊆ T) : T ⊨ T' := λf hf, satisfied_of_mem $ h hf def satisfied_trans {T₁ T₂ : set (formula L)} {f : formula L} (H' : T₁ ⊨ T₂) (H : T₂ ⊨ f) : T₁ ⊨ f := λS v h, H S v $ λf' hf', H' hf' S v h def all_satisfied_trans {T₁ T₂ T₃ : set (formula L)} (H' : T₁ ⊨ T₂) (H : T₂ ⊨ T₃) : T₁ ⊨ T₃ := λf hf, satisfied_trans H' $ H hf def satisfied_weakening {T T' : set (formula L)} (H : T ⊆ T') {f : formula L} (HT : T ⊨ f) : T' ⊨ f := λS v h, HT S v $ λf' hf', h f' $ H hf' /- soundness for a set of formulae -/ lemma formula_soundness {Γ : set (formula L)} {A : formula L} (H : Γ ⊢ A) : Γ ⊨ A := begin intro S, induction H; intros v h, { apply h, apply H_h }, { intro ha, apply H_ih, intros f hf, induction hf, { subst hf, assumption }, apply h f hf }, { exact H_ih_h₁ v h (H_ih_h₂ v h) }, { apply classical.by_contradiction, intro ha, apply H_ih v, intros f hf, induction hf, { cases hf, exact ha }, apply h f hf }, { intro x, apply H_ih, intros f hf, cases (mem_image _ _ _).mp hf with f' hf', induction hf', induction hf'_right, rw [realize_formula_subst_lift v x 0 f'], exact h f' hf'_left }, { rw [←realize_formula_subst0], apply H_ih v h (realize_term v H_t ([])) }, { dsimp, refl }, { have h' := H_ih_h₁ v h, dsimp at h', rw [←realize_formula_subst0, ←h', realize_formula_subst0], apply H_ih_h₂ v h }, end /- sentences and theories -/ variable (L) inductive bounded_preterm (n : ℕ) : ℕ → Type u | bd_var {} : ∀ (k : fin n), bounded_preterm 0 | bd_func {} : ∀ {l : ℕ} (f : L.functions l), bounded_preterm l | bd_app : ∀ {l : ℕ} (t : bounded_preterm (l + 1)) (s : bounded_preterm 0), bounded_preterm l export bounded_preterm def bounded_term (n) := bounded_preterm L n 0 def closed_preterm (l) := bounded_preterm L 0 l def closed_term := closed_preterm L 0 variable {L} prefix `&`:max := bd_var def bd_const {n} (c : L.constants) : bounded_term L n := bd_func c @[simp] def bd_apps {n} : ∀{l}, bounded_preterm L n l → dvector (bounded_term L n) l → bounded_term L n | _ t [] := t | _ t (t'::ts) := bd_apps (bd_app t t') ts namespace bounded_preterm @[simp] protected def fst {n} : ∀{l}, bounded_preterm L n l → preterm L l | _ &k := &k.1 | _ (bd_func f) := func f | _ (bd_app t s) := app (fst t) (fst s) local attribute [extensionality] fin.eq_of_veq @[extensionality] protected def eq {n} : ∀{l} {t₁ t₂ : bounded_preterm L n l} (h : t₁.fst = t₂.fst), t₁ = t₂ | _ &k &k' h := by injection h with h'; congr1; ext; exact h' | _ &k (bd_func f') h := by injection h | _ &k (bd_app t₁' t₂') h := by injection h | _ (bd_func f) &k' h := by injection h | _ (bd_func f) (bd_func f') h := by injection h with h'; rw h' | _ (bd_func f) (bd_app t₁' t₂') h := by injection h | _ (bd_app t₁ t₂) &k' h := by injection h | _ (bd_app t₁ t₂) (bd_func f') h := by injection h | _ (bd_app t₁ t₂) (bd_app t₁' t₂') h := by injection h with h₁ h₂; congr1; apply eq; assumption @[simp] protected def cast {n m} (h : n ≤ m) : ∀ {l} (t : bounded_preterm L n l), bounded_preterm L m l | _ &k := &(k.cast_le h) | _ (bd_func f) := bd_func f | _ (bd_app t s) := bd_app t.cast s.cast @[simp]lemma cast_bd_app {n m} (h : n ≤ m) {l} {t : bounded_preterm L n (l+1)} {s : bounded_preterm L n 0} : (bd_app t s).cast h = (bd_app (t.cast h) (s.cast h)) := by refl @[simp]lemma cast_bd_apps {n m } (h : n ≤ m) {l} {t : bounded_preterm L n l} {ts : dvector (bounded_term L n) l} : (bd_apps t ts).cast h = bd_apps (t.cast h) (ts.map (λ t, t.cast h)) := by {induction ts generalizing t, refl, simp*} -- @[simp]lemma cast_bd_apps_nil {n m} (h : n ≤ m) {l} {t : bounded_preterm L n (l+1)} {s : bounded_preterm L n 0} : (bd_apps t s []).cast h = (bd_app (t.cast h) (s.cast h)) @[simp] lemma cast_irrel {n m } {h h' : n ≤ m} : ∀ {l} (t : bounded_preterm L n l), (t.cast h) = (t.cast h') := by {intros, refl} @[simp] lemma cast_rfl {n} {h : n ≤ n} : ∀ {l} (t : bounded_preterm L n l), (t.cast h) = t := begin intros, induction t, {simp, unfold fin.cast_le, unfold fin.cast_lt, cases t, refl}, {refl}, {simp*} end protected def cast_eq {n m l} (h : n = m) (t : bounded_preterm L n l) : bounded_preterm L m l := t.cast $ le_of_eq h protected def cast1 {n l} (t : bounded_preterm L n l) : bounded_preterm L (n+1) l := t.cast $ n.le_add_right 1 @[simp] lemma cast_fst {n m} (h : n ≤ m) : ∀ {l} (t : bounded_preterm L n l), (t.cast h).fst = t.fst | _ &k := by refl | _ (bd_func f) := by refl | _ (bd_app t s) := by dsimp; simp [cast_fst] @[simp] lemma cast_eq_fst {n m l} (h : n = m) (t : bounded_preterm L n l) : (t.cast_eq h).fst = t.fst := t.cast_fst _ @[simp] lemma cast1_fst {n l} (t : bounded_preterm L n l) : t.cast1.fst = t.fst := t.cast_fst _ end bounded_preterm namespace closed_preterm @[reducible]protected def cast0 (n) {l} (t : closed_preterm L l) : bounded_preterm L n l := t.cast n.zero_le @[simp] lemma cast0_fst {n l : ℕ} (t : closed_preterm L l) : (t.cast0 n).fst = t.fst := cast_fst _ _ end closed_preterm @[elab_as_eliminator] def bounded_term.rec {n} {C : bounded_term L n → Sort v} (hvar : ∀(k : fin n), C &k) (hfunc : Π {l} (f : L.functions l) (ts : dvector (bounded_term L n) l) (ih_ts : ∀t, ts.pmem t → C t), C (bd_apps (bd_func f) ts)) : ∀(t : bounded_term L n), C t := have h : ∀{l} (t : bounded_preterm L n l) (ts : dvector (bounded_term L n) l) (ih_ts : ∀s, ts.pmem s → C s), C (bd_apps t ts), begin intros, induction t; try {rw ts.zero_eq}, { apply hvar }, { apply hfunc t_f ts ih_ts }, { apply t_ih_t (t_s::ts), intros t ht, cases ht, { induction ht, apply t_ih_s ([]), intros s hs, cases hs }, { exact ih_ts t ht }}, end, λt, h t ([]) (by intros s hs; cases hs) @[elab_as_eliminator] def bounded_term.rec1 {n} {C : bounded_term L (n+1) → Sort v} (hvar : ∀(k : fin (n+1)), C &k) (hfunc : Π {l} (f : L.functions l) (ts : dvector (bounded_term L (n+1)) l) (ih_ts : ∀t, ts.pmem t → C t), C (bd_apps (bd_func f) ts)) : ∀(t : bounded_term L (n+1)), C t := have h : ∀{l} (t : bounded_preterm L (n+1) l) (ts : dvector (bounded_term L (n+1)) l) (ih_ts : ∀s, ts.pmem s → C s), C (bd_apps t ts), begin intros, induction t; try {rw ts.zero_eq}, { apply hvar }, { apply hfunc t_f ts ih_ts }, { apply t_ih_t (t_s::ts), intros t ht, cases ht, { induction ht, apply t_ih_s ([]), intros s hs, cases hs }, { exact ih_ts t ht }}, end, λt, h t ([]) (by intros s hs; cases hs) lemma lift_bounded_term_irrel {n : ℕ} : ∀{l} (t : bounded_preterm L n l) (n') {m : ℕ} (h : n ≤ m), t.fst ↑' n' # m = t.fst | _ &k n' m h := have h' : ¬(m ≤ k.1), from not_le_of_lt (lt_of_lt_of_le k.2 h), by simp [h'] | _ (bd_func f) n' m h := by refl | _ (bd_app t s) n' m h := by simp [lift_bounded_term_irrel t n' h, lift_bounded_term_irrel s n' h] lemma subst_bounded_term_irrel {n : ℕ} : ∀{l} (t : bounded_preterm L n l) {n'} (s : term L) (h : n ≤ n'), t.fst[s // n'] = t.fst | _ &k n' s h := by simp [lt_of_lt_of_le k.2 h] | _ (bd_func f) n' s h := by refl | _ (bd_app t₁ t₂) n' s h := by simp* /--Given a bounded_preterm of bound n and level l, realize it using (v : dvector S n) and (xs : dvector L l) by the following structural induction: 1. Given a free de Bruijn variable &k, replace it with the kth member (indexing starting at 0) of v, 2. given a (bd_func f), replace it with its realization as a function on S, _evaluated_ at xs, and 3. given an application of terms, replace it with a literal application of terms, with the inner term evaluated at xs. --/ --- note from Mario: replace dvector.nth with dvector.nth'' @[simp] def realize_bounded_term {S : Structure L} {n} (v : dvector S n) : ∀{l} (t : bounded_preterm L n l) (xs : dvector S l), S.carrier | _ &k xs := v.nth k.1 k.2 | _ (bd_func f) xs := S.fun_map f xs | _ (bd_app t₁ t₂) xs := realize_bounded_term t₁ $ realize_bounded_term t₂ ([])::xs /- S[t ; v] -/ notation S`[`:max v ` /// `:95 t`]`:0 := @fol.realize_bounded_term _ S _ v _ t (dvector.nil) -- fol.realize_bounded_term v t (dvector.nil) @[reducible] def realize_closed_term (S : Structure L) (t : closed_term L) : S := realize_bounded_term ([]) t ([]) lemma realize_bounded_term_eq {S : Structure L} {n} {v₁ : dvector S n} {v₂ : ℕ → S} (hv : ∀k (hk : k < n), v₁.nth k hk = v₂ k) : ∀{l} (t : bounded_preterm L n l) (xs : dvector S l), realize_bounded_term v₁ t xs = realize_term v₂ t.fst xs | _ &k xs := hv k.1 k.2 | _ (bd_func f) xs := by refl | _ (bd_app t₁ t₂) xs := by dsimp; simp [realize_bounded_term_eq] lemma realize_bounded_term_irrel' {S : Structure L} {n n'} {v₁ : dvector S n} {v₂ : dvector S n'} (h : ∀m (hn : m < n) (hn' : m < n'), v₁.nth m hn = v₂.nth m hn') {l} (t : bounded_preterm L n l) (t' : bounded_preterm L n' l) (ht : t.fst = t'.fst) (xs : dvector S l) : realize_bounded_term v₁ t xs = realize_bounded_term v₂ t' xs := begin induction t; cases t'; injection ht with ht₁ ht₂, { simp, cases t'_1; dsimp at ht₁, subst ht₁, exact h t.val t.2 t'_1_is_lt }, { subst ht₁, refl }, { simp [t_ih_t t'_t ht₁, t_ih_s t'_s ht₂] } end lemma realize_bounded_term_irrel {S : Structure L} {n} {v₁ : dvector S n} (t : bounded_term L n) (t' : closed_term L) (ht : t.fst = t'.fst) (xs : dvector S 0) : realize_bounded_term v₁ t xs = realize_closed_term S t' := by cases xs; exact realize_bounded_term_irrel' (by intros m hm hm'; exfalso; exact not_lt_zero m hm') t t' ht ([]) @[simp] def lift_bounded_term_at {n} : ∀{l} (t : bounded_preterm L n l) (n' m : ℕ), bounded_preterm L (n + n') l | _ &k n' m := if m ≤ k.1 then &(k.add_nat n') else &(k.cast_le $ n.le_add_right n') | _ (bd_func f) n' m := bd_func f | _ (bd_app t₁ t₂) n' m := bd_app (lift_bounded_term_at t₁ n' m) $ lift_bounded_term_at t₂ n' m notation t ` ↑' `:90 n ` # `:90 m:90 := fol.lift_bounded_term_at t n m -- input ↑ with \u or \upa @[reducible] def lift_bounded_term {n l} (t : bounded_preterm L n l) (n' : ℕ) : bounded_preterm L (n + n') l := t ↑' n' # 0 infix ` ↑ `:100 := fol.lift_bounded_term -- input ↑' with \u or \upa @[reducible, simp] def lift_bounded_term1 {n' l} (t : bounded_preterm L n' l) : bounded_preterm L (n'+1) l := t ↑ 1 @[simp] lemma lift_bounded_term_fst {n} : ∀{l} (t : bounded_preterm L n l) (n' m : ℕ), (t ↑' n' # m).fst = t.fst ↑' n' # m | _ &k n' m := by by_cases h : m ≤ k.1; simp [h, -add_comm]; refl | _ (bd_func f) n' m := by refl | _ (bd_app t₁ t₂) n' m := by simp [lift_bounded_term_fst] -- @[simp] def lift_closed_term_at : ∀{l} (t : closed_preterm L l) (n' m : ℕ), -- bounded_preterm L n' l -- | _ &k n' m := if m ≤ k then _ else &(k.cast_le $ n.le_add_right n') -- | _ (bd_func f) n' m := bd_func f -- | _ (bd_app t₁ t₂) n' m := bd_app (lift_bounded_term_at t₁ n' m) $ lift_bounded_term_at t₂ n' m -- def lift_bounded_term_at0 {n m l} {t : preterm L l} (ht : bounded_term 0 t) : bounded_term n (t ↑' n # m) := -- by have := lift_bounded_term_at n m ht; rw [zero_add] at this; exact this /-- this is t[s//n] for bounded formulae-/ def subst_bounded_term {n n'} : ∀{l} (t : bounded_preterm L (n+n'+1) l) (s : bounded_term L n'), bounded_preterm L (n+n') l | _ &k s := if h : k.1 < n then &⟨k.1, lt_of_lt_of_le h $ n.le_add_right n'⟩ else if h' : n < k.1 then &⟨k.1-1, (nat.sub_lt_right_iff_lt_add $ one_le_of_lt h').mpr k.2⟩ else (s ↑ n).cast $ le_of_eq $ add_comm n' n | _ (bd_func f) s := bd_func f | _ (bd_app t₁ t₂) s := bd_app (subst_bounded_term t₁ s) (subst_bounded_term t₂ s) notation t `[`:max s ` // `:95 n `]`:0 := @_root_.fol.subst_bounded_term _ n _ _ t s -- notation t `[`:95 s ` // `:95 n `]`:0 := @fol.subst_bounded_term _ n _ _ t s -- notation f `[`:95 s ` // `:95 n `]`:0 := @_root_.fol.subst_bounded_term @[simp] lemma subst_bounded_term_var_lt {n n'} (s : bounded_term L n') (k : fin (n+n'+1)) (h : k.1 < n) : (subst_bounded_term &k s).fst = &k.1 := by simp [h, fol.subst_bounded_term] @[simp] lemma subst_bounded_term_var_gt {n n'} (s : bounded_term L n') (k : fin (n+n'+1)) (h : n < k.1) : (subst_bounded_term &k s).fst = &(k.1-1) := have h' : ¬(k.1 < n), from lt_asymm h, by simp [h, h', fol.subst_bounded_term] @[simp] lemma subst_bounded_term_var_eq {n n'} (s : bounded_term L n') (k : fin (n+n'+1)) (h : k.1 = n) : (subst_bounded_term &k s).fst = s.fst ↑ n := have h₂ : ¬(k.1 < n), from λh', lt_irrefl _ $ lt_of_lt_of_le h' $ le_of_eq h.symm, have h₃ : ¬(n < k.1), from λh', lt_irrefl _ $ lt_of_lt_of_le h' $ le_of_eq h, by simp [subst_bounded_term, h₂, h₃] @[simp] lemma subst_bounded_term_bd_app {n n' l} (t₁ : bounded_preterm L (n+n'+1) (l+1)) (t₂ : bounded_term L (n+n'+1)) (s : bounded_term L n') : subst_bounded_term (bd_app t₁ t₂) s = bd_app (subst_bounded_term t₁ s) (subst_bounded_term t₂ s) := by refl @[simp] lemma subst_bounded_term_fst {n n'} : ∀{l} (t : bounded_preterm L (n+n'+1) l) (s : bounded_term L n'), (subst_bounded_term t s).fst = t.fst[s.fst//n] | _ &k s := by apply lt_by_cases k.1 n; intro h; simp [h] | _ (bd_func f) s := by refl | _ (bd_app t₁ t₂) s := by simp* -- @[simp] lemma subst_bounded_term_var_eq' {n n'} (s : bounded_term L n') (h : n < n+n'+1) : -- (subst_bounded_term &⟨n, h⟩ s).fst = s.fst ↑ n := -- by simp [subst_bounded_term] def subst0_bounded_term {n l} (t : bounded_preterm L (n+1) l) (s : bounded_term L n) : bounded_preterm L n l := (subst_bounded_term (t.cast_eq $ (n+1).zero_add.symm) s).cast_eq $ n.zero_add notation t `[`:max s ` /0]`:0 := fol.subst0_bounded_term t s @[simp] lemma subst0_bounded_term_fst {n l} (t : bounded_preterm L (n+1) l) (s : bounded_term L n) : t[s/0].fst = t.fst[s.fst//0] := by simp [subst0_bounded_term] def substmax_bounded_term {n l} (t : bounded_preterm L (n+1) l) (s : closed_term L) : bounded_preterm L n l := subst_bounded_term (by exact t) s @[simp] lemma substmax_bounded_term_bd_app {n l} (t₁ : bounded_preterm L (n+1) (l+1)) (t₂ : bounded_term L (n+1)) (s : closed_term L) : substmax_bounded_term (bd_app t₁ t₂) s = bd_app (substmax_bounded_term t₁ s) (substmax_bounded_term t₂ s) := by refl def substmax_eq_subst0_term {l} (t : bounded_preterm L 1 l) (s : closed_term L) : t[s/0] = substmax_bounded_term t s := by ext; simp [substmax_bounded_term] def substmax_var_lt {n} (k : fin (n+1)) (s : closed_term L) (h : k.1 < n) : substmax_bounded_term &k s = &⟨k.1, h⟩ := by ext; simp [substmax_bounded_term, h] def substmax_var_eq {n} (k : fin (n+1)) (s : closed_term L) (h : k.1 = n) : substmax_bounded_term &k s = s.cast0 n := begin ext, simp [substmax_bounded_term, h], dsimp only [lift_term], rw [lift_bounded_term_irrel s _ (le_refl _)] end def bounded_term_of_function {l n} (f : L.functions l) : arity' (bounded_term L n) (bounded_term L n) l := arity'.of_dvector_map $ bd_apps (bd_func f) @[simp] lemma realize_bounded_term_bd_app {S : Structure L} {n l} (t : bounded_preterm L n (l+1)) (s : bounded_term L n) (xs : dvector S n) (xs' : dvector S l) : realize_bounded_term xs (bd_app t s) xs' = realize_bounded_term xs t (realize_bounded_term xs s ([])::xs') := by refl @[simp] lemma realize_closed_term_bd_apps {S : Structure L} {l} (t : closed_preterm L l) (ts : dvector (closed_term L) l) : realize_closed_term S (bd_apps t ts) = realize_bounded_term ([]) t (ts.map (λt', realize_bounded_term ([]) t' ([]))) := begin induction ts generalizing t, refl, apply ts_ih (bd_app t ts_x) end --⟨t.fst[s.fst // n], bounded_term_subst_closed t.snd s.snd⟩ lemma realize_bounded_term_bd_apps {S : Structure L} {n l} (xs : dvector S n) (t : bounded_preterm L n l) (ts : dvector (bounded_term L n) l) : realize_bounded_term xs (bd_apps t ts) ([]) = realize_bounded_term xs t (ts.map (λt, realize_bounded_term xs t ([]))) := begin induction ts generalizing t, refl, apply ts_ih (bd_app t ts_x) end /- When realizing a closed term, we can replace the realizing dvector with [] -/ @[simp] lemma realize_closed_term_v_irrel {S : Structure L} {n} {v : dvector S n} {t : bounded_term L 0} : realize_bounded_term v (t.cast (by {simp})) ([]) = realize_closed_term S t := begin revert t, refine bounded_term.rec _ _, {intro k, cases k, exfalso, exact not_lt_zero k_val k_is_lt}, {intros, simp[realize_bounded_term_bd_apps], congr' 1, apply dvector.map_congr_pmem, intros x Hx, rw[ih_ts x Hx]} end /- this is the same as realize_bounded_term, we should probably have a common generalization of this definition -/ -- @[simp] def substitute_bounded_term {n n'} (v : dvector (bounded_term n') n) : -- ∀{l} (t : bounded_term L n l, bounded_preterm L n' l -- | _ _ &k := v.nth k hk -- | _ _ (bd_func f) := bd_func f -- | _ _ (bd_app t₁ t₂) := bd_app (substitute_bounded_term ht₁) $ substitute_bounded_term ht₂ -- def substitute_bounded_term {n n' l} (t : bounded_preterm L n l) -- (v : dvector (bounded_term n') n) : bounded_preterm L n' l := -- substitute_bounded_term v t.snd variable (L) inductive bounded_preformula : ℕ → ℕ → Type u | bd_falsum {} {n} : bounded_preformula n 0 | bd_equal {n} (t₁ t₂ : bounded_term L n) : bounded_preformula n 0 | bd_rel {n l : ℕ} (R : L.relations l) : bounded_preformula n l | bd_apprel {n l} (f : bounded_preformula n (l + 1)) (t : bounded_term L n) : bounded_preformula n l | bd_imp {n} (f₁ f₂ : bounded_preformula n 0) : bounded_preformula n 0 | bd_all {n} (f : bounded_preformula (n+1) 0) : bounded_preformula n 0 export bounded_preformula @[reducible] def bounded_formula (n : ℕ) := bounded_preformula L n 0 @[reducible] def presentence (l : ℕ) := bounded_preformula L 0 l @[reducible] def sentence := presentence L 0 variable {L} instance nonempty_bounded_formula (n : ℕ) : nonempty $ bounded_formula L n := nonempty.intro (by constructor) -- @[reducible, simp] def bd_falsum' {n} : bounded_formula L n := bd_falsum -- @[reducible, simp] def bd_equal' {n} (t₁ t₂ : bounded_term L n) : bounded_formula L n := -- bd_equal t₁ t₂ -- @[reducible, simp] def bd_imp' {n} (f₁ f₂ : bounded_formula L n) : bounded_formula L n := -- bd_imp f₁ f₂ notation `⊥` := fol.bounded_preformula.bd_falsum -- input: \bot infix ` ≃ `:88 := fol.bounded_preformula.bd_equal -- input \~- or \simeq infix ` ⟹ `:62 := fol.bounded_preformula.bd_imp -- input \==> def bd_not {n} (f : bounded_formula L n) : bounded_formula L n := f ⟹ ⊥ prefix `∼`:max := fol.bd_not -- input \~, the ASCII character ~ has too low precedence def bd_and {n} (f₁ f₂ : bounded_formula L n) : bounded_formula L n := ∼(f₁ ⟹ ∼f₂) infixr ` ⊓ ` := fol.bd_and -- input: \sqcap def bd_or {n} (f₁ f₂ : bounded_formula L n) : bounded_formula L n := ∼f₁ ⟹ f₂ infixr ` ⊔ ` := fol.bd_or -- input: \sqcup def bd_biimp {n} (f₁ f₂ : bounded_formula L n) : bounded_formula L n := (f₁ ⟹ f₂) ⊓ (f₂ ⟹ f₁) infix ` ⇔ `:61 := fol.bd_biimp -- input \<=> prefix `∀'`:110 := fol.bounded_preformula.bd_all def bd_ex {n} (f : bounded_formula L (n+1)) : bounded_formula L n := ∼ (∀' (∼ f)) prefix `∃'`:110 := fol.bd_ex def bd_apps_rel : ∀{n l} (f : bounded_preformula L n l) (ts : dvector (bounded_term L n) l), bounded_formula L n | _ _ f [] := f | _ _ f (t::ts) := bd_apps_rel (bd_apprel f t) ts @[simp] lemma bd_apps_rel_zero {n} (f : bounded_formula L n) (ts : dvector (bounded_term L n) 0) : bd_apps_rel f ts = f := by cases ts; refl namespace bounded_preformula @[simp] protected def fst : ∀{n l}, bounded_preformula L n l → preformula L l | _ _ bd_falsum := ⊥ | _ _ (t₁ ≃ t₂) := t₁.fst ≃ t₂.fst | _ _ (bd_rel R) := rel R | _ _ (bd_apprel f t) := apprel f.fst t.fst | _ _ (f₁ ⟹ f₂) := f₁.fst ⟹ f₂.fst | _ _ (∀' f) := ∀' f.fst @[simp] lemma fst_not : ∀{n} {f : bounded_formula L n}, ∼(bounded_preformula.fst f) = bounded_preformula.fst (∼f) := by {intros, refl} local attribute [extensionality] fin.eq_of_veq @[extensionality] protected def eq {n l} {f₁ f₂ : bounded_preformula L n l} (h : f₁.fst = f₂.fst) : f₁ = f₂ := begin induction f₁; cases f₂; injection h with h₁ h₂, { refl }, { congr1; apply bounded_preterm.eq; assumption }, { rw h₁ }, { congr1, exact f₁_ih h₁, exact bounded_preterm.eq h₂ }, { congr1, exact f₁_ih_f₁ h₁, exact f₁_ih_f₂ h₂ }, { rw [f₁_ih h₁] } end @[simp] protected def cast : ∀ {n m l} (h : n ≤ m) (f : bounded_preformula L n l), bounded_preformula L m l | _ _ _ h bd_falsum := bd_falsum | _ _ _ h (t₁ ≃ t₂) := t₁.cast h ≃ t₂.cast h | _ _ _ h (bd_rel R) := bd_rel R | _ _ _ h (bd_apprel f t) := bd_apprel (f.cast h) $ t.cast h | _ _ _ h (f₁ ⟹ f₂) := f₁.cast h ⟹ f₂.cast h | _ _ _ h (∀' f) := ∀' f.cast (succ_le_succ h) @[simp] lemma cast_irrel : ∀ {n m l} (h h' : n ≤ m) (f : bounded_preformula L n l), (f.cast h) = (f.cast h') := by {intros, refl} @[simp] lemma cast_rfl {n} {h : n ≤ n} : ∀ {l} (f : bounded_preformula L n l), (f.cast h) = f := by {intros, induction f; simp*} protected def cast_eq {n m l} (h : n = m) (f : bounded_preformula L n l) : bounded_preformula L m l := f.cast $ le_of_eq h protected def cast_eqr {n m l} (h : n = m) (f : bounded_preformula L m l) : bounded_preformula L n l := f.cast $ ge_of_eq h protected def cast1 {n l} (f : bounded_preformula L n l) : bounded_preformula L (n+1) l := f.cast $ n.le_add_right 1 @[simp] lemma cast_fst : ∀ {l n m} (h : n ≤ m) (f : bounded_preformula L n l), (f.cast h).fst = f.fst | _ _ _ h bd_falsum := by refl | _ _ _ h (t₁ ≃ t₂) := by simp | _ _ _ h (bd_rel R) := by refl | _ _ _ h (bd_apprel f t) := by simp* | _ _ _ h (f₁ ⟹ f₂) := by simp* | _ _ _ h (∀' f) := by simp* @[simp] lemma cast_eq_fst {l n m} (h : n = m) (f : bounded_preformula L n l) : (f.cast_eq h).fst = f.fst := f.cast_fst _ @[simp] lemma cast1_fst {l n} (f : bounded_preformula L n l) : f.cast1.fst = f.fst := f.cast_fst _ /- A bounded_preformula is qf if the underlying preformula is qf -/ def quantifier_free {l n} : bounded_preformula L n l → Prop := λ f, fol.quantifier_free f.fst end bounded_preformula namespace presentence @[reducible]protected def cast0 {l} (n) (f : presentence L l) : bounded_preformula L n l := f.cast n.zero_le @[simp] lemma cast0_fst {l} (n) (f : presentence L l) : (f.cast0 n).fst = f.fst := f.cast_fst _ end presentence lemma lift_bounded_formula_irrel : ∀{n l} (f : bounded_preformula L n l) (n') {m : ℕ} (h : n ≤ m), f.fst ↑' n' # m = f.fst | _ _ bd_falsum n' m h := by refl | _ _ (t₁ ≃ t₂) n' m h := by simp [lift_bounded_term_irrel _ _ h] | _ _ (bd_rel R) n' m h := by refl | _ _ (bd_apprel f t) n' m h := by simp [*, lift_bounded_term_irrel _ _ h] | _ _ (f₁ ⟹ f₂) n' m h := by simp* | _ _ (∀' f) n' m h := by simp* lemma lift_sentence_irrel (f : sentence L) : f.fst ↑ 1 = f.fst := lift_bounded_formula_irrel f 1 $ le_refl 0 lemma subst_bounded_formula_irrel : ∀{n l} (f : bounded_preformula L n l) {n'} (s : term L) (h : n ≤ n'), f.fst[s // n'] = f.fst | _ _ bd_falsum n' s h := by refl | _ _ (t₁ ≃ t₂) n' s h := by simp [subst_bounded_term_irrel _ s h] | _ _ (bd_rel R) n' s h := by refl | _ _ (bd_apprel f t) n' s h := by simp [*, subst_bounded_term_irrel _ s h] | _ _ (f₁ ⟹ f₂) n' s h := by simp* | _ _ (∀' f) n' s h := by simp* lemma subst_sentence_irrel (f : sentence L) (n) (s : term L) : f.fst[s // n] = f.fst := subst_bounded_formula_irrel f s n.zero_le @[simp] def realize_bounded_formula {S : Structure L} : ∀{n l} (v : dvector S n) (f : bounded_preformula L n l) (xs : dvector S l), Prop | _ _ v bd_falsum xs := false | _ _ v (t₁ ≃ t₂) xs := realize_bounded_term v t₁ xs = realize_bounded_term v t₂ xs | _ _ v (bd_rel R) xs := S.rel_map R xs | _ _ v (bd_apprel f t) xs := realize_bounded_formula v f $ realize_bounded_term v t ([])::xs | _ _ v (f₁ ⟹ f₂) xs := realize_bounded_formula v f₁ xs → realize_bounded_formula v f₂ xs | _ _ v (∀' f) xs := ∀(x : S), realize_bounded_formula (x::v) f xs notation S`[`:95 f ` ;; `:95 v `]`:0 := @fol.realize_bounded_formula _ S _ 0 v f (dvector.nil) @[reducible] def realize_sentence (S : Structure L) (f : sentence L) : Prop := realize_bounded_formula ([] : dvector S 0) f ([]) notation S`[`:max f `]`:0 := fol.realize_sentence S f lemma realize_bounded_formula_iff {S : Structure L} : ∀{n} {v₁ : dvector S n} {v₂ : ℕ → S} (hv : ∀k (hk : k < n), v₁.nth k hk = v₂ k) {l} (t : bounded_preformula L n l) (xs : dvector S l), realize_bounded_formula v₁ t xs ↔ realize_formula v₂ t.fst xs | _ _ _ hv _ bd_falsum xs := by refl | _ _ _ hv _ (t₁ ≃ t₂) xs := by apply eq.congr; apply realize_bounded_term_eq hv | _ _ _ hv _ (bd_rel R) xs := by refl | _ _ _ hv _ (bd_apprel f t) xs := by simp [realize_bounded_term_eq hv, realize_bounded_formula_iff hv] | _ _ _ hv _ (f₁ ⟹ f₂) xs := by simp [realize_bounded_formula_iff hv] | _ _ _ hv _ (∀' f) xs := begin apply forall_congr, intro x, apply realize_bounded_formula_iff, intros k hk, cases k, refl, apply hv end @[simp] def lift_bounded_formula_at : ∀{n l} (f : bounded_preformula L n l) (n' m : ℕ), bounded_preformula L (n + n') l | _ _ bd_falsum n' m := ⊥ | _ _ (t₁ ≃ t₂) n' m := t₁ ↑' n' # m ≃ t₂ ↑' n' # m | _ _ (bd_rel R) n' m := bd_rel R | _ _ (bd_apprel f t) n' m := bd_apprel (lift_bounded_formula_at f n' m) $ t ↑' n' # m | _ _ (f₁ ⟹ f₂) n' m := lift_bounded_formula_at f₁ n' m ⟹ lift_bounded_formula_at f₂ n' m | _ _ (∀' f) n' m := ∀' (lift_bounded_formula_at f n' (m+1)).cast (le_of_eq $ succ_add _ _) notation f ` ↑' `:90 n ` # `:90 m:90 := fol.lift_bounded_formula_at f n m -- input ↑ with \u or \upa @[reducible] def lift_bounded_formula {n l} (f : bounded_preformula L n l) (n' : ℕ) : bounded_preformula L (n + n') l := f ↑' n' # 0 infix ` ↑ `:100 := fol.lift_bounded_formula -- input ↑' with \u or \upa @[reducible, simp] def lift_bounded_formula1 {n' l} (f : bounded_preformula L n' l) : bounded_preformula L (n'+1) l := f ↑ 1 @[simp] lemma lift_bounded_formula_fst : ∀{n l} (f : bounded_preformula L n l) (n' m : ℕ), (f ↑' n' # m).fst = f.fst ↑' n' # m | _ _ bd_falsum n' m := by refl | _ _ (t₁ ≃ t₂) n' m := by simp | _ _ (bd_rel R) n' m := by refl | _ _ (bd_apprel f t) n' m := by simp* | _ _ (f₁ ⟹ f₂) n' m := by simp* | _ _ (∀' f) n' m := by simp* def formula_below {n n' l} (f : bounded_preformula L (n+n'+1) l) (s : bounded_term L n') : bounded_preformula L (n+n') l := begin have : {f' : preformula L l // f.fst = f' } := ⟨f.fst, rfl⟩, cases this with f' pf, induction f' generalizing n; cases f; injection pf with pf₁ pf₂, { exact ⊥ }, { exact subst_bounded_term f_t₁ s ≃ subst_bounded_term f_t₂ s }, { exact bd_rel f_R }, { exact bd_apprel (f'_ih f_f pf₁) (subst_bounded_term f_t s) }, { exact f'_ih_f₁ f_f₁ pf₁ ⟹ f'_ih_f₂ f_f₂ pf₂ }, { refine ∀' (f'_ih (f_f.cast_eq $ congr_arg succ $ (succ_add n n').symm) $ (f_f.cast_eq_fst _).trans pf₁).cast_eq (succ_add n n') } end /- f[s//n] for bounded_formula, requiring an extra proof that (n+n'+1 = n'') -/ @[simp] def subst_bounded_formula : ∀{n n' n'' l} (f : bounded_preformula L n'' l) (s : bounded_term L n') (h : n+n'+1 = n''), bounded_preformula L (n+n') l | _ _ _ _ bd_falsum s rfl := ⊥ | _ _ _ _ (t₁ ≃ t₂) s rfl := subst_bounded_term t₁ s ≃ subst_bounded_term t₂ s | _ _ _ _ (bd_rel R) s rfl := bd_rel R | _ _ _ _ (bd_apprel f t) s rfl := bd_apprel (subst_bounded_formula f s rfl) (subst_bounded_term t s) | _ _ _ _ (f₁ ⟹ f₂) s rfl := subst_bounded_formula f₁ s rfl ⟹ subst_bounded_formula f₂ s rfl | _ _ _ _ (∀' f) s rfl := ∀' (subst_bounded_formula f s $ by simp [succ_add]).cast_eq (succ_add _ _) notation f `[`:95 s ` // `:95 n ` // `:95 h `]`:0 := @fol.subst_bounded_formula _ n _ _ _ f s h @[simp] def subst_bounded_formula_fst : ∀{n n' n'' l} (f : bounded_preformula L n'' l) (s : bounded_term L n') (h : n+n'+1 = n''), (subst_bounded_formula f s h).fst = f.fst[s.fst//n] | _ _ _ _ bd_falsum s rfl := by refl | _ _ _ _ (t₁ ≃ t₂) s rfl := by simp | _ _ _ _ (bd_rel R) s rfl := by refl | _ _ _ _ (bd_apprel f t) s rfl := by simp* | _ _ _ _ (f₁ ⟹ f₂) s rfl := by simp* | _ _ _ _ (∀' f) s rfl := by simp* lemma realize_bounded_formula_irrel' {S : Structure L} {n n'} {v₁ : dvector S n} {v₂ : dvector S n'} (h : ∀m (hn : m < n) (hn' : m < n'), v₁.nth m hn = v₂.nth m hn') {l} (f : bounded_preformula L n l) (f' : bounded_preformula L n' l) (hf : f.fst = f'.fst) (xs : dvector S l) : realize_bounded_formula v₁ f xs ↔ realize_bounded_formula v₂ f' xs := begin induction f generalizing n'; cases f'; injection hf with hf₁ hf₂, { refl }, { simp [realize_bounded_term_irrel' h f_t₁ f'_t₁ hf₁, realize_bounded_term_irrel' h f_t₂ f'_t₂ hf₂] }, { rw [hf₁], refl }, { simp [realize_bounded_term_irrel' h f_t f'_t hf₂, f_ih _ h f'_f hf₁] }, { apply imp_congr, apply f_ih_f₁ _ h _ hf₁, apply f_ih_f₂ _ h _ hf₂ }, { apply forall_congr, intro x, apply f_ih _ _ _ hf₁, intros, cases m, refl, apply h } end lemma realize_bounded_formula_irrel {S : Structure L} {n} {v₁ : dvector S n} (f : bounded_formula L n) (f' : sentence L) (hf : f.fst = f'.fst) (xs : dvector S 0) : realize_bounded_formula v₁ f xs ↔ realize_sentence S f' := by cases xs; exact realize_bounded_formula_irrel' (by intros m hm hm'; exfalso; exact not_lt_zero m hm') f f' hf ([]) def bounded_formula_of_relation {l n} (f : L.relations l) : arity' (bounded_term L n) (bounded_formula L n) l := arity'.of_dvector_map $ bd_apps_rel (bd_rel f) @[elab_as_eliminator] def bounded_preformula.rec1 {C : Πn l, bounded_preformula L (n+1) l → Sort v} (H0 : Π {n}, C n 0 ⊥) (H1 : Π {n} (t₁ t₂ : bounded_term L (n+1)), C n 0 (t₁ ≃ t₂)) (H2 : Π {n l : ℕ} (R : L.relations l), C n l (bd_rel R)) (H3 : Π {n l : ℕ} (f : bounded_preformula L (n+1) (l + 1)) (t : bounded_term L (n+1)) (ih : C n (l + 1) f), C n l (bd_apprel f t)) (H4 : Π {n} (f₁ f₂ : bounded_formula L (n+1)) (ih₁ : C n 0 f₁) (ih₂ : C n 0 f₂), C n 0 (f₁ ⟹ f₂)) (H5 : Π {n} (f : bounded_formula L (n+2)) (ih : C (n+1) 0 f), C n 0 (∀' f)) : ∀{{n l : ℕ}} (f : bounded_preformula L (n+1) l), C n l f := let C' : Πn l, bounded_preformula L n l → Sort v := λn, match n with | 0 := λ l f, punit | (k+1) := C k end in begin have : ∀{{n l}} (f : bounded_preformula L n l), C' n l f, { intros n l, refine bounded_preformula.rec _ _ _ _ _ _; clear n l; intros; cases n; try {exact punit.star}, apply H0, apply H1, apply H2, apply H3 _ _ ih, apply H4 _ _ ih_f₁ ih_f₂, apply H5 _ ih }, intros n l f, apply this f end @[elab_as_eliminator] def bounded_formula.rec1 {C : Πn, bounded_formula L (n+1) → Sort v} (hfalsum : Π {n}, C n ⊥) (hequal : Π {n} (t₁ t₂ : bounded_term L (n+1)), C n (t₁ ≃ t₂)) (hrel : Π {n l : ℕ} (R : L.relations l) (ts : dvector (bounded_term L (n+1)) l), C n (bd_apps_rel (bd_rel R) ts)) (himp : Π {n} {f₁ f₂ : bounded_formula L (n+1)} (ih₁ : C n f₁) (ih₂ : C n f₂), C n (f₁ ⟹ f₂)) (hall : Π {n} {f : bounded_formula L (n+2)} (ih : C (n+1) f), C n (∀' f)) {{n : ℕ}} (f : bounded_formula L (n+1)) : C n f := have h : ∀{n l} (f : bounded_preformula L (n+1) l) (ts : dvector (bounded_term L (n+1)) l), C n (bd_apps_rel f ts), begin refine bounded_preformula.rec1 _ _ _ _ _ _; intros; try {rw ts.zero_eq}, apply hfalsum, apply hequal, apply hrel, apply ih (t::ts), exact himp (ih₁ ([])) (ih₂ ([])), exact hall (ih ([])) end, h f ([]) @[elab_as_eliminator] def bounded_formula.rec {C : Πn, bounded_formula L n → Sort v} (hfalsum : Π {n}, C n ⊥) (hequal : Π {n} (t₁ t₂ : bounded_term L n), C n (t₁ ≃ t₂)) (hrel : Π {n l : ℕ} (R : L.relations l) (ts : dvector (bounded_term L n) l), C n (bd_apps_rel (bd_rel R) ts)) (himp : Π {n} {f₁ f₂ : bounded_formula L n} (ih₁ : C n f₁) (ih₂ : C n f₂), C n (f₁ ⟹ f₂)) (hall : Π {n} {f : bounded_formula L (n+1)} (ih : C (n+1) f), C n (∀' f)) : ∀{{n : ℕ}} (f : bounded_formula L n), C n f := have h : ∀{n l} (f : bounded_preformula L n l) (ts : dvector (bounded_term L n) l), C n (bd_apps_rel f ts), begin intros, induction f; try {rw ts.zero_eq}, apply hfalsum, apply hequal, apply hrel, apply f_ih (f_t::ts), exact himp (f_ih_f₁ ([])) (f_ih_f₂ ([])), exact hall (f_ih ([])) end, λn f, h f ([]) @[simp] def substmax_bounded_formula {n l} (f : bounded_preformula L (n+1) l) (s : closed_term L) : bounded_preformula L n l := by apply subst_bounded_formula f s rfl -- @[simp] lemma substmax_bounded_formula_bd_falsum {n} (s : closed_term L) : -- substmax_bounded_formula (⊥ : bounded_formula L (n+1)) s = ⊥ := by refl -- @[simp] lemma substmax_bounded_formula_bd_rel {n l} (R : L.relations l) (s : closed_term L) : -- substmax_bounded_formula (bd_rel R : bounded_preformula L (n+1) l) s = bd_rel R := by refl -- @[simp] lemma substmax_bounded_formula_bd_apprel {n l} (f : bounded_preformula L (n+1) (l+1)) -- (t : bounded_term L (n+1)) (s : closed_term L) : -- substmax_bounded_formula (bd_apprel f t) s = -- bd_apprel (substmax_bounded_formula f s) (substmax_bounded_term t s) := by refl -- @[simp] lemma substmax_bounded_formula_bd_imp {n} (f₁ f₂ : bounded_formula L (n+1)) -- (s : closed_term L) : -- substmax_bounded_formula (f₁ ⟹ f₂) s = -- substmax_bounded_formula f₁ s ⟹ substmax_bounded_formula f₂ s := by refl @[simp] lemma substmax_bounded_formula_bd_all {n} (f : bounded_formula L (n+2)) (s : closed_term L) : substmax_bounded_formula (∀' f) s = ∀' substmax_bounded_formula f s := by ext; simp lemma substmax_bounded_formula_bd_apps_rel {n l} (f : bounded_preformula L (n+1) l) (t : closed_term L) (ts : dvector (bounded_term L (n+1)) l) : substmax_bounded_formula (bd_apps_rel f ts) t = bd_apps_rel (substmax_bounded_formula f t) (ts.map $ λt', substmax_bounded_term t' t) := begin induction ts generalizing f, refl, apply ts_ih (bd_apprel f ts_x) end def subst0_bounded_formula {n l} (f : bounded_preformula L (n+1) l) (s : bounded_term L n) : bounded_preformula L n l := (subst_bounded_formula f s $ zero_add (n+1)).cast_eq $ zero_add n notation f `[`:max s ` /0]`:0 := fol.subst0_bounded_formula f s @[simp] lemma subst0_bounded_formula_fst {n l} (f : bounded_preformula L (n+1) l) (s : bounded_term L n) : (subst0_bounded_formula f s).fst = f.fst[s.fst//0] := by simp [subst0_bounded_formula] def substmax_eq_subst0_formula {l} (f : bounded_preformula L 1 l) (t : closed_term L) : f[t/0] = substmax_bounded_formula f t := by ext; simp [substmax_bounded_formula] -- def subst0_sentence {n l} (f : bounded_preformula L (n+1) l) (t : closed_term L) : -- bounded_preformula L n l := -- f [bounded_term_of_closed_term t/0] infix ` ⊨ `:51 := fol.realize_sentence -- input using \|= or \vDash, but not using \models @[simp] lemma realize_sentence_false {S : Structure L} : S ⊨ (⊥ : sentence L) ↔ false := by refl @[simp]lemma false_of_satisfied_false {S : Structure L} : (S ⊨ (⊥ : sentence L)) → false := by simp only [realize_sentence_false, imp_self] @[simp] lemma realize_sentence_imp {S : Structure L} {f₁ f₂ : sentence L} : S ⊨ f₁ ⟹ f₂ ↔ (S ⊨ f₁ → S ⊨ f₂) := by refl @[simp] lemma realize_sentence_not {S : Structure L} {f : sentence L} : S ⊨ ∼f ↔ ¬ S ⊨ f := by refl @[simp] lemma realize_sentence_dne {S : Structure L} {f : sentence L} : S ⊨ ∼∼f ↔ S ⊨ f := begin refine ⟨by apply classical.by_contradiction, _⟩, finish end @[simp] lemma realize_sentence_all {S : Structure L} {f : bounded_formula L 1} : (S ⊨ ∀'f) ↔ ∀ x : S, realize_bounded_formula([x]) f([]) := by refl @[simp]lemma realize_bounded_formula_and {L} {S : Structure L} : ∀{n} {v : dvector S n} {f g : bounded_formula L n}, realize_bounded_formula v (f ⊓ g) dvector.nil ↔ (realize_bounded_formula v f dvector.nil ∧ realize_bounded_formula v g dvector.nil) := begin intros, have : realize_bounded_formula v f dvector.nil ∧ realize_bounded_formula v g dvector.nil ↔ ¬(realize_bounded_formula v f dvector.nil → ¬ (realize_bounded_formula v g dvector.nil)), by finish, rw[this], refl end @[simp]lemma realize_bounded_formula_not {L} {S : Structure L} : ∀{n} {v : dvector S n} {f : bounded_formula L n}, realize_bounded_formula v ∼f dvector.nil ↔ ¬(realize_bounded_formula v f dvector.nil) := by {intros, refl} @[simp] def realize_bounded_formula_ex {L} {S : Structure L} : ∀ {n} {v : dvector S n} {f : bounded_formula L (n+1)}, realize_bounded_formula v (∃' f) dvector.nil ↔ ∃ x, realize_bounded_formula (x::v) f dvector.nil := by {intros, unfold bd_ex, simp [realize_bounded_formula_not], finish} @[simp] lemma realize_sentence_ex {S : Structure L} {f : bounded_formula L 1} : S ⊨ ∃' f ↔ ∃ x : S, realize_bounded_formula ([x]) f([]) := by {unfold realize_sentence, apply realize_bounded_formula_ex} @[simp] lemma realize_sentence_and {S : Structure L} {f₁ f₂ : sentence L} : S ⊨ f₁ ⊓ f₂ ↔ (S ⊨ f₁ ∧ S ⊨ f₂) := by apply realize_bounded_formula_and lemma realize_bounded_formula_bd_apps_rel {S : Structure L} {n l} (xs : dvector S n) (f : bounded_preformula L n l) (ts : dvector (bounded_term L n) l) : realize_bounded_formula xs (bd_apps_rel f ts) ([]) ↔ realize_bounded_formula xs f (ts.map (λt, realize_bounded_term xs t ([]))) := begin induction ts generalizing f, refl, apply ts_ih (bd_apprel f ts_x) end lemma realize_sentence_bd_apps_rel' {S : Structure L} {l} (f : presentence L l) (ts : dvector (closed_term L) l) : S ⊨ bd_apps_rel f ts ↔ realize_bounded_formula ([]) f (ts.map $ realize_closed_term S) := realize_bounded_formula_bd_apps_rel ([]) f ts lemma realize_bd_apps_rel {S : Structure L} {l} (R : L.relations l) (ts : dvector (closed_term L) l) : S ⊨ bd_apps_rel (bd_rel R) ts ↔ S.rel_map R (ts.map $ realize_closed_term S) := by apply realize_bounded_formula_bd_apps_rel ([]) (bd_rel R) ts lemma realize_sentence_equal {S : Structure L} (t₁ t₂ : closed_term L) : S ⊨ t₁ ≃ t₂ ↔ realize_closed_term S t₁ = realize_closed_term S t₂ := by refl lemma realize_sentence_iff {S : Structure L} (v : ℕ → S) (f : sentence L) : realize_sentence S f ↔ realize_formula v f.fst ([]) := realize_bounded_formula_iff (λk hk, by exfalso; exact not_lt_zero k hk) f _ lemma realize_sentence_of_satisfied_in {S : Structure L} [HS : nonempty S] {f : sentence L} (H : S ⊨ f.fst) : S ⊨ f := begin unfreezeI, induction HS with x, exact (realize_sentence_iff (λn, x) f).mpr (H _) end lemma satisfied_in_of_realize_sentence {S : Structure L} {f : sentence L} (H : S ⊨ f) : S ⊨ f.fst := λv, (realize_sentence_iff v f).mp H lemma realize_sentence_iff_satisfied_in {S : Structure L} [HS : nonempty S] {f : sentence L} : S ⊨ f ↔ S ⊨ f.fst := ⟨satisfied_in_of_realize_sentence, realize_sentence_of_satisfied_in⟩ def dvector_var_lift {m : ℕ} : ∀ {n : ℕ} (v : dvector (fin m) n), dvector (fin (m+1)) n | _ [] := [] | _ (x::xs) := (⟨x.val+1, by{apply nat.succ_lt_succ, exact x.is_lt}⟩) :: (dvector_var_lift xs) def dvector_lift_var { m : ℕ} : ∀ {n : ℕ} (v : dvector (fin m) n), dvector (fin (m+1)) (n+1) := λ n v, ⟨0, nat.zero_lt_succ(m)⟩ :: (dvector_var_lift v) def subst_var_bounded_term {n m : ℕ} : ∀ {l : ℕ}, bounded_preterm L n l → dvector (fin m) n → bounded_preterm L m l | _ (&k) v := &(dvector.nth v (k.val) k.is_lt) | _ (bd_func f) v := bd_func f | _ (bd_app t s) v := bd_app (subst_var_bounded_term t v) (subst_var_bounded_term s v) /-What are eqn_compiler lemmas and why don't they generate for this defn? Need to fix this to use defn for simp-/ --set_option trace.debug.eqn_compiler true set_option eqn_compiler.lemmas false def subst_var_bounded_formula: ∀ {l n m: ℕ}, bounded_preformula L n l → dvector (fin (m)) n → bounded_preformula L (m) l | _ _ _ bd_falsum _ := bd_falsum | _ _ _ (t₁ ≃ t₂) v := (subst_var_bounded_term t₁ v) ≃ (subst_var_bounded_term t₁ v) | _ _ _ (bd_rel R) _ := bd_rel R | _ _ _ (bd_apprel f t) v := bd_apprel (subst_var_bounded_formula f v) (subst_var_bounded_term t v) | _ _ _ (f₁ ⟹ f₂) v := (subst_var_bounded_formula f₁ v) ⟹ (subst_var_bounded_formula f₂ v) | _ _ _ (∀' f) v := ∀' (subst_var_bounded_formula f (dvector_lift_var v)) set_option eqn_compiler.lemmas true infix ` ⊚ `:50 := subst_var_bounded_formula --type with \oo /- theories -/ variable (L) @[reducible] def Theory := set $ sentence L variable {L} @[reducible] def Theory.fst (T : Theory L) : set (formula L) := bounded_preformula.fst '' T lemma lift_Theory_irrel (T : Theory L) : (lift_formula1 '' Theory.fst T) = Theory.fst T := by rw[image_image, image_congr' lift_sentence_irrel] def sprf (T : Theory L) (f : sentence L) := T.fst ⊢ f.fst infix ` ⊢ `:51 := fol.sprf -- input: \|- or \vdash def sprovable (T : Theory L) (f : sentence L) := T.fst ⊢' f.fst infix ` ⊢' `:51 := fol.sprovable -- input: \|- or \vdash def saxm {T : Theory L} {A : sentence L} (H : A ∈ T) : T ⊢ A := by apply axm; apply mem_image_of_mem _ H def saxm1 {T : Theory L} {A : sentence L} : insert A T ⊢ A := by apply saxm; left; refl def saxm2 {T : Theory L} {A B : sentence L} : insert A (insert B T) ⊢ B := by apply saxm; right; left; refl def simpI {T : Theory L} {A B : sentence L} (H : insert A T ⊢ B) : T ⊢ A ⟹ B := begin apply impI, simp[sprf, Theory.fst, image_insert_eq] at H, assumption end lemma simpI' {T : Theory L} {A B : sentence L} (H : insert A T ⊢' B) : T ⊢' A ⟹ B := H.map simpI def simpE {T : Theory L} (A : sentence L) {B : sentence L} (H₁ : T ⊢ A ⟹ B) (H₂ : T ⊢ A) : T ⊢ B := by apply impE A.fst H₁ H₂ lemma fst_commutes_with_imp {T : Theory L} (A B : sentence L) : (A ⟹ B).fst = A.fst ⟹ B.fst := by refl def sfalsumE {T : Theory L} {A : sentence L} (H : insert ∼A T ⊢ bd_falsum) : T ⊢ A := begin apply falsumE, simp[sprf, Theory.fst, image_insert_eq] at H, assumption end def snotI {T : Theory L} {A : sentence L} (H : T ⊢ A ⟹ bd_falsum) : T ⊢ ∼A := begin apply notI, simp[sprf, Theory.fst, image_insert_eq] at H, assumption end def sandI {T : Theory L} {A B : sentence L} (H1 : T ⊢ A) (H2 : T ⊢ B) : T ⊢ A ⊓ B := by exact andI H1 H2 lemma sandI' {T : Theory L} {A B : sentence L} (H1 : T ⊢' A) (H2 : T ⊢' B) : T ⊢' A ⊓ B := begin apply @nonempty.map ((T ⊢ A) × (T ⊢ B)) (T ⊢ A ⊓ B), intro H, cases H, apply sandI, repeat{assumption}, simp only [nonempty_prod], apply and.intro, exact H1, exact H2 end def snot_and_self {T : Theory L} {A : sentence L} (H : T ⊢ A ⊓ ∼ A) : T ⊢ bd_falsum := by exact not_and_self H lemma snot_and_self' {T : Theory L} {A : sentence L} (H : T ⊢' A ⊓ ∼A) : T ⊢' bd_falsum := by {apply nonempty.map _ H, apply snot_and_self} lemma snot_and_self'' {T : Theory L} {A : sentence L} (H₁ : T ⊢' A) (H₂ : T ⊢' ∼A) : T ⊢' bd_falsum := snot_and_self' $ sandI' H₁ H₂ lemma sprf_by_cases {Γ} (f₁) {f₂ : sentence L} (H₁ : insert f₁ Γ ⊢' f₂) (H₂ : insert ∼f₁ Γ ⊢' f₂) : Γ ⊢' f₂ := begin simp [sprovable, Theory.fst, set.image_insert_eq] at H₁ H₂, exact prf_by_cases f₁.fst H₁ H₂ end def double_negation_elim {L} {T : Theory L} {f : sentence L} : T ⊢ ∼∼f → T ⊢ f := begin intro, apply falsumE, apply impE, show preformula L 0, by exact f.fst, apply deduction, apply impI, apply axm1, apply exfalso, exact deduction a end @[simp]lemma double_negation_elim' {L} {T : Theory L} {f : sentence L} : T ⊢' ∼∼f ↔ T ⊢' f := begin apply nonempty.iff, exact double_negation_elim, intro P, apply impI, apply @not_and_self _ _ f.fst, apply andI, exact weakening1 P, apply axm, simp end def sweakening {T T' : Theory L} (h_sub : T' ⊆ T) {ψ : sentence L} (h : T' ⊢ ψ) : T ⊢ ψ := weakening (image_subset _ h_sub) h def sweakening1 {T : Theory L} {ψ₁ ψ₂ : sentence L} (h : T ⊢ ψ₂) : insert ψ₁ T ⊢ ψ₂ := sweakening (subset_insert ψ₁ T) h def sweakening2 {T : Theory L} {ψ₁ ψ₂ ψ₃ : sentence L} (h : insert ψ₁ T ⊢ ψ₃) : insert ψ₁ (insert ψ₂ T) ⊢ ψ₃ := sweakening (insert_subset_insert (subset_insert _ T)) h def sprovable_of_provable {T : Theory L} {f : sentence L} (h : T.fst ⊢ f.fst) : T ⊢ f := h def provable_of_sprovable {T : Theory L} {f : sentence L} (h : T ⊢ f) : T.fst ⊢ f.fst := h def sprovable_of_sprf {T : Theory L} {f : sentence L} (h : T ⊢ f) : T ⊢' f := ⟨h⟩ def sprovable.elim {P : Prop} {T : Theory L} {f : sentence L} (ih : T ⊢ f → P) (h : T ⊢' f) : P := by unfreezeI; cases h with h; exact ih h -- def sprovable_of_sprovable_lift_at {T : Theory L} (n m : ℕ) {f : formula L} (h : T.fst ⊢ f ↑' n # m) : -- T.fst ⊢ f := -- sorry -- def sprovable_of_sprovable_lift {T : Theory L} {f : formula L} (h : T.fst ⊢ f ↑ 1) : T.fst ⊢ f := -- sprovable_of_sprovable_lift_at 1 0 h def sprovable_lift {T : Theory L} {f : formula L} (h : T.fst ⊢ f) : T.fst ⊢ f ↑ 1 := begin have := prf_lift 1 0 h, dsimp [Theory.fst] at this, rw [image_image, image_congr' lift_sentence_irrel] at this, exact this end def all_sprovable (T T' : Theory L) := ∀(f ∈ T'), T ⊢ f infix ` ⊢ `:51 := fol.all_sprovable -- input: \|- or \vdash def all_realize_sentence (S : Structure L) (T : Theory L) := ∀{{f}}, f ∈ T → S ⊨ f infix ` ⊨ `:51 := fol.all_realize_sentence -- input using \|= or \vDash, but not using \models lemma all_realize_sentence_axm {S : Structure L} {f : sentence L} {T : Theory L} : ∀ (H : S ⊨ insert f T), S ⊨ f ∧ S ⊨ T := λ H, ⟨by {apply H, exact or.inl rfl}, by {intros ψ hψ, apply H, exact or.inr hψ}⟩ @[simp]lemma all_realize_sentence_axm_rw {S : Structure L} {f : sentence L} {T : Theory L} : (S ⊨ insert f T) ↔ S ⊨ f ∧ S ⊨ T := begin refine ⟨by apply all_realize_sentence_axm, _⟩, intro H, rcases H with ⟨Hf, HT⟩, intros g Hg, rcases Hg with ⟨Hg1, Hg2⟩, exact Hf, exact HT Hg end @[simp]lemma all_realize_sentence_singleton {S : Structure L} {f : sentence L} : S ⊨ {f} ↔ S ⊨ f := ⟨by{intro H, apply H, exact or.inl rfl}, by {intros H g Hg, repeat{cases Hg}, assumption}⟩ def ssatisfied (T : Theory L) (f : sentence L) := ∀{{S : Structure L}}, nonempty S → S ⊨ T → S ⊨ f infix ` ⊨ `:51 := fol.ssatisfied -- input using \|= or \vDash, but not using \models def all_ssatisfied (T T' : Theory L) := ∀(f ∈ T'), T ⊨ f infix ` ⊨ `:51 := fol.all_ssatisfied -- input using \|= or \vDash, but not using \models def satisfied_of_ssatisfied {T : Theory L} {f : sentence L} (H : T ⊨ f) : T.fst ⊨ f.fst := begin intros S v hT, rw [←realize_sentence_iff], apply H ⟨ v 0 ⟩, intros f' hf', rw [realize_sentence_iff v], apply hT, apply mem_image_of_mem _ hf' end def ssatisfied_of_satisfied {T : Theory L} {f : sentence L} (H : T.fst ⊨ f.fst) : T ⊨ f := begin intros S hS hT, induction hS with s, rw [realize_sentence_iff (λ_, s)], apply H, intros f' hf', rcases hf' with ⟨f', ⟨hf', h⟩⟩, induction h, rw [←realize_sentence_iff], exact hT hf' end def all_satisfied_of_all_ssatisfied {T T' : Theory L} (H : T ⊨ T') : T.fst ⊨ T'.fst := begin intros f hf, rcases hf with ⟨f, ⟨hf, rfl⟩⟩, apply satisfied_of_ssatisfied (H f hf) end def all_ssatisfied_of_all_satisfied {T T' : Theory L} (H : T.fst ⊨ T'.fst) : T ⊨ T' := begin intros f hf, apply ssatisfied_of_satisfied, apply H, exact mem_image_of_mem _ hf end def satisfied_iff_ssatisfied {T : Theory L} {f : sentence L} : T ⊨ f ↔ T.fst ⊨ f.fst := ⟨satisfied_of_ssatisfied, ssatisfied_of_satisfied⟩ def all_satisfied_sentences_iff {T T' : Theory L} : T ⊨ T' ↔ T.fst ⊨ T'.fst := ⟨all_satisfied_of_all_ssatisfied, all_ssatisfied_of_all_satisfied⟩ def ssatisfied_snot {S : Structure L} {f : sentence L} (hS : ¬(S ⊨ f)) : S ⊨ ∼ f := by exact hS def Model (T : Theory L) : Type (u+1) := Σ' (S : Structure L), S ⊨ T @[reducible]def Model_ssatisfied {T : Theory L} (M : Model T) (ψ : sentence L) := M.fst ⊨ ψ infix ` ⊨ `:51 := fol.Model_ssatisfied -- input using \|= or \vDash, but not using \models @[simp] lemma false_of_Model_absurd {T : Theory L} (M : Model T) {ψ : sentence L} (h : M ⊨ ψ) (h' : M ⊨ ∼ψ) : false := by {unfold Model_ssatisfied at *, simp[*,-h'] at h', exact h'} lemma soundness {T : Theory L} {A : sentence L} (H : T ⊢ A) : T ⊨ A := ssatisfied_of_satisfied $ formula_soundness H /-- Given a model M ⊨ T with M ⊨ ¬ ψ, ¬ T ⊨ ψ--/ @[simp]lemma not_satisfied_of_model_not {T : Theory L} {ψ : sentence L} (M : Model T) (hM : M ⊨ ∼ψ) (h_nonempty : nonempty M.fst): ¬ T ⊨ ψ := begin intro H, suffices : M ⊨ ψ, exact false_of_Model_absurd M this hM, exact H h_nonempty M.snd end --infix ` ⊨ `:51 := fol.ssatisfied -- input using \|= or \vDash, but not using \models /- consistent theories -/ def is_consistent (T : Theory L) := ¬(T ⊢' (⊥ : sentence L)) protected def is_consistent.intro {T : Theory L} (H : ¬ T ⊢' (⊥ : sentence L)) : is_consistent T := H protected def is_consistent.elim {T : Theory L} (H : is_consistent T) : ¬ T ⊢' (⊥ : sentence L) | H' := H H' lemma consis_not_of_not_provable {L} {T : Theory L} {f : sentence L} : ¬ T ⊢' f → is_consistent (T ∪ {∼f}) := begin intros h₁ h₂, cases h₂ with h₂, simp only [*, set.union_singleton] at h₂, apply h₁, exact ⟨sfalsumE h₂⟩ end /- complete theories -/ def is_complete (T : Theory L) := is_consistent T ∧ ∀(f : sentence L), f ∈ T ∨ ∼ f ∈ T def mem_of_sprf {T : Theory L} (H : is_complete T) {f : sentence L} (Hf : T ⊢ f) : f ∈ T := begin cases H.2 f, exact h, exfalso, apply H.1, constructor, refine impE _ _ Hf, apply saxm h end def mem_of_sprovable {T : Theory L} (H : is_complete T) {f : sentence L} (Hf : T ⊢' f) : f ∈ T := by destruct Hf; exact mem_of_sprf H def sprovable_of_sprovable_or {T : Theory L} (H : is_complete T) {f₁ f₂ : sentence L} (H₂ : T ⊢' f₁ ⊔ f₂) : (T ⊢' f₁) ∨ T ⊢' f₂ := begin cases H.2 f₁ with h h, { left, exact ⟨saxm h⟩ }, cases H.2 f₂ with h' h', { right, exact ⟨saxm h'⟩ }, exfalso, destruct H₂, intro H₂, apply H.1, constructor, apply orE H₂; refine impE _ _ axm1; apply weakening1; apply axm; [exact mem_image_of_mem _ h, exact mem_image_of_mem _ h'] end def impI_of_is_complete {T : Theory L} (H : is_complete T) {f₁ f₂ : sentence L} (H₂ : T ⊢' f₁ → T ⊢' f₂) : T ⊢' f₁ ⟹ f₂ := begin apply impI', cases H.2 f₁, { apply weakening1', apply H₂, exact ⟨saxm h⟩ }, apply falsumE', apply weakening1', apply impE' _ (weakening1' ⟨by apply saxm h⟩) ⟨axm1⟩ end def notI_of_is_complete {T : Theory L} (H : is_complete T) {f : sentence L} (H₂ : ¬T ⊢' f) : T ⊢' ∼f := begin apply @impI_of_is_complete _ T H f ⊥, intro h, exfalso, exact H₂ h end def has_enough_constants (T : Theory L) := ∃(C : Π(f : bounded_formula L 1), L.constants), ∀(f : bounded_formula L 1), T ⊢' ∃' f ⟹ f[bd_const (C f)/0] lemma has_enough_constants.intro {L : Language} (T : Theory L) (H : ∀(f : bounded_formula L 1), ∃ c : L.constants, T ⊢' ∃' f ⟹ f[bd_const c/0]) : has_enough_constants T := classical.axiom_of_choice H def find_counterexample_of_henkin {T : Theory L} (H₁ : is_complete T) (H₂ : has_enough_constants T) (f : bounded_formula L 1) (H₃ : ¬ T ⊢' ∀' f) : ∃(t : closed_term L), T ⊢' ∼ f[t/0] := begin induction H₂ with C HC, refine ⟨bd_const (C (∼ f)), _⟩, dsimp [sprovable] at HC, apply (HC _).map2 (impE _), apply nonempty.map ex_not_of_not_all, apply notI_of_is_complete H₁ H₃ end variables (T : Theory L) (H₁ : is_complete T) (H₂ : has_enough_constants T) def term_rel (t₁ t₂ : closed_term L) : Prop := T ⊢' t₁ ≃ t₂ def term_setoid : setoid $ closed_term L := ⟨term_rel T, λt, ⟨ref _ _⟩, λt t' H, H.map symm, λt₁ t₂ t₃ H₁ H₂, H₁.map2 trans H₂⟩ local attribute [instance] term_setoid def term_model' : Type u := quotient $ term_setoid T -- set_option pp.all true -- #print term_setoid -- set_option trace.class_instances true def term_model_fun' {l} (t : closed_preterm L l) (ts : dvector (closed_term L) l) : term_model' T := @quotient.mk _ (term_setoid T) $ bd_apps t ts -- def equal_preterms_trans {T : set (formula L)} : ∀{l} {t₁ t₂ t₃ : preterm L l} -- (h₁₂ : equal_preterms T t₁ t₂) (h₂₃ : equal_preterms T t₂ t₃), equal_preterms T t₁ t₃ variable {T} def term_model_fun_eq {l} (t t' : closed_preterm L (l+1)) (x x' : closed_term L) (Ht : equal_preterms T.fst t.fst t'.fst) (Hx : T ⊢ x ≃ x') (ts : dvector (closed_term L) l) : term_model_fun' T (bd_app t x) ts = term_model_fun' T (bd_app t' x') ts := begin induction ts generalizing x x', { apply quotient.sound, refine ⟨trans (app_congr t.fst Hx) _⟩, apply Ht ([x'.fst]) }, { apply ts_ih, apply equal_preterms_app Ht Hx, apply ref } end variable (T) def term_model_fun {l} (t : closed_preterm L l) (ts : dvector (term_model' T) l) : term_model' T := begin refine ts.quotient_lift (term_model_fun' T t) _, clear ts, intros ts ts' hts, induction hts, { refl }, { apply (hts_ih _).trans, induction hts_hx with h, apply term_model_fun_eq, refl, exact h } end def term_model_rel' {l} (f : presentence L l) (ts : dvector (closed_term L) l) : Prop := T ⊢' bd_apps_rel f ts variable {T} def term_model_rel_iff {l} (f f' : presentence L (l+1)) (x x' : closed_term L) (Ht : equiv_preformulae T.fst f.fst f'.fst) (Hx : T ⊢ x ≃ x') (ts : dvector (closed_term L) l) : term_model_rel' T (bd_apprel f x) ts ↔ term_model_rel' T (bd_apprel f' x') ts := begin induction ts generalizing x x', { apply iff.trans (apprel_congr' f.fst Hx), apply iff_of_biimp, have := Ht ([x'.fst]), exact ⟨this⟩ }, { apply ts_ih, apply equiv_preformulae_apprel Ht Hx, apply ref } end variable (T) def term_model_rel {l} (f : presentence L l) (ts : dvector (term_model' T) l) : Prop := begin refine ts.quotient_lift (term_model_rel' T f) _, clear ts, intros ts ts' hts, induction hts, { refl }, { apply (hts_ih _).trans, induction hts_hx with h, apply propext, apply term_model_rel_iff, refl, exact h } end def term_model : Structure L := ⟨term_model' T, λn, term_model_fun T ∘ bd_func, λn, term_model_rel T ∘ bd_rel⟩ @[reducible] def term_mk : closed_term L → term_model T := @quotient.mk _ $ term_setoid T -- lemma realize_bounded_preterm_term_model {l n} (ts : dvector (closed_term L) l) -- (t : bounded_preterm L l n) (ts' : dvector (closed_term L) n) : -- realize_bounded_term (ts.map term_mk) t (ts'.map term_mk) = -- (term_mk _) := -- begin -- induction t with t ht, -- sorry -- end variable {T} lemma realize_closed_preterm_term_model {l} (ts : dvector (closed_term L) l) (t : closed_preterm L l) : realize_bounded_term ([]) t (ts.map $ term_mk T) = (term_mk T (bd_apps t ts)) := begin induction t, { apply t.fin_zero_elim }, { apply dvector.quotient_beta }, { rw [realize_bounded_term_bd_app], have := t_ih_s ([]), dsimp at this, rw this, apply t_ih_t (t_s::ts) } end @[simp] lemma realize_closed_term_term_model (t : closed_term L) : realize_closed_term (term_model T) t = term_mk T t := by apply realize_closed_preterm_term_model ([]) t /- below we try to do this directly using bounded_term.rec -/ -- begin -- revert t, refine bounded_term.rec _ _; intros, -- { apply k.fin_zero_elim }, -- --{ apply dvector.quotient_beta }, -- { -- --exact dvector.quotient_beta _ _ ts, -- rw [realize_bounded_term_bd_app], -- have := t_ih_s ([]), dsimp at this, rw this, -- apply t_ih_t (t_s::ts) } -- end lemma realize_subst_preterm {S : Structure L} {n l} (t : bounded_preterm L (n+1) l) (xs : dvector S l) (s : closed_term L) (v : dvector S n) : realize_bounded_term v (substmax_bounded_term t s) xs = realize_bounded_term (v.concat (realize_closed_term S s)) t xs := begin induction t, { by_cases h : t.1 < n, { rw [substmax_var_lt t s h], dsimp, simp only [dvector.map_nth, dvector.concat_nth _ _ _ _ h, dvector.nth'], }, { have h' := le_antisymm (le_of_lt_succ t.2) (le_of_not_gt h), simp [h', dvector.nth'], rw [substmax_var_eq t s h'], apply realize_bounded_term_irrel, simp }}, { refl }, { dsimp, rw [substmax_bounded_term_bd_app], dsimp, rw [t_ih_s ([]), t_ih_t] } end lemma realize_subst_term {S : Structure L} {n} (v : dvector S n) (s : closed_term L) (t : bounded_term L (n+1)) : realize_bounded_term v (substmax_bounded_term t s) ([]) = realize_bounded_term (v.concat (realize_closed_term S s)) t ([]) := by apply realize_subst_preterm t ([]) s v lemma realize_subst_formula (S : Structure L) {n} (f : bounded_formula L (n+1)) (t : closed_term L) (v : dvector S n) : realize_bounded_formula v (substmax_bounded_formula f t) ([]) ↔ realize_bounded_formula (v.concat (realize_closed_term S t)) f ([]) := begin revert n f v, refine bounded_formula.rec1 _ _ _ _ _; intros, { simp }, { apply eq.congr, exact realize_subst_term v t t₁, exact realize_subst_term v t t₂ }, { rw [substmax_bounded_formula_bd_apps_rel, realize_bounded_formula_bd_apps_rel, realize_bounded_formula_bd_apps_rel], simp [ts.map_congr (realize_subst_term _ _)] }, { apply imp_congr, apply ih₁ v, apply ih₂ v }, { simp, apply forall_congr, intro x, apply ih (x::v) } end lemma realize_subst_formula0 (S : Structure L) (f : bounded_formula L 1) (t : closed_term L) : S ⊨ f[t/0] ↔ realize_bounded_formula ([realize_closed_term S t]) f ([]) := iff.trans (by rw [substmax_eq_subst0_formula]) (by apply realize_subst_formula S f t ([])) lemma term_model_subst0 (f : bounded_formula L 1) (t : closed_term L) : term_model T ⊨ f[t/0] ↔ realize_bounded_formula ([term_mk T t]) f ([]) := (realize_subst_formula0 (term_model T) f t).trans (by simp) include H₂ instance nonempty_term_model : nonempty $ term_model T := begin induction H₂ with C, exact ⟨term_mk T (bd_const (C (&0 ≃ &0)))⟩ end include H₁ def term_model_ssatisfied_iff {n} : ∀{l} (f : presentence L l) (ts : dvector (closed_term L) l) (h : count_quantifiers f.fst < n), T ⊢' bd_apps_rel f ts ↔ term_model T ⊨ bd_apps_rel f ts := begin refine nat.strong_induction_on n _, clear n, intros n n_ih l f ts hn, have : {f' : preformula L l // f.fst = f' } := ⟨f.fst, rfl⟩, cases this with f' hf, induction f'; cases f; injection hf with hf₁ hf₂, { simp, exact H₁.1.elim }, { simp, refine iff.trans _ (realize_sentence_equal f_t₁ f_t₂).symm, simp [term_mk], refl }, { refine iff.trans _ (realize_bd_apps_rel _ _).symm, dsimp [term_model, term_model_rel], rw [ts.map_congr realize_closed_term_term_model, dvector.quotient_beta], refl }, { apply f'_ih f_f (f_t::ts) _ hf₁, simp at hn ⊢, exact hn }, { have ih₁ := f'_ih_f₁ f_f₁ ([]) (lt_of_le_of_lt (nat.le_add_right _ _) hn) hf₁, have ih₂ := f'_ih_f₂ f_f₂ ([]) (lt_of_le_of_lt (nat.le_add_left _ _) hn) hf₂, cases ts, split; intro h, { intro h₁, apply ih₂.mp, apply h.map2 (impE _), refine ih₁.mpr h₁ }, { simp at h, simp at ih₁, rw [←ih₁] at h, simp at ih₂, rw [←ih₂] at h, exact impI_of_is_complete H₁ h }}, { cases ts, split; intro h, { simp at h ⊢, apply quotient.ind, intro t, apply (term_model_subst0 f_f t).mp, cases n with n, { exfalso, exact not_lt_zero _ hn }, refine (n_ih n (lt.base n) (f_f[t/0]) ([]) _).mp (h.map _), simp, exact lt_of_succ_lt_succ hn, rw [bd_apps_rel_zero, subst0_bounded_formula_fst], exact allE₂ _ _ }, { apply classical.by_contradiction, intro H, cases find_counterexample_of_henkin H₁ H₂ f_f H with t ht, apply H₁.left, apply impE' _ ht, cases n with n, { exfalso, exact not_lt_zero _ hn }, refine (n_ih n (lt.base n) (f_f[t/0]) ([]) _).mpr _, { simp, exact lt_of_succ_lt_succ hn }, exact (term_model_subst0 f_f t).mpr (h (term_mk T t)) }}, end def term_model_ssatisfied : term_model T ⊨ T := begin intros f hf, apply (term_model_ssatisfied_iff H₁ H₂ f ([]) (lt.base _)).mp, exact ⟨saxm hf⟩ end -- completeness for complete theories with enough constants lemma completeness' {f : sentence L} (H : T ⊨ f) : T ⊢' f := begin apply (term_model_ssatisfied_iff H₁ H₂ f ([]) (lt.base _)).mpr, apply H, exact fol.nonempty_term_model H₂, apply term_model_ssatisfied H₁ H₂, end omit H₁ H₂ def Th (S : Structure L) : Theory L := { f : sentence L | S ⊨ f } lemma realize_sentence_Th (S : Structure L) : S ⊨ Th S := λf hf, hf lemma is_complete_Th (S : Structure L) (HS : nonempty S) : is_complete (Th S) := ⟨λH, by cases H; apply soundness H HS (realize_sentence_Th S), λ(f : sentence L), classical.em (S ⊨ f)⟩ def eliminates_quantifiers : Theory L → Prop := λ T, ∀ f ∈ T, ∃ f' , bounded_preformula.quantifier_free f' ∧ (T ⊢' f ⇔ f') end fol
a9d1e8cdd9be435b42a939b04c610d8117678618
eeee7bfb5e0033ce2c5099a3cf5c87a3c71b1f62
/src/valuations.lean
08fb10b0ba38d400c5d1f1898d4d53484deb0f62
[ "Apache-2.0" ]
permissive
jesse-michael-han/lean-perfectoid-spaces
f0c652bc1a0de64de90bb8c98f26d9579b226a9c
45b42a5302dca28910ae9c6e3847c025e5ba4180
refs/heads/master
1,584,646,248,214
1,528,569,065,000
1,528,569,065,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
12,333
lean
import algebra.group_power --import Kenny_comm_alg.temp import set_theory.cardinal import ring_theory.ideals import for_mathlib.subrel import for_mathlib.ideals class linear_ordered_comm_group (α : Type) extends comm_group α, linear_order α := (mul_le_mul_left : ∀ {a b : α}, a ≤ b → ∀ c : α, c * a ≤ c * b) class linear_ordered_comm_monoid (α : Type) extends comm_monoid α, linear_order α := (mul_le_mul_left : ∀ {a b : α}, a ≤ b → ∀ c : α, c * a ≤ c * b) local infix ^ := monoid.pow namespace linear_ordered_comm_group variables {α : Type} [linear_ordered_comm_group α] {x y z : α} variables {β : Type} [linear_ordered_comm_group β] class is_hom (f : α → β) : Prop := (Hf : is_group_hom f) (Hord : ∀ {a b : α}, a ≤ b → f a ≤ f b) instance hom_is_group_hom (f : α → β) [H : is_hom f] : is_group_hom f := H.Hf structure equiv extends equiv α β := (is_hom : is_hom to_fun) lemma mul_le_mul_right (H : x ≤ y) : ∀ z : α, x * z ≤ y * z := λ z, mul_comm z x ▸ mul_comm z y ▸ mul_le_mul_left H z lemma one_le_mul_of_one_le_of_one_le (Hx : 1 ≤ x) (Hy : 1 ≤ y) : 1 ≤ x * y := have h1 : x * 1 ≤ x * y, from mul_le_mul_left Hy x, have h2 : x ≤ x * y, by rwa mul_one x at h1, le_trans Hx h2 lemma one_le_pow_of_one_le {n : ℕ} (H : 1 ≤ x) : 1 ≤ x^n := begin induction n with n ih, { exact le_refl 1 }, { exact one_le_mul_of_one_le_of_one_le H ih } end lemma mul_le_one_of_le_one_of_le_one (Hx : x ≤ 1) (Hy : y ≤ 1) : x * y ≤ 1 := have h1 : x * y ≤ x * 1, from mul_le_mul_left Hy x, have h2 : x * y ≤ x, by rwa mul_one x at h1, le_trans h2 Hx lemma pow_le_one_of_le_one {n : ℕ} (H : x ≤ 1) : x^n ≤ 1 := begin induction n with n ih, { exact le_refl 1 }, { exact mul_le_one_of_le_one_of_le_one H ih } end /-- Wedhorn Remark 1.6 (3) -/ lemma eq_one_of_pow_eq_one {n : ℕ} (H : x ^ nat.succ n = 1) : x = 1 := begin induction n with n ih, { unfold monoid.pow at H;simpa using H }, { cases le_total x 1, { have h1 := mul_le_mul_right h (x ^ nat.succ n), dsimp [monoid.pow] at H h1, rw [H, one_mul] at h1, have h2 := pow_le_one_of_le_one h, exact ih (le_antisymm h2 h1) }, { have h1 := mul_le_mul_right h (x ^ nat.succ n), dsimp [monoid.pow] at H h1, rw [H, one_mul] at h1, have h2 := one_le_pow_of_one_le h, exact ih (le_antisymm h1 h2) } } end lemma inv_le_one_of_one_le (H : 1 ≤ x) : x⁻¹ ≤ 1 := by simpa using mul_le_mul_left H (x⁻¹) lemma inv_le_inv_of_le (H : x ≤ y) : y⁻¹ ≤ x⁻¹ := have h1 : _ := mul_le_mul_left H (x⁻¹ * y⁻¹), by rwa [inv_mul_cancel_right, mul_comm x⁻¹, inv_mul_cancel_right] at h1 lemma le_one_or_inv_le_one (x : α) : x ≤ 1 ∨ x⁻¹ ≤ 1 := or.imp id inv_le_one_of_one_le (le_total x 1) lemma le_or_inv_le_inv (x y : α) : x ≤ y ∨ x⁻¹ ≤ y⁻¹ := or.imp id inv_le_inv_of_le (le_total x y) class is_convex (S : set α) : Prop := (one_mem : (1:α) ∈ S) (mul_mem : ∀ {x y}, x ∈ S → y ∈ S → x * y ∈ S) (inv_mem : ∀ {x}, x ∈ S → x⁻¹ ∈ S) (mem_of_between : ∀ {x y}, x ≤ y → y ≤ (1:α) → x ∈ S → y ∈ S) class is_proper_convex (S : set α) extends is_convex S : Prop := (exists_ne : ∃ (x y : α) (hx : x ∈ S) (hy : y ∈ S), x ≠ y) definition convex_linear_order : linear_order {S : set α // is_convex S} := { le_total := λ ⟨x, hx⟩ ⟨y, hy⟩, classical.by_contradiction $ λ h, let ⟨h1, h2⟩ := not_or_distrib.1 h, ⟨m, hmx, hmny⟩ := set.not_subset.1 h1, ⟨n, hny, hnnx⟩ := set.not_subset.1 h2 in begin cases le_total m n with hmn hnm, { cases le_one_or_inv_le_one n with hn1 hni1, { exact hnnx (@@is_convex.mem_of_between _ hx hmn hn1 hmx) }, { cases le_total m (n⁻¹) with hmni hnim, { exact hnnx (inv_inv n ▸ (@@is_convex.inv_mem _ hx $ @@is_convex.mem_of_between _ hx hmni hni1 hmx)) }, { cases le_one_or_inv_le_one m with hm1 hmi1, { exact hmny (@@is_convex.mem_of_between _ hy hnim hm1 $ @@is_convex.inv_mem _ hy hny) }, { exact hmny (inv_inv m ▸ (@@is_convex.inv_mem _ hy $ @@is_convex.mem_of_between _ hy (inv_le_inv_of_le hmn) hmi1 $ @@is_convex.inv_mem _ hy hny)) } } } }, { cases le_one_or_inv_le_one m with hm1 hmi1, { exact hmny (@@is_convex.mem_of_between _ hy hnm hm1 hny) }, { cases le_total n (m⁻¹) with hnni hmim, { exact hmny (inv_inv m ▸ (@@is_convex.inv_mem _ hy $ @@is_convex.mem_of_between _ hy hnni hmi1 hny)) }, { cases le_one_or_inv_le_one n with hn1 hni1, { exact hnnx (@@is_convex.mem_of_between _ hx hmim hn1 $ @@is_convex.inv_mem _ hx hmx) }, { exact hnnx (inv_inv n ▸ (@@is_convex.inv_mem _ hx $ @@is_convex.mem_of_between _ hx (inv_le_inv_of_le hnm) hni1 $ @@is_convex.inv_mem _ hx hmx)) } } } } end, .. subrel.partial_order } def ker (f : α → β) (hf : is_hom f) : set α := { x | f x = 1 } theorem ker.is_convex (f : α → β) (hf : is_hom f) : is_convex (ker f hf) := { one_mem := is_group_hom.one f, mul_mem := λ x y hx hy, show f (x * y) = 1, by dsimp [ker] at hx hy; rw [(hf.1).mul, hx, hy, mul_one], inv_mem := λ x hx, show f x⁻¹ = 1, by dsimp [ker] at hx; rw [@is_group_hom.inv _ _ _ _ f (hf.1) x, hx, one_inv], mem_of_between := λ x y hxy hy1 hx, le_antisymm (is_group_hom.one f ▸ is_hom.Hord _ hy1) (hx ▸ is_hom.Hord _ hxy) } def height (α : Type) [linear_ordered_comm_group α] : cardinal := cardinal.mk {S : set α // is_proper_convex S} namespace extend def mul : option α → option α → option α | (some x) (some y) := some (x * y) | _ _ := none theorem mul_assoc : ∀ (x y z : option α), mul (mul x y) z = mul x (mul y z) | (some x) (some y) (some z) := congr_arg some $ _root_.mul_assoc x y z | (some _) (some _) none := rfl | (some _) none (some z) := rfl | (some _) none none := rfl | none (some _) (some z) := rfl | none (some _) none := rfl | none none (some z) := rfl | none none none := rfl def one : option α := some 1 def one_mul : ∀ x : option α, mul one x = x | (some x) := congr_arg some $ _root_.one_mul x | none := rfl def mul_one : ∀ x : option α, mul x one = x | (some x) := congr_arg some $ _root_.mul_one x | none := rfl def mul_comm : ∀ (x y : option α), mul x y = mul y x | (some x) (some y) := congr_arg some $ _root_.mul_comm x y | (some x) none := rfl | none (some _) := rfl | none none := rfl def le : option α → option α → Prop | (some x) (some y) := x ≤ y | (some _) none := false | none _ := true theorem le_refl : ∀ x : option α, le x x | (some x) := _root_.le_refl x | none := trivial theorem le_trans : ∀ x y z : option α, le x y → le y z → le x z | (some x) (some y) (some z) hxy hyz := _root_.le_trans hxy hyz | (some _) (some _) none hxy hyz := false.elim hyz | (some _) none _ hxy hyz := false.elim hxy | none _ _ hxy hyz := trivial theorem le_antisymm : ∀ x y : option α, le x y → le y x → x = y | (some x) (some y) hxy hyx := congr_arg some $ _root_.le_antisymm hxy hyx | (some _) none hxy hyx := false.elim hxy | none (some _) hxy hyx := false.elim hyx | none none hxy hyx := rfl theorem le_total : ∀ x y : option α, le x y ∨ le y x | (some x) (some y) := _root_.le_total x y | none _ := or.inl trivial | _ none := or.inr trivial theorem mul_le_mul_left : ∀ x y : option α, le x y → ∀ z : option α, le (mul z x) (mul z y) | (some x) (some y) hxy (some z) := linear_ordered_comm_group.mul_le_mul_left hxy z | _ _ hxy none := trivial | (some _) none hxy _ := false.elim hxy | none _ hxy (some _) := trivial instance : linear_ordered_comm_monoid (option α) := { mul := mul, mul_assoc := mul_assoc, one := one, one_mul := one_mul, mul_one := mul_one, mul_comm := mul_comm, le := le, le_refl := le_refl, le_trans := le_trans, le_antisymm := le_antisymm, le_total := le_total, mul_le_mul_left := mul_le_mul_left } instance : has_zero (option α) := ⟨none⟩ theorem zero_mul : ∀ x : option α, 0 * x = 0 | _ := rfl theorem mul_zero : ∀ x : option α, x * 0 = 0 | (some _) := rfl | none := rfl theorem none_mul : ∀ x : option α, none * x = none := zero_mul theorem mul_none : ∀ x : option α, x * none = none := mul_zero theorem eq_zero_or_eq_zero_of_mul_eq_zero : ∀ x y : option α, x * y = 0 → x = 0 ∨ y = 0 | (some x) (some y) hxy := false.elim $ option.no_confusion hxy | none _ hxy := or.inl rfl | _ none hxy := or.inr rfl end extend end linear_ordered_comm_group class is_valuation {α : Type} [linear_ordered_comm_group α] {R : Type} [comm_ring R] (f : R → option α) : Prop := (map_zero : f 0 = 0) (map_one : f 1 = 1) (map_mul : ∀ x y, f (x * y) = f x * f y) (map_add : ∀ x y, f (x + y) ≤ f x ∨ f (x + y) ≤ f y) namespace is_valuation variables {α : Type} [linear_ordered_comm_group α] variables {R : Type} [comm_ring R] (f : R → option α) variables [is_valuation f] {x y z : R} theorem map_unit : x * y = 1 → option.is_some (f x) := begin intro h, have h1 := map_mul f x y, rw [h, map_one f] at h1, cases (f x), { exfalso, exact option.no_confusion h1 }, { constructor } end theorem map_neg_one : f (-1) = 1 := begin have h1 : (-1 : R) * (-1) = 1 := by simp, have h2 := map_unit f h1, have h3 := map_mul f (-1) (-1), rw [option.is_some_iff_exists] at h2, cases h2 with x h, rw h at h3 ⊢, congr, rw [h1, map_one f] at h3, replace h3 := eq.symm (option.some.inj h3), have h4 : x^2 = 1 := by simpa [monoid.pow] using h3, exact linear_ordered_comm_group.eq_one_of_pow_eq_one h4 end namespace trivial variables (S : set R) [is_prime_ideal S] [decidable_pred S] instance : is_valuation (λ x, if x ∈ S then (0 : option α) else 1) := { map_zero := if_pos (is_submodule.zero_ R S), map_one := if_neg is_proper_ideal.one_not_mem, map_mul := λ x y, begin by_cases hx : x ∈ S, { rw if_pos hx, rw linear_ordered_comm_group.extend.zero_mul, rw if_pos (is_ideal.mul_right _ hx) }, { by_cases hy : y ∈ S, { rw if_pos hy, rw linear_ordered_comm_group.extend.mul_zero, rw if_pos (is_ideal.mul_left _ hy) }, { have hxy : x * y ∉ S, { intro hxy, cases is_prime_ideal.mem_or_mem_of_mul_mem hxy with h h, { exact hx h }, { exact hy h } }, { rw [if_neg hx, if_neg hy, if_neg hxy, mul_one] } } } end, map_add := λ x y, begin by_cases hxy : x + y ∈ S, { rw if_pos hxy, left, trivial }, { rw if_neg hxy, by_cases hx : x ∈ S, { have hy : y ∉ S := mt (is_ideal.add hx) hxy, right, rw if_neg hy }, { left, rw if_neg hx } } end } end trivial def supp : set R := {x | f x = 0} instance : is_prime_ideal (supp f) := { zero_ := map_zero f, add_ := λ x y hx hy, or.cases_on (map_add f x y) (λ hxy, le_antisymm (hx ▸ hxy) trivial) (λ hxy, le_antisymm (hy ▸ hxy) trivial), smul := λ c x hx, calc f (c * x) = f c * f x : map_mul f c x ... = f c * 0 : congr_arg _ hx ... = 0 : linear_ordered_comm_group.extend.mul_zero _, ne_univ := λ h, have h1 : (1:R) ∈ supp f, by rw h; trivial, have h2 : f 1 = 0 := h1, by rw [map_one f] at h2; exact option.no_confusion h2, mem_or_mem_of_mul_mem := λ x y hxy, begin dsimp [supp] at hxy ⊢, rw [map_mul f x y] at hxy, exact linear_ordered_comm_group.extend.eq_zero_or_eq_zero_of_mul_eq_zero _ _ hxy end } end is_valuation
f967f59b469d20d0c9b8dce67d650ca5b75b311a
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/stage0/src/Init/Notation.lean
0266342b147c8ef0ca8cee9a0086492c8e0356e0
[ "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
20,379
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura Notation for operators defined at Prelude.lean -/ prelude import Init.Prelude -- DSL for specifying parser precedences and priorities namespace Lean.Parser.Syntax syntax:65 (name := addPrec) prec " + " prec:66 : prec syntax:65 (name := subPrec) prec " - " prec:66 : prec syntax:65 (name := addPrio) prio " + " prio:66 : prio syntax:65 (name := subPrio) prio " - " prio:66 : prio end Lean.Parser.Syntax macro "max" : prec => `(1024) -- maximum precedence used in term parsers, in particular for terms in function position (`ident`, `paren`, ...) macro "arg" : prec => `(1023) -- precedence used for application arguments (`do`, `by`, ...) macro "lead" : prec => `(1022) -- precedence used for terms not supposed to be used as arguments (`let`, `have`, ...) macro "(" p:prec ")" : prec => p macro "min" : prec => `(10) -- minimum precedence used in term parsers macro "min1" : prec => `(11) -- `(min+1) we can only `min+1` after `Meta.lean` /- `max:prec` as a term. It is equivalent to `eval_prec max` for `eval_prec` defined at `Meta.lean`. We use `max_prec` to workaround bootstrapping issues. -/ macro "max_prec" : term => `(1024) macro "default" : prio => `(1000) macro "low" : prio => `(100) macro "mid" : prio => `(1000) macro "high" : prio => `(10000) macro "(" p:prio ")" : prio => p -- Basic notation for defining parsers -- NOTE: precedence must be at least `arg` to be used in `macro` without parentheses syntax:arg stx:max "+" : stx syntax:arg stx:max "*" : stx syntax:arg stx:max "?" : stx syntax:2 stx:2 " <|> " stx:1 : stx macro_rules | `(stx| $p +) => `(stx| many1($p)) | `(stx| $p *) => `(stx| many($p)) | `(stx| $p ?) => `(stx| optional($p)) | `(stx| $p₁ <|> $p₂) => `(stx| orelse($p₁, $p₂)) /- Comma-separated sequence. -/ macro:arg x:stx:max ",*" : stx => `(stx| sepBy($x, ",", ", ")) macro:arg x:stx:max ",+" : stx => `(stx| sepBy1($x, ",", ", ")) /- Comma-separated sequence with optional trailing comma. -/ macro:arg x:stx:max ",*,?" : stx => `(stx| sepBy($x, ",", ", ", allowTrailingSep)) macro:arg x:stx:max ",+,?" : stx => `(stx| sepBy1($x, ",", ", ", allowTrailingSep)) macro:arg "!" x:stx:max : stx => `(stx| notFollowedBy($x)) syntax (name := rawNatLit) "nat_lit " num : term infixr:90 " ∘ " => Function.comp infixr:35 " × " => Prod infixl:55 " ||| " => HOr.hOr infixl:58 " ^^^ " => HXor.hXor infixl:60 " &&& " => HAnd.hAnd infixl:65 " + " => HAdd.hAdd infixl:65 " - " => HSub.hSub infixl:70 " * " => HMul.hMul infixl:70 " / " => HDiv.hDiv infixl:70 " % " => HMod.hMod infixl:75 " <<< " => HShiftLeft.hShiftLeft infixl:75 " >>> " => HShiftRight.hShiftRight infixr:80 " ^ " => HPow.hPow infixl:65 " ++ " => HAppend.hAppend prefix:100 "-" => Neg.neg prefix:100 "~~~" => Complement.complement /- Remark: the infix commands above ensure a delaborator is generated for each relations. We redefine the macros below to be able to use the auxiliary `binop%` elaboration helper for binary operators. It addresses issue #382. -/ macro_rules | `($x ||| $y) => `(binop% HOr.hOr $x $y) macro_rules | `($x ^^^ $y) => `(binop% HXor.hXor $x $y) macro_rules | `($x &&& $y) => `(binop% HAnd.hAnd $x $y) macro_rules | `($x + $y) => `(binop% HAdd.hAdd $x $y) macro_rules | `($x - $y) => `(binop% HSub.hSub $x $y) macro_rules | `($x * $y) => `(binop% HMul.hMul $x $y) macro_rules | `($x / $y) => `(binop% HDiv.hDiv $x $y) macro_rules | `($x ++ $y) => `(binop% HAppend.hAppend $x $y) -- declare ASCII alternatives first so that the latter Unicode unexpander wins infix:50 " <= " => LE.le infix:50 " ≤ " => LE.le infix:50 " < " => LT.lt infix:50 " >= " => GE.ge infix:50 " ≥ " => GE.ge infix:50 " > " => GT.gt infix:50 " = " => Eq infix:50 " == " => BEq.beq /- Remark: the infix commands above ensure a delaborator is generated for each relations. We redefine the macros below to be able to use the auxiliary `binrel%` elaboration helper for binary relations. It has better support for applying coercions. For example, suppose we have `binrel% Eq n i` where `n : Nat` and `i : Int`. The default elaborator fails because we don't have a coercion from `Int` to `Nat`, but `binrel%` succeeds because it also tries a coercion from `Nat` to `Int` even when the nat occurs before the int. -/ macro_rules | `($x <= $y) => `(binrel% LE.le $x $y) macro_rules | `($x ≤ $y) => `(binrel% LE.le $x $y) macro_rules | `($x < $y) => `(binrel% LT.lt $x $y) macro_rules | `($x > $y) => `(binrel% GT.gt $x $y) macro_rules | `($x >= $y) => `(binrel% GE.ge $x $y) macro_rules | `($x ≥ $y) => `(binrel% GE.ge $x $y) macro_rules | `($x = $y) => `(binrel% Eq $x $y) macro_rules | `($x == $y) => `(binrel% BEq.beq $x $y) infixr:35 " /\\ " => And infixr:35 " ∧ " => And infixr:30 " \\/ " => Or infixr:30 " ∨ " => Or notation:max "¬" p:40 => Not p infixl:35 " && " => and infixl:30 " || " => or notation:max "!" b:40 => not b infixr:67 " :: " => List.cons syntax:20 term:21 " <|> " term:20 : term syntax:60 term:61 " >> " term:60 : term infixl:55 " >>= " => Bind.bind notation:60 a:60 " <*> " b:61 => Seq.seq a fun _ : Unit => b notation:60 a:60 " <* " b:61 => SeqLeft.seqLeft a fun _ : Unit => b notation:60 a:60 " *> " b:61 => SeqRight.seqRight a fun _ : Unit => b infixr:100 " <$> " => Functor.map macro_rules | `($x <|> $y) => `(binop_lazy% HOrElse.hOrElse $x $y) macro_rules | `($x >> $y) => `(binop_lazy% HAndThen.hAndThen $x $y) syntax (name := termDepIfThenElse) ppGroup(ppDedent("if " ident " : " term " then" ppSpace term ppDedent(ppSpace "else") ppSpace term)) : term macro_rules | `(if $h:ident : $c then $t:term else $e:term) => ``(dite $c (fun $h:ident => $t) (fun $h:ident => $e)) syntax (name := termIfThenElse) ppGroup(ppDedent("if " term " then" ppSpace term ppDedent(ppSpace "else") ppSpace term)) : term macro_rules | `(if $c then $t:term else $e:term) => ``(ite $c $t $e) macro "if " "let " pat:term " := " d:term " then " t:term " else " e:term : term => `(match $d:term with | $pat:term => $t | _ => $e) syntax:min term "<|" term:min : term macro_rules | `($f $args* <| $a) => let args := args.push a; `($f $args*) | `($f <| $a) => `($f $a) syntax:min term "|>" term:min1 : term macro_rules | `($a |> $f $args*) => let args := args.push a; `($f $args*) | `($a |> $f) => `($f $a) -- Haskell-like pipe <| -- Note that we have a whitespace after `$` to avoid an ambiguity with the antiquotations. syntax:min term atomic("$" ws) term:min : term macro_rules | `($f $args* $ $a) => let args := args.push a; `($f $args*) | `($f $ $a) => `($f $a) syntax "{ " ident (" : " term)? " // " term " }" : term macro_rules | `({ $x : $type // $p }) => ``(Subtype (fun ($x:ident : $type) => $p)) | `({ $x // $p }) => ``(Subtype (fun ($x:ident : _) => $p)) /- `without_expected_type t` instructs Lean to elaborate `t` without an expected type. Recall that terms such as `match ... with ...` and `⟨...⟩` will postpone elaboration until expected type is known. So, `without_expected_type` is not effective in this case. -/ macro "without_expected_type " x:term : term => `(let aux := $x; aux) syntax "[" term,* "]" : term syntax "%[" term,* "|" term "]" : term -- auxiliary notation for creating big list literals namespace Lean macro_rules | `([ $elems,* ]) => do let rec expandListLit (i : Nat) (skip : Bool) (result : Syntax) : MacroM Syntax := do match i, skip with | 0, _ => pure result | i+1, true => expandListLit i false result | i+1, false => expandListLit i true (← ``(List.cons $(elems.elemsAndSeps[i]) $result)) if elems.elemsAndSeps.size < 64 then expandListLit elems.elemsAndSeps.size false (← ``(List.nil)) else `(%[ $elems,* | List.nil ]) notation:50 e:51 " matches " p:51 => match e with | p => true | _ => false namespace Parser.Tactic /-- Introduce one or more hypotheses, optionally naming and/or pattern-matching them. For each hypothesis to be introduced, the remaining main goal's target type must be a `let` or function type. * `intro` by itself introduces one anonymous hypothesis, which can be accessed by e.g. `assumption`. * `intro x y` introduces two hypotheses and names them. Individual hypotheses can be anonymized via `_`, or matched against a pattern: ```lean -- ... ⊢ α × β → ... intro (a, b) -- ..., a : α, b : β ⊢ ... ``` * Alternatively, `intro` can be combined with pattern matching much like `fun`: ```lean intro | n + 1, 0 => tac | ... ``` -/ syntax (name := intro) "intro " notFollowedBy("|") (colGt term:max)* : tactic /-- `intros x...` behaves like `intro x...`, but then keeps introducing (anonymous) hypotheses until goal is not of a function type. -/ syntax (name := intros) "intros " (colGt (ident <|> "_"))* : tactic /-- `rename t => x` renames the most recent hypothesis whose type matches `t` (which may contain placeholders) to `x`, or fails if no such hypothesis could be found. -/ syntax (name := rename) "rename " term " => " ident : tactic /-- `revert x...` is the inverse of `intro x...`: it moves the given hypotheses into the main goal's target type. -/ syntax (name := revert) "revert " (colGt ident)+ : tactic /-- `clear x...` removes the given hypotheses, or fails if there are remaining references to a hypothesis. -/ syntax (name := clear) "clear " (colGt ident)+ : tactic /-- `subst x...` substitutes each `x` with `e` in the goal if there is a hypothesis of type `x = e` or `e = x`. If `x` is itself a hypothesis of type `y = e` or `e = y`, `y` is substituted instead. -/ syntax (name := subst) "subst " (colGt ident)+ : tactic /-- `assumption` tries to solve the main goal using a hypothesis of compatible type, or else fails. Note also the `‹t›` term notation, which is a shorthand for `show t by assumption`. -/ syntax (name := assumption) "assumption" : tactic /-- `contradiction` closes the main goal if its hypotheses are "trivially contradictory". ```lean example (h : False) : p := by contradiction -- inductive type/family with no applicable constructors example (h : none = some true) : p := by contradiction -- injectivity of constructors example (h : 2 + 2 = 3) : p := by contradiction -- decidable false proposition example (h : p) (h' : ¬ p) : q := by contradiction example (x : Nat) (h : x ≠ x) : p := by contradiction ``` -/ syntax (name := contradiction) "contradiction" : tactic /-- `apply e` tries to match the current goal against the conclusion of `e`'s type. If it succeeds, then the tactic returns as many subgoals as the number of premises that have not been fixed by type inference or type class resolution. Non-dependent premises are added before dependent ones. The `apply` tactic uses higher-order pattern matching, type class resolution, and first-order unification with dependent types. -/ syntax (name := apply) "apply " term : tactic /-- `exact e` closes the main goal if its target type matches that of `e`. -/ syntax (name := exact) "exact " term : tactic /-- `refine e` behaves like `exact e`, except that named (`?x`) or unnamed (`?_`) holes in `e` that are not solved by unification with the main goal's target type are converted into new goals, using the hole's name, if any, as the goal case name. -/ syntax (name := refine) "refine " term : tactic /-- `refine' e` behaves like `refine e`, except that unsolved placeholders (`_`) and implicit parameters are also converted into new goals. -/ syntax (name := refine') "refine' " term : tactic /-- If the main goal's target type is an inductive type, `constructor` solves it with the first matching constructor, or else fails. -/ syntax (name := constructor) "constructor" : tactic /-- `case tag => tac` focuses on the goal with case name `tag` and solves it using `tac`, or else fails. `case tag x₁ ... xₙ => tac` additionally renames the `n` most recent hypotheses with inaccessible names to the given names. -/ syntax (name := case) "case " (ident <|> "_") (ident <|> "_")* " => " tacticSeq : tactic /-- `next => tac` focuses on the next goal solves it using `tac`, or else fails. `next x₁ ... xₙ => tac` additionally renames the `n` most recent hypotheses with inaccessible names to the given names. -/ macro "next " args:(ident <|> "_")* " => " tac:tacticSeq : tactic => `(tactic| case _ $(args.getArgs)* => $tac) /-- `allGoals tac` runs `tac` on each goal, concatenating the resulting goals, if any. -/ syntax (name := allGoals) "all_goals " tacticSeq : tactic /-- `anyGoals tac` applies the tactic `tac` to every goal, and succeeds if at least one application succeeds. -/ syntax (name := anyGoals) "any_goals " tacticSeq : tactic /-- `focus tac` focuses on the main goal, suppressing all other goals, and runs `tac` on it. Usually `· tac`, which enforces that the goal is closed by `tac`, should be preferred. -/ syntax (name := focus) "focus " tacticSeq : tactic /-- `skip` does nothing. -/ syntax (name := skip) "skip" : tactic /-- `done` succeeds iff there are no remaining goals. -/ syntax (name := done) "done" : tactic syntax (name := traceState) "trace_state" : tactic syntax (name := failIfSuccess) "fail_if_success " tacticSeq : tactic syntax (name := paren) "(" tacticSeq ")" : tactic syntax (name := withReducible) "with_reducible " tacticSeq : tactic syntax (name := withReducibleAndInstances) "with_reducible_and_instances " tacticSeq : tactic /-- `first | tac | ...` runs each `tac` until one succeeds, or else fails. -/ syntax (name := first) "first " withPosition((group(colGe "|" tacticSeq))+) : tactic syntax (name := rotateLeft) "rotate_left" (num)? : tactic syntax (name := rotateRight) "rotate_right" (num)? : tactic /-- `try tac` runs `tac` and succeeds even if `tac` failed. -/ macro "try " t:tacticSeq : tactic => `(first | $t | skip) /-- `tac <;> tac'` runs `tac` on the main goal and `tac'` on each produced goal, concatenating all goals produced by `tac'`. -/ macro:1 x:tactic " <;> " y:tactic:0 : tactic => `(tactic| focus ($x:tactic; all_goals $y:tactic)) /-- `· tac` focuses on the main goal and tries to solve it using `tac`, or else fails. -/ macro dot:("·" <|> ".") ts:tacticSeq : tactic => `(tactic| {%$dot ($ts:tacticSeq) }) /-- `rfl` is a shorthand for `exact rfl`. -/ macro "rfl" : tactic => `(exact rfl) /-- `admit` is a shorthand for `exact sorry`. -/ macro "admit" : tactic => `(exact sorry) /-- The `sorry` tactic isnxo a shorthand for `exact sorry`. -/ macro "sorry" : tactic => `(exact sorry) macro "infer_instance" : tactic => `(exact inferInstance) /-- Optional configuration option for tactics -/ syntax config := atomic("(" &"config") " := " term ")" syntax locationWildcard := "*" syntax locationHyp := (colGt ident)+ ("⊢" <|> "|-")? -- TODO: delete syntax locationTargets := (colGt ident)+ ("⊢" <|> "|-")? syntax location := withPosition(" at " (locationWildcard <|> locationHyp)) syntax (name := change) "change " term (location)? : tactic syntax (name := changeWith) "change " term " with " term (location)? : tactic syntax rwRule := ("←" <|> "<-")? term syntax rwRuleSeq := "[" rwRule,+,? "]" syntax (name := rewriteSeq) "rewrite " (config)? rwRuleSeq (location)? : tactic syntax (name := rwSeq) "rw " (config)? rwRuleSeq (location)? : tactic def rwWithRfl (kind : SyntaxNodeKind) (atom : String) (stx : Syntax) : MacroM Syntax := do -- We show the `rfl` state on `]` let seq := stx[2] let rbrak := seq[2] -- Replace `]` token with one without position information in the expanded tactic let seq := seq.setArg 2 (mkAtom "]") let tac := stx.setKind kind |>.setArg 0 (mkAtomFrom stx atom) |>.setArg 2 seq `(tactic| $tac; try (with_reducible rfl%$rbrak)) @[macro rwSeq] def expandRwSeq : Macro := rwWithRfl ``Lean.Parser.Tactic.rewriteSeq "rewrite" syntax (name := injection) "injection " term (" with " (colGt (ident <|> "_"))+)? : tactic syntax (name := injections) "injections" : tactic syntax discharger := atomic("(" (&"discharger" <|> &"disch")) " := " tacticSeq ")" syntax simpPre := "↓" syntax simpPost := "↑" syntax simpLemma := (simpPre <|> simpPost)? ("←" <|> "<-")? term syntax simpErase := "-" ident syntax simpStar := "*" syntax (name := simp) "simp " (config)? (discharger)? (&"only ")? ("[" (simpStar <|> simpErase <|> simpLemma),* "]")? (location)? : tactic syntax (name := simpAll) "simp_all " (config)? (discharger)? (&"only ")? ("[" (simpErase <|> simpLemma),* "]")? : tactic /-- Delta expand the given definition. This is a low-level tactic, it will expose how recursive definitions have been compiled by Lean. -/ syntax (name := delta) "delta " ident (location)? : tactic -- Auxiliary macro for lifting have/suffices/let/... -- It makes sure the "continuation" `?_` is the main goal after refining macro "refine_lift " e:term : tactic => `(focus (refine noImplicitLambda% $e; rotate_right)) macro "have " d:haveDecl : tactic => `(refine_lift have $d:haveDecl; ?_) /- We use a priority > default, to avoid ambiguity with previous `have` notation -/ macro (priority := high) "have" x:ident " := " p:term : tactic => `(have $x:ident : _ := $p) macro "suffices " d:sufficesDecl : tactic => `(refine_lift suffices $d:sufficesDecl; ?_) macro "let " d:letDecl : tactic => `(refine_lift let $d:letDecl; ?_) macro "show " e:term : tactic => `(refine_lift show $e:term from ?_) syntax (name := letrec) withPosition(atomic(group("let " &"rec ")) letRecDecls) : tactic macro_rules | `(tactic| let rec $d:letRecDecls) => `(tactic| refine_lift let rec $d:letRecDecls; ?_) -- Similar to `refineLift`, but using `refine'` macro "refine_lift' " e:term : tactic => `(focus (refine' noImplicitLambda% $e; rotate_right)) macro "have' " d:haveDecl : tactic => `(refine_lift' have $d:haveDecl; ?_) macro (priority := high) "have'" x:ident " := " p:term : tactic => `(have' $x:ident : _ := $p) macro "let' " d:letDecl : tactic => `(refine_lift' let $d:letDecl; ?_) syntax inductionAlt := "| " (group("@"? ident) <|> "_") (ident <|> "_")* " => " (hole <|> syntheticHole <|> tacticSeq) syntax inductionAlts := "with " (tactic)? withPosition( (colGe inductionAlt)+) syntax (name := induction) "induction " term,+ (" using " ident)? ("generalizing " ident+)? (inductionAlts)? : tactic syntax generalizeArg := atomic(ident " : ")? term:51 " = " ident /-- `generalize ([h :] e = x),+` replaces all occurrences `e`s in the main goal with a fresh hypothesis `x`s. If `h` is given, `h : e = x` is introduced as well. -/ syntax (name := generalize) "generalize " generalizeArg,+ : tactic syntax casesTarget := atomic(ident " : ")? term syntax (name := cases) "cases " casesTarget,+ (" using " ident)? (inductionAlts)? : tactic syntax (name := existsIntro) "exists " term : tactic /-- `rename_i x_1 ... x_n` renames the last `n` inaccessible names using the given names. -/ syntax (name := renameI) "rename_i " (colGt (ident <|> "_"))+ : tactic syntax "repeat " tacticSeq : tactic macro_rules | `(tactic| repeat $seq) => `(tactic| first | ($seq); repeat $seq | skip) syntax "trivial" : tactic syntax (name := split) "split " (colGt term)? (location)? : tactic /-- The tactic `specialize h a₁ ... aₙ` works on local hypothesis `h`. The premises of this hypothesis, either universal quantifications or non-dependent implications, are instantiated by concrete terms coming either from arguments `a₁` ... `aₙ`. The tactic adds a new hypothesis with the same name `h := h a₁ ... aₙ` and tries to clear the previous one. -/ syntax (name := specialize) "specialize " term : tactic macro_rules | `(tactic| trivial) => `(tactic| assumption) macro_rules | `(tactic| trivial) => `(tactic| rfl) macro_rules | `(tactic| trivial) => `(tactic| contradiction) macro_rules | `(tactic| trivial) => `(tactic| apply True.intro) macro_rules | `(tactic| trivial) => `(tactic| apply And.intro <;> trivial) macro "unhygienic " t:tacticSeq : tactic => `(set_option tactic.hygienic false in $t:tacticSeq) end Tactic namespace Attr -- simp attribute syntax syntax (name := simp) "simp" (Tactic.simpPre <|> Tactic.simpPost)? (prio)? : attr end Attr end Parser end Lean macro "‹" type:term "›" : term => `((by assumption : $type))
f49c4adee50e5e26d17f52739f06bb403a4ec344
9dc8cecdf3c4634764a18254e94d43da07142918
/src/algebra/module/localized_module.lean
9a728c1798fc855811ee8ee6526d128c79e217a3
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
15,101
lean
/- Copyright (c) 2022 Jujian Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andrew Yang, Jujian Zhang -/ import group_theory.monoid_localization import ring_theory.localization.basic /-! # Localized Module Given a commutative ring `R`, a multiplicative subset `S ⊆ R` and an `R`-module `M`, we can localize `M` by `S`. This gives us a `localization S`-module. ## Main definitions * `localized_module.r` : the equivalence relation defining this localization, namely `(m, s) ≈ (m', s')` if and only if if there is some `u : S` such that `u • s' • m = u • s • m'`. * `localized_module M S` : the localized module by `S`. * `localized_module.mk` : the canonical map sending `(m, s) : M × S ↦ m/s : localized_module M S` * `localized_module.lift_on` : any well defined function `f : M × S → α` respecting `r` descents to a function `localized_module M S → α` * `localized_module.lift_on₂` : any well defined function `f : M × S → M × S → α` respecting `r` descents to a function `localized_module M S → localized_module M S` * `localized_module.mk_add_mk` : in the localized module `mk m s + mk m' s' = mk (s' • m + s • m') (s * s')` * `localized_module.mk_smul_mk` : in the localized module, for any `r : R`, `s t : S`, `m : M`, we have `mk r s • mk m t = mk (r • m) (s * t)` where `mk r s : localization S` is localized ring by `S`. * `localized_module.is_module` : `localized_module M S` is a `localization S`-module. ## Future work * Redefine `localization` for monoids and rings to coincide with `localized_module`. -/ namespace localized_module universes u v variables {R : Type u} [comm_semiring R] (S : submonoid R) variables (M : Type v) [add_comm_monoid M] [module R M] /--The equivalence relation on `M × S` where `(m1, s1) ≈ (m2, s2)` if and only if for some (u : S), u * (s2 • m1 - s1 • m2) = 0-/ def r : (M × S) → (M × S) → Prop | ⟨m1, s1⟩ ⟨m2, s2⟩ := ∃ (u : S), u • s1 • m2 = u • s2 • m1 lemma r.is_equiv : is_equiv _ (r S M) := { refl := λ ⟨m, s⟩, ⟨1, by rw [one_smul]⟩, trans := λ ⟨m1, s1⟩ ⟨m2, s2⟩ ⟨m3, s3⟩ ⟨u1, hu1⟩ ⟨u2, hu2⟩, begin use u1 * u2 * s2, -- Put everything in the same shape, sorting the terms using `simp` have hu1' := congr_arg ((•) (u2 * s3)) hu1, have hu2' := congr_arg ((•) (u1 * s1)) hu2, simp only [← mul_smul, smul_assoc, mul_assoc, mul_comm, mul_left_comm] at ⊢ hu1' hu2', rw [hu2', hu1'] end, symm := λ ⟨m1, s1⟩ ⟨m2, s2⟩ ⟨u, hu⟩, ⟨u, hu.symm⟩ } instance r.setoid : setoid (M × S) := { r := r S M, iseqv := ⟨(r.is_equiv S M).refl, (r.is_equiv S M).symm, (r.is_equiv S M).trans⟩ } /-- If `S` is a multiplicative subset of a ring `R` and `M` an `R`-module, then we can localize `M` by `S`. -/ @[nolint has_nonempty_instance] def _root_.localized_module : Type (max u v) := quotient (r.setoid S M) section variables {M S} /--The canonical map sending `(m, s) ↦ m/s`-/ def mk (m : M) (s : S) : localized_module S M := quotient.mk ⟨m, s⟩ lemma mk_eq {m m' : M} {s s' : S} : mk m s = mk m' s' ↔ ∃ (u : S), u • s • m' = u • s' • m := quotient.eq @[elab_as_eliminator] lemma induction_on {β : localized_module S M → Prop} (h : ∀ (m : M) (s : S), β (mk m s)) : ∀ (x : localized_module S M), β x := by { rintro ⟨⟨m, s⟩⟩, exact h m s } @[elab_as_eliminator] lemma induction_on₂ {β : localized_module S M → localized_module S M → Prop} (h : ∀ (m m' : M) (s s' : S), β (mk m s) (mk m' s')) : ∀ x y, β x y := by { rintro ⟨⟨m, s⟩⟩ ⟨⟨m', s'⟩⟩, exact h m m' s s' } /--If `f : M × S → α` respects the equivalence relation `localized_module.r`, then `f` descents to a map `localized_module M S → α`. -/ def lift_on {α : Type*} (x : localized_module S M) (f : M × S → α) (wd : ∀ (p p' : M × S) (h1 : p ≈ p'), f p = f p') : α := quotient.lift_on x f wd lemma lift_on_mk {α : Type*} {f : M × S → α} (wd : ∀ (p p' : M × S) (h1 : p ≈ p'), f p = f p') (m : M) (s : S) : lift_on (mk m s) f wd = f ⟨m, s⟩ := by convert quotient.lift_on_mk f wd ⟨m, s⟩ /--If `f : M × S → M × S → α` respects the equivalence relation `localized_module.r`, then `f` descents to a map `localized_module M S → localized_module M S → α`. -/ def lift_on₂ {α : Type*} (x y : localized_module S M) (f : (M × S) → (M × S) → α) (wd : ∀ (p q p' q' : M × S) (h1 : p ≈ p') (h2 : q ≈ q'), f p q = f p' q') : α := quotient.lift_on₂ x y f wd lemma lift_on₂_mk {α : Type*} (f : (M × S) → (M × S) → α) (wd : ∀ (p q p' q' : M × S) (h1 : p ≈ p') (h2 : q ≈ q'), f p q = f p' q') (m m' : M) (s s' : S) : lift_on₂ (mk m s) (mk m' s') f wd = f ⟨m, s⟩ ⟨m', s'⟩ := by convert quotient.lift_on₂_mk f wd _ _ instance : has_zero (localized_module S M) := ⟨mk 0 1⟩ @[simp] lemma zero_mk (s : S) : mk (0 : M) s = 0 := mk_eq.mpr ⟨1, by rw [one_smul, smul_zero, smul_zero, one_smul]⟩ instance : has_add (localized_module S M) := { add := λ p1 p2, lift_on₂ p1 p2 (λ x y, mk (y.2 • x.1 + x.2 • y.1) (x.2 * y.2)) $ λ ⟨m1, s1⟩ ⟨m2, s2⟩ ⟨m1', s1'⟩ ⟨m2', s2'⟩ ⟨u1, hu1⟩ ⟨u2, hu2⟩, mk_eq.mpr ⟨u1 * u2, begin -- Put everything in the same shape, sorting the terms using `simp` have hu1' := congr_arg ((•) (u2 * s2 * s2')) hu1, have hu2' := congr_arg ((•) (u1 * s1 * s1')) hu2, simp only [smul_add, ← mul_smul, smul_assoc, mul_assoc, mul_comm, mul_left_comm] at ⊢ hu1' hu2', rw [hu1', hu2'] end⟩ } lemma mk_add_mk {m1 m2 : M} {s1 s2 : S} : mk m1 s1 + mk m2 s2 = mk (s2 • m1 + s1 • m2) (s1 * s2) := mk_eq.mpr $ ⟨1, by dsimp only; rw [one_smul]⟩ private lemma add_assoc' (x y z : localized_module S M) : x + y + z = x + (y + z) := begin induction x using localized_module.induction_on with mx sx, induction y using localized_module.induction_on with my sy, induction z using localized_module.induction_on with mz sz, simp only [mk_add_mk, smul_add], refine mk_eq.mpr ⟨1, _⟩, rw [one_smul, one_smul], congr' 1, { rw [mul_assoc] }, { rw [mul_comm, add_assoc, mul_smul, mul_smul, ←mul_smul sx sz, mul_comm, mul_smul], }, end private lemma add_comm' (x y : localized_module S M) : x + y = y + x := localized_module.induction_on₂ (λ m m' s s', by rw [mk_add_mk, mk_add_mk, add_comm, mul_comm]) x y private lemma zero_add' (x : localized_module S M) : 0 + x = x := induction_on (λ m s, by rw [← zero_mk s, mk_add_mk, smul_zero, zero_add, mk_eq]; exact ⟨1, by rw [one_smul, mul_smul, one_smul]⟩) x private lemma add_zero' (x : localized_module S M) : x + 0 = x := induction_on (λ m s, by rw [← zero_mk s, mk_add_mk, smul_zero, add_zero, mk_eq]; exact ⟨1, by rw [one_smul, mul_smul, one_smul]⟩) x instance has_nat_smul : has_smul ℕ (localized_module S M) := { smul := λ n, nsmul_rec n } private lemma nsmul_zero' (x : localized_module S M) : (0 : ℕ) • x = 0 := localized_module.induction_on (λ _ _, rfl) x private lemma nsmul_succ' (n : ℕ) (x : localized_module S M) : n.succ • x = x + n • x := localized_module.induction_on (λ _ _, rfl) x instance : add_comm_monoid (localized_module S M) := { add := (+), add_assoc := add_assoc', zero := 0, zero_add := zero_add', add_zero := add_zero', nsmul := (•), nsmul_zero' := nsmul_zero', nsmul_succ' := nsmul_succ', add_comm := add_comm' } instance : has_smul (localization S) (localized_module S M) := { smul := λ f x, localization.lift_on f (λ r s, lift_on x (λ p, mk (r • p.1) (s * p.2)) begin rintros ⟨m1, t1⟩ ⟨m2, t2⟩ ⟨u, h⟩, refine mk_eq.mpr ⟨u, _⟩, have h' := congr_arg ((•) (s • r)) h, simp only [← mul_smul, smul_assoc, mul_comm, mul_left_comm, submonoid.smul_def, submonoid.coe_mul] at ⊢ h', rw h', end) begin induction x using localized_module.induction_on with m t, rintros r r' s s' h, simp only [lift_on_mk, lift_on_mk, mk_eq], obtain ⟨u, eq1⟩ := localization.r_iff_exists.mp h, use u, have eq1' := congr_arg (• (t • m)) eq1, simp only [← mul_smul, smul_assoc, submonoid.smul_def, submonoid.coe_mul] at ⊢ eq1', ring_nf at ⊢ eq1', rw eq1' end } lemma mk_smul_mk (r : R) (m : M) (s t : S) : localization.mk r s • mk m t = mk (r • m) (s * t) := begin unfold has_smul.smul, rw [localization.lift_on_mk, lift_on_mk], end private lemma one_smul' (m : localized_module S M) : (1 : localization S) • m = m := begin induction m using localized_module.induction_on with m s, rw [← localization.mk_one, mk_smul_mk, one_smul, one_mul], end private lemma mul_smul' (x y : localization S) (m : localized_module S M) : (x * y) • m = x • y • m := begin induction x using localization.induction_on with data, induction y using localization.induction_on with data', rcases ⟨data, data'⟩ with ⟨⟨r, s⟩, ⟨r', s'⟩⟩, induction m using localized_module.induction_on with m t, rw [localization.mk_mul, mk_smul_mk, mk_smul_mk, mk_smul_mk, mul_smul, mul_assoc], end private lemma smul_add' (x : localization S) (y z : localized_module S M) : x • (y + z) = x • y + x • z := begin induction x using localization.induction_on with data, rcases data with ⟨r, u⟩, induction y using localized_module.induction_on with m s, induction z using localized_module.induction_on with n t, rw [mk_smul_mk, mk_smul_mk, mk_add_mk, mk_smul_mk, mk_add_mk, mk_eq], use 1, simp only [one_smul, smul_add, ← mul_smul, submonoid.smul_def, submonoid.coe_mul], ring_nf end private lemma smul_zero' (x : localization S) : x • (0 : localized_module S M) = 0 := begin induction x using localization.induction_on with data, rcases data with ⟨r, s⟩, rw [←zero_mk s, mk_smul_mk, smul_zero, zero_mk, zero_mk], end private lemma add_smul' (x y : localization S) (z : localized_module S M) : (x + y) • z = x • z + y • z := begin induction x using localization.induction_on with datax, induction y using localization.induction_on with datay, induction z using localized_module.induction_on with m t, rcases ⟨datax, datay⟩ with ⟨⟨r, s⟩, ⟨r', s'⟩⟩, rw [localization.add_mk, mk_smul_mk, mk_smul_mk, mk_smul_mk, mk_add_mk, mk_eq], use 1, simp only [one_smul, add_smul, smul_add, ← mul_smul, submonoid.smul_def, submonoid.coe_mul, submonoid.coe_one], rw add_comm, -- Commutativity of addition in the module is not applied by `ring`. ring_nf, end private lemma zero_smul' (x : localized_module S M) : (0 : localization S) • x = 0 := begin induction x using localized_module.induction_on with m s, rw [← localization.mk_zero s, mk_smul_mk, zero_smul, zero_mk], end instance is_module : module (localization S) (localized_module S M) := { smul := (•), one_smul := one_smul', mul_smul := mul_smul', smul_add := smul_add', smul_zero := smul_zero', add_smul := add_smul', zero_smul := zero_smul' } @[simp] lemma mk_cancel_common_left (s' s : S) (m : M) : mk (s' • m) (s' * s) = mk m s := mk_eq.mpr ⟨1, by { simp only [mul_smul, one_smul], rw smul_comm }⟩ @[simp] lemma mk_cancel (s : S) (m : M) : mk (s • m) s = mk m 1 := mk_eq.mpr ⟨1, by simp⟩ @[simp] lemma mk_cancel_common_right (s s' : S) (m : M) : mk (s' • m) (s * s') = mk m s := mk_eq.mpr ⟨1, by simp [mul_smul]⟩ instance is_module' : module R (localized_module S M) := { ..module.comp_hom (localized_module S M) $ (algebra_map R (localization S)) } lemma smul'_mk (r : R) (s : S) (m : M) : r • mk m s = mk (r • m) s := by erw [mk_smul_mk r m 1 s, one_mul] section variables (S M) /-- The function `m ↦ m / 1` as an `R`-linear map. -/ @[simps] def mk_linear_map : M →ₗ[R] localized_module S M := { to_fun := λ m, mk m 1, map_add' := λ x y, by simp [mk_add_mk], map_smul' := λ r x, (smul'_mk _ _ _).symm } end /-- For any `s : S`, there is an `R`-linear map given by `a/b ↦ a/(b*s)`. -/ @[simps] def div_by (s : S) : localized_module S M →ₗ[R] localized_module S M := { to_fun := λ p, p.lift_on (λ p, mk p.1 (s * p.2)) $ λ ⟨a, b⟩ ⟨a', b'⟩ ⟨c, eq1⟩, mk_eq.mpr ⟨c, begin rw [mul_smul, mul_smul, smul_comm c, eq1, smul_comm s]; apply_instance, end⟩, map_add' := λ x y, x.induction_on₂ (begin intros m₁ m₂ t₁ t₂, simp only [mk_add_mk, localized_module.lift_on_mk, mul_smul, ←smul_add, mul_assoc, mk_cancel_common_left s], rw show s * (t₁ * t₂) = t₁ * (s * t₂), by { ext, simp only [submonoid.coe_mul], ring }, end) y, map_smul' := λ r x, x.induction_on $ by { intros, simp [localized_module.lift_on_mk, smul'_mk] } } lemma div_by_mul_by (s : S) (p : localized_module S M) : div_by s (algebra_map R (module.End R (localized_module S M)) s p) = p := p.induction_on begin intros m t, simp only [localized_module.lift_on_mk, module.algebra_map_End_apply, smul'_mk, div_by_apply], erw mk_cancel_common_left s t, end lemma mul_by_div_by (s : S) (p : localized_module S M) : algebra_map R (module.End R (localized_module S M)) s (div_by s p) = p := p.induction_on begin intros m t, simp only [localized_module.lift_on_mk, div_by_apply, module.algebra_map_End_apply, smul'_mk], erw mk_cancel_common_left s t, end end end localized_module section is_localized_module universes u v variables {R : Type u} [comm_ring R] (S : submonoid R) variables {M M' : Type u} [add_comm_monoid M] [add_comm_monoid M'] variables [module R M] [module R M'] (f : M →ₗ[R] M') /-- The characteristic predicate for localized module. `is_localized_module S f` describes that `f : M ⟶ M'` is the localization map identifying `M'` as `localized_module S M`. -/ class is_localized_module : Prop := (map_units [] : ∀ (x : S), is_unit (algebra_map R (module.End R M') x)) (surj [] : ∀ y : M', ∃ (x : M × S), x.2 • y = f x.1) (eq_iff_exists [] : ∀ {x₁ x₂}, f x₁ = f x₂ ↔ ∃ c : S, c • x₂ = c • x₁) instance localized_module_is_localized_module : is_localized_module S (localized_module.mk_linear_map S M) := { map_units := λ s, ⟨⟨algebra_map R (module.End R (localized_module S M)) s, localized_module.div_by s, fun_like.ext _ _ $ localized_module.mul_by_div_by s, fun_like.ext _ _ $ localized_module.div_by_mul_by s⟩, fun_like.ext _ _ $ λ p, p.induction_on $ by { intros, refl }⟩, surj := λ p, p.induction_on begin intros m t, refine ⟨⟨m, t⟩, _⟩, erw [localized_module.smul'_mk, localized_module.mk_linear_map_apply, submonoid.coe_subtype, localized_module.mk_cancel t ], end, eq_iff_exists := λ m1 m2, { mp := λ eq1, by simpa only [one_smul] using localized_module.mk_eq.mp eq1, mpr := λ ⟨c, eq1⟩, localized_module.mk_eq.mpr ⟨c, by simpa only [one_smul] using eq1⟩ } } end is_localized_module
40952c037448d3c21ce61d2e15b9c50a9743ed18
e38e95b38a38a99ecfa1255822e78e4b26f65bb0
/src/certigrad/compute_grad.lean
16b11609a9993deed593d0a505fba97020d63d77
[ "Apache-2.0" ]
permissive
ColaDrill/certigrad
fefb1be3670adccd3bed2f3faf57507f156fd501
fe288251f623ac7152e5ce555f1cd9d3a20203c2
refs/heads/master
1,593,297,324,250
1,499,903,753,000
1,499,903,753,000
97,075,797
1
0
null
1,499,916,210,000
1,499,916,210,000
null
UTF-8
Lean
false
false
4,499
lean
/- Copyright (c) 2017 Daniel Selsam. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Daniel Selsam Stochastic backpropagation. -/ import .util .tensor .tvec .graph namespace certigrad open list def sum_costs (m : env) (costs : list ID) : ℝ := sumr (map (λ cost, env.get (cost, []) m) costs) -- Note: we currently sum all costs in remaining nodes when we absorb at a PDF. -- In sequential applications, it is crucial that only the costs that are topologogically downstream are summed. -- Exercise for the reader: prove that such an optimization is sound, and update the implementation accordingly. -- def sum_downstream_costs (nodes : list node) (costs : list ID) (tgt : reference) (m : env) : ℝ := -- sum (map (λ cost, DMap.get cost [] env) (filter (λ cost, IsDownstream cost tgt nodes) costs)) def sum_downstream_costs (nodes : list node) (costs : list ID) (tgt : reference) (m : env) : ℝ := sum_costs m costs def compute_grad_slow (costs : list ID) : Π (nodes : list node) (inputs : env) (tgt : reference), T tgt.2 | [] m tgt := sumr (map (λ (cost : ID), if tgt = (cost, []) then 1 else 0) costs) | (⟨ref, parents, operator.det op⟩::nodes) m tgt := compute_grad_slow nodes m tgt + list.sumr (map (λ (idx : ℕ), op^.pb (env.get_ks parents m) (env.get ref m) (compute_grad_slow nodes m ref) idx tgt.2) (filter (λ idx, tgt = dnth parents idx) (riota $ length parents))) | (⟨ref, parents, operator.rand op⟩::nodes) m tgt := compute_grad_slow nodes m tgt + list.sumr (map (λ (idx : ℕ), sum_downstream_costs nodes costs ref m ⬝ op^.glogpdf (env.get_ks parents m) (env.get ref m) idx tgt.2) (filter (λ idx, tgt = dnth parents idx) (riota $ length parents))) def compute_grad_step (costs : list ID) (callback : list node → Π (tgt : reference), T tgt.2) : Π (nodes : list node) (inputs : env) (tgt : reference), T tgt.2 | [] m tgt := sumr₁ (map (λ (cost : ID), if tgt = (cost, []) then T.one _ else T.zero _) costs) | (⟨ref, parents, operator.det op⟩::nodes) m tgt := sumrd (callback nodes tgt) (map (λ (idx : ℕ), op^.pb (env.get_ks parents m) (env.get ref m) (callback nodes ref) idx tgt.2) (filter (λ idx, tgt = dnth parents idx) (riota $ length parents))) | (⟨ref, parents, operator.rand op⟩::nodes) m tgt := sumrd (callback nodes tgt) (map (λ (idx : ℕ), sum_downstream_costs nodes costs ref m ⬝ op^.glogpdf (env.get_ks parents m) (env.get ref m) idx tgt.2) (filter (λ idx, tgt = dnth parents idx) (riota $ length parents))) def compute_init_dict (costs : list ID) : Π (nodes : list node) (tgts : list reference), env | [] tgts := foldr (λ (tgt : reference) (dict : env), env.insert tgt (compute_grad_step costs (λ (nodes : list node) (tgt' : reference), T.error "backprop-end") [] env.mk tgt) dict) env.mk tgts | (n :: nodes) tgts := compute_init_dict nodes (n^.ref :: tgts) def backprop_core_helper (costs : list ID) (init_dict : env) : Π (nodes : list node) (dict : env) (tgts : list reference), env | [] m tgts := init_dict | (n :: nodes) m tgts := let old_dict := backprop_core_helper nodes m (n^.ref :: tgts) in foldr (λ (tgt : reference) (dict : env), env.insert tgt (compute_grad_step costs (λ (nodes' : list node) (tgt' : reference), env.get tgt' old_dict) (n::nodes) m tgt) dict) env.mk tgts def backprop_core (costs : list ID) (nodes : list node) (dict : env) (tgts : list reference) : env := backprop_core_helper costs (compute_init_dict costs nodes tgts) nodes dict tgts def backprop (costs : list ID) (nodes : list node) (inputs : env) (tgts : list reference) : dvec T tgts^.p2 := let dict := backprop_core costs nodes inputs tgts in tvec.from_env tgts dict def bprop (costs : list ID) (init_dict : env) (nodes : list node) (inputs : env) (tgts : list reference) : dvec T tgts^.p2 := let dict := backprop_core_helper costs init_dict nodes inputs tgts in tvec.from_env tgts dict end certigrad
37de6c51a6c2d5310cd39612a0b1703b918a0d17
618003631150032a5676f229d13a079ac875ff77
/src/group_theory/perm/cycles.lean
99cd5289872ad7c30392d4045cd50546e7c945c2
[ "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
7,469
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import group_theory.perm.sign import group_theory.order_of_element namespace equiv.perm open equiv function finset variables {α : Type*} {β : Type*} [decidable_eq α] def same_cycle (f : perm β) (x y : β) := ∃ i : ℤ, (f ^ i) x = y @[refl] lemma same_cycle.refl (f : perm β) (x : β) : same_cycle f x x := ⟨0, rfl⟩ @[symm] lemma same_cycle.symm (f : perm β) {x y : β} : same_cycle f x y → same_cycle f y x := λ ⟨i, hi⟩, ⟨-i, by rw [gpow_neg, ← hi, inv_apply_self]⟩ @[trans] lemma same_cycle.trans (f : perm β) {x y z : β} : same_cycle f x y → same_cycle f y z → same_cycle f x z := λ ⟨i, hi⟩ ⟨j, hj⟩, ⟨j + i, by rw [gpow_add, mul_apply, hi, hj]⟩ lemma apply_eq_self_iff_of_same_cycle {f : perm β} {x y : β} : same_cycle f x y → (f x = x ↔ f y = y) := λ ⟨i, hi⟩, by rw [← hi, ← mul_apply, ← gpow_one_add, add_comm, gpow_add_one, mul_apply, (f ^ i).injective.eq_iff] lemma same_cycle_of_is_cycle {f : perm β} (hf : is_cycle f) {x y : β} (hx : f x ≠ x) (hy : f y ≠ y) : same_cycle f x y := exists_gpow_eq_of_is_cycle hf hx hy instance [fintype α] (f : perm α) : decidable_rel (same_cycle f) := λ x y, decidable_of_iff (∃ n ∈ list.range (order_of f), (f ^ n) x = y) ⟨λ ⟨n, _, hn⟩, ⟨n, hn⟩, λ ⟨i, hi⟩, ⟨(i % order_of f).nat_abs, list.mem_range.2 (int.coe_nat_lt.1 $ by rw int.nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _))); exact calc _ < _ : int.mod_lt _ (int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _)) ... = _ : by simp), by rw [← gpow_coe_nat, int.nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _))), ← gpow_eq_mod_order_of, hi]⟩⟩ lemma same_cycle_apply {f : perm β} {x y : β} : same_cycle f x (f y) ↔ same_cycle f x y := ⟨λ ⟨i, hi⟩, ⟨-1 + i, by rw [gpow_add, mul_apply, hi, gpow_neg_one, inv_apply_self]⟩, λ ⟨i, hi⟩, ⟨1 + i, by rw [gpow_add, mul_apply, hi, gpow_one]⟩⟩ lemma same_cycle_cycle {f : perm β} {x : β} (hx : f x ≠ x) : is_cycle f ↔ (∀ {y}, same_cycle f x y ↔ f y ≠ y) := ⟨λ hf y, ⟨λ ⟨i, hi⟩ hy, hx $ by rw [← gpow_apply_eq_self_of_apply_eq_self hy i, (f ^ i).injective.eq_iff] at hi; rw [hi, hy], exists_gpow_eq_of_is_cycle hf hx⟩, λ h, ⟨x, hx, λ y hy, h.2 hy⟩⟩ lemma same_cycle_inv (f : perm β) {x y : β} : same_cycle f⁻¹ x y ↔ same_cycle f x y := ⟨λ ⟨i, hi⟩, ⟨-i, by rw [gpow_neg, ← inv_gpow, hi]⟩, λ ⟨i, hi⟩, ⟨-i, by rw [gpow_neg, ← inv_gpow, inv_inv, hi]⟩ ⟩ lemma same_cycle_inv_apply {f : perm β} {x y : β} : same_cycle f x (f⁻¹ y) ↔ same_cycle f x y := by rw [← same_cycle_inv, same_cycle_apply, same_cycle_inv] def cycle_of [fintype α] (f : perm α) (x : α) : perm α := of_subtype (@subtype_perm _ f (same_cycle f x) (λ _, same_cycle_apply.symm)) lemma cycle_of_apply [fintype α] (f : perm α) (x y : α) : cycle_of f x y = if same_cycle f x y then f y else y := rfl lemma cycle_of_inv [fintype α] (f : perm α) (x : α) : (cycle_of f x)⁻¹ = cycle_of f⁻¹ x := equiv.ext $ λ y, begin rw [inv_eq_iff_eq, cycle_of_apply, cycle_of_apply]; split_ifs; simp [*, same_cycle_inv, same_cycle_inv_apply] at * end @[simp] lemma cycle_of_pow_apply_self [fintype α] (f : perm α) (x : α) : ∀ n : ℕ, (cycle_of f x ^ n) x = (f ^ n) x | 0 := rfl | (n+1) := by rw [pow_succ, mul_apply, cycle_of_apply, cycle_of_pow_apply_self, if_pos, pow_succ, mul_apply]; exact ⟨n, rfl⟩ @[simp] lemma cycle_of_gpow_apply_self [fintype α] (f : perm α) (x : α) : ∀ n : ℤ, (cycle_of f x ^ n) x = (f ^ n) x | (n : ℕ) := cycle_of_pow_apply_self f x n | -[1+ n] := by rw [gpow_neg_succ, ← inv_pow, cycle_of_inv, gpow_neg_succ, ← inv_pow, cycle_of_pow_apply_self] lemma cycle_of_apply_of_same_cycle [fintype α] {f : perm α} {x y : α} (h : same_cycle f x y) : cycle_of f x y = f y := dif_pos h lemma cycle_of_apply_of_not_same_cycle [fintype α] {f : perm α} {x y : α} (h : ¬same_cycle f x y) : cycle_of f x y = y := dif_neg h @[simp] lemma cycle_of_apply_self [fintype α] (f : perm α) (x : α) : cycle_of f x x = f x := cycle_of_apply_of_same_cycle (same_cycle.refl _ _) lemma cycle_of_cycle [fintype α] {f : perm α} (hf : is_cycle f) {x : α} (hx : f x ≠ x) : cycle_of f x = f := equiv.ext $ λ y, if h : same_cycle f x y then by rw [cycle_of_apply_of_same_cycle h] else by rw [cycle_of_apply_of_not_same_cycle h, not_not.1 (mt ((same_cycle_cycle hx).1 hf).2 h)] lemma cycle_of_one [fintype α] (x : α) : cycle_of 1 x = 1 := by rw [cycle_of, subtype_perm_one (same_cycle 1 x), of_subtype_one] lemma is_cycle_cycle_of [fintype α] (f : perm α) {x : α} (hx : f x ≠ x) : is_cycle (cycle_of f x) := have cycle_of f x x ≠ x, by rwa [cycle_of_apply_of_same_cycle (same_cycle.refl _ _)], (same_cycle_cycle this).2 $ λ y, ⟨λ h, mt (apply_eq_self_iff_of_same_cycle h).2 this, λ h, if hxy : same_cycle f x y then let ⟨i, hi⟩ := hxy in ⟨i, by rw [cycle_of_gpow_apply_self, hi]⟩ else by rw [cycle_of_apply_of_not_same_cycle hxy] at h; exact (h rfl).elim⟩ def cycle_factors_aux [fintype α] : Π (l : list α) (f : perm α), (∀ {x}, f x ≠ x → x ∈ l) → {l : list (perm α) // l.prod = f ∧ (∀ g ∈ l, is_cycle g) ∧ l.pairwise disjoint} | [] f h := ⟨[], by simp [*, imp_false, list.pairwise.nil] at *; ext; simp *⟩ | (x::l) f h := if hx : f x = x then cycle_factors_aux l f (λ y hy, list.mem_of_ne_of_mem (λ h, hy (by rwa h)) (h hy)) else let ⟨m, hm₁, hm₂, hm₃⟩ := cycle_factors_aux l ((cycle_of f x)⁻¹ * f) (λ y hy, list.mem_of_ne_of_mem (λ h : y = x, by rw [h, mul_apply, ne.def, inv_eq_iff_eq, cycle_of_apply_self] at hy; exact hy rfl) (h (λ h : f y = y, by rw [mul_apply, h, ne.def, inv_eq_iff_eq, cycle_of_apply] at hy; split_ifs at hy; cc))) in ⟨(cycle_of f x) :: m, by rw [list.prod_cons, hm₁]; simp, λ g hg, ((list.mem_cons_iff _ _ _).1 hg).elim (λ hg, hg.symm ▸ is_cycle_cycle_of _ hx) (hm₂ g), list.pairwise_cons.2 ⟨λ g hg y, or_iff_not_imp_left.2 (λ hfy, have hxy : same_cycle f x y := not_not.1 (mt cycle_of_apply_of_not_same_cycle hfy), have hgm : g :: m.erase g ~ m := list.cons_perm_iff_perm_erase.2 ⟨hg, list.perm.refl _⟩, have ∀ h ∈ m.erase g, disjoint g h, from (list.pairwise_cons.1 ((hgm.pairwise_iff (λ a b (h : disjoint a b), h.symm)).2 hm₃)).1, classical.by_cases id $ λ hgy : g y ≠ y, (disjoint_prod_right _ this y).resolve_right $ have hsc : same_cycle f⁻¹ x (f y), by rwa [same_cycle_inv, same_cycle_apply], by rw [disjoint_prod_perm hm₃ hgm.symm, list.prod_cons, ← eq_inv_mul_iff_mul_eq] at hm₁; rwa [hm₁, mul_apply, mul_apply, cycle_of_inv, cycle_of_apply_of_same_cycle hsc, inv_apply_self, inv_eq_iff_eq, eq_comm]), hm₃⟩⟩ def cycle_factors [fintype α] [decidable_linear_order α] (f : perm α) : {l : list (perm α) // l.prod = f ∧ (∀ g ∈ l, is_cycle g) ∧ l.pairwise disjoint} := cycle_factors_aux (univ.sort (≤)) f (λ _ _, (mem_sort _).2 (mem_univ _)) end equiv.perm
ac35d89550ead505fa60d907d1a200c801ad81df
6b45072eb2b3db3ecaace2a7a0241ce81f815787
/data/list/comb.lean
80a97671506fee1ed487ca51e88e2cd163cea041
[]
no_license
avigad/library_dev
27b47257382667b5eb7e6476c4f5b0d685dd3ddc
9d8ac7c7798ca550874e90fed585caad030bbfac
refs/heads/master
1,610,452,468,791
1,500,712,839,000
1,500,713,478,000
69,311,142
1
0
null
1,474,942,903,000
1,474,942,902,000
null
UTF-8
Lean
false
false
19,936
lean
/- Copyright (c) 2015 Leonardo de Moura. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. αuthors: Leonardo de Moura, Haitao Zhang, Floris van Doorn, Jeremy Avigad List combinators. -/ -- TODO(Leo): uncomment data.equiv after refactoring import .basic ..bool data.list.comb --data.bool logic.basic -- data.equiv open nat prod decidable function namespace list universe variables uu vv ww variables {α : Type uu} {β : Type vv} {γ : Type ww} -- TODO(Jeremy): this file is a good testing ground for super and auto section replicate -- 'replicate n a' returns the list that contains n copies of a. def replicate : ℕ → α → list α | 0 a := [] | (succ n) a := a :: replicate n a @[simp] theorem length_replicate : ∀ (i : ℕ) (a : α), length (replicate i a) = i | 0 a := rfl | (succ i) a := congr_arg succ (length_replicate i a) end replicate /- map -/ attribute [simp] map_cons /-def map₂ (f : α → β → γ) : list α → list β → list γ | [] _ := [] | _ [] := [] | (x::xs) (y::ys) := f x y :: map₂ xs ys-/ theorem map₂_nil1 (f : α → β → γ) : ∀ (l : list β), map₂ f [] l = [] | [] := rfl | (a::y) := rfl theorem map₂_nil2 (f : α → β → γ) : ∀ (l : list α), map₂ f l [] = [] | [] := rfl | (a::y) := rfl /- TODO(Jeremy): there is an overload ambiguity between min and nat.min -/ /-theorem length_map₂ : ∀ (f : α → β → γ) x y, length (map₂ f x y) = _root_.min (length x) (length y) | f [] [] := rfl | f (xh::xr) [] := rfl | f [] (yh::yr) := rfl | f (xh::xr) (yh::yr) := calc length (map₂ f (xh::xr) (yh::yr)) = length (map₂ f xr yr) + 1 : rfl ... = _root_.min (length xr) (length yr) + 1 : by rw length_map₂ ... = _root_.min (succ (length xr)) (succ (length yr)) : begin rw min_succ_succ, reflexivity end ... = _root_.min (length (xh::xr)) (length (yh::yr)) : rfl-/ section foldl_eq_foldr -- foldl and foldr coincide when f is commutative and associative variables {f : α → α → α} (hcomm : ∀ a b, f a b = f b a) (hassoc : ∀ a b c, f (f a b) c = f a (f b c)) include hcomm hassoc theorem foldl_eq_of_comm_of_assoc : ∀ a b l, foldl f a (b::l) = f b (foldl f a l) | a b nil := hcomm a b | a b (c::l) := begin change foldl f (f (f a b) c) l = f b (foldl f (f a c) l), rw ←foldl_eq_of_comm_of_assoc, change foldl f (f (f a b) c) l = foldl f (f (f a c) b) l, have h₁ : f (f a b) c = f (f a c) b, { rw [hassoc, hassoc, hcomm b c] }, rw h₁ end theorem foldl_eq_foldr : ∀ a l, foldl f a l = foldr f a l | a nil := rfl | a (b :: l) := begin simp [foldl_eq_of_comm_of_assoc hcomm hassoc], change f b (foldl f a l) = f b (foldr f a l), rw (foldl_eq_foldr a l) end end foldl_eq_foldr /- all & any -/ @[simp] theorem all_nil (p : α → bool) : all [] p = tt := rfl @[simp] theorem all_cons (p : α → bool) (a : α) (l : list α) : all (a::l) p = (p a && all l p) := rfl theorem all_eq_tt_of_forall {p : α → bool} : ∀ {l : list α}, (∀ a ∈ l, p a = tt) → all l p = tt | [] h := all_nil p | (a::l) h := begin simp [all_cons, h a], rw all_eq_tt_of_forall, intros a ha, simp [h a, ha] end theorem forall_mem_eq_tt_of_all_eq_tt {p : α → bool} : ∀ {l : list α}, all l p = tt → ∀ a ∈ l, p a = tt | [] h := assume a h, absurd h (not_mem_nil a) | (b::l) h := assume a, assume : a ∈ b::l, begin simp [bool.band_eq_tt] at h, cases h with h₁ h₂, simp at this, cases this with h' h', simp [*], exact forall_mem_eq_tt_of_all_eq_tt h₂ _ h' end theorem all_eq_tt_iff {p : α → bool} {l : list α} : all l p = tt ↔ ∀ a ∈ l, p a = tt := iff.intro forall_mem_eq_tt_of_all_eq_tt all_eq_tt_of_forall @[simp] theorem any_nil (p : α → bool) : any [] p = ff := rfl @[simp] theorem any_cons (p : α → bool) (a : α) (l : list α) : any (a::l) p = (p a || any l p) := rfl theorem any_of_mem {p : α → bool} {a : α} : ∀ {l : list α}, a ∈ l → p a = tt → any l p = tt | [] i h := absurd i (not_mem_nil a) | (b::l) i h := or.elim (eq_or_mem_of_mem_cons i) (assume : a = b, begin simp [this^.symm, bool.bor_eq_tt], exact (or.inl h) end) (assume : a ∈ l, begin cases (eq_or_mem_of_mem_cons i) with h' h', { simp [h'^.symm, h] }, simp [bool.bor_eq_tt, any_of_mem h', h] end) theorem exists_of_any_eq_tt {p : α → bool} : ∀ {l : list α}, any l p = tt → ∃ a : α, a ∈ l ∧ p a | [] h := begin simp at h, contradiction end | (b::l) h := begin simp [bool.bor_eq_tt] at h, cases h with h h, { existsi b, simp [h]}, cases (exists_of_any_eq_tt h) with a ha, existsi a, apply (and.intro (or.inr ha^.left) ha^.right) end theorem any_eq_tt_iff {p : α → bool} {l : list α} : any l p = tt ↔ ∃ a : α, a ∈ l ∧ p a = tt := iff.intro exists_of_any_eq_tt (begin intro h, cases h with a ha, apply any_of_mem ha^.left ha^.right end) /- bounded quantifiers over lists -/ theorem forall_mem_nil (p : α → Prop) : ∀ x ∈ @nil α, p x := assume x xnil, absurd xnil (not_mem_nil x) theorem forall_mem_cons {p : α → Prop} {a : α} {l : list α} (pa : p a) (h : ∀ x ∈ l, p x) : ∀ x ∈ a :: l, p x := assume x xal, or.elim (eq_or_mem_of_mem_cons xal) (assume : x = a, by simp [this, pa]) (assume : x ∈ l, by simp [this, h]) theorem of_forall_mem_cons {p : α → Prop} {a : α} {l : list α} (h : ∀ x ∈ a :: l, p x) : p a := h a (by simp) theorem forall_mem_of_forall_mem_cons {p : α → Prop} {a : α} {l : list α} (h : ∀ x ∈ a :: l, p x) : ∀ x ∈ l, p x := assume x xl, h x (by simp [xl]) @[simp] theorem forall_mem_cons_iff (p : α → Prop) (a : α) (l : list α) : (∀ x ∈ a :: l, p x) ↔ p a ∧ ∀ x ∈ l, p x := iff.intro (λ h, ⟨of_forall_mem_cons h, forall_mem_of_forall_mem_cons h⟩) (λ h, forall_mem_cons h^.left h^.right) theorem not_exists_mem_nil (p : α → Prop) : ¬ ∃ x ∈ @nil α, p x := assume h, bexists.elim h (λ a anil, absurd anil (not_mem_nil a)) theorem exists_mem_cons_of {p : α → Prop} {a : α} (l : list α) (h : p a) : ∃ x ∈ a :: l, p x := bexists.intro a (by simp) h theorem exists_mem_cons_of_exists {p : α → Prop} {a : α} {l : list α} (h : ∃ x ∈ l, p x) : ∃ x ∈ a :: l, p x := bexists.elim h (λ x xl px, bexists.intro x (by simp [xl]) px) theorem or_exists_of_exists_mem_cons {p : α → Prop} {a : α} {l : list α} (h : ∃ x ∈ a :: l, p x) : p a ∨ ∃ x ∈ l, p x := bexists.elim h (λ x xal px, or.elim (eq_or_mem_of_mem_cons xal) (assume : x = a, begin rw ←this, simp [px] end) (assume : x ∈ l, or.inr (bexists.intro x this px))) @[simp] theorem exists_mem_cons_iff (p : α → Prop) (a : α) (l : list α) : (∃ x ∈ a :: l, p x) ↔ p a ∨ ∃ x ∈ l, p x := iff.intro or_exists_of_exists_mem_cons (assume h, or.elim h (exists_mem_cons_of l) exists_mem_cons_of_exists) @[instance] def decidable_forall_mem {p : α → Prop} [h : decidable_pred p] : ∀ l : list α, decidable (∀ x ∈ l, p x) | [] := is_true (forall_mem_nil p) | (a :: l) := decidable_of_decidable_of_iff (@and.decidable _ _ _ (decidable_forall_mem l)) (forall_mem_cons_iff p a l)^.symm @[instance] def decidable_exists_mem {p : α → Prop} [h : decidable_pred p] : ∀ l : list α, decidable (∃ x ∈ l, p x) | [] := is_false (not_exists_mem_nil p) | (a :: l) := decidable_of_decidable_of_iff (@or.decidable _ _ _ (decidable_exists_mem l)) (exists_mem_cons_iff p a l)^.symm /- zip & unzip -/ @[simp] theorem zip_cons_cons (a : α) (b : β) (l₁ : list α) (l₂ : list β) : zip (a :: l₁) (b :: l₂) = (a, b) :: zip l₁ l₂ := rfl @[simp] theorem zip_nil_left (l : list α) : zip ([] : list β) l = [] := rfl @[simp] theorem zip_nil_right (l : list α) : zip l ([] : list β) = [] := begin cases l, reflexivity, reflexivity end @[simp] theorem unzip_nil : unzip (@nil (α × β)) = ([], []) := rfl theorem unzip_cons' (a : α) (b : β) (l : list (α × β)) : unzip ((a, b) :: l) = match (unzip l) with (la, lb) := (a :: la, b :: lb) end := rfl -- TODO(Jeremy): it seems this version is better for the simplifier @[simp] theorem unzip_cons (a : α) (b : β) (l : list (α × β)) : unzip ((a, b) :: l) = let p := unzip l in (a :: p.1, b :: p.2) := begin rw unzip_cons', cases unzip l, reflexivity end theorem zip_unzip : ∀ (l : list (α × β)), zip (unzip l).1 (unzip l).2 = l | [] := rfl | ((a, b) :: l) := begin simp [zip_unzip l] end -- TODO(Jeremy): this is as far as I got section mapAccumR variable {S : Type} -- This runs a function over a list returning the intermediate results and a -- a final result. def mapAccumR : (α → S → S × β) → list α → S → (S × list β) | f [] c := (c, []) | f (y::yr) c := let r := mapAccumR f yr c in let z := f y r.1 in (z.1, z.2 :: r.2) theorem length_mapAccumR : ∀ (f : α → S → S × β) (x : list α) (s : S), length (mapAccumR f x s).2 = length x | f (a::x) s := calc length (snd (mapAccumR f (a::x) s)) = length x + 1 : begin rw ←(length_mapAccumR f x s), reflexivity end ... = length (a::x) : rfl | f [] s := calc length (snd (mapAccumR f [] s)) = 0 : by reflexivity end mapAccumR section mapAccumR₂ variable {S : Type uu} -- This runs a function over two lists returning the intermediate results and a -- a final result. def mapAccumR₂ : (α → β → S → S × γ) → list α → list β → S → S × list γ | f [] _ c := (c,[]) | f _ [] c := (c,[]) | f (x::xr) (y::yr) c := let r := mapAccumR₂ f xr yr c in let q := f x y r.1 in (q.1, q.2 :: r.2) -- TODO(Jeremy) : again the "min" overload theorem length_mapAccumR₂ : ∀ (f : α → β → S → S × γ) (x : list α) (y : list β) (c : S), length (mapAccumR₂ f x y c).2 = _root_.min (length x) (length y) | f (a::x) (b::y) c := calc length (snd (mapAccumR₂ f (a::x) (b::y) c)) = length (snd (mapAccumR₂ f x y c)) + 1 : rfl ... = _root_.min (length x) (length y) + 1 : by rw (length_mapAccumR₂ f x y c) ... = _root_.min (succ (length x)) (succ (length y)) : begin rw min_succ_succ end ... = _root_.min (length (a::x)) (length (b::y)) : rfl | f (a::x) [] c := rfl | f [] (b::y) c := rfl | f [] [] c := rfl end mapAccumR₂ /- flat -/ def flat (l : list (list α)) : list α := foldl append nil l /- product -/ section product def product : list α → list β → list (α × β) | [] l₂ := [] | (a::l₁) l₂ := map (λ b, (a, b)) l₂ ++ product l₁ l₂ theorem nil_product (l : list β) : product (@nil α) l = [] := rfl theorem product_cons (a : α) (l₁ : list α) (l₂ : list β) : product (a::l₁) l₂ = map (λ b, (a, b)) l₂ ++ product l₁ l₂ := rfl theorem product_nil : ∀ (l : list α), product l (@nil β) = [] | [] := rfl | (a::l) := begin rw [product_cons, product_nil], reflexivity end theorem eq_of_mem_map_pair₁ {a₁ a : α} {b₁ : β} {l : list β} : (a₁, b₁) ∈ map (λ b, (a, b)) l → a₁ = a := assume ain, have fst (a₁, b₁) ∈ map fst (map (λ b, (a, b)) l), from mem_map fst ain, have a₁ ∈ map (λb, a) l, begin revert this, rw [map_map], intro this, assumption end, eq_of_map_const this theorem mem_of_mem_map_pair₁ {a₁ a : α} {b₁ : β} {l : list β} : (a₁, b₁) ∈ map (λ b, (a, b)) l → b₁ ∈ l := assume ain, have snd (a₁, b₁) ∈ map snd (map (λ b, (a, b)) l), from mem_map snd ain, have b₁ ∈ map id l, begin rw [map_map] at this, exact this end, begin rw [map_id] at this, exact this end theorem mem_product {a : α} {b : β} : ∀ {l₁ l₂}, a ∈ l₁ → b ∈ l₂ → (a, b) ∈ @product α β l₁ l₂ | [] l₂ h₁ h₂ := absurd h₁ (not_mem_nil _) | (x::l₁) l₂ h₁ h₂ := or.elim (eq_or_mem_of_mem_cons h₁) (assume aeqx : a = x, have (a, b) ∈ map (λ b, (a, b)) l₂, from mem_map _ h₂, begin rw [←aeqx, product_cons], exact mem_append_left _ this end) (assume ainl₁ : a ∈ l₁, have (a, b) ∈ product l₁ l₂, from mem_product ainl₁ h₂, begin rw [product_cons], exact mem_append_right _ this end) theorem mem_of_mem_product_left {a : α} {b : β} : ∀ {l₁ l₂}, (a, b) ∈ @product α β l₁ l₂ → a ∈ l₁ | [] l₂ h := absurd h (not_mem_nil _) | (x::l₁) l₂ h := or.elim (mem_or_mem_of_mem_append h) (assume : (a, b) ∈ map (λ b, (x, b)) l₂, have a = x, from eq_of_mem_map_pair₁ this, begin rw this, apply mem_cons_self end) (assume : (a, b) ∈ product l₁ l₂, have a ∈ l₁, from mem_of_mem_product_left this, mem_cons_of_mem _ this) theorem mem_of_mem_product_right {a : α} {b : β} : ∀ {l₁ l₂}, (a, b) ∈ @product α β l₁ l₂ → b ∈ l₂ | [] l₂ h := absurd h (not_mem_nil ((a, b))) | (x::l₁) l₂ h := or.elim (mem_or_mem_of_mem_append h) (assume : (a, b) ∈ map (λ b, (x, b)) l₂, mem_of_mem_map_pair₁ this) (assume : (a, b) ∈ product l₁ l₂, mem_of_mem_product_right this) theorem length_product : ∀ (l₁ : list α) (l₂ : list β), length (product l₁ l₂) = length l₁ * length l₂ | [] l₂ := begin simp, reflexivity end | (x::l₁) l₂ := have length (product l₁ l₂) = length l₁ * length l₂, from length_product l₁ l₂, by rw [product_cons, length_append, length_cons, length_map, this, right_distrib, one_mul, add_comm] end product -- new for list/comb dependent map theory def dinj₁ (p : α → Prop) (f : Π a, p a → β) := ∀ ⦃a1 a2⦄ (h1 : p a1) (h2 : p a2), a1 ≠ a2 → (f a1 h1) ≠ (f a2 h2) def dinj (p : α → Prop) (f : Π a, p a → β) := ∀ ⦃a1 a2⦄ (h1 : p a1) (h2 : p a2), (f a1 h1) = (f a2 h2) → a1 = a2 def dmap (p : α → Prop) [h : decidable_pred p] (f : Π a, p a → β) : list α → list β | [] := [] | (a::l) := if P : (p a) then cons (f a P) (dmap l) else (dmap l) -- properties of dmap section dmap variable {p : α → Prop} variable [h : decidable_pred p] include h variable {f : Π a, p a → β} lemma dmap_nil : dmap p f [] = [] := rfl lemma dmap_cons_of_pos {a : α} (P : p a) : ∀ l, dmap p f (a::l) = (f a P) :: dmap p f l := λ l, dif_pos P lemma dmap_cons_of_neg {a : α} (P : ¬ p a) : ∀ l, dmap p f (a::l) = dmap p f l := λ l, dif_neg P lemma mem_dmap : ∀ {l : list α} {a} (Pa : p a), a ∈ l → (f a Pa) ∈ dmap p f l | [] := assume a Pa Pinnil, absurd Pinnil (not_mem_nil _) | (a::l) := assume b Pb Pbin, or.elim (eq_or_mem_of_mem_cons Pbin) (assume Pbeqa, begin rw [eq.symm Pbeqa, dmap_cons_of_pos Pb], apply mem_cons_self end) (assume Pbinl, if pa : p a then begin rw [dmap_cons_of_pos pa], apply mem_cons_of_mem, exact mem_dmap Pb Pbinl end else begin rw [dmap_cons_of_neg pa], exact mem_dmap Pb Pbinl end) lemma exists_of_mem_dmap : ∀ {l : list α} {b : β}, b ∈ dmap p f l → ∃ a P, a ∈ l ∧ b = f a P | [] := assume b, begin rw dmap_nil, intro h, exact absurd h (not_mem_nil _) end | (a::l) := assume b, if Pa : p a then begin rw [dmap_cons_of_pos Pa, mem_cons_iff], intro Pb, cases Pb with Peq Pin, exact exists.intro a (exists.intro Pa (and.intro (mem_cons_self _ _) Peq)), have Pex : ∃ (a : α) (P : p a), a ∈ l ∧ b = f a P, exact exists_of_mem_dmap Pin, cases Pex with a' Pex', cases Pex' with Pa' P', exact exists.intro a' (exists.intro Pa' (and.intro (mem_cons_of_mem a (and.left P')) (and.right P'))) end else begin rw [dmap_cons_of_neg Pa], intro Pin, have Pex : ∃ (a : α) (P : p a), a ∈ l ∧ b = f a P, exact exists_of_mem_dmap Pin, cases Pex with a' Pex', cases Pex' with Pa' P', exact exists.intro a' (exists.intro Pa' (and.intro (mem_cons_of_mem a (and.left P')) (and.right P'))) end lemma map_dmap_of_inv_of_pos {g : β → α} (Pinv : ∀ a (Pa : p a), g (f a Pa) = a) : ∀ {l : list α}, (∀ ⦃a⦄, a ∈ l → p a) → map g (dmap p f l) = l | [] := assume Pl, by rw [dmap_nil]; reflexivity | (a::l) := assume Pal, have Pa : p a, from Pal (mem_cons_self _ _), have Pl : ∀ a, a ∈ l → p a, from assume x Pxin, Pal (mem_cons_of_mem a Pxin), by rw [dmap_cons_of_pos Pa, map_cons, Pinv, map_dmap_of_inv_of_pos Pl] lemma mem_of_dinj_of_mem_dmap (Pdi : dinj p f) : ∀ {l : list α} {a} (Pa : p a), (f a Pa) ∈ dmap p f l → a ∈ l | [] := assume a Pa Pinnil, absurd Pinnil (not_mem_nil _) | (b::l) := assume a Pa Pmap, if Pb : p b then begin rw (dmap_cons_of_pos Pb) at Pmap, rw mem_cons_iff at Pmap, rw mem_cons_iff, cases Pmap with h h, left, apply Pdi Pa Pb h, right, apply mem_of_dinj_of_mem_dmap Pa h end else begin rw (dmap_cons_of_neg Pb) at Pmap, apply mem_cons_of_mem, exact mem_of_dinj_of_mem_dmap Pa Pmap end lemma not_mem_dmap_of_dinj_of_not_mem (Pdi : dinj p f) {l : list α} {a} (Pa : p a) : a ∉ l → (f a Pa) ∉ dmap p f l := mt (mem_of_dinj_of_mem_dmap Pdi Pa) end dmap /- section open equiv def list_equiv_of_equiv {α β : Type} : α ≃ β → list α ≃ list β | (mk f g l r) := mk (map f) (map g) begin intros, rw [map_map, id_of_left_inverse l, map_id], try reflexivity end begin intros, rw [map_map, id_of_right_inverse r, map_id], try reflexivity end private def to_nat : list nat → nat | [] := 0 | (x::xs) := succ (mkpair (to_nat xs) x) open prod.ops private def of_nat.F : Π (n : nat), (Π m, m < n → list nat) → list nat | 0 f := [] | (succ n) f := (unpair n).2 :: f (unpair n).1 (unpair_lt n) private def of_nat : nat → list nat := well_founded.fix of_nat.F private lemma of_nat_zero : of_nat 0 = [] := well_founded.fix_eq of_nat.F 0 private lemma of_nat_succ (n : nat) : of_nat (succ n) = (unpair n).2 :: of_nat (unpair n).1 := well_founded.fix_eq of_nat.F (succ n) private lemma to_nat_of_nat (n : nat) : to_nat (of_nat n) = n := nat.case_strong_induction_on n _ (λ n ih, begin rw of_nat_succ, unfold to_nat, have to_nat (of_nat (unpair n).1) = (unpair n).1, from ih _ (le_of_lt_succ (unpair_lt n)), rw this, rw mkpair_unpair end) private lemma of_nat_to_nat : ∀ (l : list nat), of_nat (to_nat l) = l | [] := rfl | (x::xs) := begin unfold to_nat, rw of_nat_succ, rw *unpair_mkpair, esimp, congruence, apply of_nat_to_nat end def list_nat_equiv_nat : list nat ≃ nat := mk to_nat of_nat of_nat_to_nat to_nat_of_nat def list_equiv_self_of_equiv_nat {α : Type} : α ≃ nat → list α ≃ α := assume : α ≃ nat, calc list α ≃ list nat : list_equiv_of_equiv this ... ≃ nat : list_nat_equiv_nat ... ≃ α : this end -/ end list
8088600cac77fe9c4a7d4035a02c8340989ac142
e61a235b8468b03aee0120bf26ec615c045005d2
/stage0/src/Init/ShareCommon.lean
faca73ca5e7c0c4196b2d784e006494fbcc143a9
[ "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
6,470
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.Util import Init.Data.HashMap import Init.Data.HashSet import Init.Data.PersistentHashSet import Init.Data.PersistentHashMap import Init.Control.State universes u v namespace ShareCommon /- The max sharing primitives are implemented internally. They use maps and sets of Lean objects. We have two versions: one using `HashMap` and `HashSet`, and another using `PersistentHashMap` and `PersistentHashSet`. These maps and sets are "instantiated here using the "unsafe" primitives `Object.eq`, `Object.hash`, and `ptrAddrUnsafe`. -/ abbrev Object : Type := NonScalar unsafe def Object.ptrEq (a b : Object) : Bool := ptrAddrUnsafe a == ptrAddrUnsafe b unsafe def Object.ptrHash (a : Object) : USize := ptrAddrUnsafe a @[extern "lean_sharecommon_eq"] unsafe constant Object.eq (a b : @& Object) : Bool := arbitrary _ @[extern "lean_sharecommon_hash"] unsafe constant Object.hash (a : @& Object) : USize := arbitrary _ unsafe def ObjectMap : Type := @HashMap Object Object ⟨Object.ptrEq⟩ ⟨Object.ptrHash⟩ unsafe def ObjectSet : Type := @HashSet Object ⟨Object.eq⟩ ⟨Object.hash⟩ unsafe def ObjectPersistentMap : Type := @PersistentHashMap Object Object ⟨Object.ptrEq⟩ ⟨Object.ptrHash⟩ unsafe def ObjectPersistentSet : Type := @PersistentHashSet Object ⟨Object.eq⟩ ⟨Object.hash⟩ @[export lean_mk_object_map] unsafe def mkObjectMap : Unit → ObjectMap := fun _ => @mkHashMap Object Object ⟨Object.ptrEq⟩ ⟨Object.ptrHash⟩ 1024 @[export lean_object_map_find] unsafe def ObjectMap.find? (m : ObjectMap) (k : Object) : Option Object := @HashMap.find? Object Object ⟨Object.ptrEq⟩ ⟨Object.ptrHash⟩ m k @[export lean_object_map_insert] unsafe def ObjectMap.insert (m : ObjectMap) (k v : Object) : ObjectMap := @HashMap.insert Object Object ⟨Object.ptrEq⟩ ⟨Object.ptrHash⟩ m k v @[export lean_mk_object_set] unsafe def mkObjectSet : Unit → ObjectSet := fun _ => @mkHashSet Object ⟨Object.eq⟩ ⟨Object.hash⟩ 1024 @[export lean_object_set_find] unsafe def ObjectSet.find? (s : ObjectSet) (o : Object) : Option Object := @HashSet.find? Object ⟨Object.eq⟩ ⟨Object.hash⟩ s o @[export lean_object_set_insert] unsafe def ObjectSet.insert (s : ObjectSet) (o : Object) : ObjectSet := @HashSet.insert Object ⟨Object.eq⟩ ⟨Object.hash⟩ s o @[export lean_mk_object_pmap] unsafe def mkObjectPersistentMap : Unit → ObjectPersistentMap := fun _ => @PersistentHashMap.empty Object Object ⟨Object.ptrEq⟩ ⟨Object.ptrHash⟩ @[export lean_object_pmap_find] unsafe def ObjectPersistentMap.find? (m : ObjectPersistentMap) (k : Object) : Option Object := @PersistentHashMap.find? Object Object ⟨Object.ptrEq⟩ ⟨Object.ptrHash⟩ m k @[export lean_object_pmap_insert] unsafe def ObjectPersistentMap.insert (m : ObjectPersistentMap) (k v : Object) : ObjectPersistentMap := @PersistentHashMap.insert Object Object ⟨Object.ptrEq⟩ ⟨Object.ptrHash⟩ m k v @[export lean_mk_object_pset] unsafe def mkObjectPersistentSet : Unit → ObjectPersistentSet := fun _ => @PersistentHashSet.empty Object ⟨Object.eq⟩ ⟨Object.hash⟩ @[export lean_object_pset_find] unsafe def ObjectPersistentSet.find? (s : ObjectPersistentSet) (o : Object) : Option Object := @PersistentHashSet.find? Object ⟨Object.eq⟩ ⟨Object.hash⟩ s o @[export lean_object_pset_insert] unsafe def ObjectPersistentSet.insert (s : ObjectPersistentSet) (o : Object) : ObjectPersistentSet := @PersistentHashSet.insert Object ⟨Object.eq⟩ ⟨Object.hash⟩ s o /- Internally `State` is implemented as a pair `ObjectMap` and `ObjectSet` -/ constant StatePointed : PointedType := arbitrary _ abbrev State : Type u := StatePointed.type @[extern "lean_sharecommon_mk_state"] constant mkState : Unit → State := fun _ => StatePointed.val def State.empty : State := mkState () instance State.inhabited : Inhabited State := ⟨State.empty⟩ /- Internally `PersistentState` is implemented as a pair `ObjectPersistentMap` and `ObjectPersistentSet` -/ constant PersistentStatePointed : PointedType := arbitrary _ abbrev PersistentState : Type u := PersistentStatePointed.type @[extern "lean_sharecommon_mk_pstate"] constant mkPersistentState : Unit → PersistentState := fun _ => PersistentStatePointed.val def PersistentState.empty : PersistentState := mkPersistentState () instance PersistentState.inhabited : Inhabited PersistentState := ⟨PersistentState.empty⟩ abbrev PState : Type u := PersistentState @[extern "lean_state_sharecommon"] def State.shareCommon {α} (s : State) (a : α) : α × State := (a, s) @[extern "lean_persistent_state_sharecommon"] def PersistentState.shareCommon {α} (s : PersistentState) (a : α) : α × PersistentState := (a, s) end ShareCommon class MonadShareCommon (m : Type u → Type v) := (withShareCommon {α : Type u} : α → m α) export MonadShareCommon (withShareCommon) abbrev shareCommonM {α : Type u} {m : Type u → Type v} [MonadShareCommon m] (a : α) : m α := withShareCommon a abbrev ShareCommonT (m : Type u → Type v) := StateT ShareCommon.State m abbrev PShareCommonT (m : Type u → Type v) := StateT ShareCommon.PState m abbrev ShareCommonM := ShareCommonT Id abbrev PShareCommonM := PShareCommonT Id @[specialize] def ShareCommonT.withShareCommon {m : Type u → Type v} [Monad m] {α : Type u} (a : α) : ShareCommonT m α := modifyGet $ fun s => s.shareCommon a @[specialize] def PShareCommonT.withShareCommon {m : Type u → Type v} [Monad m] {α : Type u} (a : α) : PShareCommonT m α := modifyGet $ fun s => s.shareCommon a instance ShareCommonT.monadShareCommon {m : Type u → Type v} [Monad m] : MonadShareCommon (ShareCommonT m) := { withShareCommon := @ShareCommonT.withShareCommon _ _ } instance PShareCommonT.monadShareCommon {m : Type u → Type v} [Monad m] : MonadShareCommon (PShareCommonT m) := { withShareCommon := @PShareCommonT.withShareCommon _ _ } @[inline] def ShareCommonT.run {m : Type u → Type v} [Monad m] {α : Type u} (x : ShareCommonT m α) : m α := x.run' ShareCommon.State.empty @[inline] def PShareCommonT.run {m : Type u → Type v} [Monad m] {α : Type u} (x : PShareCommonT m α) : m α := x.run' ShareCommon.PersistentState.empty def shareCommon {α} (a : α) : α := (withShareCommon a : ShareCommonM α).run
46bfab5187aea5c77d6a9c9f931c2c3c4c6a597b
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/order/heyting/boundary.lean
e267bffe81795d70985aeb34b550a8d76df64076
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
4,599
lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import order.boolean_algebra /-! # Co-Heyting boundary > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. The boundary of an element of a co-Heyting algebra is the intersection of its Heyting negation with itself. The boundary in the co-Heyting algebra of closed sets coincides with the topological boundary. ## Main declarations * `coheyting.boundary`: Co-Heyting boundary. `coheyting.boundary a = a ⊓ ¬a` ## Notation `∂ a` is notation for `coheyting.boundary a` in locale `heyting`. -/ variables {α : Type*} namespace coheyting variables [coheyting_algebra α] {a b : α} /-- The boundary of an element of a co-Heyting algebra is the intersection of its Heyting negation with itself. Note that this is always `⊥` for a boolean algebra. -/ def boundary (a : α) : α := a ⊓ ¬a localized "prefix `∂ `:120 := coheyting.boundary" in heyting lemma inf_hnot_self (a : α) : a ⊓ ¬a = ∂ a := rfl lemma boundary_le : ∂ a ≤ a := inf_le_left lemma boundary_le_hnot : ∂ a ≤ ¬a := inf_le_right @[simp] lemma boundary_bot : ∂ (⊥ : α) = ⊥ := bot_inf_eq @[simp] lemma boundary_top : ∂ (⊤ : α) = ⊥ := by rw [boundary, hnot_top, inf_bot_eq] lemma boundary_hnot_le (a : α) : ∂ ¬a ≤ ∂ a := inf_comm.trans_le $ inf_le_inf_right _ hnot_hnot_le @[simp] lemma boundary_hnot_hnot (a : α) : ∂ ¬¬a = ∂ ¬a := by simp_rw [boundary, hnot_hnot_hnot, inf_comm] @[simp] lemma hnot_boundary (a : α) : ¬ ∂ a = ⊤ := by rw [boundary, hnot_inf_distrib, sup_hnot_self] /-- **Leibniz rule** for the co-Heyting boundary. -/ lemma boundary_inf (a b : α) : ∂ (a ⊓ b) = ∂ a ⊓ b ⊔ a ⊓ ∂ b := by { unfold boundary, rw [hnot_inf_distrib, inf_sup_left, inf_right_comm, ←inf_assoc] } lemma boundary_inf_le : ∂ (a ⊓ b) ≤ ∂ a ⊔ ∂ b := (boundary_inf _ _).trans_le $ sup_le_sup inf_le_left inf_le_right lemma boundary_sup_le : ∂ (a ⊔ b) ≤ ∂ a ⊔ ∂ b := begin rw [boundary, inf_sup_right], exact sup_le_sup (inf_le_inf_left _ $ hnot_anti le_sup_left) (inf_le_inf_left _ $ hnot_anti le_sup_right), end /- The intuitionistic version of `coheyting.boundary_le_boundary_sup_sup_boundary_inf_left`. Either proof can be obtained from the other using the equivalence of Heyting algebras and intuitionistic logic and duality between Heyting and co-Heyting algebras. It is crucial that the following proof be intuitionistic. -/ example (a b : Prop) : ((a ∧ b) ∨ ¬(a ∧ b)) ∧ ((a ∨ b) ∨ ¬ (a ∨ b)) → a ∨ ¬ a := begin rintro ⟨⟨ha, hb⟩ | hnab, (ha | hb) | hnab⟩; try { exact or.inl ha }, { exact or.inr (λ ha, hnab ⟨ha, hb⟩) }, { exact or.inr (λ ha, hnab $ or.inl ha) } end lemma boundary_le_boundary_sup_sup_boundary_inf_left : ∂ a ≤ ∂ (a ⊔ b) ⊔ ∂ (a ⊓ b) := begin simp only [boundary, sup_inf_left, sup_inf_right, sup_right_idem, le_inf_iff, sup_assoc, @sup_comm _ _ _ a], refine ⟨⟨⟨_, _⟩, _⟩, ⟨_, _⟩, _⟩; try { exact le_sup_of_le_left inf_le_left }; refine inf_le_of_right_le _, { rw [hnot_le_iff_codisjoint_right, codisjoint_left_comm], exact codisjoint_hnot_left }, { refine le_sup_of_le_right _, rw hnot_le_iff_codisjoint_right, exact codisjoint_hnot_right.mono_right (hnot_anti inf_le_left) } end lemma boundary_le_boundary_sup_sup_boundary_inf_right : ∂ b ≤ ∂ (a ⊔ b) ⊔ ∂ (a ⊓ b) := by { rw [@sup_comm _ _ a, inf_comm], exact boundary_le_boundary_sup_sup_boundary_inf_left } lemma boundary_sup_sup_boundary_inf (a b : α) : ∂ (a ⊔ b) ⊔ ∂ (a ⊓ b) = ∂ a ⊔ ∂ b := le_antisymm (sup_le boundary_sup_le boundary_inf_le) $ sup_le boundary_le_boundary_sup_sup_boundary_inf_left boundary_le_boundary_sup_sup_boundary_inf_right @[simp] lemma boundary_idem (a : α) : ∂ ∂ a = ∂ a := by rw [boundary, hnot_boundary, inf_top_eq] lemma hnot_hnot_sup_boundary (a : α) : ¬¬a ⊔ ∂ a = a := by { rw [boundary, sup_inf_left, hnot_sup_self, inf_top_eq, sup_eq_right], exact hnot_hnot_le } lemma hnot_eq_top_iff_exists_boundary : ¬a = ⊤ ↔ ∃ b, ∂ b = a := ⟨λ h, ⟨a, by rw [boundary, h, inf_top_eq]⟩, by { rintro ⟨b, rfl⟩, exact hnot_boundary _ }⟩ end coheyting open_locale heyting section boolean_algebra variables [boolean_algebra α] @[simp] lemma coheyting.boundary_eq_bot (a : α) : ∂ a = ⊥ := inf_compl_eq_bot end boolean_algebra
d5956ba0b4c2dc3cb382511d19aaff6b4380c8a3
3bdd27ffdff3ffa22d4bb010eba695afcc96bc4a
/src/combinatorics/simplicial_complex/to_move/set.lean
728119a29894007c38cca7c6971f0a8233df8d37
[]
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,304
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 data.set.lattice namespace set variables {α : Type*} {s t : set α} lemma disjoint_iff_subset_compl_right : disjoint s t ↔ s ⊆ tᶜ := disjoint_left lemma disjoint_iff_subset_compl_left : disjoint s t ↔ t ⊆ sᶜ := disjoint_right lemma subset_singleton_iff' (s : set α) (a : α) : s ⊆ {a} ↔ s = ∅ ∨ s = {a} := begin obtain (rfl | hs) := s.eq_empty_or_nonempty, { simp only [forall_false_left, mem_empty_eq, subset_singleton_iff, implies_true_iff, true_or, eq_self_iff_true]}, { simp [eq_singleton_iff_nonempty_unique_mem, hs, ne_empty_iff_nonempty.2 hs] } end lemma ssubset_singleton_iff_eq_empty (x : α) (X : set α) : X ⊂ {x} ↔ X = ∅ := begin rw [ssubset_iff_subset_ne, subset_singleton_iff', or_and_distrib_right], simp only [or_false, and_iff_left_iff_imp, and_not_self], rintro rfl, rw [ne_comm, ne_empty_iff_nonempty], apply singleton_nonempty, end theorem sdiff_union_of_subset {s₁ s₂ : set α} (h : s₁ ⊆ s₂) : (s₂ \ s₁) ∪ s₁ = s₂ := set.ext $ λ x, by simpa [em, or_comm, or_and_distrib_left] using or_iff_right_of_imp (@h x) end set
7dcda55986178058ec54cfb8d9865758bb1e5ac5
8f22844295dd3def1014f96b39d9ea3c9e64d52c
/chapter2.lean
e89074167cfaac45d18c8a41a807b367976794b7
[]
no_license
saik2121/Lean-Theorem-Prover
a7719542d9ee1d3b062395d730c577b64f35bc19
02f6304d2dd68898ad611b2ea6892c98ed991a6b
refs/heads/main
1,676,011,323,560
1,609,954,593,000
1,609,954,593,000
326,745,338
0
0
null
null
null
null
UTF-8
Lean
false
false
1,022
lean
-- definitions --first way of defining def succesor : ℕ -> ℕ := λ (x: ℕ) , x+5 #reduce succesor 5 def succesor1 (x: ℕ ): ℕ :=x+5 #check succesor1 5 --lamda abstractions #check λ x:nat,x+5 #check λ (x: ℕ )(y: ℕ ),x+y --Applying lambda abstarctions as functions --constants α β : Type --constant p : α --constant q : β --#check λ (x: α)( y: β ),x --#check (λ (x: α)( y: β ),x) p q --Using let functions #reduce let y:= 2+2 in y*y def t (x: ℕ ): ℕ := let y:= x+x in y*y #eval t 2 --Variables and sections --def compose (α β γ : Type) (g : α -> β ) ( f : γ -> α ) ( x: γ ):β := g (f x) variables (α β γ : Type*) variable x : α def compose (g : β → γ) (f : α → β) (x : α) : γ := g (f x) --Namespaces namespace foo def f (x: ℕ ) : ℕ := x+7 #check f end foo #check foo.f --Nested namespaces namespace f1 def b (a : bool)( c: bool) := tt && (a && c) #check b namespace f2 def b (a : nat)( c: nat) := tt #check b end f2 #check b #check f2.b end f1
42ff928d3010dc2af0e9b051deafa92ad45af9d6
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/topology/metric_space/gromov_hausdorff.lean
727e67aed4cc547ecf0528177d350905db856ae4
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
55,775
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Sébastien Gouëzel -/ import topology.metric_space.closeds set_theory.cardinal topology.metric_space.gromov_hausdorff_realized topology.metric_space.completion /-! # Gromov-Hausdorff distance This file defines the Gromov-Hausdorff distance on the space of nonempty compact metric spaces up to isometry. We introduce the space of all nonempty compact metric spaces, up to isometry, called `GH_space`, and endow it with a metric space structure. The distance, known as the Gromov-Hausdorff distance, is defined as follows: given two nonempty compact spaces `X` and `Y`, their distance is the minimum Hausdorff distance between all possible isometric embeddings of `X` and `Y` in all metric spaces. To define properly the Gromov-Hausdorff space, we consider the non-empty compact subsets of `ℓ^∞(ℝ)` up to isometry, which is a well-defined type, and define the distance as the infimum of the Hausdorff distance over all embeddings in `ℓ^∞(ℝ)`. We prove that this coincides with the previous description, as all separable metric spaces embed isometrically into `ℓ^∞(ℝ)`, through an embedding called the Kuratowski embedding. To prove that we have a distance, we should show that if spaces can be coupled to be arbitrarily close, then they are isometric. More generally, the Gromov-Hausdorff distance is realized, i.e., there is a coupling for which the Hausdorff distance is exactly the Gromov-Hausdorff distance. This follows from a compactness argument, essentially following from Arzela-Ascoli. ## Main results We prove the most important properties of the Gromov-Hausdorff space: it is a polish space, i.e., it is complete and second countable. We also prove the Gromov compactness criterion. -/ noncomputable theory open_locale classical topological_space universes u v w open classical set function topological_space filter metric quotient open bounded_continuous_function nat Kuratowski_embedding open sum (inl inr) set_option class.instance_max_depth 50 local attribute [instance] metric_space_sum namespace Gromov_Hausdorff section GH_space /- In this section, we define the Gromov-Hausdorff space, denoted `GH_space` as the quotient of nonempty compact subsets of `ℓ^∞(ℝ)` by identifying isometric sets. Using the Kuratwoski embedding, we get a canonical map `to_GH_space` mapping any nonempty compact type to `GH_space`. -/ /-- Equivalence relation identifying two nonempty compact sets which are isometric -/ private definition isometry_rel : nonempty_compacts ℓ_infty_ℝ → nonempty_compacts ℓ_infty_ℝ → Prop := λx y, nonempty (x.val ≃ᵢ y.val) /-- This is indeed an equivalence relation -/ private lemma is_equivalence_isometry_rel : equivalence isometry_rel := ⟨λx, ⟨isometric.refl _⟩, λx y ⟨e⟩, ⟨e.symm⟩, λ x y z ⟨e⟩ ⟨f⟩, ⟨e.trans f⟩⟩ /-- setoid instance identifying two isometric nonempty compact subspaces of ℓ^∞(ℝ) -/ instance isometry_rel.setoid : setoid (nonempty_compacts ℓ_infty_ℝ) := setoid.mk isometry_rel is_equivalence_isometry_rel /-- The Gromov-Hausdorff space -/ definition GH_space : Type := quotient (isometry_rel.setoid) /-- Map any nonempty compact type to `GH_space` -/ definition to_GH_space (α : Type u) [metric_space α] [compact_space α] [nonempty α] : GH_space := ⟦nonempty_compacts.Kuratowski_embedding α⟧ instance : inhabited GH_space := ⟨quot.mk _ ⟨{0}, by simp⟩⟩ /-- A metric space representative of any abstract point in `GH_space` -/ definition GH_space.rep (p : GH_space) : Type := (quot.out p).val lemma eq_to_GH_space_iff {α : Type u} [metric_space α] [compact_space α] [nonempty α] {p : nonempty_compacts ℓ_infty_ℝ} : ⟦p⟧ = to_GH_space α ↔ ∃Ψ : α → ℓ_infty_ℝ, isometry Ψ ∧ range Ψ = p.val := begin simp only [to_GH_space, quotient.eq], split, { assume h, rcases setoid.symm h with ⟨e⟩, have f := (Kuratowski_embedding.isometry α).isometric_on_range.trans e, use λx, f x, split, { apply isometry_subtype_val.comp f.isometry }, { rw [range_comp, f.range_coe, set.image_univ, set.range_coe_subtype] } }, { rintros ⟨Ψ, ⟨isomΨ, rangeΨ⟩⟩, have f := ((Kuratowski_embedding.isometry α).isometric_on_range.symm.trans isomΨ.isometric_on_range).symm, have E : (range Ψ ≃ᵢ (nonempty_compacts.Kuratowski_embedding α).val) = (p.val ≃ᵢ range (Kuratowski_embedding α)), by { dunfold nonempty_compacts.Kuratowski_embedding, rw [rangeΨ]; refl }, have g := cast E f, exact ⟨g⟩ } end lemma eq_to_GH_space {p : nonempty_compacts ℓ_infty_ℝ} : ⟦p⟧ = to_GH_space p.val := begin refine eq_to_GH_space_iff.2 ⟨((λx, x) : p.val → ℓ_infty_ℝ), _, subtype.val_range⟩, apply isometry_subtype_val end section local attribute [reducible] GH_space.rep instance rep_GH_space_metric_space {p : GH_space} : metric_space (p.rep) := by apply_instance instance rep_GH_space_compact_space {p : GH_space} : compact_space (p.rep) := by apply_instance instance rep_GH_space_nonempty {p : GH_space} : nonempty (p.rep) := by apply_instance end lemma GH_space.to_GH_space_rep (p : GH_space) : to_GH_space (p.rep) = p := begin change to_GH_space (quot.out p).val = p, rw ← eq_to_GH_space, exact quot.out_eq p end /-- Two nonempty compact spaces have the same image in `GH_space` if and only if they are isometric -/ lemma to_GH_space_eq_to_GH_space_iff_isometric {α : Type u} [metric_space α] [compact_space α] [nonempty α] {β : Type u} [metric_space β] [compact_space β] [nonempty β] : to_GH_space α = to_GH_space β ↔ nonempty (α ≃ᵢ β) := ⟨begin simp only [to_GH_space, quotient.eq], assume h, rcases h with e, have I : ((nonempty_compacts.Kuratowski_embedding α).val ≃ᵢ (nonempty_compacts.Kuratowski_embedding β).val) = ((range (Kuratowski_embedding α)) ≃ᵢ (range (Kuratowski_embedding β))), by { dunfold nonempty_compacts.Kuratowski_embedding, refl }, have e' := cast I e, have f := (Kuratowski_embedding.isometry α).isometric_on_range, have g := (Kuratowski_embedding.isometry β).isometric_on_range.symm, have h := (f.trans e').trans g, exact ⟨h⟩ end, begin rintros ⟨e⟩, simp only [to_GH_space, quotient.eq], have f := (Kuratowski_embedding.isometry α).isometric_on_range.symm, have g := (Kuratowski_embedding.isometry β).isometric_on_range, have h := (f.trans e).trans g, have I : ((range (Kuratowski_embedding α)) ≃ᵢ (range (Kuratowski_embedding β))) = ((nonempty_compacts.Kuratowski_embedding α).val ≃ᵢ (nonempty_compacts.Kuratowski_embedding β).val), by { dunfold nonempty_compacts.Kuratowski_embedding, refl }, have h' := cast I h, exact ⟨h'⟩ end⟩ /-- Distance on `GH_space`: the distance between two nonempty compact spaces is the infimum Hausdorff distance between isometric copies of the two spaces in a metric space. For the definition, we only consider embeddings in `ℓ^∞(ℝ)`, but we will prove below that it works for all spaces. -/ instance : has_dist (GH_space) := { dist := λx y, Inf ((λp : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ, Hausdorff_dist p.1.val p.2.val) '' (set.prod {a | ⟦a⟧ = x} {b | ⟦b⟧ = y})) } /-- The Gromov-Hausdorff distance between two nonempty compact metric spaces, equal by definition to the distance of the equivalence classes of these spaces in the Gromov-Hausdorff space. -/ def GH_dist (α : Type u) (β : Type v) [metric_space α] [nonempty α] [compact_space α] [metric_space β] [nonempty β] [compact_space β] : ℝ := dist (to_GH_space α) (to_GH_space β) lemma dist_GH_dist (p q : GH_space) : dist p q = GH_dist (p.rep) (q.rep) := by rw [GH_dist, p.to_GH_space_rep, q.to_GH_space_rep] /-- The Gromov-Hausdorff distance between two spaces is bounded by the Hausdorff distance of isometric copies of the spaces, in any metric space. -/ theorem GH_dist_le_Hausdorff_dist {α : Type u} [metric_space α] [compact_space α] [nonempty α] {β : Type v} [metric_space β] [compact_space β] [nonempty β] {γ : Type w} [metric_space γ] {Φ : α → γ} {Ψ : β → γ} (ha : isometry Φ) (hb : isometry Ψ) : GH_dist α β ≤ Hausdorff_dist (range Φ) (range Ψ) := begin /- For the proof, we want to embed `γ` in `ℓ^∞(ℝ)`, to say that the Hausdorff distance is realized in `ℓ^∞(ℝ)` and therefore bounded below by the Gromov-Hausdorff-distance. However, `γ` is not separable in general. We restrict to the union of the images of `α` and `β` in `γ`, which is separable and therefore embeddable in `ℓ^∞(ℝ)`. -/ rcases exists_mem_of_nonempty α with ⟨xα, _⟩, letI : inhabited α := ⟨xα⟩, letI : inhabited β := classical.inhabited_of_nonempty (by assumption), let s : set γ := (range Φ) ∪ (range Ψ), let Φ' : α → subtype s := λy, ⟨Φ y, mem_union_left _ (mem_range_self _)⟩, let Ψ' : β → subtype s := λy, ⟨Ψ y, mem_union_right _ (mem_range_self _)⟩, have IΦ' : isometry Φ' := λx y, ha x y, have IΨ' : isometry Ψ' := λx y, hb x y, have : compact s, from (compact_range ha.continuous).union (compact_range hb.continuous), letI : metric_space (subtype s) := by apply_instance, haveI : compact_space (subtype s) := ⟨compact_iff_compact_univ.1 ‹compact s›⟩, haveI : nonempty (subtype s) := ⟨Φ' xα⟩, have ΦΦ' : Φ = subtype.val ∘ Φ', by { funext, refl }, have ΨΨ' : Ψ = subtype.val ∘ Ψ', by { funext, refl }, have : Hausdorff_dist (range Φ) (range Ψ) = Hausdorff_dist (range Φ') (range Ψ'), { rw [ΦΦ', ΨΨ', range_comp, range_comp], exact Hausdorff_dist_image (isometry_subtype_val) }, rw this, -- Embed `s` in `ℓ^∞(ℝ)` through its Kuratowski embedding let F := Kuratowski_embedding (subtype s), have : Hausdorff_dist (F '' (range Φ')) (F '' (range Ψ')) = Hausdorff_dist (range Φ') (range Ψ') := Hausdorff_dist_image (Kuratowski_embedding.isometry _), rw ← this, -- Let `A` and `B` be the images of `α` and `β` under this embedding. They are in `ℓ^∞(ℝ)`, and -- their Hausdorff distance is the same as in the original space. let A : nonempty_compacts ℓ_infty_ℝ := ⟨F '' (range Φ'), ⟨(range_nonempty _).image _, (compact_range IΦ'.continuous).image (Kuratowski_embedding.isometry _).continuous⟩⟩, let B : nonempty_compacts ℓ_infty_ℝ := ⟨F '' (range Ψ'), ⟨(range_nonempty _).image _, (compact_range IΨ'.continuous).image (Kuratowski_embedding.isometry _).continuous⟩⟩, have Aα : ⟦A⟧ = to_GH_space α, { rw eq_to_GH_space_iff, exact ⟨λx, F (Φ' x), ⟨(Kuratowski_embedding.isometry _).comp IΦ', by rw range_comp⟩⟩ }, have Bβ : ⟦B⟧ = to_GH_space β, { rw eq_to_GH_space_iff, exact ⟨λx, F (Ψ' x), ⟨(Kuratowski_embedding.isometry _).comp IΨ', by rw range_comp⟩⟩ }, refine cInf_le ⟨0, begin simp [lower_bounds], assume t _ _ _ _ ht, rw ← ht, exact Hausdorff_dist_nonneg end⟩ _, apply (mem_image _ _ _).2, existsi (⟨A, B⟩ : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ), simp [Aα, Bβ] end /-- The optimal coupling constructed above realizes exactly the Gromov-Hausdorff distance, essentially by design. -/ lemma Hausdorff_dist_optimal {α : Type u} [metric_space α] [compact_space α] [nonempty α] {β : Type v} [metric_space β] [compact_space β] [nonempty β] : Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) = GH_dist α β := begin inhabit α, inhabit β, /- we only need to check the inequality `≤`, as the other one follows from the previous lemma. As the Gromov-Hausdorff distance is an infimum, we need to check that the Hausdorff distance in the optimal coupling is smaller than the Hausdorff distance of any coupling. First, we check this for couplings which already have small Hausdorff distance: in this case, the induced "distance" on `α ⊕ β` belongs to the candidates family introduced in the definition of the optimal coupling, and the conclusion follows from the optimality of the optimal coupling within this family. -/ have A : ∀p q : nonempty_compacts (ℓ_infty_ℝ), ⟦p⟧ = to_GH_space α → ⟦q⟧ = to_GH_space β → Hausdorff_dist (p.val) (q.val) < diam (univ : set α) + 1 + diam (univ : set β) → Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) ≤ Hausdorff_dist (p.val) (q.val), { assume p q hp hq bound, rcases eq_to_GH_space_iff.1 hp with ⟨Φ, ⟨Φisom, Φrange⟩⟩, rcases eq_to_GH_space_iff.1 hq with ⟨Ψ, ⟨Ψisom, Ψrange⟩⟩, have I : diam (range Φ ∪ range Ψ) ≤ 2 * diam (univ : set α) + 1 + 2 * diam (univ : set β), { rcases exists_mem_of_nonempty α with ⟨xα, _⟩, have : ∃y ∈ range Ψ, dist (Φ xα) y < diam (univ : set α) + 1 + diam (univ : set β), { rw Ψrange, have : Φ xα ∈ p.val := Φrange ▸ mem_range_self _, exact exists_dist_lt_of_Hausdorff_dist_lt this bound (Hausdorff_edist_ne_top_of_nonempty_of_bounded p.2.1 q.2.1 p.2.2.bounded q.2.2.bounded) }, rcases this with ⟨y, hy, dy⟩, rcases mem_range.1 hy with ⟨z, hzy⟩, rw ← hzy at dy, have DΦ : diam (range Φ) = diam (univ : set α) := Φisom.diam_range, have DΨ : diam (range Ψ) = diam (univ : set β) := Ψisom.diam_range, calc diam (range Φ ∪ range Ψ) ≤ diam (range Φ) + dist (Φ xα) (Ψ z) + diam (range Ψ) : diam_union (mem_range_self _) (mem_range_self _) ... ≤ diam (univ : set α) + (diam (univ : set α) + 1 + diam (univ : set β)) + diam (univ : set β) : by { rw [DΦ, DΨ], apply add_le_add (add_le_add (le_refl _) (le_of_lt dy)) (le_refl _) } ... = 2 * diam (univ : set α) + 1 + 2 * diam (univ : set β) : by ring }, let f : α ⊕ β → ℓ_infty_ℝ := λx, match x with | inl y := Φ y | inr z := Ψ z end, let F : (α ⊕ β) × (α ⊕ β) → ℝ := λp, dist (f p.1) (f p.2), -- check that the induced "distance" is a candidate have Fgood : F ∈ candidates α β, { simp only [candidates, forall_const, and_true, add_comm, eq_self_iff_true, dist_eq_zero, and_self, set.mem_set_of_eq], repeat {split}, { exact λx y, calc F (inl x, inl y) = dist (Φ x) (Φ y) : rfl ... = dist x y : Φisom.dist_eq }, { exact λx y, calc F (inr x, inr y) = dist (Ψ x) (Ψ y) : rfl ... = dist x y : Ψisom.dist_eq }, { exact λx y, dist_comm _ _ }, { exact λx y z, dist_triangle _ _ _ }, { exact λx y, calc F (x, y) ≤ diam (range Φ ∪ range Ψ) : begin have A : ∀z : α ⊕ β, f z ∈ range Φ ∪ range Ψ, { assume z, cases z, { apply mem_union_left, apply mem_range_self }, { apply mem_union_right, apply mem_range_self } }, refine dist_le_diam_of_mem _ (A _) (A _), rw [Φrange, Ψrange], exact (p.2.2.union q.2.2).bounded, end ... ≤ 2 * diam (univ : set α) + 1 + 2 * diam (univ : set β) : I } }, let Fb := candidates_b_of_candidates F Fgood, have : Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) ≤ HD Fb := Hausdorff_dist_optimal_le_HD _ _ (candidates_b_of_candidates_mem F Fgood), refine le_trans this (le_of_forall_le_of_dense (λr hr, _)), have I1 : ∀x : α, infi (λy:β, Fb (inl x, inr y)) ≤ r, { assume x, have : f (inl x) ∈ p.val, by { rw [← Φrange], apply mem_range_self }, rcases exists_dist_lt_of_Hausdorff_dist_lt this hr (Hausdorff_edist_ne_top_of_nonempty_of_bounded p.2.1 q.2.1 p.2.2.bounded q.2.2.bounded) with ⟨z, zq, hz⟩, have : z ∈ range Ψ, by rwa [← Ψrange] at zq, rcases mem_range.1 this with ⟨y, hy⟩, calc infi (λy:β, Fb (inl x, inr y)) ≤ Fb (inl x, inr y) : cinfi_le (by simpa using HD_below_aux1 0) ... = dist (Φ x) (Ψ y) : rfl ... = dist (f (inl x)) z : by rw hy ... ≤ r : le_of_lt hz }, have I2 : ∀y : β, infi (λx:α, Fb (inl x, inr y)) ≤ r, { assume y, have : f (inr y) ∈ q.val, by { rw [← Ψrange], apply mem_range_self }, rcases exists_dist_lt_of_Hausdorff_dist_lt' this hr (Hausdorff_edist_ne_top_of_nonempty_of_bounded p.2.1 q.2.1 p.2.2.bounded q.2.2.bounded) with ⟨z, zq, hz⟩, have : z ∈ range Φ, by rwa [← Φrange] at zq, rcases mem_range.1 this with ⟨x, hx⟩, calc infi (λx:α, Fb (inl x, inr y)) ≤ Fb (inl x, inr y) : cinfi_le (by simpa using HD_below_aux2 0) ... = dist (Φ x) (Ψ y) : rfl ... = dist z (f (inr y)) : by rw hx ... ≤ r : le_of_lt hz }, simp [HD, csupr_le I1, csupr_le I2] }, /- Get the same inequality for any coupling. If the coupling is quite good, the desired inequality has been proved above. If it is bad, then the inequality is obvious. -/ have B : ∀p q : nonempty_compacts (ℓ_infty_ℝ), ⟦p⟧ = to_GH_space α → ⟦q⟧ = to_GH_space β → Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) ≤ Hausdorff_dist (p.val) (q.val), { assume p q hp hq, by_cases h : Hausdorff_dist (p.val) (q.val) < diam (univ : set α) + 1 + diam (univ : set β), { exact A p q hp hq h }, { calc Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) ≤ HD (candidates_b_dist α β) : Hausdorff_dist_optimal_le_HD _ _ (candidates_b_dist_mem_candidates_b) ... ≤ diam (univ : set α) + 1 + diam (univ : set β) : HD_candidates_b_dist_le ... ≤ Hausdorff_dist (p.val) (q.val) : not_lt.1 h } }, refine le_antisymm _ _, { apply le_cInf, { refine (set.nonempty.prod _ _).image _; exact ⟨_, rfl⟩ }, { rintro b ⟨⟨p, q⟩, ⟨hp, hq⟩, rfl⟩, exact B p q hp hq } }, { exact GH_dist_le_Hausdorff_dist (isometry_optimal_GH_injl α β) (isometry_optimal_GH_injr α β) } end /-- The Gromov-Hausdorff distance can also be realized by a coupling in `ℓ^∞(ℝ)`, by embedding the optimal coupling through its Kuratowski embedding. -/ theorem GH_dist_eq_Hausdorff_dist (α : Type u) [metric_space α] [compact_space α] [nonempty α] (β : Type v) [metric_space β] [compact_space β] [nonempty β] : ∃Φ : α → ℓ_infty_ℝ, ∃Ψ : β → ℓ_infty_ℝ, isometry Φ ∧ isometry Ψ ∧ GH_dist α β = Hausdorff_dist (range Φ) (range Ψ) := begin let F := Kuratowski_embedding (optimal_GH_coupling α β), let Φ := F ∘ optimal_GH_injl α β, let Ψ := F ∘ optimal_GH_injr α β, refine ⟨Φ, Ψ, _, _, _⟩, { exact (Kuratowski_embedding.isometry _).comp (isometry_optimal_GH_injl α β) }, { exact (Kuratowski_embedding.isometry _).comp (isometry_optimal_GH_injr α β) }, { rw [← image_univ, ← image_univ, image_comp F, image_univ, image_comp F (optimal_GH_injr α β), image_univ, ← Hausdorff_dist_optimal], exact (Hausdorff_dist_image (Kuratowski_embedding.isometry _)).symm }, end -- without the next two lines, `{ exact closed_of_compact (range Φ) hΦ }` in the next -- proof is very slow, as the `t2_space` instance is very hard to find local attribute [instance, priority 10] order_topology.t2_space local attribute [instance, priority 10] order_closed_topology.to_t2_space /-- The Gromov-Hausdorff distance defines a genuine distance on the Gromov-Hausdorff space. -/ instance GH_space_metric_space : metric_space GH_space := { dist_self := λx, begin rcases exists_rep x with ⟨y, hy⟩, refine le_antisymm _ _, { apply cInf_le, { exact ⟨0, by { rintro b ⟨⟨u, v⟩, ⟨hu, hv⟩, rfl⟩, exact Hausdorff_dist_nonneg } ⟩}, { simp, existsi [y, y], simpa } }, { apply le_cInf, { exact (nonempty.prod ⟨y, hy⟩ ⟨y, hy⟩).image _ }, { rintro b ⟨⟨u, v⟩, ⟨hu, hv⟩, rfl⟩, exact Hausdorff_dist_nonneg } }, end, dist_comm := λx y, begin have A : (λ (p : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ), Hausdorff_dist ((p.fst).val) ((p.snd).val)) '' (set.prod {a | ⟦a⟧ = x} {b | ⟦b⟧ = y}) = ((λ (p : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ), Hausdorff_dist ((p.fst).val) ((p.snd).val)) ∘ prod.swap) '' (set.prod {a | ⟦a⟧ = x} {b | ⟦b⟧ = y}) := by { congr, funext, simp, rw Hausdorff_dist_comm }, simp only [dist, A, image_comp, prod.swap, image_swap_prod], end, eq_of_dist_eq_zero := λx y hxy, begin /- To show that two spaces at zero distance are isometric, we argue that the distance is realized by some coupling. In this coupling, the two spaces are at zero Hausdorff distance, i.e., they coincide. Therefore, the original spaces are isometric. -/ rcases GH_dist_eq_Hausdorff_dist x.rep y.rep with ⟨Φ, Ψ, Φisom, Ψisom, DΦΨ⟩, rw [← dist_GH_dist, hxy] at DΦΨ, have : range Φ = range Ψ, { have hΦ : compact (range Φ) := compact_range Φisom.continuous, have hΨ : compact (range Ψ) := compact_range Ψisom.continuous, apply (Hausdorff_dist_zero_iff_eq_of_closed _ _ _).1 (DΦΨ.symm), { exact closed_of_compact (range Φ) hΦ }, { exact closed_of_compact (range Ψ) hΨ }, { exact Hausdorff_edist_ne_top_of_nonempty_of_bounded (range_nonempty _) (range_nonempty _) hΦ.bounded hΨ.bounded } }, have T : ((range Ψ) ≃ᵢ y.rep) = ((range Φ) ≃ᵢ y.rep), by rw this, have eΨ := cast T Ψisom.isometric_on_range.symm, have e := Φisom.isometric_on_range.trans eΨ, rw [← x.to_GH_space_rep, ← y.to_GH_space_rep, to_GH_space_eq_to_GH_space_iff_isometric], exact ⟨e⟩ end, dist_triangle := λx y z, begin /- To show the triangular inequality between `X`, `Y` and `Z`, realize an optimal coupling between `X` and `Y` in a space `γ1`, and an optimal coupling between `Y`and `Z` in a space `γ2`. Then, glue these metric spaces along `Y`. We get a new space `γ` in which `X` and `Y` are optimally coupled, as well as `Y` and `Z`. Apply the triangle inequality for the Hausdorff distance in `γ` to conclude. -/ let X := x.rep, let Y := y.rep, let Z := z.rep, let γ1 := optimal_GH_coupling X Y, let γ2 := optimal_GH_coupling Y Z, let Φ : Y → γ1 := optimal_GH_injr X Y, have hΦ : isometry Φ := isometry_optimal_GH_injr X Y, let Ψ : Y → γ2 := optimal_GH_injl Y Z, have hΨ : isometry Ψ := isometry_optimal_GH_injl Y Z, let γ := glue_space hΦ hΨ, letI : metric_space γ := metric.metric_space_glue_space hΦ hΨ, have Comm : (to_glue_l hΦ hΨ) ∘ (optimal_GH_injr X Y) = (to_glue_r hΦ hΨ) ∘ (optimal_GH_injl Y Z) := to_glue_commute hΦ hΨ, calc dist x z = dist (to_GH_space X) (to_GH_space Z) : by rw [x.to_GH_space_rep, z.to_GH_space_rep] ... ≤ Hausdorff_dist (range ((to_glue_l hΦ hΨ) ∘ (optimal_GH_injl X Y))) (range ((to_glue_r hΦ hΨ) ∘ (optimal_GH_injr Y Z))) : GH_dist_le_Hausdorff_dist ((to_glue_l_isometry hΦ hΨ).comp (isometry_optimal_GH_injl X Y)) ((to_glue_r_isometry hΦ hΨ).comp (isometry_optimal_GH_injr Y Z)) ... ≤ Hausdorff_dist (range ((to_glue_l hΦ hΨ) ∘ (optimal_GH_injl X Y))) (range ((to_glue_l hΦ hΨ) ∘ (optimal_GH_injr X Y))) + Hausdorff_dist (range ((to_glue_l hΦ hΨ) ∘ (optimal_GH_injr X Y))) (range ((to_glue_r hΦ hΨ) ∘ (optimal_GH_injr Y Z))) : begin refine Hausdorff_dist_triangle (Hausdorff_edist_ne_top_of_nonempty_of_bounded (range_nonempty _) (range_nonempty _) _ _), { exact (compact_range (isometry.continuous ((to_glue_l_isometry hΦ hΨ).comp (isometry_optimal_GH_injl X Y)))).bounded }, { exact (compact_range (isometry.continuous ((to_glue_l_isometry hΦ hΨ).comp (isometry_optimal_GH_injr X Y)))).bounded } end ... = Hausdorff_dist ((to_glue_l hΦ hΨ) '' (range (optimal_GH_injl X Y))) ((to_glue_l hΦ hΨ) '' (range (optimal_GH_injr X Y))) + Hausdorff_dist ((to_glue_r hΦ hΨ) '' (range (optimal_GH_injl Y Z))) ((to_glue_r hΦ hΨ) '' (range (optimal_GH_injr Y Z))) : by simp only [eq.symm range_comp, Comm, eq_self_iff_true, add_right_inj] ... = Hausdorff_dist (range (optimal_GH_injl X Y)) (range (optimal_GH_injr X Y)) + Hausdorff_dist (range (optimal_GH_injl Y Z)) (range (optimal_GH_injr Y Z)) : by rw [Hausdorff_dist_image (to_glue_l_isometry hΦ hΨ), Hausdorff_dist_image (to_glue_r_isometry hΦ hΨ)] ... = dist (to_GH_space X) (to_GH_space Y) + dist (to_GH_space Y) (to_GH_space Z) : by rw [Hausdorff_dist_optimal, Hausdorff_dist_optimal, GH_dist, GH_dist] ... = dist x y + dist y z: by rw [x.to_GH_space_rep, y.to_GH_space_rep, z.to_GH_space_rep] end } end GH_space --section end Gromov_Hausdorff /-- In particular, nonempty compacts of a metric space map to `GH_space`. We register this in the topological_space namespace to take advantage of the notation `p.to_GH_space`. -/ definition topological_space.nonempty_compacts.to_GH_space {α : Type u} [metric_space α] (p : nonempty_compacts α) : Gromov_Hausdorff.GH_space := Gromov_Hausdorff.to_GH_space p.val open topological_space namespace Gromov_Hausdorff section nonempty_compacts variables {α : Type u} [metric_space α] theorem GH_dist_le_nonempty_compacts_dist (p q : nonempty_compacts α) : dist p.to_GH_space q.to_GH_space ≤ dist p q := begin have ha : isometry (subtype.val : p.val → α) := isometry_subtype_val, have hb : isometry (subtype.val : q.val → α) := isometry_subtype_val, have A : dist p q = Hausdorff_dist p.val q.val := rfl, have I : p.val = range (subtype.val : p.val → α), by simp, have J : q.val = range (subtype.val : q.val → α), by simp, rw [I, J] at A, rw A, exact GH_dist_le_Hausdorff_dist ha hb end lemma to_GH_space_lipschitz : lipschitz_with 1 (nonempty_compacts.to_GH_space : nonempty_compacts α → GH_space) := lipschitz_with.mk_one GH_dist_le_nonempty_compacts_dist lemma to_GH_space_continuous : continuous (nonempty_compacts.to_GH_space : nonempty_compacts α → GH_space) := to_GH_space_lipschitz.continuous end nonempty_compacts section /- In this section, we show that if two metric spaces are isometric up to `ε₂`, then their Gromov-Hausdorff distance is bounded by `ε₂ / 2`. More generally, if there are subsets which are `ε₁`-dense and `ε₃`-dense in two spaces, and isometric up to `ε₂`, then the Gromov-Hausdorff distance between the spaces is bounded by `ε₁ + ε₂/2 + ε₃`. For this, we construct a suitable coupling between the two spaces, by gluing them (approximately) along the two matching subsets. -/ variables {α : Type u} [metric_space α] [compact_space α] [nonempty α] {β : Type v} [metric_space β] [compact_space β] [nonempty β] -- we want to ignore these instances in the following theorem local attribute [instance, priority 10] sum.topological_space sum.uniform_space /-- If there are subsets which are `ε₁`-dense and `ε₃`-dense in two spaces, and isometric up to `ε₂`, then the Gromov-Hausdorff distance between the spaces is bounded by `ε₁ + ε₂/2 + ε₃`. -/ theorem GH_dist_le_of_approx_subsets {s : set α} (Φ : s → β) {ε₁ ε₂ ε₃ : ℝ} (hs : ∀x : α, ∃y ∈ s, dist x y ≤ ε₁) (hs' : ∀x : β, ∃y : s, dist x (Φ y) ≤ ε₃) (H : ∀x y : s, abs (dist x y - dist (Φ x) (Φ y)) ≤ ε₂) : GH_dist α β ≤ ε₁ + ε₂ / 2 + ε₃ := begin refine real.le_of_forall_epsilon_le (λδ δ0, _), rcases exists_mem_of_nonempty α with ⟨xα, _⟩, rcases hs xα with ⟨xs, hxs, Dxs⟩, have sne : s.nonempty := ⟨xs, hxs⟩, letI : nonempty s := sne.to_subtype, have : 0 ≤ ε₂ := le_trans (abs_nonneg _) (H ⟨xs, hxs⟩ ⟨xs, hxs⟩), have : ∀ p q : s, abs (dist p q - dist (Φ p) (Φ q)) ≤ 2 * (ε₂/2 + δ) := λp q, calc abs (dist p q - dist (Φ p) (Φ q)) ≤ ε₂ : H p q ... ≤ 2 * (ε₂/2 + δ) : by linarith, -- glue `α` and `β` along the almost matching subsets letI : metric_space (α ⊕ β) := glue_metric_approx (λ x:s, (x:α)) (λx, Φ x) (ε₂/2 + δ) (by linarith) this, let Fl := @sum.inl α β, let Fr := @sum.inr α β, have Il : isometry Fl := isometry_emetric_iff_metric.2 (λx y, rfl), have Ir : isometry Fr := isometry_emetric_iff_metric.2 (λx y, rfl), /- The proof goes as follows : the `GH_dist` is bounded by the Hausdorff distance of the images in the coupling, which is bounded (using the triangular inequality) by the sum of the Hausdorff distances of `α` and `s` (in the coupling or, equivalently in the original space), of `s` and `Φ s`, and of `Φ s` and `β` (in the coupling or, equivalently, in the original space). The first term is bounded by `ε₁`, by `ε₁`-density. The third one is bounded by `ε₃`. And the middle one is bounded by `ε₂/2` as in the coupling the points `x` and `Φ x` are at distance `ε₂/2` by construction of the coupling (in fact `ε₂/2 + δ` where `δ` is an arbitrarily small positive constant where positivity is used to ensure that the coupling is really a metric space and not a premetric space on `α ⊕ β`). -/ have : GH_dist α β ≤ Hausdorff_dist (range Fl) (range Fr) := GH_dist_le_Hausdorff_dist Il Ir, have : Hausdorff_dist (range Fl) (range Fr) ≤ Hausdorff_dist (range Fl) (Fl '' s) + Hausdorff_dist (Fl '' s) (range Fr), { have B : bounded (range Fl) := (compact_range Il.continuous).bounded, exact Hausdorff_dist_triangle (Hausdorff_edist_ne_top_of_nonempty_of_bounded (range_nonempty _) (sne.image _) B (B.subset (image_subset_range _ _))) }, have : Hausdorff_dist (Fl '' s) (range Fr) ≤ Hausdorff_dist (Fl '' s) (Fr '' (range Φ)) + Hausdorff_dist (Fr '' (range Φ)) (range Fr), { have B : bounded (range Fr) := (compact_range Ir.continuous).bounded, exact Hausdorff_dist_triangle' (Hausdorff_edist_ne_top_of_nonempty_of_bounded ((range_nonempty _).image _) (range_nonempty _) (bounded.subset (image_subset_range _ _) B) B) }, have : Hausdorff_dist (range Fl) (Fl '' s) ≤ ε₁, { rw [← image_univ, Hausdorff_dist_image Il], have : 0 ≤ ε₁ := le_trans dist_nonneg Dxs, refine Hausdorff_dist_le_of_mem_dist this (λx hx, hs x) (λx hx, ⟨x, mem_univ _, by simpa⟩) }, have : Hausdorff_dist (Fl '' s) (Fr '' (range Φ)) ≤ ε₂/2 + δ, { refine Hausdorff_dist_le_of_mem_dist (by linarith) _ _, { assume x' hx', rcases (set.mem_image _ _ _).1 hx' with ⟨x, ⟨x_in_s, xx'⟩⟩, rw ← xx', use [Fr (Φ ⟨x, x_in_s⟩), mem_image_of_mem Fr (mem_range_self _)], exact le_of_eq (glue_dist_glued_points (λ x:s, (x:α)) Φ (ε₂/2 + δ) ⟨x, x_in_s⟩) }, { assume x' hx', rcases (set.mem_image _ _ _).1 hx' with ⟨y, ⟨y_in_s', yx'⟩⟩, rcases mem_range.1 y_in_s' with ⟨x, xy⟩, use [Fl x, mem_image_of_mem _ x.2], rw [← yx', ← xy, dist_comm], exact le_of_eq (glue_dist_glued_points (@subtype.val α s) Φ (ε₂/2 + δ) x) } }, have : Hausdorff_dist (Fr '' (range Φ)) (range Fr) ≤ ε₃, { rw [← @image_univ _ _ Fr, Hausdorff_dist_image Ir], rcases exists_mem_of_nonempty β with ⟨xβ, _⟩, rcases hs' xβ with ⟨xs', Dxs'⟩, have : 0 ≤ ε₃ := le_trans dist_nonneg Dxs', refine Hausdorff_dist_le_of_mem_dist this (λx hx, ⟨x, mem_univ _, by simpa⟩) (λx _, _), rcases hs' x with ⟨y, Dy⟩, exact ⟨Φ y, mem_range_self _, Dy⟩ }, linarith end end --section /-- The Gromov-Hausdorff space is second countable. -/ instance second_countable : second_countable_topology GH_space := begin refine second_countable_of_countable_discretization (λδ δpos, _), let ε := (2/5) * δ, have εpos : 0 < ε := mul_pos (by norm_num) δpos, have : ∀p:GH_space, ∃s : set (p.rep), finite s ∧ (univ ⊆ (⋃x∈s, ball x ε)) := λp, by simpa using finite_cover_balls_of_compact (@compact_univ p.rep _ _) εpos, -- for each `p`, `s p` is a finite `ε`-dense subset of `p` (or rather the metric space -- `p.rep` representing `p`) choose s hs using this, have : ∀p:GH_space, ∀t:set (p.rep), finite t → ∃n:ℕ, ∃e:equiv t (fin n), true, { assume p t ht, letI : fintype t := finite.fintype ht, rcases fintype.exists_equiv_fin t with ⟨n, hn⟩, rcases hn with e, exact ⟨n, e, trivial⟩ }, choose N e hne using this, -- cardinality of the nice finite subset `s p` of `p.rep`, called `N p` let N := λp:GH_space, N p (s p) (hs p).1, -- equiv from `s p`, a nice finite subset of `p.rep`, to `fin (N p)`, called `E p` let E := λp:GH_space, e p (s p) (hs p).1, -- A function `F` associating to `p : GH_space` the data of all distances between points -- in the `ε`-dense set `s p`. let F : GH_space → Σn:ℕ, (fin n → fin n → ℤ) := λp, ⟨N p, λa b, floor (ε⁻¹ * dist ((E p).symm a) ((E p).symm b))⟩, refine ⟨_, by apply_instance, F, λp q hpq, _⟩, /- As the target space of F is countable, it suffices to show that two points `p` and `q` with `F p = F q` are at distance `≤ δ`. For this, we construct a map `Φ` from `s p ⊆ p.rep` (representing `p`) to `q.rep` (representing `q`) which is almost an isometry on `s p`, and with image `s q`. For this, we compose the identification of `s p` with `fin (N p)` and the inverse of the identification of `s q` with `fin (N q)`. Together with the fact that `N p = N q`, this constructs `Ψ` between `s p` and `s q`, and then composing with the canonical inclusion we get `Φ`. -/ have Npq : N p = N q := (sigma.mk.inj_iff.1 hpq).1, let Ψ : s p → s q := λx, (E q).symm (fin.cast Npq ((E p) x)), let Φ : s p → q.rep := λx, Ψ x, -- Use the almost isometry `Φ` to show that `p.rep` and `q.rep` -- are within controlled Gromov-Hausdorff distance. have main : GH_dist p.rep q.rep ≤ ε + ε/2 + ε, { refine GH_dist_le_of_approx_subsets Φ _ _ _, show ∀x : p.rep, ∃ (y : p.rep) (H : y ∈ s p), dist x y ≤ ε, { -- by construction, `s p` is `ε`-dense assume x, have : x ∈ ⋃y∈(s p), ball y ε := (hs p).2 (mem_univ _), rcases mem_bUnion_iff.1 this with ⟨y, ys, hy⟩, exact ⟨y, ys, le_of_lt hy⟩ }, show ∀x : q.rep, ∃ (z : s p), dist x (Φ z) ≤ ε, { -- by construction, `s q` is `ε`-dense, and it is the range of `Φ` assume x, have : x ∈ ⋃y∈(s q), ball y ε := (hs q).2 (mem_univ _), rcases mem_bUnion_iff.1 this with ⟨y, ys, hy⟩, let i := ((E q) ⟨y, ys⟩).1, let hi := ((E q) ⟨y, ys⟩).2, have ihi_eq : (⟨i, hi⟩ : fin (N q)) = (E q) ⟨y, ys⟩, by rw fin.ext_iff, have hiq : i < N q := hi, have hip : i < N p, { rwa Npq.symm at hiq }, let z := (E p).symm ⟨i, hip⟩, use z, have C1 : (E p) z = ⟨i, hip⟩ := (E p).apply_symm_apply ⟨i, hip⟩, have C2 : fin.cast Npq ⟨i, hip⟩ = ⟨i, hi⟩ := rfl, have C3 : (E q).symm ⟨i, hi⟩ = ⟨y, ys⟩, by { rw ihi_eq, exact (E q).symm_apply_apply ⟨y, ys⟩ }, have : Φ z = y := by { simp only [Φ, Ψ], rw [C1, C2, C3], refl }, rw this, exact le_of_lt hy }, show ∀x y : s p, abs (dist x y - dist (Φ x) (Φ y)) ≤ ε, { /- the distance between `x` and `y` is encoded in `F p`, and the distance between `Φ x` and `Φ y` (two points of `s q`) is encoded in `F q`, all this up to `ε`. As `F p = F q`, the distances are almost equal. -/ assume x y, have : dist (Φ x) (Φ y) = dist (Ψ x) (Ψ y) := rfl, rw this, -- introduce `i`, that codes both `x` and `Φ x` in `fin (N p) = fin (N q)` let i := ((E p) x).1, have hip : i < N p := ((E p) x).2, have hiq : i < N q, by rwa Npq at hip, have i' : i = ((E q) (Ψ x)).1, by { simp [Ψ] }, -- introduce `j`, that codes both `y` and `Φ y` in `fin (N p) = fin (N q)` let j := ((E p) y).1, have hjp : j < N p := ((E p) y).2, have hjq : j < N q, by rwa Npq at hjp, have j' : j = ((E q) (Ψ y)).1, by { simp [Ψ] }, -- Express `dist x y` in terms of `F p` have : (F p).2 ((E p) x) ((E p) y) = floor (ε⁻¹ * dist x y), by simp only [F, (E p).symm_apply_apply], have Ap : (F p).2 ⟨i, hip⟩ ⟨j, hjp⟩ = floor (ε⁻¹ * dist x y), by { rw ← this, congr; apply (fin.ext_iff _ _).2; refl }, -- Express `dist (Φ x) (Φ y)` in terms of `F q` have : (F q).2 ((E q) (Ψ x)) ((E q) (Ψ y)) = floor (ε⁻¹ * dist (Ψ x) (Ψ y)), by simp only [F, (E q).symm_apply_apply], have Aq : (F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩ = floor (ε⁻¹ * dist (Ψ x) (Ψ y)), by { rw ← this, congr; apply (fin.ext_iff _ _).2; [exact i', exact j'] }, -- use the equality between `F p` and `F q` to deduce that the distances have equal -- integer parts have : (F p).2 ⟨i, hip⟩ ⟨j, hjp⟩ = (F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩, { -- we want to `subst hpq` where `hpq : F p = F q`, except that `subst` only works -- with a constant, so replace `F q` (and everything that depends on it) by a constant `f` -- then `subst` revert hiq hjq, change N q with (F q).1, generalize_hyp : F q = f at hpq ⊢, subst hpq, intros, refl }, rw [Ap, Aq] at this, -- deduce that the distances coincide up to `ε`, by a straightforward computation -- that should be automated have I := calc abs (ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y)) = abs (ε⁻¹ * (dist x y - dist (Ψ x) (Ψ y))) : (abs_mul _ _).symm ... = abs ((ε⁻¹ * dist x y) - (ε⁻¹ * dist (Ψ x) (Ψ y))) : by { congr, ring } ... ≤ 1 : le_of_lt (abs_sub_lt_one_of_floor_eq_floor this), calc abs (dist x y - dist (Ψ x) (Ψ y)) = (ε * ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y)) : by rw [mul_inv_cancel (ne_of_gt εpos), one_mul] ... = ε * (abs (ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y))) : by rw [abs_of_nonneg (le_of_lt (inv_pos.2 εpos)), mul_assoc] ... ≤ ε * 1 : mul_le_mul_of_nonneg_left I (le_of_lt εpos) ... = ε : mul_one _ } }, calc dist p q = GH_dist (p.rep) (q.rep) : dist_GH_dist p q ... ≤ ε + ε/2 + ε : main ... = δ : by { simp [ε], ring } end /-- Compactness criterion: a closed set of compact metric spaces is compact if the spaces have a uniformly bounded diameter, and for all `ε` the number of balls of radius `ε` required to cover the spaces is uniformly bounded. This is an equivalence, but we only prove the interesting direction that these conditions imply compactness. -/ lemma totally_bounded {t : set GH_space} {C : ℝ} {u : ℕ → ℝ} {K : ℕ → ℕ} (ulim : tendsto u at_top (𝓝 0)) (hdiam : ∀p ∈ t, diam (univ : set (GH_space.rep p)) ≤ C) (hcov : ∀p ∈ t, ∀n:ℕ, ∃s : set (GH_space.rep p), cardinal.mk s ≤ K n ∧ univ ⊆ ⋃x∈s, ball x (u n)) : totally_bounded t := begin /- Let `δ>0`, and `ε = δ/5`. For each `p`, we construct a finite subset `s p` of `p`, which is `ε`-dense and has cardinality at most `K n`. Encoding the mutual distances of points in `s p`, up to `ε`, we will get a map `F` associating to `p` finitely many data, and making it possible to reconstruct `p` up to `ε`. This is enough to prove total boundedness. -/ refine metric.totally_bounded_of_finite_discretization (λδ δpos, _), let ε := (1/5) * δ, have εpos : 0 < ε := mul_pos (by norm_num) δpos, -- choose `n` for which `u n < ε` rcases metric.tendsto_at_top.1 ulim ε εpos with ⟨n, hn⟩, have u_le_ε : u n ≤ ε, { have := hn n (le_refl _), simp only [real.dist_eq, add_zero, sub_eq_add_neg, neg_zero] at this, exact le_of_lt (lt_of_le_of_lt (le_abs_self _) this) }, -- construct a finite subset `s p` of `p` which is `ε`-dense and has cardinal `≤ K n` have : ∀p:GH_space, ∃s : set (p.rep), ∃N ≤ K n, ∃E : equiv s (fin N), p ∈ t → univ ⊆ ⋃x∈s, ball x (u n), { assume p, by_cases hp : p ∉ t, { have : nonempty (equiv (∅ : set (p.rep)) (fin 0)), { rw ← fintype.card_eq, simp }, use [∅, 0, bot_le, choice (this)] }, { rcases hcov _ (set.not_not_mem.1 hp) n with ⟨s, ⟨scard, scover⟩⟩, rcases cardinal.lt_omega.1 (lt_of_le_of_lt scard (cardinal.nat_lt_omega _)) with ⟨N, hN⟩, rw [hN, cardinal.nat_cast_le] at scard, have : cardinal.mk s = cardinal.mk (fin N), by rw [hN, cardinal.mk_fin], cases quotient.exact this with E, use [s, N, scard, E], simp [hp, scover] } }, choose s N hN E hs using this, -- Define a function `F` taking values in a finite type and associating to `p` enough data -- to reconstruct it up to `ε`, namely the (discretized) distances between elements of `s p`. let M := (floor (ε⁻¹ * max C 0)).to_nat, let F : GH_space → (Σk:fin ((K n).succ), (fin k → fin k → fin (M.succ))) := λp, ⟨⟨N p, lt_of_le_of_lt (hN p) (nat.lt_succ_self _)⟩, λa b, ⟨min M (floor (ε⁻¹ * dist ((E p).symm a) ((E p).symm b))).to_nat, lt_of_le_of_lt ( min_le_left _ _) (nat.lt_succ_self _) ⟩ ⟩, refine ⟨_, by apply_instance, (λp, F p), _⟩, -- It remains to show that if `F p = F q`, then `p` and `q` are `ε`-close rintros ⟨p, pt⟩ ⟨q, qt⟩ hpq, have Npq : N p = N q := (fin.ext_iff _ _).1 (sigma.mk.inj_iff.1 hpq).1, let Ψ : s p → s q := λx, (E q).symm (fin.cast Npq ((E p) x)), let Φ : s p → q.rep := λx, Ψ x, have main : GH_dist (p.rep) (q.rep) ≤ ε + ε/2 + ε, { -- to prove the main inequality, argue that `s p` is `ε`-dense in `p`, and `s q` is `ε`-dense -- in `q`, and `s p` and `s q` are almost isometric. Then closeness follows -- from `GH_dist_le_of_approx_subsets` refine GH_dist_le_of_approx_subsets Φ _ _ _, show ∀x : p.rep, ∃ (y : p.rep) (H : y ∈ s p), dist x y ≤ ε, { -- by construction, `s p` is `ε`-dense assume x, have : x ∈ ⋃y∈(s p), ball y (u n) := (hs p pt) (mem_univ _), rcases mem_bUnion_iff.1 this with ⟨y, ys, hy⟩, exact ⟨y, ys, le_trans (le_of_lt hy) u_le_ε⟩ }, show ∀x : q.rep, ∃ (z : s p), dist x (Φ z) ≤ ε, { -- by construction, `s q` is `ε`-dense, and it is the range of `Φ` assume x, have : x ∈ ⋃y∈(s q), ball y (u n) := (hs q qt) (mem_univ _), rcases mem_bUnion_iff.1 this with ⟨y, ys, hy⟩, let i := ((E q) ⟨y, ys⟩).1, let hi := ((E q) ⟨y, ys⟩).2, have ihi_eq : (⟨i, hi⟩ : fin (N q)) = (E q) ⟨y, ys⟩, by rw fin.ext_iff, have hiq : i < N q := hi, have hip : i < N p, { rwa Npq.symm at hiq }, let z := (E p).symm ⟨i, hip⟩, use z, have C1 : (E p) z = ⟨i, hip⟩ := (E p).apply_symm_apply ⟨i, hip⟩, have C2 : fin.cast Npq ⟨i, hip⟩ = ⟨i, hi⟩ := rfl, have C3 : (E q).symm ⟨i, hi⟩ = ⟨y, ys⟩, by { rw ihi_eq, exact (E q).symm_apply_apply ⟨y, ys⟩ }, have : Φ z = y := by { simp only [Φ, Ψ], rw [C1, C2, C3], refl }, rw this, exact le_trans (le_of_lt hy) u_le_ε }, show ∀x y : s p, abs (dist x y - dist (Φ x) (Φ y)) ≤ ε, { /- the distance between `x` and `y` is encoded in `F p`, and the distance between `Φ x` and `Φ y` (two points of `s q`) is encoded in `F q`, all this up to `ε`. As `F p = F q`, the distances are almost equal. -/ assume x y, have : dist (Φ x) (Φ y) = dist (Ψ x) (Ψ y) := rfl, rw this, -- introduce `i`, that codes both `x` and `Φ x` in `fin (N p) = fin (N q)` let i := ((E p) x).1, have hip : i < N p := ((E p) x).2, have hiq : i < N q, by rwa Npq at hip, have i' : i = ((E q) (Ψ x)).1, by { simp [Ψ] }, -- introduce `j`, that codes both `y` and `Φ y` in `fin (N p) = fin (N q)` let j := ((E p) y).1, have hjp : j < N p := ((E p) y).2, have hjq : j < N q, by rwa Npq at hjp, have j' : j = ((E q) (Ψ y)).1, by { simp [Ψ] }, -- Express `dist x y` in terms of `F p` have Ap : ((F p).2 ⟨i, hip⟩ ⟨j, hjp⟩).1 = (floor (ε⁻¹ * dist x y)).to_nat := calc ((F p).2 ⟨i, hip⟩ ⟨j, hjp⟩).1 = ((F p).2 ((E p) x) ((E p) y)).1 : by { congr; apply (fin.ext_iff _ _).2; refl } ... = min M (floor (ε⁻¹ * dist x y)).to_nat : by simp only [F, (E p).symm_apply_apply] ... = (floor (ε⁻¹ * dist x y)).to_nat : begin refine min_eq_right (int.to_nat_le_to_nat (floor_mono _)), refine mul_le_mul_of_nonneg_left (le_trans _ (le_max_left _ _)) (le_of_lt (inv_pos.2 εpos)), change dist (x : p.rep) y ≤ C, refine le_trans (dist_le_diam_of_mem compact_univ.bounded (mem_univ _) (mem_univ _)) _, exact hdiam p pt end, -- Express `dist (Φ x) (Φ y)` in terms of `F q` have Aq : ((F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩).1 = (floor (ε⁻¹ * dist (Ψ x) (Ψ y))).to_nat := calc ((F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩).1 = ((F q).2 ((E q) (Ψ x)) ((E q) (Ψ y))).1 : by { congr; apply (fin.ext_iff _ _).2; [exact i', exact j'] } ... = min M (floor (ε⁻¹ * dist (Ψ x) (Ψ y))).to_nat : by simp only [F, (E q).symm_apply_apply] ... = (floor (ε⁻¹ * dist (Ψ x) (Ψ y))).to_nat : begin refine min_eq_right (int.to_nat_le_to_nat (floor_mono _)), refine mul_le_mul_of_nonneg_left (le_trans _ (le_max_left _ _)) (le_of_lt (inv_pos.2 εpos)), change dist (Ψ x : q.rep) (Ψ y) ≤ C, refine le_trans (dist_le_diam_of_mem compact_univ.bounded (mem_univ _) (mem_univ _)) _, exact hdiam q qt end, -- use the equality between `F p` and `F q` to deduce that the distances have equal -- integer parts have : ((F p).2 ⟨i, hip⟩ ⟨j, hjp⟩).1 = ((F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩).1, { -- we want to `subst hpq` where `hpq : F p = F q`, except that `subst` only works -- with a constant, so replace `F q` (and everything that depends on it) by a constant `f` -- then `subst` revert hiq hjq, change N q with (F q).1, generalize_hyp : F q = f at hpq ⊢, subst hpq, intros, refl }, have : floor (ε⁻¹ * dist x y) = floor (ε⁻¹ * dist (Ψ x) (Ψ y)), { rw [Ap, Aq] at this, have D : 0 ≤ floor (ε⁻¹ * dist x y) := floor_nonneg.2 (mul_nonneg (le_of_lt (inv_pos.2 εpos)) dist_nonneg), have D' : floor (ε⁻¹ * dist (Ψ x) (Ψ y)) ≥ 0 := floor_nonneg.2 (mul_nonneg (le_of_lt (inv_pos.2 εpos)) dist_nonneg), rw [← int.to_nat_of_nonneg D, ← int.to_nat_of_nonneg D', this] }, -- deduce that the distances coincide up to `ε`, by a straightforward computation -- that should be automated have I := calc abs (ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y)) = abs (ε⁻¹ * (dist x y - dist (Ψ x) (Ψ y))) : (abs_mul _ _).symm ... = abs ((ε⁻¹ * dist x y) - (ε⁻¹ * dist (Ψ x) (Ψ y))) : by { congr, ring } ... ≤ 1 : le_of_lt (abs_sub_lt_one_of_floor_eq_floor this), calc abs (dist x y - dist (Ψ x) (Ψ y)) = (ε * ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y)) : by rw [mul_inv_cancel (ne_of_gt εpos), one_mul] ... = ε * (abs (ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y))) : by rw [abs_of_nonneg (le_of_lt (inv_pos.2 εpos)), mul_assoc] ... ≤ ε * 1 : mul_le_mul_of_nonneg_left I (le_of_lt εpos) ... = ε : mul_one _ } }, calc dist p q = GH_dist (p.rep) (q.rep) : dist_GH_dist p q ... ≤ ε + ε/2 + ε : main ... = δ/2 : by { simp [ε], ring } ... < δ : half_lt_self δpos end section complete /- We will show that a sequence `u n` of compact metric spaces satisfying `dist (u n) (u (n+1)) < 1/2^n` converges, which implies completeness of the Gromov-Hausdorff space. We need to exhibit the limiting compact metric space. For this, start from a sequence `X n` of representatives of `u n`, and glue in an optimal way `X n` to `X (n+1)` for all `n`, in a common metric space. Formally, this is done as follows. Start from `Y 0 = X 0`. Then, glue `X 0` to `X 1` in an optimal way, yielding a space `Y 1` (with an embedding of `X 1`). Then, consider an optimal gluing of `X 1` and `X 2`, and glue it to `Y 1` along their common subspace `X 1`. This gives a new space `Y 2`, with an embedding of `X 2`. Go on, to obtain a sequence of spaces `Y n`. Let `Z0` be the inductive limit of the `Y n`, and finally let `Z` be the completion of `Z0`. The images `X2 n` of `X n` in `Z` are at Hausdorff distance `< 1/2^n` by construction, hence they form a Cauchy sequence for the Hausdorff distance. By completeness (of `Z`, and therefore of its set of nonempty compact subsets), they converge to a limit `L`. This is the nonempty compact metric space we are looking for. -/ variables (X : ℕ → Type) [∀n, metric_space (X n)] [∀n, compact_space (X n)] [∀n, nonempty (X n)] /-- Auxiliary structure used to glue metric spaces below, recording an isometric embedding of a type `A` in another metric space. -/ structure aux_gluing_struct (A : Type) [metric_space A] : Type 1 := (space : Type) (metric : metric_space space) (embed : A → space) (isom : isometry embed) /-- Auxiliary sequence of metric spaces, containing copies of `X 0`, ..., `X n`, where each `X i` is glued to `X (i+1)` in an optimal way. The space at step `n+1` is obtained from the space at step `n` by adding `X (n+1)`, glued in an optimal way to the `X n` already sitting there. -/ def aux_gluing (n : ℕ) : aux_gluing_struct (X n) := nat.rec_on n { space := X 0, metric := by apply_instance, embed := id, isom := λx y, rfl } (λn a, by letI : metric_space a.space := a.metric; exact { space := glue_space a.isom (isometry_optimal_GH_injl (X n) (X n.succ)), metric := metric.metric_space_glue_space a.isom (isometry_optimal_GH_injl (X n) (X n.succ)), embed := (to_glue_r a.isom (isometry_optimal_GH_injl (X n) (X n.succ))) ∘ (optimal_GH_injr (X n) (X n.succ)), isom := (to_glue_r_isometry _ _).comp (isometry_optimal_GH_injr (X n) (X n.succ)) }) /-- The Gromov-Hausdorff space is complete. -/ instance : complete_space (GH_space) := begin have : ∀ (n : ℕ), 0 < ((1:ℝ) / 2) ^ n, by { apply _root_.pow_pos, norm_num }, -- start from a sequence of nonempty compact metric spaces within distance `1/2^n` of each other refine metric.complete_of_convergent_controlled_sequences (λn, (1/2)^n) this (λu hu, _), -- `X n` is a representative of `u n` let X := λn, (u n).rep, -- glue them together successively in an optimal way, getting a sequence of metric spaces `Y n` let Y := aux_gluing X, letI : ∀n, metric_space (Y n).space := λn, (Y n).metric, have E : ∀n:ℕ, glue_space (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ)) = (Y n.succ).space := λn, by { simp [Y, aux_gluing], refl }, let c := λn, cast (E n), have ic : ∀n, isometry (c n) := λn x y, rfl, -- there is a canonical embedding of `Y n` in `Y (n+1)`, by construction let f : Πn, (Y n).space → (Y n.succ).space := λn, (c n) ∘ (to_glue_l (aux_gluing X n).isom (isometry_optimal_GH_injl (X n) (X n.succ))), have I : ∀n, isometry (f n), { assume n, apply isometry.comp, { assume x y, refl }, { apply to_glue_l_isometry } }, -- consider the inductive limit `Z0` of the `Y n`, and then its completion `Z` let Z0 := metric.inductive_limit I, let Z := uniform_space.completion Z0, let Φ := to_inductive_limit I, let coeZ := (coe : Z0 → Z), -- let `X2 n` be the image of `X n` in the space `Z` let X2 := λn, range (coeZ ∘ (Φ n) ∘ (Y n).embed), have isom : ∀n, isometry (coeZ ∘ (Φ n) ∘ (Y n).embed), { assume n, apply isometry.comp completion.coe_isometry _, apply isometry.comp _ (Y n).isom, apply to_inductive_limit_isometry }, -- The Hausdorff distance of `X2 n` and `X2 (n+1)` is by construction the distance between -- `u n` and `u (n+1)`, therefore bounded by `1/2^n` have D2 : ∀n, Hausdorff_dist (X2 n) (X2 n.succ) < (1/2)^n, { assume n, have X2n : X2 n = range ((coeZ ∘ (Φ n.succ) ∘ (c n) ∘ (to_glue_r (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ)))) ∘ (optimal_GH_injl (X n) (X n.succ))), { change X2 n = range (coeZ ∘ (Φ n.succ) ∘ (c n) ∘ (to_glue_r (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ))) ∘ (optimal_GH_injl (X n) (X n.succ))), simp only [X2, Φ], rw [← to_inductive_limit_commute I], simp only [f], rw ← to_glue_commute }, rw range_comp at X2n, have X2nsucc : X2 n.succ = range ((coeZ ∘ (Φ n.succ) ∘ (c n) ∘ (to_glue_r (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ)))) ∘ (optimal_GH_injr (X n) (X n.succ))), by refl, rw range_comp at X2nsucc, rw [X2n, X2nsucc, Hausdorff_dist_image, Hausdorff_dist_optimal, ← dist_GH_dist], { exact hu n n n.succ (le_refl n) (le_succ n) }, { apply isometry.comp completion.coe_isometry _, apply isometry.comp _ ((ic n).comp (to_glue_r_isometry _ _)), apply to_inductive_limit_isometry } }, -- consider `X2 n` as a member `X3 n` of the type of nonempty compact subsets of `Z`, which -- is a metric space let X3 : ℕ → nonempty_compacts Z := λn, ⟨X2 n, ⟨range_nonempty _, compact_range (isom n).continuous ⟩⟩, -- `X3 n` is a Cauchy sequence by construction, as the successive distances are -- bounded by `(1/2)^n` have : cauchy_seq X3, { refine cauchy_seq_of_le_geometric (1/2) 1 (by norm_num) (λn, _), rw one_mul, exact le_of_lt (D2 n) }, -- therefore, it converges to a limit `L` rcases cauchy_seq_tendsto_of_complete this with ⟨L, hL⟩, -- the images of `X3 n` in the Gromov-Hausdorff space converge to the image of `L` have M : tendsto (λn, (X3 n).to_GH_space) at_top (𝓝 L.to_GH_space) := tendsto.comp (to_GH_space_continuous.tendsto _) hL, -- By construction, the image of `X3 n` in the Gromov-Hausdorff space is `u n`. have : ∀n, (X3 n).to_GH_space = u n, { assume n, rw [nonempty_compacts.to_GH_space, ← (u n).to_GH_space_rep, to_GH_space_eq_to_GH_space_iff_isometric], constructor, convert (isom n).isometric_on_range.symm, }, -- Finally, we have proved the convergence of `u n` exact ⟨L.to_GH_space, by simpa [this] using M⟩ end end complete--section end Gromov_Hausdorff --namespace
2e45b5aa9e4aa3b3105003805a98ece03fed831c
366210ee84e5cc86b5ae5d69decefe097bb44783
/src/bigop.lean
1319a7213cc0d025916ea941e0b6c01651004c90
[]
no_license
ChrisHughes24/lean-scratchpad
016e47c4a2cb51a48f99e3249fe4563f3477f463
6cbc455cb7c5403c601a06e3146cd18b6e32a883
refs/heads/master
1,583,998,813,691
1,524,342,716,000
1,524,342,716,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,735
lean
import data.list.basic import data.nat.prime open classical open list /- The elements being manipulated are of type R, with operation op, with range r ∈ list I, filtered by the predicate P-/ variables {R : Type*} {I : Type*} (op : R → R → R) (nil: R) (r : list I) (P : I → Prop) [∀ i, decidable $ P i] (F : I → R) def apply_bigop : R := foldr (λ i x, if P i then op (F i) x else x) nil r /- variable in filtered list -/ local notation `big[`:0 op `/`:0 nil `]_(`:0 binder `∈` r `|` P:(scoped p, p) `)` F:(scoped f, f) := apply_bigop op nil r P F local notation `Σ_(`:0 binder `∈` r `|` P:(scoped p, p) `)` F:(scoped f, f) := apply_bigop (+) 0 r P F local notation `Π_(`:0 binder `∈` r `|` P:(scoped p, p) `)` F:(scoped f, f) := apply_bigop (*) 1 r P F /- variable in unfiltered list -/ local notation `big[`:0 op `/`:0 nil `]_(`:0 binder `∈` r `)` F:(scoped f, f) := apply_bigop op nil r (λ i, true) F local notation `Σ_(`:0 binder `∈` r `)` F:(scoped f, f) := apply_bigop (+) 0 r (λ i, true) F local notation `Π_(`:0 binder `∈` r `)` F:(scoped f, f) := apply_bigop (*) 1 r (λ i, true) F /- variable is natural numbers from a to b filtered -/ local notation `big[`:0 op `/`:0 nil `]_(`:0 binder `=` a `..` b `|` P:(scoped p, p) `)` F:(scoped f, f) := apply_bigop op nil (range' a (b-a+1)) P F local notation `Σ_(`:0 binder `=` a `..` b `|` P:(scoped p, p) `)` F:(scoped f, f) := apply_bigop (+) 0 (range' a (b-a+1)) P F local notation `Π_(`:0 binder `=` a `..` b `|` P:(scoped p, p) `)` F:(scoped f, f) := apply_bigop (*) 1 (range' a (b-a+1)) P F /- variable is natural numbers from a to b -/ local notation `big[`:0 op `/`:0 nil `]_(`:0 binder `=` a `..` b `)` F:(scoped f, f) := apply_bigop op nil (range' a (b-a+1)) (λ i, true) F local notation `Σ_(`:0 binder `=` a `..` b `)` F:(scoped f, f) := apply_bigop (+) 0 (range' a (b-a+1)) (λ i, true) F local notation `Π_(`:0 binder `=` a `..` b `)` F:(scoped f, f) := apply_bigop (*) 1 (range' a (b-a+1)) (λ i, true) F #eval big[(*)/1]_(i ∈ (range' 1 5) | true) i #eval big[(*)/1]_(i ∈ (range' 1 5)) i #eval big[(*)/1]_(i = 1 .. 5) i #eval big[(*)/1]_(i=1..5) i #eval Π_(i = 1..5) i #eval Π_(i ∈ (range' 1 5) | true) i #eval Σ_(i ∈ range 5 | nat.prime i) i #eval Σ_(i = 1..5 | nat.prime i) i lemma big_append [is_associative R op] (r₁ r₂ : list I) : (big[op/nil]_(i ∈ r₁++r₂ | (P i)) (F i)) = op (big[op/nil]_(i ∈ r₁ | (P i)) (F i)) (big[op/nil]_(i ∈ r₂ | (P i)) (F i)) := begin sorry end lemma sum_append (r₁ r₂ : list I) (F : I → ℕ): (Σ_(i ∈ r₁ ++ r₂ | P i) F i) = (Σ_(i ∈ r₁ | P i) F i) + Σ_(i ∈ r₂ | P i) F i := big_append _ _ _ _ _ _
1334b2beadca9e9c1f02643ba8853880ad27e615
a4673261e60b025e2c8c825dfa4ab9108246c32e
/src/Init/Data/Repr.lean
a09c84a149a65a5af9427175ece695bf3548c317
[ "Apache-2.0" ]
permissive
jcommelin/lean4
c02dec0cc32c4bccab009285475f265f17d73228
2909313475588cc20ac0436e55548a4502050d0a
refs/heads/master
1,674,129,550,893
1,606,415,348,000
1,606,415,348,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,853
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import Init.Data.String.Basic import Init.Data.UInt import Init.Data.Nat.Div import Init.Control.Id open Sum Subtype Nat universes u v class Repr (α : Type u) := (repr : α → String) export Repr (repr) -- This instance is needed because `id` is not reducible instance [Repr α] : Repr (id α) := inferInstanceAs (Repr α) instance : Repr Bool := ⟨fun b => cond b "true" "false"⟩ instance [Repr α] : Repr (Id α) := inferInstanceAs (Repr α) instance : Repr (Decidable p) := { repr := fun h => match h with | Decidable.isTrue _ => "true" | Decidable.isFalse _ => "false" } protected def List.reprAux [Repr α] : Bool → List α → String | b, [] => "" | true, x::xs => repr x ++ List.reprAux false xs | false, x::xs => ", " ++ repr x ++ List.reprAux false xs protected def List.repr [Repr α] : List α → String | [] => "[]" | x::xs => "[" ++ List.reprAux true (x::xs) ++ "]" instance [Repr α] : Repr (List α) := ⟨List.repr⟩ instance : Repr PUnit.{u+1} := ⟨fun u => "PUnit.unit"⟩ instance [Repr α] : Repr (ULift.{v} α) := ⟨fun v => "ULift.up (" ++ repr v.1 ++ ")"⟩ instance : Repr Unit := ⟨fun u => "()"⟩ instance [Repr α] : Repr (Option α) := ⟨fun | none => "none" | (some a) => "(some " ++ repr a ++ ")"⟩ instance [Repr α] [Repr β] : Repr (Sum α β) := ⟨fun | (inl a) => "(inl " ++ repr a ++ ")" | (inr b) => "(inr " ++ repr b ++ ")"⟩ instance [Repr α] [Repr β] : Repr (α × β) := ⟨fun ⟨a, b⟩ => "(" ++ repr a ++ ", " ++ repr b ++ ")"⟩ instance {β : α → Type v} [Repr α] [s : (x : α) → Repr (β x)] : Repr (Sigma β) := ⟨fun ⟨a, b⟩ => "⟨" ++ repr a ++ ", " ++ repr b ++ "⟩"⟩ instance {p : α → Prop} [Repr α] : Repr (Subtype p) := ⟨fun s => repr (val s)⟩ namespace Nat def digitChar (n : Nat) : Char := if n = 0 then '0' else if n = 1 then '1' else if n = 2 then '2' else if n = 3 then '3' else if n = 4 then '4' else if n = 5 then '5' else if n = 6 then '6' else if n = 7 then '7' else if n = 8 then '8' else if n = 9 then '9' else if n = 0xa then 'a' else if n = 0xb then 'b' else if n = 0xc then 'c' else if n = 0xd then 'd' else if n = 0xe then 'e' else if n = 0xf then 'f' else '*' def toDigitsCore (base : Nat) : Nat → Nat → List Char → List Char | 0, n, ds => ds | fuel+1, n, ds => let d := digitChar <| n % base; let n' := n / base; if n' = 0 then d::ds else toDigitsCore base fuel n' (d::ds) def toDigits (base : Nat) (n : Nat) : List Char := toDigitsCore base (n+1) n [] protected def repr (n : Nat) : String := (toDigits 10 n).asString def superDigitChar (n : Nat) : Char := if n = 0 then '⁰' else if n = 1 then '¹' else if n = 2 then '²' else if n = 3 then '³' else if n = 4 then '⁴' else if n = 5 then '⁵' else if n = 6 then '⁶' else if n = 7 then '⁷' else if n = 8 then '⁸' else if n = 9 then '⁹' else '*' partial def toSuperDigitsAux : Nat → List Char → List Char | n, ds => let d := superDigitChar <| n % 10; let n' := n / 10; if n' = 0 then d::ds else toSuperDigitsAux n' (d::ds) def toSuperDigits (n : Nat) : List Char := toSuperDigitsAux n [] def toSuperscriptString (n : Nat) : String := (toSuperDigits n).asString end Nat instance : Repr Nat := ⟨Nat.repr⟩ def hexDigitRepr (n : Nat) : String := String.singleton <| Nat.digitChar n def charToHex (c : Char) : String := let n := Char.toNat c; let d2 := n / 16; let d1 := n % 16; hexDigitRepr d2 ++ hexDigitRepr d1 def Char.quoteCore (c : Char) : String := if c = '\n' then "\\n" else if c = '\t' then "\\t" else if c = '\\' then "\\\\" else if c = '\"' then "\\\"" else if c.toNat <= 31 ∨ c = '\x7f' then "\\x" ++ charToHex c else String.singleton c instance : Repr Char := ⟨fun c => "'" ++ Char.quoteCore c ++ "'"⟩ def String.quote (s : String) : String := if s.isEmpty = true then "\"\"" else s.foldl (fun s c => s ++ c.quoteCore) "\"" ++ "\"" instance : Repr String := ⟨String.quote⟩ instance : Repr Substring := ⟨fun s => String.quote s.toString ++ ".toSubstring"⟩ instance : Repr String.Iterator := ⟨fun ⟨s, pos⟩ => "(String.Iterator.mk " ++ repr s ++ " " ++ repr pos ++ ")"⟩ instance (n : Nat) : Repr (Fin n) := ⟨fun f => repr (Fin.val f)⟩ instance : Repr UInt16 := ⟨fun n => repr n.toNat⟩ instance : Repr UInt32 := ⟨fun n => repr n.toNat⟩ instance : Repr UInt64 := ⟨fun n => repr n.toNat⟩ instance : Repr USize := ⟨fun n => repr n.toNat⟩ protected def Char.repr (c : Char) : String := repr c
229035bee5babd91ce8aa22e94c61247e3321fbc
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/measure_theory/covering/besicovitch.lean
f0576fecbea213990505ee52b3a63d8bb5a26235
[ "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
60,629
lean
/- Copyright (c) 2021 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import measure_theory.covering.differentiation import measure_theory.covering.vitali_family import measure_theory.integral.lebesgue import measure_theory.measure.regular import set_theory.ordinal.arithmetic import topology.metric_space.basic /-! # Besicovitch covering theorems The topological Besicovitch covering theorem ensures that, in a nice metric space, there exists a number `N` such that, from any family of balls with bounded radii, one can extract `N` families, each made of disjoint balls, covering together all the centers of the initial family. By "nice metric space", we mean a technical property stated as follows: there exists no satellite configuration of `N + 1` points (with a given parameter `τ > 1`). Such a configuration is a family of `N + 1` balls, where the first `N` balls all intersect the last one, but none of them contains the center of another one and their radii are controlled. This property is for instance satisfied by finite-dimensional real vector spaces. In this file, we prove the topological Besicovitch covering theorem, in `besicovitch.exist_disjoint_covering_families`. The measurable Besicovitch theorem ensures that, in the same class of metric spaces, if at every point one considers a class of balls of arbitrarily small radii, called admissible balls, then one can cover almost all the space by a family of disjoint admissible balls. It is deduced from the topological Besicovitch theorem, and proved in `besicovitch.exists_disjoint_closed_ball_covering_ae`. This implies that balls of small radius form a Vitali family in such spaces. Therefore, theorems on differentiation of measures hold as a consequence of general results. We restate them in this context to make them more easily usable. ## Main definitions and results * `satellite_config α N τ` is the type of all satellite configurations of `N + 1` points in the metric space `α`, with parameter `τ`. * `has_besicovitch_covering` is a class recording that there exist `N` and `τ > 1` such that there is no satellite configuration of `N + 1` points with parameter `τ`. * `exist_disjoint_covering_families` is the topological Besicovitch covering theorem: from any family of balls one can extract finitely many disjoint subfamilies covering the same set. * `exists_disjoint_closed_ball_covering` is the measurable Besicovitch covering theorem: from any family of balls with arbitrarily small radii at every point, one can extract countably many disjoint balls covering almost all the space. While the value of `N` is relevant for the precise statement of the topological Besicovitch theorem, it becomes irrelevant for the measurable one. Therefore, this statement is expressed using the `Prop`-valued typeclass `has_besicovitch_covering`. We also restate the following specialized versions of general theorems on differentiation of measures: * `besicovitch.ae_tendsto_rn_deriv` ensures that `ρ (closed_ball x r) / μ (closed_ball x r)` tends almost surely to the Radon-Nikodym derivative of `ρ` with respect to `μ` at `x`. * `besicovitch.ae_tendsto_measure_inter_div` states that almost every point in an arbitrary set `s` is a Lebesgue density point, i.e., `μ (s ∩ closed_ball x r) / μ (closed_ball x r)` tends to `1` as `r` tends to `0`. A stronger version for measurable sets is given in `besicovitch.ae_tendsto_measure_inter_div_of_measurable_set`. ## Implementation #### Sketch of proof of the topological Besicovitch theorem: We choose balls in a greedy way. First choose a ball with maximal radius (or rather, since there is no guarantee the maximal radius is realized, a ball with radius within a factor `τ` of the supremum). Then, remove all balls whose center is covered by the first ball, and choose among the remaining ones a ball with radius close to maximum. Go on forever until there is no available center (this is a transfinite induction in general). Then define inductively a coloring of the balls. A ball will be of color `i` if it intersects already chosen balls of color `0`, ..., `i - 1`, but none of color `i`. In this way, balls of the same color form a disjoint family, and the space is covered by the families of the different colors. The nontrivial part is to show that at most `N` colors are used. If one needs `N + 1` colors, consider the first time this happens. Then the corresponding ball intersects `N` balls of the different colors. Moreover, the inductive construction ensures that the radii of all the balls are controlled: they form a satellite configuration with `N + 1` balls (essentially by definition of satellite configurations). Since we assume that there are no such configurations, this is a contradiction. #### Sketch of proof of the measurable Besicovitch theorem: From the topological Besicovitch theorem, one can find a disjoint countable family of balls covering a proportion `> 1 / (N + 1)` of the space. Taking a large enough finite subset of these balls, one gets the same property for finitely many balls. Their union is closed. Therefore, any point in the complement has around it an admissible ball not intersecting these finitely many balls. Applying again the topological Besicovitch theorem, one extracts from these a disjoint countable subfamily covering a proportion `> 1 / (N + 1)` of the remaining points, and then even a disjoint finite subfamily. Then one goes on again and again, covering at each step a positive proportion of the remaining points, while remaining disjoint from the already chosen balls. The union of all these balls is the desired almost everywhere covering. -/ noncomputable theory universe u open metric set filter fin measure_theory topological_space open_locale topological_space classical big_operators ennreal measure_theory nnreal /-! ### Satellite configurations -/ /-- A satellite configuration is a configuration of `N+1` points that shows up in the inductive construction for the Besicovitch covering theorem. It depends on some parameter `τ ≥ 1`. This is a family of balls (indexed by `i : fin N.succ`, with center `c i` and radius `r i`) such that the last ball intersects all the other balls (condition `inter`), and given any two balls there is an order between them, ensuring that the first ball does not contain the center of the other one, and the radius of the second ball can not be larger than the radius of the first ball (up to a factor `τ`). This order corresponds to the order of choice in the inductive construction: otherwise, the second ball would have been chosen before. This is the condition `h`. Finally, the last ball is chosen after all the other ones, meaning that `h` can be strengthened by keeping only one side of the alternative in `hlast`. -/ structure besicovitch.satellite_config (α : Type*) [metric_space α] (N : ℕ) (τ : ℝ) := (c : fin N.succ → α) (r : fin N.succ → ℝ) (rpos : ∀ i, 0 < r i) (h : ∀ i j, i ≠ j → (r i ≤ dist (c i) (c j) ∧ r j ≤ τ * r i) ∨ (r j ≤ dist (c j) (c i) ∧ r i ≤ τ * r j)) (hlast : ∀ i < last N, r i ≤ dist (c i) (c (last N)) ∧ r (last N) ≤ τ * r i) (inter : ∀ i < last N, dist (c i) (c (last N)) ≤ r i + r (last N)) /-- A metric space has the Besicovitch covering property if there exist `N` and `τ > 1` such that there are no satellite configuration of parameter `τ` with `N+1` points. This is the condition that guarantees that the measurable Besicovitch covering theorem holds. It is satified by finite-dimensional real vector spaces. -/ class has_besicovitch_covering (α : Type*) [metric_space α] : Prop := (no_satellite_config [] : ∃ (N : ℕ) (τ : ℝ), 1 < τ ∧ is_empty (besicovitch.satellite_config α N τ)) /-- There is always a satellite configuration with a single point. -/ instance {α : Type*} {τ : ℝ} [inhabited α] [metric_space α] : inhabited (besicovitch.satellite_config α 0 τ) := ⟨{ c := default, r := λ i, 1, rpos := λ i, zero_lt_one, h := λ i j hij, (hij (subsingleton.elim i j)).elim, hlast := λ i hi, by { rw subsingleton.elim i (last 0) at hi, exact (lt_irrefl _ hi).elim }, inter := λ i hi, by { rw subsingleton.elim i (last 0) at hi, exact (lt_irrefl _ hi).elim } }⟩ namespace besicovitch namespace satellite_config variables {α : Type*} [metric_space α] {N : ℕ} {τ : ℝ} (a : satellite_config α N τ) lemma inter' (i : fin N.succ) : dist (a.c i) (a.c (last N)) ≤ a.r i + a.r (last N) := begin rcases lt_or_le i (last N) with H|H, { exact a.inter i H }, { have I : i = last N := top_le_iff.1 H, have := (a.rpos (last N)).le, simp only [I, add_nonneg this this, dist_self] } end lemma hlast' (i : fin N.succ) (h : 1 ≤ τ) : a.r (last N) ≤ τ * a.r i := begin rcases lt_or_le i (last N) with H|H, { exact (a.hlast i H).2 }, { have : i = last N := top_le_iff.1 H, rw this, exact le_mul_of_one_le_left (a.rpos _).le h } end end satellite_config /-! ### Extracting disjoint subfamilies from a ball covering -/ /-- A ball package is a family of balls in a metric space with positive bounded radii. -/ structure ball_package (β : Type*) (α : Type*) := (c : β → α) (r : β → ℝ) (rpos : ∀ b, 0 < r b) (r_bound : ℝ) (r_le : ∀ b, r b ≤ r_bound) /-- The ball package made of unit balls. -/ def unit_ball_package (α : Type*) : ball_package α α := { c := id, r := λ _, 1, rpos := λ _, zero_lt_one, r_bound := 1, r_le := λ _, le_rfl } instance (α : Type*) : inhabited (ball_package α α) := ⟨unit_ball_package α⟩ /-- A Besicovitch tau-package is a family of balls in a metric space with positive bounded radii, together with enough data to proceed with the Besicovitch greedy algorithm. We register this in a single structure to make sure that all our constructions in this algorithm only depend on one variable. -/ structure tau_package (β : Type*) (α : Type*) extends ball_package β α := (τ : ℝ) (one_lt_tau : 1 < τ) instance (α : Type*) : inhabited (tau_package α α) := ⟨{ τ := 2, one_lt_tau := one_lt_two, .. unit_ball_package α }⟩ variables {α : Type*} [metric_space α] {β : Type u} namespace tau_package variables [nonempty β] (p : tau_package β α) include p /-- Choose inductively large balls with centers that are not contained in the union of already chosen balls. This is a transfinite induction. -/ noncomputable def index : ordinal.{u} → β | i := -- `Z` is the set of points that are covered by already constructed balls let Z := ⋃ (j : {j // j < i}), ball (p.c (index j)) (p.r (index j)), -- `R` is the supremum of the radii of balls with centers not in `Z` R := supr (λ b : {b : β // p.c b ∉ Z}, p.r b) in -- return an index `b` for which the center `c b` is not in `Z`, and the radius is at -- least `R / τ`, if such an index exists (and garbage otherwise). classical.epsilon (λ b : β, p.c b ∉ Z ∧ R ≤ p.τ * p.r b) using_well_founded {dec_tac := `[exact j.2]} /-- The set of points that are covered by the union of balls selected at steps `< i`. -/ def Union_up_to (i : ordinal.{u}) : set α := ⋃ (j : {j // j < i}), ball (p.c (p.index j)) (p.r (p.index j)) lemma monotone_Union_up_to : monotone p.Union_up_to := begin assume i j hij, simp only [Union_up_to], exact Union_mono' (λ r, ⟨⟨r, r.2.trans_le hij⟩, subset.rfl⟩), end /-- Supremum of the radii of balls whose centers are not yet covered at step `i`. -/ def R (i : ordinal.{u}) : ℝ := supr (λ b : {b : β // p.c b ∉ p.Union_up_to i}, p.r b) /-- Group the balls into disjoint families, by assigning to a ball the smallest color for which it does not intersect any already chosen ball of this color. -/ noncomputable def color : ordinal.{u} → ℕ | i := let A : set ℕ := ⋃ (j : {j // j < i}) (hj : (closed_ball (p.c (p.index j)) (p.r (p.index j)) ∩ closed_ball (p.c (p.index i)) (p.r (p.index i))).nonempty), {color j} in Inf (univ \ A) using_well_founded {dec_tac := `[exact j.2]} /-- `p.last_step` is the first ordinal where the construction stops making sense, i.e., `f` returns garbage since there is no point left to be chosen. We will only use ordinals before this step. -/ def last_step : ordinal.{u} := Inf {i | ¬ ∃ (b : β), p.c b ∉ p.Union_up_to i ∧ p.R i ≤ p.τ * p.r b} lemma last_step_nonempty : {i | ¬ ∃ (b : β), p.c b ∉ p.Union_up_to i ∧ p.R i ≤ p.τ * p.r b}.nonempty := begin by_contra, suffices H : function.injective p.index, from not_injective_of_ordinal p.index H, assume x y hxy, wlog x_le_y : x ≤ y := le_total x y using [x y, y x], rcases eq_or_lt_of_le x_le_y with rfl|H, { refl }, simp only [nonempty_def, not_exists, exists_prop, not_and, not_lt, not_le, mem_set_of_eq, not_forall] at h, specialize h y, have A : p.c (p.index y) ∉ p.Union_up_to y, { have : p.index y = classical.epsilon (λ b : β, p.c b ∉ p.Union_up_to y ∧ p.R y ≤ p.τ * p.r b), by { rw [tau_package.index], refl }, rw this, exact (classical.epsilon_spec h).1 }, simp only [Union_up_to, not_exists, exists_prop, mem_Union, mem_closed_ball, not_and, not_le, subtype.exists, subtype.coe_mk] at A, specialize A x H, simp [hxy] at A, exact (lt_irrefl _ ((p.rpos (p.index y)).trans_le A)).elim end /-- Every point is covered by chosen balls, before `p.last_step`. -/ lemma mem_Union_up_to_last_step (x : β) : p.c x ∈ p.Union_up_to p.last_step := begin have A : ∀ (z : β), p.c z ∈ p.Union_up_to p.last_step ∨ p.τ * p.r z < p.R p.last_step, { have : p.last_step ∈ {i | ¬ ∃ (b : β), p.c b ∉ p.Union_up_to i ∧ p.R i ≤ p.τ * p.r b} := Inf_mem p.last_step_nonempty, simpa only [not_exists, mem_set_of_eq, not_and_distrib, not_le, not_not_mem] }, by_contra, rcases A x with H|H, { exact h H }, have Rpos : 0 < p.R p.last_step, { apply lt_trans (mul_pos (_root_.zero_lt_one.trans p.one_lt_tau) (p.rpos _)) H }, have B : p.τ⁻¹ * p.R p.last_step < p.R p.last_step, { conv_rhs { rw ← one_mul (p.R p.last_step) }, exact mul_lt_mul (inv_lt_one p.one_lt_tau) le_rfl Rpos zero_le_one }, obtain ⟨y, hy1, hy2⟩ : ∃ (y : β), p.c y ∉ p.Union_up_to p.last_step ∧ (p.τ)⁻¹ * p.R p.last_step < p.r y, { simpa only [exists_prop, mem_range, exists_exists_and_eq_and, subtype.exists, subtype.coe_mk] using exists_lt_of_lt_cSup _ B, rw [← image_univ, nonempty_image_iff], exact ⟨⟨_, h⟩, mem_univ _⟩ }, rcases A y with Hy|Hy, { exact hy1 Hy }, { rw ← div_eq_inv_mul at hy2, have := (div_le_iff' (_root_.zero_lt_one.trans p.one_lt_tau)).1 hy2.le, exact lt_irrefl _ (Hy.trans_le this) } end /-- If there are no configurations of satellites with `N+1` points, one never uses more than `N` distinct families in the Besicovitch inductive construction. -/ lemma color_lt {i : ordinal.{u}} (hi : i < p.last_step) {N : ℕ} (hN : is_empty (satellite_config α N p.τ)) : p.color i < N := begin /- By contradiction, consider the first ordinal `i` for which one would have `p.color i = N`. Choose for each `k < N` a ball with color `k` that intersects the ball at color `i` (there is such a ball, otherwise one would have used the color `k` and not `N`). Then this family of `N+1` balls forms a satellite configuration, which is forbidden by the assumption `hN`. -/ induction i using ordinal.induction with i IH, let A : set ℕ := ⋃ (j : {j // j < i}) (hj : (closed_ball (p.c (p.index j)) (p.r (p.index j)) ∩ closed_ball (p.c (p.index i)) (p.r (p.index i))).nonempty), {p.color j}, have color_i : p.color i = Inf (univ \ A), by rw [color], rw color_i, have N_mem : N ∈ univ \ A, { simp only [not_exists, true_and, exists_prop, mem_Union, mem_singleton_iff, mem_closed_ball, not_and, mem_univ, mem_diff, subtype.exists, subtype.coe_mk], assume j ji hj, exact (IH j ji (ji.trans hi)).ne' }, suffices : Inf (univ \ A) ≠ N, { rcases (cInf_le (order_bot.bdd_below (univ \ A)) N_mem).lt_or_eq with H|H, { exact H }, { exact (this H).elim } }, assume Inf_eq_N, have : ∀ k, k < N → ∃ j, j < i ∧ (closed_ball (p.c (p.index j)) (p.r (p.index j)) ∩ closed_ball (p.c (p.index i)) (p.r (p.index i))).nonempty ∧ k = p.color j, { assume k hk, rw ← Inf_eq_N at hk, have : k ∈ A, by simpa only [true_and, mem_univ, not_not, mem_diff] using nat.not_mem_of_lt_Inf hk, simp at this, simpa only [exists_prop, mem_Union, mem_singleton_iff, mem_closed_ball, subtype.exists, subtype.coe_mk] }, choose! g hg using this, -- Choose for each `k < N` an ordinal `G k < i` giving a ball of color `k` intersecting -- the last ball. let G : ℕ → ordinal := λ n, if n = N then i else g n, have color_G : ∀ n, n ≤ N → p.color (G n) = n, { assume n hn, unfreezingI { rcases hn.eq_or_lt with rfl|H }, { simp only [G], simp only [color_i, Inf_eq_N, if_true, eq_self_iff_true] }, { simp only [G], simp only [H.ne, (hg n H).right.right.symm, if_false] } }, have G_lt_last : ∀ n, n ≤ N → G n < p.last_step, { assume n hn, unfreezingI { rcases hn.eq_or_lt with rfl|H }, { simp only [G], simp only [hi, if_true, eq_self_iff_true], }, { simp only [G], simp only [H.ne, (hg n H).left.trans hi, if_false] } }, have fGn : ∀ n, n ≤ N → p.c (p.index (G n)) ∉ p.Union_up_to (G n) ∧ p.R (G n) ≤ p.τ * p.r (p.index (G n)), { assume n hn, have: p.index (G n) = classical.epsilon (λ t, p.c t ∉ p.Union_up_to (G n) ∧ p.R (G n) ≤ p.τ * p.r t), by { rw index, refl }, rw this, have : ∃ t, p.c t ∉ p.Union_up_to (G n) ∧ p.R (G n) ≤ p.τ * p.r t, by simpa only [not_exists, exists_prop, not_and, not_lt, not_le, mem_set_of_eq, not_forall] using not_mem_of_lt_cInf (G_lt_last n hn) (order_bot.bdd_below _), exact classical.epsilon_spec this }, -- the balls with indices `G k` satisfy the characteristic property of satellite configurations. have Gab : ∀ (a b : fin (nat.succ N)), G a < G b → p.r (p.index (G a)) ≤ dist (p.c (p.index (G a))) (p.c (p.index (G b))) ∧ p.r (p.index (G b)) ≤ p.τ * p.r (p.index (G a)), { assume a b G_lt, have ha : (a : ℕ) ≤ N := nat.lt_succ_iff.1 a.2, have hb : (b : ℕ) ≤ N := nat.lt_succ_iff.1 b.2, split, { have := (fGn b hb).1, simp only [Union_up_to, not_exists, exists_prop, mem_Union, mem_closed_ball, not_and, not_le, subtype.exists, subtype.coe_mk] at this, simpa only [dist_comm, mem_ball, not_lt] using this (G a) G_lt }, { apply le_trans _ (fGn a ha).2, have B : p.c (p.index (G b)) ∉ p.Union_up_to (G a), { assume H, exact (fGn b hb).1 (p.monotone_Union_up_to G_lt.le H) }, let b' : {t // p.c t ∉ p.Union_up_to (G a)} := ⟨p.index (G b), B⟩, apply @le_csupr _ _ _ (λ t : {t // p.c t ∉ p.Union_up_to (G a)}, p.r t) _ b', refine ⟨p.r_bound, λ t ht, _⟩, simp only [exists_prop, mem_range, subtype.exists, subtype.coe_mk] at ht, rcases ht with ⟨u, hu⟩, rw ← hu.2, exact p.r_le _ } }, -- therefore, one may use them to construct a satellite configuration with `N+1` points let sc : satellite_config α N p.τ := { c := λ k, p.c (p.index (G k)), r := λ k, p.r (p.index (G k)), rpos := λ k, p.rpos (p.index (G k)), h := begin assume a b a_ne_b, wlog G_le : G a ≤ G b := le_total (G a) (G b) using [a b, b a] tactic.skip, { have G_lt : G a < G b, { rcases G_le.lt_or_eq with H|H, { exact H }, have A : (a : ℕ) ≠ b := fin.coe_injective.ne a_ne_b, rw [← color_G a (nat.lt_succ_iff.1 a.2), ← color_G b (nat.lt_succ_iff.1 b.2), H] at A, exact (A rfl).elim }, exact or.inl (Gab a b G_lt) }, { assume a_ne_b, rw or_comm, exact this a_ne_b.symm } end, hlast := begin assume a ha, have I : (a : ℕ) < N := ha, have : G a < G (fin.last N), by { dsimp [G], simp [I.ne, (hg a I).1] }, exact Gab _ _ this, end, inter := begin assume a ha, have I : (a : ℕ) < N := ha, have J : G (fin.last N) = i, by { dsimp [G], simp only [if_true, eq_self_iff_true], }, have K : G a = g a, { dsimp [G], simp [I.ne, (hg a I).1] }, convert dist_le_add_of_nonempty_closed_ball_inter_closed_ball (hg _ I).2.1, end }, -- this is a contradiction exact (hN.false : _) sc end end tau_package open tau_package /-- The topological Besicovitch covering theorem: there exist finitely many families of disjoint balls covering all the centers in a package. More specifically, one can use `N` families if there are no satellite configurations with `N+1` points. -/ theorem exist_disjoint_covering_families {N : ℕ} {τ : ℝ} (hτ : 1 < τ) (hN : is_empty (satellite_config α N τ)) (q : ball_package β α) : ∃ s : fin N → set β, (∀ (i : fin N), (s i).pairwise_disjoint (λ j, closed_ball (q.c j) (q.r j))) ∧ (range q.c ⊆ ⋃ (i : fin N), ⋃ (j ∈ s i), ball (q.c j) (q.r j)) := begin -- first exclude the trivial case where `β` is empty (we need non-emptiness for the transfinite -- induction, to be able to choose garbage when there is no point left). casesI is_empty_or_nonempty β, { refine ⟨λ i, ∅, λ i, pairwise_disjoint_empty, _⟩, rw [← image_univ, eq_empty_of_is_empty (univ : set β)], simp }, -- Now, assume `β` is nonempty. let p : tau_package β α := { τ := τ, one_lt_tau := hτ, .. q }, -- we use for `s i` the balls of color `i`. let s := λ (i : fin N), ⋃ (k : ordinal.{u}) (hk : k < p.last_step) (h'k : p.color k = i), ({p.index k} : set β), refine ⟨s, λ i, _, _⟩, { -- show that balls of the same color are disjoint assume x hx y hy x_ne_y, obtain ⟨jx, jx_lt, jxi, rfl⟩ : ∃ (jx : ordinal), jx < p.last_step ∧ p.color jx = i ∧ x = p.index jx, by simpa only [exists_prop, mem_Union, mem_singleton_iff] using hx, obtain ⟨jy, jy_lt, jyi, rfl⟩ : ∃ (jy : ordinal), jy < p.last_step ∧ p.color jy = i ∧ y = p.index jy, by simpa only [exists_prop, mem_Union, mem_singleton_iff] using hy, wlog jxy : jx ≤ jy := le_total jx jy using [jx jy, jy jx] tactic.skip, swap, { assume h1 h2 h3 h4 h5 h6 h7, rw [function.on_fun, disjoint.comm], exact this h4 h5 h6 h1 h2 h3 h7.symm }, replace jxy : jx < jy, by { rcases lt_or_eq_of_le jxy with H|rfl, { exact H }, { exact (x_ne_y rfl).elim } }, let A : set ℕ := ⋃ (j : {j // j < jy}) (hj : (closed_ball (p.c (p.index j)) (p.r (p.index j)) ∩ closed_ball (p.c (p.index jy)) (p.r (p.index jy))).nonempty), {p.color j}, have color_j : p.color jy = Inf (univ \ A), by rw [tau_package.color], have : p.color jy ∈ univ \ A, { rw color_j, apply Inf_mem, refine ⟨N, _⟩, simp only [not_exists, true_and, exists_prop, mem_Union, mem_singleton_iff, not_and, mem_univ, mem_diff, subtype.exists, subtype.coe_mk], assume k hk H, exact (p.color_lt (hk.trans jy_lt) hN).ne' }, simp only [not_exists, true_and, exists_prop, mem_Union, mem_singleton_iff, not_and, mem_univ, mem_diff, subtype.exists, subtype.coe_mk] at this, specialize this jx jxy, contrapose! this, simpa only [jxi, jyi, and_true, eq_self_iff_true, ← not_disjoint_iff_nonempty_inter] }, { -- show that the balls of color at most `N` cover every center. refine range_subset_iff.2 (λ b, _), obtain ⟨a, ha⟩ : ∃ (a : ordinal), a < p.last_step ∧ dist (p.c b) (p.c (p.index a)) < p.r (p.index a), by simpa only [Union_up_to, exists_prop, mem_Union, mem_ball, subtype.exists, subtype.coe_mk] using p.mem_Union_up_to_last_step b, simp only [exists_prop, mem_Union, mem_ball, mem_singleton_iff, bUnion_and', exists_eq_left, Union_exists, exists_and_distrib_left], exact ⟨⟨p.color a, p.color_lt ha.1 hN⟩, a, rfl, ha⟩ } end /-! ### The measurable Besicovitch covering theorem -/ open_locale nnreal variables [second_countable_topology α] [measurable_space α] [opens_measurable_space α] /-- Consider, for each `x` in a set `s`, a radius `r x ∈ (0, 1]`. Then one can find finitely many disjoint balls of the form `closed_ball x (r x)` covering a proportion `1/(N+1)` of `s`, if there are no satellite configurations with `N+1` points. -/ lemma exist_finset_disjoint_balls_large_measure (μ : measure α) [is_finite_measure μ] {N : ℕ} {τ : ℝ} (hτ : 1 < τ) (hN : is_empty (satellite_config α N τ)) (s : set α) (r : α → ℝ) (rpos : ∀ x ∈ s, 0 < r x) (rle : ∀ x ∈ s, r x ≤ 1) : ∃ (t : finset α), (↑t ⊆ s) ∧ μ (s \ (⋃ (x ∈ t), closed_ball x (r x))) ≤ N/(N+1) * μ s ∧ (t : set α).pairwise_disjoint (λ x, closed_ball x (r x)) := begin -- exclude the trivial case where `μ s = 0`. rcases le_or_lt (μ s) 0 with hμs|hμs, { have : μ s = 0 := le_bot_iff.1 hμs, refine ⟨∅, by simp only [finset.coe_empty, empty_subset], _, _⟩, { simp only [this, diff_empty, Union_false, Union_empty, nonpos_iff_eq_zero, mul_zero] }, { simp only [finset.coe_empty, pairwise_disjoint_empty], } }, casesI is_empty_or_nonempty α, { simp only [eq_empty_of_is_empty s, measure_empty] at hμs, exact (lt_irrefl _ hμs).elim }, have Npos : N ≠ 0, { unfreezingI { rintros rfl }, inhabit α, exact (not_is_empty_of_nonempty _) hN }, -- introduce a measurable superset `o` with the same measure, for measure computations obtain ⟨o, so, omeas, μo⟩ : ∃ (o : set α), s ⊆ o ∧ measurable_set o ∧ μ o = μ s := exists_measurable_superset μ s, /- We will apply the topological Besicovitch theorem, giving `N` disjoint subfamilies of balls covering `s`. Among these, one of them covers a proportion at least `1/N` of `s`. A large enough finite subfamily will then cover a proportion at least `1/(N+1)`. -/ let a : ball_package s α := { c := λ x, x, r := λ x, r x, rpos := λ x, rpos x x.2, r_bound := 1, r_le := λ x, rle x x.2 }, rcases exist_disjoint_covering_families hτ hN a with ⟨u, hu, hu'⟩, have u_count : ∀ i, (u i).countable, { assume i, refine (hu i).countable_of_nonempty_interior (λ j hj, _), have : (ball (j : α) (r j)).nonempty := nonempty_ball.2 (a.rpos _), exact this.mono ball_subset_interior_closed_ball }, let v : fin N → set α := λ i, ⋃ (x : s) (hx : x ∈ u i), closed_ball x (r x), have : ∀ i, measurable_set (v i) := λ i, measurable_set.bUnion (u_count i) (λ b hb, measurable_set_closed_ball), have A : s = ⋃ (i : fin N), s ∩ v i, { refine subset.antisymm _ (Union_subset (λ i, inter_subset_left _ _)), assume x hx, obtain ⟨i, y, hxy, h'⟩ : ∃ (i : fin N) (i_1 : ↥s) (i : i_1 ∈ u i), x ∈ ball ↑i_1 (r ↑i_1), { have : x ∈ range a.c, by simpa only [subtype.range_coe_subtype, set_of_mem_eq], simpa only [mem_Union] using hu' this }, refine mem_Union.2 ⟨i, ⟨hx, _⟩⟩, simp only [v, exists_prop, mem_Union, set_coe.exists, exists_and_distrib_right, subtype.coe_mk], exact ⟨y, ⟨y.2, by simpa only [subtype.coe_eta]⟩, ball_subset_closed_ball h'⟩ }, have S : ∑ (i : fin N), μ s / N ≤ ∑ i, μ (s ∩ v i) := calc ∑ (i : fin N), μ s / N = μ s : begin simp only [finset.card_fin, finset.sum_const, nsmul_eq_mul], rw ennreal.mul_div_cancel', { simp only [Npos, ne.def, nat.cast_eq_zero, not_false_iff] }, { exact (ennreal.nat_ne_top _) } end ... ≤ ∑ i, μ (s ∩ v i) : by { conv_lhs { rw A }, apply measure_Union_fintype_le }, -- choose an index `i` of a subfamily covering at least a proportion `1/N` of `s`. obtain ⟨i, -, hi⟩ : ∃ (i : fin N) (hi : i ∈ finset.univ), μ s / N ≤ μ (s ∩ v i), { apply ennreal.exists_le_of_sum_le _ S, exact ⟨⟨0, bot_lt_iff_ne_bot.2 Npos⟩, finset.mem_univ _⟩ }, replace hi : μ s / (N + 1) < μ (s ∩ v i), { apply lt_of_lt_of_le _ hi, apply (ennreal.mul_lt_mul_left hμs.ne' (measure_lt_top μ s).ne).2, rw ennreal.inv_lt_inv, conv_lhs {rw ← add_zero (N : ℝ≥0∞) }, exact ennreal.add_lt_add_left (ennreal.nat_ne_top N) ennreal.zero_lt_one }, have B : μ (o ∩ v i) = ∑' (x : u i), μ (o ∩ closed_ball x (r x)), { have : o ∩ v i = ⋃ (x : s) (hx : x ∈ u i), o ∩ closed_ball x (r x), by simp only [inter_Union], rw [this, measure_bUnion (u_count i)], { refl }, { exact (hu i).mono (λ k, inter_subset_right _ _) }, { exact λ b hb, omeas.inter measurable_set_closed_ball } }, -- A large enough finite subfamily of `u i` will also cover a proportion `> 1/(N+1)` of `s`. -- Since `s` might not be measurable, we express this in terms of the measurable superset `o`. obtain ⟨w, hw⟩ : ∃ (w : finset (u i)), μ s / (N + 1) < ∑ (x : u i) in w, μ (o ∩ closed_ball (x : α) (r (x : α))), { have C : has_sum (λ (x : u i), μ (o ∩ closed_ball x (r x))) (μ (o ∩ v i)), by { rw B, exact ennreal.summable.has_sum }, have : μ s / (N+1) < μ (o ∩ v i) := hi.trans_le (measure_mono (inter_subset_inter_left _ so)), exact ((tendsto_order.1 C).1 _ this).exists }, -- Bring back the finset `w i` of `↑(u i)` to a finset of `α`, and check that it works by design. refine ⟨finset.image (λ (x : u i), x) w, _, _, _⟩, -- show that the finset is included in `s`. { simp only [image_subset_iff, coe_coe, finset.coe_image], assume y hy, simp only [subtype.coe_prop, mem_preimage] }, -- show that it covers a large enough proportion of `s`. For measure computations, we do not -- use `s` (which might not be measurable), but its measurable superset `o`. Since their measures -- are the same, this does not spoil the estimates { suffices H : μ (o \ ⋃ x ∈ w, closed_ball ↑x (r ↑x)) ≤ N/(N+1) * μ s, { rw [finset.set_bUnion_finset_image], exact le_trans (measure_mono (diff_subset_diff so (subset.refl _))) H }, rw [← diff_inter_self_eq_diff, measure_diff_le_iff_le_add _ (inter_subset_right _ _) ((measure_lt_top μ _).ne)], swap, { apply measurable_set.inter _ omeas, haveI : encodable (u i) := (u_count i).to_encodable, exact measurable_set.Union (λ b, measurable_set.Union (λ hb, measurable_set_closed_ball)) }, calc μ o = 1/(N+1) * μ s + N/(N+1) * μ s : by { rw [μo, ← add_mul, ennreal.div_add_div_same, add_comm, ennreal.div_self, one_mul]; simp } ... ≤ μ ((⋃ (x ∈ w), closed_ball ↑x (r ↑x)) ∩ o) + N/(N+1) * μ s : begin refine add_le_add _ le_rfl, rw [div_eq_mul_inv, one_mul, mul_comm, ← div_eq_mul_inv], apply hw.le.trans (le_of_eq _), rw [← finset.set_bUnion_coe, inter_comm _ o, inter_Union₂, finset.set_bUnion_coe, measure_bUnion_finset], { have : (w : set (u i)).pairwise_disjoint (λ (b : u i), closed_ball (b : α) (r (b : α))), by { assume k hk l hl hkl, exact hu i k.2 l.2 (subtype.coe_injective.ne hkl) }, exact this.mono (λ k, inter_subset_right _ _) }, { assume b hb, apply omeas.inter measurable_set_closed_ball } end }, -- show that the balls are disjoint { assume k hk l hl hkl, obtain ⟨k', k'w, rfl⟩ : ∃ (k' : u i), k' ∈ w ∧ ↑↑k' = k, by simpa only [mem_image, finset.mem_coe, coe_coe, finset.coe_image] using hk, obtain ⟨l', l'w, rfl⟩ : ∃ (l' : u i), l' ∈ w ∧ ↑↑l' = l, by simpa only [mem_image, finset.mem_coe, coe_coe, finset.coe_image] using hl, have k'nel' : (k' : s) ≠ l', by { assume h, rw h at hkl, exact hkl rfl }, exact hu i k'.2 l'.2 k'nel' } end variable [has_besicovitch_covering α] /-- The measurable Besicovitch covering theorem. Assume that, for any `x` in a set `s`, one is given a set of admissible closed balls centered at `x`, with arbitrarily small radii. Then there exists a disjoint covering of almost all `s` by admissible closed balls centered at some points of `s`. This version requires that the underlying measure is finite, and that the space has the Besicovitch covering property (which is satisfied for instance by normed real vector spaces). It expresses the conclusion in a slightly awkward form (with a subset of `α × ℝ`) coming from the proof technique. For a version assuming that the measure is sigma-finite, see `exists_disjoint_closed_ball_covering_ae_aux`. For a version giving the conclusion in a nicer form, see `exists_disjoint_closed_ball_covering_ae`. -/ theorem exists_disjoint_closed_ball_covering_ae_of_finite_measure_aux (μ : measure α) [is_finite_measure μ] (f : α → set ℝ) (s : set α) (hf : ∀ x ∈ s, ∀ δ > 0, (f x ∩ Ioo 0 δ).nonempty) : ∃ (t : set (α × ℝ)), t.countable ∧ (∀ (p : α × ℝ), p ∈ t → p.1 ∈ s) ∧ (∀ (p : α × ℝ), p ∈ t → p.2 ∈ f p.1) ∧ μ (s \ (⋃ (p : α × ℝ) (hp : p ∈ t), closed_ball p.1 p.2)) = 0 ∧ t.pairwise_disjoint (λ p, closed_ball p.1 p.2) := begin rcases has_besicovitch_covering.no_satellite_config α with ⟨N, τ, hτ, hN⟩, /- Introduce a property `P` on finsets saying that we have a nice disjoint covering of a subset of `s` by admissible balls. -/ let P : finset (α × ℝ) → Prop := λ t, (t : set (α × ℝ)).pairwise_disjoint (λ p, closed_ball p.1 p.2) ∧ (∀ (p : α × ℝ), p ∈ t → p.1 ∈ s) ∧ (∀ (p : α × ℝ), p ∈ t → p.2 ∈ f p.1), /- Given a finite good covering of a subset `s`, one can find a larger finite good covering, covering additionally a proportion at least `1/(N+1)` of leftover points. This follows from `exist_finset_disjoint_balls_large_measure` applied to balls not intersecting the initial covering. -/ have : ∀ (t : finset (α × ℝ)), P t → ∃ (u : finset (α × ℝ)), t ⊆ u ∧ P u ∧ μ (s \ (⋃ (p : α × ℝ) (hp : p ∈ u), closed_ball p.1 p.2)) ≤ N/(N+1) * μ (s \ (⋃ (p : α × ℝ) (hp : p ∈ t), closed_ball p.1 p.2)), { assume t ht, set B := ⋃ (p : α × ℝ) (hp : p ∈ t), closed_ball p.1 p.2 with hB, have B_closed : is_closed B := is_closed_bUnion (finset.finite_to_set _) (λ i hi, is_closed_ball), set s' := s \ B with hs', have : ∀ x ∈ s', ∃ r ∈ f x ∩ Ioo 0 1, disjoint B (closed_ball x r), { assume x hx, have xs : x ∈ s := ((mem_diff x).1 hx).1, rcases eq_empty_or_nonempty B with hB|hB, { have : (0 : ℝ) < 1 := zero_lt_one, rcases hf x xs 1 zero_lt_one with ⟨r, hr, h'r⟩, exact ⟨r, ⟨hr, h'r⟩, by simp only [hB, empty_disjoint]⟩ }, { let R := inf_dist x B, have : 0 < min R 1 := lt_min ((B_closed.not_mem_iff_inf_dist_pos hB).1 ((mem_diff x).1 hx).2) zero_lt_one, rcases hf x xs _ this with ⟨r, hr, h'r⟩, refine ⟨r, ⟨hr, ⟨h'r.1, h'r.2.trans_le (min_le_right _ _)⟩⟩, _⟩, rw disjoint.comm, exact disjoint_closed_ball_of_lt_inf_dist (h'r.2.trans_le (min_le_left _ _)) } }, choose! r hr using this, obtain ⟨v, vs', hμv, hv⟩ : ∃ (v : finset α), ↑v ⊆ s' ∧ μ (s' \ ⋃ (x ∈ v), closed_ball x (r x)) ≤ N/(N+1) * μ s' ∧ (v : set α).pairwise_disjoint (λ (x : α), closed_ball x (r x)), { have rI : ∀ x ∈ s', r x ∈ Ioo (0 : ℝ) 1 := λ x hx, (hr x hx).1.2, exact exist_finset_disjoint_balls_large_measure μ hτ hN s' r (λ x hx, (rI x hx).1) (λ x hx, (rI x hx).2.le) }, refine ⟨t ∪ (finset.image (λ x, (x, r x)) v), finset.subset_union_left _ _, ⟨_, _, _⟩, _⟩, { simp only [finset.coe_union, pairwise_disjoint_union, ht.1, true_and, finset.coe_image], split, { assume p hp q hq hpq, rcases (mem_image _ _ _).1 hp with ⟨p', p'v, rfl⟩, rcases (mem_image _ _ _).1 hq with ⟨q', q'v, rfl⟩, refine hv p'v q'v (λ hp'q', _), rw [hp'q'] at hpq, exact hpq rfl }, { assume p hp q hq hpq, rcases (mem_image _ _ _).1 hq with ⟨q', q'v, rfl⟩, apply disjoint_of_subset_left _ (hr q' (vs' q'v)).2, rw [hB, ← finset.set_bUnion_coe], exact subset_bUnion_of_mem hp } }, { assume p hp, rcases finset.mem_union.1 hp with h'p|h'p, { exact ht.2.1 p h'p }, { rcases finset.mem_image.1 h'p with ⟨p', p'v, rfl⟩, exact ((mem_diff _).1 (vs' (finset.mem_coe.2 p'v))).1 } }, { assume p hp, rcases finset.mem_union.1 hp with h'p|h'p, { exact ht.2.2 p h'p }, { rcases finset.mem_image.1 h'p with ⟨p', p'v, rfl⟩, exact (hr p' (vs' p'v)).1.1 } }, { convert hμv using 2, rw [finset.set_bUnion_union, ← diff_diff, finset.set_bUnion_finset_image] } }, /- Define `F` associating to a finite good covering the above enlarged good covering, covering a proportion `1/(N+1)` of leftover points. Iterating `F`, one will get larger and larger good coverings, missing in the end only a measure-zero set. -/ choose! F hF using this, let u := λ n, F^[n] ∅, have u_succ : ∀ (n : ℕ), u n.succ = F (u n) := λ n, by simp only [u, function.comp_app, function.iterate_succ'], have Pu : ∀ n, P (u n), { assume n, induction n with n IH, { simp only [u, P, prod.forall, id.def, function.iterate_zero], simp only [finset.not_mem_empty, is_empty.forall_iff, finset.coe_empty, forall_2_true_iff, and_self, pairwise_disjoint_empty] }, { rw u_succ, exact (hF (u n) IH).2.1 } }, refine ⟨⋃ n, u n, countable_Union (λ n, (u n).countable_to_set), _, _, _, _⟩, { assume p hp, rcases mem_Union.1 hp with ⟨n, hn⟩, exact (Pu n).2.1 p (finset.mem_coe.1 hn) }, { assume p hp, rcases mem_Union.1 hp with ⟨n, hn⟩, exact (Pu n).2.2 p (finset.mem_coe.1 hn) }, { have A : ∀ n, μ (s \ ⋃ (p : α × ℝ) (hp : p ∈ ⋃ (n : ℕ), (u n : set (α × ℝ))), closed_ball p.fst p.snd) ≤ μ (s \ ⋃ (p : α × ℝ) (hp : p ∈ u n), closed_ball p.fst p.snd), { assume n, apply measure_mono, apply diff_subset_diff (subset.refl _), exact bUnion_subset_bUnion_left (subset_Union (λ i, (u i : set (α × ℝ))) n) }, have B : ∀ n, μ (s \ ⋃ (p : α × ℝ) (hp : p ∈ u n), closed_ball p.fst p.snd) ≤ (N/(N+1))^n * μ s, { assume n, induction n with n IH, { simp only [le_refl, diff_empty, one_mul, Union_false, Union_empty, pow_zero] }, calc μ (s \ ⋃ (p : α × ℝ) (hp : p ∈ u n.succ), closed_ball p.fst p.snd) ≤ (N/(N+1)) * μ (s \ ⋃ (p : α × ℝ) (hp : p ∈ u n), closed_ball p.fst p.snd) : by { rw u_succ, exact (hF (u n) (Pu n)).2.2 } ... ≤ (N/(N+1))^n.succ * μ s : by { rw [pow_succ, mul_assoc], exact ennreal.mul_le_mul le_rfl IH } }, have C : tendsto (λ (n : ℕ), ((N : ℝ≥0∞)/(N+1))^n * μ s) at_top (𝓝 (0 * μ s)), { apply ennreal.tendsto.mul_const _ (or.inr (measure_lt_top μ s).ne), apply ennreal.tendsto_pow_at_top_nhds_0_of_lt_1, rw [ennreal.div_lt_iff, one_mul], { conv_lhs {rw ← add_zero (N : ℝ≥0∞) }, exact ennreal.add_lt_add_left (ennreal.nat_ne_top N) ennreal.zero_lt_one }, { simp only [true_or, add_eq_zero_iff, ne.def, not_false_iff, one_ne_zero, and_false] }, { simp only [ennreal.nat_ne_top, ne.def, not_false_iff, or_true] } }, rw zero_mul at C, apply le_bot_iff.1, exact le_of_tendsto_of_tendsto' tendsto_const_nhds C (λ n, (A n).trans (B n)) }, { refine (pairwise_disjoint_Union _).2 (λ n, (Pu n).1), apply (monotone_nat_of_le_succ (λ n, _)).directed_le, rw u_succ, exact (hF (u n) (Pu n)).1 } end /-- The measurable Besicovitch covering theorem. Assume that, for any `x` in a set `s`, one is given a set of admissible closed balls centered at `x`, with arbitrarily small radii. Then there exists a disjoint covering of almost all `s` by admissible closed balls centered at some points of `s`. This version requires that the underlying measure is sigma-finite, and that the space has the Besicovitch covering property (which is satisfied for instance by normed real vector spaces). It expresses the conclusion in a slightly awkward form (with a subset of `α × ℝ`) coming from the proof technique. For a version giving the conclusion in a nicer form, see `exists_disjoint_closed_ball_covering_ae`. -/ theorem exists_disjoint_closed_ball_covering_ae_aux (μ : measure α) [sigma_finite μ] (f : α → set ℝ) (s : set α) (hf : ∀ x ∈ s, ∀ δ > 0, (f x ∩ Ioo 0 δ).nonempty) : ∃ (t : set (α × ℝ)), t.countable ∧ (∀ (p : α × ℝ), p ∈ t → p.1 ∈ s) ∧ (∀ (p : α × ℝ), p ∈ t → p.2 ∈ f p.1) ∧ μ (s \ (⋃ (p : α × ℝ) (hp : p ∈ t), closed_ball p.1 p.2)) = 0 ∧ t.pairwise_disjoint (λ p, closed_ball p.1 p.2) := begin /- This is deduced from the finite measure case, by using a finite measure with respect to which the initial sigma-finite measure is absolutely continuous. -/ unfreezingI { rcases exists_absolutely_continuous_is_finite_measure μ with ⟨ν, hν, hμν⟩ }, rcases exists_disjoint_closed_ball_covering_ae_of_finite_measure_aux ν f s hf with ⟨t, t_count, ts, tr, tν, tdisj⟩, exact ⟨t, t_count, ts, tr, hμν tν, tdisj⟩, end /-- The measurable Besicovitch covering theorem. Assume that, for any `x` in a set `s`, one is given a set of admissible closed balls centered at `x`, with arbitrarily small radii. Then there exists a disjoint covering of almost all `s` by admissible closed balls centered at some points of `s`. We can even require that the radius at `x` is bounded by a given function `R x`. (Take `R = 1` if you don't need this additional feature). This version requires that the underlying measure is sigma-finite, and that the space has the Besicovitch covering property (which is satisfied for instance by normed real vector spaces). -/ theorem exists_disjoint_closed_ball_covering_ae (μ : measure α) [sigma_finite μ] (f : α → set ℝ) (s : set α) (hf : ∀ x ∈ s, ∀ δ > 0, (f x ∩ Ioo 0 δ).nonempty) (R : α → ℝ) (hR : ∀ x ∈ s, 0 < R x): ∃ (t : set α) (r : α → ℝ), t.countable ∧ t ⊆ s ∧ (∀ x ∈ t, r x ∈ f x ∩ Ioo 0 (R x)) ∧ μ (s \ (⋃ (x ∈ t), closed_ball x (r x))) = 0 ∧ t.pairwise_disjoint (λ x, closed_ball x (r x)) := begin let g := λ x, f x ∩ Ioo 0 (R x), have hg : ∀ x ∈ s, ∀ δ > 0, (g x ∩ Ioo 0 δ).nonempty, { assume x hx δ δpos, rcases hf x hx (min δ (R x)) (lt_min δpos (hR x hx)) with ⟨r, hr⟩, exact ⟨r, ⟨⟨hr.1, hr.2.1, hr.2.2.trans_le (min_le_right _ _)⟩, ⟨hr.2.1, hr.2.2.trans_le (min_le_left _ _)⟩⟩⟩ }, rcases exists_disjoint_closed_ball_covering_ae_aux μ g s hg with ⟨v, v_count, vs, vg, μv, v_disj⟩, let t := prod.fst '' v, have : ∀ x ∈ t, ∃ (r : ℝ), (x, r) ∈ v, { assume x hx, rcases (mem_image _ _ _).1 hx with ⟨⟨p, q⟩, hp, rfl⟩, exact ⟨q, hp⟩ }, choose! r hr using this, have im_t : (λ x, (x, r x)) '' t = v, { have I : ∀ (p : α × ℝ), p ∈ v → 0 ≤ p.2 := λ p hp, (vg p hp).2.1.le, apply subset.antisymm, { simp only [image_subset_iff], rintros ⟨x, p⟩ hxp, simp only [mem_preimage], exact hr _ (mem_image_of_mem _ hxp) }, { rintros ⟨x, p⟩ hxp, have hxrx : (x, r x) ∈ v := hr _ (mem_image_of_mem _ hxp), have : p = r x, { by_contra, have A : (x, p) ≠ (x, r x), by simpa only [true_and, prod.mk.inj_iff, eq_self_iff_true, ne.def] using h, have H := v_disj hxp hxrx A, contrapose H, rw not_disjoint_iff_nonempty_inter, refine ⟨x, by simp [I _ hxp, I _ hxrx]⟩ }, rw this, apply mem_image_of_mem, exact mem_image_of_mem _ hxp } }, refine ⟨t, r, v_count.image _, _, _, _, _⟩, { assume x hx, rcases (mem_image _ _ _).1 hx with ⟨⟨p, q⟩, hp, rfl⟩, exact vs _ hp }, { assume x hx, rcases (mem_image _ _ _).1 hx with ⟨⟨p, q⟩, hp, rfl⟩, exact vg _ (hr _ hx) }, { have : (⋃ (x : α) (H : x ∈ t), closed_ball x (r x)) = (⋃ (p : α × ℝ) (H : p ∈ (λ x, (x, r x)) '' t), closed_ball p.1 p.2), by conv_rhs { rw bUnion_image }, rw [this, im_t], exact μv }, { have A : inj_on (λ x : α, (x, r x)) t, by simp only [inj_on, prod.mk.inj_iff, implies_true_iff, eq_self_iff_true] {contextual := tt}, rwa [← im_t, A.pairwise_disjoint_image] at v_disj } end /-- In a space with the Besicovitch property, any set `s` can be covered with balls whose measures add up to at most `μ s + ε`, for any positive `ε`. This works even if one restricts the set of allowed radii around a point `x` to a set `f x` which accumulates at `0`. -/ theorem exists_closed_ball_covering_tsum_measure_le (μ : measure α) [sigma_finite μ] [measure.outer_regular μ] {ε : ℝ≥0∞} (hε : ε ≠ 0) (f : α → set ℝ) (s : set α) (hf : ∀ x ∈ s, ∀ δ > 0, (f x ∩ Ioo 0 δ).nonempty) : ∃ (t : set α) (r : α → ℝ), t.countable ∧ t ⊆ s ∧ (∀ x ∈ t, r x ∈ f x) ∧ s ⊆ (⋃ (x ∈ t), closed_ball x (r x)) ∧ ∑' (x : t), μ (closed_ball x (r x)) ≤ μ s + ε := begin /- For the proof, first cover almost all `s` with disjoint balls thanks to the usual Besicovitch theorem. Taking the balls included in a well-chosen open neighborhood `u` of `s`, one may ensure that their measures add at most to `μ s + ε / 2`. Let `s'` be the remaining set, of measure `0`. Applying the other version of Besicovitch, one may cover it with at most `N` disjoint subfamilies. Making sure that they are all included in a neighborhood `v` of `s'` of measure at most `ε / (2 N)`, the sum of their measures is at most `ε / 2`, completing the proof. -/ obtain ⟨u, su, u_open, μu⟩ : ∃ U ⊇ s, is_open U ∧ μ U ≤ μ s + ε / 2 := set.exists_is_open_le_add _ _ (by simpa only [or_false, ne.def, ennreal.div_zero_iff, ennreal.one_ne_top, ennreal.bit0_eq_top_iff] using hε), have : ∀ x ∈ s, ∃ R > 0, ball x R ⊆ u := λ x hx, metric.mem_nhds_iff.1 (u_open.mem_nhds (su hx)), choose! R hR using this, obtain ⟨t0, r0, t0_count, t0s, hr0, μt0, t0_disj⟩ : ∃ (t0 : set α) (r0 : α → ℝ), t0.countable ∧ t0 ⊆ s ∧ (∀ x ∈ t0, r0 x ∈ f x ∩ Ioo 0 (R x)) ∧ μ (s \ (⋃ (x ∈ t0), closed_ball x (r0 x))) = 0 ∧ t0.pairwise_disjoint (λ x, closed_ball x (r0 x)) := exists_disjoint_closed_ball_covering_ae μ f s hf R (λ x hx, (hR x hx).1), -- we have constructed an almost everywhere covering of `s` by disjoint balls. Let `s'` be the -- remaining set. let s' := s \ (⋃ (x ∈ t0), closed_ball x (r0 x)), have s's : s' ⊆ s := diff_subset _ _, obtain ⟨N, τ, hτ, H⟩ : ∃ N τ, 1 < τ ∧ is_empty (besicovitch.satellite_config α N τ) := has_besicovitch_covering.no_satellite_config α, obtain ⟨v, s'v, v_open, μv⟩ : ∃ v ⊇ s', is_open v ∧ μ v ≤ μ s' + (ε / 2) / N := set.exists_is_open_le_add _ _ (by simp only [hε, ennreal.nat_ne_top, with_top.mul_eq_top_iff, ne.def, ennreal.div_zero_iff, ennreal.one_ne_top, not_false_iff, and_false, false_and, or_self, ennreal.bit0_eq_top_iff]), have : ∀ x ∈ s', ∃ r1 ∈ (f x ∩ Ioo (0 : ℝ) 1), closed_ball x r1 ⊆ v, { assume x hx, rcases metric.mem_nhds_iff.1 (v_open.mem_nhds (s'v hx)) with ⟨r, rpos, hr⟩, rcases hf x (s's hx) (min r 1) (lt_min rpos zero_lt_one) with ⟨R', hR'⟩, exact ⟨R', ⟨hR'.1, hR'.2.1, hR'.2.2.trans_le (min_le_right _ _)⟩, subset.trans (closed_ball_subset_ball (hR'.2.2.trans_le (min_le_left _ _))) hr⟩, }, choose! r1 hr1 using this, let q : ball_package s' α := { c := λ x, x, r := λ x, r1 x, rpos := λ x, (hr1 x.1 x.2).1.2.1, r_bound := 1, r_le := λ x, (hr1 x.1 x.2).1.2.2.le }, -- by Besicovitch, we cover `s'` with at most `N` families of disjoint balls, all included in -- a suitable neighborhood `v` of `s'`. obtain ⟨S, S_disj, hS⟩ : ∃ S : fin N → set s', (∀ (i : fin N), (S i).pairwise_disjoint (λ j, closed_ball (q.c j) (q.r j))) ∧ (range q.c ⊆ ⋃ (i : fin N), ⋃ (j ∈ S i), ball (q.c j) (q.r j)) := exist_disjoint_covering_families hτ H q, have S_count : ∀ i, (S i).countable, { assume i, apply (S_disj i).countable_of_nonempty_interior (λ j hj, _), have : (ball (j : α) (r1 j)).nonempty := nonempty_ball.2 (q.rpos _), exact this.mono ball_subset_interior_closed_ball }, let r := λ x, if x ∈ s' then r1 x else r0 x, have r_t0 : ∀ x ∈ t0, r x = r0 x, { assume x hx, have : ¬ (x ∈ s'), { simp only [not_exists, exists_prop, mem_Union, mem_closed_ball, not_and, not_lt, not_le, mem_diff, not_forall], assume h'x, refine ⟨x, hx, _⟩, rw dist_self, exact (hr0 x hx).2.1.le }, simp only [r, if_neg this] }, -- the desired covering set is given by the union of the families constructed in the first and -- second steps. refine ⟨t0 ∪ (⋃ (i : fin N), (coe : s' → α) '' (S i)), r, _, _, _, _, _⟩, -- it remains to check that they have the desired properties { exact t0_count.union (countable_Union (λ i, (S_count i).image _)) }, { simp only [t0s, true_and, union_subset_iff, image_subset_iff, Union_subset_iff], assume i x hx, exact s's x.2 }, { assume x hx, cases hx, { rw r_t0 x hx, exact (hr0 _ hx).1 }, { have h'x : x ∈ s', { simp only [mem_Union, mem_image] at hx, rcases hx with ⟨i, y, ySi, rfl⟩, exact y.2 }, simp only [r, if_pos h'x, (hr1 x h'x).1.1] } }, { assume x hx, by_cases h'x : x ∈ s', { obtain ⟨i, y, ySi, xy⟩ : ∃ (i : fin N) (y : ↥s') (ySi : y ∈ S i), x ∈ ball (y : α) (r1 y), { have A : x ∈ range q.c, by simpa only [not_exists, exists_prop, mem_Union, mem_closed_ball, not_and, not_le, mem_set_of_eq, subtype.range_coe_subtype, mem_diff] using h'x, simpa only [mem_Union, mem_image] using hS A }, refine mem_Union₂.2 ⟨y, or.inr _, _⟩, { simp only [mem_Union, mem_image], exact ⟨i, y, ySi, rfl⟩ }, { have : (y : α) ∈ s' := y.2, simp only [r, if_pos this], exact ball_subset_closed_ball xy } }, { obtain ⟨y, yt0, hxy⟩ : ∃ (y : α), y ∈ t0 ∧ x ∈ closed_ball y (r0 y), by simpa [hx, -mem_closed_ball] using h'x, refine mem_Union₂.2 ⟨y, or.inl yt0, _⟩, rwa r_t0 _ yt0 } }, -- the only nontrivial property is the measure control, which we check now { -- the sets in the first step have measure at most `μ s + ε / 2` have A : ∑' (x : t0), μ (closed_ball x (r x)) ≤ μ s + ε / 2 := calc ∑' (x : t0), μ (closed_ball x (r x)) = ∑' (x : t0), μ (closed_ball x (r0 x)) : by { congr' 1, ext x, rw r_t0 x x.2 } ... = μ (⋃ (x : t0), closed_ball x (r0 x)) : begin haveI : encodable t0 := t0_count.to_encodable, rw measure_Union, { exact (pairwise_subtype_iff_pairwise_set _ _).2 t0_disj }, { exact λ i, measurable_set_closed_ball } end ... ≤ μ u : begin apply measure_mono, simp only [set_coe.forall, subtype.coe_mk, Union_subset_iff], assume x hx, apply subset.trans (closed_ball_subset_ball (hr0 x hx).2.2) (hR x (t0s hx)).2, end ... ≤ μ s + ε / 2 : μu, -- each subfamily in the second step has measure at most `ε / (2 N)`. have B : ∀ (i : fin N), ∑' (x : (coe : s' → α) '' (S i)), μ (closed_ball x (r x)) ≤ (ε / 2) / N := λ i, calc ∑' (x : (coe : s' → α) '' (S i)), μ (closed_ball x (r x)) = ∑' (x : S i), μ (closed_ball x (r x)) : begin have : inj_on (coe : s' → α) (S i) := subtype.coe_injective.inj_on _, let F : S i ≃ (coe : s' → α) '' (S i) := this.bij_on_image.equiv _, exact (F.tsum_eq (λ x, μ (closed_ball x (r x)))).symm, end ... = ∑' (x : S i), μ (closed_ball x (r1 x)) : by { congr' 1, ext x, have : (x : α) ∈ s' := x.1.2, simp only [r, if_pos this] } ... = μ (⋃ (x : S i), closed_ball x (r1 x)) : begin haveI : encodable (S i) := (S_count i).to_encodable, rw measure_Union, { exact (pairwise_subtype_iff_pairwise_set _ _).2 (S_disj i) }, { exact λ i, measurable_set_closed_ball } end ... ≤ μ v : begin apply measure_mono, simp only [set_coe.forall, subtype.coe_mk, Union_subset_iff], assume x xs' xSi, exact (hr1 x xs').2, end ... ≤ (ε / 2) / N : by { have : μ s' = 0 := μt0, rwa [this, zero_add] at μv }, -- add up all these to prove the desired estimate calc ∑' (x : (t0 ∪ ⋃ (i : fin N), (coe : s' → α) '' S i)), μ (closed_ball x (r x)) ≤ ∑' (x : t0), μ (closed_ball x (r x)) + ∑' (x : ⋃ (i : fin N), (coe : s' → α) '' S i), μ (closed_ball x (r x)) : ennreal.tsum_union_le (λ x, μ (closed_ball x (r x))) _ _ ... ≤ ∑' (x : t0), μ (closed_ball x (r x)) + ∑ (i : fin N), ∑' (x : (coe : s' → α) '' S i), μ (closed_ball x (r x)) : add_le_add le_rfl (ennreal.tsum_Union_le (λ x, μ (closed_ball x (r x))) _) ... ≤ (μ s + ε / 2) + ∑ (i : fin N), (ε / 2) / N : begin refine add_le_add A _, refine finset.sum_le_sum _, assume i hi, exact B i end ... ≤ (μ s + ε / 2) + ε / 2 : begin refine add_le_add le_rfl _, simp only [finset.card_fin, finset.sum_const, nsmul_eq_mul, ennreal.mul_div_le], end ... = μ s + ε : by rw [add_assoc, ennreal.add_halves] } end /-! ### Consequences on differentiation of measures -/ /-- In a space with the Besicovitch covering property, the set of closed balls with positive radius forms a Vitali family. This is essentially a restatement of the measurable Besicovitch theorem. -/ protected def vitali_family (μ : measure α) [sigma_finite μ] : vitali_family μ := { sets_at := λ x, (λ (r : ℝ), closed_ball x r) '' (Ioi (0 : ℝ)), measurable_set' := begin assume x y hy, obtain ⟨r, rpos, rfl⟩ : ∃ (r : ℝ), 0 < r ∧ closed_ball x r = y, by simpa only [mem_image, mem_Ioi] using hy, exact is_closed_ball.measurable_set end, nonempty_interior := begin assume x y hy, obtain ⟨r, rpos, rfl⟩ : ∃ (r : ℝ), 0 < r ∧ closed_ball x r = y, by simpa only [mem_image, mem_Ioi] using hy, simp only [nonempty.mono ball_subset_interior_closed_ball, rpos, nonempty_ball], end, nontrivial := λ x ε εpos, ⟨closed_ball x ε, mem_image_of_mem _ εpos, subset.refl _⟩, covering := begin assume s f fsubset ffine, let g : α → set ℝ := λ x, {r | 0 < r ∧ closed_ball x r ∈ f x}, have A : ∀ x ∈ s, ∀ δ > 0, (g x ∩ Ioo 0 δ).nonempty, { assume x xs δ δpos, obtain ⟨t, tf, ht⟩ : ∃ (t : set α) (H : t ∈ f x), t ⊆ closed_ball x (δ/2) := ffine x xs (δ/2) (half_pos δpos), obtain ⟨r, rpos, rfl⟩ : ∃ (r : ℝ), 0 < r ∧ closed_ball x r = t, by simpa using fsubset x xs tf, rcases le_total r (δ/2) with H|H, { exact ⟨r, ⟨rpos, tf⟩, ⟨rpos, H.trans_lt (half_lt_self δpos)⟩⟩ }, { have : closed_ball x r = closed_ball x (δ/2) := subset.antisymm ht (closed_ball_subset_closed_ball H), rw this at tf, refine ⟨δ/2, ⟨half_pos δpos, tf⟩, ⟨half_pos δpos, half_lt_self δpos⟩⟩ } }, obtain ⟨t, r, t_count, ts, tg, μt, tdisj⟩ : ∃ (t : set α) (r : α → ℝ), t.countable ∧ t ⊆ s ∧ (∀ x ∈ t, r x ∈ g x ∩ Ioo 0 1) ∧ μ (s \ (⋃ (x ∈ t), closed_ball x (r x))) = 0 ∧ t.pairwise_disjoint (λ x, closed_ball x (r x)) := exists_disjoint_closed_ball_covering_ae μ g s A (λ _, 1) (λ _ _, zero_lt_one), let F : α → α × set α := λ x, (x, closed_ball x (r x)), refine ⟨F '' t, _, _, _, _⟩, { rintros - ⟨x, hx, rfl⟩, exact ts hx }, { rintros p ⟨x, hx, rfl⟩ q ⟨y, hy, rfl⟩ hxy, exact tdisj hx hy (ne_of_apply_ne F hxy) }, { rintros - ⟨x, hx, rfl⟩, exact (tg x hx).1.2 }, { rwa bUnion_image } end } /-- The main feature of the Besicovitch Vitali family is that its filter at a point `x` corresponds to convergence along closed balls. We record one of the two implications here, which will enable us to deduce specific statements on differentiation of measures in this context from the general versions. -/ lemma tendsto_filter_at (μ : measure α) [sigma_finite μ] (x : α) : tendsto (λ r, closed_ball x r) (𝓝[>] 0) ((besicovitch.vitali_family μ).filter_at x) := begin assume s hs, simp only [mem_map], obtain ⟨ε, εpos, hε⟩ : ∃ (ε : ℝ) (H : ε > 0), ∀ (a : set α), a ∈ (besicovitch.vitali_family μ).sets_at x → a ⊆ closed_ball x ε → a ∈ s := (vitali_family.mem_filter_at_iff _).1 hs, have : Ioc (0 : ℝ) ε ∈ 𝓝[>] (0 : ℝ) := Ioc_mem_nhds_within_Ioi ⟨le_rfl, εpos⟩, filter_upwards [this] with _ hr, apply hε, { exact mem_image_of_mem _ hr.1 }, { exact closed_ball_subset_closed_ball hr.2 } end variables [metric_space β] [measurable_space β] [borel_space β] [sigma_compact_space β] [has_besicovitch_covering β] /-- In a space with the Besicovitch covering property, the ratio of the measure of balls converges almost surely to to the Radon-Nikodym derivative. -/ lemma ae_tendsto_rn_deriv (ρ μ : measure β) [is_locally_finite_measure μ] [is_locally_finite_measure ρ] : ∀ᵐ x ∂μ, tendsto (λ r, ρ (closed_ball x r) / μ (closed_ball x r)) (𝓝[>] 0) (𝓝 (ρ.rn_deriv μ x)) := begin haveI : second_countable_topology β := emetric.second_countable_of_sigma_compact β, filter_upwards [vitali_family.ae_tendsto_rn_deriv (besicovitch.vitali_family μ) ρ] with x hx, exact hx.comp (tendsto_filter_at μ x) end /-- Given a measurable set `s`, then `μ (s ∩ closed_ball x r) / μ (closed_ball x r)` converges when `r` tends to `0`, for almost every `x`. The limit is `1` for `x ∈ s` and `0` for `x ∉ s`. This shows that almost every point of `s` is a Lebesgue density point for `s`. A version for non-measurable sets holds, but it only gives the first conclusion, see `ae_tendsto_measure_inter_div`. -/ lemma ae_tendsto_measure_inter_div_of_measurable_set (μ : measure β) [is_locally_finite_measure μ] {s : set β} (hs : measurable_set s) : ∀ᵐ x ∂μ, tendsto (λ r, μ (s ∩ closed_ball x r) / μ (closed_ball x r)) (𝓝[>] 0) (𝓝 (s.indicator 1 x)) := begin haveI : second_countable_topology β := emetric.second_countable_of_sigma_compact β, filter_upwards [vitali_family.ae_tendsto_measure_inter_div_of_measurable_set (besicovitch.vitali_family μ) hs], assume x hx, exact hx.comp (tendsto_filter_at μ x) end /-- Given an arbitrary set `s`, then `μ (s ∩ closed_ball x r) / μ (closed_ball x r)` converges to `1` when `r` tends to `0`, for almost every `x` in `s`. This shows that almost every point of `s` is a Lebesgue density point for `s`. A stronger version holds for measurable sets, see `ae_tendsto_measure_inter_div_of_measurable_set`. See also `is_doubling_measure.ae_tendsto_measure_inter_div`. -/ lemma ae_tendsto_measure_inter_div (μ : measure β) [is_locally_finite_measure μ] (s : set β) : ∀ᵐ x ∂(μ.restrict s), tendsto (λ r, μ (s ∩ (closed_ball x r)) / μ (closed_ball x r)) (𝓝[>] 0) (𝓝 1) := begin haveI : second_countable_topology β := emetric.second_countable_of_sigma_compact β, filter_upwards [vitali_family.ae_tendsto_measure_inter_div (besicovitch.vitali_family μ)] with x hx using hx.comp (tendsto_filter_at μ x), end end besicovitch
90577c022c62e9d513eb19d541b5f2a1f322dd6f
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/ring_theory/graded_algebra/basic.lean
1e958d27d4ea110dd1976a761dcf4c4d4a991ffd
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
12,015
lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser, Kevin Buzzard, Jujian Zhang -/ import algebra.direct_sum.algebra import algebra.direct_sum.decomposition import algebra.direct_sum.internal import algebra.direct_sum.ring /-! # Internally-graded rings and algebras This file defines the typeclass `graded_algebra 𝒜`, for working with an algebra `A` that is internally graded by a collection of submodules `𝒜 : ι → submodule R A`. See the docstring of that typeclass for more information. ## Main definitions * `graded_ring 𝒜`: the typeclass, which is a combination of `set_like.graded_monoid`, and `direct_sum.decomposition 𝒜`. * `graded_algebra 𝒜`: A convenience alias for `graded_ring` when `𝒜` is a family of submodules. * `direct_sum.decompose_ring_equiv 𝒜 : A ≃ₐ[R] ⨁ i, 𝒜 i`, a more bundled version of `direct_sum.decompose 𝒜`. * `direct_sum.decompose_alg_equiv 𝒜 : A ≃ₐ[R] ⨁ i, 𝒜 i`, a more bundled version of `direct_sum.decompose 𝒜`. * `graded_algebra.proj 𝒜 i` is the linear map from `A` to its degree `i : ι` component, such that `proj 𝒜 i x = decompose 𝒜 x i`. ## Implementation notes For now, we do not have internally-graded semirings and internally-graded rings; these can be represented with `𝒜 : ι → submodule ℕ A` and `𝒜 : ι → submodule ℤ A` respectively, since all `semiring`s are ℕ-algebras via `algebra_nat`, and all `ring`s are `ℤ`-algebras via `algebra_int`. ## Tags graded algebra, graded ring, graded semiring, decomposition -/ open_locale direct_sum big_operators variables {ι R A σ : Type*} section graded_ring variables [decidable_eq ι] [add_monoid ι] [comm_semiring R] [semiring A] [algebra R A] variables [set_like σ A] [add_submonoid_class σ A] (𝒜 : ι → σ) include A open direct_sum /-- An internally-graded `R`-algebra `A` is one that can be decomposed into a collection of `submodule R A`s indexed by `ι` such that the canonical map `A → ⨁ i, 𝒜 i` is bijective and respects multiplication, i.e. the product of an element of degree `i` and an element of degree `j` is an element of degree `i + j`. Note that the fact that `A` is internally-graded, `graded_algebra 𝒜`, implies an externally-graded algebra structure `direct_sum.galgebra R (λ i, ↥(𝒜 i))`, which in turn makes available an `algebra R (⨁ i, 𝒜 i)` instance. -/ class graded_ring (𝒜 : ι → σ) extends set_like.graded_monoid 𝒜, direct_sum.decomposition 𝒜. variables [graded_ring 𝒜] namespace direct_sum /-- If `A` is graded by `ι` with degree `i` component `𝒜 i`, then it is isomorphic as a ring to a direct sum of components. -/ def decompose_ring_equiv : A ≃+* ⨁ i, 𝒜 i := ring_equiv.symm { map_mul' := (coe_ring_hom 𝒜).map_mul, map_add' := (coe_ring_hom 𝒜).map_add, ..(decompose_add_equiv 𝒜).symm } @[simp] lemma decompose_one : decompose 𝒜 (1 : A) = 1 := map_one (decompose_ring_equiv 𝒜) @[simp] lemma decompose_symm_one : (decompose 𝒜).symm 1 = (1 : A) := map_one (decompose_ring_equiv 𝒜).symm @[simp] lemma decompose_mul (x y : A) : decompose 𝒜 (x * y) = decompose 𝒜 x * decompose 𝒜 y := map_mul (decompose_ring_equiv 𝒜) x y @[simp] lemma decompose_symm_mul (x y : ⨁ i, 𝒜 i) : (decompose 𝒜).symm (x * y) = (decompose 𝒜).symm x * (decompose 𝒜).symm y := map_mul (decompose_ring_equiv 𝒜).symm x y end direct_sum /-- The projection maps of a graded ring -/ def graded_ring.proj (i : ι) : A →+ A := (add_submonoid_class.subtype (𝒜 i)).comp $ (dfinsupp.eval_add_monoid_hom i).comp $ ring_hom.to_add_monoid_hom $ ring_equiv.to_ring_hom $ direct_sum.decompose_ring_equiv 𝒜 @[simp] lemma graded_ring.proj_apply (i : ι) (r : A) : graded_ring.proj 𝒜 i r = (decompose 𝒜 r : ⨁ i, 𝒜 i) i := rfl lemma graded_ring.proj_recompose (a : ⨁ i, 𝒜 i) (i : ι) : graded_ring.proj 𝒜 i ((decompose 𝒜).symm a) = (decompose 𝒜).symm (direct_sum.of _ i (a i)) := by rw [graded_ring.proj_apply, decompose_symm_of, equiv.apply_symm_apply] lemma graded_ring.mem_support_iff [Π i (x : 𝒜 i), decidable (x ≠ 0)] (r : A) (i : ι) : i ∈ (decompose 𝒜 r).support ↔ graded_ring.proj 𝒜 i r ≠ 0 := dfinsupp.mem_support_iff.trans zero_mem_class.coe_eq_zero.not.symm end graded_ring section add_cancel_monoid open direct_sum variables [decidable_eq ι] [semiring A] [set_like σ A] [add_submonoid_class σ A] (𝒜 : ι → σ) variables {i j : ι} namespace direct_sum lemma coe_decompose_mul_add_of_left_mem [add_left_cancel_monoid ι] [graded_ring 𝒜] {a b : A} (a_mem : a ∈ 𝒜 i) : (decompose 𝒜 (a * b) (i + j) : A) = a * decompose 𝒜 b j := by { lift a to 𝒜 i using a_mem, rw [decompose_mul, decompose_coe, coe_of_mul_apply_add] } lemma coe_decompose_mul_add_of_right_mem [add_right_cancel_monoid ι] [graded_ring 𝒜] {a b : A} (b_mem : b ∈ 𝒜 j) : (decompose 𝒜 (a * b) (i + j) : A) = decompose 𝒜 a i * b := by { lift b to 𝒜 j using b_mem, rw [decompose_mul, decompose_coe, coe_mul_of_apply_add] } lemma decompose_mul_add_left [add_left_cancel_monoid ι] [graded_ring 𝒜] (a : 𝒜 i) {b : A} : decompose 𝒜 (↑a * b) (i + j) = @graded_monoid.ghas_mul.mul ι (λ i, 𝒜 i) _ _ _ _ a (decompose 𝒜 b j) := subtype.ext $ coe_decompose_mul_add_of_left_mem 𝒜 a.2 lemma decompose_mul_add_right [add_right_cancel_monoid ι] [graded_ring 𝒜] {a : A} (b : 𝒜 j) : decompose 𝒜 (a * ↑b) (i + j) = @graded_monoid.ghas_mul.mul ι (λ i, 𝒜 i) _ _ _ _ (decompose 𝒜 a i) b := subtype.ext $ coe_decompose_mul_add_of_right_mem 𝒜 b.2 end direct_sum end add_cancel_monoid section graded_algebra variables [decidable_eq ι] [add_monoid ι] [comm_semiring R] [semiring A] [algebra R A] variables (𝒜 : ι → submodule R A) /-- A special case of `graded_ring` with `σ = submodule R A`. This is useful both because it can avoid typeclass search, and because it provides a more concise name. -/ @[reducible] def graded_algebra := graded_ring 𝒜 /-- A helper to construct a `graded_algebra` when the `set_like.graded_monoid` structure is already available. This makes the `left_inv` condition easier to prove, and phrases the `right_inv` condition in a way that allows custom `@[ext]` lemmas to apply. See note [reducible non-instances]. -/ @[reducible] def graded_algebra.of_alg_hom [set_like.graded_monoid 𝒜] (decompose : A →ₐ[R] ⨁ i, 𝒜 i) (right_inv : (direct_sum.coe_alg_hom 𝒜).comp decompose = alg_hom.id R A) (left_inv : ∀ i (x : 𝒜 i), decompose (x : A) = direct_sum.of (λ i, ↥(𝒜 i)) i x) : graded_algebra 𝒜 := { decompose' := decompose, left_inv := alg_hom.congr_fun right_inv, right_inv := begin suffices : decompose.comp (direct_sum.coe_alg_hom 𝒜) = alg_hom.id _ _, from alg_hom.congr_fun this, ext i x : 2, exact (decompose.congr_arg $ direct_sum.coe_alg_hom_of _ _ _).trans (left_inv i x), end} variable [graded_algebra 𝒜] namespace direct_sum /-- If `A` is graded by `ι` with degree `i` component `𝒜 i`, then it is isomorphic as an algebra to a direct sum of components. -/ @[simps] def decompose_alg_equiv : A ≃ₐ[R] ⨁ i, 𝒜 i := alg_equiv.symm { map_mul' := (coe_alg_hom 𝒜).map_mul, map_add' := (coe_alg_hom 𝒜).map_add, commutes' := (coe_alg_hom 𝒜).commutes, ..(decompose_add_equiv 𝒜).symm } end direct_sum open direct_sum /-- The projection maps of graded algebra-/ def graded_algebra.proj (𝒜 : ι → submodule R A) [graded_algebra 𝒜] (i : ι) : A →ₗ[R] A := (𝒜 i).subtype.comp $ (dfinsupp.lapply i).comp $ (decompose_alg_equiv 𝒜).to_alg_hom.to_linear_map @[simp] lemma graded_algebra.proj_apply (i : ι) (r : A) : graded_algebra.proj 𝒜 i r = (decompose 𝒜 r : ⨁ i, 𝒜 i) i := rfl lemma graded_algebra.proj_recompose (a : ⨁ i, 𝒜 i) (i : ι) : graded_algebra.proj 𝒜 i ((decompose 𝒜).symm a) = (decompose 𝒜).symm (of _ i (a i)) := by rw [graded_algebra.proj_apply, decompose_symm_of, equiv.apply_symm_apply] lemma graded_algebra.mem_support_iff [decidable_eq A] (r : A) (i : ι) : i ∈ (decompose 𝒜 r).support ↔ graded_algebra.proj 𝒜 i r ≠ 0 := dfinsupp.mem_support_iff.trans submodule.coe_eq_zero.not.symm end graded_algebra section canonical_order open set_like.graded_monoid direct_sum variables [semiring A] [decidable_eq ι] variables [canonically_ordered_add_monoid ι] variables [set_like σ A] [add_submonoid_class σ A] (𝒜 : ι → σ) [graded_ring 𝒜] /-- If `A` is graded by a canonically ordered add monoid, then the projection map `x ↦ x₀` is a ring homomorphism. -/ @[simps] def graded_ring.proj_zero_ring_hom : A →+* A := { to_fun := λ a, decompose 𝒜 a 0, map_one' := decompose_of_mem_same 𝒜 one_mem, map_zero' := by { rw decompose_zero, refl }, map_add' := λ _ _, by { rw decompose_add, refl }, map_mul' := begin refine direct_sum.decomposition.induction_on 𝒜 (λ x, _) _ _, { simp only [zero_mul, decompose_zero, zero_apply, zero_mem_class.coe_zero] }, { rintros i ⟨c, hc⟩, refine direct_sum.decomposition.induction_on 𝒜 _ _ _, { simp only [mul_zero, decompose_zero, zero_apply, zero_mem_class.coe_zero] }, { rintros j ⟨c', hc'⟩, { simp only [subtype.coe_mk], by_cases h : i + j = 0, { rw [decompose_of_mem_same 𝒜 (show c * c' ∈ 𝒜 0, from h ▸ mul_mem hc hc'), decompose_of_mem_same 𝒜 (show c ∈ 𝒜 0, from (add_eq_zero_iff.mp h).1 ▸ hc), decompose_of_mem_same 𝒜 (show c' ∈ 𝒜 0, from (add_eq_zero_iff.mp h).2 ▸ hc')] }, { rw [decompose_of_mem_ne 𝒜 (mul_mem hc hc') h], cases (show i ≠ 0 ∨ j ≠ 0, by rwa [add_eq_zero_iff, not_and_distrib] at h) with h' h', { simp only [decompose_of_mem_ne 𝒜 hc h', zero_mul] }, { simp only [decompose_of_mem_ne 𝒜 hc' h', mul_zero] } } }, }, { intros _ _ hd he, simp only [mul_add, decompose_add, add_apply, add_mem_class.coe_add, hd, he] }, }, { rintros _ _ ha hb _, simp only [add_mul, decompose_add, add_apply, add_mem_class.coe_add, ha, hb], }, end } variables {a b : A} {n i : ι} namespace direct_sum lemma coe_decompose_mul_of_left_mem_of_not_le (a_mem : a ∈ 𝒜 i) (h : ¬ i ≤ n) : (decompose 𝒜 (a * b) n : A) = 0 := by { lift a to 𝒜 i using a_mem, rwa [decompose_mul, decompose_coe, coe_of_mul_apply_of_not_le] } lemma coe_decompose_mul_of_right_mem_of_not_le (b_mem : b ∈ 𝒜 i) (h : ¬ i ≤ n) : (decompose 𝒜 (a * b) n : A) = 0 := by { lift b to 𝒜 i using b_mem, rwa [decompose_mul, decompose_coe, coe_mul_of_apply_of_not_le] } variables [has_sub ι] [has_ordered_sub ι] [contravariant_class ι ι (+) (≤)] lemma coe_decompose_mul_of_left_mem_of_le (a_mem : a ∈ 𝒜 i) (h : i ≤ n) : (decompose 𝒜 (a * b) n : A) = a * decompose 𝒜 b (n - i) := by { lift a to 𝒜 i using a_mem, rwa [decompose_mul, decompose_coe, coe_of_mul_apply_of_le] } lemma coe_decompose_mul_of_right_mem_of_le (b_mem : b ∈ 𝒜 i) (h : i ≤ n) : (decompose 𝒜 (a * b) n : A) = decompose 𝒜 a (n - i) * b := by { lift b to 𝒜 i using b_mem, rwa [decompose_mul, decompose_coe, coe_mul_of_apply_of_le] } lemma coe_decompose_mul_of_left_mem (n) [decidable (i ≤ n)] (a_mem : a ∈ 𝒜 i) : (decompose 𝒜 (a * b) n : A) = if i ≤ n then a * decompose 𝒜 b (n - i) else 0 := by { lift a to 𝒜 i using a_mem, rwa [decompose_mul, decompose_coe, coe_of_mul_apply] } lemma coe_decompose_mul_of_right_mem (n) [decidable (i ≤ n)] (b_mem : b ∈ 𝒜 i) : (decompose 𝒜 (a * b) n : A) = if i ≤ n then decompose 𝒜 a (n - i) * b else 0 := by { lift b to 𝒜 i using b_mem, rwa [decompose_mul, decompose_coe, coe_mul_of_apply] } end direct_sum end canonical_order
f66cd810b1331a849253889a59918b1114b4352b
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/order/filter/cofinite.lean
64b35cf18d31275808e18f1fb59f2b0e66856a3c
[ "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
6,063
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Jeremy Avigad, Yury Kudryashov -/ import order.filter.at_top_bot /-! # The cofinite filter In this file we define `cofinite`: the filter of sets with finite complement and prove its basic properties. In particular, we prove that for `ℕ` it is equal to `at_top`. ## TODO Define filters for other cardinalities of the complement. -/ open set open_locale classical variables {α : Type*} namespace filter /-- The cofinite filter is the filter of subsets whose complements are finite. -/ def cofinite : filter α := { sets := {s | finite sᶜ}, univ_sets := by simp only [compl_univ, finite_empty, mem_set_of_eq], sets_of_superset := assume s t (hs : finite sᶜ) (st: s ⊆ t), hs.subset $ compl_subset_compl.2 st, inter_sets := assume s t (hs : finite sᶜ) (ht : finite (tᶜ)), by simp only [compl_inter, finite.union, ht, hs, mem_set_of_eq] } @[simp] lemma mem_cofinite {s : set α} : s ∈ (@cofinite α) ↔ finite sᶜ := iff.rfl @[simp] lemma eventually_cofinite {p : α → Prop} : (∀ᶠ x in cofinite, p x) ↔ finite {x | ¬p x} := iff.rfl instance cofinite_ne_bot [infinite α] : ne_bot (@cofinite α) := ⟨mt empty_mem_iff_bot.mpr $ by { simp only [mem_cofinite, compl_empty], exact infinite_univ }⟩ lemma frequently_cofinite_iff_infinite {p : α → Prop} : (∃ᶠ x in cofinite, p x) ↔ set.infinite {x | p x} := by simp only [filter.frequently, filter.eventually, mem_cofinite, compl_set_of, not_not, set.infinite] /-- The coproduct of the cofinite filters on two types is the cofinite filter on their product. -/ lemma coprod_cofinite {β : Type*} : (cofinite : filter α).coprod (cofinite : filter β) = cofinite := begin ext S, simp only [mem_coprod_iff, exists_prop, mem_comap, mem_cofinite], split, { rintro ⟨⟨A, hAf, hAS⟩, B, hBf, hBS⟩, rw [← compl_subset_compl, ← preimage_compl] at hAS hBS, exact (hAf.prod hBf).subset (subset_inter hAS hBS) }, { intro hS, refine ⟨⟨(prod.fst '' Sᶜ)ᶜ, _, _⟩, ⟨(prod.snd '' Sᶜ)ᶜ, _, _⟩⟩, { simpa using hS.image prod.fst }, { simpa [compl_subset_comm] using subset_preimage_image prod.fst Sᶜ }, { simpa using hS.image prod.snd }, { simpa [compl_subset_comm] using subset_preimage_image prod.snd Sᶜ } }, end /-- Finite product of finite sets is finite -/ lemma Coprod_cofinite {δ : Type*} {κ : δ → Type*} [fintype δ] : filter.Coprod (λ d, (cofinite : filter (κ d))) = cofinite := begin ext S, simp only [mem_coprod_iff, exists_prop, mem_comap, mem_cofinite], split, { rintros h, rw mem_Coprod_iff at h, choose t ht1 ht2 using h, have ht1d : ∀ (d : δ), (t d)ᶜ.finite := λ d, mem_cofinite.mp (ht1 d), refine (set.finite.pi ht1d).subset _, have ht2d : ∀ (d : δ), Sᶜ ⊆ ((λ (k : Π (d1 : δ), (λ (d2 : δ), κ d2) d1), k d) ⁻¹' ((t d)ᶜ)) := λ d, compl_subset_compl.mpr (ht2 d), convert set.subset_Inter ht2d, ext, simp }, { intro hS, rw mem_Coprod_iff, intros d, refine ⟨((λ (k : Π (d1 : δ), κ d1), k d) '' (Sᶜ))ᶜ, _, _⟩, { rw [mem_cofinite, compl_compl], exact set.finite.image _ hS }, { intros x, contrapose, intros hx, simp only [not_not, mem_preimage, mem_compl_eq, not_forall], exact ⟨x, hx, rfl⟩ } }, end end filter open filter lemma set.finite.compl_mem_cofinite {s : set α} (hs : s.finite) : sᶜ ∈ (@cofinite α) := mem_cofinite.2 $ (compl_compl s).symm ▸ hs lemma set.finite.eventually_cofinite_nmem {s : set α} (hs : s.finite) : ∀ᶠ x in cofinite, x ∉ s := hs.compl_mem_cofinite lemma finset.eventually_cofinite_nmem (s : finset α) : ∀ᶠ x in cofinite, x ∉ s := s.finite_to_set.eventually_cofinite_nmem lemma set.infinite_iff_frequently_cofinite {s : set α} : set.infinite s ↔ (∃ᶠ x in cofinite, x ∈ s) := frequently_cofinite_iff_infinite.symm lemma filter.eventually_cofinite_ne (x : α) : ∀ᶠ a in cofinite, a ≠ x := (set.finite_singleton x).eventually_cofinite_nmem /-- For natural numbers the filters `cofinite` and `at_top` coincide. -/ lemma nat.cofinite_eq_at_top : @cofinite ℕ = at_top := begin ext s, simp only [mem_cofinite, mem_at_top_sets], split, { assume hs, use (hs.to_finset.sup id) + 1, assume b hb, by_contradiction hbs, have := hs.to_finset.subset_range_sup_succ (hs.mem_to_finset.2 hbs), exact not_lt_of_le hb (finset.mem_range.1 this) }, { rintros ⟨N, hN⟩, apply (finite_lt_nat N).subset, assume n hn, change n < N, exact lt_of_not_ge (λ hn', hn $ hN n hn') } end lemma nat.frequently_at_top_iff_infinite {p : ℕ → Prop} : (∃ᶠ n in at_top, p n) ↔ set.infinite {n | p n} := by simp only [← nat.cofinite_eq_at_top, frequently_cofinite_iff_infinite] lemma filter.tendsto.exists_forall_le {α β : Type*} [nonempty α] [linear_order β] {f : α → β} (hf : tendsto f cofinite at_top) : ∃ a₀, ∀ a, f a₀ ≤ f a := begin rcases em (∃ y, ∃ x, f y < x) with ⟨y, x, hx⟩|not_all_top, { -- the set of points `{y | f y < x}` is nonempty and finite, so we take `min` over this set have : finite {y | ¬x ≤ f y} := (filter.eventually_cofinite.mp (tendsto_at_top.1 hf x)), simp only [not_le] at this, obtain ⟨a₀, ha₀ : f a₀ < x, others_bigger⟩ := exists_min_image _ f this ⟨y, hx⟩, exact ⟨a₀, λ a, (lt_or_le (f a) x).elim (others_bigger _) (le_trans ha₀.le)⟩ }, { -- in this case, f is constant because all values are at top push_neg at not_all_top, inhabit α, exact ⟨default α, λ a, not_all_top a (f $ default α)⟩ } end lemma filter.tendsto.exists_forall_ge {α β : Type*} [nonempty α] [linear_order β] {f : α → β} (hf : tendsto f cofinite at_bot) : ∃ a₀, ∀ a, f a ≤ f a₀ := @filter.tendsto.exists_forall_le _ (order_dual β) _ _ _ hf
b8cadb77edf7e62ac36728b08118593170d7ecd2
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/diamond1.lean
a5ad162faefa87ed72fc299d14f408cf41136ada
[ "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
637
lean
structure Bar (α : Type) where a : α b : Nat → α structure Baz (α : Type) where a : α → α c : Bool → α d : Nat set_option structureDiamondWarning false in structure Foo (α : Type) extends Bar α, Baz α -- Error: parent field type mismatch set_option structureDiamondWarning false in structure Foo (α : Type) extends Bar (α → α), Baz α #print Foo def f (x : Nat) : Foo Nat := { a := fun y => x + y b := (· + ·) c := fun _ => x d := x } #print f set_option structureDiamondWarning true in structure Foo' (α : Type) extends Bar (α → α), Baz α -- Warning: parent field duplication
13a173391d56258babf9a9d9fab06a0291fe4d24
12dabd587ce2621d9a4eff9f16e354d02e206c8e
/world07/level08.lean
1f3084c037d2f847df5c88e8a2148b5da1e38d20
[]
no_license
abdelq/natural-number-game
a1b5b8f1d52625a7addcefc97c966d3f06a48263
bbddadc6d2e78ece2e9acd40fa7702ecc2db75c2
refs/heads/master
1,668,606,478,691
1,594,175,058,000
1,594,175,058,000
278,673,209
0
1
null
null
null
null
UTF-8
Lean
false
false
245
lean
lemma and_or_distrib_left (P Q R : Prop) : P ∧ (Q ∨ R) ↔ (P ∧ Q) ∨ (P ∧ R) := begin split, cc, intro h, cases h with hpq hpr, cases hpq with p q, split, exact p, left, exact q, cases hpr with p r, split, exact p, right, exact r, end
51c67e6383975223619808946200eb430ca3db8a
36938939954e91f23dec66a02728db08a7acfcf9
/lean/deps/galois_stdlib/src/galois/category/coe1.lean
746f17949a941c546d391be73dd1b716bfa1b839
[ "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
609
lean
universe variables u v w -- This defines a class for "parameterized" coercisions in which we -- want to allow coercising `f tp` to some `g tp` while preserving the -- type `tp`. This allows coercing even when `tp` contains meta variables, -- and using unification across the coercision. class has_coe1 {α:Sort w} (f : α → Sort u) (g : α → Sort v) := (coe : Π{a : α}, f a → g a) namespace has_coe1 instance refl (α:Sort w) (f : α → Sort u) : has_coe1 f f := ⟨λa f, f⟩ end has_coe1 def coe1 {α:Type u} {f g : α → Type u} [h:has_coe1 f g] {a:α} (x : f a) : g a := has_coe1.coe g x
c2009f27b5346a1f16f6b860236208fd3bb7ed16
38bf3fd2bb651ab70511408fcf70e2029e2ba310
/src/category_theory/limits/limits.lean
945bba2e6a721e8f47038a1360594fc70bb06fa5
[ "Apache-2.0" ]
permissive
JaredCorduan/mathlib
130392594844f15dad65a9308c242551bae6cd2e
d5de80376088954d592a59326c14404f538050a1
refs/heads/master
1,595,862,206,333
1,570,816,457,000
1,570,816,457,000
209,134,499
0
0
Apache-2.0
1,568,746,811,000
1,568,746,811,000
null
UTF-8
Lean
false
false
36,704
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Mario Carneiro, Scott Morrison, Floris van Doorn -/ import category_theory.whiskering import category_theory.yoneda import category_theory.limits.cones import category_theory.eq_to_hom open category_theory category_theory.category category_theory.functor opposite namespace category_theory.limits universes v u u' u'' w -- declare the `v`'s first; see `category_theory.category` for an explanation -- See the notes at the top of cones.lean, explaining why we can't allow `J : Prop` here. variables {J K : Type v} [small_category J] [small_category K] variables {C : Type u} [𝒞 : category.{v} C] include 𝒞 variables {F : J ⥤ C} /-- A cone `t` on `F` is a limit cone if each cone on `F` admits a unique cone morphism to `t`. -/ structure is_limit (t : cone F) := (lift : Π (s : cone F), s.X ⟶ t.X) (fac' : ∀ (s : cone F) (j : J), lift s ≫ t.π.app j = s.π.app j . obviously) (uniq' : ∀ (s : cone F) (m : s.X ⟶ t.X) (w : ∀ j : J, m ≫ t.π.app j = s.π.app j), m = lift s . obviously) restate_axiom is_limit.fac' attribute [simp] is_limit.fac restate_axiom is_limit.uniq' namespace is_limit instance subsingleton {t : cone F} : subsingleton (is_limit t) := ⟨by intros P Q; cases P; cases Q; congr; ext; solve_by_elim⟩ /- Repackaging the definition in terms of cone morphisms. -/ def lift_cone_morphism {t : cone F} (h : is_limit t) (s : cone F) : s ⟶ t := { hom := h.lift s } lemma uniq_cone_morphism {s t : cone F} (h : is_limit t) {f f' : s ⟶ t} : f = f' := have ∀ {g : s ⟶ t}, g = h.lift_cone_morphism s, by intro g; ext; exact h.uniq _ _ g.w, this.trans this.symm def mk_cone_morphism {t : cone F} (lift : Π (s : cone F), s ⟶ t) (uniq' : ∀ (s : cone F) (m : s ⟶ t), m = lift s) : is_limit t := { lift := λ s, (lift s).hom, uniq' := λ s m w, have cone_morphism.mk m w = lift s, by apply uniq', congr_arg cone_morphism.hom this } /-- Limit cones on `F` are unique up to isomorphism. -/ def unique_up_to_iso {s t : cone F} (P : is_limit s) (Q : is_limit t) : s ≅ t := { hom := Q.lift_cone_morphism s, inv := P.lift_cone_morphism t, hom_inv_id' := P.uniq_cone_morphism, inv_hom_id' := Q.uniq_cone_morphism } def of_iso_limit {r t : cone F} (P : is_limit r) (i : r ≅ t) : is_limit t := is_limit.mk_cone_morphism (λ s, P.lift_cone_morphism s ≫ i.hom) (λ s m, by rw ←i.comp_inv_eq; apply P.uniq_cone_morphism) variables {t : cone F} lemma hom_lift (h : is_limit t) {W : C} (m : W ⟶ t.X) : m = h.lift { X := W, π := { app := λ b, m ≫ t.π.app b } } := h.uniq { X := W, π := { app := λ b, m ≫ t.π.app b } } m (λ b, rfl) /-- Two morphisms into a limit are equal if their compositions with each cone morphism are equal. -/ lemma hom_ext (h : is_limit t) {W : C} {f f' : W ⟶ t.X} (w : ∀ j, f ≫ t.π.app j = f' ≫ t.π.app j) : f = f' := by rw [h.hom_lift f, h.hom_lift f']; congr; exact funext w /-- The universal property of a limit cone: a map `W ⟶ X` is the same as a cone on `F` with vertex `W`. -/ def hom_iso (h : is_limit t) (W : C) : (W ⟶ t.X) ≅ ((const J).obj W ⟶ F) := { hom := λ f, (t.extend f).π, inv := λ π, h.lift { X := W, π := π }, hom_inv_id' := by ext f; apply h.hom_ext; intro j; simp; dsimp; refl } @[simp] lemma hom_iso_hom (h : is_limit t) {W : C} (f : W ⟶ t.X) : (is_limit.hom_iso h W).hom f = (t.extend f).π := rfl /-- The limit of `F` represents the functor taking `W` to the set of cones on `F` with vertex `W`. -/ def nat_iso (h : is_limit t) : yoneda.obj t.X ≅ F.cones := nat_iso.of_components (λ W, is_limit.hom_iso h (unop W)) (by tidy). def hom_iso' (h : is_limit t) (W : C) : ((W ⟶ t.X) : Type v) ≅ { p : Π j, W ⟶ F.obj j // ∀ {j j'} (f : j ⟶ j'), p j ≫ F.map f = p j' } := h.hom_iso W ≪≫ { hom := λ π, ⟨λ j, π.app j, λ j j' f, by convert ←(π.naturality f).symm; apply id_comp⟩, inv := λ p, { app := λ j, p.1 j, naturality' := λ j j' f, begin dsimp, rw [id_comp], exact (p.2 f).symm end } } /-- If G : C → D is a faithful functor which sends t to a limit cone, then it suffices to check that the induced maps for the image of t can be lifted to maps of C. -/ def of_faithful {t : cone F} {D : Type u'} [category.{v} D] (G : C ⥤ D) [faithful G] (ht : is_limit (G.map_cone t)) (lift : Π (s : cone F), s.X ⟶ t.X) (h : ∀ s, G.map (lift s) = ht.lift (G.map_cone s)) : is_limit t := { lift := lift, fac' := λ s j, by apply G.injectivity; rw [G.map_comp, h]; apply ht.fac, uniq' := λ s m w, begin apply G.injectivity, rw h, refine ht.uniq (G.map_cone s) _ (λ j, _), convert ←congr_arg (λ f, G.map f) (w j), apply G.map_comp end } def iso_unique_cone_morphism {t : cone F} : is_limit t ≅ Π s, unique (s ⟶ t) := { hom := λ h s, { default := h.lift_cone_morphism s, uniq := λ _, h.uniq_cone_morphism }, inv := λ h, { lift := λ s, (h s).default.hom, uniq' := λ s f w, congr_arg cone_morphism.hom ((h s).uniq ⟨f, w⟩) } } namespace of_nat_iso variables {X : C} (h : yoneda.obj X ≅ F.cones) /-- If `F.cones` is represented by `X`, each morphism `f : Y ⟶ X` gives a cone with cone point `Y`. -/ def cone_of_hom {Y : C} (f : Y ⟶ X) : cone F := { X := Y, π := h.hom.app (op Y) f } /-- If `F.cones` is represented by `X`, each cone `s` gives a morphism `s.X ⟶ X`. -/ def hom_of_cone (s : cone F) : s.X ⟶ X := h.inv.app (op s.X) s.π @[simp] lemma cone_of_hom_of_cone (s : cone F) : cone_of_hom h (hom_of_cone h s) = s := begin dsimp [cone_of_hom, hom_of_cone], cases s, congr, dsimp, exact congr_fun (congr_fun (congr_arg nat_trans.app h.inv_hom_id) (op s_X)) s_π, end @[simp] lemma hom_of_cone_of_hom {Y : C} (f : Y ⟶ X) : hom_of_cone h (cone_of_hom h f) = f := congr_fun (congr_fun (congr_arg nat_trans.app h.hom_inv_id) (op Y)) f /-- If `F.cones` is represented by `X`, the cone corresponding to the identity morphism on `X` will be a limit cone. -/ def limit_cone : cone F := cone_of_hom h (𝟙 X) /-- If `F.cones` is represented by `X`, the cone corresponding to a morphism `f : Y ⟶ X` is the limit cone extended by `f`. -/ lemma cone_of_hom_fac {Y : C} (f : Y ⟶ X) : cone_of_hom h f = (limit_cone h).extend f := begin dsimp [cone_of_hom, limit_cone, cone.extend], congr, ext j, have t := congr_fun (h.hom.naturality f.op) (𝟙 X), dsimp at t, simp only [comp_id] at t, rw congr_fun (congr_arg nat_trans.app t) j, refl, end /-- If `F.cones` is represented by `X`, any cone is the extension of the limit cone by the corresponding morphism. -/ lemma cone_fac (s : cone F) : (limit_cone h).extend (hom_of_cone h s) = s := begin rw ←cone_of_hom_of_cone h s, conv_lhs { simp only [hom_of_cone_of_hom] }, apply (cone_of_hom_fac _ _).symm, end end of_nat_iso section open of_nat_iso /-- If `F.cones` is representable, then the cone corresponding to the identity morphism on the representing object is a limit cone. -/ def of_nat_iso {X : C} (h : yoneda.obj X ≅ F.cones) : is_limit (limit_cone h) := { lift := λ s, hom_of_cone h s, fac' := λ s j, begin have h := cone_fac h s, cases s, injection h with h₁ h₂, simp only [heq_iff_eq] at h₂, conv_rhs { rw ← h₂ }, refl, end, uniq' := λ s m w, begin rw ←hom_of_cone_of_hom h m, congr, rw cone_of_hom_fac, dsimp, cases s, congr, ext j, exact w j, end } end end is_limit /-- A cocone `t` on `F` is a colimit cocone if each cocone on `F` admits a unique cocone morphism from `t`. -/ structure is_colimit (t : cocone F) := (desc : Π (s : cocone F), t.X ⟶ s.X) (fac' : ∀ (s : cocone F) (j : J), t.ι.app j ≫ desc s = s.ι.app j . obviously) (uniq' : ∀ (s : cocone F) (m : t.X ⟶ s.X) (w : ∀ j : J, t.ι.app j ≫ m = s.ι.app j), m = desc s . obviously) restate_axiom is_colimit.fac' attribute [simp] is_colimit.fac restate_axiom is_colimit.uniq' namespace is_colimit instance subsingleton {t : cocone F} : subsingleton (is_colimit t) := ⟨by intros P Q; cases P; cases Q; congr; ext; solve_by_elim⟩ /- Repackaging the definition in terms of cone morphisms. -/ def desc_cocone_morphism {t : cocone F} (h : is_colimit t) (s : cocone F) : t ⟶ s := { hom := h.desc s } lemma uniq_cocone_morphism {s t : cocone F} (h : is_colimit t) {f f' : t ⟶ s} : f = f' := have ∀ {g : t ⟶ s}, g = h.desc_cocone_morphism s, by intro g; ext; exact h.uniq _ _ g.w, this.trans this.symm def mk_cocone_morphism {t : cocone F} (desc : Π (s : cocone F), t ⟶ s) (uniq' : ∀ (s : cocone F) (m : t ⟶ s), m = desc s) : is_colimit t := { desc := λ s, (desc s).hom, uniq' := λ s m w, have cocone_morphism.mk m w = desc s, by apply uniq', congr_arg cocone_morphism.hom this } /-- Limit cones on `F` are unique up to isomorphism. -/ def unique_up_to_iso {s t : cocone F} (P : is_colimit s) (Q : is_colimit t) : s ≅ t := { hom := P.desc_cocone_morphism t, inv := Q.desc_cocone_morphism s, hom_inv_id' := P.uniq_cocone_morphism, inv_hom_id' := Q.uniq_cocone_morphism } def of_iso_colimit {r t : cocone F} (P : is_colimit r) (i : r ≅ t) : is_colimit t := is_colimit.mk_cocone_morphism (λ s, i.inv ≫ P.desc_cocone_morphism s) (λ s m, by rw i.eq_inv_comp; apply P.uniq_cocone_morphism) variables {t : cocone F} lemma hom_desc (h : is_colimit t) {W : C} (m : t.X ⟶ W) : m = h.desc { X := W, ι := { app := λ b, t.ι.app b ≫ m, naturality' := by intros; erw [←assoc, t.ι.naturality, comp_id, comp_id] } } := h.uniq { X := W, ι := { app := λ b, t.ι.app b ≫ m, naturality' := _ } } m (λ b, rfl) /-- Two morphisms out of a colimit are equal if their compositions with each cocone morphism are equal. -/ lemma hom_ext (h : is_colimit t) {W : C} {f f' : t.X ⟶ W} (w : ∀ j, t.ι.app j ≫ f = t.ι.app j ≫ f') : f = f' := by rw [h.hom_desc f, h.hom_desc f']; congr; exact funext w /-- The universal property of a colimit cocone: a map `X ⟶ W` is the same as a cocone on `F` with vertex `W`. -/ def hom_iso (h : is_colimit t) (W : C) : (t.X ⟶ W) ≅ (F ⟶ (const J).obj W) := { hom := λ f, (t.extend f).ι, inv := λ ι, h.desc { X := W, ι := ι }, hom_inv_id' := by ext f; apply h.hom_ext; intro j; simp; dsimp; refl } @[simp] lemma hom_iso_hom (h : is_colimit t) {W : C} (f : t.X ⟶ W) : (is_colimit.hom_iso h W).hom f = (t.extend f).ι := rfl /-- The colimit of `F` represents the functor taking `W` to the set of cocones on `F` with vertex `W`. -/ def nat_iso (h : is_colimit t) : coyoneda.obj (op t.X) ≅ F.cocones := nat_iso.of_components (is_colimit.hom_iso h) (by intros; ext; dsimp; rw ←assoc; refl) def hom_iso' (h : is_colimit t) (W : C) : ((t.X ⟶ W) : Type v) ≅ { p : Π j, F.obj j ⟶ W // ∀ {j j' : J} (f : j ⟶ j'), F.map f ≫ p j' = p j } := h.hom_iso W ≪≫ { hom := λ ι, ⟨λ j, ι.app j, λ j j' f, by convert ←(ι.naturality f); apply comp_id⟩, inv := λ p, { app := λ j, p.1 j, naturality' := λ j j' f, begin dsimp, rw [comp_id], exact (p.2 f) end } } /-- If G : C → D is a faithful functor which sends t to a colimit cocone, then it suffices to check that the induced maps for the image of t can be lifted to maps of C. -/ def of_faithful {t : cocone F} {D : Type u'} [category.{v} D] (G : C ⥤ D) [faithful G] (ht : is_colimit (G.map_cocone t)) (desc : Π (s : cocone F), t.X ⟶ s.X) (h : ∀ s, G.map (desc s) = ht.desc (G.map_cocone s)) : is_colimit t := { desc := desc, fac' := λ s j, by apply G.injectivity; rw [G.map_comp, h]; apply ht.fac, uniq' := λ s m w, begin apply G.injectivity, rw h, refine ht.uniq (G.map_cocone s) _ (λ j, _), convert ←congr_arg (λ f, G.map f) (w j), apply G.map_comp end } def iso_unique_cocone_morphism {t : cocone F} : is_colimit t ≅ Π s, unique (t ⟶ s) := { hom := λ h s, { default := h.desc_cocone_morphism s, uniq := λ _, h.uniq_cocone_morphism }, inv := λ h, { desc := λ s, (h s).default.hom, uniq' := λ s f w, congr_arg cocone_morphism.hom ((h s).uniq ⟨f, w⟩) } } namespace of_nat_iso variables {X : C} (h : coyoneda.obj (op X) ≅ F.cocones) /-- If `F.cocones` is corepresented by `X`, each morphism `f : X ⟶ Y` gives a cocone with cone point `Y`. -/ def cocone_of_hom {Y : C} (f : X ⟶ Y) : cocone F := { X := Y, ι := h.hom.app Y f } /-- If `F.cocones` is corepresented by `X`, each cocone `s` gives a morphism `X ⟶ s.X`. -/ def hom_of_cocone (s : cocone F) : X ⟶ s.X := h.inv.app s.X s.ι @[simp] lemma cocone_of_hom_of_cocone (s : cocone F) : cocone_of_hom h (hom_of_cocone h s) = s := begin dsimp [cocone_of_hom, hom_of_cocone], cases s, congr, dsimp, exact congr_fun (congr_fun (congr_arg nat_trans.app h.inv_hom_id) s_X) s_ι, end @[simp] lemma hom_of_cocone_of_hom {Y : C} (f : X ⟶ Y) : hom_of_cocone h (cocone_of_hom h f) = f := congr_fun (congr_fun (congr_arg nat_trans.app h.hom_inv_id) Y) f /-- If `F.cocones` is corepresented by `X`, the cocone corresponding to the identity morphism on `X` will be a colimit cocone. -/ def colimit_cocone : cocone F := cocone_of_hom h (𝟙 X) /-- If `F.cocones` is corepresented by `X`, the cocone corresponding to a morphism `f : Y ⟶ X` is the colimit cocone extended by `f`. -/ lemma cocone_of_hom_fac {Y : C} (f : X ⟶ Y) : cocone_of_hom h f = (colimit_cocone h).extend f := begin dsimp [cocone_of_hom, colimit_cocone, cocone.extend], congr, ext j, have t := congr_fun (h.hom.naturality f) (𝟙 X), dsimp at t, simp only [id_comp] at t, rw congr_fun (congr_arg nat_trans.app t) j, refl, end /-- If `F.cocones` is corepresented by `X`, any cocone is the extension of the colimit cocone by the corresponding morphism. -/ lemma cocone_fac (s : cocone F) : (colimit_cocone h).extend (hom_of_cocone h s) = s := begin rw ←cocone_of_hom_of_cocone h s, conv_lhs { simp only [hom_of_cocone_of_hom] }, apply (cocone_of_hom_fac _ _).symm, end end of_nat_iso section open of_nat_iso /-- If `F.cocones` is corepresentable, then the cocone corresponding to the identity morphism on the representing object is a colimit cocone. -/ def of_nat_iso {X : C} (h : coyoneda.obj (op X) ≅ F.cocones) : is_colimit (colimit_cocone h) := { desc := λ s, hom_of_cocone h s, fac' := λ s j, begin have h := cocone_fac h s, cases s, injection h with h₁ h₂, simp only [heq_iff_eq] at h₂, conv_rhs { rw ← h₂ }, refl, end, uniq' := λ s m w, begin rw ←hom_of_cocone_of_hom h m, congr, rw cocone_of_hom_fac, dsimp, cases s, congr, ext j, exact w j, end } end end is_colimit section limit /-- `has_limit F` represents a particular chosen limit of the diagram `F`. -/ class has_limit (F : J ⥤ C) := (cone : cone F) (is_limit : is_limit cone . tactic.apply_instance) variables (J C) /-- `C` has limits of shape `J` if we have chosen a particular limit of every functor `F : J ⥤ C`. -/ class has_limits_of_shape := (has_limit : Π F : J ⥤ C, has_limit F) /-- `C` has all (small) limits if it has limits of every shape. -/ class has_limits := (has_limits_of_shape : Π (J : Type v) [𝒥 : small_category J], has_limits_of_shape J C) variables {J C} instance has_limit_of_has_limits_of_shape {J : Type v} [small_category J] [H : has_limits_of_shape J C] (F : J ⥤ C) : has_limit F := has_limits_of_shape.has_limit F instance has_limits_of_shape_of_has_limits {J : Type v} [small_category J] [H : has_limits.{v} C] : has_limits_of_shape J C := has_limits.has_limits_of_shape C J /- Interface to the `has_limit` class. -/ def limit.cone (F : J ⥤ C) [has_limit F] : cone F := has_limit.cone F def limit (F : J ⥤ C) [has_limit F] := (limit.cone F).X def limit.π (F : J ⥤ C) [has_limit F] (j : J) : limit F ⟶ F.obj j := (limit.cone F).π.app j @[simp] lemma limit.cone_π {F : J ⥤ C} [has_limit F] (j : J) : (limit.cone F).π.app j = limit.π _ j := rfl @[simp] lemma limit.w (F : J ⥤ C) [has_limit F] {j j' : J} (f : j ⟶ j') : limit.π F j ≫ F.map f = limit.π F j' := (limit.cone F).w f def limit.is_limit (F : J ⥤ C) [has_limit F] : is_limit (limit.cone F) := has_limit.is_limit.{v} F def limit.lift (F : J ⥤ C) [has_limit F] (c : cone F) : c.X ⟶ limit F := (limit.is_limit F).lift c @[simp] lemma limit.is_limit_lift {F : J ⥤ C} [has_limit F] (c : cone F) : (limit.is_limit F).lift c = limit.lift F c := rfl @[simp, reassoc] lemma limit.lift_π {F : J ⥤ C} [has_limit F] (c : cone F) (j : J) : limit.lift F c ≫ limit.π F j = c.π.app j := is_limit.fac _ c j def limit.cone_morphism {F : J ⥤ C} [has_limit F] (c : cone F) : cone_morphism c (limit.cone F) := (limit.is_limit F).lift_cone_morphism c @[simp] lemma limit.cone_morphism_hom {F : J ⥤ C} [has_limit F] (c : cone F) : (limit.cone_morphism c).hom = limit.lift F c := rfl @[simp] lemma limit.cone_morphism_π {F : J ⥤ C} [has_limit F] (c : cone F) (j : J) : (limit.cone_morphism c).hom ≫ limit.π F j = c.π.app j := by erw is_limit.fac @[extensionality] lemma limit.hom_ext {F : J ⥤ C} [has_limit F] {X : C} {f f' : X ⟶ limit F} (w : ∀ j, f ≫ limit.π F j = f' ≫ limit.π F j) : f = f' := (limit.is_limit F).hom_ext w def limit.hom_iso (F : J ⥤ C) [has_limit F] (W : C) : (W ⟶ limit F) ≅ (F.cones.obj (op W)) := (limit.is_limit F).hom_iso W @[simp] lemma limit.hom_iso_hom (F : J ⥤ C) [has_limit F] {W : C} (f : W ⟶ limit F) : (limit.hom_iso F W).hom f = (const J).map f ≫ (limit.cone F).π := (limit.is_limit F).hom_iso_hom f def limit.hom_iso' (F : J ⥤ C) [has_limit F] (W : C) : ((W ⟶ limit F) : Type v) ≅ { p : Π j, W ⟶ F.obj j // ∀ {j j' : J} (f : j ⟶ j'), p j ≫ F.map f = p j' } := (limit.is_limit F).hom_iso' W lemma limit.lift_extend {F : J ⥤ C} [has_limit F] (c : cone F) {X : C} (f : X ⟶ c.X) : limit.lift F (c.extend f) = f ≫ limit.lift F c := by obviously def has_limit_of_iso {F G : J ⥤ C} [has_limit F] (α : F ≅ G) : has_limit G := { cone := (cones.postcompose α.hom).obj (limit.cone F), is_limit := { lift := λ s, limit.lift F ((cones.postcompose α.inv).obj s), fac' := λ s j, begin rw [cones.postcompose_obj_π, nat_trans.comp_app, limit.cone_π], rw [category.assoc_symm, limit.lift_π], simp end, uniq' := λ s m w, begin apply limit.hom_ext, intro j, rw [limit.lift_π, cones.postcompose_obj_π, nat_trans.comp_app, ←nat_iso.app_inv, iso.eq_comp_inv], simpa using w j end } } /-- If a functor `G` has the same collection of cones as a functor `F` which has a limit, then `G` also has a limit. -/ -- See the construction of limits from products and equalizers -- for an example usage. def has_limit.of_cones_iso {J K : Type v} [small_category J] [small_category K] (F : J ⥤ C) (G : K ⥤ C) (h : F.cones ≅ G.cones) [has_limit F] : has_limit G := ⟨_, is_limit.of_nat_iso ((is_limit.nat_iso (limit.is_limit F)) ≪≫ h)⟩ section pre variables (F) [has_limit F] (E : K ⥤ J) [has_limit (E ⋙ F)] def limit.pre : limit F ⟶ limit (E ⋙ F) := limit.lift (E ⋙ F) { X := limit F, π := { app := λ k, limit.π F (E.obj k) } } @[simp] lemma limit.pre_π (k : K) : limit.pre F E ≫ limit.π (E ⋙ F) k = limit.π F (E.obj k) := by erw is_limit.fac @[simp] lemma limit.lift_pre (c : cone F) : limit.lift F c ≫ limit.pre F E = limit.lift (E ⋙ F) (c.whisker E) := by ext; simp variables {L : Type v} [small_category L] variables (D : L ⥤ K) [has_limit (D ⋙ E ⋙ F)] @[simp] lemma limit.pre_pre : limit.pre F E ≫ limit.pre (E ⋙ F) D = limit.pre F (D ⋙ E) := by ext j; erw [assoc, limit.pre_π, limit.pre_π, limit.pre_π]; refl end pre section post variables {D : Type u'} [𝒟 : category.{v} D] include 𝒟 variables (F) [has_limit F] (G : C ⥤ D) [has_limit (F ⋙ G)] def limit.post : G.obj (limit F) ⟶ limit (F ⋙ G) := limit.lift (F ⋙ G) { X := G.obj (limit F), π := { app := λ j, G.map (limit.π F j), naturality' := by intros j j' f; erw [←G.map_comp, limits.cone.w, id_comp]; refl } } @[simp] lemma limit.post_π (j : J) : limit.post F G ≫ limit.π (F ⋙ G) j = G.map (limit.π F j) := by erw is_limit.fac @[simp] lemma limit.lift_post (c : cone F) : G.map (limit.lift F c) ≫ limit.post F G = limit.lift (F ⋙ G) (G.map_cone c) := by ext; rw [assoc, limit.post_π, ←G.map_comp, limit.lift_π, limit.lift_π]; refl @[simp] lemma limit.post_post {E : Type u''} [category.{v} E] (H : D ⥤ E) [has_limit ((F ⋙ G) ⋙ H)] : /- H G (limit F) ⟶ H (limit (F ⋙ G)) ⟶ limit ((F ⋙ G) ⋙ H) equals -/ /- H G (limit F) ⟶ limit (F ⋙ (G ⋙ H)) -/ H.map (limit.post F G) ≫ limit.post (F ⋙ G) H = limit.post F (G ⋙ H) := by ext; erw [assoc, limit.post_π, ←H.map_comp, limit.post_π, limit.post_π]; refl end post lemma limit.pre_post {D : Type u'} [category.{v} D] (E : K ⥤ J) (F : J ⥤ C) (G : C ⥤ D) [has_limit F] [has_limit (E ⋙ F)] [has_limit (F ⋙ G)] [has_limit ((E ⋙ F) ⋙ G)] : /- G (limit F) ⟶ G (limit (E ⋙ F)) ⟶ limit ((E ⋙ F) ⋙ G) vs -/ /- G (limit F) ⟶ limit F ⋙ G ⟶ limit (E ⋙ (F ⋙ G)) or -/ G.map (limit.pre F E) ≫ limit.post (E ⋙ F) G = limit.post F G ≫ limit.pre (F ⋙ G) E := by ext; erw [assoc, limit.post_π, ←G.map_comp, limit.pre_π, assoc, limit.pre_π, limit.post_π]; refl open category_theory.equivalence instance has_limit_equivalence_comp (e : K ≌ J) [has_limit F] : has_limit (e.functor ⋙ F) := { cone := cone.whisker e.functor (limit.cone F), is_limit := let e' := cones.postcompose (e.inv_fun_id_assoc F).hom in { lift := λ s, limit.lift F (e'.obj (cone.whisker e.inverse s)), fac' := λ s j, begin dsimp, rw [limit.lift_π], dsimp [e'], erw [inv_fun_id_assoc_hom_app, counit_functor, ←s.π.naturality, id_comp] end, uniq' := λ s m w, begin apply limit.hom_ext, intro j, erw [limit.lift_π, ←limit.w F (e.counit_iso.hom.app j)], slice_lhs 1 2 { erw [w (e.inverse.obj j)] }, simp end } } local attribute [elab_simple] inv_fun_id_assoc -- not entirely sure why this is needed def has_limit_of_equivalence_comp (e : K ≌ J) [has_limit (e.functor ⋙ F)] : has_limit F := begin haveI : has_limit (e.inverse ⋙ e.functor ⋙ F) := limits.has_limit_equivalence_comp e.symm, apply has_limit_of_iso (e.inv_fun_id_assoc F), end -- `has_limit_comp_equivalence` and `has_limit_of_comp_equivalence` -- are proved in `category_theory/adjunction/limits.lean`. section lim_functor variables [has_limits_of_shape J C] /-- `limit F` is functorial in `F`, when `C` has all limits of shape `J`. -/ def lim : (J ⥤ C) ⥤ C := { obj := λ F, limit F, map := λ F G α, limit.lift G { X := limit F, π := { app := λ j, limit.π F j ≫ α.app j, naturality' := λ j j' f, by erw [id_comp, assoc, ←α.naturality, ←assoc, limit.w] } }, map_comp' := λ F G H α β, by ext; erw [assoc, is_limit.fac, is_limit.fac, ←assoc, is_limit.fac, assoc]; refl } variables {F} {G : J ⥤ C} (α : F ⟶ G) @[simp, reassoc] lemma lim.map_π (j : J) : lim.map α ≫ limit.π G j = limit.π F j ≫ α.app j := by apply is_limit.fac @[simp] lemma limit.lift_map (c : cone F) : limit.lift F c ≫ lim.map α = limit.lift G ((cones.postcompose α).obj c) := by ext; rw [assoc, lim.map_π, ←assoc, limit.lift_π, limit.lift_π]; refl lemma limit.map_pre [has_limits_of_shape K C] (E : K ⥤ J) : lim.map α ≫ limit.pre G E = limit.pre F E ≫ lim.map (whisker_left E α) := by ext; rw [assoc, limit.pre_π, lim.map_π, assoc, lim.map_π, ←assoc, limit.pre_π]; refl lemma limit.map_pre' [has_limits_of_shape.{v} K C] (F : J ⥤ C) {E₁ E₂ : K ⥤ J} (α : E₁ ⟶ E₂) : limit.pre F E₂ = limit.pre F E₁ ≫ lim.map (whisker_right α F) := by ext1; simp [(category.assoc _ _ _ _).symm] lemma limit.id_pre (F : J ⥤ C) : limit.pre F (𝟭 _) = lim.map (functor.left_unitor F).inv := by tidy lemma limit.map_post {D : Type u'} [category.{v} D] [has_limits_of_shape J D] (H : C ⥤ D) : /- H (limit F) ⟶ H (limit G) ⟶ limit (G ⋙ H) vs H (limit F) ⟶ limit (F ⋙ H) ⟶ limit (G ⋙ H) -/ H.map (lim.map α) ≫ limit.post G H = limit.post F H ≫ lim.map (whisker_right α H) := begin ext, rw [assoc, limit.post_π, ←H.map_comp, lim.map_π, H.map_comp], rw [assoc, lim.map_π, ←assoc, limit.post_π], refl end def lim_yoneda : lim ⋙ yoneda ≅ category_theory.cones J C := nat_iso.of_components (λ F, nat_iso.of_components (λ W, limit.hom_iso F (unop W)) (by tidy)) (by tidy) end lim_functor def has_limits_of_shape_of_equivalence {J' : Type v} [small_category J'] (e : J ≌ J') [has_limits_of_shape J C] : has_limits_of_shape J' C := by { constructor, intro F, apply has_limit_of_equivalence_comp e, apply_instance } end limit section colimit /-- `has_colimit F` represents a particular chosen colimit of the diagram `F`. -/ class has_colimit (F : J ⥤ C) := (cocone : cocone F) (is_colimit : is_colimit cocone . tactic.apply_instance) variables (J C) /-- `C` has colimits of shape `J` if we have chosen a particular colimit of every functor `F : J ⥤ C`. -/ class has_colimits_of_shape := (has_colimit : Π F : J ⥤ C, has_colimit F) /-- `C` has all (small) colimits if it has colimits of every shape. -/ class has_colimits := (has_colimits_of_shape : Π (J : Type v) [𝒥 : small_category J], has_colimits_of_shape J C) variables {J C} instance has_colimit_of_has_colimits_of_shape {J : Type v} [small_category J] [H : has_colimits_of_shape J C] (F : J ⥤ C) : has_colimit F := has_colimits_of_shape.has_colimit F instance has_colimits_of_shape_of_has_colimits {J : Type v} [small_category J] [H : has_colimits.{v} C] : has_colimits_of_shape J C := has_colimits.has_colimits_of_shape C J /- Interface to the `has_colimit` class. -/ def colimit.cocone (F : J ⥤ C) [has_colimit F] : cocone F := has_colimit.cocone F def colimit (F : J ⥤ C) [has_colimit F] := (colimit.cocone F).X def colimit.ι (F : J ⥤ C) [has_colimit F] (j : J) : F.obj j ⟶ colimit F := (colimit.cocone F).ι.app j @[simp] lemma colimit.cocone_ι {F : J ⥤ C} [has_colimit F] (j : J) : (colimit.cocone F).ι.app j = colimit.ι _ j := rfl @[simp] lemma colimit.w (F : J ⥤ C) [has_colimit F] {j j' : J} (f : j ⟶ j') : F.map f ≫ colimit.ι F j' = colimit.ι F j := (colimit.cocone F).w f def colimit.is_colimit (F : J ⥤ C) [has_colimit F] : is_colimit (colimit.cocone F) := has_colimit.is_colimit.{v} F def colimit.desc (F : J ⥤ C) [has_colimit F] (c : cocone F) : colimit F ⟶ c.X := (colimit.is_colimit F).desc c @[simp] lemma colimit.is_colimit_desc {F : J ⥤ C} [has_colimit F] (c : cocone F) : (colimit.is_colimit F).desc c = colimit.desc F c := rfl /-- We have lots of lemmas describing how to simplify `colimit.ι F j ≫ _`, and combined with `colimit.ext` we rely on these lemmas for many calculations. However, since `category.assoc` is a `@[simp]` lemma, often expressions are right associated, and it's hard to apply these lemmas about `colimit.ι`. We thus use `reassoc` to define additional `@[simp]` lemmas, with an arbitrary extra morphism. (see `tactic/reassoc_axiom.lean`) -/ @[simp, reassoc] lemma colimit.ι_desc {F : J ⥤ C} [has_colimit F] (c : cocone F) (j : J) : colimit.ι F j ≫ colimit.desc F c = c.ι.app j := is_colimit.fac _ c j def colimit.cocone_morphism {F : J ⥤ C} [has_colimit F] (c : cocone F) : cocone_morphism (colimit.cocone F) c := (colimit.is_colimit F).desc_cocone_morphism c @[simp] lemma colimit.cocone_morphism_hom {F : J ⥤ C} [has_colimit F] (c : cocone F) : (colimit.cocone_morphism c).hom = colimit.desc F c := rfl @[simp] lemma colimit.ι_cocone_morphism {F : J ⥤ C} [has_colimit F] (c : cocone F) (j : J) : colimit.ι F j ≫ (colimit.cocone_morphism c).hom = c.ι.app j := by erw is_colimit.fac @[extensionality] lemma colimit.hom_ext {F : J ⥤ C} [has_colimit F] {X : C} {f f' : colimit F ⟶ X} (w : ∀ j, colimit.ι F j ≫ f = colimit.ι F j ≫ f') : f = f' := (colimit.is_colimit F).hom_ext w def colimit.hom_iso (F : J ⥤ C) [has_colimit F] (W : C) : (colimit F ⟶ W) ≅ (F.cocones.obj W) := (colimit.is_colimit F).hom_iso W @[simp] lemma colimit.hom_iso_hom (F : J ⥤ C) [has_colimit F] {W : C} (f : colimit F ⟶ W) : (colimit.hom_iso F W).hom f = (colimit.cocone F).ι ≫ (const J).map f := (colimit.is_colimit F).hom_iso_hom f def colimit.hom_iso' (F : J ⥤ C) [has_colimit F] (W : C) : ((colimit F ⟶ W) : Type v) ≅ { p : Π j, F.obj j ⟶ W // ∀ {j j'} (f : j ⟶ j'), F.map f ≫ p j' = p j } := (colimit.is_colimit F).hom_iso' W lemma colimit.desc_extend (F : J ⥤ C) [has_colimit F] (c : cocone F) {X : C} (f : c.X ⟶ X) : colimit.desc F (c.extend f) = colimit.desc F c ≫ f := begin ext1, rw [←category.assoc], simp end def has_colimit_of_iso {F G : J ⥤ C} [has_colimit F] (α : G ≅ F) : has_colimit G := { cocone := (cocones.precompose α.hom).obj (colimit.cocone F), is_colimit := { desc := λ s, colimit.desc F ((cocones.precompose α.inv).obj s), fac' := λ s j, begin rw [cocones.precompose_obj_ι, nat_trans.comp_app, colimit.cocone_ι], rw [category.assoc, colimit.ι_desc, ←nat_iso.app_hom, ←iso.eq_inv_comp], refl end, uniq' := λ s m w, begin apply colimit.hom_ext, intro j, rw [colimit.ι_desc, cocones.precompose_obj_ι, nat_trans.comp_app, ←nat_iso.app_inv, iso.eq_inv_comp], simpa using w j end } } /-- If a functor `G` has the same collection of cocones as a functor `F` which has a colimit, then `G` also has a colimit. -/ def has_colimit.of_cocones_iso {J K : Type v} [small_category J] [small_category K] (F : J ⥤ C) (G : K ⥤ C) (h : F.cocones ≅ G.cocones) [has_colimit F] : has_colimit G := ⟨_, is_colimit.of_nat_iso ((is_colimit.nat_iso (colimit.is_colimit F)) ≪≫ h)⟩ section pre variables (F) [has_colimit F] (E : K ⥤ J) [has_colimit (E ⋙ F)] def colimit.pre : colimit (E ⋙ F) ⟶ colimit F := colimit.desc (E ⋙ F) { X := colimit F, ι := { app := λ k, colimit.ι F (E.obj k) } } @[simp, reassoc] lemma colimit.ι_pre (k : K) : colimit.ι (E ⋙ F) k ≫ colimit.pre F E = colimit.ι F (E.obj k) := by erw is_colimit.fac @[simp] lemma colimit.pre_desc (c : cocone F) : colimit.pre F E ≫ colimit.desc F c = colimit.desc (E ⋙ F) (c.whisker E) := by ext; rw [←assoc, colimit.ι_pre]; simp variables {L : Type v} [small_category L] variables (D : L ⥤ K) [has_colimit (D ⋙ E ⋙ F)] @[simp] lemma colimit.pre_pre : colimit.pre (E ⋙ F) D ≫ colimit.pre F E = colimit.pre F (D ⋙ E) := begin ext j, rw [←assoc, colimit.ι_pre, colimit.ι_pre], letI : has_colimit ((D ⋙ E) ⋙ F) := show has_colimit (D ⋙ E ⋙ F), by apply_instance, exact (colimit.ι_pre F (D ⋙ E) j).symm end end pre section post variables {D : Type u'} [𝒟 : category.{v} D] include 𝒟 variables (F) [has_colimit F] (G : C ⥤ D) [has_colimit (F ⋙ G)] def colimit.post : colimit (F ⋙ G) ⟶ G.obj (colimit F) := colimit.desc (F ⋙ G) { X := G.obj (colimit F), ι := { app := λ j, G.map (colimit.ι F j), naturality' := by intros j j' f; erw [←G.map_comp, limits.cocone.w, comp_id]; refl } } @[simp, reassoc] lemma colimit.ι_post (j : J) : colimit.ι (F ⋙ G) j ≫ colimit.post F G = G.map (colimit.ι F j) := by erw is_colimit.fac @[simp] lemma colimit.post_desc (c : cocone F) : colimit.post F G ≫ G.map (colimit.desc F c) = colimit.desc (F ⋙ G) (G.map_cocone c) := by ext; rw [←assoc, colimit.ι_post, ←G.map_comp, colimit.ι_desc, colimit.ι_desc]; refl @[simp] lemma colimit.post_post {E : Type u''} [category.{v} E] (H : D ⥤ E) [has_colimit ((F ⋙ G) ⋙ H)] : /- H G (colimit F) ⟶ H (colimit (F ⋙ G)) ⟶ colimit ((F ⋙ G) ⋙ H) equals -/ /- H G (colimit F) ⟶ colimit (F ⋙ (G ⋙ H)) -/ colimit.post (F ⋙ G) H ≫ H.map (colimit.post F G) = colimit.post F (G ⋙ H) := begin ext, rw [←assoc, colimit.ι_post, ←H.map_comp, colimit.ι_post], exact (colimit.ι_post F (G ⋙ H) j).symm end end post lemma colimit.pre_post {D : Type u'} [category.{v} D] (E : K ⥤ J) (F : J ⥤ C) (G : C ⥤ D) [has_colimit F] [has_colimit (E ⋙ F)] [has_colimit (F ⋙ G)] [has_colimit ((E ⋙ F) ⋙ G)] : /- G (colimit F) ⟶ G (colimit (E ⋙ F)) ⟶ colimit ((E ⋙ F) ⋙ G) vs -/ /- G (colimit F) ⟶ colimit F ⋙ G ⟶ colimit (E ⋙ (F ⋙ G)) or -/ colimit.post (E ⋙ F) G ≫ G.map (colimit.pre F E) = colimit.pre (F ⋙ G) E ≫ colimit.post F G := begin ext, rw [←assoc, colimit.ι_post, ←G.map_comp, colimit.ι_pre, ←assoc], letI : has_colimit (E ⋙ F ⋙ G) := show has_colimit ((E ⋙ F) ⋙ G), by apply_instance, erw [colimit.ι_pre (F ⋙ G) E j, colimit.ι_post] end open category_theory.equivalence instance has_colimit_equivalence_comp (e : K ≌ J) [has_colimit F] : has_colimit (e.functor ⋙ F) := { cocone := cocone.whisker e.functor (colimit.cocone F), is_colimit := let e' := cocones.precompose (e.inv_fun_id_assoc F).inv in { desc := λ s, colimit.desc F (e'.obj (cocone.whisker e.inverse s)), fac' := λ s j, begin dsimp, rw [colimit.ι_desc], dsimp [e'], erw [inv_fun_id_assoc_inv_app, ←functor_unit, s.ι.naturality, comp_id], refl end, uniq' := λ s m w, begin apply colimit.hom_ext, intro j, erw [colimit.ι_desc], have := w (e.inverse.obj j), simp at this, erw [←colimit.w F (e.counit_iso.hom.app j)] at this, erw [assoc, ←iso.eq_inv_comp (F.map_iso $ e.counit_iso.app j)] at this, erw [this], simp end } } def has_colimit_of_equivalence_comp (e : K ≌ J) [has_colimit (e.functor ⋙ F)] : has_colimit F := begin haveI : has_colimit (e.inverse ⋙ e.functor ⋙ F) := limits.has_colimit_equivalence_comp e.symm, apply has_colimit_of_iso (e.inv_fun_id_assoc F).symm, end section colim_functor variables [has_colimits_of_shape J C] /-- `colimit F` is functorial in `F`, when `C` has all colimits of shape `J`. -/ def colim : (J ⥤ C) ⥤ C := { obj := λ F, colimit F, map := λ F G α, colimit.desc F { X := colimit G, ι := { app := λ j, α.app j ≫ colimit.ι G j, naturality' := λ j j' f, by erw [comp_id, ←assoc, α.naturality, assoc, colimit.w] } }, map_comp' := λ F G H α β, by ext; erw [←assoc, is_colimit.fac, is_colimit.fac, assoc, is_colimit.fac, ←assoc]; refl } variables {F} {G : J ⥤ C} (α : F ⟶ G) @[simp, reassoc] lemma colim.ι_map (j : J) : colimit.ι F j ≫ colim.map α = α.app j ≫ colimit.ι G j := by apply is_colimit.fac @[simp] lemma colimit.map_desc (c : cocone G) : colim.map α ≫ colimit.desc G c = colimit.desc F ((cocones.precompose α).obj c) := by ext; rw [←assoc, colim.ι_map, assoc, colimit.ι_desc, colimit.ι_desc]; refl lemma colimit.pre_map [has_colimits_of_shape K C] (E : K ⥤ J) : colimit.pre F E ≫ colim.map α = colim.map (whisker_left E α) ≫ colimit.pre G E := by ext; rw [←assoc, colimit.ι_pre, colim.ι_map, ←assoc, colim.ι_map, assoc, colimit.ι_pre]; refl lemma colimit.pre_map' [has_colimits_of_shape.{v} K C] (F : J ⥤ C) {E₁ E₂ : K ⥤ J} (α : E₁ ⟶ E₂) : colimit.pre F E₁ = colim.map (whisker_right α F) ≫ colimit.pre F E₂ := by ext1; simp [(category.assoc _ _ _ _).symm] lemma colimit.pre_id (F : J ⥤ C) : colimit.pre F (𝟭 _) = colim.map (functor.left_unitor F).hom := by tidy lemma colimit.map_post {D : Type u'} [category.{v} D] [has_colimits_of_shape J D] (H : C ⥤ D) : /- H (colimit F) ⟶ H (colimit G) ⟶ colimit (G ⋙ H) vs H (colimit F) ⟶ colimit (F ⋙ H) ⟶ colimit (G ⋙ H) -/ colimit.post F H ≫ H.map (colim.map α) = colim.map (whisker_right α H) ≫ colimit.post G H:= begin ext, rw [←assoc, colimit.ι_post, ←H.map_comp, colim.ι_map, H.map_comp], rw [←assoc, colim.ι_map, assoc, colimit.ι_post], refl end def colim_coyoneda : colim.op ⋙ coyoneda ≅ category_theory.cocones J C := nat_iso.of_components (λ F, nat_iso.of_components (colimit.hom_iso (unop F)) (by tidy)) (by tidy) end colim_functor def has_colimits_of_shape_of_equivalence {J' : Type v} [small_category J'] (e : J ≌ J') [has_colimits_of_shape J C] : has_colimits_of_shape J' C := by { constructor, intro F, apply has_colimit_of_equivalence_comp e, apply_instance } end colimit end category_theory.limits
41b4033e9607a6e49bbbcaa13b79b802128fd6ab
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Elab/Level.lean
406e7be8d0b41d9b46feeaadce35a7d86eb15c8d
[ "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
3,270
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.Elab.Exception import Lean.Elab.Log import Lean.Elab.AutoBound namespace Lean.Elab.Level structure Context where options : Options ref : Syntax autoBoundImplicit : Bool structure State where ngen : NameGenerator mctx : MetavarContext levelNames : List Name abbrev LevelElabM := ReaderT Context (EStateM Exception State) instance : MonadOptions LevelElabM where getOptions := return (← read).options instance : MonadRef LevelElabM where getRef := return (← read).ref withRef ref x := withReader (fun ctx => { ctx with ref := ref }) x instance : AddMessageContext LevelElabM where addMessageContext msg := pure msg instance : MonadNameGenerator LevelElabM where getNGen := return (← get).ngen setNGen ngen := modify fun s => { s with ngen := ngen } def mkFreshLevelMVar : LevelElabM Level := do let mvarId ← mkFreshMVarId modify fun s => { s with mctx := s.mctx.addLevelMVarDecl mvarId } return mkLevelMVar mvarId register_builtin_option maxUniverseOffset : Nat := { defValue := 32 descr := "maximum universe level offset" } private def checkUniverseOffset [Monad m] [MonadError m] [MonadOptions m] (n : Nat) : m Unit := do let max := maxUniverseOffset.get (← getOptions) unless n <= max do throwError "maximum universe level offset threshold ({max}) has been reached, you can increase the limit using option `set_option maxUniverseOffset <limit>`, but you are probably misusing universe levels since offsets are usually small natural numbers" partial def elabLevel (stx : Syntax) : LevelElabM Level := withRef stx do let kind := stx.getKind if kind == `Lean.Parser.Level.paren then elabLevel (stx.getArg 1) else if kind == `Lean.Parser.Level.max then let args := stx.getArg 1 |>.getArgs args[:args.size - 1].foldrM (init := ← elabLevel args.back) fun stx lvl => return mkLevelMax' (← elabLevel stx) lvl else if kind == `Lean.Parser.Level.imax then let args := stx.getArg 1 |>.getArgs args[:args.size - 1].foldrM (init := ← elabLevel args.back) fun stx lvl => return mkLevelIMax' (← elabLevel stx) lvl else if kind == `Lean.Parser.Level.hole then mkFreshLevelMVar else if kind == numLitKind then match stx.isNatLit? with | some val => checkUniverseOffset val; return Level.ofNat val | none => throwIllFormedSyntax else if kind == identKind then let paramName := stx.getId unless (← get).levelNames.contains paramName do if (← read).autoBoundImplicit && isValidAutoBoundLevelName paramName then modify fun s => { s with levelNames := paramName :: s.levelNames } else throwError "unknown universe level '{paramName}'" return mkLevelParam paramName else if kind == `Lean.Parser.Level.addLit then let lvl ← elabLevel (stx.getArg 0) match stx.getArg 2 |>.isNatLit? with | some val => checkUniverseOffset val; return lvl.addOffset val | none => throwIllFormedSyntax else throwError "unexpected universe level syntax kind" end Lean.Elab.Level
25a75ee0fbf10472d41c980d5d98a5a999bfd9b7
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/ring_theory/unique_factorization_domain.lean
da99bdf74e1a59f30e6ac5395b7e00ccbf552996
[ "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
50,273
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Jens Wagemaker, Aaron Anderson -/ import algebra.gcd_monoid import ring_theory.integral_domain import ring_theory.noetherian /-! # Unique factorization ## Main Definitions * `wf_dvd_monoid` holds for `monoid`s for which a strict divisibility relation is well-founded. * `unique_factorization_monoid` holds for `wf_dvd_monoid`s where `irreducible` is equivalent to `prime` ## To do * set up the complete lattice structure on `factor_set`. -/ variables {α : Type*} local infix ` ~ᵤ ` : 50 := associated /-- Well-foundedness of the strict version of |, which is equivalent to the descending chain condition on divisibility and to the ascending chain condition on principal ideals in an integral domain. -/ class wf_dvd_monoid (α : Type*) [comm_monoid_with_zero α] : Prop := (well_founded_dvd_not_unit : well_founded (@dvd_not_unit α _)) export wf_dvd_monoid (well_founded_dvd_not_unit) @[priority 100] -- see Note [lower instance priority] instance is_noetherian_ring.wf_dvd_monoid [integral_domain α] [is_noetherian_ring α] : wf_dvd_monoid α := ⟨by { convert inv_image.wf (λ a, ideal.span ({a} : set α)) (well_founded_submodule_gt _ _), ext, exact ideal.span_singleton_lt_span_singleton.symm }⟩ namespace wf_dvd_monoid variables [comm_monoid_with_zero α] open associates nat theorem of_wf_dvd_monoid_associates (h : wf_dvd_monoid (associates α)): wf_dvd_monoid α := ⟨begin haveI := h, refine (surjective.well_founded_iff mk_surjective _).2 wf_dvd_monoid.well_founded_dvd_not_unit, intros, rw mk_dvd_not_unit_mk_iff end⟩ variables [wf_dvd_monoid α] instance wf_dvd_monoid_associates : wf_dvd_monoid (associates α) := ⟨begin refine (surjective.well_founded_iff mk_surjective _).1 wf_dvd_monoid.well_founded_dvd_not_unit, intros, rw mk_dvd_not_unit_mk_iff end⟩ theorem well_founded_associates : well_founded ((<) : associates α → associates α → Prop) := subrelation.wf (λ x y, dvd_not_unit_of_lt) wf_dvd_monoid.well_founded_dvd_not_unit local attribute [elab_as_eliminator] well_founded.fix lemma exists_irreducible_factor {a : α} (ha : ¬ is_unit a) (ha0 : a ≠ 0) : ∃ i, irreducible i ∧ i ∣ a := (irreducible_or_factor a ha).elim (λ hai, ⟨a, hai, dvd_refl _⟩) (well_founded.fix wf_dvd_monoid.well_founded_dvd_not_unit (λ a ih ha ha0 ⟨x, y, hx, hy, hxy⟩, have hx0 : x ≠ 0, from λ hx0, ha0 (by rw [← hxy, hx0, zero_mul]), (irreducible_or_factor x hx).elim (λ hxi, ⟨x, hxi, hxy ▸ by simp⟩) (λ hxf, let ⟨i, hi⟩ := ih x ⟨hx0, y, hy, hxy.symm⟩ hx hx0 hxf in ⟨i, hi.1, dvd.trans hi.2 (hxy ▸ by simp)⟩)) a ha ha0) @[elab_as_eliminator] lemma induction_on_irreducible {P : α → Prop} (a : α) (h0 : P 0) (hu : ∀ u : α, is_unit u → P u) (hi : ∀ a i : α, a ≠ 0 → irreducible i → P a → P (i * a)) : P a := by haveI := classical.dec; exact well_founded.fix wf_dvd_monoid.well_founded_dvd_not_unit (λ a ih, if ha0 : a = 0 then ha0.symm ▸ h0 else if hau : is_unit a then hu a hau else let ⟨i, hii, ⟨b, hb⟩⟩ := exists_irreducible_factor hau ha0 in have hb0 : b ≠ 0, from λ hb0, by simp * at *, hb.symm ▸ hi _ _ hb0 hii (ih _ ⟨hb0, i, hii.1, by rw [hb, mul_comm]⟩)) a lemma exists_factors (a : α) : a ≠ 0 → ∃f : multiset α, (∀b ∈ f, irreducible b) ∧ associated f.prod a := wf_dvd_monoid.induction_on_irreducible a (λ h, (h rfl).elim) (λ u hu _, ⟨0, ⟨by simp [hu], associated.symm (by simp [hu, associated_one_iff_is_unit])⟩⟩) (λ a i ha0 hii ih hia0, let ⟨s, hs⟩ := ih ha0 in ⟨i ::ₘ s, ⟨by clear _let_match; finish, by { rw multiset.prod_cons, exact associated_mul_mul (by refl) hs.2 }⟩⟩) end wf_dvd_monoid theorem wf_dvd_monoid.of_well_founded_associates [comm_cancel_monoid_with_zero α] (h : well_founded ((<) : associates α → associates α → Prop)) : wf_dvd_monoid α := wf_dvd_monoid.of_wf_dvd_monoid_associates ⟨by { convert h, ext, exact associates.dvd_not_unit_iff_lt }⟩ theorem wf_dvd_monoid.iff_well_founded_associates [comm_cancel_monoid_with_zero α] : wf_dvd_monoid α ↔ well_founded ((<) : associates α → associates α → Prop) := ⟨by apply wf_dvd_monoid.well_founded_associates, wf_dvd_monoid.of_well_founded_associates⟩ section prio set_option default_priority 100 -- see Note [default priority] /-- unique factorization monoids. These are defined as `comm_cancel_monoid_with_zero`s with well-founded strict divisibility relations, but this is equivalent to more familiar definitions: Each element (except zero) is uniquely represented as a multiset of irreducible factors. Uniqueness is only up to associated elements. Each element (except zero) is non-uniquely represented as a multiset of prime factors. To define a UFD using the definition in terms of multisets of irreducible factors, use the definition `of_exists_unique_irreducible_factors` To define a UFD using the definition in terms of multisets of prime factors, use the definition `of_exists_prime_factors` -/ class unique_factorization_monoid (α : Type*) [comm_cancel_monoid_with_zero α] extends wf_dvd_monoid α : Prop := (irreducible_iff_prime : ∀ {a : α}, irreducible a ↔ prime a) instance ufm_of_gcd_of_wf_dvd_monoid [nontrivial α] [comm_cancel_monoid_with_zero α] [wf_dvd_monoid α] [gcd_monoid α] : unique_factorization_monoid α := { irreducible_iff_prime := λ _, gcd_monoid.irreducible_iff_prime .. ‹wf_dvd_monoid α› } instance associates.ufm [comm_cancel_monoid_with_zero α] [unique_factorization_monoid α] : unique_factorization_monoid (associates α) := { irreducible_iff_prime := by { rw ← associates.irreducible_iff_prime_iff, apply unique_factorization_monoid.irreducible_iff_prime, } .. (wf_dvd_monoid.wf_dvd_monoid_associates : wf_dvd_monoid (associates α)) } end prio namespace unique_factorization_monoid variables [comm_cancel_monoid_with_zero α] [unique_factorization_monoid α] theorem exists_prime_factors (a : α) : a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, prime b) ∧ f.prod ~ᵤ a := by { simp_rw ← unique_factorization_monoid.irreducible_iff_prime, apply wf_dvd_monoid.exists_factors a } @[elab_as_eliminator] lemma induction_on_prime {P : α → Prop} (a : α) (h₁ : P 0) (h₂ : ∀ x : α, is_unit x → P x) (h₃ : ∀ a p : α, a ≠ 0 → prime p → P a → P (p * a)) : P a := begin simp_rw ← unique_factorization_monoid.irreducible_iff_prime at h₃, exact wf_dvd_monoid.induction_on_irreducible a h₁ h₂ h₃, end lemma factors_unique : ∀{f g : multiset α}, (∀x∈f, irreducible x) → (∀x∈g, irreducible x) → f.prod ~ᵤ g.prod → multiset.rel associated f g := by haveI := classical.dec_eq α; exact λ f, multiset.induction_on f (λ g _ hg h, multiset.rel_zero_left.2 $ multiset.eq_zero_of_forall_not_mem (λ x hx, have is_unit g.prod, by simpa [associated_one_iff_is_unit] using h.symm, (hg x hx).not_unit (is_unit_iff_dvd_one.2 (dvd.trans (multiset.dvd_prod hx) (is_unit_iff_dvd_one.1 this))))) (λ p f ih g hf hg hfg, let ⟨b, hbg, hb⟩ := exists_associated_mem_of_dvd_prod (irreducible_iff_prime.1 (hf p (by simp))) (λ q hq, irreducible_iff_prime.1 (hg _ hq)) $ (dvd_iff_dvd_of_rel_right hfg).1 (show p ∣ (p ::ₘ f).prod, by simp) in begin rw ← multiset.cons_erase hbg, exact multiset.rel.cons hb (ih (λ q hq, hf _ (by simp [hq])) (λ q (hq : q ∈ g.erase b), hg q (multiset.mem_of_mem_erase hq)) (associated_mul_left_cancel (by rwa [← multiset.prod_cons, ← multiset.prod_cons, multiset.cons_erase hbg]) hb (hf p (by simp)).ne_zero)) end) end unique_factorization_monoid lemma prime_factors_unique [comm_cancel_monoid_with_zero α] : ∀ {f g : multiset α}, (∀ x ∈ f, prime x) → (∀ x ∈ g, prime x) → f.prod ~ᵤ g.prod → multiset.rel associated f g := by haveI := classical.dec_eq α; exact λ f, multiset.induction_on f (λ g _ hg h, multiset.rel_zero_left.2 $ multiset.eq_zero_of_forall_not_mem $ λ x hx, have is_unit g.prod, by simpa [associated_one_iff_is_unit] using h.symm, (irreducible_of_prime $ hg x hx).not_unit $ is_unit_iff_dvd_one.2 $ dvd.trans (multiset.dvd_prod hx) (is_unit_iff_dvd_one.1 this)) (λ p f ih g hf hg hfg, let ⟨b, hbg, hb⟩ := exists_associated_mem_of_dvd_prod (hf p (by simp)) (λ q hq, hg _ hq) $ (dvd_iff_dvd_of_rel_right hfg).1 (show p ∣ (p ::ₘ f).prod, by simp) in begin rw ← multiset.cons_erase hbg, exact multiset.rel.cons hb (ih (λ q hq, hf _ (by simp [hq])) (λ q (hq : q ∈ g.erase b), hg q (multiset.mem_of_mem_erase hq)) (associated_mul_left_cancel (by rwa [← multiset.prod_cons, ← multiset.prod_cons, multiset.cons_erase hbg]) hb (hf p (by simp)).ne_zero)) end) /-- If an irreducible has a prime factorization, then it is an associate of one of its prime factors. -/ lemma prime_factors_irreducible [comm_cancel_monoid_with_zero α] {a : α} {f : multiset α} (ha : irreducible a) (pfa : (∀b ∈ f, prime b) ∧ f.prod ~ᵤ a) : ∃ p, a ~ᵤ p ∧ f = p ::ₘ 0 := begin haveI := classical.dec_eq α, refine multiset.induction_on f (λ h, (ha.not_unit (associated_one_iff_is_unit.1 (associated.symm h))).elim) _ pfa.2 pfa.1, rintros p s _ ⟨u, hu⟩ hs, use p, have hs0 : s = 0, { by_contra hs0, obtain ⟨q, hq⟩ := multiset.exists_mem_of_ne_zero hs0, apply (hs q (by simp [hq])).2.1, refine (ha.is_unit_or_is_unit (_ : _ = ((p * ↑u) * (s.erase q).prod) * _)).resolve_left _, { rw [mul_right_comm _ _ q, mul_assoc, ← multiset.prod_cons, multiset.cons_erase hq, ← hu, mul_comm, mul_comm p _, mul_assoc], simp, }, apply mt is_unit_of_mul_is_unit_left (mt is_unit_of_mul_is_unit_left _), apply (hs p (multiset.mem_cons_self _ _)).2.1 }, simp only [mul_one, multiset.prod_cons, multiset.prod_zero, hs0] at *, exact ⟨associated.symm ⟨u, hu⟩, rfl⟩, end section exists_prime_factors variables [comm_cancel_monoid_with_zero α] variables (pf : ∀ (a : α), a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, prime b) ∧ f.prod ~ᵤ a) include pf lemma wf_dvd_monoid.of_exists_prime_factors : wf_dvd_monoid α := ⟨begin classical, apply rel_hom.well_founded (rel_hom.mk _ _) (with_top.well_founded_lt nat.lt_wf), { intro a, by_cases h : a = 0, { exact ⊤ }, exact (classical.some (pf a h)).card }, rintros a b ⟨ane0, ⟨c, hc, b_eq⟩⟩, rw dif_neg ane0, by_cases h : b = 0, { simp [h, lt_top_iff_ne_top] }, rw [dif_neg h, with_top.coe_lt_coe], have cne0 : c ≠ 0, { refine mt (λ con, _) h, rw [b_eq, con, mul_zero] }, calc multiset.card (classical.some (pf a ane0)) < _ + multiset.card (classical.some (pf c cne0)) : lt_add_of_pos_right _ (multiset.card_pos.mpr (λ con, hc (associated_one_iff_is_unit.mp _))) ... = multiset.card (classical.some (pf a ane0) + classical.some (pf c cne0)) : (multiset.card_add _ _).symm ... = multiset.card (classical.some (pf b h)) : multiset.card_eq_card_of_rel (prime_factors_unique _ (classical.some_spec (pf _ h)).1 _), { convert (classical.some_spec (pf c cne0)).2.symm, rw [con, multiset.prod_zero] }, { intros x hadd, rw multiset.mem_add at hadd, cases hadd; apply (classical.some_spec (pf _ _)).1 _ hadd }, { rw multiset.prod_add, transitivity a * c, { apply associated_mul_mul; apply (classical.some_spec (pf _ _)).2 }, { rw ← b_eq, apply (classical.some_spec (pf _ _)).2.symm, } } end⟩ lemma irreducible_iff_prime_of_exists_prime_factors {p : α} : irreducible p ↔ prime p := begin by_cases hp0 : p = 0, { simp [hp0] }, refine ⟨λ h, _, irreducible_of_prime⟩, obtain ⟨f, hf⟩ := pf p hp0, obtain ⟨q, hq, rfl⟩ := prime_factors_irreducible h hf, rw prime_iff_of_associated hq, exact hf.1 q (multiset.mem_cons_self _ _) end theorem unique_factorization_monoid.of_exists_prime_factors : unique_factorization_monoid α := { irreducible_iff_prime := λ _, irreducible_iff_prime_of_exists_prime_factors pf, .. wf_dvd_monoid.of_exists_prime_factors pf } end exists_prime_factors theorem unique_factorization_monoid.iff_exists_prime_factors [comm_cancel_monoid_with_zero α] : unique_factorization_monoid α ↔ (∀ (a : α), a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, prime b) ∧ f.prod ~ᵤ a) := ⟨λ h, @unique_factorization_monoid.exists_prime_factors _ _ h, unique_factorization_monoid.of_exists_prime_factors⟩ theorem irreducible_iff_prime_of_exists_unique_irreducible_factors [comm_cancel_monoid_with_zero α] (eif : ∀ (a : α), a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, irreducible b) ∧ f.prod ~ᵤ a) (uif : ∀ (f g : multiset α), (∀ x ∈ f, irreducible x) → (∀ x ∈ g, irreducible x) → f.prod ~ᵤ g.prod → multiset.rel associated f g) (p : α) : irreducible p ↔ prime p := ⟨by letI := classical.dec_eq α; exact λ hpi, ⟨hpi.ne_zero, hpi.1, λ a b ⟨x, hx⟩, if hab0 : a * b = 0 then (eq_zero_or_eq_zero_of_mul_eq_zero hab0).elim (λ ha0, by simp [ha0]) (λ hb0, by simp [hb0]) else have hx0 : x ≠ 0, from λ hx0, by simp * at *, have ha0 : a ≠ 0, from left_ne_zero_of_mul hab0, have hb0 : b ≠ 0, from right_ne_zero_of_mul hab0, begin cases eif x hx0 with fx hfx, cases eif a ha0 with fa hfa, cases eif b hb0 with fb hfb, have h : multiset.rel associated (p ::ₘ fx) (fa + fb), { apply uif, { exact λ i hi, (multiset.mem_cons.1 hi).elim (λ hip, hip.symm ▸ hpi) (hfx.1 _), }, { exact λ i hi, (multiset.mem_add.1 hi).elim (hfa.1 _) (hfb.1 _), }, calc multiset.prod (p ::ₘ fx) ~ᵤ a * b : by rw [hx, multiset.prod_cons]; exact associated_mul_mul (by refl) hfx.2 ... ~ᵤ (fa).prod * (fb).prod : associated_mul_mul hfa.2.symm hfb.2.symm ... = _ : by rw multiset.prod_add, }, exact let ⟨q, hqf, hq⟩ := multiset.exists_mem_of_rel_of_mem h (multiset.mem_cons_self p _) in (multiset.mem_add.1 hqf).elim (λ hqa, or.inl $ (dvd_iff_dvd_of_rel_left hq).2 $ (dvd_iff_dvd_of_rel_right hfa.2).1 (multiset.dvd_prod hqa)) (λ hqb, or.inr $ (dvd_iff_dvd_of_rel_left hq).2 $ (dvd_iff_dvd_of_rel_right hfb.2).1 (multiset.dvd_prod hqb)) end⟩, irreducible_of_prime⟩ theorem unique_factorization_monoid.of_exists_unique_irreducible_factors [comm_cancel_monoid_with_zero α] (eif : ∀ (a : α), a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, irreducible b) ∧ f.prod ~ᵤ a) (uif : ∀ (f g : multiset α), (∀ x ∈ f, irreducible x) → (∀ x ∈ g, irreducible x) → f.prod ~ᵤ g.prod → multiset.rel associated f g) : unique_factorization_monoid α := unique_factorization_monoid.of_exists_prime_factors (by { convert eif, simp_rw irreducible_iff_prime_of_exists_unique_irreducible_factors eif uif }) namespace unique_factorization_monoid variables [comm_cancel_monoid_with_zero α] [decidable_eq α] [nontrivial α] [normalization_monoid α] variables [unique_factorization_monoid α] /-- Noncomputably determines the multiset of prime factors. -/ noncomputable def factors (a : α) : multiset α := if h : a = 0 then 0 else multiset.map normalize $ classical.some (unique_factorization_monoid.exists_prime_factors a h) theorem factors_prod {a : α} (ane0 : a ≠ 0) : associated (factors a).prod a := begin rw [factors, dif_neg ane0], refine associated.trans _ (classical.some_spec (exists_prime_factors a ane0)).2, rw [← associates.mk_eq_mk_iff_associated, ← associates.prod_mk, ← associates.prod_mk, multiset.map_map], congr' 2, ext, rw [function.comp_apply, associates.mk_normalize], end theorem prime_of_factor {a : α} : ∀ (x : α), x ∈ factors a → prime x := begin rw [factors], split_ifs with ane0, { simp }, intros x hx, rcases multiset.mem_map.1 hx with ⟨y, ⟨hy, rfl⟩⟩, rw prime_iff_of_associated (normalize_associated), exact (classical.some_spec (unique_factorization_monoid.exists_prime_factors a ane0)).1 y hy, end theorem irreducible_of_factor {a : α} : ∀ (x : α), x ∈ factors a → irreducible x := λ x h, irreducible_of_prime (prime_of_factor x h) theorem normalize_factor {a : α} : ∀ (x : α), x ∈ factors a → normalize x = x := begin rw factors, split_ifs with h, { simp }, intros x hx, obtain ⟨y, hy, rfl⟩ := multiset.mem_map.1 hx, apply normalize_idem end lemma factors_irreducible {a : α} (ha : irreducible a) : factors a = normalize a ::ₘ 0 := begin obtain ⟨p, a_assoc, hp⟩ := prime_factors_irreducible ha ⟨prime_of_factor, factors_prod ha.ne_zero⟩, have p_mem : p ∈ factors a, { rw hp, apply multiset.mem_cons_self }, convert hp, rwa [← normalize_factor p p_mem, normalize_eq_normalize_iff, dvd_dvd_iff_associated] end lemma exists_mem_factors_of_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) : p ∣ a → ∃ q ∈ factors a, p ~ᵤ q := λ ⟨b, hb⟩, have hb0 : b ≠ 0, from λ hb0, by simp * at *, have multiset.rel associated (p ::ₘ factors b) (factors a), from factors_unique (λ x hx, (multiset.mem_cons.1 hx).elim (λ h, h.symm ▸ hp) (irreducible_of_factor _)) irreducible_of_factor (associated.symm $ calc multiset.prod (factors a) ~ᵤ a : factors_prod ha0 ... = p * b : hb ... ~ᵤ multiset.prod (p ::ₘ factors b) : by rw multiset.prod_cons; exact associated_mul_mul (associated.refl _) (associated.symm (factors_prod hb0))), multiset.exists_mem_of_rel_of_mem this (by simp) @[simp] lemma factors_zero : factors (0 : α) = 0 := dif_pos rfl @[simp] lemma factors_one : factors (1 : α) = 0 := begin rw ← multiset.rel_zero_right, apply factors_unique irreducible_of_factor, { intros x hx, exfalso, apply multiset.not_mem_zero x hx }, { simp [factors_prod (@one_ne_zero α _ _)] }, apply_instance end @[simp] lemma factors_mul {x y : α} (hx : x ≠ 0) (hy : y ≠ 0) : factors (x * y) = factors x + factors y := begin have h : (normalize : α → α) = associates.out ∘ associates.mk, { ext, rw [function.comp_apply, associates.out_mk], }, rw [← multiset.map_id' (factors (x * y)), ← multiset.map_id' (factors x), ← multiset.map_id' (factors y), ← multiset.map_congr normalize_factor, ← multiset.map_congr normalize_factor, ← multiset.map_congr normalize_factor, ← multiset.map_add, h, ← multiset.map_map associates.out, eq_comm, ← multiset.map_map associates.out], refine congr rfl _, apply multiset.map_mk_eq_map_mk_of_rel, apply factors_unique, { intros x hx, rcases multiset.mem_add.1 hx with hx | hx; exact irreducible_of_factor x hx }, { exact irreducible_of_factor }, { rw multiset.prod_add, exact associated.trans (associated_mul_mul (factors_prod hx) (factors_prod hy)) (factors_prod (mul_ne_zero hx hy)).symm, } end @[simp] lemma factors_pow {x : α} (n : ℕ) : factors (x ^ n) = n • factors x := begin induction n with n ih, { simp }, by_cases h0 : x = 0, { simp [h0, zero_pow n.succ_pos, smul_zero] }, rw [pow_succ, succ_nsmul, factors_mul h0 (pow_ne_zero _ h0), ih], end lemma dvd_iff_factors_le_factors {x y : α} (hx : x ≠ 0) (hy : y ≠ 0) : x ∣ y ↔ factors x ≤ factors y := begin split, { rintro ⟨c, rfl⟩, simp [hx, right_ne_zero_of_mul hy] }, { rw [← dvd_iff_dvd_of_rel_left (factors_prod hx), ← dvd_iff_dvd_of_rel_right (factors_prod hy)], apply multiset.prod_dvd_prod } end end unique_factorization_monoid namespace unique_factorization_monoid open_locale classical open multiset associates noncomputable theory variables [comm_cancel_monoid_with_zero α] [nontrivial α] [unique_factorization_monoid α] /-- Noncomputably defines a `normalization_monoid` structure on a `unique_factorization_monoid`. -/ protected def normalization_monoid : normalization_monoid α := normalization_monoid_of_monoid_hom_right_inverse { to_fun := λ a : associates α, if a = 0 then 0 else ((factors a).map (classical.some mk_surjective.has_right_inverse : associates α → α)).prod, map_one' := by simp, map_mul' := λ x y, by { by_cases hx : x = 0, { simp [hx] }, by_cases hy : y = 0, { simp [hy] }, simp [hx, hy] } } begin intro x, dsimp, by_cases hx : x = 0, { simp [hx] }, have h : associates.mk_monoid_hom ∘ (classical.some mk_surjective.has_right_inverse) = (id : associates α → associates α), { ext x, rw [function.comp_apply, mk_monoid_hom_apply, classical.some_spec mk_surjective.has_right_inverse x], refl }, rw [if_neg hx, ← mk_monoid_hom_apply, monoid_hom.map_multiset_prod, map_map, h, map_id, ← associated_iff_eq], apply factors_prod hx end instance : inhabited (normalization_monoid α) := ⟨unique_factorization_monoid.normalization_monoid⟩ end unique_factorization_monoid namespace unique_factorization_monoid variables {R : Type*} [comm_cancel_monoid_with_zero R] [unique_factorization_monoid R] lemma no_factors_of_no_prime_factors {a b : R} (ha : a ≠ 0) (h : (∀ {d}, d ∣ a → d ∣ b → ¬ prime d)) : ∀ {d}, d ∣ a → d ∣ b → is_unit d := λ d, induction_on_prime d (by { simp only [zero_dvd_iff], intros, contradiction }) (λ x hx _ _, hx) (λ d q hp hq ih dvd_a dvd_b, absurd hq (h (dvd_of_mul_right_dvd dvd_a) (dvd_of_mul_right_dvd dvd_b))) /-- Euclid's lemma: if `a ∣ b * c` and `a` and `c` have no common prime factors, `a ∣ b`. Compare `is_coprime.dvd_of_dvd_mul_left`. -/ lemma dvd_of_dvd_mul_left_of_no_prime_factors {a b c : R} (ha : a ≠ 0) : (∀ {d}, d ∣ a → d ∣ c → ¬ prime d) → a ∣ b * c → a ∣ b := begin refine induction_on_prime c _ _ _, { intro no_factors, simp only [dvd_zero, mul_zero, forall_prop_of_true], haveI := classical.prop_decidable, exact is_unit_iff_forall_dvd.mp (no_factors_of_no_prime_factors ha @no_factors (dvd_refl a) (dvd_zero a)) _ }, { rintros _ ⟨x, rfl⟩ _ a_dvd_bx, apply units.dvd_mul_right.mp a_dvd_bx }, { intros c p hc hp ih no_factors a_dvd_bpc, apply ih (λ q dvd_a dvd_c hq, no_factors dvd_a (dvd_mul_of_dvd_right dvd_c _) hq), rw mul_left_comm at a_dvd_bpc, refine or.resolve_left (left_dvd_or_dvd_right_of_dvd_prime_mul hp a_dvd_bpc) (λ h, _), exact no_factors h (dvd_mul_right p c) hp } end /-- Euclid's lemma: if `a ∣ b * c` and `a` and `b` have no common prime factors, `a ∣ c`. Compare `is_coprime.dvd_of_dvd_mul_right`. -/ lemma dvd_of_dvd_mul_right_of_no_prime_factors {a b c : R} (ha : a ≠ 0) (no_factors : ∀ {d}, d ∣ a → d ∣ b → ¬ prime d) : a ∣ b * c → a ∣ c := by simpa [mul_comm b c] using dvd_of_dvd_mul_left_of_no_prime_factors ha @no_factors /-- If `a ≠ 0, b` are elements of a unique factorization domain, then dividing out their common factor `c'` gives `a'` and `b'` with no factors in common. -/ lemma exists_reduced_factors : ∀ (a ≠ (0 : R)) b, ∃ a' b' c', (∀ {d}, d ∣ a' → d ∣ b' → is_unit d) ∧ c' * a' = a ∧ c' * b' = b := begin haveI := classical.prop_decidable, intros a, refine induction_on_prime a _ _ _, { intros, contradiction }, { intros a a_unit a_ne_zero b, use [a, b, 1], split, { intros p p_dvd_a _, exact is_unit_of_dvd_unit p_dvd_a a_unit }, { simp } }, { intros a p a_ne_zero p_prime ih_a pa_ne_zero b, by_cases p ∣ b, { rcases h with ⟨b, rfl⟩, obtain ⟨a', b', c', no_factor, ha', hb'⟩ := ih_a a_ne_zero b, refine ⟨a', b', p * c', @no_factor, _, _⟩, { rw [mul_assoc, ha'] }, { rw [mul_assoc, hb'] } }, { obtain ⟨a', b', c', coprime, rfl, rfl⟩ := ih_a a_ne_zero b, refine ⟨p * a', b', c', _, mul_left_comm _ _ _, rfl⟩, intros q q_dvd_pa' q_dvd_b', cases left_dvd_or_dvd_right_of_dvd_prime_mul p_prime q_dvd_pa' with p_dvd_q q_dvd_a', { have : p ∣ c' * b' := dvd_mul_of_dvd_right (dvd_trans p_dvd_q q_dvd_b') _, contradiction }, exact coprime q_dvd_a' q_dvd_b' } } end lemma exists_reduced_factors' (a b : R) (hb : b ≠ 0) : ∃ a' b' c', (∀ {d}, d ∣ a' → d ∣ b' → is_unit d) ∧ c' * a' = a ∧ c' * b' = b := let ⟨b', a', c', no_factor, hb, ha⟩ := exists_reduced_factors b hb a in ⟨a', b', c', λ _ hpb hpa, no_factor hpa hpb, ha, hb⟩ section multiplicity variables [nontrivial R] [normalization_monoid R] [decidable_eq R] variables [decidable_rel (has_dvd.dvd : R → R → Prop)] open multiplicity multiset lemma le_multiplicity_iff_repeat_le_factors {a b : R} {n : ℕ} (ha : irreducible a) (hb : b ≠ 0) : ↑n ≤ multiplicity a b ↔ repeat (normalize a) n ≤ factors b := begin rw ← pow_dvd_iff_le_multiplicity, revert b, induction n with n ih, { simp }, intros b hb, split, { rintro ⟨c, rfl⟩, rw [ne.def, pow_succ, mul_assoc, mul_eq_zero, decidable.not_or_iff_and_not] at hb, rw [pow_succ, mul_assoc, factors_mul hb.1 hb.2, repeat_succ, factors_irreducible ha, cons_add, cons_le_cons_iff, zero_add, ← ih hb.2], apply dvd.intro _ rfl }, { rw [multiset.le_iff_exists_add], rintro ⟨u, hu⟩, rw [← dvd_iff_dvd_of_rel_right (factors_prod hb), hu, prod_add, prod_repeat], apply dvd.trans (dvd_of_associated (associated_pow_pow _)) (dvd.intro u.prod rfl), apply associated_normalize } end lemma multiplicity_eq_count_factors {a b : R} (ha : irreducible a) (hb : b ≠ 0) : multiplicity a b = (factors b).count (normalize a) := begin apply le_antisymm, { apply enat.le_of_lt_add_one, rw [← enat.coe_one, ← enat.coe_add, lt_iff_not_ge, ge_iff_le, le_multiplicity_iff_repeat_le_factors ha hb, ← le_count_iff_repeat_le], simp }, rw [le_multiplicity_iff_repeat_le_factors ha hb, ← le_count_iff_repeat_le], end end multiplicity end unique_factorization_monoid namespace associates open unique_factorization_monoid associated multiset variables [comm_cancel_monoid_with_zero α] /-- `factor_set α` representation elements of unique factorization domain as multisets. `multiset α` produced by `factors` are only unique up to associated elements, while the multisets in `factor_set α` are unqiue by equality and restricted to irreducible elements. This gives us a representation of each element as a unique multisets (or the added ⊤ for 0), which has a complete lattice struture. Infimum is the greatest common divisor and supremum is the least common multiple. -/ @[reducible] def {u} factor_set (α : Type u) [comm_cancel_monoid_with_zero α] : Type u := with_top (multiset { a : associates α // irreducible a }) local attribute [instance] associated.setoid theorem factor_set.coe_add {a b : multiset { a : associates α // irreducible a }} : (↑(a + b) : factor_set α) = a + b := by norm_cast lemma factor_set.sup_add_inf_eq_add [decidable_eq (associates α)] : ∀(a b : factor_set α), a ⊔ b + a ⊓ b = a + b | none b := show ⊤ ⊔ b + ⊤ ⊓ b = ⊤ + b, by simp | a none := show a ⊔ ⊤ + a ⊓ ⊤ = a + ⊤, by simp | (some a) (some b) := show (a : factor_set α) ⊔ b + a ⊓ b = a + b, from begin rw [← with_top.coe_sup, ← with_top.coe_inf, ← with_top.coe_add, ← with_top.coe_add, with_top.coe_eq_coe], exact multiset.union_add_inter _ _ end /-- Evaluates the product of a `factor_set` to be the product of the corresponding multiset, or `0` if there is none. -/ def factor_set.prod : factor_set α → associates α | none := 0 | (some s) := (s.map coe).prod @[simp] theorem prod_top : (⊤ : factor_set α).prod = 0 := rfl @[simp] theorem prod_coe {s : multiset { a : associates α // irreducible a }} : (s : factor_set α).prod = (s.map coe).prod := rfl @[simp] theorem prod_add : ∀(a b : factor_set α), (a + b).prod = a.prod * b.prod | none b := show (⊤ + b).prod = (⊤:factor_set α).prod * b.prod, by simp | a none := show (a + ⊤).prod = a.prod * (⊤:factor_set α).prod, by simp | (some a) (some b) := show (↑a + ↑b:factor_set α).prod = (↑a:factor_set α).prod * (↑b:factor_set α).prod, by rw [← factor_set.coe_add, prod_coe, prod_coe, prod_coe, multiset.map_add, multiset.prod_add] theorem prod_mono : ∀{a b : factor_set α}, a ≤ b → a.prod ≤ b.prod | none b h := have b = ⊤, from top_unique h, by rw [this, prod_top]; exact le_refl _ | a none h := show a.prod ≤ (⊤ : factor_set α).prod, by simp; exact le_top | (some a) (some b) h := prod_le_prod $ multiset.map_le_map $ with_top.coe_le_coe.1 $ h /-- `bcount p s` is the multiplicity of `p` in the factor_set `s` (with bundled `p`)-/ def bcount [decidable_eq (associates α)] (p : {a : associates α // irreducible a}) : factor_set α → ℕ | none := 0 | (some s) := s.count p variables [dec_irr : Π (p : associates α), decidable (irreducible p)] include dec_irr /-- `count p s` is the multiplicity of the irreducible `p` in the factor_set `s`. If `p` is not irreducible, `count p s` is defined to be `0`. -/ def count [decidable_eq (associates α)] (p : associates α) : factor_set α → ℕ := if hp : irreducible p then bcount ⟨p, hp⟩ else 0 @[simp] lemma count_some [decidable_eq (associates α)] {p : associates α} (hp : irreducible p) (s : multiset _) : count p (some s) = s.count ⟨p, hp⟩:= by { dunfold count, split_ifs, refl } @[simp] lemma count_zero [decidable_eq (associates α)] {p : associates α} (hp : irreducible p) : count p (0 : factor_set α) = 0 := by { dunfold count, split_ifs, refl } lemma count_reducible [decidable_eq (associates α)] {p : associates α} (hp : ¬ irreducible p) : count p = 0 := dif_neg hp omit dec_irr /-- membership in a factor_set (bundled version) -/ def bfactor_set_mem : {a : associates α // irreducible a} → (factor_set α) → Prop | _ ⊤ := true | p (some l) := p ∈ l include dec_irr /-- `factor_set_mem p s` is the predicate that the irreducible `p` is a member of `s : factor_set α`. If `p` is not irreducible, `p` is not a member of any `factor_set`. -/ def factor_set_mem (p : associates α) (s : factor_set α) : Prop := if hp : irreducible p then bfactor_set_mem ⟨p, hp⟩ s else false instance : has_mem (associates α) (factor_set α) := ⟨factor_set_mem⟩ @[simp] lemma factor_set_mem_eq_mem (p : associates α) (s : factor_set α) : factor_set_mem p s = (p ∈ s) := rfl lemma mem_factor_set_top {p : associates α} {hp : irreducible p} : p ∈ (⊤ : factor_set α) := begin dunfold has_mem.mem, dunfold factor_set_mem, split_ifs, exact trivial end lemma mem_factor_set_some {p : associates α} {hp : irreducible p} {l : multiset {a : associates α // irreducible a }} : p ∈ (l : factor_set α) ↔ subtype.mk p hp ∈ l := begin dunfold has_mem.mem, dunfold factor_set_mem, split_ifs, refl end lemma reducible_not_mem_factor_set {p : associates α} (hp : ¬ irreducible p) (s : factor_set α) : ¬ p ∈ s := λ (h : if hp : irreducible p then bfactor_set_mem ⟨p, hp⟩ s else false), by rwa [dif_neg hp] at h omit dec_irr variable [unique_factorization_monoid α] theorem unique' {p q : multiset (associates α)} : (∀a∈p, irreducible a) → (∀a∈q, irreducible a) → p.prod = q.prod → p = q := begin apply multiset.induction_on_multiset_quot p, apply multiset.induction_on_multiset_quot q, assume s t hs ht eq, refine multiset.map_mk_eq_map_mk_of_rel (unique_factorization_monoid.factors_unique _ _ _), { exact assume a ha, ((irreducible_mk _).1 $ hs _ $ multiset.mem_map_of_mem _ ha) }, { exact assume a ha, ((irreducible_mk _).1 $ ht _ $ multiset.mem_map_of_mem _ ha) }, simpa [quot_mk_eq_mk, prod_mk, mk_eq_mk_iff_associated] using eq end variables [nontrivial α] [normalization_monoid α] private theorem forall_map_mk_factors_irreducible [decidable_eq α] (x : α) (hx : x ≠ 0) : ∀(a : associates α), a ∈ multiset.map associates.mk (factors x) → irreducible a := begin assume a ha, rcases multiset.mem_map.1 ha with ⟨c, hc, rfl⟩, exact (irreducible_mk c).2 (irreducible_of_factor _ hc) end theorem prod_le_prod_iff_le {p q : multiset (associates α)} (hp : ∀a∈p, irreducible a) (hq : ∀a∈q, irreducible a) : p.prod ≤ q.prod ↔ p ≤ q := iff.intro begin classical, rintros ⟨⟨c⟩, eqc⟩, have : c ≠ 0, from (mt mk_eq_zero.2 $ assume (hc : quot.mk setoid.r c = 0), have (0 : associates α) ∈ q, from prod_eq_zero_iff.1 $ eqc.symm ▸ hc.symm ▸ mul_zero _, not_irreducible_zero ((irreducible_mk 0).1 $ hq _ this)), have : associates.mk (factors c).prod = quot.mk setoid.r c, from mk_eq_mk_iff_associated.2 (factors_prod this), refine multiset.le_iff_exists_add.2 ⟨(factors c).map associates.mk, unique' hq _ _⟩, { assume x hx, rcases multiset.mem_add.1 hx with h | h, exact hp x h, exact forall_map_mk_factors_irreducible c ‹c ≠ 0› _ h }, { simp [multiset.prod_add, prod_mk, *] at * } end prod_le_prod variables [dec : decidable_eq α] [dec' : decidable_eq (associates α)] include dec /-- This returns the multiset of irreducible factors as a `factor_set`, a multiset of irreducible associates `with_top`. -/ noncomputable def factors' (a : α) : multiset { a : associates α // irreducible a } := (factors a).pmap (λa ha, ⟨associates.mk a, (irreducible_mk _).2 ha⟩) (irreducible_of_factor) @[simp] theorem map_subtype_coe_factors' {a : α} : (factors' a).map coe = (factors a).map associates.mk := by simp [factors', multiset.map_pmap, multiset.pmap_eq_map] theorem factors'_cong {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) (h : a ~ᵤ b) : factors' a = factors' b := have multiset.rel associated (factors a) (factors b), from factors_unique irreducible_of_factor irreducible_of_factor ((factors_prod ha).trans $ h.trans $ (factors_prod hb).symm), by simpa [(multiset.map_eq_map subtype.coe_injective).symm, rel_associated_iff_map_eq_map.symm] include dec' /-- This returns the multiset of irreducible factors of an associate as a `factor_set`, a multiset of irreducible associates `with_top`. -/ noncomputable def factors (a : associates α) : factor_set α := begin refine (if h : a = 0 then ⊤ else quotient.hrec_on a (λx h, some $ factors' x) _ h), assume a b hab, apply function.hfunext, { have : a ~ᵤ 0 ↔ b ~ᵤ 0, from iff.intro (assume ha0, hab.symm.trans ha0) (assume hb0, hab.trans hb0), simp only [associated_zero_iff_eq_zero] at this, simp only [quotient_mk_eq_mk, this, mk_eq_zero] }, exact (assume ha hb eq, heq_of_eq $ congr_arg some $ factors'_cong (λ c, ha (mk_eq_zero.2 c)) (λ c, hb (mk_eq_zero.2 c)) hab) end @[simp] theorem factors_0 : (0 : associates α).factors = ⊤ := dif_pos rfl @[simp] theorem factors_mk (a : α) (h : a ≠ 0) : (associates.mk a).factors = factors' a := by { classical, apply dif_neg, apply (mt mk_eq_zero.1 h) } theorem prod_factors : ∀(s : factor_set α), s.prod.factors = s | none := by simp [factor_set.prod]; refl | (some s) := begin unfold factor_set.prod, generalize eq_a : (s.map coe).prod = a, rcases a with ⟨a⟩, rw quot_mk_eq_mk at *, have : (s.map (coe : _ → associates α)).prod ≠ 0, from assume ha, let ⟨⟨a, ha⟩, h, eq⟩ := multiset.mem_map.1 (prod_eq_zero_iff.1 ha) in have irreducible (0 : associates α), from eq ▸ ha, not_irreducible_zero ((irreducible_mk _).1 this), have ha : a ≠ 0, by simp [*] at *, suffices : (unique_factorization_monoid.factors a).map associates.mk = s.map coe, { rw [factors_mk a ha], apply congr_arg some _, simpa [(multiset.map_eq_map subtype.coe_injective).symm] }, refine unique' (forall_map_mk_factors_irreducible _ ha) (assume a ha, let ⟨⟨x, hx⟩, ha, eq⟩ := multiset.mem_map.1 ha in eq ▸ hx) _, rw [prod_mk, eq_a, mk_eq_mk_iff_associated], exact factors_prod ha end @[simp] theorem factors_prod (a : associates α) : a.factors.prod = a := quotient.induction_on a $ assume a, decidable.by_cases (assume : associates.mk a = 0, by simp [quotient_mk_eq_mk, this]) (assume : associates.mk a ≠ 0, have a ≠ 0, by simp * at *, by simp [this, quotient_mk_eq_mk, prod_mk, mk_eq_mk_iff_associated.2 (factors_prod this)]) theorem eq_of_factors_eq_factors {a b : associates α} (h : a.factors = b.factors) : a = b := have a.factors.prod = b.factors.prod, by rw h, by rwa [factors_prod, factors_prod] at this omit dec dec' theorem eq_of_prod_eq_prod {a b : factor_set α} (h : a.prod = b.prod) : a = b := begin classical, have : a.prod.factors = b.prod.factors, by rw h, rwa [prod_factors, prod_factors] at this end include dec dec' @[simp] theorem factors_mul (a b : associates α) : (a * b).factors = a.factors + b.factors := eq_of_prod_eq_prod $ eq_of_factors_eq_factors $ by rw [prod_add, factors_prod, factors_prod, factors_prod] theorem factors_mono : ∀{a b : associates α}, a ≤ b → a.factors ≤ b.factors | s t ⟨d, rfl⟩ := by rw [factors_mul] ; exact le_add_of_nonneg_right bot_le theorem factors_le {a b : associates α} : a.factors ≤ b.factors ↔ a ≤ b := iff.intro (assume h, have a.factors.prod ≤ b.factors.prod, from prod_mono h, by rwa [factors_prod, factors_prod] at this) factors_mono omit dec dec' theorem prod_le {a b : factor_set α} : a.prod ≤ b.prod ↔ a ≤ b := begin classical, exact iff.intro (assume h, have a.prod.factors ≤ b.prod.factors, from factors_mono h, by rwa [prod_factors, prod_factors] at this) prod_mono end include dec dec' noncomputable instance : has_sup (associates α) := ⟨λa b, (a.factors ⊔ b.factors).prod⟩ noncomputable instance : has_inf (associates α) := ⟨λa b, (a.factors ⊓ b.factors).prod⟩ noncomputable instance : bounded_lattice (associates α) := { sup := (⊔), inf := (⊓), sup_le := assume a b c hac hbc, factors_prod c ▸ prod_mono (sup_le (factors_mono hac) (factors_mono hbc)), le_sup_left := assume a b, le_trans (le_of_eq (factors_prod a).symm) $ prod_mono $ le_sup_left, le_sup_right := assume a b, le_trans (le_of_eq (factors_prod b).symm) $ prod_mono $ le_sup_right, le_inf := assume a b c hac hbc, factors_prod a ▸ prod_mono (le_inf (factors_mono hac) (factors_mono hbc)), inf_le_left := assume a b, le_trans (prod_mono inf_le_left) (le_of_eq (factors_prod a)), inf_le_right := assume a b, le_trans (prod_mono inf_le_right) (le_of_eq (factors_prod b)), .. associates.partial_order, .. associates.order_top, .. associates.order_bot } lemma sup_mul_inf (a b : associates α) : (a ⊔ b) * (a ⊓ b) = a * b := show (a.factors ⊔ b.factors).prod * (a.factors ⊓ b.factors).prod = a * b, begin refine eq_of_factors_eq_factors _, rw [← prod_add, prod_factors, factors_mul, factor_set.sup_add_inf_eq_add] end include dec_irr lemma dvd_of_mem_factors {a p : associates α} {hp : irreducible p} (hm : p ∈ factors a) : p ∣ a := begin by_cases ha0 : a = 0, { rw ha0, exact dvd_zero p }, obtain ⟨a0, nza, ha'⟩ := exists_non_zero_rep ha0, rw [← associates.factors_prod a], rw [← ha', factors_mk a0 nza] at hm ⊢, erw prod_coe, apply multiset.dvd_prod, apply multiset.mem_map.mpr, exact ⟨⟨p, hp⟩, mem_factor_set_some.mp hm, rfl⟩ end omit dec' lemma dvd_of_mem_factors' {a : α} {p : associates α} {hp : irreducible p} {hz : a ≠ 0} (h_mem : subtype.mk p hp ∈ factors' a) : p ∣ associates.mk a := by { haveI := classical.dec_eq (associates α), apply @dvd_of_mem_factors _ _ _ _ _ _ _ _ _ _ hp, rw factors_mk _ hz, apply mem_factor_set_some.2 h_mem } omit dec_irr lemma mem_factors'_of_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) (hd : p ∣ a) : subtype.mk (associates.mk p) ((irreducible_mk _).2 hp) ∈ factors' a := begin obtain ⟨q, hq, hpq⟩ := exists_mem_factors_of_dvd ha0 hp hd, apply multiset.mem_pmap.mpr, use q, use hq, exact subtype.eq (eq.symm (mk_eq_mk_iff_associated.mpr hpq)) end include dec_irr lemma mem_factors'_iff_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) : subtype.mk (associates.mk p) ((irreducible_mk _).2 hp) ∈ factors' a ↔ p ∣ a := begin split, { rw ← mk_dvd_mk, apply dvd_of_mem_factors', apply ha0 }, { apply mem_factors'_of_dvd ha0 } end include dec' lemma mem_factors_of_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) (hd : p ∣ a) : (associates.mk p) ∈ factors (associates.mk a) := begin rw factors_mk _ ha0, exact mem_factor_set_some.mpr (mem_factors'_of_dvd ha0 hp hd) end lemma mem_factors_iff_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) : (associates.mk p) ∈ factors (associates.mk a) ↔ p ∣ a := begin split, { rw ← mk_dvd_mk, apply dvd_of_mem_factors, exact (irreducible_mk p).mpr hp }, { apply mem_factors_of_dvd ha0 hp } end lemma exists_prime_dvd_of_not_inf_one {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) (h : (associates.mk a) ⊓ (associates.mk b) ≠ 1) : ∃ (p : α), prime p ∧ p ∣ a ∧ p ∣ b := begin have hz : (factors (associates.mk a)) ⊓ (factors (associates.mk b)) ≠ 0, { contrapose! h with hf, change ((factors (associates.mk a)) ⊓ (factors (associates.mk b))).prod = 1, rw hf, exact multiset.prod_zero }, rw [factors_mk a ha, factors_mk b hb, ← with_top.coe_inf] at hz, obtain ⟨⟨p0, p0_irr⟩, p0_mem⟩ := multiset.exists_mem_of_ne_zero ((mt with_top.coe_eq_coe.mpr) hz), rw multiset.inf_eq_inter at p0_mem, obtain ⟨p, rfl⟩ : ∃ p, associates.mk p = p0 := quot.exists_rep p0, refine ⟨p, _, _, _⟩, { rw [← irreducible_iff_prime, ← irreducible_mk], exact p0_irr }, { apply dvd_of_mk_le_mk, apply dvd_of_mem_factors' (multiset.mem_inter.mp p0_mem).left, apply ha, }, { apply dvd_of_mk_le_mk, apply dvd_of_mem_factors' (multiset.mem_inter.mp p0_mem).right, apply hb } end theorem coprime_iff_inf_one {a b : α} (ha0 : a ≠ 0) (hb0 : b ≠ 0) : (associates.mk a) ⊓ (associates.mk b) = 1 ↔ ∀ {d : α}, d ∣ a → d ∣ b → ¬ prime d := begin split, { intros hg p ha hb hp, refine ((associates.prime_mk _).mpr hp).not_unit (is_unit_of_dvd_one _ _), rw ← hg, exact le_inf (mk_le_mk_of_dvd ha) (mk_le_mk_of_dvd hb) }, { contrapose, intros hg hc, obtain ⟨p, hp, hpa, hpb⟩ := exists_prime_dvd_of_not_inf_one ha0 hb0 hg, exact hc hpa hpb hp } end omit dec_irr theorem factors_prime_pow {p : associates α} (hp : irreducible p) (k : ℕ) : factors (p ^ k) = some (multiset.repeat ⟨p, hp⟩ k) := eq_of_prod_eq_prod (by rw [associates.factors_prod, factor_set.prod, multiset.map_repeat, multiset.prod_repeat, subtype.coe_mk]) include dec_irr theorem prime_pow_dvd_iff_le {m p : associates α} (h₁ : m ≠ 0) (h₂ : irreducible p) {k : ℕ} : p ^ k ≤ m ↔ k ≤ count p m.factors := begin obtain ⟨a, nz, rfl⟩ := associates.exists_non_zero_rep h₁, rw [factors_mk _ nz, ← with_top.some_eq_coe, count_some, multiset.le_count_iff_repeat_le, ← factors_le, factors_prime_pow h₂, factors_mk _ nz], exact with_top.coe_le_coe end theorem le_of_count_ne_zero {m p : associates α} (h0 : m ≠ 0) (hp : irreducible p) : count p m.factors ≠ 0 → p ≤ m := begin rw [← pos_iff_ne_zero], intro h, rw [← pow_one p], apply (prime_pow_dvd_iff_le h0 hp).2, simpa only end theorem count_mul {a : associates α} (ha : a ≠ 0) {b : associates α} (hb : b ≠ 0) {p : associates α} (hp : irreducible p) : count p (factors (a * b)) = count p a.factors + count p b.factors := begin obtain ⟨a0, nza, ha'⟩ := exists_non_zero_rep ha, obtain ⟨b0, nzb, hb'⟩ := exists_non_zero_rep hb, rw [factors_mul, ← ha', ← hb', factors_mk a0 nza, factors_mk b0 nzb, ← factor_set.coe_add, ← with_top.some_eq_coe, ← with_top.some_eq_coe, ← with_top.some_eq_coe, count_some hp, multiset.count_add, count_some hp, count_some hp] end theorem count_of_coprime {a : associates α} (ha : a ≠ 0) {b : associates α} (hb : b ≠ 0) (hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) {p : associates α} (hp : irreducible p) : count p a.factors = 0 ∨ count p b.factors = 0 := begin rw [or_iff_not_imp_left, ← ne.def], intro hca, contrapose! hab with hcb, exact ⟨p, le_of_count_ne_zero ha hp hca, le_of_count_ne_zero hb hp hcb, (irreducible_iff_prime.mp hp)⟩, end theorem count_mul_of_coprime {a : associates α} (ha : a ≠ 0) {b : associates α} (hb : b ≠ 0) {p : associates α} (hp : irreducible p) (hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) : count p a.factors = 0 ∨ count p a.factors = count p (a * b).factors := begin cases count_of_coprime ha hb hab hp with hz hb0, { tauto }, apply or.intro_right, rw [count_mul ha hb hp, hb0, add_zero] end theorem count_mul_of_coprime' {a : associates α} (ha : a ≠ 0) {b : associates α} (hb : b ≠ 0) {p : associates α} (hp : irreducible p) (hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) : count p (a * b).factors = count p a.factors ∨ count p (a * b).factors = count p b.factors := begin rw [count_mul ha hb hp], cases count_of_coprime ha hb hab hp with ha0 hb0, { apply or.intro_right, rw [ha0, zero_add] }, { apply or.intro_left, rw [hb0, add_zero] } end theorem dvd_count_of_dvd_count_mul {a b : associates α} (ha : a ≠ 0) (hb : b ≠ 0) {p : associates α} (hp : irreducible p) (hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) {k : ℕ} (habk : k ∣ count p (a * b).factors) : k ∣ count p a.factors := begin cases count_of_coprime ha hb hab hp with hz h, { rw hz, exact dvd_zero k }, { rw [count_mul ha hb hp, h] at habk, exact habk } end omit dec_irr @[simp] lemma factors_one : factors (1 : associates α) = 0 := begin apply eq_of_prod_eq_prod, rw associates.factors_prod, exact multiset.prod_zero, end @[simp] theorem pow_factors {a : associates α} {k : ℕ} : (a ^ k).factors = k • a.factors := begin induction k with n h, { rw [zero_nsmul, pow_zero], exact factors_one }, { rw [pow_succ, succ_nsmul, factors_mul, h] } end include dec_irr lemma count_pow {a : associates α} (ha : a ≠ 0) {p : associates α} (hp : irreducible p) (k : ℕ) : count p (a ^ k).factors = k * count p a.factors := begin induction k with n h, { rw [pow_zero, factors_one, zero_mul, count_zero hp] }, { rw [pow_succ, count_mul ha (pow_ne_zero _ ha) hp, h, nat.succ_eq_add_one], ring } end theorem dvd_count_pow {a : associates α} (ha : a ≠ 0) {p : associates α} (hp : irreducible p) (k : ℕ) : k ∣ count p (a ^ k).factors := by { rw count_pow ha hp, apply dvd_mul_right } theorem is_pow_of_dvd_count {a : associates α} (ha : a ≠ 0) {k : ℕ} (hk : ∀ (p : associates α) (hp : irreducible p), k ∣ count p a.factors) : ∃ (b : associates α), a = b ^ k := begin obtain ⟨a0, hz, rfl⟩ := exists_non_zero_rep ha, rw [factors_mk a0 hz] at hk, have hk' : ∀ (p : {a : associates α // irreducible a}), k ∣ (factors' a0).count p, { intro p, have pp : p = ⟨p.val, p.2⟩, { simp only [subtype.coe_eta, subtype.val_eq_coe] }, rw [pp, ← count_some p.2], exact hk p.val p.2 }, obtain ⟨u, hu⟩ := multiset.exists_smul_of_dvd_count _ hk', use (u : factor_set α).prod, apply eq_of_factors_eq_factors, rw [pow_factors, prod_factors, factors_mk a0 hz, ← with_top.some_eq_coe, hu], exact with_bot.coe_nsmul u k end omit dec omit dec_irr omit dec' theorem eq_pow_of_mul_eq_pow {a b c : associates α} (ha : a ≠ 0) (hb : b ≠ 0) (hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) {k : ℕ} (h : a * b = c ^ k) : ∃ (d : associates α), a = d ^ k := begin classical, by_cases hk0 : k = 0, { use 1, rw [hk0, pow_zero] at h ⊢, apply (mul_eq_one_iff.1 h).1 }, { refine is_pow_of_dvd_count ha _, intros p hp, apply dvd_count_of_dvd_count_mul ha hb hp hab, rw h, apply dvd_count_pow _ hp, rintros rfl, rw zero_pow' _ hk0 at h, cases mul_eq_zero.mp h; contradiction } end end associates section open associates unique_factorization_monoid /-- `to_gcd_monoid` constructs a GCD monoid out of a normalization on a unique factorization domain. -/ noncomputable def unique_factorization_monoid.to_gcd_monoid (α : Type*) [comm_cancel_monoid_with_zero α] [nontrivial α] [unique_factorization_monoid α] [normalization_monoid α] [decidable_eq (associates α)] [decidable_eq α] : gcd_monoid α := { gcd := λa b, (associates.mk a ⊓ associates.mk b).out, lcm := λa b, (associates.mk a ⊔ associates.mk b).out, gcd_dvd_left := assume a b, (out_dvd_iff a (associates.mk a ⊓ associates.mk b)).2 $ inf_le_left, gcd_dvd_right := assume a b, (out_dvd_iff b (associates.mk a ⊓ associates.mk b)).2 $ inf_le_right, dvd_gcd := assume a b c hac hab, show a ∣ (associates.mk c ⊓ associates.mk b).out, by rw [dvd_out_iff, le_inf_iff, mk_le_mk_iff_dvd_iff, mk_le_mk_iff_dvd_iff]; exact ⟨hac, hab⟩, lcm_zero_left := assume a, show (⊤ ⊔ associates.mk a).out = 0, by simp, lcm_zero_right := assume a, show (associates.mk a ⊔ ⊤).out = 0, by simp, gcd_mul_lcm := assume a b, show (associates.mk a ⊓ associates.mk b).out * (associates.mk a ⊔ associates.mk b).out = normalize (a * b), by rw [← out_mk, ← out_mul, mul_comm, sup_mul_inf]; refl, normalize_gcd := assume a b, by convert normalize_out _, .. ‹normalization_monoid α› } end
499e674bd17879ccdc03de1d2c938d1d4b264c5d
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/order/filter/prod.lean
b6dc75e02a0d8b6400cf7912d236fb4bc4e4dc5e
[ "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
22,230
lean
/- Copyright (c) 2022 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johanes Hölzl, Patrick Massot, Yury Kudryashov, Kevin Wilson, Heather Macbeth -/ import order.filter.basic /-! # Product and coproduct filters > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we define `filter.prod f g` (notation: `f ×ᶠ g`) and `filter.coprod f g`. The product of two filters is the largest filter `l` such that `filter.tendsto prod.fst l f` and `filter.tendsto prod.snd l g`. ## Implementation details The product filter cannot be defined using the monad structure on filters. For example: ```lean F := do {x ← seq, y ← top, return (x, y)} G := do {y ← top, x ← seq, return (x, y)} ``` hence: ```lean s ∈ F ↔ ∃ n, [n..∞] × univ ⊆ s s ∈ G ↔ ∀ i:ℕ, ∃ n, [n..∞] × {i} ⊆ s ``` Now `⋃ i, [i..∞] × {i}` is in `G` but not in `F`. As product filter we want to have `F` as result. ## Notations * `f ×ᶠ g` : `filter.prod f g`, localized in `filter`. -/ open set open_locale filter namespace filter variables {α β γ δ : Type*} {ι : Sort*} section prod variables {s : set α} {t : set β} {f : filter α} {g : filter β} /-- Product of filters. This is the filter generated by cartesian products of elements of the component filters. -/ protected def prod (f : filter α) (g : filter β) : filter (α × β) := f.comap prod.fst ⊓ g.comap prod.snd localized "infix (name := filter.prod) ` ×ᶠ `:60 := filter.prod" in filter lemma prod_mem_prod {s : set α} {t : set β} {f : filter α} {g : filter β} (hs : s ∈ f) (ht : t ∈ g) : s ×ˢ t ∈ f ×ᶠ g := inter_mem_inf (preimage_mem_comap hs) (preimage_mem_comap ht) lemma mem_prod_iff {s : set (α×β)} {f : filter α} {g : filter β} : s ∈ f ×ᶠ g ↔ (∃ t₁ ∈ f, ∃ t₂ ∈ g, t₁ ×ˢ t₂ ⊆ s) := begin simp only [filter.prod], split, { rintro ⟨t₁, ⟨s₁, hs₁, hts₁⟩, t₂, ⟨s₂, hs₂, hts₂⟩, rfl⟩, exact ⟨s₁, hs₁, s₂, hs₂, λ p ⟨h, h'⟩, ⟨hts₁ h, hts₂ h'⟩⟩ }, { rintro ⟨t₁, ht₁, t₂, ht₂, h⟩, exact mem_inf_of_inter (preimage_mem_comap ht₁) (preimage_mem_comap ht₂) h } end @[simp] lemma prod_mem_prod_iff {s : set α} {t : set β} {f : filter α} {g : filter β} [f.ne_bot] [g.ne_bot] : s ×ˢ t ∈ f ×ᶠ g ↔ s ∈ f ∧ t ∈ g := ⟨λ h, let ⟨s', hs', t', ht', H⟩ := mem_prod_iff.1 h in (prod_subset_prod_iff.1 H).elim (λ ⟨hs's, ht't⟩, ⟨mem_of_superset hs' hs's, mem_of_superset ht' ht't⟩) (λ h, h.elim (λ hs'e, absurd hs'e (nonempty_of_mem hs').ne_empty) (λ ht'e, absurd ht'e (nonempty_of_mem ht').ne_empty)), λ h, prod_mem_prod h.1 h.2⟩ lemma mem_prod_principal {f : filter α} {s : set (α × β)} {t : set β}: s ∈ f ×ᶠ 𝓟 t ↔ {a | ∀ b ∈ t, (a, b) ∈ s} ∈ f := begin rw [← @exists_mem_subset_iff _ f, mem_prod_iff], refine exists₂_congr (λ u u_in, ⟨_, λ h, ⟨t, mem_principal_self t, _⟩⟩), { rintros ⟨v, v_in, hv⟩ a a_in b b_in, exact hv (mk_mem_prod a_in $ v_in b_in) }, { rintro ⟨x, y⟩ ⟨hx, hy⟩, exact h hx y hy } end lemma mem_prod_top {f : filter α} {s : set (α × β)} : s ∈ f ×ᶠ (⊤ : filter β) ↔ {a | ∀ b, (a, b) ∈ s} ∈ f := begin rw [← principal_univ, mem_prod_principal], simp only [mem_univ, forall_true_left] end lemma eventually_prod_principal_iff {p : α × β → Prop} {s : set β} : (∀ᶠ (x : α × β) in (f ×ᶠ (𝓟 s)), p x) ↔ ∀ᶠ (x : α) in f, ∀ (y : β), y ∈ s → p (x, y) := by { rw [eventually_iff, eventually_iff, mem_prod_principal], simp only [mem_set_of_eq], } lemma comap_prod (f : α → β × γ) (b : filter β) (c : filter γ) : comap f (b ×ᶠ c) = (comap (prod.fst ∘ f) b) ⊓ (comap (prod.snd ∘ f) c) := by erw [comap_inf, filter.comap_comap, filter.comap_comap] lemma prod_top {f : filter α} : f ×ᶠ (⊤ : filter β) = f.comap prod.fst := by rw [filter.prod, comap_top, inf_top_eq] lemma sup_prod (f₁ f₂ : filter α) (g : filter β) : (f₁ ⊔ f₂) ×ᶠ g = (f₁ ×ᶠ g) ⊔ (f₂ ×ᶠ g) := by rw [filter.prod, comap_sup, inf_sup_right, ← filter.prod, ← filter.prod] lemma prod_sup (f : filter α) (g₁ g₂ : filter β) : f ×ᶠ (g₁ ⊔ g₂) = (f ×ᶠ g₁) ⊔ (f ×ᶠ g₂) := by rw [filter.prod, comap_sup, inf_sup_left, ← filter.prod, ← filter.prod] lemma eventually_prod_iff {p : α × β → Prop} {f : filter α} {g : filter β} : (∀ᶠ x in f ×ᶠ g, p x) ↔ ∃ (pa : α → Prop) (ha : ∀ᶠ x in f, pa x) (pb : β → Prop) (hb : ∀ᶠ y in g, pb y), ∀ {x}, pa x → ∀ {y}, pb y → p (x, y) := by simpa only [set.prod_subset_iff] using @mem_prod_iff α β p f g lemma tendsto_fst {f : filter α} {g : filter β} : tendsto prod.fst (f ×ᶠ g) f := tendsto_inf_left tendsto_comap lemma tendsto_snd {f : filter α} {g : filter β} : tendsto prod.snd (f ×ᶠ g) g := tendsto_inf_right tendsto_comap lemma tendsto.prod_mk {f : filter α} {g : filter β} {h : filter γ} {m₁ : α → β} {m₂ : α → γ} (h₁ : tendsto m₁ f g) (h₂ : tendsto m₂ f h) : tendsto (λ x, (m₁ x, m₂ x)) f (g ×ᶠ h) := tendsto_inf.2 ⟨tendsto_comap_iff.2 h₁, tendsto_comap_iff.2 h₂⟩ lemma tendsto_prod_swap {α1 α2 : Type*} {a1 : filter α1} {a2 : filter α2} : tendsto (prod.swap : α1 × α2 → α2 × α1) (a1 ×ᶠ a2) (a2 ×ᶠ a1) := tendsto_snd.prod_mk tendsto_fst lemma eventually.prod_inl {la : filter α} {p : α → Prop} (h : ∀ᶠ x in la, p x) (lb : filter β) : ∀ᶠ x in la ×ᶠ lb, p (x : α × β).1 := tendsto_fst.eventually h lemma eventually.prod_inr {lb : filter β} {p : β → Prop} (h : ∀ᶠ x in lb, p x) (la : filter α) : ∀ᶠ x in la ×ᶠ lb, p (x : α × β).2 := tendsto_snd.eventually h lemma eventually.prod_mk {la : filter α} {pa : α → Prop} (ha : ∀ᶠ x in la, pa x) {lb : filter β} {pb : β → Prop} (hb : ∀ᶠ y in lb, pb y) : ∀ᶠ p in la ×ᶠ lb, pa (p : α × β).1 ∧ pb p.2 := (ha.prod_inl lb).and (hb.prod_inr la) lemma eventually_eq.prod_map {δ} {la : filter α} {fa ga : α → γ} (ha : fa =ᶠ[la] ga) {lb : filter β} {fb gb : β → δ} (hb : fb =ᶠ[lb] gb) : prod.map fa fb =ᶠ[la ×ᶠ lb] prod.map ga gb := (eventually.prod_mk ha hb).mono $ λ x h, prod.ext h.1 h.2 lemma eventually_le.prod_map {δ} [has_le γ] [has_le δ] {la : filter α} {fa ga : α → γ} (ha : fa ≤ᶠ[la] ga) {lb : filter β} {fb gb : β → δ} (hb : fb ≤ᶠ[lb] gb) : prod.map fa fb ≤ᶠ[la ×ᶠ lb] prod.map ga gb := eventually.prod_mk ha hb lemma eventually.curry {la : filter α} {lb : filter β} {p : α × β → Prop} (h : ∀ᶠ x in la ×ᶠ lb, p x) : ∀ᶠ x in la, ∀ᶠ y in lb, p (x, y) := begin rcases eventually_prod_iff.1 h with ⟨pa, ha, pb, hb, h⟩, exact ha.mono (λ a ha, hb.mono $ λ b hb, h ha hb) end /-- A fact that is eventually true about all pairs `l ×ᶠ l` is eventually true about all diagonal pairs `(i, i)` -/ lemma eventually.diag_of_prod {f : filter α} {p : α × α → Prop} (h : ∀ᶠ i in f ×ᶠ f, p i) : (∀ᶠ i in f, p (i, i)) := begin obtain ⟨t, ht, s, hs, hst⟩ := eventually_prod_iff.1 h, apply (ht.and hs).mono (λ x hx, hst hx.1 hx.2), end lemma eventually.diag_of_prod_left {f : filter α} {g : filter γ} {p : (α × α) × γ → Prop} : (∀ᶠ x in (f ×ᶠ f ×ᶠ g), p x) → (∀ᶠ (x : α × γ) in (f ×ᶠ g), p ((x.1, x.1), x.2)) := begin intros h, obtain ⟨t, ht, s, hs, hst⟩ := eventually_prod_iff.1 h, refine (ht.diag_of_prod.prod_mk hs).mono (λ x hx, by simp only [hst hx.1 hx.2, prod.mk.eta]), end lemma eventually.diag_of_prod_right {f : filter α} {g : filter γ} {p : α × γ × γ → Prop} : (∀ᶠ x in (f ×ᶠ (g ×ᶠ g)), p x) → (∀ᶠ (x : α × γ) in (f ×ᶠ g), p (x.1, x.2, x.2)) := begin intros h, obtain ⟨t, ht, s, hs, hst⟩ := eventually_prod_iff.1 h, refine (ht.prod_mk hs.diag_of_prod).mono (λ x hx, by simp only [hst hx.1 hx.2, prod.mk.eta]), end lemma tendsto_diag : tendsto (λ i, (i, i)) f (f ×ᶠ f) := tendsto_iff_eventually.mpr (λ _ hpr, hpr.diag_of_prod) lemma prod_infi_left [nonempty ι] {f : ι → filter α} {g : filter β}: (⨅ i, f i) ×ᶠ g = (⨅ i, (f i) ×ᶠ g) := by { rw [filter.prod, comap_infi, infi_inf], simp only [filter.prod, eq_self_iff_true] } lemma prod_infi_right [nonempty ι] {f : filter α} {g : ι → filter β} : f ×ᶠ (⨅ i, g i) = (⨅ i, f ×ᶠ (g i)) := by { rw [filter.prod, comap_infi, inf_infi], simp only [filter.prod, eq_self_iff_true] } @[mono] lemma prod_mono {f₁ f₂ : filter α} {g₁ g₂ : filter β} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁ ×ᶠ g₁ ≤ f₂ ×ᶠ g₂ := inf_le_inf (comap_mono hf) (comap_mono hg) lemma prod_mono_left (g : filter β) {f₁ f₂ : filter α} (hf : f₁ ≤ f₂) : f₁ ×ᶠ g ≤ f₂ ×ᶠ g := filter.prod_mono hf rfl.le lemma prod_mono_right (f : filter α) {g₁ g₂ : filter β} (hf : g₁ ≤ g₂) : f ×ᶠ g₁ ≤ f ×ᶠ g₂ := filter.prod_mono rfl.le hf lemma {u v w x} prod_comap_comap_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {m₁ : β₁ → α₁} {m₂ : β₂ → α₂} : (comap m₁ f₁) ×ᶠ (comap m₂ f₂) = comap (λ p : β₁×β₂, (m₁ p.1, m₂ p.2)) (f₁ ×ᶠ f₂) := by simp only [filter.prod, comap_comap, eq_self_iff_true, comap_inf] lemma prod_comm' : f ×ᶠ g = comap (prod.swap) (g ×ᶠ f) := by simp only [filter.prod, comap_comap, (∘), inf_comm, prod.fst_swap, eq_self_iff_true, prod.snd_swap, comap_inf] lemma prod_comm : f ×ᶠ g = map (λ p : β×α, (p.2, p.1)) (g ×ᶠ f) := by { rw [prod_comm', ← map_swap_eq_comap_swap], refl } @[simp] lemma map_fst_prod (f : filter α) (g : filter β) [ne_bot g] : map prod.fst (f ×ᶠ g) = f := begin refine le_antisymm tendsto_fst (λ s hs, _), rw [mem_map, mem_prod_iff] at hs, rcases hs with ⟨t₁, h₁, t₂, h₂, hs⟩, rw [← image_subset_iff, fst_image_prod] at hs, exacts [mem_of_superset h₁ hs, nonempty_of_mem h₂] end @[simp] lemma map_snd_prod (f : filter α) (g : filter β) [ne_bot f] : map prod.snd (f ×ᶠ g) = g := by rw [prod_comm, map_map, (∘), map_fst_prod] @[simp] lemma prod_le_prod {f₁ f₂ : filter α} {g₁ g₂ : filter β} [ne_bot f₁] [ne_bot g₁] : f₁ ×ᶠ g₁ ≤ f₂ ×ᶠ g₂ ↔ f₁ ≤ f₂ ∧ g₁ ≤ g₂ := ⟨λ h, ⟨map_fst_prod f₁ g₁ ▸ tendsto_fst.mono_left h, map_snd_prod f₁ g₁ ▸ tendsto_snd.mono_left h⟩, λ h, prod_mono h.1 h.2⟩ @[simp] lemma prod_inj {f₁ f₂ : filter α} {g₁ g₂ : filter β} [ne_bot f₁] [ne_bot g₁] : f₁ ×ᶠ g₁ = f₂ ×ᶠ g₂ ↔ f₁ = f₂ ∧ g₁ = g₂ := begin refine ⟨λ h, _, λ h, h.1 ▸ h.2 ▸ rfl⟩, have hle : f₁ ≤ f₂ ∧ g₁ ≤ g₂ := prod_le_prod.1 h.le, haveI := ne_bot_of_le hle.1, haveI := ne_bot_of_le hle.2, exact ⟨hle.1.antisymm $ (prod_le_prod.1 h.ge).1, hle.2.antisymm $ (prod_le_prod.1 h.ge).2⟩ end lemma eventually_swap_iff {p : (α × β) → Prop} : (∀ᶠ (x : α × β) in (f ×ᶠ g), p x) ↔ ∀ᶠ (y : β × α) in (g ×ᶠ f), p y.swap := by { rw [prod_comm, eventually_map], simpa, } lemma prod_assoc (f : filter α) (g : filter β) (h : filter γ) : map (equiv.prod_assoc α β γ) ((f ×ᶠ g) ×ᶠ h) = f ×ᶠ (g ×ᶠ h) := by simp_rw [← comap_equiv_symm, filter.prod, comap_inf, comap_comap, inf_assoc, function.comp, equiv.prod_assoc_symm_apply] theorem prod_assoc_symm (f : filter α) (g : filter β) (h : filter γ) : map (equiv.prod_assoc α β γ).symm (f ×ᶠ (g ×ᶠ h)) = (f ×ᶠ g) ×ᶠ h := by simp_rw [map_equiv_symm, filter.prod, comap_inf, comap_comap, inf_assoc, function.comp, equiv.prod_assoc_apply] lemma tendsto_prod_assoc {f : filter α} {g : filter β} {h : filter γ} : tendsto (equiv.prod_assoc α β γ) (f ×ᶠ g ×ᶠ h) (f ×ᶠ (g ×ᶠ h)) := (prod_assoc f g h).le lemma tendsto_prod_assoc_symm {f : filter α} {g : filter β} {h : filter γ} : tendsto (equiv.prod_assoc α β γ).symm (f ×ᶠ (g ×ᶠ h)) (f ×ᶠ g ×ᶠ h) := (prod_assoc_symm f g h).le /-- A useful lemma when dealing with uniformities. -/ lemma map_swap4_prod {f : filter α} {g : filter β} {h : filter γ} {k : filter δ} : map (λ p : (α × β) × (γ × δ), ((p.1.1, p.2.1), (p.1.2, p.2.2))) ((f ×ᶠ g) ×ᶠ (h ×ᶠ k)) = (f ×ᶠ h) ×ᶠ (g ×ᶠ k) := by simp_rw [map_swap4_eq_comap, filter.prod, comap_inf, comap_comap, inf_assoc, inf_left_comm] lemma tendsto_swap4_prod {f : filter α} {g : filter β} {h : filter γ} {k : filter δ} : tendsto (λ p : (α × β) × (γ × δ), ((p.1.1, p.2.1), (p.1.2, p.2.2))) ((f ×ᶠ g) ×ᶠ (h ×ᶠ k)) ((f ×ᶠ h) ×ᶠ (g ×ᶠ k)) := map_swap4_prod.le lemma {u v w x} prod_map_map_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {m₁ : α₁ → β₁} {m₂ : α₂ → β₂} : (map m₁ f₁) ×ᶠ (map m₂ f₂) = map (λ p : α₁×α₂, (m₁ p.1, m₂ p.2)) (f₁ ×ᶠ f₂) := le_antisymm (λ s hs, let ⟨s₁, hs₁, s₂, hs₂, h⟩ := mem_prod_iff.mp hs in filter.sets_of_superset _ (prod_mem_prod (image_mem_map hs₁) (image_mem_map hs₂)) $ calc (m₁ '' s₁) ×ˢ (m₂ '' s₂) = (λ p : α₁×α₂, (m₁ p.1, m₂ p.2)) '' s₁ ×ˢ s₂ : set.prod_image_image_eq ... ⊆ _ : by rwa [image_subset_iff]) ((tendsto.comp le_rfl tendsto_fst).prod_mk (tendsto.comp le_rfl tendsto_snd)) lemma prod_map_map_eq' {α₁ : Type*} {α₂ : Type*} {β₁ : Type*} {β₂ : Type*} (f : α₁ → α₂) (g : β₁ → β₂) (F : filter α₁) (G : filter β₁) : (map f F) ×ᶠ (map g G) = map (prod.map f g) (F ×ᶠ G) := prod_map_map_eq lemma le_prod_map_fst_snd {f : filter (α × β)} : f ≤ map prod.fst f ×ᶠ map prod.snd f := le_inf le_comap_map le_comap_map lemma tendsto.prod_map {δ : Type*} {f : α → γ} {g : β → δ} {a : filter α} {b : filter β} {c : filter γ} {d : filter δ} (hf : tendsto f a c) (hg : tendsto g b d) : tendsto (prod.map f g) (a ×ᶠ b) (c ×ᶠ d) := begin erw [tendsto, ← prod_map_map_eq], exact filter.prod_mono hf hg, end protected lemma map_prod (m : α × β → γ) (f : filter α) (g : filter β) : map m (f ×ᶠ g) = (f.map (λ a b, m (a, b))).seq g := begin simp [filter.ext_iff, mem_prod_iff, mem_map_seq_iff], intro s, split, exact λ ⟨t, ht, s, hs, h⟩, ⟨s, hs, t, ht, λ x hx y hy, @h ⟨x, y⟩ ⟨hx, hy⟩⟩, exact λ ⟨s, hs, t, ht, h⟩, ⟨t, ht, s, hs, λ ⟨x, y⟩ ⟨hx, hy⟩, h x hx y hy⟩ end lemma prod_eq {f : filter α} {g : filter β} : f ×ᶠ g = (f.map prod.mk).seq g := have h : _ := f.map_prod id g, by rwa [map_id] at h lemma prod_inf_prod {f₁ f₂ : filter α} {g₁ g₂ : filter β} : (f₁ ×ᶠ g₁) ⊓ (f₂ ×ᶠ g₂) = (f₁ ⊓ f₂) ×ᶠ (g₁ ⊓ g₂) := by simp only [filter.prod, comap_inf, inf_comm, inf_assoc, inf_left_comm] @[simp] lemma prod_bot {f : filter α} : f ×ᶠ (⊥ : filter β) = ⊥ := by simp [filter.prod] @[simp] lemma bot_prod {g : filter β} : (⊥ : filter α) ×ᶠ g = ⊥ := by simp [filter.prod] @[simp] lemma prod_principal_principal {s : set α} {t : set β} : (𝓟 s) ×ᶠ (𝓟 t) = 𝓟 (s ×ˢ t) := by simp only [filter.prod, comap_principal, principal_eq_iff_eq, comap_principal, inf_principal]; refl @[simp] lemma pure_prod {a : α} {f : filter β} : pure a ×ᶠ f = map (prod.mk a) f := by rw [prod_eq, map_pure, pure_seq_eq_map] lemma map_pure_prod (f : α → β → γ) (a : α) (B : filter β) : filter.map (function.uncurry f) (pure a ×ᶠ B) = filter.map (f a) B := by { rw filter.pure_prod, refl } @[simp] lemma prod_pure {f : filter α} {b : β} : f ×ᶠ pure b = map (λ a, (a, b)) f := by rw [prod_eq, seq_pure, map_map] lemma prod_pure_pure {a : α} {b : β} : (pure a) ×ᶠ (pure b) = pure (a, b) := by simp lemma prod_eq_bot {f : filter α} {g : filter β} : f ×ᶠ g = ⊥ ↔ (f = ⊥ ∨ g = ⊥) := begin split, { intro h, rcases mem_prod_iff.1 (empty_mem_iff_bot.2 h) with ⟨s, hs, t, ht, hst⟩, rw [subset_empty_iff, set.prod_eq_empty_iff] at hst, cases hst with s_eq t_eq, { left, exact empty_mem_iff_bot.1 (s_eq ▸ hs) }, { right, exact empty_mem_iff_bot.1 (t_eq ▸ ht) } }, { rintro (rfl | rfl), exact bot_prod, exact prod_bot } end lemma prod_ne_bot {f : filter α} {g : filter β} : ne_bot (f ×ᶠ g) ↔ (ne_bot f ∧ ne_bot g) := by simp only [ne_bot_iff, ne, prod_eq_bot, not_or_distrib] lemma ne_bot.prod {f : filter α} {g : filter β} (hf : ne_bot f) (hg : ne_bot g) : ne_bot (f ×ᶠ g) := prod_ne_bot.2 ⟨hf, hg⟩ instance prod_ne_bot' {f : filter α} {g : filter β} [hf : ne_bot f] [hg : ne_bot g] : ne_bot (f ×ᶠ g) := hf.prod hg lemma tendsto_prod_iff {f : α × β → γ} {x : filter α} {y : filter β} {z : filter γ} : filter.tendsto f (x ×ᶠ y) z ↔ ∀ W ∈ z, ∃ U ∈ x, ∃ V ∈ y, ∀ x y, x ∈ U → y ∈ V → f (x, y) ∈ W := by simp only [tendsto_def, mem_prod_iff, prod_sub_preimage_iff, exists_prop, iff_self] lemma tendsto_prod_iff' {f : filter α} {g : filter β} {g' : filter γ} {s : α → β × γ} : tendsto s f (g ×ᶠ g') ↔ tendsto (λ n, (s n).1) f g ∧ tendsto (λ n, (s n).2) f g' := by { unfold filter.prod, simp only [tendsto_inf, tendsto_comap_iff, iff_self] } end prod /-! ### Coproducts of filters -/ section coprod variables {f : filter α} {g : filter β} /-- Coproduct of filters. -/ protected def coprod (f : filter α) (g : filter β) : filter (α × β) := f.comap prod.fst ⊔ g.comap prod.snd lemma mem_coprod_iff {s : set (α×β)} {f : filter α} {g : filter β} : s ∈ f.coprod g ↔ ((∃ t₁ ∈ f, prod.fst ⁻¹' t₁ ⊆ s) ∧ (∃ t₂ ∈ g, prod.snd ⁻¹' t₂ ⊆ s)) := by simp [filter.coprod] @[simp] lemma bot_coprod (l : filter β) : (⊥ : filter α).coprod l = comap prod.snd l := by simp [filter.coprod] @[simp] lemma coprod_bot (l : filter α) : l.coprod (⊥ : filter β) = comap prod.fst l := by simp [filter.coprod] lemma bot_coprod_bot : (⊥ : filter α).coprod (⊥ : filter β) = ⊥ := by simp lemma compl_mem_coprod {s : set (α × β)} {la : filter α} {lb : filter β} : sᶜ ∈ la.coprod lb ↔ (prod.fst '' s)ᶜ ∈ la ∧ (prod.snd '' s)ᶜ ∈ lb := by simp only [filter.coprod, mem_sup, compl_mem_comap] @[mono] lemma coprod_mono {f₁ f₂ : filter α} {g₁ g₂ : filter β} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.coprod g₁ ≤ f₂.coprod g₂ := sup_le_sup (comap_mono hf) (comap_mono hg) lemma coprod_ne_bot_iff : (f.coprod g).ne_bot ↔ f.ne_bot ∧ nonempty β ∨ nonempty α ∧ g.ne_bot := by simp [filter.coprod] @[instance] lemma coprod_ne_bot_left [ne_bot f] [nonempty β] : (f.coprod g).ne_bot := coprod_ne_bot_iff.2 (or.inl ⟨‹_›, ‹_›⟩) @[instance] lemma coprod_ne_bot_right [ne_bot g] [nonempty α] : (f.coprod g).ne_bot := coprod_ne_bot_iff.2 (or.inr ⟨‹_›, ‹_›⟩) lemma principal_coprod_principal (s : set α) (t : set β) : (𝓟 s).coprod (𝓟 t) = 𝓟 (sᶜ ×ˢ tᶜ)ᶜ := by rw [filter.coprod, comap_principal, comap_principal, sup_principal, set.prod_eq, compl_inter, preimage_compl, preimage_compl, compl_compl, compl_compl] -- this inequality can be strict; see `map_const_principal_coprod_map_id_principal` and -- `map_prod_map_const_id_principal_coprod_principal` below. lemma {u v w x} map_prod_map_coprod_le {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {m₁ : α₁ → β₁} {m₂ : α₂ → β₂} : map (prod.map m₁ m₂) (f₁.coprod f₂) ≤ (map m₁ f₁).coprod (map m₂ f₂) := begin intros s, simp only [mem_map, mem_coprod_iff], rintro ⟨⟨u₁, hu₁, h₁⟩, u₂, hu₂, h₂⟩, refine ⟨⟨m₁ ⁻¹' u₁, hu₁, λ _ hx, h₁ _⟩, ⟨m₂ ⁻¹' u₂, hu₂, λ _ hx, h₂ _⟩⟩; convert hx end /-- Characterization of the coproduct of the `filter.map`s of two principal filters `𝓟 {a}` and `𝓟 {i}`, the first under the constant function `λ a, b` and the second under the identity function. Together with the next lemma, `map_prod_map_const_id_principal_coprod_principal`, this provides an example showing that the inequality in the lemma `map_prod_map_coprod_le` can be strict. -/ lemma map_const_principal_coprod_map_id_principal {α β ι : Type*} (a : α) (b : β) (i : ι) : (map (λ _ : α, b) (𝓟 {a})).coprod (map id (𝓟 {i})) = 𝓟 (({b} : set β) ×ˢ univ ∪ univ ×ˢ ({i} : set ι)) := by simp only [map_principal, filter.coprod, comap_principal, sup_principal, image_singleton, image_id, prod_univ, univ_prod] /-- Characterization of the `filter.map` of the coproduct of two principal filters `𝓟 {a}` and `𝓟 {i}`, under the `prod.map` of two functions, respectively the constant function `λ a, b` and the identity function. Together with the previous lemma, `map_const_principal_coprod_map_id_principal`, this provides an example showing that the inequality in the lemma `map_prod_map_coprod_le` can be strict. -/ lemma map_prod_map_const_id_principal_coprod_principal {α β ι : Type*} (a : α) (b : β) (i : ι) : map (prod.map (λ _ : α, b) id) ((𝓟 {a}).coprod (𝓟 {i})) = 𝓟 (({b} : set β) ×ˢ (univ : set ι)) := begin rw [principal_coprod_principal, map_principal], congr, ext ⟨b', i'⟩, split, { rintro ⟨⟨a'', i''⟩, h₁, h₂, h₃⟩, simp }, { rintro ⟨h₁, h₂⟩, use (a, i'), simpa using h₁.symm } end lemma tendsto.prod_map_coprod {δ : Type*} {f : α → γ} {g : β → δ} {a : filter α} {b : filter β} {c : filter γ} {d : filter δ} (hf : tendsto f a c) (hg : tendsto g b d) : tendsto (prod.map f g) (a.coprod b) (c.coprod d) := map_prod_map_coprod_le.trans (coprod_mono hf hg) end coprod end filter
3f4f834f4541c88bd4eb5821941e75804f037393
b70031c8e2c5337b91d7e70f1e0c5f528f7b0e77
/src/logic/function/basic.lean
179a7e7ae76aa5ed0f470ccee7bbce858251d6b1
[ "Apache-2.0" ]
permissive
molodiuc/mathlib
cae2ba3ef1601c1f42ca0b625c79b061b63fef5b
98ebe5a6739fbe254f9ee9d401882d4388f91035
refs/heads/master
1,674,237,127,059
1,606,353,533,000
1,606,353,533,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
21,988
lean
/- Copyright (c) 2016 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 logic.basic import data.option.defs /-! # Miscellaneous function constructions and lemmas -/ universes u v w namespace function section variables {α β γ : Sort*} {f : α → β} /-- Evaluate a function at an argument. Useful if you want to talk about the partially applied `function.eval x : (Π x, β x) → β x`. -/ @[reducible] def eval {β : α → Sort*} (x : α) (f : Π x, β x) : β x := f x @[simp] lemma eval_apply {β : α → Sort*} (x : α) (f : Π x, β x) : eval x f = f x := rfl lemma comp_apply {α : Sort u} {β : Sort v} {φ : Sort w} (f : β → φ) (g : α → β) (a : α) : (f ∘ g) a = f (g a) := rfl lemma const_def {y : β} : (λ x : α, y) = const α y := rfl @[simp] lemma const_apply {y : β} {x : α} : const α y x = y := rfl @[simp] lemma const_comp {f : α → β} {c : γ} : const β c ∘ f = const α c := rfl @[simp] lemma comp_const {f : β → γ} {b : β} : f ∘ const α b = const α (f b) := rfl lemma hfunext {α α': Sort u} {β : α → Sort v} {β' : α' → Sort v} {f : Πa, β a} {f' : Πa, β' a} (hα : α = α') (h : ∀a a', a == a' → f a == f' a') : f == f' := begin subst hα, have : ∀a, f a == f' a, { intro a, exact h a a (heq.refl a) }, have : β = β', { funext a, exact type_eq_of_heq (this a) }, subst this, apply heq_of_eq, funext a, exact eq_of_heq (this a) end lemma funext_iff {β : α → Sort*} {f₁ f₂ : Π (x : α), β x} : f₁ = f₂ ↔ (∀a, f₁ a = f₂ a) := iff.intro (assume h a, h ▸ rfl) funext @[simp] theorem injective.eq_iff (I : injective f) {a b : α} : f a = f b ↔ a = b := ⟨@I _ _, congr_arg f⟩ theorem injective.eq_iff' (I : injective f) {a b : α} {c : β} (h : f b = c) : f a = c ↔ a = b := h ▸ I.eq_iff lemma injective.ne (hf : injective f) {a₁ a₂ : α} : a₁ ≠ a₂ → f a₁ ≠ f a₂ := mt (assume h, hf h) lemma injective.ne_iff (hf : injective f) {x y : α} : f x ≠ f y ↔ x ≠ y := ⟨mt $ congr_arg f, hf.ne⟩ lemma injective.ne_iff' (hf : injective f) {x y : α} {z : β} (h : f y = z) : f x ≠ z ↔ x ≠ y := h ▸ hf.ne_iff /-- If the co-domain `β` of an injective function `f : α → β` has decidable equality, then the domain `α` also has decidable equality. -/ def injective.decidable_eq [decidable_eq β] (I : injective f) : decidable_eq α := λ a b, decidable_of_iff _ I.eq_iff lemma injective.of_comp {g : γ → α} (I : injective (f ∘ g)) : injective g := λ x y h, I $ show f (g x) = f (g y), from congr_arg f h lemma surjective.of_comp {g : γ → α} (S : surjective (f ∘ g)) : surjective f := λ y, let ⟨x, h⟩ := S y in ⟨g x, h⟩ instance decidable_eq_pfun (p : Prop) [decidable p] (α : p → Type*) [Π hp, decidable_eq (α hp)] : decidable_eq (Π hp, α hp) | f g := decidable_of_iff (∀ hp, f hp = g hp) funext_iff.symm theorem surjective.forall {f : α → β} (hf : surjective f) {p : β → Prop} : (∀ y, p y) ↔ ∀ x, p (f x) := ⟨λ h x, h (f x), λ h y, let ⟨x, hx⟩ := hf y in hx ▸ h x⟩ theorem surjective.forall₂ {f : α → β} (hf : surjective f) {p : β → β → Prop} : (∀ y₁ y₂, p y₁ y₂) ↔ ∀ x₁ x₂, p (f x₁) (f x₂) := hf.forall.trans $ forall_congr $ λ x, hf.forall theorem surjective.forall₃ {f : α → β} (hf : surjective f) {p : β → β → β → Prop} : (∀ y₁ y₂ y₃, p y₁ y₂ y₃) ↔ ∀ x₁ x₂ x₃, p (f x₁) (f x₂) (f x₃) := hf.forall.trans $ forall_congr $ λ x, hf.forall₂ theorem surjective.exists {f : α → β} (hf : surjective f) {p : β → Prop} : (∃ y, p y) ↔ ∃ x, p (f x) := ⟨λ ⟨y, hy⟩, let ⟨x, hx⟩ := hf y in ⟨x, hx.symm ▸ hy⟩, λ ⟨x, hx⟩, ⟨f x, hx⟩⟩ theorem surjective.exists₂ {f : α → β} (hf : surjective f) {p : β → β → Prop} : (∃ y₁ y₂, p y₁ y₂) ↔ ∃ x₁ x₂, p (f x₁) (f x₂) := hf.exists.trans $ exists_congr $ λ x, hf.exists theorem surjective.exists₃ {f : α → β} (hf : surjective f) {p : β → β → β → Prop} : (∃ y₁ y₂ y₃, p y₁ y₂ y₃) ↔ ∃ x₁ x₂ x₃, p (f x₁) (f x₂) (f x₃) := hf.exists.trans $ exists_congr $ λ x, hf.exists₂ /-- Cantor's diagonal argument implies that there are no surjective functions from `α` to `set α`. -/ theorem cantor_surjective {α} (f : α → set α) : ¬ function.surjective f | h := let ⟨D, e⟩ := h (λ a, ¬ f a a) in (iff_not_self (f D D)).1 $ iff_of_eq (congr_fun e D) /-- Cantor's diagonal argument implies that there are no injective functions from `set α` to `α`. -/ theorem cantor_injective {α : Type*} (f : (set α) → α) : ¬ function.injective f | i := cantor_surjective (λ a b, ∀ U, a = f U → U b) $ right_inverse.surjective (λ U, funext $ λ a, propext ⟨λ h, h U rfl, λ h' U' e, i e ▸ h'⟩) /-- `g` is a partial inverse to `f` (an injective but not necessarily surjective function) if `g y = some x` implies `f x = y`, and `g y = none` implies that `y` is not in the range of `f`. -/ def is_partial_inv {α β} (f : α → β) (g : β → option α) : Prop := ∀ x y, g y = some x ↔ f x = y theorem is_partial_inv_left {α β} {f : α → β} {g} (H : is_partial_inv f g) (x) : g (f x) = some x := (H _ _).2 rfl theorem injective_of_partial_inv {α β} {f : α → β} {g} (H : is_partial_inv f g) : injective f := λ a b h, option.some.inj $ ((H _ _).2 h).symm.trans ((H _ _).2 rfl) theorem injective_of_partial_inv_right {α β} {f : α → β} {g} (H : is_partial_inv f g) (x y b) (h₁ : b ∈ g x) (h₂ : b ∈ g y) : x = y := ((H _ _).1 h₁).symm.trans ((H _ _).1 h₂) theorem left_inverse.comp_eq_id {f : α → β} {g : β → α} (h : left_inverse f g) : f ∘ g = id := funext h theorem left_inverse_iff_comp {f : α → β} {g : β → α} : left_inverse f g ↔ f ∘ g = id := ⟨left_inverse.comp_eq_id, congr_fun⟩ theorem right_inverse.comp_eq_id {f : α → β} {g : β → α} (h : right_inverse f g) : g ∘ f = id := funext h theorem right_inverse_iff_comp {f : α → β} {g : β → α} : right_inverse f g ↔ g ∘ f = id := ⟨right_inverse.comp_eq_id, congr_fun⟩ theorem left_inverse.comp {f : α → β} {g : β → α} {h : β → γ} {i : γ → β} (hf : left_inverse f g) (hh : left_inverse h i) : left_inverse (h ∘ f) (g ∘ i) := assume a, show h (f (g (i a))) = a, by rw [hf (i a), hh a] theorem right_inverse.comp {f : α → β} {g : β → α} {h : β → γ} {i : γ → β} (hf : right_inverse f g) (hh : right_inverse h i) : right_inverse (h ∘ f) (g ∘ i) := left_inverse.comp hh hf theorem left_inverse.right_inverse {f : α → β} {g : β → α} (h : left_inverse g f) : right_inverse f g := h theorem right_inverse.left_inverse {f : α → β} {g : β → α} (h : right_inverse g f) : left_inverse f g := h theorem left_inverse.surjective {f : α → β} {g : β → α} (h : left_inverse f g) : surjective f := h.right_inverse.surjective theorem right_inverse.injective {f : α → β} {g : β → α} (h : right_inverse f g) : injective f := h.left_inverse.injective theorem left_inverse.eq_right_inverse {f : α → β} {g₁ g₂ : β → α} (h₁ : left_inverse g₁ f) (h₂ : right_inverse g₂ f) : g₁ = g₂ := calc g₁ = g₁ ∘ f ∘ g₂ : by rw [h₂.comp_eq_id, comp.right_id] ... = g₂ : by rw [← comp.assoc, h₁.comp_eq_id, comp.left_id] local attribute [instance, priority 10] classical.prop_decidable /-- We can use choice to construct explicitly a partial inverse for a given injective function `f`. -/ noncomputable def partial_inv {α β} (f : α → β) (b : β) : option α := if h : ∃ a, f a = b then some (classical.some h) else none theorem partial_inv_of_injective {α β} {f : α → β} (I : injective f) : is_partial_inv f (partial_inv f) | a b := ⟨λ h, if h' : ∃ a, f a = b then begin rw [partial_inv, dif_pos h'] at h, injection h with h, subst h, apply classical.some_spec h' end else by rw [partial_inv, dif_neg h'] at h; contradiction, λ e, e ▸ have h : ∃ a', f a' = f a, from ⟨_, rfl⟩, (dif_pos h).trans (congr_arg _ (I $ classical.some_spec h))⟩ theorem partial_inv_left {α β} {f : α → β} (I : injective f) : ∀ x, partial_inv f (f x) = some x := is_partial_inv_left (partial_inv_of_injective I) end section inv_fun variables {α : Type u} [n : nonempty α] {β : Sort v} {f : α → β} {s : set α} {a : α} {b : β} include n local attribute [instance, priority 10] classical.prop_decidable /-- Construct the inverse for a function `f` on domain `s`. This function is a right inverse of `f` on `f '' s`. -/ noncomputable def inv_fun_on (f : α → β) (s : set α) (b : β) : α := if h : ∃a, a ∈ s ∧ f a = b then classical.some h else classical.choice n theorem inv_fun_on_pos (h : ∃a∈s, f a = b) : inv_fun_on f s b ∈ s ∧ f (inv_fun_on f s b) = b := by rw [bex_def] at h; rw [inv_fun_on, dif_pos h]; exact classical.some_spec h theorem inv_fun_on_mem (h : ∃a∈s, f a = b) : inv_fun_on f s b ∈ s := (inv_fun_on_pos h).left theorem inv_fun_on_eq (h : ∃a∈s, f a = b) : f (inv_fun_on f s b) = b := (inv_fun_on_pos h).right theorem inv_fun_on_eq' (h : ∀ (x ∈ s) (y ∈ s), f x = f y → x = y) (ha : a ∈ s) : inv_fun_on f s (f a) = a := have ∃a'∈s, f a' = f a, from ⟨a, ha, rfl⟩, h _ (inv_fun_on_mem this) _ ha (inv_fun_on_eq this) theorem inv_fun_on_neg (h : ¬ ∃a∈s, f a = b) : inv_fun_on f s b = classical.choice n := by rw [bex_def] at h; rw [inv_fun_on, dif_neg h] /-- The inverse of a function (which is a left inverse if `f` is injective and a right inverse if `f` is surjective). -/ noncomputable def inv_fun (f : α → β) : β → α := inv_fun_on f set.univ theorem inv_fun_eq (h : ∃a, f a = b) : f (inv_fun f b) = b := inv_fun_on_eq $ let ⟨a, ha⟩ := h in ⟨a, trivial, ha⟩ lemma inv_fun_neg (h : ¬ ∃ a, f a = b) : inv_fun f b = classical.choice n := by refine inv_fun_on_neg (mt _ h); exact assume ⟨a, _, ha⟩, ⟨a, ha⟩ theorem inv_fun_eq_of_injective_of_right_inverse {g : β → α} (hf : injective f) (hg : right_inverse g f) : inv_fun f = g := funext $ assume b, hf begin rw [hg b], exact inv_fun_eq ⟨g b, hg b⟩ end lemma right_inverse_inv_fun (hf : surjective f) : right_inverse (inv_fun f) f := assume b, inv_fun_eq $ hf b lemma left_inverse_inv_fun (hf : injective f) : left_inverse (inv_fun f) f := assume b, have f (inv_fun f (f b)) = f b, from inv_fun_eq ⟨b, rfl⟩, hf this lemma inv_fun_surjective (hf : injective f) : surjective (inv_fun f) := (left_inverse_inv_fun hf).surjective lemma inv_fun_comp (hf : injective f) : inv_fun f ∘ f = id := funext $ left_inverse_inv_fun hf end inv_fun section inv_fun variables {α : Type u} [i : nonempty α] {β : Sort v} {f : α → β} include i lemma injective.has_left_inverse (hf : injective f) : has_left_inverse f := ⟨inv_fun f, left_inverse_inv_fun hf⟩ lemma injective_iff_has_left_inverse : injective f ↔ has_left_inverse f := ⟨injective.has_left_inverse, has_left_inverse.injective⟩ end inv_fun section surj_inv variables {α : Sort u} {β : Sort v} {f : α → β} /-- The inverse of a surjective function. (Unlike `inv_fun`, this does not require `α` to be inhabited.) -/ noncomputable def surj_inv {f : α → β} (h : surjective f) (b : β) : α := classical.some (h b) lemma surj_inv_eq (h : surjective f) (b) : f (surj_inv h b) = b := classical.some_spec (h b) lemma right_inverse_surj_inv (hf : surjective f) : right_inverse (surj_inv hf) f := surj_inv_eq hf lemma left_inverse_surj_inv (hf : bijective f) : left_inverse (surj_inv hf.2) f := right_inverse_of_injective_of_left_inverse hf.1 (right_inverse_surj_inv hf.2) lemma surjective.has_right_inverse (hf : surjective f) : has_right_inverse f := ⟨_, right_inverse_surj_inv hf⟩ lemma surjective_iff_has_right_inverse : surjective f ↔ has_right_inverse f := ⟨surjective.has_right_inverse, has_right_inverse.surjective⟩ lemma bijective_iff_has_inverse : bijective f ↔ ∃ g, left_inverse g f ∧ right_inverse g f := ⟨λ hf, ⟨_, left_inverse_surj_inv hf, right_inverse_surj_inv hf.2⟩, λ ⟨g, gl, gr⟩, ⟨gl.injective, gr.surjective⟩⟩ lemma injective_surj_inv (h : surjective f) : injective (surj_inv h) := (right_inverse_surj_inv h).injective end surj_inv section update variables {α : Sort u} {β : α → Sort v} {α' : Sort w} [decidable_eq α] [decidable_eq α'] /-- Replacing the value of a function at a given point by a given value. -/ def update (f : Πa, β a) (a' : α) (v : β a') (a : α) : β a := if h : a = a' then eq.rec v h.symm else f a /-- On non-dependent functions, `function.update` can be expressed as an `ite` -/ lemma update_apply {β : Sort*} (f : α → β) (a' : α) (b : β) (a : α) : update f a' b a = if a = a' then b else f a := begin dunfold update, congr, funext, rw eq_rec_constant, end @[simp] lemma update_same (a : α) (v : β a) (f : Πa, β a) : update f a v a = v := dif_pos rfl lemma update_injective (f : Πa, β a) (a' : α) : injective (update f a') := λ v v' h, have _ := congr_fun h a', by rwa [update_same, update_same] at this @[simp] lemma update_noteq {a a' : α} (h : a ≠ a') (v : β a') (f : Πa, β a) : update f a' v a = f a := dif_neg h lemma rel_update_iff {β' : α → Sort*} {a : α} {b : β a} {f : Π a, β a} {g : Π a, β' a} (r : Π a, β a → β' a → Prop) : (∀ x, r x (update f a b x) (g x)) ↔ r a b (g a) ∧ ∀ x ≠ a, r x (f x) (g x) := calc (∀ x, r x (update f a b x) (g x)) ↔ ∀ x, (x = a ∨ x ≠ a) → r x (update f a b x) (g x) : by simp only [ne.def, classical.em, forall_prop_of_true] ... ↔ r a b (g a) ∧ ∀ x ≠ a, r x (update f a b x) (g x) : by simp only [or_imp_distrib, forall_and_distrib, forall_eq, update_same] ... ↔ r a b (g a) ∧ ∀ x ≠ a, r x (f x) (g x) : and_congr iff.rfl $ forall_congr $ λ x, forall_congr $ λ hx, by rw [update_noteq hx] lemma update_eq_iff {a : α} {b : β a} {f g : Π a, β a} : update f a b = g ↔ b = g a ∧ ∀ x ≠ a, f x = g x := funext_iff.trans $ rel_update_iff (λ a x y, x = y) lemma eq_update_iff {a : α} {b : β a} {f g : Π a, β a} : g = update f a b ↔ g a = b ∧ ∀ x ≠ a, g x = f x := eq_comm.trans $ update_eq_iff.trans $ by simp only [eq_comm] @[simp] lemma update_eq_self (a : α) (f : Πa, β a) : update f a (f a) = f := update_eq_iff.2 ⟨rfl, λ _ _, rfl⟩ lemma update_comp {β : Sort v} (f : α → β) {g : α' → α} (hg : injective g) (a : α') (v : β) : (update f (g a) v) ∘ g = update (f ∘ g) a v := eq_update_iff.2 ⟨update_same _ _ _, λ x hx, update_noteq (hg.ne hx) _ _⟩ lemma apply_update {ι : Sort*} [decidable_eq ι] {α β : ι → Sort*} (f : Π i, α i → β i) (g : Π i, α i) (i : ι) (v : α i) (j : ι) : f j (update g i v j) = update (λ k, f k (g k)) i (f i v) j := begin by_cases h : j = i, { subst j, simp }, { simp [h] } end lemma comp_update {α' : Sort*} {β : Sort*} (f : α' → β) (g : α → α') (i : α) (v : α') : f ∘ (update g i v) = update (f ∘ g) i (f v) := funext $ apply_update _ _ _ _ theorem update_comm {α} [decidable_eq α] {β : α → Sort*} {a b : α} (h : a ≠ b) (v : β a) (w : β b) (f : Πa, β a) : update (update f a v) b w = update (update f b w) a v := begin funext c, simp only [update], by_cases h₁ : c = b; by_cases h₂ : c = a; try {simp [h₁, h₂]}, cases h (h₂.symm.trans h₁), end @[simp] theorem update_idem {α} [decidable_eq α] {β : α → Sort*} {a : α} (v w : β a) (f : Πa, β a) : update (update f a v) a w = update f a w := by {funext b, by_cases b = a; simp [update, h]} end update section extend noncomputable theory local attribute [instance, priority 10] classical.prop_decidable variables {α β γ : Type*} {f : α → β} /-- `extend f g e'` extends a function `g : α → γ` along a function `f : α → β` to a function `β → γ`, by using the values of `g` on the range of `f` and the values of an auxiliary function `e' : β → γ` elsewhere. Mostly useful when `f` is injective. -/ def extend (f : α → β) (g : α → γ) (e' : β → γ) : β → γ := λ b, if h : ∃ a, f a = b then g (classical.some h) else e' b lemma extend_def (f : α → β) (g : α → γ) (e' : β → γ) (b : β) : extend f g e' b = if h : ∃ a, f a = b then g (classical.some h) else e' b := rfl @[simp] lemma extend_apply (hf : injective f) (g : α → γ) (e' : β → γ) (a : α) : extend f g e' (f a) = g a := begin simp only [extend_def, dif_pos, exists_apply_eq_apply], exact congr_arg g (hf $ classical.some_spec (exists_apply_eq_apply f a)) end @[simp] lemma extend_comp (hf : injective f) (g : α → γ) (e' : β → γ) : extend f g e' ∘ f = g := funext $ λ a, extend_apply hf g e' a end extend lemma uncurry_def {α β γ} (f : α → β → γ) : uncurry f = (λp, f p.1 p.2) := rfl @[simp] lemma uncurry_apply_pair {α β γ} (f : α → β → γ) (x : α) (y : β) : uncurry f (x, y) = f x y := rfl @[simp] lemma curry_apply {α β γ} (f : α × β → γ) (x : α) (y : β) : curry f x y = f (x, y) := rfl section bicomp variables {α β γ δ ε : Type*} /-- Compose a binary function `f` with a pair of unary functions `g` and `h`. If both arguments of `f` have the same type and `g = h`, then `bicompl f g g = f on g`. -/ def bicompl (f : γ → δ → ε) (g : α → γ) (h : β → δ) (a b) := f (g a) (h b) /-- Compose an unary function `f` with a binary function `g`. -/ def bicompr (f : γ → δ) (g : α → β → γ) (a b) := f (g a b) -- Suggested local notation: local notation f `∘₂` g := bicompr f g lemma uncurry_bicompr (f : α → β → γ) (g : γ → δ) : uncurry (g ∘₂ f) = (g ∘ uncurry f) := rfl lemma uncurry_bicompl (f : γ → δ → ε) (g : α → γ) (h : β → δ) : uncurry (bicompl f g h) = (uncurry f) ∘ (prod.map g h) := rfl end bicomp section uncurry variables {α β γ δ : Type*} /-- Records a way to turn an element of `α` into a function from `β` to `γ`. The most generic use is to recursively uncurry. For instance `f : α → β → γ → δ` will be turned into `↿f : α × β × γ → δ`. One can also add instances for bundled maps. -/ class has_uncurry (α : Type*) (β : out_param Type*) (γ : out_param Type*) := (uncurry : α → (β → γ)) /-- Uncurrying operator. The most generic use is to recursively uncurry. For instance `f : α → β → γ → δ` will be turned into `↿f : α × β × γ → δ`. One can also add instances for bundled maps.-/ add_decl_doc has_uncurry.uncurry notation `↿`:max x:max := has_uncurry.uncurry x instance has_uncurry_base : has_uncurry (α → β) α β := ⟨id⟩ instance has_uncurry_induction [has_uncurry β γ δ] : has_uncurry (α → β) (α × γ) δ := ⟨λ f p, ↿(f p.1) p.2⟩ end uncurry /-- A function is involutive, if `f ∘ f = id`. -/ def involutive {α} (f : α → α) : Prop := ∀ x, f (f x) = x lemma involutive_iff_iter_2_eq_id {α} {f : α → α} : involutive f ↔ (f^[2] = id) := funext_iff.symm namespace involutive variables {α : Sort u} {f : α → α} (h : involutive f) include h @[simp] lemma comp_self : f ∘ f = id := funext h protected lemma left_inverse : left_inverse f f := h protected lemma right_inverse : right_inverse f f := h protected lemma injective : injective f := h.left_inverse.injective protected lemma surjective : surjective f := λ x, ⟨f x, h x⟩ protected lemma bijective : bijective f := ⟨h.injective, h.surjective⟩ /-- Involuting an `ite` of an involuted value `x : α` negates the `Prop` condition in the `ite`. -/ protected lemma ite_not (P : Prop) [decidable P] (x : α) : f (ite P x (f x)) = ite (¬ P) x (f x) := by rw [apply_ite f, h, ite_not] end involutive /-- The property of a binary function `f : α → β → γ` being injective. Mathematically this should be thought of as the corresponding function `α × β → γ` being injective. -/ @[reducible] def injective2 {α β γ} (f : α → β → γ) : Prop := ∀ ⦃a₁ a₂ b₁ b₂⦄, f a₁ b₁ = f a₂ b₂ → a₁ = a₂ ∧ b₁ = b₂ namespace injective2 variables {α β γ : Type*} (f : α → β → γ) protected lemma left (hf : injective2 f) ⦃a₁ a₂ b₁ b₂⦄ (h : f a₁ b₁ = f a₂ b₂) : a₁ = a₂ := (hf h).1 protected lemma right (hf : injective2 f) ⦃a₁ a₂ b₁ b₂⦄ (h : f a₁ b₁ = f a₂ b₂) : b₁ = b₂ := (hf h).2 lemma eq_iff (hf : injective2 f) ⦃a₁ a₂ b₁ b₂⦄ : f a₁ b₁ = f a₂ b₂ ↔ a₁ = a₂ ∧ b₁ = b₂ := ⟨λ h, hf h, λ⟨h1, h2⟩, congr_arg2 f h1 h2⟩ end injective2 section sometimes local attribute [instance, priority 10] classical.prop_decidable /-- `sometimes f` evaluates to some value of `f`, if it exists. This function is especially interesting in the case where `α` is a proposition, in which case `f` is necessarily a constant function, so that `sometimes f = f a` for all `a`. -/ noncomputable def sometimes {α β} [nonempty β] (f : α → β) : β := if h : nonempty α then f (classical.choice h) else classical.choice ‹_› theorem sometimes_eq {p : Prop} {α} [nonempty α] (f : p → α) (a : p) : sometimes f = f a := dif_pos ⟨a⟩ theorem sometimes_spec {p : Prop} {α} [nonempty α] (P : α → Prop) (f : p → α) (a : p) (h : P (f a)) : P (sometimes f) := by rwa sometimes_eq end sometimes end function /-- `s.piecewise f g` is the function equal to `f` on the set `s`, and to `g` on its complement. -/ def set.piecewise {α : Type u} {β : α → Sort v} (s : set α) (f g : Πi, β i) [∀j, decidable (j ∈ s)] : Πi, β i := λi, if i ∈ s then f i else g i
efec89b4334d8ac11fb5f7f68df31dabc3196554
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/ring/prod.lean
5f5c2da349c5ef518881c9edc2114ed736ccdcef
[ "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
11,951
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Chris Hughes, Mario Carneiro, Yury Kudryashov -/ import data.int.cast.prod import algebra.group.prod import algebra.ring.equiv import algebra.order.monoid.prod /-! # Semiring, ring etc structures on `R × S` > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we define two-binop (`semiring`, `ring` etc) structures on `R × S`. We also prove trivial `simp` lemmas, and define the following operations on `ring_hom`s and similarly for `non_unital_ring_hom`s: * `fst R S : R × S →+* R`, `snd R S : R × S →+* S`: projections `prod.fst` and `prod.snd` as `ring_hom`s; * `f.prod g : `R →+* S × T`: sends `x` to `(f x, g x)`; * `f.prod_map g : `R × S → R' × S'`: `prod.map f g` as a `ring_hom`, sends `(x, y)` to `(f x, g y)`. -/ variables {α β R R' S S' T T' : Type*} namespace prod /-- Product of two distributive types is distributive. -/ instance [distrib R] [distrib S] : distrib (R × S) := { left_distrib := λ a b c, mk.inj_iff.mpr ⟨left_distrib _ _ _, left_distrib _ _ _⟩, right_distrib := λ a b c, mk.inj_iff.mpr ⟨right_distrib _ _ _, right_distrib _ _ _⟩, .. prod.has_add, .. prod.has_mul } /-- Product of two `non_unital_non_assoc_semiring`s is a `non_unital_non_assoc_semiring`. -/ instance [non_unital_non_assoc_semiring R] [non_unital_non_assoc_semiring S] : non_unital_non_assoc_semiring (R × S) := { .. prod.add_comm_monoid, .. prod.mul_zero_class, .. prod.distrib } /-- Product of two `non_unital_semiring`s is a `non_unital_semiring`. -/ instance [non_unital_semiring R] [non_unital_semiring S] : non_unital_semiring (R × S) := { .. prod.non_unital_non_assoc_semiring, .. prod.semigroup } /-- Product of two `non_assoc_semiring`s is a `non_assoc_semiring`. -/ instance [non_assoc_semiring R] [non_assoc_semiring S] : non_assoc_semiring (R × S) := { .. prod.non_unital_non_assoc_semiring, .. prod.mul_one_class, .. prod.add_monoid_with_one } /-- Product of two semirings is a semiring. -/ instance [semiring R] [semiring S] : semiring (R × S) := { .. prod.add_comm_monoid, .. prod.monoid_with_zero, .. prod.distrib, .. prod.add_monoid_with_one } /-- Product of two `non_unital_comm_semiring`s is a `non_unital_comm_semiring`. -/ instance [non_unital_comm_semiring R] [non_unital_comm_semiring S] : non_unital_comm_semiring (R × S) := { .. prod.non_unital_semiring, .. prod.comm_semigroup } /-- Product of two commutative semirings is a commutative semiring. -/ instance [comm_semiring R] [comm_semiring S] : comm_semiring (R × S) := { .. prod.semiring, .. prod.comm_monoid } instance [non_unital_non_assoc_ring R] [non_unital_non_assoc_ring S] : non_unital_non_assoc_ring (R × S) := { .. prod.add_comm_group, .. prod.non_unital_non_assoc_semiring } instance [non_unital_ring R] [non_unital_ring S] : non_unital_ring (R × S) := { .. prod.add_comm_group, .. prod.non_unital_semiring } instance [non_assoc_ring R] [non_assoc_ring S] : non_assoc_ring (R × S) := { .. prod.add_comm_group, .. prod.non_assoc_semiring, .. prod.add_group_with_one } /-- Product of two rings is a ring. -/ instance [ring R] [ring S] : ring (R × S) := { .. prod.add_comm_group, .. prod.add_group_with_one, .. prod.semiring } /-- Product of two `non_unital_comm_ring`s is a `non_unital_comm_ring`. -/ instance [non_unital_comm_ring R] [non_unital_comm_ring S] : non_unital_comm_ring (R × S) := { .. prod.non_unital_ring, .. prod.comm_semigroup } /-- Product of two commutative rings is a commutative ring. -/ instance [comm_ring R] [comm_ring S] : comm_ring (R × S) := { .. prod.ring, .. prod.comm_monoid } end prod namespace non_unital_ring_hom variables (R S) [non_unital_non_assoc_semiring R] [non_unital_non_assoc_semiring S] /-- Given non-unital semirings `R`, `S`, the natural projection homomorphism from `R × S` to `R`.-/ def fst : R × S →ₙ+* R := { to_fun := prod.fst, .. mul_hom.fst R S, .. add_monoid_hom.fst R S } /-- Given non-unital semirings `R`, `S`, the natural projection homomorphism from `R × S` to `S`.-/ def snd : R × S →ₙ+* S := { to_fun := prod.snd, .. mul_hom.snd R S, .. add_monoid_hom.snd R S } variables {R S} @[simp] lemma coe_fst : ⇑(fst R S) = prod.fst := rfl @[simp] lemma coe_snd : ⇑(snd R S) = prod.snd := rfl section prod variables [non_unital_non_assoc_semiring T] (f : R →ₙ+* S) (g : R →ₙ+* T) /-- Combine two non-unital ring homomorphisms `f : R →ₙ+* S`, `g : R →ₙ+* T` into `f.prod g : R →ₙ+* S × T` given by `(f.prod g) x = (f x, g x)` -/ protected def prod (f : R →ₙ+* S) (g : R →ₙ+* T) : R →ₙ+* S × T := { to_fun := λ x, (f x, g x), .. mul_hom.prod (f : mul_hom R S) (g : mul_hom R T), .. add_monoid_hom.prod (f : R →+ S) (g : R →+ T) } @[simp] lemma prod_apply (x) : f.prod g x = (f x, g x) := rfl @[simp] lemma fst_comp_prod : (fst S T).comp (f.prod g) = f := ext $ λ x, rfl @[simp] lemma snd_comp_prod : (snd S T).comp (f.prod g) = g := ext $ λ x, rfl lemma prod_unique (f : R →ₙ+* S × T) : ((fst S T).comp f).prod ((snd S T).comp f) = f := ext $ λ x, by simp only [prod_apply, coe_fst, coe_snd, comp_apply, prod.mk.eta] end prod section prod_map variables [non_unital_non_assoc_semiring R'] [non_unital_non_assoc_semiring S'] [non_unital_non_assoc_semiring T] variables (f : R →ₙ+* R') (g : S →ₙ+* S') /-- `prod.map` as a `non_unital_ring_hom`. -/ def prod_map : R × S →ₙ+* R' × S' := (f.comp (fst R S)).prod (g.comp (snd R S)) lemma prod_map_def : prod_map f g = (f.comp (fst R S)).prod (g.comp (snd R S)) := rfl @[simp] lemma coe_prod_map : ⇑(prod_map f g) = prod.map f g := rfl lemma prod_comp_prod_map (f : T →ₙ+* R) (g : T →ₙ+* S) (f' : R →ₙ+* R') (g' : S →ₙ+* S') : (f'.prod_map g').comp (f.prod g) = (f'.comp f).prod (g'.comp g) := rfl end prod_map end non_unital_ring_hom namespace ring_hom variables (R S) [non_assoc_semiring R] [non_assoc_semiring S] /-- Given semirings `R`, `S`, the natural projection homomorphism from `R × S` to `R`.-/ def fst : R × S →+* R := { to_fun := prod.fst, .. monoid_hom.fst R S, .. add_monoid_hom.fst R S } /-- Given semirings `R`, `S`, the natural projection homomorphism from `R × S` to `S`.-/ def snd : R × S →+* S := { to_fun := prod.snd, .. monoid_hom.snd R S, .. add_monoid_hom.snd R S } variables {R S} @[simp] lemma coe_fst : ⇑(fst R S) = prod.fst := rfl @[simp] lemma coe_snd : ⇑(snd R S) = prod.snd := rfl section prod variables [non_assoc_semiring T] (f : R →+* S) (g : R →+* T) /-- Combine two ring homomorphisms `f : R →+* S`, `g : R →+* T` into `f.prod g : R →+* S × T` given by `(f.prod g) x = (f x, g x)` -/ protected def prod (f : R →+* S) (g : R →+* T) : R →+* S × T := { to_fun := λ x, (f x, g x), .. monoid_hom.prod (f : R →* S) (g : R →* T), .. add_monoid_hom.prod (f : R →+ S) (g : R →+ T) } @[simp] lemma prod_apply (x) : f.prod g x = (f x, g x) := rfl @[simp] lemma fst_comp_prod : (fst S T).comp (f.prod g) = f := ext $ λ x, rfl @[simp] lemma snd_comp_prod : (snd S T).comp (f.prod g) = g := ext $ λ x, rfl lemma prod_unique (f : R →+* S × T) : ((fst S T).comp f).prod ((snd S T).comp f) = f := ext $ λ x, by simp only [prod_apply, coe_fst, coe_snd, comp_apply, prod.mk.eta] end prod section prod_map variables [non_assoc_semiring R'] [non_assoc_semiring S'] [non_assoc_semiring T] variables (f : R →+* R') (g : S →+* S') /-- `prod.map` as a `ring_hom`. -/ def prod_map : R × S →+* R' × S' := (f.comp (fst R S)).prod (g.comp (snd R S)) lemma prod_map_def : prod_map f g = (f.comp (fst R S)).prod (g.comp (snd R S)) := rfl @[simp] lemma coe_prod_map : ⇑(prod_map f g) = prod.map f g := rfl lemma prod_comp_prod_map (f : T →+* R) (g : T →+* S) (f' : R →+* R') (g' : S →+* S') : (f'.prod_map g').comp (f.prod g) = (f'.comp f).prod (g'.comp g) := rfl end prod_map end ring_hom namespace ring_equiv variables {R S R' S'} variables [non_assoc_semiring R] [non_assoc_semiring S] variables [non_assoc_semiring R'] [non_assoc_semiring S'] /-- Swapping components as an equivalence of (semi)rings. -/ def prod_comm : R × S ≃+* S × R := { ..add_equiv.prod_comm, ..mul_equiv.prod_comm } @[simp] lemma coe_prod_comm : ⇑(prod_comm : R × S ≃+* S × R) = prod.swap := rfl @[simp] lemma coe_prod_comm_symm : ⇑((prod_comm : R × S ≃+* S × R).symm) = prod.swap := rfl @[simp] lemma fst_comp_coe_prod_comm : (ring_hom.fst S R).comp ↑(prod_comm : R × S ≃+* S × R) = ring_hom.snd R S := ring_hom.ext $ λ _, rfl @[simp] lemma snd_comp_coe_prod_comm : (ring_hom.snd S R).comp ↑(prod_comm : R × S ≃+* S × R) = ring_hom.fst R S := ring_hom.ext $ λ _, rfl section variables (R R' S S') /-- Four-way commutativity of `prod`. The name matches `mul_mul_mul_comm`. -/ @[simps apply] def prod_prod_prod_comm : (R × R') × (S × S') ≃+* (R × S) × (R' × S') := { to_fun := λ rrss, ((rrss.1.1, rrss.2.1), (rrss.1.2, rrss.2.2)), inv_fun := λ rsrs, ((rsrs.1.1, rsrs.2.1), (rsrs.1.2, rsrs.2.2)), .. add_equiv.prod_prod_prod_comm R R' S S', .. mul_equiv.prod_prod_prod_comm R R' S S' } @[simp] lemma prod_prod_prod_comm_symm : (prod_prod_prod_comm R R' S S').symm = prod_prod_prod_comm R S R' S' := rfl @[simp] lemma prod_prod_prod_comm_to_add_equiv : (prod_prod_prod_comm R R' S S').to_add_equiv = add_equiv.prod_prod_prod_comm R R' S S' := rfl @[simp] lemma prod_prod_prod_comm_to_mul_equiv : (prod_prod_prod_comm R R' S S').to_mul_equiv = mul_equiv.prod_prod_prod_comm R R' S S' := rfl @[simp] lemma prod_prod_prod_comm_to_equiv : (prod_prod_prod_comm R R' S S').to_equiv = equiv.prod_prod_prod_comm R R' S S' := rfl end variables (R S) [subsingleton S] /-- A ring `R` is isomorphic to `R × S` when `S` is the zero ring -/ @[simps] def prod_zero_ring : R ≃+* R × S := { to_fun := λ x, (x, 0), inv_fun := prod.fst, map_add' := by simp, map_mul' := by simp, left_inv := λ x, rfl, right_inv := λ x, by cases x; simp } /-- A ring `R` is isomorphic to `S × R` when `S` is the zero ring -/ @[simps] def zero_ring_prod : R ≃+* S × R := { to_fun := λ x, (0, x), inv_fun := prod.snd, map_add' := by simp, map_mul' := by simp, left_inv := λ x, rfl, right_inv := λ x, by cases x; simp } end ring_equiv /-- The product of two nontrivial rings is not a domain -/ lemma false_of_nontrivial_of_product_domain (R S : Type*) [ring R] [ring S] [is_domain (R × S)] [nontrivial R] [nontrivial S] : false := begin have := no_zero_divisors.eq_zero_or_eq_zero_of_mul_eq_zero (show ((0 : R), (1 : S)) * (1, 0) = 0, by simp), rw [prod.mk_eq_zero,prod.mk_eq_zero] at this, rcases this with (⟨_,h⟩|⟨h,_⟩), { exact zero_ne_one h.symm }, { exact zero_ne_one h.symm } end /-! ### Order -/ instance [ordered_semiring α] [ordered_semiring β] : ordered_semiring (α × β) := { add_le_add_left := λ _ _, add_le_add_left, zero_le_one := ⟨zero_le_one, zero_le_one⟩, mul_le_mul_of_nonneg_left := λ a b c hab hc, ⟨mul_le_mul_of_nonneg_left hab.1 hc.1, mul_le_mul_of_nonneg_left hab.2 hc.2⟩, mul_le_mul_of_nonneg_right := λ a b c hab hc, ⟨mul_le_mul_of_nonneg_right hab.1 hc.1, mul_le_mul_of_nonneg_right hab.2 hc.2⟩, ..prod.semiring, ..prod.partial_order _ _ } instance [ordered_comm_semiring α] [ordered_comm_semiring β] : ordered_comm_semiring (α × β) := { ..prod.comm_semiring, ..prod.ordered_semiring } instance [ordered_ring α] [ordered_ring β] : ordered_ring (α × β) := { mul_nonneg := λ a b ha hb, ⟨mul_nonneg ha.1 hb.1, mul_nonneg ha.2 hb.2⟩, ..prod.ring, ..prod.ordered_semiring } instance [ordered_comm_ring α] [ordered_comm_ring β] : ordered_comm_ring (α × β) := { ..prod.comm_ring, ..prod.ordered_ring }
ec91afff01f53945938b37dd645d95f1e289334f
3f1a1d97c03bb24b55a1b9969bb4b3c619491d5a
/library/init/meta/attribute.lean
c25d7189eb8f2465d059823cdea5fbd3100b912e
[ "Apache-2.0" ]
permissive
praveenmunagapati/lean
00c3b4496cef8e758396005013b9776bb82c4f56
fc760f57d20e0a486d14bc8a08d89147b60f530c
refs/heads/master
1,630,692,342,183
1,515,626,222,000
1,515,626,222,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,865
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ prelude import init.meta.tactic init.meta.rb_map init.meta.has_reflect init.meta.lean.parser meta constant attribute.get_instances : name → tactic (list name) meta constant attribute.fingerprint : name → tactic nat meta structure user_attribute_cache_cfg (cache_ty : Type) := (mk_cache : list name → tactic cache_ty) (dependencies : list name) meta def user_attribute.dflt_cache_cfg : tactic unit := tactic.exact `(⟨λ _, pure (), []⟩ : user_attribute_cache_cfg unit) meta def user_attribute.dflt_parser : tactic unit := tactic.exact `(pure () : lean.parser unit) meta structure user_attribute (cache_ty : Type := unit) (param_ty : Type := unit) := (name : name) (descr : string) /- Optional handler that will be called after the attribute has been applied to a declaration. Failing the tactic will fail the entire `attribute/def/...` command, i.e. the attribute will not be applied after all. Declaring an `after_set` handler without a `before_unset` handler will make the attribute non-removable. -/ (after_set : option (Π (decl : _root_.name) (prio : nat) (persistent : bool), command) := none) /- Optional handler that will be called before the attribute is removed from a declaration. -/ (before_unset : option (Π (decl : _root_.name) (persistent : bool), command) := none) (cache_cfg : user_attribute_cache_cfg cache_ty . user_attribute.dflt_cache_cfg) [reflect_param : has_reflect param_ty] /- Parser that will be invoked after parsing the attribute's name. The parse result will be reflected and stored and can be retrieved with `user_attribute.get_param`. -/ (parser : lean.parser param_ty . user_attribute.dflt_parser) /- Registers a new user-defined attribute. The argument must be the name of a definition of type `user_attribute`. -/ meta def attribute.register (decl : name) : command := tactic.set_basic_attribute ``user_attribute decl tt meta constant user_attribute.get_cache {α β : Type} (attr : user_attribute α β) : tactic α meta def user_attribute.parse_reflect {α β : Type} (attr : user_attribute α β) : lean.parser expr := (λ a, attr.reflect_param a) <$> attr.parser meta constant user_attribute.get_param_untyped {α β : Type} (attr : user_attribute α β) (decl : name) : tactic expr meta constant user_attribute.set_untyped {α β : Type} [reflected β] (attr : user_attribute α β) (decl : name) (val : expr) (persistent : bool) (prio : option nat := none) : tactic unit meta def user_attribute.get_param {α β : Type} [reflected β] (attr : user_attribute α β) (n : name) : tactic β := attr.get_param_untyped n >>= tactic.eval_expr β meta def user_attribute.set {α β : Type} [reflected β] (attr : user_attribute α β) (n : name) (val : β) (persistent : bool) (prio : option nat := none) : tactic unit := attr.set_untyped n (attr.reflect_param val) persistent prio open tactic meta def register_attribute := attribute.register meta def get_attribute_cache_dyn {α : Type} [reflected α] (name : name) : tactic α := let attr : pexpr := expr.const name [] in do e ← to_expr ``(user_attribute.get_cache %%attr), t ← eval_expr (tactic α) e, t meta def mk_name_set_attr (attr_name : name) : command := do let t := `(user_attribute name_set), let v := `({name := attr_name, descr := "name_set attribute", cache_cfg := { mk_cache := λ ns, return (name_set.of_list ns), dependencies := []}} : user_attribute name_set), add_meta_definition attr_name [] t v, register_attribute attr_name meta def get_name_set_for_attr (name : name) : tactic name_set := get_attribute_cache_dyn name
f242364f81a7bf65e39681fcb4dd968672379f35
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/resolveLVal.lean
548aa3935e53d6cb2ba00ea25dd656d3d604d283
[ "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
281
lean
new_frontend namespace Foo structure A := (x : Nat) def A.doubleX (a : A) := 2 * a.x structure B extends A := (y : Nat) def f (b : B) : Nat := b.x + b.doubleX theorem ex1 : { x := 10, y := 0 : B }.doubleX = 20 := rfl theorem ex2 : f { x := 10, y := 0 } = 30 := rfl end Foo
94a23751694d9f28aab4283fe00eaed5cead9ae2
94e33a31faa76775069b071adea97e86e218a8ee
/src/topology/algebra/order/liminf_limsup.lean
e0af73d6998547b7a0ef06e0258e4c01fa71ab54
[ "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
8,380
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Yury Kudryashov -/ import order.liminf_limsup import topology.algebra.order.basic /-! # Lemmas about liminf and limsup in an order topology. -/ open filter open_locale topological_space classical universes u v variables {α : Type u} {β : Type v} section liminf_limsup section order_closed_topology variables [semilattice_sup α] [topological_space α] [order_topology α] lemma is_bounded_le_nhds (a : α) : (𝓝 a).is_bounded (≤) := (is_top_or_exists_gt a).elim (λ h, ⟨a, eventually_of_forall h⟩) (λ ⟨b, hb⟩, ⟨b, ge_mem_nhds hb⟩) lemma filter.tendsto.is_bounded_under_le {f : filter β} {u : β → α} {a : α} (h : tendsto u f (𝓝 a)) : f.is_bounded_under (≤) u := (is_bounded_le_nhds a).mono h lemma filter.tendsto.bdd_above_range_of_cofinite {u : β → α} {a : α} (h : tendsto u cofinite (𝓝 a)) : bdd_above (set.range u) := h.is_bounded_under_le.bdd_above_range_of_cofinite lemma filter.tendsto.bdd_above_range {u : ℕ → α} {a : α} (h : tendsto u at_top (𝓝 a)) : bdd_above (set.range u) := h.is_bounded_under_le.bdd_above_range lemma is_cobounded_ge_nhds (a : α) : (𝓝 a).is_cobounded (≥) := (is_bounded_le_nhds a).is_cobounded_flip lemma filter.tendsto.is_cobounded_under_ge {f : filter β} {u : β → α} {a : α} [ne_bot f] (h : tendsto u f (𝓝 a)) : f.is_cobounded_under (≥) u := h.is_bounded_under_le.is_cobounded_flip end order_closed_topology section order_closed_topology variables [semilattice_inf α] [topological_space α] [order_topology α] lemma is_bounded_ge_nhds (a : α) : (𝓝 a).is_bounded (≥) := @is_bounded_le_nhds αᵒᵈ _ _ _ a lemma filter.tendsto.is_bounded_under_ge {f : filter β} {u : β → α} {a : α} (h : tendsto u f (𝓝 a)) : f.is_bounded_under (≥) u := (is_bounded_ge_nhds a).mono h lemma filter.tendsto.bdd_below_range_of_cofinite {u : β → α} {a : α} (h : tendsto u cofinite (𝓝 a)) : bdd_below (set.range u) := h.is_bounded_under_ge.bdd_below_range_of_cofinite lemma filter.tendsto.bdd_below_range {u : ℕ → α} {a : α} (h : tendsto u at_top (𝓝 a)) : bdd_below (set.range u) := h.is_bounded_under_ge.bdd_below_range lemma is_cobounded_le_nhds (a : α) : (𝓝 a).is_cobounded (≤) := (is_bounded_ge_nhds a).is_cobounded_flip lemma filter.tendsto.is_cobounded_under_le {f : filter β} {u : β → α} {a : α} [ne_bot f] (h : tendsto u f (𝓝 a)) : f.is_cobounded_under (≤) u := h.is_bounded_under_ge.is_cobounded_flip end order_closed_topology section conditionally_complete_linear_order variables [conditionally_complete_linear_order α] theorem lt_mem_sets_of_Limsup_lt {f : filter α} {b} (h : f.is_bounded (≤)) (l : f.Limsup < b) : ∀ᶠ a in f, a < b := let ⟨c, (h : ∀ᶠ a in f, a ≤ c), hcb⟩ := exists_lt_of_cInf_lt h l in mem_of_superset h $ assume a hac, lt_of_le_of_lt hac hcb theorem gt_mem_sets_of_Liminf_gt : ∀ {f : filter α} {b}, f.is_bounded (≥) → b < f.Liminf → ∀ᶠ a in f, b < a := @lt_mem_sets_of_Limsup_lt αᵒᵈ _ variables [topological_space α] [order_topology α] /-- If the liminf and the limsup of a filter coincide, then this filter converges to their common value, at least if the filter is eventually bounded above and below. -/ theorem le_nhds_of_Limsup_eq_Liminf {f : filter α} {a : α} (hl : f.is_bounded (≤)) (hg : f.is_bounded (≥)) (hs : f.Limsup = a) (hi : f.Liminf = a) : f ≤ 𝓝 a := tendsto_order.2 $ and.intro (assume b hb, gt_mem_sets_of_Liminf_gt hg $ hi.symm ▸ hb) (assume b hb, lt_mem_sets_of_Limsup_lt hl $ hs.symm ▸ hb) theorem Limsup_nhds (a : α) : Limsup (𝓝 a) = a := cInf_eq_of_forall_ge_of_forall_gt_exists_lt (is_bounded_le_nhds a) (assume a' (h : {n : α | n ≤ a'} ∈ 𝓝 a), show a ≤ a', from @mem_of_mem_nhds α _ a _ h) (assume b (hba : a < b), show ∃c (h : {n : α | n ≤ c} ∈ 𝓝 a), c < b, from match dense_or_discrete a b with | or.inl ⟨c, hac, hcb⟩ := ⟨c, ge_mem_nhds hac, hcb⟩ | or.inr ⟨_, h⟩ := ⟨a, (𝓝 a).sets_of_superset (gt_mem_nhds hba) h, hba⟩ end) theorem Liminf_nhds : ∀ (a : α), Liminf (𝓝 a) = a := @Limsup_nhds αᵒᵈ _ _ _ /-- If a filter is converging, its limsup coincides with its limit. -/ theorem Liminf_eq_of_le_nhds {f : filter α} {a : α} [ne_bot f] (h : f ≤ 𝓝 a) : f.Liminf = a := have hb_ge : is_bounded (≥) f, from (is_bounded_ge_nhds a).mono h, have hb_le : is_bounded (≤) f, from (is_bounded_le_nhds a).mono h, le_antisymm (calc f.Liminf ≤ f.Limsup : Liminf_le_Limsup hb_le hb_ge ... ≤ (𝓝 a).Limsup : Limsup_le_Limsup_of_le h hb_ge.is_cobounded_flip (is_bounded_le_nhds a) ... = a : Limsup_nhds a) (calc a = (𝓝 a).Liminf : (Liminf_nhds a).symm ... ≤ f.Liminf : Liminf_le_Liminf_of_le h (is_bounded_ge_nhds a) hb_le.is_cobounded_flip) /-- If a filter is converging, its liminf coincides with its limit. -/ theorem Limsup_eq_of_le_nhds : ∀ {f : filter α} {a : α} [ne_bot f], f ≤ 𝓝 a → f.Limsup = a := @Liminf_eq_of_le_nhds αᵒᵈ _ _ _ /-- If a function has a limit, then its limsup coincides with its limit. -/ theorem filter.tendsto.limsup_eq {f : filter β} {u : β → α} {a : α} [ne_bot f] (h : tendsto u f (𝓝 a)) : limsup f u = a := Limsup_eq_of_le_nhds h /-- If a function has a limit, then its liminf coincides with its limit. -/ theorem filter.tendsto.liminf_eq {f : filter β} {u : β → α} {a : α} [ne_bot f] (h : tendsto u f (𝓝 a)) : liminf f u = a := Liminf_eq_of_le_nhds h /-- If the liminf and the limsup of a function coincide, then the limit of the function exists and has the same value -/ theorem tendsto_of_liminf_eq_limsup {f : filter β} {u : β → α} {a : α} (hinf : liminf f u = a) (hsup : limsup f u = a) (h : f.is_bounded_under (≤) u . is_bounded_default) (h' : f.is_bounded_under (≥) u . is_bounded_default) : tendsto u f (𝓝 a) := le_nhds_of_Limsup_eq_Liminf h h' hsup hinf /-- If a number `a` is less than or equal to the `liminf` of a function `f` at some filter and is greater than or equal to the `limsup` of `f`, then `f` tends to `a` along this filter. -/ theorem tendsto_of_le_liminf_of_limsup_le {f : filter β} {u : β → α} {a : α} (hinf : a ≤ liminf f u) (hsup : limsup f u ≤ a) (h : f.is_bounded_under (≤) u . is_bounded_default) (h' : f.is_bounded_under (≥) u . is_bounded_default) : tendsto u f (𝓝 a) := if hf : f = ⊥ then hf.symm ▸ tendsto_bot else by haveI : ne_bot f := ⟨hf⟩; exact tendsto_of_liminf_eq_limsup (le_antisymm (le_trans (liminf_le_limsup h h') hsup) hinf) (le_antisymm hsup (le_trans hinf (liminf_le_limsup h h'))) h h' /-- Assume that, for any `a < b`, a sequence can not be infinitely many times below `a` and above `b`. If it is also ultimately bounded above and below, then it has to converge. This even works if `a` and `b` are restricted to a dense subset. -/ lemma tendsto_of_no_upcrossings [densely_ordered α] {f : filter β} {u : β → α} {s : set α} (hs : dense s) (H : ∀ (a ∈ s) (b ∈ s), a < b → ¬((∃ᶠ n in f, u n < a) ∧ (∃ᶠ n in f, b < u n))) (h : f.is_bounded_under (≤) u . is_bounded_default) (h' : f.is_bounded_under (≥) u . is_bounded_default) : ∃ (c : α), tendsto u f (𝓝 c) := begin by_cases hbot : f = ⊥, { rw hbot, exact ⟨Inf ∅, tendsto_bot⟩ }, haveI : ne_bot f := ⟨hbot⟩, refine ⟨limsup f u, _⟩, apply tendsto_of_le_liminf_of_limsup_le _ le_rfl h h', by_contra' hlt, obtain ⟨a, ⟨⟨la, au⟩, as⟩⟩ : ∃ a, (f.liminf u < a ∧ a < f.limsup u) ∧ a ∈ s := dense_iff_inter_open.1 hs (set.Ioo (f.liminf u) (f.limsup u)) is_open_Ioo (set.nonempty_Ioo.2 hlt), obtain ⟨b, ⟨⟨ab, bu⟩, bs⟩⟩ : ∃ b, (a < b ∧ b < f.limsup u) ∧ b ∈ s := dense_iff_inter_open.1 hs (set.Ioo a (f.limsup u)) is_open_Ioo (set.nonempty_Ioo.2 au), have A : ∃ᶠ n in f, u n < a := frequently_lt_of_liminf_lt (is_bounded.is_cobounded_ge h) la, have B : ∃ᶠ n in f, b < u n := frequently_lt_of_lt_limsup (is_bounded.is_cobounded_le h') bu, exact H a as b bs ab ⟨A, B⟩, end end conditionally_complete_linear_order end liminf_limsup
49c9368f26a2a77632a5192f916972e1f75c2faf
8b9f17008684d796c8022dab552e42f0cb6fb347
/library/data/quotient/util.lean
f2d6ef3d6c2d32248deb0ac442b44b99706504c9
[ "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
7,105
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: data.quotient.util Author: Floris van Doorn -/ import logic ..prod algebra.relation import tools.fake_simplifier open prod eq.ops open fake_simplifier namespace quotient /- auxiliary facts about products -/ variables {A B : Type} /- flip -/ definition flip (a : A × B) : B × A := pair (pr2 a) (pr1 a) theorem flip_def (a : A × B) : flip a = pair (pr2 a) (pr1 a) := rfl theorem flip_pair (a : A) (b : B) : flip (pair a b) = pair b a := rfl theorem flip_pr1 (a : A × B) : pr1 (flip a) = pr2 a := rfl theorem flip_pr2 (a : A × B) : pr2 (flip a) = pr1 a := rfl theorem flip_flip (a : A × B) : flip (flip a) = a := destruct a (take x y, rfl) theorem P_flip {P : A → B → Prop} (a : A × B) (H : P (pr1 a) (pr2 a)) : P (pr2 (flip a)) (pr1 (flip a)) := (flip_pr1 a)⁻¹ ▸ (flip_pr2 a)⁻¹ ▸ H theorem flip_inj {a b : A × B} (H : flip a = flip b) : a = b := have H2 : flip (flip a) = flip (flip b), from congr_arg flip H, show a = b, from (flip_flip a) ▸ (flip_flip b) ▸ H2 /- coordinatewise unary maps -/ definition map_pair (f : A → B) (a : A × A) : B × B := pair (f (pr1 a)) (f (pr2 a)) theorem map_pair_def (f : A → B) (a : A × A) : map_pair f a = pair (f (pr1 a)) (f (pr2 a)) := rfl theorem map_pair_pair (f : A → B) (a a' : A) : map_pair f (pair a a') = pair (f a) (f a') := (pr1.mk a a') ▸ (pr2.mk a a') ▸ rfl theorem map_pair_pr1 (f : A → B) (a : A × A) : pr1 (map_pair f a) = f (pr1 a) := !pr1.mk theorem map_pair_pr2 (f : A → B) (a : A × A) : pr2 (map_pair f a) = f (pr2 a) := !pr2.mk /- coordinatewise binary maps -/ definition map_pair2 {A B C : Type} (f : A → B → C) (a : A × A) (b : B × B) : C × C := pair (f (pr1 a) (pr1 b)) (f (pr2 a) (pr2 b)) theorem map_pair2_def {A B C : Type} (f : A → B → C) (a : A × A) (b : B × B) : map_pair2 f a b = pair (f (pr1 a) (pr1 b)) (f (pr2 a) (pr2 b)) := rfl theorem map_pair2_pair {A B C : Type} (f : A → B → C) (a a' : A) (b b' : B) : map_pair2 f (pair a a') (pair b b') = pair (f a b) (f a' b') := calc map_pair2 f (pair a a') (pair b b') = pair (f (pr1 (pair a a')) b) (f (pr2 (pair a a')) (pr2 (pair b b'))) : {pr1.mk b b'} ... = pair (f (pr1 (pair a a')) b) (f (pr2 (pair a a')) b') : {pr2.mk b b'} ... = pair (f (pr1 (pair a a')) b) (f a' b') : {pr2.mk a a'} ... = pair (f a b) (f a' b') : {pr1.mk a a'} theorem map_pair2_pr1 {A B C : Type} (f : A → B → C) (a : A × A) (b : B × B) : pr1 (map_pair2 f a b) = f (pr1 a) (pr1 b) := !pr1.mk theorem map_pair2_pr2 {A B C : Type} (f : A → B → C) (a : A × A) (b : B × B) : pr2 (map_pair2 f a b) = f (pr2 a) (pr2 b) := !pr2.mk theorem map_pair2_flip {A B C : Type} (f : A → B → C) (a : A × A) (b : B × B) : flip (map_pair2 f a b) = map_pair2 f (flip a) (flip b) := have Hx : pr1 (flip (map_pair2 f a b)) = pr1 (map_pair2 f (flip a) (flip b)), from calc pr1 (flip (map_pair2 f a b)) = pr2 (map_pair2 f a b) : flip_pr1 _ ... = f (pr2 a) (pr2 b) : map_pair2_pr2 f a b ... = f (pr1 (flip a)) (pr2 b) : {(flip_pr1 a)⁻¹} ... = f (pr1 (flip a)) (pr1 (flip b)) : {(flip_pr1 b)⁻¹} ... = pr1 (map_pair2 f (flip a) (flip b)) : (map_pair2_pr1 f _ _)⁻¹, have Hy : pr2 (flip (map_pair2 f a b)) = pr2 (map_pair2 f (flip a) (flip b)), from calc pr2 (flip (map_pair2 f a b)) = pr1 (map_pair2 f a b) : flip_pr2 _ ... = f (pr1 a) (pr1 b) : map_pair2_pr1 f a b ... = f (pr2 (flip a)) (pr1 b) : {flip_pr2 a} ... = f (pr2 (flip a)) (pr2 (flip b)) : {flip_pr2 b} ... = pr2 (map_pair2 f (flip a) (flip b)) : (map_pair2_pr2 f _ _)⁻¹, pair_eq Hx Hy -- add_rewrite flip_pr1 flip_pr2 flip_pair -- add_rewrite map_pair_pr1 map_pair_pr2 map_pair_pair -- add_rewrite map_pair2_pr1 map_pair2_pr2 map_pair2_pair theorem map_pair2_comm {A B : Type} {f : A → A → B} (Hcomm : ∀a b : A, f a b = f b a) (v w : A × A) : map_pair2 f v w = map_pair2 f w v := have Hx : pr1 (map_pair2 f v w) = pr1 (map_pair2 f w v), from calc pr1 (map_pair2 f v w) = f (pr1 v) (pr1 w) : map_pair2_pr1 f v w ... = f (pr1 w) (pr1 v) : Hcomm _ _ ... = pr1 (map_pair2 f w v) : (map_pair2_pr1 f w v)⁻¹, have Hy : pr2 (map_pair2 f v w) = pr2 (map_pair2 f w v), from calc pr2 (map_pair2 f v w) = f (pr2 v) (pr2 w) : map_pair2_pr2 f v w ... = f (pr2 w) (pr2 v) : Hcomm _ _ ... = pr2 (map_pair2 f w v) : (map_pair2_pr2 f w v)⁻¹, pair_eq Hx Hy theorem map_pair2_assoc {A : Type} {f : A → A → A} (Hassoc : ∀a b c : A, f (f a b) c = f a (f b c)) (u v w : A × A) : map_pair2 f (map_pair2 f u v) w = map_pair2 f u (map_pair2 f v w) := have Hx : pr1 (map_pair2 f (map_pair2 f u v) w) = pr1 (map_pair2 f u (map_pair2 f v w)), from calc pr1 (map_pair2 f (map_pair2 f u v) w) = f (pr1 (map_pair2 f u v)) (pr1 w) : map_pair2_pr1 f _ _ ... = f (f (pr1 u) (pr1 v)) (pr1 w) : {map_pair2_pr1 f _ _} ... = f (pr1 u) (f (pr1 v) (pr1 w)) : Hassoc (pr1 u) (pr1 v) (pr1 w) ... = f (pr1 u) (pr1 (map_pair2 f v w)) : {(map_pair2_pr1 f _ _)⁻¹} ... = pr1 (map_pair2 f u (map_pair2 f v w)) : (map_pair2_pr1 f _ _)⁻¹, have Hy : pr2 (map_pair2 f (map_pair2 f u v) w) = pr2 (map_pair2 f u (map_pair2 f v w)), from calc pr2 (map_pair2 f (map_pair2 f u v) w) = f (pr2 (map_pair2 f u v)) (pr2 w) : map_pair2_pr2 f _ _ ... = f (f (pr2 u) (pr2 v)) (pr2 w) : {map_pair2_pr2 f _ _} ... = f (pr2 u) (f (pr2 v) (pr2 w)) : Hassoc (pr2 u) (pr2 v) (pr2 w) ... = f (pr2 u) (pr2 (map_pair2 f v w)) : {map_pair2_pr2 f _ _} ... = pr2 (map_pair2 f u (map_pair2 f v w)) : (map_pair2_pr2 f _ _)⁻¹, pair_eq Hx Hy theorem map_pair2_id_right {A B : Type} {f : A → B → A} {e : B} (Hid : ∀a : A, f a e = a) (v : A × A) : map_pair2 f v (pair e e) = v := have Hx : pr1 (map_pair2 f v (pair e e)) = pr1 v, from (calc pr1 (map_pair2 f v (pair e e)) = f (pr1 v) (pr1 (pair e e)) : by simp ... = f (pr1 v) e : by simp ... = pr1 v : Hid (pr1 v)), have Hy : pr2 (map_pair2 f v (pair e e)) = pr2 v, from (calc pr2 (map_pair2 f v (pair e e)) = f (pr2 v) (pr2 (pair e e)) : by simp ... = f (pr2 v) e : by simp ... = pr2 v : Hid (pr2 v)), prod.equal Hx Hy theorem map_pair2_id_left {A B : Type} {f : B → A → A} {e : B} (Hid : ∀a : A, f e a = a) (v : A × A) : map_pair2 f (pair e e) v = v := have Hx : pr1 (map_pair2 f (pair e e) v) = pr1 v, from calc pr1 (map_pair2 f (pair e e) v) = f (pr1 (pair e e)) (pr1 v) : by simp ... = f e (pr1 v) : by simp ... = pr1 v : Hid (pr1 v), have Hy : pr2 (map_pair2 f (pair e e) v) = pr2 v, from calc pr2 (map_pair2 f (pair e e) v) = f (pr2 (pair e e)) (pr2 v) : by simp ... = f e (pr2 v) : by simp ... = pr2 v : Hid (pr2 v), prod.equal Hx Hy end quotient
f90d89db0533677c7e7d40f027a7fd36565acdd0
367134ba5a65885e863bdc4507601606690974c1
/src/tactic/ring.lean
cd8951a8c48dde1debb0fa9a67630a260abae956
[ "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
29,194
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 tactic.norm_num import data.int.range /-! # `ring` Evaluate expressions in the language of commutative (semi)rings. Based on <http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf> . -/ namespace tactic namespace ring /-- The normal form that `ring` uses is mediated by the function `horner a x n b := a * x ^ n + b`. The reason we use a definition rather than the (more readable) expression on the right is because this expression contains a number of typeclass arguments in different positions, while `horner` contains only one `comm_semiring` instance at the top level. See also `horner_expr` for a description of normal form. -/ def horner {α} [comm_semiring α] (a x : α) (n : ℕ) (b : α) := a * x ^ n + b /-- This cache contains data required by the `ring` tactic during execution. -/ meta structure cache := (α : expr) (univ : level) (comm_semiring_inst : expr) (red : transparency) (ic : ref instance_cache) (nc : ref instance_cache) (atoms : ref (buffer expr)) /-- The monad that `ring` works in. This is a reader monad containing a mutable cache (using `ref` for mutability), as well as the list of atoms-up-to-defeq encountered thus far, used for atom sorting. -/ @[derive [monad, alternative]] meta def ring_m (α : Type) : Type := reader_t cache tactic α /-- Get the `ring` data from the monad. -/ meta def get_cache : ring_m cache := reader_t.read /-- Get an already encountered atom by its index. -/ meta def get_atom (n : ℕ) : ring_m expr := ⟨λ c, do es ← read_ref c.atoms, pure (es.read' n)⟩ /-- Get the index corresponding to an atomic expression, if it has already been encountered, or put it in the list of atoms and return the new index, otherwise. -/ meta def add_atom (e : expr) : ring_m ℕ := ⟨λ c, do let red := c.red, es ← read_ref c.atoms, es.iterate failed (λ n e' t, t <|> (is_def_eq e e' red $> n)) <|> (es.size <$ write_ref c.atoms (es.push_back e))⟩ /-- Lift a tactic into the `ring_m` monad. -/ @[inline] meta def lift {α} (m : tactic α) : ring_m α := reader_t.lift m /-- Run a `ring_m` tactic in the tactic monad. This version of `ring_m.run` uses an external atoms ref, so that subexpressions can be named across multiple `ring_m` calls. -/ meta def ring_m.run' (red : transparency) (atoms : ref (buffer expr)) (e : expr) {α} (m : ring_m α) : tactic α := do α ← infer_type e, u ← mk_meta_univ, infer_type α >>= unify (expr.sort (level.succ u)), u ← get_univ_assignment u, ic ← mk_instance_cache α, (ic, c) ← ic.get ``comm_semiring, nc ← mk_instance_cache `(ℕ), using_new_ref ic $ λ r, using_new_ref nc $ λ nr, reader_t.run m ⟨α, u, c, red, r, nr, atoms⟩ /-- Run a `ring_m` tactic in the tactic monad. -/ meta def ring_m.run (red : transparency) (e : expr) {α} (m : ring_m α) : tactic α := using_new_ref mk_buffer $ λ atoms, ring_m.run' red atoms e m /-- Lift an instance cache tactic (probably from `norm_num`) to the `ring_m` monad. This version is abstract over the instance cache in question (either the ring `α`, or `ℕ` for exponents). -/ @[inline] meta def ic_lift' (icf : cache → ref instance_cache) {α} (f : instance_cache → tactic (instance_cache × α)) : ring_m α := ⟨λ c, do let r := icf c, ic ← read_ref r, (ic', a) ← f ic, a <$ write_ref r ic'⟩ /-- Lift an instance cache tactic (probably from `norm_num`) to the `ring_m` monad. This uses the instance cache corresponding to the ring `α`. -/ @[inline] meta def ic_lift {α} : (instance_cache → tactic (instance_cache × α)) → ring_m α := ic_lift' cache.ic /-- Lift an instance cache tactic (probably from `norm_num`) to the `ring_m` monad. This uses the instance cache corresponding to `ℕ`, which is used for computations in the exponent. -/ @[inline] meta def nc_lift {α} : (instance_cache → tactic (instance_cache × α)) → ring_m α := ic_lift' cache.nc /-- Apply a theorem that expects a `comm_semiring` instance. This is a special case of `ic_lift mk_app`, but it comes up often because `horner` and all its theorems have this assumption; it also does not require the tactic monad which improves access speed a bit. -/ meta def cache.cs_app (c : cache) (n : name) : list expr → expr := (@expr.const tt n [c.univ] c.α c.comm_semiring_inst).mk_app /-- Every expression in the language of commutative semirings can be viewed as a sum of monomials, where each monomial is a product of powers of atoms. We fix a global order on atoms (up to definitional equality), and then separate the terms according to their smallest atom. So the top level expression is `a * x^n + b` where `x` is the smallest atom and `n > 0` is a numeral, and `n` is maximal (so `a` contains at least one monomial not containing an `x`), and `b` contains no monomials with an `x` (hence all atoms in `b` are larger than `x`). If there is no `x` satisfying these constraints, then the expression must be a numeral. Even though we are working over rings, we allow rational constants when these can be interpreted in the ring, so we can solve problems like `x / 3 = 1 / 3 * x` even though these are not technically in the language of rings. These constraints ensure that there is a unique normal form for each ring expression, and so the algorithm is simply to calculate the normal form of each side and compare for equality. To allow us to efficiently pattern match on normal forms, we maintain this inductive type that holds a normalized expression together with its structure. All the `expr`s in this type could be removed without loss of information, and conversely the `horner_expr` structure and the `ℕ` and `ℚ` values can be recovered from the top level `expr`, but we keep both in order to keep proof producing normalization functions efficient. -/ meta inductive horner_expr : Type | const (e : expr) (coeff : ℚ) : horner_expr | xadd (e : expr) (a : horner_expr) (x : expr × ℕ) (n : expr × ℕ) (b : horner_expr) : horner_expr /-- Get the expression corresponding to a `horner_expr`. This can be calculated recursively from the structure, but we cache the exprs in all subterms so that this function can be computed in constant time. -/ meta def horner_expr.e : horner_expr → expr | (horner_expr.const e _) := e | (horner_expr.xadd e _ _ _ _) := e /-- Is this expr the constant `0`? -/ meta def horner_expr.is_zero : horner_expr → bool | (horner_expr.const _ c) := c = 0 | _ := ff meta instance : has_coe horner_expr expr := ⟨horner_expr.e⟩ meta instance : has_coe_to_fun horner_expr := ⟨_, λ e, ((e : expr) : expr → expr)⟩ /-- Construct a `xadd` node, generating the cached expr using the input cache. -/ meta def horner_expr.xadd' (c : cache) (a : horner_expr) (x : expr × ℕ) (n : expr × ℕ) (b : horner_expr) : horner_expr := horner_expr.xadd (c.cs_app ``horner [a, x.1, n.1, b]) a x n b open horner_expr /-- Pretty printer for `horner_expr`. -/ meta def horner_expr.to_string : horner_expr → string | (const e c) := to_string (e, c) | (xadd e a x (_, n) b) := "(" ++ a.to_string ++ ") * (" ++ to_string x.1 ++ ")^" ++ to_string n ++ " + " ++ b.to_string /-- Pretty printer for `horner_expr`. -/ meta def horner_expr.pp : horner_expr → tactic format | (const e c) := pp (e, c) | (xadd e a x (_, n) b) := do pa ← a.pp, pb ← b.pp, px ← pp x.1, return $ "(" ++ pa ++ ") * (" ++ px ++ ")^" ++ to_string n ++ " + " ++ pb meta instance : has_to_tactic_format horner_expr := ⟨horner_expr.pp⟩ /-- Reflexivity conversion for a `horner_expr`. -/ meta def horner_expr.refl_conv (e : horner_expr) : ring_m (horner_expr × expr) := do p ← lift $ mk_eq_refl e, return (e, p) theorem zero_horner {α} [comm_semiring α] (x n b) : @horner α _ 0 x n b = b := by simp [horner] theorem horner_horner {α} [comm_semiring α] (a₁ x n₁ n₂ b n') (h : n₁ + n₂ = n') : @horner α _ (horner a₁ x n₁ 0) x n₂ b = horner a₁ x n' b := by simp [h.symm, horner, pow_add, mul_assoc] /-- Evaluate `horner a n x b` where `a` and `b` are already in normal form. -/ meta def eval_horner : horner_expr → expr × ℕ → expr × ℕ → horner_expr → ring_m (horner_expr × expr) | ha@(const a coeff) x n b := do c ← get_cache, if coeff = 0 then return (b, c.cs_app ``zero_horner [x.1, n.1, b]) else (xadd' c ha x n b).refl_conv | ha@(xadd a a₁ x₁ n₁ b₁) x n b := do c ← get_cache, if x₁.2 = x.2 ∧ b₁.e.to_nat = some 0 then do (n', h) ← nc_lift $ λ nc, norm_num.prove_add_nat' nc n₁.1 n.1, return (xadd' c a₁ x (n', n₁.2 + n.2) b, c.cs_app ``horner_horner [a₁, x.1, n₁.1, n.1, b, n', h]) else (xadd' c ha x n b).refl_conv theorem const_add_horner {α} [comm_semiring α] (k a x n b b') (h : k + b = b') : k + @horner α _ a x n b = horner a x n b' := by simp [h.symm, horner]; cc theorem horner_add_const {α} [comm_semiring α] (a x n b k b') (h : b + k = b') : @horner α _ a x n b + k = horner a x n b' := by simp [h.symm, horner, add_assoc] theorem horner_add_horner_lt {α} [comm_semiring α] (a₁ x n₁ b₁ a₂ n₂ b₂ k a' b') (h₁ : n₁ + k = n₂) (h₂ : (a₁ + horner a₂ x k 0 : α) = a') (h₃ : b₁ + b₂ = b') : @horner α _ a₁ x n₁ b₁ + horner a₂ x n₂ b₂ = horner a' x n₁ b' := by simp [h₂.symm, h₃.symm, h₁.symm, horner, pow_add, mul_add, mul_comm, mul_left_comm]; cc theorem horner_add_horner_gt {α} [comm_semiring α] (a₁ x n₁ b₁ a₂ n₂ b₂ k a' b') (h₁ : n₂ + k = n₁) (h₂ : (horner a₁ x k 0 + a₂ : α) = a') (h₃ : b₁ + b₂ = b') : @horner α _ a₁ x n₁ b₁ + horner a₂ x n₂ b₂ = horner a' x n₂ b' := by simp [h₂.symm, h₃.symm, h₁.symm, horner, pow_add, mul_add, mul_comm, mul_left_comm]; cc theorem horner_add_horner_eq {α} [comm_semiring α] (a₁ x n b₁ a₂ b₂ a' b' t) (h₁ : a₁ + a₂ = a') (h₂ : b₁ + b₂ = b') (h₃ : horner a' x n b' = t) : @horner α _ a₁ x n b₁ + horner a₂ x n b₂ = t := by simp [h₃.symm, h₂.symm, h₁.symm, horner, add_mul, mul_comm]; cc /-- Evaluate `a + b` where `a` and `b` are already in normal form. -/ meta def eval_add : horner_expr → horner_expr → ring_m (horner_expr × expr) | (const e₁ c₁) (const e₂ c₂) := ic_lift $ λ ic, do let n := c₁ + c₂, (ic, e) ← ic.of_rat n, (ic, p) ← norm_num.prove_add_rat ic e₁ e₂ e c₁ c₂ n, return (ic, const e n, p) | he₁@(const e₁ c₁) he₂@(xadd e₂ a x n b) := do c ← get_cache, if c₁ = 0 then ic_lift $ λ ic, do (ic, p) ← ic.mk_app ``zero_add [e₂], return (ic, he₂, p) else do (b', h) ← eval_add he₁ b, return (xadd' c a x n b', c.cs_app ``const_add_horner [e₁, a, x.1, n.1, b, b', h]) | he₁@(xadd e₁ a x n b) he₂@(const e₂ c₂) := do c ← get_cache, if c₂ = 0 then ic_lift $ λ ic, do (ic, p) ← ic.mk_app ``add_zero [e₁], return (ic, he₁, p) else do (b', h) ← eval_add b he₂, return (xadd' c a x n b', c.cs_app ``horner_add_const [a, x.1, n.1, b, e₂, b', h]) | he₁@(xadd e₁ a₁ x₁ n₁ b₁) he₂@(xadd e₂ a₂ x₂ n₂ b₂) := do c ← get_cache, if x₁.2 < x₂.2 then do (b', h) ← eval_add b₁ he₂, return (xadd' c a₁ x₁ n₁ b', c.cs_app ``horner_add_const [a₁, x₁.1, n₁.1, b₁, e₂, b', h]) else if x₁.2 ≠ x₂.2 then do (b', h) ← eval_add he₁ b₂, return (xadd' c a₂ x₂ n₂ b', c.cs_app ``const_add_horner [e₁, a₂, x₂.1, n₂.1, b₂, b', h]) else if n₁.2 < n₂.2 then do let k := n₂.2 - n₁.2, (ek, h₁) ← nc_lift (λ nc, do (nc, ek) ← nc.of_nat k, (nc, h₁) ← norm_num.prove_add_nat nc n₁.1 ek n₂.1, return (nc, ek, h₁)), α0 ← ic_lift $ λ ic, ic.mk_app ``has_zero.zero [], (a', h₂) ← eval_add a₁ (xadd' c a₂ x₁ (ek, k) (const α0 0)), (b', h₃) ← eval_add b₁ b₂, return (xadd' c a' x₁ n₁ b', c.cs_app ``horner_add_horner_lt [a₁, x₁.1, n₁.1, b₁, a₂, n₂.1, b₂, ek, a', b', h₁, h₂, h₃]) else if n₁.2 ≠ n₂.2 then do let k := n₁.2 - n₂.2, (ek, h₁) ← nc_lift (λ nc, do (nc, ek) ← nc.of_nat k, (nc, h₁) ← norm_num.prove_add_nat nc n₂.1 ek n₁.1, return (nc, ek, h₁)), α0 ← ic_lift $ λ ic, ic.mk_app ``has_zero.zero [], (a', h₂) ← eval_add (xadd' c a₁ x₁ (ek, k) (const α0 0)) a₂, (b', h₃) ← eval_add b₁ b₂, return (xadd' c a' x₁ n₂ b', c.cs_app ``horner_add_horner_gt [a₁, x₁.1, n₁.1, b₁, a₂, n₂.1, b₂, ek, a', b', h₁, h₂, h₃]) else do (a', h₁) ← eval_add a₁ a₂, (b', h₂) ← eval_add b₁ b₂, (t, h₃) ← eval_horner a' x₁ n₁ b', return (t, c.cs_app ``horner_add_horner_eq [a₁, x₁.1, n₁.1, b₁, a₂, b₂, a', b', t, h₁, h₂, h₃]) theorem horner_neg {α} [comm_ring α] (a x n b a' b') (h₁ : -a = a') (h₂ : -b = b') : -@horner α _ a x n b = horner a' x n b' := by simp [h₂.symm, h₁.symm, horner]; cc /-- Evaluate `-a` where `a` is already in normal form. -/ meta def eval_neg : horner_expr → ring_m (horner_expr × expr) | (const e coeff) := do (e', p) ← ic_lift $ λ ic, norm_num.prove_neg ic e, return (const e' (-coeff), p) | (xadd e a x n b) := do c ← get_cache, (a', h₁) ← eval_neg a, (b', h₂) ← eval_neg b, p ← ic_lift $ λ ic, ic.mk_app ``horner_neg [a, x.1, n.1, b, a', b', h₁, h₂], return (xadd' c a' x n b', p) theorem horner_const_mul {α} [comm_semiring α] (c a x n b a' b') (h₁ : c * a = a') (h₂ : c * b = b') : c * @horner α _ a x n b = horner a' x n b' := by simp [h₂.symm, h₁.symm, horner, mul_add, mul_assoc] theorem horner_mul_const {α} [comm_semiring α] (a x n b c a' b') (h₁ : a * c = a') (h₂ : b * c = b') : @horner α _ a x n b * c = horner a' x n b' := by simp [h₂.symm, h₁.symm, horner, add_mul, mul_right_comm] /-- Evaluate `k * a` where `k` is a rational numeral and `a` is in normal form. -/ meta def eval_const_mul (k : expr × ℚ) : horner_expr → ring_m (horner_expr × expr) | (const e coeff) := do (e', p) ← ic_lift $ λ ic, norm_num.prove_mul_rat ic k.1 e k.2 coeff, return (const e' (k.2 * coeff), p) | (xadd e a x n b) := do c ← get_cache, (a', h₁) ← eval_const_mul a, (b', h₂) ← eval_const_mul b, return (xadd' c a' x n b', c.cs_app ``horner_const_mul [k.1, a, x.1, n.1, b, a', b', h₁, h₂]) theorem horner_mul_horner_zero {α} [comm_semiring α] (a₁ x n₁ b₁ a₂ n₂ aa t) (h₁ : @horner α _ a₁ x n₁ b₁ * a₂ = aa) (h₂ : horner aa x n₂ 0 = t) : horner a₁ x n₁ b₁ * horner a₂ x n₂ 0 = t := by rw [← h₂, ← h₁]; simp [horner, mul_add, mul_comm, mul_left_comm, mul_assoc] theorem horner_mul_horner {α} [comm_semiring α] (a₁ x n₁ b₁ a₂ n₂ b₂ aa haa ab bb t) (h₁ : @horner α _ a₁ x n₁ b₁ * a₂ = aa) (h₂ : horner aa x n₂ 0 = haa) (h₃ : a₁ * b₂ = ab) (h₄ : b₁ * b₂ = bb) (H : haa + horner ab x n₁ bb = t) : horner a₁ x n₁ b₁ * horner a₂ x n₂ b₂ = t := by rw [← H, ← h₂, ← h₁, ← h₃, ← h₄]; simp [horner, mul_add, mul_comm, mul_left_comm, mul_assoc] /-- Evaluate `a * b` where `a` and `b` are in normal form. -/ meta def eval_mul : horner_expr → horner_expr → ring_m (horner_expr × expr) | (const e₁ c₁) (const e₂ c₂) := do (e', p) ← ic_lift $ λ ic, norm_num.prove_mul_rat ic e₁ e₂ c₁ c₂, return (const e' (c₁ * c₂), p) | (const e₁ c₁) e₂ := if c₁ = 0 then do c ← get_cache, α0 ← ic_lift $ λ ic, ic.mk_app ``has_zero.zero [], p ← ic_lift $ λ ic, ic.mk_app ``zero_mul [e₂], return (const α0 0, p) else if c₁ = 1 then do p ← ic_lift $ λ ic, ic.mk_app ``one_mul [e₂], return (e₂, p) else eval_const_mul (e₁, c₁) e₂ | e₁ he₂@(const e₂ c₂) := do p₁ ← ic_lift $ λ ic, ic.mk_app ``mul_comm [e₁, e₂], (e', p₂) ← eval_mul he₂ e₁, p ← lift $ mk_eq_trans p₁ p₂, return (e', p) | he₁@(xadd e₁ a₁ x₁ n₁ b₁) he₂@(xadd e₂ a₂ x₂ n₂ b₂) := do c ← get_cache, if x₁.2 < x₂.2 then do (a', h₁) ← eval_mul a₁ he₂, (b', h₂) ← eval_mul b₁ he₂, return (xadd' c a' x₁ n₁ b', c.cs_app ``horner_mul_const [a₁, x₁.1, n₁.1, b₁, e₂, a', b', h₁, h₂]) else if x₁.2 ≠ x₂.2 then do (a', h₁) ← eval_mul he₁ a₂, (b', h₂) ← eval_mul he₁ b₂, return (xadd' c a' x₂ n₂ b', c.cs_app ``horner_const_mul [e₁, a₂, x₂.1, n₂.1, b₂, a', b', h₁, h₂]) else do (aa, h₁) ← eval_mul he₁ a₂, α0 ← ic_lift $ λ ic, ic.mk_app ``has_zero.zero [], (haa, h₂) ← eval_horner aa x₁ n₂ (const α0 0), if b₂.is_zero then return (haa, c.cs_app ``horner_mul_horner_zero [a₁, x₁.1, n₁.1, b₁, a₂, n₂.1, aa, haa, h₁, h₂]) else do (ab, h₃) ← eval_mul a₁ b₂, (bb, h₄) ← eval_mul b₁ b₂, (t, H) ← eval_add haa (xadd' c ab x₁ n₁ bb), return (t, c.cs_app ``horner_mul_horner [a₁, x₁.1, n₁.1, b₁, a₂, n₂.1, b₂, aa, haa, ab, bb, t, h₁, h₂, h₃, h₄, H]) theorem horner_pow {α} [comm_semiring α] (a x n m n' a') (h₁ : n * m = n') (h₂ : a ^ m = a') : @horner α _ a x n 0 ^ m = horner a' x n' 0 := by simp [h₁.symm, h₂.symm, horner, mul_pow, pow_mul] theorem pow_succ {α} [comm_semiring α] (a n b c) (h₁ : (a:α) ^ n = b) (h₂ : b * a = c) : a ^ (n + 1) = c := by rw [← h₂, ← h₁, pow_succ'] /-- Evaluate `a ^ n` where `a` is in normal form and `n` is a natural numeral. -/ meta def eval_pow : horner_expr → expr × ℕ → ring_m (horner_expr × expr) | e (_, 0) := do c ← get_cache, α1 ← ic_lift $ λ ic, ic.mk_app ``has_one.one [], p ← ic_lift $ λ ic, ic.mk_app ``pow_zero [e], return (const α1 1, p) | e (_, 1) := do p ← ic_lift $ λ ic, ic.mk_app ``pow_one [e], return (e, p) | (const e coeff) (e₂, m) := ic_lift $ λ ic, do (ic, e', p) ← norm_num.prove_pow e coeff ic e₂, return (ic, const e' (coeff ^ m), p) | he@(xadd e a x n b) m := do c ← get_cache, match b.e.to_nat with | some 0 := do (n', h₁) ← nc_lift $ λ nc, norm_num.prove_mul_rat nc n.1 m.1 n.2 m.2, (a', h₂) ← eval_pow a m, α0 ← ic_lift $ λ ic, ic.mk_app ``has_zero.zero [], return (xadd' c a' x (n', n.2 * m.2) (const α0 0), c.cs_app ``horner_pow [a, x.1, n.1, m.1, n', a', h₁, h₂]) | _ := do e₂ ← nc_lift $ λ nc, nc.of_nat (m.2-1), (tl, hl) ← eval_pow he (e₂, m.2-1), (t, p₂) ← eval_mul tl he, return (t, c.cs_app ``pow_succ [e, e₂, tl, t, hl, p₂]) end theorem horner_atom {α} [comm_semiring α] (x : α) : x = horner 1 x 1 0 := by simp [horner] /-- Evaluate `a` where `a` is an atom. -/ meta def eval_atom (e : expr) : ring_m (horner_expr × expr) := do c ← get_cache, i ← add_atom e, α0 ← ic_lift $ λ ic, ic.mk_app ``has_zero.zero [], α1 ← ic_lift $ λ ic, ic.mk_app ``has_one.one [], return (xadd' c (const α1 1) (e, i) (`(1), 1) (const α0 0), c.cs_app ``horner_atom [e]) lemma subst_into_pow {α} [monoid α] (l r tl tr t) (prl : (l : α) = tl) (prr : (r : ℕ) = tr) (prt : tl ^ tr = t) : l ^ r = t := by rw [prl, prr, prt] lemma unfold_sub {α} [add_group α] (a b c : α) (h : a + -b = c) : a - b = c := by rw [sub_eq_add_neg, h] lemma unfold_div {α} [division_ring α] (a b c : α) (h : a * b⁻¹ = c) : a / b = c := by rw [div_eq_mul_inv, h] /-- Evaluate a ring expression `e` recursively to normal form, together with a proof of equality. -/ meta def eval : expr → ring_m (horner_expr × expr) | `(%%e₁ + %%e₂) := do (e₁', p₁) ← eval e₁, (e₂', p₂) ← eval e₂, (e', p') ← eval_add e₁' e₂', p ← ic_lift $ λ ic, ic.mk_app ``norm_num.subst_into_add [e₁, e₂, e₁', e₂', e', p₁, p₂, p'], return (e', p) | e@`(@has_sub.sub %%α %%inst %%e₁ %%e₂) := mcond (succeeds (lift $ mk_app ``comm_ring [α] >>= mk_instance)) (do e₂' ← ic_lift $ λ ic, ic.mk_app ``has_neg.neg [e₂], e ← ic_lift $ λ ic, ic.mk_app ``has_add.add [e₁, e₂'], (e', p) ← eval e, p' ← ic_lift $ λ ic, ic.mk_app ``unfold_sub [e₁, e₂, e', p], return (e', p')) (eval_atom e) | `(- %%e) := do (e₁, p₁) ← eval e, (e₂, p₂) ← eval_neg e₁, p ← ic_lift $ λ ic, ic.mk_app ``norm_num.subst_into_neg [e, e₁, e₂, p₁, p₂], return (e₂, p) | `(%%e₁ * %%e₂) := do (e₁', p₁) ← eval e₁, (e₂', p₂) ← eval e₂, (e', p') ← eval_mul e₁' e₂', p ← ic_lift $ λ ic, ic.mk_app ``norm_num.subst_into_mul [e₁, e₂, e₁', e₂', e', p₁, p₂, p'], return (e', p) | e@`(has_inv.inv %%_) := (do (e', p) ← lift $ norm_num.derive e <|> refl_conv e, n ← lift $ e'.to_rat, return (const e' n, p)) <|> eval_atom e | e@`(@has_div.div _ %%inst %%e₁ %%e₂) := mcond (succeeds (do inst' ← ic_lift $ λ ic, ic.mk_app ``div_inv_monoid.to_has_div [], lift $ is_def_eq inst inst')) (do e₂' ← ic_lift $ λ ic, ic.mk_app ``has_inv.inv [e₂], e ← ic_lift $ λ ic, ic.mk_app ``has_mul.mul [e₁, e₂'], (e', p) ← eval e, p' ← ic_lift $ λ ic, ic.mk_app ``unfold_div [e₁, e₂, e', p], return (e', p')) (eval_atom e) | e@`(@has_pow.pow _ _ %%P %%e₁ %%e₂) := do (e₂', p₂) ← lift $ norm_num.derive e₂ <|> refl_conv e₂, match e₂'.to_nat, P with | some k, `(monoid.has_pow) := do (e₁', p₁) ← eval e₁, (e', p') ← eval_pow e₁' (e₂, k), p ← ic_lift $ λ ic, ic.mk_app ``subst_into_pow [e₁, e₂, e₁', e₂', e', p₁, p₂, p'], return (e', p) | _, _ := eval_atom e end | e := match e.to_nat with | some n := (const e (rat.of_int n)).refl_conv | none := eval_atom e end /-- Evaluate a ring expression `e` recursively to normal form, together with a proof of equality. -/ meta def eval' (red : transparency) (atoms : ref (buffer expr)) (e : expr) : tactic (expr × expr) := ring_m.run' red atoms e $ do (e', p) ← eval e, return (e', p) theorem horner_def' {α} [comm_semiring α] (a x n b) : @horner α _ a x n b = x ^ n * a + b := by simp [horner, mul_comm] theorem mul_assoc_rev {α} [semigroup α] (a b c : α) : a * (b * c) = a * b * c := by simp [mul_assoc] theorem pow_add_rev {α} [monoid α] (a : α) (m n : ℕ) : a ^ m * a ^ n = a ^ (m + n) := by simp [pow_add] theorem pow_add_rev_right {α} [monoid α] (a b : α) (m n : ℕ) : b * a ^ m * a ^ n = b * a ^ (m + n) := by simp [pow_add, mul_assoc] theorem add_neg_eq_sub {α} [add_group α] (a b : α) : a + -b = a - b := (sub_eq_add_neg a b).symm /-- If `ring` fails to close the goal, it falls back on normalizing the expression to a "pretty" form so that you can see why it failed. This setting adjusts the resulting form: * `raw` is the form that `ring` actually uses internally, with iterated applications of `horner`. Not very readable but useful if you don't want any postprocessing. This results in terms like `horner (horner (horner 3 y 1 0) x 2 1) x 1 (horner 1 y 1 0)`. * `horner` maintains the Horner form structure, but it unfolds the `horner` definition itself, and tries to otherwise minimize parentheses. This results in terms like `(3 * x ^ 2 * y + 1) * x + y`. * `SOP` means sum of products form, expanding everything to monomials. This results in terms like `3 * x ^ 3 * y + x + y`. -/ @[derive has_reflect] inductive normalize_mode | raw | SOP | horner instance : inhabited normalize_mode := ⟨normalize_mode.horner⟩ /-- A `ring`-based normalization simplifier that rewrites ring expressions into the specified mode. * `raw` is the form that `ring` actually uses internally, with iterated applications of `horner`. Not very readable but useful if you don't want any postprocessing. This results in terms like `horner (horner (horner 3 y 1 0) x 2 1) x 1 (horner 1 y 1 0)`. * `horner` maintains the Horner form structure, but it unfolds the `horner` definition itself, and tries to otherwise minimize parentheses. This results in terms like `(3 * x ^ 2 * y + 1) * x + y`. * `SOP` means sum of products form, expanding everything to monomials. This results in terms like `3 * x ^ 3 * y + x + y`. -/ meta def normalize (red : transparency) (mode := normalize_mode.horner) (e : expr) : tactic (expr × expr) := using_new_ref mk_buffer $ λ atoms, do pow_lemma ← simp_lemmas.mk.add_simp ``pow_one, let lemmas := match mode with | normalize_mode.SOP := [``horner_def', ``add_zero, ``mul_one, ``mul_add, ``mul_sub, ``mul_assoc_rev, ``pow_add_rev, ``pow_add_rev_right, ``mul_neg_eq_neg_mul_symm, ``add_neg_eq_sub] | normalize_mode.horner := [``horner.equations._eqn_1, ``add_zero, ``one_mul, ``pow_one, ``neg_mul_eq_neg_mul_symm, ``add_neg_eq_sub] | _ := [] end, lemmas ← lemmas.mfoldl simp_lemmas.add_simp simp_lemmas.mk, (_, e', pr) ← ext_simplify_core () {} simp_lemmas.mk (λ _, failed) (λ _ _ _ _ e, do (new_e, pr) ← match mode with | normalize_mode.raw := eval' red atoms | normalize_mode.horner := trans_conv (eval' red atoms) (λ e, do (e', prf, _) ← simplify lemmas [] e, return (e', prf)) | normalize_mode.SOP := trans_conv (eval' red atoms) $ trans_conv (λ e, do (e', prf, _) ← simplify lemmas [] e, return (e', prf)) $ simp_bottom_up' (λ e, norm_num.derive e <|> pow_lemma.rewrite e) end e, guard (¬ new_e =ₐ e), return ((), new_e, some pr, ff)) (λ _ _ _ _ _, failed) `eq e, return (e', pr) end ring namespace interactive open interactive interactive.types lean.parser open tactic.ring local postfix `?`:9001 := optional /-- Tactic for solving equations in the language of *commutative* (semi)rings. This version of `ring` fails if the target is not an equality that is provable by the axioms of commutative (semi)rings. -/ meta def ring1 (red : parse (tk "!")?) : tactic unit := let transp := if red.is_some then semireducible else reducible in do `(%%e₁ = %%e₂) ← target, ((e₁', p₁), (e₂', p₂)) ← ring_m.run transp e₁ $ prod.mk <$> eval e₁ <*> eval e₂, is_def_eq e₁' e₂', p ← mk_eq_symm p₂ >>= mk_eq_trans p₁, tactic.exact p /-- Parser for `ring`'s `mode` argument, which can only be the "keywords" `raw`, `horner` or `SOP`. (Because these are not actually keywords we use a name parser and postprocess the result.) -/ meta def ring.mode : lean.parser ring.normalize_mode := with_desc "(SOP|raw|horner)?" $ do mode ← ident?, match mode with | none := return ring.normalize_mode.horner | some `horner := return ring.normalize_mode.horner | some `SOP := return ring.normalize_mode.SOP | some `raw := return ring.normalize_mode.raw | _ := failed end /-- Tactic for solving equations in the language of *commutative* (semi)rings. Attempts to prove the goal outright if there is no `at` specifier and the target is an equality, but if this fails it falls back to rewriting all ring expressions into a normal form. When writing a normal form, `ring SOP` will use sum-of-products form instead of horner form. `ring!` will use a more aggressive reducibility setting to identify atoms. Based on [Proving Equalities in a Commutative Ring Done Right in Coq](http://www.cs.ru.nl/~freek/courses/tt-2014/read/10.1.1.61.3041.pdf) by Benjamin Grégoire and Assia Mahboubi. -/ meta def ring (red : parse (tk "!")?) (SOP : parse ring.mode) (loc : parse location) : tactic unit := match loc with | interactive.loc.ns [none] := instantiate_mvars_in_target >> ring1 red | _ := failed end <|> do ns ← loc.get_locals, let transp := if red.is_some then semireducible else reducible, tt ← tactic.replace_at (normalize transp SOP) ns loc.include_goal | fail "ring failed to simplify", when loc.include_goal $ try tactic.reflexivity add_hint_tactic "ring" add_tactic_doc { name := "ring", category := doc_category.tactic, decl_names := [`tactic.interactive.ring], tags := ["arithmetic", "simplification", "decision procedure"] } end interactive end tactic namespace conv.interactive open conv interactive open tactic tactic.interactive (ring.mode ring1) open tactic.ring (normalize) local postfix `?`:9001 := optional /-- Normalises expressions in commutative (semi-)rings inside of a `conv` block using the tactic `ring`. -/ meta def ring (red : parse (lean.parser.tk "!")?) (SOP : parse ring.mode) : conv unit := let transp := if red.is_some then semireducible else reducible in discharge_eq_lhs (ring1 red) <|> replace_lhs (normalize transp SOP) <|> fail "ring failed to simplify" end conv.interactive
31a4a0e87823464247f802e53a6dcb868e16459b
8eeb99d0fdf8125f5d39a0ce8631653f588ee817
/src/data/matrix/basic.lean
f945d9649954b35405518dead8b170e3010410a2
[ "Apache-2.0" ]
permissive
jesse-michael-han/mathlib
a15c58378846011b003669354cbab7062b893cfe
fa6312e4dc971985e6b7708d99a5bc3062485c89
refs/heads/master
1,625,200,760,912
1,602,081,753,000
1,602,081,753,000
181,787,230
0
0
null
1,555,460,682,000
1,555,460,682,000
null
UTF-8
Lean
false
false
36,784
lean
/- Copyright (c) 2018 Ellen Arlt. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ellen Arlt, Blair Shi, Sean Leather, Mario Carneiro, Johan Commelin -/ import algebra.big_operators.pi import algebra.module.pi import algebra.big_operators.ring import data.fintype.card /-! # Matrices -/ universes u u' v w open_locale big_operators @[nolint unused_arguments] def matrix (m : Type u) (n : Type u') [fintype m] [fintype n] (α : Type v) : Type (max u u' v) := m → n → α variables {l m n o : Type*} [fintype l] [fintype m] [fintype n] [fintype o] variables {α : Type v} namespace matrix section ext variables {M N : matrix m n α} theorem ext_iff : (∀ i j, M i j = N i j) ↔ M = N := ⟨λ h, funext $ λ i, funext $ h i, λ h, by simp [h]⟩ @[ext] theorem ext : (∀ i j, M i j = N i j) → M = N := ext_iff.mp end ext /-- Apply a function to each matrix entry. -/ def map (M : matrix m n α) {β : Type w} (f : α → β) : matrix m n β := λ i j, f (M i j) @[simp] lemma map_apply {M : matrix m n α} {β : Type w} {f : α → β} {i : m} {j : n} : M.map f i j = f (M i j) := rfl def transpose (M : matrix m n α) : matrix n m α | x y := M y x localized "postfix `ᵀ`:1500 := matrix.transpose" in matrix def col (w : m → α) : matrix m unit α | x y := w x def row (v : n → α) : matrix unit n α | x y := v y instance [inhabited α] : inhabited (matrix m n α) := pi.inhabited _ instance [has_add α] : has_add (matrix m n α) := pi.has_add instance [add_semigroup α] : add_semigroup (matrix m n α) := pi.add_semigroup instance [add_comm_semigroup α] : add_comm_semigroup (matrix m n α) := pi.add_comm_semigroup instance [has_zero α] : has_zero (matrix m n α) := pi.has_zero instance [add_monoid α] : add_monoid (matrix m n α) := pi.add_monoid instance [add_comm_monoid α] : add_comm_monoid (matrix m n α) := pi.add_comm_monoid instance [has_neg α] : has_neg (matrix m n α) := pi.has_neg instance [add_group α] : add_group (matrix m n α) := pi.add_group instance [add_comm_group α] : add_comm_group (matrix m n α) := pi.add_comm_group @[simp] theorem zero_apply [has_zero α] (i j) : (0 : matrix m n α) i j = 0 := rfl @[simp] theorem neg_apply [has_neg α] (M : matrix m n α) (i j) : (- M) i j = - M i j := rfl @[simp] theorem add_apply [has_add α] (M N : matrix m n α) (i j) : (M + N) i j = M i j + N i j := rfl @[simp] lemma map_zero [has_zero α] {β : Type w} [has_zero β] {f : α → β} (h : f 0 = 0) : (0 : matrix m n α).map f = 0 := by { ext, simp [h], } lemma map_add [add_monoid α] {β : Type w} [add_monoid β] (f : α →+ β) (M N : matrix m n α) : (M + N).map f = M.map f + N.map f := by { ext, simp, } lemma map_sub [add_group α] {β : Type w} [add_group β] (f : α →+ β) (M N : matrix m n α) : (M - N).map f = M.map f - N.map f := by { ext, simp } lemma subsingleton_of_empty_left (hm : ¬ nonempty m) : subsingleton (matrix m n α) := ⟨λ M N, by { ext, contrapose! hm, use i }⟩ lemma subsingleton_of_empty_right (hn : ¬ nonempty n) : subsingleton (matrix m n α) := ⟨λ M N, by { ext, contrapose! hn, use j }⟩ end matrix /-- The `add_monoid_hom` between spaces of matrices induced by an `add_monoid_hom` between their coefficients. -/ def add_monoid_hom.map_matrix [add_monoid α] {β : Type w} [add_monoid β] (f : α →+ β) : matrix m n α →+ matrix m n β := { to_fun := λ M, M.map f, map_zero' := by simp, map_add' := matrix.map_add f, } @[simp] lemma add_monoid_hom.map_matrix_apply [add_monoid α] {β : Type w} [add_monoid β] (f : α →+ β) (M : matrix m n α) : f.map_matrix M = M.map f := rfl open_locale matrix namespace matrix section diagonal variables [decidable_eq n] /-- `diagonal d` is the square matrix such that `(diagonal d) i i = d i` and `(diagonal d) i j = 0` if `i ≠ j`. -/ def diagonal [has_zero α] (d : n → α) : matrix n n α := λ i j, if i = j then d i else 0 @[simp] theorem diagonal_apply_eq [has_zero α] {d : n → α} (i : n) : (diagonal d) i i = d i := by simp [diagonal] @[simp] theorem diagonal_apply_ne [has_zero α] {d : n → α} {i j : n} (h : i ≠ j) : (diagonal d) i j = 0 := by simp [diagonal, h] theorem diagonal_apply_ne' [has_zero α] {d : n → α} {i j : n} (h : j ≠ i) : (diagonal d) i j = 0 := diagonal_apply_ne h.symm @[simp] theorem diagonal_zero [has_zero α] : (diagonal (λ _, 0) : matrix n n α) = 0 := by simp [diagonal]; refl @[simp] lemma diagonal_transpose [has_zero α] (v : n → α) : (diagonal v)ᵀ = diagonal v := begin ext i j, by_cases h : i = j, { simp [h, transpose] }, { simp [h, transpose, diagonal_apply_ne' h] } end @[simp] theorem diagonal_add [add_monoid α] (d₁ d₂ : n → α) : diagonal d₁ + diagonal d₂ = diagonal (λ i, d₁ i + d₂ i) := by ext i j; by_cases h : i = j; simp [h] @[simp] lemma diagonal_map {β : Type w} [has_zero α] [has_zero β] {f : α → β} (h : f 0 = 0) {d : n → α} : (diagonal d).map f = diagonal (λ m, f (d m)) := by { ext, simp only [diagonal, map_apply], split_ifs; simp [h], } section one variables [has_zero α] [has_one α] instance : has_one (matrix n n α) := ⟨diagonal (λ _, 1)⟩ @[simp] theorem diagonal_one : (diagonal (λ _, 1) : matrix n n α) = 1 := rfl theorem one_apply {i j} : (1 : matrix n n α) i j = if i = j then 1 else 0 := rfl @[simp] theorem one_apply_eq (i) : (1 : matrix n n α) i i = 1 := diagonal_apply_eq i @[simp] theorem one_apply_ne {i j} : i ≠ j → (1 : matrix n n α) i j = 0 := diagonal_apply_ne theorem one_apply_ne' {i j} : j ≠ i → (1 : matrix n n α) i j = 0 := diagonal_apply_ne' @[simp] lemma one_map {β : Type w} [has_zero β] [has_one β] {f : α → β} (h₀ : f 0 = 0) (h₁ : f 1 = 1) : (1 : matrix n n α).map f = (1 : matrix n n β) := by { ext, simp only [one_apply, map_apply], split_ifs; simp [h₀, h₁], } end one section numeral @[simp] lemma bit0_apply [has_add α] (M : matrix m m α) (i : m) (j : m) : (bit0 M) i j = bit0 (M i j) := rfl variables [add_monoid α] [has_one α] lemma bit1_apply (M : matrix n n α) (i : n) (j : n) : (bit1 M) i j = if i = j then bit1 (M i j) else bit0 (M i j) := by dsimp [bit1]; by_cases h : i = j; simp [h] @[simp] lemma bit1_apply_eq (M : matrix n n α) (i : n) : (bit1 M) i i = bit1 (M i i) := by simp [bit1_apply] @[simp] lemma bit1_apply_ne (M : matrix n n α) {i j : n} (h : i ≠ j) : (bit1 M) i j = bit0 (M i j) := by simp [bit1_apply, h] end numeral end diagonal section dot_product /-- `dot_product v w` is the sum of the entrywise products `v i * w i` -/ def dot_product [has_mul α] [add_comm_monoid α] (v w : m → α) : α := ∑ i, v i * w i lemma dot_product_assoc [semiring α] (u : m → α) (v : m → n → α) (w : n → α) : dot_product (λ j, dot_product u (λ i, v i j)) w = dot_product u (λ i, dot_product (v i) w) := by simpa [dot_product, finset.mul_sum, finset.sum_mul, mul_assoc] using finset.sum_comm lemma dot_product_comm [comm_semiring α] (v w : m → α) : dot_product v w = dot_product w v := by simp_rw [dot_product, mul_comm] @[simp] lemma dot_product_punit [add_comm_monoid α] [has_mul α] (v w : punit → α) : dot_product v w = v ⟨⟩ * w ⟨⟩ := by simp [dot_product] @[simp] lemma dot_product_zero [semiring α] (v : m → α) : dot_product v 0 = 0 := by simp [dot_product] @[simp] lemma dot_product_zero' [semiring α] (v : m → α) : dot_product v (λ _, 0) = 0 := dot_product_zero v @[simp] lemma zero_dot_product [semiring α] (v : m → α) : dot_product 0 v = 0 := by simp [dot_product] @[simp] lemma zero_dot_product' [semiring α] (v : m → α) : dot_product (λ _, (0 : α)) v = 0 := zero_dot_product v @[simp] lemma add_dot_product [semiring α] (u v w : m → α) : dot_product (u + v) w = dot_product u w + dot_product v w := by simp [dot_product, add_mul, finset.sum_add_distrib] @[simp] lemma dot_product_add [semiring α] (u v w : m → α) : dot_product u (v + w) = dot_product u v + dot_product u w := by simp [dot_product, mul_add, finset.sum_add_distrib] @[simp] lemma diagonal_dot_product [decidable_eq m] [semiring α] (v w : m → α) (i : m) : dot_product (diagonal v i) w = v i * w i := have ∀ j ≠ i, diagonal v i j * w j = 0 := λ j hij, by simp [diagonal_apply_ne' hij], by convert finset.sum_eq_single i (λ j _, this j) _; simp @[simp] lemma dot_product_diagonal [decidable_eq m] [semiring α] (v w : m → α) (i : m) : dot_product v (diagonal w i) = v i * w i := have ∀ j ≠ i, v j * diagonal w i j = 0 := λ j hij, by simp [diagonal_apply_ne' hij], by convert finset.sum_eq_single i (λ j _, this j) _; simp @[simp] lemma dot_product_diagonal' [decidable_eq m] [semiring α] (v w : m → α) (i : m) : dot_product v (λ j, diagonal w j i) = v i * w i := have ∀ j ≠ i, v j * diagonal w j i = 0 := λ j hij, by simp [diagonal_apply_ne hij], by convert finset.sum_eq_single i (λ j _, this j) _; simp @[simp] lemma neg_dot_product [ring α] (v w : m → α) : dot_product (-v) w = - dot_product v w := by simp [dot_product] @[simp] lemma dot_product_neg [ring α] (v w : m → α) : dot_product v (-w) = - dot_product v w := by simp [dot_product] @[simp] lemma smul_dot_product [semiring α] (x : α) (v w : m → α) : dot_product (x • v) w = x * dot_product v w := by simp [dot_product, finset.mul_sum, mul_assoc] @[simp] lemma dot_product_smul [comm_semiring α] (x : α) (v w : m → α) : dot_product v (x • w) = x * dot_product v w := by simp [dot_product, finset.mul_sum, mul_assoc, mul_comm, mul_left_comm] end dot_product protected def mul [has_mul α] [add_comm_monoid α] (M : matrix l m α) (N : matrix m n α) : matrix l n α := λ i k, dot_product (λ j, M i j) (λ j, N j k) localized "infixl ` ⬝ `:75 := matrix.mul" in matrix theorem mul_apply [has_mul α] [add_comm_monoid α] {M : matrix l m α} {N : matrix m n α} {i k} : (M ⬝ N) i k = ∑ j, M i j * N j k := rfl instance [has_mul α] [add_comm_monoid α] : has_mul (matrix n n α) := ⟨matrix.mul⟩ @[simp] theorem mul_eq_mul [has_mul α] [add_comm_monoid α] (M N : matrix n n α) : M * N = M ⬝ N := rfl theorem mul_apply' [has_mul α] [add_comm_monoid α] {M N : matrix n n α} {i k} : (M ⬝ N) i k = dot_product (λ j, M i j) (λ j, N j k) := rfl section semigroup variables [semiring α] protected theorem mul_assoc (L : matrix l m α) (M : matrix m n α) (N : matrix n o α) : (L ⬝ M) ⬝ N = L ⬝ (M ⬝ N) := by { ext, apply dot_product_assoc } instance : semigroup (matrix n n α) := { mul_assoc := matrix.mul_assoc, ..matrix.has_mul } end semigroup @[simp] theorem diagonal_neg [decidable_eq n] [add_group α] (d : n → α) : -diagonal d = diagonal (λ i, -d i) := by ext i j; by_cases i = j; simp [h] section semiring variables [semiring α] @[simp] protected theorem mul_zero (M : matrix m n α) : M ⬝ (0 : matrix n o α) = 0 := by { ext i j, apply dot_product_zero } @[simp] protected theorem zero_mul (M : matrix m n α) : (0 : matrix l m α) ⬝ M = 0 := by { ext i j, apply zero_dot_product } protected theorem mul_add (L : matrix m n α) (M N : matrix n o α) : L ⬝ (M + N) = L ⬝ M + L ⬝ N := by { ext i j, apply dot_product_add } protected theorem add_mul (L M : matrix l m α) (N : matrix m n α) : (L + M) ⬝ N = L ⬝ N + M ⬝ N := by { ext i j, apply add_dot_product } @[simp] theorem diagonal_mul [decidable_eq m] (d : m → α) (M : matrix m n α) (i j) : (diagonal d).mul M i j = d i * M i j := diagonal_dot_product _ _ _ @[simp] theorem mul_diagonal [decidable_eq n] (d : n → α) (M : matrix m n α) (i j) : (M ⬝ diagonal d) i j = M i j * d j := by { rw ← diagonal_transpose, apply dot_product_diagonal } @[simp] protected theorem one_mul [decidable_eq m] (M : matrix m n α) : (1 : matrix m m α) ⬝ M = M := by ext i j; rw [← diagonal_one, diagonal_mul, one_mul] @[simp] protected theorem mul_one [decidable_eq n] (M : matrix m n α) : M ⬝ (1 : matrix n n α) = M := by ext i j; rw [← diagonal_one, mul_diagonal, mul_one] instance [decidable_eq n] : monoid (matrix n n α) := { one_mul := matrix.one_mul, mul_one := matrix.mul_one, ..matrix.has_one, ..matrix.semigroup } instance [decidable_eq n] : semiring (matrix n n α) := { mul_zero := matrix.mul_zero, zero_mul := matrix.zero_mul, left_distrib := matrix.mul_add, right_distrib := matrix.add_mul, ..matrix.add_comm_monoid, ..matrix.monoid } @[simp] theorem diagonal_mul_diagonal [decidable_eq n] (d₁ d₂ : n → α) : (diagonal d₁) ⬝ (diagonal d₂) = diagonal (λ i, d₁ i * d₂ i) := by ext i j; by_cases i = j; simp [h] theorem diagonal_mul_diagonal' [decidable_eq n] (d₁ d₂ : n → α) : diagonal d₁ * diagonal d₂ = diagonal (λ i, d₁ i * d₂ i) := diagonal_mul_diagonal _ _ lemma map_mul {L : matrix m n α} {M : matrix n o α} {β : Type w} [semiring β] {f : α →+* β} : (L ⬝ M).map f = L.map f ⬝ M.map f := by { ext, simp [mul_apply, ring_hom.map_sum], } lemma is_add_monoid_hom_mul_left (M : matrix l m α) : is_add_monoid_hom (λ x : matrix m n α, M ⬝ x) := { to_is_add_hom := ⟨matrix.mul_add _⟩, map_zero := matrix.mul_zero _ } lemma is_add_monoid_hom_mul_right (M : matrix m n α) : is_add_monoid_hom (λ x : matrix l m α, x ⬝ M) := { to_is_add_hom := ⟨λ _ _, matrix.add_mul _ _ _⟩, map_zero := matrix.zero_mul _ } protected lemma sum_mul {β : Type*} (s : finset β) (f : β → matrix l m α) (M : matrix m n α) : (∑ a in s, f a) ⬝ M = ∑ a in s, f a ⬝ M := (@finset.sum_hom _ _ _ _ _ s f (λ x, x ⬝ M) /- This line does not type-check without `id` and `: _`. Lean did not recognize that two different `add_monoid` instances were def-eq -/ (id (@is_add_monoid_hom_mul_right l _ _ _ _ _ _ _ M) : _)).symm protected lemma mul_sum {β : Type*} (s : finset β) (f : β → matrix m n α) (M : matrix l m α) : M ⬝ ∑ a in s, f a = ∑ a in s, M ⬝ f a := (@finset.sum_hom _ _ _ _ _ s f (λ x, M ⬝ x) /- This line does not type-check without `id` and `: _`. Lean did not recognize that two different `add_monoid` instances were def-eq -/ (id (@is_add_monoid_hom_mul_left _ _ n _ _ _ _ _ M) : _)).symm @[simp] lemma row_mul_col_apply (v w : m → α) (i j) : (row v ⬝ col w) i j = dot_product v w := rfl end semiring end matrix /-- The `ring_hom` between spaces of square matrices induced by a `ring_hom` between their coefficients. -/ def ring_hom.map_matrix [decidable_eq m] [semiring α] {β : Type w} [semiring β] (f : α →+* β) : matrix m m α →+* matrix m m β := { to_fun := λ M, M.map f, map_one' := by simp, map_mul' := λ L M, matrix.map_mul, ..(f.to_add_monoid_hom).map_matrix } @[simp] lemma ring_hom.map_matrix_apply [decidable_eq m] [semiring α] {β : Type w} [semiring β] (f : α →+* β) (M : matrix m m α) : f.map_matrix M = M.map f := rfl open_locale matrix namespace matrix section ring variables [ring α] @[simp] theorem neg_mul (M : matrix m n α) (N : matrix n o α) : (-M) ⬝ N = -(M ⬝ N) := by { ext, apply neg_dot_product } @[simp] theorem mul_neg (M : matrix m n α) (N : matrix n o α) : M ⬝ (-N) = -(M ⬝ N) := by { ext, apply dot_product_neg } end ring instance [decidable_eq n] [ring α] : ring (matrix n n α) := { ..matrix.semiring, ..matrix.add_comm_group } instance [semiring α] : has_scalar α (matrix m n α) := pi.has_scalar instance {β : Type w} [semiring α] [add_comm_monoid β] [semimodule α β] : semimodule α (matrix m n β) := pi.semimodule _ _ _ @[simp] lemma smul_apply [semiring α] (a : α) (A : matrix m n α) (i : m) (j : n) : (a • A) i j = a * A i j := rfl section semiring variables [semiring α] lemma smul_eq_diagonal_mul [decidable_eq m] (M : matrix m n α) (a : α) : a • M = diagonal (λ _, a) ⬝ M := by { ext, simp } @[simp] lemma smul_mul (M : matrix m n α) (a : α) (N : matrix n l α) : (a • M) ⬝ N = a • M ⬝ N := by { ext, apply smul_dot_product } @[simp] lemma mul_mul_left (M : matrix m n α) (N : matrix n o α) (a : α) : (λ i j, a * M i j) ⬝ N = a • (M ⬝ N) := begin simp only [←smul_apply], simp, end /-- The ring homomorphism `α →+* matrix n n α` sending `a` to the diagonal matrix with `a` on the diagonal. -/ def scalar (n : Type u) [decidable_eq n] [fintype n] : α →+* matrix n n α := { to_fun := λ a, a • 1, map_zero' := by simp, map_add' := by { intros, ext, simp [add_mul], }, map_one' := by simp, map_mul' := by { intros, ext, simp [mul_assoc], }, } section scalar variable [decidable_eq n] @[simp] lemma coe_scalar : (scalar n : α → matrix n n α) = λ a, a • 1 := rfl lemma scalar_apply_eq (a : α) (i : n) : scalar n a i i = a := by simp only [coe_scalar, mul_one, one_apply_eq, smul_apply] lemma scalar_apply_ne (a : α) (i j : n) (h : i ≠ j) : scalar n a i j = 0 := by simp only [h, coe_scalar, one_apply_ne, ne.def, not_false_iff, smul_apply, mul_zero] lemma scalar_inj [nonempty n] {r s : α} : scalar n r = scalar n s ↔ r = s := begin split, { intro h, inhabit n, rw [← scalar_apply_eq r (arbitrary n), ← scalar_apply_eq s (arbitrary n), h] }, { rintro rfl, refl } end end scalar end semiring section comm_semiring variables [comm_semiring α] lemma smul_eq_mul_diagonal [decidable_eq n] (M : matrix m n α) (a : α) : a • M = M ⬝ diagonal (λ _, a) := by { ext, simp [mul_comm] } @[simp] lemma mul_smul (M : matrix m n α) (a : α) (N : matrix n l α) : M ⬝ (a • N) = a • M ⬝ N := by { ext, apply dot_product_smul } @[simp] lemma mul_mul_right (M : matrix m n α) (N : matrix n o α) (a : α) : M ⬝ (λ i j, a * N i j) = a • (M ⬝ N) := begin simp only [←smul_apply], simp, end lemma scalar.commute [decidable_eq n] (r : α) (M : matrix n n α) : commute (scalar n r) M := by simp [commute, semiconj_by] end comm_semiring section semiring variables [semiring α] def vec_mul_vec (w : m → α) (v : n → α) : matrix m n α | x y := w x * v y def mul_vec (M : matrix m n α) (v : n → α) : m → α | i := dot_product (λ j, M i j) v def vec_mul (v : m → α) (M : matrix m n α) : n → α | j := dot_product v (λ i, M i j) instance mul_vec.is_add_monoid_hom_left (v : n → α) : is_add_monoid_hom (λM:matrix m n α, mul_vec M v) := { map_zero := by ext; simp [mul_vec]; refl, map_add := begin intros x y, ext m, apply add_dot_product end } lemma mul_vec_diagonal [decidable_eq m] (v w : m → α) (x : m) : mul_vec (diagonal v) w x = v x * w x := diagonal_dot_product v w x lemma vec_mul_diagonal [decidable_eq m] (v w : m → α) (x : m) : vec_mul v (diagonal w) x = v x * w x := dot_product_diagonal' v w x @[simp] lemma mul_vec_one [decidable_eq m] (v : m → α) : mul_vec 1 v = v := by { ext, rw [←diagonal_one, mul_vec_diagonal, one_mul] } @[simp] lemma vec_mul_one [decidable_eq m] (v : m → α) : vec_mul v 1 = v := by { ext, rw [←diagonal_one, vec_mul_diagonal, mul_one] } @[simp] lemma mul_vec_zero (A : matrix m n α) : mul_vec A 0 = 0 := by { ext, simp [mul_vec] } @[simp] lemma vec_mul_zero (A : matrix m n α) : vec_mul 0 A = 0 := by { ext, simp [vec_mul] } @[simp] lemma vec_mul_vec_mul (v : m → α) (M : matrix m n α) (N : matrix n o α) : vec_mul (vec_mul v M) N = vec_mul v (M ⬝ N) := by { ext, apply dot_product_assoc } @[simp] lemma mul_vec_mul_vec (v : o → α) (M : matrix m n α) (N : matrix n o α) : mul_vec M (mul_vec N v) = mul_vec (M ⬝ N) v := by { ext, symmetry, apply dot_product_assoc } lemma vec_mul_vec_eq (w : m → α) (v : n → α) : vec_mul_vec w v = (col w) ⬝ (row v) := by { ext i j, simp [vec_mul_vec, mul_apply], refl } variables [decidable_eq m] [decidable_eq n] /-- `std_basis_matrix i j a` is the matrix with `a` in the `i`-th row, `j`-th column, and zeroes elsewhere. -/ def std_basis_matrix (i : m) (j : n) (a : α) : matrix m n α := (λ i' j', if i' = i ∧ j' = j then a else 0) @[simp] lemma smul_std_basis_matrix (i : m) (j : n) (a b : α) : b • std_basis_matrix i j a = std_basis_matrix i j (b • a) := by { unfold std_basis_matrix, ext, simp } @[simp] lemma std_basis_matrix_zero (i : m) (j : n) : std_basis_matrix i j (0 : α) = 0 := by { unfold std_basis_matrix, ext, simp } lemma std_basis_matrix_add (i : m) (j : n) (a b : α) : std_basis_matrix i j (a + b) = std_basis_matrix i j a + std_basis_matrix i j b := begin unfold std_basis_matrix, ext, split_ifs with h; simp [h], end lemma matrix_eq_sum_std_basis (x : matrix n m α) : x = ∑ (i : n) (j : m), std_basis_matrix i j (x i j) := begin ext, iterate 2 {rw finset.sum_apply}, rw ← finset.sum_subset, swap 4, exact {i}, { norm_num [std_basis_matrix] }, { simp }, intros, norm_num at a, norm_num, convert finset.sum_const_zero, ext, norm_num [std_basis_matrix], rw if_neg, tauto!, end -- TODO: tie this up with the `basis` machinery of linear algebra -- this is not completely trivial because we are indexing by two types, instead of one -- TODO: add `std_basis_vec` lemma std_basis_eq_basis_mul_basis (i : m) (j : n) : std_basis_matrix i j 1 = vec_mul_vec (λ i', ite (i = i') 1 0) (λ j', ite (j = j') 1 0) := begin ext, norm_num [std_basis_matrix, vec_mul_vec], split_ifs; tauto, end @[elab_as_eliminator] protected lemma induction_on' {X : Type*} [semiring X] {M : matrix n n X → Prop} (m : matrix n n X) (h_zero : M 0) (h_add : ∀p q, M p → M q → M (p + q)) (h_std_basis : ∀ i j x, M (std_basis_matrix i j x)) : M m := begin rw [matrix_eq_sum_std_basis m, ← finset.sum_product'], apply finset.sum_induction _ _ h_add h_zero, { intros, apply h_std_basis, } end @[elab_as_eliminator] protected lemma induction_on [nonempty n] {X : Type*} [semiring X] {M : matrix n n X → Prop} (m : matrix n n X) (h_add : ∀p q, M p → M q → M (p + q)) (h_std_basis : ∀ i j x, M (std_basis_matrix i j x)) : M m := matrix.induction_on' m begin have i : n := classical.choice (by assumption), simpa using h_std_basis i i 0, end h_add h_std_basis end semiring section ring variables [ring α] lemma neg_vec_mul (v : m → α) (A : matrix m n α) : vec_mul (-v) A = - vec_mul v A := by { ext, apply neg_dot_product } lemma vec_mul_neg (v : m → α) (A : matrix m n α) : vec_mul v (-A) = - vec_mul v A := by { ext, apply dot_product_neg } lemma neg_mul_vec (v : n → α) (A : matrix m n α) : mul_vec (-A) v = - mul_vec A v := by { ext, apply neg_dot_product } lemma mul_vec_neg (v : n → α) (A : matrix m n α) : mul_vec A (-v) = - mul_vec A v := by { ext, apply dot_product_neg } end ring section transpose open_locale matrix /-- Tell `simp` what the entries are in a transposed matrix. Compare with `mul_apply`, `diagonal_apply_eq`, etc. -/ @[simp] lemma transpose_apply (M : matrix m n α) (i j) : M.transpose j i = M i j := rfl @[simp] lemma transpose_transpose (M : matrix m n α) : Mᵀᵀ = M := by ext; refl @[simp] lemma transpose_zero [has_zero α] : (0 : matrix m n α)ᵀ = 0 := by ext i j; refl @[simp] lemma transpose_one [decidable_eq n] [has_zero α] [has_one α] : (1 : matrix n n α)ᵀ = 1 := begin ext i j, unfold has_one.one transpose, by_cases i = j, { simp only [h, diagonal_apply_eq] }, { simp only [diagonal_apply_ne h, diagonal_apply_ne (λ p, h (symm p))] } end @[simp] lemma transpose_add [has_add α] (M : matrix m n α) (N : matrix m n α) : (M + N)ᵀ = Mᵀ + Nᵀ := by { ext i j, simp } @[simp] lemma transpose_sub [add_group α] (M : matrix m n α) (N : matrix m n α) : (M - N)ᵀ = Mᵀ - Nᵀ := by { ext i j, simp } @[simp] lemma transpose_mul [comm_semiring α] (M : matrix m n α) (N : matrix n l α) : (M ⬝ N)ᵀ = Nᵀ ⬝ Mᵀ := begin ext i j, apply dot_product_comm end @[simp] lemma transpose_smul [semiring α] (c : α) (M : matrix m n α) : (c • M)ᵀ = c • Mᵀ := by { ext i j, refl } @[simp] lemma transpose_neg [has_neg α] (M : matrix m n α) : (- M)ᵀ = - Mᵀ := by ext i j; refl lemma transpose_map {β : Type w} {f : α → β} {M : matrix m n α} : Mᵀ.map f = (M.map f)ᵀ := by { ext, refl } end transpose def minor (A : matrix m n α) (row : l → m) (col : o → n) : matrix l o α := λ i j, A (row i) (col j) @[reducible] def sub_left {m l r : nat} (A : matrix (fin m) (fin (l + r)) α) : matrix (fin m) (fin l) α := minor A id (fin.cast_add r) @[reducible] def sub_right {m l r : nat} (A : matrix (fin m) (fin (l + r)) α) : matrix (fin m) (fin r) α := minor A id (fin.nat_add l) @[reducible] def sub_up {d u n : nat} (A : matrix (fin (u + d)) (fin n) α) : matrix (fin u) (fin n) α := minor A (fin.cast_add d) id @[reducible] def sub_down {d u n : nat} (A : matrix (fin (u + d)) (fin n) α) : matrix (fin d) (fin n) α := minor A (fin.nat_add u) id @[reducible] def sub_up_right {d u l r : nat} (A: matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin u) (fin r) α := sub_up (sub_right A) @[reducible] def sub_down_right {d u l r : nat} (A : matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin d) (fin r) α := sub_down (sub_right A) @[reducible] def sub_up_left {d u l r : nat} (A : matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin u) (fin (l)) α := sub_up (sub_left A) @[reducible] def sub_down_left {d u l r : nat} (A: matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin d) (fin (l)) α := sub_down (sub_left A) section row_col /-! ### `row_col` section Simplification lemmas for `matrix.row` and `matrix.col`. -/ open_locale matrix @[simp] lemma col_add [semiring α] (v w : m → α) : col (v + w) = col v + col w := by { ext, refl } @[simp] lemma col_smul [semiring α] (x : α) (v : m → α) : col (x • v) = x • col v := by { ext, refl } @[simp] lemma row_add [semiring α] (v w : m → α) : row (v + w) = row v + row w := by { ext, refl } @[simp] lemma row_smul [semiring α] (x : α) (v : m → α) : row (x • v) = x • row v := by { ext, refl } @[simp] lemma col_apply (v : m → α) (i j) : matrix.col v i j = v i := rfl @[simp] lemma row_apply (v : m → α) (i j) : matrix.row v i j = v j := rfl @[simp] lemma transpose_col (v : m → α) : (matrix.col v).transpose = matrix.row v := by {ext, refl} @[simp] lemma transpose_row (v : m → α) : (matrix.row v).transpose = matrix.col v := by {ext, refl} lemma row_vec_mul [semiring α] (M : matrix m n α) (v : m → α) : matrix.row (matrix.vec_mul v M) = matrix.row v ⬝ M := by {ext, refl} lemma col_vec_mul [semiring α] (M : matrix m n α) (v : m → α) : matrix.col (matrix.vec_mul v M) = (matrix.row v ⬝ M)ᵀ := by {ext, refl} lemma col_mul_vec [semiring α] (M : matrix m n α) (v : n → α) : matrix.col (matrix.mul_vec M v) = M ⬝ matrix.col v := by {ext, refl} lemma row_mul_vec [semiring α] (M : matrix m n α) (v : n → α) : matrix.row (matrix.mul_vec M v) = (M ⬝ matrix.col v)ᵀ := by {ext, refl} end row_col section update /-- Update, i.e. replace the `i`th row of matrix `A` with the values in `b`. -/ def update_row [decidable_eq n] (M : matrix n m α) (i : n) (b : m → α) : matrix n m α := function.update M i b /-- Update, i.e. replace the `i`th column of matrix `A` with the values in `b`. -/ def update_column [decidable_eq m] (M : matrix n m α) (j : m) (b : n → α) : matrix n m α := λ i, function.update (M i) j (b i) variables {M : matrix n m α} {i : n} {j : m} {b : m → α} {c : n → α} @[simp] lemma update_row_self [decidable_eq n] : update_row M i b i = b := function.update_same i b M @[simp] lemma update_column_self [decidable_eq m] : update_column M j c i j = c i := function.update_same j (c i) (M i) @[simp] lemma update_row_ne [decidable_eq n] {i' : n} (i_ne : i' ≠ i) : update_row M i b i' = M i' := function.update_noteq i_ne b M @[simp] lemma update_column_ne [decidable_eq m] {j' : m} (j_ne : j' ≠ j) : update_column M j c i j' = M i j' := function.update_noteq j_ne (c i) (M i) lemma update_row_apply [decidable_eq n] {i' : n} : update_row M i b i' j = if i' = i then b j else M i' j := begin by_cases i' = i, { rw [h, update_row_self, if_pos rfl] }, { rwa [update_row_ne h, if_neg h] } end lemma update_column_apply [decidable_eq m] {j' : m} : update_column M j c i j' = if j' = j then c i else M i j' := begin by_cases j' = j, { rw [h, update_column_self, if_pos rfl] }, { rwa [update_column_ne h, if_neg h] } end lemma update_row_transpose [decidable_eq m] : update_row Mᵀ j c = (update_column M j c)ᵀ := begin ext i' j, rw [transpose_apply, update_row_apply, update_column_apply], refl end lemma update_column_transpose [decidable_eq n] : update_column Mᵀ i b = (update_row M i b)ᵀ := begin ext i' j, rw [transpose_apply, update_row_apply, update_column_apply], refl end end update section block_matrices /-- We can form a single large matrix by flattening smaller 'block' matrices of compatible dimensions. -/ def from_blocks (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) : matrix (n ⊕ o) (l ⊕ m) α := sum.elim (λ i, sum.elim (A i) (B i)) (λ i, sum.elim (C i) (D i)) @[simp] lemma from_blocks_apply₁₁ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (i : n) (j : l) : from_blocks A B C D (sum.inl i) (sum.inl j) = A i j := rfl @[simp] lemma from_blocks_apply₁₂ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (i : n) (j : m) : from_blocks A B C D (sum.inl i) (sum.inr j) = B i j := rfl @[simp] lemma from_blocks_apply₂₁ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (i : o) (j : l) : from_blocks A B C D (sum.inr i) (sum.inl j) = C i j := rfl @[simp] lemma from_blocks_apply₂₂ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (i : o) (j : m) : from_blocks A B C D (sum.inr i) (sum.inr j) = D i j := rfl /-- Given a matrix whose row and column indexes are sum types, we can extract the correspnding "top left" submatrix. -/ def to_blocks₁₁ (M : matrix (n ⊕ o) (l ⊕ m) α) : matrix n l α := λ i j, M (sum.inl i) (sum.inl j) /-- Given a matrix whose row and column indexes are sum types, we can extract the correspnding "top right" submatrix. -/ def to_blocks₁₂ (M : matrix (n ⊕ o) (l ⊕ m) α) : matrix n m α := λ i j, M (sum.inl i) (sum.inr j) /-- Given a matrix whose row and column indexes are sum types, we can extract the correspnding "bottom left" submatrix. -/ def to_blocks₂₁ (M : matrix (n ⊕ o) (l ⊕ m) α) : matrix o l α := λ i j, M (sum.inr i) (sum.inl j) /-- Given a matrix whose row and column indexes are sum types, we can extract the correspnding "bottom right" submatrix. -/ def to_blocks₂₂ (M : matrix (n ⊕ o) (l ⊕ m) α) : matrix o m α := λ i j, M (sum.inr i) (sum.inr j) lemma from_blocks_to_blocks (M : matrix (n ⊕ o) (l ⊕ m) α) : from_blocks M.to_blocks₁₁ M.to_blocks₁₂ M.to_blocks₂₁ M.to_blocks₂₂ = M := begin ext i j, rcases i; rcases j; refl, end @[simp] lemma to_blocks_from_blocks₁₁ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) : (from_blocks A B C D).to_blocks₁₁ = A := rfl @[simp] lemma to_blocks_from_blocks₁₂ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) : (from_blocks A B C D).to_blocks₁₂ = B := rfl @[simp] lemma to_blocks_from_blocks₂₁ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) : (from_blocks A B C D).to_blocks₂₁ = C := rfl @[simp] lemma to_blocks_from_blocks₂₂ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) : (from_blocks A B C D).to_blocks₂₂ = D := rfl lemma from_blocks_transpose (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) : (from_blocks A B C D)ᵀ = from_blocks Aᵀ Cᵀ Bᵀ Dᵀ := begin ext i j, rcases i; rcases j; simp [from_blocks], end variables [semiring α] lemma from_blocks_smul (x : α) (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) : x • (from_blocks A B C D) = from_blocks (x • A) (x • B) (x • C) (x • D) := begin ext i j, rcases i; rcases j; simp [from_blocks], end lemma from_blocks_add (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (A' : matrix n l α) (B' : matrix n m α) (C' : matrix o l α) (D' : matrix o m α) : (from_blocks A B C D) + (from_blocks A' B' C' D') = from_blocks (A + A') (B + B') (C + C') (D + D') := begin ext i j, rcases i; rcases j; refl, end lemma from_blocks_multiply {p q : Type*} [fintype p] [fintype q] (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (A' : matrix l p α) (B' : matrix l q α) (C' : matrix m p α) (D' : matrix m q α) : (from_blocks A B C D) ⬝ (from_blocks A' B' C' D') = from_blocks (A ⬝ A' + B ⬝ C') (A ⬝ B' + B ⬝ D') (C ⬝ A' + D ⬝ C') (C ⬝ B' + D ⬝ D') := begin ext i j, rcases i; rcases j; simp only [from_blocks, mul_apply, fintype.sum_sum_type, sum.elim_inl, sum.elim_inr, pi.add_apply], end variables [decidable_eq l] [decidable_eq m] @[simp] lemma from_blocks_diagonal (d₁ : l → α) (d₂ : m → α) : from_blocks (diagonal d₁) 0 0 (diagonal d₂) = diagonal (sum.elim d₁ d₂) := begin ext i j, rcases i; rcases j; simp [diagonal], end @[simp] lemma from_blocks_one : from_blocks (1 : matrix l l α) 0 0 (1 : matrix m m α) = 1 := by { ext i j, rcases i; rcases j; simp [one_apply] } end block_matrices section block_diagonal variables (M N : o → matrix m n α) [decidable_eq o] section has_zero variables [has_zero α] /-- `matrix.block_diagonal M` turns `M : o → matrix m n α'` into a `m × o`-by`n × o` block matrix which has the entries of `M` along the diagonal and zero elsewhere. -/ def block_diagonal : matrix (m × o) (n × o) α | ⟨i, k⟩ ⟨j, k'⟩ := if k = k' then M k i j else 0 lemma block_diagonal_apply (ik jk) : block_diagonal M ik jk = if ik.2 = jk.2 then M ik.2 ik.1 jk.1 else 0 := by { cases ik, cases jk, refl } @[simp] lemma block_diagonal_apply_eq (i j k) : block_diagonal M (i, k) (j, k) = M k i j := if_pos rfl lemma block_diagonal_apply_ne (i j) {k k'} (h : k ≠ k') : block_diagonal M (i, k) (j, k') = 0 := if_neg h @[simp] lemma block_diagonal_transpose : (block_diagonal M)ᵀ = (block_diagonal (λ k, (M k)ᵀ)) := begin ext, simp only [transpose_apply, block_diagonal_apply, eq_comm], split_ifs with h, { rw h }, { refl } end @[simp] lemma block_diagonal_zero : block_diagonal (0 : o → matrix m n α) = 0 := by { ext, simp [block_diagonal_apply] } @[simp] lemma block_diagonal_diagonal [decidable_eq m] (d : o → m → α) : (block_diagonal (λ k, diagonal (d k))) = diagonal (λ ik, d ik.2 ik.1) := begin ext ⟨i, k⟩ ⟨j, k'⟩, simp only [block_diagonal_apply, diagonal], split_ifs; finish end @[simp] lemma block_diagonal_one [decidable_eq m] [has_one α] : (block_diagonal (1 : o → matrix m m α)) = 1 := show (block_diagonal (λ (_ : o), diagonal (λ (_ : m), (1 : α)))) = diagonal (λ _, 1), by rw [block_diagonal_diagonal] end has_zero @[simp] lemma block_diagonal_add [add_monoid α] : block_diagonal (M + N) = block_diagonal M + block_diagonal N := begin ext, simp only [block_diagonal_apply, add_apply], split_ifs; simp end @[simp] lemma block_diagonal_neg [add_group α] : block_diagonal (-M) = - block_diagonal M := begin ext, simp only [block_diagonal_apply, neg_apply], split_ifs; simp end @[simp] lemma block_diagonal_sub [add_group α] : block_diagonal (M - N) = block_diagonal M - block_diagonal N := by simp [sub_eq_add_neg] @[simp] lemma block_diagonal_mul {p : Type*} [fintype p] [semiring α] (N : o → matrix n p α) : block_diagonal (λ k, M k ⬝ N k) = block_diagonal M ⬝ block_diagonal N := begin ext ⟨i, k⟩ ⟨j, k'⟩, simp only [block_diagonal_apply, mul_apply, ← finset.univ_product_univ, finset.sum_product], split_ifs with h; simp [h] end @[simp] lemma block_diagonal_smul {R : Type*} [semiring R] [add_comm_monoid α] [semimodule R α] (x : R) : block_diagonal (x • M) = x • block_diagonal M := by { ext, simp only [block_diagonal_apply, pi.smul_apply, smul_apply], split_ifs; simp } end block_diagonal end matrix namespace ring_hom variables {β : Type*} [semiring α] [semiring β] lemma map_matrix_mul (M : matrix m n α) (N : matrix n o α) (i : m) (j : o) (f : α →+* β) : f (matrix.mul M N i j) = matrix.mul (λ i j, f (M i j)) (λ i j, f (N i j)) i j := by simp [matrix.mul_apply, ring_hom.map_sum] end ring_hom
a4d5d75899765728c11a912696ec82f30756bcc0
8930e38ac0fae2e5e55c28d0577a8e44e2639a6d
/data/finsupp.lean
7c1774b7dcef280a9b51d2d0a249ea8685ee194d
[ "Apache-2.0" ]
permissive
SG4316/mathlib
3d64035d02a97f8556ad9ff249a81a0a51a3321a
a7846022507b531a8ab53b8af8a91953fceafd3a
refs/heads/master
1,584,869,960,527
1,530,718,645,000
1,530,724,110,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
27,931
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 Type of functions with finite support. Functions with finite support provide the basis for the following concrete instances: * ℕ →₀ α: Polynomials (where α is a ring) * (σ →₀ ℕ) →₀ α: Multivariate Polynomials (again α is a ring, and σ are variable names) * α →₀ ℕ: Multisets * α →₀ ℤ: Abelean groups freely generated by α * β →₀ α: Linear combinations over β where α is the scalar ring Most of the theory assumes that the range is a commutative monoid. This gives us the big sum operator as a powerful way to construct `finsupp` elements. A general advice is to not use α →₀ β directly, as the type class setup might not be fitting. The best is to define a copy and select the instances best suited. -/ import data.finset data.set.finite algebra.big_operators algebra.module open finset reserve infix ` →₀ `:25 universes u u₁ u₂ v v₁ v₂ v₃ w x y namespace finset variables {α : Type u} [decidable_eq α] protected def subtype (p : α → Prop) [decidable_pred p] (s : finset α) : finset (subtype p) := (s.filter p).attach.image $ λ⟨a, ha⟩, ⟨a, (mem_filter.1 ha).2⟩ @[simp] lemma mem_subtype {p : α → Prop} [decidable_pred p] {s : finset α} : ∀{a : subtype p}, a ∈ s.subtype p ↔ a.val ∈ s | ⟨a, ha⟩ := by simp [finset.subtype, ha] end finset /-- `finsupp α β`, denoted `α →₀ β`, is the type of functions `f : α → β` such that `f x = 0` for all but finitely many `x`. -/ structure finsupp (α : Type u) (β : Type v) [has_zero β] := (support : finset α) (to_fun : α → β) (mem_support_to_fun : ∀a, a ∈ support ↔ to_fun a ≠ 0) infix →₀ := finsupp namespace finsupp variables {α : Type u} {β : Type v} {γ : Type w} {α₁ : Type u₁} {α₂ : Type u₂} {β₁ : Type v₁} {β₂ : Type v₂} section basic variable [has_zero β] instance : has_coe_to_fun (α →₀ β) := ⟨λ_, α → β, finsupp.to_fun⟩ instance : has_zero (α →₀ β) := ⟨⟨∅, (λ_, 0), by simp⟩⟩ @[simp] lemma zero_apply {a : α} : (0 : α →₀ β) a = 0 := rfl @[simp] lemma support_zero : (0 : α →₀ β).support = ∅ := rfl instance : inhabited (α →₀ β) := ⟨0⟩ @[simp] lemma mem_support_iff (f : α →₀ β) : ∀a:α, a ∈ f.support ↔ f a ≠ 0 := f.mem_support_to_fun lemma ext : ∀{f g : α →₀ β}, (∀a, f a = g a) → f = g | ⟨s, f, hf⟩ ⟨t, g, hg⟩ h := begin have : f = g, { funext a, exact h a }, subst this, have : s = t, { simp [finset.ext, hf, hg] }, subst this end @[simp] lemma support_eq_empty [decidable_eq β] {f : α →₀ β} : f.support = ∅ ↔ f = 0 := ⟨assume h, ext $ assume a, by simp [finset.ext] at h; exact h a, by simp {contextual:=tt}⟩ instance [decidable_eq α] [decidable_eq β] : decidable_eq (α →₀ β) := assume f g, decidable_of_iff (f.support = g.support ∧ (∀a∈f.support, f a = g a)) ⟨assume ⟨h₁, h₂⟩, ext $ assume a, if h : a ∈ f.support then h₂ a h else have hf : f a = 0, by rwa [f.mem_support_iff, not_not] at h, have hg : g a = 0, by rwa [h₁, g.mem_support_iff, not_not] at h, by rw [hf, hg], by intro h; subst h; simp⟩ lemma finite_supp (f : α →₀ β) : set.finite {a | f a ≠ 0} := ⟨set.fintype_of_finset f.support f.mem_support_iff⟩ lemma support_subset_iff {s : set α} {f : α →₀ β} [decidable_eq α] : ↑f.support ⊆ s ↔ (∀a∉s, f a = 0) := by simp [set.subset_def]; exact forall_congr (assume a, @not_imp_comm _ _ (classical.dec _) (classical.dec _)) end basic section single variables [decidable_eq α] [decidable_eq β] [has_zero β] {a a' : α} {b : β} /-- `single a b` is the finitely supported function which has value `b` at `a` and zero otherwise. -/ def single (a : α) (b : β) : α →₀ β := ⟨(if b = 0 then ∅ else {a}), (λa', if a = a' then b else 0), begin intro a', by_cases hb : b = 0; by_cases a = a'; simp [h, hb], simp [ne.symm h, h] end⟩ lemma single_apply : (single a b : α →₀ β) a' = (if a = a' then b else 0) := rfl @[simp] lemma single_eq_same : (single a b : α →₀ β) a = b := by simp [single_apply] @[simp] lemma single_eq_of_ne (h : a ≠ a') : (single a b : α →₀ β) a' = 0 := by simp [single_apply, h] @[simp] lemma single_zero : (single a 0 : α →₀ β) = 0 := ext $ assume a', begin by_cases h : a = a', { rw [h, single_eq_same, zero_apply] }, { rw [single_eq_of_ne h, zero_apply] } end lemma support_single_ne_zero (hb : b ≠ 0) : (single a b).support = {a} := if_neg hb lemma support_single_subset : (single a b).support ⊆ {a} := by by_cases b = 0; simp [support_single_ne_zero, h] end single section on_finset variables [decidable_eq β] [has_zero β] /-- `on_finset s f hf` is the finsupp function representing `f` restricted to the set `s`. The function needs to be 0 outside of `s`. Use this when the set needs filtered anyway, otherwise often better set representation is available. -/ def on_finset (s : finset α) (f : α → β) (hf : ∀a, f a ≠ 0 → a ∈ s) : α →₀ β := ⟨s.filter (λa, f a ≠ 0), f, assume a, classical.by_cases (assume h : f a = 0, by simp [h]) (assume h : f a ≠ 0, by simp [h, hf])⟩ @[simp] lemma on_finset_apply {s : finset α} {f : α → β} {hf a} : (on_finset s f hf : α →₀ β) a = f a := rfl @[simp] lemma support_on_finset_subset {s : finset α} {f : α → β} {hf} : (on_finset s f hf).support ⊆ s := by simp [on_finset] end on_finset section map_range variables [has_zero β₁] [has_zero β₂] [decidable_eq β₂] /-- The composition of `f : β₁ → β₂` and `g : α →₀ β₁` is `map_range f hf g : α →₀ β₂`, well defined when `f 0 = 0`. -/ def map_range (f : β₁ → β₂) (hf : f 0 = 0) (g : α →₀ β₁) : α →₀ β₂ := on_finset g.support (f ∘ g) $ assume a, by rw [mem_support_iff, not_imp_not]; simp [hf] {contextual := tt} @[simp] lemma map_range_apply {f : β₁ → β₂} {hf : f 0 = 0} {g : α →₀ β₁} {a : α} : map_range f hf g a = f (g a) := rfl lemma support_map_range {f : β₁ → β₂} {hf : f 0 = 0} {g : α →₀ β₁} : (map_range f hf g).support ⊆ g.support := support_on_finset_subset end map_range section zip_with variables [has_zero β] [has_zero β₁] [has_zero β₂] [decidable_eq α] [decidable_eq β] /-- `zip_with f hf g₁ g₂` is the finitely supported function satisfying `zip_with f hf g₁ g₂ a = f (g₁ a) (g₂ a)`, and well defined when `f 0 0 = 0`. -/ def zip_with (f : β₁ → β₂ → β) (hf : f 0 0 = 0) (g₁ : α →₀ β₁) (g₂ : α →₀ β₂) : (α →₀ β) := on_finset (g₁.support ∪ g₂.support) (λa, f (g₁ a) (g₂ a)) $ assume a, classical.by_cases (assume h : g₁ a = 0, by simp [h]; rw [not_imp_not]; simp [hf] {contextual := tt}) (assume h : g₁ a ≠ 0, by simp [h]) @[simp] lemma zip_with_apply {f : β₁ → β₂ → β} {hf : f 0 0 = 0} {g₁ : α →₀ β₁} {g₂ : α →₀ β₂} {a : α} : zip_with f hf g₁ g₂ a = f (g₁ a) (g₂ a) := rfl lemma support_zip_with {f : β₁ → β₂ → β} {hf : f 0 0 = 0} {g₁ : α →₀ β₁} {g₂ : α →₀ β₂} : (zip_with f hf g₁ g₂).support ⊆ g₁.support ∪ g₂.support := support_on_finset_subset end zip_with section erase variables [decidable_eq α] [decidable_eq β] def erase [has_zero β] (a : α) (f : α →₀ β) : α →₀ β := ⟨f.support.erase a, (λa', if a' = a then 0 else f a'), assume a', by by_cases a' = a; simp [h]⟩ @[simp] lemma support_erase [has_zero β] {a : α} {f : α →₀ β} : (f.erase a).support = f.support.erase a := rfl @[simp] lemma erase_same [has_zero β] {a : α} {f : α →₀ β} : (f.erase a) a = 0 := if_pos rfl @[simp] lemma erase_ne [has_zero β] {a a' : α} {f : α →₀ β} (h : a' ≠ a) : (f.erase a) a' = f a' := if_neg h end erase -- [to_additive finsupp.sum] for finsupp.prod doesn't work, the equation lemmas are not generated /-- `sum f g` is the sum of `g a (f a)` over the support of `f`. -/ def sum [has_zero β] [add_comm_monoid γ] (f : α →₀ β) (g : α → β → γ) : γ := f.support.sum (λa, g a (f a)) /-- `prod f g` is the product of `g a (f a)` over the support of `f`. -/ @[to_additive finsupp.sum] def prod [has_zero β] [comm_monoid γ] (f : α →₀ β) (g : α → β → γ) : γ := f.support.prod (λa, g a (f a)) attribute [to_additive finsupp.sum.equations._eqn_1] finsupp.prod.equations._eqn_1 @[to_additive finsupp.sum_map_range_index] lemma prod_map_range_index [has_zero β₁] [has_zero β₂] [comm_monoid γ] [decidable_eq β₂] {f : β₁ → β₂} {hf : f 0 = 0} {g : α →₀ β₁} {h : α → β₂ → γ} (h0 : ∀a, h a 0 = 1) : (map_range f hf g).prod h = g.prod (λa b, h a (f b)) := finset.prod_subset support_map_range $ by simp [h0] {contextual := tt} @[to_additive finsupp.sum_zero_index] lemma prod_zero_index [add_comm_monoid β] [comm_monoid γ] {h : α → β → γ} : (0 : α →₀ β).prod h = 1 := by simp [finsupp.prod] section decidable variables [decidable_eq α] [decidable_eq β] section add_monoid variables [add_monoid β] @[to_additive finsupp.sum_single_index] lemma prod_single_index [comm_monoid γ] {a : α} {b : β} {h : α → β → γ} (h_zero : h a 0 = 1) : (single a b).prod h = h a b := begin by_cases h : b = 0, { simp [h, prod_zero_index, h_zero], refl }, { simp [finsupp.prod, support_single_ne_zero h] } end instance : has_add (α →₀ β) := ⟨zip_with (+) (add_zero 0)⟩ @[simp] lemma add_apply {g₁ g₂ : α →₀ β} {a : α} : (g₁ + g₂) a = g₁ a + g₂ a := rfl lemma support_add {g₁ g₂ : α →₀ β} : (g₁ + g₂).support ⊆ g₁.support ∪ g₂.support := support_zip_with @[simp] lemma single_add {a : α} {b₁ b₂ : β} : single a (b₁ + b₂) = single a b₁ + single a b₂ := ext $ assume a', begin by_cases h : a = a', { rw [h, add_apply, single_eq_same, single_eq_same, single_eq_same] }, { rw [add_apply, single_eq_of_ne h, single_eq_of_ne h, single_eq_of_ne h, zero_add] } end instance : add_monoid (α →₀ β) := { add_monoid . zero := 0, add := (+), add_assoc := assume ⟨s, f, hf⟩ ⟨t, g, hg⟩ ⟨u, h, hh⟩, ext $ assume a, add_assoc _ _ _, zero_add := assume ⟨s, f, hf⟩, ext $ assume a, zero_add _, add_zero := assume ⟨s, f, hf⟩, ext $ assume a, add_zero _ } lemma single_add_erase {a : α} {f : α →₀ β} : single a (f a) + f.erase a = f := ext $ λ a', if h : a = a' then by subst h; simp else by simp [ne.symm h, h] lemma erase_add_single {a : α} {f : α →₀ β} : f.erase a + single a (f a) = f := ext $ λ a', if h : a = a' then by subst h; simp else by simp [ne.symm h, h] protected theorem induction {p : (α →₀ β) → Prop} (f : α →₀ β) (h0 : p 0) (ha : ∀a b (f : α →₀ β), a ∉ f.support → b ≠ 0 → p f → p (single a b + f)) : p f := suffices ∀s (f : α →₀ β), f.support = s → p f, from this _ _ rfl, assume s, finset.induction_on s (by simp [h0] {contextual := tt}) $ assume a s has ih f hf, suffices p (single a (f a) + f.erase a), by rwa [single_add_erase] at this, begin apply ha, { simp }, { rw [← mem_support_iff _ a, hf], simp }, { apply ih _ _, simp [hf, has, finset.erase_insert] } end lemma induction₂ {p : (α →₀ β) → Prop} (f : α →₀ β) (h0 : p 0) (ha : ∀a b (f : α →₀ β), a ∉ f.support → b ≠ 0 → p f → p (f + single a b)) : p f := suffices ∀s (f : α →₀ β), f.support = s → p f, from this _ _ rfl, assume s, finset.induction_on s (by simp [h0] {contextual := tt}) $ assume a s has ih f hf, suffices p (f.erase a + single a (f a)), by rwa [erase_add_single] at this, begin apply ha, { simp }, { rw [← mem_support_iff _ a, hf], simp }, { apply ih _ _, simp [hf, has, finset.erase_insert] } end end add_monoid instance [add_comm_monoid β] : add_comm_monoid (α →₀ β) := { add_comm := assume ⟨s, f, _⟩ ⟨t, g, _⟩, ext $ assume a, add_comm _ _, .. finsupp.add_monoid } instance [add_group β] : add_group (α →₀ β) := { neg := map_range (has_neg.neg) neg_zero, add_left_neg := assume ⟨s, f, _⟩, ext $ assume x, add_left_neg _, .. finsupp.add_monoid } @[to_additive finsupp.sum_neg_index] lemma prod_neg_index [add_group β] [comm_monoid γ] {g : α →₀ β} {h : α → β → γ} (h0 : ∀a, h a 0 = 1) : (-g).prod h = g.prod (λa b, h a (- b)) := prod_map_range_index h0 @[simp] lemma neg_apply [add_group β] {g : α →₀ β} {a : α} : (- g) a = - g a := rfl @[simp] lemma sub_apply [add_group β] {g₁ g₂ : α →₀ β} {a : α} : (g₁ - g₂) a = g₁ a - g₂ a := rfl @[simp] lemma support_neg [add_group β] {f : α →₀ β} : support (-f) = support f := finset.subset.antisymm support_map_range (calc support f = support (- (- f)) : by simp ... ⊆ support (- f) : support_map_range) instance [add_comm_group β] : add_comm_group (α →₀ β) := { add_comm := add_comm, ..finsupp.add_group } @[simp] lemma sum_apply [has_zero β₁] [add_comm_monoid β] {f : α₁ →₀ β₁} {g : α₁ → β₁ → α →₀ β} {a₂ : α} : (f.sum g) a₂ = f.sum (λa₁ b, g a₁ b a₂) := (finset.sum_hom (λf : α →₀ β, f a₂) rfl (assume a b, rfl)).symm lemma support_sum [has_zero β₁] [add_comm_monoid β] {f : α₁ →₀ β₁} {g : α₁ → β₁ → (α →₀ β)} : (f.sum g).support ⊆ f.support.bind (λa, (g a (f a)).support) := have ∀a₁ : α, f.sum (λ (a : α₁) (b : β₁), (g a b) a₁) ≠ 0 → (∃ (a : α₁), f a ≠ 0 ∧ ¬ (g a (f a)) a₁ = 0), from assume a₁ h, let ⟨a, ha, ne⟩ := finset.exists_ne_zero_of_sum_ne_zero h in ⟨a, (f.mem_support_iff a).mp ha, ne⟩, by simpa [finset.subset_iff, mem_support_iff, finset.mem_bind, sum_apply] using this @[simp] lemma sum_zero {γ : Type w} [add_comm_monoid β] [add_comm_monoid γ] {f : α →₀ β} : f.sum (λa b, (0 : γ)) = 0 := finset.sum_const_zero @[simp] lemma sum_add {γ : Type w} [add_comm_monoid β] [add_comm_monoid γ] {f : α →₀ β} {h₁ h₂ : α → β → γ} : f.sum (λa b, h₁ a b + h₂ a b) = f.sum h₁ + f.sum h₂ := finset.sum_add_distrib @[simp] lemma sum_neg {γ : Type w} [add_comm_monoid β] [add_comm_group γ] {f : α →₀ β} {h : α → β → γ} : f.sum (λa b, - h a b) = - f.sum h := finset.sum_hom (@has_neg.neg γ _) neg_zero (assume a b, neg_add _ _) @[simp] lemma sum_single [add_comm_monoid β] {f : α →₀ β} : f.sum single = f := have ∀a:α, f.sum (λa' b, ite (a' = a) b 0) = ({a} : finset α).sum (λa', ite (a' = a) (f a') 0), begin intro a, by_cases h : a ∈ f.support, { have : (finset.singleton a : finset α) ⊆ f.support, { simp [finset.subset_iff, *] at * }, refine (finset.sum_subset this _).symm, simp {contextual := tt} }, { transitivity (f.support.sum (λa, (0 : β))), { refine (finset.sum_congr rfl _), intros a' ha', have h: a' ≠ a, { assume eq, simp * at * }, simp * at * }, { simp * at * } } end, ext $ assume a, by simp [single_apply, this] @[to_additive finsupp.sum_add_index] lemma prod_add_index {γ : Type w} [add_comm_monoid β] [comm_monoid γ] {f g : α →₀ β} {h : α → β → γ} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (f + g).prod h = f.prod h * g.prod h := have f_eq : (f.support ∪ g.support).prod (λa, h a (f a)) = f.prod h, from (finset.prod_subset finset.subset_union_left $ by simp [mem_support_iff, h_zero] {contextual := tt}).symm, have g_eq : (f.support ∪ g.support).prod (λa, h a (g a)) = g.prod h, from (finset.prod_subset finset.subset_union_right $ by simp [mem_support_iff, h_zero] {contextual := tt}).symm, calc (f + g).support.prod (λa, h a ((f + g) a)) = (f.support ∪ g.support).prod (λa, h a ((f + g) a)) : finset.prod_subset support_add $ by simp [mem_support_iff, h_zero] {contextual := tt} ... = (f.support ∪ g.support).prod (λa, h a (f a)) * (f.support ∪ g.support).prod (λa, h a (g a)) : by simp [h_add, finset.prod_mul_distrib] ... = _ : by rw [f_eq, g_eq] lemma sum_sub_index {γ : Type w} [add_comm_group β] [add_comm_group γ] {f g : α →₀ β} {h : α → β → γ} (h_sub : ∀a b₁ b₂, h a (b₁ - b₂) = h a b₁ - h a b₂) : (f - g).sum h = f.sum h - g.sum h := have h_zero : ∀a, h a 0 = 0, from assume a, have h a (0 - 0) = h a 0 - h a 0, from h_sub a 0 0, by simpa using this, have h_neg : ∀a b, h a (- b) = - h a b, from assume a b, have h a (0 - b) = h a 0 - h a b, from h_sub a 0 b, by simpa [h_zero] using this, have h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ + h a b₂, from assume a b₁ b₂, have h a (b₁ - (- b₂)) = h a b₁ - h a (- b₂), from h_sub a b₁ (-b₂), by simpa [h_neg] using this, calc (f - g).sum h = (f + - g).sum h : by simp ... = f.sum h + - g.sum h : by simp [sum_add_index, sum_neg_index, h_add, h_zero, h_neg] ... = _ : by simp @[to_additive finsupp.sum_finset_sum_index] lemma prod_finset_sum_index {γ : Type w} {ι : Type x} [add_comm_monoid β] [comm_monoid γ] [decidable_eq ι] {s : finset ι} {g : ι → α →₀ β} {h : α → β → γ} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂): s.prod (λi, (g i).prod h) = (s.sum g).prod h := finset.induction_on s (by simp [prod_zero_index]) (by simp [prod_add_index, h_zero, h_add] {contextual := tt}) @[to_additive finsupp.sum_sum_index] lemma prod_sum_index {γ : Type w} [decidable_eq α₁] [add_comm_monoid β₁] [add_comm_monoid β] [comm_monoid γ] {f : α₁ →₀ β₁} {g : α₁ → β₁ → α →₀ β} {h : α → β → γ} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂): (f.sum g).prod h = f.prod (λa b, (g a b).prod h) := (prod_finset_sum_index h_zero h_add).symm section map_domain variables [decidable_eq α₁] [decidable_eq α₂] [add_comm_monoid β] {v v₁ v₂ : α →₀ β} /-- Given `f : α₁ → α₂` and `v : α₁ →₀ β`, `map_domain f v : α₂ →₀ β` is the finitely supported function whose value at `a : α₂` is the sum of `v x` over all `x` such that `f x = a`. -/ def map_domain (f : α₁ → α₂) (v : α₁ →₀ β) : α₂ →₀ β := v.sum $ λa, single (f a) lemma map_domain_id : map_domain id v = v := sum_single lemma map_domain_comp {f : α → α₁} {g : α₁ → α₂} : map_domain (g ∘ f) v = map_domain g (map_domain f v) := by simp [map_domain, sum_sum_index, sum_single_index] lemma map_domain_single {f : α → α₁} {a : α} {b : β} : map_domain f (single a b) = single (f a) b := sum_single_index (by simp) lemma map_domain_zero {f : α → α₂} : map_domain f 0 = (0 : α₂ →₀ β) := sum_zero_index lemma map_domain_congr {f g : α → α₂} (h : ∀x∈v.support, f x = g x) : v.map_domain f = v.map_domain g := finset.sum_congr rfl $ by simp [*] at * {contextual := tt} lemma map_domain_add {f : α → α₂} : map_domain f (v₁ + v₂) = map_domain f v₁ + map_domain f v₂ := sum_add_index (by simp) (by simp) lemma map_domain_finset_sum {ι : Type x} [decidable_eq ι] {f : α → α₂} {s : finset ι} {v : ι → α →₀ β} : map_domain f (s.sum v) = s.sum (λi, map_domain f (v i)) := by refine (sum_finset_sum_index _ _).symm; simp lemma map_domain_sum [has_zero β₁] {f : α → α₂} {s : α →₀ β₁} {v : α → β₁ → α →₀ β} : map_domain f (s.sum v) = s.sum (λa b, map_domain f (v a b)) := by refine (sum_finset_sum_index _ _).symm; simp lemma map_domain_support {f : α → α₂} {s : α →₀ β} : (s.map_domain f).support ⊆ s.support.image f := finset.subset.trans support_sum $ finset.subset.trans (finset.bind_mono $ assume a ha, support_single_subset) $ by rw [finset.bind_singleton]; exact subset.refl _ @[to_additive finsupp.sum_map_domain_index] lemma prod_map_domain_index {γ : Type w} [comm_monoid γ] {f : α → α₂} {s : α →₀ β} {h : α₂ → β → γ} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (s.map_domain f).prod h = s.prod (λa b, h (f a) b) := by simp [map_domain, prod_sum_index, h_zero, h_add, prod_single_index] end map_domain /-- The product of `f g : α →₀ β` is the finitely supported function whose value at `a` is the sum of `f x * g y` over all pairs `x, y` such that `x + y = a`. (Think of the product of multivariate polynomials where `α` is the monoid of monomial exponents.) -/ instance [has_add α] [semiring β] : has_mul (α →₀ β) := ⟨λf g, f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ + a₂) (b₁ * b₂)⟩ lemma mul_def [has_add α] [semiring β] {f g : α →₀ β} : f * g = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ + a₂) (b₁ * b₂)) := rfl /-- The unit of the multiplication is `single 0 1`, i.e. the function that is 1 at 0 and zero elsewhere. -/ instance [has_zero α] [has_zero β] [has_one β] : has_one (α →₀ β) := ⟨single 0 1⟩ lemma one_def [has_zero α] [has_zero β] [has_one β] : 1 = (single 0 1 : α →₀ β) := rfl section filter -- TODO: remove filter? build upon subtype_domain? section has_zero variables [has_zero β] {p : α → Prop} [decidable_pred p] {f : α →₀ β} /-- `filter p f` is the function which is `f a` if `p a` is true and 0 otherwise. -/ def filter (p : α → Prop) [decidable_pred p] (f : α →₀ β) : α →₀ β := on_finset f.support (λa, if p a then f a else 0) (assume a, by by_cases (p a); simp [h]) @[simp] lemma filter_apply_pos {a : α} (h : p a) : f.filter p a = f a := if_pos h @[simp] lemma filter_apply_neg {a : α} (h : ¬ p a) : f.filter p a = 0 := if_neg h @[simp] lemma support_filter : (f.filter p).support = f.support.filter p := finset.ext.mpr $ assume a, by by_cases p a; simp * end has_zero lemma filter_pos_add_filter_neg [add_monoid β] {f : α →₀ β} {p : α → Prop} [decidable_pred p] [decidable_pred (λa, ¬ p a)] : f.filter p + f.filter (λa, ¬ p a) = f := finsupp.ext $ assume a, by by_cases p a; simp * end filter section subtype_domain variables {α' : Type u₁} {δ : Type y} [has_zero δ] {p : α → Prop} [decidable_pred p] section zero variables [has_zero β] {v v' : α' →₀ β} /-- `subtype_domain p f` is the restriction of the finitely supported function `f` to the subtype `p`. -/ def subtype_domain (p : α → Prop) [decidable_pred p] (f : α →₀ β) : (subtype p →₀ β) := ⟨f.support.subtype p, f ∘ subtype.val, by simp⟩ @[simp] lemma support_subtype_domain {f : α →₀ β} : (subtype_domain p f).support = f.support.subtype p := rfl @[simp] lemma subtype_domain_apply {a : subtype p} {v : α →₀ β} : (subtype_domain p v) a = v (a.val) := rfl @[simp] lemma subtype_domain_zero : subtype_domain p (0 : α →₀ β) = 0 := rfl @[to_additive finsupp.sum_subtype_domain_index] lemma prod_subtype_domain_index [comm_monoid γ] {v : α →₀ β} {h : α → β → γ} (hp : ∀x∈v.support, p x) : (v.subtype_domain p).prod (λa b, h a.1 b) = v.prod h := prod_bij (λp _, p.val) (by simp) (by simp) (assume ⟨a₀, ha₀⟩ ⟨a₁, ha₁⟩, by simp) (begin simp; exact assume b hb, ⟨b, hp _ (by simp [hb]), by simp [hb]⟩ end) end zero section monoid variables [add_monoid β] {v v' : α' →₀ β} @[simp] lemma subtype_domain_add {v v' : α →₀ β} : (v + v').subtype_domain p = v.subtype_domain p + v'.subtype_domain p := ext $ by simp end monoid section comm_monoid variables [add_comm_monoid β] lemma subtype_domain_sum {s : finset γ} {h : γ → α →₀ β} : (s.sum h).subtype_domain p = s.sum (λc, (h c).subtype_domain p) := eq.symm (finset.sum_hom _ subtype_domain_zero $ assume v v', subtype_domain_add) lemma subtype_domain_finsupp_sum {s : γ →₀ δ} {h : γ → δ → α →₀ β} : (s.sum h).subtype_domain p = s.sum (λc d, (h c d).subtype_domain p) := subtype_domain_sum end comm_monoid section group variables [add_group β] {v v' : α' →₀ β} @[simp] lemma subtype_domain_neg {v : α →₀ β} : (- v).subtype_domain p = - v.subtype_domain p := ext $ by simp @[simp] lemma subtype_domain_sub {v v' : α →₀ β} : (v - v').subtype_domain p = v.subtype_domain p - v'.subtype_domain p := ext $ by simp end group end subtype_domain section variables [add_monoid α] [semiring β] -- TODO: the simplifier unfolds 0 in the instance proof! private lemma zero_mul (f : α →₀ β) : 0 * f = 0 := by simp [mul_def, sum_zero_index] private lemma mul_zero (f : α →₀ β) : f * 0 = 0 := by simp [mul_def, sum_zero_index] private lemma left_distrib (a b c : α →₀ β) : a * (b + c) = a * b + a * c := by simp [mul_def, sum_add_index, mul_add] private lemma right_distrib (a b c : α →₀ β) : (a + b) * c = a * c + b * c := by simp [mul_def, sum_add_index, add_mul] def to_semiring : semiring (α →₀ β) := { one := 1, mul := (*), one_mul := assume f, by simp [mul_def, one_def, sum_single_index], mul_one := assume f, by simp [mul_def, one_def, sum_single_index], zero_mul := zero_mul, mul_zero := mul_zero, mul_assoc := assume f g h, by simp [mul_def, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, add_mul, mul_add, mul_assoc], left_distrib := left_distrib, right_distrib := right_distrib, .. finsupp.add_comm_monoid } end local attribute [instance] to_semiring def to_comm_semiring [add_comm_monoid α] [comm_semiring β] : comm_semiring (α →₀ β) := { mul_comm := assume f g, begin simp [mul_def, finsupp.sum, mul_comm], rw [finset.sum_comm], simp end, .. finsupp.to_semiring } local attribute [instance] to_comm_semiring def to_ring [add_monoid α] [ring β] : ring (α →₀ β) := { neg := has_neg.neg, add_left_neg := add_left_neg, .. finsupp.to_semiring } def to_comm_ring [add_comm_monoid α] [comm_ring β] : comm_ring (α →₀ β) := { mul_comm := mul_comm, .. finsupp.to_ring} lemma single_mul_single [has_add α] [semiring β] {a₁ a₂ : α} {b₁ b₂ : β}: single a₁ b₁ * single a₂ b₂ = single (a₁ + a₂) (b₁ * b₂) := by simp [mul_def, sum_single_index] lemma prod_single {ι : Type x} [decidable_eq ι] [add_comm_monoid α] [comm_semiring β] {s : finset ι} {a : ι → α} {b : ι → β} : s.prod (λi, single (a i) (b i)) = single (s.sum a) (s.prod b) := finset.induction_on s (by simp [one_def]) (by simp [single_mul_single] {contextual := tt}) def to_has_scalar [semiring β] : has_scalar β (α →₀ β) := ⟨λa v, v.map_range ((*) a) (mul_zero a)⟩ local attribute [instance] to_has_scalar @[simp] lemma smul_apply [semiring β] {a : α} {b : β} {v : α →₀ β} : (b • v) a = b * v a := rfl /- should this be stronger? [module γ β] → module γ (α →₀ β) -/ def to_module [ring β] : module β (α →₀ β) := { smul := (•), smul_add := assume a x y, finsupp.ext $ by simp [mul_add], add_smul := assume a x y, finsupp.ext $ by simp [add_mul], one_smul := assume x, finsupp.ext $ by simp, mul_smul := assume r s x, finsupp.ext $ by simp [mul_assoc], .. finsupp.add_comm_group } lemma sum_smul_index [ring β] [add_comm_monoid γ] {g : α →₀ β} {b : β} {h : α → β → γ} (h0 : ∀i, h i 0 = 0) : (b • g).sum h = g.sum (λi a, h i (b * a)) := finsupp.sum_map_range_index h0 end decidable end finsupp
8dd11897f3040b0cb2fa18cd180293059e88e7a7
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/library/data/nat/div.lean
cdb02734c94a4abdf48ff1f75b0cba57b966292b
[ "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
25,696
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura Definitions and properties of div and mod. Much of the development follows Isabelle's library. -/ import data.nat.sub open eq.ops well_founded decidable prod namespace nat /- div -/ -- auxiliary lemma used to justify div private definition div_rec_lemma {x y : nat} : 0 < y ∧ y ≤ x → x - y < x := and.rec (λ ypos ylex, sub_lt (lt_of_lt_of_le ypos ylex) ypos) private definition div.F (x : nat) (f : Π x₁, x₁ < x → nat → nat) (y : nat) : nat := if H : 0 < y ∧ y ≤ x then f (x - y) (div_rec_lemma H) y + 1 else zero protected definition div := fix div.F definition nat_has_divide [reducible] [instance] [priority nat.prio] : has_div nat := has_div.mk nat.div theorem div_def (x y : nat) : div x y = if 0 < y ∧ y ≤ x then div (x - y) y + 1 else 0 := congr_fun (fix_eq div.F x) y protected theorem div_zero [simp] (a : ℕ) : a / 0 = 0 := div_def a 0 ⬝ if_neg (!not_and_of_not_left (lt.irrefl 0)) theorem div_eq_zero_of_lt {a b : ℕ} (h : a < b) : a / b = 0 := div_def a b ⬝ if_neg (!not_and_of_not_right (not_le_of_gt h)) protected theorem zero_div [simp] (b : ℕ) : 0 / b = 0 := div_def 0 b ⬝ if_neg (and.rec not_le_of_gt) theorem div_eq_succ_sub_div {a b : ℕ} (h₁ : b > 0) (h₂ : a ≥ b) : a / b = succ ((a - b) / b) := div_def a b ⬝ if_pos (and.intro h₁ h₂) theorem add_div_self (x : ℕ) {z : ℕ} (H : z > 0) : (x + z) / z = succ (x / z) := calc (x + z) / z = if 0 < z ∧ z ≤ x + z then (x + z - z) / z + 1 else 0 : !div_def ... = (x + z - z) / z + 1 : if_pos (and.intro H (le_add_left z x)) ... = succ (x / z) : {!nat.add_sub_cancel} theorem add_div_self_left {x : ℕ} (z : ℕ) (H : x > 0) : (x + z) / x = succ (z / x) := !add.comm ▸ !add_div_self H local attribute succ_mul [simp] theorem add_mul_div_self {x y z : ℕ} (H : z > 0) : (x + y * z) / z = x / z + y := nat.induction_on y (by simp) (take y, assume IH : (x + y * z) / z = x / z + y, calc (x + succ y * z) / z = (x + y * z + z) / z : by inst_simp ... = succ ((x + y * z) / z) : !add_div_self H ... = succ (x / z + y) : IH) theorem add_mul_div_self_left (x z : ℕ) {y : ℕ} (H : y > 0) : (x + y * z) / y = x / y + z := !mul.comm ▸ add_mul_div_self H protected theorem mul_div_cancel (m : ℕ) {n : ℕ} (H : n > 0) : m * n / n = m := calc m * n / n = (0 + m * n) / n : by simp ... = 0 / n + m : add_mul_div_self H ... = m : by simp protected theorem mul_div_cancel_left {m : ℕ} (n : ℕ) (H : m > 0) : m * n / m = n := !mul.comm ▸ !nat.mul_div_cancel H /- mod -/ private definition mod.F (x : nat) (f : Π x₁, x₁ < x → nat → nat) (y : nat) : nat := if H : 0 < y ∧ y ≤ x then f (x - y) (div_rec_lemma H) y else x protected definition mod := fix mod.F definition nat_has_mod [reducible] [instance] [priority nat.prio] : has_mod nat := has_mod.mk nat.mod notation [priority nat.prio] a ≡ b `[mod `:0 c:0 `]` := a % c = b % c theorem mod_def (x y : nat) : mod x y = if 0 < y ∧ y ≤ x then mod (x - y) y else x := congr_fun (fix_eq mod.F x) y theorem mod_zero [simp] (a : ℕ) : a % 0 = a := mod_def a 0 ⬝ if_neg (!not_and_of_not_left (lt.irrefl 0)) theorem mod_eq_of_lt {a b : ℕ} (h : a < b) : a % b = a := mod_def a b ⬝ if_neg (!not_and_of_not_right (not_le_of_gt h)) theorem zero_mod [simp] (b : ℕ) : 0 % b = 0 := mod_def 0 b ⬝ if_neg (λ h, and.rec_on h (λ l r, absurd (lt_of_lt_of_le l r) (lt.irrefl 0))) theorem mod_eq_sub_mod {a b : ℕ} (h₁ : b > 0) (h₂ : a ≥ b) : a % b = (a - b) % b := mod_def a b ⬝ if_pos (and.intro h₁ h₂) theorem add_mod_self [simp] (x z : ℕ) : (x + z) % z = x % z := by_cases_zero_pos z (by rewrite add_zero) (take z, assume H : z > 0, calc (x + z) % z = if 0 < z ∧ z ≤ x + z then (x + z - z) % z else _ : mod_def ... = (x + z - z) % z : if_pos (and.intro H (le_add_left z x)) ... = x % z : nat.add_sub_cancel) theorem add_mod_self_left [simp] (x z : ℕ) : (x + z) % x = z % x := !add.comm ▸ !add_mod_self local attribute succ_mul [simp] theorem add_mul_mod_self [simp] (x y z : ℕ) : (x + y * z) % z = x % z := nat.induction_on y (by simp) (by inst_simp) theorem add_mul_mod_self_left [simp] (x y z : ℕ) : (x + y * z) % y = x % y := by inst_simp theorem mul_mod_left [simp] (m n : ℕ) : (m * n) % n = 0 := calc (m * n) % n = (0 + m * n) % n : by simp ... = 0 : by inst_simp theorem mul_mod_right [simp] (m n : ℕ) : (m * n) % m = 0 := by inst_simp theorem mod_lt (x : ℕ) {y : ℕ} (H : y > 0) : x % y < y := nat.case_strong_induction_on x (show 0 % y < y, from !zero_mod⁻¹ ▸ H) (take x, assume IH : ∀x', x' ≤ x → x' % y < y, show succ x % y < y, from by_cases -- (succ x < y) (assume H1 : succ x < y, have succ x % y = succ x, from mod_eq_of_lt H1, show succ x % y < y, from this⁻¹ ▸ H1) (assume H1 : ¬ succ x < y, have y ≤ succ x, from le_of_not_gt H1, have h : succ x % y = (succ x - y) % y, from mod_eq_sub_mod H this, have succ x - y < succ x, from sub_lt !succ_pos H, have succ x - y ≤ x, from le_of_lt_succ this, show succ x % y < y, from h⁻¹ ▸ IH _ this)) theorem mod_one (n : ℕ) : n % 1 = 0 := have H1 : n % 1 < 1, from !mod_lt !succ_pos, eq_zero_of_le_zero (le_of_lt_succ H1) /- properties of div and mod -/ -- the quotient - remainder theorem theorem eq_div_mul_add_mod (x y : ℕ) : x = x / y * y + x % y := begin eapply by_cases_zero_pos y, show x = x / 0 * 0 + x % 0, from (calc x / 0 * 0 + x % 0 = 0 + x % 0 : mul_zero ... = x % 0 : zero_add ... = x : mod_zero)⁻¹, intro y H, show x = x / y * y + x % y, begin eapply nat.case_strong_induction_on x, show 0 = (0 / y) * y + 0 % y, by rewrite [zero_mod, add_zero, nat.zero_div, zero_mul], intro x IH, show succ x = succ x / y * y + succ x % y, from if H1 : succ x < y then assert H2 : succ x / y = 0, from div_eq_zero_of_lt H1, assert H3 : succ x % y = succ x, from mod_eq_of_lt H1, begin rewrite [H2, H3, zero_mul, zero_add] end else have H2 : y ≤ succ x, from le_of_not_gt H1, assert H3 : succ x / y = succ ((succ x - y) / y), from div_eq_succ_sub_div H H2, assert H4 : succ x % y = (succ x - y) % y, from mod_eq_sub_mod H H2, have H5 : succ x - y < succ x, from sub_lt !succ_pos H, assert H6 : succ x - y ≤ x, from le_of_lt_succ H5, (calc succ x / y * y + succ x % y = succ ((succ x - y) / y) * y + succ x % y : by rewrite H3 ... = ((succ x - y) / y) * y + y + succ x % y : by rewrite succ_mul ... = ((succ x - y) / y) * y + y + (succ x - y) % y : by rewrite H4 ... = ((succ x - y) / y) * y + (succ x - y) % y + y : add.right_comm ... = succ x - y + y : by rewrite -(IH _ H6) ... = succ x : nat.sub_add_cancel H2)⁻¹ end end theorem mod_eq_sub_div_mul (x y : ℕ) : x % y = x - x / y * y := nat.eq_sub_of_add_eq (!add.comm ▸ !eq_div_mul_add_mod)⁻¹ theorem mod_add_mod (m n k : ℕ) : (m % n + k) % n = (m + k) % n := by rewrite [eq_div_mul_add_mod m n at {2}, add.assoc, add.comm (m / n * n), add_mul_mod_self] theorem add_mod_mod (m n k : ℕ) : (m + n % k) % k = (m + n) % k := by rewrite [add.comm, mod_add_mod, add.comm] theorem add_mod_eq_add_mod_right {m n k : ℕ} (i : ℕ) (H : m % n = k % n) : (m + i) % n = (k + i) % n := by rewrite [-mod_add_mod, -mod_add_mod k, H] theorem add_mod_eq_add_mod_left {m n k : ℕ} (i : ℕ) (H : m % n = k % n) : (i + m) % n = (i + k) % n := by rewrite [add.comm, add_mod_eq_add_mod_right _ H, add.comm] theorem mod_eq_mod_of_add_mod_eq_add_mod_right {m n k i : ℕ} : (m + i) % n = (k + i) % n → m % n = k % n := by_cases_zero_pos n (by rewrite [*mod_zero]; apply eq_of_add_eq_add_right) (take n, assume npos : n > 0, assume H1 : (m + i) % n = (k + i) % n, have H2 : (m + i % n) % n = (k + i % n) % n, by rewrite [*add_mod_mod, H1], assert H3 : (m + i % n + (n - i % n)) % n = (k + i % n + (n - i % n)) % n, from add_mod_eq_add_mod_right _ H2, begin revert H3, rewrite [*add.assoc, add_sub_of_le (le_of_lt (!mod_lt npos)), *add_mod_self], intros, assumption end) theorem mod_eq_mod_of_add_mod_eq_add_mod_left {m n k i : ℕ} : (i + m) % n = (i + k) % n → m % n = k % n := by rewrite [add.comm i m, add.comm i k]; apply mod_eq_mod_of_add_mod_eq_add_mod_right theorem mod_le {x y : ℕ} : x % y ≤ x := !eq_div_mul_add_mod⁻¹ ▸ !le_add_left theorem eq_remainder {q1 r1 q2 r2 y : ℕ} (H1 : r1 < y) (H2 : r2 < y) (H3 : q1 * y + r1 = q2 * y + r2) : r1 = r2 := calc r1 = r1 % y : mod_eq_of_lt H1 ... = (r1 + q1 * y) % y : !add_mul_mod_self⁻¹ ... = (q1 * y + r1) % y : add.comm ... = (r2 + q2 * y) % y : by rewrite [H3, add.comm] ... = r2 % y : !add_mul_mod_self ... = r2 : mod_eq_of_lt H2 theorem eq_quotient {q1 r1 q2 r2 y : ℕ} (H1 : r1 < y) (H2 : r2 < y) (H3 : q1 * y + r1 = q2 * y + r2) : q1 = q2 := have H4 : q1 * y + r2 = q2 * y + r2, from (eq_remainder H1 H2 H3) ▸ H3, have H5 : q1 * y = q2 * y, from add.right_cancel H4, have H6 : y > 0, from lt_of_le_of_lt !zero_le H1, show q1 = q2, from eq_of_mul_eq_mul_right H6 H5 protected theorem mul_div_mul_left {z : ℕ} (x y : ℕ) (zpos : z > 0) : (z * x) / (z * y) = x / y := if H : y = 0 then by rewrite [H, mul_zero, *nat.div_zero] else have ypos : y > 0, from pos_of_ne_zero H, have zypos : z * y > 0, from mul_pos zpos ypos, have H1 : (z * x) % (z * y) < z * y, from !mod_lt zypos, have H2 : z * (x % y) < z * y, from mul_lt_mul_of_pos_left (!mod_lt ypos) zpos, eq_quotient H1 H2 (calc ((z * x) / (z * y)) * (z * y) + (z * x) % (z * y) = z * x : eq_div_mul_add_mod ... = z * (x / y * y + x % y) : eq_div_mul_add_mod ... = z * (x / y * y) + z * (x % y) : left_distrib ... = (x / y) * (z * y) + z * (x % y) : mul.left_comm) protected theorem mul_div_mul_right {x z y : ℕ} (zpos : z > 0) : (x * z) / (y * z) = x / y := !mul.comm ▸ !mul.comm ▸ !nat.mul_div_mul_left zpos theorem mul_mod_mul_left (z x y : ℕ) : (z * x) % (z * y) = z * (x % y) := or.elim (eq_zero_or_pos z) (assume H : z = 0, H⁻¹ ▸ calc (0 * x) % (z * y) = 0 % (z * y) : zero_mul ... = 0 : zero_mod ... = 0 * (x % y) : zero_mul) (assume zpos : z > 0, or.elim (eq_zero_or_pos y) (assume H : y = 0, by rewrite [H, mul_zero, *mod_zero]) (assume ypos : y > 0, have zypos : z * y > 0, from mul_pos zpos ypos, have H1 : (z * x) % (z * y) < z * y, from !mod_lt zypos, have H2 : z * (x % y) < z * y, from mul_lt_mul_of_pos_left (!mod_lt ypos) zpos, eq_remainder H1 H2 (calc ((z * x) / (z * y)) * (z * y) + (z * x) % (z * y) = z * x : eq_div_mul_add_mod ... = z * (x / y * y + x % y) : eq_div_mul_add_mod ... = z * (x / y * y) + z * (x % y) : left_distrib ... = (x / y) * (z * y) + z * (x % y) : mul.left_comm))) theorem mul_mod_mul_right (x z y : ℕ) : (x * z) % (y * z) = (x % y) * z := mul.comm z x ▸ mul.comm z y ▸ !mul.comm ▸ !mul_mod_mul_left theorem mod_self (n : ℕ) : n % n = 0 := nat.cases_on n (by rewrite zero_mod) (take n, by rewrite [-zero_add (succ n) at {1}, add_mod_self]) theorem mul_mod_eq_mod_mul_mod (m n k : nat) : (m * n) % k = ((m % k) * n) % k := calc (m * n) % k = (((m / k) * k + m % k) * n) % k : eq_div_mul_add_mod ... = ((m % k) * n) % k : by rewrite [right_distrib, mul.right_comm, add.comm, add_mul_mod_self] theorem mul_mod_eq_mul_mod_mod (m n k : nat) : (m * n) % k = (m * (n % k)) % k := !mul.comm ▸ !mul.comm ▸ !mul_mod_eq_mod_mul_mod protected theorem div_one (n : ℕ) : n / 1 = n := assert n / 1 * 1 + n % 1 = n, from !eq_div_mul_add_mod⁻¹, begin rewrite [-this at {2}, mul_one, mod_one] end protected theorem div_self {n : ℕ} (H : n > 0) : n / n = 1 := assert (n * 1) / (n * 1) = 1 / 1, from !nat.mul_div_mul_left H, by rewrite [nat.div_one at this, -this, *mul_one] theorem div_mul_cancel_of_mod_eq_zero {m n : ℕ} (H : m % n = 0) : m / n * n = m := by rewrite [eq_div_mul_add_mod m n at {2}, H, add_zero] theorem mul_div_cancel_of_mod_eq_zero {m n : ℕ} (H : m % n = 0) : n * (m / n) = m := !mul.comm ▸ div_mul_cancel_of_mod_eq_zero H /- dvd -/ theorem dvd_of_mod_eq_zero {m n : ℕ} (H : n % m = 0) : m ∣ n := dvd.intro (!mul.comm ▸ div_mul_cancel_of_mod_eq_zero H) theorem mod_eq_zero_of_dvd {m n : ℕ} (H : m ∣ n) : n % m = 0 := dvd.elim H (take z, assume H1 : n = m * z, H1⁻¹ ▸ !mul_mod_right) theorem dvd_iff_mod_eq_zero (m n : ℕ) : m ∣ n ↔ n % m = 0 := iff.intro mod_eq_zero_of_dvd dvd_of_mod_eq_zero definition dvd.decidable_rel [instance] : decidable_rel dvd := take m n, decidable_of_decidable_of_iff _ (iff.symm !dvd_iff_mod_eq_zero) protected theorem div_mul_cancel {m n : ℕ} (H : n ∣ m) : m / n * n = m := div_mul_cancel_of_mod_eq_zero (mod_eq_zero_of_dvd H) protected theorem mul_div_cancel' {m n : ℕ} (H : n ∣ m) : n * (m / n) = m := !mul.comm ▸ nat.div_mul_cancel H theorem dvd_of_dvd_add_left {m n₁ n₂ : ℕ} (H₁ : m ∣ n₁ + n₂) (H₂ : m ∣ n₁) : m ∣ n₂ := obtain (c₁ : nat) (Hc₁ : n₁ + n₂ = m * c₁), from H₁, obtain (c₂ : nat) (Hc₂ : n₁ = m * c₂), from H₂, have aux : m * (c₁ - c₂) = n₂, from calc m * (c₁ - c₂) = m * c₁ - m * c₂ : nat.mul_sub_left_distrib ... = n₁ + n₂ - m * c₂ : Hc₁ ... = n₁ + n₂ - n₁ : Hc₂ ... = n₂ : nat.add_sub_cancel_left, dvd.intro aux theorem dvd_of_dvd_add_right {m n₁ n₂ : ℕ} (H : m ∣ n₁ + n₂) : m ∣ n₂ → m ∣ n₁ := nat.dvd_of_dvd_add_left (!add.comm ▸ H) theorem dvd_sub {m n₁ n₂ : ℕ} (H1 : m ∣ n₁) (H2 : m ∣ n₂) : m ∣ n₁ - n₂ := by_cases (assume H3 : n₁ ≥ n₂, have H4 : n₁ = n₁ - n₂ + n₂, from (nat.sub_add_cancel H3)⁻¹, show m ∣ n₁ - n₂, from nat.dvd_of_dvd_add_right (H4 ▸ H1) H2) (assume H3 : ¬ (n₁ ≥ n₂), have H4 : n₁ - n₂ = 0, from sub_eq_zero_of_le (le_of_lt (lt_of_not_ge H3)), show m ∣ n₁ - n₂, from H4⁻¹ ▸ dvd_zero _) theorem dvd.antisymm {m n : ℕ} : m ∣ n → n ∣ m → m = n := by_cases_zero_pos n (assume H1, assume H2 : 0 ∣ m, eq_zero_of_zero_dvd H2) (take n, assume Hpos : n > 0, assume H1 : m ∣ n, assume H2 : n ∣ m, obtain k (Hk : n = m * k), from exists_eq_mul_right_of_dvd H1, obtain l (Hl : m = n * l), from exists_eq_mul_right_of_dvd H2, have n * (l * k) = n, from !mul.assoc ▸ Hl ▸ Hk⁻¹, have l * k = 1, from eq_one_of_mul_eq_self_right Hpos this, have k = 1, from eq_one_of_mul_eq_one_left this, show m = n, from (mul_one m)⁻¹ ⬝ (this ▸ Hk⁻¹)) protected theorem mul_div_assoc (m : ℕ) {n k : ℕ} (H : k ∣ n) : m * n / k = m * (n / k) := or.elim (eq_zero_or_pos k) (assume H1 : k = 0, calc m * n / k = m * n / 0 : H1 ... = 0 : nat.div_zero ... = m * 0 : mul_zero m ... = m * (n / 0) : nat.div_zero ... = m * (n / k) : H1) (assume H1 : k > 0, have H2 : n = n / k * k, from (nat.div_mul_cancel H)⁻¹, calc m * n / k = m * (n / k * k) / k : H2 ... = m * (n / k) * k / k : mul.assoc ... = m * (n / k) : nat.mul_div_cancel _ H1) theorem dvd_of_mul_dvd_mul_left {m n k : ℕ} (kpos : k > 0) (H : k * m ∣ k * n) : m ∣ n := dvd.elim H (take l, assume H1 : k * n = k * m * l, have H2 : n = m * l, from eq_of_mul_eq_mul_left kpos (H1 ⬝ !mul.assoc), dvd.intro H2⁻¹) theorem dvd_of_mul_dvd_mul_right {m n k : ℕ} (kpos : k > 0) (H : m * k ∣ n * k) : m ∣ n := nat.dvd_of_mul_dvd_mul_left kpos (!mul.comm ▸ !mul.comm ▸ H) lemma dvd_of_eq_mul (i j n : nat) : n = j*i → j ∣ n := begin intros, subst n, apply dvd_mul_right end theorem div_dvd_div {k m n : ℕ} (H1 : k ∣ m) (H2 : m ∣ n) : m / k ∣ n / k := have H3 : m = m / k * k, from (nat.div_mul_cancel H1)⁻¹, have H4 : n = n / k * k, from (nat.div_mul_cancel (dvd.trans H1 H2))⁻¹, or.elim (eq_zero_or_pos k) (assume H5 : k = 0, have H6: n / k = 0, from (congr_arg _ H5 ⬝ !nat.div_zero), H6⁻¹ ▸ !dvd_zero) (assume H5 : k > 0, nat.dvd_of_mul_dvd_mul_right H5 (H3 ▸ H4 ▸ H2)) protected theorem div_eq_iff_eq_mul_right {m n : ℕ} (k : ℕ) (H : n > 0) (H' : n ∣ m) : m / n = k ↔ m = n * k := iff.intro (assume H1, by rewrite [-H1, nat.mul_div_cancel' H']) (assume H1, by rewrite [H1, !nat.mul_div_cancel_left H]) protected theorem div_eq_iff_eq_mul_left {m n : ℕ} (k : ℕ) (H : n > 0) (H' : n ∣ m) : m / n = k ↔ m = k * n := !mul.comm ▸ !nat.div_eq_iff_eq_mul_right H H' protected theorem eq_mul_of_div_eq_right {m n k : ℕ} (H1 : n ∣ m) (H2 : m / n = k) : m = n * k := calc m = n * (m / n) : nat.mul_div_cancel' H1 ... = n * k : H2 protected theorem div_eq_of_eq_mul_right {m n k : ℕ} (H1 : n > 0) (H2 : m = n * k) : m / n = k := calc m / n = n * k / n : H2 ... = k : !nat.mul_div_cancel_left H1 protected theorem eq_mul_of_div_eq_left {m n k : ℕ} (H1 : n ∣ m) (H2 : m / n = k) : m = k * n := !mul.comm ▸ !nat.eq_mul_of_div_eq_right H1 H2 protected theorem div_eq_of_eq_mul_left {m n k : ℕ} (H1 : n > 0) (H2 : m = k * n) : m / n = k := !nat.div_eq_of_eq_mul_right H1 (!mul.comm ▸ H2) lemma add_mod_eq_of_dvd (i j n : nat) : n ∣ j → (i + j) % n = i % n := assume h, obtain k (hk : j = n * k), from exists_eq_mul_right_of_dvd h, begin subst j, rewrite mul.comm, apply add_mul_mod_self end /- / and ordering -/ lemma le_of_dvd {m n : nat} : n > 0 → m ∣ n → m ≤ n := assume (h₁ : n > 0) (h₂ : m ∣ n), assert h₃ : n % m = 0, from mod_eq_zero_of_dvd h₂, by_contradiction (λ nle : ¬ m ≤ n, have h₄ : m > n, from lt_of_not_ge nle, assert h₅ : n % m = n, from mod_eq_of_lt h₄, begin rewrite h₃ at h₅, subst n, exact absurd h₁ (lt.irrefl 0) end) theorem div_mul_le (m n : ℕ) : m / n * n ≤ m := calc m = m / n * n + m % n : eq_div_mul_add_mod ... ≥ m / n * n : le_add_right protected theorem div_le_of_le_mul {m n k : ℕ} (H : m ≤ n * k) : m / k ≤ n := or.elim (eq_zero_or_pos k) (assume H1 : k = 0, calc m / k = m / 0 : H1 ... = 0 : nat.div_zero ... ≤ n : zero_le) (assume H1 : k > 0, le_of_mul_le_mul_right (calc m / k * k ≤ m / k * k + m % k : le_add_right ... = m : eq_div_mul_add_mod ... ≤ n * k : H) H1) protected theorem div_le_self (m n : ℕ) : m / n ≤ m := nat.cases_on n (!nat.div_zero⁻¹ ▸ !zero_le) take n, have H : m ≤ m * succ n, from calc m = m * 1 : mul_one ... ≤ m * succ n : !mul_le_mul_left (succ_le_succ !zero_le), nat.div_le_of_le_mul H protected theorem mul_le_of_le_div {m n k : ℕ} (H : m ≤ n / k) : m * k ≤ n := calc m * k ≤ n / k * k : !mul_le_mul_right H ... ≤ n : div_mul_le protected theorem le_div_of_mul_le {m n k : ℕ} (H1 : k > 0) (H2 : m * k ≤ n) : m ≤ n / k := have H3 : m * k < (succ (n / k)) * k, from calc m * k ≤ n : H2 ... = n / k * k + n % k : eq_div_mul_add_mod ... < n / k * k + k : add_lt_add_left (!mod_lt H1) ... = (succ (n / k)) * k : succ_mul, le_of_lt_succ (lt_of_mul_lt_mul_right H3) protected theorem le_div_iff_mul_le {m n k : ℕ} (H : k > 0) : m ≤ n / k ↔ m * k ≤ n := iff.intro !nat.mul_le_of_le_div (!nat.le_div_of_mul_le H) protected theorem div_le_div {m n : ℕ} (k : ℕ) (H : m ≤ n) : m / k ≤ n / k := by_cases_zero_pos k (by rewrite [*nat.div_zero]) (take k, assume H1 : k > 0, nat.le_div_of_mul_le H1 (le.trans !div_mul_le H)) protected theorem div_lt_of_lt_mul {m n k : ℕ} (H : m < n * k) : m / k < n := lt_of_mul_lt_mul_right (calc m / k * k ≤ m / k * k + m % k : le_add_right ... = m : eq_div_mul_add_mod ... < n * k : H) protected theorem lt_mul_of_div_lt {m n k : ℕ} (H1 : k > 0) (H2 : m / k < n) : m < n * k := assert H3 : succ (m / k) * k ≤ n * k, from !mul_le_mul_right (succ_le_of_lt H2), have H4 : m / k * k + k ≤ n * k, by rewrite [succ_mul at H3]; apply H3, calc m = m / k * k + m % k : eq_div_mul_add_mod ... < m / k * k + k : add_lt_add_left (!mod_lt H1) ... ≤ n * k : H4 protected theorem div_lt_iff_lt_mul {m n k : ℕ} (H : k > 0) : m / k < n ↔ m < n * k := iff.intro (!nat.lt_mul_of_div_lt H) !nat.div_lt_of_lt_mul protected theorem div_le_iff_le_mul_of_div {m n : ℕ} (k : ℕ) (H : n > 0) (H' : n ∣ m) : m / n ≤ k ↔ m ≤ k * n := by rewrite [propext (!le_iff_mul_le_mul_right H), !nat.div_mul_cancel H'] protected theorem le_mul_of_div_le_of_div {m n k : ℕ} (H1 : n > 0) (H2 : n ∣ m) (H3 : m / n ≤ k) : m ≤ k * n := iff.mp (!nat.div_le_iff_le_mul_of_div H1 H2) H3 -- needed for integer division theorem mul_sub_div_of_lt {m n k : ℕ} (H : k < m * n) : (m * n - (k + 1)) / m = n - k / m - 1 := begin have H1 : k / m < n, from nat.div_lt_of_lt_mul (!mul.comm ▸ H), have H2 : n - k / m ≥ 1, from nat.le_sub_of_add_le (calc 1 + k / m = succ (k / m) : add.comm ... ≤ n : succ_le_of_lt H1), have H3 : n - k / m = n - k / m - 1 + 1, from (nat.sub_add_cancel H2)⁻¹, have H4 : m > 0, from pos_of_ne_zero (assume H': m = 0, not_lt_zero k (begin rewrite [H' at H, zero_mul at H], exact H end)), have H5 : k % m + 1 ≤ m, from succ_le_of_lt (!mod_lt H4), have H6 : m - (k % m + 1) < m, from nat.sub_lt_self H4 !succ_pos, calc (m * n - (k + 1)) / m = (m * n - (k / m * m + k % m + 1)) / m : eq_div_mul_add_mod ... = (m * n - k / m * m - (k % m + 1)) / m : by rewrite [*nat.sub_sub] ... = ((n - k / m) * m - (k % m + 1)) / m : by rewrite [mul.comm m, nat.mul_sub_right_distrib] ... = ((n - k / m - 1) * m + m - (k % m + 1)) / m : by rewrite [H3 at {1}, right_distrib, nat.one_mul] ... = ((n - k / m - 1) * m + (m - (k % m + 1))) / m : {nat.add_sub_assoc H5 _} ... = (m - (k % m + 1)) / m + (n - k / m - 1) : by rewrite [add.comm, (add_mul_div_self H4)] ... = n - k / m - 1 : by rewrite [div_eq_zero_of_lt H6, zero_add] end private lemma div_div_aux (a b c : nat) : b > 0 → c > 0 → (a / b) / c = a / (b * c) := suppose b > 0, suppose c > 0, nat.strong_induction_on a (λ a ih, let k₁ := a / (b*c) in let k₂ := a %(b*c) in assert bc_pos : b*c > 0, from mul_pos `b > 0` `c > 0`, assert k₂ < b * c, from mod_lt _ bc_pos, assert k₂ ≤ a, from !mod_le, or.elim (eq_or_lt_of_le this) (suppose k₂ = a, assert i₁ : a < b * c, by rewrite -this; assumption, assert k₁ = 0, from div_eq_zero_of_lt i₁, assert a / b < c, by rewrite [mul.comm at i₁]; exact nat.div_lt_of_lt_mul i₁, begin rewrite [`k₁ = 0`], show (a / b) / c = 0, from div_eq_zero_of_lt `a / b < c` end) (suppose k₂ < a, assert a = k₁*(b*c) + k₂, from eq_div_mul_add_mod a (b*c), assert a / b = k₁*c + k₂ / b, by rewrite [this at {1}, mul.comm b c at {2}, -mul.assoc, add.comm, add_mul_div_self `b > 0`, add.comm], assert e₁ : (a / b) / c = k₁ + (k₂ / b) / c, by rewrite [this, add.comm, add_mul_div_self `c > 0`, add.comm], assert e₂ : (k₂ / b) / c = k₂ / (b * c), from ih k₂ `k₂ < a`, assert e₃ : k₂ / (b * c) = 0, from div_eq_zero_of_lt `k₂ < b * c`, assert (k₂ / b) / c = 0, by rewrite [e₂, e₃], show (a / b) / c = k₁, by rewrite [e₁, this])) protected lemma div_div_eq_div_mul (a b c : nat) : (a / b) / c = a / (b * c) := begin cases b with b, rewrite [zero_mul, *nat.div_zero, nat.zero_div], cases c with c, rewrite [mul_zero, *nat.div_zero], apply div_div_aux a (succ b) (succ c) dec_trivial dec_trivial end lemma div_lt_of_ne_zero : ∀ {n : nat}, n ≠ 0 → n / 2 < n | 0 h := absurd rfl h | (succ n) h := begin apply nat.div_lt_of_lt_mul, rewrite [-add_one, right_distrib], change n + 1 < (n * 1 + n) + (1 + 1), rewrite [mul_one, -add.assoc], apply add_lt_add_right, show n < n + n + 1, begin rewrite [add.assoc, -add_zero n at {1}], apply add_lt_add_left, apply zero_lt_succ end end end nat
7273fa2457828d19e6a311d98ed1622bb7e3399a
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/category_theory/sites/cover_preserving.lean
27705c8d2d5bbc32025a5bc6bd47e786cb67f113
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
12,016
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.limits import category_theory.functor.flat import category_theory.limits.preserves.filtered import category_theory.sites.left_exact /-! # Cover-preserving functors between sites. We define cover-preserving functors between sites as functors that push covering sieves to covering sieves. A cover-preserving and compatible-preserving functor `G : C ⥤ D` then pulls sheaves on `D` back to sheaves on `C` via `G.op ⋙ -`. ## Main definitions * `category_theory.cover_preserving`: a functor between sites is cover-preserving if it pushes covering sieves to covering sieves * `category_theory.compatible_preserving`: a functor between sites is compatible-preserving if it pushes compatible families of elements to compatible families. * `category_theory.pullback_sheaf`: the pullback of a sheaf along a cover-preserving and compatible-preserving functor. * `category_theory.sites.pullback`: the induced functor `Sheaf K A ⥤ Sheaf J A` for a cover-preserving and compatible-preserving functor `G : (C, J) ⥤ (D, K)`. * `category_theory.sites.pushforward`: the induced functor `Sheaf J A ⥤ Sheaf K A` for a cover-preserving and compatible-preserving functor `G : (C, J) ⥤ (D, K)`. * `category_theory.sites.pushforward`: the induced functor `Sheaf J A ⥤ Sheaf K A` for a cover-preserving and compatible-preserving functor `G : (C, J) ⥤ (D, K)`. ## Main results - `category_theory.sites.whiskering_left_is_sheaf_of_cover_preserving`: If `G : C ⥤ D` is cover-preserving and compatible-preserving, then `G ⋙ -` (`uᵖ`) as a functor `(Dᵒᵖ ⥤ A) ⥤ (Cᵒᵖ ⥤ A)` of presheaves maps sheaves to sheaves. ## References * [Elephant]: *Sketches of an Elephant*, P. T. Johnstone: C2.3. * https://stacks.math.columbia.edu/tag/00WW -/ universes w v₁ v₂ v₃ 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 variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] variables {A : Type u₃} [category.{v₃} A] variables (J : grothendieck_topology C) (K : grothendieck_topology D) variables {L : grothendieck_topology A} /-- A functor `G : (C, J) ⥤ (D, K)` between sites is *cover-preserving* if for all covering sieves `R` in `C`, `R.pushforward_functor G` is a covering sieve in `D`. -/ @[nolint has_nonempty_instance] structure cover_preserving (G : C ⥤ D) : Prop := (cover_preserve : ∀ {U : C} {S : sieve U} (hS : S ∈ J U), S.functor_pushforward G ∈ K (G.obj U)) /-- The identity functor on a site is cover-preserving. -/ lemma id_cover_preserving : cover_preserving J J (𝟭 _) := ⟨λ U S hS, by simpa using hS⟩ variables (J) (K) /-- The composition of two cover-preserving functors is cover-preserving. -/ lemma cover_preserving.comp {F} (hF : cover_preserving J K F) {G} (hG : cover_preserving K L G) : cover_preserving J L (F ⋙ G) := ⟨λ U S hS, begin rw sieve.functor_pushforward_comp, exact hG.cover_preserve (hF.cover_preserve hS) end⟩ /-- A functor `G : (C, J) ⥤ (D, K)` between sites is called compatible preserving if for each compatible family of elements at `C` and valued in `G.op ⋙ ℱ`, and each commuting diagram `f₁ ≫ G.map g₁ = f₂ ≫ G.map g₂`, `x g₁` and `x g₂` coincide when restricted via `fᵢ`. This is actually stronger than merely preserving compatible families because of the definition of `functor_pushforward` used. -/ @[nolint has_nonempty_instance] structure compatible_preserving (K : grothendieck_topology D) (G : C ⥤ D) : Prop := (compatible : ∀ (ℱ : SheafOfTypes.{w} K) {Z} {T : presieve Z} {x : family_of_elements (G.op ⋙ ℱ.val) T} (h : x.compatible) {Y₁ Y₂} {X} (f₁ : X ⟶ G.obj Y₁) (f₂ : X ⟶ G.obj Y₂) {g₁ : Y₁ ⟶ Z} {g₂ : Y₂ ⟶ Z} (hg₁ : T g₁) (hg₂ : T g₂) (eq : f₁ ≫ G.map g₁ = f₂ ≫ G.map g₂), ℱ.val.map f₁.op (x g₁ hg₁) = ℱ.val.map f₂.op (x g₂ hg₂)) variables {J K} {G : C ⥤ D} (hG : compatible_preserving.{w} K G) (ℱ : SheafOfTypes.{w} K) {Z : C} variables {T : presieve Z} {x : family_of_elements (G.op ⋙ ℱ.val) T} (h : x.compatible) include h hG /-- `compatible_preserving` functors indeed preserve compatible families. -/ lemma presieve.family_of_elements.compatible.functor_pushforward : (x.functor_pushforward G).compatible := begin rintros Z₁ Z₂ W g₁ g₂ f₁' f₂' H₁ H₂ eq, unfold family_of_elements.functor_pushforward, rcases get_functor_pushforward_structure H₁ with ⟨X₁, f₁, h₁, hf₁, rfl⟩, rcases get_functor_pushforward_structure H₂ with ⟨X₂, f₂, h₂, hf₂, rfl⟩, suffices : ℱ.val.map (g₁ ≫ h₁).op (x f₁ hf₁) = ℱ.val.map (g₂ ≫ h₂).op (x f₂ hf₂), simpa using this, apply hG.compatible ℱ h _ _ hf₁ hf₂, simpa using eq end @[simp] lemma compatible_preserving.apply_map {Y : C} {f : Y ⟶ Z} (hf : T f) : x.functor_pushforward G (G.map f) (image_mem_functor_pushforward G T hf) = x f hf := begin unfold family_of_elements.functor_pushforward, rcases e₁ : get_functor_pushforward_structure (image_mem_functor_pushforward G T hf) with ⟨X, g, f', hg, eq⟩, simpa using hG.compatible ℱ h f' (𝟙 _) hg hf (by simp[eq]) end omit h hG open limits.walking_cospan lemma compatible_preserving_of_flat {C : Type u₁} [category.{v₁} C] {D : Type u₁} [category.{v₁} D] (K : grothendieck_topology D) (G : C ⥤ D) [representably_flat G] : compatible_preserving K G := begin constructor, intros ℱ Z T x hx Y₁ Y₂ X f₁ f₂ g₁ g₂ hg₁ hg₂ e, /- First, `f₁` and `f₂` form a cone over `cospan g₁ g₂ ⋙ u`. -/ let c : cone (cospan g₁ g₂ ⋙ G) := (cones.postcompose (diagram_iso_cospan (cospan g₁ g₂ ⋙ G)).inv).obj (pullback_cone.mk f₁ f₂ e), /- This can then be viewed as a cospan of structured arrows, and we may obtain an arbitrary cone over it since `structured_arrow W u` is cofiltered. Then, it suffices to prove that it is compatible when restricted onto `u(c'.X.right)`. -/ let c' := is_cofiltered.cone (structured_arrow_cone.to_diagram c ⋙ structured_arrow.pre _ _ _), have eq₁ : f₁ = (c'.X.hom ≫ G.map (c'.π.app left).right) ≫ eq_to_hom (by simp), { erw ← (c'.π.app left).w, dsimp, simp }, have eq₂ : f₂ = (c'.X.hom ≫ G.map (c'.π.app right).right) ≫ eq_to_hom (by simp), { erw ← (c'.π.app right).w, dsimp, simp }, conv_lhs { rw eq₁ }, conv_rhs { rw eq₂ }, simp only [op_comp, functor.map_comp, types_comp_apply, eq_to_hom_op, eq_to_hom_map], congr' 1, /- Since everything now falls in the image of `u`, the result follows from the compatibility of `x` in the image of `u`. -/ injection c'.π.naturality walking_cospan.hom.inl with _ e₁, injection c'.π.naturality walking_cospan.hom.inr with _ e₂, exact hx (c'.π.app left).right (c'.π.app right).right hg₁ hg₂ (e₁.symm.trans e₂) end lemma compatible_preserving_of_downwards_closed (F : C ⥤ D) [full F] [faithful F] (hF : Π {c : C} {d : D} (f : d ⟶ F.obj c), Σ c', F.obj c' ≅ d) : compatible_preserving K F := begin constructor, introv hx he, obtain ⟨X', e⟩ := hF f₁, apply (ℱ.1.map_iso e.op).to_equiv.injective, simp only [iso.op_hom, iso.to_equiv_fun, ℱ.1.map_iso_hom, ← functor_to_types.map_comp_apply], simpa using hx (F.preimage $ e.hom ≫ f₁) (F.preimage $ e.hom ≫ f₂) hg₁ hg₂ (F.map_injective $ by simpa using he), end /-- If `G` is cover-preserving and compatible-preserving, then `G.op ⋙ _` pulls sheaves back to sheaves. This result is basically <https://stacks.math.columbia.edu/tag/00WW>. -/ theorem pullback_is_sheaf_of_cover_preserving {G : C ⥤ D} (hG₁ : compatible_preserving.{v₃} K G) (hG₂ : cover_preserving J K G) (ℱ : Sheaf K A) : presheaf.is_sheaf J (G.op ⋙ ℱ.val) := begin intros X U S hS x hx, change family_of_elements (G.op ⋙ ℱ.val ⋙ coyoneda.obj (op X)) _ at x, let H := ℱ.2 X _ (hG₂.cover_preserve hS), let hx' := hx.functor_pushforward hG₁ (sheaf_over ℱ X), split, swap, { apply H.amalgamate (x.functor_pushforward G), exact hx' }, split, { intros V f hf, convert H.is_amalgamation hx' (G.map f) (image_mem_functor_pushforward G S hf), rw hG₁.apply_map (sheaf_over ℱ X) hx }, { intros y hy, refine H.is_separated_for _ y _ _ (H.is_amalgamation (hx.functor_pushforward hG₁ (sheaf_over ℱ X))), rintros V f ⟨Z, f', g', h, rfl⟩, erw family_of_elements.comp_of_compatible (S.functor_pushforward G) hx' (image_mem_functor_pushforward G S h) g', dsimp, simp [hG₁.apply_map (sheaf_over ℱ X) hx h, ←hy f' h] } end /-- The pullback of a sheaf along a cover-preserving and compatible-preserving functor. -/ def pullback_sheaf {G : C ⥤ D} (hG₁ : compatible_preserving K G) (hG₂ : cover_preserving J K G) (ℱ : Sheaf K A) : Sheaf J A := ⟨G.op ⋙ ℱ.val, pullback_is_sheaf_of_cover_preserving hG₁ hG₂ ℱ⟩ variable (A) /-- The induced functor from `Sheaf K A ⥤ Sheaf J A` given by `G.op ⋙ _` if `G` is cover-preserving and compatible-preserving. -/ @[simps] def sites.pullback {G : C ⥤ D} (hG₁ : compatible_preserving K G) (hG₂ : cover_preserving J K G) : Sheaf K A ⥤ Sheaf J A := { obj := λ ℱ, pullback_sheaf hG₁ hG₂ ℱ, map := λ _ _ f, ⟨(((whiskering_left _ _ _).obj G.op)).map f.val⟩, map_id' := λ ℱ, by { ext1, apply (((whiskering_left _ _ _).obj G.op)).map_id }, map_comp' := λ _ _ _ f g, by { ext1, apply (((whiskering_left _ _ _).obj G.op)).map_comp } } end category_theory namespace category_theory variables {C : Type v₁} [small_category C] {D : Type v₁} [small_category D] variables (A : Type u₂) [category.{v₁} A] variables (J : grothendieck_topology C) (K : grothendieck_topology D) instance [has_limits A] : creates_limits (Sheaf_to_presheaf J A) := category_theory.Sheaf.category_theory.Sheaf_to_presheaf.category_theory.creates_limits.{u₂ v₁ v₁} -- The assumptions so that we have sheafification variables [concrete_category.{v₁} A] [preserves_limits (forget A)] [has_colimits A] [has_limits A] variables [preserves_filtered_colimits (forget A)] [reflects_isomorphisms (forget A)] local attribute [instance] reflects_limits_of_reflects_isomorphisms instance {X : C} : is_cofiltered (J.cover X) := infer_instance /-- The pushforward functor `Sheaf J A ⥤ Sheaf K A` associated to a functor `G : C ⥤ D` in the same direction as `G`. -/ @[simps] def sites.pushforward (G : C ⥤ D) : Sheaf J A ⥤ Sheaf K A := Sheaf_to_presheaf J A ⋙ Lan G.op ⋙ presheaf_to_Sheaf K A instance (G : C ⥤ D) [representably_flat G] : preserves_finite_limits (sites.pushforward A J K G) := begin apply_with comp_preserves_finite_limits { instances := ff }, { apply_instance }, apply_with comp_preserves_finite_limits { instances := ff }, { apply category_theory.Lan_preserves_finite_limits_of_flat }, { apply category_theory.presheaf_to_Sheaf.limits.preserves_finite_limits.{u₂ v₁ v₁}, apply_instance } end /-- The pushforward functor is left adjoint to the pullback functor. -/ def sites.pullback_pushforward_adjunction {G : C ⥤ D} (hG₁ : compatible_preserving K G) (hG₂ : cover_preserving J K G) : sites.pushforward A J K G ⊣ sites.pullback A hG₁ hG₂ := ((Lan.adjunction A G.op).comp (sheafification_adjunction K A)).restrict_fully_faithful (Sheaf_to_presheaf J A) (𝟭 _) (nat_iso.of_components (λ _, iso.refl _) (λ _ _ _,(category.comp_id _).trans (category.id_comp _).symm)) (nat_iso.of_components (λ _, iso.refl _) (λ _ _ _,(category.comp_id _).trans (category.id_comp _).symm)) end category_theory
967dc6c1b3c29beb2d884e767d567bbf0cef4ec7
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/nat/set.lean
3d64d838105254ce0c8703d137ffb8c08d4fcd75
[ "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
1,163
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import data.set.basic /-! ### Recursion on the natural numbers and `set.range` -/ namespace nat section set open set theorem zero_union_range_succ : {0} ∪ range succ = univ := by { ext n, cases n; simp } @[simp] protected lemma range_succ : range succ = {i | 0 < i} := by ext (_ | i); simp [succ_pos, succ_ne_zero] variables {α : Type*} theorem range_of_succ (f : ℕ → α) : {f 0} ∪ range (f ∘ succ) = range f := by rw [← image_singleton, range_comp, ← image_union, zero_union_range_succ, image_univ] theorem range_rec {α : Type*} (x : α) (f : ℕ → α → α) : (set.range (λ n, nat.rec x f n) : set α) = {x} ∪ set.range (λ n, nat.rec (f 0 x) (f ∘ succ) n) := begin convert (range_of_succ _).symm, ext n, induction n with n ihn, { refl }, { dsimp at ihn ⊢, rw ihn } end theorem range_cases_on {α : Type*} (x : α) (f : ℕ → α) : (set.range (λ n, nat.cases_on n x f) : set α) = {x} ∪ set.range f := (range_of_succ _).symm end set end nat
1e1de1dc56ae15c395aa116b6b7835a9c8269fd7
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/category_theory/triangulated/basic.lean
4f81246058dbeb5aeae79459195f2d1799193fcf
[ "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
4,087
lean
/- Copyright (c) 2021 Luke Kershaw. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Luke Kershaw -/ import category_theory.additive.basic import category_theory.shift import category_theory.preadditive.additive_functor /-! # Triangles This file contains the definition of triangles in an additive category with an additive shift. It also defines morphisms between these triangles. TODO: generalise this to n-angles in n-angulated categories as in https://arxiv.org/abs/1006.4592 -/ noncomputable theory open category_theory open category_theory.preadditive open category_theory.limits universes v v₀ v₁ v₂ u u₀ u₁ u₂ namespace category_theory.triangulated open category_theory.category /- We work in a category `C` equipped with a shift. -/ variables (C : Type u) [category.{v} C] [has_shift C] /-- A triangle in `C` is a sextuple `(X,Y,Z,f,g,h)` where `X,Y,Z` are objects of `C`, and `f : X ⟶ Y`, `g : Y ⟶ Z`, `h : Z ⟶ X⟦1⟧` are morphisms in `C`. See https://stacks.math.columbia.edu/tag/0144. -/ structure triangle := mk' :: (obj₁ : C) (obj₂ : C) (obj₃ : C) (mor₁ : obj₁ ⟶ obj₂) (mor₂ : obj₂ ⟶ obj₃) (mor₃ : obj₃ ⟶ obj₁⟦1⟧) /-- A triangle `(X,Y,Z,f,g,h)` in `C` is defined by the morphisms `f : X ⟶ Y`, `g : Y ⟶ Z` and `h : Z ⟶ X⟦1⟧`. -/ @[simps] def triangle.mk {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) (h : Z ⟶ X⟦1⟧) : triangle C := { obj₁ := X, obj₂ := Y, obj₃ := Z, mor₁ := f, mor₂ := g, mor₃ := h } section variables [has_zero_object C] [has_zero_morphisms C] open_locale zero_object instance : inhabited (triangle C) := ⟨⟨0,0,0,0,0,0⟩⟩ /-- For each object in `C`, there is a triangle of the form `(X,X,0,𝟙 X,0,0)` -/ @[simps] def contractible_triangle (X : C) : triangle C := triangle.mk C (𝟙 X) (0 : X ⟶ 0) 0 end variable {C} /-- A morphism of triangles `(X,Y,Z,f,g,h) ⟶ (X',Y',Z',f',g',h')` in `C` is a triple of morphisms `a : X ⟶ X'`, `b : Y ⟶ Y'`, `c : Z ⟶ Z'` such that `a ≫ f' = f ≫ b`, `b ≫ g' = g ≫ c`, and `a⟦1⟧' ≫ h = h' ≫ c`. In other words, we have a commutative diagram: ``` f g h X ───> Y ───> Z ───> X⟦1⟧ │ │ │ │ │a │b │c │a⟦1⟧' V V V V X' ───> Y' ───> Z' ───> X'⟦1⟧ f' g' h' ``` See https://stacks.math.columbia.edu/tag/0144. -/ @[ext] structure triangle_morphism (T₁ : triangle C) (T₂ : triangle C) := (hom₁ : T₁.obj₁ ⟶ T₂.obj₁) (hom₂ : T₁.obj₂ ⟶ T₂.obj₂) (hom₃ : T₁.obj₃ ⟶ T₂.obj₃) (comm₁' : T₁.mor₁ ≫ hom₂ = hom₁ ≫ T₂.mor₁ . obviously) (comm₂' : T₁.mor₂ ≫ hom₃ = hom₂ ≫ T₂.mor₂ . obviously) (comm₃' : T₁.mor₃ ≫ hom₁⟦1⟧' = hom₃ ≫ T₂.mor₃ . obviously) restate_axiom triangle_morphism.comm₁' restate_axiom triangle_morphism.comm₂' restate_axiom triangle_morphism.comm₃' attribute [simp, reassoc] triangle_morphism.comm₁ triangle_morphism.comm₂ triangle_morphism.comm₃ /-- The identity triangle morphism. -/ @[simps] def triangle_morphism_id (T : triangle C) : triangle_morphism T T := { hom₁ := 𝟙 T.obj₁, hom₂ := 𝟙 T.obj₂, hom₃ := 𝟙 T.obj₃ } instance (T : triangle C) : inhabited (triangle_morphism T T) := ⟨triangle_morphism_id T⟩ variables {T₁ T₂ T₃ : triangle C} /-- Composition of triangle morphisms gives a triangle morphism. -/ @[simps] def triangle_morphism.comp (f : triangle_morphism T₁ T₂) (g : triangle_morphism T₂ T₃) : triangle_morphism T₁ T₃ := { hom₁ := f.hom₁ ≫ g.hom₁, hom₂ := f.hom₂ ≫ g.hom₂, hom₃ := f.hom₃ ≫ g.hom₃ } /-- Triangles with triangle morphisms form a category. -/ @[simps] instance triangle_category : category (triangle C) := { hom := λ A B, triangle_morphism A B, id := λ A, triangle_morphism_id A, comp := λ A B C f g, f.comp g } end category_theory.triangulated
4359f590062a0c285f0db9b1f39e5e41c414cd80
50b3917f95cf9fe84639812ea0461b38f8f0dbe1
/canonical_isomorphism/equiv_mul_stuck.lean
26fce88d699ef9e0a7a3745f6cfe0aa0fd40dfc7
[]
no_license
roro47/xena
6389bcd7dcf395656a2c85cfc90a4366e9b825bb
237910190de38d6ff43694ffe3a9b68f79363e6c
refs/heads/master
1,598,570,061,948
1,570,052,567,000
1,570,052,567,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,903
lean
universe zfc_u variables {α β : Type zfc_u} -- ideas around the concept of α being canonically isomorphic to β namespace zfc -- mod of equiv so I can save typing structure equiv' (α : Type zfc_u) (β : Type zfc_u) := (i : α → β) (j : β → α) (ij : ∀ (x : α), j (i x) = x) (ji : ∀ (y : β), i (j y) = y) -- it's equiv to equiv, it is absolutely fundamental for the notion of canonical isomorphism, and I like -- the notation better because it gets everywhere. --#print has_mul --@[class] --structure has_mul : Type u → Type u --fields: --has_mul.mul : Π {α : Type u} [c : has_mul α], α → α → α -- Fundamental theorem of has_mul definition mul_is_add {α : Type zfc_u} : equiv' (has_mul α) (has_add α) := { i := λ ⟨mul⟩,⟨mul⟩, j := λ ⟨mul⟩,⟨mul⟩, ij := λ ⟨x⟩,rfl, ji := λ ⟨x⟩, rfl, } definition equiv_mul {α β : Type zfc_u} : equiv' α β → equiv' (has_mul α) (has_mul β) := λ E, { i := λ αmul,⟨λ b1 b2, E.i (@has_mul.mul α αmul (E.j b1) (E.j b2))⟩, j := λ βmul,⟨λ a1 a2, E.j (@has_mul.mul β βmul (E.i a1) (E.i a2))⟩, -- didn't I just write that? -- should we introduce E-dual? ij := λ f, begin cases f, -- aargh why do I struggle suffices : (λ (a1 a2 : α), E.j (E.i (f (E.j (E.i a1)) (E.j (E.i a2))))) = (λ a1 a2, f a1 a2), by rw this, funext, simp [E.ij,E.ji], -- got there in the end end, ji := -- I can't even do this in term mode so I just copy out the entire tactic mode proof again. λ g, begin cases g, -- aargh why do I struggle suffices : (λ (b1 b2 : β), E.i (E.j (g (E.i (E.j b1)) (E.i (E.j b2))))) = (λ b1 b2, g b1 b2), by rw this, funext, simp [E.ij,E.ji], -- got there in the end end, -- didn't I just write that? } #print has_mul.mul end zfc
15513df9e611543002968fe5be59d45af7f4d904
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/lie/direct_sum.lean
d55706ba5b2120f87824181216058bc41163510a
[]
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
4,439
lean
/- Copyright (c) 2020 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.lie.basic import Mathlib.linear_algebra.direct_sum.finsupp import Mathlib.PostPort universes w w₁ v u namespace Mathlib /-! # Direct sums of Lie algebras and Lie modules Direct sums of Lie algebras and Lie modules carry natural algbebra and module structures. ## Tags lie algebra, lie module, direct sum -/ namespace direct_sum /-! The direct sum of Lie modules over a fixed Lie algebra carries a natural Lie module structure. -/ protected instance lie_ring_module {ι : Type v} {L : Type w₁} {M : ι → Type w} [lie_ring L] [(i : ι) → add_comm_group (M i)] [(i : ι) → lie_ring_module L (M i)] : lie_ring_module L (direct_sum ι fun (i : ι) => M i) := lie_ring_module.mk sorry sorry sorry @[simp] theorem lie_module_bracket_apply {ι : Type v} {L : Type w₁} {M : ι → Type w} [lie_ring L] [(i : ι) → add_comm_group (M i)] [(i : ι) → lie_ring_module L (M i)] (x : L) (m : direct_sum ι fun (i : ι) => M i) (i : ι) : coe_fn (has_bracket.bracket x m) i = has_bracket.bracket x (coe_fn m i) := dfinsupp.map_range_apply (fun (i : ι) (m' : M i) => has_bracket.bracket x m') (lie_ring_module._proof_1 x) m i protected instance lie_module {R : Type u} {ι : Type v} [comm_ring R] {L : Type w₁} {M : ι → Type w} [lie_ring L] [lie_algebra R L] [(i : ι) → add_comm_group (M i)] [(i : ι) → module R (M i)] [(i : ι) → lie_ring_module L (M i)] [(i : ι) → lie_module R L (M i)] : lie_module R L (direct_sum ι fun (i : ι) => M i) := lie_module.mk sorry sorry /-- The inclusion of each component into a direct sum as a morphism of Lie modules. -/ def lie_module_of (R : Type u) (ι : Type v) [comm_ring R] (L : Type w₁) (M : ι → Type w) [lie_ring L] [lie_algebra R L] [(i : ι) → add_comm_group (M i)] [(i : ι) → module R (M i)] [(i : ι) → lie_ring_module L (M i)] [(i : ι) → lie_module R L (M i)] [DecidableEq ι] (j : ι) : lie_module_hom R L (M j) (direct_sum ι fun (i : ι) => M i) := lie_module_hom.mk (linear_map.to_fun (lof R ι M j)) sorry sorry sorry /-- The projection map onto one component, as a morphism of Lie modules. -/ def lie_module_component (R : Type u) (ι : Type v) [comm_ring R] (L : Type w₁) (M : ι → Type w) [lie_ring L] [lie_algebra R L] [(i : ι) → add_comm_group (M i)] [(i : ι) → module R (M i)] [(i : ι) → lie_ring_module L (M i)] [(i : ι) → lie_module R L (M i)] (j : ι) : lie_module_hom R L (direct_sum ι fun (i : ι) => M i) (M j) := lie_module_hom.mk (linear_map.to_fun (component R ι M j)) sorry sorry sorry /-! The direct sum of Lie algebras carries a natural Lie algebra structure. -/ protected instance lie_ring {ι : Type v} {L : ι → Type w} [(i : ι) → lie_ring (L i)] : lie_ring (direct_sum ι fun (i : ι) => L i) := lie_ring.mk sorry sorry sorry sorry @[simp] theorem bracket_apply {ι : Type v} {L : ι → Type w} [(i : ι) → lie_ring (L i)] (x : direct_sum ι fun (i : ι) => L i) (y : direct_sum ι fun (i : ι) => L i) (i : ι) : coe_fn (has_bracket.bracket x y) i = has_bracket.bracket (coe_fn x i) (coe_fn y i) := dfinsupp.zip_with_apply (fun (i : ι) (x y : L i) => has_bracket.bracket x y) lie_ring._proof_7 x y i protected instance lie_algebra {R : Type u} {ι : Type v} [comm_ring R] {L : ι → Type w} [(i : ι) → lie_ring (L i)] [(i : ι) → lie_algebra R (L i)] : lie_algebra R (direct_sum ι fun (i : ι) => L i) := lie_algebra.mk sorry /-- The inclusion of each component into the direct sum as morphism of Lie algebras. -/ def lie_algebra_of (R : Type u) (ι : Type v) [comm_ring R] (L : ι → Type w) [(i : ι) → lie_ring (L i)] [(i : ι) → lie_algebra R (L i)] [DecidableEq ι] (j : ι) : lie_algebra.morphism R (L j) (direct_sum ι fun (i : ι) => L i) := lie_algebra.morphism.mk (linear_map.to_fun (lof R ι L j)) sorry sorry sorry /-- The projection map onto one component, as a morphism of Lie algebras. -/ def lie_algebra_component (R : Type u) (ι : Type v) [comm_ring R] (L : ι → Type w) [(i : ι) → lie_ring (L i)] [(i : ι) → lie_algebra R (L i)] (j : ι) : lie_algebra.morphism R (direct_sum ι fun (i : ι) => L i) (L j) := lie_algebra.morphism.mk (linear_map.to_fun (component R ι L j)) sorry sorry sorry
d6f29a9f0a256d0a1129dfdf5ae4f64c10383edd
947b78d97130d56365ae2ec264df196ce769371a
/stage0/src/Lean/Compiler/ExternAttr.lean
44e9278ce7e1eaeea08373b7cbab24d78763d55e
[ "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
6,756
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.Expr import Lean.Environment import Lean.Attributes import Lean.ProjFns import Lean.Meta.Basic namespace Lean inductive ExternEntry | adhoc (backend : Name) | inline (backend : Name) (pattern : String) | standard (backend : Name) (fn : String) | foreign (backend : Name) (fn : String) /- - `@[extern]` encoding: ```.entries = [adhoc `all]``` - `@[extern "level_hash"]` encoding: ```.entries = [standard `all "levelHash"]``` - `@[extern cpp "lean::string_size" llvm "lean_str_size"]` encoding: ```.entries = [standard `cpp "lean::string_size", standard `llvm "leanStrSize"]``` - `@[extern cpp inline "#1 + #2"]` encoding: ```.entries = [inline `cpp "#1 + #2"]``` - `@[extern cpp "foo" llvm adhoc]` encoding: ```.entries = [standard `cpp "foo", adhoc `llvm]``` - `@[extern 2 cpp "io_prim_println"]` encoding: ```.arity? = 2, .entries = [standard `cpp "ioPrimPrintln"]``` -/ structure ExternAttrData := (arity? : Option Nat := none) (entries : List ExternEntry) instance ExternAttrData.inhabited : Inhabited ExternAttrData := ⟨{ entries := [] }⟩ private partial def syntaxToExternEntries (a : Array Syntax) : Nat → List ExternEntry → Except String (List ExternEntry) | i, entries => if i == a.size then Except.ok entries else match a.get! i with | Syntax.ident _ _ backend _ => let i := i + 1; if i == a.size then Except.error "string or identifier expected" else match (a.get! i).isIdOrAtom? with | some "adhoc" => syntaxToExternEntries (i+1) (ExternEntry.adhoc backend :: entries) | some "inline" => let i := i + 1; match (a.get! i).isStrLit? with | some pattern => syntaxToExternEntries (i+1) (ExternEntry.inline backend pattern :: entries) | none => Except.error "string literal expected" | _ => match (a.get! i).isStrLit? with | some fn => syntaxToExternEntries (i+1) (ExternEntry.standard backend fn :: entries) | none => Except.error "string literal expected" | _ => Except.error "identifier expected" private def syntaxToExternAttrData (s : Syntax) : ExceptT String Id ExternAttrData := match s with | Syntax.missing => Except.ok { entries := [ ExternEntry.adhoc `all ] } | Syntax.node _ args => if args.size == 0 then Except.error "unexpected kind of argument" else let (arity, i) : Option Nat × Nat := match (args.get! 0).isNatLit? with | some arity => (some arity, 1) | none => (none, 0); match (args.get! i).isStrLit? with | some str => if args.size == i+1 then Except.ok { arity? := arity, entries := [ ExternEntry.standard `all str ] } else Except.error "invalid extern attribute" | none => match syntaxToExternEntries args i [] with | Except.ok entries => Except.ok { arity? := arity, entries := entries } | Except.error msg => Except.error msg | _ => Except.error "unexpected kind of argument" @[extern "lean_add_extern"] constant addExtern (env : Environment) (n : Name) : ExceptT String Id Environment := arbitrary _ def mkExternAttr : IO (ParametricAttribute ExternAttrData) := registerParametricAttribute `extern "builtin and foreign functions" (fun _ stx => ofExcept $ syntaxToExternAttrData stx) (fun declName _ => do env ← getEnv; if env.isProjectionFn declName || env.isConstructor declName then do env ← ofExcept $ addExtern env declName; setEnv env else pure ()) @[init mkExternAttr] constant externAttr : ParametricAttribute ExternAttrData := arbitrary _ @[export lean_get_extern_attr_data] def getExternAttrData (env : Environment) (n : Name) : Option ExternAttrData := externAttr.getParam env n private def parseOptNum : Nat → String.Iterator → Nat → String.Iterator × Nat | 0, it, r => (it, r) | n+1, it, r => if !it.hasNext then (it, r) else let c := it.curr; if '0' <= c && c <= '9' then parseOptNum n it.next (r*10 + (c.toNat - '0'.toNat)) else (it, r) def expandExternPatternAux (args : List String) : Nat → String.Iterator → String → String | 0, it, r => r | i+1, it, r => if ¬ it.hasNext then r else let c := it.curr; if c ≠ '#' then expandExternPatternAux i it.next (r.push c) else let it := it.next; let (it, j) := parseOptNum it.remainingBytes it 0; let j := j-1; expandExternPatternAux i it (r ++ args.getD j "") def expandExternPattern (pattern : String) (args : List String) : String := expandExternPatternAux args pattern.length pattern.mkIterator "" def mkSimpleFnCall (fn : String) (args : List String) : String := fn ++ "(" ++ ((args.intersperse ", ").foldl HasAppend.append "") ++ ")" def ExternEntry.backend : ExternEntry → Name | ExternEntry.adhoc n => n | ExternEntry.inline n _ => n | ExternEntry.standard n _ => n | ExternEntry.foreign n _ => n def getExternEntryForAux (backend : Name) : List ExternEntry → Option ExternEntry | [] => none | e::es => if e.backend == `all then some e else if e.backend == backend then some e else getExternEntryForAux es def getExternEntryFor (d : ExternAttrData) (backend : Name) : Option ExternEntry := getExternEntryForAux backend d.entries def isExtern (env : Environment) (fn : Name) : Bool := (getExternAttrData env fn).isSome /- We say a Lean function marked as `[extern "<c_fn_nane>"]` is for all backends, and it is implemented using `extern "C"`. Thus, there is no name mangling. -/ def isExternC (env : Environment) (fn : Name) : Bool := match getExternAttrData env fn with | some { entries := [ ExternEntry.standard `all _ ], .. } => true | _ => false def getExternNameFor (env : Environment) (backend : Name) (fn : Name) : Option String := do data ← getExternAttrData env fn; entry ← getExternEntryFor data backend; match entry with | ExternEntry.standard _ n => pure n | ExternEntry.foreign _ n => pure n | _ => failure def getExternConstArity (declName : Name) : CoreM (Option Nat) := do env ← getEnv; match getExternAttrData env declName with | none => pure none | some data => match data.arity? with | some arity => pure arity | none => do cinfo ← getConstInfo declName; (arity, _) ← (Meta.forallTelescopeReducing cinfo.type fun xs _ => pure xs.size : MetaM Nat).run; pure (some arity) @[export lean_get_extern_const_arity] def getExternConstArityExport (env : Environment) (declName : Name) : IO (Option Nat) := catch (do (arity?, _) ← (getExternConstArity declName).toIO {} { env := env }; pure arity?) (fun _ => pure none) end Lean
130a16fc195d4fc7c5c2f47438a8ac03dc714238
42610cc2e5db9c90269470365e6056df0122eaa0
/library/theories/finite_group_theory/action.lean
4cddf49373681cb997ee8c3c1e648b3917cb27fb
[ "Apache-2.0" ]
permissive
tomsib2001/lean
2ab59bfaebd24a62109f800dcf4a7139ebd73858
eb639a7d53fb40175bea5c8da86b51d14bb91f76
refs/heads/master
1,586,128,387,740
1,468,968,950,000
1,468,968,950,000
61,027,234
0
0
null
1,465,813,585,000
1,465,813,585,000
null
UTF-8
Lean
false
false
25,697
lean
/- Copyright (c) 2015 Haitao Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author : Haitao Zhang -/ import algebra.group data .hom .perm .finsubg namespace group_theory open finset function local attribute perm.f [coercion] private lemma and_left_true {a b : Prop} (Pa : a) : a ∧ b ↔ b := by rewrite [iff_true_intro Pa, true_and] section def variables {G S : Type} [group G] [fintype S] definition is_fixed_point (hom : G → perm S) (H : finset G) (a : S) : Prop := ∀ h, h ∈ H → hom h a = a variables [decidable_eq S] definition orbit (hom : G → perm S) (H : finset G) (a : S) : finset S := image (move_by a) (image hom H) definition fixed_points [reducible] (hom : G → perm S) (H : finset G) : finset S := {a ∈ univ | orbit hom H a = '{a}} variable [decidable_eq G] -- required by {x ∈ H |p x} filtering definition moverset (hom : G → perm S) (H : finset G) (a b : S) : finset G := {f ∈ H | hom f a = b} definition stab (hom : G → perm S) (H : finset G) (a : S) : finset G := {f ∈ H | hom f a = a} end def section orbit_stabilizer variables {G S : Type} [group G] [decidable_eq G] [fintype S] [decidable_eq S] section variables {hom : G → perm S} {H : finset G} {a : S} [Hom : is_hom_class hom] include Hom lemma exists_of_orbit {b : S} : b ∈ orbit hom H a → ∃ h, h ∈ H ∧ hom h a = b := assume Pb, obtain p (Pp₁ : p ∈ image hom H) (Pp₂ : move_by a p = b), from exists_of_mem_image Pb, obtain h (Ph₁ : h ∈ H) (Ph₂ : hom h = p), from exists_of_mem_image Pp₁, have Phab : hom h a = b, from calc hom h a = p a : Ph₂ ... = b : Pp₂, exists.intro h (and.intro Ph₁ Phab) lemma orbit_of_exists {b : S} : (∃ h, h ∈ H ∧ hom h a = b) → b ∈ orbit hom H a := assume Pex, obtain h PinH Phab, from Pex, mem_image (mem_image_of_mem hom PinH) Phab lemma is_fixed_point_of_mem_fixed_points : a ∈ fixed_points hom H → is_fixed_point hom H a := assume Pain, take h, assume Phin, eq_of_mem_singleton (of_mem_sep Pain ▸ orbit_of_exists (exists.intro h (and.intro Phin rfl))) lemma mem_fixed_points_of_exists_of_is_fixed_point : (∃ h, h ∈ H) → is_fixed_point hom H a → a ∈ fixed_points hom H := assume Pex Pfp, mem_sep_of_mem !mem_univ (ext take x, iff.intro (assume Porb, obtain h Phin Pha, from exists_of_orbit Porb, by rewrite [mem_singleton_iff, -Pha, Pfp h Phin]) (obtain h Phin, from Pex, by rewrite mem_singleton_iff; intro Peq; rewrite Peq; apply orbit_of_exists; existsi h; apply and.intro Phin (Pfp h Phin))) lemma is_fixed_point_iff_mem_fixed_points_of_exists : (∃ h, h ∈ H) → (a ∈ fixed_points hom H ↔ is_fixed_point hom H a) := assume Pex, iff.intro is_fixed_point_of_mem_fixed_points (mem_fixed_points_of_exists_of_is_fixed_point Pex) lemma is_fixed_point_iff_mem_fixed_points [finsubgH : is_finsubg H] : a ∈ fixed_points hom H ↔ is_fixed_point hom H a := is_fixed_point_iff_mem_fixed_points_of_exists (exists.intro 1 !finsubg_has_one) lemma is_fixed_point_of_one : is_fixed_point hom ('{1}) a := take h, assume Ph, by rewrite [eq_of_mem_singleton Ph, hom_map_one] lemma fixed_points_of_one : fixed_points hom ('{1}) = univ := ext take s, iff.intro (assume Pl, mem_univ s) (assume Pr, mem_fixed_points_of_exists_of_is_fixed_point (exists.intro 1 !mem_singleton) is_fixed_point_of_one) open fintype lemma card_fixed_points_of_one : card (fixed_points hom ('{1})) = card S := by rewrite [fixed_points_of_one] end -- these are already specified by stab hom H a variables {hom : G → perm S} {H : finset G} {a : S} variable [Hom : is_hom_class hom] include Hom lemma perm_f_mul (f g : G): perm.f ((hom f) * (hom g)) a = ((hom f) ∘ (hom g)) a := rfl lemma stab_lmul {f g : G} : g ∈ stab hom H a → hom (f*g) a = hom f a := assume Pgstab, have hom g a = a, from of_mem_sep Pgstab, calc hom (f*g) a = perm.f ((hom f) * (hom g)) a : is_hom hom ... = ((hom f) ∘ (hom g)) a : by rewrite perm_f_mul ... = (hom f) a : by unfold comp; rewrite this lemma stab_subset : stab hom H a ⊆ H := begin apply subset_of_forall, intro f Pfstab, apply mem_of_mem_sep Pfstab end lemma reverse_move {h g : G} : g ∈ moverset hom H a (hom h a) → hom (h⁻¹*g) a = a := assume Pg, have hom g a = hom h a, from of_mem_sep Pg, calc hom (h⁻¹*g) a = perm.f ((hom h⁻¹) * (hom g)) a : by rewrite (is_hom hom) ... = ((hom h⁻¹) ∘ hom g) a : by rewrite perm_f_mul ... = perm.f ((hom h)⁻¹ * hom h) a : by unfold comp; rewrite [this, perm_f_mul, hom_map_inv hom h] ... = perm.f (1 : perm S) a : by rewrite (mul.left_inv (hom h)) ... = a : by esimp lemma moverset_inj_on_orbit : set.inj_on (moverset hom H a) (ts (orbit hom H a)) := take b1 b2, assume Pb1, obtain h1 Ph1₁ Ph1₂, from exists_of_orbit Pb1, have Ph1b1 : h1 ∈ moverset hom H a b1, from mem_sep_of_mem Ph1₁ Ph1₂, assume Psetb2 Pmeq, begin subst b1, rewrite Pmeq at Ph1b1, apply of_mem_sep Ph1b1 end variable [finsubgH : is_finsubg H] include finsubgH lemma subg_stab_of_move {h g : G} : h ∈ H → g ∈ moverset hom H a (hom h a) → h⁻¹*g ∈ stab hom H a := assume Ph Pg, have Phinvg : h⁻¹*g ∈ H, from begin apply finsubg_mul_closed H, apply finsubg_has_inv H, assumption, apply mem_of_mem_sep Pg end, mem_sep_of_mem Phinvg (reverse_move Pg) lemma subg_stab_closed : finset_mul_closed_on (stab hom H a) := take f g, assume Pfstab, have Pf : hom f a = a, from of_mem_sep Pfstab, assume Pgstab, have Pfg : hom (f*g) a = a, from calc hom (f*g) a = (hom f) a : stab_lmul Pgstab ... = a : Pf, have PfginH : (f*g) ∈ H, from finsubg_mul_closed H (mem_of_mem_sep Pfstab) (mem_of_mem_sep Pgstab), mem_sep_of_mem PfginH Pfg lemma subg_stab_has_one : 1 ∈ stab hom H a := have P : hom 1 a = a, from calc hom 1 a = perm.f (1 : perm S) a : {hom_map_one hom} ... = a : rfl, have PoneinH : 1 ∈ H, from finsubg_has_one H, mem_sep_of_mem PoneinH P lemma subg_stab_has_inv : finset_has_inv (stab hom H a) := take f, assume Pfstab, have Pf : hom f a = a, from of_mem_sep Pfstab, have Pfinv : hom f⁻¹ a = a, from calc hom f⁻¹ a = hom f⁻¹ ((hom f) a) : by rewrite Pf ... = perm.f ((hom f⁻¹) * (hom f)) a : by rewrite perm_f_mul ... = hom (f⁻¹ * f) a : by rewrite (is_hom hom) ... = hom 1 a : by rewrite mul.left_inv ... = perm.f (1 : perm S) a : by rewrite (hom_map_one hom), have PfinvinH : f⁻¹ ∈ H, from finsubg_has_inv H (mem_of_mem_sep Pfstab), mem_sep_of_mem PfinvinH Pfinv definition subg_stab_is_finsubg [instance] : is_finsubg (stab hom H a) := is_finsubg.mk subg_stab_has_one subg_stab_closed subg_stab_has_inv lemma subg_lcoset_eq_moverset {h : G} : h ∈ H → fin_lcoset (stab hom H a) h = moverset hom H a (hom h a) := assume Ph, ext (take g, iff.intro (assume Pl, obtain f (Pf₁ : f ∈ stab hom H a) (Pf₂ : h*f = g), from exists_of_mem_image Pl, have Pfstab : hom f a = a, from of_mem_sep Pf₁, have PginH : g ∈ H, begin subst Pf₂, apply finsubg_mul_closed H, assumption, apply mem_of_mem_sep Pf₁ end, have Pga : hom g a = hom h a, from calc hom g a = hom (h*f) a : by subst g ... = hom h a : stab_lmul Pf₁, mem_sep_of_mem PginH Pga) (assume Pr, begin rewrite [↑fin_lcoset, mem_image_iff], existsi h⁻¹*g, split, exact subg_stab_of_move Ph Pr, apply mul_inv_cancel_left end)) lemma subg_moverset_of_orbit_is_lcoset_of_stab (b : S) : b ∈ orbit hom H a → ∃ h, h ∈ H ∧ fin_lcoset (stab hom H a) h = moverset hom H a b := assume Porb, obtain p (Pp₁ : p ∈ image hom H) (Pp₂ : move_by a p = b), from exists_of_mem_image Porb, obtain h (Ph₁ : h ∈ H) (Ph₂ : hom h = p), from exists_of_mem_image Pp₁, have Phab : hom h a = b, from by subst p; assumption, exists.intro h (and.intro Ph₁ (Phab ▸ subg_lcoset_eq_moverset Ph₁)) lemma subg_lcoset_of_stab_is_moverset_of_orbit (h : G) : h ∈ H → ∃ b, b ∈ orbit hom H a ∧ moverset hom H a b = fin_lcoset (stab hom H a) h := assume Ph, have Pha : (hom h a) ∈ orbit hom H a, by apply mem_image_of_mem; apply mem_image_of_mem; exact Ph, exists.intro (hom h a) (and.intro Pha (eq.symm (subg_lcoset_eq_moverset Ph))) lemma subg_moversets_of_orbit_eq_stab_lcosets : image (moverset hom H a) (orbit hom H a) = fin_lcosets (stab hom H a) H := ext (take s, iff.intro (assume Pl, obtain b Pb₁ Pb₂, from exists_of_mem_image Pl, obtain h Ph, from subg_moverset_of_orbit_is_lcoset_of_stab b Pb₁, begin rewrite [↑fin_lcosets, mem_image_eq], existsi h, subst Pb₂, assumption end) (assume Pr, obtain h Ph₁ Ph₂, from exists_of_mem_image Pr, obtain b Pb, from @subg_lcoset_of_stab_is_moverset_of_orbit G S _ _ _ _ hom H a Hom _ h Ph₁, begin rewrite [mem_image_eq], existsi b, subst Ph₂, assumption end)) open nat theorem orbit_stabilizer_theorem : card H = card (orbit hom H a) * card (stab hom H a) := calc card H = card (fin_lcosets (stab hom H a) H) * card (stab hom H a) : lagrange_theorem stab_subset ... = card (image (moverset hom H a) (orbit hom H a)) * card (stab hom H a) : subg_moversets_of_orbit_eq_stab_lcosets ... = card (orbit hom H a) * card (stab hom H a) : card_image_eq_of_inj_on moverset_inj_on_orbit end orbit_stabilizer section orbit_partition variables {G S : Type} [group G] [decidable_eq G] [fintype S] [decidable_eq S] variables {hom : G → perm S} [Hom : is_hom_class hom] {H : finset G} [subgH : is_finsubg H] include Hom subgH lemma in_orbit_refl {a : S} : a ∈ orbit hom H a := mem_image (mem_image (finsubg_has_one H) (hom_map_one hom)) rfl lemma in_orbit_trans {a b c : S} : a ∈ orbit hom H b → b ∈ orbit hom H c → a ∈ orbit hom H c := assume Painb Pbinc, obtain h PhinH Phba, from exists_of_orbit Painb, obtain g PginH Pgcb, from exists_of_orbit Pbinc, orbit_of_exists (exists.intro (h*g) (and.intro (finsubg_mul_closed H PhinH PginH) (calc hom (h*g) c = perm.f ((hom h) * (hom g)) c : is_hom hom ... = ((hom h) ∘ (hom g)) c : by rewrite perm_f_mul ... = (hom h) b : Pgcb ... = a : Phba))) lemma in_orbit_symm {a b : S} : a ∈ orbit hom H b → b ∈ orbit hom H a := assume Painb, obtain h PhinH Phba, from exists_of_orbit Painb, have perm.f (hom h)⁻¹ a = b, by rewrite [-Phba, -perm_f_mul, mul.left_inv], have (hom h⁻¹) a = b, by rewrite [hom_map_inv, this], orbit_of_exists (exists.intro h⁻¹ (and.intro (finsubg_has_inv H PhinH) this)) lemma orbit_is_partition : is_partition (orbit hom H) := take a b, propext (iff.intro (assume Painb, obtain h PhinH Phba, from exists_of_orbit Painb, ext take c, iff.intro (assume Pcina, in_orbit_trans Pcina Painb) (assume Pcinb, obtain g PginH Pgbc, from exists_of_orbit Pcinb, in_orbit_trans Pcinb (in_orbit_symm Painb))) (assume Peq, Peq ▸ in_orbit_refl)) variables (hom) (H) open nat finset.partition fintype definition orbit_partition : @partition S _ := mk univ (orbit hom H) orbit_is_partition (restriction_imp_union (orbit hom H) orbit_is_partition (λ a Pa, !subset_univ)) definition orbits : finset (finset S) := equiv_classes (orbit_partition hom H) definition fixed_point_orbits : finset (finset S) := {cls ∈ orbits hom H | card cls = 1} variables {hom} {H} lemma exists_iff_mem_orbits (orb : finset S) : orb ∈ orbits hom H ↔ ∃ a : S, orbit hom H a = orb := begin esimp [orbits, equiv_classes, orbit_partition], rewrite [mem_image_iff], apply iff.intro, intro Pl, cases Pl with a Pa, rewrite (and_left_true !mem_univ) at Pa, existsi a, exact Pa, intro Pr, cases Pr with a Pa, rewrite -true_and at Pa, rewrite -(iff_true_intro (mem_univ a)) at Pa, existsi a, exact Pa end lemma exists_of_mem_orbits {orb : finset S} : orb ∈ orbits hom H → ∃ a : S, orbit hom H a = orb := iff.elim_left (exists_iff_mem_orbits orb) lemma fixed_point_orbits_eq : fixed_point_orbits hom H = image (orbit hom H) (fixed_points hom H) := ext take s, iff.intro (assume Pin, obtain Psin Ps, from iff.elim_left !mem_sep_iff Pin, obtain a Pa, from exists_of_mem_orbits Psin, mem_image (mem_sep_of_mem !mem_univ (eq.symm (eq_of_card_eq_of_subset (by rewrite [Pa, Ps]) (subset_of_forall take x, assume Pxin, eq_of_mem_singleton Pxin ▸ in_orbit_refl)))) Pa) (assume Pin, obtain a Pain Porba, from exists_of_mem_image Pin, mem_sep_of_mem (begin esimp [orbits, equiv_classes, orbit_partition], rewrite [mem_image_iff], existsi a, exact and.intro !mem_univ Porba end) (begin substvars, rewrite [of_mem_sep Pain] end)) lemma orbit_inj_on_fixed_points : set.inj_on (orbit hom H) (ts (fixed_points hom H)) := take a₁ a₂, begin rewrite [-*mem_eq_mem_to_set, ↑fixed_points, *mem_sep_iff], intro Pa₁ Pa₂, rewrite [and.right Pa₁, and.right Pa₂], exact eq_of_singleton_eq end lemma card_fixed_point_orbits_eq : card (fixed_point_orbits hom H) = card (fixed_points hom H) := by rewrite fixed_point_orbits_eq; apply card_image_eq_of_inj_on orbit_inj_on_fixed_points lemma orbit_class_equation : card S = Sum (orbits hom H) card := class_equation (orbit_partition hom H) lemma card_fixed_point_orbits : Sum (fixed_point_orbits hom H) card = card (fixed_point_orbits hom H) := calc Sum _ _ = Sum (fixed_point_orbits hom H) (λ x, 1) : Sum_ext (take c Pin, of_mem_sep Pin) ... = card (fixed_point_orbits hom H) * 1 : Sum_const_eq_card_mul ... = card (fixed_point_orbits hom H) : mul_one (card (fixed_point_orbits hom H)) local attribute nat.comm_semiring [instance] lemma orbit_class_equation' : card S = card (fixed_points hom H) + Sum {cls ∈ orbits hom H | card cls ≠ 1} card := calc card S = Sum (orbits hom H) finset.card : orbit_class_equation ... = Sum (fixed_point_orbits hom H) finset.card + Sum {cls ∈ orbits hom H | card cls ≠ 1} card : Sum_binary_union ... = card (fixed_point_orbits hom H) + Sum {cls ∈ orbits hom H | card cls ≠ 1} card : by rewrite -card_fixed_point_orbits ... = card (fixed_points hom H) + Sum {cls ∈ orbits hom H | card cls ≠ 1} card : by rewrite card_fixed_point_orbits_eq end orbit_partition section cayley variables {G : Type} [group G] [fintype G] definition action_by_lmul : G → perm G := take g, perm.mk (lmul_by g) (lmul_inj g) variable [decidable_eq G] lemma action_by_lmul_hom : homomorphic (@action_by_lmul G _ _) := take g₁ (g₂ : G), eq.symm (calc action_by_lmul g₁ * action_by_lmul g₂ = perm.mk ((lmul_by g₁)∘(lmul_by g₂)) _ : rfl ... = perm.mk (lmul_by (g₁*g₂)) _ : by congruence; apply coset.lmul_compose) lemma action_by_lmul_inj : injective (@action_by_lmul G _ _) := take g₁ g₂, assume Peq, perm.no_confusion Peq (λ Pfeq Pqeq, have Pappeq : g₁*1 = g₂*1, from congr_fun Pfeq _, calc g₁ = g₁ * 1 : mul_one ... = g₂ * 1 : Pappeq ... = g₂ : mul_one) definition action_by_lmul_is_iso [instance] : is_iso_class (@action_by_lmul G _ _) := is_iso_class.mk action_by_lmul_hom action_by_lmul_inj end cayley section conjugation_action section missing lemma injective_image {A B : Type} [HdeceqB : decidable_eq B] (f : A → B) (Hinjf : injective f) : injective (image f : finset A → finset B) := assume s1 s2 Heqfs1s2, eq_of_subset_of_subset (subset_of_forall (take x Hxs1, have Hfx : f x ∈ f ' s2, from eq.subst Heqfs1s2 (mem_image_of_mem f Hxs1), obtain x2 (Hx2 : x2 ∈ s2 ∧ f x2 = f x), from eq.subst (mem_image_eq f) Hfx, have Heq : x2 = x, from Hinjf (and.right Hx2), eq.subst Heq (and.left Hx2) )) (subset_of_forall ( take x Hxs2, have Hfx : f x ∈ f ' s1, from eq.substr Heqfs1s2 (mem_image_of_mem f Hxs2), obtain x1 (Hx1 : x1 ∈ s1 ∧ f x1 = f x), from eq.subst (mem_image_eq f) Hfx, have Heq : x1 = x, from Hinjf (and.right Hx1), eq.subst Heq (and.left Hx1) )) end missing variables {G : Type} [Hgr : group G] [ Hft : fintype G] include Hgr Hft definition ftfg [instance] : fintype (finset G) := sorry --shouldn't this be inferred by the typeclass mechanism? definition action_by_conj : G → perm G := take g, perm.mk (conj_by g) (conj_inj g) variable [Hdec : decidable_eq G] include Hdec lemma conj_by_compose (g1 g2 : G) : conj_by (g1 * g2) = (conj_by g1)∘(conj_by g2) := funext (take x, begin rewrite conj_compose end) lemma action_by_conj_hom : homomorphic (@action_by_conj G _ _) := take (g1 g2 : G), eq.symm (calc action_by_conj g1 * action_by_conj g2 = perm.mk ((conj_by g1)∘(conj_by g2)) _ : rfl ... = perm.mk ((conj_by (g1 * g2))) (conj_inj _) : begin congruence, rewrite conj_by_compose end ... = action_by_conj (g1 * g2) : rfl) -- lemma action_by_conj_inj : injective (@action_by_conj G _ _) := sorry -- lemma action_by_conj_is_iso [instance] : is_iso_class (@action_by_conj G _ _) := -- is_iso_class.mk action_by_conj_hom action_by_conj_inj lemma conj_by_im_inj (g : G) : injective (image (conj_by g)) := begin apply injective_image, exact (conj_inj g) end definition action_by_conj_on_finsets : G → perm (finset G) := take g, perm.mk (image (conj_by g)) (conj_by_im_inj g) lemma action_by_conj_on_finsets_inj : injective (@action_by_conj_on_finsets G _ _) := sorry lemma conj_by_image_compose (g1 g2 : G) : image (conj_by g1) ∘ image (conj_by g2) = image (conj_by (g1 * g2)) := funext (take s, begin rewrite conj_by_compose, rewrite image_comp end) lemma action_by_conj_on_finsets_hom : homomorphic (@action_by_conj_on_finsets G Hgr Hft Hdec) := take (g1 g2 : G), eq.symm (calc action_by_conj_on_finsets g1 * action_by_conj_on_finsets g2 = perm.mk ((image (conj_by g1))∘(image (conj_by g2))) _ : rfl ... = perm.mk ((image (conj_by (g1 * g2)))) (conj_by_im_inj _) : begin congruence, exact !conj_by_image_compose end ... = action_by_conj_on_finsets (g1 * g2) : rfl) definition conj_on_finsets_hom [instance] : is_hom_class (@action_by_conj_on_finsets G Hgr Hft Hdec) := is_hom_class.mk action_by_conj_on_finsets_hom end conjugation_action section lcosets open fintype subtype variables {G : Type} [group G] [fintype G] [decidable_eq G] variables H : finset G definition action_on_lcoset : G → perm (lcoset_type univ H) := take g, perm.mk (lcoset_lmul (mem_univ g)) lcoset_lmul_inj private definition lcoset_of (g : G) : lcoset_type univ H := tag (fin_lcoset H g) (exists.intro g (and.intro !mem_univ rfl)) variable {H} lemma action_on_lcoset_eq (g : G) (J : lcoset_type univ H) : elt_of (action_on_lcoset H g J) = fin_lcoset (elt_of J) g := rfl lemma action_on_lcoset_hom : homomorphic (action_on_lcoset H) := take g₁ g₂, eq_of_feq (funext take S, subtype.eq (by rewrite [↑action_on_lcoset, ↑lcoset_lmul, -fin_lcoset_compose])) definition action_on_lcoset_is_hom [instance] : is_hom_class (action_on_lcoset H) := is_hom_class.mk action_on_lcoset_hom variable [finsubgH : is_finsubg H] include finsubgH lemma aol_fixed_point_subset_normalizer (J : lcoset_type univ H) : is_fixed_point (action_on_lcoset H) H J → elt_of J ⊆ normalizer H := obtain j Pjin Pj, from exists_of_lcoset_type J, assume Pfp, have PH : ∀ {h}, h ∈ H → fin_lcoset (fin_lcoset H j) h = fin_lcoset H j, from take h, assume Ph, by rewrite [Pj, -action_on_lcoset_eq, Pfp h Ph], subset_of_forall take g, begin rewrite [-Pj, fin_lcoset_same, -inv_inv at {2}], intro Pg, rewrite -Pg at PH, apply finsubg_has_inv, apply mem_sep_of_mem !mem_univ, intro h Ph, have Phg : fin_lcoset (fin_lcoset H g) h = fin_lcoset H g, from PH Ph, revert Phg, rewrite [↑conj_by, inv_inv, mul.assoc, fin_lcoset_compose, -fin_lcoset_same, ↑fin_lcoset, mem_image_iff, ↑lmul_by], intro Pex, cases Pex with k Pand, cases Pand with Pkin Pk, rewrite [-Pk, inv_mul_cancel_left], exact Pkin end lemma aol_fixed_point_of_mem_normalizer {g : G} : g ∈ normalizer H → is_fixed_point (action_on_lcoset H) H (lcoset_of H g) := assume Pgin, take h, assume Phin, subtype.eq (by rewrite [action_on_lcoset_eq, ↑lcoset_of, lrcoset_same_of_mem_normalizer Pgin, fin_lrcoset_comm, finsubg_lcoset_id Phin]) lemma aol_fixed_points_eq_normalizer : Union (fixed_points (action_on_lcoset H) H) elt_of = normalizer H := ext take g, begin rewrite [mem_Union_iff], apply iff.intro, intro Pl, cases Pl with L PL, revert PL, rewrite [is_fixed_point_iff_mem_fixed_points], intro Pg, apply mem_of_subset_of_mem, apply aol_fixed_point_subset_normalizer L, exact and.left Pg, exact and.right Pg, intro Pr, existsi (lcoset_of H g), apply and.intro, rewrite [is_fixed_point_iff_mem_fixed_points], exact aol_fixed_point_of_mem_normalizer Pr, exact fin_mem_lcoset g end open nat lemma card_aol_fixed_points_eq_card_cosets : card (fixed_points (action_on_lcoset H) H) = card (lcoset_type (normalizer H) H) := have Peq : card (fixed_points (action_on_lcoset H) H) * card H = card (lcoset_type (normalizer H) H) * card H, from calc card _ * card H = card (Union (fixed_points (action_on_lcoset H) H) elt_of) : card_Union_lcosets ... = card (normalizer H) : aol_fixed_points_eq_normalizer ... = card (lcoset_type (normalizer H) H) * card H : lagrange_theorem' subset_normalizer, eq_of_mul_eq_mul_right (card_pos_of_mem !finsubg_has_one) Peq end lcosets section perm_fin open fin nat eq.ops variable {n : nat} definition lift_perm (p : perm (fin n)) : perm (fin (succ n)) := perm.mk (lift_fun p) (lift_fun_of_inj (perm.inj p)) definition lower_perm (p : perm (fin (succ n))) (P : p maxi = maxi) : perm (fin n) := perm.mk (lower_inj p (perm.inj p) P) (take i j, begin rewrite [-eq_iff_veq, *lower_inj_apply, eq_iff_veq], apply injective_comp (perm.inj p) lift_succ_inj end) lemma lift_lower_eq : ∀ {p : perm (fin (succ n))} (P : p maxi = maxi), lift_perm (lower_perm p P) = p | (perm.mk pf Pinj) := assume Pmax, begin rewrite [↑lift_perm], congruence, apply funext, intro i, have Pfmax : pf maxi = maxi, by apply Pmax, have Pd : decidable (i = maxi), from _, cases Pd with Pe Pne, rewrite [Pe, Pfmax], apply lift_fun_max, rewrite [lift_fun_of_ne_max Pne, ↑lower_perm, ↑lift_succ], rewrite [-eq_iff_veq, -val_lift, lower_inj_apply, eq_iff_veq], congruence, rewrite [-eq_iff_veq] end lemma lift_perm_inj : injective (@lift_perm n) := take p1 p2, assume Peq, eq_of_feq (lift_fun_inj (feq_of_eq Peq)) lemma lift_perm_inj_on_univ : set.inj_on (@lift_perm n) (ts univ) := eq.symm to_set_univ ▸ iff.elim_left set.injective_iff_inj_on_univ lift_perm_inj lemma lift_to_stab : image (@lift_perm n) univ = stab id univ maxi := ext (take (pp : perm (fin (succ n))), iff.intro (assume Pimg, obtain p P_ Pp, from exists_of_mem_image Pimg, have Ppp : pp maxi = maxi, from calc pp maxi = lift_perm p maxi : {eq.symm Pp} ... = lift_fun p maxi : rfl ... = maxi : lift_fun_max, mem_sep_of_mem !mem_univ Ppp) (assume Pstab, have Ppp : pp maxi = maxi, from of_mem_sep Pstab, mem_image !mem_univ (lift_lower_eq Ppp))) definition move_from_max_to (i : fin (succ n)) : perm (fin (succ n)) := perm.mk (madd (i - maxi)) madd_inj lemma orbit_max : orbit (@id (perm (fin (succ n)))) univ maxi = univ := ext (take i, iff.intro (assume P, !mem_univ) (assume P, begin apply mem_image, apply mem_image, apply mem_univ (move_from_max_to i), apply rfl, apply sub_add_cancel end)) lemma card_orbit_max : card (orbit (@id (perm (fin (succ n)))) univ maxi) = succ n := calc card (orbit (@id (perm (fin (succ n)))) univ maxi) = card univ : by rewrite orbit_max ... = succ n : card_fin (succ n) open fintype lemma card_lift_to_stab : card (stab (@id (perm (fin (succ n)))) univ maxi) = card (perm (fin n)) := calc finset.card (stab (@id (perm (fin (succ n)))) univ maxi) = finset.card (image (@lift_perm n) univ) : by rewrite lift_to_stab ... = card univ : by rewrite (card_image_eq_of_inj_on lift_perm_inj_on_univ) lemma card_perm_step : card (perm (fin (succ n))) = (succ n) * card (perm (fin n)) := calc card (perm (fin (succ n))) = card (orbit id univ maxi) * card (stab id univ maxi) : orbit_stabilizer_theorem ... = (succ n) * card (stab id univ maxi) : {card_orbit_max} ... = (succ n) * card (perm (fin n)) : by rewrite -card_lift_to_stab end perm_fin end group_theory
f76113a9117d05da1fd4267ed19e5e248885cec5
9a0b1b3a653ea926b03d1495fef64da1d14b3174
/tidy/rewrite_search/core/default.lean
2c87a1442feb1ddd7575df59d4dea686d526e565
[ "Apache-2.0" ]
permissive
khoek/mathlib-tidy
8623b27b4e04e7d598164e7eaf248610d58f768b
866afa6ab597c47f1b72e8fe2b82b97fff5b980f
refs/heads/master
1,585,598,975,772
1,538,659,544,000
1,538,659,544,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
58
lean
import .types import .debug import .engine import .explain
e26fcc18d1fd11acec9bbec7c0d986a89b84fece
bb31430994044506fa42fd667e2d556327e18dfe
/src/measure_theory/integral/interval_integral.lean
925d890f1737644241ee628fef86823f07b90c38
[ "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
141,611
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov, Patrick Massot, Sébastien Gouëzel -/ import analysis.normed_space.dual import data.set.intervals.disjoint import measure_theory.measure.haar_lebesgue import measure_theory.function.locally_integrable import measure_theory.integral.set_integral import measure_theory.integral.vitali_caratheodory import analysis.calculus.fderiv_measurable /-! # Integral over an interval In this file we define `∫ x in a..b, f x ∂μ` to be `∫ x in Ioc a b, f x ∂μ` if `a ≤ b` and `-∫ x in Ioc b a, f x ∂μ` if `b ≤ a`. We prove a few simple properties and several versions of the [fundamental theorem of calculus](https://en.wikipedia.org/wiki/Fundamental_theorem_of_calculus). Recall that its first version states that the function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(δu, δv) ↦ δv • f b - δu • f a` at `(a, b)` provided that `f` is continuous at `a` and `b`, and its second version states that, if `f` has an integrable derivative on `[a, b]`, then `∫ x in a..b, f' x = f b - f a`. ## Main statements ### FTC-1 for Lebesgue measure We prove several versions of FTC-1, all in the `interval_integral` namespace. Many of them follow the naming scheme `integral_has(_strict?)_(f?)deriv(_within?)_at(_of_tendsto_ae?)(_right|_left?)`. They formulate FTC in terms of `has(_strict?)_(f?)deriv(_within?)_at`. Let us explain the meaning of each part of the name: * `_strict` means that the theorem is about strict differentiability; * `f` means that the theorem is about differentiability in both endpoints; incompatible with `_right|_left`; * `_within` means that the theorem is about one-sided derivatives, see below for details; * `_of_tendsto_ae` means that instead of continuity the theorem assumes that `f` has a finite limit almost surely as `x` tends to `a` and/or `b`; * `_right` or `_left` mean that the theorem is about differentiability in the right (resp., left) endpoint. We also reformulate these theorems in terms of `(f?)deriv(_within?)`. These theorems are named `(f?)deriv(_within?)_integral(_of_tendsto_ae?)(_right|_left?)` with the same meaning of parts of the name. ### One-sided derivatives Theorem `integral_has_fderiv_within_at_of_tendsto_ae` states that `(u, v) ↦ ∫ x in u..v, f x` has a derivative `(δu, δv) ↦ δv • cb - δu • ca` within the set `s × t` at `(a, b)` provided that `f` tends to `ca` (resp., `cb`) almost surely at `la` (resp., `lb`), where possible values of `s`, `t`, and corresponding filters `la`, `lb` are given in the following table. | `s` | `la` | `t` | `lb` | | ------- | ---- | --- | ---- | | `Iic a` | `𝓝[≤] a` | `Iic b` | `𝓝[≤] b` | | `Ici a` | `𝓝[>] a` | `Ici b` | `𝓝[>] b` | | `{a}` | `⊥` | `{b}` | `⊥` | | `univ` | `𝓝 a` | `univ` | `𝓝 b` | We use a typeclass `FTC_filter` to make Lean automatically find `la`/`lb` based on `s`/`t`. This way we can formulate one theorem instead of `16` (or `8` if we leave only non-trivial ones not covered by `integral_has_deriv_within_at_of_tendsto_ae_(left|right)` and `integral_has_fderiv_at_of_tendsto_ae`). Similarly, `integral_has_deriv_within_at_of_tendsto_ae_right` works for both one-sided derivatives using the same typeclass to find an appropriate filter. ### FTC for a locally finite measure Before proving FTC for the Lebesgue measure, we prove a few statements that can be seen as FTC for any measure. The most general of them, `measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae`, states the following. Let `(la, la')` be an `FTC_filter` pair of filters around `a` (i.e., `FTC_filter a la la'`) and let `(lb, lb')` be an `FTC_filter` pair of filters around `b`. If `f` has finite limits `ca` and `cb` almost surely at `la'` and `lb'`, respectively, then `∫ x in va..vb, f x ∂μ - ∫ x in ua..ub, f x ∂μ = ∫ x in ub..vb, cb ∂μ - ∫ x in ua..va, ca ∂μ + o(‖∫ x in ua..va, (1:ℝ) ∂μ‖ + ‖∫ x in ub..vb, (1:ℝ) ∂μ‖)` as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`. ### FTC-2 and corollaries We use FTC-1 to prove several versions of FTC-2 for the Lebesgue measure, using a similar naming scheme as for the versions of FTC-1. They include: * `interval_integral.integral_eq_sub_of_has_deriv_right_of_le` - most general version, for functions with a right derivative * `interval_integral.integral_eq_sub_of_has_deriv_at'` - version for functions with a derivative on an open set * `interval_integral.integral_deriv_eq_sub'` - version that is easiest to use when computing the integral of a specific function We then derive additional integration techniques from FTC-2: * `interval_integral.integral_mul_deriv_eq_deriv_mul` - integration by parts * `interval_integral.integral_comp_mul_deriv''` - integration by substitution Many applications of these theorems can be found in the file `analysis.special_functions.integrals`. Note that the assumptions of FTC-2 are formulated in the form that `f'` is integrable. To use it in a context with the stronger assumption that `f'` is continuous, one can use `continuous_on.interval_integrable` or `continuous_on.integrable_on_Icc` or `continuous_on.integrable_on_interval`. ## Implementation notes ### Avoiding `if`, `min`, and `max` In order to avoid `if`s in the definition, we define `interval_integrable f μ a b` as `integrable_on f (Ioc a b) μ ∧ integrable_on f (Ioc b a) μ`. For any `a`, `b` one of these intervals is empty and the other coincides with `set.uIoc a b = set.Ioc (min a b) (max a b)`. Similarly, we define `∫ x in a..b, f x ∂μ` to be `∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ`. Again, for any `a`, `b` one of these integrals is zero, and the other gives the expected result. This way some properties can be translated from integrals over sets without dealing with the cases `a ≤ b` and `b ≤ a` separately. ### Choice of the interval We use integral over `set.uIoc a b = set.Ioc (min a b) (max a b)` instead of one of the other three possible intervals with the same endpoints for two reasons: * this way `∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ = ∫ x in a..c, f x ∂μ` holds whenever `f` is integrable on each interval; in particular, it works even if the measure `μ` has an atom at `b`; this rules out `set.Ioo` and `set.Icc` intervals; * with this definition for a probability measure `μ`, the integral `∫ x in a..b, 1 ∂μ` equals the difference $F_μ(b)-F_μ(a)$, where $F_μ(a)=μ(-∞, a]$ is the [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function) of `μ`. ### `FTC_filter` class As explained above, many theorems in this file rely on the typeclass `FTC_filter (a : ℝ) (l l' : filter ℝ)` to avoid code duplication. This typeclass combines four assumptions: - `pure a ≤ l`; - `l' ≤ 𝓝 a`; - `l'` has a basis of measurable sets; - if `u n` and `v n` tend to `l`, then for any `s ∈ l'`, `Ioc (u n) (v n)` is eventually included in `s`. This typeclass has the following “real” instances: `(a, pure a, ⊥)`, `(a, 𝓝[≥] a, 𝓝[>] a)`, `(a, 𝓝[≤] a, 𝓝[≤] a)`, `(a, 𝓝 a, 𝓝 a)`. Furthermore, we have the following instances that are equal to the previously mentioned instances: `(a, 𝓝[{a}] a, ⊥)` and `(a, 𝓝[univ] a, 𝓝[univ] a)`. While the difference between `Ici a` and `Ioi a` doesn't matter for theorems about Lebesgue measure, it becomes important in the versions of FTC about any locally finite measure if this measure has an atom at one of the endpoints. ### Combining one-sided and two-sided derivatives There are some `FTC_filter` instances where the fact that it is one-sided or two-sided depends on the point, namely `(x, 𝓝[Icc a b] x, 𝓝[Icc a b] x)` (resp. `(x, 𝓝[[a, b]] x, 𝓝[[a, b]] x)`, where `[a, b] = set.uIcc a b`), with `x ∈ Icc a b` (resp. `x ∈ [a, b]`). This results in a two-sided derivatives for `x ∈ Ioo a b` and one-sided derivatives for `x ∈ {a, b}`. Other instances could be added when needed (in that case, one also needs to add instances for `filter.is_measurably_generated` and `filter.tendsto_Ixx_class`). ## Tags integral, fundamental theorem of calculus, FTC-1, FTC-2, change of variables in integrals -/ noncomputable theory open topological_space (second_countable_topology) open measure_theory set classical filter function open_locale classical topological_space filter ennreal big_operators interval nnreal variables {ι 𝕜 E F A : Type*} [normed_add_comm_group E] /-! ### Integrability at an interval -/ /-- A function `f` is called *interval integrable* with respect to a measure `μ` on an unordered interval `a..b` if it is integrable on both intervals `(a, b]` and `(b, a]`. One of these intervals is always empty, so this property is equivalent to `f` being integrable on `(min a b, max a b]`. -/ def interval_integrable (f : ℝ → E) (μ : measure ℝ) (a b : ℝ) : Prop := integrable_on f (Ioc a b) μ ∧ integrable_on f (Ioc b a) μ section variables {f : ℝ → E} {a b : ℝ} {μ : measure ℝ} /-- A function is interval integrable with respect to a given measure `μ` on `a..b` if and only if it is integrable on `uIoc a b` with respect to `μ`. This is an equivalent definition of `interval_integrable`. -/ lemma interval_integrable_iff : interval_integrable f μ a b ↔ integrable_on f (Ι a b) μ := by rw [uIoc_eq_union, integrable_on_union, interval_integrable] /-- If a function is interval integrable with respect to a given measure `μ` on `a..b` then it is integrable on `uIoc a b` with respect to `μ`. -/ lemma interval_integrable.def (h : interval_integrable f μ a b) : integrable_on f (Ι a b) μ := interval_integrable_iff.mp h lemma interval_integrable_iff_integrable_Ioc_of_le (hab : a ≤ b) : interval_integrable f μ a b ↔ integrable_on f (Ioc a b) μ := by rw [interval_integrable_iff, uIoc_of_le hab] lemma integrable_on_Icc_iff_integrable_on_Ioc' {f : ℝ → E} (ha : μ {a} ≠ ∞) : integrable_on f (Icc a b) μ ↔ integrable_on f (Ioc a b) μ := begin cases le_or_lt a b with hab hab, { have : Icc a b = Icc a a ∪ Ioc a b := (Icc_union_Ioc_eq_Icc le_rfl hab).symm, rw [this, integrable_on_union], simp [ha.lt_top] }, { simp [hab, hab.le] }, end lemma integrable_on_Icc_iff_integrable_on_Ioc [has_no_atoms μ] {f : ℝ → E} {a b : ℝ} : integrable_on f (Icc a b) μ ↔ integrable_on f (Ioc a b) μ := integrable_on_Icc_iff_integrable_on_Ioc' (by simp) lemma integrable_on_Ioc_iff_integrable_on_Ioo' {f : ℝ → E} {a b : ℝ} (hb : μ {b} ≠ ∞) : integrable_on f (Ioc a b) μ ↔ integrable_on f (Ioo a b) μ := begin cases lt_or_le a b with hab hab, { have : Ioc a b = Ioo a b ∪ Icc b b := (Ioo_union_Icc_eq_Ioc hab le_rfl).symm, rw [this, integrable_on_union], simp [hb.lt_top] }, { simp [hab] }, end lemma integrable_on_Ioc_iff_integrable_on_Ioo [has_no_atoms μ] {f : ℝ → E} {a b : ℝ} : integrable_on f (Ioc a b) μ ↔ integrable_on f (Ioo a b) μ := integrable_on_Ioc_iff_integrable_on_Ioo' (by simp) lemma integrable_on_Icc_iff_integrable_on_Ioo [has_no_atoms μ] {f : ℝ → E} {a b : ℝ} : integrable_on f (Icc a b) μ ↔ integrable_on f (Ioo a b) μ := by rw [integrable_on_Icc_iff_integrable_on_Ioc, integrable_on_Ioc_iff_integrable_on_Ioo] lemma interval_integrable_iff' [has_no_atoms μ] : interval_integrable f μ a b ↔ integrable_on f (uIcc a b) μ := by rw [interval_integrable_iff, ←Icc_min_max, uIoc, integrable_on_Icc_iff_integrable_on_Ioc] lemma interval_integrable_iff_integrable_Icc_of_le {f : ℝ → E} {a b : ℝ} (hab : a ≤ b) {μ : measure ℝ} [has_no_atoms μ] : interval_integrable f μ a b ↔ integrable_on f (Icc a b) μ := by rw [interval_integrable_iff_integrable_Ioc_of_le hab, integrable_on_Icc_iff_integrable_on_Ioc] lemma integrable_on_Ici_iff_integrable_on_Ioi' {f : ℝ → E} (ha : μ {a} ≠ ∞) : integrable_on f (Ici a) μ ↔ integrable_on f (Ioi a) μ := begin have : Ici a = Icc a a ∪ Ioi a := (Icc_union_Ioi_eq_Ici le_rfl).symm, rw [this, integrable_on_union], simp [ha.lt_top] end lemma integrable_on_Ici_iff_integrable_on_Ioi [has_no_atoms μ] {f : ℝ → E} : integrable_on f (Ici a) μ ↔ integrable_on f (Ioi a) μ := integrable_on_Ici_iff_integrable_on_Ioi' (by simp) /-- If a function is integrable with respect to a given measure `μ` then it is interval integrable with respect to `μ` on `uIcc a b`. -/ lemma measure_theory.integrable.interval_integrable (hf : integrable f μ) : interval_integrable f μ a b := ⟨hf.integrable_on, hf.integrable_on⟩ lemma measure_theory.integrable_on.interval_integrable (hf : integrable_on f [a, b] μ) : interval_integrable f μ a b := ⟨measure_theory.integrable_on.mono_set hf (Ioc_subset_Icc_self.trans Icc_subset_uIcc), measure_theory.integrable_on.mono_set hf (Ioc_subset_Icc_self.trans Icc_subset_uIcc')⟩ lemma interval_integrable_const_iff {c : E} : interval_integrable (λ _, c) μ a b ↔ c = 0 ∨ μ (Ι a b) < ∞ := by simp only [interval_integrable_iff, integrable_on_const] @[simp] lemma interval_integrable_const [is_locally_finite_measure μ] {c : E} : interval_integrable (λ _, c) μ a b := interval_integrable_const_iff.2 $ or.inr measure_Ioc_lt_top end namespace interval_integrable section variables {f : ℝ → E} {a b c d : ℝ} {μ ν : measure ℝ} @[symm] lemma symm (h : interval_integrable f μ a b) : interval_integrable f μ b a := h.symm @[refl] lemma refl : interval_integrable f μ a a := by split; simp @[trans] lemma trans {a b c : ℝ} (hab : interval_integrable f μ a b) (hbc : interval_integrable f μ b c) : interval_integrable f μ a c := ⟨(hab.1.union hbc.1).mono_set Ioc_subset_Ioc_union_Ioc, (hbc.2.union hab.2).mono_set Ioc_subset_Ioc_union_Ioc⟩ lemma trans_iterate_Ico {a : ℕ → ℝ} {m n : ℕ} (hmn : m ≤ n) (hint : ∀ k ∈ Ico m n, interval_integrable f μ (a k) (a $ k+1)) : interval_integrable f μ (a m) (a n) := begin revert hint, refine nat.le_induction _ _ n hmn, { simp }, { assume p hp IH h, exact (IH (λ k hk, h k (Ico_subset_Ico_right p.le_succ hk))).trans (h p (by simp [hp])) } end lemma trans_iterate {a : ℕ → ℝ} {n : ℕ} (hint : ∀ k < n, interval_integrable f μ (a k) (a $ k+1)) : interval_integrable f μ (a 0) (a n) := trans_iterate_Ico bot_le (λ k hk, hint k hk.2) lemma neg (h : interval_integrable f μ a b) : interval_integrable (-f) μ a b := ⟨h.1.neg, h.2.neg⟩ lemma norm (h : interval_integrable f μ a b) : interval_integrable (λ x, ‖f x‖) μ a b := ⟨h.1.norm, h.2.norm⟩ lemma interval_integrable_norm_iff {f : ℝ → E} {μ : measure ℝ} {a b : ℝ} (hf : ae_strongly_measurable f (μ.restrict (Ι a b))) : interval_integrable (λ t, ‖f t‖) μ a b ↔ interval_integrable f μ a b := by { simp_rw [interval_integrable_iff, integrable_on], exact integrable_norm_iff hf } lemma abs {f : ℝ → ℝ} (h : interval_integrable f μ a b) : interval_integrable (λ x, |f x|) μ a b := h.norm lemma mono (hf : interval_integrable f ν a b) (h1 : [c, d] ⊆ [a, b]) (h2 : μ ≤ ν) : interval_integrable f μ c d := interval_integrable_iff.mpr $ hf.def.mono (uIoc_subset_uIoc_of_uIcc_subset_uIcc h1) h2 lemma mono_measure (hf : interval_integrable f ν a b) (h : μ ≤ ν) : interval_integrable f μ a b := hf.mono rfl.subset h lemma mono_set (hf : interval_integrable f μ a b) (h : [c, d] ⊆ [a, b]) : interval_integrable f μ c d := hf.mono h rfl.le lemma mono_set_ae (hf : interval_integrable f μ a b) (h : Ι c d ≤ᵐ[μ] Ι a b) : interval_integrable f μ c d := interval_integrable_iff.mpr $ hf.def.mono_set_ae h lemma mono_set' (hf : interval_integrable f μ a b) (hsub : Ι c d ⊆ Ι a b) : interval_integrable f μ c d := hf.mono_set_ae $ eventually_of_forall hsub lemma mono_fun [normed_add_comm_group F] {g : ℝ → F} (hf : interval_integrable f μ a b) (hgm : ae_strongly_measurable g (μ.restrict (Ι a b))) (hle : (λ x, ‖g x‖) ≤ᵐ[μ.restrict (Ι a b)] (λ x, ‖f x‖)) : interval_integrable g μ a b := interval_integrable_iff.2 $ hf.def.integrable.mono hgm hle lemma mono_fun' {g : ℝ → ℝ} (hg : interval_integrable g μ a b) (hfm : ae_strongly_measurable f (μ.restrict (Ι a b))) (hle : (λ x, ‖f x‖) ≤ᵐ[μ.restrict (Ι a b)] g) : interval_integrable f μ a b := interval_integrable_iff.2 $ hg.def.integrable.mono' hfm hle protected lemma ae_strongly_measurable (h : interval_integrable f μ a b) : ae_strongly_measurable f (μ.restrict (Ioc a b)):= h.1.ae_strongly_measurable protected lemma ae_strongly_measurable' (h : interval_integrable f μ a b) : ae_strongly_measurable f (μ.restrict (Ioc b a)):= h.2.ae_strongly_measurable end variables [normed_ring A] {f g : ℝ → E} {a b : ℝ} {μ : measure ℝ} lemma smul [normed_field 𝕜] [normed_space 𝕜 E] {f : ℝ → E} {a b : ℝ} {μ : measure ℝ} (h : interval_integrable f μ a b) (r : 𝕜) : interval_integrable (r • f) μ a b := ⟨h.1.smul r, h.2.smul r⟩ @[simp] lemma add (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) : interval_integrable (λ x, f x + g x) μ a b := ⟨hf.1.add hg.1, hf.2.add hg.2⟩ @[simp] lemma sub (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) : interval_integrable (λ x, f x - g x) μ a b := ⟨hf.1.sub hg.1, hf.2.sub hg.2⟩ lemma sum (s : finset ι) {f : ι → ℝ → E} (h : ∀ i ∈ s, interval_integrable (f i) μ a b) : interval_integrable (∑ i in s, f i) μ a b := ⟨integrable_finset_sum' s (λ i hi, (h i hi).1), integrable_finset_sum' s (λ i hi, (h i hi).2)⟩ lemma mul_continuous_on {f g : ℝ → A} (hf : interval_integrable f μ a b) (hg : continuous_on g [a, b]) : interval_integrable (λ x, f x * g x) μ a b := begin rw interval_integrable_iff at hf ⊢, exact hf.mul_continuous_on_of_subset hg measurable_set_Ioc is_compact_uIcc Ioc_subset_Icc_self end lemma continuous_on_mul {f g : ℝ → A} (hf : interval_integrable f μ a b) (hg : continuous_on g [a, b]) : interval_integrable (λ x, g x * f x) μ a b := begin rw interval_integrable_iff at hf ⊢, exact hf.continuous_on_mul_of_subset hg is_compact_uIcc measurable_set_Ioc Ioc_subset_Icc_self end lemma const_mul {f : ℝ → A} (hf : interval_integrable f μ a b) (c : A) : interval_integrable (λ x, c * f x) μ a b := hf.continuous_on_mul continuous_on_const lemma mul_const {f : ℝ → A} (hf : interval_integrable f μ a b) (c : A) : interval_integrable (λ x, f x * c) μ a b := hf.mul_continuous_on continuous_on_const lemma comp_mul_left (hf : interval_integrable f volume a b) (c : ℝ) : interval_integrable (λ x, f (c * x)) volume (a / c) (b / c) := begin rcases eq_or_ne c 0 with hc|hc, { rw hc, simp }, rw interval_integrable_iff' at hf ⊢, have A : measurable_embedding (λ x, x * c⁻¹) := (homeomorph.mul_right₀ _ (inv_ne_zero hc)).closed_embedding.measurable_embedding, rw [←real.smul_map_volume_mul_right (inv_ne_zero hc), integrable_on, measure.restrict_smul, integrable_smul_measure (by simpa : ennreal.of_real (|c⁻¹|) ≠ 0) ennreal.of_real_ne_top, ←integrable_on, measurable_embedding.integrable_on_map_iff A], convert hf using 1, { ext, simp only [comp_app], congr' 1, field_simp, ring }, { rw preimage_mul_const_uIcc (inv_ne_zero hc), field_simp [hc] }, end lemma iff_comp_neg : interval_integrable f volume a b ↔ interval_integrable (λ x, f (-x)) volume (-a) (-b) := begin split, all_goals { intro hf, convert comp_mul_left hf (-1), simp, field_simp, field_simp }, end end interval_integrable section variables {μ : measure ℝ} [is_locally_finite_measure μ] lemma continuous_on.interval_integrable {u : ℝ → E} {a b : ℝ} (hu : continuous_on u (uIcc a b)) : interval_integrable u μ a b := (continuous_on.integrable_on_Icc hu).interval_integrable lemma continuous_on.interval_integrable_of_Icc {u : ℝ → E} {a b : ℝ} (h : a ≤ b) (hu : continuous_on u (Icc a b)) : interval_integrable u μ a b := continuous_on.interval_integrable ((uIcc_of_le h).symm ▸ hu) /-- A continuous function on `ℝ` is `interval_integrable` with respect to any locally finite measure `ν` on ℝ. -/ lemma continuous.interval_integrable {u : ℝ → E} (hu : continuous u) (a b : ℝ) : interval_integrable u μ a b := hu.continuous_on.interval_integrable end section variables {μ : measure ℝ} [is_locally_finite_measure μ] [conditionally_complete_linear_order E] [order_topology E] [second_countable_topology E] lemma monotone_on.interval_integrable {u : ℝ → E} {a b : ℝ} (hu : monotone_on u (uIcc a b)) : interval_integrable u μ a b := begin rw interval_integrable_iff, exact (hu.integrable_on_is_compact is_compact_uIcc).mono_set Ioc_subset_Icc_self, end lemma antitone_on.interval_integrable {u : ℝ → E} {a b : ℝ} (hu : antitone_on u (uIcc a b)) : interval_integrable u μ a b := hu.dual_right.interval_integrable lemma monotone.interval_integrable {u : ℝ → E} {a b : ℝ} (hu : monotone u) : interval_integrable u μ a b := (hu.monotone_on _).interval_integrable lemma antitone.interval_integrable {u : ℝ → E} {a b : ℝ} (hu : antitone u) : interval_integrable u μ a b := (hu.antitone_on _).interval_integrable end /-- Let `l'` be a measurably generated filter; let `l` be a of filter such that each `s ∈ l'` eventually includes `Ioc u v` as both `u` and `v` tend to `l`. Let `μ` be a measure finite at `l'`. Suppose that `f : ℝ → E` has a finite limit at `l' ⊓ μ.ae`. Then `f` is interval integrable on `u..v` provided that both `u` and `v` tend to `l`. Typeclass instances allow Lean to find `l'` based on `l` but not vice versa, so `apply tendsto.eventually_interval_integrable_ae` will generate goals `filter ℝ` and `tendsto_Ixx_class Ioc ?m_1 l'`. -/ lemma filter.tendsto.eventually_interval_integrable_ae {f : ℝ → E} {μ : measure ℝ} {l l' : filter ℝ} (hfm : strongly_measurable_at_filter f l' μ) [tendsto_Ixx_class Ioc l l'] [is_measurably_generated l'] (hμ : μ.finite_at_filter l') {c : E} (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) {u v : ι → ℝ} {lt : filter ι} (hu : tendsto u lt l) (hv : tendsto v lt l) : ∀ᶠ t in lt, interval_integrable f μ (u t) (v t) := have _ := (hf.integrable_at_filter_ae hfm hμ).eventually, ((hu.Ioc hv).eventually this).and $ (hv.Ioc hu).eventually this /-- Let `l'` be a measurably generated filter; let `l` be a of filter such that each `s ∈ l'` eventually includes `Ioc u v` as both `u` and `v` tend to `l`. Let `μ` be a measure finite at `l'`. Suppose that `f : ℝ → E` has a finite limit at `l`. Then `f` is interval integrable on `u..v` provided that both `u` and `v` tend to `l`. Typeclass instances allow Lean to find `l'` based on `l` but not vice versa, so `apply tendsto.eventually_interval_integrable_ae` will generate goals `filter ℝ` and `tendsto_Ixx_class Ioc ?m_1 l'`. -/ lemma filter.tendsto.eventually_interval_integrable {f : ℝ → E} {μ : measure ℝ} {l l' : filter ℝ} (hfm : strongly_measurable_at_filter f l' μ) [tendsto_Ixx_class Ioc l l'] [is_measurably_generated l'] (hμ : μ.finite_at_filter l') {c : E} (hf : tendsto f l' (𝓝 c)) {u v : ι → ℝ} {lt : filter ι} (hu : tendsto u lt l) (hv : tendsto v lt l) : ∀ᶠ t in lt, interval_integrable f μ (u t) (v t) := (hf.mono_left inf_le_left).eventually_interval_integrable_ae hfm hμ hu hv /-! ### Interval integral: definition and basic properties In this section we define `∫ x in a..b, f x ∂μ` as `∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ` and prove some basic properties. -/ variables [complete_space E] [normed_space ℝ E] /-- The interval integral `∫ x in a..b, f x ∂μ` is defined as `∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ`. If `a ≤ b`, then it equals `∫ x in Ioc a b, f x ∂μ`, otherwise it equals `-∫ x in Ioc b a, f x ∂μ`. -/ def interval_integral (f : ℝ → E) (a b : ℝ) (μ : measure ℝ) : E := ∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ notation `∫` binders ` in ` a `..` b `, ` r:(scoped:60 f, f) ` ∂` μ:70 := interval_integral r a b μ notation `∫` binders ` in ` a `..` b `, ` r:(scoped:60 f, interval_integral f a b volume) := r namespace interval_integral section basic variables {a b : ℝ} {f g : ℝ → E} {μ : measure ℝ} @[simp] lemma integral_zero : ∫ x in a..b, (0 : E) ∂μ = 0 := by simp [interval_integral] lemma integral_of_le (h : a ≤ b) : ∫ x in a..b, f x ∂μ = ∫ x in Ioc a b, f x ∂μ := by simp [interval_integral, h] @[simp] lemma integral_same : ∫ x in a..a, f x ∂μ = 0 := sub_self _ lemma integral_symm (a b) : ∫ x in b..a, f x ∂μ = -∫ x in a..b, f x ∂μ := by simp only [interval_integral, neg_sub] lemma integral_of_ge (h : b ≤ a) : ∫ x in a..b, f x ∂μ = -∫ x in Ioc b a, f x ∂μ := by simp only [integral_symm b, integral_of_le h] lemma interval_integral_eq_integral_uIoc (f : ℝ → E) (a b : ℝ) (μ : measure ℝ) : ∫ x in a..b, f x ∂μ = (if a ≤ b then 1 else -1 : ℝ) • ∫ x in Ι a b, f x ∂μ := begin split_ifs with h, { simp only [integral_of_le h, uIoc_of_le h, one_smul] }, { simp only [integral_of_ge (not_le.1 h).le, uIoc_of_lt (not_le.1 h), neg_one_smul] } end lemma norm_interval_integral_eq (f : ℝ → E) (a b : ℝ) (μ : measure ℝ) : ‖∫ x in a..b, f x ∂μ‖ = ‖∫ x in Ι a b, f x ∂μ‖ := begin simp_rw [interval_integral_eq_integral_uIoc, norm_smul], split_ifs; simp only [norm_neg, norm_one, one_mul], end lemma abs_interval_integral_eq (f : ℝ → ℝ) (a b : ℝ) (μ : measure ℝ) : |∫ x in a..b, f x ∂μ| = |∫ x in Ι a b, f x ∂μ| := norm_interval_integral_eq f a b μ lemma integral_cases (f : ℝ → E) (a b) : ∫ x in a..b, f x ∂μ ∈ ({∫ x in Ι a b, f x ∂μ, -∫ x in Ι a b, f x ∂μ} : set E) := by { rw interval_integral_eq_integral_uIoc, split_ifs; simp } lemma integral_undef (h : ¬ interval_integrable f μ a b) : ∫ x in a..b, f x ∂μ = 0 := by cases le_total a b with hab hab; simp only [integral_of_le, integral_of_ge, hab, neg_eq_zero]; refine integral_undef (not_imp_not.mpr integrable.integrable_on' _); simpa [hab] using not_and_distrib.mp h lemma interval_integrable_of_integral_ne_zero {a b : ℝ} {f : ℝ → E} {μ : measure ℝ} (h : ∫ x in a..b, f x ∂μ ≠ 0) : interval_integrable f μ a b := by { contrapose! h, exact interval_integral.integral_undef h } lemma integral_non_ae_strongly_measurable (hf : ¬ ae_strongly_measurable f (μ.restrict (Ι a b))) : ∫ x in a..b, f x ∂μ = 0 := by rw [interval_integral_eq_integral_uIoc, integral_non_ae_strongly_measurable hf, smul_zero] lemma integral_non_ae_strongly_measurable_of_le (h : a ≤ b) (hf : ¬ ae_strongly_measurable f (μ.restrict (Ioc a b))) : ∫ x in a..b, f x ∂μ = 0 := integral_non_ae_strongly_measurable $ by rwa [uIoc_of_le h] lemma norm_integral_min_max (f : ℝ → E) : ‖∫ x in min a b..max a b, f x ∂μ‖ = ‖∫ x in a..b, f x ∂μ‖ := by cases le_total a b; simp [*, integral_symm a b] lemma norm_integral_eq_norm_integral_Ioc (f : ℝ → E) : ‖∫ x in a..b, f x ∂μ‖ = ‖∫ x in Ι a b, f x ∂μ‖ := by rw [← norm_integral_min_max, integral_of_le min_le_max, uIoc] lemma abs_integral_eq_abs_integral_uIoc (f : ℝ → ℝ) : |∫ x in a..b, f x ∂μ| = |∫ x in Ι a b, f x ∂μ| := norm_integral_eq_norm_integral_Ioc f lemma norm_integral_le_integral_norm_Ioc : ‖∫ x in a..b, f x ∂μ‖ ≤ ∫ x in Ι a b, ‖f x‖ ∂μ := calc ‖∫ x in a..b, f x ∂μ‖ = ‖∫ x in Ι a b, f x ∂μ‖ : norm_integral_eq_norm_integral_Ioc f ... ≤ ∫ x in Ι a b, ‖f x‖ ∂μ : norm_integral_le_integral_norm f lemma norm_integral_le_abs_integral_norm : ‖∫ x in a..b, f x ∂μ‖ ≤ |∫ x in a..b, ‖f x‖ ∂μ| := begin simp only [← real.norm_eq_abs, norm_integral_eq_norm_integral_Ioc], exact le_trans (norm_integral_le_integral_norm _) (le_abs_self _) end lemma norm_integral_le_integral_norm (h : a ≤ b) : ‖∫ x in a..b, f x ∂μ‖ ≤ ∫ x in a..b, ‖f x‖ ∂μ := norm_integral_le_integral_norm_Ioc.trans_eq $ by rw [uIoc_of_le h, integral_of_le h] lemma norm_integral_le_of_norm_le {g : ℝ → ℝ} (h : ∀ᵐ t ∂(μ.restrict $ Ι a b), ‖f t‖ ≤ g t) (hbound : interval_integrable g μ a b) : ‖∫ t in a..b, f t ∂μ‖ ≤ |∫ t in a..b, g t ∂μ| := by simp_rw [norm_interval_integral_eq, abs_interval_integral_eq, abs_eq_self.mpr (integral_nonneg_of_ae $ h.mono $ λ t ht, (norm_nonneg _).trans ht), norm_integral_le_of_norm_le hbound.def h] lemma norm_integral_le_of_norm_le_const_ae {a b C : ℝ} {f : ℝ → E} (h : ∀ᵐ x, x ∈ Ι a b → ‖f x‖ ≤ C) : ‖∫ x in a..b, f x‖ ≤ C * |b - a| := begin rw [norm_integral_eq_norm_integral_Ioc], convert norm_set_integral_le_of_norm_le_const_ae'' _ measurable_set_Ioc h, { rw [real.volume_Ioc, max_sub_min_eq_abs, ennreal.to_real_of_real (abs_nonneg _)] }, { simp only [real.volume_Ioc, ennreal.of_real_lt_top] }, end lemma norm_integral_le_of_norm_le_const {a b C : ℝ} {f : ℝ → E} (h : ∀ x ∈ Ι a b, ‖f x‖ ≤ C) : ‖∫ x in a..b, f x‖ ≤ C * |b - a| := norm_integral_le_of_norm_le_const_ae $ eventually_of_forall h @[simp] lemma integral_add (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) : ∫ x in a..b, f x + g x ∂μ = ∫ x in a..b, f x ∂μ + ∫ x in a..b, g x ∂μ := by simp only [interval_integral_eq_integral_uIoc, integral_add hf.def hg.def, smul_add] lemma integral_finset_sum {ι} {s : finset ι} {f : ι → ℝ → E} (h : ∀ i ∈ s, interval_integrable (f i) μ a b) : ∫ x in a..b, ∑ i in s, f i x ∂μ = ∑ i in s, ∫ x in a..b, f i x ∂μ := by simp only [interval_integral_eq_integral_uIoc, integral_finset_sum s (λ i hi, (h i hi).def), finset.smul_sum] @[simp] lemma integral_neg : ∫ x in a..b, -f x ∂μ = -∫ x in a..b, f x ∂μ := by { simp only [interval_integral, integral_neg], abel } @[simp] lemma integral_sub (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) : ∫ x in a..b, f x - g x ∂μ = ∫ x in a..b, f x ∂μ - ∫ x in a..b, g x ∂μ := by simpa only [sub_eq_add_neg] using (integral_add hf hg.neg).trans (congr_arg _ integral_neg) @[simp] lemma integral_smul {𝕜 : Type*} [nontrivially_normed_field 𝕜] [normed_space 𝕜 E] [smul_comm_class ℝ 𝕜 E] (r : 𝕜) (f : ℝ → E) : ∫ x in a..b, r • f x ∂μ = r • ∫ x in a..b, f x ∂μ := by simp only [interval_integral, integral_smul, smul_sub] @[simp] lemma integral_smul_const {𝕜 : Type*} [is_R_or_C 𝕜] [normed_space 𝕜 E] (f : ℝ → 𝕜) (c : E) : ∫ x in a..b, f x • c ∂μ = (∫ x in a..b, f x ∂μ) • c := by simp only [interval_integral_eq_integral_uIoc, integral_smul_const, smul_assoc] @[simp] lemma integral_const_mul {𝕜 : Type*} [is_R_or_C 𝕜] (r : 𝕜) (f : ℝ → 𝕜) : ∫ x in a..b, r * f x ∂μ = r * ∫ x in a..b, f x ∂μ := integral_smul r f @[simp] lemma integral_mul_const {𝕜 : Type*} [is_R_or_C 𝕜] (r : 𝕜) (f : ℝ → 𝕜) : ∫ x in a..b, f x * r ∂μ = ∫ x in a..b, f x ∂μ * r := by simpa only [mul_comm r] using integral_const_mul r f @[simp] lemma integral_div {𝕜 : Type*} [is_R_or_C 𝕜] (r : 𝕜) (f : ℝ → 𝕜) : ∫ x in a..b, f x / r ∂μ = ∫ x in a..b, f x ∂μ / r := by simpa only [div_eq_mul_inv] using integral_mul_const r⁻¹ f lemma integral_const' (c : E) : ∫ x in a..b, c ∂μ = ((μ $ Ioc a b).to_real - (μ $ Ioc b a).to_real) • c := by simp only [interval_integral, set_integral_const, sub_smul] @[simp] lemma integral_const (c : E) : ∫ x in a..b, c = (b - a) • c := by simp only [integral_const', real.volume_Ioc, ennreal.to_real_of_real', ← neg_sub b, max_zero_sub_eq_self] lemma integral_smul_measure (c : ℝ≥0∞) : ∫ x in a..b, f x ∂(c • μ) = c.to_real • ∫ x in a..b, f x ∂μ := by simp only [interval_integral, measure.restrict_smul, integral_smul_measure, smul_sub] end basic lemma integral_of_real {a b : ℝ} {μ : measure ℝ} {f : ℝ → ℝ} : ∫ x in a..b, (f x : ℂ) ∂μ = ↑(∫ x in a..b, f x ∂μ) := by simp only [interval_integral, integral_of_real, complex.of_real_sub] section continuous_linear_map variables {a b : ℝ} {μ : measure ℝ} {f : ℝ → E} variables [is_R_or_C 𝕜] [normed_space 𝕜 E] [normed_add_comm_group F] [normed_space 𝕜 F] open continuous_linear_map lemma _root_.continuous_linear_map.interval_integral_apply {a b : ℝ} {φ : ℝ → F →L[𝕜] E} (hφ : interval_integrable φ μ a b) (v : F) : (∫ x in a..b, φ x ∂μ) v = ∫ x in a..b, φ x v ∂μ := by simp_rw [interval_integral_eq_integral_uIoc, ← integral_apply hφ.def v, coe_smul', pi.smul_apply] variables [normed_space ℝ F] [complete_space F] lemma _root_.continuous_linear_map.interval_integral_comp_comm (L : E →L[𝕜] F) (hf : interval_integrable f μ a b) : ∫ x in a..b, L (f x) ∂μ = L (∫ x in a..b, f x ∂μ) := by simp_rw [interval_integral, L.integral_comp_comm hf.1, L.integral_comp_comm hf.2, L.map_sub] end continuous_linear_map section comp variables {a b c d : ℝ} (f : ℝ → E) @[simp] lemma integral_comp_mul_right (hc : c ≠ 0) : ∫ x in a..b, f (x * c) = c⁻¹ • ∫ x in a*c..b*c, f x := begin have A : measurable_embedding (λ x, x * c) := (homeomorph.mul_right₀ c hc).closed_embedding.measurable_embedding, conv_rhs { rw [← real.smul_map_volume_mul_right hc] }, simp_rw [integral_smul_measure, interval_integral, A.set_integral_map, ennreal.to_real_of_real (abs_nonneg c)], cases hc.lt_or_lt, { simp [h, mul_div_cancel, hc, abs_of_neg, measure.restrict_congr_set Ico_ae_eq_Ioc] }, { simp [h, mul_div_cancel, hc, abs_of_pos] } end @[simp] lemma smul_integral_comp_mul_right (c) : c • ∫ x in a..b, f (x * c) = ∫ x in a*c..b*c, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_mul_left (hc : c ≠ 0) : ∫ x in a..b, f (c * x) = c⁻¹ • ∫ x in c*a..c*b, f x := by simpa only [mul_comm c] using integral_comp_mul_right f hc @[simp] lemma smul_integral_comp_mul_left (c) : c • ∫ x in a..b, f (c * x) = ∫ x in c*a..c*b, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_div (hc : c ≠ 0) : ∫ x in a..b, f (x / c) = c • ∫ x in a/c..b/c, f x := by simpa only [inv_inv] using integral_comp_mul_right f (inv_ne_zero hc) @[simp] lemma inv_smul_integral_comp_div (c) : c⁻¹ • ∫ x in a..b, f (x / c) = ∫ x in a/c..b/c, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_add_right (d) : ∫ x in a..b, f (x + d) = ∫ x in a+d..b+d, f x := have A : measurable_embedding (λ x, x + d) := (homeomorph.add_right d).closed_embedding.measurable_embedding, calc ∫ x in a..b, f (x + d) = ∫ x in a+d..b+d, f x ∂(measure.map (λ x, x + d) volume) : by simp [interval_integral, A.set_integral_map] ... = ∫ x in a+d..b+d, f x : by rw [map_add_right_eq_self] @[simp] lemma integral_comp_add_left (d) : ∫ x in a..b, f (d + x) = ∫ x in d+a..d+b, f x := by simpa only [add_comm] using integral_comp_add_right f d @[simp] lemma integral_comp_mul_add (hc : c ≠ 0) (d) : ∫ x in a..b, f (c * x + d) = c⁻¹ • ∫ x in c*a+d..c*b+d, f x := by rw [← integral_comp_add_right, ← integral_comp_mul_left _ hc] @[simp] lemma smul_integral_comp_mul_add (c d) : c • ∫ x in a..b, f (c * x + d) = ∫ x in c*a+d..c*b+d, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_add_mul (hc : c ≠ 0) (d) : ∫ x in a..b, f (d + c * x) = c⁻¹ • ∫ x in d+c*a..d+c*b, f x := by rw [← integral_comp_add_left, ← integral_comp_mul_left _ hc] @[simp] lemma smul_integral_comp_add_mul (c d) : c • ∫ x in a..b, f (d + c * x) = ∫ x in d+c*a..d+c*b, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_div_add (hc : c ≠ 0) (d) : ∫ x in a..b, f (x / c + d) = c • ∫ x in a/c+d..b/c+d, f x := by simpa only [div_eq_inv_mul, inv_inv] using integral_comp_mul_add f (inv_ne_zero hc) d @[simp] lemma inv_smul_integral_comp_div_add (c d) : c⁻¹ • ∫ x in a..b, f (x / c + d) = ∫ x in a/c+d..b/c+d, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_add_div (hc : c ≠ 0) (d) : ∫ x in a..b, f (d + x / c) = c • ∫ x in d+a/c..d+b/c, f x := by simpa only [div_eq_inv_mul, inv_inv] using integral_comp_add_mul f (inv_ne_zero hc) d @[simp] lemma inv_smul_integral_comp_add_div (c d) : c⁻¹ • ∫ x in a..b, f (d + x / c) = ∫ x in d+a/c..d+b/c, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_mul_sub (hc : c ≠ 0) (d) : ∫ x in a..b, f (c * x - d) = c⁻¹ • ∫ x in c*a-d..c*b-d, f x := by simpa only [sub_eq_add_neg] using integral_comp_mul_add f hc (-d) @[simp] lemma smul_integral_comp_mul_sub (c d) : c • ∫ x in a..b, f (c * x - d) = ∫ x in c*a-d..c*b-d, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_sub_mul (hc : c ≠ 0) (d) : ∫ x in a..b, f (d - c * x) = c⁻¹ • ∫ x in d-c*b..d-c*a, f x := begin simp only [sub_eq_add_neg, neg_mul_eq_neg_mul], rw [integral_comp_add_mul f (neg_ne_zero.mpr hc) d, integral_symm], simp only [inv_neg, smul_neg, neg_neg, neg_smul], end @[simp] lemma smul_integral_comp_sub_mul (c d) : c • ∫ x in a..b, f (d - c * x) = ∫ x in d-c*b..d-c*a, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_div_sub (hc : c ≠ 0) (d) : ∫ x in a..b, f (x / c - d) = c • ∫ x in a/c-d..b/c-d, f x := by simpa only [div_eq_inv_mul, inv_inv] using integral_comp_mul_sub f (inv_ne_zero hc) d @[simp] lemma inv_smul_integral_comp_div_sub (c d) : c⁻¹ • ∫ x in a..b, f (x / c - d) = ∫ x in a/c-d..b/c-d, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_sub_div (hc : c ≠ 0) (d) : ∫ x in a..b, f (d - x / c) = c • ∫ x in d-b/c..d-a/c, f x := by simpa only [div_eq_inv_mul, inv_inv] using integral_comp_sub_mul f (inv_ne_zero hc) d @[simp] lemma inv_smul_integral_comp_sub_div (c d) : c⁻¹ • ∫ x in a..b, f (d - x / c) = ∫ x in d-b/c..d-a/c, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_sub_right (d) : ∫ x in a..b, f (x - d) = ∫ x in a-d..b-d, f x := by simpa only [sub_eq_add_neg] using integral_comp_add_right f (-d) @[simp] lemma integral_comp_sub_left (d) : ∫ x in a..b, f (d - x) = ∫ x in d-b..d-a, f x := by simpa only [one_mul, one_smul, inv_one] using integral_comp_sub_mul f one_ne_zero d @[simp] lemma integral_comp_neg : ∫ x in a..b, f (-x) = ∫ x in -b..-a, f x := by simpa only [zero_sub] using integral_comp_sub_left f 0 end comp /-! ### Integral is an additive function of the interval In this section we prove that `∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ = ∫ x in a..c, f x ∂μ` as well as a few other identities trivially equivalent to this one. We also prove that `∫ x in a..b, f x ∂μ = ∫ x, f x ∂μ` provided that `support f ⊆ Ioc a b`. -/ section order_closed_topology variables {a b c d : ℝ} {f g : ℝ → E} {μ : measure ℝ} /-- If two functions are equal in the relevant interval, their interval integrals are also equal. -/ lemma integral_congr {a b : ℝ} (h : eq_on f g [a, b]) : ∫ x in a..b, f x ∂μ = ∫ x in a..b, g x ∂μ := by cases le_total a b with hab hab; simpa [hab, integral_of_le, integral_of_ge] using set_integral_congr measurable_set_Ioc (h.mono Ioc_subset_Icc_self) lemma integral_add_adjacent_intervals_cancel (hab : interval_integrable f μ a b) (hbc : interval_integrable f μ b c) : ∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ + ∫ x in c..a, f x ∂μ = 0 := begin have hac := hab.trans hbc, simp only [interval_integral, sub_add_sub_comm, sub_eq_zero], iterate 4 { rw ← integral_union }, { suffices : Ioc a b ∪ Ioc b c ∪ Ioc c a = Ioc b a ∪ Ioc c b ∪ Ioc a c, by rw this, rw [Ioc_union_Ioc_union_Ioc_cycle, union_right_comm, Ioc_union_Ioc_union_Ioc_cycle, min_left_comm, max_left_comm] }, all_goals { simp [*, measurable_set.union, measurable_set_Ioc, Ioc_disjoint_Ioc_same, Ioc_disjoint_Ioc_same.symm, hab.1, hab.2, hbc.1, hbc.2, hac.1, hac.2] } end lemma integral_add_adjacent_intervals (hab : interval_integrable f μ a b) (hbc : interval_integrable f μ b c) : ∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ = ∫ x in a..c, f x ∂μ := by rw [← add_neg_eq_zero, ← integral_symm, integral_add_adjacent_intervals_cancel hab hbc] lemma sum_integral_adjacent_intervals_Ico {a : ℕ → ℝ} {m n : ℕ} (hmn : m ≤ n) (hint : ∀ k ∈ Ico m n, interval_integrable f μ (a k) (a $ k+1)) : ∑ (k : ℕ) in finset.Ico m n, ∫ x in (a k)..(a $ k+1), f x ∂μ = ∫ x in (a m)..(a n), f x ∂μ := begin revert hint, refine nat.le_induction _ _ n hmn, { simp }, { assume p hmp IH h, rw [finset.sum_Ico_succ_top hmp, IH, integral_add_adjacent_intervals], { apply interval_integrable.trans_iterate_Ico hmp (λ k hk, h k _), exact (Ico_subset_Ico le_rfl (nat.le_succ _)) hk }, { apply h, simp [hmp] }, { assume k hk, exact h _ (Ico_subset_Ico_right p.le_succ hk) } } end lemma sum_integral_adjacent_intervals {a : ℕ → ℝ} {n : ℕ} (hint : ∀ k < n, interval_integrable f μ (a k) (a $ k+1)) : ∑ (k : ℕ) in finset.range n, ∫ x in (a k)..(a $ k+1), f x ∂μ = ∫ x in (a 0)..(a n), f x ∂μ := begin rw ← nat.Ico_zero_eq_range, exact sum_integral_adjacent_intervals_Ico (zero_le n) (λ k hk, hint k hk.2), end lemma integral_interval_sub_left (hab : interval_integrable f μ a b) (hac : interval_integrable f μ a c) : ∫ x in a..b, f x ∂μ - ∫ x in a..c, f x ∂μ = ∫ x in c..b, f x ∂μ := sub_eq_of_eq_add' $ eq.symm $ integral_add_adjacent_intervals hac (hac.symm.trans hab) lemma integral_interval_add_interval_comm (hab : interval_integrable f μ a b) (hcd : interval_integrable f μ c d) (hac : interval_integrable f μ a c) : ∫ x in a..b, f x ∂μ + ∫ x in c..d, f x ∂μ = ∫ x in a..d, f x ∂μ + ∫ x in c..b, f x ∂μ := by rw [← integral_add_adjacent_intervals hac hcd, add_assoc, add_left_comm, integral_add_adjacent_intervals hac (hac.symm.trans hab), add_comm] lemma integral_interval_sub_interval_comm (hab : interval_integrable f μ a b) (hcd : interval_integrable f μ c d) (hac : interval_integrable f μ a c) : ∫ x in a..b, f x ∂μ - ∫ x in c..d, f x ∂μ = ∫ x in a..c, f x ∂μ - ∫ x in b..d, f x ∂μ := by simp only [sub_eq_add_neg, ← integral_symm, integral_interval_add_interval_comm hab hcd.symm (hac.trans hcd)] lemma integral_interval_sub_interval_comm' (hab : interval_integrable f μ a b) (hcd : interval_integrable f μ c d) (hac : interval_integrable f μ a c) : ∫ x in a..b, f x ∂μ - ∫ x in c..d, f x ∂μ = ∫ x in d..b, f x ∂μ - ∫ x in c..a, f x ∂μ := by { rw [integral_interval_sub_interval_comm hab hcd hac, integral_symm b d, integral_symm a c, sub_neg_eq_add, sub_eq_neg_add], } lemma integral_Iic_sub_Iic (ha : integrable_on f (Iic a) μ) (hb : integrable_on f (Iic b) μ) : ∫ x in Iic b, f x ∂μ - ∫ x in Iic a, f x ∂μ = ∫ x in a..b, f x ∂μ := begin wlog hab : a ≤ b using [a b] tactic.skip, { rw [sub_eq_iff_eq_add', integral_of_le hab, ← integral_union (Iic_disjoint_Ioc le_rfl), Iic_union_Ioc_eq_Iic hab], exacts [measurable_set_Ioc, ha, hb.mono_set (λ _, and.right)] }, { intros ha hb, rw [integral_symm, ← this hb ha, neg_sub] } end /-- If `μ` is a finite measure then `∫ x in a..b, c ∂μ = (μ (Iic b) - μ (Iic a)) • c`. -/ lemma integral_const_of_cdf [is_finite_measure μ] (c : E) : ∫ x in a..b, c ∂μ = ((μ (Iic b)).to_real - (μ (Iic a)).to_real) • c := begin simp only [sub_smul, ← set_integral_const], refine (integral_Iic_sub_Iic _ _).symm; simp only [integrable_on_const, measure_lt_top, or_true] end lemma integral_eq_integral_of_support_subset {a b} (h : support f ⊆ Ioc a b) : ∫ x in a..b, f x ∂μ = ∫ x, f x ∂μ := begin cases le_total a b with hab hab, { rw [integral_of_le hab, ← integral_indicator measurable_set_Ioc, indicator_eq_self.2 h]; apply_instance }, { rw [Ioc_eq_empty hab.not_lt, subset_empty_iff, support_eq_empty_iff] at h, simp [h] } end lemma integral_congr_ae' (h : ∀ᵐ x ∂μ, x ∈ Ioc a b → f x = g x) (h' : ∀ᵐ x ∂μ, x ∈ Ioc b a → f x = g x) : ∫ x in a..b, f x ∂μ = ∫ x in a..b, g x ∂μ := by simp only [interval_integral, set_integral_congr_ae (measurable_set_Ioc) h, set_integral_congr_ae (measurable_set_Ioc) h'] lemma integral_congr_ae (h : ∀ᵐ x ∂μ, x ∈ Ι a b → f x = g x) : ∫ x in a..b, f x ∂μ = ∫ x in a..b, g x ∂μ := integral_congr_ae' (ae_uIoc_iff.mp h).1 (ae_uIoc_iff.mp h).2 lemma integral_zero_ae (h : ∀ᵐ x ∂μ, x ∈ Ι a b → f x = 0) : ∫ x in a..b, f x ∂μ = 0 := calc ∫ x in a..b, f x ∂μ = ∫ x in a..b, 0 ∂μ : integral_congr_ae h ... = 0 : integral_zero lemma integral_indicator {a₁ a₂ a₃ : ℝ} (h : a₂ ∈ Icc a₁ a₃) : ∫ x in a₁..a₃, indicator {x | x ≤ a₂} f x ∂ μ = ∫ x in a₁..a₂, f x ∂ μ := begin have : {x | x ≤ a₂} ∩ Ioc a₁ a₃ = Ioc a₁ a₂, from Iic_inter_Ioc_of_le h.2, rw [integral_of_le h.1, integral_of_le (h.1.trans h.2), integral_indicator, measure.restrict_restrict, this], exact measurable_set_Iic, all_goals { apply measurable_set_Iic }, end /-- Lebesgue dominated convergence theorem for filters with a countable basis -/ lemma tendsto_integral_filter_of_dominated_convergence {ι} {l : filter ι} [l.is_countably_generated] {F : ι → ℝ → E} (bound : ℝ → ℝ) (hF_meas : ∀ᶠ n in l, ae_strongly_measurable (F n) (μ.restrict (Ι a b))) (h_bound : ∀ᶠ n in l, ∀ᵐ x ∂μ, x ∈ Ι a b → ‖F n x‖ ≤ bound x) (bound_integrable : interval_integrable bound μ a b) (h_lim : ∀ᵐ x ∂μ, x ∈ Ι a b → tendsto (λ n, F n x) l (𝓝 (f x))) : tendsto (λn, ∫ x in a..b, F n x ∂μ) l (𝓝 $ ∫ x in a..b, f x ∂μ) := begin simp only [interval_integrable_iff, interval_integral_eq_integral_uIoc, ← ae_restrict_iff' measurable_set_uIoc] at *, exact tendsto_const_nhds.smul (tendsto_integral_filter_of_dominated_convergence bound hF_meas h_bound bound_integrable h_lim) end /-- Lebesgue dominated convergence theorem for series. -/ lemma has_sum_integral_of_dominated_convergence {ι} [countable ι] {F : ι → ℝ → E} (bound : ι → ℝ → ℝ) (hF_meas : ∀ n, ae_strongly_measurable (F n) (μ.restrict (Ι a b))) (h_bound : ∀ n, ∀ᵐ t ∂μ, t ∈ Ι a b → ‖F n t‖ ≤ bound n t) (bound_summable : ∀ᵐ t ∂μ, t ∈ Ι a b → summable (λ n, bound n t)) (bound_integrable : interval_integrable (λ t, ∑' n, bound n t) μ a b) (h_lim : ∀ᵐ t ∂μ, t ∈ Ι a b → has_sum (λ n, F n t) (f t)) : has_sum (λn, ∫ t in a..b, F n t ∂μ) (∫ t in a..b, f t ∂μ) := begin simp only [interval_integrable_iff, interval_integral_eq_integral_uIoc, ← ae_restrict_iff' measurable_set_uIoc] at *, exact (has_sum_integral_of_dominated_convergence bound hF_meas h_bound bound_summable bound_integrable h_lim).const_smul end open topological_space variables {X : Type*} [topological_space X] [first_countable_topology X] /-- Continuity of interval integral with respect to a parameter, at a point within a set. Given `F : X → ℝ → E`, assume `F x` is ae-measurable on `[a, b]` for `x` in a neighborhood of `x₀` within `s` and at `x₀`, and assume it is bounded by a function integrable on `[a, b]` independent of `x` in a neighborhood of `x₀` within `s`. If `(λ x, F x t)` is continuous at `x₀` within `s` for almost every `t` in `[a, b]` then the same holds for `(λ x, ∫ t in a..b, F x t ∂μ) s x₀`. -/ lemma continuous_within_at_of_dominated_interval {F : X → ℝ → E} {x₀ : X} {bound : ℝ → ℝ} {a b : ℝ} {s : set X} (hF_meas : ∀ᶠ x in 𝓝[s] x₀, ae_strongly_measurable (F x) (μ.restrict $ Ι a b)) (h_bound : ∀ᶠ x in 𝓝[s] x₀, ∀ᵐ t ∂μ, t ∈ Ι a b → ‖F x t‖ ≤ bound t) (bound_integrable : interval_integrable bound μ a b) (h_cont : ∀ᵐ t ∂μ, t ∈ Ι a b → continuous_within_at (λ x, F x t) s x₀) : continuous_within_at (λ x, ∫ t in a..b, F x t ∂μ) s x₀ := tendsto_integral_filter_of_dominated_convergence bound hF_meas h_bound bound_integrable h_cont /-- Continuity of interval integral with respect to a parameter at a point. Given `F : X → ℝ → E`, assume `F x` is ae-measurable on `[a, b]` for `x` in a neighborhood of `x₀`, and assume it is bounded by a function integrable on `[a, b]` independent of `x` in a neighborhood of `x₀`. If `(λ x, F x t)` is continuous at `x₀` for almost every `t` in `[a, b]` then the same holds for `(λ x, ∫ t in a..b, F x t ∂μ) s x₀`. -/ lemma continuous_at_of_dominated_interval {F : X → ℝ → E} {x₀ : X} {bound : ℝ → ℝ} {a b : ℝ} (hF_meas : ∀ᶠ x in 𝓝 x₀, ae_strongly_measurable (F x) (μ.restrict $ Ι a b)) (h_bound : ∀ᶠ x in 𝓝 x₀, ∀ᵐ t ∂μ, t ∈ Ι a b → ‖F x t‖ ≤ bound t) (bound_integrable : interval_integrable bound μ a b) (h_cont : ∀ᵐ t ∂μ, t ∈ Ι a b → continuous_at (λ x, F x t) x₀) : continuous_at (λ x, ∫ t in a..b, F x t ∂μ) x₀ := tendsto_integral_filter_of_dominated_convergence bound hF_meas h_bound bound_integrable h_cont /-- Continuity of interval integral with respect to a parameter. Given `F : X → ℝ → E`, assume each `F x` is ae-measurable on `[a, b]`, and assume it is bounded by a function integrable on `[a, b]` independent of `x`. If `(λ x, F x t)` is continuous for almost every `t` in `[a, b]` then the same holds for `(λ x, ∫ t in a..b, F x t ∂μ) s x₀`. -/ lemma continuous_of_dominated_interval {F : X → ℝ → E} {bound : ℝ → ℝ} {a b : ℝ} (hF_meas : ∀ x, ae_strongly_measurable (F x) $ μ.restrict $ Ι a b) (h_bound : ∀ x, ∀ᵐ t ∂μ, t ∈ Ι a b → ‖F x t‖ ≤ bound t) (bound_integrable : interval_integrable bound μ a b) (h_cont : ∀ᵐ t ∂μ, t ∈ Ι a b → continuous (λ x, F x t)) : continuous (λ x, ∫ t in a..b, F x t ∂μ) := continuous_iff_continuous_at.mpr (λ x₀, continuous_at_of_dominated_interval (eventually_of_forall hF_meas) (eventually_of_forall h_bound) bound_integrable $ h_cont.mono $ λ x himp hx, (himp hx).continuous_at) end order_closed_topology section continuous_primitive open topological_space variables {a b b₀ b₁ b₂ : ℝ} {μ : measure ℝ} {f g : ℝ → E} lemma continuous_within_at_primitive (hb₀ : μ {b₀} = 0) (h_int : interval_integrable f μ (min a b₁) (max a b₂)) : continuous_within_at (λ b, ∫ x in a .. b, f x ∂ μ) (Icc b₁ b₂) b₀ := begin by_cases h₀ : b₀ ∈ Icc b₁ b₂, { have h₁₂ : b₁ ≤ b₂ := h₀.1.trans h₀.2, have min₁₂ : min b₁ b₂ = b₁ := min_eq_left h₁₂, have h_int' : ∀ {x}, x ∈ Icc b₁ b₂ → interval_integrable f μ b₁ x, { rintros x ⟨h₁, h₂⟩, apply h_int.mono_set, apply uIcc_subset_uIcc, { exact ⟨min_le_of_left_le (min_le_right a b₁), h₁.trans (h₂.trans $ le_max_of_le_right $ le_max_right _ _)⟩ }, { exact ⟨min_le_of_left_le $ (min_le_right _ _).trans h₁, le_max_of_le_right $ h₂.trans $ le_max_right _ _⟩ } }, have : ∀ b ∈ Icc b₁ b₂, ∫ x in a..b, f x ∂μ = ∫ x in a..b₁, f x ∂μ + ∫ x in b₁..b, f x ∂μ, { rintros b ⟨h₁, h₂⟩, rw ← integral_add_adjacent_intervals _ (h_int' ⟨h₁, h₂⟩), apply h_int.mono_set, apply uIcc_subset_uIcc, { exact ⟨min_le_of_left_le (min_le_left a b₁), le_max_of_le_right (le_max_left _ _)⟩ }, { exact ⟨min_le_of_left_le (min_le_right _ _), le_max_of_le_right (h₁.trans $ h₂.trans (le_max_right a b₂))⟩ } }, apply continuous_within_at.congr _ this (this _ h₀), clear this, refine continuous_within_at_const.add _, have : (λ b, ∫ x in b₁..b, f x ∂μ) =ᶠ[𝓝[Icc b₁ b₂] b₀] λ b, ∫ x in b₁..b₂, indicator {x | x ≤ b} f x ∂ μ, { apply eventually_eq_of_mem self_mem_nhds_within, exact λ b b_in, (integral_indicator b_in).symm }, apply continuous_within_at.congr_of_eventually_eq _ this (integral_indicator h₀).symm, have : interval_integrable (λ x, ‖f x‖) μ b₁ b₂, from interval_integrable.norm (h_int' $ right_mem_Icc.mpr h₁₂), refine continuous_within_at_of_dominated_interval _ _ this _ ; clear this, { apply eventually.mono (self_mem_nhds_within), intros x hx, erw [ae_strongly_measurable_indicator_iff, measure.restrict_restrict, Iic_inter_Ioc_of_le], { rw min₁₂, exact (h_int' hx).1.ae_strongly_measurable }, { exact le_max_of_le_right hx.2 }, exacts [measurable_set_Iic, measurable_set_Iic] }, { refine eventually_of_forall (λ x, eventually_of_forall (λ t, _)), dsimp [indicator], split_ifs ; simp }, { have : ∀ᵐ t ∂μ, t < b₀ ∨ b₀ < t, { apply eventually.mono (compl_mem_ae_iff.mpr hb₀), intros x hx, exact ne.lt_or_lt hx }, apply this.mono, rintros x₀ (hx₀ | hx₀) -, { have : ∀ᶠ x in 𝓝[Icc b₁ b₂] b₀, {t : ℝ | t ≤ x}.indicator f x₀ = f x₀, { apply mem_nhds_within_of_mem_nhds, apply eventually.mono (Ioi_mem_nhds hx₀), intros x hx, simp [hx.le] }, apply continuous_within_at_const.congr_of_eventually_eq this, simp [hx₀.le] }, { have : ∀ᶠ x in 𝓝[Icc b₁ b₂] b₀, {t : ℝ | t ≤ x}.indicator f x₀ = 0, { apply mem_nhds_within_of_mem_nhds, apply eventually.mono (Iio_mem_nhds hx₀), intros x hx, simp [hx] }, apply continuous_within_at_const.congr_of_eventually_eq this, simp [hx₀] } } }, { apply continuous_within_at_of_not_mem_closure, rwa [closure_Icc] } end lemma continuous_on_primitive [has_no_atoms μ] (h_int : integrable_on f (Icc a b) μ) : continuous_on (λ x, ∫ t in Ioc a x, f t ∂ μ) (Icc a b) := begin by_cases h : a ≤ b, { have : ∀ x ∈ Icc a b, ∫ t in Ioc a x, f t ∂μ = ∫ t in a..x, f t ∂μ, { intros x x_in, simp_rw [← uIoc_of_le h, integral_of_le x_in.1] }, rw continuous_on_congr this, intros x₀ hx₀, refine continuous_within_at_primitive (measure_singleton x₀) _, simp only [interval_integrable_iff_integrable_Ioc_of_le, min_eq_left, max_eq_right, h], exact h_int.mono Ioc_subset_Icc_self le_rfl }, { rw Icc_eq_empty h, exact continuous_on_empty _ }, end lemma continuous_on_primitive_Icc [has_no_atoms μ] (h_int : integrable_on f (Icc a b) μ) : continuous_on (λ x, ∫ t in Icc a x, f t ∂ μ) (Icc a b) := begin rw show (λ x, ∫ t in Icc a x, f t ∂μ) = λ x, ∫ t in Ioc a x, f t ∂μ, by { ext x, exact integral_Icc_eq_integral_Ioc }, exact continuous_on_primitive h_int end /-- Note: this assumes that `f` is `interval_integrable`, in contrast to some other lemmas here. -/ lemma continuous_on_primitive_interval' [has_no_atoms μ] (h_int : interval_integrable f μ b₁ b₂) (ha : a ∈ [b₁, b₂]) : continuous_on (λ b, ∫ x in a..b, f x ∂ μ) [b₁, b₂] := begin intros b₀ hb₀, refine continuous_within_at_primitive (measure_singleton _) _, rw [min_eq_right ha.1, max_eq_right ha.2], simpa [interval_integrable_iff, uIoc] using h_int, end lemma continuous_on_primitive_interval [has_no_atoms μ] (h_int : integrable_on f (uIcc a b) μ) : continuous_on (λ x, ∫ t in a..x, f t ∂ μ) (uIcc a b) := continuous_on_primitive_interval' h_int.interval_integrable left_mem_uIcc lemma continuous_on_primitive_interval_left [has_no_atoms μ] (h_int : integrable_on f (uIcc a b) μ) : continuous_on (λ x, ∫ t in x..b, f t ∂ μ) (uIcc a b) := begin rw uIcc_comm a b at h_int ⊢, simp only [integral_symm b], exact (continuous_on_primitive_interval h_int).neg, end variables [has_no_atoms μ] lemma continuous_primitive (h_int : ∀ a b, interval_integrable f μ a b) (a : ℝ) : continuous (λ b, ∫ x in a..b, f x ∂ μ) := begin rw continuous_iff_continuous_at, intro b₀, cases exists_lt b₀ with b₁ hb₁, cases exists_gt b₀ with b₂ hb₂, apply continuous_within_at.continuous_at _ (Icc_mem_nhds hb₁ hb₂), exact continuous_within_at_primitive (measure_singleton b₀) (h_int _ _) end lemma _root_.measure_theory.integrable.continuous_primitive (h_int : integrable f μ) (a : ℝ) : continuous (λ b, ∫ x in a..b, f x ∂ μ) := continuous_primitive (λ _ _, h_int.interval_integrable) a end continuous_primitive section variables {f g : ℝ → ℝ} {a b : ℝ} {μ : measure ℝ} lemma integral_eq_zero_iff_of_le_of_nonneg_ae (hab : a ≤ b) (hf : 0 ≤ᵐ[μ.restrict (Ioc a b)] f) (hfi : interval_integrable f μ a b) : ∫ x in a..b, f x ∂μ = 0 ↔ f =ᵐ[μ.restrict (Ioc a b)] 0 := by rw [integral_of_le hab, integral_eq_zero_iff_of_nonneg_ae hf hfi.1] lemma integral_eq_zero_iff_of_nonneg_ae (hf : 0 ≤ᵐ[μ.restrict (Ioc a b ∪ Ioc b a)] f) (hfi : interval_integrable f μ a b) : ∫ x in a..b, f x ∂μ = 0 ↔ f =ᵐ[μ.restrict (Ioc a b ∪ Ioc b a)] 0 := begin cases le_total a b with hab hab; simp only [Ioc_eq_empty hab.not_lt, empty_union, union_empty] at hf ⊢, { exact integral_eq_zero_iff_of_le_of_nonneg_ae hab hf hfi }, { rw [integral_symm, neg_eq_zero, integral_eq_zero_iff_of_le_of_nonneg_ae hab hf hfi.symm] } end /-- If `f` is nonnegative and integrable on the unordered interval `set.uIoc a b`, then its integral over `a..b` is positive if and only if `a < b` and the measure of `function.support f ∩ set.Ioc a b` is positive. -/ lemma integral_pos_iff_support_of_nonneg_ae' (hf : 0 ≤ᵐ[μ.restrict (Ι a b)] f) (hfi : interval_integrable f μ a b) : 0 < ∫ x in a..b, f x ∂μ ↔ a < b ∧ 0 < μ (support f ∩ Ioc a b) := begin cases lt_or_le a b with hab hba, { rw uIoc_of_le hab.le at hf, simp only [hab, true_and, integral_of_le hab.le, set_integral_pos_iff_support_of_nonneg_ae hf hfi.1] }, { suffices : ∫ x in a..b, f x ∂μ ≤ 0, by simp only [this.not_lt, hba.not_lt, false_and], rw [integral_of_ge hba, neg_nonpos], rw [uIoc_swap, uIoc_of_le hba] at hf, exact integral_nonneg_of_ae hf } end /-- If `f` is nonnegative a.e.-everywhere and it is integrable on the unordered interval `set.uIoc a b`, then its integral over `a..b` is positive if and only if `a < b` and the measure of `function.support f ∩ set.Ioc a b` is positive. -/ lemma integral_pos_iff_support_of_nonneg_ae (hf : 0 ≤ᵐ[μ] f) (hfi : interval_integrable f μ a b) : 0 < ∫ x in a..b, f x ∂μ ↔ a < b ∧ 0 < μ (support f ∩ Ioc a b) := integral_pos_iff_support_of_nonneg_ae' (ae_mono measure.restrict_le_self hf) hfi /-- If `f : ℝ → ℝ` is strictly positive and integrable on `(a, b]` for real numbers `a < b`, then its integral over `a..b` is strictly positive. -/ lemma interval_integral_pos_of_pos {f : ℝ → ℝ} {a b : ℝ} (hfi : interval_integrable f measure_space.volume a b) (h : ∀ x, 0 < f x) (hab : a < b) : 0 < ∫ x in a..b, f x := begin have hsupp : support f = univ := eq_univ_iff_forall.mpr (λ t, (h t).ne.symm), replace h₀ : 0 ≤ᵐ[volume] f := eventually_of_forall (λ x, (h x).le), rw integral_pos_iff_support_of_nonneg_ae h₀ hfi, exact ⟨hab, by simp [hsupp, hab]⟩, end /-- If `f` and `g` are two functions that are interval integrable on `a..b`, `a ≤ b`, `f x ≤ g x` for a.e. `x ∈ set.Ioc a b`, and `f x < g x` on a subset of `set.Ioc a b` of nonzero measure, then `∫ x in a..b, f x ∂μ < ∫ x in a..b, g x ∂μ`. -/ lemma integral_lt_integral_of_ae_le_of_measure_set_of_lt_ne_zero (hab : a ≤ b) (hfi : interval_integrable f μ a b) (hgi : interval_integrable g μ a b) (hle : f ≤ᵐ[μ.restrict (Ioc a b)] g) (hlt : μ.restrict (Ioc a b) {x | f x < g x} ≠ 0) : ∫ x in a..b, f x ∂μ < ∫ x in a..b, g x ∂μ := begin rw [← sub_pos, ← integral_sub hgi hfi, integral_of_le hab, measure_theory.integral_pos_iff_support_of_nonneg_ae], { refine pos_iff_ne_zero.2 (mt (measure_mono_null _) hlt), exact λ x hx, (sub_pos.2 hx).ne' }, exacts [hle.mono (λ x, sub_nonneg.2), hgi.1.sub hfi.1] end /-- If `f` and `g` are continuous on `[a, b]`, `a < b`, `f x ≤ g x` on this interval, and `f c < g c` at some point `c ∈ [a, b]`, then `∫ x in a..b, f x < ∫ x in a..b, g x`. -/ lemma integral_lt_integral_of_continuous_on_of_le_of_exists_lt {f g : ℝ → ℝ} {a b : ℝ} (hab : a < b) (hfc : continuous_on f (Icc a b)) (hgc : continuous_on g (Icc a b)) (hle : ∀ x ∈ Ioc a b, f x ≤ g x) (hlt : ∃ c ∈ Icc a b, f c < g c) : ∫ x in a..b, f x < ∫ x in a..b, g x := begin refine integral_lt_integral_of_ae_le_of_measure_set_of_lt_ne_zero hab.le (hfc.interval_integrable_of_Icc hab.le) (hgc.interval_integrable_of_Icc hab.le) ((ae_restrict_mem measurable_set_Ioc).mono hle) _, contrapose! hlt, have h_eq : f =ᵐ[volume.restrict (Ioc a b)] g, { simp only [← not_le, ← ae_iff] at hlt, exact eventually_le.antisymm ((ae_restrict_iff' measurable_set_Ioc).2 $ eventually_of_forall hle) hlt }, simp only [measure.restrict_congr_set Ioc_ae_eq_Icc] at h_eq, exact λ c hc, (measure.eq_on_Icc_of_ae_eq volume hab.ne h_eq hfc hgc hc).ge end lemma integral_nonneg_of_ae_restrict (hab : a ≤ b) (hf : 0 ≤ᵐ[μ.restrict (Icc a b)] f) : 0 ≤ (∫ u in a..b, f u ∂μ) := let H := ae_restrict_of_ae_restrict_of_subset Ioc_subset_Icc_self hf in by simpa only [integral_of_le hab] using set_integral_nonneg_of_ae_restrict H lemma integral_nonneg_of_ae (hab : a ≤ b) (hf : 0 ≤ᵐ[μ] f) : 0 ≤ (∫ u in a..b, f u ∂μ) := integral_nonneg_of_ae_restrict hab $ ae_restrict_of_ae hf lemma integral_nonneg_of_forall (hab : a ≤ b) (hf : ∀ u, 0 ≤ f u) : 0 ≤ (∫ u in a..b, f u ∂μ) := integral_nonneg_of_ae hab $ eventually_of_forall hf lemma integral_nonneg (hab : a ≤ b) (hf : ∀ u, u ∈ Icc a b → 0 ≤ f u) : 0 ≤ (∫ u in a..b, f u ∂μ) := integral_nonneg_of_ae_restrict hab $ (ae_restrict_iff' measurable_set_Icc).mpr $ ae_of_all μ hf lemma abs_integral_le_integral_abs (hab : a ≤ b) : |∫ x in a..b, f x ∂μ| ≤ ∫ x in a..b, |f x| ∂μ := by simpa only [← real.norm_eq_abs] using norm_integral_le_integral_norm hab section mono variables (hab : a ≤ b) (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) include hab hf hg lemma integral_mono_ae_restrict (h : f ≤ᵐ[μ.restrict (Icc a b)] g) : ∫ u in a..b, f u ∂μ ≤ ∫ u in a..b, g u ∂μ := let H := h.filter_mono $ ae_mono $ measure.restrict_mono Ioc_subset_Icc_self $ le_refl μ in by simpa only [integral_of_le hab] using set_integral_mono_ae_restrict hf.1 hg.1 H lemma integral_mono_ae (h : f ≤ᵐ[μ] g) : ∫ u in a..b, f u ∂μ ≤ ∫ u in a..b, g u ∂μ := by simpa only [integral_of_le hab] using set_integral_mono_ae hf.1 hg.1 h lemma integral_mono_on (h : ∀ x ∈ Icc a b, f x ≤ g x) : ∫ u in a..b, f u ∂μ ≤ ∫ u in a..b, g u ∂μ := let H := λ x hx, h x $ Ioc_subset_Icc_self hx in by simpa only [integral_of_le hab] using set_integral_mono_on hf.1 hg.1 measurable_set_Ioc H lemma integral_mono (h : f ≤ g) : ∫ u in a..b, f u ∂μ ≤ ∫ u in a..b, g u ∂μ := integral_mono_ae hab hf hg $ ae_of_all _ h omit hg hab lemma integral_mono_interval {c d} (hca : c ≤ a) (hab : a ≤ b) (hbd : b ≤ d) (hf : 0 ≤ᵐ[μ.restrict (Ioc c d)] f) (hfi : interval_integrable f μ c d): ∫ x in a..b, f x ∂μ ≤ ∫ x in c..d, f x ∂μ := begin rw [integral_of_le hab, integral_of_le (hca.trans (hab.trans hbd))], exact set_integral_mono_set hfi.1 hf (Ioc_subset_Ioc hca hbd).eventually_le end lemma abs_integral_mono_interval {c d } (h : Ι a b ⊆ Ι c d) (hf : 0 ≤ᵐ[μ.restrict (Ι c d)] f) (hfi : interval_integrable f μ c d) : |∫ x in a..b, f x ∂μ| ≤ |∫ x in c..d, f x ∂μ| := have hf' : 0 ≤ᵐ[μ.restrict (Ι a b)] f, from ae_mono (measure.restrict_mono h le_rfl) hf, calc |∫ x in a..b, f x ∂μ| = |∫ x in Ι a b, f x ∂μ| : abs_integral_eq_abs_integral_uIoc f ... = ∫ x in Ι a b, f x ∂μ : abs_of_nonneg (measure_theory.integral_nonneg_of_ae hf') ... ≤ ∫ x in Ι c d, f x ∂μ : set_integral_mono_set hfi.def hf h.eventually_le ... ≤ |∫ x in Ι c d, f x ∂μ| : le_abs_self _ ... = |∫ x in c..d, f x ∂μ| : (abs_integral_eq_abs_integral_uIoc f).symm end mono end /-! ### Fundamental theorem of calculus, part 1, for any measure In this section we prove a few lemmas that can be seen as versions of FTC-1 for interval integrals w.r.t. any measure. Many theorems are formulated for one or two pairs of filters related by `FTC_filter a l l'`. This typeclass has exactly four “real” instances: `(a, pure a, ⊥)`, `(a, 𝓝[≥] a, 𝓝[>] a)`, `(a, 𝓝[≤] a, 𝓝[≤] a)`, `(a, 𝓝 a, 𝓝 a)`, and two instances that are equal to the first and last “real” instances: `(a, 𝓝[{a}] a, ⊥)` and `(a, 𝓝[univ] a, 𝓝[univ] a)`. We use this approach to avoid repeating arguments in many very similar cases. Lean can automatically find both `a` and `l'` based on `l`. The most general theorem `measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae` can be seen as a generalization of lemma `integral_has_strict_fderiv_at` below which states strict differentiability of `∫ x in u..v, f x` in `(u, v)` at `(a, b)` for a measurable function `f` that is integrable on `a..b` and is continuous at `a` and `b`. The lemma is generalized in three directions: first, `measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae` deals with any locally finite measure `μ`; second, it works for one-sided limits/derivatives; third, it assumes only that `f` has finite limits almost surely at `a` and `b`. Namely, let `f` be a measurable function integrable on `a..b`. Let `(la, la')` be a pair of `FTC_filter`s around `a`; let `(lb, lb')` be a pair of `FTC_filter`s around `b`. Suppose that `f` has finite limits `ca` and `cb` at `la' ⊓ μ.ae` and `lb' ⊓ μ.ae`, respectively. Then `∫ x in va..vb, f x ∂μ - ∫ x in ua..ub, f x ∂μ = ∫ x in ub..vb, cb ∂μ - ∫ x in ua..va, ca ∂μ + o(‖∫ x in ua..va, (1:ℝ) ∂μ‖ + ‖∫ x in ub..vb, (1:ℝ) ∂μ‖)` as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`. This theorem is formulated with integral of constants instead of measures in the right hand sides for two reasons: first, this way we avoid `min`/`max` in the statements; second, often it is possible to write better `simp` lemmas for these integrals, see `integral_const` and `integral_const_of_cdf`. In the next subsection we apply this theorem to prove various theorems about differentiability of the integral w.r.t. Lebesgue measure. -/ /-- An auxiliary typeclass for the Fundamental theorem of calculus, part 1. It is used to formulate theorems that work simultaneously for left and right one-sided derivatives of `∫ x in u..v, f x`. -/ class FTC_filter (a : out_param ℝ) (outer : filter ℝ) (inner : out_param $ filter ℝ) extends tendsto_Ixx_class Ioc outer inner : Prop := (pure_le : pure a ≤ outer) (le_nhds : inner ≤ 𝓝 a) [meas_gen : is_measurably_generated inner] /- The `dangerous_instance` linter doesn't take `out_param`s into account, so it thinks that `FTC_filter.to_tendsto_Ixx_class` is dangerous. Disable this linter using `nolint`. -/ attribute [nolint dangerous_instance] FTC_filter.to_tendsto_Ixx_class namespace FTC_filter instance pure (a : ℝ) : FTC_filter a (pure a) ⊥ := { pure_le := le_rfl, le_nhds := bot_le } instance nhds_within_singleton (a : ℝ) : FTC_filter a (𝓝[{a}] a) ⊥ := by { rw [nhds_within, principal_singleton, inf_eq_right.2 (pure_le_nhds a)], apply_instance } lemma finite_at_inner {a : ℝ} (l : filter ℝ) {l'} [h : FTC_filter a l l'] {μ : measure ℝ} [is_locally_finite_measure μ] : μ.finite_at_filter l' := (μ.finite_at_nhds a).filter_mono h.le_nhds instance nhds (a : ℝ) : FTC_filter a (𝓝 a) (𝓝 a) := { pure_le := pure_le_nhds a, le_nhds := le_rfl } instance nhds_univ (a : ℝ) : FTC_filter a (𝓝[univ] a) (𝓝 a) := by { rw nhds_within_univ, apply_instance } instance nhds_left (a : ℝ) : FTC_filter a (𝓝[≤] a) (𝓝[≤] a) := { pure_le := pure_le_nhds_within right_mem_Iic, le_nhds := inf_le_left } instance nhds_right (a : ℝ) : FTC_filter a (𝓝[≥] a) (𝓝[>] a) := { pure_le := pure_le_nhds_within left_mem_Ici, le_nhds := inf_le_left } instance nhds_Icc {x a b : ℝ} [h : fact (x ∈ Icc a b)] : FTC_filter x (𝓝[Icc a b] x) (𝓝[Icc a b] x) := { pure_le := pure_le_nhds_within h.out, le_nhds := inf_le_left } instance nhds_uIcc {x a b : ℝ} [h : fact (x ∈ [a, b])] : FTC_filter x (𝓝[[a, b]] x) (𝓝[[a, b]] x) := by { haveI : fact (x ∈ set.Icc (min a b) (max a b)) := h, exact FTC_filter.nhds_Icc } end FTC_filter open asymptotics section variables {f : ℝ → E} {a b : ℝ} {c ca cb : E} {l l' la la' lb lb' : filter ℝ} {lt : filter ι} {μ : measure ℝ} {u v ua va ub vb : ι → ℝ} /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `tendsto_Ixx_class Ioc`. If `f` has a finite limit `c` at `l' ⊓ μ.ae`, where `μ` is a measure finite at `l'`, then `∫ x in u..v, f x ∂μ = ∫ x in u..v, c ∂μ + o(∫ x in u..v, 1 ∂μ)` as both `u` and `v` tend to `l`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae` for a version assuming `[FTC_filter a l l']` and `[is_locally_finite_measure μ]`. If `l` is one of `𝓝[≥] a`, `𝓝[≤] a`, `𝓝 a`, then it's easier to apply the non-primed version. The primed version also works, e.g., for `l = l' = at_top`. We use integrals of constants instead of measures because this way it is easier to formulate a statement that works in both cases `u ≤ v` and `v ≤ u`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae' [is_measurably_generated l'] [tendsto_Ixx_class Ioc l l'] (hfm : strongly_measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hl : μ.finite_at_filter l') (hu : tendsto u lt l) (hv : tendsto v lt l) : (λ t, ∫ x in u t..v t, f x ∂μ - ∫ x in u t..v t, c ∂μ) =o[lt] (λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) := begin have A := hf.integral_sub_linear_is_o_ae hfm hl (hu.Ioc hv), have B := hf.integral_sub_linear_is_o_ae hfm hl (hv.Ioc hu), simp only [integral_const'], convert (A.trans_le _).sub (B.trans_le _), { ext t, simp_rw [interval_integral, sub_smul], abel }, all_goals { intro t, cases le_total (u t) (v t) with huv huv; simp [huv] } end /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `tendsto_Ixx_class Ioc`. If `f` has a finite limit `c` at `l ⊓ μ.ae`, where `μ` is a measure finite at `l`, then `∫ x in u..v, f x ∂μ = μ (Ioc u v) • c + o(μ(Ioc u v))` as both `u` and `v` tend to `l` so that `u ≤ v`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_le` for a version assuming `[FTC_filter a l l']` and `[is_locally_finite_measure μ]`. If `l` is one of `𝓝[≥] a`, `𝓝[≤] a`, `𝓝 a`, then it's easier to apply the non-primed version. The primed version also works, e.g., for `l = l' = at_top`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_le' [is_measurably_generated l'] [tendsto_Ixx_class Ioc l l'] (hfm : strongly_measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hl : μ.finite_at_filter l') (hu : tendsto u lt l) (hv : tendsto v lt l) (huv : u ≤ᶠ[lt] v) : (λ t, ∫ x in u t..v t, f x ∂μ - (μ (Ioc (u t) (v t))).to_real • c) =o[lt] (λ t, (μ $ Ioc (u t) (v t)).to_real) := (measure_integral_sub_linear_is_o_of_tendsto_ae' hfm hf hl hu hv).congr' (huv.mono $ λ x hx, by simp [integral_const', hx]) (huv.mono $ λ x hx, by simp [integral_const', hx]) /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `tendsto_Ixx_class Ioc`. If `f` has a finite limit `c` at `l ⊓ μ.ae`, where `μ` is a measure finite at `l`, then `∫ x in u..v, f x ∂μ = -μ (Ioc v u) • c + o(μ(Ioc v u))` as both `u` and `v` tend to `l` so that `v ≤ u`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge` for a version assuming `[FTC_filter a l l']` and `[is_locally_finite_measure μ]`. If `l` is one of `𝓝[≥] a`, `𝓝[≤] a`, `𝓝 a`, then it's easier to apply the non-primed version. The primed version also works, e.g., for `l = l' = at_top`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge' [is_measurably_generated l'] [tendsto_Ixx_class Ioc l l'] (hfm : strongly_measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hl : μ.finite_at_filter l') (hu : tendsto u lt l) (hv : tendsto v lt l) (huv : v ≤ᶠ[lt] u) : (λ t, ∫ x in u t..v t, f x ∂μ + (μ (Ioc (v t) (u t))).to_real • c) =o[lt] (λ t, (μ $ Ioc (v t) (u t)).to_real) := (measure_integral_sub_linear_is_o_of_tendsto_ae_of_le' hfm hf hl hv hu huv).neg_left.congr_left $ λ t, by simp [integral_symm (u t), add_comm] section variables [is_locally_finite_measure μ] [FTC_filter a l l'] include a local attribute [instance] FTC_filter.meas_gen /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `[FTC_filter a l l']`; let `μ` be a locally finite measure. If `f` has a finite limit `c` at `l' ⊓ μ.ae`, then `∫ x in u..v, f x ∂μ = ∫ x in u..v, c ∂μ + o(∫ x in u..v, 1 ∂μ)` as both `u` and `v` tend to `l`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae'` for a version that also works, e.g., for `l = l' = at_top`. We use integrals of constants instead of measures because this way it is easier to formulate a statement that works in both cases `u ≤ v` and `v ≤ u`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae (hfm : strongly_measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt l) (hv : tendsto v lt l) : (λ t, ∫ x in u t..v t, f x ∂μ - ∫ x in u t..v t, c ∂μ) =o[lt] (λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) := measure_integral_sub_linear_is_o_of_tendsto_ae' hfm hf (FTC_filter.finite_at_inner l) hu hv /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `[FTC_filter a l l']`; let `μ` be a locally finite measure. If `f` has a finite limit `c` at `l' ⊓ μ.ae`, then `∫ x in u..v, f x ∂μ = μ (Ioc u v) • c + o(μ(Ioc u v))` as both `u` and `v` tend to `l`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_le'` for a version that also works, e.g., for `l = l' = at_top`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_le (hfm : strongly_measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt l) (hv : tendsto v lt l) (huv : u ≤ᶠ[lt] v) : (λ t, ∫ x in u t..v t, f x ∂μ - (μ (Ioc (u t) (v t))).to_real • c) =o[lt] (λ t, (μ $ Ioc (u t) (v t)).to_real) := measure_integral_sub_linear_is_o_of_tendsto_ae_of_le' hfm hf (FTC_filter.finite_at_inner l) hu hv huv /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `[FTC_filter a l l']`; let `μ` be a locally finite measure. If `f` has a finite limit `c` at `l' ⊓ μ.ae`, then `∫ x in u..v, f x ∂μ = -μ (Ioc v u) • c + o(μ(Ioc v u))` as both `u` and `v` tend to `l`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge'` for a version that also works, e.g., for `l = l' = at_top`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge (hfm : strongly_measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt l) (hv : tendsto v lt l) (huv : v ≤ᶠ[lt] u) : (λ t, ∫ x in u t..v t, f x ∂μ + (μ (Ioc (v t) (u t))).to_real • c) =o[lt] (λ t, (μ $ Ioc (v t) (u t)).to_real) := measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge' hfm hf (FTC_filter.finite_at_inner l) hu hv huv end local attribute [instance] FTC_filter.meas_gen variables [FTC_filter a la la'] [FTC_filter b lb lb'] [is_locally_finite_measure μ] /-- Fundamental theorem of calculus-1, strict derivative in both limits for a locally finite measure. Let `f` be a measurable function integrable on `a..b`. Let `(la, la')` be a pair of `FTC_filter`s around `a`; let `(lb, lb')` be a pair of `FTC_filter`s around `b`. Suppose that `f` has finite limits `ca` and `cb` at `la' ⊓ μ.ae` and `lb' ⊓ μ.ae`, respectively. Then `∫ x in va..vb, f x ∂μ - ∫ x in ua..ub, f x ∂μ = ∫ x in ub..vb, cb ∂μ - ∫ x in ua..va, ca ∂μ + o(‖∫ x in ua..va, (1:ℝ) ∂μ‖ + ‖∫ x in ub..vb, (1:ℝ) ∂μ‖)` as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`. -/ lemma measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae (hab : interval_integrable f μ a b) (hmeas_a : strongly_measurable_at_filter f la' μ) (hmeas_b : strongly_measurable_at_filter f lb' μ) (ha_lim : tendsto f (la' ⊓ μ.ae) (𝓝 ca)) (hb_lim : tendsto f (lb' ⊓ μ.ae) (𝓝 cb)) (hua : tendsto ua lt la) (hva : tendsto va lt la) (hub : tendsto ub lt lb) (hvb : tendsto vb lt lb) : (λ t, (∫ x in va t..vb t, f x ∂μ) - (∫ x in ua t..ub t, f x ∂μ) - (∫ x in ub t..vb t, cb ∂μ - ∫ x in ua t..va t, ca ∂μ)) =o[lt] (λ t, ‖∫ x in ua t..va t, (1:ℝ) ∂μ‖ + ‖∫ x in ub t..vb t, (1:ℝ) ∂μ‖) := begin refine ((measure_integral_sub_linear_is_o_of_tendsto_ae hmeas_a ha_lim hua hva).neg_left.add_add (measure_integral_sub_linear_is_o_of_tendsto_ae hmeas_b hb_lim hub hvb)).congr' _ eventually_eq.rfl, have A : ∀ᶠ t in lt, interval_integrable f μ (ua t) (va t) := ha_lim.eventually_interval_integrable_ae hmeas_a (FTC_filter.finite_at_inner la) hua hva, have A' : ∀ᶠ t in lt, interval_integrable f μ a (ua t) := ha_lim.eventually_interval_integrable_ae hmeas_a (FTC_filter.finite_at_inner la) (tendsto_const_pure.mono_right FTC_filter.pure_le) hua, have B : ∀ᶠ t in lt, interval_integrable f μ (ub t) (vb t) := hb_lim.eventually_interval_integrable_ae hmeas_b (FTC_filter.finite_at_inner lb) hub hvb, have B' : ∀ᶠ t in lt, interval_integrable f μ b (ub t) := hb_lim.eventually_interval_integrable_ae hmeas_b (FTC_filter.finite_at_inner lb) (tendsto_const_pure.mono_right FTC_filter.pure_le) hub, filter_upwards [A, A', B, B'] with _ ua_va a_ua ub_vb b_ub, rw [← integral_interval_sub_interval_comm'], { dsimp only [], abel, }, exacts [ub_vb, ua_va, b_ub.symm.trans $ hab.symm.trans a_ua] end /-- Fundamental theorem of calculus-1, strict derivative in right endpoint for a locally finite measure. Let `f` be a measurable function integrable on `a..b`. Let `(lb, lb')` be a pair of `FTC_filter`s around `b`. Suppose that `f` has a finite limit `c` at `lb' ⊓ μ.ae`. Then `∫ x in a..v, f x ∂μ - ∫ x in a..u, f x ∂μ = ∫ x in u..v, c ∂μ + o(∫ x in u..v, (1:ℝ) ∂μ)` as `u` and `v` tend to `lb`. -/ lemma measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right (hab : interval_integrable f μ a b) (hmeas : strongly_measurable_at_filter f lb' μ) (hf : tendsto f (lb' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt lb) (hv : tendsto v lt lb) : (λ t, ∫ x in a..v t, f x ∂μ - ∫ x in a..u t, f x ∂μ - ∫ x in u t..v t, c ∂μ) =o[lt] (λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) := by simpa using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae hab strongly_measurable_at_bot hmeas ((tendsto_bot : tendsto _ ⊥ (𝓝 0)).mono_left inf_le_left) hf (tendsto_const_pure : tendsto _ _ (pure a)) tendsto_const_pure hu hv /-- Fundamental theorem of calculus-1, strict derivative in left endpoint for a locally finite measure. Let `f` be a measurable function integrable on `a..b`. Let `(la, la')` be a pair of `FTC_filter`s around `a`. Suppose that `f` has a finite limit `c` at `la' ⊓ μ.ae`. Then `∫ x in v..b, f x ∂μ - ∫ x in u..b, f x ∂μ = -∫ x in u..v, c ∂μ + o(∫ x in u..v, (1:ℝ) ∂μ)` as `u` and `v` tend to `la`. -/ lemma measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_left (hab : interval_integrable f μ a b) (hmeas : strongly_measurable_at_filter f la' μ) (hf : tendsto f (la' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt la) (hv : tendsto v lt la) : (λ t, ∫ x in v t..b, f x ∂μ - ∫ x in u t..b, f x ∂μ + ∫ x in u t..v t, c ∂μ) =o[lt] (λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) := by simpa using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae hab hmeas strongly_measurable_at_bot hf ((tendsto_bot : tendsto _ ⊥ (𝓝 0)).mono_left inf_le_left) hu hv (tendsto_const_pure : tendsto _ _ (pure b)) tendsto_const_pure end /-! ### Fundamental theorem of calculus-1 for Lebesgue measure In this section we restate theorems from the previous section for Lebesgue measure. In particular, we prove that `∫ x in u..v, f x` is strictly differentiable in `(u, v)` at `(a, b)` provided that `f` is integrable on `a..b` and is continuous at `a` and `b`. -/ variables {f : ℝ → E} {c ca cb : E} {l l' la la' lb lb' : filter ℝ} {lt : filter ι} {a b z : ℝ} {u v ua ub va vb : ι → ℝ} [FTC_filter a la la'] [FTC_filter b lb lb'] /-! #### Auxiliary `is_o` statements In this section we prove several lemmas that can be interpreted as strict differentiability of `(u, v) ↦ ∫ x in u..v, f x ∂μ` in `u` and/or `v` at a filter. The statements use `is_o` because we have no definition of `has_strict_(f)deriv_at_filter` in the library. -/ /-- Fundamental theorem of calculus-1, local version. If `f` has a finite limit `c` almost surely at `l'`, where `(l, l')` is an `FTC_filter` pair around `a`, then `∫ x in u..v, f x ∂μ = (v - u) • c + o (v - u)` as both `u` and `v` tend to `l`. -/ lemma integral_sub_linear_is_o_of_tendsto_ae [FTC_filter a l l'] (hfm : strongly_measurable_at_filter f l') (hf : tendsto f (l' ⊓ volume.ae) (𝓝 c)) {u v : ι → ℝ} (hu : tendsto u lt l) (hv : tendsto v lt l) : (λ t, (∫ x in u t..v t, f x) - (v t - u t) • c) =o[lt] (v - u) := by simpa [integral_const] using measure_integral_sub_linear_is_o_of_tendsto_ae hfm hf hu hv /-- Fundamental theorem of calculus-1, strict differentiability at filter in both endpoints. If `f` is a measurable function integrable on `a..b`, `(la, la')` is an `FTC_filter` pair around `a`, and `(lb, lb')` is an `FTC_filter` pair around `b`, and `f` has finite limits `ca` and `cb` almost surely at `la'` and `lb'`, respectively, then `(∫ x in va..vb, f x) - ∫ x in ua..ub, f x = (vb - ub) • cb - (va - ua) • ca + o(‖va - ua‖ + ‖vb - ub‖)` as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`. This lemma could've been formulated using `has_strict_fderiv_at_filter` if we had this definition. -/ lemma integral_sub_integral_sub_linear_is_o_of_tendsto_ae (hab : interval_integrable f volume a b) (hmeas_a : strongly_measurable_at_filter f la') (hmeas_b : strongly_measurable_at_filter f lb') (ha_lim : tendsto f (la' ⊓ volume.ae) (𝓝 ca)) (hb_lim : tendsto f (lb' ⊓ volume.ae) (𝓝 cb)) (hua : tendsto ua lt la) (hva : tendsto va lt la) (hub : tendsto ub lt lb) (hvb : tendsto vb lt lb) : (λ t, (∫ x in va t..vb t, f x) - (∫ x in ua t..ub t, f x) - ((vb t - ub t) • cb - (va t - ua t) • ca)) =o[lt] (λ t, ‖va t - ua t‖ + ‖vb t - ub t‖) := by simpa [integral_const] using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae hab hmeas_a hmeas_b ha_lim hb_lim hua hva hub hvb /-- Fundamental theorem of calculus-1, strict differentiability at filter in both endpoints. If `f` is a measurable function integrable on `a..b`, `(lb, lb')` is an `FTC_filter` pair around `b`, and `f` has a finite limit `c` almost surely at `lb'`, then `(∫ x in a..v, f x) - ∫ x in a..u, f x = (v - u) • c + o(‖v - u‖)` as `u` and `v` tend to `lb`. This lemma could've been formulated using `has_strict_deriv_at_filter` if we had this definition. -/ lemma integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right (hab : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f lb') (hf : tendsto f (lb' ⊓ volume.ae) (𝓝 c)) (hu : tendsto u lt lb) (hv : tendsto v lt lb) : (λ t, (∫ x in a..v t, f x) - (∫ x in a..u t, f x) - (v t - u t) • c) =o[lt] (v - u) := by simpa only [integral_const, smul_eq_mul, mul_one] using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right hab hmeas hf hu hv /-- Fundamental theorem of calculus-1, strict differentiability at filter in both endpoints. If `f` is a measurable function integrable on `a..b`, `(la, la')` is an `FTC_filter` pair around `a`, and `f` has a finite limit `c` almost surely at `la'`, then `(∫ x in v..b, f x) - ∫ x in u..b, f x = -(v - u) • c + o(‖v - u‖)` as `u` and `v` tend to `la`. This lemma could've been formulated using `has_strict_deriv_at_filter` if we had this definition. -/ lemma integral_sub_integral_sub_linear_is_o_of_tendsto_ae_left (hab : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f la') (hf : tendsto f (la' ⊓ volume.ae) (𝓝 c)) (hu : tendsto u lt la) (hv : tendsto v lt la) : (λ t, (∫ x in v t..b, f x) - (∫ x in u t..b, f x) + (v t - u t) • c) =o[lt] (v - u) := by simpa only [integral_const, smul_eq_mul, mul_one] using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_left hab hmeas hf hu hv open continuous_linear_map (fst snd smul_right sub_apply smul_right_apply coe_fst' coe_snd' map_sub) /-! #### Strict differentiability In this section we prove that for a measurable function `f` integrable on `a..b`, * `integral_has_strict_fderiv_at_of_tendsto_ae`: the function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)` in the sense of strict differentiability provided that `f` tends to `ca` and `cb` almost surely as `x` tendsto to `a` and `b`, respectively; * `integral_has_strict_fderiv_at`: the function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • f b - u • f a` at `(a, b)` in the sense of strict differentiability provided that `f` is continuous at `a` and `b`; * `integral_has_strict_deriv_at_of_tendsto_ae_right`: the function `u ↦ ∫ x in a..u, f x` has derivative `c` at `b` in the sense of strict differentiability provided that `f` tends to `c` almost surely as `x` tends to `b`; * `integral_has_strict_deriv_at_right`: the function `u ↦ ∫ x in a..u, f x` has derivative `f b` at `b` in the sense of strict differentiability provided that `f` is continuous at `b`; * `integral_has_strict_deriv_at_of_tendsto_ae_left`: the function `u ↦ ∫ x in u..b, f x` has derivative `-c` at `a` in the sense of strict differentiability provided that `f` tends to `c` almost surely as `x` tends to `a`; * `integral_has_strict_deriv_at_left`: the function `u ↦ ∫ x in u..b, f x` has derivative `-f a` at `a` in the sense of strict differentiability provided that `f` is continuous at `a`. -/ /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has finite limits `ca` and `cb` almost surely as `x` tends to `a` and `b`, respectively, then `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)` in the sense of strict differentiability. -/ lemma integral_has_strict_fderiv_at_of_tendsto_ae (hf : interval_integrable f volume a b) (hmeas_a : strongly_measurable_at_filter f (𝓝 a)) (hmeas_b : strongly_measurable_at_filter f (𝓝 b)) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 cb)) : has_strict_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) (a, b) := begin have := integral_sub_integral_sub_linear_is_o_of_tendsto_ae hf hmeas_a hmeas_b ha hb ((continuous_fst.comp continuous_snd).tendsto ((a, b), (a, b))) ((continuous_fst.comp continuous_fst).tendsto ((a, b), (a, b))) ((continuous_snd.comp continuous_snd).tendsto ((a, b), (a, b))) ((continuous_snd.comp continuous_fst).tendsto ((a, b), (a, b))), refine (this.congr_left _).trans_is_O _, { intro x, simp [sub_smul] }, { exact is_O_fst_prod.norm_left.add is_O_snd_prod.norm_left } end /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a` and `b`, then `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)` in the sense of strict differentiability. -/ lemma integral_has_strict_fderiv_at (hf : interval_integrable f volume a b) (hmeas_a : strongly_measurable_at_filter f (𝓝 a)) (hmeas_b : strongly_measurable_at_filter f (𝓝 b)) (ha : continuous_at f a) (hb : continuous_at f b) : has_strict_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a)) (a, b) := integral_has_strict_fderiv_at_of_tendsto_ae hf hmeas_a hmeas_b (ha.mono_left inf_le_left) (hb.mono_left inf_le_left) /-- **First Fundamental Theorem of Calculus**: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `c` at `b` in the sense of strict differentiability. -/ lemma integral_has_strict_deriv_at_of_tendsto_ae_right (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 b)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 c)) : has_strict_deriv_at (λ u, ∫ x in a..u, f x) c b := integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right hf hmeas hb continuous_at_snd continuous_at_fst /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `f b` at `b` in the sense of strict differentiability. -/ lemma integral_has_strict_deriv_at_right (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 b)) (hb : continuous_at f b) : has_strict_deriv_at (λ u, ∫ x in a..u, f x) (f b) b := integral_has_strict_deriv_at_of_tendsto_ae_right hf hmeas (hb.mono_left inf_le_left) /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-c` at `a` in the sense of strict differentiability. -/ lemma integral_has_strict_deriv_at_of_tendsto_ae_left (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 a)) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 c)) : has_strict_deriv_at (λ u, ∫ x in u..b, f x) (-c) a := by simpa only [← integral_symm] using (integral_has_strict_deriv_at_of_tendsto_ae_right hf.symm hmeas ha).neg /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-f a` at `a` in the sense of strict differentiability. -/ lemma integral_has_strict_deriv_at_left (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 a)) (ha : continuous_at f a) : has_strict_deriv_at (λ u, ∫ x in u..b, f x) (-f a) a := by simpa only [← integral_symm] using (integral_has_strict_deriv_at_right hf.symm hmeas ha).neg /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is continuous, then `u ↦ ∫ x in a..u, f x` has derivative `f b` at `b` in the sense of strict differentiability. -/ lemma _root_.continuous.integral_has_strict_deriv_at {f : ℝ → E} (hf : continuous f) (a b : ℝ) : has_strict_deriv_at (λ u, ∫ (x : ℝ) in a..u, f x) (f b) b := integral_has_strict_deriv_at_right (hf.interval_integrable _ _) (hf.strongly_measurable_at_filter _ _) hf.continuous_at /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is continuous, then the derivative of `u ↦ ∫ x in a..u, f x` at `b` is `f b`. -/ lemma _root_.continuous.deriv_integral (f : ℝ → E) (hf : continuous f) (a b : ℝ) : deriv (λ u, ∫ (x : ℝ) in a..u, f x) b = f b := (hf.integral_has_strict_deriv_at a b).has_deriv_at.deriv /-! #### Fréchet differentiability In this subsection we restate results from the previous subsection in terms of `has_fderiv_at`, `has_deriv_at`, `fderiv`, and `deriv`. -/ /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has finite limits `ca` and `cb` almost surely as `x` tends to `a` and `b`, respectively, then `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)`. -/ lemma integral_has_fderiv_at_of_tendsto_ae (hf : interval_integrable f volume a b) (hmeas_a : strongly_measurable_at_filter f (𝓝 a)) (hmeas_b : strongly_measurable_at_filter f (𝓝 b)) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 cb)) : has_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) (a, b) := (integral_has_strict_fderiv_at_of_tendsto_ae hf hmeas_a hmeas_b ha hb).has_fderiv_at /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a` and `b`, then `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)`. -/ lemma integral_has_fderiv_at (hf : interval_integrable f volume a b) (hmeas_a : strongly_measurable_at_filter f (𝓝 a)) (hmeas_b : strongly_measurable_at_filter f (𝓝 b)) (ha : continuous_at f a) (hb : continuous_at f b) : has_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a)) (a, b) := (integral_has_strict_fderiv_at hf hmeas_a hmeas_b ha hb).has_fderiv_at /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has finite limits `ca` and `cb` almost surely as `x` tends to `a` and `b`, respectively, then `fderiv` derivative of `(u, v) ↦ ∫ x in u..v, f x` at `(a, b)` equals `(u, v) ↦ v • cb - u • ca`. -/ lemma fderiv_integral_of_tendsto_ae (hf : interval_integrable f volume a b) (hmeas_a : strongly_measurable_at_filter f (𝓝 a)) (hmeas_b : strongly_measurable_at_filter f (𝓝 b)) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 cb)) : fderiv ℝ (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) (a, b) = (snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca := (integral_has_fderiv_at_of_tendsto_ae hf hmeas_a hmeas_b ha hb).fderiv /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a` and `b`, then `fderiv` derivative of `(u, v) ↦ ∫ x in u..v, f x` at `(a, b)` equals `(u, v) ↦ v • cb - u • ca`. -/ lemma fderiv_integral (hf : interval_integrable f volume a b) (hmeas_a : strongly_measurable_at_filter f (𝓝 a)) (hmeas_b : strongly_measurable_at_filter f (𝓝 b)) (ha : continuous_at f a) (hb : continuous_at f b) : fderiv ℝ (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) (a, b) = (snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a) := (integral_has_fderiv_at hf hmeas_a hmeas_b ha hb).fderiv /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `c` at `b`. -/ lemma integral_has_deriv_at_of_tendsto_ae_right (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 b)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 c)) : has_deriv_at (λ u, ∫ x in a..u, f x) c b := (integral_has_strict_deriv_at_of_tendsto_ae_right hf hmeas hb).has_deriv_at /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `f b` at `b`. -/ lemma integral_has_deriv_at_right (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 b)) (hb : continuous_at f b) : has_deriv_at (λ u, ∫ x in a..u, f x) (f b) b := (integral_has_strict_deriv_at_right hf hmeas hb).has_deriv_at /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` has a finite limit `c` almost surely at `b`, then the derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `c`. -/ lemma deriv_integral_of_tendsto_ae_right (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 b)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 c)) : deriv (λ u, ∫ x in a..u, f x) b = c := (integral_has_deriv_at_of_tendsto_ae_right hf hmeas hb).deriv /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `b`, then the derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `f b`. -/ lemma deriv_integral_right (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 b)) (hb : continuous_at f b) : deriv (λ u, ∫ x in a..u, f x) b = f b := (integral_has_deriv_at_right hf hmeas hb).deriv /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-c` at `a`. -/ lemma integral_has_deriv_at_of_tendsto_ae_left (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 a)) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 c)) : has_deriv_at (λ u, ∫ x in u..b, f x) (-c) a := (integral_has_strict_deriv_at_of_tendsto_ae_left hf hmeas ha).has_deriv_at /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-f a` at `a`. -/ lemma integral_has_deriv_at_left (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 a)) (ha : continuous_at f a) : has_deriv_at (λ u, ∫ x in u..b, f x) (-f a) a := (integral_has_strict_deriv_at_left hf hmeas ha).has_deriv_at /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` has a finite limit `c` almost surely at `a`, then the derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-c`. -/ lemma deriv_integral_of_tendsto_ae_left (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 a)) (hb : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 c)) : deriv (λ u, ∫ x in u..b, f x) a = -c := (integral_has_deriv_at_of_tendsto_ae_left hf hmeas hb).deriv /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a`, then the derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-f a`. -/ lemma deriv_integral_left (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 a)) (hb : continuous_at f a) : deriv (λ u, ∫ x in u..b, f x) a = -f a := (integral_has_deriv_at_left hf hmeas hb).deriv /-! #### One-sided derivatives -/ /-- Let `f` be a measurable function integrable on `a..b`. The function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` within `s × t` at `(a, b)`, where `s ∈ {Iic a, {a}, Ici a, univ}` and `t ∈ {Iic b, {b}, Ici b, univ}` provided that `f` tends to `ca` and `cb` almost surely at the filters `la` and `lb` from the following table. | `s` | `la` | `t` | `lb` | | ------- | ---- | --- | ---- | | `Iic a` | `𝓝[≤] a` | `Iic b` | `𝓝[≤] b` | | `Ici a` | `𝓝[>] a` | `Ici b` | `𝓝[>] b` | | `{a}` | `⊥` | `{b}` | `⊥` | | `univ` | `𝓝 a` | `univ` | `𝓝 b` | -/ lemma integral_has_fderiv_within_at_of_tendsto_ae (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) la] [FTC_filter b (𝓝[t] b) lb] (hmeas_a : strongly_measurable_at_filter f la) (hmeas_b : strongly_measurable_at_filter f lb) (ha : tendsto f (la ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (lb ⊓ volume.ae) (𝓝 cb)) : has_fderiv_within_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) (s ×ˢ t) (a, b) := begin rw [has_fderiv_within_at, nhds_within_prod_eq], have := integral_sub_integral_sub_linear_is_o_of_tendsto_ae hf hmeas_a hmeas_b ha hb (tendsto_const_pure.mono_right FTC_filter.pure_le : tendsto _ _ (𝓝[s] a)) tendsto_fst (tendsto_const_pure.mono_right FTC_filter.pure_le : tendsto _ _ (𝓝[t] b)) tendsto_snd, refine (this.congr_left _).trans_is_O _, { intro x, simp [sub_smul] }, { exact is_O_fst_prod.norm_left.add is_O_snd_prod.norm_left } end /-- Let `f` be a measurable function integrable on `a..b`. The function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • f b - u • f a` within `s × t` at `(a, b)`, where `s ∈ {Iic a, {a}, Ici a, univ}` and `t ∈ {Iic b, {b}, Ici b, univ}` provided that `f` tends to `f a` and `f b` at the filters `la` and `lb` from the following table. In most cases this assumption is definitionally equal `continuous_at f _` or `continuous_within_at f _ _`. | `s` | `la` | `t` | `lb` | | ------- | ---- | --- | ---- | | `Iic a` | `𝓝[≤] a` | `Iic b` | `𝓝[≤] b` | | `Ici a` | `𝓝[>] a` | `Ici b` | `𝓝[>] b` | | `{a}` | `⊥` | `{b}` | `⊥` | | `univ` | `𝓝 a` | `univ` | `𝓝 b` | -/ lemma integral_has_fderiv_within_at (hf : interval_integrable f volume a b) (hmeas_a : strongly_measurable_at_filter f la) (hmeas_b : strongly_measurable_at_filter f lb) {s t : set ℝ} [FTC_filter a (𝓝[s] a) la] [FTC_filter b (𝓝[t] b) lb] (ha : tendsto f la (𝓝 $ f a)) (hb : tendsto f lb (𝓝 $ f b)) : has_fderiv_within_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a)) (s ×ˢ t) (a, b) := integral_has_fderiv_within_at_of_tendsto_ae hf hmeas_a hmeas_b (ha.mono_left inf_le_left) (hb.mono_left inf_le_left) /-- An auxiliary tactic closing goals `unique_diff_within_at ℝ s a` where `s ∈ {Iic a, Ici a, univ}`. -/ meta def unique_diff_within_at_Ici_Iic_univ : tactic unit := `[apply_rules [unique_diff_on.unique_diff_within_at, unique_diff_on_Ici, unique_diff_on_Iic, left_mem_Ici, right_mem_Iic, unique_diff_within_at_univ]] /-- Let `f` be a measurable function integrable on `a..b`. Choose `s ∈ {Iic a, Ici a, univ}` and `t ∈ {Iic b, Ici b, univ}`. Suppose that `f` tends to `ca` and `cb` almost surely at the filters `la` and `lb` from the table below. Then `fderiv_within ℝ (λ p, ∫ x in p.1..p.2, f x) (s ×ˢ t)` is equal to `(u, v) ↦ u • cb - v • ca`. | `s` | `la` | `t` | `lb` | | ------- | ---- | --- | ---- | | `Iic a` | `𝓝[≤] a` | `Iic b` | `𝓝[≤] b` | | `Ici a` | `𝓝[>] a` | `Ici b` | `𝓝[>] b` | | `{a}` | `⊥` | `{b}` | `⊥` | | `univ` | `𝓝 a` | `univ` | `𝓝 b` | -/ lemma fderiv_within_integral_of_tendsto_ae (hf : interval_integrable f volume a b) (hmeas_a : strongly_measurable_at_filter f la) (hmeas_b : strongly_measurable_at_filter f lb) {s t : set ℝ} [FTC_filter a (𝓝[s] a) la] [FTC_filter b (𝓝[t] b) lb] (ha : tendsto f (la ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (lb ⊓ volume.ae) (𝓝 cb)) (hs : unique_diff_within_at ℝ s a . unique_diff_within_at_Ici_Iic_univ) (ht : unique_diff_within_at ℝ t b . unique_diff_within_at_Ici_Iic_univ) : fderiv_within ℝ (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) (s ×ˢ t) (a, b) = ((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) := (integral_has_fderiv_within_at_of_tendsto_ae hf hmeas_a hmeas_b ha hb).fderiv_within $ hs.prod ht /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely as `x` tends to `b` from the right or from the left, then `u ↦ ∫ x in a..u, f x` has right (resp., left) derivative `c` at `b`. -/ lemma integral_has_deriv_within_at_of_tendsto_ae_right (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)] (hmeas : strongly_measurable_at_filter f (𝓝[t] b)) (hb : tendsto f (𝓝[t] b ⊓ volume.ae) (𝓝 c)) : has_deriv_within_at (λ u, ∫ x in a..u, f x) c s b := integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right hf hmeas hb (tendsto_const_pure.mono_right FTC_filter.pure_le) tendsto_id /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous from the left or from the right at `b`, then `u ↦ ∫ x in a..u, f x` has left (resp., right) derivative `f b` at `b`. -/ lemma integral_has_deriv_within_at_right (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)] (hmeas : strongly_measurable_at_filter f (𝓝[t] b)) (hb : continuous_within_at f t b) : has_deriv_within_at (λ u, ∫ x in a..u, f x) (f b) s b := integral_has_deriv_within_at_of_tendsto_ae_right hf hmeas (hb.mono_left inf_le_left) /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely as `x` tends to `b` from the right or from the left, then the right (resp., left) derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `c`. -/ lemma deriv_within_integral_of_tendsto_ae_right (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)] (hmeas: strongly_measurable_at_filter f (𝓝[t] b)) (hb : tendsto f (𝓝[t] b ⊓ volume.ae) (𝓝 c)) (hs : unique_diff_within_at ℝ s b . unique_diff_within_at_Ici_Iic_univ) : deriv_within (λ u, ∫ x in a..u, f x) s b = c := (integral_has_deriv_within_at_of_tendsto_ae_right hf hmeas hb).deriv_within hs /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous on the right or on the left at `b`, then the right (resp., left) derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `f b`. -/ lemma deriv_within_integral_right (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)] (hmeas : strongly_measurable_at_filter f (𝓝[t] b)) (hb : continuous_within_at f t b) (hs : unique_diff_within_at ℝ s b . unique_diff_within_at_Ici_Iic_univ) : deriv_within (λ u, ∫ x in a..u, f x) s b = f b := (integral_has_deriv_within_at_right hf hmeas hb).deriv_within hs /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely as `x` tends to `a` from the right or from the left, then `u ↦ ∫ x in u..b, f x` has right (resp., left) derivative `-c` at `a`. -/ lemma integral_has_deriv_within_at_of_tendsto_ae_left (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)] (hmeas : strongly_measurable_at_filter f (𝓝[t] a)) (ha : tendsto f (𝓝[t] a ⊓ volume.ae) (𝓝 c)) : has_deriv_within_at (λ u, ∫ x in u..b, f x) (-c) s a := by { simp only [integral_symm b], exact (integral_has_deriv_within_at_of_tendsto_ae_right hf.symm hmeas ha).neg } /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous from the left or from the right at `a`, then `u ↦ ∫ x in u..b, f x` has left (resp., right) derivative `-f a` at `a`. -/ lemma integral_has_deriv_within_at_left (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)] (hmeas : strongly_measurable_at_filter f (𝓝[t] a)) (ha : continuous_within_at f t a) : has_deriv_within_at (λ u, ∫ x in u..b, f x) (-f a) s a := integral_has_deriv_within_at_of_tendsto_ae_left hf hmeas (ha.mono_left inf_le_left) /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely as `x` tends to `a` from the right or from the left, then the right (resp., left) derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-c`. -/ lemma deriv_within_integral_of_tendsto_ae_left (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)] (hmeas : strongly_measurable_at_filter f (𝓝[t] a)) (ha : tendsto f (𝓝[t] a ⊓ volume.ae) (𝓝 c)) (hs : unique_diff_within_at ℝ s a . unique_diff_within_at_Ici_Iic_univ) : deriv_within (λ u, ∫ x in u..b, f x) s a = -c := (integral_has_deriv_within_at_of_tendsto_ae_left hf hmeas ha).deriv_within hs /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous on the right or on the left at `a`, then the right (resp., left) derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-f a`. -/ lemma deriv_within_integral_left (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)] (hmeas : strongly_measurable_at_filter f (𝓝[t] a)) (ha : continuous_within_at f t a) (hs : unique_diff_within_at ℝ s a . unique_diff_within_at_Ici_Iic_univ) : deriv_within (λ u, ∫ x in u..b, f x) s a = -f a := (integral_has_deriv_within_at_left hf hmeas ha).deriv_within hs /-- The integral of a continuous function is differentiable on a real set `s`. -/ theorem differentiable_on_integral_of_continuous {s : set ℝ} (hintg : ∀ x ∈ s, interval_integrable f volume a x) (hcont : continuous f) : differentiable_on ℝ (λ u, ∫ x in a..u, f x) s := λ y hy, (integral_has_deriv_at_right (hintg y hy) hcont.ae_strongly_measurable.strongly_measurable_at_filter hcont.continuous_at) .differentiable_at.differentiable_within_at /-! ### Fundamental theorem of calculus, part 2 This section contains theorems pertaining to FTC-2 for interval integrals, i.e., the assertion that `∫ x in a..b, f' x = f b - f a` under suitable assumptions. The most classical version of this theorem assumes that `f'` is continuous. However, this is unnecessarily strong: the result holds if `f'` is just integrable. We prove the strong version, following [Rudin, *Real and Complex Analysis* (Theorem 7.21)][rudin2006real]. The proof is first given for real-valued functions, and then deduced for functions with a general target space. For a real-valued function `g`, it suffices to show that `g b - g a ≤ (∫ x in a..b, g' x) + ε` for all positive `ε`. To prove this, choose a lower-semicontinuous function `G'` with `g' < G'` and with integral close to that of `g'` (its existence is guaranteed by the Vitali-Carathéodory theorem). It satisfies `g t - g a ≤ ∫ x in a..t, G' x` for all `t ∈ [a, b]`: this inequality holds at `a`, and if it holds at `t` then it holds for `u` close to `t` on its right, as the left hand side increases by `g u - g t ∼ (u -t) g' t`, while the right hand side increases by `∫ x in t..u, G' x` which is roughly at least `∫ x in t..u, G' t = (u - t) G' t`, by lower semicontinuity. As `g' t < G' t`, this gives the conclusion. One can therefore push progressively this inequality to the right until the point `b`, where it gives the desired conclusion. -/ variables {g' g φ : ℝ → ℝ} /-- Hard part of FTC-2 for integrable derivatives, real-valued functions: one has `g b - g a ≤ ∫ y in a..b, g' y` when `g'` is integrable. Auxiliary lemma in the proof of `integral_eq_sub_of_has_deriv_right_of_le`. We give the slightly more general version that `g b - g a ≤ ∫ y in a..b, φ y` when `g' ≤ φ` and `φ` is integrable (even if `g'` is not known to be integrable). Version assuming that `g` is differentiable on `[a, b)`. -/ lemma sub_le_integral_of_has_deriv_right_of_le_Ico (hab : a ≤ b) (hcont : continuous_on g (Icc a b)) (hderiv : ∀ x ∈ Ico a b, has_deriv_within_at g (g' x) (Ioi x) x) (φint : integrable_on φ (Icc a b)) (hφg : ∀ x ∈ Ico a b, g' x ≤ φ x) : g b - g a ≤ ∫ y in a..b, φ y := begin refine le_of_forall_pos_le_add (λ ε εpos, _), -- Bound from above `g'` by a lower-semicontinuous function `G'`. rcases exists_lt_lower_semicontinuous_integral_lt φ φint εpos with ⟨G', f_lt_G', G'cont, G'int, G'lt_top, hG'⟩, -- we will show by "induction" that `g t - g a ≤ ∫ u in a..t, G' u` for all `t ∈ [a, b]`. set s := {t | g t - g a ≤ ∫ u in a..t, (G' u).to_real} ∩ Icc a b, -- the set `s` of points where this property holds is closed. have s_closed : is_closed s, { have : continuous_on (λ t, (g t - g a, ∫ u in a..t, (G' u).to_real)) (Icc a b), { rw ← uIcc_of_le hab at G'int ⊢ hcont, exact (hcont.sub continuous_on_const).prod (continuous_on_primitive_interval G'int) }, simp only [s, inter_comm], exact this.preimage_closed_of_closed is_closed_Icc order_closed_topology.is_closed_le' }, have main : Icc a b ⊆ {t | g t - g a ≤ ∫ u in a..t, (G' u).to_real }, { -- to show that the set `s` is all `[a, b]`, it suffices to show that any point `t` in `s` -- with `t < b` admits another point in `s` slightly to its right -- (this is a sort of real induction). apply s_closed.Icc_subset_of_forall_exists_gt (by simp only [integral_same, mem_set_of_eq, sub_self]) (λ t ht v t_lt_v, _), obtain ⟨y, g'_lt_y', y_lt_G'⟩ : ∃ (y : ℝ), (g' t : ereal) < y ∧ (y : ereal) < G' t := ereal.lt_iff_exists_real_btwn.1 ((ereal.coe_le_coe_iff.2 (hφg t ht.2)).trans_lt (f_lt_G' t)), -- bound from below the increase of `∫ x in a..u, G' x` on the right of `t`, using the lower -- semicontinuity of `G'`. have I1 : ∀ᶠ u in 𝓝[>] t, (u - t) * y ≤ ∫ w in t..u, (G' w).to_real, { have B : ∀ᶠ u in 𝓝 t, (y : ereal) < G' u := G'cont.lower_semicontinuous_at _ _ y_lt_G', rcases mem_nhds_iff_exists_Ioo_subset.1 B with ⟨m, M, ⟨hm, hM⟩, H⟩, have : Ioo t (min M b) ∈ 𝓝[>] t := mem_nhds_within_Ioi_iff_exists_Ioo_subset.2 ⟨min M b, by simp only [hM, ht.right.right, lt_min_iff, mem_Ioi, and_self], subset.refl _⟩, filter_upwards [this] with u hu, have I : Icc t u ⊆ Icc a b := Icc_subset_Icc ht.2.1 (hu.2.le.trans (min_le_right _ _)), calc (u - t) * y = ∫ v in Icc t u, y : by simp only [hu.left.le, measure_theory.integral_const, algebra.id.smul_eq_mul, sub_nonneg, measurable_set.univ, real.volume_Icc, measure.restrict_apply, univ_inter, ennreal.to_real_of_real] ... ≤ ∫ w in t..u, (G' w).to_real : begin rw [interval_integral.integral_of_le hu.1.le, ← integral_Icc_eq_integral_Ioc], apply set_integral_mono_ae_restrict, { simp only [integrable_on_const, real.volume_Icc, ennreal.of_real_lt_top, or_true] }, { exact integrable_on.mono_set G'int I }, { have C1 : ∀ᵐ (x : ℝ) ∂volume.restrict (Icc t u), G' x < ∞ := ae_mono (measure.restrict_mono I le_rfl) G'lt_top, have C2 : ∀ᵐ (x : ℝ) ∂volume.restrict (Icc t u), x ∈ Icc t u := ae_restrict_mem measurable_set_Icc, filter_upwards [C1, C2] with x G'x hx, apply ereal.coe_le_coe_iff.1, have : x ∈ Ioo m M, by simp only [hm.trans_le hx.left, (hx.right.trans_lt hu.right).trans_le (min_le_left M b), mem_Ioo, and_self], convert le_of_lt (H this), exact ereal.coe_to_real G'x.ne (ne_bot_of_gt (f_lt_G' x)) } end }, -- bound from above the increase of `g u - g a` on the right of `t`, using the derivative at `t` have I2 : ∀ᶠ u in 𝓝[>] t, g u - g t ≤ (u - t) * y, { have g'_lt_y : g' t < y := ereal.coe_lt_coe_iff.1 g'_lt_y', filter_upwards [(hderiv t ⟨ht.2.1, ht.2.2⟩).limsup_slope_le' (not_mem_Ioi.2 le_rfl) g'_lt_y, self_mem_nhds_within] with u hu t_lt_u, have := mul_le_mul_of_nonneg_left hu.le (sub_pos.2 t_lt_u).le, rwa [← smul_eq_mul, sub_smul_slope] at this }, -- combine the previous two bounds to show that `g u - g a` increases less quickly than -- `∫ x in a..u, G' x`. have I3 : ∀ᶠ u in 𝓝[>] t, g u - g t ≤ ∫ w in t..u, (G' w).to_real, { filter_upwards [I1, I2] with u hu1 hu2 using hu2.trans hu1, }, have I4 : ∀ᶠ u in 𝓝[>] t, u ∈ Ioc t (min v b), { refine mem_nhds_within_Ioi_iff_exists_Ioc_subset.2 ⟨min v b, _, subset.refl _⟩, simp only [lt_min_iff, mem_Ioi], exact ⟨t_lt_v, ht.2.2⟩ }, -- choose a point `x` slightly to the right of `t` which satisfies the above bound rcases (I3.and I4).exists with ⟨x, hx, h'x⟩, -- we check that it belongs to `s`, essentially by construction refine ⟨x, _, Ioc_subset_Ioc le_rfl (min_le_left _ _) h'x⟩, calc g x - g a = (g t - g a) + (g x - g t) : by abel ... ≤ (∫ w in a..t, (G' w).to_real) + ∫ w in t..x, (G' w).to_real : add_le_add ht.1 hx ... = ∫ w in a..x, (G' w).to_real : begin apply integral_add_adjacent_intervals, { rw interval_integrable_iff_integrable_Ioc_of_le ht.2.1, exact integrable_on.mono_set G'int (Ioc_subset_Icc_self.trans (Icc_subset_Icc le_rfl ht.2.2.le)) }, { rw interval_integrable_iff_integrable_Ioc_of_le h'x.1.le, apply integrable_on.mono_set G'int, refine Ioc_subset_Icc_self.trans (Icc_subset_Icc ht.2.1 (h'x.2.trans (min_le_right _ _))) } end }, -- now that we know that `s` contains `[a, b]`, we get the desired result by applying this to `b`. calc g b - g a ≤ ∫ y in a..b, (G' y).to_real : main (right_mem_Icc.2 hab) ... ≤ (∫ y in a..b, φ y) + ε : begin convert hG'.le; { rw interval_integral.integral_of_le hab, simp only [integral_Icc_eq_integral_Ioc', real.volume_singleton] }, end end /-- Hard part of FTC-2 for integrable derivatives, real-valued functions: one has `g b - g a ≤ ∫ y in a..b, g' y` when `g'` is integrable. Auxiliary lemma in the proof of `integral_eq_sub_of_has_deriv_right_of_le`. We give the slightly more general version that `g b - g a ≤ ∫ y in a..b, φ y` when `g' ≤ φ` and `φ` is integrable (even if `g'` is not known to be integrable). Version assuming that `g` is differentiable on `(a, b)`. -/ lemma sub_le_integral_of_has_deriv_right_of_le (hab : a ≤ b) (hcont : continuous_on g (Icc a b)) (hderiv : ∀ x ∈ Ioo a b, has_deriv_within_at g (g' x) (Ioi x) x) (φint : integrable_on φ (Icc a b)) (hφg : ∀ x ∈ Ioo a b, g' x ≤ φ x) : g b - g a ≤ ∫ y in a..b, φ y := begin -- This follows from the version on a closed-open interval (applied to `[t, b)` for `t` close to -- `a`) and a continuity argument. obtain rfl|a_lt_b := hab.eq_or_lt, { simp }, set s := {t | g b - g t ≤ ∫ u in t..b, φ u} ∩ Icc a b, have s_closed : is_closed s, { have : continuous_on (λ t, (g b - g t, ∫ u in t..b, φ u)) (Icc a b), { rw ← uIcc_of_le hab at ⊢ hcont φint, exact (continuous_on_const.sub hcont).prod (continuous_on_primitive_interval_left φint) }, simp only [s, inter_comm], exact this.preimage_closed_of_closed is_closed_Icc is_closed_le_prod, }, have A : closure (Ioc a b) ⊆ s, { apply s_closed.closure_subset_iff.2, assume t ht, refine ⟨_, ⟨ht.1.le, ht.2⟩⟩, exact sub_le_integral_of_has_deriv_right_of_le_Ico ht.2 (hcont.mono (Icc_subset_Icc ht.1.le le_rfl)) (λ x hx, hderiv x ⟨ht.1.trans_le hx.1, hx.2⟩) (φint.mono_set (Icc_subset_Icc ht.1.le le_rfl)) (λ x hx, hφg x ⟨ht.1.trans_le hx.1, hx.2⟩) }, rw closure_Ioc a_lt_b.ne at A, exact (A (left_mem_Icc.2 hab)).1, end /-- Auxiliary lemma in the proof of `integral_eq_sub_of_has_deriv_right_of_le`. -/ lemma integral_le_sub_of_has_deriv_right_of_le (hab : a ≤ b) (hcont : continuous_on g (Icc a b)) (hderiv : ∀ x ∈ Ioo a b, has_deriv_within_at g (g' x) (Ioi x) x) (φint : integrable_on φ (Icc a b)) (hφg : ∀ x ∈ Ioo a b, φ x ≤ g' x) : ∫ y in a..b, φ y ≤ g b - g a := begin rw ← neg_le_neg_iff, convert sub_le_integral_of_has_deriv_right_of_le hab hcont.neg (λ x hx, (hderiv x hx).neg) φint.neg (λ x hx, neg_le_neg (hφg x hx)), { abel }, { simp only [← integral_neg], refl }, end /-- Auxiliary lemma in the proof of `integral_eq_sub_of_has_deriv_right_of_le`: real version -/ lemma integral_eq_sub_of_has_deriv_right_of_le_real (hab : a ≤ b) (hcont : continuous_on g (Icc a b)) (hderiv : ∀ x ∈ Ioo a b, has_deriv_within_at g (g' x) (Ioi x) x) (g'int : integrable_on g' (Icc a b)) : ∫ y in a..b, g' y = g b - g a := le_antisymm (integral_le_sub_of_has_deriv_right_of_le hab hcont hderiv g'int (λ x hx, le_rfl)) (sub_le_integral_of_has_deriv_right_of_le hab hcont hderiv g'int (λ x hx, le_rfl)) variable {f' : ℝ → E} /-- **Fundamental theorem of calculus-2**: If `f : ℝ → E` is continuous on `[a, b]` (where `a ≤ b`) and has a right derivative at `f' x` for all `x` in `(a, b)`, and `f'` is integrable on `[a, b]`, then `∫ y in a..b, f' y` equals `f b - f a`. -/ theorem integral_eq_sub_of_has_deriv_right_of_le (hab : a ≤ b) (hcont : continuous_on f (Icc a b)) (hderiv : ∀ x ∈ Ioo a b, has_deriv_within_at f (f' x) (Ioi x) x) (f'int : interval_integrable f' volume a b) : ∫ y in a..b, f' y = f b - f a := begin refine (normed_space.eq_iff_forall_dual_eq ℝ).2 (λ g, _), rw [← g.interval_integral_comp_comm f'int, g.map_sub], exact integral_eq_sub_of_has_deriv_right_of_le_real hab (g.continuous.comp_continuous_on hcont) (λ x hx, g.has_fderiv_at.comp_has_deriv_within_at x (hderiv x hx)) (g.integrable_comp ((interval_integrable_iff_integrable_Icc_of_le hab).1 f'int)) end /-- Fundamental theorem of calculus-2: If `f : ℝ → E` is continuous on `[a, b]` and has a right derivative at `f' x` for all `x` in `[a, b)`, and `f'` is integrable on `[a, b]` then `∫ y in a..b, f' y` equals `f b - f a`. -/ theorem integral_eq_sub_of_has_deriv_right (hcont : continuous_on f (uIcc a b)) (hderiv : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_within_at f (f' x) (Ioi x) x) (hint : interval_integrable f' volume a b) : ∫ y in a..b, f' y = f b - f a := begin cases le_total a b with hab hab, { simp only [uIcc_of_le, min_eq_left, max_eq_right, hab] at hcont hderiv hint, apply integral_eq_sub_of_has_deriv_right_of_le hab hcont hderiv hint }, { simp only [uIcc_of_ge, min_eq_right, max_eq_left, hab] at hcont hderiv, rw [integral_symm, integral_eq_sub_of_has_deriv_right_of_le hab hcont hderiv hint.symm, neg_sub] } end /-- Fundamental theorem of calculus-2: If `f : ℝ → E` is continuous on `[a, b]` (where `a ≤ b`) and has a derivative at `f' x` for all `x` in `(a, b)`, and `f'` is integrable on `[a, b]`, then `∫ y in a..b, f' y` equals `f b - f a`. -/ theorem integral_eq_sub_of_has_deriv_at_of_le (hab : a ≤ b) (hcont : continuous_on f (Icc a b)) (hderiv : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hint : interval_integrable f' volume a b) : ∫ y in a..b, f' y = f b - f a := integral_eq_sub_of_has_deriv_right_of_le hab hcont (λ x hx, (hderiv x hx).has_deriv_within_at) hint /-- Fundamental theorem of calculus-2: If `f : ℝ → E` has a derivative at `f' x` for all `x` in `[a, b]` and `f'` is integrable on `[a, b]`, then `∫ y in a..b, f' y` equals `f b - f a`. -/ theorem integral_eq_sub_of_has_deriv_at (hderiv : ∀ x ∈ uIcc a b, has_deriv_at f (f' x) x) (hint : interval_integrable f' volume a b) : ∫ y in a..b, f' y = f b - f a := integral_eq_sub_of_has_deriv_right (has_deriv_at.continuous_on hderiv) (λ x hx, (hderiv _ (mem_Icc_of_Ioo hx)).has_deriv_within_at) hint theorem integral_eq_sub_of_has_deriv_at_of_tendsto (hab : a < b) {fa fb} (hderiv : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hint : interval_integrable f' volume a b) (ha : tendsto f (𝓝[>] a) (𝓝 fa)) (hb : tendsto f (𝓝[<] b) (𝓝 fb)) : ∫ y in a..b, f' y = fb - fa := begin set F : ℝ → E := update (update f a fa) b fb, have Fderiv : ∀ x ∈ Ioo a b, has_deriv_at F (f' x) x, { refine λ x hx, (hderiv x hx).congr_of_eventually_eq _, filter_upwards [Ioo_mem_nhds hx.1 hx.2] with _ hy, simp only [F], rw [update_noteq hy.2.ne, update_noteq hy.1.ne'], }, have hcont : continuous_on F (Icc a b), { rw [continuous_on_update_iff, continuous_on_update_iff, Icc_diff_right, Ico_diff_left], refine ⟨⟨λ z hz, (hderiv z hz).continuous_at.continuous_within_at, _⟩, _⟩, { exact λ _, ha.mono_left (nhds_within_mono _ Ioo_subset_Ioi_self) }, { rintro -, refine (hb.congr' _).mono_left (nhds_within_mono _ Ico_subset_Iio_self), filter_upwards [Ioo_mem_nhds_within_Iio (right_mem_Ioc.2 hab)] with _ hz using (update_noteq hz.1.ne' _ _).symm } }, simpa [F, hab.ne, hab.ne'] using integral_eq_sub_of_has_deriv_at_of_le hab.le hcont Fderiv hint end /-- Fundamental theorem of calculus-2: If `f : ℝ → E` is differentiable at every `x` in `[a, b]` and its derivative is integrable on `[a, b]`, then `∫ y in a..b, deriv f y` equals `f b - f a`. -/ theorem integral_deriv_eq_sub (hderiv : ∀ x ∈ uIcc a b, differentiable_at ℝ f x) (hint : interval_integrable (deriv f) volume a b) : ∫ y in a..b, deriv f y = f b - f a := integral_eq_sub_of_has_deriv_at (λ x hx, (hderiv x hx).has_deriv_at) hint theorem integral_deriv_eq_sub' (f) (hderiv : deriv f = f') (hdiff : ∀ x ∈ uIcc a b, differentiable_at ℝ f x) (hcont : continuous_on f' (uIcc a b)) : ∫ y in a..b, f' y = f b - f a := begin rw [← hderiv, integral_deriv_eq_sub hdiff], rw hderiv, exact hcont.interval_integrable end /-! ### Automatic integrability for nonnegative derivatives -/ /-- When the right derivative of a function is nonnegative, then it is automatically integrable. -/ lemma integrable_on_deriv_right_of_nonneg (hab : a ≤ b) (hcont : continuous_on g (Icc a b)) (hderiv : ∀ x ∈ Ioo a b, has_deriv_within_at g (g' x) (Ioi x) x) (g'pos : ∀ x ∈ Ioo a b, 0 ≤ g' x) : integrable_on g' (Ioc a b) := begin rw integrable_on_Ioc_iff_integrable_on_Ioo, have meas_g' : ae_measurable g' (volume.restrict (Ioo a b)), { apply (ae_measurable_deriv_within_Ioi g _).congr, refine (ae_restrict_mem measurable_set_Ioo).mono (λ x hx, _), exact (hderiv x hx).deriv_within (unique_diff_within_at_Ioi _) }, suffices H : ∫⁻ x in Ioo a b, ‖g' x‖₊ ≤ ennreal.of_real (g b - g a), from ⟨meas_g'.ae_strongly_measurable, H.trans_lt ennreal.of_real_lt_top⟩, by_contra' H, obtain ⟨f, fle, fint, hf⟩ : ∃ (f : simple_func ℝ ℝ≥0), (∀ x, f x ≤ ‖g' x‖₊) ∧ ∫⁻ (x : ℝ) in Ioo a b, f x < ∞ ∧ ennreal.of_real (g b - g a) < ∫⁻ (x : ℝ) in Ioo a b, f x := exists_lt_lintegral_simple_func_of_lt_lintegral H, let F : ℝ → ℝ := coe ∘ f, have intF : integrable_on F (Ioo a b), { refine ⟨f.measurable.coe_nnreal_real.ae_strongly_measurable, _⟩, simpa only [has_finite_integral, nnreal.nnnorm_eq] using fint }, have A : ∫⁻ (x : ℝ) in Ioo a b, f x = ennreal.of_real (∫ x in Ioo a b, F x) := lintegral_coe_eq_integral _ intF, rw A at hf, have B : ∫ (x : ℝ) in Ioo a b, F x ≤ g b - g a, { rw [← integral_Ioc_eq_integral_Ioo, ← interval_integral.integral_of_le hab], apply integral_le_sub_of_has_deriv_right_of_le hab hcont hderiv _ (λ x hx, _), { rwa integrable_on_Icc_iff_integrable_on_Ioo }, { convert nnreal.coe_le_coe.2 (fle x), simp only [real.norm_of_nonneg (g'pos x hx), coe_nnnorm] } }, exact lt_irrefl _ (hf.trans_le (ennreal.of_real_le_of_real B)), end /-- When the derivative of a function is nonnegative, then it is automatically integrable, Ioc version. -/ lemma integrable_on_deriv_of_nonneg (hab : a ≤ b) (hcont : continuous_on g (Icc a b)) (hderiv : ∀ x ∈ Ioo a b, has_deriv_at g (g' x) x) (g'pos : ∀ x ∈ Ioo a b, 0 ≤ g' x) : integrable_on g' (Ioc a b) := integrable_on_deriv_right_of_nonneg hab hcont (λ x hx, (hderiv x hx).has_deriv_within_at) g'pos /-- When the derivative of a function is nonnegative, then it is automatically integrable, interval version. -/ theorem interval_integrable_deriv_of_nonneg (hcont : continuous_on g (uIcc a b)) (hderiv : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_at g (g' x) x) (hpos : ∀ x ∈ Ioo (min a b) (max a b), 0 ≤ g' x) : interval_integrable g' volume a b := begin cases le_total a b with hab hab, { simp only [uIcc_of_le, min_eq_left, max_eq_right, hab, interval_integrable, hab, Ioc_eq_empty_of_le, integrable_on_empty, and_true] at hcont hderiv hpos ⊢, exact integrable_on_deriv_of_nonneg hab hcont hderiv hpos, }, { simp only [uIcc_of_ge, min_eq_right, max_eq_left, hab, interval_integrable, Ioc_eq_empty_of_le, integrable_on_empty, true_and] at hcont hderiv hpos ⊢, exact integrable_on_deriv_of_nonneg hab hcont hderiv hpos } end /-! ### Integration by parts -/ section parts variables [normed_ring A] [normed_algebra ℝ A] [complete_space A] theorem integral_deriv_mul_eq_sub {u v u' v' : ℝ → A} (hu : ∀ x ∈ uIcc a b, has_deriv_at u (u' x) x) (hv : ∀ x ∈ uIcc a b, has_deriv_at v (v' x) x) (hu' : interval_integrable u' volume a b) (hv' : interval_integrable v' volume a b) : ∫ x in a..b, u' x * v x + u x * v' x = u b * v b - u a * v a := integral_eq_sub_of_has_deriv_at (λ x hx, (hu x hx).mul (hv x hx)) $ (hu'.mul_continuous_on (has_deriv_at.continuous_on hv)).add (hv'.continuous_on_mul ((has_deriv_at.continuous_on hu))) theorem integral_mul_deriv_eq_deriv_mul {u v u' v' : ℝ → A} (hu : ∀ x ∈ uIcc a b, has_deriv_at u (u' x) x) (hv : ∀ x ∈ uIcc a b, has_deriv_at v (v' x) x) (hu' : interval_integrable u' volume a b) (hv' : interval_integrable v' volume a b) : ∫ x in a..b, u x * v' x = u b * v b - u a * v a - ∫ x in a..b, u' x * v x := begin rw [← integral_deriv_mul_eq_sub hu hv hu' hv', ← integral_sub], { exact integral_congr (λ x hx, by simp only [add_sub_cancel']) }, { exact ((hu'.mul_continuous_on (has_deriv_at.continuous_on hv)).add (hv'.continuous_on_mul (has_deriv_at.continuous_on hu))) }, { exact hu'.mul_continuous_on (has_deriv_at.continuous_on hv) }, end end parts /-! ### Integration by substitution / Change of variables -/ section smul /-- Change of variables, general form. If `f` is continuous on `[a, b]` and has right-derivative `f'` in `(a, b)`, `g` is continuous on `f '' (a, b)` and integrable on `f '' [a, b]`, and `f' x • (g ∘ f) x` is integrable on `[a, b]`, then we can substitute `u = f x` to get `∫ x in a..b, f' x • (g ∘ f) x = ∫ u in f a..f b, g u`. -/ theorem integral_comp_smul_deriv''' {f f' : ℝ → ℝ} {g : ℝ → E} (hf : continuous_on f [a, b]) (hff' : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_within_at f (f' x) (Ioi x) x) (hg_cont : continuous_on g (f '' Ioo (min a b) (max a b))) (hg1 : integrable_on g (f '' [a, b]) ) (hg2 : integrable_on (λ x, f'(x) • (g ∘ f) x) [a, b]) : ∫ x in a..b, f' x • (g ∘ f) x = ∫ u in f a..f b, g u := begin rw [hf.image_uIcc, ←interval_integrable_iff'] at hg1, have h_cont : continuous_on (λ u, ∫ t in f a..f u, g t) [a, b], { refine (continuous_on_primitive_interval' hg1 _).comp hf _, { rw ← hf.image_uIcc, exact mem_image_of_mem f left_mem_uIcc }, { rw ← hf.image_uIcc, exact maps_to_image _ _ } }, have h_der : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_within_at (λ u, ∫ t in f a..f u, g t) (f' x • ((g ∘ f) x)) (Ioi x) x, { intros x hx, obtain ⟨c, hc⟩ := nonempty_Ioo.mpr hx.1, obtain ⟨d, hd⟩ := nonempty_Ioo.mpr hx.2, have cdsub : [c, d] ⊆ Ioo (min a b) (max a b), { rw uIcc_of_le (hc.2.trans hd.1).le, exact Icc_subset_Ioo hc.1 hd.2 }, replace hg_cont := hg_cont.mono (image_subset f cdsub), let J := [Inf (f '' [c, d]), Sup (f '' [c, d])], have hJ : f '' [c, d] = J := (hf.mono (cdsub.trans Ioo_subset_Icc_self)).image_uIcc, rw hJ at hg_cont, have h2x : f x ∈ J, { rw ←hJ, exact mem_image_of_mem _ (mem_uIcc_of_le hc.2.le hd.1.le), }, have h2g : interval_integrable g volume (f a) (f x), { refine hg1.mono_set _, rw ←hf.image_uIcc, exact hf.surj_on_uIcc left_mem_uIcc (Ioo_subset_Icc_self hx) }, have h3g := hg_cont.strongly_measurable_at_filter_nhds_within measurable_set_Icc (f x), haveI : fact (f x ∈ J) := ⟨h2x⟩, have : has_deriv_within_at (λ u, ∫ x in f a..u, g x) (g (f x)) J (f x) := interval_integral.integral_has_deriv_within_at_right h2g h3g (hg_cont (f x) h2x), refine (this.scomp x ((hff' x hx).Ioo_of_Ioi hd.1) _).Ioi_of_Ioo hd.1, rw ←hJ, refine (maps_to_image _ _).mono _ subset.rfl, exact Ioo_subset_Icc_self.trans ((Icc_subset_Icc_left hc.2.le).trans Icc_subset_uIcc) }, rw ←interval_integrable_iff' at hg2, simp_rw [integral_eq_sub_of_has_deriv_right h_cont h_der hg2, integral_same, sub_zero], end /-- Change of variables for continuous integrands. If `f` is continuous on `[a, b]` and has continuous right-derivative `f'` in `(a, b)`, and `g` is continuous on `f '' [a, b]` then we can substitute `u = f x` to get `∫ x in a..b, f' x • (g ∘ f) x = ∫ u in f a..f b, g u`. -/ theorem integral_comp_smul_deriv'' {f f' : ℝ → ℝ} {g : ℝ → E} (hf : continuous_on f [a, b]) (hff' : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_within_at f (f' x) (Ioi x) x) (hf' : continuous_on f' [a, b]) (hg : continuous_on g (f '' [a, b])) : ∫ x in a..b, f' x • (g ∘ f) x = ∫ u in f a..f b, g u := begin refine integral_comp_smul_deriv''' hf hff' (hg.mono $ image_subset _ Ioo_subset_Icc_self) _ (hf'.smul (hg.comp hf $ subset_preimage_image f _)).integrable_on_Icc, rw hf.image_uIcc at hg ⊢, exact hg.integrable_on_Icc, end /-- Change of variables. If `f` is has continuous derivative `f'` on `[a, b]`, and `g` is continuous on `f '' [a, b]`, then we can substitute `u = f x` to get `∫ x in a..b, f' x • (g ∘ f) x = ∫ u in f a..f b, g u`. Compared to `interval_integral.integral_comp_smul_deriv` we only require that `g` is continuous on `f '' [a, b]`. -/ theorem integral_comp_smul_deriv' {f f' : ℝ → ℝ} {g : ℝ → E} (h : ∀ x ∈ uIcc a b, has_deriv_at f (f' x) x) (h' : continuous_on f' (uIcc a b)) (hg : continuous_on g (f '' [a, b])) : ∫ x in a..b, f' x • (g ∘ f) x = ∫ x in f a..f b, g x := integral_comp_smul_deriv'' (λ x hx, (h x hx).continuous_at.continuous_within_at) (λ x hx, (h x $ Ioo_subset_Icc_self hx).has_deriv_within_at) h' hg /-- Change of variables, most common version. If `f` is has continuous derivative `f'` on `[a, b]`, and `g` is continuous, then we can substitute `u = f x` to get `∫ x in a..b, f' x • (g ∘ f) x = ∫ u in f a..f b, g u`. -/ theorem integral_comp_smul_deriv {f f' : ℝ → ℝ} {g : ℝ → E} (h : ∀ x ∈ uIcc a b, has_deriv_at f (f' x) x) (h' : continuous_on f' (uIcc a b)) (hg : continuous g) : ∫ x in a..b, f' x • (g ∘ f) x = ∫ x in f a..f b, g x := integral_comp_smul_deriv' h h' hg.continuous_on theorem integral_deriv_comp_smul_deriv' {f f' : ℝ → ℝ} {g g' : ℝ → E} (hf : continuous_on f [a, b]) (hff' : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_within_at f (f' x) (Ioi x) x) (hf' : continuous_on f' [a, b]) (hg : continuous_on g [f a, f b]) (hgg' : ∀ x ∈ Ioo (min (f a) (f b)) (max (f a) (f b)), has_deriv_within_at g (g' x) (Ioi x) x) (hg' : continuous_on g' (f '' [a, b])) : ∫ x in a..b, f' x • (g' ∘ f) x = (g ∘ f) b - (g ∘ f) a := begin rw [integral_comp_smul_deriv'' hf hff' hf' hg', integral_eq_sub_of_has_deriv_right hg hgg' (hg'.mono _).interval_integrable], exact intermediate_value_uIcc hf end theorem integral_deriv_comp_smul_deriv {f f' : ℝ → ℝ} {g g' : ℝ → E} (hf : ∀ x ∈ uIcc a b, has_deriv_at f (f' x) x) (hg : ∀ x ∈ uIcc a b, has_deriv_at g (g' (f x)) (f x)) (hf' : continuous_on f' (uIcc a b)) (hg' : continuous g') : ∫ x in a..b, f' x • (g' ∘ f) x = (g ∘ f) b - (g ∘ f) a := integral_eq_sub_of_has_deriv_at (λ x hx, (hg x hx).scomp x $ hf x hx) (hf'.smul (hg'.comp_continuous_on $ has_deriv_at.continuous_on hf)).interval_integrable end smul section mul /-- Change of variables, general form for scalar functions. If `f` is continuous on `[a, b]` and has continuous right-derivative `f'` in `(a, b)`, `g` is continuous on `f '' (a, b)` and integrable on `f '' [a, b]`, and `(g ∘ f) x * f' x` is integrable on `[a, b]`, then we can substitute `u = f x` to get `∫ x in a..b, (g ∘ f) x * f' x = ∫ u in f a..f b, g u`. -/ theorem integral_comp_mul_deriv''' {a b : ℝ} {f f' : ℝ → ℝ} {g : ℝ → ℝ} (hf : continuous_on f [a, b]) (hff' : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_within_at f (f' x) (Ioi x) x) (hg_cont : continuous_on g (f '' Ioo (min a b) (max a b))) (hg1 : integrable_on g (f '' [a, b]) ) (hg2 : integrable_on (λ x, (g ∘ f) x * f' x) [a, b]) : ∫ x in a..b, (g ∘ f) x * f' x = ∫ u in f a..f b, g u := begin have hg2' : integrable_on (λ x, f' x • (g ∘ f) x) [a, b] := by simpa [mul_comm] using hg2, simpa [mul_comm] using integral_comp_smul_deriv''' hf hff' hg_cont hg1 hg2', end /-- Change of variables for continuous integrands. If `f` is continuous on `[a, b]` and has continuous right-derivative `f'` in `(a, b)`, and `g` is continuous on `f '' [a, b]` then we can substitute `u = f x` to get `∫ x in a..b, (g ∘ f) x * f' x = ∫ u in f a..f b, g u`. -/ theorem integral_comp_mul_deriv'' {f f' g : ℝ → ℝ} (hf : continuous_on f [a, b]) (hff' : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_within_at f (f' x) (Ioi x) x) (hf' : continuous_on f' [a, b]) (hg : continuous_on g (f '' [a, b])) : ∫ x in a..b, (g ∘ f) x * f' x = ∫ u in f a..f b, g u := by simpa [mul_comm] using integral_comp_smul_deriv'' hf hff' hf' hg /-- Change of variables. If `f` is has continuous derivative `f'` on `[a, b]`, and `g` is continuous on `f '' [a, b]`, then we can substitute `u = f x` to get `∫ x in a..b, (g ∘ f) x * f' x = ∫ u in f a..f b, g u`. Compared to `interval_integral.integral_comp_mul_deriv` we only require that `g` is continuous on `f '' [a, b]`. -/ theorem integral_comp_mul_deriv' {f f' g : ℝ → ℝ} (h : ∀ x ∈ uIcc a b, has_deriv_at f (f' x) x) (h' : continuous_on f' (uIcc a b)) (hg : continuous_on g (f '' [a, b])) : ∫ x in a..b, (g ∘ f) x * f' x = ∫ x in f a..f b, g x := by simpa [mul_comm] using integral_comp_smul_deriv' h h' hg /-- Change of variables, most common version. If `f` is has continuous derivative `f'` on `[a, b]`, and `g` is continuous, then we can substitute `u = f x` to get `∫ x in a..b, (g ∘ f) x * f' x = ∫ u in f a..f b, g u`. -/ theorem integral_comp_mul_deriv {f f' g : ℝ → ℝ} (h : ∀ x ∈ uIcc a b, has_deriv_at f (f' x) x) (h' : continuous_on f' (uIcc a b)) (hg : continuous g) : ∫ x in a..b, (g ∘ f) x * f' x = ∫ x in f a..f b, g x := integral_comp_mul_deriv' h h' hg.continuous_on theorem integral_deriv_comp_mul_deriv' {f f' g g' : ℝ → ℝ} (hf : continuous_on f [a, b]) (hff' : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_within_at f (f' x) (Ioi x) x) (hf' : continuous_on f' [a, b]) (hg : continuous_on g [f a, f b]) (hgg' : ∀ x ∈ Ioo (min (f a) (f b)) (max (f a) (f b)), has_deriv_within_at g (g' x) (Ioi x) x) (hg' : continuous_on g' (f '' [a, b])) : ∫ x in a..b, (g' ∘ f) x * f' x = (g ∘ f) b - (g ∘ f) a := by simpa [mul_comm] using integral_deriv_comp_smul_deriv' hf hff' hf' hg hgg' hg' theorem integral_deriv_comp_mul_deriv {f f' g g' : ℝ → ℝ} (hf : ∀ x ∈ uIcc a b, has_deriv_at f (f' x) x) (hg : ∀ x ∈ uIcc a b, has_deriv_at g (g' (f x)) (f x)) (hf' : continuous_on f' (uIcc a b)) (hg' : continuous g') : ∫ x in a..b, (g' ∘ f) x * f' x = (g ∘ f) b - (g ∘ f) a := by simpa [mul_comm] using integral_deriv_comp_smul_deriv hf hg hf' hg' end mul end interval_integral
670e60ee0f952a8499815f3533a805de478709cd
60bf3fa4185ec5075eaea4384181bfbc7e1dc319
/src/game/order/level05.lean
2fc01b53e0a1ddbf74c7f39fe2beef7975afd1ff
[ "Apache-2.0" ]
permissive
anrddh/real-number-game
660f1127d03a78fd35986c771d65c3132c5f4025
c708c4e02ec306c657e1ea67862177490db041b0
refs/heads/master
1,668,214,277,092
1,593,105,075,000
1,593,105,075,000
264,269,218
0
0
null
1,589,567,264,000
1,589,567,264,000
null
UTF-8
Lean
false
false
1,267
lean
import data.real.basic import game.order.level04 namespace xena -- hide /- # Chapter 2 : Order ## Level 5 Another well-known property of the absolute value. -/ notation `|` x `|` := abs x -- hide /- Lemma For any two real numbers $a$ and $b$, we have that $$| |a| - |b| | ≤ |a - b|$$. -/ theorem abs_of_sub_le_abs (a b : ℝ) : | |a| - |b| | ≤ |a - b| := begin have h1 : a = (a - b) + b, norm_num, have h2 : |a| = |(a-b) + b|, rw h1, simp, have h3 : |(a-b) + b | ≤ |a-b| + |b|, exact abs_add _ _, rw ← h2 at h3, have h4a : |a| - |b| ≤ |a - b|, linarith, clear h1 h2 h3, have h1 : b = (b - a) + a, norm_num, have h2 : |b| = |(b-a) + a|, rw h1, simp, have h3 : |(b-a) + a | ≤ |b-a| + |a|, exact abs_add _ _, rw ← h2 at h3, have h4b : |b| - |a| ≤ |b - a|, linarith, clear h1 h2 h3, have h1 := eq.symm ( abs_neg (a-b) ), have h2 : -(a-b) = b - a, norm_num, rw h2 at h1, clear h2, rw ← h1 at h4b, clear h1, have H : max ( |a| - |b| ) ( |b| - |a| ) ≤ | a - b |, simp, split, exact h4a, exact h4b, unfold abs, unfold abs at H, have G: -(max a (-a) - max b (-b)) = max b (-b) - max a (-a), norm_num, rw G, exact H, done, end end xena --hide
f553cff521477e11ab5684e4f4a2513f8be06c95
32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7
/tests/lean/run/typeclass_loop.lean
6483f497da41e55e1185981efb5e012140ff980f
[ "Apache-2.0" ]
permissive
walterhu1015/lean4
b2c71b688975177402758924eaa513475ed6ce72
2214d81e84646a905d0b20b032c89caf89c737ad
refs/heads/master
1,671,342,096,906
1,599,695,985,000
1,599,695,985,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,308
lean
set_option trace.class_instances true example (M : Type → Type) [Monad M] : ExceptT Unit (ReaderT Unit (StateT Unit M)) Unit := do ctx ← read; pure () /- ... [class_instances] (1) ?x_8 : HasMonadLift (ReaderT ?x_10 (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT (OptionT List)))))))))))))))))))))))))))))) (ExceptT Unit (ReaderT Unit (StateT Unit M))) := @ExceptT.exceptTOfExcept ?x_82 ?x_83 ?x_84 failed is_def_eq ... error: maximum class-instance resolution depth has been reached -/
ffddaccb936cdcd73048bf13aea47d315a2e7685
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/mv_polynomial/rename_auto.lean
3556ce0bde4f8425397bbd3097dda564d6306c1f
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
7,360
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Johan Commelin, Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.mv_polynomial.basic import Mathlib.PostPort universes u_1 u_2 u_4 u_5 u_3 namespace Mathlib /-! # Renaming variables of polynomials This file establishes the `rename` operation on multivariate polynomials, which modifies the set of variables. ## Main declarations * `mv_polynomial.rename` ## Notation As in other polynomial files, we typically use the notation: + `σ τ α : Type*` (indexing the variables) + `R S : Type*` `[comm_semiring R]` `[comm_semiring S]` (the coefficients) + `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set. This will give rise to a monomial in `mv_polynomial σ R` which mathematicians might call `X^s` + `r : R` elements of the coefficient ring + `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians + `p : mv_polynomial σ α` -/ namespace mv_polynomial /-- Rename all the variables in a multivariable polynomial. -/ def rename {σ : Type u_1} {τ : Type u_2} {R : Type u_4} [comm_semiring R] (f : σ → τ) : alg_hom R (mv_polynomial σ R) (mv_polynomial τ R) := aeval (X ∘ f) @[simp] theorem rename_C {σ : Type u_1} {τ : Type u_2} {R : Type u_4} [comm_semiring R] (f : σ → τ) (r : R) : coe_fn (rename f) (coe_fn C r) = coe_fn C r := eval₂_C (algebra_map R (mv_polynomial τ R)) (fun (n : σ) => function.comp X f n) r @[simp] theorem rename_X {σ : Type u_1} {τ : Type u_2} {R : Type u_4} [comm_semiring R] (f : σ → τ) (i : σ) : coe_fn (rename f) (X i) = X (f i) := eval₂_X (algebra_map R (mv_polynomial τ R)) (fun (n : σ) => function.comp X f n) i theorem map_rename {σ : Type u_1} {τ : Type u_2} {R : Type u_4} {S : Type u_5} [comm_semiring R] [comm_semiring S] (f : R →+* S) (g : σ → τ) (p : mv_polynomial σ R) : coe_fn (map f) (coe_fn (rename g) p) = coe_fn (rename g) (coe_fn (map f) p) := sorry @[simp] theorem rename_rename {σ : Type u_1} {τ : Type u_2} {α : Type u_3} {R : Type u_4} [comm_semiring R] (f : σ → τ) (g : τ → α) (p : mv_polynomial σ R) : coe_fn (rename g) (coe_fn (rename f) p) = coe_fn (rename (g ∘ f)) p := sorry @[simp] theorem rename_id {σ : Type u_1} {R : Type u_4} [comm_semiring R] (p : mv_polynomial σ R) : coe_fn (rename id) p = p := eval₂_eta p theorem rename_monomial {σ : Type u_1} {τ : Type u_2} {R : Type u_4} [comm_semiring R] (f : σ → τ) (d : σ →₀ ℕ) (r : R) : coe_fn (rename f) (monomial d r) = monomial (finsupp.map_domain f d) r := sorry theorem rename_eq {σ : Type u_1} {τ : Type u_2} {R : Type u_4} [comm_semiring R] (f : σ → τ) (p : mv_polynomial σ R) : coe_fn (rename f) p = finsupp.map_domain (finsupp.map_domain f) p := sorry theorem rename_injective {σ : Type u_1} {τ : Type u_2} {R : Type u_4} [comm_semiring R] (f : σ → τ) (hf : function.injective f) : function.injective ⇑(rename f) := sorry theorem eval₂_rename {σ : Type u_1} {τ : Type u_2} {R : Type u_4} {S : Type u_5} [comm_semiring R] [comm_semiring S] (f : R →+* S) (k : σ → τ) (g : τ → S) (p : mv_polynomial σ R) : eval₂ f g (coe_fn (rename k) p) = eval₂ f (g ∘ k) p := sorry theorem eval₂_hom_rename {σ : Type u_1} {τ : Type u_2} {R : Type u_4} {S : Type u_5} [comm_semiring R] [comm_semiring S] (f : R →+* S) (k : σ → τ) (g : τ → S) (p : mv_polynomial σ R) : coe_fn (eval₂_hom f g) (coe_fn (rename k) p) = coe_fn (eval₂_hom f (g ∘ k)) p := eval₂_rename f k (fun (n : τ) => g n) p theorem aeval_rename {σ : Type u_1} {τ : Type u_2} {R : Type u_4} {S : Type u_5} [comm_semiring R] [comm_semiring S] (k : σ → τ) (g : τ → S) (p : mv_polynomial σ R) [algebra R S] : coe_fn (aeval g) (coe_fn (rename k) p) = coe_fn (aeval (g ∘ k)) p := eval₂_hom_rename (algebra_map R S) k (fun (n : τ) => g n) p theorem rename_eval₂ {σ : Type u_1} {τ : Type u_2} {R : Type u_4} [comm_semiring R] (k : σ → τ) (p : mv_polynomial σ R) (g : τ → mv_polynomial σ R) : coe_fn (rename k) (eval₂ C (g ∘ k) p) = eval₂ C (⇑(rename k) ∘ g) (coe_fn (rename k) p) := sorry theorem rename_prodmk_eval₂ {σ : Type u_1} {τ : Type u_2} {R : Type u_4} [comm_semiring R] (p : mv_polynomial σ R) (j : τ) (g : σ → mv_polynomial σ R) : coe_fn (rename (Prod.mk j)) (eval₂ C g p) = eval₂ C (fun (x : σ) => coe_fn (rename (Prod.mk j)) (g x)) p := sorry theorem eval₂_rename_prodmk {σ : Type u_1} {τ : Type u_2} {R : Type u_4} {S : Type u_5} [comm_semiring R] [comm_semiring S] (f : R →+* S) (g : σ × τ → S) (i : σ) (p : mv_polynomial τ R) : eval₂ f g (coe_fn (rename (Prod.mk i)) p) = eval₂ f (fun (j : τ) => g (i, j)) p := sorry theorem eval_rename_prodmk {σ : Type u_1} {τ : Type u_2} {R : Type u_4} [comm_semiring R] (g : σ × τ → R) (i : σ) (p : mv_polynomial τ R) : coe_fn (eval g) (coe_fn (rename (Prod.mk i)) p) = coe_fn (eval fun (j : τ) => g (i, j)) p := eval₂_rename_prodmk (ring_hom.id R) (fun (n : σ × τ) => g n) i p /-- Every polynomial is a polynomial in finitely many variables. -/ theorem exists_finset_rename {σ : Type u_1} {R : Type u_4} [comm_semiring R] (p : mv_polynomial σ R) : ∃ (s : finset σ), ∃ (q : mv_polynomial (Subtype fun (x : σ) => x ∈ s) R), p = coe_fn (rename coe) q := sorry /-- Every polynomial is a polynomial in finitely many variables. -/ theorem exists_fin_rename {σ : Type u_1} {R : Type u_4} [comm_semiring R] (p : mv_polynomial σ R) : ∃ (n : ℕ), ∃ (f : fin n → σ), ∃ (hf : function.injective f), ∃ (q : mv_polynomial (fin n) R), p = coe_fn (rename f) q := sorry theorem eval₂_cast_comp {σ : Type u_1} {τ : Type u_2} {R : Type u_4} [comm_semiring R] (f : σ → τ) (c : ℤ →+* R) (g : τ → R) (p : mv_polynomial σ ℤ) : eval₂ c (g ∘ f) p = eval₂ c g (coe_fn (rename f) p) := sorry @[simp] theorem coeff_rename_map_domain {σ : Type u_1} {τ : Type u_2} {R : Type u_4} [comm_semiring R] (f : σ → τ) (hf : function.injective f) (φ : mv_polynomial σ R) (d : σ →₀ ℕ) : coeff (finsupp.map_domain f d) (coe_fn (rename f) φ) = coeff d φ := sorry theorem coeff_rename_eq_zero {σ : Type u_1} {τ : Type u_2} {R : Type u_4} [comm_semiring R] (f : σ → τ) (φ : mv_polynomial σ R) (d : τ →₀ ℕ) (h : ∀ (u : σ →₀ ℕ), finsupp.map_domain f u = d → coeff u φ = 0) : coeff d (coe_fn (rename f) φ) = 0 := sorry theorem coeff_rename_ne_zero {σ : Type u_1} {τ : Type u_2} {R : Type u_4} [comm_semiring R] (f : σ → τ) (φ : mv_polynomial σ R) (d : τ →₀ ℕ) (h : coeff d (coe_fn (rename f) φ) ≠ 0) : ∃ (u : σ →₀ ℕ), finsupp.map_domain f u = d ∧ coeff u φ ≠ 0 := sorry @[simp] theorem constant_coeff_rename {σ : Type u_1} {R : Type u_4} [comm_semiring R] {τ : Type u_2} (f : σ → τ) (φ : mv_polynomial σ R) : coe_fn constant_coeff (coe_fn (rename f) φ) = coe_fn constant_coeff φ := sorry end Mathlib
bcf74b8a8d789185d04ace8bec2b2ed6fd909154
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/run/forInElabBug.lean
e2382905559dfcd5e5a4e911a8f84486021dd175
[ "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
381
lean
import Std namespace Std.BinomialHeapImp open Heap partial def toArrayUnordered' (h : Heap α) : Array α := go #[] h where go (acc : Array α) : Heap α → Array α | heap ns => Id.run do let mut acc := acc for h₁ : n in ns do acc := acc.push n.val for h₂ : h in n.children do acc := go acc h return acc
9665ea6020d4a450ecf0d230f4e693cb181feb18
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/inductionTacticBug.lean
9455bc5cfddbdbc603e07b7924510109e492b452
[ "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
175
lean
def ex {α} : Subsingleton (Squash α) := Subsingleton.intro $ by intro a b induction a using Squash.ind induction b using Squash.ind apply Quot.sound exact trivial
f575d956baf20ce856b59f7ef5eae79e35984b8b
c777c32c8e484e195053731103c5e52af26a25d1
/src/measure_theory/function/conditional_expectation/basic.lean
0cacb74285b495681362c00f16b74c4946eb331a
[ "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
109,102
lean
/- Copyright (c) 2021 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne -/ import analysis.inner_product_space.projection import measure_theory.function.l2_space import measure_theory.function.ae_eq_of_integral /-! # Conditional expectation We build the conditional expectation of an integrable function `f` with value in a Banach space with respect to a measure `μ` (defined on a measurable space structure `m0`) and a measurable space structure `m` with `hm : m ≤ m0` (a sub-sigma-algebra). This is an `m`-strongly measurable function `μ[f|hm]` which is integrable and verifies `∫ x in s, μ[f|hm] x ∂μ = ∫ x in s, f x ∂μ` for all `m`-measurable sets `s`. It is unique as an element of `L¹`. The construction is done in four steps: * Define the conditional expectation of an `L²` function, as an element of `L²`. This is the orthogonal projection on the subspace of almost everywhere `m`-measurable functions. * Show that the conditional expectation of the indicator of a measurable set with finite measure is integrable and define a map `set α → (E →L[ℝ] (α →₁[μ] E))` which to a set associates a linear map. That linear map sends `x ∈ E` to the conditional expectation of the indicator of the set with value `x`. * Extend that map to `condexp_L1_clm : (α →₁[μ] E) →L[ℝ] (α →₁[μ] E)`. This is done using the same construction as the Bochner integral (see the file `measure_theory/integral/set_to_L1`). * Define the conditional expectation of a function `f : α → E`, which is an integrable function `α → E` equal to 0 if `f` is not integrable, and equal to an `m`-measurable representative of `condexp_L1_clm` applied to `[f]`, the equivalence class of `f` in `L¹`. ## Main results The conditional expectation and its properties * `condexp (m : measurable_space α) (μ : measure α) (f : α → E)`: conditional expectation of `f` with respect to `m`. * `integrable_condexp` : `condexp` is integrable. * `strongly_measurable_condexp` : `condexp` is `m`-strongly-measurable. * `set_integral_condexp (hf : integrable f μ) (hs : measurable_set[m] s)` : if `m ≤ m0` (the σ-algebra over which the measure is defined), then the conditional expectation verifies `∫ x in s, condexp m μ f x ∂μ = ∫ x in s, f x ∂μ` for any `m`-measurable set `s`. While `condexp` is function-valued, we also define `condexp_L1` with value in `L1` and a continuous linear map `condexp_L1_clm` from `L1` to `L1`. `condexp` should be used in most cases. Uniqueness of the conditional expectation * `Lp.ae_eq_of_forall_set_integral_eq'`: two `Lp` functions verifying the equality of integrals defining the conditional expectation are equal. * `ae_eq_of_forall_set_integral_eq_of_sigma_finite'`: two functions verifying the equality of integrals defining the conditional expectation are equal almost everywhere. Requires `[sigma_finite (μ.trim hm)]`. * `ae_eq_condexp_of_forall_set_integral_eq`: an a.e. `m`-measurable function which verifies the equality of integrals is a.e. equal to `condexp`. ## Notations For a measure `μ` defined on a measurable space structure `m0`, another measurable space structure `m` with `hm : m ≤ m0` (a sub-σ-algebra) and a function `f`, we define the notation * `μ[f|m] = condexp m μ f`. ## Implementation notes Most of the results in this file are valid for a complete real normed space `F`. However, some lemmas also use `𝕜 : is_R_or_C`: * `condexp_L2` is defined only for an `inner_product_space` for now, and we use `𝕜` for its field. * results about scalar multiplication are stated not only for `ℝ` but also for `𝕜` if we happen to have `normed_space 𝕜 F`. ## Tags conditional expectation, conditional expected value -/ noncomputable theory open topological_space measure_theory.Lp filter continuous_linear_map open_locale nnreal ennreal topology big_operators measure_theory namespace measure_theory /-- A function `f` verifies `ae_strongly_measurable' m f μ` if it is `μ`-a.e. equal to an `m`-strongly measurable function. This is similar to `ae_strongly_measurable`, but the `measurable_space` structures used for the measurability statement and for the measure are different. -/ def ae_strongly_measurable' {α β} [topological_space β] (m : measurable_space α) {m0 : measurable_space α} (f : α → β) (μ : measure α) : Prop := ∃ g : α → β, strongly_measurable[m] g ∧ f =ᵐ[μ] g namespace ae_strongly_measurable' variables {α β 𝕜 : Type*} {m m0 : measurable_space α} {μ : measure α} [topological_space β] {f g : α → β} lemma congr (hf : ae_strongly_measurable' m f μ) (hfg : f =ᵐ[μ] g) : ae_strongly_measurable' m g μ := by { obtain ⟨f', hf'_meas, hff'⟩ := hf, exact ⟨f', hf'_meas, hfg.symm.trans hff'⟩, } lemma add [has_add β] [has_continuous_add β] (hf : ae_strongly_measurable' m f μ) (hg : ae_strongly_measurable' m g μ) : ae_strongly_measurable' m (f+g) μ := begin rcases hf with ⟨f', h_f'_meas, hff'⟩, rcases hg with ⟨g', h_g'_meas, hgg'⟩, exact ⟨f' + g', h_f'_meas.add h_g'_meas, hff'.add hgg'⟩, end lemma neg [add_group β] [topological_add_group β] {f : α → β} (hfm : ae_strongly_measurable' m f μ) : ae_strongly_measurable' m (-f) μ := begin rcases hfm with ⟨f', hf'_meas, hf_ae⟩, refine ⟨-f', hf'_meas.neg, hf_ae.mono (λ x hx, _)⟩, simp_rw pi.neg_apply, rw hx, end lemma sub [add_group β] [topological_add_group β] {f g : α → β} (hfm : ae_strongly_measurable' m f μ) (hgm : ae_strongly_measurable' m g μ) : ae_strongly_measurable' m (f - g) μ := begin rcases hfm with ⟨f', hf'_meas, hf_ae⟩, rcases hgm with ⟨g', hg'_meas, hg_ae⟩, refine ⟨f'-g', hf'_meas.sub hg'_meas, hf_ae.mp (hg_ae.mono (λ x hx1 hx2, _))⟩, simp_rw pi.sub_apply, rw [hx1, hx2], end lemma const_smul [has_smul 𝕜 β] [has_continuous_const_smul 𝕜 β] (c : 𝕜) (hf : ae_strongly_measurable' m f μ) : ae_strongly_measurable' m (c • f) μ := begin rcases hf with ⟨f', h_f'_meas, hff'⟩, refine ⟨c • f', h_f'_meas.const_smul c, _⟩, exact eventually_eq.fun_comp hff' (λ x, c • x), end lemma const_inner {𝕜 β} [is_R_or_C 𝕜] [normed_add_comm_group β] [inner_product_space 𝕜 β] {f : α → β} (hfm : ae_strongly_measurable' m f μ) (c : β) : ae_strongly_measurable' m (λ x, (inner c (f x) : 𝕜)) μ := begin rcases hfm with ⟨f', hf'_meas, hf_ae⟩, refine ⟨λ x, (inner c (f' x) : 𝕜), (@strongly_measurable_const _ _ m _ _).inner hf'_meas, hf_ae.mono (λ x hx, _)⟩, dsimp only, rw hx, end /-- An `m`-strongly measurable function almost everywhere equal to `f`. -/ def mk (f : α → β) (hfm : ae_strongly_measurable' m f μ) : α → β := hfm.some lemma strongly_measurable_mk {f : α → β} (hfm : ae_strongly_measurable' m f μ) : strongly_measurable[m] (hfm.mk f) := hfm.some_spec.1 lemma ae_eq_mk {f : α → β} (hfm : ae_strongly_measurable' m f μ) : f =ᵐ[μ] hfm.mk f := hfm.some_spec.2 lemma continuous_comp {γ} [topological_space γ] {f : α → β} {g : β → γ} (hg : continuous g) (hf : ae_strongly_measurable' m f μ) : ae_strongly_measurable' m (g ∘ f) μ := ⟨λ x, g (hf.mk _ x), @continuous.comp_strongly_measurable _ _ _ m _ _ _ _ hg hf.strongly_measurable_mk, hf.ae_eq_mk.mono (λ x hx, by rw [function.comp_apply, hx])⟩ end ae_strongly_measurable' lemma ae_strongly_measurable'_of_ae_strongly_measurable'_trim {α β} {m m0 m0' : measurable_space α} [topological_space β] (hm0 : m0 ≤ m0') {μ : measure α} {f : α → β} (hf : ae_strongly_measurable' m f (μ.trim hm0)) : ae_strongly_measurable' m f μ := by { obtain ⟨g, hg_meas, hfg⟩ := hf, exact ⟨g, hg_meas, ae_eq_of_ae_eq_trim hfg⟩, } lemma strongly_measurable.ae_strongly_measurable' {α β} {m m0 : measurable_space α} [topological_space β] {μ : measure α} {f : α → β} (hf : strongly_measurable[m] f) : ae_strongly_measurable' m f μ := ⟨f, hf, ae_eq_refl _⟩ lemma ae_eq_trim_iff_of_ae_strongly_measurable' {α β} [topological_space β] [metrizable_space β] {m m0 : measurable_space α} {μ : measure α} {f g : α → β} (hm : m ≤ m0) (hfm : ae_strongly_measurable' m f μ) (hgm : ae_strongly_measurable' m g μ) : hfm.mk f =ᵐ[μ.trim hm] hgm.mk g ↔ f =ᵐ[μ] g := (ae_eq_trim_iff hm hfm.strongly_measurable_mk hgm.strongly_measurable_mk).trans ⟨λ h, hfm.ae_eq_mk.trans (h.trans hgm.ae_eq_mk.symm), λ h, hfm.ae_eq_mk.symm.trans (h.trans hgm.ae_eq_mk)⟩ /-- If the restriction to a set `s` of a σ-algebra `m` is included in the restriction to `s` of another σ-algebra `m₂` (hypothesis `hs`), the set `s` is `m` measurable and a function `f` almost everywhere supported on `s` is `m`-ae-strongly-measurable, then `f` is also `m₂`-ae-strongly-measurable. -/ lemma ae_strongly_measurable'.ae_strongly_measurable'_of_measurable_space_le_on {α E} {m m₂ m0 : measurable_space α} {μ : measure α} [topological_space E] [has_zero E] (hm : m ≤ m0) {s : set α} {f : α → E} (hs_m : measurable_set[m] s) (hs : ∀ t, measurable_set[m] (s ∩ t) → measurable_set[m₂] (s ∩ t)) (hf : ae_strongly_measurable' m f μ) (hf_zero : f =ᵐ[μ.restrict sᶜ] 0) : ae_strongly_measurable' m₂ f μ := begin let f' := hf.mk f, have h_ind_eq : s.indicator (hf.mk f) =ᵐ[μ] f, { refine filter.eventually_eq.trans _ (indicator_ae_eq_of_restrict_compl_ae_eq_zero (hm _ hs_m) hf_zero), filter_upwards [hf.ae_eq_mk] with x hx, by_cases hxs : x ∈ s, { simp [hxs, hx], }, { simp [hxs], }, }, suffices : strongly_measurable[m₂] (s.indicator (hf.mk f)), from ae_strongly_measurable'.congr this.ae_strongly_measurable' h_ind_eq, have hf_ind : strongly_measurable[m] (s.indicator (hf.mk f)), from hf.strongly_measurable_mk.indicator hs_m, exact hf_ind.strongly_measurable_of_measurable_space_le_on hs_m hs (λ x hxs, set.indicator_of_not_mem hxs _), end variables {α β γ E E' F F' G G' H 𝕜 : Type*} {p : ℝ≥0∞} [is_R_or_C 𝕜] -- 𝕜 for ℝ or ℂ [topological_space β] -- β for a generic topological space -- E for an inner product space [normed_add_comm_group E] [inner_product_space 𝕜 E] -- E' for an inner product space on which we compute integrals [normed_add_comm_group E'] [inner_product_space 𝕜 E'] [complete_space E'] [normed_space ℝ E'] -- F for a Lp submodule [normed_add_comm_group F] [normed_space 𝕜 F] -- F' for integrals on a Lp submodule [normed_add_comm_group F'] [normed_space 𝕜 F'] [normed_space ℝ F'] [complete_space F'] -- G for a Lp add_subgroup [normed_add_comm_group G] -- G' for integrals on a Lp add_subgroup [normed_add_comm_group G'] [normed_space ℝ G'] [complete_space G'] -- H for a normed group (hypotheses of mem_ℒp) [normed_add_comm_group H] section Lp_meas /-! ## The subset `Lp_meas` of `Lp` functions a.e. measurable with respect to a sub-sigma-algebra -/ variables (F) /-- `Lp_meas_subgroup F m p μ` is the subspace of `Lp F p μ` containing functions `f` verifying `ae_strongly_measurable' m f μ`, i.e. functions which are `μ`-a.e. equal to an `m`-strongly measurable function. -/ def Lp_meas_subgroup (m : measurable_space α) [measurable_space α] (p : ℝ≥0∞) (μ : measure α) : add_subgroup (Lp F p μ) := { carrier := {f : (Lp F p μ) | ae_strongly_measurable' m f μ} , zero_mem' := ⟨(0 : α → F), @strongly_measurable_zero _ _ m _ _, Lp.coe_fn_zero _ _ _⟩, add_mem' := λ f g hf hg, (hf.add hg).congr (Lp.coe_fn_add f g).symm, neg_mem' := λ f hf, ae_strongly_measurable'.congr hf.neg (Lp.coe_fn_neg f).symm, } variables (𝕜) /-- `Lp_meas F 𝕜 m p μ` is the subspace of `Lp F p μ` containing functions `f` verifying `ae_strongly_measurable' m f μ`, i.e. functions which are `μ`-a.e. equal to an `m`-strongly measurable function. -/ def Lp_meas (m : measurable_space α) [measurable_space α] (p : ℝ≥0∞) (μ : measure α) : submodule 𝕜 (Lp F p μ) := { carrier := {f : (Lp F p μ) | ae_strongly_measurable' m f μ} , zero_mem' := ⟨(0 : α → F), @strongly_measurable_zero _ _ m _ _, Lp.coe_fn_zero _ _ _⟩, add_mem' := λ f g hf hg, (hf.add hg).congr (Lp.coe_fn_add f g).symm, smul_mem' := λ c f hf, (hf.const_smul c).congr (Lp.coe_fn_smul c f).symm, } variables {F 𝕜} variables lemma mem_Lp_meas_subgroup_iff_ae_strongly_measurable' {m m0 : measurable_space α} {μ : measure α} {f : Lp F p μ} : f ∈ Lp_meas_subgroup F m p μ ↔ ae_strongly_measurable' m f μ := by rw [← add_subgroup.mem_carrier, Lp_meas_subgroup, set.mem_set_of_eq] lemma mem_Lp_meas_iff_ae_strongly_measurable' {m m0 : measurable_space α} {μ : measure α} {f : Lp F p μ} : f ∈ Lp_meas F 𝕜 m p μ ↔ ae_strongly_measurable' m f μ := by rw [← set_like.mem_coe, ← submodule.mem_carrier, Lp_meas, set.mem_set_of_eq] lemma Lp_meas.ae_strongly_measurable' {m m0 : measurable_space α} {μ : measure α} (f : Lp_meas F 𝕜 m p μ) : ae_strongly_measurable' m f μ := mem_Lp_meas_iff_ae_strongly_measurable'.mp f.mem lemma mem_Lp_meas_self {m0 : measurable_space α} (μ : measure α) (f : Lp F p μ) : f ∈ Lp_meas F 𝕜 m0 p μ := mem_Lp_meas_iff_ae_strongly_measurable'.mpr (Lp.ae_strongly_measurable f) lemma Lp_meas_subgroup_coe {m m0 : measurable_space α} {μ : measure α} {f : Lp_meas_subgroup F m p μ} : ⇑f = (f : Lp F p μ) := coe_fn_coe_base f lemma Lp_meas_coe {m m0 : measurable_space α} {μ : measure α} {f : Lp_meas F 𝕜 m p μ} : ⇑f = (f : Lp F p μ) := coe_fn_coe_base f lemma mem_Lp_meas_indicator_const_Lp {m m0 : measurable_space α} (hm : m ≤ m0) {μ : measure α} {s : set α} (hs : measurable_set[m] s) (hμs : μ s ≠ ∞) {c : F} : indicator_const_Lp p (hm s hs) hμs c ∈ Lp_meas F 𝕜 m p μ := ⟨s.indicator (λ x : α, c), (@strongly_measurable_const _ _ m _ _).indicator hs, indicator_const_Lp_coe_fn⟩ section complete_subspace /-! ## The subspace `Lp_meas` is complete. We define an `isometry_equiv` between `Lp_meas_subgroup` and the `Lp` space corresponding to the measure `μ.trim hm`. As a consequence, the completeness of `Lp` implies completeness of `Lp_meas_subgroup` (and `Lp_meas`). -/ variables {ι : Type*} {m m0 : measurable_space α} {μ : measure α} /-- If `f` belongs to `Lp_meas_subgroup F m p μ`, then the measurable function it is almost everywhere equal to (given by `ae_measurable.mk`) belongs to `ℒp` for the measure `μ.trim hm`. -/ lemma mem_ℒp_trim_of_mem_Lp_meas_subgroup (hm : m ≤ m0) (f : Lp F p μ) (hf_meas : f ∈ Lp_meas_subgroup F m p μ) : mem_ℒp (mem_Lp_meas_subgroup_iff_ae_strongly_measurable'.mp hf_meas).some p (μ.trim hm) := begin have hf : ae_strongly_measurable' m f μ, from (mem_Lp_meas_subgroup_iff_ae_strongly_measurable'.mp hf_meas), let g := hf.some, obtain ⟨hg, hfg⟩ := hf.some_spec, change mem_ℒp g p (μ.trim hm), refine ⟨hg.ae_strongly_measurable, _⟩, have h_snorm_fg : snorm g p (μ.trim hm) = snorm f p μ, by { rw snorm_trim hm hg, exact snorm_congr_ae hfg.symm, }, rw h_snorm_fg, exact Lp.snorm_lt_top f, end /-- If `f` belongs to `Lp` for the measure `μ.trim hm`, then it belongs to the subgroup `Lp_meas_subgroup F m p μ`. -/ lemma mem_Lp_meas_subgroup_to_Lp_of_trim (hm : m ≤ m0) (f : Lp F p (μ.trim hm)) : (mem_ℒp_of_mem_ℒp_trim hm (Lp.mem_ℒp f)).to_Lp f ∈ Lp_meas_subgroup F m p μ := begin let hf_mem_ℒp := mem_ℒp_of_mem_ℒp_trim hm (Lp.mem_ℒp f), rw mem_Lp_meas_subgroup_iff_ae_strongly_measurable', refine ae_strongly_measurable'.congr _ (mem_ℒp.coe_fn_to_Lp hf_mem_ℒp).symm, refine ae_strongly_measurable'_of_ae_strongly_measurable'_trim hm _, exact Lp.ae_strongly_measurable f, end variables (F p μ) /-- Map from `Lp_meas_subgroup` to `Lp F p (μ.trim hm)`. -/ def Lp_meas_subgroup_to_Lp_trim (hm : m ≤ m0) (f : Lp_meas_subgroup F m p μ) : Lp F p (μ.trim hm) := mem_ℒp.to_Lp (mem_Lp_meas_subgroup_iff_ae_strongly_measurable'.mp f.mem).some (mem_ℒp_trim_of_mem_Lp_meas_subgroup hm f f.mem) variables (𝕜) /-- Map from `Lp_meas` to `Lp F p (μ.trim hm)`. -/ def Lp_meas_to_Lp_trim (hm : m ≤ m0) (f : Lp_meas F 𝕜 m p μ) : Lp F p (μ.trim hm) := mem_ℒp.to_Lp (mem_Lp_meas_iff_ae_strongly_measurable'.mp f.mem).some (mem_ℒp_trim_of_mem_Lp_meas_subgroup hm f f.mem) variables {𝕜} /-- Map from `Lp F p (μ.trim hm)` to `Lp_meas_subgroup`, inverse of `Lp_meas_subgroup_to_Lp_trim`. -/ def Lp_trim_to_Lp_meas_subgroup (hm : m ≤ m0) (f : Lp F p (μ.trim hm)) : Lp_meas_subgroup F m p μ := ⟨(mem_ℒp_of_mem_ℒp_trim hm (Lp.mem_ℒp f)).to_Lp f, mem_Lp_meas_subgroup_to_Lp_of_trim hm f⟩ variables (𝕜) /-- Map from `Lp F p (μ.trim hm)` to `Lp_meas`, inverse of `Lp_meas_to_Lp_trim`. -/ def Lp_trim_to_Lp_meas (hm : m ≤ m0) (f : Lp F p (μ.trim hm)) : Lp_meas F 𝕜 m p μ := ⟨(mem_ℒp_of_mem_ℒp_trim hm (Lp.mem_ℒp f)).to_Lp f, mem_Lp_meas_subgroup_to_Lp_of_trim hm f⟩ variables {F 𝕜 p μ} lemma Lp_meas_subgroup_to_Lp_trim_ae_eq (hm : m ≤ m0) (f : Lp_meas_subgroup F m p μ) : Lp_meas_subgroup_to_Lp_trim F p μ hm f =ᵐ[μ] f := (ae_eq_of_ae_eq_trim (mem_ℒp.coe_fn_to_Lp (mem_ℒp_trim_of_mem_Lp_meas_subgroup hm ↑f f.mem))).trans (mem_Lp_meas_subgroup_iff_ae_strongly_measurable'.mp f.mem).some_spec.2.symm lemma Lp_trim_to_Lp_meas_subgroup_ae_eq (hm : m ≤ m0) (f : Lp F p (μ.trim hm)) : Lp_trim_to_Lp_meas_subgroup F p μ hm f =ᵐ[μ] f := mem_ℒp.coe_fn_to_Lp _ lemma Lp_meas_to_Lp_trim_ae_eq (hm : m ≤ m0) (f : Lp_meas F 𝕜 m p μ) : Lp_meas_to_Lp_trim F 𝕜 p μ hm f =ᵐ[μ] f := (ae_eq_of_ae_eq_trim (mem_ℒp.coe_fn_to_Lp (mem_ℒp_trim_of_mem_Lp_meas_subgroup hm ↑f f.mem))).trans (mem_Lp_meas_subgroup_iff_ae_strongly_measurable'.mp f.mem).some_spec.2.symm lemma Lp_trim_to_Lp_meas_ae_eq (hm : m ≤ m0) (f : Lp F p (μ.trim hm)) : Lp_trim_to_Lp_meas F 𝕜 p μ hm f =ᵐ[μ] f := mem_ℒp.coe_fn_to_Lp _ /-- `Lp_trim_to_Lp_meas_subgroup` is a right inverse of `Lp_meas_subgroup_to_Lp_trim`. -/ lemma Lp_meas_subgroup_to_Lp_trim_right_inv (hm : m ≤ m0) : function.right_inverse (Lp_trim_to_Lp_meas_subgroup F p μ hm) (Lp_meas_subgroup_to_Lp_trim F p μ hm) := begin intro f, ext1, refine ae_eq_trim_of_strongly_measurable hm (Lp.strongly_measurable _) (Lp.strongly_measurable _) _, exact (Lp_meas_subgroup_to_Lp_trim_ae_eq hm _).trans (Lp_trim_to_Lp_meas_subgroup_ae_eq hm _), end /-- `Lp_trim_to_Lp_meas_subgroup` is a left inverse of `Lp_meas_subgroup_to_Lp_trim`. -/ lemma Lp_meas_subgroup_to_Lp_trim_left_inv (hm : m ≤ m0) : function.left_inverse (Lp_trim_to_Lp_meas_subgroup F p μ hm) (Lp_meas_subgroup_to_Lp_trim F p μ hm) := begin intro f, ext1, ext1, rw ← Lp_meas_subgroup_coe, exact (Lp_trim_to_Lp_meas_subgroup_ae_eq hm _).trans (Lp_meas_subgroup_to_Lp_trim_ae_eq hm _), end lemma Lp_meas_subgroup_to_Lp_trim_add (hm : m ≤ m0) (f g : Lp_meas_subgroup F m p μ) : Lp_meas_subgroup_to_Lp_trim F p μ hm (f + g) = Lp_meas_subgroup_to_Lp_trim F p μ hm f + Lp_meas_subgroup_to_Lp_trim F p μ hm g := begin ext1, refine eventually_eq.trans _ (Lp.coe_fn_add _ _).symm, refine ae_eq_trim_of_strongly_measurable hm (Lp.strongly_measurable _) _ _, { exact (Lp.strongly_measurable _).add (Lp.strongly_measurable _), }, refine (Lp_meas_subgroup_to_Lp_trim_ae_eq hm _).trans _, refine eventually_eq.trans _ (eventually_eq.add (Lp_meas_subgroup_to_Lp_trim_ae_eq hm f).symm (Lp_meas_subgroup_to_Lp_trim_ae_eq hm g).symm), refine (Lp.coe_fn_add _ _).trans _, simp_rw Lp_meas_subgroup_coe, exact eventually_of_forall (λ x, by refl), end lemma Lp_meas_subgroup_to_Lp_trim_neg (hm : m ≤ m0) (f : Lp_meas_subgroup F m p μ) : Lp_meas_subgroup_to_Lp_trim F p μ hm (-f) = -Lp_meas_subgroup_to_Lp_trim F p μ hm f := begin ext1, refine eventually_eq.trans _ (Lp.coe_fn_neg _).symm, refine ae_eq_trim_of_strongly_measurable hm (Lp.strongly_measurable _) _ _, { exact @strongly_measurable.neg _ _ _ m _ _ _ (Lp.strongly_measurable _), }, refine (Lp_meas_subgroup_to_Lp_trim_ae_eq hm _).trans _, refine eventually_eq.trans _ (eventually_eq.neg (Lp_meas_subgroup_to_Lp_trim_ae_eq hm f).symm), refine (Lp.coe_fn_neg _).trans _, simp_rw Lp_meas_subgroup_coe, exact eventually_of_forall (λ x, by refl), end lemma Lp_meas_subgroup_to_Lp_trim_sub (hm : m ≤ m0) (f g : Lp_meas_subgroup F m p μ) : Lp_meas_subgroup_to_Lp_trim F p μ hm (f - g) = Lp_meas_subgroup_to_Lp_trim F p μ hm f - Lp_meas_subgroup_to_Lp_trim F p μ hm g := by rw [sub_eq_add_neg, sub_eq_add_neg, Lp_meas_subgroup_to_Lp_trim_add, Lp_meas_subgroup_to_Lp_trim_neg] lemma Lp_meas_to_Lp_trim_smul (hm : m ≤ m0) (c : 𝕜) (f : Lp_meas F 𝕜 m p μ) : Lp_meas_to_Lp_trim F 𝕜 p μ hm (c • f) = c • Lp_meas_to_Lp_trim F 𝕜 p μ hm f := begin ext1, refine eventually_eq.trans _ (Lp.coe_fn_smul _ _).symm, refine ae_eq_trim_of_strongly_measurable hm (Lp.strongly_measurable _) _ _, { exact (Lp.strongly_measurable _).const_smul c, }, refine (Lp_meas_to_Lp_trim_ae_eq hm _).trans _, refine (Lp.coe_fn_smul _ _).trans _, refine (Lp_meas_to_Lp_trim_ae_eq hm f).mono (λ x hx, _), rw [pi.smul_apply, pi.smul_apply, hx], refl, end /-- `Lp_meas_subgroup_to_Lp_trim` preserves the norm. -/ lemma Lp_meas_subgroup_to_Lp_trim_norm_map [hp : fact (1 ≤ p)] (hm : m ≤ m0) (f : Lp_meas_subgroup F m p μ) : ‖Lp_meas_subgroup_to_Lp_trim F p μ hm f‖ = ‖f‖ := begin rw [Lp.norm_def, snorm_trim hm (Lp.strongly_measurable _), snorm_congr_ae (Lp_meas_subgroup_to_Lp_trim_ae_eq hm _), Lp_meas_subgroup_coe, ← Lp.norm_def], congr, end lemma isometry_Lp_meas_subgroup_to_Lp_trim [hp : fact (1 ≤ p)] (hm : m ≤ m0) : isometry (Lp_meas_subgroup_to_Lp_trim F p μ hm) := isometry.of_dist_eq $ λ f g, by rw [dist_eq_norm, ← Lp_meas_subgroup_to_Lp_trim_sub, Lp_meas_subgroup_to_Lp_trim_norm_map, dist_eq_norm] variables (F p μ) /-- `Lp_meas_subgroup` and `Lp F p (μ.trim hm)` are isometric. -/ def Lp_meas_subgroup_to_Lp_trim_iso [hp : fact (1 ≤ p)] (hm : m ≤ m0) : Lp_meas_subgroup F m p μ ≃ᵢ Lp F p (μ.trim hm) := { to_fun := Lp_meas_subgroup_to_Lp_trim F p μ hm, inv_fun := Lp_trim_to_Lp_meas_subgroup F p μ hm, left_inv := Lp_meas_subgroup_to_Lp_trim_left_inv hm, right_inv := Lp_meas_subgroup_to_Lp_trim_right_inv hm, isometry_to_fun := isometry_Lp_meas_subgroup_to_Lp_trim hm, } variables (𝕜) /-- `Lp_meas_subgroup` and `Lp_meas` are isometric. -/ def Lp_meas_subgroup_to_Lp_meas_iso [hp : fact (1 ≤ p)] : Lp_meas_subgroup F m p μ ≃ᵢ Lp_meas F 𝕜 m p μ := isometry_equiv.refl (Lp_meas_subgroup F m p μ) /-- `Lp_meas` and `Lp F p (μ.trim hm)` are isometric, with a linear equivalence. -/ def Lp_meas_to_Lp_trim_lie [hp : fact (1 ≤ p)] (hm : m ≤ m0) : Lp_meas F 𝕜 m p μ ≃ₗᵢ[𝕜] Lp F p (μ.trim hm) := { to_fun := Lp_meas_to_Lp_trim F 𝕜 p μ hm, inv_fun := Lp_trim_to_Lp_meas F 𝕜 p μ hm, left_inv := Lp_meas_subgroup_to_Lp_trim_left_inv hm, right_inv := Lp_meas_subgroup_to_Lp_trim_right_inv hm, map_add' := Lp_meas_subgroup_to_Lp_trim_add hm, map_smul' := Lp_meas_to_Lp_trim_smul hm, norm_map' := Lp_meas_subgroup_to_Lp_trim_norm_map hm, } variables {F 𝕜 p μ} instance [hm : fact (m ≤ m0)] [complete_space F] [hp : fact (1 ≤ p)] : complete_space (Lp_meas_subgroup F m p μ) := by { rw (Lp_meas_subgroup_to_Lp_trim_iso F p μ hm.elim).complete_space_iff, apply_instance, } instance [hm : fact (m ≤ m0)] [complete_space F] [hp : fact (1 ≤ p)] : complete_space (Lp_meas F 𝕜 m p μ) := by { rw (Lp_meas_subgroup_to_Lp_meas_iso F 𝕜 p μ).symm.complete_space_iff, apply_instance, } lemma is_complete_ae_strongly_measurable' [hp : fact (1 ≤ p)] [complete_space F] (hm : m ≤ m0) : is_complete {f : Lp F p μ | ae_strongly_measurable' m f μ} := begin rw ← complete_space_coe_iff_is_complete, haveI : fact (m ≤ m0) := ⟨hm⟩, change complete_space (Lp_meas_subgroup F m p μ), apply_instance, end lemma is_closed_ae_strongly_measurable' [hp : fact (1 ≤ p)] [complete_space F] (hm : m ≤ m0) : is_closed {f : Lp F p μ | ae_strongly_measurable' m f μ} := is_complete.is_closed (is_complete_ae_strongly_measurable' hm) end complete_subspace section strongly_measurable variables {m m0 : measurable_space α} {μ : measure α} /-- We do not get `ae_fin_strongly_measurable f (μ.trim hm)`, since we don't have `f =ᵐ[μ.trim hm] Lp_meas_to_Lp_trim F 𝕜 p μ hm f` but only the weaker `f =ᵐ[μ] Lp_meas_to_Lp_trim F 𝕜 p μ hm f`. -/ lemma Lp_meas.ae_fin_strongly_measurable' (hm : m ≤ m0) (f : Lp_meas F 𝕜 m p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) : ∃ g, fin_strongly_measurable g (μ.trim hm) ∧ f =ᵐ[μ] g := ⟨Lp_meas_subgroup_to_Lp_trim F p μ hm f, Lp.fin_strongly_measurable _ hp_ne_zero hp_ne_top, (Lp_meas_subgroup_to_Lp_trim_ae_eq hm f).symm⟩ /-- When applying the inverse of `Lp_meas_to_Lp_trim_lie` (which takes a function in the Lp space of the sub-sigma algebra and returns its version in the larger Lp space) to an indicator of the sub-sigma-algebra, we obtain an indicator in the Lp space of the larger sigma-algebra. -/ lemma Lp_meas_to_Lp_trim_lie_symm_indicator [one_le_p : fact (1 ≤ p)] [normed_space ℝ F] {hm : m ≤ m0} {s : set α} {μ : measure α} (hs : measurable_set[m] s) (hμs : μ.trim hm s ≠ ∞) (c : F) : ((Lp_meas_to_Lp_trim_lie F ℝ p μ hm).symm (indicator_const_Lp p hs hμs c) : Lp F p μ) = indicator_const_Lp p (hm s hs) ((le_trim hm).trans_lt hμs.lt_top).ne c := begin ext1, rw ← Lp_meas_coe, change Lp_trim_to_Lp_meas F ℝ p μ hm (indicator_const_Lp p hs hμs c) =ᵐ[μ] (indicator_const_Lp p _ _ c : α → F), refine (Lp_trim_to_Lp_meas_ae_eq hm _).trans _, exact (ae_eq_of_ae_eq_trim indicator_const_Lp_coe_fn).trans indicator_const_Lp_coe_fn.symm, end lemma Lp_meas_to_Lp_trim_lie_symm_to_Lp [one_le_p : fact (1 ≤ p)] [normed_space ℝ F] (hm : m ≤ m0) (f : α → F) (hf : mem_ℒp f p (μ.trim hm)) : ((Lp_meas_to_Lp_trim_lie F ℝ p μ hm).symm (hf.to_Lp f) : Lp F p μ) = (mem_ℒp_of_mem_ℒp_trim hm hf).to_Lp f := begin ext1, rw ← Lp_meas_coe, refine (Lp_trim_to_Lp_meas_ae_eq hm _).trans _, exact (ae_eq_of_ae_eq_trim (mem_ℒp.coe_fn_to_Lp hf)).trans (mem_ℒp.coe_fn_to_Lp _).symm, end end strongly_measurable end Lp_meas section induction variables {m m0 : measurable_space α} {μ : measure α} [fact (1 ≤ p)] [normed_space ℝ F] /-- Auxiliary lemma for `Lp.induction_strongly_measurable`. -/ @[elab_as_eliminator] lemma Lp.induction_strongly_measurable_aux (hm : m ≤ m0) (hp_ne_top : p ≠ ∞) (P : Lp F p μ → Prop) (h_ind : ∀ (c : F) {s : set α} (hs : measurable_set[m] s) (hμs : μ s < ∞), P (Lp.simple_func.indicator_const p (hm s hs) hμs.ne c)) (h_add : ∀ ⦃f g⦄, ∀ hf : mem_ℒp f p μ, ∀ hg : mem_ℒp g p μ, ∀ hfm : ae_strongly_measurable' m f μ, ∀ hgm : ae_strongly_measurable' m g μ, disjoint (function.support f) (function.support g) → P (hf.to_Lp f) → P (hg.to_Lp g) → P ((hf.to_Lp f) + (hg.to_Lp g))) (h_closed : is_closed {f : Lp_meas F ℝ m p μ | P f}) : ∀ f : Lp F p μ, ae_strongly_measurable' m f μ → P f := begin intros f hf, let f' := (⟨f, hf⟩ : Lp_meas F ℝ m p μ), let g := Lp_meas_to_Lp_trim_lie F ℝ p μ hm f', have hfg : f' = (Lp_meas_to_Lp_trim_lie F ℝ p μ hm).symm g, by simp only [linear_isometry_equiv.symm_apply_apply], change P ↑f', rw hfg, refine @Lp.induction α F m _ p (μ.trim hm) _ hp_ne_top (λ g, P ((Lp_meas_to_Lp_trim_lie F ℝ p μ hm).symm g)) _ _ _ g, { intros b t ht hμt, rw [Lp.simple_func.coe_indicator_const, Lp_meas_to_Lp_trim_lie_symm_indicator ht hμt.ne b], have hμt' : μ t < ∞, from (le_trim hm).trans_lt hμt, specialize h_ind b ht hμt', rwa Lp.simple_func.coe_indicator_const at h_ind, }, { intros f g hf hg h_disj hfP hgP, rw linear_isometry_equiv.map_add, push_cast, have h_eq : ∀ (f : α → F) (hf : mem_ℒp f p (μ.trim hm)), ((Lp_meas_to_Lp_trim_lie F ℝ p μ hm).symm (mem_ℒp.to_Lp f hf) : Lp F p μ) = (mem_ℒp_of_mem_ℒp_trim hm hf).to_Lp f, from Lp_meas_to_Lp_trim_lie_symm_to_Lp hm, rw h_eq f hf at hfP ⊢, rw h_eq g hg at hgP ⊢, exact h_add (mem_ℒp_of_mem_ℒp_trim hm hf) (mem_ℒp_of_mem_ℒp_trim hm hg) (ae_strongly_measurable'_of_ae_strongly_measurable'_trim hm hf.ae_strongly_measurable) (ae_strongly_measurable'_of_ae_strongly_measurable'_trim hm hg.ae_strongly_measurable) h_disj hfP hgP, }, { change is_closed ((Lp_meas_to_Lp_trim_lie F ℝ p μ hm).symm ⁻¹' {g : Lp_meas F ℝ m p μ | P ↑g}), exact is_closed.preimage (linear_isometry_equiv.continuous _) h_closed, }, end /-- To prove something for an `Lp` function a.e. strongly measurable with respect to a sub-σ-algebra `m` in a normed space, it suffices to show that * the property holds for (multiples of) characteristic functions which are measurable w.r.t. `m`; * is closed under addition; * the set of functions in `Lp` strongly measurable w.r.t. `m` for which the property holds is closed. -/ @[elab_as_eliminator] lemma Lp.induction_strongly_measurable (hm : m ≤ m0) (hp_ne_top : p ≠ ∞) (P : Lp F p μ → Prop) (h_ind : ∀ (c : F) {s : set α} (hs : measurable_set[m] s) (hμs : μ s < ∞), P (Lp.simple_func.indicator_const p (hm s hs) hμs.ne c)) (h_add : ∀ ⦃f g⦄, ∀ hf : mem_ℒp f p μ, ∀ hg : mem_ℒp g p μ, ∀ hfm : strongly_measurable[m] f, ∀ hgm : strongly_measurable[m] g, disjoint (function.support f) (function.support g) → P (hf.to_Lp f) → P (hg.to_Lp g) → P ((hf.to_Lp f) + (hg.to_Lp g))) (h_closed : is_closed {f : Lp_meas F ℝ m p μ | P f}) : ∀ f : Lp F p μ, ae_strongly_measurable' m f μ → P f := begin intros f hf, suffices h_add_ae : ∀ ⦃f g⦄, ∀ hf : mem_ℒp f p μ, ∀ hg : mem_ℒp g p μ, ∀ hfm : ae_strongly_measurable' m f μ, ∀ hgm : ae_strongly_measurable' m g μ, disjoint (function.support f) (function.support g) → P (hf.to_Lp f) → P (hg.to_Lp g) → P ((hf.to_Lp f) + (hg.to_Lp g)), from Lp.induction_strongly_measurable_aux hm hp_ne_top P h_ind h_add_ae h_closed f hf, intros f g hf hg hfm hgm h_disj hPf hPg, let s_f : set α := function.support (hfm.mk f), have hs_f : measurable_set[m] s_f := hfm.strongly_measurable_mk.measurable_set_support, have hs_f_eq : s_f =ᵐ[μ] function.support f := hfm.ae_eq_mk.symm.support, let s_g : set α := function.support (hgm.mk g), have hs_g : measurable_set[m] s_g := hgm.strongly_measurable_mk.measurable_set_support, have hs_g_eq : s_g =ᵐ[μ] function.support g := hgm.ae_eq_mk.symm.support, have h_inter_empty : ((s_f ∩ s_g) : set α) =ᵐ[μ] (∅ : set α), { refine (hs_f_eq.inter hs_g_eq).trans _, suffices : function.support f ∩ function.support g = ∅, by rw this, exact set.disjoint_iff_inter_eq_empty.mp h_disj, }, let f' := (s_f \ s_g).indicator (hfm.mk f), have hff' : f =ᵐ[μ] f', { have : s_f \ s_g =ᵐ[μ] s_f, { rw [← set.diff_inter_self_eq_diff, set.inter_comm], refine ((ae_eq_refl s_f).diff h_inter_empty).trans _, rw set.diff_empty, }, refine ((indicator_ae_eq_of_ae_eq_set this).trans _).symm, rw set.indicator_support, exact hfm.ae_eq_mk.symm, }, have hf'_meas : strongly_measurable[m] f', from hfm.strongly_measurable_mk.indicator (hs_f.diff hs_g), have hf'_Lp : mem_ℒp f' p μ := hf.ae_eq hff', let g' := (s_g \ s_f).indicator (hgm.mk g), have hgg' : g =ᵐ[μ] g', { have : s_g \ s_f =ᵐ[μ] s_g, { rw [← set.diff_inter_self_eq_diff], refine ((ae_eq_refl s_g).diff h_inter_empty).trans _, rw set.diff_empty, }, refine ((indicator_ae_eq_of_ae_eq_set this).trans _).symm, rw set.indicator_support, exact hgm.ae_eq_mk.symm, }, have hg'_meas : strongly_measurable[m] g', from hgm.strongly_measurable_mk.indicator (hs_g.diff hs_f), have hg'_Lp : mem_ℒp g' p μ := hg.ae_eq hgg', have h_disj : disjoint (function.support f') (function.support g'), { have : disjoint (s_f \ s_g) (s_g \ s_f) := disjoint_sdiff_sdiff, exact this.mono set.support_indicator_subset set.support_indicator_subset, }, rw ← mem_ℒp.to_Lp_congr hf'_Lp hf hff'.symm at ⊢ hPf, rw ← mem_ℒp.to_Lp_congr hg'_Lp hg hgg'.symm at ⊢ hPg, exact h_add hf'_Lp hg'_Lp hf'_meas hg'_meas h_disj hPf hPg, end /-- To prove something for an arbitrary `mem_ℒp` function a.e. strongly measurable with respect to a sub-σ-algebra `m` in a normed space, it suffices to show that * the property holds for (multiples of) characteristic functions which are measurable w.r.t. `m`; * is closed under addition; * the set of functions in the `Lᵖ` space strongly measurable w.r.t. `m` for which the property holds is closed. * the property is closed under the almost-everywhere equal relation. -/ @[elab_as_eliminator] lemma mem_ℒp.induction_strongly_measurable (hm : m ≤ m0) (hp_ne_top : p ≠ ∞) (P : (α → F) → Prop) (h_ind : ∀ (c : F) ⦃s⦄, measurable_set[m] s → μ s < ∞ → P (s.indicator (λ _, c))) (h_add : ∀ ⦃f g : α → F⦄, disjoint (function.support f) (function.support g) → mem_ℒp f p μ → mem_ℒp g p μ → strongly_measurable[m] f → strongly_measurable[m] g → P f → P g → P (f + g)) (h_closed : is_closed {f : Lp_meas F ℝ m p μ | P f} ) (h_ae : ∀ ⦃f g⦄, f =ᵐ[μ] g → mem_ℒp f p μ → P f → P g) : ∀ ⦃f : α → F⦄ (hf : mem_ℒp f p μ) (hfm : ae_strongly_measurable' m f μ), P f := begin intros f hf hfm, let f_Lp := hf.to_Lp f, have hfm_Lp : ae_strongly_measurable' m f_Lp μ, from hfm.congr hf.coe_fn_to_Lp.symm, refine h_ae (hf.coe_fn_to_Lp) (Lp.mem_ℒp _) _, change P f_Lp, refine Lp.induction_strongly_measurable hm hp_ne_top (λ f, P ⇑f) _ _ h_closed f_Lp hfm_Lp, { intros c s hs hμs, rw Lp.simple_func.coe_indicator_const, refine h_ae (indicator_const_Lp_coe_fn).symm _ (h_ind c hs hμs), exact mem_ℒp_indicator_const p (hm s hs) c (or.inr hμs.ne), }, { intros f g hf_mem hg_mem hfm hgm h_disj hfP hgP, have hfP' : P f := h_ae (hf_mem.coe_fn_to_Lp) (Lp.mem_ℒp _) hfP, have hgP' : P g := h_ae (hg_mem.coe_fn_to_Lp) (Lp.mem_ℒp _) hgP, specialize h_add h_disj hf_mem hg_mem hfm hgm hfP' hgP', refine h_ae _ (hf_mem.add hg_mem) h_add, exact ((hf_mem.coe_fn_to_Lp).symm.add (hg_mem.coe_fn_to_Lp).symm).trans (Lp.coe_fn_add _ _).symm, }, end end induction section uniqueness_of_conditional_expectation /-! ## Uniqueness of the conditional expectation -/ variables {m m0 : measurable_space α} {μ : measure α} lemma Lp_meas.ae_eq_zero_of_forall_set_integral_eq_zero (hm : m ≤ m0) (f : Lp_meas E' 𝕜 m p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hf_int_finite : ∀ s, measurable_set[m] s → μ s < ∞ → integrable_on f s μ) (hf_zero : ∀ s : set α, measurable_set[m] s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) : f =ᵐ[μ] 0 := begin obtain ⟨g, hg_sm, hfg⟩ := Lp_meas.ae_fin_strongly_measurable' hm f hp_ne_zero hp_ne_top, refine hfg.trans _, refine ae_eq_zero_of_forall_set_integral_eq_of_fin_strongly_measurable_trim hm _ _ hg_sm, { intros s hs hμs, have hfg_restrict : f =ᵐ[μ.restrict s] g, from ae_restrict_of_ae hfg, rw [integrable_on, integrable_congr hfg_restrict.symm], exact hf_int_finite s hs hμs, }, { intros s hs hμs, have hfg_restrict : f =ᵐ[μ.restrict s] g, from ae_restrict_of_ae hfg, rw integral_congr_ae hfg_restrict.symm, exact hf_zero s hs hμs, }, end include 𝕜 variables (𝕜) lemma Lp.ae_eq_zero_of_forall_set_integral_eq_zero' (hm : m ≤ m0) (f : Lp E' p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hf_int_finite : ∀ s, measurable_set[m] s → μ s < ∞ → integrable_on f s μ) (hf_zero : ∀ s : set α, measurable_set[m] s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) (hf_meas : ae_strongly_measurable' m f μ) : f =ᵐ[μ] 0 := begin let f_meas : Lp_meas E' 𝕜 m p μ := ⟨f, hf_meas⟩, have hf_f_meas : f =ᵐ[μ] f_meas, by simp only [coe_fn_coe_base', subtype.coe_mk], refine hf_f_meas.trans _, refine Lp_meas.ae_eq_zero_of_forall_set_integral_eq_zero hm f_meas hp_ne_zero hp_ne_top _ _, { intros s hs hμs, have hfg_restrict : f =ᵐ[μ.restrict s] f_meas, from ae_restrict_of_ae hf_f_meas, rw [integrable_on, integrable_congr hfg_restrict.symm], exact hf_int_finite s hs hμs, }, { intros s hs hμs, have hfg_restrict : f =ᵐ[μ.restrict s] f_meas, from ae_restrict_of_ae hf_f_meas, rw integral_congr_ae hfg_restrict.symm, exact hf_zero s hs hμs, }, end /-- **Uniqueness of the conditional expectation** -/ lemma Lp.ae_eq_of_forall_set_integral_eq' (hm : m ≤ m0) (f g : Lp E' p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hf_int_finite : ∀ s, measurable_set[m] s → μ s < ∞ → integrable_on f s μ) (hg_int_finite : ∀ s, measurable_set[m] s → μ s < ∞ → integrable_on g s μ) (hfg : ∀ s : set α, measurable_set[m] s → μ s < ∞ → ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ) (hf_meas : ae_strongly_measurable' m f μ) (hg_meas : ae_strongly_measurable' m g μ) : f =ᵐ[μ] g := begin suffices h_sub : ⇑(f-g) =ᵐ[μ] 0, by { rw ← sub_ae_eq_zero, exact (Lp.coe_fn_sub f g).symm.trans h_sub, }, have hfg' : ∀ s : set α, measurable_set[m] s → μ s < ∞ → ∫ x in s, (f - g) x ∂μ = 0, { intros s hs hμs, rw integral_congr_ae (ae_restrict_of_ae (Lp.coe_fn_sub f g)), rw integral_sub' (hf_int_finite s hs hμs) (hg_int_finite s hs hμs), exact sub_eq_zero.mpr (hfg s hs hμs), }, have hfg_int : ∀ s, measurable_set[m] s → μ s < ∞ → integrable_on ⇑(f-g) s μ, { intros s hs hμs, rw [integrable_on, integrable_congr (ae_restrict_of_ae (Lp.coe_fn_sub f g))], exact (hf_int_finite s hs hμs).sub (hg_int_finite s hs hμs), }, have hfg_meas : ae_strongly_measurable' m ⇑(f - g) μ, from ae_strongly_measurable'.congr (hf_meas.sub hg_meas) (Lp.coe_fn_sub f g).symm, exact Lp.ae_eq_zero_of_forall_set_integral_eq_zero' 𝕜 hm (f-g) hp_ne_zero hp_ne_top hfg_int hfg' hfg_meas, end variables {𝕜} omit 𝕜 lemma ae_eq_of_forall_set_integral_eq_of_sigma_finite' (hm : m ≤ m0) [sigma_finite (μ.trim hm)] {f g : α → F'} (hf_int_finite : ∀ s, measurable_set[m] s → μ s < ∞ → integrable_on f s μ) (hg_int_finite : ∀ s, measurable_set[m] s → μ s < ∞ → integrable_on g s μ) (hfg_eq : ∀ s : set α, measurable_set[m] s → μ s < ∞ → ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ) (hfm : ae_strongly_measurable' m f μ) (hgm : ae_strongly_measurable' m g μ) : f =ᵐ[μ] g := begin rw ← ae_eq_trim_iff_of_ae_strongly_measurable' hm hfm hgm, have hf_mk_int_finite : ∀ s, measurable_set[m] s → μ.trim hm s < ∞ → @integrable_on _ _ m _ (hfm.mk f) s (μ.trim hm), { intros s hs hμs, rw trim_measurable_set_eq hm hs at hμs, rw [integrable_on, restrict_trim hm _ hs], refine integrable.trim hm _ hfm.strongly_measurable_mk, exact integrable.congr (hf_int_finite s hs hμs) (ae_restrict_of_ae hfm.ae_eq_mk), }, have hg_mk_int_finite : ∀ s, measurable_set[m] s → μ.trim hm s < ∞ → @integrable_on _ _ m _ (hgm.mk g) s (μ.trim hm), { intros s hs hμs, rw trim_measurable_set_eq hm hs at hμs, rw [integrable_on, restrict_trim hm _ hs], refine integrable.trim hm _ hgm.strongly_measurable_mk, exact integrable.congr (hg_int_finite s hs hμs) (ae_restrict_of_ae hgm.ae_eq_mk), }, have hfg_mk_eq : ∀ s : set α, measurable_set[m] s → μ.trim hm s < ∞ → ∫ x in s, (hfm.mk f x) ∂(μ.trim hm) = ∫ x in s, (hgm.mk g x) ∂(μ.trim hm), { intros s hs hμs, rw trim_measurable_set_eq hm hs at hμs, rw [restrict_trim hm _ hs, ← integral_trim hm hfm.strongly_measurable_mk, ← integral_trim hm hgm.strongly_measurable_mk, integral_congr_ae (ae_restrict_of_ae hfm.ae_eq_mk.symm), integral_congr_ae (ae_restrict_of_ae hgm.ae_eq_mk.symm)], exact hfg_eq s hs hμs, }, exact ae_eq_of_forall_set_integral_eq_of_sigma_finite hf_mk_int_finite hg_mk_int_finite hfg_mk_eq, end end uniqueness_of_conditional_expectation section integral_norm_le variables {m m0 : measurable_space α} {μ : measure α} {s : set α} /-- Let `m` be a sub-σ-algebra of `m0`, `f` a `m0`-measurable function and `g` a `m`-measurable function, such that their integrals coincide on `m`-measurable sets with finite measure. Then `∫ x in s, ‖g x‖ ∂μ ≤ ∫ x in s, ‖f x‖ ∂μ` on all `m`-measurable sets with finite measure. -/ lemma integral_norm_le_of_forall_fin_meas_integral_eq (hm : m ≤ m0) {f g : α → ℝ} (hf : strongly_measurable f) (hfi : integrable_on f s μ) (hg : strongly_measurable[m] g) (hgi : integrable_on g s μ) (hgf : ∀ t, measurable_set[m] t → μ t < ∞ → ∫ x in t, g x ∂μ = ∫ x in t, f x ∂μ) (hs : measurable_set[m] s) (hμs : μ s ≠ ∞) : ∫ x in s, ‖g x‖ ∂μ ≤ ∫ x in s, ‖f x‖ ∂μ := begin rw [integral_norm_eq_pos_sub_neg hgi, integral_norm_eq_pos_sub_neg hfi], have h_meas_nonneg_g : measurable_set[m] {x | 0 ≤ g x}, from (@strongly_measurable_const _ _ m _ _).measurable_set_le hg, have h_meas_nonneg_f : measurable_set {x | 0 ≤ f x}, from strongly_measurable_const.measurable_set_le hf, have h_meas_nonpos_g : measurable_set[m] {x | g x ≤ 0}, from hg.measurable_set_le (@strongly_measurable_const _ _ m _ _), have h_meas_nonpos_f : measurable_set {x | f x ≤ 0}, from hf.measurable_set_le strongly_measurable_const, refine sub_le_sub _ _, { rw [measure.restrict_restrict (hm _ h_meas_nonneg_g), measure.restrict_restrict h_meas_nonneg_f, hgf _ (@measurable_set.inter α m _ _ h_meas_nonneg_g hs) ((measure_mono (set.inter_subset_right _ _)).trans_lt (lt_top_iff_ne_top.mpr hμs)), ← measure.restrict_restrict (hm _ h_meas_nonneg_g), ← measure.restrict_restrict h_meas_nonneg_f], exact set_integral_le_nonneg (hm _ h_meas_nonneg_g) hf hfi, }, { rw [measure.restrict_restrict (hm _ h_meas_nonpos_g), measure.restrict_restrict h_meas_nonpos_f, hgf _ (@measurable_set.inter α m _ _ h_meas_nonpos_g hs) ((measure_mono (set.inter_subset_right _ _)).trans_lt (lt_top_iff_ne_top.mpr hμs)), ← measure.restrict_restrict (hm _ h_meas_nonpos_g), ← measure.restrict_restrict h_meas_nonpos_f], exact set_integral_nonpos_le (hm _ h_meas_nonpos_g) hf hfi, }, end /-- Let `m` be a sub-σ-algebra of `m0`, `f` a `m0`-measurable function and `g` a `m`-measurable function, such that their integrals coincide on `m`-measurable sets with finite measure. Then `∫⁻ x in s, ‖g x‖₊ ∂μ ≤ ∫⁻ x in s, ‖f x‖₊ ∂μ` on all `m`-measurable sets with finite measure. -/ lemma lintegral_nnnorm_le_of_forall_fin_meas_integral_eq (hm : m ≤ m0) {f g : α → ℝ} (hf : strongly_measurable f) (hfi : integrable_on f s μ) (hg : strongly_measurable[m] g) (hgi : integrable_on g s μ) (hgf : ∀ t, measurable_set[m] t → μ t < ∞ → ∫ x in t, g x ∂μ = ∫ x in t, f x ∂μ) (hs : measurable_set[m] s) (hμs : μ s ≠ ∞) : ∫⁻ x in s, ‖g x‖₊ ∂μ ≤ ∫⁻ x in s, ‖f x‖₊ ∂μ := begin rw [← of_real_integral_norm_eq_lintegral_nnnorm hfi, ← of_real_integral_norm_eq_lintegral_nnnorm hgi, ennreal.of_real_le_of_real_iff], { exact integral_norm_le_of_forall_fin_meas_integral_eq hm hf hfi hg hgi hgf hs hμs, }, { exact integral_nonneg (λ x, norm_nonneg _), }, end end integral_norm_le /-! ## Conditional expectation in L2 We define a conditional expectation in `L2`: it is the orthogonal projection on the subspace `Lp_meas`. -/ section condexp_L2 variables [complete_space E] {m m0 : measurable_space α} {μ : measure α} {s t : set α} local notation `⟪`x`, `y`⟫` := @inner 𝕜 E _ x y local notation `⟪`x`, `y`⟫₂` := @inner 𝕜 (α →₂[μ] E) _ x y variables (𝕜) /-- Conditional expectation of a function in L2 with respect to a sigma-algebra -/ def condexp_L2 (hm : m ≤ m0) : (α →₂[μ] E) →L[𝕜] (Lp_meas E 𝕜 m 2 μ) := @orthogonal_projection 𝕜 (α →₂[μ] E) _ _ _ (Lp_meas E 𝕜 m 2 μ) (by { haveI : fact (m ≤ m0) := ⟨hm⟩, exact infer_instance, }) variables {𝕜} lemma ae_strongly_measurable'_condexp_L2 (hm : m ≤ m0) (f : α →₂[μ] E) : ae_strongly_measurable' m (condexp_L2 𝕜 hm f) μ := Lp_meas.ae_strongly_measurable' _ lemma integrable_on_condexp_L2_of_measure_ne_top (hm : m ≤ m0) (hμs : μ s ≠ ∞) (f : α →₂[μ] E) : integrable_on (condexp_L2 𝕜 hm f) s μ := integrable_on_Lp_of_measure_ne_top ((condexp_L2 𝕜 hm f) : α →₂[μ] E) fact_one_le_two_ennreal.elim hμs lemma integrable_condexp_L2_of_is_finite_measure (hm : m ≤ m0) [is_finite_measure μ] {f : α →₂[μ] E} : integrable (condexp_L2 𝕜 hm f) μ := integrable_on_univ.mp $ integrable_on_condexp_L2_of_measure_ne_top hm (measure_ne_top _ _) f lemma norm_condexp_L2_le_one (hm : m ≤ m0) : ‖@condexp_L2 α E 𝕜 _ _ _ _ _ _ μ hm‖ ≤ 1 := by { haveI : fact (m ≤ m0) := ⟨hm⟩, exact orthogonal_projection_norm_le _, } lemma norm_condexp_L2_le (hm : m ≤ m0) (f : α →₂[μ] E) : ‖condexp_L2 𝕜 hm f‖ ≤ ‖f‖ := ((@condexp_L2 _ E 𝕜 _ _ _ _ _ _ μ hm).le_op_norm f).trans (mul_le_of_le_one_left (norm_nonneg _) (norm_condexp_L2_le_one hm)) lemma snorm_condexp_L2_le (hm : m ≤ m0) (f : α →₂[μ] E) : snorm (condexp_L2 𝕜 hm f) 2 μ ≤ snorm f 2 μ := begin rw [Lp_meas_coe, ← ennreal.to_real_le_to_real (Lp.snorm_ne_top _) (Lp.snorm_ne_top _), ← Lp.norm_def, ← Lp.norm_def, submodule.norm_coe], exact norm_condexp_L2_le hm f, end lemma norm_condexp_L2_coe_le (hm : m ≤ m0) (f : α →₂[μ] E) : ‖(condexp_L2 𝕜 hm f : α →₂[μ] E)‖ ≤ ‖f‖ := begin rw [Lp.norm_def, Lp.norm_def, ← Lp_meas_coe], refine (ennreal.to_real_le_to_real _ (Lp.snorm_ne_top _)).mpr (snorm_condexp_L2_le hm f), exact Lp.snorm_ne_top _, end lemma inner_condexp_L2_left_eq_right (hm : m ≤ m0) {f g : α →₂[μ] E} : ⟪(condexp_L2 𝕜 hm f : α →₂[μ] E), g⟫₂ = ⟪f, (condexp_L2 𝕜 hm g : α →₂[μ] E)⟫₂ := by { haveI : fact (m ≤ m0) := ⟨hm⟩, exact inner_orthogonal_projection_left_eq_right _ f g, } lemma condexp_L2_indicator_of_measurable (hm : m ≤ m0) (hs : measurable_set[m] s) (hμs : μ s ≠ ∞) (c : E) : (condexp_L2 𝕜 hm (indicator_const_Lp 2 (hm s hs) hμs c) : α →₂[μ] E) = indicator_const_Lp 2 (hm s hs) hμs c := begin rw condexp_L2, haveI : fact (m ≤ m0) := ⟨hm⟩, have h_mem : indicator_const_Lp 2 (hm s hs) hμs c ∈ Lp_meas E 𝕜 m 2 μ, from mem_Lp_meas_indicator_const_Lp hm hs hμs, let ind := (⟨indicator_const_Lp 2 (hm s hs) hμs c, h_mem⟩ : Lp_meas E 𝕜 m 2 μ), have h_coe_ind : (ind : α →₂[μ] E) = indicator_const_Lp 2 (hm s hs) hμs c, by refl, have h_orth_mem := orthogonal_projection_mem_subspace_eq_self ind, rw [← h_coe_ind, h_orth_mem], end lemma inner_condexp_L2_eq_inner_fun (hm : m ≤ m0) (f g : α →₂[μ] E) (hg : ae_strongly_measurable' m g μ) : ⟪(condexp_L2 𝕜 hm f : α →₂[μ] E), g⟫₂ = ⟪f, g⟫₂ := begin symmetry, rw [← sub_eq_zero, ← inner_sub_left, condexp_L2], simp only [mem_Lp_meas_iff_ae_strongly_measurable'.mpr hg, orthogonal_projection_inner_eq_zero], end section real variables {hm : m ≤ m0} lemma integral_condexp_L2_eq_of_fin_meas_real (f : Lp 𝕜 2 μ) (hs : measurable_set[m] s) (hμs : μ s ≠ ∞) : ∫ x in s, condexp_L2 𝕜 hm f x ∂μ = ∫ x in s, f x ∂μ := begin rw ← L2.inner_indicator_const_Lp_one (hm s hs) hμs, have h_eq_inner : ∫ x in s, condexp_L2 𝕜 hm f x ∂μ = inner (indicator_const_Lp 2 (hm s hs) hμs (1 : 𝕜)) (condexp_L2 𝕜 hm f), { rw L2.inner_indicator_const_Lp_one (hm s hs) hμs, congr, }, rw [h_eq_inner, ← inner_condexp_L2_left_eq_right, condexp_L2_indicator_of_measurable hm hs hμs], end lemma lintegral_nnnorm_condexp_L2_le (hs : measurable_set[m] s) (hμs : μ s ≠ ∞) (f : Lp ℝ 2 μ) : ∫⁻ x in s, ‖condexp_L2 ℝ hm f x‖₊ ∂μ ≤ ∫⁻ x in s, ‖f x‖₊ ∂μ := begin let h_meas := Lp_meas.ae_strongly_measurable' (condexp_L2 ℝ hm f), let g := h_meas.some, have hg_meas : strongly_measurable[m] g, from h_meas.some_spec.1, have hg_eq : g =ᵐ[μ] condexp_L2 ℝ hm f, from h_meas.some_spec.2.symm, have hg_eq_restrict : g =ᵐ[μ.restrict s] condexp_L2 ℝ hm f, from ae_restrict_of_ae hg_eq, have hg_nnnorm_eq : (λ x, (‖g x‖₊ : ℝ≥0∞)) =ᵐ[μ.restrict s] (λ x, (‖condexp_L2 ℝ hm f x‖₊ : ℝ≥0∞)), { refine hg_eq_restrict.mono (λ x hx, _), dsimp only, rw hx, }, rw lintegral_congr_ae hg_nnnorm_eq.symm, refine lintegral_nnnorm_le_of_forall_fin_meas_integral_eq hm (Lp.strongly_measurable f) _ _ _ _ hs hμs, { exact integrable_on_Lp_of_measure_ne_top f fact_one_le_two_ennreal.elim hμs, }, { exact hg_meas, }, { rw [integrable_on, integrable_congr hg_eq_restrict], exact integrable_on_condexp_L2_of_measure_ne_top hm hμs f, }, { intros t ht hμt, rw ← integral_condexp_L2_eq_of_fin_meas_real f ht hμt.ne, exact set_integral_congr_ae (hm t ht) (hg_eq.mono (λ x hx _, hx)), }, end lemma condexp_L2_ae_eq_zero_of_ae_eq_zero (hs : measurable_set[m] s) (hμs : μ s ≠ ∞) {f : Lp ℝ 2 μ} (hf : f =ᵐ[μ.restrict s] 0) : condexp_L2 ℝ hm f =ᵐ[μ.restrict s] 0 := begin suffices h_nnnorm_eq_zero : ∫⁻ x in s, ‖condexp_L2 ℝ hm f x‖₊ ∂μ = 0, { rw lintegral_eq_zero_iff at h_nnnorm_eq_zero, refine h_nnnorm_eq_zero.mono (λ x hx, _), dsimp only at hx, rw pi.zero_apply at hx ⊢, { rwa [ennreal.coe_eq_zero, nnnorm_eq_zero] at hx, }, { refine measurable.coe_nnreal_ennreal (measurable.nnnorm _), rw Lp_meas_coe, exact (Lp.strongly_measurable _).measurable }, }, refine le_antisymm _ (zero_le _), refine (lintegral_nnnorm_condexp_L2_le hs hμs f).trans (le_of_eq _), rw lintegral_eq_zero_iff, { refine hf.mono (λ x hx, _), dsimp only, rw hx, simp, }, { exact (Lp.strongly_measurable _).ennnorm, }, end lemma lintegral_nnnorm_condexp_L2_indicator_le_real (hs : measurable_set s) (hμs : μ s ≠ ∞) (ht : measurable_set[m] t) (hμt : μ t ≠ ∞) : ∫⁻ a in t, ‖condexp_L2 ℝ hm (indicator_const_Lp 2 hs hμs (1 : ℝ)) a‖₊ ∂μ ≤ μ (s ∩ t) := begin refine (lintegral_nnnorm_condexp_L2_le ht hμt _).trans (le_of_eq _), have h_eq : ∫⁻ x in t, ‖(indicator_const_Lp 2 hs hμs (1 : ℝ)) x‖₊ ∂μ = ∫⁻ x in t, s.indicator (λ x, (1 : ℝ≥0∞)) x ∂μ, { refine lintegral_congr_ae (ae_restrict_of_ae _), refine (@indicator_const_Lp_coe_fn _ _ _ 2 _ _ _ hs hμs (1 : ℝ)).mono (λ x hx, _), rw hx, classical, simp_rw set.indicator_apply, split_ifs; simp, }, rw [h_eq, lintegral_indicator _ hs, lintegral_const, measure.restrict_restrict hs], simp only [one_mul, set.univ_inter, measurable_set.univ, measure.restrict_apply], end end real /-- `condexp_L2` commutes with taking inner products with constants. See the lemma `condexp_L2_comp_continuous_linear_map` for a more general result about commuting with continuous linear maps. -/ lemma condexp_L2_const_inner (hm : m ≤ m0) (f : Lp E 2 μ) (c : E) : condexp_L2 𝕜 hm (((Lp.mem_ℒp f).const_inner c).to_Lp (λ a, ⟪c, f a⟫)) =ᵐ[μ] λ a, ⟪c, condexp_L2 𝕜 hm f a⟫ := begin rw Lp_meas_coe, have h_mem_Lp : mem_ℒp (λ a, ⟪c, condexp_L2 𝕜 hm f a⟫) 2 μ, { refine mem_ℒp.const_inner _ _, rw Lp_meas_coe, exact Lp.mem_ℒp _, }, have h_eq : h_mem_Lp.to_Lp _ =ᵐ[μ] λ a, ⟪c, condexp_L2 𝕜 hm f a⟫, from h_mem_Lp.coe_fn_to_Lp, refine eventually_eq.trans _ h_eq, refine Lp.ae_eq_of_forall_set_integral_eq' 𝕜 hm _ _ two_ne_zero ennreal.coe_ne_top (λ s hs hμs, integrable_on_condexp_L2_of_measure_ne_top hm hμs.ne _) _ _ _ _, { intros s hs hμs, rw [integrable_on, integrable_congr (ae_restrict_of_ae h_eq)], exact (integrable_on_condexp_L2_of_measure_ne_top hm hμs.ne _).const_inner _, }, { intros s hs hμs, rw [← Lp_meas_coe, integral_condexp_L2_eq_of_fin_meas_real _ hs hμs.ne, integral_congr_ae (ae_restrict_of_ae h_eq), Lp_meas_coe, ← L2.inner_indicator_const_Lp_eq_set_integral_inner 𝕜 ↑(condexp_L2 𝕜 hm f) (hm s hs) c hμs.ne, ← inner_condexp_L2_left_eq_right, condexp_L2_indicator_of_measurable, L2.inner_indicator_const_Lp_eq_set_integral_inner 𝕜 f (hm s hs) c hμs.ne, set_integral_congr_ae (hm s hs) ((mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp f).const_inner c)).mono (λ x hx hxs, hx))], }, { rw ← Lp_meas_coe, exact Lp_meas.ae_strongly_measurable' _, }, { refine ae_strongly_measurable'.congr _ h_eq.symm, exact (Lp_meas.ae_strongly_measurable' _).const_inner _, }, end /-- `condexp_L2` verifies the equality of integrals defining the conditional expectation. -/ lemma integral_condexp_L2_eq (hm : m ≤ m0) (f : Lp E' 2 μ) (hs : measurable_set[m] s) (hμs : μ s ≠ ∞) : ∫ x in s, condexp_L2 𝕜 hm f x ∂μ = ∫ x in s, f x ∂μ := begin rw [← sub_eq_zero, Lp_meas_coe, ← integral_sub' (integrable_on_Lp_of_measure_ne_top _ fact_one_le_two_ennreal.elim hμs) (integrable_on_Lp_of_measure_ne_top _ fact_one_le_two_ennreal.elim hμs)], refine integral_eq_zero_of_forall_integral_inner_eq_zero 𝕜 _ _ _, { rw integrable_congr (ae_restrict_of_ae (Lp.coe_fn_sub ↑(condexp_L2 𝕜 hm f) f).symm), exact integrable_on_Lp_of_measure_ne_top _ fact_one_le_two_ennreal.elim hμs, }, intro c, simp_rw [pi.sub_apply, inner_sub_right], rw integral_sub ((integrable_on_Lp_of_measure_ne_top _ fact_one_le_two_ennreal.elim hμs).const_inner c) ((integrable_on_Lp_of_measure_ne_top _ fact_one_le_two_ennreal.elim hμs).const_inner c), have h_ae_eq_f := mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp f).const_inner c), rw [← Lp_meas_coe, sub_eq_zero, ← set_integral_congr_ae (hm s hs) ((condexp_L2_const_inner hm f c).mono (λ x hx _, hx)), ← set_integral_congr_ae (hm s hs) (h_ae_eq_f.mono (λ x hx _, hx))], exact integral_condexp_L2_eq_of_fin_meas_real _ hs hμs, end variables {E'' 𝕜' : Type*} [is_R_or_C 𝕜'] [normed_add_comm_group E''] [inner_product_space 𝕜' E''] [complete_space E''] [normed_space ℝ E''] variables (𝕜 𝕜') lemma condexp_L2_comp_continuous_linear_map (hm : m ≤ m0) (T : E' →L[ℝ] E'') (f : α →₂[μ] E') : (condexp_L2 𝕜' hm (T.comp_Lp f) : α →₂[μ] E'') =ᵐ[μ] T.comp_Lp (condexp_L2 𝕜 hm f : α →₂[μ] E') := begin refine Lp.ae_eq_of_forall_set_integral_eq' 𝕜' hm _ _ two_ne_zero ennreal.coe_ne_top (λ s hs hμs, integrable_on_condexp_L2_of_measure_ne_top hm hμs.ne _) (λ s hs hμs, integrable_on_Lp_of_measure_ne_top _ fact_one_le_two_ennreal.elim hμs.ne) _ _ _, { intros s hs hμs, rw [T.set_integral_comp_Lp _ (hm s hs), T.integral_comp_comm (integrable_on_Lp_of_measure_ne_top _ fact_one_le_two_ennreal.elim hμs.ne), ← Lp_meas_coe, ← Lp_meas_coe, integral_condexp_L2_eq hm f hs hμs.ne, integral_condexp_L2_eq hm (T.comp_Lp f) hs hμs.ne, T.set_integral_comp_Lp _ (hm s hs), T.integral_comp_comm (integrable_on_Lp_of_measure_ne_top f fact_one_le_two_ennreal.elim hμs.ne)], }, { rw ← Lp_meas_coe, exact Lp_meas.ae_strongly_measurable' _, }, { have h_coe := T.coe_fn_comp_Lp (condexp_L2 𝕜 hm f : α →₂[μ] E'), rw ← eventually_eq at h_coe, refine ae_strongly_measurable'.congr _ h_coe.symm, exact (Lp_meas.ae_strongly_measurable' (condexp_L2 𝕜 hm f)).continuous_comp T.continuous, }, end variables {𝕜 𝕜'} section condexp_L2_indicator variables (𝕜) lemma condexp_L2_indicator_ae_eq_smul (hm : m ≤ m0) (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : E') : condexp_L2 𝕜 hm (indicator_const_Lp 2 hs hμs x) =ᵐ[μ] λ a, (condexp_L2 ℝ hm (indicator_const_Lp 2 hs hμs (1 : ℝ)) a) • x := begin rw indicator_const_Lp_eq_to_span_singleton_comp_Lp hs hμs x, have h_comp := condexp_L2_comp_continuous_linear_map ℝ 𝕜 hm (to_span_singleton ℝ x) (indicator_const_Lp 2 hs hμs (1 : ℝ)), rw ← Lp_meas_coe at h_comp, refine h_comp.trans _, exact (to_span_singleton ℝ x).coe_fn_comp_Lp _, end lemma condexp_L2_indicator_eq_to_span_singleton_comp (hm : m ≤ m0) (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : E') : (condexp_L2 𝕜 hm (indicator_const_Lp 2 hs hμs x) : α →₂[μ] E') = (to_span_singleton ℝ x).comp_Lp (condexp_L2 ℝ hm (indicator_const_Lp 2 hs hμs (1 : ℝ))) := begin ext1, rw ← Lp_meas_coe, refine (condexp_L2_indicator_ae_eq_smul 𝕜 hm hs hμs x).trans _, have h_comp := (to_span_singleton ℝ x).coe_fn_comp_Lp (condexp_L2 ℝ hm (indicator_const_Lp 2 hs hμs (1 : ℝ)) : α →₂[μ] ℝ), rw ← eventually_eq at h_comp, refine eventually_eq.trans _ h_comp.symm, refine eventually_of_forall (λ y, _), refl, end variables {𝕜} lemma set_lintegral_nnnorm_condexp_L2_indicator_le (hm : m ≤ m0) (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : E') {t : set α} (ht : measurable_set[m] t) (hμt : μ t ≠ ∞) : ∫⁻ a in t, ‖condexp_L2 𝕜 hm (indicator_const_Lp 2 hs hμs x) a‖₊ ∂μ ≤ μ (s ∩ t) * ‖x‖₊ := calc ∫⁻ a in t, ‖condexp_L2 𝕜 hm (indicator_const_Lp 2 hs hμs x) a‖₊ ∂μ = ∫⁻ a in t, ‖(condexp_L2 ℝ hm (indicator_const_Lp 2 hs hμs (1 : ℝ)) a) • x‖₊ ∂μ : set_lintegral_congr_fun (hm t ht) ((condexp_L2_indicator_ae_eq_smul 𝕜 hm hs hμs x).mono (λ a ha hat, by rw ha)) ... = ∫⁻ a in t, ‖condexp_L2 ℝ hm (indicator_const_Lp 2 hs hμs (1 : ℝ)) a‖₊ ∂μ * ‖x‖₊ : begin simp_rw [nnnorm_smul, ennreal.coe_mul], rw [lintegral_mul_const, Lp_meas_coe], exact (Lp.strongly_measurable _).ennnorm end ... ≤ μ (s ∩ t) * ‖x‖₊ : mul_le_mul_right' (lintegral_nnnorm_condexp_L2_indicator_le_real hs hμs ht hμt) _ lemma lintegral_nnnorm_condexp_L2_indicator_le (hm : m ≤ m0) (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : E') [sigma_finite (μ.trim hm)] : ∫⁻ a, ‖condexp_L2 𝕜 hm (indicator_const_Lp 2 hs hμs x) a‖₊ ∂μ ≤ μ s * ‖x‖₊ := begin refine lintegral_le_of_forall_fin_meas_le' hm (μ s * ‖x‖₊) _ (λ t ht hμt, _), { rw Lp_meas_coe, exact (Lp.ae_strongly_measurable _).ennnorm }, refine (set_lintegral_nnnorm_condexp_L2_indicator_le hm hs hμs x ht hμt).trans _, exact mul_le_mul_right' (measure_mono (set.inter_subset_left _ _)) _ end /-- If the measure `μ.trim hm` is sigma-finite, then the conditional expectation of a measurable set with finite measure is integrable. -/ lemma integrable_condexp_L2_indicator (hm : m ≤ m0) [sigma_finite (μ.trim hm)] (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : E') : integrable (condexp_L2 𝕜 hm (indicator_const_Lp 2 hs hμs x)) μ := begin refine integrable_of_forall_fin_meas_le' hm (μ s * ‖x‖₊) (ennreal.mul_lt_top hμs ennreal.coe_ne_top) _ _, { rw Lp_meas_coe, exact Lp.ae_strongly_measurable _, }, { refine λ t ht hμt, (set_lintegral_nnnorm_condexp_L2_indicator_le hm hs hμs x ht hμt).trans _, exact mul_le_mul_right' (measure_mono (set.inter_subset_left _ _)) _, }, end end condexp_L2_indicator section condexp_ind_smul variables [normed_space ℝ G] {hm : m ≤ m0} /-- Conditional expectation of the indicator of a measurable set with finite measure, in L2. -/ def condexp_ind_smul (hm : m ≤ m0) (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : G) : Lp G 2 μ := (to_span_singleton ℝ x).comp_LpL 2 μ (condexp_L2 ℝ hm (indicator_const_Lp 2 hs hμs (1 : ℝ))) lemma ae_strongly_measurable'_condexp_ind_smul (hm : m ≤ m0) (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : G) : ae_strongly_measurable' m (condexp_ind_smul hm hs hμs x) μ := begin have h : ae_strongly_measurable' m (condexp_L2 ℝ hm (indicator_const_Lp 2 hs hμs (1 : ℝ))) μ, from ae_strongly_measurable'_condexp_L2 _ _, rw condexp_ind_smul, suffices : ae_strongly_measurable' m ((to_span_singleton ℝ x) ∘ (condexp_L2 ℝ hm (indicator_const_Lp 2 hs hμs (1 : ℝ)))) μ, { refine ae_strongly_measurable'.congr this _, refine eventually_eq.trans _ (coe_fn_comp_LpL _ _).symm, rw Lp_meas_coe, }, exact ae_strongly_measurable'.continuous_comp (to_span_singleton ℝ x).continuous h, end lemma condexp_ind_smul_add (hs : measurable_set s) (hμs : μ s ≠ ∞) (x y : G) : condexp_ind_smul hm hs hμs (x + y) = condexp_ind_smul hm hs hμs x + condexp_ind_smul hm hs hμs y := by { simp_rw [condexp_ind_smul], rw [to_span_singleton_add, add_comp_LpL, add_apply], } lemma condexp_ind_smul_smul (hs : measurable_set s) (hμs : μ s ≠ ∞) (c : ℝ) (x : G) : condexp_ind_smul hm hs hμs (c • x) = c • condexp_ind_smul hm hs hμs x := by { simp_rw [condexp_ind_smul], rw [to_span_singleton_smul, smul_comp_LpL, smul_apply], } lemma condexp_ind_smul_smul' [normed_space ℝ F] [smul_comm_class ℝ 𝕜 F] (hs : measurable_set s) (hμs : μ s ≠ ∞) (c : 𝕜) (x : F) : condexp_ind_smul hm hs hμs (c • x) = c • condexp_ind_smul hm hs hμs x := by rw [condexp_ind_smul, condexp_ind_smul, to_span_singleton_smul', (to_span_singleton ℝ x).smul_comp_LpL_apply c ↑(condexp_L2 ℝ hm (indicator_const_Lp 2 hs hμs (1 : ℝ)))] lemma condexp_ind_smul_ae_eq_smul (hm : m ≤ m0) (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : G) : condexp_ind_smul hm hs hμs x =ᵐ[μ] λ a, (condexp_L2 ℝ hm (indicator_const_Lp 2 hs hμs (1 : ℝ)) a) • x := (to_span_singleton ℝ x).coe_fn_comp_LpL _ lemma set_lintegral_nnnorm_condexp_ind_smul_le (hm : m ≤ m0) (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : G) {t : set α} (ht : measurable_set[m] t) (hμt : μ t ≠ ∞) : ∫⁻ a in t, ‖condexp_ind_smul hm hs hμs x a‖₊ ∂μ ≤ μ (s ∩ t) * ‖x‖₊ := calc ∫⁻ a in t, ‖condexp_ind_smul hm hs hμs x a‖₊ ∂μ = ∫⁻ a in t, ‖condexp_L2 ℝ hm (indicator_const_Lp 2 hs hμs (1 : ℝ)) a • x‖₊ ∂μ : set_lintegral_congr_fun (hm t ht) ((condexp_ind_smul_ae_eq_smul hm hs hμs x).mono (λ a ha hat, by rw ha )) ... = ∫⁻ a in t, ‖condexp_L2 ℝ hm (indicator_const_Lp 2 hs hμs (1 : ℝ)) a‖₊ ∂μ * ‖x‖₊ : begin simp_rw [nnnorm_smul, ennreal.coe_mul], rw [lintegral_mul_const, Lp_meas_coe], exact (Lp.strongly_measurable _).ennnorm end ... ≤ μ (s ∩ t) * ‖x‖₊ : mul_le_mul_right' (lintegral_nnnorm_condexp_L2_indicator_le_real hs hμs ht hμt) _ lemma lintegral_nnnorm_condexp_ind_smul_le (hm : m ≤ m0) (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : G) [sigma_finite (μ.trim hm)] : ∫⁻ a, ‖condexp_ind_smul hm hs hμs x a‖₊ ∂μ ≤ μ s * ‖x‖₊ := begin refine lintegral_le_of_forall_fin_meas_le' hm (μ s * ‖x‖₊) _ (λ t ht hμt, _), { exact (Lp.ae_strongly_measurable _).ennnorm }, refine (set_lintegral_nnnorm_condexp_ind_smul_le hm hs hμs x ht hμt).trans _, exact mul_le_mul_right' (measure_mono (set.inter_subset_left _ _)) _ end /-- If the measure `μ.trim hm` is sigma-finite, then the conditional expectation of a measurable set with finite measure is integrable. -/ lemma integrable_condexp_ind_smul (hm : m ≤ m0) [sigma_finite (μ.trim hm)] (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : G) : integrable (condexp_ind_smul hm hs hμs x) μ := begin refine integrable_of_forall_fin_meas_le' hm (μ s * ‖x‖₊) (ennreal.mul_lt_top hμs ennreal.coe_ne_top) _ _, { exact Lp.ae_strongly_measurable _, }, { refine λ t ht hμt, (set_lintegral_nnnorm_condexp_ind_smul_le hm hs hμs x ht hμt).trans _, exact mul_le_mul_right' (measure_mono (set.inter_subset_left _ _)) _, }, end lemma condexp_ind_smul_empty {x : G} : condexp_ind_smul hm measurable_set.empty ((@measure_empty _ _ μ).le.trans_lt ennreal.coe_lt_top).ne x = 0 := begin rw [condexp_ind_smul, indicator_const_empty], simp only [coe_fn_coe_base, submodule.coe_zero, continuous_linear_map.map_zero], end lemma set_integral_condexp_L2_indicator (hs : measurable_set[m] s) (ht : measurable_set t) (hμs : μ s ≠ ∞) (hμt : μ t ≠ ∞) : ∫ x in s, (condexp_L2 ℝ hm (indicator_const_Lp 2 ht hμt (1 : ℝ))) x ∂μ = (μ (t ∩ s)).to_real := calc ∫ x in s, (condexp_L2 ℝ hm (indicator_const_Lp 2 ht hμt (1 : ℝ))) x ∂μ = ∫ x in s, indicator_const_Lp 2 ht hμt (1 : ℝ) x ∂μ : @integral_condexp_L2_eq α _ ℝ _ _ _ _ _ _ _ _ _ hm (indicator_const_Lp 2 ht hμt (1 : ℝ)) hs hμs ... = (μ (t ∩ s)).to_real • 1 : set_integral_indicator_const_Lp (hm s hs) ht hμt (1 : ℝ) ... = (μ (t ∩ s)).to_real : by rw [smul_eq_mul, mul_one] lemma set_integral_condexp_ind_smul (hs : measurable_set[m] s) (ht : measurable_set t) (hμs : μ s ≠ ∞) (hμt : μ t ≠ ∞) (x : G') : ∫ a in s, (condexp_ind_smul hm ht hμt x) a ∂μ = (μ (t ∩ s)).to_real • x := calc ∫ a in s, (condexp_ind_smul hm ht hμt x) a ∂μ = (∫ a in s, (condexp_L2 ℝ hm (indicator_const_Lp 2 ht hμt (1 : ℝ)) a • x) ∂μ) : set_integral_congr_ae (hm s hs) ((condexp_ind_smul_ae_eq_smul hm ht hμt x).mono (λ x hx hxs, hx)) ... = (∫ a in s, condexp_L2 ℝ hm (indicator_const_Lp 2 ht hμt (1 : ℝ)) a ∂μ) • x : integral_smul_const _ x ... = (μ (t ∩ s)).to_real • x : by rw set_integral_condexp_L2_indicator hs ht hμs hμt lemma condexp_L2_indicator_nonneg (hm : m ≤ m0) (hs : measurable_set s) (hμs : μ s ≠ ∞) [sigma_finite (μ.trim hm)] : 0 ≤ᵐ[μ] condexp_L2 ℝ hm (indicator_const_Lp 2 hs hμs (1 : ℝ)) := begin have h : ae_strongly_measurable' m (condexp_L2 ℝ hm (indicator_const_Lp 2 hs hμs (1 : ℝ))) μ, from ae_strongly_measurable'_condexp_L2 _ _, refine eventually_le.trans_eq _ h.ae_eq_mk.symm, refine @ae_le_of_ae_le_trim _ _ _ _ _ _ hm _ _ _, refine ae_nonneg_of_forall_set_integral_nonneg_of_sigma_finite _ _, { intros t ht hμt, refine @integrable.integrable_on _ _ m _ _ _ _ _, refine integrable.trim hm _ _, { rw integrable_congr h.ae_eq_mk.symm, exact integrable_condexp_L2_indicator hm hs hμs _, }, { exact h.strongly_measurable_mk, }, }, { intros t ht hμt, rw ← set_integral_trim hm h.strongly_measurable_mk ht, have h_ae : ∀ᵐ x ∂μ, x ∈ t → h.mk _ x = condexp_L2 ℝ hm (indicator_const_Lp 2 hs hμs (1 : ℝ)) x, { filter_upwards [h.ae_eq_mk] with x hx, exact λ _, hx.symm, }, rw [set_integral_congr_ae (hm t ht) h_ae, set_integral_condexp_L2_indicator ht hs ((le_trim hm).trans_lt hμt).ne hμs], exact ennreal.to_real_nonneg, }, end lemma condexp_ind_smul_nonneg {E} [normed_lattice_add_comm_group E] [normed_space ℝ E] [ordered_smul ℝ E] [sigma_finite (μ.trim hm)] (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : E) (hx : 0 ≤ x) : 0 ≤ᵐ[μ] condexp_ind_smul hm hs hμs x := begin refine eventually_le.trans_eq _ (condexp_ind_smul_ae_eq_smul hm hs hμs x).symm, filter_upwards [condexp_L2_indicator_nonneg hm hs hμs] with a ha, exact smul_nonneg ha hx, end end condexp_ind_smul end condexp_L2 section condexp_ind /-! ## Conditional expectation of an indicator as a continuous linear map. The goal of this section is to build `condexp_ind (hm : m ≤ m0) (μ : measure α) (s : set s) : G →L[ℝ] α →₁[μ] G`, which takes `x : G` to the conditional expectation of the indicator of the set `s` with value `x`, seen as an element of `α →₁[μ] G`. -/ variables {m m0 : measurable_space α} {μ : measure α} {s t : set α} [normed_space ℝ G] section condexp_ind_L1_fin /-- Conditional expectation of the indicator of a measurable set with finite measure, as a function in L1. -/ def condexp_ind_L1_fin (hm : m ≤ m0) [sigma_finite (μ.trim hm)] (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : G) : α →₁[μ] G := (integrable_condexp_ind_smul hm hs hμs x).to_L1 _ lemma condexp_ind_L1_fin_ae_eq_condexp_ind_smul (hm : m ≤ m0) [sigma_finite (μ.trim hm)] (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : G) : condexp_ind_L1_fin hm hs hμs x =ᵐ[μ] condexp_ind_smul hm hs hμs x := (integrable_condexp_ind_smul hm hs hμs x).coe_fn_to_L1 variables {hm : m ≤ m0} [sigma_finite (μ.trim hm)] lemma condexp_ind_L1_fin_add (hs : measurable_set s) (hμs : μ s ≠ ∞) (x y : G) : condexp_ind_L1_fin hm hs hμs (x + y) = condexp_ind_L1_fin hm hs hμs x + condexp_ind_L1_fin hm hs hμs y := begin ext1, refine (mem_ℒp.coe_fn_to_Lp _).trans _, refine eventually_eq.trans _ (Lp.coe_fn_add _ _).symm, refine eventually_eq.trans _ (eventually_eq.add (mem_ℒp.coe_fn_to_Lp _).symm (mem_ℒp.coe_fn_to_Lp _).symm), rw condexp_ind_smul_add, refine (Lp.coe_fn_add _ _).trans (eventually_of_forall (λ a, _)), refl, end lemma condexp_ind_L1_fin_smul (hs : measurable_set s) (hμs : μ s ≠ ∞) (c : ℝ) (x : G) : condexp_ind_L1_fin hm hs hμs (c • x) = c • condexp_ind_L1_fin hm hs hμs x := begin ext1, refine (mem_ℒp.coe_fn_to_Lp _).trans _, refine eventually_eq.trans _ (Lp.coe_fn_smul _ _).symm, rw condexp_ind_smul_smul hs hμs c x, refine (Lp.coe_fn_smul _ _).trans _, refine (condexp_ind_L1_fin_ae_eq_condexp_ind_smul hm hs hμs x).mono (λ y hy, _), rw [pi.smul_apply, pi.smul_apply, hy], end lemma condexp_ind_L1_fin_smul' [normed_space ℝ F] [smul_comm_class ℝ 𝕜 F] (hs : measurable_set s) (hμs : μ s ≠ ∞) (c : 𝕜) (x : F) : condexp_ind_L1_fin hm hs hμs (c • x) = c • condexp_ind_L1_fin hm hs hμs x := begin ext1, refine (mem_ℒp.coe_fn_to_Lp _).trans _, refine eventually_eq.trans _ (Lp.coe_fn_smul _ _).symm, rw condexp_ind_smul_smul' hs hμs c x, refine (Lp.coe_fn_smul _ _).trans _, refine (condexp_ind_L1_fin_ae_eq_condexp_ind_smul hm hs hμs x).mono (λ y hy, _), rw [pi.smul_apply, pi.smul_apply, hy], end lemma norm_condexp_ind_L1_fin_le (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : G) : ‖condexp_ind_L1_fin hm hs hμs x‖ ≤ (μ s).to_real * ‖x‖ := begin have : 0 ≤ ∫ (a : α), ‖condexp_ind_L1_fin hm hs hμs x a‖ ∂μ, from integral_nonneg (λ a, norm_nonneg _), rw [L1.norm_eq_integral_norm, ← ennreal.to_real_of_real (norm_nonneg x), ← ennreal.to_real_mul, ← ennreal.to_real_of_real this, ennreal.to_real_le_to_real ennreal.of_real_ne_top (ennreal.mul_ne_top hμs ennreal.of_real_ne_top), of_real_integral_norm_eq_lintegral_nnnorm], swap, { rw [← mem_ℒp_one_iff_integrable], exact Lp.mem_ℒp _, }, have h_eq : ∫⁻ a, ‖condexp_ind_L1_fin hm hs hμs x a‖₊ ∂μ = ∫⁻ a, ‖condexp_ind_smul hm hs hμs x a‖₊ ∂μ, { refine lintegral_congr_ae _, refine (condexp_ind_L1_fin_ae_eq_condexp_ind_smul hm hs hμs x).mono (λ z hz, _), dsimp only, rw hz, }, rw [h_eq, of_real_norm_eq_coe_nnnorm], exact lintegral_nnnorm_condexp_ind_smul_le hm hs hμs x, end lemma condexp_ind_L1_fin_disjoint_union (hs : measurable_set s) (ht : measurable_set t) (hμs : μ s ≠ ∞) (hμt : μ t ≠ ∞) (hst : s ∩ t = ∅) (x : G) : condexp_ind_L1_fin hm (hs.union ht) ((measure_union_le s t).trans_lt (lt_top_iff_ne_top.mpr (ennreal.add_ne_top.mpr ⟨hμs, hμt⟩))).ne x = condexp_ind_L1_fin hm hs hμs x + condexp_ind_L1_fin hm ht hμt x := begin ext1, have hμst := ((measure_union_le s t).trans_lt (lt_top_iff_ne_top.mpr (ennreal.add_ne_top.mpr ⟨hμs, hμt⟩))).ne, refine (condexp_ind_L1_fin_ae_eq_condexp_ind_smul hm (hs.union ht) hμst x).trans _, refine eventually_eq.trans _ (Lp.coe_fn_add _ _).symm, have hs_eq := condexp_ind_L1_fin_ae_eq_condexp_ind_smul hm hs hμs x, have ht_eq := condexp_ind_L1_fin_ae_eq_condexp_ind_smul hm ht hμt x, refine eventually_eq.trans _ (eventually_eq.add hs_eq.symm ht_eq.symm), rw condexp_ind_smul, rw indicator_const_Lp_disjoint_union hs ht hμs hμt hst (1 : ℝ), rw (condexp_L2 ℝ hm).map_add, push_cast, rw ((to_span_singleton ℝ x).comp_LpL 2 μ).map_add, refine (Lp.coe_fn_add _ _).trans _, refine eventually_of_forall (λ y, _), refl, end end condexp_ind_L1_fin open_locale classical section condexp_ind_L1 /-- Conditional expectation of the indicator of a set, as a function in L1. Its value for sets which are not both measurable and of finite measure is not used: we set it to 0. -/ def condexp_ind_L1 {m m0 : measurable_space α} (hm : m ≤ m0) (μ : measure α) (s : set α) [sigma_finite (μ.trim hm)] (x : G) : α →₁[μ] G := if hs : measurable_set s ∧ μ s ≠ ∞ then condexp_ind_L1_fin hm hs.1 hs.2 x else 0 variables {hm : m ≤ m0} [sigma_finite (μ.trim hm)] lemma condexp_ind_L1_of_measurable_set_of_measure_ne_top (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : G) : condexp_ind_L1 hm μ s x = condexp_ind_L1_fin hm hs hμs x := by simp only [condexp_ind_L1, and.intro hs hμs, dif_pos, ne.def, not_false_iff, and_self] lemma condexp_ind_L1_of_measure_eq_top (hμs : μ s = ∞) (x : G) : condexp_ind_L1 hm μ s x = 0 := by simp only [condexp_ind_L1, hμs, eq_self_iff_true, not_true, ne.def, dif_neg, not_false_iff, and_false] lemma condexp_ind_L1_of_not_measurable_set (hs : ¬ measurable_set s) (x : G) : condexp_ind_L1 hm μ s x = 0 := by simp only [condexp_ind_L1, hs, dif_neg, not_false_iff, false_and] lemma condexp_ind_L1_add (x y : G) : condexp_ind_L1 hm μ s (x + y) = condexp_ind_L1 hm μ s x + condexp_ind_L1 hm μ s y := begin by_cases hs : measurable_set s, swap, {simp_rw condexp_ind_L1_of_not_measurable_set hs, rw zero_add, }, by_cases hμs : μ s = ∞, { simp_rw condexp_ind_L1_of_measure_eq_top hμs, rw zero_add, }, { simp_rw condexp_ind_L1_of_measurable_set_of_measure_ne_top hs hμs, exact condexp_ind_L1_fin_add hs hμs x y, }, end lemma condexp_ind_L1_smul (c : ℝ) (x : G) : condexp_ind_L1 hm μ s (c • x) = c • condexp_ind_L1 hm μ s x := begin by_cases hs : measurable_set s, swap, {simp_rw condexp_ind_L1_of_not_measurable_set hs, rw smul_zero, }, by_cases hμs : μ s = ∞, { simp_rw condexp_ind_L1_of_measure_eq_top hμs, rw smul_zero, }, { simp_rw condexp_ind_L1_of_measurable_set_of_measure_ne_top hs hμs, exact condexp_ind_L1_fin_smul hs hμs c x, }, end lemma condexp_ind_L1_smul' [normed_space ℝ F] [smul_comm_class ℝ 𝕜 F] (c : 𝕜) (x : F) : condexp_ind_L1 hm μ s (c • x) = c • condexp_ind_L1 hm μ s x := begin by_cases hs : measurable_set s, swap, {simp_rw condexp_ind_L1_of_not_measurable_set hs, rw smul_zero, }, by_cases hμs : μ s = ∞, { simp_rw condexp_ind_L1_of_measure_eq_top hμs, rw smul_zero, }, { simp_rw condexp_ind_L1_of_measurable_set_of_measure_ne_top hs hμs, exact condexp_ind_L1_fin_smul' hs hμs c x, }, end lemma norm_condexp_ind_L1_le (x : G) : ‖condexp_ind_L1 hm μ s x‖ ≤ (μ s).to_real * ‖x‖ := begin by_cases hs : measurable_set s, swap, {simp_rw condexp_ind_L1_of_not_measurable_set hs, rw Lp.norm_zero, exact mul_nonneg ennreal.to_real_nonneg (norm_nonneg _), }, by_cases hμs : μ s = ∞, { rw [condexp_ind_L1_of_measure_eq_top hμs x, Lp.norm_zero], exact mul_nonneg ennreal.to_real_nonneg (norm_nonneg _), }, { rw condexp_ind_L1_of_measurable_set_of_measure_ne_top hs hμs x, exact norm_condexp_ind_L1_fin_le hs hμs x, }, end lemma continuous_condexp_ind_L1 : continuous (λ x : G, condexp_ind_L1 hm μ s x) := continuous_of_linear_of_bound condexp_ind_L1_add condexp_ind_L1_smul norm_condexp_ind_L1_le lemma condexp_ind_L1_disjoint_union (hs : measurable_set s) (ht : measurable_set t) (hμs : μ s ≠ ∞) (hμt : μ t ≠ ∞) (hst : s ∩ t = ∅) (x : G) : condexp_ind_L1 hm μ (s ∪ t) x = condexp_ind_L1 hm μ s x + condexp_ind_L1 hm μ t x := begin have hμst : μ (s ∪ t) ≠ ∞, from ((measure_union_le s t).trans_lt (lt_top_iff_ne_top.mpr (ennreal.add_ne_top.mpr ⟨hμs, hμt⟩))).ne, rw [condexp_ind_L1_of_measurable_set_of_measure_ne_top hs hμs x, condexp_ind_L1_of_measurable_set_of_measure_ne_top ht hμt x, condexp_ind_L1_of_measurable_set_of_measure_ne_top (hs.union ht) hμst x], exact condexp_ind_L1_fin_disjoint_union hs ht hμs hμt hst x, end end condexp_ind_L1 /-- Conditional expectation of the indicator of a set, as a linear map from `G` to L1. -/ def condexp_ind {m m0 : measurable_space α} (hm : m ≤ m0) (μ : measure α) [sigma_finite (μ.trim hm)] (s : set α) : G →L[ℝ] α →₁[μ] G := { to_fun := condexp_ind_L1 hm μ s, map_add' := condexp_ind_L1_add, map_smul' := condexp_ind_L1_smul, cont := continuous_condexp_ind_L1, } lemma condexp_ind_ae_eq_condexp_ind_smul (hm : m ≤ m0) [sigma_finite (μ.trim hm)] (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : G) : condexp_ind hm μ s x =ᵐ[μ] condexp_ind_smul hm hs hμs x := begin refine eventually_eq.trans _ (condexp_ind_L1_fin_ae_eq_condexp_ind_smul hm hs hμs x), simp [condexp_ind, condexp_ind_L1, hs, hμs], end variables {hm : m ≤ m0} [sigma_finite (μ.trim hm)] lemma ae_strongly_measurable'_condexp_ind (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : G) : ae_strongly_measurable' m (condexp_ind hm μ s x) μ := ae_strongly_measurable'.congr (ae_strongly_measurable'_condexp_ind_smul hm hs hμs x) (condexp_ind_ae_eq_condexp_ind_smul hm hs hμs x).symm @[simp] lemma condexp_ind_empty : condexp_ind hm μ ∅ = (0 : G →L[ℝ] α →₁[μ] G) := begin ext1, ext1, refine (condexp_ind_ae_eq_condexp_ind_smul hm measurable_set.empty (by simp) x).trans _, rw condexp_ind_smul_empty, refine (Lp.coe_fn_zero G 2 μ).trans _, refine eventually_eq.trans _ (Lp.coe_fn_zero G 1 μ).symm, refl, end lemma condexp_ind_smul' [normed_space ℝ F] [smul_comm_class ℝ 𝕜 F] (c : 𝕜) (x : F) : condexp_ind hm μ s (c • x) = c • condexp_ind hm μ s x := condexp_ind_L1_smul' c x lemma norm_condexp_ind_apply_le (x : G) : ‖condexp_ind hm μ s x‖ ≤ (μ s).to_real * ‖x‖ := norm_condexp_ind_L1_le x lemma norm_condexp_ind_le : ‖(condexp_ind hm μ s : G →L[ℝ] α →₁[μ] G)‖ ≤ (μ s).to_real := continuous_linear_map.op_norm_le_bound _ ennreal.to_real_nonneg norm_condexp_ind_apply_le lemma condexp_ind_disjoint_union_apply (hs : measurable_set s) (ht : measurable_set t) (hμs : μ s ≠ ∞) (hμt : μ t ≠ ∞) (hst : s ∩ t = ∅) (x : G) : condexp_ind hm μ (s ∪ t) x = condexp_ind hm μ s x + condexp_ind hm μ t x := condexp_ind_L1_disjoint_union hs ht hμs hμt hst x lemma condexp_ind_disjoint_union (hs : measurable_set s) (ht : measurable_set t) (hμs : μ s ≠ ∞) (hμt : μ t ≠ ∞) (hst : s ∩ t = ∅) : (condexp_ind hm μ (s ∪ t) : G →L[ℝ] α →₁[μ] G) = condexp_ind hm μ s + condexp_ind hm μ t := by { ext1, push_cast, exact condexp_ind_disjoint_union_apply hs ht hμs hμt hst x, } variables (G) lemma dominated_fin_meas_additive_condexp_ind (hm : m ≤ m0) (μ : measure α) [sigma_finite (μ.trim hm)] : dominated_fin_meas_additive μ (condexp_ind hm μ : set α → G →L[ℝ] α →₁[μ] G) 1 := ⟨λ s t, condexp_ind_disjoint_union, λ s _ _, norm_condexp_ind_le.trans (one_mul _).symm.le⟩ variables {G} lemma set_integral_condexp_ind (hs : measurable_set[m] s) (ht : measurable_set t) (hμs : μ s ≠ ∞) (hμt : μ t ≠ ∞) (x : G') : ∫ a in s, condexp_ind hm μ t x a ∂μ = (μ (t ∩ s)).to_real • x := calc ∫ a in s, condexp_ind hm μ t x a ∂μ = ∫ a in s, condexp_ind_smul hm ht hμt x a ∂μ : set_integral_congr_ae (hm s hs) ((condexp_ind_ae_eq_condexp_ind_smul hm ht hμt x).mono (λ x hx hxs, hx)) ... = (μ (t ∩ s)).to_real • x : set_integral_condexp_ind_smul hs ht hμs hμt x lemma condexp_ind_of_measurable (hs : measurable_set[m] s) (hμs : μ s ≠ ∞) (c : G) : condexp_ind hm μ s c = indicator_const_Lp 1 (hm s hs) hμs c := begin ext1, refine eventually_eq.trans _ indicator_const_Lp_coe_fn.symm, refine (condexp_ind_ae_eq_condexp_ind_smul hm (hm s hs) hμs c).trans _, refine (condexp_ind_smul_ae_eq_smul hm (hm s hs) hμs c).trans _, rw [Lp_meas_coe, condexp_L2_indicator_of_measurable hm hs hμs (1 : ℝ)], refine (@indicator_const_Lp_coe_fn α _ _ 2 μ _ s (hm s hs) hμs (1 : ℝ)).mono (λ x hx, _), dsimp only, rw hx, by_cases hx_mem : x ∈ s; simp [hx_mem], end lemma condexp_ind_nonneg {E} [normed_lattice_add_comm_group E] [normed_space ℝ E] [ordered_smul ℝ E] (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : E) (hx : 0 ≤ x) : 0 ≤ condexp_ind hm μ s x := begin rw ← coe_fn_le, refine eventually_le.trans_eq _ (condexp_ind_ae_eq_condexp_ind_smul hm hs hμs x).symm, exact (coe_fn_zero E 1 μ).trans_le (condexp_ind_smul_nonneg hs hμs x hx), end end condexp_ind section condexp_L1 variables {m m0 : measurable_space α} {μ : measure α} {hm : m ≤ m0} [sigma_finite (μ.trim hm)] {f g : α → F'} {s : set α} /-- Conditional expectation of a function as a linear map from `α →₁[μ] F'` to itself. -/ def condexp_L1_clm (hm : m ≤ m0) (μ : measure α) [sigma_finite (μ.trim hm)] : (α →₁[μ] F') →L[ℝ] α →₁[μ] F' := L1.set_to_L1 (dominated_fin_meas_additive_condexp_ind F' hm μ) lemma condexp_L1_clm_smul (c : 𝕜) (f : α →₁[μ] F') : condexp_L1_clm hm μ (c • f) = c • condexp_L1_clm hm μ f := L1.set_to_L1_smul (dominated_fin_meas_additive_condexp_ind F' hm μ) (λ c s x, condexp_ind_smul' c x) c f lemma condexp_L1_clm_indicator_const_Lp (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : F') : (condexp_L1_clm hm μ) (indicator_const_Lp 1 hs hμs x) = condexp_ind hm μ s x := L1.set_to_L1_indicator_const_Lp (dominated_fin_meas_additive_condexp_ind F' hm μ) hs hμs x lemma condexp_L1_clm_indicator_const (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : F') : (condexp_L1_clm hm μ) ↑(simple_func.indicator_const 1 hs hμs x) = condexp_ind hm μ s x := by { rw Lp.simple_func.coe_indicator_const, exact condexp_L1_clm_indicator_const_Lp hs hμs x, } /-- Auxiliary lemma used in the proof of `set_integral_condexp_L1_clm`. -/ lemma set_integral_condexp_L1_clm_of_measure_ne_top (f : α →₁[μ] F') (hs : measurable_set[m] s) (hμs : μ s ≠ ∞) : ∫ x in s, condexp_L1_clm hm μ f x ∂μ = ∫ x in s, f x ∂μ := begin refine Lp.induction ennreal.one_ne_top (λ f : α →₁[μ] F', ∫ x in s, condexp_L1_clm hm μ f x ∂μ = ∫ x in s, f x ∂μ) _ _ (is_closed_eq _ _) f, { intros x t ht hμt, simp_rw condexp_L1_clm_indicator_const ht hμt.ne x, rw [Lp.simple_func.coe_indicator_const, set_integral_indicator_const_Lp (hm _ hs)], exact set_integral_condexp_ind hs ht hμs hμt.ne x, }, { intros f g hf_Lp hg_Lp hfg_disj hf hg, simp_rw (condexp_L1_clm hm μ).map_add, rw set_integral_congr_ae (hm s hs) ((Lp.coe_fn_add (condexp_L1_clm hm μ (hf_Lp.to_Lp f)) (condexp_L1_clm hm μ (hg_Lp.to_Lp g))).mono (λ x hx hxs, hx)), rw set_integral_congr_ae (hm s hs) ((Lp.coe_fn_add (hf_Lp.to_Lp f) (hg_Lp.to_Lp g)).mono (λ x hx hxs, hx)), simp_rw pi.add_apply, rw [integral_add (L1.integrable_coe_fn _).integrable_on (L1.integrable_coe_fn _).integrable_on, integral_add (L1.integrable_coe_fn _).integrable_on (L1.integrable_coe_fn _).integrable_on, hf, hg], }, { exact (continuous_set_integral s).comp (condexp_L1_clm hm μ).continuous, }, { exact continuous_set_integral s, }, end /-- The integral of the conditional expectation `condexp_L1_clm` over an `m`-measurable set is equal to the integral of `f` on that set. See also `set_integral_condexp`, the similar statement for `condexp`. -/ lemma set_integral_condexp_L1_clm (f : α →₁[μ] F') (hs : measurable_set[m] s) : ∫ x in s, condexp_L1_clm hm μ f x ∂μ = ∫ x in s, f x ∂μ := begin let S := spanning_sets (μ.trim hm), have hS_meas : ∀ i, measurable_set[m] (S i) := measurable_spanning_sets (μ.trim hm), have hS_meas0 : ∀ i, measurable_set (S i) := λ i, hm _ (hS_meas i), have hs_eq : s = ⋃ i, S i ∩ s, { simp_rw set.inter_comm, rw [← set.inter_Union, (Union_spanning_sets (μ.trim hm)), set.inter_univ], }, have hS_finite : ∀ i, μ (S i ∩ s) < ∞, { refine λ i, (measure_mono (set.inter_subset_left _ _)).trans_lt _, have hS_finite_trim := measure_spanning_sets_lt_top (μ.trim hm) i, rwa trim_measurable_set_eq hm (hS_meas i) at hS_finite_trim, }, have h_mono : monotone (λ i, (S i) ∩ s), { intros i j hij x, simp_rw set.mem_inter_iff, exact λ h, ⟨monotone_spanning_sets (μ.trim hm) hij h.1, h.2⟩, }, have h_eq_forall : (λ i, ∫ x in (S i) ∩ s, condexp_L1_clm hm μ f x ∂μ) = λ i, ∫ x in (S i) ∩ s, f x ∂μ, from funext (λ i, set_integral_condexp_L1_clm_of_measure_ne_top f (@measurable_set.inter α m _ _ (hS_meas i) hs) (hS_finite i).ne), have h_right : tendsto (λ i, ∫ x in (S i) ∩ s, f x ∂μ) at_top (𝓝 (∫ x in s, f x ∂μ)), { have h := tendsto_set_integral_of_monotone (λ i, (hS_meas0 i).inter (hm s hs)) h_mono (L1.integrable_coe_fn f).integrable_on, rwa ← hs_eq at h, }, have h_left : tendsto (λ i, ∫ x in (S i) ∩ s, condexp_L1_clm hm μ f x ∂μ) at_top (𝓝 (∫ x in s, condexp_L1_clm hm μ f x ∂μ)), { have h := tendsto_set_integral_of_monotone (λ i, (hS_meas0 i).inter (hm s hs)) h_mono (L1.integrable_coe_fn (condexp_L1_clm hm μ f)).integrable_on, rwa ← hs_eq at h, }, rw h_eq_forall at h_left, exact tendsto_nhds_unique h_left h_right, end lemma ae_strongly_measurable'_condexp_L1_clm (f : α →₁[μ] F') : ae_strongly_measurable' m (condexp_L1_clm hm μ f) μ := begin refine Lp.induction ennreal.one_ne_top (λ f : α →₁[μ] F', ae_strongly_measurable' m (condexp_L1_clm hm μ f) μ) _ _ _ f, { intros c s hs hμs, rw condexp_L1_clm_indicator_const hs hμs.ne c, exact ae_strongly_measurable'_condexp_ind hs hμs.ne c, }, { intros f g hf hg h_disj hfm hgm, rw (condexp_L1_clm hm μ).map_add, refine ae_strongly_measurable'.congr _ (coe_fn_add _ _).symm, exact ae_strongly_measurable'.add hfm hgm, }, { have : {f : Lp F' 1 μ | ae_strongly_measurable' m (condexp_L1_clm hm μ f) μ} = (condexp_L1_clm hm μ) ⁻¹' {f | ae_strongly_measurable' m f μ}, by refl, rw this, refine is_closed.preimage (condexp_L1_clm hm μ).continuous _, exact is_closed_ae_strongly_measurable' hm, }, end lemma condexp_L1_clm_Lp_meas (f : Lp_meas F' ℝ m 1 μ) : condexp_L1_clm hm μ (f : α →₁[μ] F') = ↑f := begin let g := Lp_meas_to_Lp_trim_lie F' ℝ 1 μ hm f, have hfg : f = (Lp_meas_to_Lp_trim_lie F' ℝ 1 μ hm).symm g, by simp only [linear_isometry_equiv.symm_apply_apply], rw hfg, refine @Lp.induction α F' m _ 1 (μ.trim hm) _ ennreal.coe_ne_top (λ g : α →₁[μ.trim hm] F', condexp_L1_clm hm μ ((Lp_meas_to_Lp_trim_lie F' ℝ 1 μ hm).symm g : α →₁[μ] F') = ↑((Lp_meas_to_Lp_trim_lie F' ℝ 1 μ hm).symm g)) _ _ _ g, { intros c s hs hμs, rw [Lp.simple_func.coe_indicator_const, Lp_meas_to_Lp_trim_lie_symm_indicator hs hμs.ne c, condexp_L1_clm_indicator_const_Lp], exact condexp_ind_of_measurable hs ((le_trim hm).trans_lt hμs).ne c, }, { intros f g hf hg hfg_disj hf_eq hg_eq, rw linear_isometry_equiv.map_add, push_cast, rw [map_add, hf_eq, hg_eq], }, { refine is_closed_eq _ _, { refine (condexp_L1_clm hm μ).continuous.comp (continuous_induced_dom.comp _), exact linear_isometry_equiv.continuous _, }, { refine continuous_induced_dom.comp _, exact linear_isometry_equiv.continuous _, }, }, end lemma condexp_L1_clm_of_ae_strongly_measurable' (f : α →₁[μ] F') (hfm : ae_strongly_measurable' m f μ) : condexp_L1_clm hm μ f = f := condexp_L1_clm_Lp_meas (⟨f, hfm⟩ : Lp_meas F' ℝ m 1 μ) /-- Conditional expectation of a function, in L1. Its value is 0 if the function is not integrable. The function-valued `condexp` should be used instead in most cases. -/ def condexp_L1 (hm : m ≤ m0) (μ : measure α) [sigma_finite (μ.trim hm)] (f : α → F') : α →₁[μ] F' := set_to_fun μ (condexp_ind hm μ) (dominated_fin_meas_additive_condexp_ind F' hm μ) f lemma condexp_L1_undef (hf : ¬ integrable f μ) : condexp_L1 hm μ f = 0 := set_to_fun_undef (dominated_fin_meas_additive_condexp_ind F' hm μ) hf lemma condexp_L1_eq (hf : integrable f μ) : condexp_L1 hm μ f = condexp_L1_clm hm μ (hf.to_L1 f) := set_to_fun_eq (dominated_fin_meas_additive_condexp_ind F' hm μ) hf @[simp] lemma condexp_L1_zero : condexp_L1 hm μ (0 : α → F') = 0 := set_to_fun_zero _ @[simp] lemma condexp_L1_measure_zero (hm : m ≤ m0) : condexp_L1 hm (0 : measure α) f = 0 := set_to_fun_measure_zero _ rfl lemma ae_strongly_measurable'_condexp_L1 {f : α → F'} : ae_strongly_measurable' m (condexp_L1 hm μ f) μ := begin by_cases hf : integrable f μ, { rw condexp_L1_eq hf, exact ae_strongly_measurable'_condexp_L1_clm _, }, { rw condexp_L1_undef hf, refine ae_strongly_measurable'.congr _ (coe_fn_zero _ _ _).symm, exact strongly_measurable.ae_strongly_measurable' (@strongly_measurable_zero _ _ m _ _), }, end lemma condexp_L1_congr_ae (hm : m ≤ m0) [sigma_finite (μ.trim hm)] (h : f =ᵐ[μ] g) : condexp_L1 hm μ f = condexp_L1 hm μ g := set_to_fun_congr_ae _ h lemma integrable_condexp_L1 (f : α → F') : integrable (condexp_L1 hm μ f) μ := L1.integrable_coe_fn _ /-- The integral of the conditional expectation `condexp_L1` over an `m`-measurable set is equal to the integral of `f` on that set. See also `set_integral_condexp`, the similar statement for `condexp`. -/ lemma set_integral_condexp_L1 (hf : integrable f μ) (hs : measurable_set[m] s) : ∫ x in s, condexp_L1 hm μ f x ∂μ = ∫ x in s, f x ∂μ := begin simp_rw condexp_L1_eq hf, rw set_integral_condexp_L1_clm (hf.to_L1 f) hs, exact set_integral_congr_ae (hm s hs) ((hf.coe_fn_to_L1).mono (λ x hx hxs, hx)), end lemma condexp_L1_add (hf : integrable f μ) (hg : integrable g μ) : condexp_L1 hm μ (f + g) = condexp_L1 hm μ f + condexp_L1 hm μ g := set_to_fun_add _ hf hg lemma condexp_L1_neg (f : α → F') : condexp_L1 hm μ (-f) = - condexp_L1 hm μ f := set_to_fun_neg _ f lemma condexp_L1_smul (c : 𝕜) (f : α → F') : condexp_L1 hm μ (c • f) = c • condexp_L1 hm μ f := set_to_fun_smul _ (λ c _ x, condexp_ind_smul' c x) c f lemma condexp_L1_sub (hf : integrable f μ) (hg : integrable g μ) : condexp_L1 hm μ (f - g) = condexp_L1 hm μ f - condexp_L1 hm μ g := set_to_fun_sub _ hf hg lemma condexp_L1_of_ae_strongly_measurable' (hfm : ae_strongly_measurable' m f μ) (hfi : integrable f μ) : condexp_L1 hm μ f =ᵐ[μ] f := begin rw condexp_L1_eq hfi, refine eventually_eq.trans _ (integrable.coe_fn_to_L1 hfi), rw condexp_L1_clm_of_ae_strongly_measurable', exact ae_strongly_measurable'.congr hfm (integrable.coe_fn_to_L1 hfi).symm, end lemma condexp_L1_mono {E} [normed_lattice_add_comm_group E] [complete_space E] [normed_space ℝ E] [ordered_smul ℝ E] {f g : α → E} (hf : integrable f μ) (hg : integrable g μ) (hfg : f ≤ᵐ[μ] g) : condexp_L1 hm μ f ≤ᵐ[μ] condexp_L1 hm μ g := begin rw coe_fn_le, have h_nonneg : ∀ s, measurable_set s → μ s < ∞ → ∀ x : E, 0 ≤ x → 0 ≤ condexp_ind hm μ s x, from λ s hs hμs x hx, condexp_ind_nonneg hs hμs.ne x hx, exact set_to_fun_mono (dominated_fin_meas_additive_condexp_ind E hm μ) h_nonneg hf hg hfg, end end condexp_L1 section condexp /-! ### Conditional expectation of a function -/ open_locale classical variables {𝕜} {m m0 : measurable_space α} {μ : measure α} {f g : α → F'} {s : set α} /-- Conditional expectation of a function. It is defined as 0 if any one of the following conditions is true: - `m` is not a sub-σ-algebra of `m0`, - `μ` is not σ-finite with respect to `m`, - `f` is not integrable. -/ @[irreducible] def condexp (m : measurable_space α) {m0 : measurable_space α} (μ : measure α) (f : α → F') : α → F' := if hm : m ≤ m0 then if h : sigma_finite (μ.trim hm) ∧ integrable f μ then if strongly_measurable[m] f then f else (@ae_strongly_measurable'_condexp_L1 _ _ _ _ _ m m0 μ hm h.1 _).mk (@condexp_L1 _ _ _ _ _ _ _ hm μ h.1 f) else 0 else 0 -- We define notation `μ[f|m]` for the conditional expectation of `f` with respect to `m`. localized "notation (name := measure_theory.condexp) μ `[` f `|` m `]` := measure_theory.condexp m μ f" in measure_theory lemma condexp_of_not_le (hm_not : ¬ m ≤ m0) : μ[f|m] = 0 := by rw [condexp, dif_neg hm_not] lemma condexp_of_not_sigma_finite (hm : m ≤ m0) (hμm_not : ¬ sigma_finite (μ.trim hm)) : μ[f|m] = 0 := by { rw [condexp, dif_pos hm, dif_neg], push_neg, exact λ h, absurd h hμm_not, } lemma condexp_of_sigma_finite (hm : m ≤ m0) [hμm : sigma_finite (μ.trim hm)] : μ[f|m] = if integrable f μ then if strongly_measurable[m] f then f else ae_strongly_measurable'_condexp_L1.mk (condexp_L1 hm μ f) else 0 := begin rw [condexp, dif_pos hm], simp only [hμm, ne.def, true_and], by_cases hf : integrable f μ, { rw [dif_pos hf, if_pos hf], }, { rw [dif_neg hf, if_neg hf], }, end lemma condexp_of_strongly_measurable (hm : m ≤ m0) [hμm : sigma_finite (μ.trim hm)] {f : α → F'} (hf : strongly_measurable[m] f) (hfi : integrable f μ) : μ[f|m] = f := by { rw [condexp_of_sigma_finite hm, if_pos hfi, if_pos hf], apply_instance, } lemma condexp_const (hm : m ≤ m0) (c : F') [is_finite_measure μ] : μ[(λ x : α, c)|m] = λ _, c := condexp_of_strongly_measurable hm (@strongly_measurable_const _ _ m _ _) (integrable_const c) lemma condexp_ae_eq_condexp_L1 (hm : m ≤ m0) [hμm : sigma_finite (μ.trim hm)] (f : α → F') : μ[f|m] =ᵐ[μ] condexp_L1 hm μ f := begin rw condexp_of_sigma_finite hm, by_cases hfi : integrable f μ, { rw if_pos hfi, by_cases hfm : strongly_measurable[m] f, { rw if_pos hfm, exact (condexp_L1_of_ae_strongly_measurable' (strongly_measurable.ae_strongly_measurable' hfm) hfi).symm, }, { rw if_neg hfm, exact (ae_strongly_measurable'.ae_eq_mk ae_strongly_measurable'_condexp_L1).symm, }, }, rw [if_neg hfi, condexp_L1_undef hfi], exact (coe_fn_zero _ _ _).symm, end lemma condexp_ae_eq_condexp_L1_clm (hm : m ≤ m0) [sigma_finite (μ.trim hm)] (hf : integrable f μ) : μ[f|m] =ᵐ[μ] condexp_L1_clm hm μ (hf.to_L1 f) := begin refine (condexp_ae_eq_condexp_L1 hm f).trans (eventually_of_forall (λ x, _)), rw condexp_L1_eq hf, end lemma condexp_undef (hf : ¬ integrable f μ) : μ[f|m] = 0 := begin by_cases hm : m ≤ m0, swap, { rw condexp_of_not_le hm, }, by_cases hμm : sigma_finite (μ.trim hm), swap, { rw condexp_of_not_sigma_finite hm hμm, }, haveI : sigma_finite (μ.trim hm) := hμm, rw [condexp_of_sigma_finite, if_neg hf], end @[simp] lemma condexp_zero : μ[(0 : α → F')|m] = 0 := begin by_cases hm : m ≤ m0, swap, { rw condexp_of_not_le hm, }, by_cases hμm : sigma_finite (μ.trim hm), swap, { rw condexp_of_not_sigma_finite hm hμm, }, haveI : sigma_finite (μ.trim hm) := hμm, exact condexp_of_strongly_measurable hm (@strongly_measurable_zero _ _ m _ _) (integrable_zero _ _ _), end lemma strongly_measurable_condexp : strongly_measurable[m] (μ[f|m]) := begin by_cases hm : m ≤ m0, swap, { rw condexp_of_not_le hm, exact strongly_measurable_zero, }, by_cases hμm : sigma_finite (μ.trim hm), swap, { rw condexp_of_not_sigma_finite hm hμm, exact strongly_measurable_zero, }, haveI : sigma_finite (μ.trim hm) := hμm, rw condexp_of_sigma_finite hm, swap, { apply_instance, }, split_ifs with hfi hfm, { exact hfm, }, { exact ae_strongly_measurable'.strongly_measurable_mk _, }, { exact strongly_measurable_zero, }, end lemma condexp_congr_ae (h : f =ᵐ[μ] g) : μ[f | m] =ᵐ[μ] μ[g | m] := begin by_cases hm : m ≤ m0, swap, { simp_rw condexp_of_not_le hm, }, by_cases hμm : sigma_finite (μ.trim hm), swap, { simp_rw condexp_of_not_sigma_finite hm hμm, }, haveI : sigma_finite (μ.trim hm) := hμm, exact (condexp_ae_eq_condexp_L1 hm f).trans (filter.eventually_eq.trans (by rw condexp_L1_congr_ae hm h) (condexp_ae_eq_condexp_L1 hm g).symm), end lemma condexp_of_ae_strongly_measurable' (hm : m ≤ m0) [hμm : sigma_finite (μ.trim hm)] {f : α → F'} (hf : ae_strongly_measurable' m f μ) (hfi : integrable f μ) : μ[f|m] =ᵐ[μ] f := begin refine ((condexp_congr_ae hf.ae_eq_mk).trans _).trans hf.ae_eq_mk.symm, rw condexp_of_strongly_measurable hm hf.strongly_measurable_mk ((integrable_congr hf.ae_eq_mk).mp hfi), end lemma integrable_condexp : integrable (μ[f|m]) μ := begin by_cases hm : m ≤ m0, swap, { rw condexp_of_not_le hm, exact integrable_zero _ _ _, }, by_cases hμm : sigma_finite (μ.trim hm), swap, { rw condexp_of_not_sigma_finite hm hμm, exact integrable_zero _ _ _, }, haveI : sigma_finite (μ.trim hm) := hμm, exact (integrable_condexp_L1 f).congr (condexp_ae_eq_condexp_L1 hm f).symm, end /-- The integral of the conditional expectation `μ[f|hm]` over an `m`-measurable set is equal to the integral of `f` on that set. -/ lemma set_integral_condexp (hm : m ≤ m0) [sigma_finite (μ.trim hm)] (hf : integrable f μ) (hs : measurable_set[m] s) : ∫ x in s, μ[f|m] x ∂μ = ∫ x in s, f x ∂μ := begin rw set_integral_congr_ae (hm s hs) ((condexp_ae_eq_condexp_L1 hm f).mono (λ x hx _, hx)), exact set_integral_condexp_L1 hf hs, end lemma integral_condexp (hm : m ≤ m0) [hμm : sigma_finite (μ.trim hm)] (hf : integrable f μ) : ∫ x, μ[f|m] x ∂μ = ∫ x, f x ∂μ := begin suffices : ∫ x in set.univ, μ[f|m] x ∂μ = ∫ x in set.univ, f x ∂μ, by { simp_rw integral_univ at this, exact this, }, exact set_integral_condexp hm hf (@measurable_set.univ _ m), end /-- **Uniqueness of the conditional expectation** If a function is a.e. `m`-measurable, verifies an integrability condition and has same integral as `f` on all `m`-measurable sets, then it is a.e. equal to `μ[f|hm]`. -/ lemma ae_eq_condexp_of_forall_set_integral_eq (hm : m ≤ m0) [sigma_finite (μ.trim hm)] {f g : α → F'} (hf : integrable f μ) (hg_int_finite : ∀ s, measurable_set[m] s → μ s < ∞ → integrable_on g s μ) (hg_eq : ∀ s : set α, measurable_set[m] s → μ s < ∞ → ∫ x in s, g x ∂μ = ∫ x in s, f x ∂μ) (hgm : ae_strongly_measurable' m g μ) : g =ᵐ[μ] μ[f|m] := begin refine ae_eq_of_forall_set_integral_eq_of_sigma_finite' hm hg_int_finite (λ s hs hμs, integrable_condexp.integrable_on) (λ s hs hμs, _) hgm (strongly_measurable.ae_strongly_measurable' strongly_measurable_condexp), rw [hg_eq s hs hμs, set_integral_condexp hm hf hs], end lemma condexp_bot' [hμ : μ.ae.ne_bot] (f : α → F') : μ[f|⊥] = λ _, (μ set.univ).to_real⁻¹ • ∫ x, f x ∂μ := begin by_cases hμ_finite : is_finite_measure μ, swap, { have h : ¬ sigma_finite (μ.trim bot_le), { rwa sigma_finite_trim_bot_iff, }, rw not_is_finite_measure_iff at hμ_finite, rw [condexp_of_not_sigma_finite bot_le h], simp only [hμ_finite, ennreal.top_to_real, inv_zero, zero_smul], refl, }, haveI : is_finite_measure μ := hμ_finite, by_cases hf : integrable f μ, swap, { rw [integral_undef hf, smul_zero, condexp_undef hf], refl, }, have h_meas : strongly_measurable[⊥] (μ[f|⊥]) := strongly_measurable_condexp, obtain ⟨c, h_eq⟩ := strongly_measurable_bot_iff.mp h_meas, rw h_eq, have h_integral : ∫ x, μ[f|⊥] x ∂μ = ∫ x, f x ∂μ := integral_condexp bot_le hf, simp_rw [h_eq, integral_const] at h_integral, rw [← h_integral, ← smul_assoc, smul_eq_mul, inv_mul_cancel, one_smul], rw [ne.def, ennreal.to_real_eq_zero_iff, auto.not_or_eq, measure.measure_univ_eq_zero, ← ae_eq_bot, ← ne.def, ← ne_bot_iff], exact ⟨hμ, measure_ne_top μ set.univ⟩, end lemma condexp_bot_ae_eq (f : α → F') : μ[f|⊥] =ᵐ[μ] λ _, (μ set.univ).to_real⁻¹ • ∫ x, f x ∂μ := begin by_cases μ.ae.ne_bot, { refine eventually_of_forall (λ x, _), rw condexp_bot' f, exact h, }, { rw [ne_bot_iff, not_not, ae_eq_bot] at h, simp only [h, ae_zero], }, end lemma condexp_bot [is_probability_measure μ] (f : α → F') : μ[f|⊥] = λ _, ∫ x, f x ∂μ := by { refine (condexp_bot' f).trans _, rw [measure_univ, ennreal.one_to_real, inv_one, one_smul], } lemma condexp_add (hf : integrable f μ) (hg : integrable g μ) : μ[f + g | m] =ᵐ[μ] μ[f|m] + μ[g|m] := begin by_cases hm : m ≤ m0, swap, { simp_rw condexp_of_not_le hm, simp, }, by_cases hμm : sigma_finite (μ.trim hm), swap, { simp_rw condexp_of_not_sigma_finite hm hμm, simp, }, haveI : sigma_finite (μ.trim hm) := hμm, refine (condexp_ae_eq_condexp_L1 hm _).trans _, rw condexp_L1_add hf hg, exact (coe_fn_add _ _).trans ((condexp_ae_eq_condexp_L1 hm _).symm.add (condexp_ae_eq_condexp_L1 hm _).symm), end lemma condexp_finset_sum {ι : Type*} {s : finset ι} {f : ι → α → F'} (hf : ∀ i ∈ s, integrable (f i) μ) : μ[∑ i in s, f i | m] =ᵐ[μ] ∑ i in s, μ[f i | m] := begin induction s using finset.induction_on with i s his heq hf, { rw [finset.sum_empty, finset.sum_empty, condexp_zero] }, { rw [finset.sum_insert his, finset.sum_insert his], exact (condexp_add (hf i $ finset.mem_insert_self i s) $ integrable_finset_sum' _ (λ j hmem, hf j $ finset.mem_insert_of_mem hmem)).trans ((eventually_eq.refl _ _).add (heq $ λ j hmem, hf j $ finset.mem_insert_of_mem hmem)) } end lemma condexp_smul (c : 𝕜) (f : α → F') : μ[c • f | m] =ᵐ[μ] c • μ[f|m] := begin by_cases hm : m ≤ m0, swap, { simp_rw condexp_of_not_le hm, simp, }, by_cases hμm : sigma_finite (μ.trim hm), swap, { simp_rw condexp_of_not_sigma_finite hm hμm, simp, }, haveI : sigma_finite (μ.trim hm) := hμm, refine (condexp_ae_eq_condexp_L1 hm _).trans _, rw condexp_L1_smul c f, refine (@condexp_ae_eq_condexp_L1 _ _ _ _ _ m _ _ hm _ f).mp _, refine (coe_fn_smul c (condexp_L1 hm μ f)).mono (λ x hx1 hx2, _), rw [hx1, pi.smul_apply, pi.smul_apply, hx2], end lemma condexp_neg (f : α → F') : μ[-f|m] =ᵐ[μ] - μ[f|m] := by letI : module ℝ (α → F') := @pi.module α (λ _, F') ℝ _ _ (λ _, infer_instance); calc μ[-f|m] = μ[(-1 : ℝ) • f|m] : by rw neg_one_smul ℝ f ... =ᵐ[μ] (-1 : ℝ) • μ[f|m] : condexp_smul (-1) f ... = -μ[f|m] : neg_one_smul ℝ (μ[f|m]) lemma condexp_sub (hf : integrable f μ) (hg : integrable g μ) : μ[f - g | m] =ᵐ[μ] μ[f|m] - μ[g|m] := begin simp_rw sub_eq_add_neg, exact (condexp_add hf hg.neg).trans (eventually_eq.rfl.add (condexp_neg g)), end lemma condexp_condexp_of_le {m₁ m₂ m0 : measurable_space α} {μ : measure α} (hm₁₂ : m₁ ≤ m₂) (hm₂ : m₂ ≤ m0) [sigma_finite (μ.trim hm₂)] : μ[ μ[f|m₂] | m₁] =ᵐ[μ] μ[f | m₁] := begin by_cases hμm₁ : sigma_finite (μ.trim (hm₁₂.trans hm₂)), swap, { simp_rw condexp_of_not_sigma_finite (hm₁₂.trans hm₂) hμm₁, }, haveI : sigma_finite (μ.trim (hm₁₂.trans hm₂)) := hμm₁, by_cases hf : integrable f μ, swap, { simp_rw [condexp_undef hf, condexp_zero], }, refine ae_eq_of_forall_set_integral_eq_of_sigma_finite' (hm₁₂.trans hm₂) (λ s hs hμs, integrable_condexp.integrable_on) (λ s hs hμs, integrable_condexp.integrable_on) _ (strongly_measurable.ae_strongly_measurable' strongly_measurable_condexp) (strongly_measurable.ae_strongly_measurable' strongly_measurable_condexp), intros s hs hμs, rw set_integral_condexp (hm₁₂.trans hm₂) integrable_condexp hs, swap, { apply_instance, }, rw [set_integral_condexp (hm₁₂.trans hm₂) hf hs, set_integral_condexp hm₂ hf (hm₁₂ s hs)], end lemma condexp_mono {E} [normed_lattice_add_comm_group E] [complete_space E] [normed_space ℝ E] [ordered_smul ℝ E] {f g : α → E} (hf : integrable f μ) (hg : integrable g μ) (hfg : f ≤ᵐ[μ] g) : μ[f | m] ≤ᵐ[μ] μ[g | m] := begin by_cases hm : m ≤ m0, swap, { simp_rw condexp_of_not_le hm, }, by_cases hμm : sigma_finite (μ.trim hm), swap, { simp_rw condexp_of_not_sigma_finite hm hμm, }, haveI : sigma_finite (μ.trim hm) := hμm, exact (condexp_ae_eq_condexp_L1 hm _).trans_le ((condexp_L1_mono hf hg hfg).trans_eq (condexp_ae_eq_condexp_L1 hm _).symm), end lemma condexp_nonneg {E} [normed_lattice_add_comm_group E] [complete_space E] [normed_space ℝ E] [ordered_smul ℝ E] {f : α → E} (hf : 0 ≤ᵐ[μ] f) : 0 ≤ᵐ[μ] μ[f | m] := begin by_cases hfint : integrable f μ, { rw (condexp_zero.symm : (0 : α → E) = μ[0 | m]), exact condexp_mono (integrable_zero _ _ _) hfint hf }, { rw condexp_undef hfint, } end lemma condexp_nonpos {E} [normed_lattice_add_comm_group E] [complete_space E] [normed_space ℝ E] [ordered_smul ℝ E] {f : α → E} (hf : f ≤ᵐ[μ] 0) : μ[f | m] ≤ᵐ[μ] 0 := begin by_cases hfint : integrable f μ, { rw (condexp_zero.symm : (0 : α → E) = μ[0 | m]), exact condexp_mono hfint (integrable_zero _ _ _) hf }, { rw condexp_undef hfint, } end /-- **Lebesgue dominated convergence theorem**: sufficient conditions under which almost everywhere convergence of a sequence of functions implies the convergence of their image by `condexp_L1`. -/ lemma tendsto_condexp_L1_of_dominated_convergence (hm : m ≤ m0) [sigma_finite (μ.trim hm)] {fs : ℕ → α → F'} {f : α → F'} (bound_fs : α → ℝ) (hfs_meas : ∀ n, ae_strongly_measurable (fs n) μ) (h_int_bound_fs : integrable bound_fs μ) (hfs_bound : ∀ n, ∀ᵐ x ∂μ, ‖fs n x‖ ≤ bound_fs x) (hfs : ∀ᵐ x ∂μ, tendsto (λ n, fs n x) at_top (𝓝 (f x))) : tendsto (λ n, condexp_L1 hm μ (fs n)) at_top (𝓝 (condexp_L1 hm μ f)) := tendsto_set_to_fun_of_dominated_convergence _ bound_fs hfs_meas h_int_bound_fs hfs_bound hfs /-- If two sequences of functions have a.e. equal conditional expectations at each step, converge and verify dominated convergence hypotheses, then the conditional expectations of their limits are a.e. equal. -/ lemma tendsto_condexp_unique (fs gs : ℕ → α → F') (f g : α → F') (hfs_int : ∀ n, integrable (fs n) μ) (hgs_int : ∀ n, integrable (gs n) μ) (hfs : ∀ᵐ x ∂μ, tendsto (λ n, fs n x) at_top (𝓝 (f x))) (hgs : ∀ᵐ x ∂μ, tendsto (λ n, gs n x) at_top (𝓝 (g x))) (bound_fs : α → ℝ) (h_int_bound_fs : integrable bound_fs μ) (bound_gs : α → ℝ) (h_int_bound_gs : integrable bound_gs μ) (hfs_bound : ∀ n, ∀ᵐ x ∂μ, ‖fs n x‖ ≤ bound_fs x) (hgs_bound : ∀ n, ∀ᵐ x ∂μ, ‖gs n x‖ ≤ bound_gs x) (hfg : ∀ n, μ[fs n | m] =ᵐ[μ] μ[gs n | m]) : μ[f | m] =ᵐ[μ] μ[g | m] := begin by_cases hm : m ≤ m0, swap, { simp_rw condexp_of_not_le hm, }, by_cases hμm : sigma_finite (μ.trim hm), swap, { simp_rw condexp_of_not_sigma_finite hm hμm, }, haveI : sigma_finite (μ.trim hm) := hμm, refine (condexp_ae_eq_condexp_L1 hm f).trans ((condexp_ae_eq_condexp_L1 hm g).trans _).symm, rw ← Lp.ext_iff, have hn_eq : ∀ n, condexp_L1 hm μ (gs n) = condexp_L1 hm μ (fs n), { intros n, ext1, refine (condexp_ae_eq_condexp_L1 hm (gs n)).symm.trans ((hfg n).symm.trans _), exact (condexp_ae_eq_condexp_L1 hm (fs n)), }, have hcond_fs : tendsto (λ n, condexp_L1 hm μ (fs n)) at_top (𝓝 (condexp_L1 hm μ f)), from tendsto_condexp_L1_of_dominated_convergence hm _ (λ n, (hfs_int n).1) h_int_bound_fs hfs_bound hfs, have hcond_gs : tendsto (λ n, condexp_L1 hm μ (gs n)) at_top (𝓝 (condexp_L1 hm μ g)), from tendsto_condexp_L1_of_dominated_convergence hm _ (λ n, (hgs_int n).1) h_int_bound_gs hgs_bound hgs, exact tendsto_nhds_unique_of_eventually_eq hcond_gs hcond_fs (eventually_of_forall hn_eq), end end condexp end measure_theory
e862fa73a701d83d0997bdc67c9f8510372e123b
64874bd1010548c7f5a6e3e8902efa63baaff785
/tests/lean/run/ind0.lean
40ac4b3044dbc0f17172357b756f9cc09672e348
[ "Apache-2.0" ]
permissive
tjiaqi/lean
4634d729795c164664d10d093f3545287c76628f
d0ce4cf62f4246b0600c07e074d86e51f2195e30
refs/heads/master
1,622,323,796,480
1,422,643,069,000
1,422,643,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
92
lean
prelude inductive nat : Type := zero : nat, succ : nat → nat check nat check nat.rec.{1}
46ad7c0aa471d2163233eaa91dfdb9109ac5880f
491068d2ad28831e7dade8d6dff871c3e49d9431
/tests/lean/run/contra1.lean
cd22b35f518cad3077966143a7e1c7859c1043dc
[ "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
363
lean
example (a b : nat) (h : false) : a = b := by contradiction example : ∀ (a b : nat), false → a = b := by contradiction example : ∀ (a b : nat), 0 = 1 → a = b := by contradiction definition id {A : Type} (a : A) := a example : ∀ (a b : nat), id false → a = b := by contradiction example : ∀ (a b : nat), id (0 = 1) → a = b := by contradiction
12843d35f3f63bc572ae9f4379626fe69c8ae296
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/test/local_cache.lean
c8cd34835760559feb76f1894b12cc4b48e54d54
[ "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
10,078
lean
import tactic.local_cache open tactic def do_trace : bool := ff meta def trace_cache_regenerating : tactic unit := if do_trace then trace "(test/local_cache): cache regenerating" else skip namespace block_local section example_tactic def TEST_NS_1 : name := `my_tactic def TEST_NS_2 : name := `my_other_tactic -- Example "expensive" function meta def generate_some_data : tactic (list ℕ) := do trace_cache_regenerating, return [1, 2, 3, 4] meta def my_tactic : tactic unit := do my_cached_data ← run_once TEST_NS_1 generate_some_data, -- Do some stuff with `my_cached_data` skip meta def my_other_tactic : tactic unit := run_once TEST_NS_2 (return [10, 20, 30, 40]) >> skip end example_tactic section example_usage -- Note only a single cache regeneration (only a single trace message), -- even upon descent to a sub-tactic-block. lemma my_lemma : true := begin my_tactic, my_tactic, my_tactic, have h : true, { my_tactic, trivial }, trivial end end example_usage section test meta def fail_if_cache_miss (ns : name) : tactic unit := do p ← local_cache.present ns, if p then skip else fail "cache miss" meta def fail_if_cache_miss_1 : tactic unit := fail_if_cache_miss TEST_NS_1 meta def fail_if_cache_miss_2 : tactic unit := fail_if_cache_miss TEST_NS_2 end test -- Test: the cache persists only within a single tactic block section test_scope structure dummy := (a b : ℕ) def my_definition : dummy := ⟨ begin my_tactic, fail_if_cache_miss_1, exact 1 end, begin success_if_fail { fail_if_cache_miss_1 }, exact 1 end, ⟩ def my_definition' : dummy := ⟨ begin success_if_fail { fail_if_cache_miss_1 }, exact 1 end, begin success_if_fail { fail_if_cache_miss_1 }, exact 1 end, ⟩ noncomputable lemma my_lemma' : dummy := ⟨ begin my_tactic, fail_if_cache_miss_1, exact 1 end, begin success_if_fail { fail_if_cache_miss_1 }, exact 1 end, ⟩ end test_scope -- Test: the cache is reliably persistent, decends to sub-blocks, -- the api to inspect whether a cache entry is present works, and -- the cache can be manually cleared. section test_persistence lemma my_test_ps : true := begin success_if_fail { fail_if_cache_miss_1 }, my_tactic, fail_if_cache_miss_1, fail_if_cache_miss_1, success_if_fail { fail_if_cache_miss_2 }, have h : true, { fail_if_cache_miss_1, trivial }, -- Manually clear cache local_cache.clear TEST_NS_1, success_if_fail { fail_if_cache_miss_1 }, trivial end end test_persistence -- Test: caching under different namespaces doesn't share the -- cached state. section test_ns_collison lemma my_test_ns : true := begin my_tactic, fail_if_cache_miss_1, success_if_fail { fail_if_cache_miss_2 }, my_other_tactic, fail_if_cache_miss_1, fail_if_cache_miss_2, local_cache.clear TEST_NS_1, success_if_fail { fail_if_cache_miss_1 }, fail_if_cache_miss_2, my_other_tactic, success_if_fail { fail_if_cache_miss_1 }, fail_if_cache_miss_2, trivial end end test_ns_collison -- Test: cached results don't leak between `def`s or `lemma`s. section test_locality def my_def_1 : true := begin success_if_fail { fail_if_cache_miss_1 }, my_tactic, fail_if_cache_miss_1, trivial end def my_def_2 : true := begin success_if_fail { fail_if_cache_miss_1 }, my_tactic, fail_if_cache_miss_1, trivial end lemma my_lemma_1 : true := begin success_if_fail { fail_if_cache_miss_1 }, my_tactic, fail_if_cache_miss_1, trivial end lemma my_lemma_2 : true := begin success_if_fail { fail_if_cache_miss_1 }, my_tactic, fail_if_cache_miss_1, trivial end end test_locality -- Test: the `local_cache.get` function. section test_get meta def assert_equal {α : Type} [decidable_eq α] (a : α) (ta : tactic α) : tactic unit := do a' ← ta, if a = a' then skip else fail "not equal!" lemma my_lemma_3 : true := begin assert_equal none (local_cache.get TEST_NS_1 (list ℕ)), my_tactic, my_other_tactic, assert_equal (some [1,2,3,4]) (local_cache.get TEST_NS_1 (list ℕ)), assert_equal (some [10, 20, 30, 40]) (local_cache.get TEST_NS_2 (list ℕ)), trivial end end test_get end block_local --------------------------- -- Now test again with the `def_local` scope. --------------------------- namespace def_local open tactic.local_cache.cache_scope section example_tactic def TEST_NS_1 : name := `my_tactic def TEST_NS_2 : name := `my_other_tactic -- Example "expensive" function meta def generate_some_data : tactic (list ℕ) := do trace_cache_regenerating, return [1, 2, 3, 4] meta def my_tactic : tactic unit := do my_cached_data ← run_once TEST_NS_1 generate_some_data def_local, -- Do some stuff with `my_cached_data` skip meta def my_other_tactic : tactic unit := run_once TEST_NS_2 (return [10, 20, 30, 40]) def_local >> skip end example_tactic section example_usage -- Note only a single cache regeneration (only a single trace message), -- even upon descent to a sub-tactic-block. lemma my_lemma : true := begin my_tactic, my_tactic, my_tactic, have h : true, { my_tactic, trivial }, trivial end end example_usage section test meta def fail_if_cache_miss (ns : name) : tactic unit := do p ← local_cache.present ns def_local, if p then skip else fail "cache miss" meta def fail_if_cache_miss_1 : tactic unit := fail_if_cache_miss TEST_NS_1 meta def fail_if_cache_miss_2 : tactic unit := fail_if_cache_miss TEST_NS_2 end test -- Test: the cache really does persist over a whole definition section test_scope structure dummy := (a b : ℕ) def my_definition : dummy := ⟨ begin my_tactic, fail_if_cache_miss_1, exact 1 end, begin fail_if_cache_miss_1, exact 1 end, ⟩ def my_definition' : dummy := ⟨ begin success_if_fail { fail_if_cache_miss_1 }, exact 1 end, begin success_if_fail { fail_if_cache_miss_1 }, exact 1 end, ⟩ noncomputable lemma my_lemma' : dummy := ⟨ begin my_tactic, fail_if_cache_miss_1, exact 1 end, begin fail_if_cache_miss_1, exact 1 end, ⟩ end test_scope -- Test: the cache is reliably persistent, decends to sub-blocks, -- the api to inspect whether a cache entry is present works, and -- the cache can be manually cleared. section test_persistence lemma my_test_ps : true := begin success_if_fail { fail_if_cache_miss_1 }, my_tactic, my_tactic, fail_if_cache_miss_1, fail_if_cache_miss_1, success_if_fail { fail_if_cache_miss_2 }, have h : true, { fail_if_cache_miss_1, trivial }, -- Manually clear cache local_cache.clear TEST_NS_1 def_local, success_if_fail { fail_if_cache_miss_1 }, trivial end end test_persistence -- Test: caching under different namespaces doesn't share the -- cached state. section test_ns_collison lemma my_test_ns : true := begin my_tactic, fail_if_cache_miss_1, success_if_fail { fail_if_cache_miss_2 }, my_other_tactic, fail_if_cache_miss_1, fail_if_cache_miss_2, local_cache.clear TEST_NS_1 def_local, success_if_fail { fail_if_cache_miss_1 }, fail_if_cache_miss_2, my_other_tactic, success_if_fail { fail_if_cache_miss_1 }, fail_if_cache_miss_2, trivial end end test_ns_collison -- Test: cached results don't leak between `def`s or `lemma`s. section test_locality def my_def_1 : true := begin success_if_fail { fail_if_cache_miss_1 }, my_tactic, fail_if_cache_miss_1, trivial end def my_def_2 : true := begin success_if_fail { fail_if_cache_miss_1 }, my_tactic, fail_if_cache_miss_1, trivial end lemma my_lemma_1 : true := begin success_if_fail { fail_if_cache_miss_1 }, my_tactic, fail_if_cache_miss_1, trivial end lemma my_lemma_2 : true := begin success_if_fail { fail_if_cache_miss_1 }, my_tactic, fail_if_cache_miss_1, trivial end end test_locality -- Test: the `local_cache.get` function. section test_get meta def assert_equal {α : Type} [decidable_eq α] (a : α) (ta : tactic α) : tactic unit := do a' ← ta, if a = a' then skip else fail "not equal!" lemma my_lemma_3 : true := begin assert_equal none (local_cache.get TEST_NS_1 (list ℕ)), my_tactic, my_other_tactic, assert_equal (some [1,2,3,4]) (local_cache.get TEST_NS_1 (list ℕ) def_local), assert_equal (some [10, 20, 30, 40]) (local_cache.get TEST_NS_2 (list ℕ) def_local), trivial end end test_get end def_local -- Test: finally, make sure the `block_local` and `def_local` caches -- don't collide. namespace collision open tactic.local_cache.cache_scope def TEST_NS : name := `my_tactic -- Example "expensive" function meta def generate_some_data : tactic (list ℕ) := do trace_cache_regenerating, return [1, 2, 3, 4] meta def tac_block : tactic unit := do my_cached_data ← run_once TEST_NS generate_some_data block_local, skip meta def tac_def : tactic unit := do my_cached_data ← run_once TEST_NS generate_some_data def_local, skip meta def fail_if_cache_miss_def : tactic unit := do p ← local_cache.present TEST_NS def_local, if p then skip else fail "cache miss" meta def fail_if_cache_miss_block : tactic unit := do p ← local_cache.present TEST_NS block_local, if p then skip else fail "cache miss" lemma my_lemma_1 : true := begin tac_block, fail_if_cache_miss_block, success_if_fail { fail_if_cache_miss_def }, trivial end lemma my_lemma_2 : true := begin tac_def, fail_if_cache_miss_def, success_if_fail { fail_if_cache_miss_block }, trivial end lemma my_lemma_3 : true := begin tac_block, tac_def, local_cache.clear TEST_NS block_local, fail_if_cache_miss_def, success_if_fail { fail_if_cache_miss_block }, trivial end lemma my_lemma_4 : true := begin tac_block, tac_def, local_cache.clear TEST_NS def_local, fail_if_cache_miss_block, success_if_fail { fail_if_cache_miss_def }, trivial end end collision
54dc568886090359a2a4f58927a1fea145fdcff1
ea5678cc400c34ff95b661fa26d15024e27ea8cd
/PID.lean
912993cef115e6b995376de68e08e8b4ec92d7b6
[]
no_license
ChrisHughes24/leanstuff
dca0b5349c3ed893e8792ffbd98cbcadaff20411
9efa85f72efaccd1d540385952a6acc18fce8687
refs/heads/master
1,654,883,241,759
1,652,873,885,000
1,652,873,885,000
134,599,537
1
0
null
null
null
null
UTF-8
Lean
false
false
3,730
lean
import algebra.euclidean_domain ring_theory.ideals universes u v variables {α : Type u} {β : Type v} [comm_ring α] {a b : α} open set function local attribute [instance] classical.prop_decidable class is_principal_ideal (S : set α) : Prop := (principal : ∃ a : α, S = {x | a ∣ x}) class principal_ideal_domain (α : Type u) extends integral_domain α := (principal : ∀ (S : set α) [is_ideal S], is_principal_ideal S) namespace is_principal_ideal noncomputable def generator (S : set α) [is_principal_ideal S] : α := classical.some (principal S) lemma generator_generates (S : set α) [is_principal_ideal S] : {x | generator S ∣ x} = S := eq.symm (classical.some_spec (principal S)) instance to_is_ideal (S : set α) [is_principal_ideal S] : is_ideal S := { to_is_submodule := { zero_ := by rw ← generator_generates S; simp, add_ := λ x y h, by rw ← generator_generates S at *; exact (dvd_add_iff_right h).1, smul := λ c x h, by rw ← generator_generates S at h ⊢; exact dvd_mul_of_dvd_right h _ } } end is_principal_ideal @[simp] lemma zero_dvd_iff {α : Type u} [comm_semiring α] {a : α} : 0 ∣ a ↔ a = 0 := ⟨eq_zero_of_zero_dvd, λ h, by rw h⟩ open euclidean_domain is_principal_ideal lemma mod_eq_sub_mul_div {α : Type*} [euclidean_domain α] (a b : α) : a % b = a - b * (a / b) := calc a % b = b * (a / b) + a % b - b * (a / b) : by simp ... = a - b * (a / b) : by rw div_add_mod lemma mod_mem_iff {α : Type*} [euclidean_domain α] {S : set α} [is_ideal S] {x y : α} (hy : y ∈ S) : x % y ∈ S ↔ x ∈ S := ⟨λ hxy, div_add_mod x y ▸ is_ideal.add (is_ideal.mul_right hy) hxy, λ hx, (mod_eq_sub_mul_div x y).symm ▸ is_ideal.sub hx (is_ideal.mul_right hy)⟩ instance euclidean_domain.to_principal_ideal_domain {α : Type*} [euclidean_domain α] : principal_ideal_domain α := { principal := λ S h, by exactI ⟨if h : {x : α | x ∈ S ∧ x ≠ 0} = ∅ then ⟨0, set.ext $ λ a, ⟨by finish [set.ext_iff], λ h₁, (show a = 0, by simpa using h₁).symm ▸ is_ideal.zero S⟩⟩ else have wf : well_founded euclidean_domain.r := euclidean_domain.r_well_founded α, have hmin : well_founded.min wf {x : α | x ∈ S ∧ x ≠ 0} h ∈ S ∧ well_founded.min wf {x : α | x ∈ S ∧ x ≠ 0} h ≠ 0, from well_founded.min_mem wf {x : α | x ∈ S ∧ x ≠ 0} h, ⟨well_founded.min wf {x : α | x ∈ S ∧ x ≠ 0} h, set.ext $ λ x, ⟨λ hx, div_add_mod x (well_founded.min wf {x : α | x ∈ S ∧ x ≠ 0} h) ▸ dvd_add (dvd_mul_right _ _) (have (x % (well_founded.min wf {x : α | x ∈ S ∧ x ≠ 0} h) ∉ {x : α | x ∈ S ∧ x ≠ 0}), from λ h₁, well_founded.not_lt_min wf _ h h₁ (mod_lt x hmin.2), have x % well_founded.min wf {x : α | x ∈ S ∧ x ≠ 0} h = 0, by finish [(mod_mem_iff hmin.1).2 hx], by simp *), λ hx, let ⟨y, hy⟩ := hx in hy.symm ▸ is_ideal.mul_right hmin.1⟩⟩⟩ } #print set.subset_a def is_prime_ideal.to_maximal_ideal {α : Type*} [principal_ideal_domain α] (S : set α) [hpi : is_prime_ideal S] : is_maximal_ideal S := { eq_or_univ_of_subset := λ T hT (hST : S ⊆ T), begin haveI := principal_ideal_domain.principal S, haveI := @principal_ideal_domain.principal _ _ T { ..hT }, rw [← generator_generates S, ← generator_generates T] at *, cases hST (dvd_refl _) with x hx, cases is_prime_ideal.mem_or_mem_of_mul_mem (show generator T * x ∈ {x : α | generator S ∣ x}, by simp [hx.symm]), { exact or.inl (set.subset.antisymm (λ y, dvd.trans h) hST) }, { right, cases h with y hy, dsimp at *, } end, ..hpi }
a42f8a8efe9fc8dd21545cfdb339e5668f5001b2
9dc8cecdf3c4634764a18254e94d43da07142918
/src/measure_theory/measure/regular.lean
97c39ce5cf98dcaa71d0fcfba7e0cb29e75a8741
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
33,792
lean
/- Copyright (c) 2021 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris Van Doorn, Yury Kudryashov -/ import measure_theory.constructions.borel_space /-! # Regular measures A measure is `outer_regular` if the measure of any measurable set `A` is the infimum of `μ U` over all open sets `U` containing `A`. A measure is `regular` if it satisfies the following properties: * it is finite on compact sets; * it is outer regular; * it is inner regular for open sets with respect to compacts sets: the measure of any open set `U` is the supremum of `μ K` over all compact sets `K` contained in `U`. A measure is `weakly_regular` if it satisfies the following properties: * it is outer regular; * it is inner regular for open sets with respect to closed sets: the measure of any open set `U` is the supremum of `μ F` over all closed sets `F` contained in `U`. In a Hausdorff topological space, regularity implies weak regularity. These three conditions are registered as typeclasses for a measure `μ`, and this implication is recorded as an instance. In order to avoid code duplication, we also define a measure `μ` to be `inner_regular` for sets satisfying a predicate `q` with respect to sets satisfying a predicate `p` if for any set `U ∈ {U | q U}` and a number `r < μ U` there exists `F ⊆ U` such that `p F` and `r < μ F`. We prove that inner regularity for open sets with respect to compact sets or closed sets implies inner regularity for all measurable sets of finite measure (with respect to compact sets or closed sets respectively), and register some corollaries for (weakly) regular measures. Note that a similar statement for measurable sets of infinite mass can fail. For a counterexample, consider the group `ℝ × ℝ` where the first factor has the discrete topology and the second one the usual topology. It is a locally compact Hausdorff topological group, with Haar measure equal to Lebesgue measure on each vertical fiber. The set `ℝ × {0}` has infinite measure (by outer regularity), but any compact set it contains has zero measure (as it is finite). Several authors require as a definition of regularity that all measurable sets are inner regular. We have opted for the slightly weaker definition above as it holds for all Haar measures, it is enough for essentially all applications, and it is equivalent to the other definition when the measure is finite. The interest of the notion of weak regularity is that it is enough for many applications, and it is automatically satisfied by any finite measure on a metric space. ## Main definitions * `measure_theory.measure.outer_regular μ`: a typeclass registering that a measure `μ` on a topological space is outer regular. * `measure_theory.measure.regular μ`: a typeclass registering that a measure `μ` on a topological space is regular. * `measure_theory.measure.weakly_regular μ`: a typeclass registering that a measure `μ` on a topological space is weakly regular. * `measure_theory.measure.inner_regular μ p q`: a non-typeclass predicate saying that a measure `μ` is inner regular for sets satisfying `q` with respect to sets satisfying `p`. ## Main results ### Outer regular measures * `set.measure_eq_infi_is_open` asserts that, when `μ` is outer regular, the measure of a set is the infimum of the measure of open sets containing it. * `set.exists_is_open_lt_of_lt'` asserts that, when `μ` is outer regular, for every set `s` and `r > μ s` there exists an open superset `U ⊇ s` of measure less than `r`. * push forward of an outer regular measure is outer regular, and scalar multiplication of a regular measure by a finite number is outer regular. * `measure_theory.measure.outer_regular.of_sigma_compact_space_of_is_locally_finite_measure`: a locally finite measure on a `σ`-compact metric (or even pseudo emetric) space is outer regular. ### Weakly regular measures * `is_open.measure_eq_supr_is_closed` asserts that the measure of an open set is the supremum of the measure of closed sets it contains. * `is_open.exists_lt_is_closed`: for an open set `U` and `r < μ U`, there exists a closed `F ⊆ U` of measure greater than `r`; * `measurable_set.measure_eq_supr_is_closed_of_ne_top` asserts that the measure of a measurable set of finite measure is the supremum of the measure of closed sets it contains. * `measurable_set.exists_lt_is_closed_of_ne_top` and `measurable_set.exists_is_closed_lt_add`: a measurable set of finite measure can be approximated by a closed subset (stated as `r < μ F` and `μ s < μ F + ε`, respectively). * `measure_theory.measure.weakly_regular.of_pseudo_emetric_space_of_is_finite_measure` is an instance registering that a finite measure on a metric space is weakly regular (in fact, a pseudo emetric space is enough); * `measure_theory.measure.weakly_regular.of_pseudo_emetric_sigma_compact_space_of_locally_finite` is an instance registering that a locally finite measure on a `σ`-compact metric space (or even a pseudo emetric space) is weakly regular. ### Regular measures * `is_open.measure_eq_supr_is_compact` asserts that the measure of an open set is the supremum of the measure of compact sets it contains. * `is_open.exists_lt_is_compact`: for an open set `U` and `r < μ U`, there exists a compact `K ⊆ U` of measure greater than `r`; * `measurable_set.measure_eq_supr_is_compact_of_ne_top` asserts that the measure of a measurable set of finite measure is the supremum of the measure of compact sets it contains. * `measurable_set.exists_lt_is_compact_of_ne_top` and `measurable_set.exists_is_compact_lt_add`: a measurable set of finite measure can be approximated by a compact subset (stated as `r < μ K` and `μ s < μ K + ε`, respectively). * `measure_theory.measure.regular.of_sigma_compact_space_of_is_locally_finite_measure` is an instance registering that a locally finite measure on a `σ`-compact metric space is regular (in fact, an emetric space is enough). ## Implementation notes The main nontrivial statement is `measure_theory.measure.inner_regular.weakly_regular_of_finite`, expressing that in a finite measure space, if every open set can be approximated from inside by closed sets, then the measure is in fact weakly regular. To prove that we show that any measurable set can be approximated from inside by closed sets and from outside by open sets. This statement is proved by measurable induction, starting from open sets and checking that it is stable by taking complements (this is the point of this condition, being symmetrical between inside and outside) and countable disjoint unions. Once this statement is proved, one deduces results for `σ`-finite measures from this statement, by restricting them to finite measure sets (and proving that this restriction is weakly regular, using again the same statement). ## References [Halmos, Measure Theory, §52][halmos1950measure]. Note that Halmos uses an unusual definition of Borel sets (for him, they are elements of the `σ`-algebra generated by compact sets!), so his proofs or statements do not apply directly. [Billingsley, Convergence of Probability Measures][billingsley1999] -/ open set filter open_locale ennreal topological_space nnreal big_operators namespace measure_theory namespace measure /-- We say that a measure `μ` is *inner regular* with respect to predicates `p q : set α → Prop`, if for every `U` such that `q U` and `r < μ U`, there exists a subset `K ⊆ U` satisfying `p K` of measure greater than `r`. This definition is used to prove some facts about regular and weakly regular measures without repeating the proofs. -/ def inner_regular {α} {m : measurable_space α} (μ : measure α) (p q : set α → Prop) := ∀ ⦃U⦄, q U → ∀ r < μ U, ∃ K ⊆ U, p K ∧ r < μ K namespace inner_regular variables {α : Type*} {m : measurable_space α} {μ : measure α} {p q : set α → Prop} {U : set α} {ε : ℝ≥0∞} lemma measure_eq_supr (H : inner_regular μ p q) (hU : q U) : μ U = ⨆ (K ⊆ U) (hK : p K), μ K := begin refine le_antisymm (le_of_forall_lt $ λ r hr, _) (supr₂_le $ λ K hK, supr_le $ λ _, μ.mono hK), simpa only [lt_supr_iff, exists_prop] using H hU r hr end lemma exists_subset_lt_add (H : inner_regular μ p q) (h0 : p ∅) (hU : q U) (hμU : μ U ≠ ∞) (hε : ε ≠ 0) : ∃ K ⊆ U, p K ∧ μ U < μ K + ε := begin cases eq_or_ne (μ U) 0 with h₀ h₀, { refine ⟨∅, empty_subset _, h0, _⟩, rwa [measure_empty, h₀, zero_add, pos_iff_ne_zero] }, { rcases H hU _ (ennreal.sub_lt_self hμU h₀ hε) with ⟨K, hKU, hKc, hrK⟩, exact ⟨K, hKU, hKc, ennreal.lt_add_of_sub_lt_right (or.inl hμU) hrK⟩ } end lemma map {α β} [measurable_space α] [measurable_space β] {μ : measure α} {pa qa : set α → Prop} (H : inner_regular μ pa qa) (f : α ≃ β) (hf : ae_measurable f μ) {pb qb : set β → Prop} (hAB : ∀ U, qb U → qa (f ⁻¹' U)) (hAB' : ∀ K, pa K → pb (f '' K)) (hB₁ : ∀ K, pb K → measurable_set K) (hB₂ : ∀ U, qb U → measurable_set U) : inner_regular (map f μ) pb qb := begin intros U hU r hr, rw [map_apply_of_ae_measurable hf (hB₂ _ hU)] at hr, rcases H (hAB U hU) r hr with ⟨K, hKU, hKc, hK⟩, refine ⟨f '' K, image_subset_iff.2 hKU, hAB' _ hKc, _⟩, rwa [map_apply_of_ae_measurable hf (hB₁ _ $ hAB' _ hKc), f.preimage_image] end lemma smul (H : inner_regular μ p q) (c : ℝ≥0∞) : inner_regular (c • μ) p q := begin intros U hU r hr, rw [smul_apply, H.measure_eq_supr hU, smul_eq_mul] at hr, simpa only [ennreal.mul_supr, lt_supr_iff, exists_prop] using hr end lemma trans {q' : set α → Prop} (H : inner_regular μ p q) (H' : inner_regular μ q q') : inner_regular μ p q' := begin intros U hU r hr, rcases H' hU r hr with ⟨F, hFU, hqF, hF⟩, rcases H hqF _ hF with ⟨K, hKF, hpK, hrK⟩, exact ⟨K, hKF.trans hFU, hpK, hrK⟩ end end inner_regular variables {α β : Type*} [measurable_space α] [topological_space α] {μ : measure α} /-- A measure `μ` is outer regular if `μ(A) = inf {μ(U) | A ⊆ U open}` for a measurable set `A`. This definition implies the same equality for any (not necessarily measurable) set, see `set.measure_eq_infi_is_open`. -/ @[protect_proj] class outer_regular (μ : measure α) : Prop := (outer_regular : ∀ ⦃A : set α⦄, measurable_set A → ∀ r > μ A, ∃ U ⊇ A, is_open U ∧ μ U < r) /-- A measure `μ` is regular if - it is finite on all compact sets; - it is outer regular: `μ(A) = inf {μ(U) | A ⊆ U open}` for `A` measurable; - it is inner regular for open sets, using compact sets: `μ(U) = sup {μ(K) | K ⊆ U compact}` for `U` open. -/ @[protect_proj] class regular (μ : measure α) extends is_finite_measure_on_compacts μ, outer_regular μ : Prop := (inner_regular : inner_regular μ is_compact is_open) /-- A measure `μ` is weakly regular if - it is outer regular: `μ(A) = inf {μ(U) | A ⊆ U open}` for `A` measurable; - it is inner regular for open sets, using closed sets: `μ(U) = sup {μ(F) | F ⊆ U compact}` for `U` open. -/ @[protect_proj] class weakly_regular (μ : measure α) extends outer_regular μ : Prop := (inner_regular : inner_regular μ is_closed is_open) /-- A regular measure is weakly regular. -/ @[priority 100] -- see Note [lower instance priority] instance regular.weakly_regular [t2_space α] [regular μ] : weakly_regular μ := { inner_regular := λ U hU r hr, let ⟨K, hKU, hcK, hK⟩ := regular.inner_regular hU r hr in ⟨K, hKU, hcK.is_closed, hK⟩ } namespace outer_regular instance zero : outer_regular (0 : measure α) := ⟨λ A hA r hr, ⟨univ, subset_univ A, is_open_univ, hr⟩⟩ /-- Given `r` larger than the measure of a set `A`, there exists an open superset of `A` with measure less than `r`. -/ lemma _root_.set.exists_is_open_lt_of_lt [outer_regular μ] (A : set α) (r : ℝ≥0∞) (hr : μ A < r) : ∃ U ⊇ A, is_open U ∧ μ U < r := begin rcases outer_regular.outer_regular (measurable_set_to_measurable μ A) r (by rwa measure_to_measurable) with ⟨U, hAU, hUo, hU⟩, exact ⟨U, (subset_to_measurable _ _).trans hAU, hUo, hU⟩ end /-- For an outer regular measure, the measure of a set is the infimum of the measures of open sets containing it. -/ lemma _root_.set.measure_eq_infi_is_open (A : set α) (μ : measure α) [outer_regular μ] : μ A = (⨅ (U : set α) (h : A ⊆ U) (h2 : is_open U), μ U) := begin refine le_antisymm (le_infi₂ $ λ s hs, le_infi $ λ h2s, μ.mono hs) _, refine le_of_forall_lt' (λ r hr, _), simpa only [infi_lt_iff, exists_prop] using A.exists_is_open_lt_of_lt r hr end lemma _root_.set.exists_is_open_lt_add [outer_regular μ] (A : set α) (hA : μ A ≠ ∞) {ε : ℝ≥0∞} (hε : ε ≠ 0) : ∃ U ⊇ A, is_open U ∧ μ U < μ A + ε := A.exists_is_open_lt_of_lt _ (ennreal.lt_add_right hA hε) lemma _root_.set.exists_is_open_le_add (A : set α) (μ : measure α) [outer_regular μ] {ε : ℝ≥0∞} (hε : ε ≠ 0) : ∃ U ⊇ A, is_open U ∧ μ U ≤ μ A + ε := begin rcases le_or_lt ∞ (μ A) with H|H, { exact ⟨univ, subset_univ _, is_open_univ, by simp only [top_le_iff.mp H, ennreal.top_add, le_top]⟩ }, { rcases A.exists_is_open_lt_add H.ne hε with ⟨U, AU, U_open, hU⟩, exact ⟨U, AU, U_open, hU.le⟩ } end lemma _root_.measurable_set.exists_is_open_diff_lt [outer_regular μ] {A : set α} (hA : measurable_set A) (hA' : μ A ≠ ∞) {ε : ℝ≥0∞} (hε : ε ≠ 0) : ∃ U ⊇ A, is_open U ∧ μ U < ∞ ∧ μ (U \ A) < ε := begin rcases A.exists_is_open_lt_add hA' hε with ⟨U, hAU, hUo, hU⟩, use [U, hAU, hUo, hU.trans_le le_top], exact measure_diff_lt_of_lt_add hA hAU hA' hU, end protected lemma map [opens_measurable_space α] [measurable_space β] [topological_space β] [borel_space β] (f : α ≃ₜ β) (μ : measure α) [outer_regular μ] : (measure.map f μ).outer_regular := begin refine ⟨λ A hA r hr, _⟩, rw [map_apply f.measurable hA, ← f.image_symm] at hr, rcases set.exists_is_open_lt_of_lt _ r hr with ⟨U, hAU, hUo, hU⟩, have : is_open (f.symm ⁻¹' U), from hUo.preimage f.symm.continuous, refine ⟨f.symm ⁻¹' U, image_subset_iff.1 hAU, this, _⟩, rwa [map_apply f.measurable this.measurable_set, f.preimage_symm, f.preimage_image], end protected lemma smul (μ : measure α) [outer_regular μ] {x : ℝ≥0∞} (hx : x ≠ ∞) : (x • μ).outer_regular := begin rcases eq_or_ne x 0 with rfl|h0, { rw zero_smul, exact outer_regular.zero }, { refine ⟨λ A hA r hr, _⟩, rw [smul_apply, A.measure_eq_infi_is_open, smul_eq_mul] at hr, simpa only [ennreal.mul_infi_of_ne h0 hx, gt_iff_lt, infi_lt_iff, exists_prop] using hr } end end outer_regular /-- If a measure `μ` admits finite spanning open sets such that the restriction of `μ` to each set is outer regular, then the original measure is outer regular as well. -/ protected lemma finite_spanning_sets_in.outer_regular [opens_measurable_space α] {μ : measure α} (s : μ.finite_spanning_sets_in {U | is_open U ∧ outer_regular (μ.restrict U)}) : outer_regular μ := begin refine ⟨λ A hA r hr, _⟩, have hm : ∀ n, measurable_set (s.set n), from λ n, (s.set_mem n).1.measurable_set, haveI : ∀ n, outer_regular (μ.restrict (s.set n)) := λ n, (s.set_mem n).2, -- Note that `A = ⋃ n, A ∩ disjointed s n`. We replace `A` with this sequence. obtain ⟨A, hAm, hAs, hAd, rfl⟩ : ∃ A' : ℕ → set α, (∀ n, measurable_set (A' n)) ∧ (∀ n, A' n ⊆ s.set n) ∧ pairwise (disjoint on A') ∧ A = ⋃ n, A' n, { refine ⟨λ n, A ∩ disjointed s.set n, λ n, hA.inter (measurable_set.disjointed hm _), λ n, (inter_subset_right _ _).trans (disjointed_subset _ _), (disjoint_disjointed s.set).mono (λ k l hkl, hkl.mono inf_le_right inf_le_right), _⟩, rw [← inter_Union, Union_disjointed, s.spanning, inter_univ] }, rcases ennreal.exists_pos_sum_of_countable' (tsub_pos_iff_lt.2 hr).ne' ℕ with ⟨δ, δ0, hδε⟩, rw [lt_tsub_iff_right, add_comm] at hδε, have : ∀ n, ∃ U ⊇ A n, is_open U ∧ μ U < μ (A n) + δ n, { intro n, have H₁ : ∀ t, μ.restrict (s.set n) t = μ (t ∩ s.set n), from λ t, restrict_apply' (hm n), have Ht : μ.restrict (s.set n) (A n) ≠ ⊤, { rw H₁, exact ((measure_mono $ inter_subset_right _ _).trans_lt (s.finite n)).ne }, rcases (A n).exists_is_open_lt_add Ht (δ0 n).ne' with ⟨U, hAU, hUo, hU⟩, rw [H₁, H₁, inter_eq_self_of_subset_left (hAs _)] at hU, exact ⟨U ∩ s.set n, subset_inter hAU (hAs _), hUo.inter (s.set_mem n).1, hU⟩ }, choose U hAU hUo hU, refine ⟨⋃ n, U n, Union_mono hAU, is_open_Union hUo, _⟩, calc μ (⋃ n, U n) ≤ ∑' n, μ (U n) : measure_Union_le _ ... ≤ ∑' n, (μ (A n) + δ n) : ennreal.tsum_le_tsum (λ n, (hU n).le) ... = ∑' n, μ (A n) + ∑' n, δ n : ennreal.tsum_add ... = μ (⋃ n, A n) + ∑' n, δ n : congr_arg2 (+) (measure_Union hAd hAm).symm rfl ... < r : hδε end namespace inner_regular variables {p q : set α → Prop} {U s : set α} {ε r : ℝ≥0∞} /-- If a measure is inner regular (using closed or compact sets), then every measurable set of finite measure can by approximated by a (closed or compact) subset. -/ lemma measurable_set_of_open [outer_regular μ] (H : inner_regular μ p is_open) (h0 : p ∅) (hd : ∀ ⦃s U⦄, p s → is_open U → p (s \ U)) : inner_regular μ p (λ s, measurable_set s ∧ μ s ≠ ∞) := begin rintros s ⟨hs, hμs⟩ r hr, obtain ⟨ε, hε, hεs, rfl⟩ : ∃ ε ≠ 0, ε + ε ≤ μ s ∧ r = μ s - (ε + ε), { use (μ s - r) / 2, simp [*, hr.le, ennreal.add_halves, ennreal.sub_sub_cancel, le_add_right] }, rcases hs.exists_is_open_diff_lt hμs hε with ⟨U, hsU, hUo, hUt, hμU⟩, rcases (U \ s).exists_is_open_lt_of_lt _ hμU with ⟨U', hsU', hU'o, hμU'⟩, replace hsU' := diff_subset_comm.1 hsU', rcases H.exists_subset_lt_add h0 hUo hUt.ne hε with ⟨K, hKU, hKc, hKr⟩, refine ⟨K \ U', λ x hx, hsU' ⟨hKU hx.1, hx.2⟩, hd hKc hU'o, ennreal.sub_lt_of_lt_add hεs _⟩, calc μ s ≤ μ U : μ.mono hsU ... < μ K + ε : hKr ... ≤ μ (K \ U') + μ U' + ε : add_le_add_right (tsub_le_iff_right.1 le_measure_diff) _ ... ≤ μ (K \ U') + ε + ε : by { mono*, exacts [hμU'.le, le_rfl] } ... = μ (K \ U') + (ε + ε) : add_assoc _ _ _ end open finset /-- In a finite measure space, assume that any open set can be approximated from inside by closed sets. Then the measure is weakly regular. -/ lemma weakly_regular_of_finite [borel_space α] (μ : measure α) [is_finite_measure μ] (H : inner_regular μ is_closed is_open) : weakly_regular μ := begin have hfin : ∀ {s}, μ s ≠ ⊤ := measure_ne_top μ, suffices : ∀ s, measurable_set s → ∀ ε ≠ 0, ∃ (F ⊆ s) (U ⊇ s), is_closed F ∧ is_open U ∧ μ s ≤ μ F + ε ∧ μ U ≤ μ s + ε, { refine { outer_regular := λ s hs r hr, _, inner_regular := H }, rcases exists_between hr with ⟨r', hsr', hr'r⟩, rcases this s hs _ (tsub_pos_iff_lt.2 hsr').ne' with ⟨-, -, U, hsU, -, hUo, -, H⟩, refine ⟨U, hsU, hUo, _⟩, rw [add_tsub_cancel_of_le hsr'.le] at H, exact H.trans_lt hr'r }, refine measurable_set.induction_on_open _ _ _, /- The proof is by measurable induction: we should check that the property is true for the empty set, for open sets, and is stable by taking the complement and by taking countable disjoint unions. The point of the property we are proving is that it is stable by taking complements (exchanging the roles of closed and open sets and thanks to the finiteness of the measure). -/ -- check for open set { intros U hU ε hε, rcases H.exists_subset_lt_add is_closed_empty hU hfin hε with ⟨F, hsF, hFc, hF⟩, exact ⟨F, hsF, U, subset.rfl, hFc, hU, hF.le, le_self_add⟩ }, -- check for complements { rintros s hs H ε hε, rcases H ε hε with ⟨F, hFs, U, hsU, hFc, hUo, hF, hU⟩, refine ⟨Uᶜ, compl_subset_compl.2 hsU, Fᶜ, compl_subset_compl.2 hFs, hUo.is_closed_compl, hFc.is_open_compl, _⟩, simp only [measure_compl_le_add_iff, *, hUo.measurable_set, hFc.measurable_set, true_and] }, -- check for disjoint unions { intros s hsd hsm H ε ε0, have ε0' : ε / 2 ≠ 0, from (ennreal.half_pos ε0).ne', rcases ennreal.exists_pos_sum_of_countable' ε0' ℕ with ⟨δ, δ0, hδε⟩, choose F hFs U hsU hFc hUo hF hU using λ n, H n (δ n) (δ0 n).ne', -- the approximating closed set is constructed by considering finitely many sets `s i`, which -- cover all the measure up to `ε/2`, approximating each of these by a closed set `F i`, and -- taking the union of these (finitely many) `F i`. have : tendsto (λ t, ∑ k in t, μ (s k) + ε / 2) at_top (𝓝 $ μ (⋃ n, s n) + ε / 2), { rw measure_Union hsd hsm, exact tendsto.add ennreal.summable.has_sum tendsto_const_nhds }, rcases (this.eventually $ lt_mem_nhds $ ennreal.lt_add_right hfin ε0').exists with ⟨t, ht⟩, -- the approximating open set is constructed by taking for each `s n` an approximating open set -- `U n` with measure at most `μ (s n) + δ n` for a summable `δ`, and taking the union of these. refine ⟨⋃ k ∈ t, F k, Union_mono $ λ k, Union_subset $ λ _, hFs _, ⋃ n, U n, Union_mono hsU, is_closed_bUnion t.finite_to_set $ λ k _, hFc k, is_open_Union hUo, ht.le.trans _, _⟩, { calc ∑ k in t, μ (s k) + ε / 2 ≤ ∑ k in t, μ (F k) + ∑ k in t, δ k + ε / 2 : by { rw ← sum_add_distrib, exact add_le_add_right (sum_le_sum $ λ k hk, hF k) _ } ... ≤ ∑ k in t, μ (F k) + ε / 2 + ε / 2 : add_le_add_right (add_le_add_left ((ennreal.sum_le_tsum _).trans hδε.le) _) _ ... = μ (⋃ k ∈ t, F k) + ε : _, rw [measure_bUnion_finset, add_assoc, ennreal.add_halves], exacts [λ k _ n _ hkn, (hsd k n hkn).mono (hFs k) (hFs n), λ k hk, (hFc k).measurable_set] }, { calc μ (⋃ n, U n) ≤ ∑' n, μ (U n) : measure_Union_le _ ... ≤ ∑' n, (μ (s n) + δ n) : ennreal.tsum_le_tsum hU ... = μ (⋃ n, s n) + ∑' n, δ n : by rw [measure_Union hsd hsm, ennreal.tsum_add] ... ≤ μ (⋃ n, s n) + ε : add_le_add_left (hδε.le.trans ennreal.half_le_self) _ } } end /-- In a metric space (or even a pseudo emetric space), an open set can be approximated from inside by closed sets. -/ lemma of_pseudo_emetric_space {X : Type*} [pseudo_emetric_space X] [measurable_space X] (μ : measure X) : inner_regular μ is_closed is_open := begin intros U hU r hr, rcases hU.exists_Union_is_closed with ⟨F, F_closed, -, rfl, F_mono⟩, rw measure_Union_eq_supr F_mono.directed_le at hr, rcases lt_supr_iff.1 hr with ⟨n, hn⟩, exact ⟨F n, subset_Union _ _, F_closed n, hn⟩ end /-- In a `σ`-compact space, any closed set can be approximated by a compact subset. -/ lemma is_compact_is_closed {X : Type*} [topological_space X] [sigma_compact_space X] [measurable_space X] (μ : measure X) : inner_regular μ is_compact is_closed := begin intros F hF r hr, set B : ℕ → set X := compact_covering X, have hBc : ∀ n, is_compact (F ∩ B n), from λ n, (is_compact_compact_covering X n).inter_left hF, have hBU : (⋃ n, F ∩ B n) = F, by rw [← inter_Union, Union_compact_covering, set.inter_univ], have : μ F = ⨆ n, μ (F ∩ B n), { rw [← measure_Union_eq_supr, hBU], exact monotone.directed_le (λ m n h, inter_subset_inter_right _ (compact_covering_subset _ h)) }, rw this at hr, rcases lt_supr_iff.1 hr with ⟨n, hn⟩, exact ⟨_, inter_subset_left _ _, hBc n, hn⟩ end end inner_regular namespace regular instance zero : regular (0 : measure α) := ⟨λ U hU r hr, ⟨∅, empty_subset _, is_compact_empty, hr⟩⟩ /-- If `μ` is a regular measure, then any open set can be approximated by a compact subset. -/ lemma _root_.is_open.exists_lt_is_compact [regular μ] ⦃U : set α⦄ (hU : is_open U) {r : ℝ≥0∞} (hr : r < μ U) : ∃ K ⊆ U, is_compact K ∧ r < μ K := regular.inner_regular hU r hr /-- The measure of an open set is the supremum of the measures of compact sets it contains. -/ lemma _root_.is_open.measure_eq_supr_is_compact ⦃U : set α⦄ (hU : is_open U) (μ : measure α) [regular μ] : μ U = (⨆ (K : set α) (h : K ⊆ U) (h2 : is_compact K), μ K) := regular.inner_regular.measure_eq_supr hU lemma exists_compact_not_null [regular μ] : (∃ K, is_compact K ∧ μ K ≠ 0) ↔ μ ≠ 0 := by simp_rw [ne.def, ← measure_univ_eq_zero, is_open_univ.measure_eq_supr_is_compact, ennreal.supr_eq_zero, not_forall, exists_prop, subset_univ, true_and] /-- If `μ` is a regular measure, then any measurable set of finite measure can be approximated by a compact subset. See also `measurable_set.exists_is_compact_lt_add` and `measurable_set.exists_lt_is_compact_of_ne_top`. -/ lemma inner_regular_measurable [regular μ] : inner_regular μ is_compact (λ s, measurable_set s ∧ μ s ≠ ∞) := regular.inner_regular.measurable_set_of_open is_compact_empty (λ _ _, is_compact.diff) /-- If `μ` is a regular measure, then any measurable set of finite measure can be approximated by a compact subset. See also `measurable_set.exists_lt_is_compact_of_ne_top`. -/ lemma _root_.measurable_set.exists_is_compact_lt_add [regular μ] ⦃A : set α⦄ (hA : measurable_set A) (h'A : μ A ≠ ∞) {ε : ℝ≥0∞} (hε : ε ≠ 0) : ∃ K ⊆ A, is_compact K ∧ μ A < μ K + ε := regular.inner_regular_measurable.exists_subset_lt_add is_compact_empty ⟨hA, h'A⟩ h'A hε /-- If `μ` is a regular measure, then any measurable set of finite measure can be approximated by a compact subset. See also `measurable_set.exists_is_compact_lt_add` and `measurable_set.exists_lt_is_compact_of_ne_top`. -/ lemma _root_.measurable_set.exists_is_compact_diff_lt [opens_measurable_space α] [t2_space α] [regular μ] ⦃A : set α⦄ (hA : measurable_set A) (h'A : μ A ≠ ∞) {ε : ℝ≥0∞} (hε : ε ≠ 0) : ∃ K ⊆ A, is_compact K ∧ μ (A \ K) < ε := begin rcases hA.exists_is_compact_lt_add h'A hε with ⟨K, hKA, hKc, hK⟩, exact ⟨K, hKA, hKc, measure_diff_lt_of_lt_add hKc.measurable_set hKA (ne_top_of_le_ne_top h'A $ measure_mono hKA) hK⟩ end /-- If `μ` is a regular measure, then any measurable set of finite measure can be approximated by a compact subset. See also `measurable_set.exists_is_compact_lt_add`. -/ lemma _root_.measurable_set.exists_lt_is_compact_of_ne_top [regular μ] ⦃A : set α⦄ (hA : measurable_set A) (h'A : μ A ≠ ∞) {r : ℝ≥0∞} (hr : r < μ A) : ∃ K ⊆ A, is_compact K ∧ r < μ K := regular.inner_regular_measurable ⟨hA, h'A⟩ _ hr /-- Given a regular measure, any measurable set of finite mass can be approximated from inside by compact sets. -/ lemma _root_.measurable_set.measure_eq_supr_is_compact_of_ne_top [regular μ] ⦃A : set α⦄ (hA : measurable_set A) (h'A : μ A ≠ ∞) : μ A = (⨆ (K ⊆ A) (h : is_compact K), μ K) := regular.inner_regular_measurable.measure_eq_supr ⟨hA, h'A⟩ protected lemma map [opens_measurable_space α] [measurable_space β] [topological_space β] [t2_space β] [borel_space β] [regular μ] (f : α ≃ₜ β) : (measure.map f μ).regular := begin haveI := outer_regular.map f μ, haveI := is_finite_measure_on_compacts.map μ f, exact ⟨regular.inner_regular.map f.to_equiv f.measurable.ae_measurable (λ U hU, hU.preimage f.continuous) (λ K hK, hK.image f.continuous) (λ K hK, hK.measurable_set) (λ U hU, hU.measurable_set)⟩ end protected lemma smul [regular μ] {x : ℝ≥0∞} (hx : x ≠ ∞) : (x • μ).regular := begin haveI := outer_regular.smul μ hx, haveI := is_finite_measure_on_compacts.smul μ hx, exact ⟨regular.inner_regular.smul x⟩ end /-- A regular measure in a σ-compact space is σ-finite. -/ @[priority 100] -- see Note [lower instance priority] instance sigma_finite [sigma_compact_space α] [regular μ] : sigma_finite μ := ⟨⟨{ set := compact_covering α, set_mem := λ n, trivial, finite := λ n, (is_compact_compact_covering α n).measure_lt_top, spanning := Union_compact_covering α }⟩⟩ end regular namespace weakly_regular /-- If `μ` is a weakly regular measure, then any open set can be approximated by a closed subset. -/ lemma _root_.is_open.exists_lt_is_closed [weakly_regular μ] ⦃U : set α⦄ (hU : is_open U) {r : ℝ≥0∞} (hr : r < μ U) : ∃ F ⊆ U, is_closed F ∧ r < μ F := weakly_regular.inner_regular hU r hr /-- If `μ` is a weakly regular measure, then any open set can be approximated by a closed subset. -/ lemma _root_.is_open.measure_eq_supr_is_closed ⦃U : set α⦄ (hU : is_open U) (μ : measure α) [weakly_regular μ] : μ U = (⨆ (F ⊆ U) (h : is_closed F), μ F) := weakly_regular.inner_regular.measure_eq_supr hU lemma inner_regular_measurable [weakly_regular μ] : inner_regular μ is_closed (λ s, measurable_set s ∧ μ s ≠ ∞) := weakly_regular.inner_regular.measurable_set_of_open is_closed_empty (λ _ _ h₁ h₂, h₁.inter h₂.is_closed_compl) /-- If `s` is a measurable set, a weakly regular measure `μ` is finite on `s`, and `ε` is a positive number, then there exist a closed set `K ⊆ s` such that `μ s < μ K + ε`. -/ lemma _root_.measurable_set.exists_is_closed_lt_add [weakly_regular μ] {s : set α} (hs : measurable_set s) (hμs : μ s ≠ ∞) {ε : ℝ≥0∞} (hε : ε ≠ 0) : ∃ K ⊆ s, is_closed K ∧ μ s < μ K + ε := inner_regular_measurable.exists_subset_lt_add is_closed_empty ⟨hs, hμs⟩ hμs hε lemma _root_.measurable_set.exists_is_closed_diff_lt [opens_measurable_space α] [weakly_regular μ] ⦃A : set α⦄ (hA : measurable_set A) (h'A : μ A ≠ ∞) {ε : ℝ≥0∞} (hε : ε ≠ 0) : ∃ F ⊆ A, is_closed F ∧ μ (A \ F) < ε := begin rcases hA.exists_is_closed_lt_add h'A hε with ⟨F, hFA, hFc, hF⟩, exact ⟨F, hFA, hFc, measure_diff_lt_of_lt_add hFc.measurable_set hFA (ne_top_of_le_ne_top h'A $ measure_mono hFA) hF⟩ end /-- Given a weakly regular measure, any measurable set of finite mass can be approximated from inside by closed sets. -/ lemma _root_.measurable_set.exists_lt_is_closed_of_ne_top [weakly_regular μ] ⦃A : set α⦄ (hA : measurable_set A) (h'A : μ A ≠ ∞) {r : ℝ≥0∞} (hr : r < μ A) : ∃ K ⊆ A, is_closed K ∧ r < μ K := inner_regular_measurable ⟨hA, h'A⟩ _ hr /-- Given a weakly regular measure, any measurable set of finite mass can be approximated from inside by closed sets. -/ lemma _root_.measurable_set.measure_eq_supr_is_closed_of_ne_top [weakly_regular μ] ⦃A : set α⦄ (hA : measurable_set A) (h'A : μ A ≠ ∞) : μ A = (⨆ (K ⊆ A) (h : is_closed K), μ K) := inner_regular_measurable.measure_eq_supr ⟨hA, h'A⟩ /-- The restriction of a weakly regular measure to a measurable set of finite measure is weakly regular. -/ lemma restrict_of_measurable_set [borel_space α] [weakly_regular μ] (A : set α) (hA : measurable_set A) (h'A : μ A ≠ ∞) : weakly_regular (μ.restrict A) := begin haveI : fact (μ A < ∞) := ⟨h'A.lt_top⟩, refine inner_regular.weakly_regular_of_finite _ (λ V V_open, _), simp only [restrict_apply' hA], intros r hr, have : μ (V ∩ A) ≠ ∞, from ne_top_of_le_ne_top h'A (measure_mono $ inter_subset_right _ _), rcases (V_open.measurable_set.inter hA).exists_lt_is_closed_of_ne_top this hr with ⟨F, hFVA, hFc, hF⟩, refine ⟨F, hFVA.trans (inter_subset_left _ _), hFc, _⟩, rwa inter_eq_self_of_subset_left (hFVA.trans $ inter_subset_right _ _) end /-- Any finite measure on a metric space (or even a pseudo emetric space) is weakly regular. -/ @[priority 100] -- see Note [lower instance priority] instance of_pseudo_emetric_space_of_is_finite_measure {X : Type*} [pseudo_emetric_space X] [measurable_space X] [borel_space X] (μ : measure X) [is_finite_measure μ] : weakly_regular μ := (inner_regular.of_pseudo_emetric_space μ).weakly_regular_of_finite μ /-- Any locally finite measure on a `σ`-compact metric space (or even a pseudo emetric space) is weakly regular. -/ @[priority 100] -- see Note [lower instance priority] instance of_pseudo_emetric_sigma_compact_space_of_locally_finite {X : Type*} [pseudo_emetric_space X] [sigma_compact_space X] [measurable_space X] [borel_space X] (μ : measure X) [is_locally_finite_measure μ] : weakly_regular μ := begin haveI : outer_regular μ, { refine (μ.finite_spanning_sets_in_open.mono' $ λ U hU, _).outer_regular, haveI : fact (μ U < ∞), from ⟨hU.2⟩, exact ⟨hU.1, infer_instance⟩ }, exact ⟨inner_regular.of_pseudo_emetric_space μ⟩ end end weakly_regular /-- Any locally finite measure on a `σ`-compact (e)metric space is regular. -/ @[priority 100] -- see Note [lower instance priority] instance regular.of_sigma_compact_space_of_is_locally_finite_measure {X : Type*} [emetric_space X] [sigma_compact_space X] [measurable_space X] [borel_space X] (μ : measure X) [is_locally_finite_measure μ] : regular μ := { lt_top_of_is_compact := λ K hK, hK.measure_lt_top, inner_regular := (inner_regular.is_compact_is_closed μ).trans (inner_regular.of_pseudo_emetric_space μ) } end measure end measure_theory
fa29124fe7ef8c6168f29abba33bef4833f7998c
ed27983dd289b3bcad416f0b1927105d6ef19db8
/src/inClassNotes/type_library/nat_test.lean
61c3f415238b2b8678e6aa7c2a977d575b924e8a
[]
no_license
liuxin-James/complogic-s21
0d55b76dbe25024473d31d98b5b83655c365f811
13e03e0114626643b44015c654151fb651603486
refs/heads/master
1,681,109,264,463
1,618,848,261,000
1,618,848,261,000
337,599,491
0
0
null
1,613,141,619,000
1,612,925,555,000
null
UTF-8
Lean
false
false
5,930
lean
import .nat namespace hidden /- Finally, nat: a properly inductive type, where larger values of this type are constructed using smaller values *of the same type*. -/ def n0 := nat.zero def n1 := nat.succ _ def n2 := nat.succ _ variable n : nat -- assume (n : nat) #check nat.succ n -- succ is also nat /- To fully understand how this definition produces a set of terms that we can view interpret as representing natural numbers, you need a few additional facts: (1) Constructors are *disjoint*. This means that terms built using different constructors are never equal. So zero is not equal to any successor of zero. (2) Constructors are *injective*. This means applying a constructor different argument values always yields different results. It's especially useful in many cases to think of this rule backwards: (n : ℕ) (m : ℕ) (h: succ n = succ m) ------------------------------------- (n = m) Injective means that different values are sent to different places. So if succ is injective and it sends both n and m to the same value, then n and m must have the same value. So now, for example, we can't have succ 5 = zero (which would be the case for ℕ mod 5.) (3) The values of a type include *all* values obtainable by any *finite* number of applications of its constructors. In other words, a type is "closed" under finite applications of its constructors. So (succ n) is a nat for *any* n. (4) A type defines the *smallest* set of values closed under applications of its constructors. There are not values in a type that "creep in" wihtout being constructed by some finite number of applications of available constructors. What this means is that if we're given *any* value of a type, it *must* have been constructed using some constructor. applied to some arguments, and we can recover those specific arguments. This fact allows us to know that when we do case analysis by constructor, we are sure to match *any* value of any type. -/ /- The key to writing functions that take nat arguments is, as usual, case analysis. If we're given a natural number (term of type ℕ), n, we first determine which constructor was used to create it (zero or succ), and if it was succ, then what one-smaller nat value succ was applied to to produce our argument. -/ /- Zero. Zero is the smallest natural number. It is implemented by the zero constructor. -/ #eval nat.zero /- Successor. The successor function takes a nat and yields a one-bigger nat. It is implemented by the succ constructor. This constructor takes a term of type nat as an argument and constructs/returns a new term with one additional "succ" at its front. -/ #eval nat.succ nat.zero #eval nat.succ _ #eval nat.succ (nat.succ (nat.succ _ ) ) /- Inductive data type definitions define this kind of "tree-structured" data. In this case each "node" is either "zero" or "succ" and another "node" zero succ | zero succ | succ | ... any finite number of times | zero -/ /- We've now see how nat is defined. We close the hidden name space and just use Lean's nats from now on. The benefit is that we get additional Lean notations. -/ -- Compare this with what follows. #reduce nat.succ (nat.succ nat.zero) end hidden /- NOTATION (concrete syntax) -/ #eval nat.succ (nat.succ nat.zero) -- yay #check 2 variable (n : nat) /- The notation 2 *could* have been defined to mean (2 : nat), (2 : ℚ), (2 : ℝ), or (2 : ℂ), or even something else, but in Lean it's *hardwired* to mean (2 : ℕ) by default. If you want a 2 of type rational, real, or complex, for example, you have to say so explicitly, and you have to have imported the necessary math library. We skip that for now. -/ /- FUNCTIONS -/ def is0 : ℕ → bool | nat.zero := tt | _ := ff def inc (n : ℕ) := nat.succ n def pred' : nat → nat | nat.zero := nat.zero | (nat.succ n') := n' #eval pred' 0 #eval pred' 1 #eval pred' 5 -- equivalent def pred : nat → nat | nat.zero := nat.zero | (n' + 1) := n' -- this notation won't work def pred'' : nat → nat | nat.zero := nat.zero | 1 + n' := n' -- subtle difference in notations here #reduce n + 1 -- succ n #reduce 1 + n -- nat.add 1 n #reduce n + 2 #reduce 2 + n -- can only match constructor expressions /- Because larger values of type nat are constructed from smaller values of the same type, many functions that consume a nat argument will work by applying themselves recursively to smaller values of the same type. -/ def double : ℕ → ℕ | nat.zero := 0 -- 0 is nat.zero | (nat.succ n') := double n' + 2 def div2 : ℕ → ℕ | 0 := 0 | 1 := 0 | (n' + 2) := div2 n' + 1 #eval div2 5 -- Exercise: define mod2 def fac : ℕ → ℕ | 0 := 1 | (n' + 1) := (n' + 1) * fac n' def fib : ℕ → ℕ | 0 := 1 | 1 := 1 | (n' + 2) := fib (n' + 1) + fib n' #eval fib 6 #eval fac 3 /- Exercise: On a piece of paper, draw the computation tree for fib 5. fib 5 / \ fib 4 fib 3 / \ /\ ... fib 3 fib 2 / \ /\ ... fib 2 fib 1 / \ /\ ... fib 1 fib 0 | | 1 1 -/ /- Addition: 0 + m = m (n' + 1) + m = (n' + m) + 1 Be sure you understand that! -/ def add : ℕ → ℕ → ℕ | 0 m := m | (n' + 1) m := (add n' m) + 1 /- EXERCISE: write a mathematical definition of multiplication of n by m, then implement it, using your add function if/as necessary to carry out addition. -/ /- EXERCISE: write a mathematical definition of exponentiation and implement it, using your definition of multiplication if/as necessary. -/ /- Addition is iterated increment. Multiplication is iterated addition. Exponentiation is iterated multiplication. What function is iterated exponentiation? Exercise: implement it. -/
1fd0bbad566f06f6be00b25d672165c51ef18dcd
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/number_theory/divisors.lean
74836cc445837b0e467e82db78a18355983f5096
[ "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
12,851
lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import algebra.big_operators.order import data.nat.interval import data.nat.prime /-! # Divisor finsets This file defines sets of divisors of a natural number. This is particularly useful as background for defining Dirichlet convolution. ## Main Definitions Let `n : ℕ`. All of the following definitions are in the `nat` namespace: * `divisors n` is the `finset` of natural numbers that divide `n`. * `proper_divisors n` is the `finset` of natural numbers that divide `n`, other than `n`. * `divisors_antidiagonal n` is the `finset` of pairs `(x,y)` such that `x * y = n`. * `perfect n` is true when `n` is positive and the sum of `proper_divisors n` is `n`. ## Implementation details * `divisors 0`, `proper_divisors 0`, and `divisors_antidiagonal 0` are defined to be `∅`. ## Tags divisors, perfect numbers -/ open_locale classical open_locale big_operators open finset namespace nat variable (n : ℕ) /-- `divisors n` is the `finset` of divisors of `n`. As a special case, `divisors 0 = ∅`. -/ def divisors : finset ℕ := finset.filter (λ x : ℕ, x ∣ n) (finset.Ico 1 (n + 1)) /-- `proper_divisors n` is the `finset` of divisors of `n`, other than `n`. As a special case, `proper_divisors 0 = ∅`. -/ def proper_divisors : finset ℕ := finset.filter (λ x : ℕ, x ∣ n) (finset.Ico 1 n) /-- `divisors_antidiagonal n` is the `finset` of pairs `(x,y)` such that `x * y = n`. As a special case, `divisors_antidiagonal 0 = ∅`. -/ def divisors_antidiagonal : finset (ℕ × ℕ) := ((finset.Ico 1 (n + 1)).product (finset.Ico 1 (n + 1))).filter (λ x, x.fst * x.snd = n) variable {n} lemma proper_divisors.not_self_mem : ¬ n ∈ proper_divisors n := begin rw proper_divisors, simp, end @[simp] lemma mem_proper_divisors {m : ℕ} : n ∈ proper_divisors m ↔ n ∣ m ∧ n < m := begin rw [proper_divisors, finset.mem_filter, finset.mem_Ico, and_comm], apply and_congr_right, rw and_iff_right_iff_imp, intros hdvd hlt, apply nat.pos_of_ne_zero _, rintro rfl, rw zero_dvd_iff.1 hdvd at hlt, apply lt_irrefl 0 hlt, end lemma divisors_eq_proper_divisors_insert_self_of_pos (h : 0 < n): divisors n = has_insert.insert n (proper_divisors n) := by rw [divisors, proper_divisors, Ico_succ_right_eq_insert_Ico h, finset.filter_insert, if_pos (dvd_refl n)] @[simp] lemma mem_divisors {m : ℕ} : n ∈ divisors m ↔ (n ∣ m ∧ m ≠ 0) := begin cases m, { simp [divisors] }, simp only [divisors, finset.mem_Ico, ne.def, finset.mem_filter, succ_ne_zero, and_true, and_iff_right_iff_imp, not_false_iff], intro hdvd, split, { apply nat.pos_of_ne_zero, rintro rfl, apply nat.succ_ne_zero, rwa zero_dvd_iff at hdvd }, { rw nat.lt_succ_iff, apply nat.le_of_dvd (nat.succ_pos m) hdvd } end lemma dvd_of_mem_divisors {m : ℕ} (h : n ∈ divisors m) : n ∣ m := begin cases m, { apply dvd_zero }, { simp [mem_divisors.1 h], } end @[simp] lemma mem_divisors_antidiagonal {x : ℕ × ℕ} : x ∈ divisors_antidiagonal n ↔ x.fst * x.snd = n ∧ n ≠ 0 := begin simp only [divisors_antidiagonal, finset.mem_Ico, ne.def, finset.mem_filter, finset.mem_product], rw and_comm, apply and_congr_right, rintro rfl, split; intro h, { contrapose! h, simp [h], }, { rw [nat.lt_add_one_iff, nat.lt_add_one_iff], rw [mul_eq_zero, decidable.not_or_iff_and_not] at h, simp only [succ_le_of_lt (nat.pos_of_ne_zero h.1), succ_le_of_lt (nat.pos_of_ne_zero h.2), true_and], exact ⟨le_mul_of_pos_right (nat.pos_of_ne_zero h.2), le_mul_of_pos_left (nat.pos_of_ne_zero h.1)⟩ } end variable {n} lemma divisor_le {m : ℕ}: n ∈ divisors m → n ≤ m := begin cases m, { simp }, simp only [mem_divisors, m.succ_ne_zero, and_true, ne.def, not_false_iff], exact nat.le_of_dvd (nat.succ_pos m), end lemma divisors_subset_of_dvd {m : ℕ} (hzero : n ≠ 0) (h : m ∣ n) : divisors m ⊆ divisors n := finset.subset_iff.2 $ λ x hx, nat.mem_divisors.mpr (⟨(nat.mem_divisors.mp hx).1.trans h, hzero⟩) lemma divisors_subset_proper_divisors {m : ℕ} (hzero : n ≠ 0) (h : m ∣ n) (hdiff : m ≠ n) : divisors m ⊆ proper_divisors n := begin apply finset.subset_iff.2, intros x hx, exact nat.mem_proper_divisors.2 (⟨(nat.mem_divisors.1 hx).1.trans h, lt_of_le_of_lt (divisor_le hx) (lt_of_le_of_ne (divisor_le (nat.mem_divisors.2 ⟨h, hzero⟩)) hdiff)⟩) end @[simp] lemma divisors_zero : divisors 0 = ∅ := by { ext, simp } @[simp] lemma proper_divisors_zero : proper_divisors 0 = ∅ := by { ext, simp } lemma proper_divisors_subset_divisors : proper_divisors n ⊆ divisors n := begin cases n, { simp }, rw [divisors_eq_proper_divisors_insert_self_of_pos (nat.succ_pos _)], apply subset_insert, end @[simp] lemma divisors_one : divisors 1 = {1} := by { ext, simp } @[simp] lemma proper_divisors_one : proper_divisors 1 = ∅ := begin ext, simp only [finset.not_mem_empty, nat.dvd_one, not_and, not_lt, mem_proper_divisors, iff_false], apply ge_of_eq, end lemma pos_of_mem_divisors {m : ℕ} (h : m ∈ n.divisors) : 0 < m := begin cases m, { rw [mem_divisors, zero_dvd_iff] at h, rcases h with ⟨rfl, h⟩, exfalso, apply h rfl }, apply nat.succ_pos, end lemma pos_of_mem_proper_divisors {m : ℕ} (h : m ∈ n.proper_divisors) : 0 < m := pos_of_mem_divisors (proper_divisors_subset_divisors h) lemma one_mem_proper_divisors_iff_one_lt : 1 ∈ n.proper_divisors ↔ 1 < n := by rw [mem_proper_divisors, and_iff_right (one_dvd _)] @[simp] lemma divisors_antidiagonal_zero : divisors_antidiagonal 0 = ∅ := by { ext, simp } @[simp] lemma divisors_antidiagonal_one : divisors_antidiagonal 1 = {(1,1)} := by { ext, simp [nat.mul_eq_one_iff, prod.ext_iff], } lemma swap_mem_divisors_antidiagonal {x : ℕ × ℕ} (h : x ∈ divisors_antidiagonal n) : x.swap ∈ divisors_antidiagonal n := begin rw [mem_divisors_antidiagonal, mul_comm] at h, simp [h.1, h.2], end lemma fst_mem_divisors_of_mem_antidiagonal {x : ℕ × ℕ} (h : x ∈ divisors_antidiagonal n) : x.fst ∈ divisors n := begin rw mem_divisors_antidiagonal at h, simp [dvd.intro _ h.1, h.2], end lemma snd_mem_divisors_of_mem_antidiagonal {x : ℕ × ℕ} (h : x ∈ divisors_antidiagonal n) : x.snd ∈ divisors n := begin rw mem_divisors_antidiagonal at h, simp [dvd.intro_left _ h.1, h.2], end @[simp] lemma map_swap_divisors_antidiagonal : (divisors_antidiagonal n).map ⟨prod.swap, prod.swap_right_inverse.injective⟩ = divisors_antidiagonal n := begin ext, simp only [exists_prop, mem_divisors_antidiagonal, finset.mem_map, function.embedding.coe_fn_mk, ne.def, prod.swap_prod_mk, prod.exists], split, { rintros ⟨x, y, ⟨⟨rfl, h⟩, rfl⟩⟩, simp [mul_comm, h], }, { rintros ⟨rfl, h⟩, use [a.snd, a.fst], rw mul_comm, simp [h] } end lemma sum_divisors_eq_sum_proper_divisors_add_self : ∑ i in divisors n, i = ∑ i in proper_divisors n, i + n := begin cases n, { simp }, { rw [divisors_eq_proper_divisors_insert_self_of_pos (nat.succ_pos _), finset.sum_insert (proper_divisors.not_self_mem), add_comm] } end /-- `n : ℕ` is perfect if and only the sum of the proper divisors of `n` is `n` and `n` is positive. -/ def perfect (n : ℕ) : Prop := (∑ i in proper_divisors n, i = n) ∧ 0 < n theorem perfect_iff_sum_proper_divisors (h : 0 < n) : perfect n ↔ ∑ i in proper_divisors n, i = n := and_iff_left h theorem perfect_iff_sum_divisors_eq_two_mul (h : 0 < n) : perfect n ↔ ∑ i in divisors n, i = 2 * n := begin rw [perfect_iff_sum_proper_divisors h, sum_divisors_eq_sum_proper_divisors_add_self, two_mul], split; intro h, { rw h }, { apply add_right_cancel h } end lemma mem_divisors_prime_pow {p : ℕ} (pp : p.prime) (k : ℕ) {x : ℕ} : x ∈ divisors (p ^ k) ↔ ∃ (j : ℕ) (H : j ≤ k), x = p ^ j := by rw [mem_divisors, nat.dvd_prime_pow pp, and_iff_left (ne_of_gt (pow_pos pp.pos k))] lemma prime.divisors {p : ℕ} (pp : p.prime) : divisors p = {1, p} := begin ext, simp only [pp.ne_zero, and_true, ne.def, not_false_iff, finset.mem_insert, finset.mem_singleton, mem_divisors], refine ⟨pp.2 a, λ h, _⟩, rcases h; subst h, apply one_dvd, end lemma prime.proper_divisors {p : ℕ} (pp : p.prime) : proper_divisors p = {1} := by rw [← erase_insert (proper_divisors.not_self_mem), ← divisors_eq_proper_divisors_insert_self_of_pos pp.pos, pp.divisors, insert_singleton_comm, erase_insert (λ con, pp.ne_one (mem_singleton.1 con))] lemma divisors_prime_pow {p : ℕ} (pp : p.prime) (k : ℕ) : divisors (p ^ k) = (finset.range (k + 1)).map ⟨pow p, pow_right_injective pp.two_le⟩ := by { ext, simp [mem_divisors_prime_pow, pp, nat.lt_succ_iff, @eq_comm _ a] } lemma eq_proper_divisors_of_subset_of_sum_eq_sum {s : finset ℕ} (hsub : s ⊆ n.proper_divisors) : ∑ x in s, x = ∑ x in n.proper_divisors, x → s = n.proper_divisors := begin cases n, { rw [proper_divisors_zero, subset_empty] at hsub, simp [hsub] }, classical, rw [← sum_sdiff hsub], intros h, apply subset.antisymm hsub, rw [← sdiff_eq_empty_iff_subset], contrapose h, rw [← ne.def, ← nonempty_iff_ne_empty] at h, apply ne_of_lt, rw [← zero_add (∑ x in s, x), ← add_assoc, add_zero], apply add_lt_add_right, have hlt := sum_lt_sum_of_nonempty h (λ x hx, pos_of_mem_proper_divisors (sdiff_subset _ _ hx)), simp only [sum_const_zero] at hlt, apply hlt end lemma sum_proper_divisors_dvd (h : ∑ x in n.proper_divisors, x ∣ n) : (∑ x in n.proper_divisors, x = 1) ∨ (∑ x in n.proper_divisors, x = n) := begin cases n, { simp }, cases n, { contrapose! h, simp, }, rw or_iff_not_imp_right, intro ne_n, have hlt : ∑ x in n.succ.succ.proper_divisors, x < n.succ.succ := lt_of_le_of_ne (nat.le_of_dvd (nat.succ_pos _) h) ne_n, symmetry, rw [← mem_singleton, eq_proper_divisors_of_subset_of_sum_eq_sum (singleton_subset_iff.2 (mem_proper_divisors.2 ⟨h, hlt⟩)) sum_singleton, mem_proper_divisors], refine ⟨one_dvd _, nat.succ_lt_succ (nat.succ_pos _)⟩, end @[simp] lemma prime.sum_proper_divisors {α : Type*} [add_comm_monoid α] {p : ℕ} {f : ℕ → α} (h : p.prime) : ∑ x in p.proper_divisors, f x = f 1 := by simp [h.proper_divisors] @[simp] lemma prime.sum_divisors {α : Type*} [add_comm_monoid α] {p : ℕ} {f : ℕ → α} (h : p.prime) : ∑ x in p.divisors, f x = f p + f 1 := by rw [divisors_eq_proper_divisors_insert_self_of_pos h.pos, sum_insert proper_divisors.not_self_mem, h.sum_proper_divisors] lemma proper_divisors_eq_singleton_one_iff_prime : n.proper_divisors = {1} ↔ n.prime := ⟨λ h, begin have h1 := mem_singleton.2 rfl, rw [← h, mem_proper_divisors] at h1, refine ⟨h1.2, _⟩, intros m hdvd, rw [← mem_singleton, ← h, mem_proper_divisors], cases lt_or_eq_of_le (nat.le_of_dvd (lt_trans (nat.succ_pos _) h1.2) hdvd), { left, exact ⟨hdvd, h_1⟩ }, { right, exact h_1 } end, prime.proper_divisors⟩ lemma sum_proper_divisors_eq_one_iff_prime : ∑ x in n.proper_divisors, x = 1 ↔ n.prime := begin cases n, { simp [nat.not_prime_zero] }, cases n, { simp [nat.not_prime_one] }, rw [← proper_divisors_eq_singleton_one_iff_prime], refine ⟨λ h, _, λ h, h.symm ▸ sum_singleton⟩, rw [@eq_comm (finset ℕ) _ _], apply eq_proper_divisors_of_subset_of_sum_eq_sum (singleton_subset_iff.2 (one_mem_proper_divisors_iff_one_lt.2 (succ_lt_succ (nat.succ_pos _)))) (eq.trans sum_singleton h.symm) end @[simp] lemma prod_divisors_prime {α : Type*} [comm_monoid α] {p : ℕ} {f : ℕ → α} (h : p.prime) : ∏ x in p.divisors, f x = f p * f 1 := @prime.sum_divisors (additive α) _ _ _ h @[simp] lemma sum_divisors_prime_pow {α : Type*} [add_comm_monoid α] {k p : ℕ} {f : ℕ → α} (h : p.prime) : ∑ x in (p ^ k).divisors, f x = ∑ x in range (k + 1), f (p ^ x) := by simp [h, divisors_prime_pow] @[simp] lemma prod_divisors_prime_pow {α : Type*} [comm_monoid α] {k p : ℕ} {f : ℕ → α} (h : p.prime) : ∏ x in (p ^ k).divisors, f x = ∏ x in range (k + 1), f (p ^ x) := @sum_divisors_prime_pow (additive α) _ _ _ _ h @[simp] lemma filter_dvd_eq_divisors {n : ℕ} (h : n ≠ 0) : finset.filter (λ (x : ℕ), x ∣ n) (finset.range (n : ℕ).succ) = (n : ℕ).divisors := begin apply finset.ext, simp only [h, mem_filter, and_true, and_iff_right_iff_imp, cast_id, mem_range, ne.def, not_false_iff, mem_divisors], intros a ha, exact nat.lt_succ_of_le (nat.divisor_le (nat.mem_divisors.2 ⟨ha, h⟩)) end end nat
8e067668776fe8e491b81d78c60342616531dc98
d29d82a0af640c937e499f6be79fc552eae0aa13
/src/data/nat/gcd.lean
807839231aca21250d68e9a630f0f18dfc223c9a
[ "Apache-2.0" ]
permissive
AbdulMajeedkhurasani/mathlib
835f8a5c5cf3075b250b3737172043ab4fa1edf6
79bc7323b164aebd000524ebafd198eb0e17f956
refs/heads/master
1,688,003,895,660
1,627,788,521,000
1,627,788,521,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,471
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura -/ import data.nat.pow /-! # Definitions and properties of `gcd`, `lcm`, and `coprime` -/ namespace nat /-! ### `gcd` -/ theorem gcd_dvd (m n : ℕ) : (gcd m n ∣ m) ∧ (gcd m n ∣ n) := gcd.induction m n (λn, by rw gcd_zero_left; exact ⟨dvd_zero n, dvd_refl n⟩) (λm n npos, by rw ←gcd_rec; exact λ ⟨IH₁, IH₂⟩, ⟨IH₂, (dvd_mod_iff IH₂).1 IH₁⟩) theorem gcd_dvd_left (m n : ℕ) : gcd m n ∣ m := (gcd_dvd m n).left theorem gcd_dvd_right (m n : ℕ) : gcd m n ∣ n := (gcd_dvd m n).right theorem gcd_le_left {m} (n) (h : 0 < m) : gcd m n ≤ m := le_of_dvd h $ gcd_dvd_left m n theorem gcd_le_right (m) {n} (h : 0 < n) : gcd m n ≤ n := le_of_dvd h $ gcd_dvd_right m n theorem dvd_gcd {m n k : ℕ} : k ∣ m → k ∣ n → k ∣ gcd m n := gcd.induction m n (λn _ kn, by rw gcd_zero_left; exact kn) (λn m mpos IH H1 H2, by rw gcd_rec; exact IH ((dvd_mod_iff H1).2 H2) H1) theorem dvd_gcd_iff {m n k : ℕ} : k ∣ gcd m n ↔ k ∣ m ∧ k ∣ n := iff.intro (λ h, ⟨dvd_trans h (gcd_dvd m n).left, dvd_trans h (gcd_dvd m n).right⟩) (λ h, dvd_gcd h.left h.right) theorem gcd_comm (m n : ℕ) : gcd m n = gcd n m := dvd_antisymm (dvd_gcd (gcd_dvd_right m n) (gcd_dvd_left m n)) (dvd_gcd (gcd_dvd_right n m) (gcd_dvd_left n m)) theorem gcd_eq_left_iff_dvd {m n : ℕ} : m ∣ n ↔ gcd m n = m := ⟨λ h, by rw [gcd_rec, mod_eq_zero_of_dvd h, gcd_zero_left], λ h, h ▸ gcd_dvd_right m n⟩ theorem gcd_eq_right_iff_dvd {m n : ℕ} : m ∣ n ↔ gcd n m = m := by rw gcd_comm; apply gcd_eq_left_iff_dvd theorem gcd_assoc (m n k : ℕ) : gcd (gcd m n) k = gcd m (gcd n k) := dvd_antisymm (dvd_gcd (dvd.trans (gcd_dvd_left (gcd m n) k) (gcd_dvd_left m n)) (dvd_gcd (dvd.trans (gcd_dvd_left (gcd m n) k) (gcd_dvd_right m n)) (gcd_dvd_right (gcd m n) k))) (dvd_gcd (dvd_gcd (gcd_dvd_left m (gcd n k)) (dvd.trans (gcd_dvd_right m (gcd n k)) (gcd_dvd_left n k))) (dvd.trans (gcd_dvd_right m (gcd n k)) (gcd_dvd_right n k))) @[simp] theorem gcd_one_right (n : ℕ) : gcd n 1 = 1 := eq.trans (gcd_comm n 1) $ gcd_one_left n theorem gcd_mul_left (m n k : ℕ) : gcd (m * n) (m * k) = m * gcd n k := gcd.induction n k (λk, by repeat {rw mul_zero <|> rw gcd_zero_left}) (λk n H IH, by rwa [←mul_mod_mul_left, ←gcd_rec, ←gcd_rec] at IH) theorem gcd_mul_right (m n k : ℕ) : gcd (m * n) (k * n) = gcd m k * n := by rw [mul_comm m n, mul_comm k n, mul_comm (gcd m k) n, gcd_mul_left] theorem gcd_pos_of_pos_left {m : ℕ} (n : ℕ) (mpos : 0 < m) : 0 < gcd m n := pos_of_dvd_of_pos (gcd_dvd_left m n) mpos theorem gcd_pos_of_pos_right (m : ℕ) {n : ℕ} (npos : 0 < n) : 0 < gcd m n := pos_of_dvd_of_pos (gcd_dvd_right m n) npos theorem eq_zero_of_gcd_eq_zero_left {m n : ℕ} (H : gcd m n = 0) : m = 0 := or.elim (eq_zero_or_pos m) id (assume H1 : 0 < m, absurd (eq.symm H) (ne_of_lt (gcd_pos_of_pos_left _ H1))) theorem eq_zero_of_gcd_eq_zero_right {m n : ℕ} (H : gcd m n = 0) : n = 0 := by rw gcd_comm at H; exact eq_zero_of_gcd_eq_zero_left H theorem gcd_div {m n k : ℕ} (H1 : k ∣ m) (H2 : k ∣ n) : gcd (m / k) (n / k) = gcd m n / k := or.elim (eq_zero_or_pos k) (λk0, by rw [k0, nat.div_zero, nat.div_zero, nat.div_zero, gcd_zero_right]) (λH3, nat.eq_of_mul_eq_mul_right H3 $ by rw [ nat.div_mul_cancel (dvd_gcd H1 H2), ←gcd_mul_right, nat.div_mul_cancel H1, nat.div_mul_cancel H2]) theorem gcd_dvd_gcd_of_dvd_left {m k : ℕ} (n : ℕ) (H : m ∣ k) : gcd m n ∣ gcd k n := dvd_gcd (dvd.trans (gcd_dvd_left m n) H) (gcd_dvd_right m n) theorem gcd_dvd_gcd_of_dvd_right {m k : ℕ} (n : ℕ) (H : m ∣ k) : gcd n m ∣ gcd n k := dvd_gcd (gcd_dvd_left n m) (dvd.trans (gcd_dvd_right n m) H) theorem gcd_dvd_gcd_mul_left (m n k : ℕ) : gcd m n ∣ gcd (k * m) n := gcd_dvd_gcd_of_dvd_left _ (dvd_mul_left _ _) theorem gcd_dvd_gcd_mul_right (m n k : ℕ) : gcd m n ∣ gcd (m * k) n := gcd_dvd_gcd_of_dvd_left _ (dvd_mul_right _ _) theorem gcd_dvd_gcd_mul_left_right (m n k : ℕ) : gcd m n ∣ gcd m (k * n) := gcd_dvd_gcd_of_dvd_right _ (dvd_mul_left _ _) theorem gcd_dvd_gcd_mul_right_right (m n k : ℕ) : gcd m n ∣ gcd m (n * k) := gcd_dvd_gcd_of_dvd_right _ (dvd_mul_right _ _) theorem gcd_eq_left {m n : ℕ} (H : m ∣ n) : gcd m n = m := dvd_antisymm (gcd_dvd_left _ _) (dvd_gcd (dvd_refl _) H) theorem gcd_eq_right {m n : ℕ} (H : n ∣ m) : gcd m n = n := by rw [gcd_comm, gcd_eq_left H] @[simp] lemma gcd_mul_left_left (m n : ℕ) : gcd (m * n) n = n := dvd_antisymm (gcd_dvd_right _ _) (dvd_gcd (dvd_mul_left _ _) (dvd_refl _)) @[simp] lemma gcd_mul_left_right (m n : ℕ) : gcd n (m * n) = n := by rw [gcd_comm, gcd_mul_left_left] @[simp] lemma gcd_mul_right_left (m n : ℕ) : gcd (n * m) n = n := by rw [mul_comm, gcd_mul_left_left] @[simp] lemma gcd_mul_right_right (m n : ℕ) : gcd n (n * m) = n := by rw [gcd_comm, gcd_mul_right_left] @[simp] lemma gcd_gcd_self_right_left (m n : ℕ) : gcd m (gcd m n) = gcd m n := dvd_antisymm (gcd_dvd_right _ _) (dvd_gcd (gcd_dvd_left _ _) (dvd_refl _)) @[simp] lemma gcd_gcd_self_right_right (m n : ℕ) : gcd m (gcd n m) = gcd n m := by rw [gcd_comm n m, gcd_gcd_self_right_left] @[simp] lemma gcd_gcd_self_left_right (m n : ℕ) : gcd (gcd n m) m = gcd n m := by rw [gcd_comm, gcd_gcd_self_right_right] @[simp] lemma gcd_gcd_self_left_left (m n : ℕ) : gcd (gcd m n) m = gcd m n := by rw [gcd_comm m n, gcd_gcd_self_left_right] lemma gcd_add_mul_self (m n k : ℕ) : gcd m (n + k * m) = gcd m n := by simp [gcd_rec m (n + k * m), gcd_rec m n] theorem gcd_eq_zero_iff {i j : ℕ} : gcd i j = 0 ↔ i = 0 ∧ j = 0 := begin split, { intro h, exact ⟨eq_zero_of_gcd_eq_zero_left h, eq_zero_of_gcd_eq_zero_right h⟩, }, { intro h, rw [h.1, h.2], exact nat.gcd_zero_right _ } end /-! ### `lcm` -/ theorem lcm_comm (m n : ℕ) : lcm m n = lcm n m := by delta lcm; rw [mul_comm, gcd_comm] @[simp] theorem lcm_zero_left (m : ℕ) : lcm 0 m = 0 := by delta lcm; rw [zero_mul, nat.zero_div] @[simp] theorem lcm_zero_right (m : ℕ) : lcm m 0 = 0 := lcm_comm 0 m ▸ lcm_zero_left m @[simp] theorem lcm_one_left (m : ℕ) : lcm 1 m = m := by delta lcm; rw [one_mul, gcd_one_left, nat.div_one] @[simp] theorem lcm_one_right (m : ℕ) : lcm m 1 = m := lcm_comm 1 m ▸ lcm_one_left m @[simp] theorem lcm_self (m : ℕ) : lcm m m = m := or.elim (eq_zero_or_pos m) (λh, by rw [h, lcm_zero_left]) (λh, by delta lcm; rw [gcd_self, nat.mul_div_cancel _ h]) theorem dvd_lcm_left (m n : ℕ) : m ∣ lcm m n := dvd.intro (n / gcd m n) (nat.mul_div_assoc _ $ gcd_dvd_right m n).symm theorem dvd_lcm_right (m n : ℕ) : n ∣ lcm m n := lcm_comm n m ▸ dvd_lcm_left n m theorem gcd_mul_lcm (m n : ℕ) : gcd m n * lcm m n = m * n := by delta lcm; rw [nat.mul_div_cancel' (dvd.trans (gcd_dvd_left m n) (dvd_mul_right m n))] theorem lcm_dvd {m n k : ℕ} (H1 : m ∣ k) (H2 : n ∣ k) : lcm m n ∣ k := or.elim (eq_zero_or_pos k) (λh, by rw h; exact dvd_zero _) (λkpos, dvd_of_mul_dvd_mul_left (gcd_pos_of_pos_left n (pos_of_dvd_of_pos H1 kpos)) $ by rw [gcd_mul_lcm, ←gcd_mul_right, mul_comm n k]; exact dvd_gcd (mul_dvd_mul_left _ H2) (mul_dvd_mul_right H1 _)) lemma lcm_dvd_iff {m n k : ℕ} : lcm m n ∣ k ↔ m ∣ k ∧ n ∣ k := ⟨λ h, ⟨dvd_trans (dvd_lcm_left _ _) h, dvd_trans (dvd_lcm_right _ _) h⟩, and_imp.2 lcm_dvd⟩ theorem lcm_assoc (m n k : ℕ) : lcm (lcm m n) k = lcm m (lcm n k) := dvd_antisymm (lcm_dvd (lcm_dvd (dvd_lcm_left m (lcm n k)) (dvd.trans (dvd_lcm_left n k) (dvd_lcm_right m (lcm n k)))) (dvd.trans (dvd_lcm_right n k) (dvd_lcm_right m (lcm n k)))) (lcm_dvd (dvd.trans (dvd_lcm_left m n) (dvd_lcm_left (lcm m n) k)) (lcm_dvd (dvd.trans (dvd_lcm_right m n) (dvd_lcm_left (lcm m n) k)) (dvd_lcm_right (lcm m n) k))) theorem lcm_ne_zero {m n : ℕ} (hm : m ≠ 0) (hn : n ≠ 0) : lcm m n ≠ 0 := by { intro h, simpa [h, hm, hn] using gcd_mul_lcm m n, } /-! ### `coprime` See also `nat.coprime_of_dvd` and `nat.coprime_of_dvd'` to prove `nat.coprime m n`. -/ instance (m n : ℕ) : decidable (coprime m n) := by unfold coprime; apply_instance theorem coprime_iff_gcd_eq_one {m n : ℕ} : coprime m n ↔ gcd m n = 1 := iff.rfl theorem coprime.gcd_eq_one {m n : ℕ} : coprime m n → gcd m n = 1 := id theorem coprime.symm {m n : ℕ} : coprime n m → coprime m n := (gcd_comm m n).trans theorem coprime_comm {m n : ℕ} : coprime n m ↔ coprime m n := ⟨coprime.symm, coprime.symm⟩ theorem coprime.dvd_of_dvd_mul_right {m n k : ℕ} (H1 : coprime k n) (H2 : k ∣ m * n) : k ∣ m := let t := dvd_gcd (dvd_mul_left k m) H2 in by rwa [gcd_mul_left, H1.gcd_eq_one, mul_one] at t theorem coprime.dvd_of_dvd_mul_left {m n k : ℕ} (H1 : coprime k m) (H2 : k ∣ m * n) : k ∣ n := by rw mul_comm at H2; exact H1.dvd_of_dvd_mul_right H2 theorem coprime.gcd_mul_left_cancel {k : ℕ} (m : ℕ) {n : ℕ} (H : coprime k n) : gcd (k * m) n = gcd m n := have H1 : coprime (gcd (k * m) n) k, by rw [coprime, gcd_assoc, H.symm.gcd_eq_one, gcd_one_right], dvd_antisymm (dvd_gcd (H1.dvd_of_dvd_mul_left (gcd_dvd_left _ _)) (gcd_dvd_right _ _)) (gcd_dvd_gcd_mul_left _ _ _) theorem coprime.gcd_mul_right_cancel (m : ℕ) {k n : ℕ} (H : coprime k n) : gcd (m * k) n = gcd m n := by rw [mul_comm m k, H.gcd_mul_left_cancel m] theorem coprime.gcd_mul_left_cancel_right {k m : ℕ} (n : ℕ) (H : coprime k m) : gcd m (k * n) = gcd m n := by rw [gcd_comm m n, gcd_comm m (k * n), H.gcd_mul_left_cancel n] theorem coprime.gcd_mul_right_cancel_right {k m : ℕ} (n : ℕ) (H : coprime k m) : gcd m (n * k) = gcd m n := by rw [mul_comm n k, H.gcd_mul_left_cancel_right n] theorem coprime_div_gcd_div_gcd {m n : ℕ} (H : 0 < gcd m n) : coprime (m / gcd m n) (n / gcd m n) := by rw [coprime_iff_gcd_eq_one, gcd_div (gcd_dvd_left m n) (gcd_dvd_right m n), nat.div_self H] theorem not_coprime_of_dvd_of_dvd {m n d : ℕ} (dgt1 : 1 < d) (Hm : d ∣ m) (Hn : d ∣ n) : ¬ coprime m n := λ co, not_lt_of_ge (le_of_dvd zero_lt_one $ by rw [←co.gcd_eq_one]; exact dvd_gcd Hm Hn) dgt1 theorem exists_coprime {m n : ℕ} (H : 0 < gcd m n) : ∃ m' n', coprime m' n' ∧ m = m' * gcd m n ∧ n = n' * gcd m n := ⟨_, _, coprime_div_gcd_div_gcd H, (nat.div_mul_cancel (gcd_dvd_left m n)).symm, (nat.div_mul_cancel (gcd_dvd_right m n)).symm⟩ theorem exists_coprime' {m n : ℕ} (H : 0 < gcd m n) : ∃ g m' n', 0 < g ∧ coprime m' n' ∧ m = m' * g ∧ n = n' * g := let ⟨m', n', h⟩ := exists_coprime H in ⟨_, m', n', H, h⟩ theorem coprime.mul {m n k : ℕ} (H1 : coprime m k) (H2 : coprime n k) : coprime (m * n) k := (H1.gcd_mul_left_cancel n).trans H2 theorem coprime.mul_right {k m n : ℕ} (H1 : coprime k m) (H2 : coprime k n) : coprime k (m * n) := (H1.symm.mul H2.symm).symm theorem coprime.coprime_dvd_left {m k n : ℕ} (H1 : m ∣ k) (H2 : coprime k n) : coprime m n := eq_one_of_dvd_one (by delta coprime at H2; rw ← H2; exact gcd_dvd_gcd_of_dvd_left _ H1) theorem coprime.coprime_dvd_right {m k n : ℕ} (H1 : n ∣ m) (H2 : coprime k m) : coprime k n := (H2.symm.coprime_dvd_left H1).symm theorem coprime.coprime_mul_left {k m n : ℕ} (H : coprime (k * m) n) : coprime m n := H.coprime_dvd_left (dvd_mul_left _ _) theorem coprime.coprime_mul_right {k m n : ℕ} (H : coprime (m * k) n) : coprime m n := H.coprime_dvd_left (dvd_mul_right _ _) theorem coprime.coprime_mul_left_right {k m n : ℕ} (H : coprime m (k * n)) : coprime m n := H.coprime_dvd_right (dvd_mul_left _ _) theorem coprime.coprime_mul_right_right {k m n : ℕ} (H : coprime m (n * k)) : coprime m n := H.coprime_dvd_right (dvd_mul_right _ _) theorem coprime.coprime_div_left {m n a : ℕ} (cmn : coprime m n) (dvd : a ∣ m) : coprime (m / a) n := begin by_cases a_split : (a = 0), { subst a_split, rw zero_dvd_iff at dvd, simpa [dvd] using cmn, }, { rcases dvd with ⟨k, rfl⟩, rw nat.mul_div_cancel_left _ (nat.pos_of_ne_zero a_split), exact coprime.coprime_mul_left cmn, }, end theorem coprime.coprime_div_right {m n a : ℕ} (cmn : coprime m n) (dvd : a ∣ n) : coprime m (n / a) := (coprime.coprime_div_left cmn.symm dvd).symm lemma coprime_mul_iff_left {k m n : ℕ} : coprime (m * n) k ↔ coprime m k ∧ coprime n k := ⟨λ h, ⟨coprime.coprime_mul_right h, coprime.coprime_mul_left h⟩, λ ⟨h, _⟩, by rwa [coprime_iff_gcd_eq_one, coprime.gcd_mul_left_cancel n h]⟩ lemma coprime_mul_iff_right {k m n : ℕ} : coprime k (m * n) ↔ coprime k m ∧ coprime k n := by simpa only [coprime_comm] using coprime_mul_iff_left lemma coprime.gcd_left (k : ℕ) {m n : ℕ} (hmn : coprime m n) : coprime (gcd k m) n := hmn.coprime_dvd_left $ gcd_dvd_right k m lemma coprime.gcd_right (k : ℕ) {m n : ℕ} (hmn : coprime m n) : coprime m (gcd k n) := hmn.coprime_dvd_right $ gcd_dvd_right k n lemma coprime.gcd_both (k l : ℕ) {m n : ℕ} (hmn : coprime m n) : coprime (gcd k m) (gcd l n) := (hmn.gcd_left k).gcd_right l lemma coprime.mul_dvd_of_dvd_of_dvd {a n m : ℕ} (hmn : coprime m n) (hm : m ∣ a) (hn : n ∣ a) : m * n ∣ a := let ⟨k, hk⟩ := hm in hk.symm ▸ mul_dvd_mul_left _ (hmn.symm.dvd_of_dvd_mul_left (hk ▸ hn)) theorem coprime_one_left : ∀ n, coprime 1 n := gcd_one_left theorem coprime_one_right : ∀ n, coprime n 1 := gcd_one_right theorem coprime.pow_left {m k : ℕ} (n : ℕ) (H1 : coprime m k) : coprime (m ^ n) k := nat.rec_on n (coprime_one_left _) (λn IH, H1.mul IH) theorem coprime.pow_right {m k : ℕ} (n : ℕ) (H1 : coprime k m) : coprime k (m ^ n) := (H1.symm.pow_left n).symm theorem coprime.pow {k l : ℕ} (m n : ℕ) (H1 : coprime k l) : coprime (k ^ m) (l ^ n) := (H1.pow_left _).pow_right _ theorem coprime.eq_one_of_dvd {k m : ℕ} (H : coprime k m) (d : k ∣ m) : k = 1 := by rw [← H.gcd_eq_one, gcd_eq_left d] @[simp] theorem coprime_zero_left (n : ℕ) : coprime 0 n ↔ n = 1 := by simp [coprime] @[simp] theorem coprime_zero_right (n : ℕ) : coprime n 0 ↔ n = 1 := by simp [coprime] theorem not_coprime_zero_zero : ¬ coprime 0 0 := by simp @[simp] theorem coprime_one_left_iff (n : ℕ) : coprime 1 n ↔ true := by simp [coprime] @[simp] theorem coprime_one_right_iff (n : ℕ) : coprime n 1 ↔ true := by simp [coprime] @[simp] theorem coprime_self (n : ℕ) : coprime n n ↔ n = 1 := by simp [coprime] lemma coprime.eq_of_mul_eq_zero {m n : ℕ} (h : m.coprime n) (hmn : m * n = 0) : m = 0 ∧ n = 1 ∨ m = 1 ∧ n = 0 := (nat.eq_zero_of_mul_eq_zero hmn).imp (λ hm, ⟨hm, n.coprime_zero_left.mp $ hm ▸ h⟩) (λ hn, ⟨m.coprime_zero_left.mp $ hn ▸ h.symm, hn⟩) /-- Represent a divisor of `m * n` as a product of a divisor of `m` and a divisor of `n`. -/ def prod_dvd_and_dvd_of_dvd_prod {m n k : ℕ} (H : k ∣ m * n) : { d : {m' // m' ∣ m} × {n' // n' ∣ n} // k = d.1 * d.2 } := begin cases h0 : (gcd k m), case nat.zero { have : k = 0 := eq_zero_of_gcd_eq_zero_left h0, subst this, have : m = 0 := eq_zero_of_gcd_eq_zero_right h0, subst this, exact ⟨⟨⟨0, dvd_refl 0⟩, ⟨n, dvd_refl n⟩⟩, (zero_mul n).symm⟩ }, case nat.succ : tmp { have hpos : 0 < gcd k m := h0.symm ▸ nat.zero_lt_succ _; clear h0 tmp, have hd : gcd k m * (k / gcd k m) = k := (nat.mul_div_cancel' (gcd_dvd_left k m)), refine ⟨⟨⟨gcd k m, gcd_dvd_right k m⟩, ⟨k / gcd k m, _⟩⟩, hd.symm⟩, apply dvd_of_mul_dvd_mul_left hpos, rw [hd, ← gcd_mul_right], exact dvd_gcd (dvd_mul_right _ _) H } end theorem gcd_mul_dvd_mul_gcd (k m n : ℕ) : gcd k (m * n) ∣ gcd k m * gcd k n := begin rcases (prod_dvd_and_dvd_of_dvd_prod $ gcd_dvd_right k (m * n)) with ⟨⟨⟨m', hm'⟩, ⟨n', hn'⟩⟩, h⟩, replace h : gcd k (m * n) = m' * n' := h, rw h, have hm'n' : m' * n' ∣ k := h ▸ gcd_dvd_left _ _, apply mul_dvd_mul, { have hm'k : m' ∣ k := dvd_trans (dvd_mul_right m' n') hm'n', exact dvd_gcd hm'k hm' }, { have hn'k : n' ∣ k := dvd_trans (dvd_mul_left n' m') hm'n', exact dvd_gcd hn'k hn' } end theorem coprime.gcd_mul (k : ℕ) {m n : ℕ} (h : coprime m n) : gcd k (m * n) = gcd k m * gcd k n := dvd_antisymm (gcd_mul_dvd_mul_gcd k m n) ((h.gcd_both k k).mul_dvd_of_dvd_of_dvd (gcd_dvd_gcd_mul_right_right _ _ _) (gcd_dvd_gcd_mul_left_right _ _ _)) theorem pow_dvd_pow_iff {a b n : ℕ} (n0 : 0 < n) : a ^ n ∣ b ^ n ↔ a ∣ b := begin refine ⟨λ h, _, λ h, pow_dvd_pow_of_dvd h _⟩, cases eq_zero_or_pos (gcd a b) with g0 g0, { simp [eq_zero_of_gcd_eq_zero_right g0] }, rcases exists_coprime' g0 with ⟨g, a', b', g0', co, rfl, rfl⟩, rw [mul_pow, mul_pow] at h, replace h := dvd_of_mul_dvd_mul_right (pow_pos g0' _) h, have := pow_dvd_pow a' n0, rw [pow_one, (co.pow n n).eq_one_of_dvd h] at this, simp [eq_one_of_dvd_one this] end lemma gcd_mul_gcd_of_coprime_of_mul_eq_mul {a b c d : ℕ} (cop : c.coprime d) (h : a * b = c * d) : a.gcd c * b.gcd c = c := begin apply dvd_antisymm, { apply nat.coprime.dvd_of_dvd_mul_right (nat.coprime.mul (cop.gcd_left _) (cop.gcd_left _)), rw ← h, apply mul_dvd_mul (gcd_dvd _ _).1 (gcd_dvd _ _).1 }, { rw [gcd_comm a _, gcd_comm b _], transitivity c.gcd (a * b), rw [h, gcd_mul_right_right d c], apply gcd_mul_dvd_mul_gcd } end end nat
2b021f7db95266bd83efcd7f7cb9f47dd16732d9
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/univ_delay_thm_bug.lean
415ef0b3a6c26cae33a6126d306f38633d32fec1
[ "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
88
lean
lemma foo : ∀B A : Type _, ∀a:A, a=a := let x : ∀A, ∀a:A, a=a := @rfl in λB, x
07efc8acaa03eeaf548e672034dafaf1a7705433
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/int/range.lean
9bde57f55dfcd54ea4c1bcbf1439f22895bd4d1a
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
2,137
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Kenny Lau -/ import data.list.range import data.int.order.basic /-! # Intervals in ℤ > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines integer ranges. `range m n` is the set of integers greater than `m` and strictly less than `n`. ## Note This could be unified with `data.list.intervals`. See the TODOs there. -/ namespace int /-- List enumerating `[m, n)`. This is the ℤ variant of `list.Ico`. -/ def range (m n : ℤ) : list ℤ := (list.range (to_nat (n-m))).map $ λ r, m+r theorem mem_range_iff {m n r : ℤ} : r ∈ range m n ↔ m ≤ r ∧ r < n := ⟨λ H, let ⟨s, h1, h2⟩ := list.mem_map.1 H in h2 ▸ ⟨le_add_of_nonneg_right (coe_zero_le s), add_lt_of_lt_sub_left $ match n-m, h1 with | (k:ℕ), h1 := by rwa [list.mem_range, to_nat_coe_nat, ← coe_nat_lt] at h1 end⟩, λ ⟨h1, h2⟩, list.mem_map.2 ⟨to_nat (r-m), list.mem_range.2 $ by rw [← coe_nat_lt, to_nat_of_nonneg (sub_nonneg_of_le h1), to_nat_of_nonneg (sub_nonneg_of_le (le_of_lt (lt_of_le_of_lt h1 h2)))]; exact sub_lt_sub_right h2 _, show m + _ = _, by rw [to_nat_of_nonneg (sub_nonneg_of_le h1), add_sub_cancel'_right]⟩⟩ instance decidable_le_lt (P : int → Prop) [decidable_pred P] (m n : ℤ) : decidable (∀ r, m ≤ r → r < n → P r) := decidable_of_iff (∀ r ∈ range m n, P r) $ by simp only [mem_range_iff, and_imp] instance decidable_le_le (P : int → Prop) [decidable_pred P] (m n : ℤ) : decidable (∀ r, m ≤ r → r ≤ n → P r) := decidable_of_iff (∀ r ∈ range m (n+1), P r) $ by simp only [mem_range_iff, and_imp, lt_add_one_iff] instance decidable_lt_lt (P : int → Prop) [decidable_pred P] (m n : ℤ) : decidable (∀ r, m < r → r < n → P r) := int.decidable_le_lt P _ _ instance decidable_lt_le (P : int → Prop) [decidable_pred P] (m n : ℤ) : decidable (∀ r, m < r → r ≤ n → P r) := int.decidable_le_le P _ _ end int
c772e17d3fd7d81548222b08d14065845ada3cb9
94637389e03c919023691dcd05bd4411b1034aa5
/src/inClassNotes/type_library/prod.lean
c5f7dbcd3037c75662bf8c9a0de00846763309ec
[]
no_license
kevinsullivan/complogic-s21
7c4eef2105abad899e46502270d9829d913e8afc
99039501b770248c8ceb39890be5dfe129dc1082
refs/heads/master
1,682,985,669,944
1,621,126,241,000
1,621,126,241,000
335,706,272
0
38
null
1,618,325,669,000
1,612,374,118,000
Lean
UTF-8
Lean
false
false
408
lean
namespace hidden /- The polymorphic type, prod α β, is the type whose values are ordered pairs the first elements of which are of type α and the second elements of which are of type β. -/ universe u structure prod (α β : Type u) : Type u := (fst : α) (snd : β) /- The names of the fields, fst and snd, are also used to generated projection functions for the corresponding fields. -/ end hidden
ebfc83aa4b6f81cc21e89290a2f9cc72941ed139
a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7
/src/linear_algebra/nonsingular_inverse.lean
6226ef30518ad0f455b08a03d8f99bfbb1ddfdc0
[ "Apache-2.0" ]
permissive
kmill/mathlib
ea5a007b67ae4e9e18dd50d31d8aa60f650425ee
1a419a9fea7b959317eddd556e1bb9639f4dcc05
refs/heads/master
1,668,578,197,719
1,593,629,163,000
1,593,629,163,000
276,482,939
0
0
null
1,593,637,960,000
1,593,637,959,000
null
UTF-8
Lean
false
false
17,385
lean
/- Copyright (c) 2019 Tim Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Tim Baanen. -/ import algebra.associated import linear_algebra.determinant import tactic.linarith import tactic.ring_exp /-! # Nonsingular inverses In this file, we define an inverse for square matrices of invertible determinant. For matrices that are not square or not of full rank, there is a more general notion of pseudoinverses. Unfortunately, the definition of pseudoinverses is typically in terms of inverses of nonsingular matrices, so we need to define those first. The file also doesn't define a `has_inv` instance for `matrix` so that can be used for the pseudoinverse instead. The definition of inverse used in this file is the adjugate divided by the determinant. The adjugate is calculated with Cramer's rule, which we introduce first. The vectors returned by Cramer's rule are given by the linear map `cramer`, which sends a matrix `A` and vector `b` to the vector consisting of the determinant of replacing the `i`th column of `A` with `b` at index `i` (written as `(A.update_column i b).det`). Using Cramer's rule, we can compute for each matrix `A` the matrix `adjugate A`. The entries of the adjugate are the determinants of each minor of `A`. Instead of defining a minor to be `A` with column `i` and row `j` deleted, we replace the `i`th column of `A` with the `j`th basis vector; this has the same determinant as the minor but more importantly equals Cramer's rule applied to `A` and the `j`th basis vector, simplifying the subsequent proofs. We prove the adjugate behaves like `det A • A⁻¹`. Finally, we show that dividing the adjugate by `det A` (if possible), giving a matrix `nonsing_inv A`, will result in a multiplicative inverse to `A`. ## References * https://en.wikipedia.org/wiki/Cramer's_rule#Finding_inverse_matrix ## Tags matrix inverse, cramer, cramer's rule, adjugate -/ namespace matrix universes u v variables {n : Type u} [fintype n] [decidable_eq n] {α : Type v} open_locale matrix big_operators open equiv equiv.perm finset section update /-- Update, i.e. replace the `i`th column of matrix `A` with the values in `b`. -/ def update_column (A : matrix n n α) (i : n) (b : n → α) : matrix n n α := function.update A i b /-- Update, i.e. replace the `i`th row of matrix `A` with the values in `b`. -/ def update_row (A : matrix n n α) (j : n) (b : n → α) : matrix n n α := λ i, function.update (A i) j (b i) variables {A : matrix n n α} {i j : n} {b : n → α} @[simp] lemma update_column_self : update_column A i b i = b := function.update_same i b A @[simp] lemma update_row_self : update_row A j b i j = b i := function.update_same j (b i) (A i) @[simp] lemma update_column_ne {i' : n} (i_ne : i' ≠ i) : update_column A i b i' = A i' := function.update_noteq i_ne b A @[simp] lemma update_row_ne {j' : n} (j_ne : j' ≠ j) : update_row A j b i j' = A i j' := function.update_noteq j_ne (b i) (A i) lemma update_column_val {i' : n} : update_column A i b i' j = if i' = i then b j else A i' j := begin by_cases i' = i, { rw [h, update_column_self, if_pos rfl] }, { rw [update_column_ne h, if_neg h] } end lemma update_row_val {j' : n} : update_row A j b i j' = if j' = j then b i else A i j' := begin by_cases j' = j, { rw [h, update_row_self, if_pos rfl] }, { rw [update_row_ne h, if_neg h] } end lemma update_column_transpose : update_column Aᵀ i b = (update_row A i b)ᵀ := begin ext i' j, rw [transpose_val, update_column_val, update_row_val], refl end end update section cramer /-! ### `cramer` section Introduce the linear map `cramer` with values defined by `cramer_map`. After defining `cramer_map` and showing it is linear, we will restrict our proofs to using `cramer`. -/ variables [comm_ring α] (A : matrix n n α) (b : n → α) /-- `cramer_map A b i` is the determinant of the matrix `A` with column `i` replaced with `b`, and thus `cramer_map A b` is the vector output by Cramer's rule on `A` and `b`. If `A ⬝ x = b` has a unique solution in `x`, `cramer_map` sends a square matrix `A` and vector `b` to the vector `x` such that `A ⬝ x = b`. Otherwise, the outcome of `cramer_map` is well-defined but not necessarily useful. -/ def cramer_map (i : n) : α := (A.update_column i b).det lemma cramer_map_is_linear (i : n) : is_linear_map α (λ b, cramer_map A b i) := begin have : Π {f : n → n} {i : n} (x : n → α), (∏ i' : n, (update_column A i x)ᵀ (f i') i') = (∏ i' : n, if i' = i then x (f i') else A i' (f i')), { intros, congr, ext i', rw [transpose_val, update_column_val] }, split, { intros x y, repeat { rw [cramer_map, ←det_transpose, det] }, rw [←sum_add_distrib], congr, ext σ, rw [←mul_add ↑↑(sign σ)], congr, repeat { erw [this, finset.prod_ite] }, erw [finset.filter_eq', if_pos (mem_univ i), prod_singleton, prod_singleton, prod_singleton, ←add_mul], refl }, { intros c x, repeat { rw [cramer_map, ←det_transpose, det] }, rw [smul_eq_mul, mul_sum], congr, ext σ, rw [←mul_assoc, mul_comm c, mul_assoc], congr, repeat { erw [this, finset.prod_ite] }, erw [finset.filter_eq', if_pos (mem_univ i), prod_singleton, prod_singleton, mul_assoc], } end lemma cramer_is_linear : is_linear_map α (cramer_map A) := begin split; intros; ext i, { apply (cramer_map_is_linear A i).1 }, { apply (cramer_map_is_linear A i).2 } end /-- The linear map of vectors associated to Cramer's rule. To help the elaborator, we need to make the type `α` an explicit argument to `cramer`. Otherwise, the coercion `⇑(cramer A) : (n → α) → (n → α)` gives an error because it fails to infer the type (even though `α` can be inferred from `A : matrix n n α`). -/ def cramer (α : Type v) [comm_ring α] (A : matrix n n α) : (n → α) →ₗ[α] (n → α) := is_linear_map.mk' (cramer_map A) (cramer_is_linear A) lemma cramer_apply (i : n) : cramer α A b i = (A.update_column i b).det := rfl /-- Applying Cramer's rule to a column of the matrix gives a scaled basis vector. -/ lemma cramer_column_self (i : n) : cramer α A (A i) = (λ j, if i = j then A.det else 0) := begin ext j, rw cramer_apply, by_cases i = j, { -- i = j: this entry should be `A.det` rw [if_pos h, ←h], congr, ext i', by_cases h : i' = i, { rw [h, update_column_self] }, { rw [update_column_ne h]} }, { -- i ≠ j: this entry should be 0 rw [if_neg h], apply det_zero_of_column_eq h, rw [update_column_self, update_column_ne], apply h } end /-- Use linearity of `cramer` to take it out of a summation. -/ lemma sum_cramer {β} (s : finset β) (f : β → n → α) : ∑ x in s, cramer α A (f x) = cramer α A (∑ x in s, f x) := (linear_map.map_sum (cramer α A)).symm /-- Use linearity of `cramer` and vector evaluation to take `cramer A _ i` out of a summation. -/ lemma sum_cramer_apply {β} (s : finset β) (f : n → β → α) (i : n) : ∑ x in s, cramer α A (λ j, f j x) i = cramer α A (λ (j : n), ∑ x in s, f j x) i := calc ∑ x in s, cramer α A (λ j, f j x) i = (∑ x in s, cramer α A (λ j, f j x)) i : (pi.finset_sum_apply i s _).symm ... = cramer α A (λ (j : n), ∑ x in s, f j x) i : by { rw [sum_cramer, cramer_apply], congr, ext j, apply pi.finset_sum_apply } end cramer section adjugate /-! ### `adjugate` section Define the `adjugate` matrix and a few equations. These will hold for any matrix over a commutative ring, while the `inv` section is specifically for invertible matrices. -/ variable [comm_ring α] /-- The adjugate matrix is the transpose of the cofactor matrix. Typically, the cofactor matrix is defined by taking the determinant of minors, i.e. the matrix with a row and column removed. However, the proof of `mul_adjugate` becomes a lot easier if we define the minor as replacing a column with a basis vector, since it allows us to use facts about the `cramer` map. -/ def adjugate (A : matrix n n α) : matrix n n α := λ i, cramer α A (λ j, if i = j then 1 else 0) lemma adjugate_def (A : matrix n n α) : adjugate A = λ i, cramer α A (λ j, if i = j then 1 else 0) := rfl lemma adjugate_val (A : matrix n n α) (i j : n) : adjugate A i j = (A.update_column j (λ j, if i = j then 1 else 0)).det := rfl lemma adjugate_transpose (A : matrix n n α) : (adjugate A)ᵀ = adjugate (Aᵀ) := begin ext i j, rw [transpose_val, adjugate_val, adjugate_val, update_column_transpose, det_transpose], apply finset.sum_congr rfl, intros σ _, congr' 1, by_cases i = σ j, { -- Everything except `(i , j)` (= `(σ j , j)`) is given by A, and the rest is a single `1`. congr; ext j', have := (@equiv.injective _ _ σ j j' : σ j = σ j' → j = j'), rw [update_column_val, update_row_val], finish }, { -- Otherwise, we need to show that there is a `0` somewhere in the product. have : (∏ j' : n, update_row A j (λ (i' : n), ite (i = i') 1 0) (σ j') j') = 0, { apply prod_eq_zero (mem_univ j), rw [update_row_self], exact if_neg h }, rw this, apply prod_eq_zero (mem_univ (σ⁻¹ i)), erw [apply_symm_apply σ i, update_column_self], apply if_neg, intro h', exact h ((symm_apply_eq σ).mp h'.symm) } end lemma mul_adjugate_val (A : matrix n n α) (i j k) : A i k * adjugate A k j = cramer α A (λ j, if k = j then A i k else 0) j := begin erw [←smul_eq_mul, ←pi.smul_apply, ←linear_map.map_smul], congr, ext, rw [pi.smul_apply, smul_eq_mul, mul_boole], end lemma mul_adjugate (A : matrix n n α) : A ⬝ adjugate A = A.det • 1 := begin ext i j, rw [mul_val, smul_val, one_val, mul_boole], calc ∑ k : n, A i k * adjugate A k j = ∑ k : n, cramer α A (λ j, if k = j then A i k else 0) j : by {congr, ext k, apply mul_adjugate_val A i j k} ... = cramer α A (λ j, ∑ k : n, if k = j then A i k else 0) j : sum_cramer_apply A univ (λ (j k : n), if k = j then A i k else 0) j ... = cramer α A (A i) j : by { rw [cramer_apply], congr, ext, rw [sum_ite_eq' univ x (A i), if_pos (mem_univ _)] } ... = if i = j then det A else 0 : by rw [cramer_column_self] end lemma adjugate_mul (A : matrix n n α) : adjugate A ⬝ A = A.det • 1 := calc adjugate A ⬝ A = (Aᵀ ⬝ (adjugate Aᵀ))ᵀ : by rw [←adjugate_transpose, ←transpose_mul, transpose_transpose] ... = A.det • 1 : by rw [mul_adjugate (Aᵀ), det_transpose, transpose_smul, transpose_one] /-- `det_adjugate_of_cancel` is an auxiliary lemma for computing `(adjugate A).det`, used in `det_adjugate_eq_one` and `det_adjugate_of_is_unit`. The formula for the determinant of the adjugate of an `n` by `n` matrix `A` is in general `(adjugate A).det = A.det ^ (n - 1)`, but the proof differs in several cases. This lemma `det_adjugate_of_cancel` covers the case that `det A` cancels on the left of the equation `A.det * b = A.det ^ n`. -/ lemma det_adjugate_of_cancel {A : matrix n n α} (h : ∀ b, A.det * b = A.det ^ fintype.card n → b = A.det ^ (fintype.card n - 1)) : (adjugate A).det = A.det ^ (fintype.card n - 1) := h (adjugate A).det (calc A.det * (adjugate A).det = (A ⬝ adjugate A).det : (det_mul _ _).symm ... = A.det ^ fintype.card n : by simp [mul_adjugate]) lemma adjugate_eq_one_of_card_eq_one {A : matrix n n α} (h : fintype.card n = 1) : adjugate A = 1 := begin ext i j, have univ_eq_i := univ_eq_singleton_of_card_one i h, have univ_eq_j := univ_eq_singleton_of_card_one j h, have i_eq_j : i = j := singleton_inj.mp (by rw [←univ_eq_i, univ_eq_j]), 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 [adjugate_val, det, univ_eq_i, perm_eq, i_eq_j] end @[simp] lemma adjugate_zero (h : 1 < fintype.card n) : adjugate (0 : matrix n n α) = 0 := begin ext i j, obtain ⟨j', hj'⟩ : ∃ j', j' ≠ j := fintype.exists_ne_of_one_lt_card h j, apply det_eq_zero_of_column_eq_zero j', intro j'', simp [update_column_ne hj'] end lemma det_adjugate_eq_one {A : matrix n n α} (h : A.det = 1) : (adjugate A).det = 1 := calc (adjugate A).det = A.det ^ (fintype.card n - 1) : det_adjugate_of_cancel (λ b hb, by simpa [h] using hb) ... = 1 : by rw [h, one_pow] /-- `det_adjugate_of_is_unit` gives the formula for `(adjugate A).det` if `A.det` has an inverse. The formula for the determinant of the adjugate of an `n` by `n` matrix `A` is in general `(adjugate A).det = A.det ^ (n - 1)`, but the proof differs in several cases. This lemma `det_adjugate_of_is_unit` covers the case that `det A` has an inverse. -/ lemma det_adjugate_of_is_unit {A : matrix n n α} (h : is_unit A.det) : (adjugate A).det = A.det ^ (fintype.card n - 1) := begin rcases is_unit_iff_exists_inv'.mp h with ⟨a, ha⟩, by_cases card_lt_zero : fintype.card n ≤ 0, { have h : fintype.card n = 0 := by linarith, simp [det_eq_one_of_card_eq_zero h] }, have zero_lt_card : 0 < fintype.card n := by linarith, have n_nonempty : nonempty n := fintype.card_pos_iff.mp zero_lt_card, by_cases card_lt_one : fintype.card n ≤ 1, { have h : fintype.card n = 1 := by linarith, simp [h, adjugate_eq_one_of_card_eq_one h] }, have one_lt_card : 1 < fintype.card n := by linarith, have zero_lt_card_sub_one : 0 < fintype.card n - 1 := (nat.sub_lt_sub_right_iff (refl 1)).mpr one_lt_card, apply det_adjugate_of_cancel, intros b hb, calc b = a * (det A ^ (fintype.card n - 1 + 1)) : by rw [←one_mul b, ←ha, mul_assoc, hb, nat.sub_add_cancel zero_lt_card] ... = a * det A * det A ^ (fintype.card n - 1) : by ring_exp ... = det A ^ (fintype.card n - 1) : by rw [ha, one_mul] end end adjugate section inv /-! ### `inv` section Defines the matrix `nonsing_inv A` and proves it is the inverse matrix of a square matrix `A` as long as `det A` has a multiplicative inverse. -/ variables [comm_ring α] variables (A : matrix n n α) open_locale classical lemma is_unit_det_transpose (h : is_unit A.det) : is_unit Aᵀ.det := by { rw det_transpose, exact h, } /-- The inverse of a square matrix, when it is invertible (and zero otherwise).-/ noncomputable def nonsing_inv : matrix n n α := if h : is_unit A.det then (↑h.unit⁻¹ : α) • A.adjugate else 0 noncomputable instance : has_inv (matrix n n α) := ⟨matrix.nonsing_inv⟩ lemma nonsing_inv_apply (h : is_unit A.det) : A⁻¹ = (↑h.unit⁻¹ : α) • A.adjugate := by { change A.nonsing_inv = _, dunfold nonsing_inv, simp only [dif_pos, h], } lemma transpose_nonsing_inv (h : is_unit A.det) : (A⁻¹)ᵀ = (Aᵀ)⁻¹ := begin have h' := A.is_unit_det_transpose h, have dets_eq : (↑h.unit : α) = ↑h'.unit := by rw [h.unit_spec, h'.unit_spec, det_transpose], rw [A.nonsing_inv_apply h, Aᵀ.nonsing_inv_apply h', units.inv_unique dets_eq, A.adjugate_transpose.symm], refl, end /-- The `nonsing_inv` of `A` is a right inverse. -/ @[simp] lemma mul_nonsing_inv (h : is_unit A.det) : A ⬝ A⁻¹ = 1 := by rw [A.nonsing_inv_apply h, mul_smul, mul_adjugate, smul_smul, units.inv_mul' h.unit_spec, one_smul] /-- The `nonsing_inv` of `A` is a left inverse. -/ @[simp] lemma nonsing_inv_mul (h : is_unit A.det) : A⁻¹ ⬝ A = 1 := calc A⁻¹ ⬝ A = (Aᵀ ⬝ (Aᵀ)⁻¹)ᵀ : by { rw [transpose_mul, Aᵀ.transpose_nonsing_inv (A.is_unit_det_transpose h), transpose_transpose], } ... = 1ᵀ : by { rw Aᵀ.mul_nonsing_inv, exact A.is_unit_det_transpose h, } ... = 1 : transpose_one @[simp] lemma nonsing_inv_det (h : is_unit A.det) : A⁻¹.det * A.det = 1 := by rw [←det_mul, A.nonsing_inv_mul h, det_one] lemma is_unit_nonsing_inv_det (h : is_unit A.det) : is_unit A⁻¹.det := is_unit_of_mul_eq_one _ _ (A.nonsing_inv_det h) @[simp] lemma nonsing_inv_nonsing_inv (h : is_unit A.det) : (A⁻¹)⁻¹ = A := calc (A⁻¹)⁻¹ = 1 ⬝ (A⁻¹)⁻¹ : by rw matrix.one_mul ... = A ⬝ A⁻¹ ⬝ (A⁻¹)⁻¹ : by rw A.mul_nonsing_inv h ... = A : by { rw [matrix.mul_assoc, (A⁻¹).mul_nonsing_inv (A.is_unit_nonsing_inv_det h), matrix.mul_one], } /-- A matrix whose determinant is a unit is itself a unit. -/ noncomputable def nonsing_inv_unit (h : is_unit A.det) : units (matrix n n α) := { val := A, inv := A⁻¹, val_inv := by { rw matrix.mul_eq_mul, apply A.mul_nonsing_inv h, }, inv_val := by { rw matrix.mul_eq_mul, apply A.nonsing_inv_mul h, } } lemma is_unit_iff_is_unit_det : is_unit A ↔ is_unit A.det := begin split; intros h, { -- is_unit A → is_unit A.det suffices : ∃ (B : matrix n n α), A ⬝ B = 1, { rcases this with ⟨B, hB⟩, apply is_unit_of_mul_eq_one _ B.det, rw [←det_mul, hB, det_one], }, refine ⟨↑h.unit⁻¹, _⟩, conv_lhs { congr, rw ←h.unit_spec, }, exact h.unit.mul_inv, }, { -- is_unit A.det → is_unit A exact is_unit_unit (A.nonsing_inv_unit h), }, end end inv end matrix
6f697b8e0c2943181d1c0d5b24e14dbb34c989c2
5756a081670ba9c1d1d3fca7bd47cb4e31beae66
/Mathport/Syntax/Transform.lean
889d7aa9205f16db3b226f3e27943c85e5564989
[ "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
478
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ import Mathport.Syntax.Transform.Basic import Mathport.Syntax.Transform.Declaration import Mathport.Syntax.Transform.Tactic import Mathport.Syntax.Transform.Expr namespace Mathport namespace Transform open Lean Elab Term partial def transform : Syntax → M Syntax := applyTransformers mathport_transformer_list%
0fc1ebfd9a9024e8253336047e5cd69d77202f27
63abd62053d479eae5abf4951554e1064a4c45b4
/src/set_theory/cofinality.lean
b86a31ee45b82abae6207606c565a228b52ebbef
[ "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
22,018
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import set_theory.cardinal_ordinal /-! # Cofinality on ordinals, regular cardinals -/ noncomputable theory open function cardinal set open_locale classical universes u v w variables {α : Type*} {r : α → α → Prop} namespace order /-- Cofinality of a reflexive order `≼`. This is the smallest cardinality of a subset `S : set α` such that `∀ a, ∃ b ∈ S, a ≼ b`. -/ def cof (r : α → α → Prop) [is_refl α r] : cardinal := @cardinal.min {S : set α // ∀ a, ∃ b ∈ S, r a b} ⟨⟨set.univ, λ a, ⟨a, ⟨⟩, refl _⟩⟩⟩ (λ S, mk S) lemma cof_le (r : α → α → Prop) [is_refl α r] {S : set α} (h : ∀a, ∃(b ∈ S), r a b) : order.cof r ≤ mk S := le_trans (cardinal.min_le _ ⟨S, h⟩) (le_refl _) lemma le_cof {r : α → α → Prop} [is_refl α r] (c : cardinal) : c ≤ order.cof r ↔ ∀ {S : set α} (h : ∀a, ∃(b ∈ S), r a b) , c ≤ mk S := by { rw [order.cof, cardinal.le_min], exact ⟨λ H S h, H ⟨S, h⟩, λ H ⟨S, h⟩, H h ⟩ } end order theorem rel_iso.cof.aux {α : Type u} {β : Type v} {r s} [is_refl α r] [is_refl β s] (f : r ≃r s) : cardinal.lift.{u (max u v)} (order.cof r) ≤ cardinal.lift.{v (max u v)} (order.cof s) := begin rw [order.cof, order.cof, lift_min, lift_min, cardinal.le_min], intro S, cases S with S H, simp [(∘)], refine le_trans (min_le _ _) _, { exact ⟨f ⁻¹' S, λ a, let ⟨b, bS, h⟩ := H (f a) in ⟨f.symm b, by simp [bS, f.map_rel_iff', h, -coe_fn_coe_base, -coe_fn_coe_trans, principal_seg.coe_coe_fn', initial_seg.coe_coe_fn]⟩⟩ }, { exact lift_mk_le.{u v (max u v)}.2 ⟨⟨λ ⟨x, h⟩, ⟨f x, h⟩, λ ⟨x, h₁⟩ ⟨y, h₂⟩ h₃, by congr; injection h₃ with h'; exact f.to_equiv.injective h'⟩⟩ } end theorem rel_iso.cof {α : Type u} {β : Type v} {r s} [is_refl α r] [is_refl β s] (f : r ≃r s) : cardinal.lift.{u (max u v)} (order.cof r) = cardinal.lift.{v (max u v)} (order.cof s) := le_antisymm (rel_iso.cof.aux f) (rel_iso.cof.aux f.symm) def strict_order.cof (r : α → α → Prop) [h : is_irrefl α r] : cardinal := @order.cof α (λ x y, ¬ r y x) ⟨h.1⟩ namespace ordinal /-- Cofinality of an ordinal. This is the smallest cardinal of a subset `S` of the ordinal which is unbounded, in the sense `∀ a, ∃ b ∈ S, ¬(b > a)`. It is defined for all ordinals, but `cof 0 = 0` and `cof (succ o) = 1`, so it is only really interesting on limit ordinals (when it is an infinite cardinal). -/ def cof (o : ordinal.{u}) : cardinal.{u} := quot.lift_on o (λ ⟨α, r, _⟩, by exactI strict_order.cof r) begin rintros ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨⟨f, hf⟩⟩, rw ← cardinal.lift_inj, apply rel_iso.cof ⟨f, _⟩, simp [hf] end lemma cof_type (r : α → α → Prop) [is_well_order α r] : (type r).cof = strict_order.cof r := rfl theorem le_cof_type [is_well_order α r] {c} : c ≤ cof (type r) ↔ ∀ S : set α, (∀ a, ∃ b ∈ S, ¬ r b a) → c ≤ mk S := by dsimp [cof, strict_order.cof, order.cof, type, quotient.mk, quot.lift_on]; rw [cardinal.le_min, subtype.forall]; refl theorem cof_type_le [is_well_order α r] (S : set α) (h : ∀ a, ∃ b ∈ S, ¬ r b a) : cof (type r) ≤ mk S := le_cof_type.1 (le_refl _) S h theorem lt_cof_type [is_well_order α r] (S : set α) (hl : mk S < cof (type r)) : ∃ a, ∀ b ∈ S, r b a := not_forall_not.1 $ λ h, not_le_of_lt hl $ cof_type_le S (λ a, not_ball.1 (h a)) theorem cof_eq (r : α → α → Prop) [is_well_order α r] : ∃ S : set α, (∀ a, ∃ b ∈ S, ¬ r b a) ∧ mk S = cof (type r) := begin have : ∃ i, cof (type r) = _, { dsimp [cof, order.cof, type, quotient.mk, quot.lift_on], apply cardinal.min_eq }, exact let ⟨⟨S, hl⟩, e⟩ := this in ⟨S, hl, e.symm⟩, end theorem ord_cof_eq (r : α → α → Prop) [is_well_order α r] : ∃ S : set α, (∀ a, ∃ b ∈ S, ¬ r b a) ∧ type (subrel r S) = (cof (type r)).ord := let ⟨S, hS, e⟩ := cof_eq r, ⟨s, _, e'⟩ := cardinal.ord_eq S, T : set α := {a | ∃ aS : a ∈ S, ∀ b : S, s b ⟨_, aS⟩ → r b a} in begin resetI, suffices, { refine ⟨T, this, le_antisymm _ (cardinal.ord_le.2 $ cof_type_le T this)⟩, rw [← e, e'], refine type_le'.2 ⟨rel_embedding.of_monotone (λ a, ⟨a, let ⟨aS, _⟩ := a.2 in aS⟩) (λ a b h, _)⟩, rcases a with ⟨a, aS, ha⟩, rcases b with ⟨b, bS, hb⟩, change s ⟨a, _⟩ ⟨b, _⟩, refine ((trichotomous_of s _ _).resolve_left (λ hn, _)).resolve_left _, { exact asymm h (ha _ hn) }, { intro e, injection e with e, subst b, exact irrefl _ h } }, { intro a, have : {b : S | ¬ r b a}.nonempty := let ⟨b, bS, ba⟩ := hS a in ⟨⟨b, bS⟩, ba⟩, let b := (is_well_order.wf).min _ this, have ba : ¬r b a := (is_well_order.wf).min_mem _ this, refine ⟨b, ⟨b.2, λ c, not_imp_not.1 $ λ h, _⟩, ba⟩, rw [show ∀b:S, (⟨b, b.2⟩:S) = b, by intro b; cases b; refl], exact (is_well_order.wf).not_lt_min _ this (is_order_connected.neg_trans h ba) } end theorem lift_cof (o) : (cof o).lift = cof o.lift := induction_on o $ begin introsI α r _, cases lift_type r with _ e, rw e, apply le_antisymm, { unfreezingI { refine le_cof_type.2 (λ S H, _) }, have : (mk (ulift.up ⁻¹' S)).lift ≤ mk S := ⟨⟨λ ⟨⟨x, h⟩⟩, ⟨⟨x⟩, h⟩, λ ⟨⟨x, h₁⟩⟩ ⟨⟨y, h₂⟩⟩ e, by simp at e; congr; injection e⟩⟩, refine le_trans (cardinal.lift_le.2 $ cof_type_le _ _) this, exact λ a, let ⟨⟨b⟩, bs, br⟩ := H ⟨a⟩ in ⟨b, bs, br⟩ }, { rcases cof_eq r with ⟨S, H, e'⟩, have : mk (ulift.down ⁻¹' S) ≤ (mk S).lift := ⟨⟨λ ⟨⟨x⟩, h⟩, ⟨⟨x, h⟩⟩, λ ⟨⟨x⟩, h₁⟩ ⟨⟨y⟩, h₂⟩ e, by simp at e; congr; injections⟩⟩, rw e' at this, unfreezingI { refine le_trans (cof_type_le _ _) this }, exact λ ⟨a⟩, let ⟨b, bs, br⟩ := H a in ⟨⟨b⟩, bs, br⟩ } end theorem cof_le_card (o) : cof o ≤ card o := induction_on o $ λ α r _, begin resetI, have : mk (@set.univ α) = card (type r) := quotient.sound ⟨equiv.set.univ _⟩, rw ← this, exact cof_type_le set.univ (λ a, ⟨a, ⟨⟩, irrefl a⟩) end theorem cof_ord_le (c : cardinal) : cof c.ord ≤ c := by simpa using cof_le_card c.ord @[simp] theorem cof_zero : cof 0 = 0 := le_antisymm (by simpa using cof_le_card 0) (cardinal.zero_le _) @[simp] theorem cof_eq_zero {o} : cof o = 0 ↔ o = 0 := ⟨induction_on o $ λ α r _ z, by exactI let ⟨S, hl, e⟩ := cof_eq r in type_eq_zero_iff_empty.2 $ λ ⟨a⟩, let ⟨b, h, _⟩ := hl a in ne_zero_iff_nonempty.2 (by exact ⟨⟨_, h⟩⟩) (e.trans z), λ e, by simp [e]⟩ @[simp] theorem cof_succ (o) : cof (succ o) = 1 := begin apply le_antisymm, { refine induction_on o (λ α r _, _), change cof (type _) ≤ _, rw [← (_ : mk _ = 1)], apply cof_type_le, { refine λ a, ⟨sum.inr punit.star, set.mem_singleton _, _⟩, rcases a with a|⟨⟨⟨⟩⟩⟩; simp [empty_relation] }, { rw [cardinal.fintype_card, set.card_singleton], simp } }, { rw [← cardinal.succ_zero, cardinal.succ_le], simpa [lt_iff_le_and_ne, cardinal.zero_le] using λ h, succ_ne_zero o (cof_eq_zero.1 (eq.symm h)) } end @[simp] theorem cof_eq_one_iff_is_succ {o} : cof.{u} o = 1 ↔ ∃ a, o = succ a := ⟨induction_on o $ λ α r _ z, begin resetI, rcases cof_eq r with ⟨S, hl, e⟩, rw z at e, cases ne_zero_iff_nonempty.1 (by rw e; exact one_ne_zero) with a, refine ⟨typein r a, eq.symm $ quotient.sound ⟨rel_iso.of_surjective (rel_embedding.of_monotone _ (λ x y, _)) (λ x, _)⟩⟩, { apply sum.rec; [exact subtype.val, exact λ _, a] }, { rcases x with x|⟨⟨⟨⟩⟩⟩; rcases y with y|⟨⟨⟨⟩⟩⟩; simp [subrel, order.preimage, empty_relation], exact x.2 }, { suffices : r x a ∨ ∃ (b : punit), ↑a = x, {simpa}, rcases trichotomous_of r x a with h|h|h, { exact or.inl h }, { exact or.inr ⟨punit.star, h.symm⟩ }, { rcases hl x with ⟨a', aS, hn⟩, rw (_ : ↑a = a') at h, {exact absurd h hn}, refine congr_arg subtype.val (_ : a = ⟨a', aS⟩), haveI := le_one_iff_subsingleton.1 (le_of_eq e), apply subsingleton.elim } } end, λ ⟨a, e⟩, by simp [e]⟩ @[simp] theorem cof_add (a b : ordinal) : b ≠ 0 → cof (a + b) = cof b := induction_on a $ λ α r _, induction_on b $ λ β s _ b0, begin resetI, change cof (type _) = _, refine eq_of_forall_le_iff (λ c, _), rw [le_cof_type, le_cof_type], split; intros H S hS, { refine le_trans (H {a | sum.rec_on a (∅:set α) S} (λ a, _)) ⟨⟨_, _⟩⟩, { cases a with a b, { cases type_ne_zero_iff_nonempty.1 b0 with b, rcases hS b with ⟨b', bs, _⟩, exact ⟨sum.inr b', bs, by simp⟩ }, { rcases hS b with ⟨b', bs, h⟩, exact ⟨sum.inr b', bs, by simp [h]⟩ } }, { exact λ a, match a with ⟨sum.inr b, h⟩ := ⟨b, h⟩ end }, { exact λ a b, match a, b with ⟨sum.inr a, h₁⟩, ⟨sum.inr b, h₂⟩, h := by congr; injection h end } }, { refine le_trans (H (sum.inr ⁻¹' S) (λ a, _)) ⟨⟨_, _⟩⟩, { rcases hS (sum.inr a) with ⟨a'|b', bs, h⟩; simp at h, { cases h }, { exact ⟨b', bs, h⟩ } }, { exact λ ⟨a, h⟩, ⟨_, h⟩ }, { exact λ ⟨a, h₁⟩ ⟨b, h₂⟩ h, by injection h with h; congr; injection h } } end @[simp] theorem cof_cof (o : ordinal) : cof (cof o).ord= cof o := le_antisymm (le_trans (cof_le_card _) (by simp)) $ induction_on o $ λ α r _, by exactI let ⟨S, hS, e₁⟩ := ord_cof_eq r, ⟨T, hT, e₂⟩ := cof_eq (subrel r S) in begin rw e₁ at e₂, rw ← e₂, refine le_trans (cof_type_le {a | ∃ h, (subtype.mk a h : S) ∈ T} (λ a, _)) ⟨⟨_, _⟩⟩, { rcases hS a with ⟨b, bS, br⟩, rcases hT ⟨b, bS⟩ with ⟨⟨c, cS⟩, cT, cs⟩, exact ⟨c, ⟨cS, cT⟩, is_order_connected.neg_trans cs br⟩ }, { exact λ ⟨a, h⟩, ⟨⟨a, h.fst⟩, h.snd⟩ }, { exact λ ⟨a, ha⟩ ⟨b, hb⟩ h, by injection h with h; congr; injection h }, end theorem omega_le_cof {o} : cardinal.omega ≤ cof o ↔ is_limit o := begin rcases zero_or_succ_or_limit o with rfl|⟨o,rfl⟩|l, { simp [not_zero_is_limit, cardinal.omega_ne_zero] }, { simp [not_succ_is_limit, cardinal.one_lt_omega] }, { simp [l], refine le_of_not_lt (λ h, _), cases cardinal.lt_omega.1 h with n e, have := cof_cof o, rw [e, ord_nat] at this, cases n, { simp at e, simpa [e, not_zero_is_limit] using l }, { rw [← nat_cast_succ, cof_succ] at this, rw [← this, cof_eq_one_iff_is_succ] at e, rcases e with ⟨a, rfl⟩, exact not_succ_is_limit _ l } } end @[simp] theorem cof_omega : cof omega = cardinal.omega := le_antisymm (by rw ← card_omega; apply cof_le_card) (omega_le_cof.2 omega_is_limit) theorem cof_eq' (r : α → α → Prop) [is_well_order α r] (h : is_limit (type r)) : ∃ S : set α, (∀ a, ∃ b ∈ S, r a b) ∧ mk S = cof (type r) := let ⟨S, H, e⟩ := cof_eq r in ⟨S, λ a, let a' := enum r _ (h.2 _ (typein_lt_type r a)) in let ⟨b, h, ab⟩ := H a' in ⟨b, h, (is_order_connected.conn a b a' $ (typein_lt_typein r).1 (by rw typein_enum; apply ordinal.lt_succ_self)).resolve_right ab⟩, e⟩ theorem cof_sup_le_lift {ι} (f : ι → ordinal) (H : ∀ i, f i < sup f) : cof (sup f) ≤ (mk ι).lift := begin generalize e : sup f = o, refine ordinal.induction_on o _ e, introsI α r _ e', rw e' at H, refine le_trans (cof_type_le (set.range (λ i, enum r _ (H i))) _) ⟨embedding.of_surjective _ _⟩, { intro a, by_contra h, apply not_le_of_lt (typein_lt_type r a), rw [← e', sup_le], intro i, have h : ∀ (x : ι), r (enum r (f x) _) a, { simpa using h }, simpa only [typein_enum] using le_of_lt ((typein_lt_typein r).2 (h i)) }, { exact λ i, ⟨_, set.mem_range_self i.1⟩ }, { intro a, rcases a with ⟨_, i, rfl⟩, exact ⟨⟨i⟩, by simp⟩ } end theorem cof_sup_le {ι} (f : ι → ordinal) (H : ∀ i, f i < sup.{u u} f) : cof (sup.{u u} f) ≤ mk ι := by simpa using cof_sup_le_lift.{u u} f H theorem cof_bsup_le_lift {o : ordinal} : ∀ (f : Π a < o, ordinal), (∀ i h, f i h < bsup o f) → cof (bsup o f) ≤ o.card.lift := induction_on o $ λ α r _ f H, by rw bsup_type; refine cof_sup_le_lift _ _; rw ← bsup_type; intro a; apply H theorem cof_bsup_le {o : ordinal} : ∀ (f : Π a < o, ordinal), (∀ i h, f i h < bsup.{u u} o f) → cof (bsup.{u u} o f) ≤ o.card := induction_on o $ λ α r _ f H, by simpa using cof_bsup_le_lift.{u u} f H @[simp] theorem cof_univ : cof univ.{u v} = cardinal.univ := le_antisymm (cof_le_card _) begin refine le_of_forall_lt (λ c h, _), rcases lt_univ'.1 h with ⟨c, rfl⟩, rcases @cof_eq ordinal.{u} (<) _ with ⟨S, H, Se⟩, rw [univ, ← lift_cof, ← cardinal.lift_lift, cardinal.lift_lt, ← Se], refine lt_of_not_ge (λ h, _), cases cardinal.lift_down h with a e, refine quotient.induction_on a (λ α e, _) e, cases quotient.exact e with f, have f := equiv.ulift.symm.trans f, let g := λ a, (f a).1, let o := succ (sup.{u u} g), rcases H o with ⟨b, h, l⟩, refine l (lt_succ.2 _), rw ← show g (f.symm ⟨b, h⟩) = b, by dsimp [g]; simp, apply le_sup end theorem sup_lt_ord {ι} (f : ι → ordinal) {c : ordinal} (H1 : cardinal.mk ι < c.cof) (H2 : ∀ i, f i < c) : sup.{u u} f < c := begin apply lt_of_le_of_ne, { rw [sup_le], exact λ i, le_of_lt (H2 i) }, rintro h, apply not_le_of_lt H1, simpa [sup_ord, H2, h] using cof_sup_le.{u} f end theorem sup_lt {ι} (f : ι → cardinal) {c : cardinal} (H1 : cardinal.mk ι < c.ord.cof) (H2 : ∀ i, f i < c) : cardinal.sup.{u u} f < c := by { rw [←ord_lt_ord, ←sup_ord], apply sup_lt_ord _ H1, intro i, rw ord_lt_ord, apply H2 } /-- If the union of s is unbounded and s is smaller than the cofinality, then s has an unbounded member -/ theorem unbounded_of_unbounded_sUnion (r : α → α → Prop) [wo : is_well_order α r] {s : set (set α)} (h₁ : unbounded r $ ⋃₀ s) (h₂ : mk s < strict_order.cof r) : ∃(x ∈ s), unbounded r x := begin by_contra h, simp only [not_exists, exists_prop, not_and, not_unbounded_iff] at h, apply not_le_of_lt h₂, let f : s → α := λ x : s, wo.wf.sup x (h x.1 x.2), let t : set α := range f, have : mk t ≤ mk s, exact mk_range_le, refine le_trans _ this, have : unbounded r t, { intro x, rcases h₁ x with ⟨y, ⟨c, hc, hy⟩, hxy⟩, refine ⟨f ⟨c, hc⟩, mem_range_self _, _⟩, intro hxz, apply hxy, refine trans (wo.wf.lt_sup _ hy) hxz }, exact cardinal.min_le _ (subtype.mk t this) end /-- If the union of s is unbounded and s is smaller than the cofinality, then s has an unbounded member -/ theorem unbounded_of_unbounded_Union {α β : Type u} (r : α → α → Prop) [wo : is_well_order α r] (s : β → set α) (h₁ : unbounded r $ ⋃x, s x) (h₂ : mk β < strict_order.cof r) : ∃x : β, unbounded r (s x) := begin rw [← sUnion_range] at h₁, have : mk ↥(range (λ (i : β), s i)) < strict_order.cof r := lt_of_le_of_lt mk_range_le h₂, rcases unbounded_of_unbounded_sUnion r h₁ this with ⟨_, ⟨x, rfl⟩, u⟩, exact ⟨x, u⟩ end /-- The infinite pigeonhole principle-/ theorem infinite_pigeonhole {β α : Type u} (f : β → α) (h₁ : cardinal.omega ≤ mk β) (h₂ : mk α < (mk β).ord.cof) : ∃a : α, mk (f ⁻¹' {a}) = mk β := begin have : ¬∀a, mk (f ⁻¹' {a}) < mk β, { intro h, apply not_lt_of_ge (ge_of_eq $ mk_univ), rw [←@preimage_univ _ _ f, ←Union_of_singleton, preimage_Union], apply lt_of_le_of_lt mk_Union_le_sum_mk, apply lt_of_le_of_lt (sum_le_sup _), apply mul_lt_of_lt h₁ (lt_of_lt_of_le h₂ $ cof_ord_le _), exact sup_lt _ h₂ h }, rw [not_forall] at this, cases this with x h, use x, apply le_antisymm _ (le_of_not_gt h), rw [le_mk_iff_exists_set], exact ⟨_, rfl⟩ end /-- pigeonhole principle for a cardinality below the cardinality of the domain -/ theorem infinite_pigeonhole_card {β α : Type u} (f : β → α) (θ : cardinal) (hθ : θ ≤ mk β) (h₁ : cardinal.omega ≤ θ) (h₂ : mk α < θ.ord.cof) : ∃a : α, θ ≤ mk (f ⁻¹' {a}) := begin rcases le_mk_iff_exists_set.1 hθ with ⟨s, rfl⟩, cases infinite_pigeonhole (f ∘ subtype.val : s → α) h₁ h₂ with a ha, use a, rw [←ha, @preimage_comp _ _ _ subtype.val f], apply mk_preimage_of_injective _ _ subtype.val_injective end theorem infinite_pigeonhole_set {β α : Type u} {s : set β} (f : s → α) (θ : cardinal) (hθ : θ ≤ mk s) (h₁ : cardinal.omega ≤ θ) (h₂ : mk α < θ.ord.cof) : ∃(a : α) (t : set β) (h : t ⊆ s), θ ≤ mk t ∧ ∀{{x}} (hx : x ∈ t), f ⟨x, h hx⟩ = a := begin cases infinite_pigeonhole_card f θ hθ h₁ h₂ with a ha, refine ⟨a, {x | ∃(h : x ∈ s), f ⟨x, h⟩ = a}, _, _, _⟩, { rintro x ⟨hx, hx'⟩, exact hx }, { refine le_trans ha _, apply ge_of_eq, apply quotient.sound, constructor, refine equiv.trans _ (equiv.subtype_subtype_equiv_subtype_exists _ _).symm, simp only [set_coe_eq_subtype, mem_singleton_iff, mem_preimage, mem_set_of_eq] }, rintro x ⟨hx, hx'⟩, exact hx' end end ordinal namespace cardinal open ordinal local infixr ^ := @pow cardinal.{u} cardinal cardinal.has_pow /-- A cardinal is a limit if it is not zero or a successor cardinal. Note that `ω` is a limit cardinal by this definition. -/ def is_limit (c : cardinal) : Prop := c ≠ 0 ∧ ∀ x < c, succ x < c /-- A cardinal is a strong limit if it is not zero and it is closed under powersets. Note that `ω` is a strong limit by this definition. -/ def is_strong_limit (c : cardinal) : Prop := c ≠ 0 ∧ ∀ x < c, 2 ^ x < c theorem is_strong_limit.is_limit {c} (H : is_strong_limit c) : is_limit c := ⟨H.1, λ x h, lt_of_le_of_lt (succ_le.2 $ cantor _) (H.2 _ h)⟩ /-- A cardinal is regular if it is infinite and it equals its own cofinality. -/ def is_regular (c : cardinal) : Prop := omega ≤ c ∧ c.ord.cof = c theorem cof_is_regular {o : ordinal} (h : o.is_limit) : is_regular o.cof := ⟨omega_le_cof.2 h, cof_cof _⟩ theorem omega_is_regular : is_regular omega := ⟨le_refl _, by simp⟩ theorem succ_is_regular {c : cardinal.{u}} (h : omega ≤ c) : is_regular (succ c) := ⟨le_trans h (le_of_lt $ lt_succ_self _), begin refine le_antisymm (cof_ord_le _) (succ_le.2 _), cases quotient.exists_rep (succ c) with α αe, simp at αe, rcases ord_eq α with ⟨r, wo, re⟩, resetI, have := ord_is_limit (le_trans h $ le_of_lt $ lt_succ_self _), rw [← αe, re] at this ⊢, rcases cof_eq' r this with ⟨S, H, Se⟩, rw [← Se], apply lt_imp_lt_of_le_imp_le (mul_le_mul_right c), rw [mul_eq_self h, ← succ_le, ← αe, ← sum_const], refine le_trans _ (sum_le_sum (λ x:S, card (typein r x)) _ _), { simp [typein, sum_mk (λ x:S, {a//r a x})], refine ⟨embedding.of_surjective _ _⟩, { exact λ x, x.2.1 }, { exact λ a, let ⟨b, h, ab⟩ := H a in ⟨⟨⟨_, h⟩, _, ab⟩, rfl⟩ } }, { intro i, rw [← lt_succ, ← lt_ord, ← αe, re], apply typein_lt_type } end⟩ theorem sup_lt_ord_of_is_regular {ι} (f : ι → ordinal) {c} (hc : is_regular c) (H1 : cardinal.mk ι < c) (H2 : ∀ i, f i < c.ord) : ordinal.sup.{u u} f < c.ord := by { apply sup_lt_ord _ _ H2, rw [hc.2], exact H1 } theorem sup_lt_of_is_regular {ι} (f : ι → cardinal) {c} (hc : is_regular c) (H1 : cardinal.mk ι < c) (H2 : ∀ i, f i < c) : sup.{u u} f < c := by { apply sup_lt _ _ H2, rwa [hc.2] } theorem sum_lt_of_is_regular {ι} (f : ι → cardinal) {c} (hc : is_regular c) (H1 : cardinal.mk ι < c) (H2 : ∀ i, f i < c) : sum.{u u} f < c := lt_of_le_of_lt (sum_le_sup _) $ mul_lt_of_lt hc.1 H1 $ sup_lt_of_is_regular f hc H1 H2 /-- A cardinal is inaccessible if it is an uncountable regular strong limit cardinal. -/ def is_inaccessible (c : cardinal) := omega < c ∧ is_regular c ∧ is_strong_limit c theorem is_inaccessible.mk {c} (h₁ : omega < c) (h₂ : c ≤ c.ord.cof) (h₃ : ∀ x < c, 2 ^ x < c) : is_inaccessible c := ⟨h₁, ⟨le_of_lt h₁, le_antisymm (cof_ord_le _) h₂⟩, ne_of_gt (lt_trans omega_pos h₁), h₃⟩ /- Lean's foundations prove the existence of ω many inaccessible cardinals -/ theorem univ_inaccessible : is_inaccessible (univ.{u v}) := is_inaccessible.mk (by simpa using lift_lt_univ' omega) (by simp) (λ c h, begin rcases lt_univ'.1 h with ⟨c, rfl⟩, rw ← lift_two_power.{u (max (u+1) v)}, apply lift_lt_univ' end) theorem lt_power_cof {c : cardinal.{u}} : omega ≤ c → c < c ^ cof c.ord := quotient.induction_on c $ λ α h, begin rcases ord_eq α with ⟨r, wo, re⟩, resetI, have := ord_is_limit h, rw [mk_def, re] at this ⊢, rcases cof_eq' r this with ⟨S, H, Se⟩, have := sum_lt_prod (λ a:S, mk {x // r x a}) (λ _, mk α) (λ i, _), { simp [Se.symm] at this ⊢, refine lt_of_le_of_lt _ this, refine ⟨embedding.of_surjective _ _⟩, { exact λ x, x.2.1 }, { exact λ a, let ⟨b, h, ab⟩ := H a in ⟨⟨⟨_, h⟩, _, ab⟩, rfl⟩ } }, { have := typein_lt_type r i, rwa [← re, lt_ord] at this } end theorem lt_cof_power {a b : cardinal} (ha : omega ≤ a) (b1 : 1 < b) : a < cof (b ^ a).ord := begin have b0 : b ≠ 0 := ne_of_gt (lt_trans zero_lt_one b1), apply lt_imp_lt_of_le_imp_le (power_le_power_left $ power_ne_zero a b0), rw [power_mul, mul_eq_self ha], exact lt_power_cof (le_trans ha $ le_of_lt $ cantor' _ b1), end end cardinal
f1ca999789e00566d3e3433a96740a88b853d2bd
1fbca480c1574e809ae95a3eda58188ff42a5e41
/test/tactic/perm.lean
8f380d1a78b5e02266b2a7d12395c78e93e63ade
[]
no_license
unitb/lean-lib
560eea0acf02b1fd4bcaac9986d3d7f1a4290e7e
439b80e606b4ebe4909a08b1d77f4f5c0ee3dee9
refs/heads/master
1,610,706,025,400
1,570,144,245,000
1,570,144,245,000
99,579,229
5
0
null
null
null
null
UTF-8
Lean
false
false
503
lean
import util.meta.tactic example : list.perm [1,2,3] [3,2,1] := by { prove_perm } example : list.perm [1,2,3,4] [3,2,4,1] := by { prove_perm } example : list.perm [1,2,3,4] [3,1,4] := by { success_if_fail { prove_perm }, admit } example : list.perm [1,2,3,4] [3,2,4,5] := by { success_if_fail { prove_perm }, admit } example : list.perm [1,2,3,4] [1,3,2,4,5] := by { success_if_fail { prove_perm }, admit } example : list.perm [1,2,3,4] [1,3,2,4,5] := by { success_if_fail { prove_perm }, admit }
db8bd7c94b78d0984e88f175642cb8e469dd41f4
82e44445c70db0f03e30d7be725775f122d72f3e
/src/algebra/pointwise.lean
761ce8dc47a033d6ad9e54e238de7d30bcb42ee3
[ "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
27,423
lean
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Floris van Doorn -/ import algebra.module.basic import data.set.finite import group_theory.submonoid.basic /-! # Pointwise addition, multiplication, and scalar multiplication of sets. This file defines pointwise algebraic operations on sets. * For a type `α` with multiplication, multiplication is defined on `set α` by taking `s * t` to be the set of all `x * y` where `x ∈ s` and `y ∈ t`. Similarly for addition. * For `α` a semigroup, `set α` is a semigroup. * If `α` is a (commutative) monoid, we define an alias `set_semiring α` for `set α`, which then becomes a (commutative) semiring with union as addition and pointwise multiplication as multiplication. * For a type `β` with scalar multiplication by another type `α`, this file defines a scalar multiplication of `set β` by `set α` and a separate scalar multiplication of `set β` by `α`. * We also define pointwise multiplication on `finset`. Appropriate definitions and results are also transported to the additive theory via `to_additive`. ## Implementation notes * The following expressions are considered in simp-normal form in a group: `(λ h, h * g) ⁻¹' s`, `(λ h, g * h) ⁻¹' s`, `(λ h, h * g⁻¹) ⁻¹' s`, `(λ h, g⁻¹ * h) ⁻¹' s`, `s * t`, `s⁻¹`, `(1 : set _)` (and similarly for additive variants). Expressions equal to one of these will be simplified. ## Tags set multiplication, set addition, pointwise addition, pointwise multiplication -/ namespace set open function variables {α : Type*} {β : Type*} {s s₁ s₂ t t₁ t₂ u : set α} {a b : α} {x y : β} /-! ### Properties about 1 -/ @[to_additive] instance [has_one α] : has_one (set α) := ⟨{1}⟩ @[to_additive] lemma singleton_one [has_one α] : ({1} : set α) = 1 := rfl @[simp, to_additive] lemma mem_one [has_one α] : a ∈ (1 : set α) ↔ a = 1 := iff.rfl @[to_additive] lemma one_mem_one [has_one α] : (1 : α) ∈ (1 : set α) := eq.refl _ @[simp, to_additive] theorem one_subset [has_one α] : 1 ⊆ s ↔ (1 : α) ∈ s := singleton_subset_iff @[to_additive] theorem one_nonempty [has_one α] : (1 : set α).nonempty := ⟨1, rfl⟩ @[simp, to_additive] theorem image_one [has_one α] {f : α → β} : f '' 1 = {f 1} := image_singleton /-! ### Properties about multiplication -/ @[to_additive] instance [has_mul α] : has_mul (set α) := ⟨image2 has_mul.mul⟩ @[simp, to_additive] lemma image2_mul [has_mul α] : image2 has_mul.mul s t = s * t := rfl @[to_additive] lemma mem_mul [has_mul α] : a ∈ s * t ↔ ∃ x y, x ∈ s ∧ y ∈ t ∧ x * y = a := iff.rfl @[to_additive] lemma mul_mem_mul [has_mul α] (ha : a ∈ s) (hb : b ∈ t) : a * b ∈ s * t := mem_image2_of_mem ha hb @[to_additive add_image_prod] lemma image_mul_prod [has_mul α] : (λ x : α × α, x.fst * x.snd) '' s.prod t = s * t := image_prod _ @[simp, to_additive] lemma image_mul_left [group α] : (λ b, a * b) '' t = (λ b, a⁻¹ * b) ⁻¹' t := by { rw image_eq_preimage_of_inverse; intro c; simp } @[simp, to_additive] lemma image_mul_right [group α] : (λ a, a * b) '' t = (λ a, a * b⁻¹) ⁻¹' t := by { rw image_eq_preimage_of_inverse; intro c; simp } @[to_additive] lemma image_mul_left' [group α] : (λ b, a⁻¹ * b) '' t = (λ b, a * b) ⁻¹' t := by simp @[to_additive] lemma image_mul_right' [group α] : (λ a, a * b⁻¹) '' t = (λ a, a * b) ⁻¹' t := by simp @[simp, to_additive] lemma preimage_mul_left_singleton [group α] : ((*) a) ⁻¹' {b} = {a⁻¹ * b} := by rw [← image_mul_left', image_singleton] @[simp, to_additive] lemma preimage_mul_right_singleton [group α] : (* a) ⁻¹' {b} = {b * a⁻¹} := by rw [← image_mul_right', image_singleton] @[simp, to_additive] lemma preimage_mul_left_one [group α] : (λ b, a * b) ⁻¹' 1 = {a⁻¹} := by rw [← image_mul_left', image_one, mul_one] @[simp, to_additive] lemma preimage_mul_right_one [group α] : (λ a, a * b) ⁻¹' 1 = {b⁻¹} := by rw [← image_mul_right', image_one, one_mul] @[to_additive] lemma preimage_mul_left_one' [group α] : (λ b, a⁻¹ * b) ⁻¹' 1 = {a} := by simp @[to_additive] lemma preimage_mul_right_one' [group α] : (λ a, a * b⁻¹) ⁻¹' 1 = {b} := by simp @[simp, to_additive] lemma mul_singleton [has_mul α] : s * {b} = (λ a, a * b) '' s := image2_singleton_right @[simp, to_additive] lemma singleton_mul [has_mul α] : {a} * t = (λ b, a * b) '' t := image2_singleton_left @[simp, to_additive] lemma singleton_mul_singleton [has_mul α] : ({a} : set α) * {b} = {a * b} := image2_singleton @[to_additive set.add_zero_class] instance [mul_one_class α] : mul_one_class (set α) := { mul_one := λ s, by { simp only [← singleton_one, mul_singleton, mul_one, image_id'] }, one_mul := λ s, by { simp only [← singleton_one, singleton_mul, one_mul, image_id'] }, ..set.has_one, ..set.has_mul } @[to_additive set.add_semigroup] instance [semigroup α] : semigroup (set α) := { mul_assoc := λ _ _ _, image2_assoc mul_assoc, ..set.has_mul } @[to_additive set.add_monoid] instance [monoid α] : monoid (set α) := { ..set.semigroup, ..set.mul_one_class } lemma pow_mem_pow [monoid α] (ha : a ∈ s) (n : ℕ) : a ^ n ∈ s ^ n := begin induction n with n ih, { rw pow_zero, exact set.mem_singleton 1 }, { rw pow_succ, exact set.mul_mem_mul ha ih }, end @[to_additive] protected lemma mul_comm [comm_semigroup α] : s * t = t * s := by simp only [← image2_mul, image2_swap _ s, mul_comm] @[to_additive set.add_comm_monoid] instance [comm_monoid α] : comm_monoid (set α) := { mul_comm := λ _ _, set.mul_comm, ..set.monoid } /-- Under `[has_mul M]`, the `singleton` map from `M` to `set M` as a `mul_hom`, that is, a map which preserves multiplication. -/ @[to_additive "Under `[has_add A]`, the `singleton` map from `A` to `set A` as an `add_hom`, that is, a map which preserves addition.", simps] def singleton_mul_hom [has_mul α] : mul_hom α (set α) := { to_fun := singleton, map_mul' := λ a b, singleton_mul_singleton.symm } @[simp, to_additive] lemma empty_mul [has_mul α] : ∅ * s = ∅ := image2_empty_left @[simp, to_additive] lemma mul_empty [has_mul α] : s * ∅ = ∅ := image2_empty_right lemma empty_pow [monoid α] (n : ℕ) (hn : n ≠ 0) : (∅ : set α) ^ n = ∅ := by rw [←nat.sub_add_cancel (nat.pos_of_ne_zero hn), pow_succ, empty_mul] instance decidable_mem_mul [monoid α] [fintype α] [decidable_eq α] [decidable_pred (∈ s)] [decidable_pred (∈ t)] : decidable_pred (∈ s * t) := λ _, decidable_of_iff _ mem_mul.symm instance decidable_mem_pow [monoid α] [fintype α] [decidable_eq α] [decidable_pred (∈ s)] (n : ℕ) : decidable_pred (∈ (s ^ n)) := begin induction n with n ih, { simp_rw [pow_zero, mem_one], apply_instance }, { letI := ih, rw pow_succ, apply_instance } end @[to_additive] lemma mul_subset_mul [has_mul α] (h₁ : s₁ ⊆ t₁) (h₂ : s₂ ⊆ t₂) : s₁ * s₂ ⊆ t₁ * t₂ := image2_subset h₁ h₂ lemma pow_subset_pow [monoid α] (hst : s ⊆ t) (n : ℕ) : s ^ n ⊆ t ^ n := begin induction n with n ih, { rw pow_zero, exact subset.rfl }, { rw [pow_succ, pow_succ], exact mul_subset_mul hst ih }, end @[to_additive] lemma union_mul [has_mul α] : (s ∪ t) * u = (s * u) ∪ (t * u) := image2_union_left @[to_additive] lemma mul_union [has_mul α] : s * (t ∪ u) = (s * t) ∪ (s * u) := image2_union_right @[to_additive] lemma Union_mul_left_image [has_mul α] : (⋃ a ∈ s, (λ x, a * x) '' t) = s * t := Union_image_left _ @[to_additive] lemma Union_mul_right_image [has_mul α] : (⋃ a ∈ t, (λ x, x * a) '' s) = s * t := Union_image_right _ @[simp, to_additive] lemma univ_mul_univ [monoid α] : (univ : set α) * univ = univ := begin have : ∀x, ∃a b : α, a * b = x := λx, ⟨x, ⟨1, mul_one x⟩⟩, simpa only [mem_mul, eq_univ_iff_forall, mem_univ, true_and] end /-- `singleton` is a monoid hom. -/ @[to_additive singleton_add_hom "singleton is an add monoid hom"] def singleton_hom [monoid α] : α →* set α := { to_fun := singleton, map_one' := rfl, map_mul' := λ a b, singleton_mul_singleton.symm } @[to_additive] lemma nonempty.mul [has_mul α] : s.nonempty → t.nonempty → (s * t).nonempty := nonempty.image2 @[to_additive] lemma finite.mul [has_mul α] (hs : finite s) (ht : finite t) : finite (s * t) := hs.image2 _ ht /-- multiplication preserves finiteness -/ @[to_additive "addition preserves finiteness"] def fintype_mul [has_mul α] [decidable_eq α] (s t : set α) [hs : fintype s] [ht : fintype t] : fintype (s * t : set α) := set.fintype_image2 _ s t @[to_additive] lemma bdd_above_mul [ordered_comm_monoid α] {A B : set α} : bdd_above A → bdd_above B → bdd_above (A * B) := begin rintros ⟨bA, hbA⟩ ⟨bB, hbB⟩, use bA * bB, rintros x ⟨xa, xb, hxa, hxb, rfl⟩, exact mul_le_mul' (hbA hxa) (hbB hxb), end section big_operators open_locale big_operators variables {ι : Type*} [comm_monoid α] /-- The n-ary version of `set.mem_mul`. -/ @[to_additive /-" The n-ary version of `set.mem_add`. "-/] lemma mem_finset_prod (t : finset ι) (f : ι → set α) (a : α) : a ∈ ∏ i in t, f i ↔ ∃ (g : ι → α) (hg : ∀ {i}, i ∈ t → g i ∈ f i), ∏ i in t, g i = a := begin classical, induction t using finset.induction_on with i is hi ih generalizing a, { simp_rw [finset.prod_empty, set.mem_one], exact ⟨λ h, ⟨λ i, a, λ i, false.elim, h.symm⟩, λ ⟨f, _, hf⟩, hf.symm⟩ }, rw [finset.prod_insert hi, set.mem_mul], simp_rw [finset.prod_insert hi], simp_rw ih, split, { rintros ⟨x, y, hx, ⟨g, hg, rfl⟩, rfl⟩, refine ⟨function.update g i x, λ j hj, _, _⟩, obtain rfl | hj := finset.mem_insert.mp hj, { rw function.update_same, exact hx }, { rw update_noteq (ne_of_mem_of_not_mem hj hi), exact hg hj, }, rw [finset.prod_update_of_not_mem hi, function.update_same], }, { rintros ⟨g, hg, rfl⟩, exact ⟨g i, is.prod g, hg (is.mem_insert_self _), ⟨g, λ i hi, hg (finset.mem_insert_of_mem hi), rfl⟩, rfl⟩ }, end /-- A version of `set.mem_finset_prod` with a simpler RHS for products over a fintype. -/ @[to_additive /-" A version of `set.mem_finset_sum` with a simpler RHS for sums over a fintype. "-/] lemma mem_fintype_prod [fintype ι] (f : ι → set α) (a : α) : a ∈ ∏ i, f i ↔ ∃ (g : ι → α) (hg : ∀ i, g i ∈ f i), ∏ i, g i = a := by { rw mem_finset_prod, simp } /-- The n-ary version of `set.mul_mem_mul`. -/ @[to_additive /-" The n-ary version of `set.add_mem_add`. "-/] lemma finset_prod_mem_finset_prod (t : finset ι) (f : ι → set α) (g : ι → α) (hg : ∀ i ∈ t, g i ∈ f i) : ∏ i in t, g i ∈ ∏ i in t, f i := by { rw mem_finset_prod, exact ⟨g, hg, rfl⟩ } /-- The n-ary version of `set.mul_subset_mul`. -/ @[to_additive /-" The n-ary version of `set.add_subset_add`. "-/] lemma finset_prod_subset_finset_prod (t : finset ι) (f₁ f₂ : ι → set α) (hf : ∀ {i}, i ∈ t → f₁ i ⊆ f₂ i) : ∏ i in t, f₁ i ⊆ ∏ i in t, f₂ i := begin intro a, rw [mem_finset_prod, mem_finset_prod], rintro ⟨g, hg, rfl⟩, exact ⟨g, λ i hi, hf hi $ hg hi, rfl⟩ end /-! TODO: define `decidable_mem_finset_prod` and `decidable_mem_finset_sum`. -/ end big_operators /-! ### Properties about inversion -/ @[to_additive set.has_neg] instance [has_inv α] : has_inv (set α) := ⟨preimage has_inv.inv⟩ @[simp, to_additive] lemma mem_inv [has_inv α] : a ∈ s⁻¹ ↔ a⁻¹ ∈ s := iff.rfl @[to_additive] lemma inv_mem_inv [group α] : a⁻¹ ∈ s⁻¹ ↔ a ∈ s := by simp only [mem_inv, inv_inv] @[simp, to_additive] lemma inv_preimage [has_inv α] : has_inv.inv ⁻¹' s = s⁻¹ := rfl @[simp, to_additive] lemma image_inv [group α] : has_inv.inv '' s = s⁻¹ := by { simp only [← inv_preimage], rw [image_eq_preimage_of_inverse]; intro; simp only [inv_inv] } @[simp, to_additive] lemma inter_inv [has_inv α] : (s ∩ t)⁻¹ = s⁻¹ ∩ t⁻¹ := preimage_inter @[simp, to_additive] lemma union_inv [has_inv α] : (s ∪ t)⁻¹ = s⁻¹ ∪ t⁻¹ := preimage_union @[simp, to_additive] lemma compl_inv [has_inv α] : (sᶜ)⁻¹ = (s⁻¹)ᶜ := preimage_compl @[simp, to_additive] protected lemma inv_inv [group α] : s⁻¹⁻¹ = s := by { simp only [← inv_preimage, preimage_preimage, inv_inv, preimage_id'] } @[simp, to_additive] protected lemma univ_inv [group α] : (univ : set α)⁻¹ = univ := preimage_univ @[simp, to_additive] lemma inv_subset_inv [group α] {s t : set α} : s⁻¹ ⊆ t⁻¹ ↔ s ⊆ t := (equiv.inv α).surjective.preimage_subset_preimage_iff @[to_additive] lemma inv_subset [group α] {s t : set α} : s⁻¹ ⊆ t ↔ s ⊆ t⁻¹ := by { rw [← inv_subset_inv, set.inv_inv] } @[to_additive] lemma finite.inv [group α] {s : set α} (hs : finite s) : finite s⁻¹ := hs.preimage $ inv_injective.inj_on _ /-! ### Properties about scalar multiplication -/ /-- Scaling a set: multiplying every element by a scalar. -/ instance has_scalar_set [has_scalar α β] : has_scalar α (set β) := ⟨λ a, image (has_scalar.smul a)⟩ @[simp] lemma image_smul [has_scalar α β] {t : set β} : (λ x, a • x) '' t = a • t := rfl lemma mem_smul_set [has_scalar α β] {t : set β} : x ∈ a • t ↔ ∃ y, y ∈ t ∧ a • y = x := iff.rfl lemma smul_mem_smul_set [has_scalar α β] {t : set β} (hy : y ∈ t) : a • y ∈ a • t := ⟨y, hy, rfl⟩ lemma smul_set_union [has_scalar α β] {s t : set β} : a • (s ∪ t) = a • s ∪ a • t := by simp only [← image_smul, image_union] @[simp] lemma smul_set_empty [has_scalar α β] (a : α) : a • (∅ : set β) = ∅ := by rw [← image_smul, image_empty] lemma smul_set_mono [has_scalar α β] {s t : set β} (h : s ⊆ t) : a • s ⊆ a • t := by { simp only [← image_smul, image_subset, h] } /-- Pointwise scalar multiplication by a set of scalars. -/ instance [has_scalar α β] : has_scalar (set α) (set β) := ⟨image2 has_scalar.smul⟩ @[simp] lemma image2_smul [has_scalar α β] {t : set β} : image2 has_scalar.smul s t = s • t := rfl lemma mem_smul [has_scalar α β] {t : set β} : x ∈ s • t ↔ ∃ a y, a ∈ s ∧ y ∈ t ∧ a • y = x := iff.rfl lemma image_smul_prod [has_scalar α β] {t : set β} : (λ x : α × β, x.fst • x.snd) '' s.prod t = s • t := image_prod _ theorem range_smul_range [has_scalar α β] {ι κ : Type*} (b : ι → α) (c : κ → β) : range b • range c = range (λ p : ι × κ, b p.1 • c p.2) := ext $ λ x, ⟨λ hx, let ⟨p, q, ⟨i, hi⟩, ⟨j, hj⟩, hpq⟩ := set.mem_smul.1 hx in ⟨(i, j), hpq ▸ hi ▸ hj ▸ rfl⟩, λ ⟨⟨i, j⟩, h⟩, set.mem_smul.2 ⟨b i, c j, ⟨i, rfl⟩, ⟨j, rfl⟩, h⟩⟩ lemma singleton_smul [has_scalar α β] {t : set β} : ({a} : set α) • t = a • t := image2_singleton_left instance smul_comm_class_set {γ : Type*} [has_scalar α γ] [has_scalar β γ] [smul_comm_class α β γ] : smul_comm_class α (set β) (set γ) := { smul_comm := λ a T T', by simp only [←image2_smul, ←image_smul, image2_image_right, image_image2, smul_comm] } instance smul_comm_class_set' {γ : Type*} [has_scalar α γ] [has_scalar β γ] [smul_comm_class α β γ] : smul_comm_class (set α) β (set γ) := by haveI := smul_comm_class.symm α β γ; exact smul_comm_class.symm _ _ _ instance smul_comm_class {γ : Type*} [has_scalar α γ] [has_scalar β γ] [smul_comm_class α β γ] : smul_comm_class (set α) (set β) (set γ) := { smul_comm := λ T T' T'', begin simp only [←image2_smul, image2_swap _ T], exact image2_assoc (λ b c a, smul_comm a b c), end } instance is_scalar_tower {γ : Type*} [has_scalar α β] [has_scalar α γ] [has_scalar β γ] [is_scalar_tower α β γ] : is_scalar_tower α β (set γ) := { smul_assoc := λ a b T, by simp only [←image_smul, image_image, smul_assoc] } instance is_scalar_tower' {γ : Type*} [has_scalar α β] [has_scalar α γ] [has_scalar β γ] [is_scalar_tower α β γ] : is_scalar_tower α (set β) (set γ) := { smul_assoc := λ a T T', by simp only [←image_smul, ←image2_smul, image_image2, image2_image_left, smul_assoc] } instance is_scalar_tower'' {γ : Type*} [has_scalar α β] [has_scalar α γ] [has_scalar β γ] [is_scalar_tower α β γ] : is_scalar_tower (set α) (set β) (set γ) := { smul_assoc := λ T T' T'', image2_assoc smul_assoc } section monoid /-! ### `set α` as a `(∪,*)`-semiring -/ /-- An alias for `set α`, which has a semiring structure given by `∪` as "addition" and pointwise multiplication `*` as "multiplication". -/ @[derive inhabited] def set_semiring (α : Type*) : Type* := set α /-- The identitiy function `set α → set_semiring α`. -/ protected def up (s : set α) : set_semiring α := s /-- The identitiy function `set_semiring α → set α`. -/ protected def set_semiring.down (s : set_semiring α) : set α := s @[simp] protected lemma down_up {s : set α} : s.up.down = s := rfl @[simp] protected lemma up_down {s : set_semiring α} : s.down.up = s := rfl instance set_semiring.add_comm_monoid : add_comm_monoid (set_semiring α) := { add := λ s t, (s ∪ t : set α), zero := (∅ : set α), add_assoc := union_assoc, zero_add := empty_union, add_zero := union_empty, add_comm := union_comm, } instance set_semiring.non_unital_non_assoc_semiring [has_mul α] : non_unital_non_assoc_semiring (set_semiring α) := { zero_mul := λ s, empty_mul, mul_zero := λ s, mul_empty, left_distrib := λ _ _ _, mul_union, right_distrib := λ _ _ _, union_mul, ..set.has_mul, ..set_semiring.add_comm_monoid } instance set_semiring.non_assoc_semiring [mul_one_class α] : non_assoc_semiring (set_semiring α) := { ..set_semiring.non_unital_non_assoc_semiring, ..set.mul_one_class } instance set_semiring.non_unital_semiring [semigroup α] : non_unital_semiring (set_semiring α) := { ..set_semiring.non_unital_non_assoc_semiring, ..set.semigroup } instance set_semiring.semiring [monoid α] : semiring (set_semiring α) := { ..set_semiring.non_assoc_semiring, ..set_semiring.non_unital_semiring } instance set_semiring.comm_semiring [comm_monoid α] : comm_semiring (set_semiring α) := { ..set.comm_monoid, ..set_semiring.semiring } /-- A multiplicative action of a monoid on a type β gives also a multiplicative action on the subsets of β. -/ instance mul_action_set [monoid α] [mul_action α β] : mul_action α (set β) := { mul_smul := by { intros, simp only [← image_smul, image_image, ← mul_smul] }, one_smul := by { intros, simp only [← image_smul, image_eta, one_smul, image_id'] }, ..set.has_scalar_set } section mul_hom variables [has_mul α] [has_mul β] (m : mul_hom α β) @[to_additive] lemma image_mul : m '' (s * t) = m '' s * m '' t := by { simp only [← image2_mul, image_image2, image2_image_left, image2_image_right, m.map_mul] } @[to_additive] lemma preimage_mul_preimage_subset {s t : set β} : m ⁻¹' s * m ⁻¹' t ⊆ m ⁻¹' (s * t) := by { rintros _ ⟨_, _, _, _, rfl⟩, exact ⟨_, _, ‹_›, ‹_›, (m.map_mul _ _).symm ⟩ } end mul_hom /-- The image of a set under function is a ring homomorphism with respect to the pointwise operations on sets. -/ def image_hom [monoid α] [monoid β] (f : α →* β) : set_semiring α →+* set_semiring β := { to_fun := image f, map_zero' := image_empty _, map_one' := by simp only [← singleton_one, image_singleton, f.map_one], map_add' := image_union _, map_mul' := λ _ _, image_mul f.to_mul_hom } end monoid end set open set section variables {α : Type*} {β : Type*} /-- A nonempty set in a module is scaled by zero to the singleton containing 0 in the module. -/ lemma zero_smul_set [semiring α] [add_comm_monoid β] [module α β] {s : set β} (h : s.nonempty) : (0 : α) • s = (0 : set β) := by simp only [← image_smul, image_eta, zero_smul, h.image_const, singleton_zero] lemma mem_inv_smul_set_iff [field α] [mul_action α β] {a : α} (ha : a ≠ 0) (A : set β) (x : β) : x ∈ a⁻¹ • A ↔ a • x ∈ A := by simp only [← image_smul, mem_image, inv_smul_eq_iff' ha, exists_eq_right] lemma mem_smul_set_iff_inv_smul_mem [field α] [mul_action α β] {a : α} (ha : a ≠ 0) (A : set β) (x : β) : x ∈ a • A ↔ a⁻¹ • x ∈ A := by rw [← mem_inv_smul_set_iff $ inv_ne_zero ha, inv_inv'] end namespace finset variables {α : Type*} [decidable_eq α] /-- The pointwise product of two finite sets `s` and `t`: `st = s ⬝ t = s * t = { x * y | x ∈ s, y ∈ t }`. -/ @[to_additive "The pointwise sum of two finite sets `s` and `t`: `s + t = { x + y | x ∈ s, y ∈ t }`."] instance [has_mul α] : has_mul (finset α) := ⟨λ s t, (s.product t).image (λ p : α × α, p.1 * p.2)⟩ @[to_additive] lemma mul_def [has_mul α] {s t : finset α} : s * t = (s.product t).image (λ p : α × α, p.1 * p.2) := rfl @[to_additive] lemma mem_mul [has_mul α] {s t : finset α} {x : α} : x ∈ s * t ↔ ∃ y z, y ∈ s ∧ z ∈ t ∧ y * z = x := by { simp only [finset.mul_def, and.assoc, mem_image, exists_prop, prod.exists, mem_product] } @[simp, norm_cast, to_additive] lemma coe_mul [has_mul α] {s t : finset α} : (↑(s * t) : set α) = ↑s * ↑t := by { ext, simp only [mem_mul, set.mem_mul, mem_coe] } @[to_additive] lemma mul_mem_mul [has_mul α] {s t : finset α} {x y : α} (hx : x ∈ s) (hy : y ∈ t) : x * y ∈ s * t := by { simp only [finset.mem_mul], exact ⟨x, y, hx, hy, rfl⟩ } lemma add_card_le [has_add α] {s t : finset α} : (s + t).card ≤ s.card * t.card := by { convert finset.card_image_le, rw [finset.card_product, mul_comm] } @[to_additive] lemma mul_card_le [has_mul α] {s t : finset α} : (s * t).card ≤ s.card * t.card := by { convert finset.card_image_le, rw [finset.card_product, mul_comm] } open_locale classical /-- A finite set `U` contained in the product of two sets `S * S'` is also contained in the product of two finite sets `T * T' ⊆ S * S'`. -/ @[to_additive] lemma subset_mul {M : Type*} [monoid M] {S : set M} {S' : set M} {U : finset M} (f : ↑U ⊆ S * S') : ∃ (T T' : finset M), ↑T ⊆ S ∧ ↑T' ⊆ S' ∧ U ⊆ T * T' := begin apply finset.induction_on' U, { use [∅, ∅], simp only [finset.empty_subset, finset.coe_empty, set.empty_subset, and_self], }, rintros a s haU hs has ⟨T, T', hS, hS', h⟩, obtain ⟨x, y, hx, hy, ha⟩ := set.mem_mul.1 (f haU), use [insert x T, insert y T'], simp only [finset.coe_insert], repeat { rw [set.insert_subset], }, use [hx, hS, hy, hS'], refine finset.insert_subset.mpr ⟨_, _⟩, { rw finset.mem_mul, use [x,y], simpa only [true_and, true_or, eq_self_iff_true, finset.mem_insert], }, { suffices g : (s : set M) ⊆ insert x T * insert y T', { norm_cast at g, assumption, }, transitivity ↑(T * T'), apply h, rw finset.coe_mul, apply set.mul_subset_mul (set.subset_insert x T) (set.subset_insert y T'), }, end end finset /-! Some lemmas about pointwise multiplication and submonoids. Ideally we put these in `group_theory.submonoid.basic`, but currently we cannot because that file is imported by this. -/ namespace submonoid variables {M : Type*} [monoid M] @[to_additive] lemma mul_subset {s t : set M} {S : submonoid M} (hs : s ⊆ S) (ht : t ⊆ S) : s * t ⊆ S := by { rintro _ ⟨p, q, hp, hq, rfl⟩, exact submonoid.mul_mem _ (hs hp) (ht hq) } @[to_additive] lemma mul_subset_closure {s t u : set M} (hs : s ⊆ u) (ht : t ⊆ u) : s * t ⊆ submonoid.closure u := mul_subset (subset.trans hs submonoid.subset_closure) (subset.trans ht submonoid.subset_closure) @[to_additive] lemma coe_mul_self_eq (s : submonoid M) : (s : set M) * s = s := begin ext x, refine ⟨_, λ h, ⟨x, 1, h, s.one_mem, mul_one x⟩⟩, rintros ⟨a, b, ha, hb, rfl⟩, exact s.mul_mem ha hb end @[to_additive] lemma closure_mul_le (S T : set M) : closure (S * T) ≤ closure S ⊔ closure T := Inf_le $ λ x ⟨s, t, hs, ht, hx⟩, hx ▸ (closure S ⊔ closure T).mul_mem (set_like.le_def.mp le_sup_left $ subset_closure hs) (set_like.le_def.mp le_sup_right $ subset_closure ht) @[to_additive] lemma sup_eq_closure (H K : submonoid M) : H ⊔ K = closure (H * K) := le_antisymm (sup_le (λ h hh, subset_closure ⟨h, 1, hh, K.one_mem, mul_one h⟩) (λ k hk, subset_closure ⟨1, k, H.one_mem, hk, one_mul k⟩)) (by conv_rhs { rw [← closure_eq H, ← closure_eq K] }; apply closure_mul_le) end submonoid namespace group lemma card_pow_eq_card_pow_card_univ_aux {f : ℕ → ℕ} (h1 : monotone f) {B : ℕ} (h2 : ∀ n, f n ≤ B) (h3 : ∀ n, f n = f (n + 1) → f (n + 1) = f (n + 2)) : ∀ k, B ≤ k → f k = f B := begin have key : ∃ n : ℕ, n ≤ B ∧ f n = f (n + 1), { contrapose! h2, suffices : ∀ n : ℕ, n ≤ B + 1 → n ≤ f n, { exact ⟨B + 1, this (B + 1) (le_refl (B + 1))⟩ }, exact λ n, nat.rec (λ h, nat.zero_le (f 0)) (λ n ih h, lt_of_le_of_lt (ih (n.le_succ.trans h)) (lt_of_le_of_ne (h1 n.le_succ) (h2 n (nat.succ_le_succ_iff.mp h)))) n }, { obtain ⟨n, hn1, hn2⟩ := key, replace key : ∀ k : ℕ, f (n + k) = f (n + k + 1) ∧ f (n + k) = f n := λ k, nat.rec ⟨hn2, rfl⟩ (λ k ih, ⟨h3 _ ih.1, ih.1.symm.trans ih.2⟩) k, replace key : ∀ k : ℕ, n ≤ k → f k = f n := λ k hk, (congr_arg f (nat.add_sub_cancel' hk)).symm.trans (key (k - n)).2, exact λ k hk, (key k (hn1.trans hk)).trans (key B hn1).symm }, end variables {G : Type*} [group G] [fintype G] (S : set G) lemma card_pow_eq_card_pow_card_univ [∀ (k : ℕ), decidable_pred (∈ (S ^ k))] : ∀ k, fintype.card G ≤ k → fintype.card ↥(S ^ k) = fintype.card ↥(S ^ (fintype.card G)) := begin have hG : 0 < fintype.card G := fintype.card_pos_iff.mpr ⟨1⟩, by_cases hS : S = ∅, { intros k hk, congr' 2, rw [hS, empty_pow _ (ne_of_gt (lt_of_lt_of_le hG hk)), empty_pow _ (ne_of_gt hG)] }, obtain ⟨a, ha⟩ := set.ne_empty_iff_nonempty.mp hS, classical, have key : ∀ a (s t : set G), (∀ b : G, b ∈ s → a * b ∈ t) → fintype.card s ≤ fintype.card t, { refine λ a s t h, fintype.card_le_of_injective (λ ⟨b, hb⟩, ⟨a * b, h b hb⟩) _, rintros ⟨b, hb⟩ ⟨c, hc⟩ hbc, exact subtype.ext (mul_left_cancel (subtype.ext_iff.mp hbc)) }, have mono : monotone (λ n, fintype.card ↥(S ^ n) : ℕ → ℕ) := monotone_of_monotone_nat (λ n, key a _ _ (λ b hb, set.mul_mem_mul ha hb)), convert card_pow_eq_card_pow_card_univ_aux mono (λ n, set_fintype_card_le_univ (S ^ n)) (λ n h, le_antisymm (mono (n + 1).le_succ) (key a⁻¹ _ _ _)), { simp only [finset.filter_congr_decidable, fintype.card_of_finset] }, replace h : {a} * S ^ n = S ^ (n + 1), { refine set.eq_of_subset_of_card_le _ (le_trans (ge_of_eq h) _), { exact mul_subset_mul (set.singleton_subset_iff.mpr ha) set.subset.rfl }, { convert key a (S ^ n) ({a} * S ^ n) (λ b hb, set.mul_mem_mul (set.mem_singleton a) hb) } }, rw [pow_succ', ←h, mul_assoc, ←pow_succ', h], rintros _ ⟨b, c, hb, hc, rfl⟩, rwa [set.mem_singleton_iff.mp hb, inv_mul_cancel_left], end end group
b5acd6da23c25b9baa9cdf0145134f6bd223ab74
351a46035517d2a1985619b8cabdf263754d343a
/src/ch4.lean
2190b7bb82e76b6cf34beeb9e283aed33ce13b27
[]
no_license
kaychaks/logic_proof
accc212517b613caca92c10db77e6aaf6b7ccfbc
90f3bf0acbabf558ba2f82dee968255d8bfe2de1
refs/heads/master
1,587,001,734,509
1,548,235,051,000
1,548,235,051,000
165,186,786
0
0
null
null
null
null
UTF-8
Lean
false
false
799
lean
variables A B C D : Prop example : A ∧ (A → B) → B := assume h, show B, from h.right h.left example : A → ¬ (¬ A ∧ B) := assume h1 : A, assume h2: ¬ A ∧ B, show false, from h2.left h1 example : ¬ (A ∧ B) → (A → ¬ B) := λ h1 h2 h3, h1 (⟨ h2 , h3 ⟩) example (h₁ : A ∨ B) (h₂ : A → C) (h₃ : B → D) : C ∨ D := or.elim h₁ (assume h₄ : A, show C ∨ D, from or.inl (h₂ h₄)) (assume h₄ : B, show C ∨ D, from or.inr (h₃ h₄)) example (h : ¬ A ∧ ¬ B) : ¬ (A ∨ B) := assume h1 : A ∨ B, or.elim h1 (assume h2 : A, show false, from h.left h2) (assume h3 : B, show false, from h.right h3) example : ¬ (A ↔ ¬ A) := assume h, have h1 : ¬ A, from assume a : A, show false, from (h.mp a) a, show false, from h1 (h.mpr h1)
23e1a953dd17bb4b4ce4f15acc55c3c6d9b20237
4727251e0cd73359b15b664c3170e5d754078599
/src/algebraic_topology/topological_simplex.lean
abf904e11b970398fc2fa24c295968170f3fdb90
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
3,138
lean
/- Copyright (c) 2021 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Adam Topaz -/ import algebraic_topology.simplex_category import topology.category.Top.basic import topology.instances.nnreal /-! # Topological simplices We define the natural functor from `simplex_category` to `Top` sending `[n]` to the topological `n`-simplex. This is used to define `Top.to_sSet` in `algebraic_topology.simpliciaL_set`. -/ noncomputable theory namespace simplex_category open_locale simplicial nnreal big_operators classical local attribute [instance] category_theory.concrete_category.has_coe_to_sort category_theory.concrete_category.has_coe_to_fun /-- The topological simplex associated to `x : simplex_category`. This is the object part of the functor `simplex_category.to_Top`. -/ def to_Top_obj (x : simplex_category) := { f : x → ℝ≥0 | ∑ i, f i = 1 } instance (x : simplex_category) : has_coe_to_fun x.to_Top_obj (λ _, x → ℝ≥0) := ⟨λ f, (f : x → ℝ≥0)⟩ @[ext] lemma to_Top_obj.ext {x : simplex_category} (f g : x.to_Top_obj) : (f : x → ℝ≥0) = g → f = g := subtype.ext /-- A morphism in `simplex_category` induces a map on the associated topological spaces. -/ def to_Top_map {x y : simplex_category} (f : x ⟶ y) : x.to_Top_obj → y.to_Top_obj := λ g, ⟨λ i, ∑ j in (finset.univ.filter (λ k, f k = i)), g j, begin dsimp [to_Top_obj], simp only [finset.filter_congr_decidable, finset.sum_congr], rw ← finset.sum_bUnion, convert g.2, { rw finset.eq_univ_iff_forall, intros i, rw finset.mem_bUnion, exact ⟨f i, by simp, by simp⟩ }, { intros i hi j hj h e he, apply h, simp only [true_and, finset.inf_eq_inter, finset.mem_univ, finset.mem_filter, finset.mem_inter] at he, rw [← he.1, ← he.2] } end⟩ @[simp] lemma coe_to_Top_map {x y : simplex_category} (f : x ⟶ y) (g : x.to_Top_obj) (i : y) : to_Top_map f g i = ∑ j in (finset.univ.filter (λ k, f k = i)), g j := rfl @[continuity] lemma continuous_to_Top_map {x y : simplex_category} (f : x ⟶ y) : continuous (to_Top_map f) := continuous_subtype_mk _ $ continuous_pi $ λ i, continuous_finset_sum _ $ λ j hj, continuous.comp (continuous_apply _) continuous_subtype_val /-- The functor associating the topological `n`-simplex to `[n] : simplex_category`. -/ @[simps] def to_Top : simplex_category ⥤ Top := { obj := λ x, Top.of x.to_Top_obj, map := λ x y f, ⟨to_Top_map f⟩, map_id' := begin intros x, ext f i : 3, change (finset.univ.filter (λ k, k = i)).sum _ = _, simp [finset.sum_filter] end, map_comp' := begin intros x y z f g, ext h i : 3, dsimp, erw ← finset.sum_bUnion, apply finset.sum_congr, { exact finset.ext (λ j, ⟨λ hj, by simpa using hj, λ hj, by simpa using hj⟩) }, { tauto }, { intros j hj k hk h e he, apply h, simp only [true_and, finset.inf_eq_inter, finset.mem_univ, finset.mem_filter, finset.mem_inter] at he, rw [← he.1, ← he.2] }, end } end simplex_category
860ceb55b67802451c46590f7f0783d7629f82f1
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/category_theory/reflects_isomorphisms.lean
f418e8d326981c8fc86d500124bd9fb0d8a4c46b
[ "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
1,521
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import category_theory.fully_faithful /-! # Functors which reflect isomorphisms A functor `F` reflects isomorphisms if whenever `F.map f` is an isomorphism, `f` was too. It is formalized as a `Prop` valued typeclass `reflects_isomorphisms F`. Any fully faithful functor reflects isomorphisms. -/ open category_theory namespace category_theory universes v₁ v₂ u₁ u₂ variables {C : Type u₁} [category.{v₁} C] section reflects_iso variables {D : Type u₂} [category.{v₂} D] /-- Define what it means for a functor `F : C ⥤ D` to reflect isomorphisms: for any morphism `f : A ⟶ B`, if `F.map f` is an isomorphism then `f` is as well. Note that we do not assume or require that `F` is faithful. -/ class reflects_isomorphisms (F : C ⥤ D) : Prop := (reflects : Π {A B : C} (f : A ⟶ B) [is_iso (F.map f)], is_iso f) /-- If `F` reflects isos and `F.map f` is an iso, then `f` is an iso. -/ lemma is_iso_of_reflects_iso {A B : C} (f : A ⟶ B) (F : C ⥤ D) [is_iso (F.map f)] [reflects_isomorphisms F] : is_iso f := reflects_isomorphisms.reflects F f @[priority 100] instance of_full_and_faithful (F : C ⥤ D) [full F] [faithful F] : reflects_isomorphisms F := { reflects := λ X Y f i, by exactI ⟨⟨F.preimage (inv (F.map f)), ⟨F.map_injective (by simp), F.map_injective (by simp)⟩⟩⟩ } end reflects_iso end category_theory
433216271d181638ad0644284559a94e987382a5
d9ed0fce1c218297bcba93e046cb4e79c83c3af8
/library/init/coe.lean
80016cecff03653751a4e3779e5f6d6a75c2e1d0
[ "Apache-2.0" ]
permissive
leodemoura/lean_clone
005c63aa892a6492f2d4741ee3c2cb07a6be9d7f
cc077554b584d39bab55c360bc12a6fe7957afe6
refs/heads/master
1,610,506,475,484
1,482,348,354,000
1,482,348,543,000
77,091,586
0
0
null
null
null
null
UTF-8
Lean
false
false
5,344
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ /- The elaborator tries to insert coercions automatically. Only instances of has_coe type class are considered in the process. Lean also provides a "lifting" operator: ↑a. It uses all instances of has_lift type class. Every has_coe instance is also a has_lift instance. We recommend users only use has_coe for coercions that do not produce a lot of ambiguity. All coercions and lifts can be identified with the constant coe. We use the has_coe_to_fun type class for encoding coercions from a type to a function space. We use the has_coe_to_sort type class for encoding coercions from a type to a sort. -/ prelude import init.data.list.basic init.data.subtype.basic init.data.prod universe variables u v class has_lift (a : Type u) (b : Type v) := (lift : a → b) /- auxiliary class that contains the transitive closure of has_lift. -/ class has_lift_t (a : Type u) (b : Type v) := (lift : a → b) class has_coe (a : Type u) (b : Type v) := (coe : a → b) /- auxiliary class that contains the transitive closure of has_coe. -/ class has_coe_t (a : Type u) (b : Type v) := (coe : a → b) class has_coe_to_fun (a : Type u) : Type (max u (v+1)) := (F : a → Type v) (coe : Π x, F x) class has_coe_to_sort (a : Type u) : Type (max u (v+1)) := (S : Type v) (coe : a → S) def lift {a : Type u} {b : Type v} [has_lift a b] : a → b := @has_lift.lift a b _ def lift_t {a : Type u} {b : Type v} [has_lift_t a b] : a → b := @has_lift_t.lift a b _ def coe_b {a : Type u} {b : Type v} [has_coe a b] : a → b := @has_coe.coe a b _ def coe_t {a : Type u} {b : Type v} [has_coe_t a b] : a → b := @has_coe_t.coe a b _ def coe_fn_b {a : Type u} [has_coe_to_fun.{u v} a] : Π x : a, has_coe_to_fun.F.{u v} x := has_coe_to_fun.coe /- User level coercion operators -/ def coe {a : Type u} {b : Type v} [has_lift_t a b] : a → b := lift_t def coe_fn {a : Type u} [has_coe_to_fun.{u v} a] : Π x : a, has_coe_to_fun.F.{u v} x := has_coe_to_fun.coe def coe_sort {a : Type u} [has_coe_to_sort.{u v} a] : a → has_coe_to_sort.S.{u v} a := has_coe_to_sort.coe /- Notation -/ notation `↑`:max x:max := coe x notation `⇑`:max x:max := coe_fn x notation `↥`:max x:max := coe_sort x universe variables u₁ u₂ u₃ /- Transitive closure for has_lift, has_coe, has_coe_to_fun -/ instance lift_trans {a : Type u₁} {b : Type u₂} {c : Type u₃} [has_lift a b] [has_lift_t b c] : has_lift_t a c := ⟨λ x, lift_t (lift x : b)⟩ instance lift_base {a : Type u} {b : Type v} [has_lift a b] : has_lift_t a b := ⟨lift⟩ instance coe_trans {a : Type u₁} {b : Type u₂} {c : Type u₃} [has_coe a b] [has_coe_t b c] : has_coe_t a c := ⟨λ x, coe_t (coe_b x : b)⟩ instance coe_base {a : Type u} {b : Type v} [has_coe a b] : has_coe_t a b := ⟨coe_b⟩ instance coe_fn_trans {a : Type u₁} {b : Type u₂} [has_lift_t a b] [has_coe_to_fun.{u₂ u₃} b] : has_coe_to_fun.{u₁ u₃} a := { F := λ x, @has_coe_to_fun.F.{u₂ u₃} b _ (coe x), coe := λ x, coe_fn (coe x) } instance coe_sort_trans {a : Type u₁} {b : Type u₂} [has_lift_t a b] [has_coe_to_sort.{u₂ u₃} b] : has_coe_to_sort.{u₁ u₃} a := { S := has_coe_to_sort.S.{u₂ u₃} b, coe := λ x, coe_sort (coe x) } /- Every coercion is also a lift -/ instance coe_to_lift {a : Type u} {b : Type v} [has_coe_t a b] : has_lift_t a b := ⟨coe_t⟩ /- basic coercions -/ instance coe_bool_to_Prop : has_coe bool Prop := ⟨λ y, y = tt⟩ instance coe_decidable_eq (x : bool) : decidable (coe x) := show decidable (x = tt), from bool.decidable_eq x tt instance coe_subtype {a : Type u} {p : a → Prop} : has_coe {x // p x} a := ⟨λ s, subtype.elt_of s⟩ /- basic lifts -/ universe variables ua ua₁ ua₂ ub ub₁ ub₂ /- Remark: we cant use [has_lift_t a₂ a₁] since it will produce non-termination whenever a type class resolution problem does not have a solution. -/ instance lift_fn {a₁ : Type ua₁} {a₂ : Type ua₂} {b₁ : Type ub₁} {b₂ : Type ub₂} [has_lift a₂ a₁] [has_lift_t b₁ b₂] : has_lift (a₁ → b₁) (a₂ → b₂) := ⟨λ f x, ↑(f ↑x)⟩ instance lift_fn_range {a : Type ua} {b₁ : Type ub₁} {b₂ : Type ub₂} [has_lift_t b₁ b₂] : has_lift (a → b₁) (a → b₂) := ⟨λ f x, ↑(f x)⟩ instance lift_fn_dom {a₁ : Type ua₁} {a₂ : Type ua₂} {b : Type ub} [has_lift a₂ a₁] : has_lift (a₁ → b) (a₂ → b) := ⟨λ f x, f ↑x⟩ instance lift_pair {a₁ : Type ua₁} {a₂ : Type ub₂} {b₁ : Type ub₁} {b₂ : Type ub₂} [has_lift_t a₁ a₂] [has_lift_t b₁ b₂] : has_lift (a₁ × b₁) (a₂ × b₂) := ⟨λ p, prod.cases_on p (λ x y, (↑x, ↑y))⟩ instance lift_pair₁ {a₁ : Type ua₁} {a₂ : Type ua₂} {b : Type ub} [has_lift_t a₁ a₂] : has_lift (a₁ × b) (a₂ × b) := ⟨λ p, prod.cases_on p (λ x y, (↑x, y))⟩ instance lift_pair₂ {a : Type ua} {b₁ : Type ub₁} {b₂ : Type ub₂} [has_lift_t b₁ b₂] : has_lift (a × b₁) (a × b₂) := ⟨λ p, prod.cases_on p (λ x y, (x, ↑y))⟩ instance lift_list {a : Type u} {b : Type v} [has_lift_t a b] : has_lift (list a) (list b) := ⟨λ l, list.map (@coe a b _) l⟩
5d3771fc251bab57a926578fac56eba5218814b7
a81826cfd4fd71c797ea79b8e827b03ccb332c17
/src/wonky_sq/periodicity.lean
97ae6a69f0244f6f6e215bef40133c94d236d33a
[ "Apache-2.0" ]
permissive
jamesa9283/Generalised-Trigonometric-Functions-for-Lean
0a0f4774363c3708be466526935c6d07f4b970f0
33775fb8286eacfc17397fe41af9d446cbdd78f3
refs/heads/master
1,669,337,232,958
1,597,061,947,000
1,597,061,947,000
276,104,804
0
0
null
1,593,540,795,000
1,593,523,262,000
Lean
UTF-8
Lean
false
false
6,460
lean
import tactic import data.real.basic data.int.basic init.data.int.basic import data.complex.exponential import analysis.special_functions.trigonometric -- sinm imports import wonky_sq.basic wonky_sq.addition_formulae /-! Here we are going to prove periodicity lemma's like sinm(2π) = 0 or even sinm(x∓π) = -sin(x) and so on. Makng sure we know the periodicity of the functions we defined in the previous section. -/ open real /- 012 First let us prove that sinm π = 0 -/ lemma sinm_pi (m : ℝ) : sinm pi m = 0 := begin unfold sinm, rw [sin_pi, zero_mul], end -- We need some half pi lemmas to prove the zero for cosm /- 013 -/ lemma cos_half_pi : cos (pi/2) = 0 := by simp /- 014 -/ lemma sin_half_pi : sin (pi/2) = 1 := by simp /- 015 Now let's prove the cosm_halfpi lemma, it is much the same as the sim_pi above -/ lemma cosm_halfpi (m : ℝ) (H: m ≠ 0): cosm (pi/2) m = 0 := begin unfold cosm, rw cos_half_pi, simp only [zero_mul], end /- 016 mathlib doen't seem to have a sin_2pi function so I created it for the next lemma CORRECTION: I DIDN'T HAVE THE RIGHT IMPORTS -/ lemma sin_2pi : sin (2*pi) = 0 := begin simp, end /- 017 Now for the second case of the cosm, but first let us prove it for cos -/ lemma cos_3on2pi : cos (3 *pi / 2) = 0 := begin have H : pi + pi/2 = 3 * pi/2, {ring}, { rw ←H, rw cos_add, simp} end /- 018 Now we can prove the above result for our general sine -/ lemma sinm_2pi (m : ℝ) : sinm (2 * pi) m = 0 := begin unfold sinm, -- I could at this point use simp, but I thought this was nicer and neater rw [sin_2pi, zero_mul], end /- 019 Proving that cosₘ function is zero at 3π/2 -/ lemma cosm_3on2pi (m : ℝ) : cosm (3 * pi / 2) m = 0 := begin unfold cosm, rw [cos_3on2pi, zero_mul] end /- 020 We are now going to prove that for the naturals that sin(n*π)=0. I am aware this in mathlib -/ lemma sin_npi_nat (n : ℕ) : sin (n * pi) = 0 := begin induction n with d hd, { simp }, { simp only [nat.cast_succ], rw [add_mul,sin_add], simp [hd] }, end /- 021 Now for sinₘ where n is an integer -/ lemma sinm_npi (m : ℝ) (n : ℤ) : sinm (n * pi) m = 0 := begin unfold sinm, rw [sin_int_mul_pi, zero_mul] end /- 022 I got annoyed that you couldn't just say that (a + b) / c = a/c + b/c so I proved it. -/ lemma div_distrib (a b c : ℝ) (c ≠ 0) : (a + b) / c = a/c + b/c := begin ring, end /- 034 Just something I forgot I needed -/ lemma sinm_halfpi (x m : ℝ) (H : m ≠ 0): sinm (pi/2) m = 1 := begin unfold sinm, unfold radius, rw [sin_half_pi,cos_half_pi], norm_num, rw zero_rpow H, norm_num, end /- 023 Now to prove that that cos ((2n + 1)π/2) = 0 ∀ n ∈ ℕ -/ lemma cos_nat_pi_half (n : ℕ) (H : n ≠ 0) : cos ((2 * n + 1) * pi / 2) = 0 := begin induction n with d hd, { simp }, -- It goes downhill from here! { simp only [nat.cast_succ], rw [add_mul], have H : (2 * (↑d + 1) * pi + 1 * pi) / 2 = (↑d + 1) * pi + pi/2, {ring,}, rw [H, cos_add, cos_half_pi, sin_half_pi, mul_zero], norm_num, rw add_mul, norm_num, rw sin_add, simp only [add_zero, mul_one, sin_pi, cos_pi, mul_neg_eq_neg_mul_symm, neg_eq_zero, mul_zero], -- this is messy and I dislike it rw ←sin_nat_mul_pi, } end /- 024 Now to prove the above for the integers. It is a lot cleaner. -/ lemma cos_npi_half (n : ℤ) (H : n ≠ 0) : cos (((2 * n + 1) * pi )/ 2) = 0 := begin rw add_mul, have H : (2 * ↑n * pi + 1 * pi) / 2 = ↑n * pi + pi/2, {ring,}, rw [H, cos_add, cos_half_pi, sin_half_pi, mul_zero, mul_one], simp [sin_int_mul_pi], end /- 025 The generalised for, so ∀ n ≠ 0, cosₘ (2n+1)π/2 = 0 -/ lemma cosm_npi_half (n : ℤ) (H : n ≠ 0) (m : ℝ) : cosm ((2 *n + 1) * pi / 2) m = 0 := begin unfold cosm, rw [cos_npi_half n, zero_mul], exact H, end /- We are now going to prove some things relating to taking a result ±π -/ /- 026 First up is normal sin! (Again this is probably in mathlib somewhere but I couldn't find it) -/ lemma sin_selfsim (x : ℝ) : sin(pi - x) = sin(x) := begin rw sin_sub, simp, end /- 027 Now for general cosine (") -/ lemma cos_selfsim (x : ℝ) : cos (pi - x) = -cos x := begin rw cos_sub, simp, end /- 028 Finally to something interesting! We get to do the generalised sine version of 026 -/ lemma sinm_selfsim (x m : ℝ) : sinm (pi - x) m = sinm x m := begin unfold sinm, rw sin_selfsim, unfold radius, rw [sin_selfsim, cos_selfsim], simp, end /- 029 and the same as 028 but for cosₘ -/ lemma cosm_selfsim (x m : ℝ) : cosm(pi - x) m = -cosm x m := begin unfold cosm, rw cos_selfsim, unfold radius, rw [sin_selfsim, cos_selfsim], simp, end -- I may rearrange these wrt whether they are sin, cos, sinₘ, cosₘ etc. /- 030 Lets now prove that sinₘ (x + π/2) = cosₘ x -/ lemma sinm_plus_half_pi_eq_cosm ( x m : ℝ) (h : m ≠ 0): sinm (x + pi/2) m = cosm x m := begin -- want addition formula first rw sinm_add x (pi/2) m; rw [cos_half_pi, mul_zero, sin_half_pi], norm_num, rw add_comm, end /- 034 -/ lemma sinm_plus_half_pi_eq_negsinm ( x m : ℝ) (h : m ≠ 0) : cosm (x + pi/2) m = - sinm x m := begin rw [cosm_add, cos_half_pi, sin_half_pi, mul_zero, mul_zero, mul_one, zero_sub, zero_add, mul_one], rw abs_neg, unfold sinm, unfold radius, ring, end /- 035.0 -/ lemma sinm_self_sim_pos (x m : ℝ) : sinm (x + pi) m = - sinm x m := begin rw sinm_add, rw [cos_pi, sin_pi, mul_comm, neg_one_mul, mul_zero, add_zero, mul_zero, sub_zero, mul_comm, neg_one_mul], repeat {rw[abs_neg]}, apply neg_inj, rw [neg_neg, neg_div, neg_neg, sinm_unfolded], end /- 35.5 -/ lemma sinm_self_sim_neg (x m : ℝ) : sinm (x - pi) m = - sinm x m := begin rw sinm_sub, rw [sin_pi, cos_pi, mul_neg_eq_neg_mul_symm, mul_zero, mul_one], norm_num, apply neg_inj, rw [neg_neg, neg_div, neg_neg,inv_eq_one_div], end /- 040 -/ lemma cosm_self_sim_neg (x m : ℝ) : cosm (x - pi) m = - cosm x m := begin rw cosm_sub, rw [sin_pi, cos_pi, mul_neg_eq_neg_mul_symm, mul_zero, mul_one], norm_num, apply neg_inj, rw [neg_neg, neg_div, neg_neg,inv_eq_one_div], end /- 041 -/ lemma cosm_self_sim_pos (x m : ℝ) : cosm (x + pi) m = - cosm x m := begin rw cosm_add, rw [sin_pi, cos_pi, mul_neg_eq_neg_mul_symm, mul_zero, mul_one], norm_num, apply neg_inj, rw [neg_neg, neg_div, neg_neg,inv_eq_one_div], end
ec9e42cb55cdf6488d318024e74d04ca786fe2c6
c45b34bfd44d8607a2e8762c926e3cfaa7436201
/uexp/src/uexp/rules/aggregateGroupingSetsProjectMerge.lean
1baa374a67f9197ce9dc4fc0da81b92e2b9d257f
[ "BSD-2-Clause" ]
permissive
Shamrock-Frost/Cosette
b477c442c07e45082348a145f19ebb35a7f29392
24cbc4adebf627f13f5eac878f04ffa20d1209af
refs/heads/master
1,619,721,304,969
1,526,082,841,000
1,526,082,841,000
121,695,605
1
0
null
1,518,737,210,000
1,518,737,210,000
null
UTF-8
Lean
false
false
1,055
lean
import ..extra_constants import ..sql import ..u_semiring import ..cosette_tactics open SQL open Proj open Pred open Expr theorem rule : forall (Γ scm_s : Schema) (rel_r : relation scm_s) (s_a : Column datatypes.int scm_s) (s_b : Column datatypes.int scm_s) (s_c : Column datatypes.int scm_s), let var_a : Expr (Γ ++ scm_s) datatypes.int := uvariable (right⋅s_a), var_b : Expr (Γ ++ scm_s) datatypes.int := uvariable (right⋅s_b), var_c : Expr (Γ ++ scm_s) datatypes.int := uvariable (right⋅s_c), groupByVars := combineGroupByProj PLAIN(var_a) $ combineGroupByProj PLAIN(var_b) $ COUNT(var_c) in denoteSQL (SELECT (groupByVars) FROM1 table rel_r GROUP BY combine (right⋅s_a) (right⋅s_b) : SQL Γ _) = denoteSQL (SELECT (groupByVars) FROM1 table rel_r GROUP BY combine (right⋅s_b) (right⋅s_a) : SQL Γ _) := begin intros, unfold_all_denotations, funext, simp, sorry end
ac4492fb685100d2e70e51caba6f8a0c4b251002
618003631150032a5676f229d13a079ac875ff77
/src/data/set/disjointed.lean
5eacb7966a130c969226c983b37f291ddaf12921
[ "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
3,748
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 Disjointed sets -/ import data.set.lattice import tactic.wlog open set classical open_locale classical universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} {s t u : set α} /-- A relation `p` holds pairwise if `p i j` for all `i ≠ j`. -/ def pairwise {α : Type*} (p : α → α → Prop) := ∀i j, i ≠ j → p i j theorem pairwise_on_bool {r} (hr : symmetric r) {a b : α} : pairwise (r on (λ c, cond c a b)) ↔ r a b := by simp [pairwise, bool.forall_bool, function.on_fun]; exact and_iff_right_of_imp (@hr _ _) theorem pairwise_disjoint_on_bool [semilattice_inf_bot α] {a b : α} : pairwise (disjoint on (λ c, cond c a b)) ↔ disjoint a b := pairwise_on_bool $ λ _ _, disjoint.symm namespace set /-- If `f : ℕ → set α` is a sequence of sets, then `disjointed f` is the sequence formed with each set subtracted from the later ones in the sequence, to form a disjoint sequence. -/ def disjointed (f : ℕ → set α) (n : ℕ) : set α := f n ∩ (⋂i<n, - f i) lemma disjoint_disjointed {f : ℕ → set α} : pairwise (disjoint on disjointed f) := λ i j h, begin wlog h' : i ≤ j; [skip, {revert a, exact (this h.symm).symm}], rintro a ⟨⟨h₁, _⟩, h₂, h₃⟩, simp at h₃, exact h₃ _ (lt_of_le_of_ne h' h) h₁ end lemma disjoint_disjointed' {f : ℕ → set α} : ∀ i j, i ≠ j → (disjointed f i) ∩ (disjointed f j) = ∅ := begin assume i j hij, have := @disjoint_disjointed _ f i j hij, rw [function.on_fun, disjoint_iff] at this, exact this end lemma disjointed_subset {f : ℕ → set α} {n : ℕ} : disjointed f n ⊆ f n := inter_subset_left _ _ lemma Union_lt_succ {f : ℕ → set α} {n} : (⋃i < nat.succ n, f i) = f n ∪ (⋃i < n, f i) := ext $ λ a, by simp [nat.lt_succ_iff_lt_or_eq, or_and_distrib_right, exists_or_distrib, or_comm] lemma Inter_lt_succ {f : ℕ → set α} {n} : (⋂i < nat.succ n, f i) = f n ∩ (⋂i < n, f i) := ext $ λ a, by simp [nat.lt_succ_iff_lt_or_eq, or_imp_distrib, forall_and_distrib, and_comm] lemma Union_disjointed {f : ℕ → set α} : (⋃n, disjointed f n) = (⋃n, f n) := subset.antisymm (Union_subset_Union $ assume i, inter_subset_left _ _) $ assume x h, have ∃n, x ∈ f n, from mem_Union.mp h, have hn : ∀ (i : ℕ), i < nat.find this → x ∉ f i, from assume i, nat.find_min this, mem_Union.mpr ⟨nat.find this, nat.find_spec this, by simp; assumption⟩ lemma disjointed_induct {f : ℕ → set α} {n : ℕ} {p : set α → Prop} (h₁ : p (f n)) (h₂ : ∀t i, p t → p (t \ f i)) : p (disjointed f n) := begin rw disjointed, generalize_hyp : f n = t at h₁ ⊢, induction n, case nat.zero { simp [nat.not_lt_zero, h₁] }, case nat.succ : n ih { rw [Inter_lt_succ, inter_comm (-f n), ← inter_assoc], exact h₂ _ n ih } end lemma disjointed_of_mono {f : ℕ → set α} {n : ℕ} (hf : monotone f) : disjointed f (n + 1) = f (n + 1) \ f n := have (⋂i (h : i < n + 1), -f i) = - f n, from le_antisymm (infi_le_of_le n $ infi_le_of_le (nat.lt_succ_self _) $ subset.refl _) (le_infi $ assume i, le_infi $ assume hi, compl_le_compl $ hf $ nat.le_of_succ_le_succ hi), by simp [disjointed, this, diff_eq] lemma Union_disjointed_of_mono {f : ℕ → set α} (hf : monotone f) : ∀ n : ℕ, (⋃i<n.succ, disjointed f i) = f n | 0 := by rw [Union_lt_succ]; simp [disjointed, nat.not_lt_zero] | (n+1) := by rw [Union_lt_succ, disjointed_of_mono hf, Union_disjointed_of_mono n, diff_union_self, union_eq_self_of_subset_right (hf (nat.le_succ _))] end set
f579aebe615fa49a690ce0be57ff65677dd2039e
2a73ce8bc0731b170b40e8c9faca9b49d34ba5c6
/alaoglu.lean
e319087f8d9c06784a4e68e7d080a21ac85312e7
[]
no_license
kkytola/lean-questions
f383016b7870432807d8f4ced256f7506a59b0ff
9a7ded8036534575b682e28ddfed4c2f1089959d
refs/heads/main
1,692,989,428,439
1,634,665,853,000
1,634,665,853,000
353,850,251
0
0
null
null
null
null
UTF-8
Lean
false
false
22,817
lean
/- Copyright (c) 2021 Kalle Kytölä. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kalle Kytölä -/ import tactic import analysis.normed_space.bounded_linear_maps import analysis.normed_space.weak_dual import analysis.seminorm import linear_algebra.affine_space.basic noncomputable theory open normed_space open metric open basis open set open function open filter open_locale topological_space section alaoglu /-! ### Banach-Alaoglu theorem We prove that the dual unit ball of a normed space `E` over either `ℝ` or `ℂ` is compact in the weak-star topology. (In fact more generally any polar of a neighborhood of the origin is compact in `weak_dual 𝕜 E` for `[is_R_or_C 𝕜]`.) -/ -- Where to place? lemma linear_map_bound_of_sphere_bound {𝕜 : Type*} [is_R_or_C 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {r : ℝ} (r_pos : 0 < r) (c : ℝ) (f : E →ₗ[𝕜] 𝕜) (h : ∀ z ∈ sphere (0 : E) r, ∥ f z ∥ ≤ c) (z : E) : ∥ f z ∥ ≤ c / r * ∥ z ∥ := begin by_cases z_zero : z = 0, { rw z_zero, simp only [linear_map.map_zero, norm_zero, mul_zero], }, have norm_z_eq : ∥(∥ z ∥ : 𝕜)∥ = ∥ z ∥ := norm_norm' 𝕜 E z, have norm_r_eq : ∥(r : 𝕜)∥ = r, { rw [is_R_or_C.norm_eq_abs, is_R_or_C.abs_of_real], exact abs_of_pos r_pos, }, set z₁ := (r * ∥ z ∥⁻¹ : 𝕜) • z with hz₁, have norm_f_z₁ : ∥ f z₁ ∥ ≤ c, { apply h z₁, rw [mem_sphere_zero_iff_norm, hz₁, norm_smul, normed_field.norm_mul], simp only [normed_field.norm_inv], rw [norm_z_eq, mul_assoc, inv_mul_cancel (norm_pos_iff.mpr z_zero).ne.symm, mul_one], unfold_coes, simp only [norm_algebra_map_eq, ring_hom.to_fun_eq_coe], exact abs_of_pos r_pos, }, have r_ne_zero : (r : 𝕜) ≠ 0 := (algebra_map ℝ 𝕜).map_ne_zero.mpr r_pos.ne.symm, have eq : f z = ∥ z ∥ / r * (f z₁), { rw [hz₁, linear_map.map_smul, smul_eq_mul], rw [← mul_assoc, ← mul_assoc, div_mul_cancel _ r_ne_zero, mul_inv_cancel, one_mul], simp only [z_zero, is_R_or_C.of_real_eq_zero, norm_eq_zero, ne.def, not_false_iff], }, rw [eq, normed_field.norm_mul, normed_field.norm_div, norm_z_eq, norm_r_eq, div_mul_eq_mul_div, div_mul_eq_mul_div, mul_comm], apply div_le_div _ _ r_pos rfl.ge, { exact mul_nonneg ((norm_nonneg _).trans norm_f_z₁) (norm_nonneg z), }, apply mul_le_mul norm_f_z₁ rfl.le (norm_nonneg z) ((norm_nonneg _).trans norm_f_z₁), end -- Where to place? `analysis/normed_space/operator_norm`? lemma linear_map_bound_of_ball_bound {𝕜 : Type*} [is_R_or_C 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {r : ℝ} (r_pos : 0 < r) (c : ℝ) (f : E →ₗ[𝕜] 𝕜) (h : ∀ z ∈ closed_ball (0 : E) r, ∥ f z ∥ ≤ c) : ∀ (z : E), ∥ f z ∥ ≤ c / r * ∥ z ∥ := begin apply linear_map_bound_of_sphere_bound r_pos c f, exact λ z hz, h z hz.le, end -- Where to place? `analysis/normed_space/operator_norm`? lemma _root_.continuous_linear_map.op_norm_bound_of_ball_bound {𝕜 : Type*} [is_R_or_C 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {r : ℝ} (r_pos : 0 < r) (c : ℝ) (f : E →L[𝕜] 𝕜) (h : ∀ z ∈ closed_ball (0 : E) r, ∥ f z ∥ ≤ c) : ∥ f ∥ ≤ c / r := begin apply continuous_linear_map.op_norm_le_bound, { apply div_nonneg _ r_pos.le, exact (norm_nonneg _).trans (h 0 (by simp only [norm_zero, mem_closed_ball, dist_zero_left, r_pos.le])), }, apply linear_map_bound_of_ball_bound r_pos, exact λ z hz, h z hz, end variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] variables {E : Type*} [normed_group E] [normed_space 𝕜 E] namespace weak_dual /-- Given a subset `s` in a normed space `E` (over a field `𝕜`), the polar `polar s` is the subset of `weak_dual 𝕜 E` consisting of those functionals which evaluate to something of norm at most one at all points `z ∈ s`. -/ def polar (s : set E) : set (weak_dual 𝕜 E) := {x' : weak_dual 𝕜 E | ∀ z ∈ s, ∥ x' z ∥ ≤ 1 } @[simp] lemma zero_mem_polar (s : set E) : (0 : weak_dual 𝕜 E) ∈ (@polar 𝕜 _ E _ _ s) := λ _ _, by simp only [zero_le_one, continuous_linear_map.zero_apply, norm_zero] lemma polar_eq_Inter (s : set E) : (@polar 𝕜 _ E _ _ s) = ⋂ z ∈ s, {x' : weak_dual 𝕜 E | ∥ x' z ∥ ≤ 1 } := by { dunfold polar, ext, simp only [mem_bInter_iff, mem_set_of_eq], } -- Exists already? If not, where to place? `topology/basic`? lemma _root_.continuous.is_closed_preimage {α β : Type*} [topological_space α] [topological_space β] {f : α → β} (f_cont : continuous f) {s : set β} (s_closed : is_closed s) : is_closed (f⁻¹' s) := begin apply is_open_compl_iff.mp, rw ← preimage_compl, exact f_cont.is_open_preimage _ (is_open_compl_iff.mpr s_closed), end /-- The polar `polar s` of a set `s : E` is a closed subset of `weak_dual 𝕜 E`. -/ lemma polar_closed (s : set E) : is_closed (@polar 𝕜 _ E _ _ s) := begin rw polar_eq_Inter, apply is_closed_bInter, intros z hz, have eq : {x' : weak_dual 𝕜 E | ∥x' z∥ ≤ 1} = (λ (x' : weak_dual 𝕜 E), ∥x' z∥)⁻¹' (Iic 1), by refl, rw eq, refine continuous.is_closed_preimage _ (is_closed_Iic), apply continuous.comp continuous_norm (eval_continuous _ _ z), end /-- If `x'` is a dual element such that the norms `∥ x' z ∥` are bounded for `z ∈ s`, then a small scalar multiple of `x'` is in `polar s`. -/ lemma smul_mem_polar {s : set E} {x' : weak_dual 𝕜 E} {c : 𝕜} (hc : ∀ z, z ∈ s → ∥ x' z ∥ ≤ ∥c∥) : (c⁻¹ • x') ∈ (@polar 𝕜 _ E _ _ s) := begin by_cases c_zero : c = 0, { rw c_zero, dunfold polar, simp only [zero_le_one, continuous_linear_map.zero_apply, norm_zero, mem_set_of_eq, implies_true_iff, inv_zero, zero_smul], }, have eq : ∀ z, ∥ c⁻¹ • (x' z) ∥ = ∥ c⁻¹ ∥ * ∥ x' z ∥ := λ z, norm_smul c⁻¹ _, have le : ∀ z, z ∈ s → ∥ c⁻¹ • (x' z) ∥ ≤ ∥ c⁻¹ ∥ * ∥ c ∥, { intros z hzs, rw eq z, apply mul_le_mul (le_of_eq rfl) (hc z hzs) (norm_nonneg _) (norm_nonneg _), }, have cancel : ∥ c⁻¹ ∥ * ∥ c ∥ = 1, by simp only [c_zero, norm_eq_zero, ne.def, not_false_iff, inv_mul_cancel, normed_field.norm_inv], rwa cancel at le, end /-- The `polar` of closed unit ball in a normed space `E` is the closed unit ball of the (normed) dual (seen as a subset of the weak dual). -/ lemma polar_closed_unit_ball {𝕜 : Type*} [is_R_or_C 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] : (@polar 𝕜 _ E _ _ (closed_ball (0 : E) 1)) = {x' : weak_dual 𝕜 E | (∥ x'.to_normed_dual ∥ ≤ 1) } := begin ext x', simp only [mem_closed_ball, mem_set_of_eq, dist_zero_right], split, { intros h, apply continuous_linear_map.op_norm_le_of_ball zero_lt_one zero_le_one, intros z hz, have key := linear_map_bound_of_ball_bound zero_lt_one 1 x'.to_normed_dual.to_linear_map h z, simp only [continuous_linear_map.to_linear_map_eq_coe, continuous_linear_map.coe_coe, div_one] at key, exact key, }, { intros h z hz, simp only [mem_closed_ball, dist_zero_right] at hz, apply (continuous_linear_map.unit_le_op_norm x'.to_normed_dual z hz).trans h, }, end /-- If `s` is a neighborhood of the origin in a normed space `E`, then at any point `z : E` there exists a bound for the norms of the values `x' z` of the elements `x' ∈ polar s` of the polar of `s`. -/ lemma polar_eval_bounded_of_nbhd_zero {s : set E} (s_nhd : s ∈ 𝓝 (0 : E)) (z : E) : ∃ (r : ℝ), ∀ (x' : weak_dual 𝕜 E), x' ∈ @polar 𝕜 _ E _ _ s → (∥ x' z ∥ ≤ r) := begin have s_absnt : absorbent 𝕜 s := absorbent_nhds_zero s_nhd, rcases s_absnt z with ⟨c, ⟨c_pos, hc⟩⟩, cases normed_field.exists_lt_norm 𝕜 c with a ha, specialize hc a ha.le, have a_norm_pos : 0 < ∥ a ∥ := lt_trans c_pos ha, have a_ne_zero : a ≠ 0 := norm_pos_iff.mp a_norm_pos, have w_in_s : a⁻¹ • z ∈ s, { rcases hc with ⟨ w , ⟨hws, haw⟩⟩, rwa [← haw, ← mul_smul, inv_mul_cancel a_ne_zero, one_smul], }, use ∥a∥, intros x' hx', specialize hx' _ w_in_s, simp only [algebra.id.smul_eq_mul, normed_field.norm_mul, continuous_linear_map.map_smul, normed_field.norm_inv] at hx', have key := mul_le_mul (@rfl _ ∥ a ∥).ge hx' _ (norm_nonneg a), rwa [mul_one, ← mul_assoc, mul_inv_cancel (ne_of_gt a_norm_pos), one_mul] at key, apply mul_nonneg _ (norm_nonneg _), simp only [inv_nonneg, norm_nonneg], end /-- If `s` is a neighborhood of the origin in a normed space `E`, then there exists a function `r : E → ℝ` such that for all elements `x' ∈ polar s` one has `∥ x' z ∥ ≤ r(z)`. -/ lemma polar_finite_values_of_nbhd_zero {s : set E} (s_nhd : s ∈ 𝓝 (0 : E)) : ∃ (r : E → ℝ), ∀ (x' : weak_dual 𝕜 E) (z : E), x' ∈ (@polar 𝕜 _ E _ _ s) → ∥ x' z ∥ ≤ r z := begin cases classical.axiom_of_choice (@polar_eval_bounded_of_nbhd_zero 𝕜 _ E _ _ s s_nhd) with r hr, use r, intros x' z, exact hr z x', end /-- Given a neighborhood `s` of the origin in a normed space `E` over `ℝ` or `ℂ`, the dual norms of all elements of the polar `polar s` are bounded by a constant. -/ lemma polar_bounded_of_nbhd_zero {𝕜 : Type*} [is_R_or_C 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {s : set E} (s_nhd : s ∈ 𝓝 (0 : E)) : ∃ (c : ℝ), ∀ (x' : weak_dual 𝕜 E), x' ∈ @polar 𝕜 _ E _ _ s → (∥ x'.to_normed_dual ∥ ≤ c) := begin rcases metric.mem_nhds_iff.mp s_nhd with ⟨r, ⟨r_pos, r_ball⟩⟩, have half_r_pos : 0 < r / 2 := by linarith, use 2 / r, intros x' hx', have key := continuous_linear_map.op_norm_bound_of_ball_bound half_r_pos 1 x', simp only [one_div_div] at key, apply key, intros z hz, have z_mem_ball : z ∈ ball (0 : E) r, { simp only [mem_ball_zero_iff], simp only [mem_closed_ball, dist_zero_right] at hz, linarith, }, exact hx' z (r_ball z_mem_ball), end /-- Given a neighborhood `s` of the origin in a normed space `E`, for any `z : E` it is possible to choose a real number `r` such that for any functional `x' ∈ polar s` in the polar of `s`, the value at `z` satisfies the norm bound `∥ x' z ∥ ≤ r`. Such an `r` is given by `bounds_fun _ z`. -/ private def bounds_fun {s : set E} (s_nhd : s ∈ 𝓝 (0 : E)) : E → ℝ := classical.some (classical.axiom_of_choice (@polar_eval_bounded_of_nbhd_zero 𝕜 _ E _ _ s s_nhd)) private lemma bounds_fun_spec {s : set E} (s_nhd : s ∈ 𝓝 (0 : E)) (x' : weak_dual 𝕜 E) (z : E) : x' ∈ @polar 𝕜 _ E _ _ s → ∥ x' z ∥ ≤ @bounds_fun 𝕜 _ E _ _ s s_nhd z := classical.some_spec (classical.axiom_of_choice (@polar_eval_bounded_of_nbhd_zero 𝕜 _ E _ _ s s_nhd)) z x' /-- The (weak) dual `weak_dual 𝕜 E` of a normed space `E` consists of bounded linear functionals `E → 𝕜`. Such functionals can be interpreted as elements of the Cartesian product `Π (_ : E), 𝕜` via the function `weak_dual.to_Pi : weak_dual 𝕜 E → Π (_ : E), 𝕜`. -/ def _root_.weak_dual.to_Pi {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [topological_space E] [add_comm_group E] [module 𝕜 E] (x' : weak_dual 𝕜 E) := ((λ z, (x' z)) : (Π (_ : E), 𝕜)) /-- The function `weak_dual.to_Pi : weak_dual 𝕜 E → Π (_ : E), 𝕜` is an embedding. -/ lemma embedding_weak_dual_to_pi : embedding (λ (x' : weak_dual 𝕜 E), x'.to_Pi) := { induced := eq_of_nhds_eq_nhds (congr_fun rfl), inj := begin intros φ₁ φ₂ h, ext z, exact congr_fun h z, end, } lemma pi_ball_bounds_fun_cpt [proper_space 𝕜] {s : set E} (s_nhd : s ∈ 𝓝 (0 : E)) : is_compact (set.pi (univ : set E) (λ z, (closed_ball (0 : 𝕜) (@bounds_fun 𝕜 _ E _ _ s s_nhd z)))) := begin apply is_compact_univ_pi, exact λ z, proper_space.is_compact_closed_ball 0 _, end /-- The image of the polar `polar s` of a neighborhood `s` of the origin under `weak_dual.to_Pi : weak_dual 𝕜 E → Π (_ : E), 𝕜` is contained in a product of closed balls. -/ lemma embedding_weak_dual_to_pi.image_polar_nhd_subset {s : set E} (s_nhd : s ∈ 𝓝 (0 : E)) : (@weak_dual.to_Pi 𝕜 _ E _ _ _) '' (polar s) ⊆ (set.pi (univ : set E) (λ z, (closed_ball (0 : 𝕜) (@bounds_fun 𝕜 _ E _ _ s s_nhd z)))) := begin intros f hf, simp at hf, rcases hf with ⟨x', hx', f_eq⟩, simp only [mem_closed_ball, dist_zero_right, mem_univ_pi], intros z, have key := bounds_fun_spec s_nhd x' z, have eq : x' z = f z := congr_fun f_eq z, rw eq at key, exact key hx', end /-- In the product of copies of a normed field, sets of the form `{g | ∥ f(i) - g(i) ∥ < ε}` for `ε > 0` are neighborhoods of `f`. -/ lemma basic_nhd_of_Pi_normed_field {ι : Type*} (f : (Π (_ : ι), 𝕜)) (i : ι) {ε : ℝ} (ε_pos : 0 < ε) : {g : (Π (_ : ι), 𝕜) | ∥ f i - g i ∥ < ε} ∈ 𝓝 f := begin have eq : { g : (Π (_ : ι), 𝕜) | ∥ f i - g i ∥ < ε} = set.pi ({i} : set ι) (λ _, ball (f i) ε), { ext g, simp only [mem_ball, eval_apply, singleton_pi, mem_set_of_eq, mem_preimage], rw dist_comm, exact mem_ball_iff_norm.symm, }, rw eq, apply set_pi_mem_nhds, exact finite_singleton i, intros j hj, have eq₀ : j = i := hj, rw eq₀, exact ball_mem_nhds (f i) ε_pos, end /-- Elements of the closure of the range of the embedding `weak_dual.to_Pi : weak_dual 𝕜 E → Π (_ : E), 𝕜` are linear. Here it is stated as the elements respecting linear combinations. -/ lemma embedding_weak_dual_to_pi.linear_of_mem_closure_range' (f : (Π (_ : E), 𝕜)) (hf : f ∈ closure (range (@weak_dual.to_Pi 𝕜 _ E _ _ _))) (c₁ c₂ : 𝕜) (z₁ z₂ : E) : f (c₁ • z₁ + c₂ • z₂) = c₁ • f(z₁) + c₂ • f(z₂) := begin set φ : (Π (_ : E), 𝕜) → 𝕜 := (λ g, g (c₁ • z₁ + c₂ • z₂) - c₁ • g(z₁) - c₂ • g(z₂)) with hφ, have φ_cont : continuous φ, { apply continuous.sub, { apply continuous.sub, { exact continuous_apply (c₁ • z₁ + c₂ • z₂), }, exact continuous.smul continuous_const (continuous_apply _), }, exact continuous.smul continuous_const (continuous_apply _), }, have sin_closed : is_closed ({0} : set 𝕜) := t1_space.t1 0, have preim_cl : is_closed (φ⁻¹' ({0} : set 𝕜)) := φ_cont.is_closed_preimage sin_closed, suffices : range (@weak_dual.to_Pi 𝕜 _ E _ _ _) ⊆ φ⁻¹' ({0} : set 𝕜), { have key := (is_closed.closure_subset_iff preim_cl).mpr this hf, exact sub_eq_iff_eq_add'.mp (sub_eq_zero.mp key), }, intros g hg, cases hg with g₀ hg₀, simp only [algebra.id.smul_eq_mul, mem_singleton_iff, norm_eq_zero, mem_preimage], rw [hφ, ← hg₀], dunfold weak_dual.to_Pi, rw add_comm, simp only [algebra.id.smul_eq_mul, continuous_linear_map.map_add, add_sub_cancel, sub_self, continuous_linear_map.map_smul], end /-- Elements of the closure of the range of the embedding `weak_dual.to_Pi : weak_dual 𝕜 E → Π (_ : E), 𝕜` can be viewed as linear maps `E → 𝕜`. -/ def embedding_weak_dual_to_pi.linear_of_mem_closure_range (f : (Π (_ : E), 𝕜)) (hf : f ∈ closure (range (@weak_dual.to_Pi 𝕜 _ E _ _ _))) : E →ₗ[𝕜] 𝕜 := { to_fun := f, map_add' := begin intros z₁ z₂, have key := embedding_weak_dual_to_pi.linear_of_mem_closure_range' f hf (1 : 𝕜) (1 : 𝕜) z₁ z₂, rwa [one_smul, one_smul, one_smul 𝕜 _, one_smul 𝕜 _] at key, end, map_smul' := begin intros c z, have key := embedding_weak_dual_to_pi.linear_of_mem_closure_range' f hf c (0 : 𝕜) z (0 : E), rwa [zero_smul, zero_smul, add_zero, add_zero] at key, end, } lemma embedding_weak_dual_to_pi.linear_of_mem_closure_range_apply (f : (Π (_ : E), 𝕜)) (hf : f ∈ closure (range (@weak_dual.to_Pi 𝕜 _ E _ _ _))) (z : E) : embedding_weak_dual_to_pi.linear_of_mem_closure_range f hf z = f z := rfl /-- Elements of the closure of the image under `weak_dual.to_Pi : weak_dual 𝕜 E → Π (_ : E), 𝕜` of a subset defined by a non-strict bound on the norm still satisfy the same bound. -/ lemma embedding_weak_dual_to_pi.norm_eval_le_of_mem_closure_norm_eval_le (z : E) (c : ℝ) (f : (Π (_ : E), 𝕜)) (hf : f ∈ closure ((@weak_dual.to_Pi 𝕜 _ E _ _ _) '' {x' : weak_dual 𝕜 E | ∥ x' z ∥ ≤ c})) : ∥ f z ∥ ≤ c := begin suffices : ∀ (ε : ℝ), 0 < ε → ∥ f (z) ∥ ≤ c + ε, { exact le_of_forall_pos_le_add this, }, intros ε ε_pos, have nhd := basic_nhd_of_Pi_normed_field f z ε_pos, have clos := mem_closure_iff_nhds.mp hf _ nhd, cases clos with g hg, simp only [mem_image, mem_inter_eq, mem_set_of_eq] at hg, rcases hg with ⟨tri, ⟨y', ⟨at_z_le, eq_g⟩⟩⟩, have eq : y'.to_Pi z = y' z := rfl, rw [← eq_g, eq] at tri, have key := norm_add_le_of_le tri.le at_z_le, rwa [sub_add_cancel, add_comm] at key, end /-- Elements of the closure of the image under `weak_dual.to_Pi : weak_dual 𝕜 E → Π (_ : E), 𝕜` of a polar `polar s` of a neighborhood `s` of the origin are continuous (linear) functions. -/ lemma embedding_weak_dual_to_pi.continuous_of_mem_closure_polar_nhd {𝕜 : Type*} [is_R_or_C 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {s : set E} (s_nhd : s ∈ 𝓝 (0 : E)) (φ : (Π (_ : E), 𝕜)) (hφ : φ ∈ closure ((@weak_dual.to_Pi 𝕜 _ E _ _ _) '' (@polar 𝕜 _ E _ _ s))) : @continuous E 𝕜 _ _ φ := begin cases @polar_bounded_of_nbhd_zero 𝕜 _ E _ _ s s_nhd with c hc, have c_nn : 0 ≤ c := le_trans (norm_nonneg _) (hc 0 (zero_mem_polar s)), have hφ' : φ ∈ closure (range (@weak_dual.to_Pi 𝕜 _ E _ _ _)), { apply mem_of_mem_of_subset hφ _, apply closure_mono, simp only [preimage_range, subset_univ, image_subset_iff], }, set flin := embedding_weak_dual_to_pi.linear_of_mem_closure_range φ hφ' with hflin, suffices : continuous flin, { assumption, }, apply linear_map.continuous_of_bound flin c, intros z, set θ := λ (ψ : Π (_ : E), 𝕜), ∥ ψ z ∥ with hθ, have θ_cont : continuous θ, { apply continuous.comp continuous_norm, exact continuous_apply z, }, have sin_closed : is_closed (Icc (-c * ∥z∥) (c * ∥z∥) : set ℝ) := is_closed_Icc, have preim_cl := θ_cont.is_closed_preimage sin_closed, suffices : (@weak_dual.to_Pi 𝕜 _ E _ _ _) '' (@polar 𝕜 _ E _ _ s) ⊆ θ⁻¹' (Icc (-c * ∥z∥) (c * ∥z∥)), { exact ((is_closed.closure_subset_iff preim_cl).mpr this hφ).right, }, intros ψ hψ, rcases hψ with ⟨x', ⟨polar_x', ψ_x'⟩⟩, rw ← ψ_x', simp only [neg_mul_eq_neg_mul_symm, mem_preimage, mem_Icc, hθ], split, { apply le_trans _ (norm_nonneg _), rw right.neg_nonpos_iff, exact mul_nonneg c_nn (norm_nonneg _), }, apply le_trans (continuous_linear_map.le_op_norm x' z) _, exact mul_le_mul (hc x' polar_x') rfl.ge (norm_nonneg z) c_nn, end /-- The image under `weak_dual.to_Pi : weak_dual 𝕜 E → Π (_ : E), 𝕜` of a polar `polar s` of a neighborhood `s` of the origin is a closed set. -/ lemma embedding_weak_dual_to_pi.image_polar_nhd_closed {𝕜 : Type*} [is_R_or_C 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {s : set E} (s_nhd : s ∈ 𝓝 (0 : E)) : is_closed ((@weak_dual.to_Pi 𝕜 _ E _ _ _) '' (@polar 𝕜 _ E _ _ s)) := begin apply is_closed_iff_cluster_pt.mpr, intros f hf, simp only [mem_image, mem_set_of_eq], have f_in_closure : f ∈ closure ((@weak_dual.to_Pi 𝕜 _ E _ _ _) '' (@polar 𝕜 _ E _ _ s)), from mem_closure_iff_cluster_pt.mpr hf, have f_in_closure₀ : f ∈ closure (range (@weak_dual.to_Pi 𝕜 _ E _ _ _)), { apply closure_mono (image_subset_range _ _), exact mem_closure_iff_cluster_pt.mpr hf, }, set f_lin := embedding_weak_dual_to_pi.linear_of_mem_closure_range f f_in_closure₀ with h_f_lin, have f_cont := embedding_weak_dual_to_pi.continuous_of_mem_closure_polar_nhd s_nhd f f_in_closure, set φ : weak_dual 𝕜 E := { to_fun := f, map_add' := begin intros z₁ z₂, have key := f_lin.map_add z₁ z₂, rw h_f_lin at key, repeat {rwa embedding_weak_dual_to_pi.linear_of_mem_closure_range_apply f f_in_closure₀ _ at key, }, end, map_smul' := begin intros c z, have key := f_lin.map_smul c z, rw h_f_lin at key, repeat {rwa embedding_weak_dual_to_pi.linear_of_mem_closure_range_apply f f_in_closure₀ _ at key, }, end, cont := f_cont, } with hφ, use φ, split, { dunfold polar, simp, intros z hz, apply embedding_weak_dual_to_pi.norm_eval_le_of_mem_closure_norm_eval_le z 1 f, have ss : polar s ⊆ {x' : weak_dual 𝕜 E | ∥x' z∥ ≤ 1}, { intros x' hx', exact hx' z hz, }, apply closure_mono (image_subset _ ss), exact mem_closure_iff_cluster_pt.mpr hf, }, { ext z, dunfold to_Pi, rw hφ, simp, }, end /-- The image under `weak_dual.to_Pi : weak_dual 𝕜 E → Π (_ : E), 𝕜` of the polar `polar s` of a neighborhood `s` of the origin is compact. -/ lemma embedding_weak_dual_to_pi.image_polar_nhd_compact {𝕜 : Type*} [is_R_or_C 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {s : set E} (s_nhd : s ∈ 𝓝 (0 : E)) : is_compact ((@weak_dual.to_Pi 𝕜 _ E _ _ _) '' (polar s)) := begin apply compact_of_is_closed_subset _ _ (embedding_weak_dual_to_pi.image_polar_nhd_subset s_nhd), exact pi_ball_bounds_fun_cpt s_nhd, exact embedding_weak_dual_to_pi.image_polar_nhd_closed s_nhd, end /-- The Banach-Alaoglu theorem: the polar `polar s` of a neighborhood `s` of the origin in a normed space `E` over `𝕜` is compact subset of `weak_dual 𝕜 E` (assuming `[is_R_or_C 𝕜]`). -/ theorem polar_nhd_weak_star_compact {𝕜 : Type*} [is_R_or_C 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {s : set E} (s_nhd : s ∈ 𝓝 (0 : E)) : is_compact (@polar 𝕜 _ E _ _ s) := begin apply (@embedding_weak_dual_to_pi 𝕜 _ E _ _ ).is_compact_iff_is_compact_image.mpr, exact embedding_weak_dual_to_pi.image_polar_nhd_compact s_nhd, end /-- The Banach-Alaoglu theorem: the dual unit ball is compact in the weak-star topology. -/ theorem unit_ball_weak_star_compact {𝕜 : Type*} [is_R_or_C 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] : is_compact {x' : weak_dual 𝕜 E | (∥ x'.to_normed_dual ∥ ≤ 1)} := begin rw ← polar_closed_unit_ball, apply polar_nhd_weak_star_compact (closed_ball_mem_nhds (0 : E) (@zero_lt_one ℝ _ _)), end end weak_dual end alaoglu
d9863ab57879e6c1c72789bb36c91434d3de0487
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/archive/imo/imo2013_q1.lean
842d2de4c36f59eddbed8f9aaa5dcba74e5588ee
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
4,432
lean
/- Copyright (c) 2021 David Renshaw. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Renshaw -/ import data.pnat.basic import data.nat.parity import algebra.big_operators.pi import tactic.ring import tactic.field_simp /-! # IMO 2013 Q1 > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Prove that for any pair of positive integers k and n, there exist k positive integers m₁, m₂, ..., mₖ (not necessarily different) such that 1 + (2ᵏ - 1)/ n = (1 + 1/m₁) * (1 + 1/m₂) * ... * (1 + 1/mₖ). # Solution Adaptation of the solution found in https://www.imo-official.org/problems/IMO2013SL.pdf We prove a slightly more general version where k does not need to be strictly positive. -/ open_locale big_operators namespace imo2013_q1 lemma arith_lemma (k n : ℕ) : 0 < 2 * n + 2^k.succ := calc 0 < 2 : zero_lt_two ... = 2^1 : (pow_one 2).symm ... ≤ 2^k.succ : nat.pow_le_pow_of_le_right zero_lt_two (nat.le_add_left 1 k) ... ≤ 2 * n + 2^k.succ : nat.le_add_left _ _ lemma prod_lemma (m : ℕ → ℕ+) (k : ℕ) (nm : ℕ+): ∏ (i : ℕ) in finset.range k, ((1 : ℚ) + 1 / ↑(if i < k then m i else nm)) = ∏ (i : ℕ) in finset.range k, (1 + 1 / m i) := begin suffices : ∀ i, i ∈ finset.range k → (1 : ℚ) + 1 / ↑(if i < k then m i else nm) = 1 + 1 / m i, from finset.prod_congr rfl this, intros i hi, simp [finset.mem_range.mp hi] end end imo2013_q1 open imo2013_q1 theorem imo2013_q1 (n : ℕ+) (k : ℕ) : (∃ m : ℕ → ℕ+, (1 : ℚ) + (2^k - 1) / n = (∏ i in finset.range k, (1 + 1 / m i))) := begin revert n, induction k with pk hpk, { intro n, use (λ_, 1), simp }, -- For the base case, any m works. intro n, obtain ⟨t, ht : ↑n = t + t⟩ | ⟨t, ht : ↑n = 2 * t + 1⟩ := (n : ℕ).even_or_odd, { -- even case rw ← two_mul at ht, cases t, -- Eliminate the zero case to simplify later calculations. { exfalso, rw mul_zero at ht, exact pnat.ne_zero n ht }, -- Now we have ht : ↑n = 2 * (t + 1). let t_succ : ℕ+ := ⟨t + 1, t.succ_pos⟩, obtain ⟨pm, hpm⟩ := hpk t_succ, let m := λi, if i < pk then pm i else ⟨2 * t + 2^pk.succ, arith_lemma pk t⟩, use m, have hmpk : (m pk : ℚ) = 2 * t + 2^pk.succ, { have : m pk = ⟨2 * t + 2^pk.succ, _⟩ := if_neg (irrefl pk), simp [this] }, have denom_ne_zero : (2 * (t:ℚ) + 2^pk.succ) ≠ 0, { norm_cast, exact (ne_of_gt $ arith_lemma pk t) }, calc (1 : ℚ) + (2 ^ pk.succ - 1) / ↑n = 1 + (2 * 2 ^ pk - 1) / (2 * (t + 1) : ℕ) : by rw [coe_coe n, ht, pow_succ] ... = (1 + 1 / (2 * t + 2 * 2^pk)) * (1 + (2 ^ pk - 1) / (↑t + 1)) : by { field_simp [t.cast_add_one_ne_zero], ring } ... = (1 + 1 / (2 * t + 2^pk.succ)) * (1 + (2 ^ pk - 1) / t_succ) : by norm_cast ... = (∏ i in finset.range pk, (1 + 1 / m i)) * (1 + 1 / (m pk)) : by rw [prod_lemma, hpm, ←hmpk, mul_comm] ... = ∏ i in finset.range pk.succ, (1 + 1 / m i) : by rw ← finset.prod_range_succ _ pk }, { -- odd case let t_succ : ℕ+ := ⟨t + 1, t.succ_pos⟩, obtain ⟨pm, hpm⟩ := hpk t_succ, let m := λi, if i < pk then pm i else ⟨2 * t + 1, nat.succ_pos _⟩, use m, have hmpk : (m pk : ℚ) = 2 * t + 1, { have : m pk = ⟨2 * t + 1, _⟩ := if_neg (irrefl pk), simp [this] }, have denom_ne_zero : (2 * (t : ℚ) + 1) ≠ 0 := by { norm_cast, apply (2 * t).succ_ne_zero }, calc (1 : ℚ) + (2 ^ pk.succ - 1) / ↑n = 1 + (2 * 2^pk - 1) / (2 * t + 1 : ℕ) : by rw [coe_coe n, ht, pow_succ] ... = (1 + 1 / (2 * t + 1)) * (1 + (2^pk - 1) / (t + 1)) : by { field_simp [t.cast_add_one_ne_zero], ring } ... = (1 + 1 / (2 * t + 1)) * (1 + (2^pk - 1) / t_succ) : by norm_cast ... = (∏ i in finset.range pk, (1 + 1 / m i)) * (1 + 1 / ↑(m pk)) : by rw [prod_lemma, hpm, ←hmpk, mul_comm] ... = ∏ i in finset.range pk.succ, (1 + 1 / m i) : by rw ← finset.prod_range_succ _ pk } end
bf247c5bf2a9fe229bd1e1512604cd8e5a321144
e39f04f6ff425fe3b3f5e26a8998b817d1dba80f
/category_theory/yoneda.lean
8be9f8ece9f10a8cff20244486b294a53de88e49
[ "Apache-2.0" ]
permissive
kristychoi/mathlib
c504b5e8f84e272ea1d8966693c42de7523bf0ec
257fd84fe98927ff4a5ffe044f68c4e9d235cc75
refs/heads/master
1,586,520,722,896
1,544,030,145,000
1,544,031,933,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,352
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison /- The Yoneda embedding, as a functor `yoneda : C ⥤ ((Cᵒᵖ) ⥤ (Type v₁))`, along with an instance that it is `fully_faithful`. Also the Yoneda lemma, `yoneda_lemma : (yoneda_pairing C) ≅ (yoneda_evaluation C)`. -/ import category_theory.natural_transformation import category_theory.opposites import category_theory.types import category_theory.fully_faithful import category_theory.natural_isomorphism namespace category_theory universes u₁ v₁ u₂ variables {C : Type u₁} [𝒞 : category.{u₁ v₁} C] include 𝒞 def yoneda : C ⥤ (Cᵒᵖ ⥤ Type v₁) := { obj := λ X, { obj := λ Y : C, Y ⟶ X, map := λ Y Y' f g, f ≫ g, map_comp' := begin intros X_1 Y Z f g, ext1, dsimp at *, erw [category.assoc] end, map_id' := begin intros X_1, ext1, dsimp at *, erw [category.id_comp] end }, map := λ X X' f, { app := λ Y g, g ≫ f } } def coyoneda : Cᵒᵖ ⥤ (C ⥤ Type v₁) := { obj := λ X : C, { obj := λ Y, X ⟶ Y, map := λ Y Y' f g, g ≫ f, map_comp' := begin intros X_1 Y Z f g, ext1, dsimp at *, erw [category.assoc] end, map_id' := begin intros X_1, ext1, dsimp at *, erw [category.comp_id] end }, map := λ X X' f, { app := λ Y g, f ≫ g }, map_comp' := begin intros X Y Z f g, ext1, ext1, dsimp at *, erw [category.assoc] end, map_id' := begin intros X, ext1, ext1, dsimp at *, erw [category.id_comp] end } namespace yoneda @[simp] lemma obj_obj (X Y : C) : (yoneda.obj X).obj Y = (Y ⟶ X) := rfl @[simp] lemma obj_map (X : C) {Y Y' : C} (f : Y ⟶ Y') : (yoneda.obj X).map f = λ g, f ≫ g := rfl @[simp] lemma map_app {X X' : C} (f : X ⟶ X') (Y : C) : (yoneda.map f).app Y = λ g, g ≫ f := rfl lemma obj_map_id {X Y : Cᵒᵖ} (f : X ⟶ Y) : ((@yoneda C _).obj X).map f (𝟙 X) = ((@yoneda C _).map f).app Y (𝟙 Y) := by obviously @[simp] lemma naturality {X Y : C} (α : yoneda.obj X ⟶ yoneda.obj Y) {Z Z' : C} (f : Z ⟶ Z') (h : Z' ⟶ X) : f ≫ α.app Z' h = α.app Z (f ≫ h) := begin erw [functor_to_types.naturality], refl end instance yoneda_fully_faithful : fully_faithful (@yoneda C _) := { preimage := λ X Y f, (f.app X) (𝟙 X), injectivity' := λ X Y f g p, begin injection p with h, convert (congr_fun (congr_fun h X) (𝟙 X)) ; simp end } /-- Extensionality via Yoneda. The typical usage would be ``` -- Goal is `X ≅ Y` apply yoneda.ext, -- Goals are now functions `(Z ⟶ X) → (Z ⟶ Y)`, `(Z ⟶ Y) → (Z ⟶ X)`, and the fact that these functions are inverses and natural in `Z`. ``` -/ def ext (X Y : C) (p : Π {Z : C}, (Z ⟶ X) → (Z ⟶ Y)) (q : Π {Z : C}, (Z ⟶ Y) → (Z ⟶ X)) (h₁ : Π {Z : C} (f : Z ⟶ X), q (p f) = f) (h₂ : Π {Z : C} (f : Z ⟶ Y), p (q f) = f) (n : Π {Z Z' : C} (f : Z' ⟶ Z) (g : Z ⟶ X), p (f ≫ g) = f ≫ p g) : X ≅ Y := @preimage_iso _ _ _ _ yoneda _ _ _ _ (nat_iso.of_components (λ Z, { hom := p, inv := q, }) (by tidy)) -- We need to help typeclass inference with some awkward universe levels here. instance prod_category_instance_1 : category (((Cᵒᵖ) ⥤ Type v₁) × (Cᵒᵖ)) := category_theory.prod.{(max u₁ (v₁+1)) (max u₁ v₁) u₁ v₁} (Cᵒᵖ ⥤ Type v₁) (Cᵒᵖ) instance prod_category_instance_2 : category ((Cᵒᵖ) × ((Cᵒᵖ) ⥤ Type v₁)) := category_theory.prod.{u₁ v₁ (max u₁ (v₁+1)) (max u₁ v₁)} (Cᵒᵖ) (Cᵒᵖ ⥤ Type v₁) end yoneda namespace coyoneda @[simp] lemma obj_obj (X Y : C) : (coyoneda.obj X).obj Y = (X ⟶ Y) := rfl @[simp] lemma obj_map {X' X : C} (f : X' ⟶ X) (Y : C) : (coyoneda.obj Y).map f = λ g, g ≫ f := rfl @[simp] lemma map_app (X : C) {Y Y' : C} (f : Y ⟶ Y') : (coyoneda.map f).app X = λ g, f ≫ g := rfl end coyoneda class representable (F : Cᵒᵖ ⥤ Type v₁) := (X : C) (w : yoneda.obj X ≅ F) variables (C) open yoneda def yoneda_evaluation : (Cᵒᵖ × (Cᵒᵖ ⥤ Type v₁)) ⥤ Type (max u₁ v₁) := (evaluation_uncurried (Cᵒᵖ) (Type v₁)) ⋙ ulift_functor.{v₁ u₁} @[simp] lemma yoneda_evaluation_map_down (P Q : Cᵒᵖ × (Cᵒᵖ ⥤ Type v₁)) (α : P ⟶ Q) (x : (yoneda_evaluation C).obj P) : ((yoneda_evaluation C).map α x).down = α.2.app Q.1 (P.2.map α.1 x.down) := rfl def yoneda_pairing : (Cᵒᵖ × (Cᵒᵖ ⥤ Type v₁)) ⥤ Type (max u₁ v₁) := (functor.prod yoneda.op (functor.id (Cᵒᵖ ⥤ Type v₁))) ⋙ functor.hom (Cᵒᵖ ⥤ Type v₁) @[simp] lemma yoneda_pairing_map (P Q : Cᵒᵖ × (Cᵒᵖ ⥤ Type v₁)) (α : P ⟶ Q) (β : (yoneda_pairing C).obj P) : (yoneda_pairing C).map α β = yoneda.map α.1 ≫ β ≫ α.2 := rfl def yoneda_lemma : yoneda_pairing C ≅ yoneda_evaluation C := { hom := { app := λ F x, ulift.up ((x.app F.1) (𝟙 F.1)), naturality' := begin intros X Y f, ext1, ext1, cases f, cases Y, cases X, dsimp at *, simp at *, erw [←functor_to_types.naturality, obj_map_id, functor_to_types.naturality, functor_to_types.map_id] end }, inv := { app := λ F x, { app := λ X a, (F.2.map a) x.down, naturality' := begin intros X Y f, ext1, cases x, cases F, dsimp at *, erw [functor_to_types.map_comp] end }, naturality' := begin intros X Y f, ext1, ext1, ext1, cases x, cases f, cases Y, cases X, dsimp at *, erw [←functor_to_types.naturality, functor_to_types.map_comp] end }, hom_inv_id' := begin ext1, ext1, ext1, ext1, cases X, dsimp at *, erw [←functor_to_types.naturality, obj_map_id, functor_to_types.naturality, functor_to_types.map_id] end, inv_hom_id' := begin ext1, ext1, ext1, cases x, cases X, dsimp at *, erw [functor_to_types.map_id] end }. variables {C} @[simp] def yoneda_sections (X : C) (F : Cᵒᵖ ⥤ Type v₁) : (yoneda.obj X ⟹ F) ≅ ulift.{u₁} (F.obj X) := nat_iso.app (yoneda_lemma C) (X, F) omit 𝒞 @[simp] def yoneda_sections_small {C : Type u₁} [small_category C] (X : C) (F : Cᵒᵖ ⥤ Type u₁) : (yoneda.obj X ⟹ F) ≅ F.obj X := yoneda_sections X F ≪≫ ulift_trivial _ end category_theory
9640898584c69496c9b7718508cf47179f97e6b5
957a80ea22c5abb4f4670b250d55534d9db99108
/library/init/relator.lean
f1d8bb20e564e5aa3cc2ff1f061e63e85cbd02de
[ "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
2,840
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 Relator for functions, pairs, sums, and lists. -/ prelude import init.core init.data.basic namespace relator universe variables u₁ u₂ v₁ v₂ reserve infixr ` ⇒ `:40 /- TODO(johoelzl): * should we introduce relators of datatypes as recursive function or as inductive predicate? For now we stick to the recursor approach. * relation lift for datatypes, Π, Σ, set, and subtype types * proof composition and identity laws * implement method to derive relators from datatype -/ section variables {α : Type u₁} {β : Type u₂} {γ : Type v₁} {δ : Type v₂} variables (R : α → β → Prop) (S : γ → δ → Prop) def lift_fun (f : α → γ) (g : β → δ) : Prop := ∀{a b}, R a b → S (f a) (g b) infixr ⇒ := lift_fun end section variables {α : Type u₁} {β : inout Type u₂} (R : inout α → β → Prop) @[class] def right_total := ∀b, ∃a, R a b @[class] def left_total := ∀a, ∃b, R a b @[class] def bi_total := left_total R ∧ right_total R end section variables {α : Type u₁} {β : Type u₂} (R : α → β → Prop) @[class] def left_unique := ∀{a b c}, R a b → R c b → a = c @[class] def right_unique := ∀{a b c}, R a b → R a c → b = c lemma rel_forall_of_right_total [t : right_total R] : ((R ⇒ implies) ⇒ implies) (λp, ∀i, p i) (λq, ∀i, q i) := assume p q Hrel H b, exists.elim (t b) (assume a Rab, Hrel Rab (H _)) lemma rel_exists_of_left_total [t : left_total R] : ((R ⇒ implies) ⇒ implies) (λp, ∃i, p i) (λq, ∃i, q i) := assume p q Hrel ⟨a, pa⟩, let ⟨b, Rab⟩ := t a in ⟨b, Hrel Rab pa⟩ lemma rel_forall_of_total [t : bi_total R] : ((R ⇒ iff) ⇒ iff) (λp, ∀i, p i) (λq, ∀i, q i) := assume p q Hrel, ⟨assume H b, exists.elim (t.right b) (assume a Rab, (Hrel Rab).mp (H _)), assume H a, exists.elim (t.left a) (assume b Rab, (Hrel Rab).mpr (H _))⟩ lemma rel_exists_of_total [t : bi_total R] : ((R ⇒ iff) ⇒ iff) (λp, ∃i, p i) (λq, ∃i, q i) := assume p q Hrel, ⟨assume ⟨a, pa⟩, let ⟨b, Rab⟩ := t.left a in ⟨b, (Hrel Rab).1 pa⟩, assume ⟨b, qb⟩, let ⟨a, Rab⟩ := t.right b in ⟨a, (Hrel Rab).2 qb⟩⟩ lemma left_unique_of_rel_eq {eq' : β → β → Prop} (he : (R ⇒ (R ⇒ iff)) eq eq') : left_unique R | a b c (ab : R a b) (cb : R c b) := have eq' b b, from iff.mp (he ab ab) rfl, iff.mpr (he ab cb) this end lemma rel_imp : (iff ⇒ (iff ⇒ iff)) implies implies := assume p q h r s l, imp_congr h l lemma rel_not : (iff ⇒ iff) not not := assume p q h, not_congr h instance bi_total_eq {α : Type u₁} : relator.bi_total (@eq α) := ⟨assume a, ⟨a, rfl⟩, assume a, ⟨a, rfl⟩⟩ end relator
ce8e48076160c6a84736541ed5da9cca8a17e5cb
f7315930643edc12e76c229a742d5446dad77097
/hott/init/path.hlean
d1dfac72e760ecbcf358e5ffd76c16d4a96607a4
[ "Apache-2.0" ]
permissive
bmalehorn/lean
8f77b762a76c59afff7b7403f9eb5fc2c3ce70c1
53653c352643751c4b62ff63ec5e555f11dae8eb
refs/heads/master
1,610,945,684,489
1,429,681,220,000
1,429,681,449,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
24,364
hlean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: init.path Authors: Jeremy Avigad, Jakob von Raumer, Floris van Doorn Ported from Coq HoTT -/ prelude import .function .datatypes .relation .tactic open function eq /- Path equality -/ namespace eq variables {A B C : Type} {P : A → Type} {x y z t : A} --notation a = b := eq a b notation x = y `:>`:50 A:49 := @eq A x y definition idp {a : A} := refl a definition idpath (a : A) := refl a -- unbased path induction definition rec' [reducible] {P : Π (a b : A), (a = b) -> Type} (H : Π (a : A), P a a idp) {a b : A} (p : a = b) : P a b p := eq.rec (H a) p definition rec_on' [reducible] {P : Π (a b : A), (a = b) -> Type} {a b : A} (p : a = b) (H : Π (a : A), P a a idp) : P a b p := eq.rec (H a) p /- Concatenation and inverse -/ definition concat (p : x = y) (q : y = z) : x = z := eq.rec (λu, u) q p definition inverse (p : x = y) : y = x := eq.rec (refl x) p notation p₁ ⬝ p₂ := concat p₁ p₂ notation p ⁻¹ := inverse p --a second notation for the inverse, which is not overloaded postfix [parsing-only] `⁻¹ᵖ`:std.prec.max_plus := inverse /- The 1-dimensional groupoid structure -/ -- The identity path is a right unit. definition con_idp (p : x = y) : p ⬝ idp = p := eq.rec_on p idp -- The identity path is a right unit. definition idp_con (p : x = y) : idp ⬝ p = p := eq.rec_on p idp -- Concatenation is associative. definition con.assoc' (p : x = y) (q : y = z) (r : z = t) : p ⬝ (q ⬝ r) = (p ⬝ q) ⬝ r := eq.rec_on r (eq.rec_on q idp) definition con.assoc (p : x = y) (q : y = z) (r : z = t) : (p ⬝ q) ⬝ r = p ⬝ (q ⬝ r) := eq.rec_on r (eq.rec_on q idp) -- The left inverse law. definition con.right_inv (p : x = y) : p ⬝ p⁻¹ = idp := eq.rec_on p idp -- The right inverse law. definition con.left_inv (p : x = y) : p⁻¹ ⬝ p = idp := eq.rec_on p idp /- Several auxiliary theorems about canceling inverses across associativity. These are somewhat redundant, following from earlier theorems. -/ definition inv_con_cancel_left (p : x = y) (q : y = z) : p⁻¹ ⬝ (p ⬝ q) = q := eq.rec_on q (eq.rec_on p idp) definition con_inv_cancel_left (p : x = y) (q : x = z) : p ⬝ (p⁻¹ ⬝ q) = q := eq.rec_on q (eq.rec_on p idp) definition con_inv_cancel_right (p : x = y) (q : y = z) : (p ⬝ q) ⬝ q⁻¹ = p := eq.rec_on q (eq.rec_on p idp) definition inv_con_cancel_right (p : x = z) (q : y = z) : (p ⬝ q⁻¹) ⬝ q = p := eq.rec_on q (take p, eq.rec_on p idp) p -- Inverse distributes over concatenation definition con_inv (p : x = y) (q : y = z) : (p ⬝ q)⁻¹ = q⁻¹ ⬝ p⁻¹ := eq.rec_on q (eq.rec_on p idp) definition inv_con_inv_left (p : y = x) (q : y = z) : (p⁻¹ ⬝ q)⁻¹ = q⁻¹ ⬝ p := eq.rec_on q (eq.rec_on p idp) -- universe metavariables definition inv_con_inv_right (p : x = y) (q : z = y) : (p ⬝ q⁻¹)⁻¹ = q ⬝ p⁻¹ := eq.rec_on p (take q, eq.rec_on q idp) q definition inv_con_inv_inv (p : y = x) (q : z = y) : (p⁻¹ ⬝ q⁻¹)⁻¹ = q ⬝ p := eq.rec_on p (eq.rec_on q idp) -- Inverse is an involution. definition inv_inv (p : x = y) : p⁻¹⁻¹ = p := eq.rec_on p idp -- auxiliary definition used by 'cases' tactic definition elim_inv_inv {A : Type} {a b : A} {C : a = b → Type} (H₁ : a = b) (H₂ : C (H₁⁻¹⁻¹)) : C H₁ := eq.rec_on (inv_inv H₁) H₂ /- Theorems for moving things around in equations -/ definition con_eq_of_eq_inv_con (p : x = z) (q : y = z) (r : y = x) : p = r⁻¹ ⬝ q → r ⬝ p = q := eq.rec_on r (take p h, idp_con _ ⬝ h ⬝ idp_con _) p definition con_eq_of_eq_con_inv (p : x = z) (q : y = z) (r : y = x) : r = q ⬝ p⁻¹ → r ⬝ p = q := eq.rec_on p (take q h, (con_idp _ ⬝ h ⬝ con_idp _)) q definition inv_con_eq_of_eq_con (p : x = z) (q : y = z) (r : x = y) : p = r ⬝ q → r⁻¹ ⬝ p = q := eq.rec_on r (take q h, idp_con _ ⬝ h ⬝ idp_con _) q definition con_inv_eq_of_eq_con (p : z = x) (q : y = z) (r : y = x) : r = q ⬝ p → r ⬝ p⁻¹ = q := eq.rec_on p (take r h, con_idp _ ⬝ h ⬝ con_idp _) r definition eq_con_of_inv_con_eq (p : x = z) (q : y = z) (r : y = x) : r⁻¹ ⬝ q = p → q = r ⬝ p := eq.rec_on r (take p h, (idp_con _)⁻¹ ⬝ h ⬝ (idp_con _)⁻¹) p definition eq_con_of_con_inv_eq (p : x = z) (q : y = z) (r : y = x) : q ⬝ p⁻¹ = r → q = r ⬝ p := eq.rec_on p (take q h, (con_idp _)⁻¹ ⬝ h ⬝ (con_idp _)⁻¹) q definition eq_inv_con_of_con_eq (p : x = z) (q : y = z) (r : x = y) : r ⬝ q = p → q = r⁻¹ ⬝ p := eq.rec_on r (take q h, (idp_con _)⁻¹ ⬝ h ⬝ (idp_con _)⁻¹) q definition eq_con_inv_of_con_eq (p : z = x) (q : y = z) (r : y = x) : q ⬝ p = r → q = r ⬝ p⁻¹ := eq.rec_on p (take r h, (con_idp _)⁻¹ ⬝ h ⬝ (con_idp _)⁻¹) r definition eq_of_con_inv_eq_idp (p q : x = y) : p ⬝ q⁻¹ = idp → p = q := eq.rec_on q (take p h, (con_idp _)⁻¹ ⬝ h) p definition eq_of_inv_con_eq_idp (p q : x = y) : q⁻¹ ⬝ p = idp → p = q := eq.rec_on q (take p h, (idp_con _)⁻¹ ⬝ h) p definition eq_inv_of_con_eq_idp' (p : x = y) (q : y = x) : p ⬝ q = idp → p = q⁻¹ := eq.rec_on q (take p h, (con_idp _)⁻¹ ⬝ h) p definition eq_inv_of_con_eq_idp (p : x = y) (q : y = x) : q ⬝ p = idp → p = q⁻¹ := eq.rec_on q (take p h, (idp_con _)⁻¹ ⬝ h) p definition eq_of_idp_eq_inv_con (p q : x = y) : idp = p⁻¹ ⬝ q → p = q := eq.rec_on p (take q h, h ⬝ (idp_con _)) q definition eq_of_idp_eq_con_inv (p q : x = y) : idp = q ⬝ p⁻¹ → p = q := eq.rec_on p (take q h, h ⬝ (con_idp _)) q definition inv_eq_of_idp_eq_con (p : x = y) (q : y = x) : idp = q ⬝ p → p⁻¹ = q := eq.rec_on p (take q h, h ⬝ (con_idp _)) q definition inv_eq_of_idp_eq_con' (p : x = y) (q : y = x) : idp = p ⬝ q → p⁻¹ = q := eq.rec_on p (take q h, h ⬝ (idp_con _)) q /- Transport -/ definition transport [reducible] (P : A → Type) {x y : A} (p : x = y) (u : P x) : P y := eq.rec_on p u -- This idiom makes the operation right associative. notation p `▹`:65 x:64 := transport _ p x definition tr_inv [reducible] (P : A → Type) {x y : A} (p : x = y) (u : P y) : P x := p⁻¹ ▹ u definition ap ⦃A B : Type⦄ (f : A → B) {x y:A} (p : x = y) : f x = f y := eq.rec_on p idp definition ap01 [reducible] := ap definition homotopy [reducible] (f g : Πx, P x) : Type := Πx : A, f x = g x notation f ∼ g := homotopy f g namespace homotopy protected definition refl (f : Πx, P x) : f ∼ f := λ x, idp protected definition symm {f g : Πx, P x} (H : f ∼ g) : g ∼ f := λ x, inverse (H x) protected definition trans {f g h : Πx, P x} (H1 : f ∼ g) (H2 : g ∼ h) : f ∼ h := λ x, concat (H1 x) (H2 x) calc_refl refl calc_symm symm calc_trans trans end homotopy definition apD10 {f g : Πx, P x} (H : f = g) : f ∼ g := λx, eq.rec_on H idp --the next theorem is useful if you want to write "apply (apD10' a)" definition apD10' {f g : Πx, P x} (a : A) (H : f = g) : f a = g a := eq.rec_on H idp definition ap10 {f g : A → B} (H : f = g) : f ∼ g := apD10 H definition ap11 {f g : A → B} (H : f = g) {x y : A} (p : x = y) : f x = g y := eq.rec_on H (eq.rec_on p idp) definition apD (f : Πa:A, P a) {x y : A} (p : x = y) : p ▹ (f x) = f y := eq.rec_on p idp /- calc enviroment -/ calc_subst transport calc_trans concat calc_refl refl calc_symm inverse /- More theorems for moving things around in equations -/ definition tr_eq_of_eq_inv_tr (P : A → Type) {x y : A} (p : x = y) (u : P x) (v : P y) : u = p⁻¹ ▹ v → p ▹ u = v := eq.rec_on p (take v, id) v definition inv_tr_eq_of_eq_tr (P : A → Type) {x y : A} (p : y = x) (u : P x) (v : P y) : u = p ▹ v → p⁻¹ ▹ u = v := eq.rec_on p (take u, id) u definition eq_inv_tr_of_tr_eq (P : A → Type) {x y : A} (p : x = y) (u : P x) (v : P y) : p ▹ u = v → u = p⁻¹ ▹ v := eq.rec_on p (take v, id) v definition eq_tr_of_inv_tr_eq (P : A → Type) {x y : A} (p : y = x) (u : P x) (v : P y) : p⁻¹ ▹ u = v → u = p ▹ v := eq.rec_on p (take u, id) u /- Functoriality of functions -/ -- Here we prove that functions behave like functors between groupoids, and that [ap] itself is -- functorial. -- Functions take identity paths to identity paths definition ap_idp (x : A) (f : A → B) : (ap f idp) = idp :> (f x = f x) := idp definition apD_idp (x : A) (f : Π x : A, P x) : apD f idp = idp :> (f x = f x) := idp -- Functions commute with concatenation. definition ap_con (f : A → B) {x y z : A} (p : x = y) (q : y = z) : ap f (p ⬝ q) = (ap f p) ⬝ (ap f q) := eq.rec_on q (eq.rec_on p idp) definition con_ap_con_eq_con_ap_con_ap (f : A → B) {w x y z : A} (r : f w = f x) (p : x = y) (q : y = z) : r ⬝ (ap f (p ⬝ q)) = (r ⬝ ap f p) ⬝ (ap f q) := eq.rec_on q (take p, eq.rec_on p (con.assoc' r idp idp)) p definition ap_con_con_eq_ap_con_ap_con (f : A → B) {w x y z : A} (p : x = y) (q : y = z) (r : f z = f w) : (ap f (p ⬝ q)) ⬝ r = (ap f p) ⬝ (ap f q ⬝ r) := eq.rec_on q (eq.rec_on p (take r, con.assoc _ _ _)) r -- Functions commute with path inverses. definition ap_inv' (f : A → B) {x y : A} (p : x = y) : (ap f p)⁻¹ = ap f p⁻¹ := eq.rec_on p idp definition ap_inv {A B : Type} (f : A → B) {x y : A} (p : x = y) : ap f p⁻¹ = (ap f p)⁻¹ := eq.rec_on p idp -- [ap] itself is functorial in the first argument. definition ap_id (p : x = y) : ap id p = p := eq.rec_on p idp definition ap_compose (f : A → B) (g : B → C) {x y : A} (p : x = y) : ap (g ∘ f) p = ap g (ap f p) := eq.rec_on p idp -- Sometimes we don't have the actual function [compose]. definition ap_compose' (f : A → B) (g : B → C) {x y : A} (p : x = y) : ap (λa, g (f a)) p = ap g (ap f p) := eq.rec_on p idp -- The action of constant maps. definition ap_constant (p : x = y) (z : B) : ap (λu, z) p = idp := eq.rec_on p idp -- Naturality of [ap]. definition ap_con_eq_con_ap {f g : A → B} (p : Π x, f x = g x) {x y : A} (q : x = y) : (ap f q) ⬝ (p y) = (p x) ⬝ (ap g q) := eq.rec_on q (idp_con _ ⬝ (con_idp _)⁻¹) -- Naturality of [ap] at identity. definition ap_con_eq_con {f : A → A} (p : Πx, f x = x) {x y : A} (q : x = y) : (ap f q) ⬝ (p y) = (p x) ⬝ q := eq.rec_on q (idp_con _ ⬝ (con_idp _)⁻¹) definition con_ap_eq_con {f : A → A} (p : Πx, x = f x) {x y : A} (q : x = y) : (p x) ⬝ (ap f q) = q ⬝ (p y) := eq.rec_on q (con_idp _ ⬝ (idp_con _)⁻¹) -- Naturality with other paths hanging around. definition con_ap_con_con_eq_con_con_ap_con {f g : A → B} (p : f ∼ g) {x y : A} (q : x = y) {w z : B} (r : w = f x) (s : g y = z) : (r ⬝ ap f q) ⬝ (p y ⬝ s) = (r ⬝ p x) ⬝ (ap g q ⬝ s) := eq.rec_on s (eq.rec_on q idp) definition con_ap_con_eq_con_con_ap {f g : A → B} (p : f ∼ g) {x y : A} (q : x = y) {w : B} (r : w = f x) : (r ⬝ ap f q) ⬝ p y = (r ⬝ p x) ⬝ ap g q := eq.rec_on q idp -- TODO: try this using the simplifier, and compare proofs definition ap_con_con_eq_con_ap_con {f g : A → B} (p : f ∼ g) {x y : A} (q : x = y) {z : B} (s : g y = z) : (ap f q) ⬝ (p y ⬝ s) = (p x) ⬝ (ap g q ⬝ s) := eq.rec_on s (eq.rec_on q (calc (ap f idp) ⬝ (p x ⬝ idp) = idp ⬝ p x : idp ... = p x : idp_con _ ... = (p x) ⬝ (ap g idp ⬝ idp) : idp)) -- This also works: -- eq.rec_on s (eq.rec_on q (idp_con _ ▹ idp)) definition con_ap_con_con_eq_con_con_con {f : A → A} (p : f ∼ id) {x y : A} (q : x = y) {w z : A} (r : w = f x) (s : y = z) : (r ⬝ ap f q) ⬝ (p y ⬝ s) = (r ⬝ p x) ⬝ (q ⬝ s) := eq.rec_on s (eq.rec_on q idp) definition con_con_ap_con_eq_con_con_con {g : A → A} (p : id ∼ g) {x y : A} (q : x = y) {w z : A} (r : w = x) (s : g y = z) : (r ⬝ p x) ⬝ (ap g q ⬝ s) = (r ⬝ q) ⬝ (p y ⬝ s) := eq.rec_on s (eq.rec_on q idp) definition con_ap_con_eq_con_con {f : A → A} (p : f ∼ id) {x y : A} (q : x = y) {w : A} (r : w = f x) : (r ⬝ ap f q) ⬝ p y = (r ⬝ p x) ⬝ q := eq.rec_on q idp definition ap_con_con_eq_con_con {f : A → A} (p : f ∼ id) {x y : A} (q : x = y) {z : A} (s : y = z) : (ap f q) ⬝ (p y ⬝ s) = (p x) ⬝ (q ⬝ s) := eq.rec_on s (eq.rec_on q (idp_con _ ▹ idp)) definition con_con_ap_eq_con_con {g : A → A} (p : id ∼ g) {x y : A} (q : x = y) {w : A} (r : w = x) : (r ⬝ p x) ⬝ ap g q = (r ⬝ q) ⬝ p y := eq.rec_on q idp definition con_ap_con_eq_con_con' {g : A → A} (p : id ∼ g) {x y : A} (q : x = y) {z : A} (s : g y = z) : p x ⬝ (ap g q ⬝ s) = q ⬝ (p y ⬝ s) := begin apply (eq.rec_on s), apply (eq.rec_on q), apply (idp_con (p x) ▹ idp) end /- Action of [apD10] and [ap10] on paths -/ -- Application of paths between functions preserves the groupoid structure definition apD10_idp (f : Πx, P x) (x : A) : apD10 (refl f) x = idp := idp definition apD10_con {f f' f'' : Πx, P x} (h : f = f') (h' : f' = f'') (x : A) : apD10 (h ⬝ h') x = apD10 h x ⬝ apD10 h' x := eq.rec_on h (take h', eq.rec_on h' idp) h' definition apD10_inv {f g : Πx : A, P x} (h : f = g) (x : A) : apD10 h⁻¹ x = (apD10 h x)⁻¹ := eq.rec_on h idp definition ap10_idp {f : A → B} (x : A) : ap10 (refl f) x = idp := idp definition ap10_con {f f' f'' : A → B} (h : f = f') (h' : f' = f'') (x : A) : ap10 (h ⬝ h') x = ap10 h x ⬝ ap10 h' x := apD10_con h h' x definition ap10_inv {f g : A → B} (h : f = g) (x : A) : ap10 h⁻¹ x = (ap10 h x)⁻¹ := apD10_inv h x -- [ap10] also behaves nicely on paths produced by [ap] definition ap_ap10 (f g : A → B) (h : B → C) (p : f = g) (a : A) : ap h (ap10 p a) = ap10 (ap (λ f', h ∘ f') p) a:= eq.rec_on p idp /- Transport and the groupoid structure of paths -/ definition tr_idp (P : A → Type) {x : A} (u : P x) : idp ▹ u = u := idp definition tr_con (P : A → Type) {x y z : A} (p : x = y) (q : y = z) (u : P x) : p ⬝ q ▹ u = q ▹ p ▹ u := eq.rec_on q (eq.rec_on p idp) definition tr_inv_tr (P : A → Type) {x y : A} (p : x = y) (z : P y) : p ▹ p⁻¹ ▹ z = z := (tr_con P p⁻¹ p z)⁻¹ ⬝ ap (λr, transport P r z) (con.left_inv p) definition inv_tr_tr (P : A → Type) {x y : A} (p : x = y) (z : P x) : p⁻¹ ▹ p ▹ z = z := (tr_con P p p⁻¹ z)⁻¹ ⬝ ap (λr, transport P r z) (con.right_inv p) definition tr_con_lemma (P : A → Type) {x y z w : A} (p : x = y) (q : y = z) (r : z = w) (u : P x) : ap (λe, e ▹ u) (con.assoc' p q r) ⬝ (tr_con P (p ⬝ q) r u) ⬝ ap (transport P r) (tr_con P p q u) = (tr_con P p (q ⬝ r) u) ⬝ (tr_con P q r (p ▹ u)) :> ((p ⬝ (q ⬝ r)) ▹ u = r ▹ q ▹ p ▹ u) := eq.rec_on r (eq.rec_on q (eq.rec_on p idp)) -- Here is another coherence lemma for transport. definition tr_inv_tr_lemma (P : A → Type) {x y : A} (p : x = y) (z : P x) : tr_inv_tr P p (transport P p z) = ap (transport P p) (inv_tr_tr P p z) := eq.rec_on p idp -- Dependent transport in a doubly dependent type. -- should P, Q and y all be explicit here? definition transportD (P : A → Type) (Q : Π a : A, P a → Type) {a a' : A} (p : a = a') (b : P a) (z : Q a b) : Q a' (p ▹ b) := eq.rec_on p z -- In Coq the variables B, C and y are explicit, but in Lean we can probably have them implicit using the following notation notation p `▹D`:65 x:64 := transportD _ _ p _ x -- Transporting along higher-dimensional paths definition transport2 (P : A → Type) {x y : A} {p q : x = y} (r : p = q) (z : P x) : p ▹ z = q ▹ z := ap (λp', p' ▹ z) r notation p `▹2`:65 x:64 := transport2 _ p _ x -- An alternative definition. definition tr2_eq_ap10 (Q : A → Type) {x y : A} {p q : x = y} (r : p = q) (z : Q x) : transport2 Q r z = ap10 (ap (transport Q) r) z := eq.rec_on r idp definition tr2_con (P : A → Type) {x y : A} {p1 p2 p3 : x = y} (r1 : p1 = p2) (r2 : p2 = p3) (z : P x) : transport2 P (r1 ⬝ r2) z = transport2 P r1 z ⬝ transport2 P r2 z := eq.rec_on r1 (eq.rec_on r2 idp) definition tr2_inv (Q : A → Type) {x y : A} {p q : x = y} (r : p = q) (z : Q x) : transport2 Q r⁻¹ z = (transport2 Q r z)⁻¹ := eq.rec_on r idp definition transportD2 (B C : A → Type) (D : Π(a:A), B a → C a → Type) {x1 x2 : A} (p : x1 = x2) (y : B x1) (z : C x1) (w : D x1 y z) : D x2 (p ▹ y) (p ▹ z) := eq.rec_on p w notation p `▹D2`:65 x:64 := transportD2 _ _ _ p _ _ x definition ap_tr_con_tr2 (P : A → Type) {x y : A} {p q : x = y} {z w : P x} (r : p = q) (s : z = w) : ap (transport P p) s ⬝ transport2 P r w = transport2 P r z ⬝ ap (transport P q) s := eq.rec_on r (con_idp _ ⬝ (idp_con _)⁻¹) definition fn_tr_eq_tr_fn {P Q : A → Type} {x y : A} (p : x = y) (f : Πx, P x → Q x) (z : P x) : f y (p ▹ z) = (p ▹ (f x z)) := eq.rec_on p idp /- Transporting in particular fibrations -/ /- From the Coq HoTT library: One frequently needs lemmas showing that transport in a certain dependent type is equal to some more explicitly defined operation, defined according to the structure of that dependent type. For most dependent types, we prove these lemmas in the appropriate file in the types/ subdirectory. Here we consider only the most basic cases. -/ -- Transporting in a constant fibration. definition tr_constant (p : x = y) (z : B) : transport (λx, B) p z = z := eq.rec_on p idp definition tr2_constant {p q : x = y} (r : p = q) (z : B) : tr_constant p z = transport2 (λu, B) r z ⬝ tr_constant q z := eq.rec_on r (idp_con _)⁻¹ -- Transporting in a pulled back fibration. -- TODO: P can probably be implicit -- rename: tr_compose definition transport_compose (P : B → Type) (f : A → B) (p : x = y) (z : P (f x)) : transport (P ∘ f) p z = transport P (ap f p) z := eq.rec_on p idp definition ap_precompose (f : A → B) (g g' : B → C) (p : g = g') : ap (λh, h ∘ f) p = transport (λh : B → C, g ∘ f = h ∘ f) p idp := eq.rec_on p idp definition apD10_ap_precompose (f : A → B) (g g' : B → C) (p : g = g') (a : A) : apD10 (ap (λh : B → C, h ∘ f) p) a = apD10 p (f a) := eq.rec_on p idp definition apD10_ap_postcompose (f : B → C) (g g' : A → B) (p : g = g') (a : A) : apD10 (ap (λh : A → B, f ∘ h) p) a = ap f (apD10 p a) := eq.rec_on p idp -- A special case of [transport_compose] which seems to come up a lot. definition tr_eq_tr_id_ap (P : A → Type) x y (p : x = y) (u : P x) : transport P p u = transport id (ap P p) u := eq.rec_on p idp /- The behavior of [ap] and [apD] -/ -- In a constant fibration, [apD] reduces to [ap], modulo [transport_const]. definition apD_eq_tr_constant_con_ap (f : A → B) (p: x = y) : apD f p = tr_constant p (f x) ⬝ ap f p := eq.rec_on p idp /- The 2-dimensional groupoid structure -/ -- Horizontal composition of 2-dimensional paths. definition concat2 {p p' : x = y} {q q' : y = z} (h : p = p') (h' : q = q') : p ⬝ q = p' ⬝ q' := eq.rec_on h (eq.rec_on h' idp) infixl `◾`:75 := concat2 -- 2-dimensional path inversion definition inverse2 {p q : x = y} (h : p = q) : p⁻¹ = q⁻¹ := eq.rec_on h idp /- Whiskering -/ definition whisker_left (p : x = y) {q r : y = z} (h : q = r) : p ⬝ q = p ⬝ r := idp ◾ h definition whisker_right {p q : x = y} (h : p = q) (r : y = z) : p ⬝ r = q ⬝ r := h ◾ idp -- Unwhiskering, a.k.a. cancelling definition cancel_left {x y z : A} (p : x = y) (q r : y = z) : (p ⬝ q = p ⬝ r) → (q = r) := λs, !inv_con_cancel_left⁻¹ ⬝ whisker_left p⁻¹ s ⬝ !inv_con_cancel_left definition cancel_right {x y z : A} (p q : x = y) (r : y = z) : (p ⬝ r = q ⬝ r) → (p = q) := λs, !con_inv_cancel_right⁻¹ ⬝ whisker_right s r⁻¹ ⬝ !con_inv_cancel_right -- Whiskering and identity paths. definition whisker_right_idp_right {p q : x = y} (h : p = q) : (con_idp p)⁻¹ ⬝ whisker_right h idp ⬝ con_idp q = h := eq.rec_on h (eq.rec_on p idp) definition whisker_right_idp_left (p : x = y) (q : y = z) : whisker_right idp q = idp :> (p ⬝ q = p ⬝ q) := eq.rec_on q idp definition whisker_left_idp_right (p : x = y) (q : y = z) : whisker_left p idp = idp :> (p ⬝ q = p ⬝ q) := eq.rec_on q idp definition whisker_left_idp_left {p q : x = y} (h : p = q) : (idp_con p) ⁻¹ ⬝ whisker_left idp h ⬝ idp_con q = h := eq.rec_on h (eq.rec_on p idp) definition con2_idp {p q : x = y} (h : p = q) : h ◾ idp = whisker_right h idp :> (p ⬝ idp = q ⬝ idp) := idp definition idp_con2 {p q : x = y} (h : p = q) : idp ◾ h = whisker_left idp h :> (idp ⬝ p = idp ⬝ q) := idp -- The interchange law for concatenation. definition con2_con_con2 {p p' p'' : x = y} {q q' q'' : y = z} (a : p = p') (b : p' = p'') (c : q = q') (d : q' = q'') : (a ◾ c) ⬝ (b ◾ d) = (a ⬝ b) ◾ (c ⬝ d) := eq.rec_on d (eq.rec_on c (eq.rec_on b (eq.rec_on a idp))) definition whisker_right_con_whisker_left {x y z : A} (p p' : x = y) (q q' : y = z) (a : p = p') (b : q = q') : (whisker_right a q) ⬝ (whisker_left p' b) = (whisker_left p b) ⬝ (whisker_right a q') := eq.rec_on b (eq.rec_on a (idp_con _)⁻¹) -- Structure corresponding to the coherence equations of a bicategory. -- The "pentagonator": the 3-cell witnessing the associativity pentagon. definition pentagon {v w x y z : A} (p : v = w) (q : w = x) (r : x = y) (s : y = z) : whisker_left p (con.assoc' q r s) ⬝ con.assoc' p (q ⬝ r) s ⬝ whisker_right (con.assoc' p q r) s = con.assoc' p q (r ⬝ s) ⬝ con.assoc' (p ⬝ q) r s := eq.rec_on s (eq.rec_on r (eq.rec_on q (eq.rec_on p idp))) -- The 3-cell witnessing the left unit triangle. definition triangulator (p : x = y) (q : y = z) : con.assoc' p idp q ⬝ whisker_right (con_idp p) q = whisker_left p (idp_con q) := eq.rec_on q (eq.rec_on p idp) definition eckmann_hilton {x:A} (p q : idp = idp :> (x = x)) : p ⬝ q = q ⬝ p := (!whisker_right_idp_right ◾ !whisker_left_idp_left)⁻¹ ⬝ (!con_idp ◾ !con_idp) ⬝ (!idp_con ◾ !idp_con) ⬝ !whisker_right_con_whisker_left ⬝ (!idp_con ◾ !idp_con)⁻¹ ⬝ (!con_idp ◾ !con_idp)⁻¹ ⬝ (!whisker_left_idp_left ◾ !whisker_right_idp_right) -- The action of functions on 2-dimensional paths definition ap02 (f:A → B) {x y : A} {p q : x = y} (r : p = q) : ap f p = ap f q := eq.rec_on r idp definition ap02_con (f : A → B) {x y : A} {p p' p'' : x = y} (r : p = p') (r' : p' = p'') : ap02 f (r ⬝ r') = ap02 f r ⬝ ap02 f r' := eq.rec_on r (eq.rec_on r' idp) definition ap02_con2 (f : A → B) {x y z : A} {p p' : x = y} {q q' :y = z} (r : p = p') (s : q = q') : ap02 f (r ◾ s) = ap_con f p q ⬝ (ap02 f r ◾ ap02 f s) ⬝ (ap_con f p' q')⁻¹ := eq.rec_on r (eq.rec_on s (eq.rec_on q (eq.rec_on p idp))) definition apD02 {p q : x = y} (f : Π x, P x) (r : p = q) : apD f p = transport2 P r (f x) ⬝ apD f q := eq.rec_on r (idp_con _)⁻¹ -- And now for a lemma whose statement is much longer than its proof. definition apD02_con (P : A → Type) (f : Π x:A, P x) {x y : A} {p1 p2 p3 : x = y} (r1 : p1 = p2) (r2 : p2 = p3) : apD02 f (r1 ⬝ r2) = apD02 f r1 ⬝ whisker_left (transport2 P r1 (f x)) (apD02 f r2) ⬝ con.assoc' _ _ _ ⬝ (whisker_right (tr2_con P r1 r2 (f x))⁻¹ (apD f p3)) := eq.rec_on r2 (eq.rec_on r1 (eq.rec_on p1 idp)) end eq
d5c849f40960b444215c9362b60fab9ec24b5f09
3d2a7f1582fe5bae4d0bdc2fe86e997521239a65
/misc/family1.lean
db18d7701048c11fb1ac29187d121747945ecd86
[]
no_license
own-pt/common-sense-lean
e4fa643ae010459de3d1bf673be7cbc7062563c9
f672210aecb4172f5bae265e43e6867397e13b1c
refs/heads/master
1,622,065,660,261
1,589,487,533,000
1,589,487,533,000
254,167,782
3
2
null
1,589,487,535,000
1,586,370,214,000
Lean
UTF-8
Lean
false
false
354
lean
/- From adam's serie: https://www.youtube.com/watch?v=SkruxPmN0kk https://www.youtube.com/watch?v=nvHatVfiLPU -/ constants Entity Hominid Human : Type constant isHominid : Entity → Prop constant hasHair : Entity → Prop def isHuman (x : Entity) : Prop := isHominid x ∧ hasHair x constant hasBirth : { x : Entity // isHominid x } → Prop
23559791a4d9bd860cbc1af33fa58dc5bb24938e
7cef822f3b952965621309e88eadf618da0c8ae9
/src/topology/separation.lean
09afd5ce5d95ed2d05e7d0d4e9171442e5080921
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
16,770
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro Separation properties of topological spaces. -/ import topology.subset_properties open set filter lattice open_locale topological_space local attribute [instance] classical.prop_decidable -- TODO: use "open_locale classical" universes u v variables {α : Type u} {β : Type v} [topological_space α] section separation /-- A T₀ space, also known as a Kolmogorov space, is a topological space where for every pair `x ≠ y`, there is an open set containing one but not the other. -/ class t0_space (α : Type u) [topological_space α] : Prop := (t0 : ∀ x y, x ≠ y → ∃ U:set α, is_open U ∧ (xor (x ∈ U) (y ∈ U))) theorem exists_open_singleton_of_fintype [t0_space α] [f : fintype α] [decidable_eq α] [ha : nonempty α] : ∃ x:α, is_open ({x}:set α) := have H : ∀ (T : finset α), T ≠ ∅ → ∃ x ∈ T, ∃ u, is_open u ∧ {x} = {y | y ∈ T} ∩ u := begin intro T, apply finset.case_strong_induction_on T, { intro h, exact (h rfl).elim }, { intros x S hxS ih h, by_cases hs : S = ∅, { existsi [x, finset.mem_insert_self x S, univ, is_open_univ], rw [hs, inter_univ], refl }, { rcases ih S (finset.subset.refl S) hs with ⟨y, hy, V, hv1, hv2⟩, by_cases hxV : x ∈ V, { cases t0_space.t0 x y (λ hxy, hxS $ by rwa hxy) with U hu, rcases hu with ⟨hu1, ⟨hu2, hu3⟩ | ⟨hu2, hu3⟩⟩, { existsi [x, finset.mem_insert_self x S, U ∩ V, is_open_inter hu1 hv1], apply set.ext, intro z, split, { intro hzx, rw set.mem_singleton_iff at hzx, rw hzx, exact ⟨finset.mem_insert_self x S, ⟨hu2, hxV⟩⟩ }, { intro hz, rw set.mem_singleton_iff, rcases hz with ⟨hz1, hz2, hz3⟩, cases finset.mem_insert.1 hz1 with hz4 hz4, { exact hz4 }, { have h1 : z ∈ {y : α | y ∈ S} ∩ V, { exact ⟨hz4, hz3⟩ }, rw ← hv2 at h1, rw set.mem_singleton_iff at h1, rw h1 at hz2, exact (hu3 hz2).elim } } }, { existsi [y, finset.mem_insert_of_mem hy, U ∩ V, is_open_inter hu1 hv1], apply set.ext, intro z, split, { intro hz, rw set.mem_singleton_iff at hz, rw hz, refine ⟨finset.mem_insert_of_mem hy, hu2, _⟩, have h1 : y ∈ {y} := set.mem_singleton y, rw hv2 at h1, exact h1.2 }, { intro hz, rw set.mem_singleton_iff, cases hz with hz1 hz2, cases finset.mem_insert.1 hz1 with hz3 hz3, { rw hz3 at hz2, exact (hu3 hz2.1).elim }, { have h1 : z ∈ {y : α | y ∈ S} ∩ V := ⟨hz3, hz2.2⟩, rw ← hv2 at h1, rw set.mem_singleton_iff at h1, exact h1 } } } }, { existsi [y, finset.mem_insert_of_mem hy, V, hv1], apply set.ext, intro z, split, { intro hz, rw set.mem_singleton_iff at hz, rw hz, split, { exact finset.mem_insert_of_mem hy }, { have h1 : y ∈ {y} := set.mem_singleton y, rw hv2 at h1, exact h1.2 } }, { intro hz, rw hv2, cases hz with hz1 hz2, cases finset.mem_insert.1 hz1 with hz3 hz3, { rw hz3 at hz2, exact (hxV hz2).elim }, { exact ⟨hz3, hz2⟩ } } } } } end, begin apply nonempty.elim ha, intro x, specialize H finset.univ (finset.ne_empty_of_mem $ finset.mem_univ x), rcases H with ⟨y, hyf, U, hu1, hu2⟩, existsi y, have h1 : {y : α | y ∈ finset.univ} = (univ : set α), { exact set.eq_univ_of_forall (λ x : α, by rw mem_set_of_eq; exact finset.mem_univ x) }, rw h1 at hu2, rw set.univ_inter at hu2, rw hu2, exact hu1 end /-- A T₁ space, also known as a Fréchet space, is a topological space where every singleton set is closed. Equivalently, for every pair `x ≠ y`, there is an open set containing `x` and not `y`. -/ class t1_space (α : Type u) [topological_space α] : Prop := (t1 : ∀x, is_closed ({x} : set α)) lemma is_closed_singleton [t1_space α] {x : α} : is_closed ({x} : set α) := t1_space.t1 x @[priority 100] -- see Note [lower instance priority] instance t1_space.t0_space [t1_space α] : t0_space α := ⟨λ x y h, ⟨-{x}, is_open_compl_iff.2 is_closed_singleton, or.inr ⟨λ hyx, or.cases_on hyx h.symm id, λ hx, hx $ or.inl rfl⟩⟩⟩ lemma compl_singleton_mem_nhds [t1_space α] {x y : α} (h : y ≠ x) : - {x} ∈ 𝓝 y := mem_nhds_sets is_closed_singleton $ by rwa [mem_compl_eq, mem_singleton_iff] @[simp] lemma closure_singleton [t1_space α] {a : α} : closure ({a} : set α) = {a} := closure_eq_of_is_closed is_closed_singleton /-- A T₂ space, also known as a Hausdorff space, is one in which for every `x ≠ y` there exists disjoint open sets around `x` and `y`. This is the most widely used of the separation axioms. -/ class t2_space (α : Type u) [topological_space α] : Prop := (t2 : ∀x y, x ≠ y → ∃u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅) lemma t2_separation [t2_space α] {x y : α} (h : x ≠ y) : ∃u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅ := t2_space.t2 x y h @[priority 100] -- see Note [lower instance priority] instance t2_space.t1_space [t2_space α] : t1_space α := ⟨λ x, is_open_iff_forall_mem_open.2 $ λ y hxy, let ⟨u, v, hu, hv, hyu, hxv, huv⟩ := t2_separation (mt mem_singleton_of_eq hxy) in ⟨u, λ z hz1 hz2, ((ext_iff _ _).1 huv x).1 ⟨mem_singleton_iff.1 hz2 ▸ hz1, hxv⟩, hu, hyu⟩⟩ lemma eq_of_nhds_neq_bot [ht : t2_space α] {x y : α} (h : 𝓝 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 have u ∩ v ∈ 𝓝 x ⊓ 𝓝 y, from inter_mem_inf_sets (mem_nhds_sets hu hx) (mem_nhds_sets hv hy), h $ empty_in_sets_eq_bot.mp $ huv ▸ this lemma t2_iff_nhds : t2_space α ↔ ∀ {x y : α}, 𝓝 x ⊓ 𝓝 y ≠ ⊥ → x = y := ⟨assume h, by exactI λ x y, eq_of_nhds_neq_bot, assume h, ⟨assume x y xy, have 𝓝 x ⊓ 𝓝 y = ⊥ := classical.by_contradiction (mt h xy), let ⟨u', hu', v', hv', u'v'⟩ := empty_in_sets_eq_bot.mpr this, ⟨u, uu', uo, hu⟩ := mem_nhds_sets_iff.mp hu', ⟨v, vv', vo, hv⟩ := mem_nhds_sets_iff.mp hv' in ⟨u, v, uo, vo, hu, hv, disjoint.eq_bot $ disjoint_mono uu' vv' u'v'⟩⟩⟩ lemma t2_iff_ultrafilter : t2_space α ↔ ∀ f {x y : α}, is_ultrafilter f → f ≤ 𝓝 x → f ≤ 𝓝 y → x = y := t2_iff_nhds.trans ⟨assume h f x y u fx fy, h $ neq_bot_of_le_neq_bot u.1 (le_inf fx fy), assume h x y xy, let ⟨f, hf, uf⟩ := exists_ultrafilter xy in h f uf (le_trans hf lattice.inf_le_left) (le_trans hf lattice.inf_le_right)⟩ @[simp] lemma nhds_eq_nhds_iff {a b : α} [t2_space α] : 𝓝 a = 𝓝 b ↔ a = b := ⟨assume h, eq_of_nhds_neq_bot $ by rw [h, inf_idem]; exact nhds_neq_bot, assume h, h ▸ rfl⟩ @[simp] lemma nhds_le_nhds_iff {a b : α} [t2_space α] : 𝓝 a ≤ 𝓝 b ↔ a = b := ⟨assume h, eq_of_nhds_neq_bot $ by rw [inf_of_le_left h]; exact nhds_neq_bot, assume h, h ▸ le_refl _⟩ lemma tendsto_nhds_unique [t2_space α] {f : β → α} {l : filter β} {a b : α} (hl : l ≠ ⊥) (ha : tendsto f l (𝓝 a)) (hb : tendsto f l (𝓝 b)) : a = b := eq_of_nhds_neq_bot $ neq_bot_of_le_neq_bot (map_ne_bot hl) $ le_inf ha hb section lim variables [inhabited α] [t2_space α] {f : filter α} lemma lim_eq {a : α} (hf : f ≠ ⊥) (h : f ≤ 𝓝 a) : lim f = a := eq_of_nhds_neq_bot $ neq_bot_of_le_neq_bot hf $ le_inf (lim_spec ⟨_, h⟩) h @[simp] lemma lim_nhds_eq {a : α} : lim (𝓝 a) = a := lim_eq nhds_neq_bot (le_refl _) @[simp] lemma lim_nhds_eq_of_closure {a : α} {s : set α} (h : a ∈ closure s) : lim (𝓝 a ⊓ principal s) = a := lim_eq begin rw [closure_eq_nhds] at h, exact h end inf_le_left end lim @[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 _, mem_insert _ _, mem_insert _ _, eq_empty_iff_forall_not_mem.2 $ by intros z hz; cases eq_of_mem_singleton hz.1; cases eq_of_mem_singleton hz.2; cc⟩ } private lemma separated_by_f {α : Type*} {β : Type*} [tα : topological_space α] [tβ : topological_space β] [t2_space β] (f : α → β) (hf : tα ≤ tβ.induced 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, hf _ ⟨u, uo, rfl⟩, hf _ ⟨v, vo, rfl⟩, xu, yv, by rw [←preimage_inter, uv, preimage_empty]⟩ instance {α : Type*} {p : α → Prop} [t : topological_space α] [t2_space α] : t2_space (subtype p) := ⟨assume x y h, separated_by_f subtype.val (le_refl _) (mt subtype.eq h)⟩ instance {α : 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_f prod.fst inf_le_left h₁) (λ h₂, separated_by_f prod.snd inf_le_right h₂)⟩ 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_f (λz, z i) (infi_le _ i) hi⟩ lemma is_closed_diagonal [t2_space α] : is_closed {p:α×α | p.1 = p.2} := is_closed_iff_nhds.mpr $ assume ⟨a₁, a₂⟩ h, eq_of_nhds_neq_bot $ assume : 𝓝 a₁ ⊓ 𝓝 a₂ = ⊥, h $ let ⟨t₁, ht₁, t₂, ht₂, (h' : t₁ ∩ t₂ ⊆ ∅)⟩ := by rw [←empty_in_sets_eq_bot, mem_inf_sets] at this; exact this in begin change t₁ ∈ 𝓝 a₁ at ht₁, change t₂ ∈ 𝓝 a₂ at ht₂, rw [nhds_prod_eq, ←empty_in_sets_eq_bot], apply filter.sets_of_superset, apply inter_mem_inf_sets (prod_mem_prod ht₁ ht₂) (mem_principal_sets.mpr (subset.refl _)), exact assume ⟨x₁, x₂⟩ ⟨⟨hx₁, hx₂⟩, (heq : x₁ = x₂)⟩, show false, from @h' x₁ ⟨hx₁, heq.symm ▸ hx₂⟩ end 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 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 : compact s) (ht : compact t) (hst : s ∩ t = ∅) : ∃u v : set α, is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ u ∩ v = ∅ := by simp only [prod_subset_compl_diagonal_iff_disjoint.symm] at ⊢ hst; exact generalized_tube_lemma hs ht is_closed_diagonal hst lemma closed_of_compact [t2_space α] (s : set α) (hs : compact s) : is_closed s := is_open_compl_iff.mpr $ is_open_iff_forall_mem_open.mpr $ assume x hx, let ⟨u, v, uo, vo, su, xv, uv⟩ := compact_compact_separated hs (compact_singleton : compact {x}) (by rwa [inter_comm, ←subset_compl_iff_disjoint, singleton_subset_iff]) in have v ⊆ -s, from subset_compl_comm.mp (subset.trans su (subset_compl_iff_disjoint.mpr uv)), ⟨v, this, vo, by simpa using xv⟩ lemma locally_compact_of_compact_nhds [t2_space α] (h : ∀ x : α, ∃ s, s ∈ 𝓝 x ∧ compact s) : locally_compact_space α := ⟨assume x n hn, let ⟨u, un, uo, xu⟩ := mem_nhds_sets_iff.mp hn in let ⟨k, kx, kc⟩ := h x in -- K is compact but not necessarily contained in N. -- K \ U is again compact and doesn't contain x, so -- we may find open sets V, W separating x from K \ U. -- Then K \ W is a compact neighborhood of x contained in U. let ⟨v, w, vo, wo, xv, kuw, vw⟩ := compact_compact_separated compact_singleton (compact_diff kc uo) (by rw [singleton_inter_eq_empty]; exact λ h, h.2 xu) in have wn : -w ∈ 𝓝 x, from mem_nhds_sets_iff.mpr ⟨v, subset_compl_iff_disjoint.mpr vw, vo, singleton_subset_iff.mp xv⟩, ⟨k - w, filter.inter_mem_sets kx wn, subset.trans (diff_subset_comm.mp kuw) un, compact_diff kc wo⟩⟩ @[priority 100] -- see Note [lower instance priority] instance locally_compact_of_compact [t2_space α] [compact_space α] : locally_compact_space α := locally_compact_of_compact_nhds (assume x, ⟨univ, mem_nhds_sets is_open_univ trivial, compact_univ⟩) end separation section regularity section prio set_option default_priority 100 -- see Note [default priority] /-- A T₃ space, also known as a regular space (although this condition sometimes omits T₂), is one in which for every closed `C` and `x ∉ C`, there exist disjoint open sets containing `x` and `C` respectively. -/ class regular_space (α : Type u) [topological_space α] extends t1_space α : Prop := (regular : ∀{s:set α} {a}, is_closed s → a ∉ s → ∃t, is_open t ∧ s ⊆ t ∧ 𝓝 a ⊓ principal t = ⊥) end prio lemma nhds_is_closed [regular_space α] {a : α} {s : set α} (h : s ∈ 𝓝 a) : ∃t∈(𝓝 a), t ⊆ s ∧ is_closed t := let ⟨s', h₁, h₂, h₃⟩ := mem_nhds_sets_iff.mp h in have ∃t, is_open t ∧ -s' ⊆ t ∧ 𝓝 a ⊓ principal t = ⊥, from regular_space.regular (is_closed_compl_iff.mpr h₂) (not_not_intro h₃), let ⟨t, ht₁, ht₂, ht₃⟩ := this in ⟨-t, mem_sets_of_neq_bot $ by rwa [lattice.neg_neg], subset.trans (compl_subset_comm.1 ht₂) h₁, is_closed_compl_iff.mpr ht₁⟩ variable (α) @[priority 100] -- see Note [lower instance priority] instance regular_space.t2_space [regular_space α] : t2_space α := ⟨λ x y hxy, let ⟨s, hs, hys, hxs⟩ := regular_space.regular is_closed_singleton (mt mem_singleton_iff.1 hxy), ⟨t, hxt, u, hsu, htu⟩ := empty_in_sets_eq_bot.2 hxs, ⟨v, hvt, hv, hxv⟩ := mem_nhds_sets_iff.1 hxt in ⟨v, s, hv, hs, hxv, singleton_subset_iff.1 hys, eq_empty_of_subset_empty $ λ z ⟨hzv, hzs⟩, htu ⟨hvt hzv, hsu hzs⟩⟩⟩ end regularity section normality section prio set_option default_priority 100 -- see Note [default priority] /-- 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) end prio theorem normal_separation [normal_space α] (s t : set α) (H1 : is_closed s) (H2 : is_closed t) (H3 : disjoint s t) : ∃ u v, is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ disjoint u v := normal_space.normal s t H1 H2 H3 @[priority 100] -- see Note [lower instance priority] instance normal_space.regular_space [normal_space α] : regular_space α := { regular := λ s x hs hxs, let ⟨u, v, hu, hv, hsu, hxv, huv⟩ := normal_separation s {x} hs is_closed_singleton (λ _ ⟨hx, hy⟩, hxs $ set.mem_of_eq_of_mem (set.eq_of_mem_singleton hy).symm hx) in ⟨u, hu, hsu, filter.empty_in_sets_eq_bot.1 $ filter.mem_inf_sets.2 ⟨v, mem_nhds_sets hv (set.singleton_subset_iff.1 hxv), u, filter.mem_principal_self u, set.inter_comm u v ▸ huv⟩⟩ } -- We can't make this an instance because it could cause an instance loop. lemma normal_of_compact_t2 [compact_space α] [t2_space α] : normal_space α := begin refine ⟨assume s t hs ht st, _⟩, simp only [disjoint_iff], exact compact_compact_separated hs.compact ht.compact st.eq_bot end end normality
3535e4cd3b46d6ed783e861c443ac5dbf11a4ba5
80d0f8071ea62262937ab36f5887a61735adea09
/src/certigrad/tgrads.lean
da0099f1c55ce9555ab27020161f7326e6663a6c
[ "Apache-2.0" ]
permissive
wudcscheme/certigrad
94805fa6a61f993c69a824429a103c9613a65a48
c9a06e93f1ec58196d6d3b8563b29868d916727f
refs/heads/master
1,679,386,475,077
1,551,651,022,000
1,551,651,022,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
31,499
lean
/- Copyright (c) 2017 Daniel Selsam. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Daniel Selsam Properties of gradients. -/ import .tensor .tfacts .tactics namespace certigrad namespace T open list -- is_cdifferentiable axiom is_cdifferentiable_binary {shape : S} (k : T shape → T shape → ℝ) (θ : T shape) : is_cdifferentiable (λ θ₀, k θ₀ θ) θ → is_cdifferentiable (λ θ₀, k θ θ₀) θ → is_cdifferentiable (λ θ₀, k θ₀ θ₀) θ axiom is_cdifferentiable_multiple_args {fshape : S} (tgt : reference) (parents : list reference) (m : env) (f : dvec T parents^.p2 → T fshape) (θ : T tgt.2) (k : T fshape → ℝ) : (∀ (idx : ℕ) (H_idx_in_riota: idx ∈ riota (length parents)) (H_tgt_eq_dnth_idx : tgt = dnth parents idx), is_cdifferentiable (λ θ₀, k (f (dvec.update_at θ₀ (env.get_ks parents (env.insert tgt θ m)) idx))) θ) → is_cdifferentiable (λ θ₀, k (f (env.get_ks parents (env.insert tgt θ₀ m)))) θ axiom is_cdifferentiable_integral : ∀ {ishape tshape : S} (f : T ishape → T tshape → ℝ) (θ : T tshape), (∀ x, is_cdifferentiable (f x) θ) → is_uniformly_integrable_around (λ θ₀ x, f x θ₀) θ → is_uniformly_integrable_around (λ θ₀ x, ∇ (λ θ₁, f x θ₁) θ₀) θ → is_cdifferentiable (λ θ₀, ∫ (λ x, f x θ₀)) θ axiom is_cdifferentiable_const {ishape : S} (θ : T ishape) (x : ℝ) : is_cdifferentiable (λ (θ₀ : T ishape), x) θ axiom is_cdifferentiable_id (θ : ℝ) : is_cdifferentiable (λ (θ₀ : ℝ), θ₀) θ axiom is_cdifferentiable_exp {shape : S} (k : T shape → ℝ) (θ : T shape) : is_cdifferentiable k (exp θ) → is_cdifferentiable (λ θ, k (exp θ)) θ axiom is_cdifferentiable_log {shape : S} (k : T shape → ℝ) (θ : T shape) : θ > 0 → is_cdifferentiable k (log θ) → is_cdifferentiable (λ θ, k (log θ)) θ axiom is_cdifferentiable_sqrt {shape : S} (k : T shape → ℝ) (θ : T shape) : is_cdifferentiable k (sqrt θ) → is_cdifferentiable (λ θ, k (sqrt θ)) θ axiom is_cdifferentiable_inv {shape : S} (k : T shape → ℝ) (θ : T shape) : θ > 0 → is_cdifferentiable k θ⁻¹ → is_cdifferentiable (λ θ, k θ⁻¹) θ axiom is_cdifferentiable_scale {shape : S} (k : T shape → ℝ) (α : ℝ) (x : T shape) : is_cdifferentiable k (α ⬝ x) → is_cdifferentiable (λ x, k (α ⬝ x)) x axiom is_cdifferentiable_neg {shape : S} (k : T shape → ℝ) (θ : T shape) : is_cdifferentiable k (- θ) → is_cdifferentiable (λ θ, k (- θ)) θ axiom is_cdifferentiable_add₁ {shape : S} (k : T shape → ℝ) (x₁ x₂ : T shape) : is_cdifferentiable k (x₁ + x₂) → is_cdifferentiable (λ x₁, k (x₁ + x₂)) x₁ axiom is_cdifferentiable_add₂ {shape : S} (k : T shape → ℝ) (x₁ x₂ : T shape) : is_cdifferentiable k (x₁ + x₂) → is_cdifferentiable (λ x₂, k (x₁ + x₂)) x₂ axiom is_cdifferentiable_sub₁ {shape : S} (k : T shape → ℝ) (x₁ x₂ : T shape) : is_cdifferentiable k (x₁ - x₂) → is_cdifferentiable (λ x₁, k (x₁ - x₂)) x₁ axiom is_cdifferentiable_sub₂ {shape : S} (k : T shape → ℝ) (x₁ x₂ : T shape) : is_cdifferentiable k (x₁ - x₂) → is_cdifferentiable (λ x₂, k (x₁ - x₂)) x₂ axiom is_cdifferentiable_mul₁ {shape : S} (k : T shape → ℝ) (x₁ x₂ : T shape) : is_cdifferentiable k (x₁ * x₂) → is_cdifferentiable (λ x₁, k (x₁ * x₂)) x₁ axiom is_cdifferentiable_mul₂ {shape : S} (k : T shape → ℝ) (x₁ x₂ : T shape) : is_cdifferentiable k (x₁ * x₂) → is_cdifferentiable (λ x₂, k (x₁ * x₂)) x₂ axiom is_cdifferentiable_div₁ {shape : S} (k : T shape → ℝ) (x₁ x₂ : T shape) : square x₂ > 0 → is_cdifferentiable k (x₁ / x₂) → is_cdifferentiable (λ x₁, k (x₁ / x₂)) x₁ axiom is_cdifferentiable_div₂ {shape : S} (k : T shape → ℝ) (x₁ x₂ : T shape) : square x₂ > 0 → is_cdifferentiable k (x₁ / x₂) → is_cdifferentiable (λ x₂, k (x₁ / x₂)) x₂ axiom is_cdifferentiable_sum (k : ℝ → ℝ) (shape : S) (x : T shape) : is_cdifferentiable k (sum x) → is_cdifferentiable (λ x, k (sum x)) x axiom is_cdifferentiable_prod (k : ℝ → ℝ) (shape : S) (x : T shape) : is_cdifferentiable k (prod x) → is_cdifferentiable (λ x, k (prod x)) x axiom is_cdifferentiable_square {shape : S} (k : T shape → ℝ) (x : T shape) : is_cdifferentiable k (square x) → is_cdifferentiable (λ x, k (square x)) x axiom is_cdifferentiable_gemm₁ {m p : ℕ} (k : T [m, p] → ℝ) (n : ℕ) (M : T [m, n]) (N : T [n, p]) : is_cdifferentiable k (gemm M N) → is_cdifferentiable (λ M, k (gemm M N)) M axiom is_cdifferentiable_gemm₂ {m p : ℕ} (k : T [m, p] → ℝ) (n : ℕ) (M : T [m, n]) (N : T [n, p]) : is_cdifferentiable k (gemm M N) → is_cdifferentiable (λ N, k (gemm M N)) N axiom is_cdifferentiable_add_fs {shape : S} (f₁ f₂ : T shape → ℝ) (θ : T shape): (is_cdifferentiable f₁ θ ∧ is_cdifferentiable f₂ θ) ↔ is_cdifferentiable (λ θ₀, f₁ θ₀ + f₂ θ₀) θ axiom is_cdifferentiable_scale_f {shape : S} (α : ℝ) (f : T shape → ℝ) (θ : T shape): is_cdifferentiable f θ ↔ is_cdifferentiable (λ x, α ⬝ f x) θ axiom is_cdifferentiable_fscale {shape : S} (f : T shape → ℝ) (y : ℝ) (θ : T shape): is_cdifferentiable f θ ↔ is_cdifferentiable (λ x, f x ⬝ y) θ -- Provable axiom is_cdifferentiable_sumr {X : Type} {shape : S} (θ : T shape) (f : T shape → X → ℝ) : Π (xs : list X), (∀ (x : X), x ∈ xs → is_cdifferentiable (λ θ₀, f θ₀ x) θ) → is_cdifferentiable (λ (θ₀ : T shape), sumr (map (f θ₀) xs)) θ -- axiom grad_binary {shape : S} (k : T shape → T shape → ℝ) (θ : T shape) : is_cdifferentiable (λ θ₀, k θ₀ θ) θ → is_cdifferentiable (λ θ₀, k θ θ₀) θ → ∇ (λ θ₀, k θ₀ θ₀) θ = ∇ (λ θ₀, k θ₀ θ) θ + ∇ (λ θ₀, k θ θ₀) θ axiom grad_tmulT {ishape oshape : S} : ∀ (f : T ishape → T oshape) (k : T oshape → ℝ) (θ : T ishape), ∇ (λ θ₀, k (f θ₀)) θ = tmulT (D (λ θ₀, f θ₀) θ) (∇ k (f θ)) axiom grad_chain_rule : ∀ {shape₁ shape₂ : S} (f : T shape₁ → T shape₂) (g : T shape₂ → ℝ) (θ : T shape₁), ∇ (λ (θ₀ : T shape₁), g (f θ₀)) θ = tmulT (D f θ) (∇ g (f θ)) -- See Lang (Page 340, Theorem 3.4) -- f continuously differentiable -- f and grad_2 f both uniformly integrable axiom grad_integral : ∀ {ishape tshape : S} (f : T ishape → T tshape → ℝ) (θ : T tshape), (∀ x, is_cdifferentiable (f x) θ) → is_uniformly_integrable_around (λ θ₀ x, f x θ₀) θ → is_uniformly_integrable_around (λ θ₀ x, ∇ (λ θ₁, f x θ₁) θ₀) θ → ∇ (λ θ₀, ∫ (λ x, f x θ₀)) θ = ∫ (λ x, ∇ (λ θ₀, f x θ₀) θ) lemma grad_congr {shape : S} {f g : T shape → ℝ} {x : T shape} (H : ∀ x, f x = g x) : ∇ f x = ∇ g x := begin change (∇ (λ x, f x) x = ∇ (λ x, g x) x), rw (funext H) end axiom grad_const : ∀ {ishape : S} (θ : T ishape) (x : ℝ), ∇ (λ (θ₀ : T ishape), x) θ = 0 axiom grad_id : ∀ (θ : ℝ), ∇ (λ θ, θ) θ = 1 -- Unary axiom grad_exp {shape : S} (k : T shape → ℝ) (θ : T shape) : ∇ (λ θ, k (exp θ)) θ = ∇ k (exp θ) * exp θ axiom grad_log {shape : S} (k : T shape → ℝ) (θ : T shape) : θ > 0 → ∇ (λ θ, k (log θ)) θ = ∇ k (log θ) / θ axiom grad_sqrt {shape : S} (k : T shape → ℝ) (θ : T shape) : θ > 0 → ∇ (λ θ, k (sqrt θ)) θ = ∇ k (sqrt θ) / (2 * sqrt θ) axiom grad_scale {shape : S} (k : T shape → ℝ) (α : ℝ) (x : T shape) : ∇ (λ x, k (α ⬝ x)) x = α ⬝ ∇ k (α ⬝ x) axiom grad_neg {shape : S} (k : T shape → ℝ) (θ : T shape) : ∇ (λ θ, k (- θ)) θ = - (∇ k (- θ)) -- Binary axiom grad_add₁ {shape : S} (k : T shape → ℝ) (x₁ x₂ : T shape) : ∇ (λ x₁, k (x₁ + x₂)) x₁ = ∇ k (x₁ + x₂) axiom grad_add₂ {shape : S} (k : T shape → ℝ) (x₁ x₂ : T shape) : ∇ (λ x₂, k (x₁ + x₂)) x₂ = ∇ k (x₁ + x₂) axiom grad_sub₁ {shape : S} (k : T shape → ℝ) (x₁ x₂ : T shape) : ∇ (λ x₁, k (x₁ - x₂)) x₁ = ∇ k (x₁ - x₂) axiom grad_sub₂ {shape : S} (k : T shape → ℝ) (x₁ x₂ : T shape) : ∇ (λ x₂, k (x₁ - x₂)) x₂ = - ∇ k (x₁ - x₂) axiom grad_mul₁ {shape : S} (k : T shape → ℝ) (x₁ x₂ : T shape) : ∇ (λ x₁, k (x₁ * x₂)) x₁ = ∇ k (x₁ * x₂) * x₂ axiom grad_mul₂ {shape : S} (k : T shape → ℝ) (x₁ x₂ : T shape) : ∇ (λ x₂, k (x₁ * x₂)) x₂ = ∇ k (x₁ * x₂) * x₁ -- Note: can be proved from grad_binary and grad_mul*, but resulting theorem -- would have `is_cdifferentiable k` as a pre-condition. -- It is safe to avoid that here because of the symmetry of the function. axiom grad_square {shape : S} (k : T shape → ℝ) (x : T shape) : ∇ (λ x, k (square x)) x = ∇ k (square x) * 2 * x axiom grad_div₁ {shape : S} (k : T shape → ℝ) (x₁ x₂ : T shape) : square x₂ > 0 → ∇ (λ x₁, k (x₁ / x₂)) x₁ = ∇ k (x₁ / x₂) / x₂ axiom grad_div₂ {shape : S} (k : T shape → ℝ) (x₁ x₂ : T shape) : square x₂ > 0 → ∇ (λ x₂, k (x₁ / x₂)) x₂ = - (∇ k (x₁ / x₂) * x₁) / (square x₂) -- Tensors axiom grad_sum (k : ℝ → ℝ) (shape : S) (x : T shape) : ∇ (λ x, k (sum x)) x = ∇ k (sum x) ⬝ 1 axiom grad_dot₁ {shape : S} (x₁ x₂ : T shape) : ∇ (λ x₁, dot x₁ x₂) x₁ = x₂ axiom grad_dot₂ {shape : S} (x₁ x₂ : T shape) : ∇ (λ x₂, dot x₁ x₂) x₂ = x₁ axiom grad_gemm₁ {m p : ℕ} (k : T [m, p] → ℝ) (n : ℕ) (M : T [m, n]) (N : T [n, p]) : ∇ (λ M, k (gemm M N)) M = gemm (∇ k (gemm M N)) (transpose N) axiom grad_gemm₂ {m p : ℕ} (k : T [m, p] → ℝ) (n : ℕ) (M : T [m, n]) (N : T [n, p]) : ∇ (λ N, k (gemm M N)) N = gemm (transpose M) (∇ k (gemm M N)) -- Congruences axiom grad_congr_pos {shape : S} (f g : T shape → ℝ) (θ : T shape) : θ > 0 → (∀ (θ₀ : T shape), θ₀ > 0 → f θ₀ = g θ₀) → ∇ f θ = ∇ g θ -- Compound lemma grad_softplus {shape : S} (k : T shape → ℝ) (θ : T shape) : ∇ (λ θ, k (softplus θ)) θ = ∇ k (softplus θ) / (1 + exp (- θ)) := have H : (exp θ) / (exp θ + 1) = 1 / (1 + exp (- θ)), from calc (exp θ) / (exp θ + 1) = ((exp θ) / (exp θ + 1)) * ((exp θ)⁻¹ / (exp θ)⁻¹) : by simp [T.div_self (inv_pos (@exp_pos _ θ))] ... = ((exp θ * (exp θ)⁻¹) / ((exp θ + 1) * (exp θ)⁻¹)) : by simp [T.div_mul_div] ... = (1 / ((exp θ + 1) * (exp θ)⁻¹)) : by simp only [T.mul_inv_cancel (@exp_pos _ θ)] ... = 1 / ((exp θ * (exp θ)⁻¹) + 1 * (exp θ)⁻¹) : by simp only [right_distrib] ... = 1 / (1 + exp (- θ)) : by { simp only [T.mul_inv_cancel (@exp_pos _ θ), one_mul], rw exp_inv}, calc ∇ (λ θ, k (softplus θ)) θ = ∇ (λ θ, k (log (exp θ + 1))) θ : rfl ... = ∇ (λ θ, k (log (θ + 1))) (exp θ) * exp θ : by rw T.grad_exp (λ θ, k (log (θ + 1))) ... = ∇ (λ θ, k (log θ)) (exp θ + 1) * exp θ : by rw T.grad_add₁ (λ θ, k (log θ)) ... = ∇ k (log (exp θ + 1)) / (exp θ + 1) * exp θ : by rw (T.grad_log k (exp θ + 1) (plus_one_pos exp_pos)) ... = ∇ k (softplus θ) * (exp θ / (exp θ + 1)) : by { rw [-T.mul_div_mul], reflexivity } ... = ∇ k (softplus θ) * (1 / (1 + exp (- θ))) : by rw H ... = ∇ k (softplus θ) / (1 + exp (- θ)) : by simp [T.one_div_inv, T.div_mul_inv] lemma grad_sigmoid {shape : S} (k : T shape → ℝ) (θ : T shape) : ∇ (λ θ, k (sigmoid θ)) θ = ∇ k (sigmoid θ) * sigmoid θ * (1 - sigmoid θ) := have H_pre : 1 + exp (- θ) > 0, from one_plus_pos exp_pos, have H : exp (- θ) / (1 + exp (- θ)) = 1 - sigmoid θ, from calc exp (- θ) / (1 + exp (- θ)) = ((1 + exp (- θ)) - 1) / (1 + exp (- θ)) : by simp [sub_add_eq_sub_sub] ... = ((1 + exp (- θ)) / (1 + exp (- θ))) - 1 / (1 + exp (- θ)) : by simp [T.div_sub_div_same] ... = 1 - sigmoid θ : by { rw T.div_self (one_plus_pos exp_pos), reflexivity }, calc ∇ (λ θ, k (sigmoid θ)) θ = ∇ (λ θ, k (1 / (1 + exp (- θ)))) θ : rfl ... = - ∇ (λ θ, k (1 / (1 + exp θ))) (- θ) : by rw T.grad_neg (λ θ, k (1 / (1 + exp θ))) ... = - (∇ (λ θ, k (1 / (1 + θ))) (exp (- θ)) * exp (- θ)) : by rw T.grad_exp (λ θ, k (1 / (1 + θ))) ... = - (∇ (λ θ, k (1 / θ)) (1 + exp (- θ)) * exp (- θ)) : by rw T.grad_add₂ (λ θ, k (1 / θ)) ... = -(-(∇ k (1 / (1 + exp (-θ))) * 1) / square (1 + exp (-θ)) * exp (-θ)) : by rw (T.grad_div₂ k 1 (1 + exp (- θ)) (square_pos_of_pos $ one_plus_pos exp_pos)) ... = (∇ k (1 / (1 + exp (-θ)))) / square (1 + exp (-θ)) * exp (-θ) : begin rw T.neg_div, simp [mul_neg_eq_neg_mul_symm] end ... = (∇ k (sigmoid θ)) / square (1 + exp (-θ)) * exp (-θ) : rfl ... = (∇ k (sigmoid θ)) * (1 / (1 + exp (-θ))) * (exp (-θ) / (1 + exp (- θ))) : by simp [square, T.div_mul_inv, T.mul_inv_pos H_pre H_pre] ... = (∇ k (sigmoid θ)) * sigmoid θ * (exp (-θ) / (1 + exp (- θ))) : rfl ... = ∇ k (sigmoid θ) * sigmoid θ * (1 - sigmoid θ) : by rw H -- Gradients wrt arbitrary functions lemma grad_add_fs {ishape : S} (θ : T ishape) (f₁ f₂ : T ishape → ℝ) : is_cdifferentiable f₁ θ → is_cdifferentiable f₂ θ → ∇ (λ θ₀, f₁ θ₀ + f₂ θ₀) θ = ∇ (λ θ₀, f₁ θ₀) θ + ∇ (λ θ₀, f₂ θ₀) θ := assume H_f₁ H_f₂, have H₁ : is_cdifferentiable (λ θ₀, f₁ θ₀ + f₂ θ) θ, begin apply iff.mp (is_cdifferentiable_add_fs _ _ _), split, exact H_f₁, apply is_cdifferentiable_const end, have H₂ : is_cdifferentiable (λ θ₀, f₁ θ + f₂ θ₀) θ, begin apply iff.mp (is_cdifferentiable_add_fs _ _ _), split, apply is_cdifferentiable_const, exact H_f₂ end, begin rw grad_binary (λ θ₁ θ₂, f₁ θ₁ + f₂ θ₂) _ H₁ H₂, rw [grad_chain_rule _ (λ θ₀, θ₀ + f₂ θ) θ, grad_chain_rule _ (λ θ₀, f₁ θ + θ₀) θ], rw [tmulT_scalar, D_scalar, tmulT_scalar, D_scalar], rw [grad_add₁ (λ θ, θ), grad_id, one_smul], rw [grad_add₂ (λ θ, θ), grad_id, one_smul] end lemma grad_scale_f {ishape : S} (θ : T ishape) (α : ℝ) (f : T ishape → ℝ) : ∇ (λ θ₀, α ⬝ f θ₀) θ = α ⬝ ∇ (λ θ₀, f θ₀) θ := begin rw grad_chain_rule f (λ θ, α ⬝ θ) θ, rw grad_scale (λ θ, θ), rw grad_id, rw smul.def, rw mul_one, rw tmulT_scalar, rw D_scalar, dunfold smul has_smul.smul scalar_mul, rw const_scalar end lemma grad_log_f {shape : S} (θ : T shape) (f : T shape → ℝ) : f θ > 0 → ∇ (λ θ₀, log (f θ₀)) θ = (f θ)⁻¹ ⬝ ∇ f θ := assume H_pos, have H_grad_log_simple : Π {θ : ℝ}, θ > 0 → ∇ log θ = θ⁻¹, from begin intros θ H_pos, rw grad_log (λ θ, θ) _ H_pos, rw grad_id, apply T.one_div_inv end, by rw [grad_chain_rule, tmulT_scalar, D_scalar, H_grad_log_simple H_pos] section simplify_grad open list expr tactic lemma id_rule {A : Type*} (a : A) : id a = a := rfl meta def reduce_k (k : expr) : tactic expr := do slss ← simp_lemmas.add_simp simp_lemmas.mk `certigrad.T.id_rule, slss^.dsimplify k <|> return k meta def has_x (x e : expr) : bool := expr.fold e ff (λ (m : expr) (d : nat) (b : bool), if m = x then tt else b) meta def compute_outer_inner_functions_core (x : expr) : Π (k e : expr), tactic expr := λ (k e : expr), do let f := get_app_fn e, let args := get_app_args e, let n := length args, let barg₁ := dnth args (n-2), let barg₂ := dnth args (n-1), barg₁_type ← infer_type barg₁, barg₂_type ← infer_type barg₂, if barg₁ = x ∨ barg₂ = x then return k else if has_x x barg₁ then compute_outer_inner_functions_core (lam `x binder_info.default barg₁_type (app k $ mk_app f $ update_nth args (n-2) (var 0))) barg₁ else if has_x x barg₂ then compute_outer_inner_functions_core (lam `x binder_info.default barg₂_type (app k $ mk_app f $ update_nth args (n-1) (var 0))) barg₂ else tactic.fail "no var0" meta def compute_outer_inner_functions (grad : expr) : tactic expr := let g := app_arg (app_fn grad) in do f ← head_eta_expand g, x ← mk_local_def `x (binding_domain f), body ← return (instantiate_var (binding_body f) x), body_type ← infer_type body, initial_k ← return (lam `x binder_info.default body_type (var 0)), compute_outer_inner_functions_core x initial_k body <|> return initial_k meta def compute_k (grad : expr) : tactic expr := do k ← compute_outer_inner_functions grad, k_simp ← reduce_k k, head_eta_expand k_simp meta def check_grad (e : expr) : tactic expr := if is_napp_of e `certigrad.T.grad 3 then head_eta_expand e else tactic.fail "not ∇" meta def try_add_simp (s : simp_lemmas) (p : pexpr) : tactic simp_lemmas := do oe ← try_core $ to_expr p, match oe with | none := return s | (some e) := simp_lemmas.add s e end meta def build_simplify_grad_simp_lemmas (k : expr) : tactic simp_lemmas := do es ← monad.mapm to_expr [``(@certigrad.T.grad_const) , ``(@certigrad.T.grad_id) , ``(certigrad.T.grad_exp %%k) , ``(certigrad.T.grad_log %%k) , ``(certigrad.T.grad_scale %%k) , ``(certigrad.T.grad_neg %%k) , ``(certigrad.T.grad_add₁ %%k) , ``(certigrad.T.grad_add₂ %%k) , ``(certigrad.T.grad_sub₁ %%k) , ``(certigrad.T.grad_sub₂ %%k) , ``(certigrad.T.grad_mul₁ %%k) , ``(certigrad.T.grad_mul₂ %%k) , ``(certigrad.T.grad_div₁ %%k) , ``(certigrad.T.grad_div₂ %%k) , ``(@certigrad.T.grad_dot₁) , ``(@certigrad.T.grad_dot₂) , ``(certigrad.T.grad_square %%k) , ``(certigrad.T.grad_sqrt %%k) , ``(certigrad.T.grad_softplus %%k) , ``(certigrad.T.grad_sigmoid %%k) ], s ← simp_lemmas.append simp_lemmas.mk es, -- These have shape requirements that may cause `to_expr` to fail s ← try_add_simp s ``(certigrad.T.grad_gemm₁ %%k), s ← try_add_simp s ``(certigrad.T.grad_gemm₂ %%k), s ← try_add_simp s ``(certigrad.T.grad_sum %%k), -- These haven't been defined yet s ← try_add_simp s ```(certigrad.T.grad_mvn_kl₁ %%k), s ← try_add_simp s ```(certigrad.T.grad_mvn_kl₂ %%k), s ← try_add_simp s ```(certigrad.T.grad_bernoulli_neglogpdf₁ %%k), s ← try_add_simp s ```(certigrad.T.grad_bernoulli_neglogpdf₂ %%k), s ← try_add_simp s ``(@certigrad.T.grad_scale_f), return s meta def simplify_grad_core_helper (tac : tactic unit) : conv unit := λ r e, do guard $ r = `eq, grad ← check_grad e, k ← compute_k grad, s ← build_simplify_grad_simp_lemmas k, conv.apply_lemmas_core reducible s tac r e meta def simplify_grad_core (tac : tactic unit) : tactic unit := at_target (λ e, do (a, new_e, pf) ← ext_simplify_core () {zeta := ff, beta := ff, eta := ff, proj := ff} simp_lemmas.mk (λ u, failed) (λ a s r p e, failed) (λ a s r p e, do ⟨u, new_e, pr⟩ ← simplify_grad_core_helper tac r e, return ((), new_e, pr, tt)) `eq e, return (new_e, pf)) meta def check_is_cdifferentiable (e : expr) : tactic expr := if is_napp_of e `certigrad.T.is_cdifferentiable 3 then head_eta_expand e else tactic.fail "not is_cdifferentiable" meta def prove_differentiable_core_helper (grad : expr) : tactic unit := do k ← compute_k grad, first [applyc `certigrad.T.is_cdifferentiable_const , applyc `certigrad.T.is_cdifferentiable_id -- these haven't been defined yet , to_expr ```(T.is_cdifferentiable_sigmoid %%k) >>= apply , to_expr ```(T.is_cdifferentiable_softplus %%k) >>= apply , to_expr ```(T.is_cdifferentiable_mvn_kl₁ %%k) >>= apply , to_expr ```(T.is_cdifferentiable_mvn_kl₂ %%k) >>= apply , to_expr ```(T.is_cdifferentiable_bernoulli_neglogpdf₁ %%k) >>= apply , to_expr ```(T.is_cdifferentiable_bernoulli_neglogpdf₂ %%k) >>= apply , to_expr ``(T.is_cdifferentiable_exp %%k) >>= apply , to_expr ``(T.is_cdifferentiable_log %%k) >>= apply , to_expr ``(T.is_cdifferentiable_sqrt %%k) >>= apply , to_expr ``(T.is_cdifferentiable_scale %%k) >>= apply , to_expr ``(T.is_cdifferentiable_neg %%k) >>= apply , to_expr ``(T.is_cdifferentiable_inv %%k) >>= apply , to_expr ``(T.is_cdifferentiable_add₁ %%k) >>= apply , to_expr ``(T.is_cdifferentiable_add₂ %%k) >>= apply , to_expr ``(T.is_cdifferentiable_sub₁ %%k) >>= apply , to_expr ``(T.is_cdifferentiable_sub₂ %%k) >>= apply , to_expr ``(T.is_cdifferentiable_mul₁ %%k) >>= apply , to_expr ``(T.is_cdifferentiable_mul₂ %%k) >>= apply , to_expr ``(T.is_cdifferentiable_div₁ %%k) >>= apply , to_expr ``(T.is_cdifferentiable_div₂ %%k) >>= apply , to_expr ``(T.is_cdifferentiable_square %%k) >>= apply , to_expr ``(T.is_cdifferentiable_sum %%k) >>= apply , to_expr ``(T.is_cdifferentiable_prod %%k) >>= apply , to_expr ``(T.is_cdifferentiable_gemm₁ %%k) >>= apply , to_expr ``(T.is_cdifferentiable_gemm₂ %%k) >>= apply ] meta def prove_differentiable_core : tactic unit := target >>= check_is_cdifferentiable >>= prove_differentiable_core_helper meta def prove_differentiable : tactic unit := repeat (prove_differentiable_core <|> prove_preconditions_core) meta def simplify_grad : tactic unit := simplify_grad_core (repeat $ prove_preconditions_core <|> prove_differentiable_core) end simplify_grad -- Compounds with simplify_grad lemma grad_mvn_kl₁ (k : ℝ → ℝ) (shape : S) (μ σ : T shape) : ∇ (λ μ, k (mvn_kl μ σ)) μ = ∇ k (mvn_kl μ σ) ⬝ μ := begin dunfold T.mvn_kl, simplify_grad, simp [T.smul.def, T.const_neg, T.const_mul, T.const_zero, T.const_one, T.const_bit0, T.const_bit1, T.const_inv], rw [-(mul_assoc (2 : T shape) 2⁻¹), T.mul_inv_cancel two_pos], simp end lemma grad_mvn_kl₂ (k : ℝ → ℝ) (shape : S) (μ σ : T shape) (H_σ : σ > 0) (H_k : is_cdifferentiable k (mvn_kl μ σ)) : ∇ (λ σ, k (mvn_kl μ σ)) σ = ∇ k (mvn_kl μ σ) ⬝ (σ - (1 / σ)) := have H_σ₂ : square σ > 0, from square_pos_of_pos H_σ, have H_diff₁ : is_cdifferentiable (λ (θ₀ : T shape), k (-2⁻¹ * T.sum (1 + T.log (square θ₀) - square μ - square σ))) σ, by prove_differentiable, have H_diff₂ : is_cdifferentiable (λ (θ₀ : T shape), k (-2⁻¹ * T.sum (1 + T.log (square σ) - square μ - square θ₀))) σ, by prove_differentiable, begin dunfold T.mvn_kl, rw (T.grad_binary (λ θ₁ θ₂, k ((- 2⁻¹) * T.sum (1 + T.log (square θ₁) - square μ - square θ₂))) _ H_diff₁ H_diff₂), dsimp, simplify_grad, simp [T.smul.def, T.const_neg, T.const_mul, T.const_zero, T.const_one, T.const_bit0, T.const_bit1, T.const_inv, left_distrib, right_distrib], rw [-(mul_assoc (2 : T shape) 2⁻¹), T.mul_inv_cancel two_pos], erw T.neg_div, simp [mul_neg_eq_neg_mul_symm, neg_mul_eq_neg_mul_symm], apply congr_arg, apply congr_arg, simp only [T.mul_div_mul, square], rw [-mul_assoc, T.mul_div_mul, (@T.div_self_square _ σ H_σ)], simp, rw [-(mul_assoc (2 : T shape) 2⁻¹), T.mul_inv_cancel two_pos], simp, rw T.div_mul_inv, simp end lemma grad_bernoulli_neglogpdf₁ (k : ℝ → ℝ) (shape : S) (p z : T shape) (H_p₁ : 0 < p) (H_p₂ : 0 < 1 - p) (H_k : is_cdifferentiable k (bernoulli_neglogpdf p z)) : ∇ (λ p, k (bernoulli_neglogpdf p z)) p = ∇ k (bernoulli_neglogpdf p z) ⬝ ((1 - z) / (eps shape + (1 - p)) - z / (eps shape + p)) := have H_diff₁ : is_cdifferentiable (λ (θ₀ : T shape), k (-T.sum (z * T.log (eps shape + θ₀) + (1 - z) * T.log (eps shape + (1 - p))))) p, by prove_differentiable, have H_diff₂ : is_cdifferentiable (λ (θ₀ : T shape), k (-T.sum (z * T.log (eps shape + p) + (1 - z) * T.log (eps shape + (1 - θ₀))))) p, by prove_differentiable, begin dunfold T.bernoulli_neglogpdf, rw T.grad_binary (λ θ₁ θ₂, k ( - T.sum (z * T.log (eps shape + θ₁) + (1 - z) * T.log (eps shape + (1 - θ₂))))) _ H_diff₁ H_diff₂, dsimp, simplify_grad, simp [T.smul.def, const_neg, T.neg_div, T.div_mul_inv, left_distrib, right_distrib], end lemma grad_bernoulli_neglogpdf₂ (k : ℝ → ℝ) (shape : S) (p z : T shape) (H_p₁ : 0 < p) (H_p₂ : 0 < 1 - p) (H_k : is_cdifferentiable k (bernoulli_neglogpdf p z)) : ∇ (λ z, k (bernoulli_neglogpdf p z)) z = ∇ k (bernoulli_neglogpdf p z) ⬝ (log (eps shape + (1 - p)) - log (eps shape + p)) := have H_diff₁ : is_cdifferentiable (λ (θ₀ : T shape), k (-T.sum (θ₀ * T.log (eps shape + p) + (1 - z) * T.log (eps shape + (1 - p))))) z, by prove_differentiable, have H_diff₂ : is_cdifferentiable (λ (θ₀ : T shape), k (-T.sum (z * T.log (eps shape + p) + (1 - θ₀) * T.log (eps shape + (1 - p))))) z, by prove_differentiable, begin dunfold T.bernoulli_neglogpdf, rw T.grad_binary (λ θ₁ θ₂, k (- T.sum (θ₁ * T.log (eps shape + p) + (1 - θ₂) * T.log (eps shape + (1 - p))))) _ H_diff₁ H_diff₂, dsimp, simplify_grad, simp [T.smul.def, const_neg, left_distrib, right_distrib], end -- Compounds with prove_differentiable lemma is_cdifferentiable_sigmoid {shape : S} (k : T shape → ℝ) (θ : T shape) : is_cdifferentiable k (sigmoid θ) → is_cdifferentiable (λ θ, k (sigmoid θ)) θ := begin intro H, dunfold sigmoid, prove_differentiable end lemma is_cdifferentiable_softplus {shape : S} (k : T shape → ℝ) (θ : T shape) : is_cdifferentiable k (softplus θ) → is_cdifferentiable (λ θ, k (softplus θ)) θ := begin intro H, dunfold softplus, prove_differentiable end lemma is_cdifferentiable_mvn_kl₁ (k : ℝ → ℝ) (shape : S) (μ σ : T shape) : is_cdifferentiable k (mvn_kl μ σ) → is_cdifferentiable (λ μ, k (mvn_kl μ σ)) μ := begin intro H, dunfold mvn_kl, prove_differentiable end lemma is_cdifferentiable_mvn_kl₂ (k : ℝ → ℝ) (shape : S) (μ σ : T shape) (H_σ : σ > 0) : is_cdifferentiable k (mvn_kl μ σ) → is_cdifferentiable (λ σ, k (mvn_kl μ σ)) σ := begin intro H, dunfold mvn_kl, apply is_cdifferentiable_binary (λ θ₁ θ₂, k (-2⁻¹ * T.sum (1 + T.log (square θ₁) + -square μ + -square θ₂))), { dsimp, prove_differentiable }, { dsimp, prove_differentiable } end lemma is_cdifferentiable_bernoulli_neglogpdf₁ (k : ℝ → ℝ) (shape : S) (p z : T shape) (H_p₁ : p > 0) (H_p₂ : p < 1) : is_cdifferentiable k (bernoulli_neglogpdf p z) → is_cdifferentiable (λ p, k (bernoulli_neglogpdf p z)) p := begin intro H, dunfold bernoulli_neglogpdf, apply is_cdifferentiable_binary (λ θ₁ θ₂, k (-T.sum (z * T.log (eps shape + θ₁) + (1 + -z) * T.log (eps shape + (1 + -θ₂))))), { dsimp, prove_differentiable }, { dsimp, prove_differentiable } end lemma is_cdifferentiable_bernoulli_neglogpdf₂ (k : ℝ → ℝ) (shape : S) (p z : T shape) : is_cdifferentiable k (bernoulli_neglogpdf p z) → is_cdifferentiable (λ z, k (bernoulli_neglogpdf p z)) z := begin intro H, dunfold bernoulli_neglogpdf, apply is_cdifferentiable_binary (λ θ₁ θ₂, k (-T.sum (θ₁ * T.log (eps shape + p) + (1 + -θ₂) * T.log (eps shape + (1 + -p))))), { dsimp, prove_differentiable }, { dsimp, prove_differentiable } end -- Random lemma mvn_grad_logpdf_μ_correct {shape : S} (μ σ x : T shape) (H_σ : σ > 0) : ∇ (λ θ, mvn_logpdf θ σ x) μ = mvn_grad_logpdf_μ μ σ x := begin dunfold mvn_logpdf, note H := square_pos_of_pos H_σ, simplify_grad, simp [smul.def, const_bit0, const_one, const_neg, const_inv, T.neg_div], rw -mul_assoc, rw T.mul_inv_cancel two_pos, simp, rw T.div_div_eq_div_mul, reflexivity end lemma mvn_grad_logpdf_σ_correct {shape : S} (μ σ x : T shape) (H_σ : σ > 0) : ∇ (λ θ, mvn_logpdf μ θ x) σ = mvn_grad_logpdf_σ μ σ x := have H_σ₂ : square σ > 0, from square_pos_of_pos H_σ, have H_d₁ : is_cdifferentiable (λ θ₀, -2⁻¹ * sum (square ((x - μ) / θ₀) + log (2 * pi shape) + log (square σ))) σ, by prove_differentiable, have H_d₂ : is_cdifferentiable (λ θ₀, -2⁻¹ * sum (square ((x - μ) / σ) + log (2 * pi shape) + log (square θ₀))) σ, by prove_differentiable, have H₁ : (2 * (2⁻¹ / square σ)) = σ⁻¹ * σ⁻¹, begin dunfold square, rw [T.mul_div_mul_alt, T.mul_inv_cancel two_pos, one_div_inv, T.mul_inv_pos H_σ H_σ] end, have H₂ : 2 * ((x + -μ) * ((x + -μ) * 2⁻¹)) = (2 * 2⁻¹) * square (x - μ), by simp [square], begin dunfold mvn_logpdf, rw grad_binary (λ θ₁ θ₂, -2⁻¹ * sum (square ((x - μ) / θ₁) + log (2 * pi shape) + log (square θ₂))) _ H_d₁ H_d₂, dsimp, simplify_grad, simp [smul.def, const_bit0, const_one, const_neg, const_inv, T.neg_div, T.div_div_eq_div_mul], rw H₁, rw -mul_assoc, rw T.mul_inv_cancel H_σ, simp [T.mul_div_mul_alt, T.div_div_eq_div_mul], rw [H₂, T.mul_inv_cancel two_pos], simp [mvn_grad_logpdf_σ] end -- With data structures lemma grad_sumr {X : Type} {shape : S} (θ : T shape) (f : T shape → X → ℝ) : Π (xs : list X), is_cdifferentiable (λ (θ₀ : T shape), sumr (map (f θ₀) xs)) θ → ∇ (λ (θ₀ : T shape), list.sumr (map (f θ₀) xs)) θ = list.sumr (map (λ x, ∇ (λ θ₀, f θ₀ x) θ) xs) | [] H_diff := by { dunfold map sumr, rw grad_const } | (x::xs) H_diff := begin dunfold map sumr, dunfold map sumr at H_diff, rw grad_add_fs _ _ _ (iff.mpr (is_cdifferentiable_add_fs _ _ _) H_diff)^.left (iff.mpr (is_cdifferentiable_add_fs _ _ _) H_diff)^.right, rw grad_sumr _ (iff.mpr (is_cdifferentiable_add_fs _ _ _) H_diff)^.right end -- Note: this could be proved from a `select`/`replicate` formulation, -- but it is arguably a more natural way of axiomatizing the property anyway. axiom multiple_args_general : ∀ (parents : list reference) (tgt : reference) (m : env) (f : dvec T parents^.p2 → T tgt.2 → ℝ) (θ : T tgt.2), is_cdifferentiable (λ θ₀, f (env.get_ks parents (env.insert tgt θ m)) θ₀) θ → is_cdifferentiable (λ θ₀, sumr (map (λ (idx : ℕ), f (dvec.update_at θ₀ (env.get_ks parents (env.insert tgt θ m)) idx) θ) (filter (λ idx, tgt = dnth parents idx) (riota $ length parents)))) θ → ∇ (λ (θ₀ : T tgt.2), f (env.get_ks parents (env.insert tgt θ₀ m)) θ₀) θ = ∇ (λ θ₀, f (env.get_ks parents (env.insert tgt θ m)) θ₀) θ + sumr (map (λ (idx : ℕ), ∇ (λ θ₀, f (dvec.update_at θ₀ (env.get_ks parents (env.insert tgt θ m)) idx) θ) θ) (filter (λ idx, tgt = dnth parents idx) (riota $ length parents))) end T end certigrad
156d0b471f4a175c59357e014a575736353e9b51
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/limits/constructions/binary_products_auto.lean
e1a2dd47a2914ee1d5cc8e6021688736e975a8bb
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
1,058
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.limits.shapes.terminal import Mathlib.category_theory.limits.shapes.pullbacks import Mathlib.category_theory.limits.shapes.binary_products import Mathlib.PostPort universes v u namespace Mathlib /-! # Constructing binary product from pullbacks and terminal object. If a category has pullbacks and a terminal object, then it has binary products. TODO: provide the dual result. -/ /-- Any category with pullbacks and terminal object has binary products. -/ -- This is not an instance, as it is not always how one wants to construct binary products! theorem has_binary_products_of_terminal_and_pullbacks (C : Type u) [𝒞 : category_theory.category C] [category_theory.limits.has_terminal C] [category_theory.limits.has_pullbacks C] : category_theory.limits.has_binary_products C := sorry end Mathlib
41395ef6055ab01964980cb98eabccf692c4e2b5
a4673261e60b025e2c8c825dfa4ab9108246c32e
/stage0/src/Lean/Compiler/IR/ElimDeadBranches.lean
fd0532fef6a7adc38e8b2b3145c27e1edb75dd4d
[ "Apache-2.0" ]
permissive
jcommelin/lean4
c02dec0cc32c4bccab009285475f265f17d73228
2909313475588cc20ac0436e55548a4502050d0a
refs/heads/master
1,674,129,550,893
1,606,415,348,000
1,606,415,348,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,572
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Compiler.IR.Format import Lean.Compiler.IR.Basic import Lean.Compiler.IR.CompilerM namespace Lean.IR.UnreachableBranches /-- Value used in the abstract interpreter -/ inductive Value := | bot -- undefined | top -- any value | ctor (i : CtorInfo) (vs : Array Value) | choice (vs : List Value) namespace Value instance : Inhabited Value := ⟨top⟩ protected partial def beq : Value → Value → Bool | bot, bot => true | top, top => true | ctor i₁ vs₁, ctor i₂ vs₂ => i₁ == i₂ && Array.isEqv vs₁ vs₂ Value.beq | choice vs₁, choice vs₂ => vs₁.all (fun v₁ => vs₂.any fun v₂ => Value.beq v₁ v₂) && vs₂.all (fun v₂ => vs₁.any fun v₁ => Value.beq v₁ v₂) | _, _ => false instance : BEq Value := ⟨Value.beq⟩ partial def addChoice (merge : Value → Value → Value) : List Value → Value → List Value | [], v => [v] | v₁@(ctor i₁ vs₁) :: cs, v₂@(ctor i₂ vs₂) => if i₁ == i₂ then merge v₁ v₂ :: cs else v₁ :: addChoice merge cs v₂ | _, _ => panic! "invalid addChoice" partial def merge : Value → Value → Value | bot, v => v | v, bot => v | top, _ => top | _, top => top | v₁@(ctor i₁ vs₁), v₂@(ctor i₂ vs₂) => if i₁ == i₂ then ctor i₁ $ vs₁.size.fold (init := #[]) fun i r => r.push (merge vs₁[i] vs₂[i]) else choice [v₁, v₂] | choice vs₁, choice vs₂ => choice $ vs₁.foldl (addChoice merge) vs₂ | choice vs, v => choice $ addChoice merge vs v | v, choice vs => choice $ addChoice merge vs v protected partial def format : Value → Format | top => "top" | bot => "bot" | choice vs => fmt "@" ++ @List.format _ ⟨Value.format⟩ vs | ctor i vs => fmt "#" ++ if vs.isEmpty then fmt i.name else Format.paren (fmt i.name ++ @formatArray _ ⟨Value.format⟩ vs) instance : ToFormat Value := ⟨Value.format⟩ instance : ToString Value := ⟨Format.pretty ∘ Value.format⟩ /- Make sure constructors of recursive inductive datatypes can only occur once in each path. We use this function this function to implement a simple widening operation for our abstract interpreter. -/ partial def truncate (env : Environment) : Value → NameSet → Value | ctor i vs, found => let I := i.name.getPrefix if found.contains I then top else let cont (found' : NameSet) : Value := ctor i (vs.map fun v => truncate env v found') match env.find? I with | some (ConstantInfo.inductInfo d) => if d.isRec then cont (found.insert I) else cont found | _ => cont found | choice vs, found => let newVs := vs.map fun v => truncate env v found if newVs.elem top then top else choice newVs | v, _ => v /- Widening operator that guarantees termination in our abstract interpreter. -/ def widening (env : Environment) (v₁ v₂ : Value) : Value := truncate env (merge v₁ v₂) {} end Value abbrev FunctionSummaries := SMap FunId Value builtin_initialize functionSummariesExt : SimplePersistentEnvExtension (FunId × Value) FunctionSummaries ← registerSimplePersistentEnvExtension { name := `unreachBranchesFunSummary, addImportedFn := fun as => let cache : FunctionSummaries := mkStateFromImportedEntries (fun s (p : FunId × Value) => s.insert p.1 p.2) {} as cache.switch, addEntryFn := fun s ⟨e, n⟩ => s.insert e n } def addFunctionSummary (env : Environment) (fid : FunId) (v : Value) : Environment := functionSummariesExt.addEntry env (fid, v) def getFunctionSummary? (env : Environment) (fid : FunId) : Option Value := (functionSummariesExt.getState env).find? fid abbrev Assignment := Std.HashMap VarId Value structure InterpContext := (currFnIdx : Nat := 0) (decls : Array Decl) (env : Environment) (lctx : LocalContext := {}) structure InterpState := (assignments : Array Assignment) (funVals : Std.PArray Value) -- we take snapshots during fixpoint computations abbrev M := ReaderT InterpContext (StateM InterpState) open Value def findVarValue (x : VarId) : M Value := do let ctx ← read let s ← get let assignment := s.assignments[ctx.currFnIdx] pure $ assignment.findD x bot def findArgValue (arg : Arg) : M Value := match arg with | Arg.var x => findVarValue x | _ => pure top def updateVarAssignment (x : VarId) (v : Value) : M Unit := do let v' ← findVarValue x let ctx ← read modify fun s => { s with assignments := s.assignments.modify ctx.currFnIdx fun a => a.insert x (merge v v') } def resetVarAssignment (x : VarId) : M Unit := do let ctx ← read modify fun s => { s with assignments := s.assignments.modify ctx.currFnIdx fun a => a.insert x Value.bot } def resetParamAssignment (y : Param) : M Unit := resetVarAssignment y.x partial def projValue : Value → Nat → Value | ctor _ vs, i => vs.getD i bot | choice vs, i => vs.foldl (fun r v => merge r (projValue v i)) bot | v, _ => v def interpExpr : Expr → M Value | Expr.ctor i ys => return ctor i (← ys.mapM fun y => findArgValue y) | Expr.proj i x => return projValue (← findVarValue x) i | Expr.fap fid ys => do let ctx ← read match getFunctionSummary? ctx.env fid with | some v => pure v | none => do let s ← get match ctx.decls.findIdx? (fun decl => decl.name == fid) with | some idx => pure s.funVals[idx] | none => pure top | _ => pure top partial def containsCtor : Value → CtorInfo → Bool | top, _ => true | ctor i _, j => i == j | choice vs, j => vs.any $ fun v => containsCtor v j | _, _ => false def updateCurrFnSummary (v : Value) : M Unit := do let ctx ← read let currFnIdx := ctx.currFnIdx modify fun s => { s with funVals := s.funVals.modify currFnIdx (fun v' => widening ctx.env v v') } /-- Return true if the assignment of at least one parameter has been updated. -/ def updateJPParamsAssignment (ys : Array Param) (xs : Array Arg) : M Bool := do let ctx ← read let currFnIdx := ctx.currFnIdx ys.size.foldM (init := false) fun i r => do let y := ys[i] let x := xs[i] let yVal ← findVarValue y.x let xVal ← findArgValue x let newVal := merge yVal xVal if newVal == yVal then pure r else modify fun s => { s with assignments := s.assignments.modify currFnIdx fun a => a.insert y.x newVal } pure true private partial def resetNestedJPParams : FnBody → M Unit | FnBody.jdecl _ ys b k => do let ctx ← read let currFnIdx := ctx.currFnIdx ys.forM resetParamAssignment /- Remark we don't need to reset the parameters of joint-points nested in `b` since they will be reset if this JP is used. -/ resetNestedJPParams k | FnBody.case _ _ _ alts => alts.forM fun alt => match alt with | Alt.ctor _ b => resetNestedJPParams b | Alt.default b => resetNestedJPParams b | e => do unless e.isTerminal do resetNestedJPParams e.body partial def interpFnBody : FnBody → M Unit | FnBody.vdecl x _ e b => do let v ← interpExpr e updateVarAssignment x v interpFnBody b | FnBody.jdecl j ys v b => withReader (fun ctx => { ctx with lctx := ctx.lctx.addJP j ys v }) do interpFnBody b | FnBody.case _ x _ alts => do let v ← findVarValue x alts.forM fun alt => do match alt with | Alt.ctor i b => if containsCtor v i then interpFnBody b | Alt.default b => interpFnBody b | FnBody.ret x => do let v ← findArgValue x -- dbgTrace ("ret " ++ toString v) $ fun _ => updateCurrFnSummary v | FnBody.jmp j xs => do let ctx ← read let ys := (ctx.lctx.getJPParams j).get! let b := (ctx.lctx.getJPBody j).get! let updated ← updateJPParamsAssignment ys xs if updated then -- We must reset the value of nested join-point parameters since they depend on `ys` values resetNestedJPParams b interpFnBody b | e => do unless e.isTerminal do interpFnBody e.body def inferStep : M Bool := do let ctx ← read modify fun s => { s with assignments := ctx.decls.map fun _ => {} } ctx.decls.size.foldM (init := false) fun idx modified => do match ctx.decls[idx] with | Decl.fdecl fid ys _ b => do let s ← get let currVals := s.funVals[idx] withReader (fun ctx => { ctx with currFnIdx := idx }) do ys.forM fun y => updateVarAssignment y.x top interpFnBody b let s ← get let newVals := s.funVals[idx] pure (modified || currVals != newVals) | Decl.extern _ _ _ _ => pure modified partial def inferMain : M Unit := do let modified ← inferStep if modified then inferMain else pure () partial def elimDeadAux (assignment : Assignment) : FnBody → FnBody | FnBody.vdecl x t e b => FnBody.vdecl x t e (elimDeadAux assignment b) | FnBody.jdecl j ys v b => FnBody.jdecl j ys (elimDeadAux assignment v) (elimDeadAux assignment b) | FnBody.case tid x xType alts => let v := assignment.findD x bot let alts := alts.map fun alt => match alt with | Alt.ctor i b => Alt.ctor i $ if containsCtor v i then elimDeadAux assignment b else FnBody.unreachable | Alt.default b => Alt.default (elimDeadAux assignment b) FnBody.case tid x xType alts | e => if e.isTerminal then e else let (instr, b) := e.split let b := elimDeadAux assignment b instr.setBody b partial def elimDead (assignment : Assignment) : Decl → Decl | Decl.fdecl fid ys t b => Decl.fdecl fid ys t $ elimDeadAux assignment b | other => other end UnreachableBranches open UnreachableBranches def elimDeadBranches (decls : Array Decl) : CompilerM (Array Decl) := do let s ← get let env := s.env let assignments : Array Assignment := decls.map fun _ => {} let funVals := Std.mkPArray decls.size Value.bot let ctx : InterpContext := { decls := decls, env := env } let s : InterpState := { assignments := assignments, funVals := funVals } let (_, s) := (inferMain ctx).run s let funVals := s.funVals let assignments := s.assignments modify fun s => let env := decls.size.fold (init := s.env) fun i env => addFunctionSummary env decls[i].name funVals[i] { s with env := env } pure $ decls.mapIdx fun i decl => elimDead assignments[i] decl end Lean.IR
129a50111b0e29dfb94d7a6286fd743c26411d71
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/limits/shapes/zero_morphisms.lean
d43911a8b2dae5a94b2a9112c1b503f989b4c9a6
[ "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
19,899
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.limits.shapes.products import category_theory.limits.shapes.images import category_theory.isomorphism_classes import category_theory.limits.shapes.zero_objects /-! # Zero morphisms and zero objects A category "has zero morphisms" if there is a designated "zero morphism" in each morphism space, and compositions of zero morphisms with anything give the zero morphism. (Notice this is extra structure, not merely a property.) A category "has a zero object" if it has an object which is both initial and terminal. Having a zero object provides zero morphisms, as the unique morphisms factoring through the zero object. ## References * https://en.wikipedia.org/wiki/Zero_morphism * [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2] -/ noncomputable theory universes v u universes v' u' open category_theory open category_theory.category namespace category_theory.limits variables (C : Type u) [category.{v} C] variables (D : Type u') [category.{v'} D] /-- A category "has zero morphisms" if there is a designated "zero morphism" in each morphism space, and compositions of zero morphisms with anything give the zero morphism. -/ class has_zero_morphisms := [has_zero : Π X Y : C, has_zero (X ⟶ Y)] (comp_zero' : ∀ {X Y : C} (f : X ⟶ Y) (Z : C), f ≫ (0 : Y ⟶ Z) = (0 : X ⟶ Z) . obviously) (zero_comp' : ∀ (X : C) {Y Z : C} (f : Y ⟶ Z), (0 : X ⟶ Y) ≫ f = (0 : X ⟶ Z) . obviously) attribute [instance] has_zero_morphisms.has_zero restate_axiom has_zero_morphisms.comp_zero' restate_axiom has_zero_morphisms.zero_comp' variables {C} @[simp] lemma comp_zero [has_zero_morphisms C] {X Y : C} {f : X ⟶ Y} {Z : C} : f ≫ (0 : Y ⟶ Z) = (0 : X ⟶ Z) := has_zero_morphisms.comp_zero f Z @[simp] lemma zero_comp [has_zero_morphisms C] {X : C} {Y Z : C} {f : Y ⟶ Z} : (0 : X ⟶ Y) ≫ f = (0 : X ⟶ Z) := has_zero_morphisms.zero_comp X f instance has_zero_morphisms_pempty : has_zero_morphisms (discrete pempty) := { has_zero := by tidy } instance has_zero_morphisms_punit : has_zero_morphisms (discrete punit) := { has_zero := by tidy } namespace has_zero_morphisms variables {C} /-- This lemma will be immediately superseded by `ext`, below. -/ private lemma ext_aux (I J : has_zero_morphisms C) (w : ∀ X Y : C, (@has_zero_morphisms.has_zero _ _ I X Y).zero = (@has_zero_morphisms.has_zero _ _ J X Y).zero) : I = J := begin casesI I, casesI J, congr, { ext X Y, exact w X Y }, { apply proof_irrel_heq, }, { apply proof_irrel_heq, } end /-- If you're tempted to use this lemma "in the wild", you should probably carefully consider whether you've made a mistake in allowing two instances of `has_zero_morphisms` to exist at all. See, particularly, the note on `zero_morphisms_of_zero_object` below. -/ lemma ext (I J : has_zero_morphisms C) : I = J := begin apply ext_aux, intros X Y, rw ←@has_zero_morphisms.comp_zero _ _ I X X (@has_zero_morphisms.has_zero _ _ J X X).zero, rw @has_zero_morphisms.zero_comp _ _ J, end instance : subsingleton (has_zero_morphisms C) := ⟨ext⟩ end has_zero_morphisms open opposite has_zero_morphisms instance has_zero_morphisms_opposite [has_zero_morphisms C] : has_zero_morphisms Cᵒᵖ := { has_zero := λ X Y, ⟨(0 : unop Y ⟶ unop X).op⟩, comp_zero' := λ X Y f Z, congr_arg quiver.hom.op (has_zero_morphisms.zero_comp (unop Z) f.unop), zero_comp' := λ X Y Z f, congr_arg quiver.hom.op (has_zero_morphisms.comp_zero f.unop (unop X)), } section variables {C} [has_zero_morphisms C] lemma zero_of_comp_mono {X Y Z : C} {f : X ⟶ Y} (g : Y ⟶ Z) [mono g] (h : f ≫ g = 0) : f = 0 := by { rw [←zero_comp, cancel_mono] at h, exact h } lemma zero_of_epi_comp {X Y Z : C} (f : X ⟶ Y) {g : Y ⟶ Z} [epi f] (h : f ≫ g = 0) : g = 0 := by { rw [←comp_zero, cancel_epi] at h, exact h } lemma eq_zero_of_image_eq_zero {X Y : C} {f : X ⟶ Y} [has_image f] (w : image.ι f = 0) : f = 0 := by rw [←image.fac f, w, has_zero_morphisms.comp_zero] lemma nonzero_image_of_nonzero {X Y : C} {f : X ⟶ Y} [has_image f] (w : f ≠ 0) : image.ι f ≠ 0 := λ h, w (eq_zero_of_image_eq_zero h) end section variables [has_zero_morphisms D] instance : has_zero_morphisms (C ⥤ D) := { has_zero := λ F G, ⟨{ app := λ X, 0, }⟩ } @[simp] lemma zero_app (F G : C ⥤ D) (j : C) : (0 : F ⟶ G).app j = 0 := rfl end namespace is_zero variables [has_zero_morphisms C] lemma eq_zero_of_src {X Y : C} (o : is_zero X) (f : X ⟶ Y) : f = 0 := o.eq_of_src _ _ lemma eq_zero_of_tgt {X Y : C} (o : is_zero Y) (f : X ⟶ Y) : f = 0 := o.eq_of_tgt _ _ lemma iff_id_eq_zero (X : C) : is_zero X ↔ (𝟙 X = 0) := ⟨λ h, h.eq_of_src _ _, λ h, ⟨ λ Y, ⟨⟨⟨0⟩, λ f, by { rw [←id_comp f, ←id_comp default, h, zero_comp, zero_comp], }⟩⟩, λ Y, ⟨⟨⟨0⟩, λ f, by { rw [←comp_id f, ←comp_id default, h, comp_zero, comp_zero], }⟩⟩⟩⟩ lemma of_mono_zero (X Y : C) [mono (0 : X ⟶ Y)] : is_zero X := (iff_id_eq_zero X).mpr ((cancel_mono (0 : X ⟶ Y)).1 (by simp)) lemma of_epi_zero (X Y : C) [epi (0 : X ⟶ Y)] : is_zero Y := (iff_id_eq_zero Y).mpr ((cancel_epi (0 : X ⟶ Y)).1 (by simp)) lemma of_mono_eq_zero {X Y : C} (f : X ⟶ Y) [mono f] (h : f = 0) : is_zero X := by { unfreezingI { subst h, }, apply of_mono_zero X Y, } lemma of_epi_eq_zero {X Y : C} (f : X ⟶ Y) [epi f] (h : f = 0) : is_zero Y := by { unfreezingI { subst h, }, apply of_epi_zero X Y, } lemma iff_split_mono_eq_zero {X Y : C} (f : X ⟶ Y) [split_mono f] : is_zero X ↔ f = 0 := begin rw iff_id_eq_zero, split, { intro h, rw [←category.id_comp f, h, zero_comp], }, { intro h, rw [←split_mono.id f], simp [h], }, end lemma iff_split_epi_eq_zero {X Y : C} (f : X ⟶ Y) [split_epi f] : is_zero Y ↔ f = 0 := begin rw iff_id_eq_zero, split, { intro h, rw [←category.comp_id f, h, comp_zero], }, { intro h, rw [←split_epi.id f], simp [h], }, end lemma of_mono {X Y : C} (f : X ⟶ Y) [mono f] (i : is_zero Y) : is_zero X := begin unfreezingI { have hf := i.eq_zero_of_tgt f, subst hf, }, exact is_zero.of_mono_zero X Y, end lemma of_epi {X Y : C} (f : X ⟶ Y) [epi f] (i : is_zero X) : is_zero Y := begin unfreezingI { have hf := i.eq_zero_of_src f, subst hf, }, exact is_zero.of_epi_zero X Y, end end is_zero /-- A category with a zero object has zero morphisms. It is rarely a good idea to use this. Many categories that have a zero object have zero morphisms for some other reason, for example from additivity. Library code that uses `zero_morphisms_of_zero_object` will then be incompatible with these categories because the `has_zero_morphisms` instances will not be definitionally equal. For this reason library code should generally ask for an instance of `has_zero_morphisms` separately, even if it already asks for an instance of `has_zero_objects`. -/ def is_zero.has_zero_morphisms {O : C} (hO : is_zero O) : has_zero_morphisms C := { has_zero := λ X Y, { zero := hO.from X ≫ hO.to Y }, zero_comp' := λ X Y Z f, by { rw category.assoc, congr, apply hO.eq_of_src, }, comp_zero' := λ X Y Z f, by { rw ←category.assoc, congr, apply hO.eq_of_tgt, }} namespace has_zero_object variables [has_zero_object C] open_locale zero_object /-- A category with a zero object has zero morphisms. It is rarely a good idea to use this. Many categories that have a zero object have zero morphisms for some other reason, for example from additivity. Library code that uses `zero_morphisms_of_zero_object` will then be incompatible with these categories because the `has_zero_morphisms` instances will not be definitionally equal. For this reason library code should generally ask for an instance of `has_zero_morphisms` separately, even if it already asks for an instance of `has_zero_objects`. -/ def zero_morphisms_of_zero_object : has_zero_morphisms C := { has_zero := λ X Y, { zero := (default : X ⟶ 0) ≫ default }, zero_comp' := λ X Y Z f, by { dunfold has_zero.zero, rw category.assoc, congr, }, comp_zero' := λ X Y Z f, by { dunfold has_zero.zero, rw ←category.assoc, congr, }} section has_zero_morphisms variables [has_zero_morphisms C] @[simp] lemma zero_iso_is_initial_hom {X : C} (t : is_initial X) : (zero_iso_is_initial t).hom = 0 := by ext @[simp] lemma zero_iso_is_initial_inv {X : C} (t : is_initial X) : (zero_iso_is_initial t).inv = 0 := by ext @[simp] lemma zero_iso_is_terminal_hom {X : C} (t : is_terminal X) : (zero_iso_is_terminal t).hom = 0 := by ext @[simp] lemma zero_iso_is_terminal_inv {X : C} (t : is_terminal X) : (zero_iso_is_terminal t).inv = 0 := by ext @[simp] lemma zero_iso_initial_hom [has_initial C] : zero_iso_initial.hom = (0 : 0 ⟶ ⊥_ C) := by ext @[simp] lemma zero_iso_initial_inv [has_initial C] : zero_iso_initial.inv = (0 : ⊥_ C ⟶ 0) := by ext @[simp] lemma zero_iso_terminal_hom [has_terminal C] : zero_iso_terminal.hom = (0 : 0 ⟶ ⊤_ C) := by ext @[simp] lemma zero_iso_terminal_inv [has_terminal C] : zero_iso_terminal.inv = (0 : ⊤_ C ⟶ 0) := by ext end has_zero_morphisms open_locale zero_object instance {B : Type*} [category B] : has_zero_object (B ⥤ C) := (((category_theory.functor.const B).obj (0 : C)).is_zero $ λ X, is_zero_zero _).has_zero_object end has_zero_object open_locale zero_object variables {D} @[simp] lemma is_zero.map [has_zero_object D] [has_zero_morphisms D] {F : C ⥤ D} (hF : is_zero F) {X Y : C} (f : X ⟶ Y) : F.map f = 0 := (hF.obj _).eq_of_src _ _ @[simp] lemma _root_.category_theory.functor.zero_obj [has_zero_object D] (X : C) : is_zero ((0 : C ⥤ D).obj X) := (is_zero_zero _).obj _ @[simp] lemma _root_.category_theory.zero_map [has_zero_object D] [has_zero_morphisms D] {X Y : C} (f : X ⟶ Y) : (0 : C ⥤ D).map f = 0 := (is_zero_zero _).map _ section variables [has_zero_object C] [has_zero_morphisms C] open_locale zero_object @[simp] lemma id_zero : 𝟙 (0 : C) = (0 : 0 ⟶ 0) := by ext /-- An arrow ending in the zero object is zero -/ -- This can't be a `simp` lemma because the left hand side would be a metavariable. lemma zero_of_to_zero {X : C} (f : X ⟶ 0) : f = 0 := by ext lemma zero_of_target_iso_zero {X Y : C} (f : X ⟶ Y) (i : Y ≅ 0) : f = 0 := begin have h : f = f ≫ i.hom ≫ 𝟙 0 ≫ i.inv := by simp only [iso.hom_inv_id, id_comp, comp_id], simpa using h, end /-- An arrow starting at the zero object is zero -/ lemma zero_of_from_zero {X : C} (f : 0 ⟶ X) : f = 0 := by ext lemma zero_of_source_iso_zero {X Y : C} (f : X ⟶ Y) (i : X ≅ 0) : f = 0 := begin have h : f = i.hom ≫ 𝟙 0 ≫ i.inv ≫ f := by simp only [iso.hom_inv_id_assoc, id_comp, comp_id], simpa using h, end lemma zero_of_source_iso_zero' {X Y : C} (f : X ⟶ Y) (i : is_isomorphic X 0) : f = 0 := zero_of_source_iso_zero f (nonempty.some i) lemma zero_of_target_iso_zero' {X Y : C} (f : X ⟶ Y) (i : is_isomorphic Y 0) : f = 0 := zero_of_target_iso_zero f (nonempty.some i) lemma mono_of_source_iso_zero {X Y : C} (f : X ⟶ Y) (i : X ≅ 0) : mono f := ⟨λ Z g h w, by rw [zero_of_target_iso_zero g i, zero_of_target_iso_zero h i]⟩ lemma epi_of_target_iso_zero {X Y : C} (f : X ⟶ Y) (i : Y ≅ 0) : epi f := ⟨λ Z g h w, by rw [zero_of_source_iso_zero g i, zero_of_source_iso_zero h i]⟩ /-- An object `X` has `𝟙 X = 0` if and only if it is isomorphic to the zero object. Because `X ≅ 0` contains data (even if a subsingleton), we express this `↔` as an `≃`. -/ def id_zero_equiv_iso_zero (X : C) : (𝟙 X = 0) ≃ (X ≅ 0) := { to_fun := λ h, { hom := 0, inv := 0, }, inv_fun := λ i, zero_of_target_iso_zero (𝟙 X) i, left_inv := by tidy, right_inv := by tidy, } @[simp] lemma id_zero_equiv_iso_zero_apply_hom (X : C) (h : 𝟙 X = 0) : ((id_zero_equiv_iso_zero X) h).hom = 0 := rfl @[simp] lemma id_zero_equiv_iso_zero_apply_inv (X : C) (h : 𝟙 X = 0) : ((id_zero_equiv_iso_zero X) h).inv = 0 := rfl /-- If `0 : X ⟶ Y` is an monomorphism, then `X ≅ 0`. -/ @[simps] def iso_zero_of_mono_zero {X Y : C} (h : mono (0 : X ⟶ Y)) : X ≅ 0 := { hom := 0, inv := 0, hom_inv_id' := (cancel_mono (0 : X ⟶ Y)).mp (by simp) } /-- If `0 : X ⟶ Y` is an epimorphism, then `Y ≅ 0`. -/ @[simps] def iso_zero_of_epi_zero {X Y : C} (h : epi (0 : X ⟶ Y)) : Y ≅ 0 := { hom := 0, inv := 0, hom_inv_id' := (cancel_epi (0 : X ⟶ Y)).mp (by simp) } /-- If a monomorphism out of `X` is zero, then `X ≅ 0`. -/ def iso_zero_of_mono_eq_zero {X Y : C} {f : X ⟶ Y} [mono f] (h : f = 0) : X ≅ 0 := by { unfreezingI { subst h, }, apply iso_zero_of_mono_zero ‹_›, } /-- If an epimorphism in to `Y` is zero, then `Y ≅ 0`. -/ def iso_zero_of_epi_eq_zero {X Y : C} {f : X ⟶ Y} [epi f] (h : f = 0) : Y ≅ 0 := by { unfreezingI { subst h, }, apply iso_zero_of_epi_zero ‹_›, } /-- If an object `X` is isomorphic to 0, there's no need to use choice to construct an explicit isomorphism: the zero morphism suffices. -/ def iso_of_is_isomorphic_zero {X : C} (P : is_isomorphic X 0) : X ≅ 0 := { hom := 0, inv := 0, hom_inv_id' := begin casesI P, rw ←P.hom_inv_id, rw ←category.id_comp P.inv, simp, end, inv_hom_id' := by simp, } end section is_iso variables [has_zero_morphisms C] /-- A zero morphism `0 : X ⟶ Y` is an isomorphism if and only if the identities on both `X` and `Y` are zero. -/ @[simps] def is_iso_zero_equiv (X Y : C) : is_iso (0 : X ⟶ Y) ≃ (𝟙 X = 0 ∧ 𝟙 Y = 0) := { to_fun := by { introsI i, rw ←is_iso.hom_inv_id (0 : X ⟶ Y), rw ←is_iso.inv_hom_id (0 : X ⟶ Y), simp }, inv_fun := λ h, ⟨⟨(0 : Y ⟶ X), by tidy⟩⟩, left_inv := by tidy, right_inv := by tidy, } /-- A zero morphism `0 : X ⟶ X` is an isomorphism if and only if the identity on `X` is zero. -/ def is_iso_zero_self_equiv (X : C) : is_iso (0 : X ⟶ X) ≃ (𝟙 X = 0) := by simpa using is_iso_zero_equiv X X variables [has_zero_object C] open_locale zero_object /-- A zero morphism `0 : X ⟶ Y` is an isomorphism if and only if `X` and `Y` are isomorphic to the zero object. -/ def is_iso_zero_equiv_iso_zero (X Y : C) : is_iso (0 : X ⟶ Y) ≃ (X ≅ 0) × (Y ≅ 0) := begin -- This is lame, because `prod` can't cope with `Prop`, so we can't use `equiv.prod_congr`. refine (is_iso_zero_equiv X Y).trans _, symmetry, fsplit, { rintros ⟨eX, eY⟩, fsplit, exact (id_zero_equiv_iso_zero X).symm eX, exact (id_zero_equiv_iso_zero Y).symm eY, }, { rintros ⟨hX, hY⟩, fsplit, exact (id_zero_equiv_iso_zero X) hX, exact (id_zero_equiv_iso_zero Y) hY, }, { tidy, }, { tidy, }, end lemma is_iso_of_source_target_iso_zero {X Y : C} (f : X ⟶ Y) (i : X ≅ 0) (j : Y ≅ 0) : is_iso f := begin rw zero_of_source_iso_zero f i, exact (is_iso_zero_equiv_iso_zero _ _).inv_fun ⟨i, j⟩, end /-- A zero morphism `0 : X ⟶ X` is an isomorphism if and only if `X` is isomorphic to the zero object. -/ def is_iso_zero_self_equiv_iso_zero (X : C) : is_iso (0 : X ⟶ X) ≃ (X ≅ 0) := (is_iso_zero_equiv_iso_zero X X).trans subsingleton_prod_self_equiv end is_iso /-- If there are zero morphisms, any initial object is a zero object. -/ lemma has_zero_object_of_has_initial_object [has_zero_morphisms C] [has_initial C] : has_zero_object C := begin refine ⟨⟨⊥_ C, λ X, ⟨⟨⟨0⟩, by tidy⟩⟩, λ X, ⟨⟨⟨0⟩, λ f, _⟩⟩⟩⟩, calc f = f ≫ 𝟙 _ : (category.comp_id _).symm ... = f ≫ 0 : by congr ... = 0 : has_zero_morphisms.comp_zero _ _ end /-- If there are zero morphisms, any terminal object is a zero object. -/ lemma has_zero_object_of_has_terminal_object [has_zero_morphisms C] [has_terminal C] : has_zero_object C := begin refine ⟨⟨⊤_ C, λ X, ⟨⟨⟨0⟩, λ f, _⟩⟩, λ X, ⟨⟨⟨0⟩, by tidy⟩⟩⟩⟩, calc f = 𝟙 _ ≫ f : (category.id_comp _).symm ... = 0 ≫ f : by congr ... = 0 : zero_comp end section image variable [has_zero_morphisms C] lemma image_ι_comp_eq_zero {X Y Z : C} {f : X ⟶ Y} {g : Y ⟶ Z} [has_image f] [epi (factor_thru_image f)] (h : f ≫ g = 0) : image.ι f ≫ g = 0 := zero_of_epi_comp (factor_thru_image f) $ by simp [h] lemma comp_factor_thru_image_eq_zero {X Y Z : C} {f : X ⟶ Y} {g : Y ⟶ Z} [has_image g] (h : f ≫ g = 0) : f ≫ factor_thru_image g = 0 := zero_of_comp_mono (image.ι g) $ by simp [h] variables [has_zero_object C] open_locale zero_object /-- The zero morphism has a `mono_factorisation` through the zero object. -/ @[simps] def mono_factorisation_zero (X Y : C) : mono_factorisation (0 : X ⟶ Y) := { I := 0, m := 0, e := 0, } /-- The factorisation through the zero object is an image factorisation. -/ def image_factorisation_zero (X Y : C) : image_factorisation (0 : X ⟶ Y) := { F := mono_factorisation_zero X Y, is_image := { lift := λ F', 0 } } instance has_image_zero {X Y : C} : has_image (0 : X ⟶ Y) := has_image.mk $ image_factorisation_zero _ _ /-- The image of a zero morphism is the zero object. -/ def image_zero {X Y : C} : image (0 : X ⟶ Y) ≅ 0 := is_image.iso_ext (image.is_image (0 : X ⟶ Y)) (image_factorisation_zero X Y).is_image /-- The image of a morphism which is equal to zero is the zero object. -/ def image_zero' {X Y : C} {f : X ⟶ Y} (h : f = 0) [has_image f] : image f ≅ 0 := image.eq_to_iso h ≪≫ image_zero @[simp] lemma image.ι_zero {X Y : C} [has_image (0 : X ⟶ Y)] : image.ι (0 : X ⟶ Y) = 0 := begin rw ←image.lift_fac (mono_factorisation_zero X Y), simp, end /-- If we know `f = 0`, it requires a little work to conclude `image.ι f = 0`, because `f = g` only implies `image f ≅ image g`. -/ @[simp] lemma image.ι_zero' [has_equalizers C] {X Y : C} {f : X ⟶ Y} (h : f = 0) [has_image f] : image.ι f = 0 := by { rw image.eq_fac h, simp } end image /-- In the presence of zero morphisms, coprojections into a coproduct are (split) monomorphisms. -/ instance split_mono_sigma_ι {β : Type v} [decidable_eq β] [has_zero_morphisms C] (f : β → C) [has_colimit (discrete.functor f)] (b : β) : split_mono (sigma.ι f b) := { retraction := sigma.desc (λ b', if h : b' = b then eq_to_hom (congr_arg f h) else 0), } /-- In the presence of zero morphisms, projections into a product are (split) epimorphisms. -/ instance split_epi_pi_π {β : Type v} [decidable_eq β] [has_zero_morphisms C] (f : β → C) [has_limit (discrete.functor f)] (b : β) : split_epi (pi.π f b) := { section_ := pi.lift (λ b', if h : b = b' then eq_to_hom (congr_arg f h) else 0), } /-- In the presence of zero morphisms, coprojections into a coproduct are (split) monomorphisms. -/ instance split_mono_coprod_inl [has_zero_morphisms C] {X Y : C} [has_colimit (pair X Y)] : split_mono (coprod.inl : X ⟶ X ⨿ Y) := { retraction := coprod.desc (𝟙 X) 0, } /-- In the presence of zero morphisms, coprojections into a coproduct are (split) monomorphisms. -/ instance split_mono_coprod_inr [has_zero_morphisms C] {X Y : C} [has_colimit (pair X Y)] : split_mono (coprod.inr : Y ⟶ X ⨿ Y) := { retraction := coprod.desc 0 (𝟙 Y), } /-- In the presence of zero morphisms, projections into a product are (split) epimorphisms. -/ instance split_epi_prod_fst [has_zero_morphisms C] {X Y : C} [has_limit (pair X Y)] : split_epi (prod.fst : X ⨯ Y ⟶ X) := { section_ := prod.lift (𝟙 X) 0, } /-- In the presence of zero morphisms, projections into a product are (split) epimorphisms. -/ instance split_epi_prod_snd [has_zero_morphisms C] {X Y : C} [has_limit (pair X Y)] : split_epi (prod.snd : X ⨯ Y ⟶ Y) := { section_ := prod.lift 0 (𝟙 Y), } end category_theory.limits
94c731bac835367200dbeb1594f4a46c425c55f5
38ee9024fb5974f555fb578fcf5a5a7b71e669b5
/Mathlib/Algebra/Group/Defs.lean
47ac7822a3a98fdbf8d3d79afc96e19b23467915
[ "Apache-2.0" ]
permissive
denayd/mathlib4
750e0dcd106554640a1ac701e51517501a574715
7f40a5c514066801ab3c6d431e9f405baa9b9c58
refs/heads/master
1,693,743,991,894
1,636,618,048,000
1,636,618,048,000
373,926,241
0
0
null
null
null
null
UTF-8
Lean
false
false
12,883
lean
import Mathlib.Data.Nat.Basic -- *only* for notation ℕ which should be in a "prelude" import Mathlib.Data.Int.Basic -- *only* for notation ℤ which should be in a "prelude" import Mathlib.Tactic.Spread /-! # Typeclasses for monoids and groups etc -/ local macro "ofNat_class" Class:ident n:num : command => let field := Lean.mkIdent <| Class.getId.eraseMacroScopes.getString!.toLower `(class $Class:ident.{u} (α : Type u) where $field:ident : α instance {α} [$Class α] : OfNat α (nat_lit $n) where ofNat := ‹$Class α›.1 instance {α} [OfNat α (nat_lit $n)] : $Class α where $field:ident := $n) example : Nat := 0 -- terminate macro block ofNat_class Zero 0 ofNat_class One 1 class Inv (α : Type u) where inv : α → α postfix:max "⁻¹" => Inv.inv /- ## The trick for adding a natural action onto monoids -/ section nat_action variable {M : Type u} -- see npow_rec comment for explanation about why not nsmul_rec n a + a /-- The fundamental scalar multiplication in an additive monoid. `nsmul_rec n a = a+a+...+a` n times. Use instead `n • a`, which has better definitional behavior. -/ def nsmul_rec [Zero M] [Add M] : ℕ → M → M | 0 , a => 0 | n+1, a => a + nsmul_rec n a -- use `x * npow_rec n x` and not `npow_rec n x * x` in the definition to make sure that -- definitional unfolding of `npow_rec` is blocked, to avoid deep recursion issues. /-- The fundamental power operation in a monoid. `npow_rec n a = a*a*...*a` n times. Use instead `a ^ n`, which has better definitional behavior. -/ def npow_rec [One M] [Mul M] : ℕ → M → M | 0 , a => 1 | n+1, a => a * npow_rec n a end nat_action section int_action /-- The fundamental scalar multiplication in an additive group. `gsmul_rec n a = a+a+...+a` n times, for integer `n`. Use instead `n • a`, which has better definitional behavior. -/ def gsmul_rec {G : Type u} [Zero G] [Add G] [Neg G]: ℤ → G → G | (Int.ofNat n) , a => nsmul_rec n a | (Int.negSucc n), a => - (nsmul_rec n.succ a) /-- The fundamental power operation in a group. `gpow_rec n a = a*a*...*a` n times, for integer `n`. Use instead `a ^ n`, which has better definitional behavior. -/ def gpow_rec {G : Type u} [One G] [Mul G] [Inv G] : ℤ → G → G | (Int.ofNat n) , a => npow_rec n a | (Int.negSucc n), a => (npow_rec n.succ a) ⁻¹ end int_action /- ## Additive semigroups, monoids and groups -/ /- ### Semigroups -/ class AddSemigroup (A : Type u) extends Add A where add_assoc (a b c : A) : (a + b) + c = a + (b + c) theorem add_assoc {G : Type u} [AddSemigroup G] : ∀ a b c : G, (a + b) + c = a + (b + c) := AddSemigroup.add_assoc class AddCommSemigroup (A : Type u) extends AddSemigroup A where add_comm (a b : A) : a + b = b + a theorem add_comm {A : Type u} [AddCommSemigroup A] (a b : A) : a + b = b + a := AddCommSemigroup.add_comm a b /- ### Cancellative semigroups -/ class IsAddLeftCancel (A : Type u) [Add A] where add_left_cancel (a b c : A) : a + b = a + c → b = c class IsAddRightCancel (A : Type u) [Add A] where add_right_cancel (a b c : A) : b + a = c + a → b = c section AddLeftCancel_lemmas variable {A : Type u} [AddSemigroup A] [IsAddLeftCancel A] {a b c : A} theorem add_left_cancel : a + b = a + c → b = c := IsAddLeftCancel.add_left_cancel a b c theorem add_left_cancel_iff : a + b = a + c ↔ b = c := ⟨add_left_cancel, congrArg _⟩ -- no `function.injective`? --theorem add_right_injective (a : G) : function.injective (c * .) := --λ a b => add_left_cancel @[simp] theorem add_right_inj (a : A) {b c : A} : a + b = a + c ↔ b = c := ⟨add_left_cancel, congrArg _⟩ --theorem add_ne_add_right (a : A) {b c : A} : a + b ≠ a + c ↔ b ≠ c := --(add_right_injective a).ne_iff end AddLeftCancel_lemmas section AddRightCancel_lemmas variable {A : Type u} [AddSemigroup A] [IsAddRightCancel A] {a b c : A} theorem add_right_cancel : b + a = c + a → b = c := IsAddRightCancel.add_right_cancel a b c theorem add_right_cancel_iff : b + a = c + a ↔ b = c := ⟨add_right_cancel, λ h => h ▸ rfl⟩ @[simp] theorem add_left_inj (a : A) {b c : A} : b + a = c + a ↔ b = c := ⟨add_right_cancel, λ h => h ▸ rfl⟩ end AddRightCancel_lemmas /- ### Additive monoids -/ class AddMonoid (A : Type u) extends AddSemigroup A, Zero A where add_zero (a : A) : a + 0 = a zero_add (a : A) : 0 + a = a nsmul : ℕ → A → A := nsmul_rec nsmul_zero' : ∀ x, nsmul 0 x = 0 -- fill in with tactic once we can do this nsmul_succ' : ∀ (n : ℕ) x, nsmul n.succ x = x + nsmul n x -- fill in with tactic section AddMonoid_lemmas variable {A : Type u} [AddMonoid A] {a b c : A} @[simp] theorem add_zero (a : A) : a + 0 = a := AddMonoid.add_zero a @[simp] theorem zero_add (a : A) : 0 + a = a := AddMonoid.zero_add a theorem left_neg_eq_right_neg (hba : b + a = 0) (hac : a + c = 0) : b = c := by rw [←zero_add c, ←hba, add_assoc, hac, add_zero b] end AddMonoid_lemmas /- ### Commutative additive monoids -/ class AddCommMonoid (A : Type u) extends AddMonoid A where add_comm (a b : A) : a + b = b + a instance (A : Type u) [AddCommMonoid A] : AddCommSemigroup A where __ := ‹AddCommMonoid A› /- ### sub_neg_monoids Additive groups can "pick up" several equal but not defeq actions of ℤ. This trick isolates one such action, `gsmul`, and decrees it to be "the canonical one". -/ class SubNegMonoid (A : Type u) extends AddMonoid A, Neg A, Sub A where sub := λ a b => a + -b sub_eq_add_neg : ∀ a b : A, a - b = a + -b gsmul : ℤ → A → A := gsmul_rec gsmul_zero' : ∀ (a : A), gsmul 0 a = 0 gsmul_succ' (n : ℕ) (a : A) : gsmul (Int.ofNat n.succ) a = a + gsmul (Int.ofNat n) a gsmul_neg' (n : ℕ) (a : A) : gsmul (Int.negSucc n) a = -(gsmul ↑(n.succ) a) /- ### Additive groups -/ class AddGroup (A : Type u) extends SubNegMonoid A where add_left_neg (a : A) : -a + a = 0 section AddGroup_lemmas variable {A : Type u} [AddGroup A] {a b c : A} @[simp] theorem add_left_neg : ∀ a : A, -a + a = 0 := AddGroup.add_left_neg theorem neg_add_self (a : A) : -a + a = 0 := add_left_neg a @[simp] theorem neg_add_cancel_left (a b : A) : -a + (a + b) = b := by rw [← add_assoc, add_left_neg, zero_add] @[simp] theorem neg_eq_of_add_eq_zero (h : a + b = 0) : -a = b := left_neg_eq_right_neg (neg_add_self a) h @[simp] theorem neg_neg (a : A) : -(-a) = a := neg_eq_of_add_eq_zero (add_left_neg a) @[simp] theorem add_right_neg (a : A) : a + -a = 0 := by rw [←add_left_neg (-a), neg_neg] -- synonym theorem add_neg_self (a : A) : a + -a = 0 := add_right_neg a @[simp] theorem add_neg_cancel_right (a b : A) : a + b + -b = a := by rw [add_assoc, add_right_neg, add_zero] instance (A : Type u) [AddGroup A] : IsAddRightCancel A where add_right_cancel a b c h := by rw [← add_neg_cancel_right b a, h, add_neg_cancel_right] instance (A : Type u) [AddGroup A] : IsAddLeftCancel A where add_left_cancel a b c h := by rw [← neg_add_cancel_left a b, h, neg_add_cancel_left] end AddGroup_lemmas class AddCommGroup (A : Type u) extends AddGroup A where add_comm (a b : A) : a + b = b + a instance (A : Type u) [AddCommGroup A] : AddCommMonoid A where __ := ‹AddCommGroup A› /- ## Multiplicative semigroups, monoids and groups -/ /- ## Semigroups -/ class Semigroup (G : Type u) extends Mul G where mul_assoc (a b c : G) : (a * b) * c = a * (b * c) theorem mul_assoc {G : Type u} [Semigroup G] : ∀ a b c : G, a * b * c = a * (b * c) := Semigroup.mul_assoc class CommSemigroup (G : Type u) extends Semigroup G where mul_comm (a b : G) : a * b = b * a theorem mul_comm {M : Type u} [CommSemigroup M] : ∀ a b : M, a * b = b * a := CommSemigroup.mul_comm /- ### Cancellative semigroups -/ class IsMulLeftCancel (G : Type u) [Mul G] where mul_left_cancel (a b c : G) : a * b = a * c → b = c class IsMulRightCancel (G : Type u) [Mul G] where mul_right_cancel (a b c : G) : b * a = c * a → b = c section MulLeftCancel variable {G : Type u} [Semigroup G] [IsMulLeftCancel G] {a b c : G} theorem mul_left_cancel : a * b = a * c → b = c := IsMulLeftCancel.mul_left_cancel a b c theorem mul_left_cancel_iff : a * b = a * c ↔ b = c := ⟨mul_left_cancel, congrArg _⟩ -- no `function.injective`? --theorem mul_right_injective (a : G) : function.injective (c * .) := --λ a b => mul_left_cancel @[simp] theorem mul_right_inj (a : G) {b c : G} : a * b = a * c ↔ b = c := ⟨mul_left_cancel, congrArg _⟩ --theorem mul_ne_mul_right (a : G) {b c : G} : a * b ≠ a * c ↔ b ≠ c := --(mul_right_injective a).ne_iff end MulLeftCancel section MulRightCancel variable {G : Type u} [Semigroup G] [IsMulRightCancel G] {a b c : G} theorem mul_right_cancel : b * a = c * a → b = c := IsMulRightCancel.mul_right_cancel a b c theorem mul_right_cancel_iff : b * a = c * a ↔ b = c := ⟨mul_right_cancel, λ h => h ▸ rfl⟩ @[simp] theorem mul_left_inj (a : G) {b c : G} : b * a = c * a ↔ b = c := ⟨mul_right_cancel, λ h => h ▸ rfl⟩ end MulRightCancel /- ### Monoids -/ class Monoid (M : Type u) extends Semigroup M, One M where mul_one (m : M) : m * 1 = m one_mul (m : M) : 1 * m = m npow : ℕ → M → M := npow_rec npow_zero' : ∀ x, npow 0 x = 1 -- fill in with tactic once we can do this npow_succ' : ∀ (n : ℕ) x, npow n.succ x = x * npow n x -- fill in with tactic section Monoid variable {M : Type u} [Monoid M] @[defaultInstance high] instance Monoid.HPow : HPow M ℕ M := ⟨λ a n => Monoid.npow n a⟩ @[simp] theorem mul_one : ∀ (a : M), a * 1 = a := Monoid.mul_one @[simp] theorem one_mul : ∀ (a : M), 1 * a = a := Monoid.one_mul theorem npow_eq_pow (n : ℕ) (a : M) : Monoid.npow n a = a^n := rfl @[simp] theorem pow_zero : ∀ (a : M), a ^ (0:ℕ) = 1 := Monoid.npow_zero' theorem pow_succ' : ∀ (n : ℕ) (a : M), a ^ n.succ = a * a ^ n := Monoid.npow_succ' theorem pow_mul_comm (a : M) (n : ℕ) : a^n * a = a * a^n := by induction n with | zero => simp | succ n ih => simp [ih, pow_succ', mul_assoc] theorem pow_succ (n : ℕ) (a : M) : a ^ n.succ = a ^ n * a := by rw [pow_succ', pow_mul_comm] @[simp] theorem pow_one (a : M) : a ^ (1:ℕ) = a := by rw [Nat.one_succ_zero, pow_succ, pow_zero, one_mul] theorem pow_add (a : M) (m n : ℕ) : a^(m + n) = a^m * a^n := by induction n with | zero => simp | succ n ih => rw [Nat.add_succ, pow_succ',ih, pow_succ', ← mul_assoc, ← mul_assoc, pow_mul_comm] theorem pow_mul {M} [Monoid M] (a : M) (m n : ℕ) : a^(m * n) = (a^m)^n := by induction n with | zero => simp | succ n ih => rw [Nat.mul_succ, pow_add, ih, pow_succ', pow_mul_comm] theorem left_inv_eq_right_inv {M : Type u} [Monoid M] {a b c : M} (hba : b * a = 1) (hac : a * c = 1) : b = c := by rw [←one_mul c, ←hba, mul_assoc, hac, mul_one b] end Monoid /- ### Commutative monoids -/ class CommMonoid (M : Type u) extends Monoid M where mul_comm (a b : M) : a * b = b * a section CommMonoid variable {M} [CommMonoid M] instance : CommSemigroup M where __ := ‹CommMonoid M› theorem mul_pow {M} [CommMonoid M] (a b : M) (n : ℕ) : (a * b)^n= a^n * b^n := by induction n with | zero => simp | succ n ih => simp [pow_succ', ih, @mul_comm M _, @mul_assoc M _] end CommMonoid /- ### Div inv monoids -/ class DivInvMonoid (G : Type u) extends Monoid G, Inv G, Div G := (div := λ a b => a * b⁻¹) (div_eq_mul_inv : ∀ a b : G, a / b = a * b⁻¹) (gpow : ℤ → G → G := gpow_rec) (gpow_zero' : ∀ (a : G), gpow 0 a = 1) (gpow_succ' : ∀ (n : ℕ) (a : G), gpow (Int.ofNat n.succ) a = a * gpow (Int.ofNat n) a) (gpow_neg' : ∀ (n : ℕ) (a : G), gpow (Int.negSucc n) a = (gpow n.succ a) ⁻¹) /- ### Groups -/ class Group (G : Type u) extends DivInvMonoid G where mul_left_inv (a : G) : a⁻¹ * a = 1 section Group_lemmas variable {G : Type u} [Group G] {a b c : G} @[simp] theorem mul_left_inv : ∀ a : G, a⁻¹ * a = 1 := Group.mul_left_inv theorem inv_mul_self (a : G) : a⁻¹ * a = 1 := mul_left_inv a @[simp] theorem inv_mul_cancel_left (a b : G) : a⁻¹ * (a * b) = b := by rw [← mul_assoc, mul_left_inv, one_mul] @[simp] theorem inv_eq_of_mul_eq_one (h : a * b = 1) : a⁻¹ = b := left_inv_eq_right_inv (inv_mul_self a) h @[simp] theorem inv_inv (a : G) : (a⁻¹)⁻¹ = a := inv_eq_of_mul_eq_one (mul_left_inv a) @[simp] theorem mul_right_inv (a : G) : a * a⁻¹ = 1 := by rw [←mul_left_inv (a⁻¹), inv_inv] -- synonym theorem mul_inv_self (a : G) : a * a⁻¹ = 1 := mul_right_inv a @[simp] theorem mul_inv_cancel_right (a b : G) : a * b * b⁻¹ = a := by rw [mul_assoc, mul_right_inv, mul_one] end Group_lemmas class CommGroup (G : Type u) extends Group G where mul_comm (a b : G) : a * b = b * a instance (G : Type u) [CommGroup G] : CommMonoid G where __ := ‹CommGroup G›
3a8f7a9d0e0a1fe8daeaf41f52bbec2cd846f11e
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/ring/ulift.lean
ea974bbd8c39e125cee2e490aaf16de24b99c856
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
4,386
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.group.ulift import algebra.ring.equiv /-! # `ulift` instances for ring > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines instances for ring, semiring and related structures on `ulift` types. (Recall `ulift α` is just a "copy" of a type `α` in a higher universe.) We also provide `ulift.ring_equiv : ulift R ≃+* R`. -/ universes u v variables {α : Type u} {x y : ulift.{v} α} namespace ulift instance mul_zero_class [mul_zero_class α] : mul_zero_class (ulift α) := by refine_struct { zero := (0 : ulift α), mul := (*), .. }; tactic.pi_instance_derive_field instance distrib [distrib α] : distrib (ulift α) := by refine_struct { add := (+), mul := (*), .. }; tactic.pi_instance_derive_field instance non_unital_non_assoc_semiring [non_unital_non_assoc_semiring α] : non_unital_non_assoc_semiring (ulift α) := by refine_struct { zero := (0 : ulift α), add := (+), mul := (*), nsmul := add_monoid.nsmul, }; tactic.pi_instance_derive_field instance non_assoc_semiring [non_assoc_semiring α] : non_assoc_semiring (ulift α) := by refine_struct { zero := (0 : ulift α), one := 1, add := (+), mul := (*), nsmul := add_monoid.nsmul, .. ulift.add_monoid_with_one }; tactic.pi_instance_derive_field instance non_unital_semiring [non_unital_semiring α] : non_unital_semiring (ulift α) := by refine_struct { zero := (0 : ulift α), add := (+), mul := (*), nsmul := add_monoid.nsmul, }; tactic.pi_instance_derive_field instance semiring [semiring α] : semiring (ulift α) := by refine_struct { zero := (0 : ulift α), one := 1, add := (+), mul := (*), nsmul := add_monoid.nsmul, npow := monoid.npow, .. ulift.add_monoid_with_one }; tactic.pi_instance_derive_field /-- The ring equivalence between `ulift α` and `α`. -/ def ring_equiv [non_unital_non_assoc_semiring α] : ulift α ≃+* α := { to_fun := ulift.down, inv_fun := ulift.up, map_mul' := λ x y, rfl, map_add' := λ x y, rfl, left_inv := by tidy, right_inv := by tidy, } instance non_unital_comm_semiring [non_unital_comm_semiring α] : non_unital_comm_semiring (ulift α) := by refine_struct { zero := (0 : ulift α), add := (+), mul := (*), nsmul := add_monoid.nsmul }; tactic.pi_instance_derive_field instance comm_semiring [comm_semiring α] : comm_semiring (ulift α) := by refine_struct { zero := (0 : ulift α), one := 1, add := (+), mul := (*), nsmul := add_monoid.nsmul, npow := monoid.npow, .. ulift.semiring }; tactic.pi_instance_derive_field instance non_unital_non_assoc_ring [non_unital_non_assoc_ring α] : non_unital_non_assoc_ring (ulift α) := by refine_struct { zero := (0 : ulift α), add := (+), mul := (*), sub := has_sub.sub, neg := has_neg.neg, nsmul := add_monoid.nsmul, zsmul := sub_neg_monoid.zsmul }; tactic.pi_instance_derive_field instance non_unital_ring [non_unital_ring α] : non_unital_ring (ulift α) := by refine_struct { zero := (0 : ulift α), add := (+), mul := (*), sub := has_sub.sub, neg := has_neg.neg, nsmul := add_monoid.nsmul, zsmul := sub_neg_monoid.zsmul }; tactic.pi_instance_derive_field instance non_assoc_ring [non_assoc_ring α] : non_assoc_ring (ulift α) := by refine_struct { zero := (0 : ulift α), one := 1, add := (+), mul := (*), sub := has_sub.sub, neg := has_neg.neg, nsmul := add_monoid.nsmul, zsmul := sub_neg_monoid.zsmul, .. ulift.add_group_with_one }; tactic.pi_instance_derive_field instance ring [ring α] : ring (ulift α) := by refine_struct { zero := (0 : ulift α), one := 1, add := (+), mul := (*), sub := has_sub.sub, neg := has_neg.neg, nsmul := add_monoid.nsmul, npow := monoid.npow, zsmul := sub_neg_monoid.zsmul, .. ulift.semiring, .. ulift.add_group_with_one }; tactic.pi_instance_derive_field instance non_unital_comm_ring [non_unital_comm_ring α] : non_unital_comm_ring (ulift α) := by refine_struct { zero := (0 : ulift α), add := (+), mul := (*), sub := has_sub.sub, neg := has_neg.neg, nsmul := add_monoid.nsmul, zsmul := sub_neg_monoid.zsmul }; tactic.pi_instance_derive_field instance comm_ring [comm_ring α] : comm_ring (ulift α) := by refine_struct { .. ulift.ring }; tactic.pi_instance_derive_field end ulift
6630cd86f5ed9ebce7a8631bc96de27a97481ea6
5a5e1bb8063d7934afac91f30aa17c715821040b
/lean3SOS/src/util/parser.lean
9b93ab5e2aa2906ee94835a110a5d5826b726210
[]
no_license
ramonfmir/leanSOS
1883392d73710db5c6e291a2abd03a6c5b44a42b
14b50713dc887f6d408b7b2bce1f8af5bb619958
refs/heads/main
1,683,348,826,105
1,622,056,982,000
1,622,056,982,000
341,232,766
1
0
null
null
null
null
UTF-8
Lean
false
false
2,370
lean
import data.matrix.basic import data.real.basic import data.mv_polynomial.basic import tactic.fin_cases import float.basic open lean.parser tactic interactive -- Parsing strings. private meta def parse_dim_aux : char → ℕ | '1' := 1 | '2' := 2 | '3' := 3 | '4' := 4 | _ := 0 -- and so on meta def parse_dim : string → ℕ := λ s, parse_dim_aux (string.head s) private meta def nat_list_of_lists_from_string_aux (st : string) : lean.parser expr := do (t, s) ← with_input types.texpr st, e <- to_expr ``(%%t : list (list ℕ)), n <- eval_expr' (list (list ℕ)) e, return (reflect n) meta def nat_list_of_lists_from_string (st : string) : tactic expr := do e ← run (nat_list_of_lists_from_string_aux st), return e private meta def rat_list_of_lists_from_string_aux (st : string) : lean.parser expr := do (t, s) ← with_input types.texpr st, e <- to_expr ``(%%t : list (list (ℤ × ℤ))), n <- eval_expr' (list (list (ℤ × ℤ))) e, return (reflect n) meta def rat_list_of_lists_from_string (st : string) : tactic expr := do e ← run (rat_list_of_lists_from_string_aux st), return e -- Transforming lists (definitions). @[reducible] noncomputable def list_to_monomial (l : list ℕ) : mv_polynomial ℕ float := (l.map mv_polynomial.X).foldl (*) (mv_polynomial.C 1) @[reducible] noncomputable def list_to_monomials (l : list (list ℕ)) : list (mv_polynomial ℕ float) := l.map list_to_monomial @[reducible] def list_to_vector {α} (n : ℕ) (l : list α) (hl : l.length = n) : fin n → α := λ i, l.nth_le i.1 (hl.symm ▸ i.2) @[reducible] def list_to_matrix {α} (n m : ℕ) (l : list (list α)) (hl : l.length = n) (hl' : ∀ i : fin n, (l.nth_le i.1 (hl.symm ▸ i.2)).length = m) : matrix (fin n) (fin m) α := λ i j, (l.nth_le i.1 (hl.symm ▸ i.2)).nth_le j.1 ((hl' i).symm ▸ j.2) -- Transforming lists (tactics). meta def monomials_from_list (d : ℕ) (l : expr) : tactic expr := do e ← to_expr ``(list_to_vector %%d (list_to_monomials %%l) (by refl)), return e meta def matrix_from_list (n m : ℕ) (l : expr) : tactic expr := do e ← to_expr ``(list_to_matrix %%n %%m %%l (by refl) (λ i, by fin_cases i; refl)), return e -- def test_matrix : matrix (fin 2) (fin 2) ℚ := -- by do { e ← (matrix_from_list 2 2 `([[0.1, 0.2], [0.3, 0.4]] : list (list ℚ)) ), tactic.exact e }