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
0a2db30e9b50feb1b91799281622d0b2671799ed
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/data/real/ereal.lean
2259863753197b3e18691a14eff5bf252f06bc93
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
18,702
lean
/- Copyright (c) 2019 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard -/ import data.real.basic import data.real.ennreal /-! # The extended reals [-∞, ∞]. This file defines `ereal`, the real numbers together with a top and bottom element, referred to as ⊤ and ⊥. It is implemented as `with_top (with_bot ℝ)` Addition and multiplication are problematic in the presence of ±∞, but negation has a natural definition and satisfies the usual properties. An ad hoc addition is defined, for which `ereal` is an `add_comm_monoid`, and even an ordered one (if `a ≤ a'` and `b ≤ b'` then `a + b ≤ a' + b'`). Note however that addition is badly behaved at `(⊥, ⊤)` and `(⊤, ⊥)` so this can not be upgraded to a group structure. Our choice is that `⊥ + ⊤ = ⊤ + ⊥ = ⊤`. An ad hoc subtraction is then defined by `x - y = x + (-y)`. It does not have nice properties, but it is sometimes convenient to have. An ad hoc multiplication is defined, for which `ereal` is a `comm_monoid_with_zero`. This does not distribute with addition, as `⊤ = ⊤ - ⊥ = 1*⊤ - 1*⊤ ≠ (1 - 1) * ⊤ = 0 * ⊤ = 0`. `ereal` is a `complete_linear_order`; this is deduced by type class inference from the fact that `with_top (with_bot L)` is a complete linear order if `L` is a conditionally complete linear order. Coercions from `ℝ` and from `ℝ≥0∞` are registered, and their basic properties are proved. The main one is the real coercion, and is usually referred to just as `coe` (lemmas such as `ereal.coe_add` deal with this coercion). The one from `ennreal` is usually called `coe_ennreal` in the `ereal` namespace. ## Tags real, ereal, complete lattice ## TODO abs : ereal → ℝ≥0∞ In Isabelle they define + - * and / (making junk choices for things like -∞ + ∞) and then prove whatever bits of the ordered ring/field axioms still hold. They also do some limits stuff (liminf/limsup etc). See https://isabelle.in.tum.de/dist/library/HOL/HOL-Library/Extended_Real.html -/ open_locale ennreal nnreal /-- ereal : The type `[-∞, ∞]` -/ @[derive [order_bot, order_top, comm_monoid_with_zero, has_Sup, has_Inf, complete_linear_order, linear_ordered_add_comm_monoid_with_top]] def ereal := with_top (with_bot ℝ) /-- The canonical inclusion froms reals to ereals. Do not use directly: as this is registered as a coercion, use the coercion instead. -/ def real.to_ereal : ℝ → ereal := some ∘ some namespace ereal @[simp] lemma bot_lt_top : (⊥ : ereal) < ⊤ := with_top.coe_lt_top _ @[simp] lemma bot_ne_top : (⊥ : ereal) ≠ ⊤ := bot_lt_top.ne instance : has_coe ℝ ereal := ⟨real.to_ereal⟩ @[simp, norm_cast] protected lemma coe_le_coe_iff {x y : ℝ} : (x : ereal) ≤ (y : ereal) ↔ x ≤ y := by { unfold_coes, simp [real.to_ereal] } @[simp, norm_cast] protected lemma coe_lt_coe_iff {x y : ℝ} : (x : ereal) < (y : ereal) ↔ x < y := by { unfold_coes, simp [real.to_ereal] } @[simp, norm_cast] protected lemma coe_eq_coe_iff {x y : ℝ} : (x : ereal) = (y : ereal) ↔ x = y := by { unfold_coes, simp [real.to_ereal, option.some_inj] } /-- The canonical map from nonnegative extended reals to extended reals -/ def _root_.ennreal.to_ereal : ℝ≥0∞ → ereal | ⊤ := ⊤ | (some x) := x.1 instance has_coe_ennreal : has_coe ℝ≥0∞ ereal := ⟨ennreal.to_ereal⟩ instance : has_zero ereal := ⟨(0 : ℝ)⟩ instance : inhabited ereal := ⟨0⟩ /-- A way to case on an element of `ereal`, separating the bot, real and top cases. A typical invocation looks like `rcases x.cases with rfl|⟨x, rfl⟩|rfl` -/ protected lemma cases : ∀ (a : ereal), a = ⊥ ∨ (∃ (x : ℝ), a = x) ∨ a = ⊤ | ⊤ := by simp | ⊥ := by simp | (a : ℝ) := by simp /-! ### Real coercion -/ instance : can_lift ereal ℝ := { coe := coe, cond := λ r, r ≠ ⊤ ∧ r ≠ ⊥, prf := λ x hx, begin rcases x.cases with rfl|⟨x, rfl⟩|rfl, { simpa using hx }, { simp }, { simpa using hx } end } /-- The map from extended reals to reals sending infinities to zero. -/ def to_real : ereal → ℝ | ⊥ := 0 | ⊤ := 0 | (x : ℝ) := x @[simp] lemma to_real_top : to_real ⊤ = 0 := rfl @[simp] lemma to_real_bot : to_real ⊥ = 0 := rfl @[simp] lemma to_real_zero : to_real 0 = 0 := rfl @[simp] lemma to_real_coe (x : ℝ) : to_real (x : ereal) = x := rfl @[simp] lemma bot_lt_coe (x : ℝ) : (⊥ : ereal) < x := by { apply with_top.coe_lt_coe.2, exact with_bot.bot_lt_coe _ } @[simp] lemma coe_ne_bot (x : ℝ) : (x : ereal) ≠ ⊥ := (bot_lt_coe x).ne' @[simp] lemma bot_ne_coe (x : ℝ) : (⊥ : ereal) ≠ x := (bot_lt_coe x).ne @[simp] lemma coe_lt_top (x : ℝ) : (x : ereal) < ⊤ := with_top.coe_lt_top _ @[simp] lemma coe_ne_top (x : ℝ) : (x : ereal) ≠ ⊤ := (coe_lt_top x).ne @[simp] lemma top_ne_coe (x : ℝ) : (⊤ : ereal) ≠ x := (coe_lt_top x).ne' @[simp] lemma bot_lt_zero : (⊥ : ereal) < 0 := bot_lt_coe 0 @[simp] lemma bot_ne_zero : (⊥ : ereal) ≠ 0 := (coe_ne_bot 0).symm @[simp] lemma zero_ne_bot : (0 : ereal) ≠ ⊥ := coe_ne_bot 0 @[simp] lemma zero_lt_top : (0 : ereal) < ⊤ := coe_lt_top 0 @[simp] lemma zero_ne_top : (0 : ereal) ≠ ⊤ := coe_ne_top 0 @[simp] lemma top_ne_zero : (⊤ : ereal) ≠ 0 := (coe_ne_top 0).symm @[simp, norm_cast] lemma coe_add (x y : ℝ) : ((x + y : ℝ) : ereal) = (x : ereal) + (y : ereal) := rfl @[simp] lemma coe_zero : ((0 : ℝ) : ereal) = 0 := rfl lemma to_real_le_to_real {x y : ereal} (h : x ≤ y) (hx : x ≠ ⊥) (hy : y ≠ ⊤) : x.to_real ≤ y.to_real := begin lift x to ℝ, lift y to ℝ, { simpa using h }, { simp [hy, ((bot_lt_iff_ne_bot.2 hx).trans_le h).ne'] }, { simp [hx, (h.trans_lt (lt_top_iff_ne_top.2 hy)).ne], }, end lemma coe_to_real {x : ereal} (hx : x ≠ ⊤) (h'x : x ≠ ⊥) : (x.to_real : ereal) = x := begin rcases x.cases with rfl|⟨x, rfl⟩|rfl, { simpa using h'x }, { refl }, { simpa using hx }, end /-! ### ennreal coercion -/ @[simp] lemma to_real_coe_ennreal : ∀ {x : ℝ≥0∞}, to_real (x : ereal) = ennreal.to_real x | ⊤ := rfl | (some x) := rfl lemma coe_nnreal_eq_coe_real (x : ℝ≥0) : ((x : ℝ≥0∞) : ereal) = (x : ℝ) := rfl @[simp] lemma coe_ennreal_top : ((⊤ : ℝ≥0∞) : ereal) = ⊤ := rfl @[simp] lemma coe_ennreal_eq_top_iff : ∀ {x : ℝ≥0∞}, (x : ereal) = ⊤ ↔ x = ⊤ | ⊤ := by simp | (some x) := by { simp only [ennreal.coe_ne_top, iff_false, ennreal.some_eq_coe], dec_trivial } lemma coe_nnreal_ne_top (x : ℝ≥0) : ((x : ℝ≥0∞) : ereal) ≠ ⊤ := dec_trivial @[simp] lemma coe_nnreal_lt_top (x : ℝ≥0) : ((x : ℝ≥0∞) : ereal) < ⊤ := dec_trivial @[simp, norm_cast] lemma coe_ennreal_le_coe_ennreal_iff : ∀ {x y : ℝ≥0∞}, (x : ereal) ≤ (y : ereal) ↔ x ≤ y | x ⊤ := by simp | ⊤ (some y) := by simp | (some x) (some y) := by simp [coe_nnreal_eq_coe_real] @[simp, norm_cast] lemma coe_ennreal_lt_coe_ennreal_iff : ∀ {x y : ℝ≥0∞}, (x : ereal) < (y : ereal) ↔ x < y | ⊤ ⊤ := by simp | (some x) ⊤ := by simp | ⊤ (some y) := by simp | (some x) (some y) := by simp [coe_nnreal_eq_coe_real] @[simp, norm_cast] lemma coe_ennreal_eq_coe_ennreal_iff : ∀ {x y : ℝ≥0∞}, (x : ereal) = (y : ereal) ↔ x = y | ⊤ ⊤ := by simp | (some x) ⊤ := by simp | ⊤ (some y) := by simp [(coe_nnreal_lt_top y).ne'] | (some x) (some y) := by simp [coe_nnreal_eq_coe_real] lemma coe_ennreal_nonneg (x : ℝ≥0∞) : (0 : ereal) ≤ x := coe_ennreal_le_coe_ennreal_iff.2 (zero_le x) @[simp] lemma bot_lt_coe_ennreal (x : ℝ≥0∞) : (⊥ : ereal) < x := (bot_lt_coe 0).trans_le (coe_ennreal_nonneg _) @[simp] lemma coe_ennreal_ne_bot (x : ℝ≥0∞) : (x : ereal) ≠ ⊥ := (bot_lt_coe_ennreal x).ne' @[simp, norm_cast] lemma coe_ennreal_add : ∀ (x y : ennreal), ((x + y : ℝ≥0∞) : ereal) = x + y | ⊤ y := rfl | x ⊤ := by simp | (some x) (some y) := rfl @[simp] lemma coe_ennreal_zero : ((0 : ℝ≥0∞) : ereal) = 0 := rfl /-! ### Order -/ lemma exists_rat_btwn_of_lt : Π {a b : ereal} (hab : a < b), ∃ (x : ℚ), a < (x : ℝ) ∧ ((x : ℝ) : ereal) < b | ⊤ b h := (not_top_lt h).elim | (a : ℝ) ⊥ h := (lt_irrefl _ ((bot_lt_coe a).trans h)).elim | (a : ℝ) (b : ℝ) h := by simp [exists_rat_btwn (ereal.coe_lt_coe_iff.1 h)] | (a : ℝ) ⊤ h := let ⟨b, hab⟩ := exists_rat_gt a in ⟨b, by simpa using hab, coe_lt_top _⟩ | ⊥ ⊥ h := (lt_irrefl _ h).elim | ⊥ (a : ℝ) h := let ⟨b, hab⟩ := exists_rat_lt a in ⟨b, bot_lt_coe _, by simpa using hab⟩ | ⊥ ⊤ h := ⟨0, bot_lt_coe _, coe_lt_top _⟩ lemma lt_iff_exists_rat_btwn {a b : ereal} : a < b ↔ ∃ (x : ℚ), a < (x : ℝ) ∧ ((x : ℝ) : ereal) < b := ⟨λ hab, exists_rat_btwn_of_lt hab, λ ⟨x, ax, xb⟩, ax.trans xb⟩ lemma lt_iff_exists_real_btwn {a b : ereal} : a < b ↔ ∃ (x : ℝ), a < x ∧ (x : ereal) < b := ⟨λ hab, let ⟨x, ax, xb⟩ := exists_rat_btwn_of_lt hab in ⟨(x : ℝ), ax, xb⟩, λ ⟨x, ax, xb⟩, ax.trans xb⟩ /-- The set of numbers in `ereal` that are not equal to `±∞` is equivalent to `ℝ`. -/ def ne_top_bot_equiv_real : ({⊥, ⊤} : set ereal).compl ≃ ℝ := { to_fun := λ x, ereal.to_real x, inv_fun := λ x, ⟨x, by simp⟩, left_inv := λ ⟨x, hx⟩, subtype.eq $ begin lift x to ℝ, { simp }, { simpa [not_or_distrib, and_comm] using hx } end, right_inv := λ x, by simp } /-! ### Addition -/ @[simp] lemma add_top (x : ereal) : x + ⊤ = ⊤ := add_top _ @[simp] lemma top_add (x : ereal) : ⊤ + x = ⊤ := top_add _ @[simp] lemma bot_add_bot : (⊥ : ereal) + ⊥ = ⊥ := rfl @[simp] lemma bot_add_coe (x : ℝ) : (⊥ : ereal) + x = ⊥ := rfl @[simp] lemma coe_add_bot (x : ℝ) : (x : ereal) + ⊥ = ⊥ := rfl lemma to_real_add : ∀ {x y : ereal} (hx : x ≠ ⊤) (h'x : x ≠ ⊥) (hy : y ≠ ⊤) (h'y : y ≠ ⊥), to_real (x + y) = to_real x + to_real y | ⊥ y hx h'x hy h'y := (h'x rfl).elim | ⊤ y hx h'x hy h'y := (hx rfl).elim | x ⊤ hx h'x hy h'y := (hy rfl).elim | x ⊥ hx h'x hy h'y := (h'y rfl).elim | (x : ℝ) (y : ℝ) hx h'x hy h'y := by simp [← ereal.coe_add] lemma add_lt_add_right_coe {x y : ereal} (h : x < y) (z : ℝ) : x + z < y + z := begin rcases x.cases with rfl|⟨x, rfl⟩|rfl; rcases y.cases with rfl|⟨y, rfl⟩|rfl, { exact (lt_irrefl _ h).elim }, { simp only [bot_lt_coe, bot_add_coe, ← coe_add] }, { simp }, { exact (lt_irrefl _ (h.trans (bot_lt_coe x))).elim }, { norm_cast at h ⊢, exact add_lt_add_right h _ }, { simp only [← coe_add, top_add, coe_lt_top] }, { exact (lt_irrefl _ (h.trans_le le_top)).elim }, { exact (lt_irrefl _ (h.trans_le le_top)).elim }, { exact (lt_irrefl _ (h.trans_le le_top)).elim }, end lemma add_lt_add_of_lt_of_le {x y z t : ereal} (h : x < y) (h' : z ≤ t) (hz : z ≠ ⊥) (ht : t ≠ ⊤) : x + z < y + t := begin rcases z.cases with rfl|⟨z, rfl⟩|rfl, { simpa only using hz }, { calc x + z < y + z : add_lt_add_right_coe h _ ... ≤ y + t : add_le_add (le_refl _) h' }, { exact (ht (top_le_iff.1 h')).elim } end lemma add_lt_add_left_coe {x y : ereal} (h : x < y) (z : ℝ) : (z : ereal) + x < z + y := by simpa [add_comm] using add_lt_add_right_coe h z lemma add_lt_add {x y z t : ereal} (h1 : x < y) (h2 : z < t) : x + z < y + t := begin rcases y.cases with rfl|⟨y, rfl⟩|rfl, { exact (lt_irrefl _ (bot_le.trans_lt h1)).elim }, { calc x + z ≤ y + z : add_le_add h1.le (le_refl _) ... < y + t : add_lt_add_left_coe h2 _ }, { simp [lt_top_iff_ne_top, with_top.add_eq_top, h1.ne, (h2.trans_le le_top).ne] } end @[simp] lemma add_eq_top_iff {x y : ereal} : x + y = ⊤ ↔ x = ⊤ ∨ y = ⊤ := begin rcases x.cases with rfl|⟨x, rfl⟩|rfl; rcases y.cases with rfl|⟨x, rfl⟩|rfl; simp [← ereal.coe_add], end @[simp] lemma add_lt_top_iff {x y : ereal} : x + y < ⊤ ↔ x < ⊤ ∧ y < ⊤ := by simp [lt_top_iff_ne_top, not_or_distrib] /-! ### Negation -/ /-- negation on `ereal` -/ protected def neg : ereal → ereal | ⊥ := ⊤ | ⊤ := ⊥ | (x : ℝ) := (-x : ℝ) instance : has_neg ereal := ⟨ereal.neg⟩ @[norm_cast] protected lemma neg_def (x : ℝ) : ((-x : ℝ) : ereal) = -x := rfl @[simp] lemma neg_top : - (⊤ : ereal) = ⊥ := rfl @[simp] lemma neg_bot : - (⊥ : ereal) = ⊤ := rfl @[simp] lemma neg_zero : - (0 : ereal) = 0 := by { change ((-0 : ℝ) : ereal) = 0, simp } /-- `- -a = a` on `ereal`. -/ @[simp] protected theorem neg_neg : ∀ (a : ereal), - (- a) = a | ⊥ := rfl | ⊤ := rfl | (a : ℝ) := by { norm_cast, simp [neg_neg a] } theorem neg_inj {a b : ereal} (h : -a = -b) : a = b := by rw [←ereal.neg_neg a, h, ereal.neg_neg b] @[simp] theorem neg_eq_neg_iff (a b : ereal) : - a = - b ↔ a = b := ⟨λ h, neg_inj h, λ h, by rw [h]⟩ @[simp] lemma to_real_neg : ∀ {a : ereal}, to_real (-a) = - to_real a | ⊤ := by simp | ⊥ := by simp | (x : ℝ) := rfl /-- Even though `ereal` is not an additive group, `-a = b ↔ -b = a` still holds -/ theorem neg_eq_iff_neg_eq {a b : ereal} : -a = b ↔ -b = a := ⟨by {intro h, rw ←h, exact ereal.neg_neg a}, by {intro h, rw ←h, exact ereal.neg_neg b}⟩ @[simp] lemma neg_eg_top_iff {x : ereal} : - x = ⊤ ↔ x = ⊥ := by { rw neg_eq_iff_neg_eq, simp [eq_comm] } @[simp] lemma neg_eg_bot_iff {x : ereal} : - x = ⊥ ↔ x = ⊤ := by { rw neg_eq_iff_neg_eq, simp [eq_comm] } @[simp] lemma neg_eg_zero_iff {x : ereal} : - x = 0 ↔ x = 0 := by { rw neg_eq_iff_neg_eq, simp [eq_comm] } /-- if `-a ≤ b` then `-b ≤ a` on `ereal`. -/ protected theorem neg_le_of_neg_le : ∀ {a b : ereal} (h : -a ≤ b), -b ≤ a | ⊥ ⊥ h := h | ⊥ (some b) h := by cases (top_le_iff.1 h) | ⊤ l h := le_top | (a : ℝ) ⊥ h := by cases (le_bot_iff.1 h) | l ⊤ h := bot_le | (a : ℝ) (b : ℝ) h := by { norm_cast at h ⊢, exact _root_.neg_le_of_neg_le h } /-- `-a ≤ b ↔ -b ≤ a` on `ereal`. -/ protected theorem neg_le {a b : ereal} : -a ≤ b ↔ -b ≤ a := ⟨ereal.neg_le_of_neg_le, ereal.neg_le_of_neg_le⟩ /-- `a ≤ -b → b ≤ -a` on ereal -/ theorem le_neg_of_le_neg {a b : ereal} (h : a ≤ -b) : b ≤ -a := by rwa [←ereal.neg_neg b, ereal.neg_le, ereal.neg_neg] @[simp] lemma neg_le_neg_iff {a b : ereal} : - a ≤ - b ↔ b ≤ a := by conv_lhs { rw [ereal.neg_le, ereal.neg_neg] } @[simp, norm_cast] lemma coe_neg (x : ℝ) : ((- x : ℝ) : ereal) = - (x : ereal) := rfl /-- Negation as an order reversing isomorphism on `ereal`. -/ def neg_order_iso : ereal ≃o (order_dual ereal) := { to_fun := ereal.neg, inv_fun := ereal.neg, left_inv := ereal.neg_neg, right_inv := ereal.neg_neg, map_rel_iff' := λ x y, neg_le_neg_iff } lemma neg_lt_of_neg_lt {a b : ereal} (h : -a < b) : -b < a := begin apply lt_of_le_of_ne (ereal.neg_le_of_neg_le h.le), assume H, rw [← H, ereal.neg_neg] at h, exact lt_irrefl _ h end lemma neg_lt_iff_neg_lt {a b : ereal} : -a < b ↔ -b < a := ⟨λ h, ereal.neg_lt_of_neg_lt h, λ h, ereal.neg_lt_of_neg_lt h⟩ /-! ### Subtraction -/ /-- Subtraction on `ereal`, defined by `x - y = x + (-y)`. Since addition is badly behaved at some points, so is subtraction. There is no standard algebraic typeclass involving subtraction that is registered on `ereal` because of this bad behavior. -/ protected noncomputable def sub (x y : ereal) : ereal := x + (-y) noncomputable instance : has_sub ereal := ⟨ereal.sub⟩ @[simp] lemma top_sub (x : ereal) : ⊤ - x = ⊤ := top_add x @[simp] lemma sub_bot (x : ereal) : x - ⊥ = ⊤ := add_top x @[simp] lemma bot_sub_top : (⊥ : ereal) - ⊤ = ⊥ := rfl @[simp] lemma bot_sub_coe (x : ℝ) : (⊥ : ereal) - x = ⊥ := rfl @[simp] lemma coe_sub_bot (x : ℝ) : (x : ereal) - ⊤ = ⊥ := rfl @[simp] lemma sub_zero (x : ereal) : x - 0 = x := by { change x + (-0) = x, simp } @[simp] lemma zero_sub (x : ereal) : 0 - x = - x := by { change 0 + (-x) = - x, simp } lemma sub_eq_add_neg (x y : ereal) : x - y = x + -y := rfl lemma sub_le_sub {x y z t : ereal} (h : x ≤ y) (h' : t ≤ z) : x - z ≤ y - t := add_le_add h (neg_le_neg_iff.2 h') lemma sub_lt_sub_of_lt_of_le {x y z t : ereal} (h : x < y) (h' : z ≤ t) (hz : z ≠ ⊥) (ht : t ≠ ⊤) : x - t < y - z := add_lt_add_of_lt_of_le h (neg_le_neg_iff.2 h') (by simp [ht]) (by simp [hz]) lemma coe_real_ereal_eq_coe_to_nnreal_sub_coe_to_nnreal (x : ℝ) : (x : ereal) = real.to_nnreal x - real.to_nnreal (-x) := begin rcases le_or_lt 0 x with h|h, { have : real.to_nnreal x = ⟨x, h⟩, by { ext, simp [h] }, simp only [real.to_nnreal_of_nonpos (neg_nonpos.mpr h), this, sub_zero, ennreal.coe_zero, coe_ennreal_zero, coe_coe], refl }, { have : (x : ereal) = - (- x : ℝ), by simp, conv_lhs { rw this }, have : real.to_nnreal (-x) = ⟨-x, neg_nonneg.mpr h.le⟩, by { ext, simp [neg_nonneg.mpr h.le], }, simp only [real.to_nnreal_of_nonpos h.le, this, zero_sub, neg_eq_neg_iff, coe_neg, ennreal.coe_zero, coe_ennreal_zero, coe_coe], refl } end lemma to_real_sub {x y : ereal} (hx : x ≠ ⊤) (h'x : x ≠ ⊥) (hy : y ≠ ⊤) (h'y : y ≠ ⊥) : to_real (x - y) = to_real x - to_real y := begin rw [ereal.sub_eq_add_neg, to_real_add hx h'x, to_real_neg], { refl }, { simpa using hy }, { simpa using h'y } end /-! ### Multiplication -/ @[simp] lemma coe_one : ((1 : ℝ) : ereal) = 1 := rfl @[simp, norm_cast] lemma coe_mul (x y : ℝ) : ((x * y : ℝ) : ereal) = (x : ereal) * (y : ereal) := eq.trans (with_bot.coe_eq_coe.mpr with_bot.coe_mul) with_top.coe_mul @[simp] lemma mul_top (x : ereal) (h : x ≠ 0) : x * ⊤ = ⊤ := with_top.mul_top h @[simp] lemma top_mul (x : ereal) (h : x ≠ 0) : ⊤ * x = ⊤ := with_top.top_mul h @[simp] lemma bot_mul_bot : (⊥ : ereal) * ⊥ = ⊥ := rfl @[simp] lemma bot_mul_coe (x : ℝ) (h : x ≠ 0) : (⊥ : ereal) * x = ⊥ := with_top.coe_mul.symm.trans $ with_bot.coe_eq_coe.mpr $ with_bot.bot_mul $ function.injective.ne (@option.some.inj _) h @[simp] lemma coe_mul_bot (x : ℝ) (h : x ≠ 0) : (x : ereal) * ⊥ = ⊥ := with_top.coe_mul.symm.trans $ with_bot.coe_eq_coe.mpr $ with_bot.mul_bot $ function.injective.ne (@option.some.inj _) h @[simp] lemma to_real_one : to_real 1 = 1 := rfl lemma to_real_mul : ∀ {x y : ereal}, to_real (x * y) = to_real x * to_real y | ⊤ y := by by_cases hy : y = 0; simp [hy] | x ⊤ := by by_cases hx : x = 0; simp [hx] | (x : ℝ) (y : ℝ) := by simp [← ereal.coe_mul] | ⊥ (y : ℝ) := by by_cases hy : y = 0; simp [hy] | (x : ℝ) ⊥ := by by_cases hx : x = 0; simp [hx] | ⊥ ⊥ := by simp end ereal
2d38cc6088e97746dc0649eef53adebe7f439133
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/data/finset/fold.lean
93970566ca4d20072169f3fc4b51d7e4aeaba717
[ "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
6,325
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.finset.basic import data.multiset.fold /-! # The fold operation for a commutative associative operation over a finset. -/ namespace finset open multiset variables {α β γ : Type*} /-! ### fold -/ section fold variables (op : β → β → β) [hc : is_commutative β op] [ha : is_associative β op] local notation a * b := op a b include hc ha /-- `fold op b f s` folds the commutative associative operation `op` over the `f`-image of `s`, i.e. `fold (+) b f {1,2,3} = `f 1 + f 2 + f 3 + b`. -/ def fold (b : β) (f : α → β) (s : finset α) : β := (s.1.map f).fold op b variables {op} {f : α → β} {b : β} {s : finset α} {a : α} @[simp] theorem fold_empty : (∅ : finset α).fold op b f = b := rfl @[simp] theorem fold_cons (h : a ∉ s) : (cons a s h).fold op b f = f a * s.fold op b f := by { dunfold fold, rw [cons_val, map_cons, fold_cons_left], } @[simp] theorem fold_insert [decidable_eq α] (h : a ∉ s) : (insert a s).fold op b f = f a * s.fold op b f := by unfold fold; rw [insert_val, ndinsert_of_not_mem h, map_cons, fold_cons_left] @[simp] theorem fold_singleton : ({a} : finset α).fold op b f = f a * b := rfl @[simp] theorem fold_map {g : γ ↪ α} {s : finset γ} : (s.map g).fold op b f = s.fold op b (f ∘ g) := by simp only [fold, map, multiset.map_map] @[simp] theorem fold_image [decidable_eq α] {g : γ → α} {s : finset γ} (H : ∀ (x ∈ s) (y ∈ s), g x = g y → x = y) : (s.image g).fold op b f = s.fold op b (f ∘ g) := by simp only [fold, image_val_of_inj_on H, multiset.map_map] @[congr] theorem fold_congr {g : α → β} (H : ∀ x ∈ s, f x = g x) : s.fold op b f = s.fold op b g := by rw [fold, fold, map_congr H] theorem fold_op_distrib {f g : α → β} {b₁ b₂ : β} : s.fold op (b₁ * b₂) (λx, f x * g x) = s.fold op b₁ f * s.fold op b₂ g := by simp only [fold, fold_distrib] theorem fold_hom {op' : γ → γ → γ} [is_commutative γ op'] [is_associative γ op'] {m : β → γ} (hm : ∀x y, m (op x y) = op' (m x) (m y)) : s.fold op' (m b) (λx, m (f x)) = m (s.fold op b f) := by rw [fold, fold, ← fold_hom op hm, multiset.map_map] theorem fold_union_inter [decidable_eq α] {s₁ s₂ : finset α} {b₁ b₂ : β} : (s₁ ∪ s₂).fold op b₁ f * (s₁ ∩ s₂).fold op b₂ f = s₁.fold op b₂ f * s₂.fold op b₁ f := by unfold fold; rw [← fold_add op, ← map_add, union_val, inter_val, union_add_inter, map_add, hc.comm, fold_add] @[simp] theorem fold_insert_idem [decidable_eq α] [hi : is_idempotent β op] : (insert a s).fold op b f = f a * s.fold op b f := begin by_cases (a ∈ s), { rw [← insert_erase h], simp [← ha.assoc, hi.idempotent] }, { apply fold_insert h }, end theorem fold_image_idem [decidable_eq α] {g : γ → α} {s : finset γ} [hi : is_idempotent β op] : (image g s).fold op b f = s.fold op b (f ∘ g) := begin induction s using finset.cons_induction with x xs hx ih, { rw [fold_empty, image_empty, fold_empty] }, { haveI := classical.dec_eq γ, rw [fold_cons, cons_eq_insert, image_insert, fold_insert_idem, ih], } end lemma fold_op_rel_iff_and {r : β → β → Prop} (hr : ∀ {x y z}, r x (op y z) ↔ (r x y ∧ r x z)) {c : β} : r c (s.fold op b f) ↔ (r c b ∧ ∀ x∈s, r c (f x)) := begin classical, apply finset.induction_on s, { simp }, clear s, intros a s ha IH, rw [finset.fold_insert ha, hr, IH, ← and_assoc, and_comm (r c (f a)), and_assoc], apply and_congr iff.rfl, split, { rintro ⟨h₁, h₂⟩, intros b hb, rw finset.mem_insert at hb, rcases hb with rfl|hb; solve_by_elim }, { intro h, split, { exact h a (finset.mem_insert_self _ _), }, { intros b hb, apply h b, rw finset.mem_insert, right, exact hb } } end lemma fold_op_rel_iff_or {r : β → β → Prop} (hr : ∀ {x y z}, r x (op y z) ↔ (r x y ∨ r x z)) {c : β} : r c (s.fold op b f) ↔ (r c b ∨ ∃ x∈s, r c (f x)) := begin classical, apply finset.induction_on s, { simp }, clear s, intros a s ha IH, rw [finset.fold_insert ha, hr, IH, ← or_assoc, or_comm (r c (f a)), or_assoc], apply or_congr iff.rfl, split, { rintro (h₁|⟨x, hx, h₂⟩), { use a, simp [h₁] }, { refine ⟨x, by simp [hx], h₂⟩ } }, { rintro ⟨x, hx, h⟩, rw mem_insert at hx, cases hx, { left, rwa hx at h }, { right, exact ⟨x, hx, h⟩ } } end omit hc ha @[simp] lemma fold_union_empty_singleton [decidable_eq α] (s : finset α) : finset.fold (∪) ∅ singleton s = s := begin apply finset.induction_on s, { simp only [fold_empty], }, { intros a s has ih, rw [fold_insert has, ih, insert_eq], } end lemma fold_sup_bot_singleton [decidable_eq α] (s : finset α) : finset.fold (⊔) ⊥ singleton s = s := fold_union_empty_singleton s section order variables [linear_order β] (c : β) lemma le_fold_min : c ≤ s.fold min b f ↔ (c ≤ b ∧ ∀ x∈s, c ≤ f x) := fold_op_rel_iff_and $ λ x y z, le_min_iff lemma fold_min_le : s.fold min b f ≤ c ↔ (b ≤ c ∨ ∃ x∈s, f x ≤ c) := begin show _ ≥ _ ↔ _, apply fold_op_rel_iff_or, intros x y z, show _ ≤ _ ↔ _, exact min_le_iff end lemma lt_fold_min : c < s.fold min b f ↔ (c < b ∧ ∀ x∈s, c < f x) := fold_op_rel_iff_and $ λ x y z, lt_min_iff lemma fold_min_lt : s.fold min b f < c ↔ (b < c ∨ ∃ x∈s, f x < c) := begin show _ > _ ↔ _, apply fold_op_rel_iff_or, intros x y z, show _ < _ ↔ _, exact min_lt_iff end lemma fold_max_le : s.fold max b f ≤ c ↔ (b ≤ c ∧ ∀ x∈s, f x ≤ c) := begin show _ ≥ _ ↔ _, apply fold_op_rel_iff_and, intros x y z, show _ ≤ _ ↔ _, exact max_le_iff end lemma le_fold_max : c ≤ s.fold max b f ↔ (c ≤ b ∨ ∃ x∈s, c ≤ f x) := fold_op_rel_iff_or $ λ x y z, le_max_iff lemma fold_max_lt : s.fold max b f < c ↔ (b < c ∧ ∀ x∈s, f x < c) := begin show _ > _ ↔ _, apply fold_op_rel_iff_and, intros x y z, show _ < _ ↔ _, exact max_lt_iff end lemma lt_fold_max : c < s.fold max b f ↔ (c < b ∨ ∃ x∈s, c < f x) := fold_op_rel_iff_or $ λ x y z, lt_max_iff end order end fold end finset
cd10ee2d1f44df3628b88719af77a725b82b7e89
75c54c8946bb4203e0aaf196f918424a17b0de99
/src/bvm_extras2.lean
d6587e6470864264f462ec6ddc3a487e6c583d81
[ "Apache-2.0" ]
permissive
urkud/flypitch
261e2a45f1038130178575406df8aea78255ba77
2250f5eda14b6ef9fc3e4e1f4a9ac4005634de5c
refs/heads/master
1,653,266,469,246
1,577,819,679,000
1,577,819,679,000
259,862,235
1
0
Apache-2.0
1,588,147,244,000
1,588,147,244,000
null
UTF-8
Lean
false
false
31,659
lean
import .bvm_extras open lattice universe u local infix ` ⟹ `:65 := lattice.imp local infix ` ⇔ `:50 := lattice.biimp local infix `≺`:75 := (λ x y, -(bSet.larger_than x y)) local infix `≼`:75 := (λ x y, bSet.injects_into x y) namespace bSet section lemmas variables {𝔹 : Type u} [nontrivial_complete_boolean_algebra 𝔹] {Γ : 𝔹} lemma prod_subset {x₁ x₂ y₁ y₂ : bSet 𝔹} (H_sub₁ : Γ ≤ x₁ ⊆ᴮ x₂) (H_sub₂ : Γ ≤ y₁ ⊆ᴮ y₂) : Γ ≤ prod x₁ y₁ ⊆ᴮ prod x₂ y₂ := begin rw subset_unfold', bv_intro pr, bv_imp_intro Hpr, rw mem_prod_iff₂ at Hpr ⊢, rcases Hpr with ⟨v,Hv,w,Hw,H_eq⟩, have Hv' := mem_of_mem_subset H_sub₁ Hv, have Hw' := mem_of_mem_subset H_sub₂ Hw, exact ⟨v,‹_›,w,‹_›,‹_›⟩ end lemma prod_subset_left {x₁ x₂ y : bSet 𝔹} (H_sub : Γ ≤ x₁ ⊆ᴮ x₂) : Γ ≤ prod x₁ y ⊆ᴮ prod x₂ y := prod_subset H_sub subset_self lemma prod_subset_right {x y₁ y₂ : bSet 𝔹} (H_sub : Γ ≤ y₁ ⊆ᴮ y₂) : Γ ≤ prod x y₁ ⊆ᴮ prod x y₂ := prod_subset subset_self H_sub end lemmas section inj_inverse_surj variables {𝔹 : Type u} [nontrivial_complete_boolean_algebra 𝔹] {x y f : bSet 𝔹} {Γ : 𝔹} (H_func : Γ ≤ is_func' x y f) (H_inj : Γ ≤ is_inj f) lemma inj_inverse.is_total_surj (H_surj : Γ ≤ is_surj x y f) : Γ ≤ is_total y x (inj_inverse H_func H_inj) := begin have := bv_symm (image_eq_codomain_of_surj H_surj), apply @bv_rw' _ _ _ _ _ this (λ z, is_total z x (inj_inverse H_func H_inj)), simp, apply inj_inverse.is_total end lemma inj_inverse.is_function_surj (H_surj : Γ ≤ is_surj x y f) : Γ ≤ is_function y x (inj_inverse H_func H_inj) := begin have := bv_symm (image_eq_codomain_of_surj H_surj), apply @bv_rw' _ _ _ _ _ this (λ z, is_function z x (inj_inverse H_func H_inj)), simp, apply inj_inverse.is_function end lemma inj_inverse.is_surj_surj (H_surj : Γ ≤ is_surj x y f) : Γ ≤ is_surj y x (inj_inverse H_func H_inj) := begin apply @bv_rw' _ _ _ _ _ (bv_symm (image_eq_codomain_of_surj H_surj)) (λ z, is_surj z x (inj_inverse H_func H_inj)), simp, apply inj_inverse.is_surj end end inj_inverse_surj section Ord variables {𝔹 : Type u} [nontrivial_complete_boolean_algebra 𝔹] {Γ : 𝔹} lemma subset_of_mem_Ord {x y : bSet 𝔹} {Γ} (H_mem : Γ ≤ x ∈ᴮ y) (H_Ord : Γ ≤ Ord y) : Γ ≤ x ⊆ᴮ y := subset_of_mem_transitive (bv_and.right ‹_›) ‹_› lemma mem_of_mem_Ord {x y z : bSet 𝔹} {Γ} (H_mem : Γ ≤ x ∈ᴮ y) (H_mem' : Γ ≤ y ∈ᴮ z) (H_ord₂ : Γ ≤ Ord z) : Γ ≤ x ∈ᴮ z := begin refine mem_of_mem_subset _ H_mem, apply subset_of_mem_Ord; from ‹_› end -- @[reducible]def Ord_max {x y : bSet 𝔹} {Γ : 𝔹} (H₁ : Γ ≤ Ord x) (H₂ : Γ ≤ Ord y) : bSet 𝔹 := -- succ (binary_union x y) lemma transitive_union {u : bSet 𝔹} {Γ : 𝔹} (Hu : Γ ≤ ⨅z, z ∈ᴮ u ⟹ is_transitive z) : Γ ≤ is_transitive (bv_union u) := begin bv_intro x, bv_imp_intro H_mem, rw mem_bv_union_iff at H_mem, bv_cases_at H_mem y Hy, bv_split_at Hy, rw subset_unfold', bv_intro w, bv_imp_intro Hw, rw mem_bv_union_iff, apply bv_use y, refine le_inf ‹_› _, simp only [is_transitive] at Hu, exact mem_of_mem_subset (Hu y ‹_› x ‹_›) ‹_› end lemma transitive_binary_inter {x y : bSet 𝔹} {Γ} (H₁ : Γ ≤ Ord x) (H₂ : Γ ≤ Ord y) : Γ ≤ is_transitive (x ∩ᴮ y) := begin bv_intro z, bv_imp_intro H_mem, rw mem_binary_inter_iff at H_mem, cases H_mem with H_mem₁ H_mem₂, rw subset_unfold', bv_intro w, bv_imp_intro Hw, rw mem_binary_inter_iff, refine ⟨_,_⟩, { have := (bv_and.right H₁), unfold is_transitive at this, exact mem_of_mem_subset (this z ‹_›) ‹_› }, { have := (bv_and.right H₂), unfold is_transitive at this, exact mem_of_mem_subset (this z ‹_›) ‹_› } end lemma epsilon_trichotomy_binary_inter {x y : bSet 𝔹} {Γ} (H₁ : Γ ≤ Ord x) : Γ ≤ epsilon_trichotomy (x ∩ᴮ y) := begin bv_intro w, bv_imp_intro Hw_mem, bv_intro z, bv_imp_intro Hz_mem, rw mem_binary_inter_iff at Hw_mem Hz_mem, cases Hz_mem with Hz_mem_x Hz_mem_y, cases Hw_mem with Hw_mem_x Hw_mem_y, exact epsilon_trichotomy_of_Ord Hw_mem_x Hz_mem_x ‹_› end lemma epsilon_well_founded_binary_inter {x y : bSet 𝔹} {Γ} (H₁ : Γ ≤ Ord x) : Γ ≤ epsilon_well_founded (x ∩ᴮ y) := begin bv_intro w, bv_imp_intro Hw_sub, bv_imp_intro H_nonempty, rcases subset_binary_inter_iff.mp Hw_sub with ⟨Hw_sub₁, Hw_sub₂⟩, exact (bv_and.right (bv_and.left H₁) w) Hw_sub₁ ‹_›, end lemma Ord_binary_inter {x y : bSet 𝔹} {Γ} (H₁ : Γ ≤ Ord x) (H₂ : Γ ≤ Ord y) : Γ ≤ Ord (binary_inter x y) := begin refine le_inf _ _, { from le_inf (epsilon_trichotomy_binary_inter H₁) (epsilon_well_founded_binary_inter ‹_›) }, { bv_intro z, bv_imp_intro H_mem, rw mem_binary_inter_iff at H_mem, cases H_mem with H_mem₁ H_mem₂, rw subset_unfold', bv_intro w, bv_imp_intro Hw, rw mem_binary_inter_iff, refine ⟨_,_⟩, { have := (bv_and.right H₁), unfold is_transitive at this, exact mem_of_mem_subset (this z ‹_›) ‹_› }, { have := (bv_and.right H₂), unfold is_transitive at this, exact mem_of_mem_subset (this z ‹_›) ‹_› }} end section compl def compl (x y : bSet 𝔹) := comprehend (λ z, - (z ∈ᴮ y)) x lemma compl_subset {x y : bSet 𝔹} : Γ ≤ compl x y ⊆ᴮ x := by {rw compl, apply comprehend_subset, simp} lemma mem_compl_iff {x y : bSet 𝔹} {z} : Γ ≤ z ∈ᴮ compl x y ↔ (Γ ≤ z ∈ᴮ x ∧ Γ ≤ -(z ∈ᴮ y)) := begin unfold compl, refine ⟨_,_⟩; intro H, { rw mem_comprehend_iff₂ at H, refine ⟨_,_⟩, { bv_cases_at H w Hw, bv_split, bv_split, bv_cc }, { bv_cases_at H w Hw, bv_split, bv_split, apply bv_rw' Hw_right_left, simp, from ‹_› }, { simp } }, { rw mem_comprehend_iff₂, cases H with H₁ H₂, apply bv_use z, refine le_inf ‹_› (le_inf bv_refl _), from ‹_›, simp } end lemma compl_empty_of_subset {x y : bSet 𝔹} (H_sub : Γ ≤ x ⊆ᴮ y) : Γ ≤ compl x y =ᴮ ∅ := begin apply bv_by_contra, bv_imp_intro H_contra, rw nonempty_iff_exists_mem at H_contra, bv_cases_at H_contra w Hw, rw mem_compl_iff at Hw, cases Hw with Hw₁ Hw₂, suffices : Γ_2 ≤ w ∈ᴮ y, by bv_contradiction, from mem_of_mem_subset ‹_› ‹_› end lemma nonempty_compl_of_ne {x y : bSet 𝔹} (H_ne : Γ ≤ - ( x=ᴮ y)) : Γ ≤ (- ((compl x y) =ᴮ ∅)) ⊔ (- ((compl y x) =ᴮ ∅)) := begin rw bv_eq_unfold' at H_ne, simp only with bv_push_neg at H_ne, bv_or_elim_at H_ne, { refine bv_or_left _, rw nonempty_iff_exists_mem, bv_cases_at H_ne.left z Hz, apply bv_use z, rw mem_compl_iff, bv_split, from ⟨‹_›,‹_›⟩ }, { refine bv_or_right _, rw nonempty_iff_exists_mem, bv_cases_at H_ne.right z Hz, apply bv_use z, rw mem_compl_iff, bv_split, from ⟨‹_›,‹_›⟩ } end end compl lemma eq_iff_not_mem_of_Ord {x y z : bSet 𝔹} (H_mem₁ : Γ ≤ x ∈ᴮ z) (H_mem₂ : Γ ≤ y ∈ᴮ z) (H_ord : Γ ≤ Ord z) : Γ ≤ x =ᴮ y ↔ (Γ ≤ -(x ∈ᴮ y) ∧ Γ ≤ -(y ∈ᴮ x)) := begin have H_tri := epsilon_trichotomy_of_Ord H_mem₁ H_mem₂ H_ord, refine ⟨_,_⟩; intro H, { refine ⟨_,_⟩, { apply bv_rw' H, simp, rw ←imp_bot, bv_imp_intro H', from bot_of_mem_self' ‹_› }, { apply bv_rw' H, simp, rw ←imp_bot, bv_imp_intro H', from bot_of_mem_self' ‹_› }}, { cases H with H₁ H₂, bv_or_elim_at H_tri, bv_or_elim_at H_tri.left, { from ‹_› }, { apply bv_exfalso, bv_contradiction }, { apply bv_exfalso, bv_contradiction }} end lemma Ord.lt_of_ne_and_le {x y : bSet 𝔹} (H₁ : Γ ≤ Ord x) (H₂ : Γ ≤ Ord y) (H_ne : Γ ≤ -(x =ᴮ y)) (H_le : Γ ≤ x ⊆ᴮ y) : Γ ≤ x ∈ᴮ y := begin have H_compl_nonempty : Γ ≤ - (compl y x =ᴮ ∅), by { have this₁ := nonempty_compl_of_ne H_ne, have this₂ := compl_empty_of_subset H_le, bv_or_elim_at this₁, { apply bv_exfalso, from bv_absurd _ this₂ ‹_› }, { from ‹_› } }, have H_ex_min := bSet_axiom_of_regularity _ H_compl_nonempty, bv_cases_at H_ex_min z Hz, bv_split_at Hz, cases mem_compl_iff.mp Hz_left with Hz₁ Hz₂, suffices H_eq : Γ_1 ≤ x =ᴮ z, by bv_cc, rw bv_eq_unfold', refine le_inf _ _, { bv_intro a, bv_imp_intro Ha, have this' := epsilon_trichotomy_of_Ord (mem_of_mem_subset H_le Ha) ‹_› ‹_›, bv_or_elim_at this', bv_or_elim_at this'.left, { apply bv_exfalso, exact bv_absurd (z ∈ᴮ x) (by bv_cc) ‹_› }, { from ‹_› }, { apply bv_exfalso, refine bv_absurd (z ∈ᴮ x) _ ‹_›, apply mem_of_mem_Ord this'.right ‹_› ‹_› }}, { bv_intro a, bv_imp_intro Ha, apply bv_by_contra, bv_imp_intro H_contra, have Ha' : Γ_3 ≤ a ∈ᴮ y, by {refine mem_of_mem_Ord Ha ‹_› H₂, }, have : Γ_3 ≤ a ∈ᴮ y ∧ Γ_3 ≤ -(a ∈ᴮ x) := ⟨‹_›,‹_›⟩, rw ←mem_compl_iff at this, refine bv_absurd _ Ha _, exact Hz_right a ‹_› } end lemma Ord.le_or_le {x y : bSet 𝔹} (H₁ : Γ ≤ Ord x) (H₂ : Γ ≤ Ord y) : Γ ≤ x ⊆ᴮ y ⊔ y ⊆ᴮ x := begin let w := x ∩ᴮ y, have w_Ord : Γ ≤ Ord w := Ord_binary_inter H₁ H₂, have : Γ ≤ w =ᴮ x ⊔ w =ᴮ y, by { apply bv_by_contra, bv_imp_intro H_contra, simp only with bv_push_neg at H_contra, suffices : Γ_1 ≤ w ∈ᴮ x ∧ Γ_1 ≤ w ∈ᴮ y, by { suffices : Γ_1 ≤ w ∈ᴮ w, from bot_of_mem_self' ‹_›, rwa mem_binary_inter_iff }, bv_split_at H_contra, refine ⟨_,_⟩, { apply Ord.lt_of_ne_and_le w_Ord, repeat {assumption}, from binary_inter_subset_left }, { apply Ord.lt_of_ne_and_le w_Ord, repeat {assumption}, from binary_inter_subset_right }}, bv_or_elim_at this, { refine bv_or_left _, apply bv_rw' (bv_symm this.left), simp, exact binary_inter_subset_right }, { refine bv_or_right _, apply bv_rw' (bv_symm this.right), simp, exact binary_inter_subset_left } end lemma Ord.trichotomy {x y : bSet 𝔹} (H₁ : Γ ≤ Ord x) (H₂ : Γ ≤ Ord y) : Γ ≤ x =ᴮ y ⊔ x ∈ᴮ y ⊔ y ∈ᴮ x := begin have := Ord.le_or_le H₁ H₂, bv_or_elim_at this, { bv_cases_on x =ᴮ y, { from bv_or_left (bv_or_left ‹_›) }, { refine bv_or_left (bv_or_right _), apply Ord.lt_of_ne_and_le, repeat {assumption} }}, { bv_cases_on x =ᴮ y, { from bv_or_left (bv_or_left ‹_›) }, { refine bv_or_right _, rw bv_eq_symm at H.right, apply Ord.lt_of_ne_and_le, repeat {assumption} }} end lemma Ord.eq_iff_not_mem {x y : bSet 𝔹} (H₁ : Γ ≤ Ord x) (H₂ : Γ ≤ Ord y) : Γ ≤ x =ᴮ y ↔ (Γ ≤ -(x ∈ᴮ y) ∧ Γ ≤ -(y ∈ᴮ x)) := begin refine ⟨_,_⟩; intro H, { refine ⟨_,_⟩, { rw ←imp_bot, bv_imp_intro H_contra, apply bot_of_mem_self', show bSet 𝔹, from y, bv_cc }, { rw ←imp_bot, bv_imp_intro H_contra, apply bot_of_mem_self', show bSet 𝔹, from y, bv_cc } }, { cases H with H₁' H₂', have := Ord.trichotomy H₁ H₂, bv_or_elim_at this, bv_or_elim_at this.left, all_goals { assumption <|> {apply bv_exfalso; bv_contradiction} } } end lemma Ord.eq_of_not_mem {x y : bSet 𝔹} (H₁ : Γ ≤ Ord x) (H₂ : Γ ≤ Ord y) (H_nmem₁ : Γ ≤ -(x ∈ᴮ y)) (H_nmem₂ : Γ ≤ -(y ∈ᴮ x)) : Γ ≤ x =ᴮ y := by { rw Ord.eq_iff_not_mem; simp* } lemma Ord.le_iff_lt_or_eq {x y : bSet 𝔹} (H₁ : Γ ≤ Ord x) (H₂ : Γ ≤ Ord y) : Γ ≤ x ⊆ᴮ y ↔ (Γ ≤ x ∈ᴮ y ⊔ x =ᴮ y) := begin refine ⟨_,_⟩; intro H, { bv_cases_on x =ᴮ y, { exact bv_or_right ‹_› }, { refine bv_or_left _, apply Ord.lt_of_ne_and_le ‹_› H₂ ‹_› ‹_› } }, { bv_or_elim_at H, { from subset_of_mem_Ord ‹_› ‹_› }, { apply bv_rw' H.right, simp, from subset_self }} end lemma Ord.lt_of_not_le {x y : bSet 𝔹} (H₁ : Γ ≤ Ord x) (H₂ : Γ ≤ Ord y) : Γ ≤ -(x ⊆ᴮ y) → Γ ≤ y ∈ᴮ x := begin intro H_not_le, apply bv_by_contra, bv_imp_intro H_contra, rw ←imp_bot at H_not_le, refine H_not_le _, rw Ord.le_iff_lt_or_eq, { have := Ord.trichotomy H₁ H₂, bv_or_elim_at this, { rwa sup_comm }, { apply bv_exfalso, bv_contradiction } }, { from ‹_› }, { from ‹_› } end lemma Ord.resolve_lt {x y : bSet 𝔹} (H₁ : Γ ≤ Ord x) (H₂ : Γ ≤ Ord y) : Γ ≤ -(x ∈ᴮ y) → Γ ≤ (y ∈ᴮ x) ⊔ (y =ᴮ x) := begin intro H_not_mem, have := Ord.trichotomy H₁ H₂, bv_or_elim_at this, bv_or_elim_at this.left, { from bv_or_right (bv_symm ‹_›) }, { from bv_exfalso (by bv_contradiction) }, { from bv_or_left ‹_› } end lemma epsilon_trichotomy_of_sub_Ord {Γ : 𝔹} (u : bSet 𝔹) (H_ord : Γ ≤ ⨅ x, x ∈ᴮ u ⟹ Ord x) : Γ ≤ (⨅y, y∈ᴮ u ⟹ (⨅z, z ∈ᴮ u ⟹ (y =ᴮ z ⊔ y ∈ᴮ z ⊔ z ∈ᴮ y))) := begin bv_intro y, bv_imp_intro Hy, bv_intro z, bv_imp_intro Hz, have H₁ : Γ_2 ≤ Ord y := H_ord y ‹_›, have H₂ : Γ_2 ≤ Ord z := H_ord z ‹_›, exact Ord.trichotomy H₁ H₂ end lemma epsilon_wf_of_sub_Ord {Γ : 𝔹} (u : bSet 𝔹) : Γ ≤ (⨅x, x ⊆ᴮ u ⟹ (- (x =ᴮ ∅) ⟹ ⨆y, y∈ᴮ x ⊓ (⨅z', z' ∈ᴮ x ⟹ (- (z' ∈ᴮ y))))) := begin bv_intro x, bv_imp_intro Hsub, bv_imp_intro H_nonempty, exact bSet_axiom_of_regularity _ H_nonempty, end def exists_two (η : bSet 𝔹) : 𝔹 := (⨅x, x ∈ᴮ η ⟹ ⨆ z, z ∈ᴮ η ⊓ (x ∈ᴮ z ⊔ z ∈ᴮ x)) @[simp]lemma B_ext_exists_two : B_ext (exists_two : bSet 𝔹 → 𝔹) := begin unfold B_ext, unfold exists_two, change B_ext _, simp end lemma one_mem_of_not_zero_and_not_one {η : bSet 𝔹} {Γ : 𝔹} (H_ord : Γ ≤ Ord η) (H_not_zero : Γ ≤ -(η =ᴮ 0)) (H_not_one : Γ ≤ -(η =ᴮ 1)) : Γ ≤ 1 ∈ᴮ η := begin have := Ord.trichotomy (H_ord) Ord_one, bv_or_elim_at this, bv_or_elim_at this.left, { apply bv_exfalso, bv_contradiction }, { suffices : Γ_2 ≤ η =ᴮ 0, by apply bv_exfalso; bv_contradiction, exact eq_zero_of_mem_one this.left.right }, { from ‹_› } end lemma exists_two_iff { η : bSet 𝔹 } { Γ : 𝔹 } (H_ord : Γ ≤ Ord η): Γ ≤ exists_two η ↔ Γ ≤ (- (η =ᴮ 1)) := begin refine ⟨_,_⟩; intro H, { rw ←imp_bot, bv_imp_intro H_contra, have : Γ_1 ≤ 0 ∈ᴮ η, by { apply bv_rw' H_contra, simp, simp }, unfold exists_two at H, replace H := H (0 : bSet 𝔹) ‹_›, bv_cases_at H w Hw, bv_split_at Hw, bv_or_elim_at Hw_right, { suffices : Γ_3 ≤ 0 ∈ᴮ 0, by exact bot_of_mem_self' ‹_›, suffices : Γ_3 ≤ w =ᴮ 0, by bv_cc, exact eq_zero_of_mem_one (by bv_cc) }, { suffices : Γ_3 ≤ 0 ∈ᴮ 0, by exact bot_of_mem_self' ‹_›, suffices : Γ_3 ≤ w =ᴮ 0, by bv_cc, exact eq_zero_of_mem_one (by bv_cc) } }, { bv_cases_on η =ᴮ 0, { apply bv_rw' H_1.left, simp, apply bv_rw' zero_eq_empty, simp, apply forall_empty }, { suffices : Γ_1 ≤ 1 ∈ᴮ η, by { bv_intro z, bv_imp_intro Hz_mem, have this' := Ord.trichotomy (Ord_of_mem_Ord Hz_mem H_ord) (Ord_one), bv_or_elim_at this', bv_or_elim_at this'.left, { apply bv_use (0 : bSet 𝔹), refine le_inf _ (bv_or_right _), { exact mem_of_mem_Ord (zero_mem_one) ‹_› ‹_› }, { apply bv_rw' ‹_ ≤ z =ᴮ 1›, simp, exact zero_mem_one } }, { apply bv_use (1 : bSet 𝔹), exact le_inf ‹_› (bv_or_left ‹_›) }, { apply bv_use (1 : bSet 𝔹), refine le_inf ‹_› (bv_or_right ‹_›) }}, exact one_mem_of_not_zero_and_not_one ‹_› ‹_› ‹_› }} end end Ord section eps_iso variables {𝔹 : Type u} [nontrivial_complete_boolean_algebra 𝔹] @[reducible]def strong_eps_hom (x y f : bSet 𝔹) : 𝔹 := (⨅ z₁, z₁ ∈ᴮ x ⟹ ⨅ z₂, z₂ ∈ᴮ x ⟹ ⨅ w₁, w₁ ∈ᴮ y ⟹ ⨅ w₂, w₂ ∈ᴮ y ⟹ (pair z₁ w₁ ∈ᴮ f ⟹ (pair z₂ w₂ ∈ᴮ f ⟹ (z₁ ∈ᴮ z₂ ⇔ w₁ ∈ᴮ w₂)))) lemma strong_eps_hom_iff {x y f : bSet 𝔹} {Γ} : Γ ≤ strong_eps_hom x y f ↔ ∀ {Γ'} (H_le : Γ' ≤ Γ), ∀ z₁ (Hz₁_mem : Γ' ≤ z₁ ∈ᴮ x) (z₂) (Hz₂_mem : Γ' ≤ z₂ ∈ᴮ x) (w₁) (Hw₁_mem : Γ' ≤ w₁ ∈ᴮ y) (w₂) (Hw₂_mem : Γ' ≤ w₂ ∈ᴮ y) (Hpr₁_mem : Γ' ≤ pair z₁ w₁ ∈ᴮ f) (Hpr₂_mem : Γ' ≤ pair z₂ w₂ ∈ᴮ f), Γ' ≤ z₁ ∈ᴮ z₂ ↔ Γ' ≤ w₁ ∈ᴮ w₂ := begin refine ⟨_,_⟩; intro H, { intros, have := (le_trans H_le H) z₁ ‹_› z₂ ‹_› w₁ ‹_› w₂ ‹_› ‹_› ‹_›, rw bv_biimp_iff at this, apply this, refl }, { rw strong_eps_hom, bv_intro z₁, bv_imp_intro' Hz₁_mem, bv_intro z₂, bv_imp_intro Hz₂_mem, bv_intro w₁, bv_imp_intro Hw₁_mem, bv_intro w₁, bv_imp_intro Hw₂_mem, bv_imp_intro Hpr₁_mem, bv_imp_intro HPr₂_mem, rw bv_biimp_iff, intros Γ' H_Γ', apply_all le_trans H_Γ', apply H, refine le_trans H_Γ' (by { dsimp[Γ_6,Γ_5,Γ_4,Γ_3,Γ_2,Γ_1], tidy_context }), repeat { assumption } } end lemma strong_eps_hom_unfold {x y f : bSet 𝔹} {Γ} : Γ ≤ strong_eps_hom x y f → ∀ z₁ (Hz₁_mem : Γ ≤ z₁ ∈ᴮ x) (z₂) (Hz₂_mem : Γ ≤ z₂ ∈ᴮ x) (w₁) (Hw₁_mem : Γ ≤ w₁ ∈ᴮ y) (w₂) (Hw₂_mem : Γ ≤ w₂ ∈ᴮ y) (Hpr₁_mem : Γ ≤ pair z₁ w₁ ∈ᴮ f) (Hpr₂_mem : Γ ≤ pair z₂ w₂ ∈ᴮ f), Γ ≤ z₁ ∈ᴮ z₂ ↔ Γ ≤ w₁ ∈ᴮ w₂ := λ H, begin intros, have := H z₁ ‹_› z₂ ‹_› w₁ ‹_› w₂ ‹_› ‹_› ‹_›, rw bv_biimp_iff at this, apply this, refl end def eps_iso (x y f : bSet 𝔹) : 𝔹 := is_function x y f ⊓ (strong_eps_hom x y f) ⊓ is_surj x y f lemma is_surj_of_eps_iso {x y f : bSet 𝔹} {Γ} (H_eps_iso : Γ ≤ eps_iso x y f) : Γ ≤ is_surj x y f := bv_and.right ‹_› lemma is_function_of_eps_iso {x y f : bSet 𝔹} {Γ} (H_eps_iso : Γ ≤ eps_iso x y f) : Γ ≤ is_function x y f := bv_and.left (bv_and.left ‹_›) lemma strong_eps_hom_of_eps_iso {x y f : bSet 𝔹} {Γ} (H_eps_iso : Γ ≤ eps_iso x y f) : Γ ≤ strong_eps_hom x y f := by {bv_split_at H_eps_iso, from bv_and.right ‹_›} lemma eps_iso_mem {x y f z₁ z₂ : bSet 𝔹} {Γ} (H₂ : Γ ≤ eps_iso x y f) (H_mem : Γ ≤ z₁ ∈ᴮ x) (H_mem' : Γ ≤ z₂ ∈ᴮ x) (H_mem'' : Γ ≤ z₁ ∈ᴮ z₂) {w₁} (H_mem''' : Γ ≤ w₁ ∈ᴮ y) (H_mem_pr₁ : Γ ≤ pair z₁ w₁ ∈ᴮ f) {w₂} (H_mem'''' : Γ ≤ w₂ ∈ᴮ y) (H_mem_pr₂ : Γ ≤ pair z₂ w₂ ∈ᴮ f) : Γ ≤ w₁ ∈ᴮ w₂ := by rwa ←(strong_eps_hom_unfold (strong_eps_hom_of_eps_iso ‹_›) z₁ ‹_› z₂ ‹_› w₁ ‹_› w₂ ‹_› ‹_› ‹_›) lemma eps_iso_mem' {x y f z₁ z₂ : bSet 𝔹} {Γ} (H₂ : Γ ≤ eps_iso x y f) (H_mem : Γ ≤ z₁ ∈ᴮ x) (H_mem' : Γ ≤ z₂ ∈ᴮ x) {w₁} (H_mem''' : Γ ≤ w₁ ∈ᴮ y) (H_mem_pr₁ : Γ ≤ pair z₁ w₁ ∈ᴮ f) {w₂} (H_mem'''' : Γ ≤ w₂ ∈ᴮ y) (H_mem_pr₂ : Γ ≤ pair z₂ w₂ ∈ᴮ f) (H_mem'' : Γ ≤ w₁ ∈ᴮ w₂) : Γ ≤ z₁ ∈ᴮ z₂ := by rwa (strong_eps_hom_unfold (strong_eps_hom_of_eps_iso ‹_›) z₁ ‹_› z₂ ‹_› w₁ ‹_› w₂ ‹_› ‹_› ‹_›) lemma eps_iso_not_mem {x y f z₁ z₂ : bSet 𝔹} {Γ} (H₂ : Γ ≤ eps_iso x y f) (H_mem : Γ ≤ z₁ ∈ᴮ x) (H_mem' : Γ ≤ z₂ ∈ᴮ x) (H_mem'' : Γ ≤ -(z₁ ∈ᴮ z₂)) {w₁} (H_mem''' : Γ ≤ w₁ ∈ᴮ y) (H_mem_pr₁ : Γ ≤ pair z₁ w₁ ∈ᴮ f) {w₂} (H_mem'''' : Γ ≤ w₂ ∈ᴮ y) (H_mem_pr₂ : Γ ≤ pair z₂ w₂ ∈ᴮ f) : Γ ≤ -(w₁ ∈ᴮ w₂) := begin rw ←imp_bot at ⊢ H_mem'', bv_imp_intro Hw_mem, refine H_mem'' _, rwa (strong_eps_hom_unfold (strong_eps_hom_of_eps_iso ‹_›) z₁ ‹_› z₂ ‹_› w₁ ‹_› w₂ ‹_› ‹_› ‹_›) end lemma eps_iso_not_mem' {x y f z₁ z₂ : bSet 𝔹} {Γ} (H₂ : Γ ≤ eps_iso x y f) (H_mem : Γ ≤ z₁ ∈ᴮ x) (H_mem' : Γ ≤ z₂ ∈ᴮ x) {w₁} (H_mem''' : Γ ≤ w₁ ∈ᴮ y) (H_mem_pr₁ : Γ ≤ pair z₁ w₁ ∈ᴮ f) {w₂} (H_mem'''' : Γ ≤ w₂ ∈ᴮ y) (H_mem_pr₂ : Γ ≤ pair z₂ w₂ ∈ᴮ f) (H_mem'' : Γ ≤ -(w₁ ∈ᴮ w₂)) : Γ ≤ -(z₁ ∈ᴮ z₂) := begin rw ←imp_bot at ⊢ H_mem'', bv_imp_intro Hw_mem, refine H_mem'' _, rwa ←(strong_eps_hom_unfold (strong_eps_hom_of_eps_iso ‹_›) z₁ ‹_› z₂ ‹_› w₁ ‹_› w₂ ‹_› ‹_› ‹_›) end lemma eps_iso_inj_of_Ord {x y f : bSet 𝔹} {Γ} (H₁ : Γ ≤ Ord x) (H₂ : Γ ≤ Ord y) (H₃ : Γ ≤ eps_iso x y f) : Γ ≤ is_inj f := begin bv_intro w₁, bv_intro w₂, bv_intro v₁, bv_intro v₂, bv_imp_intro H, bv_split_at H, bv_split_at H_left, have H_function := is_function_of_eps_iso ‹_›, have Hw₁_mem : Γ_1 ≤ w₁ ∈ᴮ x := mem_domain_of_is_function ‹_› ‹_›, have Hw₂_mem : Γ_1 ≤ w₂ ∈ᴮ x := mem_domain_of_is_function ‹_› ‹_›, have Hv₁_mem : Γ_1 ≤ v₁ ∈ᴮ y := mem_codomain_of_is_function ‹_› ‹_›, have Hv₂_mem : Γ_1 ≤ v₂ ∈ᴮ y := mem_codomain_of_is_function ‹_› ‹_›, have Hw₁_ord : Γ_1 ≤ Ord w₁ := Ord_of_mem_Ord ‹_› ‹_›, have Hw₂_ord : Γ_1 ≤ Ord w₂ := Ord_of_mem_Ord ‹_› ‹_›, have Hv₁_ord : Γ_1 ≤ Ord v₁ := Ord_of_mem_Ord ‹_› ‹_›, have Hv₂_ord : Γ_1 ≤ Ord v₂ := Ord_of_mem_Ord ‹_› ‹_›, suffices : Γ_1 ≤ - (w₁ ∈ᴮ w₂) ∧ Γ_1 ≤ -(w₂ ∈ᴮ w₁), by { refine Ord.eq_of_not_mem ‹_› ‹_› this.left this.right } , rw Ord.eq_iff_not_mem at H_right, { cases H_right with H_nmem₁ H_nmem₂, refine ⟨_,_⟩, { exact eps_iso_not_mem' ‹_› Hw₁_mem Hw₂_mem Hv₁_mem ‹_› Hv₂_mem ‹_› ‹_›, }, { exact eps_iso_not_mem' ‹_› Hw₂_mem Hw₁_mem Hv₂_mem ‹_› Hv₁_mem ‹_› ‹_› } }, { from ‹_› }, { from ‹_› } end def eps_iso_inv {x y f : bSet 𝔹} {Γ} (H₁ : Γ ≤ Ord x) (H₂ : Γ ≤ Ord y) (H₃ : Γ ≤ eps_iso x y f) : bSet 𝔹 := inj_inverse (is_func'_of_is_function (bv_and.left $ bv_and.left H₃)) (eps_iso_inj_of_Ord H₁ H₂ H₃) lemma eps_iso_inv_surj {x y f : bSet 𝔹} {Γ} {H₁ : Γ ≤ Ord x} {H₂ : Γ ≤ Ord y} {H₃ : Γ ≤ eps_iso x y f} : Γ ≤ is_surj y x (eps_iso_inv H₁ H₂ H₃) := inj_inverse.is_surj_surj _ _ (is_surj_of_eps_iso ‹_›) lemma eps_iso_inv_is_function {x y f : bSet 𝔹} {Γ} {H₁ : Γ ≤ Ord x} {H₂ : Γ ≤ Ord y} {H₃ : Γ ≤ eps_iso x y f} : Γ ≤ is_function y x (eps_iso_inv H₁ H₂ H₃) := begin apply inj_inverse.is_function_surj, from is_surj_of_eps_iso ‹_› end lemma eps_iso_inv_strong_eps_hom {x y f : bSet 𝔹} {Γ} {H₁ : Γ ≤ Ord x} {H₂ : Γ ≤ Ord y} {H₃ : Γ ≤ eps_iso x y f} : Γ ≤ strong_eps_hom y x (eps_iso_inv H₁ H₂ H₃) := begin have := (strong_eps_hom_of_eps_iso ‹_›), rw strong_eps_hom, bv_intro z₁, bv_imp_intro' Hz₁_mem, bv_intro z₂, bv_imp_intro Hz₂_mem, bv_intro w₁, bv_imp_intro Hw₁_mem, bv_intro w₂, bv_imp_intro Hw₂_mem, bv_imp_intro Hpr₁_mem, bv_imp_intro Hpr₂_mem, rw biimp_symm, have Hpr₁_mem' : Γ_6 ≤ pair w₁ z₁ ∈ᴮ f, by { erw mem_inj_inverse_iff at Hpr₁_mem, simp* }, have Hpr₂_mem' : Γ_6 ≤ pair w₂ z₂ ∈ᴮ f, by { erw mem_inj_inverse_iff at Hpr₂_mem, simp* }, rw strong_eps_hom_iff at this, rw bv_biimp_iff, intros Γ' H_Γ', apply_all le_trans H_Γ', specialize @this Γ' (by refine le_trans H_Γ' _; dsimp[Γ_6, Γ_5, Γ_4, Γ_3, Γ_2, Γ_1]; tidy_context), apply this, repeat {assumption} end lemma eps_iso_eps_iso_inv {x y f : bSet 𝔹} {Γ} {H₁ : Γ ≤ Ord x} {H₂ : Γ ≤ Ord y} {H₃ : Γ ≤ eps_iso x y f} : Γ ≤ eps_iso y x (eps_iso_inv H₁ H₂ H₃) := le_inf (le_inf eps_iso_inv_is_function eps_iso_inv_strong_eps_hom) (eps_iso_inv_surj) lemma eps_iso_symm {x y : bSet 𝔹} {Γ} (H₁ : Γ ≤ Ord x) (H₂ : Γ ≤ Ord y) : (Γ ≤ ⨆ f, eps_iso x y f) ↔ (Γ ≤ ⨆ f, eps_iso y x f) := begin refine ⟨_,_⟩; intro H; bv_cases_at H f Hf, { apply bv_use (eps_iso_inv H₁ H₂ ‹_›), from eps_iso_eps_iso_inv }, { apply bv_use (eps_iso_inv H₂ H₁ ‹_›), from eps_iso_eps_iso_inv } end lemma eps_iso_mono {x y z f : bSet 𝔹} {Γ} (H₁ : Γ ≤ Ord y) (H₂ : Γ ≤ z ⊆ᴮ y) (H₃ : Γ ≤ eps_iso y z f) (H₄ : Γ ≤ x ∈ᴮ y) (w' : bSet 𝔹) (Hw' : Γ ≤ pair x w' ∈ᴮ f) : Γ ≤ x ⊆ᴮ w' := begin suffices : Γ ≤ (comprehend (λ v, ⨅ w, pair v w ∈ᴮ f ⟹ w ∈ᴮ v) y) =ᴮ ∅, by { apply bv_by_contra, bv_imp_intro H_contra, suffices : Γ_1 ≤ -(comprehend (λ (v : bSet 𝔹), ⨅ (w : bSet 𝔹), pair v w ∈ᴮ f ⟹ w ∈ᴮ v) y =ᴮ ∅), by bv_contradiction, apply nonempty_of_exists_mem, apply bv_use x, rw mem_comprehend_iff₂, apply bv_use x, refine le_inf ‹_› (le_inf bv_refl _), { bv_intro w, bv_imp_intro Hw, have := Ord.lt_of_not_le _ _ H_contra, suffices : Γ_2 ≤ w =ᴮ w', by bv_cc, apply eq_of_is_function_of_eq (bv_and.left $ bv_and.left ‹_›), from (bv_refl : _ ≤ x =ᴮ x), from ‹_›, from ‹_›, { exact Ord_of_mem_Ord H₄ ‹_› }, { refine Ord_of_mem_Ord (_ : _ ≤ w' ∈ᴮ y) ‹_›, refine mem_of_mem_subset H₂ _, exact mem_codomain_of_is_function ‹_› (bv_and.left $ bv_and.left ‹_›) } }, { simp }, }, apply bv_by_contra, bv_imp_intro H_contra, replace H_contra := bSet_axiom_of_regularity _ H_contra, bv_cases_at H_contra a Ha, bv_split_at Ha, refine bv_absurd _ Ha_right _, simp only with bv_push_neg, have H_total := is_total_of_is_function (bv_and.left $ bv_and.left ‹_›), rw mem_comprehend_iff₂ at Ha_left, {bv_cases_at Ha_left a' Ha', bv_split_at Ha', bv_split_at Ha'_right, have a_mem_y : Γ_3 ≤ a ∈ᴮ y := by bv_cc, replace H_total := H_total a a_mem_y, bv_cases_at H_total wa Hwa, bv_split_at Hwa, have pair_a'_mem : Γ_4 ≤ pair a' wa ∈ᴮ f, by { apply bv_rw' (bv_symm Ha'_right_left), from B_ext_pair_mem_left, from ‹_› }, have wa_mem_a : Γ_4 ≤ wa ∈ᴮ a, by { suffices : Γ_4 ≤ wa ∈ᴮ a', by bv_cc, from Ha'_right_right wa pair_a'_mem }, apply bv_use wa, refine le_inf _ _, { rw mem_comprehend_iff₂, { apply bv_use wa, have wa_mem_y : Γ_4 ≤ wa ∈ᴮ y, by { exact mem_of_mem_subset H₂ ‹_› }, refine le_inf ‹_› (le_inf bv_refl _), { bv_intro wa', bv_imp_intro Hwa', refine eps_iso_mem ‹_› wa_mem_y a_mem_y wa_mem_a _ ‹_› ‹_› ‹_›, from mem_codomain_of_is_function Hwa' (bv_and.left $ bv_and.left H₃) } }, { simp } }, { from ‹_› } }, { simp } end lemma eq_of_Ord_eps_iso_aux {x y : bSet 𝔹} {Γ} (Hx_ord : Γ ≤ Ord x) (Hy_ord : Γ ≤ Ord y) (H_eps_iso : Γ ≤ ⨆ f, eps_iso y x f) (H_mem : Γ ≤ x ∈ᴮ y) : Γ ≤ ⊥ := begin bv_cases_at H_eps_iso f Hf, have H_function := bv_and.left (bv_and.left Hf), have H_total := is_total_of_is_function H_function, replace H_total := H_total x ‹_›, bv_cases_at H_total w Hw, bv_split_at Hw, refine bot_of_mem_mem' _ _ _ Hw_left, have x_sub_y : Γ_2 ≤ x ⊆ᴮ y, by {apply subset_of_mem_Ord ‹_› ‹_›}, suffices x_sub_w : Γ_2 ≤ x ⊆ᴮ w, by {rw Ord.le_iff_lt_or_eq at x_sub_w, bv_or_elim_at x_sub_w, {from ‹_›}, { apply bv_exfalso, suffices : Γ_3 ≤ w ∈ᴮ w, by { exact bot_of_mem_self' ‹_› }, bv_cc }, from ‹_›, from Ord_of_mem_Ord ‹_› Hx_ord }, apply eps_iso_mono Hy_ord x_sub_y, repeat { assumption } end lemma eq_of_Ord_eps_iso {x y : bSet 𝔹} {Γ} (Hx_ord : Γ ≤ Ord x) (Hy_ord : Γ ≤ Ord y) (H_eps_iso : Γ ≤ ⨆ f, eps_iso x y f) : Γ ≤ x =ᴮ y := begin have := Ord.trichotomy Hx_ord Hy_ord, bv_or_elim_at this, { bv_or_elim_at this.left, { from ‹_› }, { rw eps_iso_symm at H_eps_iso, apply bv_exfalso, from eq_of_Ord_eps_iso_aux Hx_ord Hy_ord ‹_› ‹_›, repeat {from ‹_›} }}, { apply bv_exfalso, from eq_of_Ord_eps_iso_aux Hy_ord Hx_ord ‹_› ‹_› } end end eps_iso variables {𝔹 : Type*} [nontrivial_complete_boolean_algebra 𝔹] def is_limit (η : bSet 𝔹) : 𝔹 := (∅ ∈ᴮ η) ⊓ (⨅ x, x ∈ᴮ η ⟹ ⨆y, y ∈ᴮ η ⊓ x ∈ᴮ y) lemma is_epsilon_well_founded {x : bSet 𝔹} {Γ : 𝔹} : Γ ≤ epsilon_well_founded x := by { bv_intro x, bv_imp_intro Hsub, bv_imp_intro H_nonempty, exact bSet_axiom_of_regularity _ H_nonempty } lemma Ord_succ {η : bSet 𝔹} {Γ : 𝔹} (H_Ord : Γ ≤ Ord η) : Γ ≤ Ord (succ η) := begin refine le_inf (le_inf _ _) _, { bv_intro y, bv_imp_intro H_mem, bv_intro z, bv_imp_intro Hz, erw mem_insert1 at H_mem Hz, bv_or_elim_at Hz; bv_or_elim_at H_mem, { exact bv_or_left (bv_or_left (by bv_cc)) }, { exact bv_or_left (bv_or_right (by bv_cc)) }, { exact bv_or_right (by bv_cc) }, { exact epsilon_trichotomy_of_Ord H_mem.right Hz.right H_Ord }}, { bv_intro x, bv_imp_intro Hsub, bv_imp_intro H_nonempty, exact bSet_axiom_of_regularity _ H_nonempty }, { bv_intro z, bv_imp_intro Hz, erw mem_insert1 at Hz, bv_or_elim_at Hz, { apply bv_rw' Hz.left, simp, simp }, { refine subset_trans' (subset_of_mem_Ord Hz.right ‹_›) _, simp }}, end lemma Ord.succ_le_of_lt {η ρ : bSet 𝔹} {Γ : 𝔹} (H_Ord' : Γ ≤ Ord ρ) (H_lt : Γ ≤ η ∈ᴮ ρ) : Γ ≤ succ η ⊆ᴮ ρ := begin rw subset_unfold', bv_intro w, bv_imp_intro Hw, erw mem_insert1 at Hw, bv_or_elim_at Hw, { bv_cc }, { refine mem_of_mem_Ord Hw.right ‹_› ‹_› } end lemma omega_least_is_limit {Γ : 𝔹} : Γ ≤ ⨅ η, Ord η ⟹ ((is_limit η) ⟹ omega ⊆ᴮ η) := begin bv_intro η, bv_imp_intro H_η, bv_imp_intro H_limit, bv_intro x, bv_imp_intro Hx, induction x, induction x with x ih, { dsimp, change _ ≤ 0 ∈ᴮ _, change _ ≤ (λ z, z ∈ᴮ η) _,apply bv_rw' zero_eq_empty, simp, from bv_and.left ‹_› }, { dsimp at *, change _ ≤ bSet.of_nat _ ∈ᴮ _, rw check_succ_eq_succ_check, specialize ih H_η ‹_› (le_top), bv_split_at H_limit, rcases exists_convert (H_limit_right (of_nat x) ‹_›) with ⟨y,Hy⟩, bv_split_at Hy, have H_y_Ord := Ord_of_mem_Ord Hy_left ‹_›, bv_cases_on y =ᴮ (succ (of_nat x)), { bv_cc }, -- bv_cc { have := Ord.succ_le_of_lt _ Hy_right, rw Ord.le_iff_lt_or_eq at this, bv_or_elim_at this, { apply mem_of_mem_Ord this.left ‹_› ‹_›, }, { bv_cc }, { apply Ord_succ, apply Ord_of_nat }, { exact H_y_Ord }, { exact H_y_Ord }}} end end bSet
20f988e100145f0e21e3ef01f4fbb616548f34ff
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/analysis/convex/extrema.lean
9c65bf40eb1bdcab5c2ef017a91f8e04c588c1f3
[ "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
5,436
lean
/- Copyright (c) 2020 Frédéric Dupuis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Frédéric Dupuis -/ import analysis.convex.basic import topology.algebra.affine import topology.local_extr /-! # Minima and maxima of convex functions We show that if a function `f : E → β` is convex, then a local minimum is also a global minimum, and likewise for concave functions. -/ variables {E β: Type*} [add_comm_group E] [topological_space E] [module ℝ E] [topological_add_group E] [has_continuous_smul ℝ E] [linear_ordered_add_comm_group β] [module ℝ β] [ordered_smul ℝ β] {s : set E} open set filter open_locale classical /-- Helper lemma for the more general case: `is_min_on.of_is_local_min_on_of_convex_on`. -/ lemma is_min_on.of_is_local_min_on_of_convex_on_Icc {f : ℝ → β} {a b : ℝ} (a_lt_b : a < b) (h_local_min : is_local_min_on f (Icc a b) a) (h_conv : convex_on (Icc a b) f) : ∀ x ∈ Icc a b, f a ≤ f x := begin by_contradiction H_cont, push_neg at H_cont, rcases H_cont with ⟨x, ⟨h_ax, h_xb⟩, fx_lt_fa⟩, obtain ⟨z, hz, ge_on_nhd⟩ : ∃ z > a, ∀ y ∈ (Icc a z), f y ≥ f a, { rcases eventually_iff_exists_mem.mp h_local_min with ⟨U, U_in_nhds_within, fy_ge_fa⟩, rw [nhds_within_Icc_eq_nhds_within_Ici a_lt_b, mem_nhds_within_Ici_iff_exists_Icc_subset] at U_in_nhds_within, rcases U_in_nhds_within with ⟨ε, ε_in_Ioi, Ioc_in_U⟩, exact ⟨ε, mem_Ioi.mp ε_in_Ioi, λ y y_in_Ioc, fy_ge_fa y $ Ioc_in_U y_in_Ioc⟩ }, have a_lt_x : a < x := lt_of_le_of_ne h_ax (λ H, by subst H; exact lt_irrefl (f a) fx_lt_fa), have lt_on_nhd : ∀ y ∈ Ioc a x, f y < f a, { intros y y_in_Ioc, rcases (convex.mem_Ioc a_lt_x).mp y_in_Ioc with ⟨ya, yx, ya_pos, yx_pos, yax, y_combo⟩, calc f y = f (ya * a + yx * x) : by rw [y_combo] ... ≤ ya • f a + yx • f x : h_conv.2 (left_mem_Icc.mpr (le_of_lt a_lt_b)) ⟨h_ax, h_xb⟩ (ya_pos) (le_of_lt yx_pos) yax ... < ya • f a + yx • f a : add_lt_add_left (smul_lt_smul_of_pos fx_lt_fa yx_pos) _ ... = f a : by rw [←add_smul, yax, one_smul] }, by_cases h_xz : x ≤ z, { exact not_lt_of_ge (ge_on_nhd x (show x ∈ Icc a z, by exact ⟨h_ax, h_xz⟩)) fx_lt_fa, }, { have h₁ : z ∈ Ioc a x := ⟨hz, le_of_not_ge h_xz⟩, have h₂ : z ∈ Icc a z := ⟨le_of_lt hz, le_refl z⟩, exact not_lt_of_ge (ge_on_nhd z h₂) (lt_on_nhd z h₁) } end /-- A local minimum of a convex function is a global minimum, restricted to a set `s`. -/ lemma is_min_on.of_is_local_min_on_of_convex_on {f : E → β} {a : E} (a_in_s : a ∈ s) (h_localmin: is_local_min_on f s a) (h_conv : convex_on s f) : ∀ x ∈ s, f a ≤ f x := begin by_contradiction H_cont, push_neg at H_cont, rcases H_cont with ⟨x, ⟨x_in_s, fx_lt_fa⟩⟩, let g : ℝ →ᵃ[ℝ] E := affine_map.line_map a x, have hg0 : g 0 = a := affine_map.line_map_apply_zero a x, have hg1 : g 1 = x := affine_map.line_map_apply_one a x, have fg_local_min_on : is_local_min_on (f ∘ g) (g ⁻¹' s) 0, { rw ←hg0 at h_localmin, refine is_local_min_on.comp_continuous_on h_localmin subset.rfl (continuous.continuous_on (affine_map.line_map_continuous)) _, simp [mem_preimage, hg0, a_in_s] }, have fg_min_on : ∀ x ∈ (Icc 0 1 : set ℝ), (f ∘ g) 0 ≤ (f ∘ g) x, { have Icc_in_s' : Icc 0 1 ⊆ (g ⁻¹' s), { have h0 : (0 : ℝ) ∈ (g ⁻¹' s) := by simp [mem_preimage, a_in_s], have h1 : (1 : ℝ) ∈ (g ⁻¹' s) := by simp [mem_preimage, hg1, x_in_s], rw ←segment_eq_Icc (show (0 : ℝ) ≤ 1, by linarith), exact (convex.affine_preimage g h_conv.1).segment_subset (by simp [mem_preimage, hg0, a_in_s]) (by simp [mem_preimage, hg1, x_in_s]) }, have fg_local_min_on' : is_local_min_on (f ∘ g) (Icc 0 1) 0 := is_local_min_on.on_subset fg_local_min_on Icc_in_s', refine is_min_on.of_is_local_min_on_of_convex_on_Icc (by linarith) fg_local_min_on' _, exact (convex_on.comp_affine_map g h_conv).subset Icc_in_s' (convex_Icc 0 1) }, have gx_lt_ga : (f ∘ g) 1 < (f ∘ g) 0 := by simp [hg1, fx_lt_fa, hg0], exact not_lt_of_ge (fg_min_on 1 (mem_Icc.mpr ⟨zero_le_one, le_refl 1⟩)) gx_lt_ga, end /-- A local maximum of a concave function is a global maximum, restricted to a set `s`. -/ lemma is_max_on.of_is_local_max_on_of_concave_on {f : E → β} {a : E} (a_in_s : a ∈ s) (h_localmax: is_local_max_on f s a) (h_conc : concave_on s f) : ∀ x ∈ s, f x ≤ f a := @is_min_on.of_is_local_min_on_of_convex_on _ (order_dual β) _ _ _ _ _ _ _ _ s f a a_in_s h_localmax h_conc /-- A local minimum of a convex function is a global minimum. -/ lemma is_min_on.of_is_local_min_of_convex_univ {f : E → β} {a : E} (h_local_min : is_local_min f a) (h_conv : convex_on univ f) : ∀ x, f a ≤ f x := λ x, (is_min_on.of_is_local_min_on_of_convex_on (mem_univ a) (is_local_min.on h_local_min univ) h_conv) x (mem_univ x) /-- A local maximum of a concave function is a global maximum. -/ lemma is_max_on.of_is_local_max_of_convex_univ {f : E → β} {a : E} (h_local_max : is_local_max f a) (h_conc : concave_on univ f) : ∀ x, f x ≤ f a := @is_min_on.of_is_local_min_of_convex_univ _ (order_dual β) _ _ _ _ _ _ _ _ f a h_local_max h_conc
17fd0f1b0fe0f6daf9ab1dcef480cbae67e07e26
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/order/hom/basic.lean
40cfa0f1d22713035624d74be01a3aacd778d2ed
[ "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
39,220
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import logic.equiv.option import order.rel_iso.basic import tactic.monotonicity.basic import tactic.assert_exists import order.disjoint /-! # Order homomorphisms This file defines order homomorphisms, which are bundled monotone functions. A preorder homomorphism `f : α →o β` is a function `α → β` along with a proof that `∀ x y, x ≤ y → f x ≤ f y`. ## Main definitions In this file we define the following bundled monotone maps: * `order_hom α β` a.k.a. `α →o β`: Preorder homomorphism. An `order_hom α β` is a function `f : α → β` such that `a₁ ≤ a₂ → f a₁ ≤ f a₂` * `order_embedding α β` a.k.a. `α ↪o β`: Relation embedding. An `order_embedding α β` is an embedding `f : α ↪ β` such that `a ≤ b ↔ f a ≤ f b`. Defined as an abbreviation of `@rel_embedding α β (≤) (≤)`. * `order_iso`: Relation isomorphism. An `order_iso α β` is an equivalence `f : α ≃ β` such that `a ≤ b ↔ f a ≤ f b`. Defined as an abbreviation of `@rel_iso α β (≤) (≤)`. We also define many `order_hom`s. In some cases we define two versions, one with `ₘ` suffix and one without it (e.g., `order_hom.compₘ` and `order_hom.comp`). This means that the former function is a "more bundled" version of the latter. We can't just drop the "less bundled" version because the more bundled version usually does not work with dot notation. * `order_hom.id`: identity map as `α →o α`; * `order_hom.curry`: an order isomorphism between `α × β →o γ` and `α →o β →o γ`; * `order_hom.comp`: composition of two bundled monotone maps; * `order_hom.compₘ`: composition of bundled monotone maps as a bundled monotone map; * `order_hom.const`: constant function as a bundled monotone map; * `order_hom.prod`: combine `α →o β` and `α →o γ` into `α →o β × γ`; * `order_hom.prodₘ`: a more bundled version of `order_hom.prod`; * `order_hom.prod_iso`: order isomorphism between `α →o β × γ` and `(α →o β) × (α →o γ)`; * `order_hom.diag`: diagonal embedding of `α` into `α × α` as a bundled monotone map; * `order_hom.on_diag`: restrict a monotone map `α →o α →o β` to the diagonal; * `order_hom.fst`: projection `prod.fst : α × β → α` as a bundled monotone map; * `order_hom.snd`: projection `prod.snd : α × β → β` as a bundled monotone map; * `order_hom.prod_map`: `prod.map f g` as a bundled monotone map; * `pi.eval_order_hom`: evaluation of a function at a point `function.eval i` as a bundled monotone map; * `order_hom.coe_fn_hom`: coercion to function as a bundled monotone map; * `order_hom.apply`: application of a `order_hom` at a point as a bundled monotone map; * `order_hom.pi`: combine a family of monotone maps `f i : α →o π i` into a monotone map `α →o Π i, π i`; * `order_hom.pi_iso`: order isomorphism between `α →o Π i, π i` and `Π i, α →o π i`; * `order_hom.subtyle.val`: embedding `subtype.val : subtype p → α` as a bundled monotone map; * `order_hom.dual`: reinterpret a monotone map `α →o β` as a monotone map `αᵒᵈ →o βᵒᵈ`; * `order_hom.dual_iso`: order isomorphism between `α →o β` and `(αᵒᵈ →o βᵒᵈ)ᵒᵈ`; * `order_iso.compl`: order isomorphism `α ≃o αᵒᵈ` given by taking complements in a boolean algebra; We also define two functions to convert other bundled maps to `α →o β`: * `order_embedding.to_order_hom`: convert `α ↪o β` to `α →o β`; * `rel_hom.to_order_hom`: convert a `rel_hom` between strict orders to a `order_hom`. ## Tags monotone map, bundled morphism -/ open order_dual variables {F α β γ δ : Type*} /-- Bundled monotone (aka, increasing) function -/ structure order_hom (α β : Type*) [preorder α] [preorder β] := (to_fun : α → β) (monotone' : monotone to_fun) infixr ` →o `:25 := order_hom /-- An order embedding is an embedding `f : α ↪ β` such that `a ≤ b ↔ (f a) ≤ (f b)`. This definition is an abbreviation of `rel_embedding (≤) (≤)`. -/ abbreviation order_embedding (α β : Type*) [has_le α] [has_le β] := @rel_embedding α β (≤) (≤) infix ` ↪o `:25 := order_embedding /-- An order isomorphism is an equivalence such that `a ≤ b ↔ (f a) ≤ (f b)`. This definition is an abbreviation of `rel_iso (≤) (≤)`. -/ abbreviation order_iso (α β : Type*) [has_le α] [has_le β] := @rel_iso α β (≤) (≤) infix ` ≃o `:25 := order_iso section set_option old_structure_cmd true /-- `order_hom_class F α b` asserts that `F` is a type of `≤`-preserving morphisms. -/ abbreviation order_hom_class (F : Type*) (α β : out_param Type*) [has_le α] [has_le β] := rel_hom_class F ((≤) : α → α → Prop) ((≤) : β → β → Prop) /-- `order_iso_class F α β` states that `F` is a type of order isomorphisms. You should extend this class when you extend `order_iso`. -/ class order_iso_class (F : Type*) (α β : out_param Type*) [has_le α] [has_le β] extends equiv_like F α β := (map_le_map_iff (f : F) {a b : α} : f a ≤ f b ↔ a ≤ b) end export order_iso_class (map_le_map_iff) attribute [simp] map_le_map_iff instance [has_le α] [has_le β] [order_iso_class F α β] : has_coe_t F (α ≃o β) := ⟨λ f, ⟨f, λ _ _, map_le_map_iff f⟩⟩ @[priority 100] -- See note [lower instance priority] instance order_iso_class.to_order_hom_class [has_le α] [has_le β] [order_iso_class F α β] : order_hom_class F α β := { map_rel := λ f a b, (map_le_map_iff f).2, ..equiv_like.to_embedding_like } namespace order_hom_class variables [preorder α] [preorder β] [order_hom_class F α β] protected lemma monotone (f : F) : monotone (f : α → β) := λ _ _, map_rel f protected lemma mono (f : F) : monotone (f : α → β) := λ _ _, map_rel f instance : has_coe_t F (α →o β) := ⟨λ f, { to_fun := f, monotone' := order_hom_class.mono _ }⟩ end order_hom_class section order_iso_class section has_le variables [has_le α] [has_le β] [order_iso_class F α β] @[simp] lemma map_inv_le_iff (f : F) {a : α} {b : β} : equiv_like.inv f b ≤ a ↔ b ≤ f a := by { convert (map_le_map_iff _).symm, exact (equiv_like.right_inv _ _).symm } @[simp] lemma le_map_inv_iff (f : F) {a : α} {b : β} : a ≤ equiv_like.inv f b ↔ f a ≤ b := by { convert (map_le_map_iff _).symm, exact (equiv_like.right_inv _ _).symm } end has_le variables [preorder α] [preorder β] [order_iso_class F α β] include β lemma map_lt_map_iff (f : F) {a b : α} : f a < f b ↔ a < b := lt_iff_lt_of_le_iff_le' (map_le_map_iff f) (map_le_map_iff f) @[simp] lemma map_inv_lt_iff (f : F) {a : α} {b : β} : equiv_like.inv f b < a ↔ b < f a := by { convert (map_lt_map_iff _).symm, exact (equiv_like.right_inv _ _).symm } @[simp] lemma lt_map_inv_iff (f : F) {a : α} {b : β} : a < equiv_like.inv f b ↔ f a < b := by { convert (map_lt_map_iff _).symm, exact (equiv_like.right_inv _ _).symm } end order_iso_class namespace order_hom variables [preorder α] [preorder β] [preorder γ] [preorder δ] /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (α →o β) (λ _, α → β) := ⟨order_hom.to_fun⟩ initialize_simps_projections order_hom (to_fun → coe) protected lemma monotone (f : α →o β) : monotone f := f.monotone' protected lemma mono (f : α →o β) : monotone f := f.monotone instance : order_hom_class (α →o β) α β := { coe := to_fun, coe_injective' := λ f g h, by { cases f, cases g, congr' }, map_rel := λ f, f.monotone } @[simp] lemma to_fun_eq_coe {f : α →o β} : f.to_fun = f := rfl @[simp] lemma coe_fun_mk {f : α → β} (hf : _root_.monotone f) : (mk f hf : α → β) = f := rfl @[ext] -- See library note [partially-applied ext lemmas] lemma ext (f g : α →o β) (h : (f : α → β) = g) : f = g := fun_like.coe_injective h lemma coe_eq (f : α →o β) : coe f = f := by ext ; refl /-- One can lift an unbundled monotone function to a bundled one. -/ instance : can_lift (α → β) (α →o β) coe_fn monotone := { prf := λ f h, ⟨⟨f, h⟩, rfl⟩ } /-- Copy of an `order_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : α →o β) (f' : α → β) (h : f' = f) : α →o β := ⟨f', h.symm.subst f.monotone'⟩ @[simp] lemma coe_copy (f : α →o β) (f' : α → β) (h : f' = f) : ⇑(f.copy f' h) = f' := rfl lemma copy_eq (f : α →o β) (f' : α → β) (h : f' = f) : f.copy f' h = f := fun_like.ext' h /-- The identity function as bundled monotone function. -/ @[simps {fully_applied := ff}] def id : α →o α := ⟨id, monotone_id⟩ instance : inhabited (α →o α) := ⟨id⟩ /-- The preorder structure of `α →o β` is pointwise inequality: `f ≤ g ↔ ∀ a, f a ≤ g a`. -/ instance : preorder (α →o β) := @preorder.lift (α →o β) (α → β) _ coe_fn instance {β : Type*} [partial_order β] : partial_order (α →o β) := @partial_order.lift (α →o β) (α → β) _ coe_fn ext lemma le_def {f g : α →o β} : f ≤ g ↔ ∀ x, f x ≤ g x := iff.rfl @[simp, norm_cast] lemma coe_le_coe {f g : α →o β} : (f : α → β) ≤ g ↔ f ≤ g := iff.rfl @[simp] lemma mk_le_mk {f g : α → β} {hf hg} : mk f hf ≤ mk g hg ↔ f ≤ g := iff.rfl @[mono] lemma apply_mono {f g : α →o β} {x y : α} (h₁ : f ≤ g) (h₂ : x ≤ y) : f x ≤ g y := (h₁ x).trans $ g.mono h₂ /-- Curry/uncurry as an order isomorphism between `α × β →o γ` and `α →o β →o γ`. -/ def curry : (α × β →o γ) ≃o (α →o β →o γ) := { to_fun := λ f, ⟨λ x, ⟨function.curry f x, λ y₁ y₂ h, f.mono ⟨le_rfl, h⟩⟩, λ x₁ x₂ h y, f.mono ⟨h, le_rfl⟩⟩, inv_fun := λ f, ⟨function.uncurry (λ x, f x), λ x y h, (f.mono h.1 x.2).trans $ (f y.1).mono h.2⟩, left_inv := λ f, by { ext ⟨x, y⟩, refl }, right_inv := λ f, by { ext x y, refl }, map_rel_iff' := λ f g, by simp [le_def] } @[simp] lemma curry_apply (f : α × β →o γ) (x : α) (y : β) : curry f x y = f (x, y) := rfl @[simp] lemma curry_symm_apply (f : α →o β →o γ) (x : α × β) : curry.symm f x = f x.1 x.2 := rfl /-- The composition of two bundled monotone functions. -/ @[simps {fully_applied := ff}] def comp (g : β →o γ) (f : α →o β) : α →o γ := ⟨g ∘ f, g.mono.comp f.mono⟩ @[mono] lemma comp_mono ⦃g₁ g₂ : β →o γ⦄ (hg : g₁ ≤ g₂) ⦃f₁ f₂ : α →o β⦄ (hf : f₁ ≤ f₂) : g₁.comp f₁ ≤ g₂.comp f₂ := λ x, (hg _).trans (g₂.mono $ hf _) /-- The composition of two bundled monotone functions, a fully bundled version. -/ @[simps {fully_applied := ff}] def compₘ : (β →o γ) →o (α →o β) →o α →o γ := curry ⟨λ f : (β →o γ) × (α →o β), f.1.comp f.2, λ f₁ f₂ h, comp_mono h.1 h.2⟩ @[simp] lemma comp_id (f : α →o β) : comp f id = f := by { ext, refl } @[simp] lemma id_comp (f : α →o β) : comp id f = f := by { ext, refl } /-- Constant function bundled as a `order_hom`. -/ @[simps {fully_applied := ff}] def const (α : Type*) [preorder α] {β : Type*} [preorder β] : β →o α →o β := { to_fun := λ b, ⟨function.const α b, λ _ _ _, le_rfl⟩, monotone' := λ b₁ b₂ h x, h } @[simp] lemma const_comp (f : α →o β) (c : γ) : (const β c).comp f = const α c := rfl @[simp] lemma comp_const (γ : Type*) [preorder γ] (f : α →o β) (c : α) : f.comp (const γ c) = const γ (f c) := rfl /-- Given two bundled monotone maps `f`, `g`, `f.prod g` is the map `x ↦ (f x, g x)` bundled as a `order_hom`. -/ @[simps] protected def prod (f : α →o β) (g : α →o γ) : α →o (β × γ) := ⟨λ x, (f x, g x), λ x y h, ⟨f.mono h, g.mono h⟩⟩ @[mono] lemma prod_mono {f₁ f₂ : α →o β} (hf : f₁ ≤ f₂) {g₁ g₂ : α →o γ} (hg : g₁ ≤ g₂) : f₁.prod g₁ ≤ f₂.prod g₂ := λ x, prod.le_def.2 ⟨hf _, hg _⟩ lemma comp_prod_comp_same (f₁ f₂ : β →o γ) (g : α →o β) : (f₁.comp g).prod (f₂.comp g) = (f₁.prod f₂).comp g := rfl /-- Given two bundled monotone maps `f`, `g`, `f.prod g` is the map `x ↦ (f x, g x)` bundled as a `order_hom`. This is a fully bundled version. -/ @[simps] def prodₘ : (α →o β) →o (α →o γ) →o α →o β × γ := curry ⟨λ f : (α →o β) × (α →o γ), f.1.prod f.2, λ f₁ f₂ h, prod_mono h.1 h.2⟩ /-- Diagonal embedding of `α` into `α × α` as a `order_hom`. -/ @[simps] def diag : α →o α × α := id.prod id /-- Restriction of `f : α →o α →o β` to the diagonal. -/ @[simps {simp_rhs := tt}] def on_diag (f : α →o α →o β) : α →o β := (curry.symm f).comp diag /-- `prod.fst` as a `order_hom`. -/ @[simps] def fst : α × β →o α := ⟨prod.fst, λ x y h, h.1⟩ /-- `prod.snd` as a `order_hom`. -/ @[simps] def snd : α × β →o β := ⟨prod.snd, λ x y h, h.2⟩ @[simp] lemma fst_prod_snd : (fst : α × β →o α).prod snd = id := by { ext ⟨x, y⟩ : 2, refl } @[simp] lemma fst_comp_prod (f : α →o β) (g : α →o γ) : fst.comp (f.prod g) = f := ext _ _ rfl @[simp] lemma snd_comp_prod (f : α →o β) (g : α →o γ) : snd.comp (f.prod g) = g := ext _ _ rfl /-- Order isomorphism between the space of monotone maps to `β × γ` and the product of the spaces of monotone maps to `β` and `γ`. -/ @[simps] def prod_iso : (α →o β × γ) ≃o (α →o β) × (α →o γ) := { to_fun := λ f, (fst.comp f, snd.comp f), inv_fun := λ f, f.1.prod f.2, left_inv := λ f, by ext; refl, right_inv := λ f, by ext; refl, map_rel_iff' := λ f g, forall_and_distrib.symm } /-- `prod.map` of two `order_hom`s as a `order_hom`. -/ @[simps] def prod_map (f : α →o β) (g : γ →o δ) : α × γ →o β × δ := ⟨prod.map f g, λ x y h, ⟨f.mono h.1, g.mono h.2⟩⟩ variables {ι : Type*} {π : ι → Type*} [Π i, preorder (π i)] /-- Evaluation of an unbundled function at a point (`function.eval`) as a `order_hom`. -/ @[simps {fully_applied := ff}] def _root_.pi.eval_order_hom (i : ι) : (Π j, π j) →o π i := ⟨function.eval i, function.monotone_eval i⟩ /-- The "forgetful functor" from `α →o β` to `α → β` that takes the underlying function, is monotone. -/ @[simps {fully_applied := ff}] def coe_fn_hom : (α →o β) →o (α → β) := { to_fun := λ f, f, monotone' := λ x y h, h } /-- Function application `λ f, f a` (for fixed `a`) is a monotone function from the monotone function space `α →o β` to `β`. See also `pi.eval_order_hom`. -/ @[simps {fully_applied := ff}] def apply (x : α) : (α →o β) →o β := (pi.eval_order_hom x).comp coe_fn_hom /-- Construct a bundled monotone map `α →o Π i, π i` from a family of monotone maps `f i : α →o π i`. -/ @[simps] def pi (f : Π i, α →o π i) : α →o (Π i, π i) := ⟨λ x i, f i x, λ x y h i, (f i).mono h⟩ /-- Order isomorphism between bundled monotone maps `α →o Π i, π i` and families of bundled monotone maps `Π i, α →o π i`. -/ @[simps] def pi_iso : (α →o Π i, π i) ≃o Π i, α →o π i := { to_fun := λ f i, (pi.eval_order_hom i).comp f, inv_fun := pi, left_inv := λ f, by { ext x i, refl }, right_inv := λ f, by { ext x i, refl }, map_rel_iff' := λ f g, forall_swap } /-- `subtype.val` as a bundled monotone function. -/ @[simps {fully_applied := ff}] def subtype.val (p : α → Prop) : subtype p →o α := ⟨subtype.val, λ x y h, h⟩ /-- There is a unique monotone map from a subsingleton to itself. -/ instance unique [subsingleton α] : unique (α →o α) := { default := order_hom.id, uniq := λ a, ext _ _ (subsingleton.elim _ _) } lemma order_hom_eq_id [subsingleton α] (g : α →o α) : g = order_hom.id := subsingleton.elim _ _ /-- Reinterpret a bundled monotone function as a monotone function between dual orders. -/ @[simps] protected def dual : (α →o β) ≃ (αᵒᵈ →o βᵒᵈ) := { to_fun := λ f, ⟨order_dual.to_dual ∘ f ∘ order_dual.of_dual, f.mono.dual⟩, inv_fun := λ f, ⟨order_dual.of_dual ∘ f ∘ order_dual.to_dual, f.mono.dual⟩, left_inv := λ f, ext _ _ rfl, right_inv := λ f, ext _ _ rfl } @[simp] lemma dual_id : (order_hom.id : α →o α).dual = order_hom.id := rfl @[simp] lemma dual_comp (g : β →o γ) (f : α →o β) : (g.comp f).dual = g.dual.comp f.dual := rfl @[simp] lemma symm_dual_id : order_hom.dual.symm order_hom.id = (order_hom.id : α →o α) := rfl @[simp] lemma symm_dual_comp (g : βᵒᵈ →o γᵒᵈ) (f : αᵒᵈ →o βᵒᵈ) : order_hom.dual.symm (g.comp f) = (order_hom.dual.symm g).comp (order_hom.dual.symm f) := rfl /-- `order_hom.dual` as an order isomorphism. -/ def dual_iso (α β : Type*) [preorder α] [preorder β] : (α →o β) ≃o (αᵒᵈ →o βᵒᵈ)ᵒᵈ := { to_equiv := order_hom.dual.trans order_dual.to_dual, map_rel_iff' := λ f g, iff.rfl } /-- Lift an order homomorphism `f : α →o β` to an order homomorphism `with_bot α →o with_bot β`. -/ @[simps { fully_applied := ff }] protected def with_bot_map (f : α →o β) : with_bot α →o with_bot β := ⟨with_bot.map f, f.mono.with_bot_map⟩ /-- Lift an order homomorphism `f : α →o β` to an order homomorphism `with_top α →o with_top β`. -/ @[simps { fully_applied := ff }] protected def with_top_map (f : α →o β) : with_top α →o with_top β := ⟨with_top.map f, f.mono.with_top_map⟩ end order_hom /-- Embeddings of partial orders that preserve `<` also preserve `≤`. -/ def rel_embedding.order_embedding_of_lt_embedding [partial_order α] [partial_order β] (f : ((<) : α → α → Prop) ↪r ((<) : β → β → Prop)) : α ↪o β := { map_rel_iff' := by { intros, simp [le_iff_lt_or_eq,f.map_rel_iff, f.injective.eq_iff] }, .. f } @[simp] lemma rel_embedding.order_embedding_of_lt_embedding_apply [partial_order α] [partial_order β] {f : ((<) : α → α → Prop) ↪r ((<) : β → β → Prop)} {x : α} : rel_embedding.order_embedding_of_lt_embedding f x = f x := rfl namespace order_embedding variables [preorder α] [preorder β] (f : α ↪o β) /-- `<` is preserved by order embeddings of preorders. -/ def lt_embedding : ((<) : α → α → Prop) ↪r ((<) : β → β → Prop) := { map_rel_iff' := by intros; simp [lt_iff_le_not_le, f.map_rel_iff], .. f } @[simp] lemma lt_embedding_apply (x : α) : f.lt_embedding x = f x := rfl @[simp] theorem le_iff_le {a b} : (f a) ≤ (f b) ↔ a ≤ b := f.map_rel_iff @[simp] theorem lt_iff_lt {a b} : f a < f b ↔ a < b := f.lt_embedding.map_rel_iff @[simp] lemma eq_iff_eq {a b} : f a = f b ↔ a = b := f.injective.eq_iff protected theorem monotone : monotone f := order_hom_class.monotone f protected theorem strict_mono : strict_mono f := λ x y, f.lt_iff_lt.2 protected theorem acc (a : α) : acc (<) (f a) → acc (<) a := f.lt_embedding.acc a protected theorem well_founded : well_founded ((<) : β → β → Prop) → well_founded ((<) : α → α → Prop) := f.lt_embedding.well_founded protected theorem is_well_order [is_well_order β (<)] : is_well_order α (<) := f.lt_embedding.is_well_order /-- An order embedding is also an order embedding between dual orders. -/ protected def dual : αᵒᵈ ↪o βᵒᵈ := ⟨f.to_embedding, λ a b, f.map_rel_iff⟩ /-- A version of `with_bot.map` for order embeddings. -/ @[simps { fully_applied := ff }] protected def with_bot_map (f : α ↪o β) : with_bot α ↪o with_bot β := { to_fun := with_bot.map f, map_rel_iff' := with_bot.map_le_iff f (λ a b, f.map_rel_iff), .. f.to_embedding.option_map } /-- A version of `with_top.map` for order embeddings. -/ @[simps { fully_applied := ff }] protected def with_top_map (f : α ↪o β) : with_top α ↪o with_top β := { to_fun := with_top.map f, .. f.dual.with_bot_map.dual } /-- To define an order embedding from a partial order to a preorder it suffices to give a function together with a proof that it satisfies `f a ≤ f b ↔ a ≤ b`. -/ def of_map_le_iff {α β} [partial_order α] [preorder β] (f : α → β) (hf : ∀ a b, f a ≤ f b ↔ a ≤ b) : α ↪o β := rel_embedding.of_map_rel_iff f hf @[simp] lemma coe_of_map_le_iff {α β} [partial_order α] [preorder β] {f : α → β} (h) : ⇑(of_map_le_iff f h) = f := rfl /-- A strictly monotone map from a linear order is an order embedding. -/ def of_strict_mono {α β} [linear_order α] [preorder β] (f : α → β) (h : strict_mono f) : α ↪o β := of_map_le_iff f (λ _ _, h.le_iff_le) @[simp] lemma coe_of_strict_mono {α β} [linear_order α] [preorder β] {f : α → β} (h : strict_mono f) : ⇑(of_strict_mono f h) = f := rfl /-- Embedding of a subtype into the ambient type as an `order_embedding`. -/ @[simps {fully_applied := ff}] def subtype (p : α → Prop) : subtype p ↪o α := ⟨function.embedding.subtype p, λ x y, iff.rfl⟩ /-- Convert an `order_embedding` to a `order_hom`. -/ @[simps {fully_applied := ff}] def to_order_hom {X Y : Type*} [preorder X] [preorder Y] (f : X ↪o Y) : X →o Y := { to_fun := f, monotone' := f.monotone } end order_embedding section rel_hom variables [partial_order α] [preorder β] namespace rel_hom variables (f : ((<) : α → α → Prop) →r ((<) : β → β → Prop)) /-- A bundled expression of the fact that a map between partial orders that is strictly monotone is weakly monotone. -/ @[simps {fully_applied := ff}] def to_order_hom : α →o β := { to_fun := f, monotone' := strict_mono.monotone (λ x y, f.map_rel), } end rel_hom lemma rel_embedding.to_order_hom_injective (f : ((<) : α → α → Prop) ↪r ((<) : β → β → Prop)) : function.injective (f : ((<) : α → α → Prop) →r ((<) : β → β → Prop)).to_order_hom := λ _ _ h, f.injective h end rel_hom namespace order_iso section has_le variables [has_le α] [has_le β] [has_le γ] instance : order_iso_class (α ≃o β) α β := { coe := λ f, f.to_fun, inv := λ f, f.inv_fun, left_inv := λ f, f.left_inv, right_inv := λ f, f.right_inv, coe_injective' := λ f g h₁ h₂, by { obtain ⟨⟨_, _⟩, _⟩ := f, obtain ⟨⟨_, _⟩, _⟩ := g, congr' }, map_le_map_iff := λ f _ _, f.map_rel_iff' } @[simp] lemma to_fun_eq_coe {f : α ≃o β} : f.to_fun = f := rfl @[ext] -- See note [partially-applied ext lemmas] lemma ext {f g : α ≃o β} (h : (f : α → β) = g) : f = g := fun_like.coe_injective h /-- Reinterpret an order isomorphism as an order embedding. -/ def to_order_embedding (e : α ≃o β) : α ↪o β := e.to_rel_embedding @[simp] lemma coe_to_order_embedding (e : α ≃o β) : ⇑(e.to_order_embedding) = e := rfl protected lemma bijective (e : α ≃o β) : function.bijective e := e.to_equiv.bijective protected lemma injective (e : α ≃o β) : function.injective e := e.to_equiv.injective protected lemma surjective (e : α ≃o β) : function.surjective e := e.to_equiv.surjective @[simp] lemma apply_eq_iff_eq (e : α ≃o β) {x y : α} : e x = e y ↔ x = y := e.to_equiv.apply_eq_iff_eq /-- Identity order isomorphism. -/ def refl (α : Type*) [has_le α] : α ≃o α := rel_iso.refl (≤) @[simp] lemma coe_refl : ⇑(refl α) = id := rfl @[simp] lemma refl_apply (x : α) : refl α x = x := rfl @[simp] lemma refl_to_equiv : (refl α).to_equiv = equiv.refl α := rfl /-- Inverse of an order isomorphism. -/ def symm (e : α ≃o β) : β ≃o α := e.symm @[simp] lemma apply_symm_apply (e : α ≃o β) (x : β) : e (e.symm x) = x := e.to_equiv.apply_symm_apply x @[simp] lemma symm_apply_apply (e : α ≃o β) (x : α) : e.symm (e x) = x := e.to_equiv.symm_apply_apply x @[simp] lemma symm_refl (α : Type*) [has_le α] : (refl α).symm = refl α := rfl lemma apply_eq_iff_eq_symm_apply (e : α ≃o β) (x : α) (y : β) : e x = y ↔ x = e.symm y := e.to_equiv.apply_eq_iff_eq_symm_apply theorem symm_apply_eq (e : α ≃o β) {x : α} {y : β} : e.symm y = x ↔ y = e x := e.to_equiv.symm_apply_eq @[simp] lemma symm_symm (e : α ≃o β) : e.symm.symm = e := by { ext, refl } lemma symm_injective : function.injective (symm : (α ≃o β) → (β ≃o α)) := λ e e' h, by rw [← e.symm_symm, h, e'.symm_symm] @[simp] lemma to_equiv_symm (e : α ≃o β) : e.to_equiv.symm = e.symm.to_equiv := rfl /-- Composition of two order isomorphisms is an order isomorphism. -/ @[trans] def trans (e : α ≃o β) (e' : β ≃o γ) : α ≃o γ := e.trans e' @[simp] lemma coe_trans (e : α ≃o β) (e' : β ≃o γ) : ⇑(e.trans e') = e' ∘ e := rfl @[simp] lemma trans_apply (e : α ≃o β) (e' : β ≃o γ) (x : α) : e.trans e' x = e' (e x) := rfl @[simp] lemma refl_trans (e : α ≃o β) : (refl α).trans e = e := by { ext x, refl } @[simp] lemma trans_refl (e : α ≃o β) : e.trans (refl β) = e := by { ext x, refl } @[simp] lemma symm_trans_apply (e₁ : α ≃o β) (e₂ : β ≃o γ) (c : γ) : (e₁.trans e₂).symm c = e₁.symm (e₂.symm c) := rfl lemma symm_trans (e₁ : α ≃o β) (e₂ : β ≃o γ) : (e₁.trans e₂).symm = e₂.symm.trans e₁.symm := rfl /-- `prod.swap` as an `order_iso`. -/ def prod_comm : (α × β) ≃o (β × α) := { to_equiv := equiv.prod_comm α β, map_rel_iff' := λ a b, prod.swap_le_swap } @[simp] lemma coe_prod_comm : ⇑(prod_comm : (α × β) ≃o (β × α)) = prod.swap := rfl @[simp] lemma prod_comm_symm : (prod_comm : (α × β) ≃o (β × α)).symm = prod_comm := rfl variables (α) /-- The order isomorphism between a type and its double dual. -/ def dual_dual : α ≃o αᵒᵈᵒᵈ := refl α @[simp] lemma coe_dual_dual : ⇑(dual_dual α) = to_dual ∘ to_dual := rfl @[simp] lemma coe_dual_dual_symm : ⇑(dual_dual α).symm = of_dual ∘ of_dual := rfl variables {α} @[simp] lemma dual_dual_apply (a : α) : dual_dual α a = to_dual (to_dual a) := rfl @[simp] lemma dual_dual_symm_apply (a : αᵒᵈᵒᵈ) : (dual_dual α).symm a = of_dual (of_dual a) := rfl end has_le open set section le variables [has_le α] [has_le β] [has_le γ] @[simp] lemma le_iff_le (e : α ≃o β) {x y : α} : e x ≤ e y ↔ x ≤ y := e.map_rel_iff lemma le_symm_apply (e : α ≃o β) {x : α} {y : β} : x ≤ e.symm y ↔ e x ≤ y := e.rel_symm_apply lemma symm_apply_le (e : α ≃o β) {x : α} {y : β} : e.symm y ≤ x ↔ y ≤ e x := e.symm_apply_rel end le variables [preorder α] [preorder β] [preorder γ] protected lemma monotone (e : α ≃o β) : monotone e := e.to_order_embedding.monotone protected lemma strict_mono (e : α ≃o β) : strict_mono e := e.to_order_embedding.strict_mono @[simp] lemma lt_iff_lt (e : α ≃o β) {x y : α} : e x < e y ↔ x < y := e.to_order_embedding.lt_iff_lt /-- Converts an `order_iso` into a `rel_iso (<) (<)`. -/ def to_rel_iso_lt (e : α ≃o β) : ((<) : α → α → Prop) ≃r ((<) : β → β → Prop) := ⟨e.to_equiv, λ x y, lt_iff_lt e⟩ @[simp] lemma to_rel_iso_lt_apply (e : α ≃o β) (x : α) : e.to_rel_iso_lt x = e x := rfl @[simp] lemma to_rel_iso_lt_symm (e : α ≃o β) : e.to_rel_iso_lt.symm = e.symm.to_rel_iso_lt := rfl /-- Converts a `rel_iso (<) (<)` into an `order_iso`. -/ def of_rel_iso_lt {α β} [partial_order α] [partial_order β] (e : ((<) : α → α → Prop) ≃r ((<) : β → β → Prop)) : α ≃o β := ⟨e.to_equiv, λ x y, by simp [le_iff_eq_or_lt, e.map_rel_iff]⟩ @[simp] lemma of_rel_iso_lt_apply {α β} [partial_order α] [partial_order β] (e : ((<) : α → α → Prop) ≃r ((<) : β → β → Prop)) (x : α) : of_rel_iso_lt e x = e x := rfl @[simp] lemma of_rel_iso_lt_symm {α β} [partial_order α] [partial_order β] (e : ((<) : α → α → Prop) ≃r ((<) : β → β → Prop)) : (of_rel_iso_lt e).symm = of_rel_iso_lt e.symm := rfl @[simp] lemma of_rel_iso_lt_to_rel_iso_lt {α β} [partial_order α] [partial_order β] (e : α ≃o β) : of_rel_iso_lt (to_rel_iso_lt e) = e := by { ext, simp } @[simp] lemma to_rel_iso_lt_of_rel_iso_lt {α β} [partial_order α] [partial_order β] (e : ((<) : α → α → Prop) ≃r ((<) : β → β → Prop)) : to_rel_iso_lt (of_rel_iso_lt e) = e := by { ext, simp } /-- To show that `f : α → β`, `g : β → α` make up an order isomorphism of linear orders, it suffices to prove `cmp a (g b) = cmp (f a) b`. -/ def of_cmp_eq_cmp {α β} [linear_order α] [linear_order β] (f : α → β) (g : β → α) (h : ∀ (a : α) (b : β), cmp a (g b) = cmp (f a) b) : α ≃o β := have gf : ∀ (a : α), a = g (f a) := by { intro, rw [←cmp_eq_eq_iff, h, cmp_self_eq_eq] }, { to_fun := f, inv_fun := g, left_inv := λ a, (gf a).symm, right_inv := by { intro, rw [←cmp_eq_eq_iff, ←h, cmp_self_eq_eq] }, map_rel_iff' := by { intros, apply le_iff_le_of_cmp_eq_cmp, convert (h _ _).symm, apply gf } } /-- To show that `f : α →o β` and `g : β →o α` make up an order isomorphism it is enough to show that `g` is the inverse of `f`-/ def of_hom_inv {F G : Type*} [order_hom_class F α β] [order_hom_class G β α] (f : F) (g : G) (h₁ : (f : α →o β).comp (g : β →o α) = order_hom.id) (h₂ : (g : β →o α).comp (f : α →o β) = order_hom.id) : α ≃o β := { to_fun := f, inv_fun := g, left_inv := fun_like.congr_fun h₂, right_inv := fun_like.congr_fun h₁, map_rel_iff' := λ a b, ⟨λ h, by { replace h := map_rel g h, rwa [equiv.coe_fn_mk, (show g (f a) = (g : β →o α).comp (f : α →o β) a, from rfl), (show g (f b) = (g : β →o α).comp (f : α →o β) b, from rfl), h₂] at h }, λ h, (f : α →o β).monotone h⟩ } /-- Order isomorphism between `α → β` and `β`, where `α` has a unique element. -/ @[simps to_equiv apply] def fun_unique (α β : Type*) [unique α] [preorder β] : (α → β) ≃o β := { to_equiv := equiv.fun_unique α β, map_rel_iff' := λ f g, by simp [pi.le_def, unique.forall_iff] } @[simp] lemma fun_unique_symm_apply {α β : Type*} [unique α] [preorder β] : ((fun_unique α β).symm : β → α → β) = function.const α := rfl end order_iso namespace equiv variables [preorder α] [preorder β] /-- If `e` is an equivalence with monotone forward and inverse maps, then `e` is an order isomorphism. -/ def to_order_iso (e : α ≃ β) (h₁ : monotone e) (h₂ : monotone e.symm) : α ≃o β := ⟨e, λ x y, ⟨λ h, by simpa only [e.symm_apply_apply] using h₂ h, λ h, h₁ h⟩⟩ @[simp] lemma coe_to_order_iso (e : α ≃ β) (h₁ : monotone e) (h₂ : monotone e.symm) : ⇑(e.to_order_iso h₁ h₂) = e := rfl @[simp] lemma to_order_iso_to_equiv (e : α ≃ β) (h₁ : monotone e) (h₂ : monotone e.symm) : (e.to_order_iso h₁ h₂).to_equiv = e := rfl end equiv namespace strict_mono variables {α β} [linear_order α] [preorder β] variables (f : α → β) (h_mono : strict_mono f) (h_surj : function.surjective f) /-- A strictly monotone function with a right inverse is an order isomorphism. -/ @[simps {fully_applied := false}] def order_iso_of_right_inverse (g : β → α) (hg : function.right_inverse g f) : α ≃o β := { to_fun := f, inv_fun := g, left_inv := λ x, h_mono.injective $ hg _, right_inv := hg, .. order_embedding.of_strict_mono f h_mono } end strict_mono /-- An order isomorphism is also an order isomorphism between dual orders. -/ protected def order_iso.dual [has_le α] [has_le β] (f : α ≃o β) : αᵒᵈ ≃o βᵒᵈ := ⟨f.to_equiv, λ _ _, f.map_rel_iff⟩ section lattice_isos lemma order_iso.map_bot' [has_le α] [partial_order β] (f : α ≃o β) {x : α} {y : β} (hx : ∀ x', x ≤ x') (hy : ∀ y', y ≤ y') : f x = y := by { refine le_antisymm _ (hy _), rw [← f.apply_symm_apply y, f.map_rel_iff], apply hx } lemma order_iso.map_bot [has_le α] [partial_order β] [order_bot α] [order_bot β] (f : α ≃o β) : f ⊥ = ⊥ := f.map_bot' (λ _, bot_le) (λ _, bot_le) lemma order_iso.map_top' [has_le α] [partial_order β] (f : α ≃o β) {x : α} {y : β} (hx : ∀ x', x' ≤ x) (hy : ∀ y', y' ≤ y) : f x = y := f.dual.map_bot' hx hy lemma order_iso.map_top [has_le α] [partial_order β] [order_top α] [order_top β] (f : α ≃o β) : f ⊤ = ⊤ := f.dual.map_bot lemma order_embedding.map_inf_le [semilattice_inf α] [semilattice_inf β] (f : α ↪o β) (x y : α) : f (x ⊓ y) ≤ f x ⊓ f y := f.monotone.map_inf_le x y lemma order_embedding.le_map_sup [semilattice_sup α] [semilattice_sup β] (f : α ↪o β) (x y : α) : f x ⊔ f y ≤ f (x ⊔ y) := f.monotone.le_map_sup x y lemma order_iso.map_inf [semilattice_inf α] [semilattice_inf β] (f : α ≃o β) (x y : α) : f (x ⊓ y) = f x ⊓ f y := begin refine (f.to_order_embedding.map_inf_le x y).antisymm _, apply f.symm.le_iff_le.1, simpa using f.symm.to_order_embedding.map_inf_le (f x) (f y), end lemma order_iso.map_sup [semilattice_sup α] [semilattice_sup β] (f : α ≃o β) (x y : α) : f (x ⊔ y) = f x ⊔ f y := f.dual.map_inf x y /-- Note that this goal could also be stated `(disjoint on f) a b` -/ lemma disjoint.map_order_iso [semilattice_inf α] [order_bot α] [semilattice_inf β] [order_bot β] {a b : α} (f : α ≃o β) (ha : disjoint a b) : disjoint (f a) (f b) := by { rw [disjoint_iff_inf_le, ←f.map_inf, ←f.map_bot], exact f.monotone ha.le_bot } /-- Note that this goal could also be stated `(codisjoint on f) a b` -/ lemma codisjoint.map_order_iso [semilattice_sup α] [order_top α] [semilattice_sup β] [order_top β] {a b : α} (f : α ≃o β) (ha : codisjoint a b) : codisjoint (f a) (f b) := by { rw [codisjoint_iff_le_sup, ←f.map_sup, ←f.map_top], exact f.monotone ha.top_le } @[simp] lemma disjoint_map_order_iso_iff [semilattice_inf α] [order_bot α] [semilattice_inf β] [order_bot β] {a b : α} (f : α ≃o β) : disjoint (f a) (f b) ↔ disjoint a b := ⟨λ h, f.symm_apply_apply a ▸ f.symm_apply_apply b ▸ h.map_order_iso f.symm, λ h, h.map_order_iso f⟩ @[simp] lemma codisjoint_map_order_iso_iff [semilattice_sup α] [order_top α] [semilattice_sup β] [order_top β] {a b : α} (f : α ≃o β) : codisjoint (f a) (f b) ↔ codisjoint a b := ⟨λ h, f.symm_apply_apply a ▸ f.symm_apply_apply b ▸ h.map_order_iso f.symm, λ h, h.map_order_iso f⟩ namespace with_bot /-- Taking the dual then adding `⊥` is the same as adding `⊤` then taking the dual. This is the order iso form of `with_bot.of_dual`, as proven by `coe_to_dual_top_equiv_eq`. -/ protected def to_dual_top_equiv [has_le α] : with_bot αᵒᵈ ≃o (with_top α)ᵒᵈ := order_iso.refl _ @[simp] lemma to_dual_top_equiv_coe [has_le α] (a : α) : with_bot.to_dual_top_equiv ↑(to_dual a) = to_dual (a : with_top α) := rfl @[simp] lemma to_dual_top_equiv_symm_coe [has_le α] (a : α) : with_bot.to_dual_top_equiv.symm (to_dual (a : with_top α)) = ↑(to_dual a) := rfl @[simp] lemma to_dual_top_equiv_bot [has_le α] : with_bot.to_dual_top_equiv (⊥ : with_bot αᵒᵈ) = ⊥ := rfl @[simp] lemma to_dual_top_equiv_symm_bot [has_le α] : with_bot.to_dual_top_equiv.symm (⊥ : (with_top α)ᵒᵈ) = ⊥ := rfl lemma coe_to_dual_top_equiv_eq [has_le α] : (with_bot.to_dual_top_equiv : with_bot αᵒᵈ → (with_top α)ᵒᵈ) = to_dual ∘ with_bot.of_dual := funext $ λ _, rfl end with_bot namespace with_top /-- Taking the dual then adding `⊤` is the same as adding `⊥` then taking the dual. This is the order iso form of `with_top.of_dual`, as proven by `coe_to_dual_bot_equiv_eq`. -/ protected def to_dual_bot_equiv [has_le α] : with_top αᵒᵈ ≃o (with_bot α)ᵒᵈ := order_iso.refl _ @[simp] lemma to_dual_bot_equiv_coe [has_le α] (a : α) : with_top.to_dual_bot_equiv ↑(to_dual a) = to_dual (a : with_bot α) := rfl @[simp] lemma to_dual_bot_equiv_symm_coe [has_le α] (a : α) : with_top.to_dual_bot_equiv.symm (to_dual (a : with_bot α)) = ↑(to_dual a) := rfl @[simp] lemma to_dual_bot_equiv_top [has_le α] : with_top.to_dual_bot_equiv (⊤ : with_top αᵒᵈ) = ⊤ := rfl @[simp] lemma to_dual_bot_equiv_symm_top [has_le α] : with_top.to_dual_bot_equiv.symm (⊤ : (with_bot α)ᵒᵈ) = ⊤ := rfl lemma coe_to_dual_bot_equiv_eq [has_le α] : (with_top.to_dual_bot_equiv : with_top αᵒᵈ → (with_bot α)ᵒᵈ) = to_dual ∘ with_top.of_dual := funext $ λ _, rfl end with_top namespace order_iso variables [partial_order α] [partial_order β] [partial_order γ] /-- A version of `equiv.option_congr` for `with_top`. -/ @[simps apply] def with_top_congr (e : α ≃o β) : with_top α ≃o with_top β := { to_equiv := e.to_equiv.option_congr, .. e.to_order_embedding.with_top_map } @[simp] lemma with_top_congr_refl : (order_iso.refl α).with_top_congr = order_iso.refl _ := rel_iso.to_equiv_injective equiv.option_congr_refl @[simp] lemma with_top_congr_symm (e : α ≃o β) : e.with_top_congr.symm = e.symm.with_top_congr := rel_iso.to_equiv_injective e.to_equiv.option_congr_symm @[simp] lemma with_top_congr_trans (e₁ : α ≃o β) (e₂ : β ≃o γ) : e₁.with_top_congr.trans e₂.with_top_congr = (e₁.trans e₂).with_top_congr := rel_iso.to_equiv_injective $ e₁.to_equiv.option_congr_trans e₂.to_equiv /-- A version of `equiv.option_congr` for `with_bot`. -/ @[simps apply] def with_bot_congr (e : α ≃o β) : with_bot α ≃o with_bot β := { to_equiv := e.to_equiv.option_congr, .. e.to_order_embedding.with_bot_map } @[simp] lemma with_bot_congr_refl : (order_iso.refl α).with_bot_congr = order_iso.refl _ := rel_iso.to_equiv_injective equiv.option_congr_refl @[simp] lemma with_bot_congr_symm (e : α ≃o β) : e.with_bot_congr.symm = e.symm.with_bot_congr := rel_iso.to_equiv_injective e.to_equiv.option_congr_symm @[simp] lemma with_bot_congr_trans (e₁ : α ≃o β) (e₂ : β ≃o γ) : e₁.with_bot_congr.trans e₂.with_bot_congr = (e₁.trans e₂).with_bot_congr := rel_iso.to_equiv_injective $ e₁.to_equiv.option_congr_trans e₂.to_equiv end order_iso section bounded_order variables [lattice α] [lattice β] [bounded_order α] [bounded_order β] (f : α ≃o β) include f lemma order_iso.is_compl {x y : α} (h : is_compl x y) : is_compl (f x) (f y) := ⟨h.1.map_order_iso _, h.2.map_order_iso _⟩ theorem order_iso.is_compl_iff {x y : α} : is_compl x y ↔ is_compl (f x) (f y) := ⟨f.is_compl, λ h, f.symm_apply_apply x ▸ f.symm_apply_apply y ▸ f.symm.is_compl h⟩ lemma order_iso.complemented_lattice [complemented_lattice α] : complemented_lattice β := ⟨λ x, begin obtain ⟨y, hy⟩ := exists_is_compl (f.symm x), rw ← f.symm_apply_apply y at hy, refine ⟨f y, f.symm.is_compl_iff.2 hy⟩, end⟩ theorem order_iso.complemented_lattice_iff : complemented_lattice α ↔ complemented_lattice β := ⟨by { introI, exact f.complemented_lattice }, by { introI, exact f.symm.complemented_lattice }⟩ end bounded_order end lattice_isos -- Developments relating order homs and sets belong in `order.hom.set` or later. assert_not_exists set.range
c5338e7a36ac29f7e02b32c142db5dca65de0cbd
d0f9af2b0ace5ce352570d61b09019c8ef4a3b96
/hw3/dm_prod.lean
2da11ed5c2ebc93a70de7e2b977b729fe817bd76
[]
no_license
jngo13/Discrete-Mathematics
8671540ef2da7c75915d32332dd20c02f001474e
bf674a866e61f60e6e6d128df85fa73819091787
refs/heads/master
1,675,615,657,924
1,609,142,011,000
1,609,142,011,000
267,190,341
0
0
null
null
null
null
UTF-8
Lean
false
false
1,642
lean
-- Justin Ngo -- jmn4fms -- 2/3/20 -- Sullivan 2102-001 /- 1. [30 points] In class we implemented a polymorphic ordered pair abstract data type that we called prod_S_T. In a new file called dm_prod.lean re-implement this ADT but call it dm_prod. Implement at least the following functions: - fst - snd - set_fst - set_snd - swap For this part of the homework, write the function definitions using lambda expressions and in all cases specific "implicit arguments" for the type arguments of these functions. -/ /- 2. [40 points]. After each function definition in your dm_prod.test file, write two new equivalent function definitions, the first one using C-style and the second using "by cases" style. Add one prime/tick mark to the name of each C-style function to avoid name conflicts, and two tick marks for the "cases-style" definitions. For all functions, specify the use of implicit arguments for type arguments. -/ inductive dm_prod (S T :Type) : Type | mk : S → T → dm_prod def fst {S T: Type}: (dm_prod S T) → S:= λ (p : dm_prod S T), match p with |(dm_prod.mk x _) := x end def snd {S T: Type}: (dm_prod S T) → T:= λ (p : dm_prod S T), match p with |(dm_prod.mk _ y) := y end def set_fst {S T : Type} : (dm_prod S T) → S → dm_prod S T := λ (p : dm_prod S T) (s : S), dm_prod.mk s (snd p) def set_snd {S T : Type}: (dm_prod S T) → T → dm_prod S T := λ (p : dm_prod S T) (t : T), dm_prod.mk (fst p) t def swap {S T: Type}: (dm_prod S T) → S → T → dm_prod T S:= λ (p: dm_prod S T) (s:S) (t:T), dm_prod.mk (snd p) (fst p)
4c05b4e766076187d944074314306b5cf3ca237d
c31182a012eec69da0a1f6c05f42b0f0717d212d
/src/system_of_complexes/rescale.lean
3ee8192a617d7781f5a5b6d19d995da7a2b60c89
[]
no_license
Ja1941/lean-liquid
fbec3ffc7fc67df1b5ca95b7ee225685ab9ffbdc
8e80ed0cbdf5145d6814e833a674eaf05a1495c1
refs/heads/master
1,689,437,983,362
1,628,362,719,000
1,628,362,719,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,507
lean
import system_of_complexes.basic import rescale.normed_group /-! # rescaling norms on a system of complexes This file defines the `rescale` functor which will take a system of complexes of seminormed groups and systematically rescale all the norms on all the seminormed groups by a constant factor. -/ noncomputable theory universe variables u open category_theory open_locale nat nnreal namespace nnreal def MulLeft (κ : ℝ≥0) : ℝ≥0 ⥤ ℝ≥0 := { obj := λ c, κ * c, map := λ c₁ c₂ h, hom_of_le $ mul_le_mul' le_rfl (le_of_hom h) } def MulRight (κ : ℝ≥0) : ℝ≥0 ⥤ ℝ≥0 := { obj := λ c, c * κ, map := λ c₁ c₂ h, hom_of_le $ mul_le_mul' (le_of_hom h) le_rfl } end nnreal namespace system_of_complexes def rescale (r : ℝ≥0) [fact (0 < r)] : system_of_complexes.{u} ⥤ system_of_complexes.{u} := (whiskering_right _ _ _).obj $ (SemiNormedGroup.rescale r).map_homological_complex _ lemma rescale_obj (r c : ℝ≥0) [fact (0 < r)] (C : system_of_complexes) (i : ℕ) : ↥(((rescale r).obj C) c i) = _root_.rescale r (C c i) := rfl lemma rescale_d (r c : ℝ≥0) [fact (0 < r)] (C : system_of_complexes) (i j : ℕ) (v : (((rescale r).obj C) c i)) : (((rescale r).obj C).d i j) v = @rescale.of r _ ((C.d i j) (((@rescale.of r _).symm) v)) := rfl instance rescale.additive (r : ℝ≥0) [fact (0 < r)] : (rescale r).additive := { map_zero' := λ X Y, by { ext, refl }, -- ext can be removed but it makes the proof longer map_add' := λ X Y f g, by { ext, refl } } -- a heavy refl . -- can we golf this? speed it up? def to_rescale (r : ℝ≥0) [fact (0 < r)] : 𝟭 system_of_complexes ⟶ rescale r := { app := λ C, { app := λ c, { f := λ _, (SemiNormedGroup.to_rescale r).app _, comm' := by { intros, exact ((SemiNormedGroup.to_rescale r).naturality _).symm } }, naturality' := by { intros c₁ c₂ h, ext i : 2, refl } }, naturality' := λ C₁ C₂ f, by { ext, refl } } . def scale (i j : ℝ≥0) [fact (0 < i)] [fact (0 < j)] : rescale i ⟶ rescale j := (whiskering_right _ _ _).map $ nat_trans.map_homological_complex (SemiNormedGroup.scale i j) _ section exact_and_admissible variables {k K : ℝ≥0} [fact (1 ≤ k)] {m : ℕ} {c₀ : ℝ≥0} lemma rescale_is_weak_bounded_exact (r : ℝ≥0) [hr : fact (0 < r)] (C : system_of_complexes) (hC : C.is_weak_bounded_exact k K m c₀) : ((rescale r).obj C).is_weak_bounded_exact k K m c₀ := begin intros c hc i hi x ε hε, obtain ⟨_, _, rfl, rfl, y, hy⟩ := hC c hc i hi ((@rescale.of r _).symm x) (ε * r) _, swap, { exact mul_pos hε hr.out }, refine ⟨_, _, rfl, rfl, (@rescale.of r _) y, _⟩, erw [rescale.norm_def, rescale.norm_def], rwa [div_le_iff, add_mul, mul_assoc, div_mul_cancel], { apply ne_of_gt, exact hr.out }, { exact hr.out }, end . /-- `rescale C` is admissible if `C` is. -/ lemma rescale_admissible (r : ℝ≥0) [fact (0 < r)] (C : system_of_complexes) (hC : C.admissible) : ((rescale r).obj C).admissible := { d_norm_noninc' := begin rintro c i j h, rintro (v : _root_.rescale r (C c i)), -- rw rescale_obj gives motive issues rw [rescale_d, rescale.norm_def, rescale.norm_def, equiv.symm_apply_apply], refine div_le_div_of_le_of_nonneg _ _, { apply hC.d_norm_noninc' c i j h}, { exact nnreal.coe_nonneg r }, end, res_norm_noninc := λ c' c i h v, div_le_div_of_le_of_nonneg (hC.res_norm_noninc c' c i h _) (nnreal.coe_nonneg r) } end exact_and_admissible instance (m : ℕ) : fact (0 < m!) := ⟨nat.factorial_pos _⟩ def rescale_functor : ℕ → (system_of_complexes ⥤ system_of_complexes) | 0 := 𝟭 _ | 1 := 𝟭 _ | (m+2) := rescale (m+2)! instance rescale_functor.additive : Π m, (rescale_functor m).additive | 0 := functor.id.additive | 1 := functor.id.additive | (m+2) := show (rescale (m+2)!).additive, from rescale.additive _ def rescale_nat_trans : Π i j, rescale_functor i ⟶ rescale_functor j | 0 1 := 𝟙 _ | 1 (j+2) := to_rescale (j+2)! | (i+2) (j+2) := scale (i+2)! (j+2)! | _ _ := 0 section scale_index @[simps] def ScaleIndexLeft (κ : ℝ≥0) : system_of_complexes ⥤ system_of_complexes := (whiskering_left _ _ _).obj (nnreal.MulLeft κ).op @[simp] lemma ScaleIndexLeft_apply (C : system_of_complexes) (κ c : ℝ≥0) (i : ℕ) : (ScaleIndexLeft κ).obj C c i = C (κ * c) i := rfl def scale_index_left (C : system_of_complexes) (κ : ℝ≥0) := (ScaleIndexLeft κ).obj C lemma admissible.scale_index_left {C : system_of_complexes} (hC : C.admissible) (κ : ℝ≥0) : (C.scale_index_left κ).admissible := { d_norm_noninc' := λ c i j hij, (by { apply admissible.d_norm_noninc C hC (κ * c) i j, }), res_norm_noninc := λ c₁ c₂ i hc, hC.res_norm_noninc _ _ i (by { resetI, dsimp, apply_instance }) } lemma is_weak_bounded_exact.scale_index_left {C : system_of_complexes} {k K :ℝ≥0} {m : ℕ} (c₀ c₁: ℝ≥0) [fact (1 ≤ k)] (hC : C.is_weak_bounded_exact k K m c₀) (κ : ℝ≥0) [hκ : fact (c₀ ≤ κ * c₁)] (C_adm : C.admissible) : (C.scale_index_left κ).is_weak_bounded_exact k K m c₁ := begin intros c hc i hi x ε hε, dsimp [scale_index_left, ScaleIndexLeft_apply] at x, haveI aux1 : fact (k * (κ * c) ≤ κ * (k * c)) := ⟨(mul_left_comm _ _ _).le⟩, obtain ⟨i₀, j, hi₀, hj, y, hy⟩ := hC (κ * c) _ i hi (res x) ε hε, swap, { exact ⟨hκ.1.trans $ fact.out _⟩, }, refine ⟨i₀, j, hi₀, hj, y, _⟩, simp only [res_res, d_res] at hy, refine hy.trans (add_le_add (mul_le_mul le_rfl _ (norm_nonneg _) K.coe_nonneg) le_rfl), apply C_adm.res_norm_noninc, end @[simps] def ScaleIndexRight (κ : ℝ≥0) : system_of_complexes ⥤ system_of_complexes := (whiskering_left _ _ _).obj (nnreal.MulRight κ).op @[simp] lemma ScaleIndexRight_apply (C : system_of_complexes) (κ c : ℝ≥0) (i : ℕ) : (ScaleIndexRight κ).obj C c i = C (c * κ) i := rfl def scale_index_right (C : system_of_complexes) (κ : ℝ≥0) := (ScaleIndexRight κ).obj C lemma admissible.scale_index_right {C : system_of_complexes} (hC : C.admissible) (κ : ℝ≥0) : (C.scale_index_right κ).admissible := { d_norm_noninc' := λ c i j hij, (by { apply admissible.d_norm_noninc C hC (c * κ) i j, }), res_norm_noninc := λ c₁ c₂ i hc, hC.res_norm_noninc _ _ i (by { resetI, dsimp, apply_instance }) } lemma is_weak_bounded_exact.scale_index_right {C : system_of_complexes} {k K :ℝ≥0} {m : ℕ} (c₀ c₁ : ℝ≥0) [fact (1 ≤ k)] (hC : C.is_weak_bounded_exact k K m c₀) (κ : ℝ≥0) [hκ : fact (c₀ ≤ κ * c₁)] (C_adm : C.admissible) : (C.scale_index_right κ).is_weak_bounded_exact k K m c₁ := begin intros c hc i hi x ε hε, dsimp [scale_index_right, ScaleIndexRight_apply] at x, haveI aux1 : fact (k * (c * κ) ≤ k * c * κ) := ⟨(mul_assoc _ _ _).ge⟩, obtain ⟨i₀, j, hi₀, hj, y, hy⟩ := hC (c * κ) _ i hi (res x) ε hε, swap, { rw mul_comm, exact ⟨hκ.1.trans $ fact.out _⟩, }, refine ⟨i₀, j, hi₀, hj, y, _⟩, simp only [res_res, d_res] at hy, refine hy.trans (add_le_add (mul_le_mul le_rfl _ (norm_nonneg _) K.coe_nonneg) le_rfl), apply C_adm.res_norm_noninc, end end scale_index end system_of_complexes namespace thm95 def rescale_functor' : ℕ → ((ℝ≥0ᵒᵖ ⥤ SemiNormedGroup) ⥤ (ℝ≥0ᵒᵖ ⥤ SemiNormedGroup)) | 0 := 𝟭 _ | 1 := 𝟭 _ | (m+2) := (whiskering_right _ _ _).obj (SemiNormedGroup.rescale (m+2)!) instance rescale_functor'.additive : Π m, (rescale_functor' m).additive | 0 := functor.id.additive | 1 := functor.id.additive | (m+2) := {} def to_rescale' (r : ℝ≥0) [fact (0 < r)] : 𝟭 (ℝ≥0ᵒᵖ ⥤ SemiNormedGroup) ⟶ ((whiskering_right _ _ _).obj (SemiNormedGroup.rescale r)) := { app := λ V, { app := λ c, (SemiNormedGroup.to_rescale r).app _, naturality' := by { intros c₁ c₂ h, dsimp, ext i : 2, refl } }, naturality' := λ C₁ C₂ f, by { ext, refl } } @[simps app] def scale' (i j : ℝ≥0) [fact (0 < i)] [fact (0 < j)] : ((whiskering_right ℝ≥0ᵒᵖ _ _).obj (SemiNormedGroup.rescale i)) ⟶ ((whiskering_right ℝ≥0ᵒᵖ _ _).obj (SemiNormedGroup.rescale j)) := (whiskering_right ℝ≥0ᵒᵖ _ _).map $ SemiNormedGroup.scale i j def rescale_nat_trans' : Π i j, rescale_functor' i ⟶ rescale_functor' j | 0 1 := 𝟙 _ | 1 (j+2) := to_rescale' (j+2)! | (i+2) (j+2) := scale' (i+2)! (j+2)! | _ _ := 0 end thm95
464d877c2722de9b6cf15d9c0355e51930d1ab93
b32d3853770e6eaf06817a1b8c52064baaed0ef1
/src/super/utils.lean
8e37cb69a4171e2844a46a39ef457ac732528d8a
[]
no_license
gebner/super2
4d58b7477b6f7d945d5d866502982466db33ab0b
9bc5256c31750021ab97d6b59b7387773e54b384
refs/heads/master
1,635,021,682,021
1,634,886,326,000
1,634,886,326,000
225,600,688
4
2
null
1,598,209,306,000
1,575,371,550,000
Lean
UTF-8
Lean
false
false
8,488
lean
import tactic.core attribute [inline] or.decidable decidable.to_bool bool.decidable_eq and.decidable nat.decidable_eq ne.decidable decidable.false implies.decidable option.get_or_else option.map meta def format.form (as : list format) : format := (format.join (as.intersperse format.line)).paren.group open native meta def expr.meta_vars (e : expr) : rb_set expr := e.fold mk_rb_set $ λ e i ms, match e with | expr.mvar _ _ _ := ms.insert e | _ := ms end meta def list.dup_by_native {α β} [has_lt β] [decidable_rel ((<) : β → β → Prop)] (f : α → β) (xs : list α) : list α := rb_map.values $ xs.foldl (λ m x, m.insert (f x) x) mk_rb_map meta def level.mvar_name : level → name | (level.mvar n) := n | _ := name.anonymous private meta def level.meta_vars_core : level → name_set → name_set | level.zero := id | (level.param _) := id | (level.succ l) := l.meta_vars_core | (level.mvar n) := λ ms, ms.insert n | (level.max a b) := a.meta_vars_core ∘ b.meta_vars_core | (level.imax a b) := a.meta_vars_core ∘ b.meta_vars_core meta def level.meta_vars (l : level) : name_set := level.meta_vars_core l mk_name_set meta def expr.univ_meta_vars (e : expr) : name_set := e.fold mk_name_set $ λ e i ms, match e with | expr.sort l := level.meta_vars_core l ms | expr.const _ ls := ls.foldr level.meta_vars_core ms | _ := ms end def list.index_of_core' {α} [decidable_eq α] (a : α) : ℕ → list α → option ℕ | i [] := none | i (x::xs) := if x = a then some i else xs.index_of_core' (i+1) def list.index_of' {α} [decidable_eq α] (a : α) (xs : list α) : option ℕ := xs.index_of_core' a 0 meta def expr.abstract_mvars (e : expr) (mvars : list name) : expr := e.replace $ λ e i, match e with | e@(expr.mvar n _ _) := (mvars.index_of' n).map (λ j, expr.var (i + j)) | e := none end meta def expr.meta_uniq_name : expr → name | (expr.mvar n _ _) := n | _ := name.anonymous meta def expr.meta_type : expr → expr | (expr.mvar _ _ t) := t | _ := default _ meta def expr.abstract_mvars' (e : expr) (mvars : list expr) : expr := e.abstract_mvars (mvars.map expr.meta_uniq_name) open tactic private meta def sorted_mvars_core : list expr → list expr → tactic (list expr) | (e@(expr.mvar n pp_n _)::es) ctx := if ∃ m ∈ ctx, (m : expr).meta_uniq_name = e.meta_uniq_name then sorted_mvars_core es ctx else do t ← infer_type e >>= instantiate_mvars, ctx ← sorted_mvars_core t.meta_vars.to_list ctx, sorted_mvars_core es (e :: ctx) | [] ctx := pure ctx | (e::es) ctx := sorted_mvars_core (e.meta_vars.to_list ++ es) ctx meta def expr.sorted_mvars' (es : list expr) : tactic (list expr) := sorted_mvars_core es [] meta def expr.sorted_mvars (e : expr) : tactic (list expr) := expr.sorted_mvars' [e] meta def abstract_mvar_telescope : list expr → tactic (list expr) | [] := pure [] | (m :: ms) := do t ← infer_type m >>= instantiate_mvars, ms' ← abstract_mvar_telescope ms, pure $ t.abstract_mvars' ms :: ms' meta def level.instantiate_univ_mvars (subst : rb_map name level) : level → level | level.zero := level.zero | (level.succ a) := a.instantiate_univ_mvars.succ | (level.max a b) := level.max (a.instantiate_univ_mvars) (b.instantiate_univ_mvars) | (level.imax a b) := level.imax (a.instantiate_univ_mvars) (b.instantiate_univ_mvars) | l@(level.param _) := l | l@(level.mvar n) := (subst.find n).get_or_else l meta def expr.instantiate_univ_mvars (subst : rb_map name level) (e : expr) : expr := e.replace $ λ e i, match e with | (expr.const n ls) := some $ expr.const n (ls.map (level.instantiate_univ_mvars subst)) | (expr.sort l) := some $ expr.sort (l.instantiate_univ_mvars subst) | _ := none end meta def expr.mk_lambda (x e : expr) : expr := expr.lam x.local_pp_name x.local_binding_info x.local_type (e.abstract x) meta def expr.mk_lambdas (xs : list expr) (e : expr) : expr := xs.foldr expr.mk_lambda e meta def expr.mk_pi (x e : expr) : expr := expr.pi x.local_pp_name x.local_binding_info x.local_type (e.abstract x) meta def expr.mk_pis (xs : list expr) (e : expr) : expr := xs.foldr expr.mk_pi e meta def expr.app' : expr → expr → expr | (expr.lam _ _ _ a) b := a.instantiate_var b | a b := a b lemma or_imp_congr {p p' q q'} (hp : p → p') (hq : q → q') : p ∨ q → p' ∨ q' | (or.inl h) := or.inl (hp h) | (or.inr h) := or.inr (hq h) lemma or_imp_congr_left {p q r} (h : q → r) : q ∨ p → r ∨ p := or_imp_congr h id lemma or_imp_congr_right {p q r} (h : q → r) : p ∨ q → p ∨ r := or_imp_congr id h lemma or_imp_congr_right_strong {p q r} (h : ¬ p → q → r) : p ∨ q → p ∨ r := match classical.prop_decidable p with | decidable.is_true hp := λ _, or.inl hp | decidable.is_false hp := or_imp_congr_right (h hp) end lemma imp_iff_or_not {p q : Prop} : (p → q) ↔ (¬ p ∨ q) := by cases classical.prop_decidable p; simp * lemma not_imp_iff_or {p q : Prop} : (¬ p → q) ↔ (p ∨ q) := by cases classical.prop_decidable p; simp * theorem iff_imp {a b c} : ((a ↔ b) → c) ↔ ((a → b) → (b → a) → c) := iff.intro (λ h ha hb, h ⟨ha, hb⟩) (λ h ⟨ha, hb⟩, h ha hb) lemma classical.forall_imp_iff_exists_not_or {α} {p : α → Prop} {q : Prop} : ((∀ x, p x) → q) ↔ ((∃ x, ¬ p x) ∨ q) := by simp [imp_iff_or_not] def list.has_dups_core {α} [decidable_eq α] : list α → list α → bool | (x::xs) ys := x ∈ ys ∨ xs.has_dups_core (x::ys) | [] _ := ff def list.has_dups {α} [decidable_eq α] (xs : list α) : bool := xs.has_dups_core [] def list.zip_with_index_core {α} : ℕ → list α → list (α × ℕ) | _ [] := [] | i (x::xs) := (x,i) :: list.zip_with_index_core (i+1) xs def list.zip_with_index {α} : list α → list (α × ℕ) := list.zip_with_index_core 0 def list.filter_maximal {α} (gt : α → α → bool) (l : list α) : list α := l.filter $ λ x, ∀ y ∈ l, ¬ gt y x def list.m_any {α m} [monad m] (f : α → m bool) : list α → m bool | [] := pure ff | (x::xs) := do f_x ← f x, if f_x then pure tt else xs.m_any meta def mk_metas_core : list expr → tactic (list expr) | [] := pure [] | (t::ts) := do ms ← mk_metas_core ts, m ← mk_meta_var (t.instantiate_vars ms), pure (m::ms) meta def expr.name_hint : expr → option name | (expr.const n _) := n | (expr.app a b) := a.name_hint <|> b.name_hint | (expr.pi pp_n _ a b) := b.name_hint <|> a.name_hint <|> pp_n | (expr.lam pp_n _ a b) := b.name_hint <|> pp_n | (expr.sort _) := `type | (expr.local_const _ pp_n _ _) := pp_n | _ := name.anonymous meta def expr.hyp_name_hint (e : expr) : name := match e.name_hint with | some (name.mk_string s _) := ("h_" ++ s : string) | _ := `h end meta def mk_locals_core : list expr → tactic (list expr) | [] := pure [] | (t::ts) := do lcs ← mk_locals_core ts, lc ← mk_local' t.hyp_name_hint binder_info.default (t.instantiate_vars lcs), pure (lc :: lcs) meta def expr.const_levels : expr → list level | (expr.const n ls) := ls | _ := [] @[inline] instance has_monad_lift.refl {m} [monad m] : has_monad_lift m m := ⟨λ _, id⟩ meta def tactic.unify_level (l1 l2 : level) : tactic unit := tactic.unify (expr.sort l1) (expr.sort l2) namespace tactic meta def unify_with_type (a b : expr) (trnsp := transparency.semireducible) (approx := ff) : tactic unit := do ta ← infer_type a, tb ← infer_type b, unify ta tb trnsp approx, unify a b trnsp approx meta def minimal_tc_failure : expr → tactic expr | e := do ff ← succeeds (type_check e), match e with | (expr.app a b) := minimal_tc_failure a <|> minimal_tc_failure b | (expr.lam n bi a b) := minimal_tc_failure a <|> (do l ← mk_local' n bi a, minimal_tc_failure (b.instantiate_var a)) | (expr.pi n bi a b) := minimal_tc_failure a <|> (do l ← mk_local' n bi a, minimal_tc_failure (b.instantiate_var a)) | (expr.elet n t v b) := minimal_tc_failure t <|> minimal_tc_failure v <|> minimal_tc_failure (b.instantiate_var v) | e := pure e end <|> pure e end tactic -- decidable instance for bounded existence is not short-circuiting?!? def list.existsb {α} (p : α → bool) : list α → bool | [] := ff | (x :: xs) := if p x then tt else xs.existsb meta def infer_univ (type : expr) : tactic level := do sort_of_type ← infer_type type >>= whnf, match sort_of_type with | expr.sort lvl := pure lvl | not_sort := do fmt ← pp not_sort, fail $ (to_fmt "cannot get universe level of sort:" ++ format.line ++ fmt).group.nest 1 end
85e459ac531690355e86e452fb4fbad40d482877
ee8cdbabf07f77e7be63a449b8483ce308d37218
/lean/src/valid/mathd-numbertheory-110.lean
b041aa3ed6a17fb9d7f5ae62d714efbc83dfd81a
[ "MIT", "Apache-2.0" ]
permissive
zeta1999/miniF2F
6d66c75d1c18152e224d07d5eed57624f731d4b7
c1ba9629559c5273c92ec226894baa0c1ce27861
refs/heads/main
1,681,897,460,642
1,620,646,361,000
1,620,646,361,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
344
lean
/- Copyright (c) 2021 OpenAI. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kunhao Zheng -/ import data.pnat.basic import data.real.basic example (a b : ℕ) (h₀ : 0 < a ∧ 0 < b ∧ b ≤ a) (h₁ : (a + b) % 10 = 2) (h₂ : (2 * a + b) % 10 = 1) : (a - b) % 10 = 6 := begin sorry end
9de56aedbf925ec43085af1dcf6ab79e9ff97f15
94e33a31faa76775069b071adea97e86e218a8ee
/src/analysis/special_functions/complex/arg.lean
787c96a7bd4cf0ca65b8fb66d080ad8ec8c8f6c8
[ "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
25,842
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Benjamin Davidson -/ import analysis.special_functions.trigonometric.angle import analysis.special_functions.trigonometric.inverse /-! # The argument of a complex number. We define `arg : ℂ → ℝ`, returing a real number in the range (-π, π], such that for `x ≠ 0`, `sin (arg x) = x.im / x.abs` and `cos (arg x) = x.re / x.abs`, while `arg 0` defaults to `0` -/ noncomputable theory namespace complex open_locale complex_conjugate real topological_space open filter set /-- `arg` returns values in the range (-π, π], such that for `x ≠ 0`, `sin (arg x) = x.im / x.abs` and `cos (arg x) = x.re / x.abs`, `arg 0` defaults to `0` -/ noncomputable def arg (x : ℂ) : ℝ := if 0 ≤ x.re then real.arcsin (x.im / x.abs) else if 0 ≤ x.im then real.arcsin ((-x).im / x.abs) + π else real.arcsin ((-x).im / x.abs) - π lemma sin_arg (x : ℂ) : real.sin (arg x) = x.im / x.abs := by unfold arg; split_ifs; simp [sub_eq_add_neg, arg, real.sin_arcsin (abs_le.1 (abs_im_div_abs_le_one x)).1 (abs_le.1 (abs_im_div_abs_le_one x)).2, real.sin_add, neg_div, real.arcsin_neg, real.sin_neg] lemma cos_arg {x : ℂ} (hx : x ≠ 0) : real.cos (arg x) = x.re / x.abs := begin have habs : 0 < abs x := abs_pos.2 hx, have him : |im x / abs x| ≤ 1, { rw [_root_.abs_div, abs_abs], exact div_le_one_of_le x.abs_im_le_abs x.abs_nonneg }, rw abs_le at him, rw arg, split_ifs with h₁ h₂ h₂, { rw [real.cos_arcsin]; field_simp [real.sqrt_sq, habs.le, *] }, { rw [real.cos_add_pi, real.cos_arcsin], { field_simp [real.sqrt_div (sq_nonneg _), real.sqrt_sq_eq_abs, _root_.abs_of_neg (not_le.1 h₁), *] }, { simpa [neg_div] using him.2 }, { simpa [neg_div, neg_le] using him.1 } }, { rw [real.cos_sub_pi, real.cos_arcsin], { field_simp [real.sqrt_div (sq_nonneg _), real.sqrt_sq_eq_abs, _root_.abs_of_neg (not_le.1 h₁), *] }, { simpa [neg_div] using him.2 }, { simpa [neg_div, neg_le] using him.1 } } end @[simp] lemma abs_mul_exp_arg_mul_I (x : ℂ) : ↑(abs x) * exp (arg x * I) = x := begin rcases eq_or_ne x 0 with (rfl|hx), { simp }, { have : abs x ≠ 0 := abs_ne_zero.2 hx, ext; field_simp [sin_arg, cos_arg hx, this, mul_comm (abs x)] } end @[simp] lemma abs_mul_cos_add_sin_mul_I (x : ℂ) : (abs x * (cos (arg x) + sin (arg x) * I) : ℂ) = x := by rw [← exp_mul_I, abs_mul_exp_arg_mul_I] lemma abs_eq_one_iff (z : ℂ) : abs z = 1 ↔ ∃ θ : ℝ, exp (θ * I) = z := begin refine ⟨λ hz, ⟨arg z, _⟩, _⟩, { calc exp (arg z * I) = abs z * exp (arg z * I) : by rw [hz, of_real_one, one_mul] ... = z : abs_mul_exp_arg_mul_I z }, { rintro ⟨θ, rfl⟩, exact complex.abs_exp_of_real_mul_I θ }, end @[simp] lemma range_exp_mul_I : range (λ x : ℝ, exp (x * I)) = metric.sphere 0 1 := by { ext x, simp only [mem_sphere_zero_iff_norm, norm_eq_abs, abs_eq_one_iff, mem_range] } lemma arg_mul_cos_add_sin_mul_I {r : ℝ} (hr : 0 < r) {θ : ℝ} (hθ : θ ∈ Ioc (-π) π) : arg (r * (cos θ + sin θ * I)) = θ := begin have hπ := real.pi_pos, simp only [arg, abs_mul, abs_cos_add_sin_mul_I, abs_of_nonneg hr.le, mul_one], simp only [of_real_mul_re, of_real_mul_im, neg_im, ← of_real_cos, ← of_real_sin, ← mk_eq_add_mul_I, neg_div, mul_div_cancel_left _ hr.ne', mul_nonneg_iff_right_nonneg_of_pos hr], by_cases h₁ : θ ∈ Icc (-(π / 2)) (π / 2), { rw if_pos, exacts [real.arcsin_sin' h₁, real.cos_nonneg_of_mem_Icc h₁] }, { rw [mem_Icc, not_and_distrib, not_le, not_le] at h₁, cases h₁, { replace hθ := hθ.1, have hcos : real.cos θ < 0, { rw [← neg_pos, ← real.cos_add_pi], refine real.cos_pos_of_mem_Ioo ⟨_, _⟩; linarith }, have hsin : real.sin θ < 0 := real.sin_neg_of_neg_of_neg_pi_lt (by linarith) hθ, rw [if_neg, if_neg, ← real.sin_add_pi, real.arcsin_sin, add_sub_cancel]; [linarith, linarith, exact hsin.not_le, exact hcos.not_le] }, { replace hθ := hθ.2, have hcos : real.cos θ < 0 := real.cos_neg_of_pi_div_two_lt_of_lt h₁ (by linarith), have hsin : 0 ≤ real.sin θ := real.sin_nonneg_of_mem_Icc ⟨by linarith, hθ⟩, rw [if_neg, if_pos, ← real.sin_sub_pi, real.arcsin_sin, sub_add_cancel]; [linarith, linarith, exact hsin, exact hcos.not_le] } } end lemma arg_cos_add_sin_mul_I {θ : ℝ} (hθ : θ ∈ Ioc (-π) π) : arg (cos θ + sin θ * I) = θ := by rw [← one_mul (_ + _), ← of_real_one, arg_mul_cos_add_sin_mul_I zero_lt_one hθ] @[simp] lemma arg_zero : arg 0 = 0 := by simp [arg, le_refl] lemma ext_abs_arg {x y : ℂ} (h₁ : x.abs = y.abs) (h₂ : x.arg = y.arg) : x = y := by rw [← abs_mul_exp_arg_mul_I x, ← abs_mul_exp_arg_mul_I y, h₁, h₂] lemma ext_abs_arg_iff {x y : ℂ} : x = y ↔ abs x = abs y ∧ arg x = arg y := ⟨λ h, h ▸ ⟨rfl, rfl⟩, and_imp.2 ext_abs_arg⟩ lemma arg_mem_Ioc (z : ℂ) : arg z ∈ Ioc (-π) π := begin have hπ : 0 < π := real.pi_pos, rcases eq_or_ne z 0 with (rfl|hz), simp [hπ, hπ.le], rcases exists_unique_add_zsmul_mem_Ioc real.two_pi_pos (arg z) (-π) with ⟨N, hN, -⟩, rw [two_mul, neg_add_cancel_left, ← two_mul, zsmul_eq_mul] at hN, rw [← abs_mul_cos_add_sin_mul_I z, ← cos_add_int_mul_two_pi _ N, ← sin_add_int_mul_two_pi _ N], simp only [← of_real_one, ← of_real_bit0, ← of_real_mul, ← of_real_add, ← of_real_int_cast], rwa [arg_mul_cos_add_sin_mul_I (abs_pos.2 hz) hN] end @[simp] lemma range_arg : range arg = Ioc (-π) π := (range_subset_iff.2 arg_mem_Ioc).antisymm (λ x hx, ⟨_, arg_cos_add_sin_mul_I hx⟩) lemma arg_le_pi (x : ℂ) : arg x ≤ π := (arg_mem_Ioc x).2 lemma neg_pi_lt_arg (x : ℂ) : -π < arg x := (arg_mem_Ioc x).1 @[simp] lemma arg_nonneg_iff {z : ℂ} : 0 ≤ arg z ↔ 0 ≤ z.im := begin rcases eq_or_ne z 0 with (rfl|h₀), { simp }, calc 0 ≤ arg z ↔ 0 ≤ real.sin (arg z) : ⟨λ h, real.sin_nonneg_of_mem_Icc ⟨h, arg_le_pi z⟩, by { contrapose!, intro h, exact real.sin_neg_of_neg_of_neg_pi_lt h (neg_pi_lt_arg _) }⟩ ... ↔ _ : by rw [sin_arg, le_div_iff (abs_pos.2 h₀), zero_mul] end @[simp] lemma arg_neg_iff {z : ℂ} : arg z < 0 ↔ z.im < 0 := lt_iff_lt_of_le_iff_le arg_nonneg_iff lemma arg_real_mul (x : ℂ) {r : ℝ} (hr : 0 < r) : arg (r * x) = arg x := begin rcases eq_or_ne x 0 with (rfl|hx), { rw mul_zero }, conv_lhs { rw [← abs_mul_cos_add_sin_mul_I x, ← mul_assoc, ← of_real_mul, arg_mul_cos_add_sin_mul_I (mul_pos hr (abs_pos.2 hx)) x.arg_mem_Ioc] } end lemma arg_eq_arg_iff {x y : ℂ} (hx : x ≠ 0) (hy : y ≠ 0) : arg x = arg y ↔ (abs y / abs x : ℂ) * x = y := begin simp only [ext_abs_arg_iff, abs_mul, abs_div, abs_of_real, abs_abs, div_mul_cancel _ (abs_ne_zero.2 hx), eq_self_iff_true, true_and], rw [← of_real_div, arg_real_mul], exact div_pos (abs_pos.2 hy) (abs_pos.2 hx) end @[simp] lemma arg_one : arg 1 = 0 := by simp [arg, zero_le_one] @[simp] lemma arg_neg_one : arg (-1) = π := by simp [arg, le_refl, not_le.2 (@zero_lt_one ℝ _ _)] @[simp] lemma arg_I : arg I = π / 2 := by simp [arg, le_refl] @[simp] lemma arg_neg_I : arg (-I) = -(π / 2) := by simp [arg, le_refl] @[simp] lemma tan_arg (x : ℂ) : real.tan (arg x) = x.im / x.re := begin by_cases h : x = 0, { simp only [h, zero_div, complex.zero_im, complex.arg_zero, real.tan_zero, complex.zero_re] }, rw [real.tan_eq_sin_div_cos, sin_arg, cos_arg h, div_div_div_cancel_right _ (abs_ne_zero.2 h)] end lemma arg_of_real_of_nonneg {x : ℝ} (hx : 0 ≤ x) : arg x = 0 := by simp [arg, hx] lemma arg_eq_zero_iff {z : ℂ} : arg z = 0 ↔ 0 ≤ z.re ∧ z.im = 0 := begin refine ⟨λ h, _, _⟩, { rw [←abs_mul_cos_add_sin_mul_I z, h], simp [abs_nonneg] }, { cases z with x y, rintro ⟨h, rfl : y = 0⟩, exact arg_of_real_of_nonneg h } end lemma arg_eq_pi_iff {z : ℂ} : arg z = π ↔ z.re < 0 ∧ z.im = 0 := begin by_cases h₀ : z = 0, { simp [h₀, lt_irrefl, real.pi_ne_zero.symm] }, split, { intro h, rw [← abs_mul_cos_add_sin_mul_I z, h], simp [h₀] }, { cases z with x y, rintro ⟨h : x < 0, rfl : y = 0⟩, rw [← arg_neg_one, ← arg_real_mul (-1) (neg_pos.2 h)], simp [← of_real_def] } end lemma arg_lt_pi_iff {z : ℂ} : arg z < π ↔ 0 ≤ z.re ∨ z.im ≠ 0 := by rw [(arg_le_pi z).lt_iff_ne, not_iff_comm, not_or_distrib, not_le, not_not, arg_eq_pi_iff] lemma arg_of_real_of_neg {x : ℝ} (hx : x < 0) : arg x = π := arg_eq_pi_iff.2 ⟨hx, rfl⟩ lemma arg_eq_pi_div_two_iff {z : ℂ} : arg z = π / 2 ↔ z.re = 0 ∧ 0 < z.im := begin by_cases h₀ : z = 0, { simp [h₀, lt_irrefl, real.pi_div_two_pos.ne] }, split, { intro h, rw [← abs_mul_cos_add_sin_mul_I z, h], simp [h₀] }, { cases z with x y, rintro ⟨rfl : x = 0, hy : 0 < y⟩, rw [← arg_I, ← arg_real_mul I hy, of_real_mul', I_re, I_im, mul_zero, mul_one] } end lemma arg_eq_neg_pi_div_two_iff {z : ℂ} : arg z = - (π / 2) ↔ z.re = 0 ∧ z.im < 0 := begin by_cases h₀ : z = 0, { simp [h₀, lt_irrefl, real.pi_ne_zero] }, split, { intro h, rw [← abs_mul_cos_add_sin_mul_I z, h], simp [h₀] }, { cases z with x y, rintro ⟨rfl : x = 0, hy : y < 0⟩, rw [← arg_neg_I, ← arg_real_mul (-I) (neg_pos.2 hy), mk_eq_add_mul_I], simp } end lemma arg_of_re_nonneg {x : ℂ} (hx : 0 ≤ x.re) : arg x = real.arcsin (x.im / x.abs) := if_pos hx lemma arg_of_re_neg_of_im_nonneg {x : ℂ} (hx_re : x.re < 0) (hx_im : 0 ≤ x.im) : arg x = real.arcsin ((-x).im / x.abs) + π := by simp only [arg, hx_re.not_le, hx_im, if_true, if_false] lemma arg_of_re_neg_of_im_neg {x : ℂ} (hx_re : x.re < 0) (hx_im : x.im < 0) : arg x = real.arcsin ((-x).im / x.abs) - π := by simp only [arg, hx_re.not_le, hx_im.not_le, if_false] lemma arg_of_im_nonneg_of_ne_zero {z : ℂ} (h₁ : 0 ≤ z.im) (h₂ : z ≠ 0) : arg z = real.arccos (z.re / abs z) := by rw [← cos_arg h₂, real.arccos_cos (arg_nonneg_iff.2 h₁) (arg_le_pi _)] lemma arg_of_im_pos {z : ℂ} (hz : 0 < z.im) : arg z = real.arccos (z.re / abs z) := arg_of_im_nonneg_of_ne_zero hz.le (λ h, hz.ne' $ h.symm ▸ rfl) lemma arg_of_im_neg {z : ℂ} (hz : z.im < 0) : arg z = -real.arccos (z.re / abs z) := begin have h₀ : z ≠ 0, from mt (congr_arg im) hz.ne, rw [← cos_arg h₀, ← real.cos_neg, real.arccos_cos, neg_neg], exacts [neg_nonneg.2 (arg_neg_iff.2 hz).le, neg_le.2 (neg_pi_lt_arg z).le] end lemma arg_conj (x : ℂ) : arg (conj x) = if arg x = π then π else -arg x := begin simp_rw [arg_eq_pi_iff, arg, neg_im, conj_im, conj_re, abs_conj, neg_div, neg_neg, real.arcsin_neg, apply_ite has_neg.neg, neg_add, neg_sub, neg_neg, ←sub_eq_add_neg, sub_neg_eq_add, add_comm π], rcases lt_trichotomy x.re 0 with (hr|hr|hr); rcases lt_trichotomy x.im 0 with (hi|hi|hi), { simp [hr, hr.not_le, hi.le, hi.ne, not_le.2 hi] }, { simp [hr, hr.not_le, hi] }, { simp [hr, hr.not_le, hi.ne.symm, hi.le, not_le.2 hi] }, { simp [hr] }, { simp [hr] }, { simp [hr] }, { simp [hr, hr.le, hi.ne] }, { simp [hr, hr.le, hr.le.not_lt] }, { simp [hr, hr.le, hr.le.not_lt] }, end lemma arg_inv (x : ℂ) : arg x⁻¹ = if arg x = π then π else -arg x := begin rw [←arg_conj, inv_def, mul_comm], by_cases hx : x = 0, { simp [hx] }, { exact arg_real_mul (conj x) (by simp [hx]) } end lemma arg_le_pi_div_two_iff {z : ℂ} : arg z ≤ π / 2 ↔ 0 ≤ re z ∨ im z < 0 := begin cases le_or_lt 0 (re z) with hre hre, { simp only [hre, arg_of_re_nonneg hre, real.arcsin_le_pi_div_two, true_or] }, simp only [hre.not_le, false_or], cases le_or_lt 0 (im z) with him him, { simp only [him.not_lt], rw [iff_false, not_le, arg_of_re_neg_of_im_nonneg hre him, ← sub_lt_iff_lt_add, half_sub, real.neg_pi_div_two_lt_arcsin, neg_im, neg_div, neg_lt_neg_iff, div_lt_one, ← _root_.abs_of_nonneg him, abs_im_lt_abs], exacts [hre.ne, abs_pos.2 $ ne_of_apply_ne re hre.ne] }, { simp only [him], rw [iff_true, arg_of_re_neg_of_im_neg hre him], exact (sub_le_self _ real.pi_pos.le).trans (real.arcsin_le_pi_div_two _) } end lemma neg_pi_div_two_le_arg_iff {z : ℂ} : -(π / 2) ≤ arg z ↔ 0 ≤ re z ∨ 0 ≤ im z := begin cases le_or_lt 0 (re z) with hre hre, { simp only [hre, arg_of_re_nonneg hre, real.neg_pi_div_two_le_arcsin, true_or] }, simp only [hre.not_le, false_or], cases le_or_lt 0 (im z) with him him, { simp only [him], rw [iff_true, arg_of_re_neg_of_im_nonneg hre him], exact (real.neg_pi_div_two_le_arcsin _).trans (le_add_of_nonneg_right real.pi_pos.le) }, { simp only [him.not_le], rw [iff_false, not_le, arg_of_re_neg_of_im_neg hre him, sub_lt_iff_lt_add', ← sub_eq_add_neg, sub_half, real.arcsin_lt_pi_div_two, div_lt_one, neg_im, ← abs_of_neg him, abs_im_lt_abs], exacts [hre.ne, abs_pos.2 $ ne_of_apply_ne re hre.ne] } end @[simp] lemma abs_arg_le_pi_div_two_iff {z : ℂ} : |arg z| ≤ π / 2 ↔ 0 ≤ re z := by rw [abs_le, arg_le_pi_div_two_iff, neg_pi_div_two_le_arg_iff, ← or_and_distrib_left, ← not_le, and_not_self, or_false] @[simp] lemma arg_conj_coe_angle (x : ℂ) : (arg (conj x) : real.angle) = -arg x := begin by_cases h : arg x = π; simp [arg_conj, h] end @[simp] lemma arg_inv_coe_angle (x : ℂ) : (arg x⁻¹ : real.angle) = -arg x := begin by_cases h : arg x = π; simp [arg_inv, h] end lemma arg_neg_eq_arg_sub_pi_of_im_pos {x : ℂ} (hi : 0 < x.im) : arg (-x) = arg x - π := begin rw [arg_of_im_pos hi, arg_of_im_neg (show (-x).im < 0, from left.neg_neg_iff.2 hi)], simp [neg_div, real.arccos_neg] end lemma arg_neg_eq_arg_add_pi_of_im_neg {x : ℂ} (hi : x.im < 0) : arg (-x) = arg x + π := begin rw [arg_of_im_neg hi, arg_of_im_pos (show 0 < (-x).im, from left.neg_pos_iff.2 hi)], simp [neg_div, real.arccos_neg, add_comm, ←sub_eq_add_neg] end lemma arg_neg_eq_arg_sub_pi_iff {x : ℂ} : arg (-x) = arg x - π ↔ (0 < x.im ∨ x.im = 0 ∧ x.re < 0) := begin rcases lt_trichotomy x.im 0 with (hi|hi|hi), { simp [hi, hi.ne, hi.not_lt, arg_neg_eq_arg_add_pi_of_im_neg, sub_eq_add_neg, ←add_eq_zero_iff_eq_neg, real.pi_ne_zero] }, { rw (ext rfl hi : x = x.re), rcases lt_trichotomy x.re 0 with (hr|hr|hr), { rw [arg_of_real_of_neg hr, ←of_real_neg, arg_of_real_of_nonneg (left.neg_pos_iff.2 hr).le], simp [hr] }, { simp [hr, hi, real.pi_ne_zero] }, { rw [arg_of_real_of_nonneg hr.le, ←of_real_neg, arg_of_real_of_neg (left.neg_neg_iff.2 hr)], simp [hr.not_lt, ←add_eq_zero_iff_eq_neg, real.pi_ne_zero] } }, { simp [hi, arg_neg_eq_arg_sub_pi_of_im_pos] } end lemma arg_neg_eq_arg_add_pi_iff {x : ℂ} : arg (-x) = arg x + π ↔ (x.im < 0 ∨ x.im = 0 ∧ 0 < x.re) := begin rcases lt_trichotomy x.im 0 with (hi|hi|hi), { simp [hi, arg_neg_eq_arg_add_pi_of_im_neg] }, { rw (ext rfl hi : x = x.re), rcases lt_trichotomy x.re 0 with (hr|hr|hr), { rw [arg_of_real_of_neg hr, ←of_real_neg, arg_of_real_of_nonneg (left.neg_pos_iff.2 hr).le], simp [hr.not_lt, ←two_mul, real.pi_ne_zero] }, { simp [hr, hi, real.pi_ne_zero.symm] }, { rw [arg_of_real_of_nonneg hr.le, ←of_real_neg, arg_of_real_of_neg (left.neg_neg_iff.2 hr)], simp [hr] } }, { simp [hi, hi.ne.symm, hi.not_lt, arg_neg_eq_arg_sub_pi_of_im_pos, sub_eq_add_neg, ←add_eq_zero_iff_neg_eq, real.pi_ne_zero] } end lemma arg_neg_coe_angle {x : ℂ} (hx : x ≠ 0) : (arg (-x) : real.angle) = arg x + π := begin rcases lt_trichotomy x.im 0 with (hi|hi|hi), { rw [arg_neg_eq_arg_add_pi_of_im_neg hi, real.angle.coe_add] }, { rw (ext rfl hi : x = x.re), rcases lt_trichotomy x.re 0 with (hr|hr|hr), { rw [arg_of_real_of_neg hr, ←of_real_neg, arg_of_real_of_nonneg (left.neg_pos_iff.2 hr).le, ←real.angle.coe_add, ←two_mul, real.angle.coe_two_pi, real.angle.coe_zero] }, { exact false.elim (hx (ext hr hi)) }, { rw [arg_of_real_of_nonneg hr.le, ←of_real_neg, arg_of_real_of_neg (left.neg_neg_iff.2 hr), real.angle.coe_zero, zero_add] } }, { rw [arg_neg_eq_arg_sub_pi_of_im_pos hi, real.angle.coe_sub, real.angle.sub_coe_pi_eq_add_coe_pi] } end lemma arg_mul_cos_add_sin_mul_I_eq_mul_fract {r : ℝ} (hr : 0 < r) (θ : ℝ) : arg (r * (cos θ + sin θ * I)) = π - 2 * π * int.fract ((π - θ) / (2 * π)) := begin have hi : π - 2 * π * int.fract ((π - θ) / (2 * π)) ∈ Ioc (-π) π, { rw [←mem_preimage, preimage_const_sub_Ioc, ←mem_preimage, preimage_const_mul_Ico _ _ real.two_pi_pos, sub_self, zero_div, sub_neg_eq_add, ←two_mul, div_self real.two_pi_pos.ne.symm], refine set.mem_of_mem_of_subset (set.mem_range_self _) _, rw [←image_univ, int.image_fract], simp }, have hs : π - 2 * π * int.fract ((π - θ) / (2 * π)) = 2 * π * ⌊(π - θ) / (2 * π)⌋ + θ, { rw [int.fract, mul_sub, mul_div_cancel' _ real.two_pi_pos.ne.symm], abel }, convert arg_mul_cos_add_sin_mul_I hr hi using 3, simp_rw [hs, mul_comm (2 * π), add_comm _ θ, ←of_real_cos, ←of_real_sin, real.cos_add_int_mul_two_pi, real.sin_add_int_mul_two_pi] end lemma arg_cos_add_sin_mul_I_eq_mul_fract (θ : ℝ) : arg (cos θ + sin θ * I) = π - 2 * π * int.fract ((π - θ) / (2 * π)) := by rw [←one_mul (_ + _), ←of_real_one, arg_mul_cos_add_sin_mul_I_eq_mul_fract zero_lt_one] lemma arg_mul_cos_add_sin_mul_I_sub {r : ℝ} (hr : 0 < r) (θ : ℝ) : arg (r * (cos θ + sin θ * I)) - θ = 2 * π * ⌊(π - θ) / (2 * π)⌋ := begin rw [arg_mul_cos_add_sin_mul_I_eq_mul_fract hr, int.fract, mul_sub, mul_div_cancel' _ real.two_pi_pos.ne.symm], abel end lemma arg_cos_add_sin_mul_I_sub (θ : ℝ) : arg (cos θ + sin θ * I) - θ = 2 * π * ⌊(π - θ) / (2 * π)⌋ := by rw [←one_mul (_ + _), ←of_real_one, arg_mul_cos_add_sin_mul_I_sub zero_lt_one] lemma arg_mul_cos_add_sin_mul_I_coe_angle {r : ℝ} (hr : 0 < r) (θ : real.angle) : (arg (r * (real.angle.cos θ + real.angle.sin θ * I)) : real.angle) = θ := begin induction θ using real.angle.induction_on, rw [real.angle.cos_coe, real.angle.sin_coe, real.angle.angle_eq_iff_two_pi_dvd_sub], use ⌊(π - θ) / (2 * π)⌋, exact_mod_cast arg_mul_cos_add_sin_mul_I_sub hr θ end lemma arg_cos_add_sin_mul_I_coe_angle (θ : real.angle) : (arg (real.angle.cos θ + real.angle.sin θ * I) : real.angle) = θ := by rw [←one_mul (_ + _), ←of_real_one, arg_mul_cos_add_sin_mul_I_coe_angle zero_lt_one] lemma arg_mul_coe_angle {x y : ℂ} (hx : x ≠ 0) (hy : y ≠ 0) : (arg (x * y) : real.angle) = arg x + arg y := begin convert arg_mul_cos_add_sin_mul_I_coe_angle (mul_pos (abs_pos.2 hx) (abs_pos.2 hy)) (arg x + arg y : real.angle) using 3, simp_rw [←real.angle.coe_add, real.angle.sin_coe, real.angle.cos_coe, of_real_cos, of_real_sin, cos_add_sin_I, of_real_add, add_mul, exp_add, of_real_mul], rw [mul_assoc, mul_comm (exp _), ←mul_assoc (abs y : ℂ), abs_mul_exp_arg_mul_I, mul_comm y, ←mul_assoc, abs_mul_exp_arg_mul_I] end lemma arg_div_coe_angle {x y : ℂ} (hx : x ≠ 0) (hy : y ≠ 0) : (arg (x / y) : real.angle) = arg x - arg y := by rw [div_eq_mul_inv, arg_mul_coe_angle hx (inv_ne_zero hy), arg_inv_coe_angle, sub_eq_add_neg] @[simp] lemma arg_coe_angle_eq_iff {x y : ℂ} : (arg x : real.angle) = arg y ↔ arg x = arg y := begin split, { intro h, rw real.angle.angle_eq_iff_two_pi_dvd_sub at h, rcases h with ⟨k, hk⟩, rw ←sub_eq_zero, have ha : -(2 * π) < arg x - arg y, { linarith only [neg_pi_lt_arg x, arg_le_pi y] }, have hb : arg x - arg y < 2 * π, { linarith only [arg_le_pi x, neg_pi_lt_arg y] }, rw [hk, neg_lt, neg_mul_eq_mul_neg, mul_lt_iff_lt_one_right real.two_pi_pos, neg_lt, ←int.cast_one, ←int.cast_neg, int.cast_lt] at ha, rw [hk, mul_lt_iff_lt_one_right real.two_pi_pos, ←int.cast_one, int.cast_lt] at hb, have hk' : k = 0, { linarith only [ha, hb] }, rw hk' at hk, simpa using hk }, { intro h, rw h } end section continuity variables {x z : ℂ} lemma arg_eq_nhds_of_re_pos (hx : 0 < x.re) : arg =ᶠ[𝓝 x] λ x, real.arcsin (x.im / x.abs) := ((continuous_re.tendsto _).eventually (lt_mem_nhds hx)).mono $ λ y hy, arg_of_re_nonneg hy.le lemma arg_eq_nhds_of_re_neg_of_im_pos (hx_re : x.re < 0) (hx_im : 0 < x.im) : arg =ᶠ[𝓝 x] λ x, real.arcsin ((-x).im / x.abs) + π := begin suffices h_forall_nhds : ∀ᶠ (y : ℂ) in (𝓝 x), y.re < 0 ∧ 0 < y.im, from h_forall_nhds.mono (λ y hy, arg_of_re_neg_of_im_nonneg hy.1 hy.2.le), refine is_open.eventually_mem _ (⟨hx_re, hx_im⟩ : x.re < 0 ∧ 0 < x.im), exact is_open.and (is_open_lt continuous_re continuous_zero) (is_open_lt continuous_zero continuous_im), end lemma arg_eq_nhds_of_re_neg_of_im_neg (hx_re : x.re < 0) (hx_im : x.im < 0) : arg =ᶠ[𝓝 x] λ x, real.arcsin ((-x).im / x.abs) - π := begin suffices h_forall_nhds : ∀ᶠ (y : ℂ) in (𝓝 x), y.re < 0 ∧ y.im < 0, from h_forall_nhds.mono (λ y hy, arg_of_re_neg_of_im_neg hy.1 hy.2), refine is_open.eventually_mem _ (⟨hx_re, hx_im⟩ : x.re < 0 ∧ x.im < 0), exact is_open.and (is_open_lt continuous_re continuous_zero) (is_open_lt continuous_im continuous_zero), end lemma arg_eq_nhds_of_im_pos (hz : 0 < im z) : arg =ᶠ[𝓝 z] λ x, real.arccos (x.re / abs x) := ((continuous_im.tendsto _).eventually (lt_mem_nhds hz)).mono $ λ x, arg_of_im_pos lemma arg_eq_nhds_of_im_neg (hz : im z < 0) : arg =ᶠ[𝓝 z] λ x, -real.arccos (x.re / abs x) := ((continuous_im.tendsto _).eventually (gt_mem_nhds hz)).mono $ λ x, arg_of_im_neg lemma continuous_at_arg (h : 0 < x.re ∨ x.im ≠ 0) : continuous_at arg x := begin have h₀ : abs x ≠ 0, { rw abs_ne_zero, rintro rfl, simpa using h }, rw [← lt_or_lt_iff_ne] at h, rcases h with (hx_re|hx_im|hx_im), exacts [(real.continuous_at_arcsin.comp (continuous_im.continuous_at.div continuous_abs.continuous_at h₀)).congr (arg_eq_nhds_of_re_pos hx_re).symm, (real.continuous_arccos.continuous_at.comp (continuous_re.continuous_at.div continuous_abs.continuous_at h₀)).neg.congr (arg_eq_nhds_of_im_neg hx_im).symm, (real.continuous_arccos.continuous_at.comp (continuous_re.continuous_at.div continuous_abs.continuous_at h₀)).congr (arg_eq_nhds_of_im_pos hx_im).symm] end lemma tendsto_arg_nhds_within_im_neg_of_re_neg_of_im_zero {z : ℂ} (hre : z.re < 0) (him : z.im = 0) : tendsto arg (𝓝[{z : ℂ | z.im < 0}] z) (𝓝 (-π)) := begin suffices H : tendsto (λ x : ℂ, real.arcsin ((-x).im / x.abs) - π) (𝓝[{z : ℂ | z.im < 0}] z) (𝓝 (-π)), { refine H.congr' _, have : ∀ᶠ x : ℂ in 𝓝 z, x.re < 0, from continuous_re.tendsto z (gt_mem_nhds hre), filter_upwards [self_mem_nhds_within, mem_nhds_within_of_mem_nhds this] with _ him hre, rw [arg, if_neg hre.not_le, if_neg him.not_le], }, convert (real.continuous_at_arcsin.comp_continuous_within_at ((continuous_im.continuous_at.comp_continuous_within_at continuous_within_at_neg).div continuous_abs.continuous_within_at _)).sub tendsto_const_nhds, { simp [him] }, { lift z to ℝ using him, simpa using hre.ne } end lemma continuous_within_at_arg_of_re_neg_of_im_zero {z : ℂ} (hre : z.re < 0) (him : z.im = 0) : continuous_within_at arg {z : ℂ | 0 ≤ z.im} z := begin have : arg =ᶠ[𝓝[{z : ℂ | 0 ≤ z.im}] z] λ x, real.arcsin ((-x).im / x.abs) + π, { have : ∀ᶠ x : ℂ in 𝓝 z, x.re < 0, from continuous_re.tendsto z (gt_mem_nhds hre), filter_upwards [self_mem_nhds_within, mem_nhds_within_of_mem_nhds this] with _ him hre, rw [arg, if_neg hre.not_le, if_pos him] }, refine continuous_within_at.congr_of_eventually_eq _ this _, { refine (real.continuous_at_arcsin.comp_continuous_within_at ((continuous_im.continuous_at.comp_continuous_within_at continuous_within_at_neg).div continuous_abs.continuous_within_at _)).add tendsto_const_nhds, lift z to ℝ using him, simpa using hre.ne }, { rw [arg, if_neg hre.not_le, if_pos him.ge] } end lemma tendsto_arg_nhds_within_im_nonneg_of_re_neg_of_im_zero {z : ℂ} (hre : z.re < 0) (him : z.im = 0) : tendsto arg (𝓝[{z : ℂ | 0 ≤ z.im}] z) (𝓝 π) := by simpa only [arg_eq_pi_iff.2 ⟨hre, him⟩] using (continuous_within_at_arg_of_re_neg_of_im_zero hre him).tendsto lemma continuous_at_arg_coe_angle (h : x ≠ 0) : continuous_at (coe ∘ arg : ℂ → real.angle) x := begin by_cases hs : 0 < x.re ∨ x.im ≠ 0, { exact real.angle.continuous_coe.continuous_at.comp (continuous_at_arg hs) }, { rw [←function.comp.right_id (coe ∘ arg), (function.funext_iff.2 (λ _, (neg_neg _).symm) : (id : ℂ → ℂ) = has_neg.neg ∘ has_neg.neg), ←function.comp.assoc], refine continuous_at.comp _ continuous_neg.continuous_at, suffices : continuous_at (function.update ((coe ∘ arg) ∘ has_neg.neg : ℂ → real.angle) 0 π) (-x), by rwa continuous_at_update_of_ne (neg_ne_zero.2 h) at this, have ha : function.update ((coe ∘ arg) ∘ has_neg.neg : ℂ → real.angle) 0 π = λ z, (arg z : real.angle) + π, { rw function.update_eq_iff, exact ⟨by simp, λ z hz, arg_neg_coe_angle hz⟩ }, rw ha, push_neg at hs, refine (real.angle.continuous_coe.continuous_at.comp (continuous_at_arg (or.inl _))).add continuous_at_const, rw [neg_re, neg_pos], exact hs.1.lt_of_ne (λ h0, h (ext_iff.2 ⟨h0, hs.2⟩)) } end end continuity end complex
3c0845308430a0ae4748ae2d940da88750f4a069
8f0ea954c1cc4f4cda83826954d6186c68fc9536
/hott/homotopy/wedge.hlean
0e3b4c28a28a9c5f8338d260d57e15ff151f82e2
[ "Apache-2.0" ]
permissive
piyush-kurur/lean
fb3cbd339dfa8c70c49559fbea88ac0d3102b8ca
a8db8bc61a0b00379b3d0be8ecaf0d0858dc82ee
refs/heads/master
1,594,387,140,961
1,457,548,608,000
1,457,909,538,000
53,615,930
0
0
null
1,457,642,939,000
1,457,642,939,000
null
UTF-8
Lean
false
false
2,925
hlean
/- Copyright (c) 2016 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jakob von Raumer, Ulrik Buchholtz The Wedge Sum of Two pType Types -/ import hit.pointed_pushout .connectedness open eq pushout pointed unit trunc_index definition pwedge (A B : Type*) : Type* := ppushout (pconst punit A) (pconst punit B) namespace wedge -- TODO maybe find a cleaner proof protected definition unit (A : Type*) : A ≃* pwedge punit A := begin fapply pequiv_of_pmap, { fapply pmap.mk, intro a, apply pinr a, apply respect_pt }, { fapply is_equiv.adjointify, intro x, fapply pushout.elim_on x, exact λ x, Point A, exact id, intro u, reflexivity, intro x, fapply pushout.rec_on x, intro u, cases u, esimp, apply (glue unit.star)⁻¹, intro a, reflexivity, intro u, cases u, esimp, apply eq_pathover, refine _ ⬝hp !ap_id⁻¹, fapply eq_hconcat, apply ap_compose inr, krewrite elim_glue, fapply eq_hconcat, apply ap_idp, apply square_of_eq, apply con.left_inv, intro a, reflexivity}, end end wedge open trunc is_trunc function homotopy namespace wedge_extension section -- The wedge connectivity lemma (Lemma 8.6.2) parameters {A B : Type*} (n m : ℕ) [cA : is_conn n A] [cB : is_conn m B] (P : A → B → Type) [HP : Πa b, is_trunc (m + n) (P a b)] (f : Πa : A, P a pt) (g : Πb : B, P pt b) (p : f pt = g pt) include cA cB HP private definition Q (a : A) : Type := fiber (λs : (Πb : B, P a b), s (Point B)) (f a) private definition is_trunc_Q (a : A) : is_trunc (n.-1) (Q a) := begin refine @is_conn.elim_general (m.-1) _ _ _ (P a) _ (f a), rewrite [-succ_add_succ, of_nat_add_of_nat], intro b, apply HP end local attribute is_trunc_Q [instance] private definition Q_sec : Πa : A, Q a := is_conn.elim (n.-1) Q (fiber.mk g p⁻¹) protected definition ext : Π(a : A)(b : B), P a b := λa, fiber.point (Q_sec a) protected definition β_left (a : A) : ext a (Point B) = f a := fiber.point_eq (Q_sec a) private definition coh_aux : Σq : ext (Point A) = g, β_left (Point A) = ap (λs : (Πb : B, P (Point A) b), s (Point B)) q ⬝ p⁻¹ := equiv.to_fun (fiber.fiber_eq_equiv (Q_sec (Point A)) (fiber.mk g p⁻¹)) (is_conn.elim_β (n.-1) Q (fiber.mk g p⁻¹)) protected definition β_right (b : B) : ext (Point A) b = g b := apd10 (sigma.pr1 coh_aux) b private definition lem : β_left (Point A) = β_right (Point B) ⬝ p⁻¹ := begin unfold β_right, unfold β_left, krewrite (apd10_eq_ap_eval (sigma.pr1 coh_aux) (Point B)), exact sigma.pr2 coh_aux, end protected definition coh : (β_left (Point A))⁻¹ ⬝ β_right (Point B) = p := by rewrite [lem,con_inv,inv_inv,con.assoc,con.left_inv] end end wedge_extension
227259a04a73d12e05d34eb1d0667a4de2d79b10
9cb9db9d79fad57d80ca53543dc07efb7c4f3838
/src/for_mathlib/linear_algebra.lean
3b9653fdc2a984e84189e70931f53e0af9127415
[]
no_license
mr-infty/lean-liquid
3ff89d1f66244b434654c59bdbd6b77cb7de0109
a8db559073d2101173775ccbd85729d3a4f1ed4d
refs/heads/master
1,678,465,145,334
1,614,565,310,000
1,614,565,310,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
487
lean
import linear_algebra.matrix namespace matrix open equiv variables {m n R : Type*} [fintype m] [fintype n] [semiring R] (M : matrix m n R) lemma reindex_linear_equiv_sum_empty_symm : (reindex_linear_equiv (sum_empty _) (sum_empty _)).symm M = from_blocks M 0 0 0 := begin ext (i|i) (j|j), { simp only [sum_empty_apply_inl, reindex_linear_equiv_symm_apply, from_blocks_apply₁₁] }, { cases j }, { cases i } end end matrix #lint- only unused_arguments def_lemma doc_blame
47f382ce68f5be338beac058aa198112ca10afc5
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/nat/with_bot_auto.lean
44c57df1e433d6fdaf25b7901fef8e4a4c8956e9
[]
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,026
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.nat.basic import Mathlib.algebra.ordered_group import Mathlib.PostPort namespace Mathlib /-! # `with_bot ℕ` Lemmas about the type of natural numbers with a bottom element adjoined. -/ namespace nat theorem with_bot.add_eq_zero_iff {n : with_bot ℕ} {m : with_bot ℕ} : n + m = 0 ↔ n = 0 ∧ m = 0 := sorry theorem with_bot.add_eq_one_iff {n : with_bot ℕ} {m : with_bot ℕ} : n + m = 1 ↔ n = 0 ∧ m = 1 ∨ n = 1 ∧ m = 0 := sorry @[simp] theorem with_bot.coe_nonneg {n : ℕ} : 0 ≤ ↑n := eq.mpr (id (Eq._oldrec (Eq.refl (0 ≤ ↑n)) (Eq.symm with_bot.coe_zero))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑0 ≤ ↑n)) (propext with_bot.coe_le_coe))) (zero_le n)) @[simp] theorem with_bot.lt_zero_iff (n : with_bot ℕ) : n < 0 ↔ n = ⊥ := sorry end Mathlib
f053d2813218ef8d89ce7fdd660b774f3aea00e5
c31182a012eec69da0a1f6c05f42b0f0717d212d
/src/for_mathlib/nnreal.lean
1f3a9f2593884c50e0fb0e33424ca8806063d611
[]
no_license
Ja1941/lean-liquid
fbec3ffc7fc67df1b5ca95b7ee225685ab9ffbdc
8e80ed0cbdf5145d6814e833a674eaf05a1495c1
refs/heads/master
1,689,437,983,362
1,628,362,719,000
1,628,362,719,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
541
lean
import data.real.nnreal open_locale nnreal -- There doesn't seem to be a real analogue of this one, but probably should be? lemma nnreal.div_le_div_left_of {a b c : ℝ≥0} (w : 0 < c) (h : c ≤ b) : a / b ≤ a / c := begin rcases a with ⟨a, a_pos⟩, rcases b with ⟨b, b_pos⟩, rcases c with ⟨c, c_pos⟩, change a / b ≤ a / c, change 0 < c at w, change c ≤ b at h, by_cases p : 0 < a, { rw div_le_div_left p (lt_of_lt_of_le w h) w, exact h, }, { have q : a = 0, linarith, subst q, simp, } end
9feab4ac10f5e53fc21442cb73235d24dacdba0e
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/tests/lean/run/tactic.lean
961110454407455a0f1e26acf4c05a4195f51dcc
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
464
lean
import Lean.Meta open Lean open Lean.Meta axiom simple : forall {p q : Prop}, p → q → p def print (msg : MessageData) : MetaM Unit := trace! `Meta.Tactic msg def tst1 : MetaM Unit := do let cinfo ← getConstInfo `simple let mvar ← mkFreshExprSyntheticOpaqueMVar cinfo.type let mvarId := mvar.mvarId! let (_, mvarId) ← introN mvarId 4 assumption mvarId let result ← instantiateMVars mvar print result set_option trace.Meta.Tactic true #eval tst1
d839a2fb088944b243c0e42d3e677b9627b3db85
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/ring_theory/witt_vector/frobenius_fraction_field.lean
d251d81d5ad0b5acdd414728c6724d2095719526
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
11,859
lean
/- Copyright (c) 2022 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis, Heather Macbeth -/ import data.nat.cast.with_top import field_theory.is_alg_closed.basic import ring_theory.witt_vector.discrete_valuation_ring /-! # Solving equations about the Frobenius map on the field of fractions of `𝕎 k` The goal of this file is to prove `witt_vector.exists_frobenius_solution_fraction_ring`, which says that for an algebraically closed field `k` of characteristic `p` and `a, b` in the field of fractions of Witt vectors over `k`, there is a solution `b` to the equation `φ b * a = p ^ m * b`, where `φ` is the Frobenius map. Most of this file builds up the equivalent theorem over `𝕎 k` directly, moving to the field of fractions at the end. See `witt_vector.frobenius_rotation` and its specification. The construction proceeds by recursively defining a sequence of coefficients as solutions to a polynomial equation in `k`. We must define these as generic polynomials using Witt vector API (`witt_vector.witt_mul`, `witt_polynomial`) to show that they satisfy the desired equation. Preliminary work is done in the dependency `ring_theory.witt_vector.mul_coeff` to isolate the `n+1`st coefficients of `x` and `y` in the `n+1`st coefficient of `x*y`. This construction is described in Dupuis, Lewis, and Macbeth, [Formalized functional analysis via semilinear maps][dupuis-lewis-macbeth2022]. We approximately follow an approach sketched on MathOverflow: <https://mathoverflow.net/questions/62468/about-frobenius-of-witt-vectors> The result is a dependency for the proof of `witt_vector.isocrystal_classification`, the classification of one-dimensional isocrystals over an algebraically closed field. -/ noncomputable theory namespace witt_vector variables (p : ℕ) [hp : fact p.prime] local notation `𝕎` := witt_vector p namespace recursion_main /-! ## The recursive case of the vector coefficients The first coefficient of our solution vector is easy to define below. In this section we focus on the recursive case. The goal is to turn `witt_poly_prod n` into a univariate polynomial whose variable represents the `n`th coefficient of `x` in `x * a`. -/ section comm_ring include hp variables {k : Type*} [comm_ring k] [char_p k p] open polynomial /-- The root of this polynomial determines the `n+1`st coefficient of our solution. -/ def succ_nth_defining_poly (n : ℕ) (a₁ a₂ : 𝕎 k) (bs : fin (n+1) → k) : polynomial k := X^p * C (a₁.coeff 0 ^ (p^(n+1))) - X * C (a₂.coeff 0 ^ (p^(n+1))) + C (a₁.coeff (n+1) * ((bs 0)^p)^(p^(n+1)) + nth_remainder p n (λ v, (bs v)^p) (truncate_fun (n+1) a₁) - a₂.coeff (n+1) * (bs 0)^p^(n+1) - nth_remainder p n bs (truncate_fun (n+1) a₂)) lemma succ_nth_defining_poly_degree [is_domain k] (n : ℕ) (a₁ a₂ : 𝕎 k) (bs : fin (n+1) → k) (ha₁ : a₁.coeff 0 ≠ 0) (ha₂ : a₂.coeff 0 ≠ 0) : (succ_nth_defining_poly p n a₁ a₂ bs).degree = p := begin have : (X ^ p * C (a₁.coeff 0 ^ p ^ (n+1))).degree = p, { rw [degree_mul, degree_C], { simp only [nat.cast_with_bot, add_zero, degree_X, degree_pow, nat.smul_one_eq_coe] }, { exact pow_ne_zero _ ha₁ } }, have : (X ^ p * C (a₁.coeff 0 ^ p ^ (n+1)) - X * C (a₂.coeff 0 ^ p ^ (n+1))).degree = p, { rw [degree_sub_eq_left_of_degree_lt, this], rw [this, degree_mul, degree_C, degree_X, add_zero], { exact_mod_cast hp.out.one_lt }, { exact pow_ne_zero _ ha₂ } }, rw [succ_nth_defining_poly, degree_add_eq_left_of_degree_lt, this], apply lt_of_le_of_lt (degree_C_le), rw [this], exact_mod_cast hp.out.pos end end comm_ring section is_alg_closed include hp variables {k : Type*} [field k] [char_p k p] [is_alg_closed k] lemma root_exists (n : ℕ) (a₁ a₂ : 𝕎 k) (bs : fin (n+1) → k) (ha₁ : a₁.coeff 0 ≠ 0) (ha₂ : a₂.coeff 0 ≠ 0) : ∃ b : k, (succ_nth_defining_poly p n a₁ a₂ bs).is_root b := is_alg_closed.exists_root _ $ by simp only [(succ_nth_defining_poly_degree p n a₁ a₂ bs ha₁ ha₂), hp.out.ne_zero, with_top.coe_eq_zero, ne.def, not_false_iff] /-- This is the `n+1`st coefficient of our solution, projected from `root_exists`. -/ def succ_nth_val (n : ℕ) (a₁ a₂ : 𝕎 k) (bs : fin (n+1) → k) (ha₁ : a₁.coeff 0 ≠ 0) (ha₂ : a₂.coeff 0 ≠ 0) : k := classical.some (root_exists p n a₁ a₂ bs ha₁ ha₂) lemma succ_nth_val_spec (n : ℕ) (a₁ a₂ : 𝕎 k) (bs : fin (n+1) → k) (ha₁ : a₁.coeff 0 ≠ 0) (ha₂ : a₂.coeff 0 ≠ 0) : (succ_nth_defining_poly p n a₁ a₂ bs).is_root (succ_nth_val p n a₁ a₂ bs ha₁ ha₂) := classical.some_spec (root_exists p n a₁ a₂ bs ha₁ ha₂) lemma succ_nth_val_spec' (n : ℕ) (a₁ a₂ : 𝕎 k) (bs : fin (n+1) → k) (ha₁ : a₁.coeff 0 ≠ 0) (ha₂ : a₂.coeff 0 ≠ 0) : (succ_nth_val p n a₁ a₂ bs ha₁ ha₂)^p * a₁.coeff 0 ^ (p^(n+1)) + a₁.coeff (n+1) * ((bs 0)^p)^(p^(n+1)) + nth_remainder p n (λ v, (bs v)^p) (truncate_fun (n+1) a₁) = (succ_nth_val p n a₁ a₂ bs ha₁ ha₂) * a₂.coeff 0 ^ (p^(n+1)) + a₂.coeff (n+1) * (bs 0)^(p^(n+1)) + nth_remainder p n bs (truncate_fun (n+1) a₂) := begin rw ← sub_eq_zero, have := succ_nth_val_spec p n a₁ a₂ bs ha₁ ha₂, simp only [polynomial.map_add, polynomial.eval_X, polynomial.map_pow, polynomial.eval_C, polynomial.eval_pow, succ_nth_defining_poly, polynomial.eval_mul, polynomial.eval_add, polynomial.eval_sub, polynomial.map_mul, polynomial.map_sub, polynomial.is_root.def] at this, convert this using 1, ring end end is_alg_closed end recursion_main namespace recursion_base include hp variables {k : Type*} [field k] [is_alg_closed k] lemma solution_pow (a₁ a₂ : 𝕎 k) : ∃ x : k, x^(p-1) = a₂.coeff 0 / a₁.coeff 0 := is_alg_closed.exists_pow_nat_eq _ $ by linarith [hp.out.one_lt, le_of_lt hp.out.one_lt] /-- The base case (0th coefficient) of our solution vector. -/ def solution (a₁ a₂ : 𝕎 k) : k := classical.some $ solution_pow p a₁ a₂ lemma solution_spec (a₁ a₂ : 𝕎 k) : (solution p a₁ a₂)^(p-1) = a₂.coeff 0 / a₁.coeff 0 := classical.some_spec $ solution_pow p a₁ a₂ lemma solution_nonzero {a₁ a₂ : 𝕎 k} (ha₁ : a₁.coeff 0 ≠ 0) (ha₂ : a₂.coeff 0 ≠ 0) : solution p a₁ a₂ ≠ 0 := begin intro h, have := solution_spec p a₁ a₂, rw [h, zero_pow] at this, { simpa [ha₁, ha₂] using _root_.div_eq_zero_iff.mp this.symm }, { linarith [hp.out.one_lt, le_of_lt hp.out.one_lt] } end lemma solution_spec' {a₁ : 𝕎 k} (ha₁ : a₁.coeff 0 ≠ 0) (a₂ : 𝕎 k) : (solution p a₁ a₂)^p * a₁.coeff 0 = (solution p a₁ a₂) * a₂.coeff 0 := begin have := solution_spec p a₁ a₂, cases nat.exists_eq_succ_of_ne_zero hp.out.ne_zero with q hq, have hq' : q = p - 1 := by simp only [hq, tsub_zero, nat.succ_sub_succ_eq_sub], conv_lhs {congr, congr, skip, rw hq}, rw [pow_succ', hq', this], field_simp [ha₁, mul_comm], end end recursion_base open recursion_main recursion_base section frobenius_rotation section is_alg_closed include hp variables {k : Type*} [field k] [char_p k p] [is_alg_closed k] /-- Recursively defines the sequence of coefficients for `witt_vector.frobenius_rotation`. -/ noncomputable def frobenius_rotation_coeff {a₁ a₂ : 𝕎 k} (ha₁ : a₁.coeff 0 ≠ 0) (ha₂ : a₂.coeff 0 ≠ 0) : ℕ → k | 0 := solution p a₁ a₂ | (n + 1) := succ_nth_val p n a₁ a₂ (λ i, frobenius_rotation_coeff i.val) ha₁ ha₂ using_well_founded { dec_tac := `[apply fin.is_lt] } /-- For nonzero `a₁` and `a₂`, `frobenius_rotation a₁ a₂` is a Witt vector that satisfies the equation `frobenius (frobenius_rotation a₁ a₂) * a₁ = (frobenius_rotation a₁ a₂) * a₂`. -/ def frobenius_rotation {a₁ a₂ : 𝕎 k} (ha₁ : a₁.coeff 0 ≠ 0) (ha₂ : a₂.coeff 0 ≠ 0) : 𝕎 k := witt_vector.mk p (frobenius_rotation_coeff p ha₁ ha₂) lemma frobenius_rotation_nonzero {a₁ a₂ : 𝕎 k} (ha₁ : a₁.coeff 0 ≠ 0) (ha₂ : a₂.coeff 0 ≠ 0) : frobenius_rotation p ha₁ ha₂ ≠ 0 := begin intro h, apply solution_nonzero p ha₁ ha₂, simpa [← h, frobenius_rotation, frobenius_rotation_coeff] using witt_vector.zero_coeff p k 0 end lemma frobenius_frobenius_rotation {a₁ a₂ : 𝕎 k} (ha₁ : a₁.coeff 0 ≠ 0) (ha₂ : a₂.coeff 0 ≠ 0) : frobenius (frobenius_rotation p ha₁ ha₂) * a₁ = (frobenius_rotation p ha₁ ha₂) * a₂ := begin ext n, induction n with n ih, { simp only [witt_vector.mul_coeff_zero, witt_vector.coeff_frobenius_char_p, frobenius_rotation, frobenius_rotation_coeff], apply solution_spec' _ ha₁ }, { simp only [nth_remainder_spec, witt_vector.coeff_frobenius_char_p, frobenius_rotation_coeff, frobenius_rotation, fin.val_eq_coe], have := succ_nth_val_spec' p n a₁ a₂ (λ (i : fin (n + 1)), frobenius_rotation_coeff p ha₁ ha₂ i.val) ha₁ ha₂, simp only [frobenius_rotation_coeff, fin.val_eq_coe, fin.val_zero] at this, convert this using 4, apply truncated_witt_vector.ext, intro i, simp only [fin.val_eq_coe, witt_vector.coeff_truncate_fun, witt_vector.coeff_frobenius_char_p], refl } end local notation `φ` := is_fraction_ring.field_equiv_of_ring_equiv (frobenius_equiv p k) lemma exists_frobenius_solution_fraction_ring_aux (m n : ℕ) (r' q' : 𝕎 k) (hr' : r'.coeff 0 ≠ 0) (hq' : q'.coeff 0 ≠ 0) (hq : ↑p ^ n * q' ∈ non_zero_divisors (𝕎 k)) : let b : 𝕎 k := frobenius_rotation p hr' hq' in is_fraction_ring.field_equiv_of_ring_equiv (frobenius_equiv p k) (algebra_map (𝕎 k) (fraction_ring (𝕎 k)) b) * localization.mk (↑p ^ m * r') ⟨↑p ^ n * q', hq⟩ = ↑p ^ (m - n : ℤ) * algebra_map (𝕎 k) (fraction_ring (𝕎 k)) b := begin intros b, have key : witt_vector.frobenius b * p ^ m * r' * p ^ n = p ^ m * b * (p ^ n * q'), { have H := congr_arg (λ x : 𝕎 k, x * p ^ m * p ^ n) (frobenius_frobenius_rotation p hr' hq'), dsimp at H, refine (eq.trans _ H).trans _; ring }, have hq'' : algebra_map (𝕎 k) (fraction_ring (𝕎 k)) q' ≠ 0, { have hq''' : q' ≠ 0 := λ h, hq' (by simp [h]), simpa only [ne.def, map_zero] using (is_fraction_ring.injective (𝕎 k) (fraction_ring (𝕎 k))).ne hq''' }, rw zpow_sub₀ (fraction_ring.p_nonzero p k), field_simp [fraction_ring.p_nonzero p k], simp only [is_fraction_ring.field_equiv_of_ring_equiv, is_localization.ring_equiv_of_ring_equiv_eq, ring_equiv.coe_of_bijective], convert congr_arg (λ x, algebra_map (𝕎 k) (fraction_ring (𝕎 k)) x) key using 1, { simp only [ring_hom.map_mul, ring_hom.map_pow, map_nat_cast, frobenius_equiv_apply], ring }, { simp only [ring_hom.map_mul, ring_hom.map_pow, map_nat_cast] } end lemma exists_frobenius_solution_fraction_ring {a : fraction_ring (𝕎 k)} (ha : a ≠ 0) : ∃ (b : fraction_ring (𝕎 k)) (hb : b ≠ 0) (m : ℤ), φ b * a = p ^ m * b := begin revert ha, refine localization.induction_on a _, rintros ⟨r, q, hq⟩ hrq, have hq0 : q ≠ 0 := mem_non_zero_divisors_iff_ne_zero.1 hq, have hr0 : r ≠ 0 := λ h, hrq (by simp [h]), obtain ⟨m, r', hr', rfl⟩ := exists_eq_pow_p_mul r hr0, obtain ⟨n, q', hq', rfl⟩ := exists_eq_pow_p_mul q hq0, let b := frobenius_rotation p hr' hq', refine ⟨algebra_map (𝕎 k) _ b, _, m - n, _⟩, { simpa only [map_zero] using (is_fraction_ring.injective (witt_vector p k) (fraction_ring (witt_vector p k))).ne (frobenius_rotation_nonzero p hr' hq')}, exact exists_frobenius_solution_fraction_ring_aux p m n r' q' hr' hq' hq, end end is_alg_closed end frobenius_rotation end witt_vector
3529d388d1e49a492b43abf3140d14f3aa3e4e35
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/src/Lean/Elab/Attributes.lean
79b928e7b773f22f132169b6d28cb3009af21e16
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,874
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Parser.Attr import Lean.Attributes import Lean.MonadEnv import Lean.Elab.Util namespace Lean.Elab structure Attribute where kind : AttributeKind := AttributeKind.global name : Name stx : Syntax := Syntax.missing instance : ToFormat Attribute where format attr := let kindStr := match attr.kind with | AttributeKind.global => "" | AttributeKind.local => "local " | AttributeKind.scoped => "scoped " Format.bracket "@[" f!"{kindStr}{attr.name}{toString attr.stx}" "]" instance : Inhabited Attribute where default := { name := arbitrary } /- ``` attrKind := parser! optional («scoped» <|> «local») ``` -/ def toAttributeKind [Monad m] [MonadResolveName m] [MonadError m] (attrKindStx : Syntax) : m AttributeKind := do if attrKindStx[0].isNone then return AttributeKind.global else if attrKindStx[0][0].getKind == `Lean.Parser.Term.scoped then if (← getCurrNamespace).isAnonymous then throwError! "scoped attributes must be used inside namespaces" return AttributeKind.scoped else return AttributeKind.local def mkAttrKindGlobal : Syntax := Syntax.node `Lean.Parser.Term.attrKind #[mkNullNode] def elabAttr {m} [Monad m] [MonadEnv m] [MonadResolveName m] [MonadError m] [MonadMacroAdapter m] [MonadRecDepth m] (attrInstance : Syntax) : m Attribute := do /- attrInstance := ppGroup $ parser! attrKind >> attrParser -/ let attrKind ← toAttributeKind attrInstance[0] let attr := attrInstance[1] let attr ← liftMacroM <| expandMacros attr let attrName ← if attr.getKind == ``Parser.Attr.simple then pure attr[0].getId.eraseMacroScopes else match attr.getKind with | Name.str _ s _ => pure <| Name.mkSimple s | _ => throwErrorAt attr "unknown attribute" unless isAttribute (← getEnv) attrName do throwError! "unknown attribute [{attrName}]" /- The `AttrM` does not have sufficient information for expanding macros in `args`. So, we expand them before here before we invoke the attributer handlers implemented using `AttrM`. -/ pure { kind := attrKind, name := attrName, stx := attr } -- sepBy1 attrInstance ", " def elabAttrs {m} [Monad m] [MonadEnv m] [MonadResolveName m] [MonadError m] [MonadMacroAdapter m] [MonadRecDepth m] (stx : Syntax) : m (Array Attribute) := do let mut attrs := #[] for attr in stx.getSepArgs do attrs := attrs.push (← elabAttr attr) return attrs -- parser! "@[" >> sepBy1 attrInstance ", " >> "]" def elabDeclAttrs {m} [Monad m] [MonadEnv m] [MonadResolveName m] [MonadError m] [MonadMacroAdapter m] [MonadRecDepth m] (stx : Syntax) : m (Array Attribute) := elabAttrs stx[1] end Lean.Elab
febe8d3d43d0cd82112b25915ac23335a111ca0b
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/tests/lean/t11.lean
a5af8c29792baa5ffda94bc506cde6ef7387a661
[ "Apache-2.0" ]
permissive
codyroux/lean
7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3
0cca265db19f7296531e339192e9b9bae4a31f8b
refs/heads/master
1,610,909,964,159
1,407,084,399,000
1,416,857,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
380
lean
constant A : Type.{1} definition bool : Type.{1} := Type.{0} constant Exists (P : A → bool) : bool notation `exists` binders `,` b:(scoped b, Exists b) := b notation `∃` binders `,` b:(scoped b, Exists b) := b constant p : A → bool constant q : A → A → bool check exists x : A, p x check ∃ x y : A, q x y notation `{` binder `|` b:scoped `}` := b check {x : A | x}
e17d7bd266739fe6a1d41eb8facc4142762faf73
63abd62053d479eae5abf4951554e1064a4c45b4
/src/category_theory/skeletal.lean
cd2822c6d7cff2b937f8d49425ec7eed6715d4ca
[ "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
7,738
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.isomorphism_classes import category_theory.thin /-! # Skeleton of a category Define skeletal categories as categories in which any two isomorphic objects are equal. Construct the skeleton of a thin category as a partial ordering, and (noncomputably) show it is a skeleton of the original category. The advantage of this special case being handled separately is that lemmas and definitions about orderings can be used directly, for example for the subobject lattice (TODO). In addition, some of the commutative diagrams about the functors commute definitionally on the nose which is convenient in practice. (TODO): Construct the skeleton of a general category using choice, and show it is a skeleton. -/ universes v₁ v₂ v₃ u₁ u₂ u₃ namespace category_theory open category variables (C : Type u₁) [category.{v₁} C] variables (D : Type u₂) [category.{v₂} D] variables {E : Type u₃} [category.{v₃} E] /-- A category is skeletal if isomorphic objects are equal. -/ def skeletal : Prop := ∀ ⦃X Y : C⦄, is_isomorphic X Y → X = Y /-- `is_skeleton_of C D F` says that `F : D ⥤ C` exhibits `D` as a skeletal full subcategory of `C`, in particular `F` is a (strong) equivalence and `D` is skeletal. -/ structure is_skeleton_of (F : D ⥤ C) := (skel : skeletal D) (eqv : is_equivalence F) local attribute [instance] is_isomorphic_setoid variables {C D} /-- If `C` is thin and skeletal, then any naturally isomorphic functors to `C` are equal. -/ lemma functor.eq_of_iso {F₁ F₂ : D ⥤ C} [∀ X Y : C, subsingleton (X ⟶ Y)] (hC : skeletal C) (hF : F₁ ≅ F₂) : F₁ = F₂ := functor.ext (λ X, hC ⟨hF.app X⟩) (λ _ _ _, subsingleton.elim _ _) /-- If `C` is thin and skeletal, `D ⥤ C` is skeletal. `category_theory.functor_thin` shows it is thin also. -/ lemma functor_skeletal [∀ X Y : C, subsingleton (X ⟶ Y)] (hC : skeletal C) : skeletal (D ⥤ C) := λ F₁ F₂ h, h.elim (functor.eq_of_iso hC) variables (C D) /-- Construct the skeleton category by taking the quotient of objects. This construction gives a preorder with nice definitional properties, but is only really appropriate for thin categories. -/ def thin_skeleton : Type u₁ := quotient (is_isomorphic_setoid C) instance inhabited_thin_skeleton [inhabited C] : inhabited (thin_skeleton C) := ⟨quotient.mk (default _)⟩ instance thin_skeleton.preorder : preorder (thin_skeleton C) := { le := quotient.lift₂ (λ X Y, nonempty (X ⟶ Y)) begin rintros _ _ _ _ ⟨i₁⟩ ⟨i₂⟩, exact propext ⟨nonempty.map (λ f, i₁.inv ≫ f ≫ i₂.hom), nonempty.map (λ f, i₁.hom ≫ f ≫ i₂.inv)⟩, end, le_refl := begin refine quotient.ind (λ a, _), exact ⟨𝟙 _⟩, end, le_trans := λ a b c, quotient.induction_on₃ a b c $ λ A B C, nonempty.map2 (≫) } /-- The functor from a category to its thin skeleton. -/ @[simps] def to_thin_skeleton : C ⥤ thin_skeleton C := { obj := quotient.mk, map := λ X Y f, hom_of_le (nonempty.intro f) } /-! The constructions here are intended to be used when the category `C` is thin, even though some of the statements can be shown without this assumption. -/ namespace thin_skeleton /-- The thin skeleton is thin. -/ instance thin {X Y : thin_skeleton C} : subsingleton (X ⟶ Y) := ⟨by { rintros ⟨⟨f₁⟩⟩ ⟨⟨f₂⟩⟩, refl }⟩ variables {C} {D} /-- A functor `C ⥤ D` computably lowers to a functor `thin_skeleton C ⥤ thin_skeleton D`. -/ @[simps] def map (F : C ⥤ D) : thin_skeleton C ⥤ thin_skeleton D := { obj := quotient.map F.obj $ λ X₁ X₂ ⟨hX⟩, ⟨F.map_iso hX⟩, map := λ X Y, quotient.rec_on_subsingleton₂ X Y $ λ x y k, hom_of_le ((le_of_hom k).elim (λ t, ⟨F.map t⟩)) } lemma comp_to_thin_skeleton (F : C ⥤ D) : F ⋙ to_thin_skeleton D = to_thin_skeleton C ⋙ map F := rfl /-- Given a natural transformation `F₁ ⟶ F₂`, induce a natural transformation `map F₁ ⟶ map F₂`.-/ def map_nat_trans {F₁ F₂ : C ⥤ D} (k : F₁ ⟶ F₂) : map F₁ ⟶ map F₂ := { app := λ X, quotient.rec_on_subsingleton X (λ x, ⟨⟨⟨k.app x⟩⟩⟩) } -- TODO: state the lemmas about what happens when you compose with `to_thin_skeleton` /-- A functor `C ⥤ D ⥤ E` computably lowers to a functor `thin_skeleton C ⥤ thin_skeleton D ⥤ thin_skeleton E` -/ @[simps] def map₂ (F : C ⥤ D ⥤ E) : thin_skeleton C ⥤ thin_skeleton D ⥤ thin_skeleton E := { obj := λ x, { obj := λ y, quotient.map₂ (λ X Y, (F.obj X).obj Y) (λ X₁ X₂ ⟨hX⟩ Y₁ Y₂ ⟨hY⟩, ⟨(F.obj X₁).map_iso hY ≪≫ (F.map_iso hX).app Y₂⟩) x y, map := λ y₁ y₂, quotient.rec_on_subsingleton x $ λ X, quotient.rec_on_subsingleton₂ y₁ y₂ $ λ Y₁ Y₂ hY, hom_of_le ((le_of_hom hY).elim (λ g, ⟨(F.obj X).map g⟩)) }, map := λ x₁ x₂, quotient.rec_on_subsingleton₂ x₁ x₂ $ λ X₁ X₂ f, { app := λ y, quotient.rec_on_subsingleton y (λ Y, hom_of_le ((le_of_hom f).elim (λ f', ⟨(F.map f').app Y⟩))) } } variables (C) [∀ X Y : C, subsingleton (X ⟶ Y)] instance to_thin_skeleton_faithful : faithful (to_thin_skeleton C) := {} /-- Use `quotient.out` to create a functor out of the thin skeleton. -/ @[simps] noncomputable def from_thin_skeleton : thin_skeleton C ⥤ C := { obj := quotient.out, map := λ x y, quotient.rec_on_subsingleton₂ x y $ λ X Y f, (nonempty.some (quotient.mk_out X)).hom ≫ (le_of_hom f).some ≫ (nonempty.some (quotient.mk_out Y)).inv } noncomputable instance from_thin_skeleton_equivalence : is_equivalence (from_thin_skeleton C) := { inverse := to_thin_skeleton C, counit_iso := nat_iso.of_components (λ X, (nonempty.some (quotient.mk_out X))) (by tidy), unit_iso := nat_iso.of_components (λ x, quotient.rec_on_subsingleton x (λ X, eq_to_iso (quotient.sound ⟨(nonempty.some (quotient.mk_out X)).symm⟩))) (by tidy) } variables {C} lemma equiv_of_both_ways {X Y : C} (f : X ⟶ Y) (g : Y ⟶ X) : X ≈ Y := ⟨iso_of_both_ways f g⟩ instance thin_skeleton_partial_order : partial_order (thin_skeleton C) := { le_antisymm := quotient.ind₂ begin rintros _ _ ⟨f⟩ ⟨g⟩, apply quotient.sound (equiv_of_both_ways f g), end, ..category_theory.thin_skeleton.preorder C } lemma skeletal : skeletal (thin_skeleton C) := λ X Y, quotient.induction_on₂ X Y $ λ x y h, h.elim $ λ i, le_antisymm (le_of_hom i.1) (le_of_hom i.2) lemma map_comp_eq (F : E ⥤ D) (G : D ⥤ C) : map (F ⋙ G) = map F ⋙ map G := functor.eq_of_iso skeletal $ nat_iso.of_components (λ X, quotient.rec_on_subsingleton X (λ x, iso.refl _)) (by tidy) lemma map_id_eq : map (𝟭 C) = 𝟭 (thin_skeleton C) := functor.eq_of_iso skeletal $ nat_iso.of_components (λ X, quotient.rec_on_subsingleton X (λ x, iso.refl _)) (by tidy) lemma map_iso_eq {F₁ F₂ : D ⥤ C} (h : F₁ ≅ F₂) : map F₁ = map F₂ := functor.eq_of_iso skeletal { hom := map_nat_trans h.hom, inv := map_nat_trans h.inv } /-- `from_thin_skeleton C` exhibits the thin skeleton as a skeleton. -/ noncomputable def thin_skeleton_is_skeleton : is_skeleton_of C (thin_skeleton C) (from_thin_skeleton C) := { skel := skeletal, eqv := thin_skeleton.from_thin_skeleton_equivalence C } noncomputable instance is_skeleton_of_inhabited : inhabited (is_skeleton_of C (thin_skeleton C) (from_thin_skeleton C)) := ⟨thin_skeleton_is_skeleton⟩ end thin_skeleton end category_theory
54d63e7d3f550784c81e65d0a143037a890f3e9a
c777c32c8e484e195053731103c5e52af26a25d1
/src/analysis/special_functions/trigonometric/euler_sine_prod.lean
7fd83358cee8922b13fc4ffafe7bf74172dbad9d
[ "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
15,937
lean
/- Copyright (c) 2023 David Loeffler. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Loeffler -/ import analysis.special_functions.integrals import measure_theory.integral.peak_function /-! # Euler's infinite product for the sine function This file proves the infinite product formula $$ \sin \pi z = \pi z \prod_{n = 1}^\infty \left(1 - \frac{z ^ 2}{n ^ 2}\right) $$ for any real or complex `z`. Our proof closely follows the article [Salwinski, *Euler's Sine Product Formula: An Elementary Proof*][salwinski2018]: the basic strategy is to prove a recurrence relation for the integrals `∫ x in 0..π/2, cos 2 z x * cos x ^ (2 * n)`, generalising the arguments used to prove Wallis' limit formula for `π`. -/ open_locale real topology big_operators open real set filter interval_integral measure_theory.measure_space namespace euler_sine section integral_recursion /-! ## Recursion formula for the integral of `cos (2 * z * x) * cos x ^ n` We evaluate the integral of `cos (2 * z * x) * cos x ^ n`, for any complex `z` and even integers `n`, via repeated integration by parts. -/ variables {z : ℂ} {n : ℕ} lemma antideriv_cos_comp_const_mul (hz : z ≠ 0) (x : ℝ) : has_deriv_at (λ y:ℝ, complex.sin (2 * z * y) / (2 * z)) (complex.cos (2 * z * x)) x := begin have a : has_deriv_at _ _ ↑x := has_deriv_at_mul_const _, have b : has_deriv_at (λ (y : ℂ), complex.sin (y * (2 * z))) _ ↑x := has_deriv_at.comp x (complex.has_deriv_at_sin (x * (2 * z))) a, convert (b.comp_of_real).div_const (2 * z), { ext1 x, rw mul_comm _ (2 * z) }, { field_simp, rw mul_comm _ (2 * z) }, end lemma antideriv_sin_comp_const_mul (hz : z ≠ 0) (x : ℝ) : has_deriv_at (λ y:ℝ, -complex.cos (2 * z * y) / (2 * z)) (complex.sin (2 * z * x)) x := begin have a : has_deriv_at _ _ ↑x := has_deriv_at_mul_const _, have b : has_deriv_at (λ (y : ℂ), complex.cos (y * (2 * z))) _ ↑x := has_deriv_at.comp x (complex.has_deriv_at_cos (x * (2 * z))) a, convert ((b.comp_of_real).div_const (2 * z)).neg, { ext1 x, rw mul_comm _ (2 * z), field_simp }, { field_simp, rw mul_comm _ (2 * z) }, end lemma integral_cos_mul_cos_pow_aux (hn : 2 ≤ n) (hz : z ≠ 0): (∫ x:ℝ in 0..π/2, complex.cos (2 * z * x) * cos x ^ n) = n / (2 * z) * ∫ x:ℝ in 0..π/2, complex.sin (2 * z * x) * sin x * cos x ^ (n - 1) := begin have der1 : ∀ (x : ℝ), (x ∈ uIcc 0 (π/2)) → has_deriv_at (λ y, (↑(cos y)) ^ n : ℝ → ℂ) (-n * sin x * cos x ^ (n - 1)) x, { intros x hx, have b : has_deriv_at (λ y, ↑(cos y) : ℝ → ℂ) (-sin x) x, by simpa using (has_deriv_at_cos x).of_real_comp, convert has_deriv_at.comp x (has_deriv_at_pow _ _) b using 1, ring, }, convert integral_mul_deriv_eq_deriv_mul der1 (λ x hx, antideriv_cos_comp_const_mul hz x) _ _, { ext1 x, rw mul_comm }, { rw [complex.of_real_zero, mul_zero, complex.sin_zero, zero_div, mul_zero, sub_zero, cos_pi_div_two, complex.of_real_zero, zero_pow (by positivity : 0 < n), zero_mul, zero_sub, ←integral_neg, ←integral_const_mul], refine integral_congr (λ x hx, _), field_simp, ring }, { apply continuous.interval_integrable, exact (continuous_const.mul (complex.continuous_of_real.comp continuous_sin)).mul ((complex.continuous_of_real.comp continuous_cos).pow (n - 1)) }, { apply continuous.interval_integrable, exact complex.continuous_cos.comp (continuous_const.mul complex.continuous_of_real) } end lemma integral_sin_mul_sin_mul_cos_pow_eq (hn : 2 ≤ n) (hz : z ≠ 0) : ∫ x:ℝ in 0..π/2, complex.sin (2 * z * x) * sin x * cos x ^ (n - 1) = n / (2 * z) * (∫ x:ℝ in 0..π/2, complex.cos (2 * z * x) * cos x ^ n) - (n - 1) / (2 * z) * (∫ x:ℝ in 0..π/2, complex.cos (2 * z * x) * cos x ^ (n - 2)) := begin have der1 : ∀ (x : ℝ), (x ∈ uIcc 0 (π/2)) → has_deriv_at (λ y, (sin y) * (cos y) ^ (n - 1) : ℝ → ℂ) (cos x ^ n - (n - 1) * sin x ^ 2 * cos x ^ (n - 2)) x, { intros x hx, have c := has_deriv_at.comp (x:ℂ) (has_deriv_at_pow (n - 1) _) (complex.has_deriv_at_cos x), convert ((complex.has_deriv_at_sin x).mul c).comp_of_real using 1, { ext1 y, simp only [complex.of_real_sin, complex.of_real_cos] }, { simp only [complex.of_real_cos, complex.of_real_sin], rw [mul_neg, mul_neg, ←sub_eq_add_neg, function.comp_app], congr' 1, { rw [←pow_succ, nat.sub_add_cancel (by linarith : 1 ≤ n)] }, { have : ((n - 1 : ℕ) : ℂ) = (n:ℂ) - 1, { rw [nat.cast_sub (one_le_two.trans hn), nat.cast_one] }, rw [nat.sub_sub, this], ring } } }, convert integral_mul_deriv_eq_deriv_mul der1 (λ x hx, antideriv_sin_comp_const_mul hz x) _ _ using 1, { refine integral_congr (λ x hx, _), ring_nf }, { -- now a tedious rearrangement of terms -- gather into a single integral, and deal with continuity subgoals: rw [sin_zero, cos_pi_div_two, complex.of_real_zero, zero_pow, zero_mul, mul_zero, zero_mul, zero_mul, sub_zero, zero_sub, ←integral_neg, ←integral_const_mul, ←integral_const_mul, ←integral_sub], rotate, { apply continuous.interval_integrable, exact continuous_const.mul ((complex.continuous_cos.comp (continuous_const.mul complex.continuous_of_real)).mul ((complex.continuous_of_real.comp continuous_cos).pow n)) }, { apply continuous.interval_integrable, exact continuous_const.mul ((complex.continuous_cos.comp (continuous_const.mul complex.continuous_of_real)).mul ((complex.continuous_of_real.comp continuous_cos).pow (n - 2))), }, { apply nat.sub_pos_of_lt, exact one_lt_two.trans_le hn }, refine integral_congr (λ x hx, _), dsimp only, -- get rid of real trig functions and divions by 2 * z: rw [complex.of_real_cos, complex.of_real_sin, complex.sin_sq, ←mul_div_right_comm, ←mul_div_right_comm, ←sub_div, mul_div, ←neg_div], congr' 1, have : complex.cos ↑x ^ n = complex.cos ↑x ^ (n - 2) * complex.cos ↑x ^ 2, { conv_lhs { rw [←nat.sub_add_cancel hn, pow_add] } }, rw this, ring }, { apply continuous.interval_integrable, exact ((complex.continuous_of_real.comp continuous_cos).pow n).sub ((continuous_const.mul ((complex.continuous_of_real.comp continuous_sin).pow 2)).mul ((complex.continuous_of_real.comp continuous_cos).pow (n - 2))) }, { apply continuous.interval_integrable, exact complex.continuous_sin.comp (continuous_const.mul complex.continuous_of_real) }, end /-- Note this also holds for `z = 0`, but we do not need this case for `sin_pi_mul_eq`. -/ lemma integral_cos_mul_cos_pow (hn : 2 ≤ n) (hz : z ≠ 0) : (1 - 4 * z ^ 2 / n ^ 2) * (∫ x:ℝ in 0..π/2, complex.cos (2 * z * x) * cos x ^ n) = (n - 1 : ℂ) / n * ∫ x:ℝ in 0..π/2, complex.cos (2 * z * x) * cos x ^ (n - 2) := begin have nne : (n : ℂ) ≠ 0, { contrapose! hn, rw nat.cast_eq_zero at hn, rw hn, exact zero_lt_two }, have := integral_cos_mul_cos_pow_aux hn hz, rw [integral_sin_mul_sin_mul_cos_pow_eq hn hz, sub_eq_neg_add, mul_add, ←sub_eq_iff_eq_add] at this, convert congr_arg (λ u:ℂ, -u * (2 * z) ^ 2 / n ^ 2) this using 1; { field_simp, ring }, end /-- Note this also holds for `z = 0`, but we do not need this case for `sin_pi_mul_eq`. -/ lemma integral_cos_mul_cos_pow_even (n : ℕ) (hz : z ≠ 0) : (1 - z ^ 2 / (n + 1) ^ 2) * (∫ x:ℝ in 0..π/2, complex.cos (2 * z * x) * cos x ^ (2 * n + 2)) = (2 * n + 1 : ℂ) / (2 * n + 2) * ∫ x:ℝ in 0..π/2, complex.cos (2 * z * x) * cos x ^ (2 * n) := begin convert integral_cos_mul_cos_pow (by linarith : 2 ≤ 2 * n + 2) hz using 3, { simp only [nat.cast_add, nat.cast_mul, nat.cast_two], nth_rewrite_rhs 2 ←mul_one (2:ℂ), rw [←mul_add, mul_pow, ←div_div], ring }, { push_cast, ring }, { push_cast, ring }, end /-- Relate the integral `cos x ^ n` over `[0, π/2]` to the integral of `sin x ^ n` over `[0, π]`, which is studied in `data.real.pi.wallis` and other places. -/ lemma integral_cos_pow_eq (n : ℕ) : (∫ (x:ℝ) in 0..π/2, cos x ^ n) = 1 / 2 * (∫ (x:ℝ) in 0..π, (sin x) ^ n) := begin rw [mul_comm (1/2 : ℝ), ←div_eq_iff (one_div_ne_zero (two_ne_zero' ℝ)), ←div_mul, div_one, mul_two], have L : interval_integrable _ volume 0 (π / 2) := (continuous_sin.pow n).interval_integrable _ _, have R : interval_integrable _ volume (π / 2) π := (continuous_sin.pow n).interval_integrable _ _, rw ←integral_add_adjacent_intervals L R, congr' 1, { nth_rewrite 0 (by ring : 0 = π/2 - π/2), nth_rewrite 2 (by ring : π/2 = π/2 - 0), rw ←integral_comp_sub_left, refine integral_congr (λ x _, _), dsimp only, rw cos_pi_div_two_sub }, { nth_rewrite 2 (by ring : π = π/2 + π/2), nth_rewrite 1 (by ring : π/2 = 0 + π/2), rw ←integral_comp_add_right, refine integral_congr (λ x _, _), dsimp only, rw sin_add_pi_div_two }, end lemma integral_cos_pow_pos (n : ℕ) : 0 < (∫ (x:ℝ) in 0..π/2, cos x ^ n) := (integral_cos_pow_eq n).symm ▸ (mul_pos one_half_pos (integral_sin_pow_pos _)) /-- Finite form of Euler's sine product, with remainder term expressed as a ratio of cosine integrals. -/ lemma sin_pi_mul_eq (z : ℂ) (n : ℕ) : complex.sin (π * z) = π * z * (∏ j in finset.range n, (1 - z ^ 2 / (j + 1) ^ 2)) * (∫ x in 0..π/2, complex.cos (2 * z * x) * cos x ^ (2 * n)) / ↑∫ x in 0..π/2, cos x ^ (2 * n) := begin rcases eq_or_ne z 0 with rfl | hz, { simp }, induction n with n hn, { simp_rw [mul_zero, pow_zero, mul_one, finset.prod_range_zero, mul_one, integral_one, sub_zero], rw [integral_cos_mul_complex (mul_ne_zero two_ne_zero hz), complex.of_real_zero, mul_zero, complex.sin_zero, zero_div, sub_zero, (by { push_cast, field_simp, ring } : 2 * z * ↑(π / 2) = π * z)], field_simp [complex.of_real_ne_zero.mpr pi_pos.ne'], ring }, { rw [hn, finset.prod_range_succ], set A := ∏ j in finset.range n, (1 - z ^ 2 / (j + 1) ^ 2), set B := ∫ x:ℝ in 0..π/2, complex.cos (2 * z * x) * cos x ^ (2 * n), set C := ∫ x:ℝ in 0..π/2, cos x ^ (2 * n), have aux' : 2 * n.succ = 2 * n + 2, { rw [nat.succ_eq_add_one, mul_add, mul_one], }, have : ∫ x:ℝ in 0..π/2, cos x ^ (2 * n.succ) = (2 * (n:ℝ) + 1) / (2 * n + 2) * C, { rw integral_cos_pow_eq, dsimp only [C], rw [integral_cos_pow_eq, aux', integral_sin_pow, sin_zero, sin_pi, pow_succ, zero_mul, zero_mul, zero_mul, sub_zero, zero_div, zero_add, ←mul_assoc, ←mul_assoc, mul_comm (1 / 2 : ℝ) _, nat.cast_mul, nat.cast_bit0, nat.cast_one] }, rw this, change ↑π * z * A * B / ↑C = (↑π * z * (A * (1 - z ^ 2 / (↑n + 1) ^ 2)) * ∫ (x : ℝ) in 0..π / 2, complex.cos (2 * z * ↑x) * ↑(cos x) ^ (2 * n.succ)) / ↑((2 * ↑n + 1) / (2 * ↑n + 2) * C), have : ↑π * z * (A * (1 - z ^ 2 / (↑n + 1) ^ 2)) * ∫ (x : ℝ) in 0..π / 2, complex.cos (2 * z * ↑x) * ↑(cos x) ^ (2 * n.succ) = ↑π * z * A * ((1 - z ^ 2 / (↑n.succ) ^ 2) * ∫ (x : ℝ) in 0..π / 2, complex.cos (2 * z * ↑x) * ↑(cos x) ^ (2 * n.succ)), { nth_rewrite_rhs 0 nat.succ_eq_add_one, rw nat.cast_add_one, ring }, rw this, suffices : (1 - z ^ 2 / ↑(n.succ) ^ 2) * ∫ (x : ℝ) in 0..π / 2, complex.cos (2 * z * ↑x) * ↑(cos x) ^ (2 * n.succ) = (2 * n + 1) / (2 * n + 2) * B, { rw [this, complex.of_real_mul, complex.of_real_div], have : (C:ℂ) ≠ 0 := complex.of_real_ne_zero.mpr (integral_cos_pow_pos _).ne', have : 2 * (n:ℂ) + 1 ≠ 0, { convert (nat.cast_add_one_ne_zero (2 * n) : (↑(2 * n) + 1 : ℂ) ≠ 0), simp }, have : 2 * (n:ℂ) + 2 ≠ 0, { convert (nat.cast_add_one_ne_zero (2 * n + 1) : (↑(2 * n + 1) + 1 : ℂ) ≠ 0) using 1, push_cast, ring }, field_simp, ring }, convert integral_cos_mul_cos_pow_even n hz, rw nat.cast_succ } end end integral_recursion /-! ## Conclusion of the proof The main theorem `complex.tendsto_euler_sin_prod`, and its real variant `real.tendsto_euler_sin_prod`, now follow by combining `sin_pi_mul_eq` with a lemma stating that the sequence of measures on `[0, π/2]` given by integration against `cos x ^ n` (suitably normalised) tends to the Dirac measure at 0, as a special case of the general result `tendsto_set_integral_pow_smul_of_unique_maximum_of_is_compact_of_continuous_on`. -/ lemma tendsto_integral_cos_pow_mul_div {f : ℝ → ℂ} (hf : continuous_on f (Icc 0 (π/2))) : tendsto (λ (n : ℕ), (∫ x:ℝ in 0..π/2, ↑(cos x) ^ n * f x) / ↑(∫ x:ℝ in 0..π/2, (cos x) ^ n)) at_top (𝓝 $ f 0) := begin simp_rw [div_eq_inv_mul _ (coe _), ←complex.of_real_inv, integral_of_le (pi_div_two_pos.le), ←measure_theory.integral_Icc_eq_integral_Ioc, ←complex.of_real_pow, ←complex.real_smul], have c_lt : ∀ (y : ℝ), y ∈ Icc 0 (π / 2) → y ≠ 0 → cos y < cos 0, from λ y hy hy', cos_lt_cos_of_nonneg_of_le_pi_div_two (le_refl 0) hy.2 (lt_of_le_of_ne hy.1 hy'.symm), have c_nonneg : ∀ (x : ℝ), x ∈ Icc 0 (π / 2) → 0 ≤ cos x, from λ x hx, cos_nonneg_of_mem_Icc ((Icc_subset_Icc_left (neg_nonpos_of_nonneg pi_div_two_pos.le)) hx), have c_zero_pos : 0 < cos 0, by { rw cos_zero, exact zero_lt_one }, have zero_mem : (0:ℝ) ∈ closure (interior (Icc 0 (π / 2))), { rw [interior_Icc, closure_Ioo pi_div_two_pos.ne, left_mem_Icc], exact pi_div_two_pos.le }, exact tendsto_set_integral_pow_smul_of_unique_maximum_of_is_compact_of_continuous_on is_compact_Icc continuous_on_cos c_lt c_nonneg c_zero_pos zero_mem hf end /-- Euler's infinite product formula for the complex sine function. -/ lemma _root_.complex.tendsto_euler_sin_prod (z : ℂ) : tendsto (λ n:ℕ, ↑π * z * (∏ j in finset.range n, (1 - z ^ 2 / (j + 1) ^ 2))) at_top (𝓝 $ complex.sin (π * z)) := begin have A : tendsto (λ n:ℕ, ↑π * z * (∏ j in finset.range n, (1 - z ^ 2 / (j + 1) ^ 2)) * (∫ x in 0..π / 2, complex.cos (2 * z * x) * cos x ^ (2 * n)) / ↑∫ x in 0..π / 2, cos x ^ (2 * n)) at_top (𝓝 $ _) := tendsto.congr (λ n, (sin_pi_mul_eq z n)) tendsto_const_nhds, have : 𝓝 (complex.sin (π * z)) = 𝓝 (complex.sin (π * z) * 1) := by rw mul_one, simp_rw [this, mul_div_assoc] at A, convert (tendsto_mul_iff_of_ne_zero _ one_ne_zero).mp A, suffices : tendsto (λ n:ℕ, (∫ x:ℝ in 0..π/2, complex.cos (2 * z * x) * cos x ^ n) / ↑(∫ x:ℝ in 0..π/2, cos x ^ n)) at_top (𝓝 1), from this.comp (tendsto_id.const_mul_at_top' zero_lt_two), have : continuous_on (λ x:ℝ, complex.cos (2 * z * x)) (Icc 0 (π/2)), from (complex.continuous_cos.comp (continuous_const.mul complex.continuous_of_real)).continuous_on, convert tendsto_integral_cos_pow_mul_div this, { ext1 n, congr' 2 with x:1, rw mul_comm }, { rw [complex.of_real_zero, mul_zero, complex.cos_zero] }, end /-- Euler's infinite product formula for the real sine function. -/ lemma _root_.real.tendsto_euler_sin_prod (x : ℝ) : tendsto (λ n:ℕ, π * x * (∏ j in finset.range n, (1 - x ^ 2 / (j + 1) ^ 2))) at_top (𝓝 $ sin (π * x)) := begin convert (complex.continuous_re.tendsto _).comp (complex.tendsto_euler_sin_prod x), { ext1 n, rw [function.comp_app, ←complex.of_real_mul, complex.of_real_mul_re], suffices : ∏ (j : ℕ) in finset.range n, (1 - (x:ℂ) ^ 2 / (↑j + 1) ^ 2) = ↑∏ (j : ℕ) in finset.range n, (1 - x ^ 2 / (↑j + 1) ^ 2), by rw [this, complex.of_real_re], rw complex.of_real_prod, refine finset.prod_congr (by refl) (λ n hn, _), norm_cast }, { rw [←complex.of_real_mul, ←complex.of_real_sin, complex.of_real_re] } end end euler_sine
e5f4755d35a1aedf6b510f77ccef4f10c38515da
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/model_theory/definability.lean
e689a61f60d3aa5c477c70e699f638e6712faaa7
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
11,314
lean
/- Copyright (c) 2021 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import data.set_like.basic import model_theory.semantics /-! # Definable Sets This file defines what it means for a set over a first-order structure to be definable. ## Main Definitions * `set.definable` is defined so that `A.definable L s` indicates that the set `s` of a finite cartesian power of `M` is definable with parameters in `A`. * `set.definable₁` is defined so that `A.definable₁ L s` indicates that `(s : set M)` is definable with parameters in `A`. * `set.definable₂` is defined so that `A.definable₂ L s` indicates that `(s : set (M × M))` is definable with parameters in `A`. * A `first_order.language.definable_set` is defined so that `L.definable_set A α` is the boolean algebra of subsets of `α → M` defined by formulas with parameters in `A`. ## Main Results * `L.definable_set A α` forms a `boolean_algebra` * `set.definable.image_comp` shows that definability is closed under projections in finite dimensions. -/ universes u v w namespace set variables {M : Type w} (A : set M) (L : first_order.language.{u v}) [L.Structure M] open_locale first_order open first_order.language first_order.language.Structure variables {α : Type*} {β : Type*} /-- A subset of a finite Cartesian product of a structure is definable over a set `A` when membership in the set is given by a first-order formula with parameters from `A`. -/ def definable (s : set (α → M)) : Prop := ∃ (φ : L[[A]].formula α), s = set_of φ.realize variables {L} {A} {B : set M} {s : set (α → M)} lemma definable.map_expansion {L' : first_order.language} [L'.Structure M] (h : A.definable L s) (φ : L →ᴸ L') [φ.is_expansion_on M] : A.definable L' s := begin obtain ⟨ψ, rfl⟩ := h, refine ⟨(φ.add_constants A).on_formula ψ, _⟩, ext x, simp only [mem_set_of_eq, Lhom.realize_on_formula], end lemma empty_definable_iff : (∅ : set M).definable L s ↔ ∃ (φ : L.formula α), s = set_of φ.realize := begin rw [definable, equiv.exists_congr_left (Lequiv.add_empty_constants L (∅ : set M)).on_formula], simp, end lemma definable_iff_empty_definable_with_params : A.definable L s ↔ (∅ : set M).definable (L[[A]]) s := empty_definable_iff.symm lemma definable.mono (hAs : A.definable L s) (hAB : A ⊆ B) : B.definable L s := begin rw [definable_iff_empty_definable_with_params] at *, exact hAs.map_expansion (L.Lhom_with_constants_map (set.inclusion hAB)), end @[simp] lemma definable_empty : A.definable L (∅ : set (α → M)) := ⟨⊥, by {ext, simp} ⟩ @[simp] lemma definable_univ : A.definable L (univ : set (α → M)) := ⟨⊤, by {ext, simp} ⟩ @[simp] lemma definable.inter {f g : set (α → M)} (hf : A.definable L f) (hg : A.definable L g) : A.definable L (f ∩ g) := begin rcases hf with ⟨φ, rfl⟩, rcases hg with ⟨θ, rfl⟩, refine ⟨φ ⊓ θ, _⟩, ext, simp, end @[simp] lemma definable.union {f g : set (α → M)} (hf : A.definable L f) (hg : A.definable L g) : A.definable L (f ∪ g) := begin rcases hf with ⟨φ, hφ⟩, rcases hg with ⟨θ, hθ⟩, refine ⟨φ ⊔ θ, _⟩, ext, rw [hφ, hθ, mem_set_of_eq, formula.realize_sup, mem_union, mem_set_of_eq, mem_set_of_eq], end lemma definable_finset_inf {ι : Type*} {f : Π (i : ι), set (α → M)} (hf : ∀ i, A.definable L (f i)) (s : finset ι) : A.definable L (s.inf f) := begin classical, refine finset.induction definable_univ (λ i s is h, _) s, rw finset.inf_insert, exact (hf i).inter h, end lemma definable_finset_sup {ι : Type*} {f : Π (i : ι), set (α → M)} (hf : ∀ i, A.definable L (f i)) (s : finset ι) : A.definable L (s.sup f) := begin classical, refine finset.induction definable_empty (λ i s is h, _) s, rw finset.sup_insert, exact (hf i).union h, end lemma definable_finset_bInter {ι : Type*} {f : Π (i : ι), set (α → M)} (hf : ∀ i, A.definable L (f i)) (s : finset ι) : A.definable L (⋂ i ∈ s, f i) := begin rw ← finset.inf_set_eq_bInter, exact definable_finset_inf hf s, end lemma definable_finset_bUnion {ι : Type*} {f : Π (i : ι), set (α → M)} (hf : ∀ i, A.definable L (f i)) (s : finset ι) : A.definable L (⋃ i ∈ s, f i) := begin rw ← finset.sup_set_eq_bUnion, exact definable_finset_sup hf s, end @[simp] lemma definable.compl {s : set (α → M)} (hf : A.definable L s) : A.definable L sᶜ := begin rcases hf with ⟨φ, hφ⟩, refine ⟨φ.not, _⟩, rw hφ, refl, end @[simp] lemma definable.sdiff {s t : set (α → M)} (hs : A.definable L s) (ht : A.definable L t) : A.definable L (s \ t) := hs.inter ht.compl lemma definable.preimage_comp (f : α → β) {s : set (α → M)} (h : A.definable L s) : A.definable L ((λ g : β → M, g ∘ f) ⁻¹' s) := begin obtain ⟨φ, rfl⟩ := h, refine ⟨(φ.relabel f), _⟩, ext, simp only [set.preimage_set_of_eq, mem_set_of_eq, formula.realize_relabel], end lemma definable.image_comp_equiv {s : set (β → M)} (h : A.definable L s) (f : α ≃ β) : A.definable L ((λ g : β → M, g ∘ f) '' s) := begin refine (congr rfl _).mp (h.preimage_comp f.symm), rw image_eq_preimage_of_inverse, { intro i, ext b, simp only [function.comp_app, equiv.apply_symm_apply], }, { intro i, ext a, simp } end /-- This lemma is only intended as a helper for `definable.image_comp. -/ lemma definable.image_comp_sum_inl_fin (m : ℕ) {s : set ((α ⊕ fin m) → M)} (h : A.definable L s) : A.definable L ((λ g : (α ⊕ fin m) → M, g ∘ sum.inl) '' s) := begin obtain ⟨φ, rfl⟩ := h, refine ⟨(bounded_formula.relabel id φ).exs, _⟩, ext x, simp only [set.mem_image, mem_set_of_eq, bounded_formula.realize_exs, bounded_formula.realize_relabel, function.comp.right_id, fin.cast_add_zero, fin.cast_refl], split, { rintro ⟨y, hy, rfl⟩, exact ⟨y ∘ sum.inr, (congr (congr rfl (sum.elim_comp_inl_inr y).symm) (funext fin_zero_elim)).mp hy⟩ }, { rintro ⟨y, hy⟩, exact ⟨sum.elim x y, (congr rfl (funext fin_zero_elim)).mp hy, sum.elim_comp_inl _ _⟩, }, end /-- Shows that definability is closed under finite projections. -/ lemma definable.image_comp_embedding {s : set (β → M)} (h : A.definable L s) (f : α ↪ β) [finite β] : A.definable L ((λ g : β → M, g ∘ f) '' s) := begin classical, casesI nonempty_fintype β, refine (congr rfl (ext (λ x, _))).mp (((h.image_comp_equiv (equiv.set.sum_compl (range f))).image_comp_equiv (equiv.sum_congr (equiv.of_injective f f.injective) (fintype.equiv_fin _).symm)).image_comp_sum_inl_fin _), simp only [mem_preimage, mem_image, exists_exists_and_eq_and], refine exists_congr (λ y, and_congr_right (λ ys, eq.congr_left (funext (λ a, _)))), simp, end /-- Shows that definability is closed under finite projections. -/ lemma definable.image_comp {s : set (β → M)} (h : A.definable L s) (f : α → β) [finite α] [finite β] : A.definable L ((λ g : β → M, g ∘ f) '' s) := begin classical, casesI nonempty_fintype α, casesI nonempty_fintype β, have h := (((h.image_comp_equiv (equiv.set.sum_compl (range f))).image_comp_equiv (equiv.sum_congr (_root_.equiv.refl _) (fintype.equiv_fin _).symm)).image_comp_sum_inl_fin _).preimage_comp (range_splitting f), have h' : A.definable L ({ x : α → M | ∀ a, x a = x (range_splitting f (range_factorization f a))}), { have h' : ∀ a, A.definable L {x : α → M | x a = x (range_splitting f (range_factorization f a))}, { refine λ a, ⟨(var a).equal (var (range_splitting f (range_factorization f a))), ext _⟩, simp, }, refine (congr rfl (ext _)).mp (definable_finset_bInter h' finset.univ), simp }, refine (congr rfl (ext (λ x, _))).mp (h.inter h'), simp only [equiv.coe_trans, mem_inter_iff, mem_preimage, mem_image, exists_exists_and_eq_and, mem_set_of_eq], split, { rintro ⟨⟨y, ys, hy⟩, hx⟩, refine ⟨y, ys, _⟩, ext a, rw [hx a, ← function.comp_apply x, ← hy], simp, }, { rintro ⟨y, ys, rfl⟩, refine ⟨⟨y, ys, _⟩, λ a, _⟩, { ext, simp [set.apply_range_splitting f] }, { rw [function.comp_apply, function.comp_apply, apply_range_splitting f, range_factorization_coe], }} end variables (L) {M} (A) /-- A 1-dimensional version of `definable`, for `set M`. -/ def definable₁ (s : set M) : Prop := A.definable L { x : fin 1 → M | x 0 ∈ s } /-- A 2-dimensional version of `definable`, for `set (M × M)`. -/ def definable₂ (s : set (M × M)) : Prop := A.definable L { x : fin 2 → M | (x 0, x 1) ∈ s } end set namespace first_order namespace language open set variables (L : first_order.language.{u v}) {M : Type w} [L.Structure M] (A : set M) (α : Type*) /-- Definable sets are subsets of finite Cartesian products of a structure such that membership is given by a first-order formula. -/ def definable_set := { s : set (α → M) // A.definable L s} namespace definable_set variables {L A α} {s t : L.definable_set A α} {x : α → M} instance : set_like (L.definable_set A α) (α → M) := { coe := subtype.val, coe_injective' := subtype.val_injective } instance : has_top (L.definable_set A α) := ⟨⟨⊤, definable_univ⟩⟩ instance : has_bot (L.definable_set A α) := ⟨⟨⊥, definable_empty⟩⟩ instance : has_sup (L.definable_set A α) := ⟨λ s t, ⟨s ∪ t, s.2.union t.2⟩⟩ instance : has_inf (L.definable_set A α) := ⟨λ s t, ⟨s ∩ t, s.2.inter t.2⟩⟩ instance : has_compl (L.definable_set A α) := ⟨λ s, ⟨sᶜ, s.2.compl⟩⟩ instance : has_sdiff (L.definable_set A α) := ⟨λ s t, ⟨s \ t, s.2.sdiff t.2⟩⟩ instance : inhabited (L.definable_set A α) := ⟨⊥⟩ lemma le_iff : s ≤ t ↔ (s : set (α → M)) ≤ (t : set (α → M)) := iff.rfl @[simp] lemma mem_top : x ∈ (⊤ : L.definable_set A α) := mem_univ x @[simp] lemma not_mem_bot {x : α → M} : ¬ x ∈ (⊥ : L.definable_set A α) := not_mem_empty x @[simp] lemma mem_sup : x ∈ s ⊔ t ↔ x ∈ s ∨ x ∈ t := iff.rfl @[simp] lemma mem_inf : x ∈ s ⊓ t ↔ x ∈ s ∧ x ∈ t := iff.rfl @[simp] lemma mem_compl : x ∈ sᶜ ↔ ¬ x ∈ s := iff.rfl @[simp] lemma mem_sdiff : x ∈ s \ t ↔ x ∈ s ∧ ¬ x ∈ t := iff.rfl @[simp, norm_cast] lemma coe_top : ((⊤ : L.definable_set A α) : set (α → M)) = univ := rfl @[simp, norm_cast] lemma coe_bot : ((⊥ : L.definable_set A α) : set (α → M)) = ∅ := rfl @[simp, norm_cast] lemma coe_sup (s t : L.definable_set A α) : (↑(s ⊔ t) : set (α → M)) = s ∪ t := rfl @[simp, norm_cast] lemma coe_inf (s t : L.definable_set A α) : (↑(s ⊓ t) : set (α → M)) = s ∩ t := rfl @[simp, norm_cast] lemma coe_compl (s : L.definable_set A α) : (↑(sᶜ) : set (α → M)) = sᶜ := rfl @[simp, norm_cast] lemma coe_sdiff (s t : L.definable_set A α) : (↑(s \ t) : set (α → M)) = s \ t := rfl instance : boolean_algebra (L.definable_set A α) := subtype.coe_injective.boolean_algebra _ coe_sup coe_inf coe_top coe_bot coe_compl coe_sdiff end definable_set end language end first_order
141cabd6ccb18f9355f6edbe8b7f7a32ffc9770f
63abd62053d479eae5abf4951554e1064a4c45b4
/src/order/rel_classes.lean
71f8b654568478334f1672ff28be8fc6b63eebf8
[ "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
13,594
lean
/- Copyright (c) 2020 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Mario Carneiro, Yury G. Kudryashov -/ import order.basic /-! # Unbundled relation classes In this file we prove some properties of `is_*` classes defined in `init.algebra.classes`. The main difference between these classes and the usual order classes (`preorder` etc) is that usual classes extend `has_le` and/or `has_lt` while these classes take a relation as an explicit argument. -/ universes u v variables {α : Type u} {β : Type v} {r : α → α → Prop} {s : β → β → Prop} open function theorem is_refl.swap (r) [is_refl α r] : is_refl α (swap r) := ⟨refl_of r⟩ theorem is_irrefl.swap (r) [is_irrefl α r] : is_irrefl α (swap r) := ⟨irrefl_of r⟩ theorem is_trans.swap (r) [is_trans α r] : is_trans α (swap r) := ⟨λ a b c h₁ h₂, trans_of r h₂ h₁⟩ theorem is_antisymm.swap (r) [is_antisymm α r] : is_antisymm α (swap r) := ⟨λ a b h₁ h₂, antisymm h₂ h₁⟩ theorem is_asymm.swap (r) [is_asymm α r] : is_asymm α (swap r) := ⟨λ a b h₁ h₂, asymm_of r h₂ h₁⟩ theorem is_total.swap (r) [is_total α r] : is_total α (swap r) := ⟨λ a b, (total_of r a b).swap⟩ theorem is_trichotomous.swap (r) [is_trichotomous α r] : is_trichotomous α (swap r) := ⟨λ a b, by simpa [swap, or.comm, or.left_comm] using trichotomous_of r a b⟩ theorem is_preorder.swap (r) [is_preorder α r] : is_preorder α (swap r) := {..@is_refl.swap α r _, ..@is_trans.swap α r _} theorem is_strict_order.swap (r) [is_strict_order α r] : is_strict_order α (swap r) := {..@is_irrefl.swap α r _, ..@is_trans.swap α r _} theorem is_partial_order.swap (r) [is_partial_order α r] : is_partial_order α (swap r) := {..@is_preorder.swap α r _, ..@is_antisymm.swap α r _} theorem is_total_preorder.swap (r) [is_total_preorder α r] : is_total_preorder α (swap r) := {..@is_preorder.swap α r _, ..@is_total.swap α r _} theorem is_linear_order.swap (r) [is_linear_order α r] : is_linear_order α (swap r) := {..@is_partial_order.swap α r _, ..@is_total.swap α r _} protected theorem is_asymm.is_antisymm (r) [is_asymm α r] : is_antisymm α r := ⟨λ x y h₁ h₂, (asymm h₁ h₂).elim⟩ protected theorem is_asymm.is_irrefl [is_asymm α r] : is_irrefl α r := ⟨λ a h, asymm h h⟩ /- Convert algebraic structure style to explicit relation style typeclasses -/ instance [preorder α] : is_refl α (≤) := ⟨le_refl⟩ instance [preorder α] : is_refl α (≥) := is_refl.swap _ instance [preorder α] : is_trans α (≤) := ⟨@le_trans _ _⟩ instance [preorder α] : is_trans α (≥) := is_trans.swap _ instance [preorder α] : is_preorder α (≤) := {} instance [preorder α] : is_preorder α (≥) := {} instance [preorder α] : is_irrefl α (<) := ⟨lt_irrefl⟩ instance [preorder α] : is_irrefl α (>) := is_irrefl.swap _ instance [preorder α] : is_trans α (<) := ⟨@lt_trans _ _⟩ instance [preorder α] : is_trans α (>) := is_trans.swap _ instance [preorder α] : is_asymm α (<) := ⟨@lt_asymm _ _⟩ instance [preorder α] : is_asymm α (>) := is_asymm.swap _ instance [preorder α] : is_antisymm α (<) := is_asymm.is_antisymm _ instance [preorder α] : is_antisymm α (>) := is_asymm.is_antisymm _ instance [preorder α] : is_strict_order α (<) := {} instance [preorder α] : is_strict_order α (>) := {} instance preorder.is_total_preorder [preorder α] [is_total α (≤)] : is_total_preorder α (≤) := {} instance [partial_order α] : is_antisymm α (≤) := ⟨@le_antisymm _ _⟩ instance [partial_order α] : is_antisymm α (≥) := is_antisymm.swap _ instance [partial_order α] : is_partial_order α (≤) := {} instance [partial_order α] : is_partial_order α (≥) := {} instance [linear_order α] : is_total α (≤) := ⟨le_total⟩ instance [linear_order α] : is_total α (≥) := is_total.swap _ instance linear_order.is_total_preorder [linear_order α] : is_total_preorder α (≤) := by apply_instance instance [linear_order α] : is_total_preorder α (≥) := {} instance [linear_order α] : is_linear_order α (≤) := {} instance [linear_order α] : is_linear_order α (≥) := {} instance [linear_order α] : is_trichotomous α (<) := ⟨lt_trichotomy⟩ instance [linear_order α] : is_trichotomous α (>) := is_trichotomous.swap _ instance order_dual.is_total_le [has_le α] [is_total α (≤)] : is_total (order_dual α) (≤) := @is_total.swap α _ _ lemma trans_trichotomous_left [is_trans α r] [is_trichotomous α r] {a b c : α} : ¬r b a → r b c → r a c := begin intros h₁ h₂, rcases trichotomous_of r a b with h₃|h₃|h₃, exact trans h₃ h₂, rw h₃, exact h₂, exfalso, exact h₁ h₃ end lemma trans_trichotomous_right [is_trans α r] [is_trichotomous α r] {a b c : α} : r a b → ¬r c b → r a c := begin intros h₁ h₂, rcases trichotomous_of r b c with h₃|h₃|h₃, exact trans h₁ h₃, rw ←h₃, exact h₁, exfalso, exact h₂ h₃ end /-- Construct a partial order from a `is_strict_order` relation -/ def partial_order_of_SO (r) [is_strict_order α r] : partial_order α := { le := λ x y, x = y ∨ r x y, lt := r, le_refl := λ x, or.inl rfl, le_trans := λ x y z h₁ h₂, match y, z, h₁, h₂ with | _, _, or.inl rfl, h₂ := h₂ | _, _, h₁, or.inl rfl := h₁ | _, _, or.inr h₁, or.inr h₂ := or.inr (trans h₁ h₂) end, le_antisymm := λ x y h₁ h₂, match y, h₁, h₂ with | _, or.inl rfl, h₂ := rfl | _, h₁, or.inl rfl := rfl | _, or.inr h₁, or.inr h₂ := (asymm h₁ h₂).elim end, lt_iff_le_not_le := λ x y, ⟨λ h, ⟨or.inr h, not_or (λ e, by rw e at h; exact irrefl _ h) (asymm h)⟩, λ ⟨h₁, h₂⟩, h₁.resolve_left (λ e, h₂ $ e ▸ or.inl rfl)⟩ } /-- This is basically the same as `is_strict_total_order`, but that definition is in Type (probably by mistake) and also has redundant assumptions. -/ @[algebra] class is_strict_total_order' (α : Type u) (lt : α → α → Prop) extends is_trichotomous α lt, is_strict_order α lt : Prop. /-- Construct a linear order from an `is_strict_total_order'` relation -/ def linear_order_of_STO' (r) [is_strict_total_order' α r] [Π x y, decidable (¬ r x y)] : linear_order α := { le_total := λ x y, match y, trichotomous_of r x y with | y, or.inl h := or.inl (or.inr h) | _, or.inr (or.inl rfl) := or.inl (or.inl rfl) | _, or.inr (or.inr h) := or.inr (or.inr h) end, decidable_le := λ x y, decidable_of_iff (¬ r y x) ⟨λ h, ((trichotomous_of r y x).resolve_left h).imp eq.symm id, λ h, h.elim (λ h, h ▸ irrefl_of _ _) (asymm_of r)⟩, ..partial_order_of_SO r } theorem is_strict_total_order'.swap (r) [is_strict_total_order' α r] : is_strict_total_order' α (swap r) := {..is_trichotomous.swap r, ..is_strict_order.swap r} instance [linear_order α] : is_strict_total_order' α (<) := {} /-- A connected order is one satisfying the condition `a < c → a < b ∨ b < c`. This is recognizable as an intuitionistic substitute for `a ≤ b ∨ b ≤ a` on the constructive reals, and is also known as negative transitivity, since the contrapositive asserts transitivity of the relation `¬ a < b`. -/ @[algebra] class is_order_connected (α : Type u) (lt : α → α → Prop) : Prop := (conn : ∀ a b c, lt a c → lt a b ∨ lt b c) theorem is_order_connected.neg_trans {r : α → α → Prop} [is_order_connected α r] {a b c} (h₁ : ¬ r a b) (h₂ : ¬ r b c) : ¬ r a c := mt (is_order_connected.conn a b c) $ by simp [h₁, h₂] theorem is_strict_weak_order_of_is_order_connected [is_asymm α r] [is_order_connected α r] : is_strict_weak_order α r := { trans := λ a b c h₁ h₂, (is_order_connected.conn _ c _ h₁).resolve_right (asymm h₂), incomp_trans := λ a b c ⟨h₁, h₂⟩ ⟨h₃, h₄⟩, ⟨is_order_connected.neg_trans h₁ h₃, is_order_connected.neg_trans h₄ h₂⟩, ..@is_asymm.is_irrefl α r _ } @[priority 100] -- see Note [lower instance priority] instance is_order_connected_of_is_strict_total_order' [is_strict_total_order' α r] : is_order_connected α r := ⟨λ a b c h, (trichotomous _ _).imp_right (λ o, o.elim (λ e, e ▸ h) (λ h', trans h' h))⟩ @[priority 100] -- see Note [lower instance priority] instance is_strict_total_order_of_is_strict_total_order' [is_strict_total_order' α r] : is_strict_total_order α r := {..is_strict_weak_order_of_is_order_connected} instance [linear_order α] : is_strict_total_order α (<) := by apply_instance instance [linear_order α] : is_order_connected α (<) := by apply_instance instance [linear_order α] : is_incomp_trans α (<) := by apply_instance instance [linear_order α] : is_strict_weak_order α (<) := by apply_instance /-- An extensional relation is one in which an element is determined by its set of predecessors. It is named for the `x ∈ y` relation in set theory, whose extensionality is one of the first axioms of ZFC. -/ @[algebra] class is_extensional (α : Type u) (r : α → α → Prop) : Prop := (ext : ∀ a b, (∀ x, r x a ↔ r x b) → a = b) @[priority 100] -- see Note [lower instance priority] instance is_extensional_of_is_strict_total_order' [is_strict_total_order' α r] : is_extensional α r := ⟨λ a b H, ((@trichotomous _ r _ a b) .resolve_left $ mt (H _).2 (irrefl a)) .resolve_right $ mt (H _).1 (irrefl b)⟩ /-- A well order is a well-founded linear order. -/ @[algebra] class is_well_order (α : Type u) (r : α → α → Prop) extends is_strict_total_order' α r : Prop := (wf : well_founded r) @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_strict_total_order {α} (r : α → α → Prop) [is_well_order α r] : is_strict_total_order α r := by apply_instance @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_extensional {α} (r : α → α → Prop) [is_well_order α r] : is_extensional α r := by apply_instance @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_trichotomous {α} (r : α → α → Prop) [is_well_order α r] : is_trichotomous α r := by apply_instance @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_trans {α} (r : α → α → Prop) [is_well_order α r] : is_trans α r := by apply_instance @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_irrefl {α} (r : α → α → Prop) [is_well_order α r] : is_irrefl α r := by apply_instance @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_asymm {α} (r : α → α → Prop) [is_well_order α r] : is_asymm α r := by apply_instance /-- Construct a decidable linear order from a well-founded linear order. -/ noncomputable def is_well_order.linear_order (r : α → α → Prop) [is_well_order α r] : linear_order α := by { letI := λ x y, classical.dec (¬r x y), exact linear_order_of_STO' r } instance empty_relation.is_well_order [subsingleton α] : is_well_order α empty_relation := { trichotomous := λ a b, or.inr $ or.inl $ subsingleton.elim _ _, irrefl := λ a, id, trans := λ a b c, false.elim, wf := ⟨λ a, ⟨_, λ y, false.elim⟩⟩ } instance nat.lt.is_well_order : is_well_order ℕ (<) := ⟨nat.lt_wf⟩ instance sum.lex.is_well_order [is_well_order α r] [is_well_order β s] : is_well_order (α ⊕ β) (sum.lex r s) := { trichotomous := λ a b, by cases a; cases b; simp; apply trichotomous, irrefl := λ a, by cases a; simp; apply irrefl, trans := λ a b c, by cases a; cases b; simp; cases c; simp; apply trans, wf := sum.lex_wf is_well_order.wf is_well_order.wf } instance prod.lex.is_well_order [is_well_order α r] [is_well_order β s] : is_well_order (α × β) (prod.lex r s) := { trichotomous := λ ⟨a₁, a₂⟩ ⟨b₁, b₂⟩, match @trichotomous _ r _ a₁ b₁ with | or.inl h₁ := or.inl $ prod.lex.left _ _ h₁ | or.inr (or.inr h₁) := or.inr $ or.inr $ prod.lex.left _ _ h₁ | or.inr (or.inl e) := e ▸ match @trichotomous _ s _ a₂ b₂ with | or.inl h := or.inl $ prod.lex.right _ h | or.inr (or.inr h) := or.inr $ or.inr $ prod.lex.right _ h | or.inr (or.inl e) := e ▸ or.inr $ or.inl rfl end end, irrefl := λ ⟨a₁, a₂⟩ h, by cases h with _ _ _ _ h _ _ _ h; [exact irrefl _ h, exact irrefl _ h], trans := λ a b c h₁ h₂, begin cases h₁ with a₁ a₂ b₁ b₂ ab a₁ b₁ b₂ ab; cases h₂ with _ _ c₁ c₂ bc _ _ c₂ bc, { exact prod.lex.left _ _ (trans ab bc) }, { exact prod.lex.left _ _ ab }, { exact prod.lex.left _ _ bc }, { exact prod.lex.right _ (trans ab bc) } end, wf := prod.lex_wf is_well_order.wf is_well_order.wf } /-- An unbounded or cofinal set -/ def unbounded (r : α → α → Prop) (s : set α) : Prop := ∀ a, ∃ b ∈ s, ¬ r b a /-- A bounded or final set -/ def bounded (r : α → α → Prop) (s : set α) : Prop := ∃a, ∀ b ∈ s, r b a @[simp] lemma not_bounded_iff {r : α → α → Prop} (s : set α) : ¬bounded r s ↔ unbounded r s := begin classical, simp only [bounded, unbounded, not_forall, not_exists, exists_prop, not_and, not_not] end @[simp] lemma not_unbounded_iff {r : α → α → Prop} (s : set α) : ¬unbounded r s ↔ bounded r s := by { classical, rw [not_iff_comm, not_bounded_iff] }
a9f11d1cc129a91f880f512d07868fb80a97e48a
ebbdcbd7ddc89a9ef7c3b397b301d5f5272a918f
/qp/p1_categories/c2_limits/s6_stability.lean
21d06e5d88980ca33af0fa506ab139c305775e5a
[]
no_license
intoverflow/qvr
34b9ef23604738381ca20b7d622fd0399d88f2dd
0cfcd33fe4bf8d93851a00cec5bfd21e77105d74
refs/heads/master
1,616,591,570,371
1,492,575,772,000
1,492,575,772,000
80,061,627
0
0
null
null
null
null
UTF-8
Lean
false
false
5,627
lean
/- ----------------------------------------------------------------------- Stability of limits and colimits. ----------------------------------------------------------------------- -/ import .s1_limits import .s2_products import .s3_pullbacks namespace qp open stdaux universe variables ℓ ℓobj ℓhom /- ----------------------------------------------------------------------- Stability of pullbacks and coproducts. ----------------------------------------------------------------------- -/ /-! #brief The hom Σ (A × B) → A × Σ B. -/ definition coproduct_pullback.pullback_coproduct {C : Cat.{ℓobj ℓhom}} [C_HasAllPullbacks : HasAllPullbacks C] [C_HasAllCoProducts : HasAllCoProducts C] {x y : C^.obj} (f : C^.hom x y) {I : Type ℓ} (factors : I → C^.obj) (h : ∀ (i : I), C^.hom (factors i) y) : C^.hom (coproduct C (λ (i : I), pullback C (f ↗→ h i ↗→↗))) (pullback C (f ↗→ coproduct.univ C factors (CoProductCone.mk y h) ↗→↗)) := let ccone₁ : CoProductCone C (λ (i : I), pullback C (f↗→(h i)↗→↗)) := CoProductCone.mk x (λ i, pullback.π C (f↗→(h i)↗→↗) (@fin_of 1 0)) in let h₁ : ⟦C : coproduct C (λ (i : I), pullback C (f↗→(h i)↗→↗)) →→ x⟧ := coproduct.univ C (λ (i : I), pullback C (f↗→(h i)↗→↗)) ccone₁ in let ccone₂ : CoProductCone C (λ (i : I), pullback C (f↗→(h i)↗→↗)) := CoProductCone.mk (coproduct C factors) (λ i, coproduct.ι C factors i ∘∘ pullback.π C (f↗→(h i)↗→↗) (@fin_of 0 1)) in let h₂ : ⟦C : coproduct C (λ (i : I), pullback C (f↗→(h i)↗→↗)) →→ coproduct C factors⟧ := coproduct.univ C (λ (i : I), pullback C (f↗→(h i)↗→↗)) ccone₂ in let ccone₃ : CoProductCone C (λ (i : I), pullback C (f↗→(h i)↗→↗)) := CoProductCone.mk y (λ i, f ∘∘ pullback.π C (f↗→(h i)↗→↗) (@fin_of 1 0)) in pullback.univ C (f ↗→ coproduct.univ C factors (CoProductCone.mk y h) ↗→↗) (PullbackCone.mk (f ↗→ coproduct.univ C factors (CoProductCone.mk y h) ↗→↗) (coproduct C (λ (i : I), pullback C (f ↗→ h i ↗→↗))) (coproduct.univ C (λ (i : I), pullback C (f↗→(h i)↗→↗)) ccone₃) (h₁ ↗← h₂ ↗←↗) begin apply HomsList.eq, { apply eq.symm, apply coproduct.univ.uniq C (λ (i : I), pullback C (f↗→(h i)↗→↗)) ccone₃, intro i, apply eq.symm, apply eq.trans (eq.symm C^.circ_assoc), apply Cat.circ.congr_right, apply eq.symm, apply coproduct.univ.mediates C (λ (i : I), pullback C (f↗→(h i)↗→↗)) ccone₁ }, apply HomsList.eq, { apply eq.symm, apply coproduct.univ.uniq C (λ (i : I), pullback C (f↗→(h i)↗→↗)) ccone₃, intro i, apply eq.symm, apply eq.trans (eq.symm C^.circ_assoc), apply eq.trans (Cat.circ.congr_right (eq.symm (coproduct.univ.mediates C (λ (i : I), pullback C (f↗→(h i)↗→↗)) ccone₂ i))), apply eq.trans C^.circ_assoc, apply eq.trans (Cat.circ.congr_left (eq.symm (coproduct.univ.mediates C factors (CoProductCone.mk y h) i))), dsimp [CoProductCone.mk, ProductCone.mk], apply pullback.π_comm C (f↗→(h i)↗→↗) (@fin_of 0 1) (@fin_of 1 0) }, trivial end) /-! #brief A category where pullbacks and coproducts commute. -/ class HasStable_CoProduct_Pullback (C : Cat.{ℓobj ℓhom}) [C_HasAllPullbacks : HasAllPullbacks C] [C_HasAllCoProducts : HasAllCoProducts C] := (inv : ∀ {x y : C^.obj} (f : C^.hom x y) {I : Type ℓ} (factors : I → C^.obj) (h : ∀ (i : I), C^.hom (factors i) y) , C^.hom (pullback C (f ↗→ coproduct.univ C factors (CoProductCone.mk y h) ↗→↗)) (coproduct C (λ (i : I), pullback C (f ↗→ h i ↗→↗)))) (iso : ∀ {x y : C^.obj} (f : C^.hom x y) {I : Type ℓ} (factors : I → C^.obj) (h : ∀ (i : I), C^.hom (factors i) y) , Iso (coproduct_pullback.pullback_coproduct f factors h) (inv f factors h)) /-! #brief The hom A × Σ B → Σ (A × B). -/ definition pullback_coproduct.coproduct_pullback {C : Cat.{ℓobj ℓhom}} [C_HasAllPullbacks : HasAllPullbacks C] [C_HasAllCoProducts : HasAllCoProducts C] [C_HasStable_CoProduct_Pullback : HasStable_CoProduct_Pullback C] {x y : C^.obj} (f : C^.hom x y) {I : Type ℓ} (factors : I → C^.obj) (h : ∀ (i : I), C^.hom (factors i) y) : C^.hom (pullback C (f ↗→ coproduct.univ C factors (CoProductCone.mk y h) ↗→↗)) (coproduct C (λ (i : I), pullback C (f ↗→ h i ↗→↗))) := HasStable_CoProduct_Pullback.inv C_HasAllPullbacks C_HasAllCoProducts f factors h /-! #brief The iso Σ (A × B) ≅ A × Σ B. -/ definition coproduct_pullback.iso {C : Cat.{ℓobj ℓhom}} [C_HasAllPullbacks : HasAllPullbacks C] [C_HasAllCoProducts : HasAllCoProducts C] [C_HasStable_CoProduct_Pullback : HasStable_CoProduct_Pullback C] {x y : C^.obj} (f : C^.hom x y) {I : Type ℓ} (factors : I → C^.obj) (h : ∀ (i : I), C^.hom (factors i) y) : Iso (coproduct_pullback.pullback_coproduct f factors h) (pullback_coproduct.coproduct_pullback f factors h) := HasStable_CoProduct_Pullback.iso C_HasAllPullbacks C_HasAllCoProducts f factors h end qp
029f972a15608666664fdc14899fc7d6ade735f6
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/t11.lean
73dd755d8c05586aa69cc7e266bc2e0b9328d49e
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
272
lean
constant A : Type.{1} constants a b c : A constant f : A → A → A check f a b section parameters A B : Type parameters {C D : Type} parameters [e : A] [d : A] check A check B definition g (a : A) (b : B) (c : C) : A := e end check g.{2 1} constants x y : A
902a3f9a28bfa98a9a83cd2918baff32c60d3931
94e33a31faa76775069b071adea97e86e218a8ee
/src/topology/paracompact.lean
f045a6edb3fc5769f45a5315228c9200aaa55672
[ "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
15,119
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Yury Kudryashov -/ import topology.subset_properties import topology.separation import data.option.basic /-! # Paracompact topological spaces A topological space `X` is said to be paracompact if every open covering of `X` admits a locally finite refinement. The definition requires that each set of the new covering is a subset of one of the sets of the initial covering. However, one can ensure that each open covering `s : ι → set X` admits a *precise* locally finite refinement, i.e., an open covering `t : ι → set X` with the same index set such that `∀ i, t i ⊆ s i`, see lemma `precise_refinement`. We also provide a convenience lemma `precise_refinement_set` that deals with open coverings of a closed subset of `X` instead of the whole space. We also prove the following facts. * Every compact space is paracompact, see instance `paracompact_of_compact`. * A locally compact sigma compact Hausdorff space is paracompact, see instance `paracompact_of_locally_compact_sigma_compact`. Moreover, we can choose a locally finite refinement with sets in a given collection of filter bases of `𝓝 x, `x : X`, see `refinement_of_locally_compact_sigma_compact_of_nhds_basis`. For example, in a proper metric space every open covering `⋃ i, s i` admits a refinement `⋃ i, metric.ball (c i) (r i)`. * Every paracompact Hausdorff space is normal. This statement is not an instance to avoid loops in the instance graph. * Every `emetric_space` is a paracompact space, see instance `emetric_space.paracompact_space` in `topology/metric_space/emetric_space`. ## TODO Prove (some of) [Michael's theorems](https://ncatlab.org/nlab/show/Michael%27s+theorem). ## Tags compact space, paracompact space, locally finite covering -/ open set filter function open_locale filter topological_space universes u v /-- A topological space is called paracompact, if every open covering of this space admits a locally finite refinement. We use the same universe for all types in the definition to avoid creating a class like `paracompact_space.{u v}`. Due to lemma `precise_refinement` below, every open covering `s : α → set X` indexed on `α : Type v` has a *precise* locally finite refinement, i.e., a locally finite refinement `t : α → set X` indexed on the same type such that each `∀ i, t i ⊆ s i`. -/ class paracompact_space (X : Type v) [topological_space X] : Prop := (locally_finite_refinement : ∀ (α : Type v) (s : α → set X) (ho : ∀ a, is_open (s a)) (hc : (⋃ a, s a) = univ), ∃ (β : Type v) (t : β → set X) (ho : ∀ b, is_open (t b)) (hc : (⋃ b, t b) = univ), locally_finite t ∧ ∀ b, ∃ a, t b ⊆ s a) variables {ι : Type u} {X : Type v} [topological_space X] /-- Any open cover of a paracompact space has a locally finite *precise* refinement, that is, one indexed on the same type with each open set contained in the corresponding original one. -/ lemma precise_refinement [paracompact_space X] (u : ι → set X) (uo : ∀ a, is_open (u a)) (uc : (⋃ i, u i) = univ) : ∃ v : ι → set X, (∀ a, is_open (v a)) ∧ (⋃ i, v i) = univ ∧ locally_finite v ∧ (∀ a, v a ⊆ u a) := begin -- Apply definition to `range u`, then turn existence quantifiers into functions using `choose` have := paracompact_space.locally_finite_refinement (range u) coe (set_coe.forall.2 $ forall_range_iff.2 uo) (by rwa [← sUnion_range, subtype.range_coe]), simp only [set_coe.exists, subtype.coe_mk, exists_range_iff', Union_eq_univ_iff, exists_prop] at this, choose α t hto hXt htf ind hind, choose t_inv ht_inv using hXt, choose U hxU hU using htf, -- Send each `i` to the union of `t a` over `a ∈ ind ⁻¹' {i}` refine ⟨λ i, ⋃ (a : α) (ha : ind a = i), t a, _, _, _, _⟩, { exact λ a, is_open_Union (λ a, is_open_Union $ λ ha, hto a) }, { simp only [eq_univ_iff_forall, mem_Union], exact λ x, ⟨ind (t_inv x), _, rfl, ht_inv _⟩ }, { refine λ x, ⟨U x, hxU x, ((hU x).image ind).subset _⟩, simp only [subset_def, mem_Union, mem_set_of_eq, set.nonempty, mem_inter_eq], rintro i ⟨y, ⟨a, rfl, hya⟩, hyU⟩, exact mem_image_of_mem _ ⟨y, hya, hyU⟩ }, { simp only [subset_def, mem_Union], rintro i x ⟨a, rfl, hxa⟩, exact hind _ hxa } end /-- In a paracompact space, every open covering of a closed set admits a locally finite refinement indexed by the same type. -/ lemma precise_refinement_set [paracompact_space X] {s : set X} (hs : is_closed s) (u : ι → set X) (uo : ∀ i, is_open (u i)) (us : s ⊆ ⋃ i, u i) : ∃ v : ι → set X, (∀ i, is_open (v i)) ∧ (s ⊆ ⋃ i, v i) ∧ locally_finite v ∧ (∀ i, v i ⊆ u i) := begin rcases precise_refinement (option.elim sᶜ u) (option.forall.2 ⟨is_open_compl_iff.2 hs, uo⟩) _ with ⟨v, vo, vc, vf, vu⟩, refine ⟨v ∘ some, λ i, vo _, _, vf.comp_injective (option.some_injective _), λ i, vu _⟩, { simp only [Union_option, ← compl_subset_iff_union] at vc, exact subset.trans (subset_compl_comm.1 $ vu option.none) vc }, { simpa only [Union_option, option.elim, ← compl_subset_iff_union, compl_compl] } end /-- A compact space is paracompact. -/ @[priority 100] -- See note [lower instance priority] instance paracompact_of_compact [compact_space X] : paracompact_space X := begin -- the proof is trivial: we choose a finite subcover using compactness, and use it refine ⟨λ ι s ho hu, _⟩, rcases compact_univ.elim_finite_subcover _ ho hu.ge with ⟨T, hT⟩, have := hT, simp only [subset_def, mem_Union] at this, choose i hiT hi using λ x, this x (mem_univ x), refine ⟨(T : set ι), λ t, s t, λ t, ho _, _, locally_finite_of_finite _, λ t, ⟨t, subset.rfl⟩⟩, simpa only [Union_coe_set, ← univ_subset_iff] end /-- Let `X` be a locally compact sigma compact Hausdorff topological space, let `s` be a closed set in `X`. Suppose that for each `x ∈ s` the sets `B x : ι x → set X` with the predicate `p x : ι x → Prop` form a basis of the filter `𝓝 x`. Then there exists a locally finite covering `λ i, B (c i) (r i)` of `s` such that all “centers” `c i` belong to `s` and each `r i` satisfies `p (c i)`. The notation is inspired by the case `B x r = metric.ball x r` but the theorem applies to `nhds_basis_opens` as well. If the covering must be subordinate to some open covering of `s`, then the user should use a basis obtained by `filter.has_basis.restrict_subset` or a similar lemma, see the proof of `paracompact_of_locally_compact_sigma_compact` for an example. The formalization is based on two [ncatlab](https://ncatlab.org/) proofs: * [locally compact and sigma compact spaces are paracompact](https://ncatlab.org/nlab/show/locally+compact+and+sigma-compact+spaces+are+paracompact); * [open cover of smooth manifold admits locally finite refinement by closed balls](https://ncatlab.org/nlab/show/partition+of+unity#ExistenceOnSmoothManifolds). See also `refinement_of_locally_compact_sigma_compact_of_nhds_basis` for a version of this lemma dealing with a covering of the whole space. In most cases (namely, if `B c r ∪ B c r'` is again a set of the form `B c r''`) it is possible to choose `α = X`. This fact is not yet formalized in `mathlib`. -/ theorem refinement_of_locally_compact_sigma_compact_of_nhds_basis_set [locally_compact_space X] [sigma_compact_space X] [t2_space X] {ι : X → Type u} {p : Π x, ι x → Prop} {B : Π x, ι x → set X} {s : set X} (hs : is_closed s) (hB : ∀ x ∈ s, (𝓝 x).has_basis (p x) (B x)) : ∃ (α : Type v) (c : α → X) (r : Π a, ι (c a)), (∀ a, c a ∈ s ∧ p (c a) (r a)) ∧ (s ⊆ ⋃ a, B (c a) (r a)) ∧ locally_finite (λ a, B (c a) (r a)) := begin classical, -- For technical reasons we prepend two empty sets to the sequence `compact_exhaustion.choice X` set K' : compact_exhaustion X := compact_exhaustion.choice X, set K : compact_exhaustion X := K'.shiftr.shiftr, set Kdiff := λ n, K (n + 1) \ interior (K n), -- Now we restate some properties of `compact_exhaustion` for `K`/`Kdiff` have hKcov : ∀ x, x ∈ Kdiff (K'.find x + 1), { intro x, simpa only [K'.find_shiftr] using diff_subset_diff_right interior_subset (K'.shiftr.mem_diff_shiftr_find x) }, have Kdiffc : ∀ n, is_compact (Kdiff n ∩ s), from λ n, ((K.is_compact _).diff is_open_interior).inter_right hs, -- Next we choose a finite covering `B (c n i) (r n i)` of each -- `Kdiff (n + 1) ∩ s` such that `B (c n i) (r n i) ∩ s` is disjoint with `K n` have : ∀ n (x : Kdiff (n + 1) ∩ s), (K n)ᶜ ∈ 𝓝 (x : X), from λ n x, is_open.mem_nhds (K.is_closed n).is_open_compl (λ hx', x.2.1.2 $ K.subset_interior_succ _ hx'), haveI : ∀ n (x : Kdiff n ∩ s), nonempty (ι x) := λ n x, (hB x x.2.2).nonempty, choose! r hrp hr using (λ n (x : Kdiff (n + 1) ∩ s), (hB x x.2.2).mem_iff.1 (this n x)), have hxr : ∀ n x (hx : x ∈ Kdiff (n + 1) ∩ s), B x (r n ⟨x, hx⟩) ∈ 𝓝 x, from λ n x hx, (hB x hx.2).mem_of_mem (hrp _ ⟨x, hx⟩), choose T hT using λ n, (Kdiffc (n + 1)).elim_nhds_subcover' _ (hxr n), set T' : Π n, set ↥(Kdiff (n + 1) ∩ s) := λ n, T n, -- Finally, we take the union of all these coverings refine ⟨Σ n, T' n, λ a, a.2, λ a, r a.1 a.2, _, _, _⟩, { rintro ⟨n, x, hx⟩, exact ⟨x.2.2, hrp _ _⟩ }, { refine (λ x hx, mem_Union.2 _), rcases mem_Union₂.1 (hT _ ⟨hKcov x, hx⟩) with ⟨⟨c, hc⟩, hcT, hcx⟩, exact ⟨⟨_, ⟨c, hc⟩, hcT⟩, hcx⟩ }, { intro x, refine ⟨interior (K (K'.find x + 3)), is_open.mem_nhds is_open_interior (K.subset_interior_succ _ (hKcov x).1), _⟩, have : (⋃ k ≤ K'.find x + 2, (range $ sigma.mk k) : set (Σ n, T' n)).finite, from (finite_le_nat _).bUnion (λ k hk, finite_range _), apply this.subset, rintro ⟨k, c, hc⟩, simp only [mem_Union, mem_set_of_eq, mem_image_eq, subtype.coe_mk], rintro ⟨x, hxB : x ∈ B c (r k c), hxK⟩, refine ⟨k, _, ⟨c, hc⟩, rfl⟩, have := (mem_compl_iff _ _).1 (hr k c hxB), contrapose! this with hnk, exact K.subset hnk (interior_subset hxK) }, end /-- Let `X` be a locally compact sigma compact Hausdorff topological space. Suppose that for each `x` the sets `B x : ι x → set X` with the predicate `p x : ι x → Prop` form a basis of the filter `𝓝 x`. Then there exists a locally finite covering `λ i, B (c i) (r i)` of `X` such that each `r i` satisfies `p (c i)` The notation is inspired by the case `B x r = metric.ball x r` but the theorem applies to `nhds_basis_opens` as well. If the covering must be subordinate to some open covering of `s`, then the user should use a basis obtained by `filter.has_basis.restrict_subset` or a similar lemma, see the proof of `paracompact_of_locally_compact_sigma_compact` for an example. The formalization is based on two [ncatlab](https://ncatlab.org/) proofs: * [locally compact and sigma compact spaces are paracompact](https://ncatlab.org/nlab/show/locally+compact+and+sigma-compact+spaces+are+paracompact); * [open cover of smooth manifold admits locally finite refinement by closed balls](https://ncatlab.org/nlab/show/partition+of+unity#ExistenceOnSmoothManifolds). See also `refinement_of_locally_compact_sigma_compact_of_nhds_basis_set` for a version of this lemma dealing with a covering of a closed set. In most cases (namely, if `B c r ∪ B c r'` is again a set of the form `B c r''`) it is possible to choose `α = X`. This fact is not yet formalized in `mathlib`. -/ theorem refinement_of_locally_compact_sigma_compact_of_nhds_basis [locally_compact_space X] [sigma_compact_space X] [t2_space X] {ι : X → Type u} {p : Π x, ι x → Prop} {B : Π x, ι x → set X} (hB : ∀ x, (𝓝 x).has_basis (p x) (B x)) : ∃ (α : Type v) (c : α → X) (r : Π a, ι (c a)), (∀ a, p (c a) (r a)) ∧ (⋃ a, B (c a) (r a)) = univ ∧ locally_finite (λ a, B (c a) (r a)) := let ⟨α, c, r, hp, hU, hfin⟩ := refinement_of_locally_compact_sigma_compact_of_nhds_basis_set is_closed_univ (λ x _, hB x) in ⟨α, c, r, λ a, (hp a).2, univ_subset_iff.1 hU, hfin⟩ /-- A locally compact sigma compact Hausdorff space is paracompact. See also `refinement_of_locally_compact_sigma_compact_of_nhds_basis` for a more precise statement. -/ @[priority 100] -- See note [lower instance priority] instance paracompact_of_locally_compact_sigma_compact [locally_compact_space X] [sigma_compact_space X] [t2_space X] : paracompact_space X := begin refine ⟨λ α s ho hc, _⟩, choose i hi using Union_eq_univ_iff.1 hc, have : ∀ x : X, (𝓝 x).has_basis (λ t : set X, (x ∈ t ∧ is_open t) ∧ t ⊆ s (i x)) id, from λ x : X, (nhds_basis_opens x).restrict_subset (is_open.mem_nhds (ho (i x)) (hi x)), rcases refinement_of_locally_compact_sigma_compact_of_nhds_basis this with ⟨β, c, t, hto, htc, htf⟩, exact ⟨β, t, λ x, (hto x).1.2, htc, htf, λ b, ⟨i $ c b, (hto b).2⟩⟩ end /- Dieudonné‘s theorem: a paracompact Hausdorff space is normal. Formalization is based on the proof at [ncatlab](https://ncatlab.org/nlab/show/paracompact+Hausdorff+spaces+are+normal). -/ lemma normal_of_paracompact_t2 [t2_space X] [paracompact_space X] : normal_space X := begin /- First we show how to go from points to a set on one side. -/ have : ∀ (s t : set X), is_closed s → is_closed t → (∀ x ∈ s, ∃ u v, is_open u ∧ is_open v ∧ x ∈ u ∧ t ⊆ v ∧ disjoint u v) → ∃ u v, is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ disjoint u v, { /- For each `x ∈ s` we choose open disjoint `u x ∋ x` and `v x ⊇ t`. The sets `u x` form an open covering of `s`. We choose a locally finite refinement `u' : s → set X`, then `⋃ i, u' i` and `(closure (⋃ i, u' i))ᶜ` are disjoint open neighborhoods of `s` and `t`. -/ intros s t hs ht H, choose u v hu hv hxu htv huv using set_coe.forall'.1 H, rcases precise_refinement_set hs u hu (λ x hx, mem_Union.2 ⟨⟨x, hx⟩, hxu _⟩) with ⟨u', hu'o, hcov', hu'fin, hsub⟩, refine ⟨⋃ i, u' i, (closure (⋃ i, u' i))ᶜ, is_open_Union hu'o, is_closed_closure.is_open_compl, hcov', _, disjoint_compl_right.mono le_rfl (compl_le_compl subset_closure)⟩, rw [hu'fin.closure_Union, compl_Union, subset_Inter_iff], refine λ i x hxt hxu, absurd (htv i hxt) (closure_minimal _ (is_closed_compl_iff.2 $ hv _) hxu), exact λ y hyu hyv, huv i ⟨hsub _ hyu, hyv⟩ }, /- Now we apply the lemma twice: first to `s` and `t`, then to `t` and each point of `s`. -/ refine ⟨λ s t hs ht hst, this s t hs ht (λ x hx, _)⟩, rcases this t {x} ht is_closed_singleton (λ y hy, _) with ⟨v, u, hv, hu, htv, hxu, huv⟩, { exact ⟨u, v, hu, hv, singleton_subset_iff.1 hxu, htv, huv.symm⟩ }, { simp_rw singleton_subset_iff, exact t2_separation (hst.symm.ne_of_mem hy hx) } end
3d65aff494538d8fe647fca1300b02c8c18f2d19
4727251e0cd73359b15b664c3170e5d754078599
/src/ring_theory/localization/as_subring.lean
c3457a89f702bd446aeb8abaddeb34812f4b24d5
[ "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
5,255
lean
/- Copyright (c) 2022 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz, Junyan Xu -/ import ring_theory.localization.localization_localization /-! # Localizations of domains as subalgebras of the fraction field. Given a domain `A` with fraction field `K`, and a submonoid `S` of `A` which does not contain zero, this file constructs the localization of `A` at `S` as a subalgebra of the field `K` over `A`. -/ namespace localization open_locale non_zero_divisors variables {A : Type*} (K : Type*) [comm_ring A] (S : submonoid A) (hS : S ≤ A⁰) section comm_ring variables [comm_ring K] [algebra A K] [is_fraction_ring A K] lemma map_is_unit_of_le (hS : S ≤ A⁰) (s : S) : is_unit (algebra_map A K s) := by apply is_localization.map_units K (⟨s.1, hS s.2⟩ : A⁰) /-- The canonical map from a localization of `A` at `S` to the fraction ring of `A`, given that `S ≤ A⁰`. -/ noncomputable def map_to_fraction_ring (B : Type*) [comm_ring B] [algebra A B] [is_localization S B] (hS : S ≤ A⁰) : B →ₐ[A] K := { commutes' := λ a, by simp, ..is_localization.lift (map_is_unit_of_le K S hS) } @[simp] lemma map_to_fraction_ring_apply {B : Type*} [comm_ring B] [algebra A B] [is_localization S B] (hS : S ≤ A⁰) (b : B) : map_to_fraction_ring K S B hS b = is_localization.lift (map_is_unit_of_le K S hS) b := rfl lemma mem_range_map_to_fraction_ring_iff (B : Type*) [comm_ring B] [algebra A B] [is_localization S B] (hS : S ≤ A⁰) (x : K) : x ∈ (map_to_fraction_ring K S B hS).range ↔ ∃ (a s : A) (hs : s ∈ S), x = is_localization.mk' K a ⟨s, hS hs⟩ := ⟨ by { rintro ⟨x,rfl⟩, obtain ⟨a,s,rfl⟩ := is_localization.mk'_surjective S x, use [a, s, s.2], apply is_localization.lift_mk' }, by { rintro ⟨a,s,hs,rfl⟩, use is_localization.mk' _ a ⟨s,hs⟩, apply is_localization.lift_mk' } ⟩ instance is_localization_range_map_to_fraction_ring (B : Type*) [comm_ring B] [algebra A B] [is_localization S B] (hS : S ≤ A⁰) : is_localization S (map_to_fraction_ring K S B hS).range := is_localization.is_localization_of_alg_equiv S $ show B ≃ₐ[A] _, from alg_equiv.of_bijective (map_to_fraction_ring K S B hS).range_restrict begin refine ⟨λ a b h, _, set.surjective_onto_range⟩, refine (is_localization.lift_injective_iff _).2 (λ a b, _) (subtype.ext_iff.1 h), exact ⟨λ h, congr_arg _ (is_localization.injective _ hS h), λ h, congr_arg _ (is_fraction_ring.injective A K h)⟩, end instance is_fraction_ring_range_map_to_fraction_ring (B : Type*) [comm_ring B] [algebra A B] [is_localization S B] (hS : S ≤ A⁰) : is_fraction_ring (map_to_fraction_ring K S B hS).range K := is_fraction_ring.is_fraction_ring_of_is_localization S _ _ hS /-- Given a commutative ring `A` with fraction ring `K`, and a submonoid `S` of `A` which contains no zero divisor, this is the localization of `A` at `S`, considered as a subalgebra of `K` over `A`. The carrier of this subalgebra is defined as the set of all `x : K` of the form `is_localization.mk' K a ⟨s, _⟩`, where `s ∈ S`. -/ noncomputable def subalgebra (hS : S ≤ A⁰) : subalgebra A K := (map_to_fraction_ring K S (localization S) hS).range.copy { x | ∃ (a s : A) (hs : s ∈ S), x = is_localization.mk' K a ⟨s, hS hs⟩ } $ by { ext, symmetry, apply mem_range_map_to_fraction_ring_iff } namespace subalgebra instance is_localization_subalgebra : is_localization S (subalgebra K S hS) := by { dunfold localization.subalgebra, rw subalgebra.copy_eq, apply_instance } instance is_fraction_ring : is_fraction_ring (subalgebra K S hS) K := is_fraction_ring.is_fraction_ring_of_is_localization S _ _ hS end subalgebra end comm_ring section field variables [field K] [algebra A K] [is_fraction_ring A K] namespace subalgebra lemma mem_range_map_to_fraction_ring_iff_of_field (B : Type*) [comm_ring B] [algebra A B] [is_localization S B] (x : K) : x ∈ (map_to_fraction_ring K S B hS).range ↔ ∃ (a s : A) (hs : s ∈ S), x = algebra_map A K a * (algebra_map A K s)⁻¹ := begin rw mem_range_map_to_fraction_ring_iff, iterate 3 { congr' with }, convert iff.rfl, rw units.coe_inv', refl, end /-- Given a domain `A` with fraction field `K`, and a submonoid `S` of `A` which contains no zero divisor, this is the localization of `A` at `S`, considered as a subalgebra of `K` over `A`. The carrier of this subalgebra is defined as the set of all `x : K` of the form `algebra_map A K a * (algebra_map A K s)⁻¹` where `a s : A` and `s ∈ S`. -/ noncomputable def of_field : _root_.subalgebra A K := (map_to_fraction_ring K S (localization S) hS).range.copy { x | ∃ (a s : A) (hs : s ∈ S), x = algebra_map A K a * (algebra_map A K s)⁻¹ } $ by { ext, symmetry, apply mem_range_map_to_fraction_ring_iff_of_field } instance is_localization_of_field : is_localization S (subalgebra.of_field K S hS) := by { dunfold localization.subalgebra.of_field, rw subalgebra.copy_eq, apply_instance } instance is_fraction_ring_of_field : is_fraction_ring (subalgebra.of_field K S hS) K := is_fraction_ring.is_fraction_ring_of_is_localization S _ _ hS end subalgebra end field end localization
b00e093d3a2a37c0b4bcc7128a0c8ffbea21e4aa
a6f55abce20abcd06e718cb3e5fba7bf8a230fa1
/topic/vegetable.lean
e94ef98fc8864f1ca0a965ebc179238bab04a883
[]
no_license
sonna0909/abc
b8a53e906d4d000d1f2347173a1cd4221757fabf
ff7b4c621cdf6d53937f2d1b6def28de2085a2aa
refs/heads/master
1,599,114,664,248
1,573,634,309,000
1,573,634,309,000
219,406,484
0
0
null
null
null
null
UTF-8
Lean
false
false
6,591
lean
[ { "key": "lettuce", "title": "Lettuce", "spelling": "/ˈlet.ɪs/", "subs": [ { "key": "lettuce1", "title": "I hate eating lettuce", "spelling": "" } ] }, { "key": "corn", "title": "Corn", "spelling": "/kɔːn/", "subs": [ { "key": "Corn1", "title": "I usually eat corn", "spelling": "" } ] }, { "key": "tomato", "title": "Tomato", "spelling": "/təˈmɑː.təʊ/", "subs": [ { "key": "7_Tomatoes", "title": "There are 7 tomatoes", "spelling": "" } ] }, { "key": "potato", "title": "Potato", "spelling": "/pəˈteɪ.təʊ/", "subs": [ { "key": "5_potatoes", "title": "There are 5 potatoes", "spelling": "" } ] }, { "key": "cabbage", "title": "Cabbage", "spelling": "/ˈkæb.ɪdʒ/", "subs": [ { "key": "cabbage1", "title": "I like eating cabbage", "spelling": "" } ] }, { "key": "mushroom", "title": "Mushroom", "spelling": "/ˈmʌʃ.ruːm/", "subs": [ { "key": "mushroom1", "title": "He hates eating mushroom", "spelling": "" } ] }, { "key": "onion", "title": "Onion ", "spelling": "/ˈʌn.jən/", "subs": [ { "key": "onion1", "title": "I don't like onion", "spelling": "" } ] }, { "key": "yam", "title": "Yam", "spelling": "/jæm/", "subs": [ { "key": "yam1", "title": "He likes yam", "spelling": "" } ] }, { "key": "garlic", "title": "Garlic", "spelling": "/ˈgɑː.lɪk/", "subs": [ { "key": "garlic1", "title": "My son hates garlic", "spelling": "" } ] }, { "key": "carrot", "title": "Carrot", "spelling": "/ˈkær.ət/", "subs": [ { "key": "carrot1", "title": "Eating carrot is very good for healthy", "spelling": "" } ] }, { "key": "celery", "title": "Celery", "spelling": "/ˈsel.ər.i/", "subs": [ { "key": "celery1", "title": "I can't eat celery", "spelling": "" } ] }, { "key": "green-onion", "title": "Green onion", "spelling": "/griːn ˈʌn.jən/", "subs": [ { "key": "green-onion1", "title": "I hate eating green-onion", "spelling": "" } ] }, { "key": "broccoli", "title": "Broccoli", "spelling": "/ˈbrɒk.əl.i/", "subs": [ { "key": "broccoli1", "title": "My son likes eating broccoli", "spelling": "" } ] }, { "key": "cauliflower", "title": "Cauliflower", "spelling": "/ˈkɒl.ɪˌflaʊ.əʳ/", "subs": [ { "key": "cauliflower1", "title": "He likes cauliflower", "spelling": "" } ] }, { "key": "salad", "title": "Salad", "spelling": "/ˈsæl.əd/", "subs": [ { "key": "salad1", "title": "They usually eat salad", "spelling": "" } ] }, { "key": "pepper", "title": "Pepper", "spelling": "/ˈpep.əʳ/", "subs": [ { "key": "pepper1", "title": "I don't can eat pepper", "spelling": "" } ] }, { "key": "bell-pepper", "title": "Bell-pepper", "spelling": "/bel ˈpep.əʳ/", "subs": [ { "key": "bell-pepper1", "title": "I hate bell-pepper", "spelling": "" } ] }, { "key": "bean", "title": "Bean", "spelling": "/biːn/", "subs": [ { "key": "bean1", "title": "My son likes eating bean", "spelling": "" } ] }, { "key": "pea", "title": "Pea", "spelling": "/piː/", "subs": [ { "key": "pea1", "title": "She hates pea", "spelling": "" } ] }, { "key": "pumpkin", "title": "Pumpkin", "spelling": "/ˈpʌmp.kɪn/", "subs": [ { "key": "pumpkin1", "title": "Pumpkin is very good for children’s health", "spelling": "" } ] }, { "key": "beet", "title": "Beet", "spelling": "/biːt/", "subs": [ { "key": "3_Beets", "title": "There are 3 beets", "spelling": "" } ] }, { "key": "radish", "title": "Radish", "spelling": "/ˈræd.ɪʃ/", "subs": [ { "key": "radish1", "title": "I like eating radish", "spelling": "" } ] }, { "key": "artichoke", "title": "Artichoke", "spelling": "/ˈɑː.tɪ.tʃəʊk/", "subs": [ { "key": "artichoke1", "title": "Artichoke is very good for health", "spelling": "" } ] }, { "key": "eggplant", "title": "Eggplant", "spelling": "/ˈeg.plɑːnt/", "subs": [ { "key": "eggplant1", "title": "I don't like eat eggplant", "spelling": "" } ] }, { "key": "cucumber", "title": "Cucumber", "spelling": "/ˈkjuː.kʌm.bəʳ/", "subs": [ { "key": "cucumber1", "title": "He likes eating cucumber", "spelling": "" } ] }, { "key": "zucchini", "title": "Zucchini", "spelling": "/zʊˈkiː.ni/", "subs": [ { "key": "zucchini1", "title": "My son likes zucchini", "spelling": "" } ] }, { "key": "asparagus", "title": "Asparagus", "spelling": "/əˈspær.ə.gəs/", "subs": [ { "key": "asparagus1", "title": "She don't like eat asparagus", "spelling": "" } ] }, { "key": "runner-bean", "title": "Runner-bean", "spelling": "/ˌrʌn.ə ˈbiːn/", "subs": [ { "key": "runner-bean1", "title": "He likes runner bean", "spelling": "" } ] }, { "key": "sprout", "title": "Sprout", "spelling": "/spraʊt/", "subs": [ { "key": "sprout1", "title": "I like eating sprout", "spelling": "" } ] }, { "key": "water-spinach", "title": "Water-spinach", "spelling": "/ˈwɔː.tər ˈspɪn.ɪtʃ/", "subs": [ { "key": "water-spinach1", "title": "My son don't like water spinach", "spelling": "" } ] } ]
7e351c63bd49f8d756a732d91bfb749a3ee5b63b
958488bc7f3c2044206e0358e56d7690b6ae696c
/lean/choice.lean
08b6c2c9c8ecd488005e1a482c3a2bf2c2ef1a89
[]
no_license
possientis/Prog
a08eec1c1b121c2fd6c70a8ae89e2fbef952adb4
d4b3debc37610a88e0dac3ac5914903604fd1d1f
refs/heads/master
1,692,263,717,723
1,691,757,179,000
1,691,757,179,000
40,361,602
3
0
null
1,679,896,438,000
1,438,953,859,000
Coq
UTF-8
Lean
false
false
1,216
lean
universe u class inductive nonempty2 (α : Sort u) : Prop | intro : α → nonempty2 --#check @nonempty --#check @nonempty2 lemma L1 : ∀ {α : Type u}, nonempty α ↔ ∃ (x:α), true := begin intros α, split; intros H; cases H with x, {existsi x, constructor}, {constructor, exact x} end axiom choice2 {α : Sort u} : nonempty α → α --#check @choice2 --open classical --#check @choice noncomputable theorem indefinite_description2 {α : Sort u} (p : α → Prop) : (∃ x, p x) → {x // p x} := begin intros H, apply classical.choice, cases H with x H /- now possible -/, constructor, constructor, exact H end noncomputable def some2 {α : Sort u} {p : α → Prop} (H:∃ x, p x) : α := subtype.val (classical.indefinite_description p H) theorem some_spec {α : Sort u} {p : α → Prop} (H: ∃ x, p x) : p (classical.some H) := subtype.property (classical.indefinite_description p H) noncomputable theorem inhabited_of_nonempty {α : Type u} : nonempty α → inhabited α := begin intros H, apply classical.choice, cases H with x /- now possible -/, constructor, constructor, exact x end --#check @classical.strong_indefinite_description --#check @classical.epsilon
22447d6b30db14db3ef25e469245fd124d2f6338
82e44445c70db0f03e30d7be725775f122d72f3e
/src/measure_theory/haar_measure.lean
8f82e78172986f3650c5293fe894ea5bf08b0fc1
[ "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,616
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import measure_theory.content import measure_theory.prod_group /-! # Haar measure In this file we prove the existence of Haar measure for a locally compact Hausdorff topological group. For the construction, we follow the write-up by Jonathan Gleason, *Existence and Uniqueness of Haar Measure*. This is essentially the same argument as in https://en.wikipedia.org/wiki/Haar_measure#A_construction_using_compact_subsets. We construct the Haar measure first on compact sets. For this we define `(K : U)` as the (smallest) number of left-translates of `U` are needed to cover `K` (`index` in the formalization). Then we define a function `h` on compact sets as `lim_U (K : U) / (K₀ : U)`, where `U` becomes a smaller and smaller open neighborhood of `1`, and `K₀` is a fixed compact set with nonempty interior. This function is `chaar` in the formalization, and we define the limit formally using Tychonoff's theorem. This function `h` forms a content, which we can extend to an outer measure and then a measure (`haar_measure`). We normalize the Haar measure so that the measure of `K₀` is `1`. We show that for second countable spaces any left invariant Borel measure is a scalar multiple of the Haar measure. Note that `μ` need not coincide with `h` on compact sets, according to [halmos1950measure, ch. X, §53 p.233]. However, we know that `h(K)` lies between `μ(Kᵒ)` and `μ(K)`, where `ᵒ` denotes the interior. ## Main Declarations * `haar_measure`: the Haar measure on a locally compact Hausdorff group. This is a left invariant regular measure. It takes as argument a compact set of the group (with non-empty interior), and is normalized so that the measure of the given set is 1. * `haar_measure_self`: the Haar measure is normalized. * `is_left_invariant_haar_measure`: the Haar measure is left invariant. * `regular_haar_measure`: the Haar measure is a regular measure. ## References * Paul Halmos (1950), Measure Theory, §53 * Jonathan Gleason, Existence and Uniqueness of Haar Measure - Note: step 9, page 8 contains a mistake: the last defined `μ` does not extend the `μ` on compact sets, see Halmos (1950) p. 233, bottom of the page. This makes some other steps (like step 11) invalid. * https://en.wikipedia.org/wiki/Haar_measure -/ noncomputable theory open set has_inv function topological_space measurable_space open_locale nnreal classical ennreal variables {G : Type*} [group G] namespace measure_theory namespace measure /-! We put the internal functions in the construction of the Haar measure in a namespace, so that the chosen names don't clash with other declarations. We first define a couple of the functions before proving the properties (that require that `G` is a topological group). -/ namespace haar /-- The index or Haar covering number or ratio of `K` w.r.t. `V`, denoted `(K : V)`: it is the smallest number of (left) translates of `V` that is necessary to cover `K`. It is defined to be 0 if no finite number of translates cover `K`. -/ @[to_additive add_index "additive version of `measure_theory.measure.haar.index`"] def index (K V : set G) : ℕ := Inf $ finset.card '' {t : finset G | K ⊆ ⋃ g ∈ t, (λ h, g * h) ⁻¹' V } @[to_additive add_index_empty] lemma index_empty {V : set G} : index ∅ V = 0 := begin simp only [index, nat.Inf_eq_zero], left, use ∅, simp only [finset.card_empty, empty_subset, mem_set_of_eq, eq_self_iff_true, and_self], end variables [topological_space G] /-- `prehaar K₀ U K` is a weighted version of the index, defined as `(K : U)/(K₀ : U)`. In the applications `K₀` is compact with non-empty interior, `U` is open containing `1`, and `K` is any compact set. The argument `K` is a (bundled) compact set, so that we can consider `prehaar K₀ U` as an element of `haar_product` (below). -/ @[to_additive "additive version of `measure_theory.measure.haar.prehaar`"] def prehaar (K₀ U : set G) (K : compacts G) : ℝ := (index K.1 U : ℝ) / index K₀ U @[to_additive] lemma prehaar_empty (K₀ : positive_compacts G) {U : set G} : prehaar K₀.1 U ⊥ = 0 := by { simp only [prehaar, compacts.bot_val, index_empty, nat.cast_zero, zero_div] } @[to_additive] lemma prehaar_nonneg (K₀ : positive_compacts G) {U : set G} (K : compacts G) : 0 ≤ prehaar K₀.1 U K := by apply div_nonneg; norm_cast; apply zero_le /-- `haar_product K₀` is the product of intervals `[0, (K : K₀)]`, for all compact sets `K`. For all `U`, we can show that `prehaar K₀ U ∈ haar_product K₀`. -/ @[to_additive "additive version of `measure_theory.measure.haar.haar_product`"] def haar_product (K₀ : set G) : set (compacts G → ℝ) := pi univ (λ K, Icc 0 $ index K.1 K₀) @[simp, to_additive] lemma mem_prehaar_empty {K₀ : set G} {f : compacts G → ℝ} : f ∈ haar_product K₀ ↔ ∀ K : compacts G, f K ∈ Icc (0 : ℝ) (index K.1 K₀) := by simp only [haar_product, pi, forall_prop_of_true, mem_univ, mem_set_of_eq] /-- The closure of the collection of elements of the form `prehaar K₀ U`, for `U` open neighbourhoods of `1`, contained in `V`. The closure is taken in the space `compacts G → ℝ`, with the topology of pointwise convergence. We show that the intersection of all these sets is nonempty, and the Haar measure on compact sets is defined to be an element in the closure of this intersection. -/ @[to_additive "additive version of `measure_theory.measure.haar.cl_prehaar`"] def cl_prehaar (K₀ : set G) (V : open_nhds_of (1 : G)) : set (compacts G → ℝ) := closure $ prehaar K₀ '' { U : set G | U ⊆ V.1 ∧ is_open U ∧ (1 : G) ∈ U } variables [topological_group G] /-! ### Lemmas about `index` -/ /-- If `K` is compact and `V` has nonempty interior, then the index `(K : V)` is well-defined, there is a finite set `t` satisfying the desired properties. -/ @[to_additive add_index_defined] lemma index_defined {K V : set G} (hK : is_compact K) (hV : (interior V).nonempty) : ∃ n : ℕ, n ∈ finset.card '' {t : finset G | K ⊆ ⋃ g ∈ t, (λ h, g * h) ⁻¹' V } := by { rcases compact_covered_by_mul_left_translates hK hV with ⟨t, ht⟩, exact ⟨t.card, t, ht, rfl⟩ } @[to_additive add_index_elim] lemma index_elim {K V : set G} (hK : is_compact K) (hV : (interior V).nonempty) : ∃ (t : finset G), K ⊆ (⋃ g ∈ t, (λ h, g * h) ⁻¹' V) ∧ finset.card t = index K V := by { have := nat.Inf_mem (index_defined hK hV), rwa [mem_image] at this } @[to_additive le_add_index_mul] lemma le_index_mul (K₀ : positive_compacts G) (K : compacts G) {V : set G} (hV : (interior V).nonempty) : index K.1 V ≤ index K.1 K₀.1 * index K₀.1 V := begin rcases index_elim K.2 K₀.2.2 with ⟨s, h1s, h2s⟩, rcases index_elim K₀.2.1 hV with ⟨t, h1t, h2t⟩, rw [← h2s, ← h2t, mul_comm], refine le_trans _ finset.mul_card_le, apply nat.Inf_le, refine ⟨_, _, rfl⟩, rw [mem_set_of_eq], refine subset.trans h1s _, apply bUnion_subset, intros g₁ hg₁, rw preimage_subset_iff, intros g₂ hg₂, have := h1t hg₂, rcases this with ⟨_, ⟨g₃, rfl⟩, A, ⟨hg₃, rfl⟩, h2V⟩, rw [mem_preimage, ← mul_assoc] at h2V, exact mem_bUnion (finset.mul_mem_mul hg₃ hg₁) h2V end @[to_additive add_index_pos] lemma index_pos (K : positive_compacts G) {V : set G} (hV : (interior V).nonempty) : 0 < index K.1 V := begin unfold index, rw [nat.Inf_def, nat.find_pos, mem_image], { rintro ⟨t, h1t, h2t⟩, rw [finset.card_eq_zero] at h2t, subst h2t, cases K.2.2 with g hg, show g ∈ (∅ : set G), convert h1t (interior_subset hg), symmetry, apply bUnion_empty }, { exact index_defined K.2.1 hV } end @[to_additive add_index_mono] lemma index_mono {K K' V : set G} (hK' : is_compact K') (h : K ⊆ K') (hV : (interior V).nonempty) : index K V ≤ index K' V := begin rcases index_elim hK' hV with ⟨s, h1s, h2s⟩, apply nat.Inf_le, rw [mem_image], refine ⟨s, subset.trans h h1s, h2s⟩ end @[to_additive add_index_union_le] lemma index_union_le (K₁ K₂ : compacts G) {V : set G} (hV : (interior V).nonempty) : index (K₁.1 ∪ K₂.1) V ≤ index K₁.1 V + index K₂.1 V := begin rcases index_elim K₁.2 hV with ⟨s, h1s, h2s⟩, rcases index_elim K₂.2 hV with ⟨t, h1t, h2t⟩, rw [← h2s, ← h2t], refine le_trans _ (finset.card_union_le _ _), apply nat.Inf_le, refine ⟨_, _, rfl⟩, rw [mem_set_of_eq], apply union_subset; refine subset.trans (by assumption) _; apply bUnion_subset_bUnion_left; intros g hg; simp only [mem_def] at hg; simp only [mem_def, multiset.mem_union, finset.union_val, hg, or_true, true_or] end @[to_additive add_index_union_eq] lemma index_union_eq (K₁ K₂ : compacts G) {V : set G} (hV : (interior V).nonempty) (h : disjoint (K₁.1 * V⁻¹) (K₂.1 * V⁻¹)) : index (K₁.1 ∪ K₂.1) V = index K₁.1 V + index K₂.1 V := begin apply le_antisymm (index_union_le K₁ K₂ hV), rcases index_elim (K₁.2.union K₂.2) hV with ⟨s, h1s, h2s⟩, rw [← h2s], have : ∀ (K : set G) , K ⊆ (⋃ g ∈ s, (λ h, g * h) ⁻¹' V) → index K V ≤ (s.filter (λ g, ((λ (h : G), g * h) ⁻¹' V ∩ K).nonempty)).card, { intros K hK, apply nat.Inf_le, refine ⟨_, _, rfl⟩, rw [mem_set_of_eq], intros g hg, rcases hK hg with ⟨_, ⟨g₀, rfl⟩, _, ⟨h1g₀, rfl⟩, h2g₀⟩, simp only [mem_preimage] at h2g₀, simp only [mem_Union], use g₀, split, { simp only [finset.mem_filter, h1g₀, true_and], use g, simp only [hg, h2g₀, mem_inter_eq, mem_preimage, and_self] }, exact h2g₀ }, refine le_trans (add_le_add (this K₁.1 $ subset.trans (subset_union_left _ _) h1s) (this K₂.1 $ subset.trans (subset_union_right _ _) h1s)) _, rw [← finset.card_union_eq, finset.filter_union_right], exact s.card_filter_le _, apply finset.disjoint_filter.mpr, rintro g₁ h1g₁ ⟨g₂, h1g₂, h2g₂⟩ ⟨g₃, h1g₃, h2g₃⟩, simp only [mem_preimage] at h1g₃ h1g₂, apply @h g₁⁻¹, split; simp only [set.mem_inv, set.mem_mul, exists_exists_and_eq_and, exists_and_distrib_left], { refine ⟨_, h2g₂, (g₁ * g₂)⁻¹, _, _⟩, simp only [inv_inv, h1g₂], simp only [mul_inv_rev, mul_inv_cancel_left] }, { refine ⟨_, h2g₃, (g₁ * g₃)⁻¹, _, _⟩, simp only [inv_inv, h1g₃], simp only [mul_inv_rev, mul_inv_cancel_left] } end @[to_additive add_left_add_index_le] lemma mul_left_index_le {K : set G} (hK : is_compact K) {V : set G} (hV : (interior V).nonempty) (g : G) : index ((λ h, g * h) '' K) V ≤ index K V := begin rcases index_elim hK hV with ⟨s, h1s, h2s⟩, rw [← h2s], apply nat.Inf_le, rw [mem_image], refine ⟨s.map (equiv.mul_right g⁻¹).to_embedding, _, finset.card_map _⟩, { simp only [mem_set_of_eq], refine subset.trans (image_subset _ h1s) _, rintro _ ⟨g₁, ⟨_, ⟨g₂, rfl⟩, ⟨_, ⟨hg₂, rfl⟩, hg₁⟩⟩, rfl⟩, simp only [mem_preimage] at hg₁, simp only [exists_prop, mem_Union, finset.mem_map, equiv.coe_mul_right, exists_exists_and_eq_and, mem_preimage, equiv.to_embedding_apply], refine ⟨_, hg₂, _⟩, simp only [mul_assoc, hg₁, inv_mul_cancel_left] } end @[to_additive is_left_invariant_add_index] lemma is_left_invariant_index {K : set G} (hK : is_compact K) (g : G) {V : set G} (hV : (interior V).nonempty) : index ((λ h, g * h) '' K) V = index K V := begin refine le_antisymm (mul_left_index_le hK hV g) _, convert mul_left_index_le (hK.image $ continuous_mul_left g) hV g⁻¹, rw [image_image], symmetry, convert image_id' _, ext h, apply inv_mul_cancel_left end /-! ### Lemmas about `prehaar` -/ @[to_additive add_prehaar_le_add_index] lemma prehaar_le_index (K₀ : positive_compacts G) {U : set G} (K : compacts G) (hU : (interior U).nonempty) : prehaar K₀.1 U K ≤ index K.1 K₀.1 := begin unfold prehaar, rw [div_le_iff]; norm_cast, { apply le_index_mul K₀ K hU }, { exact index_pos K₀ hU } end @[to_additive] lemma prehaar_pos (K₀ : positive_compacts G) {U : set G} (hU : (interior U).nonempty) {K : set G} (h1K : is_compact K) (h2K : (interior K).nonempty) : 0 < prehaar K₀.1 U ⟨K, h1K⟩ := by { apply div_pos; norm_cast, apply index_pos ⟨K, h1K, h2K⟩ hU, exact index_pos K₀ hU } @[to_additive] lemma prehaar_mono {K₀ : positive_compacts G} {U : set G} (hU : (interior U).nonempty) {K₁ K₂ : compacts G} (h : K₁.1 ⊆ K₂.1) : prehaar K₀.1 U K₁ ≤ prehaar K₀.1 U K₂ := begin simp only [prehaar], rw [div_le_div_right], exact_mod_cast index_mono K₂.2 h hU, exact_mod_cast index_pos K₀ hU end @[to_additive] lemma prehaar_self {K₀ : positive_compacts G} {U : set G} (hU : (interior U).nonempty) : prehaar K₀.1 U ⟨K₀.1, K₀.2.1⟩ = 1 := by { simp only [prehaar], rw [div_self], apply ne_of_gt, exact_mod_cast index_pos K₀ hU } @[to_additive] lemma prehaar_sup_le {K₀ : positive_compacts G} {U : set G} (K₁ K₂ : compacts G) (hU : (interior U).nonempty) : prehaar K₀.1 U (K₁ ⊔ K₂) ≤ prehaar K₀.1 U K₁ + prehaar K₀.1 U K₂ := begin simp only [prehaar], rw [div_add_div_same, div_le_div_right], exact_mod_cast index_union_le K₁ K₂ hU, exact_mod_cast index_pos K₀ hU end @[to_additive] lemma prehaar_sup_eq {K₀ : positive_compacts G} {U : set G} {K₁ K₂ : compacts G} (hU : (interior U).nonempty) (h : disjoint (K₁.1 * U⁻¹) (K₂.1 * U⁻¹)) : prehaar K₀.1 U (K₁ ⊔ K₂) = prehaar K₀.1 U K₁ + prehaar K₀.1 U K₂ := by { simp only [prehaar], rw [div_add_div_same], congr', exact_mod_cast index_union_eq K₁ K₂ hU h } @[to_additive] lemma is_left_invariant_prehaar {K₀ : positive_compacts G} {U : set G} (hU : (interior U).nonempty) (g : G) (K : compacts G) : prehaar K₀.1 U (K.map _ $ continuous_mul_left g) = prehaar K₀.1 U K := by simp only [prehaar, compacts.map_val, is_left_invariant_index K.2 _ hU] /-! ### Lemmas about `haar_product` -/ @[to_additive] lemma prehaar_mem_haar_product (K₀ : positive_compacts G) {U : set G} (hU : (interior U).nonempty) : prehaar K₀.1 U ∈ haar_product K₀.1 := by { rintro ⟨K, hK⟩ h2K, rw [mem_Icc], exact ⟨prehaar_nonneg K₀ _, prehaar_le_index K₀ _ hU⟩ } @[to_additive] lemma nonempty_Inter_cl_prehaar (K₀ : positive_compacts G) : (haar_product K₀.1 ∩ ⋂ (V : open_nhds_of (1 : G)), cl_prehaar K₀.1 V).nonempty := begin have : is_compact (haar_product K₀.1), { apply is_compact_univ_pi, intro K, apply is_compact_Icc }, refine this.inter_Inter_nonempty (cl_prehaar K₀.1) (λ s, is_closed_closure) (λ t, _), let V₀ := ⋂ (V ∈ t), (V : open_nhds_of 1).1, have h1V₀ : is_open V₀, { apply is_open_bInter, apply finite_mem_finset, rintro ⟨V, hV⟩ h2V, exact hV.1 }, have h2V₀ : (1 : G) ∈ V₀, { simp only [mem_Inter], rintro ⟨V, hV⟩ h2V, exact hV.2 }, refine ⟨prehaar K₀.1 V₀, _⟩, split, { apply prehaar_mem_haar_product K₀, use 1, rwa h1V₀.interior_eq }, { simp only [mem_Inter], rintro ⟨V, hV⟩ h2V, apply subset_closure, apply mem_image_of_mem, rw [mem_set_of_eq], exact ⟨subset.trans (Inter_subset _ ⟨V, hV⟩) (Inter_subset _ h2V), h1V₀, h2V₀⟩ }, end /-! ### Lemmas about `chaar` -/ /-- This is the "limit" of `prehaar K₀.1 U K` as `U` becomes a smaller and smaller open neighborhood of `(1 : G)`. More precisely, it is defined to be an arbitrary element in the intersection of all the sets `cl_prehaar K₀ V` in `haar_product K₀`. This is roughly equal to the Haar measure on compact sets, but it can differ slightly. We do know that `haar_measure K₀ (interior K.1) ≤ chaar K₀ K ≤ haar_measure K₀ K.1`. -/ @[to_additive add_chaar "additive version of `measure_theory.measure.haar.chaar`"] def chaar (K₀ : positive_compacts G) (K : compacts G) : ℝ := classical.some (nonempty_Inter_cl_prehaar K₀) K @[to_additive add_chaar_mem_add_haar_product] lemma chaar_mem_haar_product (K₀ : positive_compacts G) : chaar K₀ ∈ haar_product K₀.1 := (classical.some_spec (nonempty_Inter_cl_prehaar K₀)).1 @[to_additive add_chaar_mem_cl_add_prehaar] lemma chaar_mem_cl_prehaar (K₀ : positive_compacts G) (V : open_nhds_of (1 : G)) : chaar K₀ ∈ cl_prehaar K₀.1 V := by { have := (classical.some_spec (nonempty_Inter_cl_prehaar K₀)).2, rw [mem_Inter] at this, exact this V } @[to_additive add_chaar_nonneg] lemma chaar_nonneg (K₀ : positive_compacts G) (K : compacts G) : 0 ≤ chaar K₀ K := by { have := chaar_mem_haar_product K₀ K (mem_univ _), rw mem_Icc at this, exact this.1 } @[to_additive add_chaar_empty] lemma chaar_empty (K₀ : positive_compacts G) : chaar K₀ ⊥ = 0 := begin let eval : (compacts G → ℝ) → ℝ := λ f, f ⊥, have : continuous eval := continuous_apply ⊥, show chaar K₀ ∈ eval ⁻¹' {(0 : ℝ)}, apply mem_of_subset_of_mem _ (chaar_mem_cl_prehaar K₀ ⟨set.univ, is_open_univ, mem_univ _⟩), unfold cl_prehaar, rw is_closed.closure_subset_iff, { rintro _ ⟨U, ⟨h1U, h2U, h3U⟩, rfl⟩, apply prehaar_empty }, { apply continuous_iff_is_closed.mp this, exact is_closed_singleton }, end @[to_additive add_chaar_self] lemma chaar_self (K₀ : positive_compacts G) : chaar K₀ ⟨K₀.1, K₀.2.1⟩ = 1 := begin let eval : (compacts G → ℝ) → ℝ := λ f, f ⟨K₀.1, K₀.2.1⟩, have : continuous eval := continuous_apply _, show chaar K₀ ∈ eval ⁻¹' {(1 : ℝ)}, apply mem_of_subset_of_mem _ (chaar_mem_cl_prehaar K₀ ⟨set.univ, is_open_univ, mem_univ _⟩), unfold cl_prehaar, rw is_closed.closure_subset_iff, { rintro _ ⟨U, ⟨h1U, h2U, h3U⟩, rfl⟩, apply prehaar_self, rw h2U.interior_eq, exact ⟨1, h3U⟩ }, { apply continuous_iff_is_closed.mp this, exact is_closed_singleton } end @[to_additive add_chaar_mono] lemma chaar_mono {K₀ : positive_compacts G} {K₁ K₂ : compacts G} (h : K₁.1 ⊆ K₂.1) : chaar K₀ K₁ ≤ chaar K₀ K₂ := begin let eval : (compacts G → ℝ) → ℝ := λ f, f K₂ - f K₁, have : continuous eval := (continuous_apply K₂).sub (continuous_apply K₁), rw [← sub_nonneg], show chaar K₀ ∈ eval ⁻¹' (Ici (0 : ℝ)), apply mem_of_subset_of_mem _ (chaar_mem_cl_prehaar K₀ ⟨set.univ, is_open_univ, mem_univ _⟩), unfold cl_prehaar, rw is_closed.closure_subset_iff, { rintro _ ⟨U, ⟨h1U, h2U, h3U⟩, rfl⟩, simp only [mem_preimage, mem_Ici, eval, sub_nonneg], apply prehaar_mono _ h, rw h2U.interior_eq, exact ⟨1, h3U⟩ }, { apply continuous_iff_is_closed.mp this, exact is_closed_Ici }, end @[to_additive add_chaar_sup_le] lemma chaar_sup_le {K₀ : positive_compacts G} (K₁ K₂ : compacts G) : chaar K₀ (K₁ ⊔ K₂) ≤ chaar K₀ K₁ + chaar K₀ K₂ := begin let eval : (compacts G → ℝ) → ℝ := λ f, f K₁ + f K₂ - f (K₁ ⊔ K₂), have : continuous eval := ((@continuous_add ℝ _ _ _).comp ((continuous_apply K₁).prod_mk (continuous_apply K₂))).sub (continuous_apply (K₁ ⊔ K₂)), rw [← sub_nonneg], show chaar K₀ ∈ eval ⁻¹' (Ici (0 : ℝ)), apply mem_of_subset_of_mem _ (chaar_mem_cl_prehaar K₀ ⟨set.univ, is_open_univ, mem_univ _⟩), unfold cl_prehaar, rw is_closed.closure_subset_iff, { rintro _ ⟨U, ⟨h1U, h2U, h3U⟩, rfl⟩, simp only [mem_preimage, mem_Ici, eval, sub_nonneg], apply prehaar_sup_le, rw h2U.interior_eq, exact ⟨1, h3U⟩ }, { apply continuous_iff_is_closed.mp this, exact is_closed_Ici }, end @[to_additive add_chaar_sup_eq] lemma chaar_sup_eq [t2_space G] {K₀ : positive_compacts G} {K₁ K₂ : compacts G} (h : disjoint K₁.1 K₂.1) : chaar K₀ (K₁ ⊔ K₂) = chaar K₀ K₁ + chaar K₀ K₂ := begin rcases compact_compact_separated K₁.2 K₂.2 (disjoint_iff.mp h) with ⟨U₁, U₂, h1U₁, h1U₂, h2U₁, h2U₂, hU⟩, rw [← disjoint_iff_inter_eq_empty] at hU, rcases compact_open_separated_mul K₁.2 h1U₁ h2U₁ with ⟨V₁, h1V₁, h2V₁, h3V₁⟩, rcases compact_open_separated_mul K₂.2 h1U₂ h2U₂ with ⟨V₂, h1V₂, h2V₂, h3V₂⟩, let eval : (compacts G → ℝ) → ℝ := λ f, f K₁ + f K₂ - f (K₁ ⊔ K₂), have : continuous eval := ((@continuous_add ℝ _ _ _).comp ((continuous_apply K₁).prod_mk (continuous_apply K₂))).sub (continuous_apply (K₁ ⊔ K₂)), rw [eq_comm, ← sub_eq_zero], show chaar K₀ ∈ eval ⁻¹' {(0 : ℝ)}, let V := V₁ ∩ V₂, apply mem_of_subset_of_mem _ (chaar_mem_cl_prehaar K₀ ⟨V⁻¹, (is_open.inter h1V₁ h1V₂).preimage continuous_inv, by simp only [mem_inv, one_inv, h2V₁, h2V₂, V, mem_inter_eq, true_and]⟩), unfold cl_prehaar, rw is_closed.closure_subset_iff, { rintro _ ⟨U, ⟨h1U, h2U, h3U⟩, rfl⟩, simp only [mem_preimage, eval, sub_eq_zero, mem_singleton_iff], rw [eq_comm], apply prehaar_sup_eq, { rw h2U.interior_eq, exact ⟨1, h3U⟩ }, { refine disjoint_of_subset _ _ hU, { refine subset.trans (mul_subset_mul subset.rfl _) h3V₁, exact subset.trans (inv_subset.mpr h1U) (inter_subset_left _ _) }, { refine subset.trans (mul_subset_mul subset.rfl _) h3V₂, exact subset.trans (inv_subset.mpr h1U) (inter_subset_right _ _) }}}, { apply continuous_iff_is_closed.mp this, exact is_closed_singleton }, end @[to_additive is_left_invariant_add_chaar] lemma is_left_invariant_chaar {K₀ : positive_compacts G} (g : G) (K : compacts G) : chaar K₀ (K.map _ $ continuous_mul_left g) = chaar K₀ K := begin let eval : (compacts G → ℝ) → ℝ := λ f, f (K.map _ $ continuous_mul_left g) - f K, have : continuous eval := (continuous_apply (K.map _ _)).sub (continuous_apply K), rw [← sub_eq_zero], show chaar K₀ ∈ eval ⁻¹' {(0 : ℝ)}, apply mem_of_subset_of_mem _ (chaar_mem_cl_prehaar K₀ ⟨set.univ, is_open_univ, mem_univ _⟩), unfold cl_prehaar, rw is_closed.closure_subset_iff, { rintro _ ⟨U, ⟨h1U, h2U, h3U⟩, rfl⟩, simp only [mem_singleton_iff, mem_preimage, eval, sub_eq_zero], apply is_left_invariant_prehaar, rw h2U.interior_eq, exact ⟨1, h3U⟩ }, { apply continuous_iff_is_closed.mp this, exact is_closed_singleton }, end variable [t2_space G] /-- The function `chaar` interpreted in `ℝ≥0`, as a content -/ @[to_additive "additive version of `measure_theory.measure.haar.haar_content`"] def haar_content (K₀ : positive_compacts G) : content G := { to_fun := λ K, ⟨chaar K₀ K, chaar_nonneg _ _⟩, mono' := λ K₁ K₂ h, by simp only [←nnreal.coe_le_coe, subtype.coe_mk, chaar_mono, h], sup_disjoint' := λ K₁ K₂ h, by { simp only [chaar_sup_eq h], refl }, sup_le' := λ K₁ K₂, by simp only [←nnreal.coe_le_coe, nnreal.coe_add, subtype.coe_mk, chaar_sup_le] } /-! We only prove the properties for `haar_content` that we use at least twice below. -/ @[to_additive] lemma haar_content_apply (K₀ : positive_compacts G) (K : compacts G) : haar_content K₀ K = show nnreal, from ⟨chaar K₀ K, chaar_nonneg _ _⟩ := rfl /-- The variant of `chaar_self` for `haar_content` -/ @[to_additive] lemma haar_content_self {K₀ : positive_compacts G} : haar_content K₀ ⟨K₀.1, K₀.2.1⟩ = 1 := by { simp_rw [← ennreal.coe_one, haar_content_apply, ennreal.coe_eq_coe, chaar_self], refl } /-- The variant of `is_left_invariant_chaar` for `haar_content` -/ @[to_additive] lemma is_left_invariant_haar_content {K₀ : positive_compacts G} (g : G) (K : compacts G) : haar_content K₀ (K.map _ $ continuous_mul_left g) = haar_content K₀ K := by simpa only [ennreal.coe_eq_coe, ←nnreal.coe_eq, haar_content_apply] using is_left_invariant_chaar g K @[to_additive] lemma haar_content_outer_measure_self_pos {K₀ : positive_compacts G} : 0 < (haar_content K₀).outer_measure K₀.1 := begin apply ennreal.zero_lt_one.trans_le, rw [content.outer_measure_eq_infi], refine le_binfi _, intros U hU, refine le_infi _, intros h2U, refine le_trans (le_of_eq _) (le_bsupr ⟨K₀.1, K₀.2.1⟩ h2U), exact haar_content_self.symm end end haar open haar /-! ### The Haar measure -/ variables [topological_space G] [t2_space G] [topological_group G] [measurable_space G] [borel_space G] /-- The Haar measure on the locally compact group `G`, scaled so that `haar_measure K₀ K₀ = 1`. -/ @[to_additive "The Haar measure on the locally compact additive group `G`, scaled so that `add_haar_measure K₀ K₀ = 1`."] def haar_measure (K₀ : positive_compacts G) : measure G := ((haar_content K₀).outer_measure K₀.1)⁻¹ • (haar_content K₀).measure @[to_additive] lemma haar_measure_apply {K₀ : positive_compacts G} {s : set G} (hs : measurable_set s) : haar_measure K₀ s = (haar_content K₀).outer_measure s / (haar_content K₀).outer_measure K₀.1 := by simp only [haar_measure, hs, div_eq_mul_inv, mul_comm, content.measure_apply, algebra.id.smul_eq_mul, pi.smul_apply, measure.coe_smul] @[to_additive] lemma is_mul_left_invariant_haar_measure (K₀ : positive_compacts G) : is_mul_left_invariant (haar_measure K₀) := begin intros g A hA, rw [haar_measure_apply hA, haar_measure_apply (measurable_const_mul g hA)], congr' 1, apply content.is_mul_left_invariant_outer_measure, apply is_left_invariant_haar_content, end @[to_additive] lemma haar_measure_self [locally_compact_space G] {K₀ : positive_compacts G} : haar_measure K₀ K₀.1 = 1 := begin rw [haar_measure_apply K₀.2.1.measurable_set, ennreal.div_self], { rw [← pos_iff_ne_zero], exact haar_content_outer_measure_self_pos }, { exact ne_of_lt (content.outer_measure_lt_top_of_is_compact _ K₀.2.1) } end @[to_additive] lemma haar_measure_pos_of_is_open [locally_compact_space G] {K₀ : positive_compacts G} {U : set G} (hU : is_open U) (h2U : U.nonempty) : 0 < haar_measure K₀ U := begin rw [haar_measure_apply hU.measurable_set, ennreal.div_pos_iff], refine ⟨_, ne_of_lt $ content.outer_measure_lt_top_of_is_compact _ K₀.2.1⟩, rw [← pos_iff_ne_zero], exact content.outer_measure_pos_of_is_mul_left_invariant _ is_left_invariant_haar_content ⟨K₀.1, K₀.2.1⟩ (by simp only [haar_content_self, ennreal.zero_lt_one]) hU h2U end /-- The Haar measure is regular. -/ @[to_additive] instance regular_haar_measure [locally_compact_space G] {K₀ : positive_compacts G} : (haar_measure K₀).regular := begin apply regular.smul, rw ennreal.inv_lt_top, exact haar_content_outer_measure_self_pos, end section unique variables [locally_compact_space G] [second_countable_topology G] {μ : measure G} [sigma_finite μ] /-- The Haar measure is unique up to scaling. More precisely: every σ-finite left invariant measure is a scalar multiple of the Haar measure. -/ @[to_additive] theorem haar_measure_unique (hμ : is_mul_left_invariant μ) (K₀ : positive_compacts G) : μ = μ K₀.1 • haar_measure K₀ := begin ext1 s hs, have := measure_mul_measure_eq hμ (is_mul_left_invariant_haar_measure K₀) K₀.2.1 hs, rw [haar_measure_self, one_mul] at this, rw [← this (by norm_num), smul_apply], end @[to_additive] theorem regular_of_is_mul_left_invariant (hμ : is_mul_left_invariant μ) {K} (hK : is_compact K) (h2K : (interior K).nonempty) (hμK : μ K < ∞) : regular μ := begin rw [haar_measure_unique hμ ⟨K, hK, h2K⟩], exact regular.smul hμK end end unique end measure end measure_theory
0263285a8dce9384b0a36381d87cbfa66926ce36
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/tactic/norm_cast.lean
662b1f9e06cead3b0a1d58dd0e84cfe0697855e7
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
26,391
lean
/- Copyright (c) 2019 Paul-Nicolas Madelaine. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Paul-Nicolas Madelaine, Robert Y. Lewis -/ import tactic.converter.interactive import tactic.hint /-! # A tactic for normalizing casts inside expressions This tactic normalizes casts inside expressions. It can be thought of as a call to the simplifier with a specific set of lemmas to move casts upwards in the expression. It has special handling of numerals and a simple heuristic to help moving casts "past" binary operators. Contrary to simp, it should be safe to use as a non-terminating tactic. The algorithm implemented here is described in the paper <https://lean-forward.github.io/norm_cast/norm_cast.pdf>. ## Important definitions * `tactic.interactive.norm_cast` * `tactic.interactive.push_cast` * `tactic.interactive.exact_mod_cast` * `tactic.interactive.apply_mod_cast` * `tactic.interactive.rw_mod_cast` * `tactic.interactive.assumption_mod_cast` -/ setup_tactic_parser namespace tactic /-- Runs `mk_instance` with a time limit. This is a work around to the fact that in some cases mk_instance times out instead of failing, for example: `has_lift_t ℤ ℕ` `mk_instance_fast` is used when we assume the type class search should end instantly. -/ meta def mk_instance_fast (e : expr) (timeout := 1000) : tactic expr := try_for timeout (mk_instance e) end tactic namespace norm_cast open tactic expr declare_trace norm_cast /-- Output a trace message if `trace.norm_cast` is enabled. -/ meta def trace_norm_cast {α} [has_to_tactic_format α] (msg : string) (a : α) : tactic unit := when_tracing `norm_cast $ do a ← pp a, trace ("[norm_cast] " ++ msg ++ a : format) mk_simp_attribute push_cast "The `push_cast` simp attribute uses `norm_cast` lemmas to move casts toward the leaf nodes of the expression." /-- `label` is a type used to classify `norm_cast` lemmas. * elim lemma: LHS has 0 head coes and ≥ 1 internal coe * move lemma: LHS has 1 head coe and 0 internal coes, RHS has 0 head coes and ≥ 1 internal coes * squash lemma: LHS has ≥ 1 head coes and 0 internal coes, RHS has fewer head coes -/ @[derive [decidable_eq, has_reflect, inhabited]] inductive label | elim : label | move : label | squash : label namespace label /-- Convert `label` into `string`. -/ protected def to_string : label → string | elim := "elim" | move := "move" | squash := "squash" instance : has_to_string label := ⟨label.to_string⟩ instance : has_repr label := ⟨label.to_string⟩ meta instance : has_to_format label := ⟨λ l, l.to_string⟩ /-- Convert `string` into `label`. -/ def of_string : string -> option label | "elim" := some elim | "move" := some move | "squash" := some squash | _ := none end label open label /-- Count how many coercions are at the top of the expression. -/ meta def count_head_coes : expr → ℕ | `(coe %%e) := count_head_coes e + 1 | `(coe_sort %%e) := count_head_coes e + 1 | `(coe_fn %%e) := count_head_coes e + 1 | _ := 0 /-- Count how many coercions are inside the expression, including the top ones. -/ meta def count_coes : expr → tactic ℕ | `(coe %%e) := (+1) <$> count_coes e | `(coe_sort %%e) := (+1) <$> count_coes e | `(coe_fn %%e) := (+1) <$> count_coes e | (app `(coe_fn %%e) x) := (+) <$> count_coes x <*> (+1) <$> count_coes e | (expr.lam n bi t e) := do l ← mk_local' n bi t, count_coes $ e.instantiate_var l | e := do as ← e.get_simp_args, list.sum <$> as.mmap count_coes /-- Count how many coercions are inside the expression, excluding the top ones. -/ private meta def count_internal_coes (e : expr) : tactic ℕ := do ncoes ← count_coes e, pure $ ncoes - count_head_coes e /-- Classifies a declaration of type `ty` as a `norm_cast` rule. -/ meta def classify_type (ty : expr) : tactic label := do (_, ty) ← open_pis ty, (lhs, rhs) ← match ty with | `(%%lhs = %%rhs) := pure (lhs, rhs) | `(%%lhs ↔ %%rhs) := pure (lhs, rhs) | _ := fail "norm_cast: lemma must be = or ↔" end, lhs_coes ← count_coes lhs, when (lhs_coes = 0) $ fail "norm_cast: badly shaped lemma, lhs must contain at least one coe", let lhs_head_coes := count_head_coes lhs, lhs_internal_coes ← count_internal_coes lhs, let rhs_head_coes := count_head_coes rhs, rhs_internal_coes ← count_internal_coes rhs, if lhs_head_coes = 0 then return elim else if lhs_head_coes = 1 then do when (rhs_head_coes ≠ 0) $ fail "norm_cast: badly shaped lemma, rhs can't start with coe", if rhs_internal_coes = 0 then return squash else return move else if rhs_head_coes < lhs_head_coes then do return squash else do fail "norm_cast: badly shaped shaped squash lemma, rhs must have fewer head coes than lhs" /-- The cache for `norm_cast` attribute stores three `simp_lemma` objects. -/ meta structure norm_cast_cache := (up : simp_lemmas) (down : simp_lemmas) (squash : simp_lemmas) /-- Empty `norm_cast_cache`. -/ meta def empty_cache : norm_cast_cache := { up := simp_lemmas.mk, down := simp_lemmas.mk, squash := simp_lemmas.mk, } meta instance : inhabited norm_cast_cache := ⟨empty_cache⟩ /-- `add_elim cache e` adds `e` as an `elim` lemma to `cache`. -/ meta def add_elim (cache : norm_cast_cache) (e : expr) : tactic norm_cast_cache := do new_up ← simp_lemmas.add cache.up e, return { up := new_up, down := cache.down, squash := cache.squash, } /-- `add_move cache e` adds `e` as a `move` lemma to `cache`. -/ meta def add_move (cache : norm_cast_cache) (e : expr) : tactic norm_cast_cache := do ty ← infer_type e, new_up ← cache.up.add e tt, new_down ← simp_lemmas.add cache.down e, return { up := new_up, down := new_down, squash := cache.squash, } /-- `add_squash cache e` adds `e` as an `squash` lemma to `cache`. -/ meta def add_squash (cache : norm_cast_cache) (e : expr) : tactic norm_cast_cache := do new_squash ← simp_lemmas.add cache.squash e, new_down ← simp_lemmas.add cache.down e, return { up := cache.up, down := new_down, squash := new_squash, } /-- The type of the `norm_cast` attribute. The optional label is used to overwrite the classifier. -/ meta def norm_cast_attr_ty : Type := user_attribute norm_cast_cache (option label) /-- Efficient getter for the `@[norm_cast]` attribute parameter that does not call `eval_expr`. See Note [user attribute parameters]. -/ meta def get_label_param (attr : norm_cast_attr_ty) (decl : name) : tactic (option label) := do p ← attr.get_param_untyped decl, match p with | `(none) := pure none | `(some label.elim) := pure label.elim | `(some label.move) := pure label.move | `(some label.squash) := pure label.squash | _ := fail p end /-- `add_lemma cache decl` infers the proper `norm_cast` attribute for `decl` and adds it to `cache`. -/ meta def add_lemma (attr : norm_cast_attr_ty) (cache : norm_cast_cache) (decl : name) : tactic norm_cast_cache := do e ← mk_const decl, param ← get_label_param attr decl, l ← param <|> (infer_type e >>= classify_type), match l with | elim := add_elim cache e | move := add_move cache e | squash := add_squash cache e end -- special lemmas to handle the ≥, > and ≠ operators private lemma ge_from_le {α} [has_le α] : ∀ (x y : α), x ≥ y ↔ y ≤ x := λ _ _, iff.rfl private lemma gt_from_lt {α} [has_lt α] : ∀ (x y : α), x > y ↔ y < x := λ _ _, iff.rfl private lemma ne_from_not_eq {α} : ∀ (x y : α), x ≠ y ↔ ¬(x = y) := λ _ _, iff.rfl /-- `mk_cache names` creates a `norm_cast_cache`. It infers the proper `norm_cast` attributes for names in `names`, and collects the lemmas attributed with specific `norm_cast` attributes. -/ meta def mk_cache (attr : thunk norm_cast_attr_ty) (names : list name) : tactic norm_cast_cache := do -- names has the declarations in reverse order cache ← names.mfoldr (λ name cache, add_lemma (attr ()) cache name) empty_cache, --some special lemmas to handle binary relations let up := cache.up, up ← up.add_simp ``ge_from_le, up ← up.add_simp ``gt_from_lt, up ← up.add_simp ``ne_from_not_eq, let down := cache.down, down ← down.add_simp ``coe_coe, pure { up := up, down := down, squash := cache.squash } /-- The `norm_cast` attribute. -/ @[user_attribute] meta def norm_cast_attr : user_attribute norm_cast_cache (option label) := { name := `norm_cast, descr := "attribute for norm_cast", parser := (do some l ← (label.of_string ∘ to_string) <$> ident, return l) <|> return none, after_set := some (λ decl prio persistent, do param ← get_label_param norm_cast_attr decl, match param with | some l := when (l ≠ elim) $ simp_attr.push_cast.set decl () tt | none := do e ← mk_const decl, ty ← infer_type e, l ← classify_type ty, norm_cast_attr.set decl l persistent prio end), before_unset := some $ λ _ _, tactic.skip, cache_cfg := { mk_cache := mk_cache norm_cast_attr, dependencies := [] } } /-- Classify a declaration as a `norm_cast` rule. -/ meta def make_guess (decl : name) : tactic label := do e ← mk_const decl, ty ← infer_type e, classify_type ty /-- Gets the `norm_cast` classification label for a declaration. Applies the override specified on the attribute, if necessary. -/ meta def get_label (decl : name) : tactic label := do param ← get_label_param norm_cast_attr decl, param <|> make_guess decl end norm_cast namespace tactic.interactive open norm_cast /-- `push_cast` rewrites the expression to move casts toward the leaf nodes. For example, `↑(a + b)` will be written to `↑a + ↑b`. Equivalent to `simp only with push_cast`. Can also be used at hypotheses. `push_cast` can also be used at hypotheses and with extra simp rules. ```lean example (a b : ℕ) (h1 : ((a + b : ℕ) : ℤ) = 10) (h2 : ((a + b + 0 : ℕ) : ℤ) = 10) : ((a + b : ℕ) : ℤ) = 10 := begin push_cast, push_cast at h1, push_cast [int.add_zero] at h2, end ``` -/ meta def push_cast (hs : parse tactic.simp_arg_list) (l : parse location) : tactic unit := tactic.interactive.simp none none tt hs [`push_cast] l end tactic.interactive namespace norm_cast open tactic expr /-- Prove `a = b` using the given simp set. -/ meta def prove_eq_using (s : simp_lemmas) (a b : expr) : tactic expr := do (a', a_a', _) ← simplify s [] a {fail_if_unchanged := ff}, (b', b_b', _) ← simplify s [] b {fail_if_unchanged := ff}, on_exception (trace_norm_cast "failed: " (to_expr ``(%%a' = %%b') >>= pp)) $ is_def_eq a' b' reducible, b'_b ← mk_eq_symm b_b', mk_eq_trans a_a' b'_b /-- Prove `a = b` by simplifying using move and squash lemmas. -/ meta def prove_eq_using_down (a b : expr) : tactic expr := do cache ← norm_cast_attr.get_cache, trace_norm_cast "proving: " (to_expr ``(%%a = %%b) >>= pp), prove_eq_using cache.down a b /-- This is the main heuristic used alongside the elim and move lemmas. The goal is to help casts move past operators by adding intermediate casts. An expression of the shape: op (↑(x : α) : γ) (↑(y : β) : γ) is rewritten to: op (↑(↑(x : α) : β) : γ) (↑(y : β) : γ) when (↑(↑(x : α) : β) : γ) = (↑(x : α) : γ) can be proven with a squash lemma -/ meta def splitting_procedure : expr → tactic (expr × expr) | (app (app op x) y) := (do `(@coe %%α %%δ %%coe1 %%xx) ← return x, `(@coe %%β %%γ %%coe2 %%yy) ← return y, success_if_fail $ is_def_eq α β, is_def_eq δ γ, (do coe3 ← mk_app `has_lift_t [α, β] >>= mk_instance_fast, new_x ← to_expr ``(@coe %%β %%δ %%coe2 (@coe %%α %%β %%coe3 %%xx)), let new_e := app (app op new_x) y, eq_x ← prove_eq_using_down x new_x, pr ← mk_congr_arg op eq_x, pr ← mk_congr_fun pr y, return (new_e, pr) ) <|> (do coe3 ← mk_app `has_lift_t [β, α] >>= mk_instance_fast, new_y ← to_expr ``(@coe %%α %%δ %%coe1 (@coe %%β %%α %%coe3 %%yy)), let new_e := app (app op x) new_y, eq_y ← prove_eq_using_down y new_y, pr ← mk_congr_arg (app op x) eq_y, return (new_e, pr) ) ) <|> (do `(@coe %%α %%β %%coe1 %%xx) ← return x, `(@has_one.one %%β %%h1) ← return y, h2 ← to_expr ``(has_one %%α) >>= mk_instance_fast, new_y ← to_expr ``(@coe %%α %%β %%coe1 (@has_one.one %%α %%h2)), eq_y ← prove_eq_using_down y new_y, let new_e := app (app op x) new_y, pr ← mk_congr_arg (app op x) eq_y, return (new_e, pr) ) <|> (do `(@coe %%α %%β %%coe1 %%xx) ← return x, `(@has_zero.zero %%β %%h1) ← return y, h2 ← to_expr ``(has_zero %%α) >>= mk_instance_fast, new_y ← to_expr ``(@coe %%α %%β %%coe1 (@has_zero.zero %%α %%h2)), eq_y ← prove_eq_using_down y new_y, let new_e := app (app op x) new_y, pr ← mk_congr_arg (app op x) eq_y, return (new_e, pr) ) <|> (do `(@has_one.one %%β %%h1) ← return x, `(@coe %%α %%β %%coe1 %%xx) ← return y, h1 ← to_expr ``(has_one %%α) >>= mk_instance_fast, new_x ← to_expr ``(@coe %%α %%β %%coe1 (@has_one.one %%α %%h1)), eq_x ← prove_eq_using_down x new_x, let new_e := app (app op new_x) y, pr ← mk_congr_arg (lam `x binder_info.default β (app (app op (var 0)) y)) eq_x, return (new_e, pr) ) <|> (do `(@has_zero.zero %%β %%h1) ← return x, `(@coe %%α %%β %%coe1 %%xx) ← return y, h1 ← to_expr ``(has_zero %%α) >>= mk_instance_fast, new_x ← to_expr ``(@coe %%α %%β %%coe1 (@has_zero.zero %%α %%h1)), eq_x ← prove_eq_using_down x new_x, let new_e := app (app op new_x) y, pr ← mk_congr_arg (lam `x binder_info.default β (app (app op (var 0)) y)) eq_x, return (new_e, pr) ) | _ := failed /-- Discharging function used during simplification in the "squash" step. TODO: norm_cast takes a list of expressions to use as lemmas for the discharger TODO: a tactic to print the results the discharger fails to proove -/ private meta def prove : tactic unit := assumption /-- Core rewriting function used in the "squash" step, which moves casts upwards and eliminates them. It tries to rewrite an expression using the elim and move lemmas. On failure, it calls the splitting procedure heuristic. -/ meta def upward_and_elim (s : simp_lemmas) (e : expr) : tactic (expr × expr) := (do r ← mcond (is_prop e) (return `iff) (return `eq), (new_e, pr) ← s.rewrite e prove r, pr ← match r with | `iff := mk_app `propext [pr] | _ := return pr end, return (new_e, pr) ) <|> splitting_procedure e /-! The following auxiliary functions are used to handle numerals. -/ /-- If possible, rewrite `(n : α)` to `((n : ℕ) : α)` where `n` is a numeral and `α ≠ ℕ`. Returns a pair of the new expression and proof that they are equal. -/ meta def numeral_to_coe (e : expr) : tactic (expr × expr) := do α ← infer_type e, success_if_fail $ is_def_eq α `(ℕ), n ← e.to_nat, h1 ← mk_app `has_lift_t [`(ℕ), α] >>= mk_instance_fast, let new_e : expr := reflect n, new_e ← to_expr ``(@coe ℕ %%α %%h1 %%new_e), pr ← prove_eq_using_down e new_e, return (new_e, pr) /-- If possible, rewrite `((n : ℕ) : α)` to `(n : α)` where `n` is a numeral. Returns a pair of the new expression and proof that they are equal. -/ meta def coe_to_numeral (e : expr) : tactic (expr × expr) := do `(@coe ℕ %%α %%h1 %%e') ← return e, n ← e'.to_nat, -- replace e' by normalized numeral is_def_eq (reflect n) e' reducible, let e := e.app_fn (reflect n), new_e ← expr.of_nat α n, pr ← prove_eq_using_down e new_e, return (new_e, pr) /-- A local variant on `simplify_top_down`. -/ private meta def simplify_top_down' {α} (a : α) (pre : α → expr → tactic (α × expr × expr)) (e : expr) (cfg : simp_config := {}) : tactic (α × expr × expr) := ext_simplify_core a cfg simp_lemmas.mk (λ _, failed) (λ a _ _ _ e, do (new_a, new_e, pr) ← pre a e, guard (¬ new_e =ₐ e), return (new_a, new_e, some pr, ff)) (λ _ _ _ _ _, failed) `eq e /-- The core simplification routine of `norm_cast`. -/ meta def derive (e : expr) : tactic (expr × expr) := do cache ← norm_cast_attr.get_cache, e ← instantiate_mvars e, let cfg : simp_config := { zeta := ff, beta := ff, eta := ff, proj := ff, iota := ff, iota_eqn := ff, fail_if_unchanged := ff }, let e0 := e, -- step 1: pre-processing of numerals ((), e1, pr1) ← simplify_top_down' () (λ _ e, prod.mk () <$> numeral_to_coe e) e0 cfg, trace_norm_cast "after numeral_to_coe: " e1, -- step 2: casts are moved upwards and eliminated ((), e2, pr2) ← simplify_bottom_up () (λ _ e, prod.mk () <$> upward_and_elim cache.up e) e1 cfg, trace_norm_cast "after upward_and_elim: " e2, -- step 3: casts are squashed (e3, pr3, _) ← simplify cache.squash [] e2 cfg, trace_norm_cast "after squashing: " e3, -- step 4: post-processing of numerals ((), e4, pr4) ← simplify_top_down' () (λ _ e, prod.mk () <$> coe_to_numeral e) e3 cfg, trace_norm_cast "after coe_to_numeral: " e4, let new_e := e4, guard (¬ new_e =ₐ e), pr ← mk_eq_trans pr1 pr2, pr ← mk_eq_trans pr pr3, pr ← mk_eq_trans pr pr4, return (new_e, pr) /-- A small variant of `push_cast` suited for non-interactive use. `derive_push_cast extra_lems e` returns an expression `e'` and a proof that `e = e'`. -/ meta def derive_push_cast (extra_lems : list simp_arg_type) (e : expr) : tactic (expr × expr) := do (s, _) ← mk_simp_set tt [`push_cast] extra_lems, (e, prf, _) ← simplify (s.erase [`int.coe_nat_succ]) [] e {fail_if_unchanged := ff}, return (e, prf) end norm_cast namespace tactic open expr norm_cast /-- `aux_mod_cast e` runs `norm_cast` on `e` and returns the result. If `include_goal` is true, it also normalizes the goal. -/ meta def aux_mod_cast (e : expr) (include_goal : bool := tt) : tactic expr := match e with | local_const _ lc _ _ := do e ← get_local lc, replace_at derive [e] include_goal, get_local lc | e := do t ← infer_type e, e ← assertv `this t e, replace_at derive [e] include_goal, get_local `this end /-- `exact_mod_cast e` runs `norm_cast` on the goal and `e`, and tries to use `e` to close the goal. -/ meta def exact_mod_cast (e : expr) : tactic unit := decorate_error "exact_mod_cast failed:" $ do new_e ← aux_mod_cast e, exact new_e /-- `apply_mod_cast e` runs `norm_cast` on the goal and `e`, and tries to apply `e`. -/ meta def apply_mod_cast (e : expr) : tactic (list (name × expr)) := decorate_error "apply_mod_cast failed:" $ do new_e ← aux_mod_cast e, apply new_e /-- `assumption_mod_cast` runs `norm_cast` on the goal. For each local hypothesis `h`, it also normalizes `h` and tries to use that to close the goal. -/ meta def assumption_mod_cast : tactic unit := decorate_error "assumption_mod_cast failed:" $ do let cfg : simp_config := { fail_if_unchanged := ff, canonize_instances := ff, canonize_proofs := ff, proj := ff }, replace_at derive [] tt, ctx ← local_context, ctx.mfirst (λ h, aux_mod_cast h ff >>= tactic.exact) end tactic namespace tactic.interactive open tactic norm_cast /-- Normalize casts at the given locations by moving them "upwards". As opposed to simp, norm_cast can be used without necessarily closing the goal. -/ meta def norm_cast (loc : parse location) : tactic unit := do ns ← loc.get_locals, tt ← replace_at derive ns loc.include_goal | fail "norm_cast failed to simplify", when loc.include_goal $ try tactic.reflexivity, when loc.include_goal $ try tactic.triv, when (¬ ns.empty) $ try tactic.contradiction /-- Rewrite with the given rules and normalize casts between steps. -/ meta def rw_mod_cast (rs : parse rw_rules) (loc : parse location) : tactic unit := decorate_error "rw_mod_cast failed:" $ do let cfg_norm : simp_config := {}, let cfg_rw : rewrite_cfg := {}, ns ← loc.get_locals, monad.mapm' (λ r : rw_rule, do save_info r.pos, replace_at derive ns loc.include_goal, rw ⟨[r], none⟩ loc {} ) rs.rules, replace_at derive ns loc.include_goal, skip /-- Normalize the goal and the given expression, then close the goal with exact. -/ meta def exact_mod_cast (e : parse texpr) : tactic unit := do e ← i_to_expr e <|> do { ty ← target, e ← i_to_expr_strict ``(%%e : %%ty), pty ← pp ty, ptgt ← pp e, fail ("exact_mod_cast failed, expression type not directly " ++ "inferrable. Try:\n\nexact_mod_cast ...\nshow " ++ to_fmt pty ++ ",\nfrom " ++ ptgt : format) }, tactic.exact_mod_cast e /-- Normalize the goal and the given expression, then apply the expression to the goal. -/ meta def apply_mod_cast (e : parse texpr) : tactic unit := do e ← i_to_expr_for_apply e, concat_tags $ tactic.apply_mod_cast e /-- Normalize the goal and every expression in the local context, then close the goal with assumption. -/ meta def assumption_mod_cast : tactic unit := tactic.assumption_mod_cast end tactic.interactive namespace conv.interactive open conv open norm_cast (derive) /-- the converter version of `norm_cast' -/ meta def norm_cast : conv unit := replace_lhs derive end conv.interactive -- TODO: move this elsewhere? @[norm_cast] lemma ite_cast {α β} [has_lift_t α β] {c : Prop} [decidable c] {a b : α} : ↑(ite c a b) = ite c (↑a : β) (↑b : β) := by by_cases h : c; simp [h] @[norm_cast] lemma dite_cast {α β} [has_lift_t α β] {c : Prop} [decidable c] {a : c → α} {b : ¬ c → α} : ↑(dite c a b) = dite c (λ h, (↑(a h) : β)) (λ h, (↑(b h) : β)) := by by_cases h : c; simp [h] add_hint_tactic "norm_cast at *" /-- The `norm_cast` family of tactics is used to normalize casts inside expressions. It is basically a simp tactic with a specific set of lemmas to move casts upwards in the expression. Therefore it can be used more safely as a non-terminating tactic. It also has special handling of numerals. For instance, given an assumption ```lean a b : ℤ h : ↑a + ↑b < (10 : ℚ) ``` writing `norm_cast at h` will turn `h` into ```lean h : a + b < 10 ``` You can also use `exact_mod_cast`, `apply_mod_cast`, `rw_mod_cast` or `assumption_mod_cast`. Writing `exact_mod_cast h` and `apply_mod_cast h` will normalize the goal and `h` before using `exact h` or `apply h`. Writing `assumption_mod_cast` will normalize the goal and for every expression `h` in the context it will try to normalize `h` and use `exact h`. `rw_mod_cast` acts like the `rw` tactic but it applies `norm_cast` between steps. `push_cast` rewrites the expression to move casts toward the leaf nodes. This uses `norm_cast` lemmas in the forward direction. For example, `↑(a + b)` will be written to `↑a + ↑b`. It is equivalent to `simp only with push_cast`. It can also be used at hypotheses with `push_cast at h` and with extra simp lemmas with `push_cast [int.add_zero]`. ```lean example (a b : ℕ) (h1 : ((a + b : ℕ) : ℤ) = 10) (h2 : ((a + b + 0 : ℕ) : ℤ) = 10) : ((a + b : ℕ) : ℤ) = 10 := begin push_cast, push_cast at h1, push_cast [int.add_zero] at h2, end ``` The implementation and behavior of the `norm_cast` family is described in detail at <https://lean-forward.github.io/norm_cast/norm_cast.pdf>. -/ add_tactic_doc { name := "norm_cast", category := doc_category.tactic, decl_names := [``tactic.interactive.norm_cast, ``tactic.interactive.rw_mod_cast, ``tactic.interactive.apply_mod_cast, ``tactic.interactive.assumption_mod_cast, ``tactic.interactive.exact_mod_cast, ``tactic.interactive.push_cast], tags := ["coercions", "simplification"] } /-- The `norm_cast` attribute should be given to lemmas that describe the behaviour of a coercion in regard to an operator, a relation, or a particular function. It only concerns equality or iff lemmas involving `↑`, `⇑` and `↥`, describing the behavior of the coercion functions. It does not apply to the explicit functions that define the coercions. Examples: ```lean @[norm_cast] theorem coe_nat_inj' {m n : ℕ} : (↑m : ℤ) = ↑n ↔ m = n @[norm_cast] theorem coe_int_denom (n : ℤ) : (n : ℚ).denom = 1 @[norm_cast] theorem cast_id : ∀ n : ℚ, ↑n = n @[norm_cast] theorem coe_nat_add (m n : ℕ) : (↑(m + n) : ℤ) = ↑m + ↑n @[norm_cast] theorem cast_sub [add_group α] [has_one α] {m n} (h : m ≤ n) : ((n - m : ℕ) : α) = n - m @[norm_cast] theorem coe_nat_bit0 (n : ℕ) : (↑(bit0 n) : ℤ) = bit0 ↑n @[norm_cast] theorem cast_coe_nat (n : ℕ) : ((n : ℤ) : α) = n @[norm_cast] theorem cast_one : ((1 : ℚ) : α) = 1 ``` Lemmas tagged with `@[norm_cast]` are classified into three categories: `move`, `elim`, and `squash`. They are classified roughly as follows: * elim lemma: LHS has 0 head coes and ≥ 1 internal coe * move lemma: LHS has 1 head coe and 0 internal coes, RHS has 0 head coes and ≥ 1 internal coes * squash lemma: LHS has ≥ 1 head coes and 0 internal coes, RHS has fewer head coes `norm_cast` uses `move` and `elim` lemmas to factor coercions toward the root of an expression and to cancel them from both sides of an equation or relation. It uses `squash` lemmas to clean up the result. Occasionally you may want to override the automatic classification. You can do this by giving an optional `elim`, `move`, or `squash` parameter to the attribute. ```lean @[simp, norm_cast elim] lemma nat_cast_re (n : ℕ) : (n : ℂ).re = n := by rw [← of_real_nat_cast, of_real_re] ``` Don't do this unless you understand what you are doing. A full description of the tactic, and the use of each lemma category, can be found at <https://lean-forward.github.io/norm_cast/norm_cast.pdf>. -/ add_tactic_doc { name := "norm_cast attributes", category := doc_category.attr, decl_names := [``norm_cast.norm_cast_attr], tags := ["coercions", "simplification"] } -- Lemmas defined in core. attribute [norm_cast] int.nat_abs_of_nat int.coe_nat_sub int.coe_nat_mul int.coe_nat_zero int.coe_nat_one int.coe_nat_add -- Lemmas about nat.succ need to get a low priority, so that they are tried last. -- This is because `nat.succ _` matches `1`, `3`, `x+1`, etc. -- Rewriting would then produce really wrong terms. attribute [norm_cast, priority 500] int.coe_nat_succ
9a7aa1c5d6b002b409a676ceb1cdeefd6b4747db
097294e9b80f0d9893ac160b9c7219aa135b51b9
/instructor/objects/nat.lean
dde67352f94d5eb2090348c471b8430494aafe73
[]
no_license
AbigailCastro17/CS2102-Discrete-Math
cf296251be9418ce90206f5e66bde9163e21abf9
d741e4d2d6a9b2e0c8380e51706218b8f608cee4
refs/heads/main
1,682,891,087,358
1,621,401,341,000
1,621,401,341,000
368,749,959
0
0
null
null
null
null
UTF-8
Lean
false
false
558
lean
-- Natural numbers -- data type #eval nat.zero #eval (nat.succ nat.zero) #eval nat.succ (nat.succ nat.zero) -- notation #eval 0 #eval 1 #eval 2 -- operators #eval nat.add 1 2 -- addition #eval nat.mul 2 3 -- multiplication #eval nat.pow 2 3 -- exponentiation #eval nat.sub 4 1 -- subtraction #eval nat.div 4 3 -- quotient #eval nat.mod 4 3 -- remainder -- notation #eval 1 + 2 #eval 2 * 3 #eval 4 - 1 #eval 4 / 3 #eval 4 % 3 -- a theorem and its already Lean-defined proof def add_commutes : ∀ (n m : ℕ), n + m = m + n := nat.add_comm
e72174ab8a418270c542a9bced034089de6df338
c45b34bfd44d8607a2e8762c926e3cfaa7436201
/uexp/src/uexp/rules/mergeJoinFilter.lean
7a8d97770a5d602f0781f094ba7e15d8668de878
[ "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,567
lean
import ..sql import ..tactics import ..u_semiring import ..extra_constants import ..ucongr import ..TDP set_option profiler true open Expr open Proj open Pred open SQL open tree notation `int` := datatypes.int variable integer_10: const datatypes.int theorem rule: forall ( Γ scm_dept scm_emp: Schema) (rel_dept: relation scm_dept) (rel_emp: relation scm_emp) (dept_deptno : Column int scm_dept) (dept_name : Column int scm_dept) (emp_empno : Column int scm_emp) (emp_ename : Column int scm_emp) (emp_job : Column int scm_emp) (emp_mgr : Column int scm_emp) (emp_hiredate : Column int scm_emp) (emp_comm : Column int scm_emp) (emp_sal : Column int scm_emp) (emp_deptno : Column int scm_emp) (emp_slacker : Column int scm_emp), denoteSQL ((SELECT * FROM1 ((SELECT1 (combine (right⋅right⋅dept_deptno) (right⋅left⋅emp_ename)) FROM1 (product (table rel_emp) (table rel_dept)) WHERE (and (equal (uvariable (right⋅left⋅emp_deptno)) (uvariable (right⋅right⋅dept_deptno))) (equal (uvariable (right⋅right⋅dept_deptno)) (constantExpr integer_10))))) WHERE (equal (uvariable (right⋅left)) (constantExpr integer_10))) :SQL Γ _) = denoteSQL ((SELECT1 (combine (right⋅right⋅dept_deptno) (right⋅left⋅emp_ename)) FROM1 (product (table rel_emp) ((SELECT * FROM1 (table rel_dept) WHERE (equal (uvariable (right⋅dept_deptno)) (constantExpr integer_10))))) WHERE (equal (uvariable (right⋅left⋅emp_deptno)) (uvariable (right⋅right⋅dept_deptno)))) :SQL Γ _) := begin intros, unfold_all_denotations, funext, simp, TDP' ucongr, end
41c5824d7345d98d12799a52c4dd61481d7973e9
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/test/fin_cases.lean
eaadb04f31273c2a45489bb2524ff329917647c3
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
3,167
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import data.nat.interval import data.nat.prime import group_theory.perm.sign import tactic.fin_cases example (f : ℕ → Prop) (p : fin 3) (h0 : f 0) (h1 : f 1) (h2 : f 2) : f p.val := begin fin_cases *, simp, assumption, simp, assumption, simp, assumption, end example (f : ℕ → Prop) (p : fin 0) : f p.val := by fin_cases * example (f : ℕ → Prop) (p : fin 1) (h : f 0) : f p.val := begin fin_cases p, assumption end example (x2 : fin 2) (x3 : fin 3) (n : nat) (y : fin n) : x2.val * x3.val = x3.val * x2.val := begin fin_cases x2; fin_cases x3, success_if_fail { fin_cases * }, success_if_fail { fin_cases y }, all_goals { refl }, end open finset example (x : ℕ) (h : x ∈ Ico 2 5) : x = 2 ∨ x = 3 ∨ x = 4 := begin fin_cases h, all_goals { simp } end open nat example (x : ℕ) (h : x ∈ [2,3,5,7]) : x = 2 ∨ x = 3 ∨ x = 5 ∨ x = 7 := begin fin_cases h, all_goals { simp } end example (x : ℕ) (h : x ∈ [2,3,5,7]) : true := begin success_if_fail { fin_cases h with [3,3,5,7] }, trivial end example (x : list ℕ) (h : x ∈ [[1],[2]]) : x.length = 1 := begin fin_cases h with [[1],[1+1]], simp, guard_target (list.length [1 + 1] = 1), simp end -- testing that `with` arguments are elaborated with respect to the expected type: example (x : ℤ) (h : x ∈ ([2,3] : list ℤ)) : x = 2 ∨ x = 3 := begin fin_cases h with [2,3], all_goals { simp } end instance (n : ℕ) : decidable (nat.prime n) := decidable_prime_1 n example (x : ℕ) (h : x ∈ (range 10).filter nat.prime) : x = 2 ∨ x = 3 ∨ x = 5 ∨ x = 7 := begin fin_cases h; exact dec_trivial end open equiv.perm example (x : (Σ (a : fin 4), fin 4)) (h : x ∈ fin_pairs_lt 4) : x.1.val < 4 := begin fin_cases h; simp, any_goals { exact dec_trivial }, end example (x : fin 3) : x.val < 5 := begin fin_cases x; exact dec_trivial end example (f : ℕ → Prop) (p : fin 3) (h0 : f 0) (h1 : f 1) (h2 : f 2) : f p.val := begin fin_cases *, all_goals { assumption } end example (n : ℕ) (h : n % 3 ∈ [0,1]) : true := begin fin_cases h, guard_hyp h : n % 3 = 0, trivial, guard_hyp h : n % 3 = 1, trivial, end /- In some circumstances involving `let`, the temporary hypothesis that `fin_cases` creates does not get deleted. We test that this is correctly named and that the name can be changed. -/ example (f : ℕ → fin 3) : true := begin let a := f 3, fin_cases a, guard_hyp a := f 3, guard_hyp this : a = (0 : fin 3), trivial, trivial, let b := f 2, fin_cases b using what, guard_hyp what : b = (0 : fin 3), all_goals {trivial} end /- The behavior above can be worked around with `fin_cases with`. -/ example (f : ℕ → fin 3) : true := begin let a := f 3, fin_cases a with [0, 1, 2], guard_hyp a := f 3, guard_hyp this : a = 0, trivial, guard_hyp this : a = 1, trivial, guard_hyp this : a = 2, let b := f 2, fin_cases b with [0, 1, 2] using what, guard_hyp what : b = 0, all_goals {trivial} end
93b1e6dbfb57fbe80b18f8abcd29038ea5a987f1
9028d228ac200bbefe3a711342514dd4e4458bff
/src/algebra/continued_fractions/computation/approximations.lean
5109a9e78b6526b556016d4e23dfb7cfaa2e1bd6
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,578
lean
/- Copyright (c) 2020 Kevin Kappelmann. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Kappelmann -/ import algebra.continued_fractions.computation.translations import algebra.continued_fractions.continuants_recurrence import algebra.continued_fractions.terminated_stable import data.nat.fib /-! # Approximations for Continued Fraction Computations (`gcf.of`) ## Summary Let us write `gcf` for `generalized_continued_fraction`. This file contains useful approximations for the values involved in the continued fractions computation `gcf.of`. ## Main Theorems - `gcf.of_part_num_eq_one`: shows that all partial numerators `aᵢ` are equal to one. - `gcf.exists_int_eq_of_part_denom`: shows that all partial denominators `bᵢ` correspond to an integer. - `gcf.one_le_of_nth_part_denom`: shows that `1 ≤ bᵢ`. - `gcf.succ_nth_fib_le_of_nth_denom`: shows that the `n`th denominator `Bₙ` is greater than or equal to the `n + 1`th fibonacci number `nat.fib (n + 1)`. - `gcf.le_of_succ_nth_denom`: shows that `Bₙ₊₁ ≥ bₙ * Bₙ`, where `bₙ` is the `n`th partial denominator of the continued fraction. ## References - [*Hardy, GH and Wright, EM and Heath-Brown, Roger and Silverman, Joseph*][hardy2008introduction] -/ namespace generalized_continued_fraction open generalized_continued_fraction as gcf variables {K : Type*} {v : K} {n : ℕ} [discrete_linear_ordered_field K] [floor_ring K] /- We begin with some lemmas about the stream of `int_fract_pair`s, which presumably are not of great interest for the end user. -/ namespace int_fract_pair /-- Shows that the fractional parts of the stream are in `[0,1)`. -/ lemma nth_stream_fr_nonneg_lt_one {ifp_n : int_fract_pair K} (nth_stream_eq : int_fract_pair.stream v n = some ifp_n) : 0 ≤ ifp_n.fr ∧ ifp_n.fr < 1 := begin cases n, case nat.zero { have : int_fract_pair.of v = ifp_n, by injection nth_stream_eq, simp [fract_lt_one, fract_nonneg, int_fract_pair.of, this.symm] }, case nat.succ { rcases (succ_nth_stream_eq_some_iff.elim_left nth_stream_eq) with ⟨_, _, _, if_of_eq_ifp_n⟩, simp [fract_lt_one, fract_nonneg, int_fract_pair.of, if_of_eq_ifp_n.symm] } end /-- Shows that the fractional parts of the stream are nonnegative. -/ lemma nth_stream_fr_nonneg {ifp_n : int_fract_pair K} (nth_stream_eq : int_fract_pair.stream v n = some ifp_n) : 0 ≤ ifp_n.fr := (nth_stream_fr_nonneg_lt_one nth_stream_eq).left /-- Shows that the fractional parts of the stream smaller than one. -/ lemma nth_stream_fr_lt_one {ifp_n : int_fract_pair K} (nth_stream_eq : int_fract_pair.stream v n = some ifp_n) : ifp_n.fr < 1 := (nth_stream_fr_nonneg_lt_one nth_stream_eq).right /-- Shows that the integer parts of the stream are at least one. -/ lemma one_le_succ_nth_stream_b {ifp_succ_n : int_fract_pair K} (succ_nth_stream_eq : int_fract_pair.stream v (n + 1) = some ifp_succ_n) : 1 ≤ ifp_succ_n.b := begin obtain ⟨ifp_n, nth_stream_eq, stream_nth_fr_ne_zero, ⟨_⟩⟩ : ∃ ifp_n, int_fract_pair.stream v n = some ifp_n ∧ ifp_n.fr ≠ 0 ∧ int_fract_pair.of ifp_n.fr⁻¹ = ifp_succ_n, from succ_nth_stream_eq_some_iff.elim_left succ_nth_stream_eq, change 1 ≤ ⌊ifp_n.fr⁻¹⌋, suffices : 1 ≤ ifp_n.fr⁻¹, { rw_mod_cast [le_floor], assumption }, suffices : ifp_n.fr ≤ 1, { have h : 0 < ifp_n.fr := lt_of_le_of_ne (nth_stream_fr_nonneg nth_stream_eq) stream_nth_fr_ne_zero.symm, apply one_le_inv h this }, simp [(le_of_lt (nth_stream_fr_lt_one nth_stream_eq))] end /-- Shows that the `n + 1`th integer part `bₙ₊₁` of the stream is smaller or equal than the inverse of the `n`th fractional part `frₙ` of the stream. This result is straight-forward as `bₙ₊₁` is defined as the floor of `1 / frₙ` -/ lemma succ_nth_stream_b_le_nth_stream_fr_inv {ifp_n ifp_succ_n : int_fract_pair K} (nth_stream_eq : int_fract_pair.stream v n = some ifp_n) (succ_nth_stream_eq : int_fract_pair.stream v (n + 1) = some ifp_succ_n) : (ifp_succ_n.b : K) ≤ ifp_n.fr⁻¹ := begin suffices : (⌊ifp_n.fr⁻¹⌋ : K) ≤ ifp_n.fr⁻¹, { cases ifp_n with _ ifp_n_fr, have : ifp_n_fr ≠ 0, { intro h, simpa [h, int_fract_pair.stream, nth_stream_eq] using succ_nth_stream_eq }, have : int_fract_pair.of ifp_n_fr⁻¹ = ifp_succ_n, { simpa [this, int_fract_pair.stream, nth_stream_eq, option.coe_def] using succ_nth_stream_eq }, rwa ←this }, exact (floor_le ifp_n.fr⁻¹) end end int_fract_pair /- Next we translate above results about the stream of `int_fract_pair`s to the computed continued fraction `gcf.of`. -/ /-- Shows that the integer parts of the continued fraction are at least one. -/ lemma of_one_le_nth_part_denom {b : K} (nth_part_denom_eq : (gcf.of v).partial_denominators.nth n = some b) : 1 ≤ b := begin obtain ⟨gp_n, nth_s_eq, ⟨_⟩⟩ : ∃ gp_n, (gcf.of v).s.nth n = some gp_n ∧ gp_n.b = b, from exists_s_b_of_part_denom nth_part_denom_eq, obtain ⟨ifp_n, succ_nth_stream_eq, ifp_n_b_eq_gp_n_b⟩ : ∃ ifp, int_fract_pair.stream v (n + 1) = some ifp ∧ (ifp.b : K) = gp_n.b, from int_fract_pair.exists_succ_nth_stream_of_gcf_of_nth_eq_some nth_s_eq, rw [←ifp_n_b_eq_gp_n_b], exact_mod_cast (int_fract_pair.one_le_succ_nth_stream_b succ_nth_stream_eq) end /-- Shows that the partial numerators `aᵢ` of the continued fraction are equal to one and the partial denominators `bᵢ` correspond to integers. -/ lemma of_part_num_eq_one_and_exists_int_part_denom_eq {gp : gcf.pair K} (nth_s_eq : (gcf.of v).s.nth n = some gp) : gp.a = 1 ∧ ∃ (z : ℤ), gp.b = (z : K) := begin obtain ⟨ifp, stream_succ_nth_eq, ifp_b_eq_gp_b⟩ : ∃ ifp, int_fract_pair.stream v (n + 1) = some ifp ∧ (ifp.b : K) = gp.b, from int_fract_pair.exists_succ_nth_stream_of_gcf_of_nth_eq_some nth_s_eq, have : (gcf.of v).s.nth n = some ⟨1, ifp.b⟩, from nth_of_eq_some_of_succ_nth_int_fract_pair_stream stream_succ_nth_eq, have : some gp = some ⟨1, ifp.b⟩, by rwa nth_s_eq at this, have : gp = ⟨1, ifp.b⟩, by injection this, -- We know the shape of gp, so now we just have to split the goal and use this knowledge. cases gp, split, { injection this }, { existsi ifp.b, injection this } end /-- Shows that the partial numerators `aᵢ` are equal to one. -/ lemma of_part_num_eq_one {a : K} (nth_part_num_eq : (gcf.of v).partial_numerators.nth n = some a) : a = 1 := begin obtain ⟨gp, nth_s_eq, gp_a_eq_a_n⟩ : ∃ gp, (gcf.of v).s.nth n = some gp ∧ gp.a = a, from exists_s_a_of_part_num nth_part_num_eq, have : gp.a = 1, from (of_part_num_eq_one_and_exists_int_part_denom_eq nth_s_eq).left, rwa gp_a_eq_a_n at this end /-- Shows that the partial denominators `bᵢ` correspond to an integer. -/ lemma exists_int_eq_of_part_denom {b : K} (nth_part_denom_eq : (gcf.of v).partial_denominators.nth n = some b) : ∃ (z : ℤ), b = (z : K) := begin obtain ⟨gp, nth_s_eq, gp_b_eq_b_n⟩ : ∃ gp, (gcf.of v).s.nth n = some gp ∧ gp.b = b, from exists_s_b_of_part_denom nth_part_denom_eq, have : ∃ (z : ℤ), gp.b = (z : K), from (of_part_num_eq_one_and_exists_int_part_denom_eq nth_s_eq).right, rwa gp_b_eq_b_n at this end /- One of our next goals is to show that `Bₙ₊₁ ≥ bₙ * Bₙ`. For this, we first show that the partial denominators `Bₙ` are bounded from below by the fibonacci sequence `nat.fib`. This then implies that `0 ≤ Bₙ` and hence `Bₙ₊₂ = bₙ₊₁ * Bₙ₊₁ + Bₙ ≥ bₙ₊₁ * Bₙ₊₁ + 0 = bₙ₊₁ * Bₙ₊₁`. -/ -- open `nat` as we will make use of fibonacci numbers. open nat lemma fib_le_of_continuants_aux_b : (n ≤ 1 ∨ ¬(gcf.of v).terminated_at (n - 2)) → (fib n : K) ≤ ((gcf.of v).continuants_aux n).b := nat.strong_induction_on n begin clear n, assume n IH hyp, rcases n with _|_|n, { simp [fib_succ_succ, continuants_aux] }, -- case n = 0 { simp [fib_succ_succ, continuants_aux] }, -- case n = 1 { let g := gcf.of v, -- case 2 ≤ n have : ¬(n + 2 ≤ 1), by linarith, have not_terminated_at_n : ¬g.terminated_at n, from or.resolve_left hyp this, obtain ⟨gp, s_ppred_nth_eq⟩ : ∃ gp, g.s.nth n = some gp, from option.ne_none_iff_exists'.mp not_terminated_at_n, set pconts := g.continuants_aux (n + 1) with pconts_eq, set ppconts := g.continuants_aux n with ppconts_eq, -- use the recurrence of continuants_aux suffices : (fib n : K) + fib (n + 1) ≤ gp.a * ppconts.b + gp.b * pconts.b, by simpa [fib_succ_succ, add_comm, (continuants_aux_recurrence s_ppred_nth_eq ppconts_eq pconts_eq)], -- make use of the fact that gp.a = 1 suffices : (fib n : K) + fib (n + 1) ≤ ppconts.b + gp.b * pconts.b, by simpa [(of_part_num_eq_one $ part_num_eq_s_a s_ppred_nth_eq)], have not_terminated_at_pred_n : ¬g.terminated_at (n - 1), from mt (terminated_stable $ nat.sub_le n 1) not_terminated_at_n, have not_terminated_at_ppred_n : ¬terminated_at g (n - 2), from mt (terminated_stable (n - 1).pred_le) not_terminated_at_pred_n, -- use the IH to get the inequalities for `pconts` and `ppconts` have : (fib (n + 1) : K) ≤ pconts.b, from IH _ (nat.lt.base $ n + 1) (or.inr not_terminated_at_pred_n), have ppred_nth_fib_le_ppconts_B : (fib n : K) ≤ ppconts.b, from IH n (lt_trans (nat.lt.base n) $ nat.lt.base $ n + 1) (or.inr not_terminated_at_ppred_n), suffices : (fib (n + 1) : K) ≤ gp.b * pconts.b, solve_by_elim [(add_le_add ppred_nth_fib_le_ppconts_B)], -- finally use the fact that 1 ≤ gp.b to solve the goal suffices : 1 * (fib (n + 1) : K) ≤ gp.b * pconts.b, by rwa [one_mul] at this, have one_le_gp_b : (1 : K) ≤ gp.b, from of_one_le_nth_part_denom (part_denom_eq_s_b s_ppred_nth_eq), have : (0 : K) ≤ fib (n + 1), by exact_mod_cast (fib (n + 1)).zero_le, have : (0 : K) ≤ gp.b, from le_trans zero_le_one one_le_gp_b, mono } end /-- Shows that the `n`th denominator is greater than or equal to the `n + 1`th fibonacci number, that is `nat.fib (n + 1) ≤ Bₙ`. -/ lemma succ_nth_fib_le_of_nth_denom (hyp: n = 0 ∨ ¬(gcf.of v).terminated_at (n - 1)) : (fib (n + 1) : K) ≤ (gcf.of v).denominators n := begin rw [denom_eq_conts_b, nth_cont_eq_succ_nth_cont_aux], have : (n + 1) ≤ 1 ∨ ¬(gcf.of v).terminated_at (n - 1), by { cases n, case nat.zero : { exact (or.inl $ le_refl 1) }, case nat.succ : { exact or.inr (or.resolve_left hyp n.succ_ne_zero) } }, exact (fib_le_of_continuants_aux_b this) end /- As a simple consequence, we can now derive that all denominators are nonnegative. -/ lemma zero_le_of_continuants_aux_b : 0 ≤ ((gcf.of v).continuants_aux n).b := begin let g := gcf.of v, induction n with n IH, case nat.zero: { refl }, case nat.succ: { cases (decidable.em $ g.terminated_at (n - 1)) with terminated not_terminated, { cases n, { simp[zero_le_one] }, { have : g.continuants_aux (n + 2) = g.continuants_aux (n + 1), from continuants_aux_stable_step_of_terminated terminated, simp[this, IH] } }, { calc (0 : K) ≤ fib (n + 1) : by exact_mod_cast (n + 1).fib.zero_le ... ≤ ((gcf.of v).continuants_aux (n + 1)).b : fib_le_of_continuants_aux_b (or.inr not_terminated) } } end /-- Shows that all denominators are nonnegative. -/ lemma zero_le_of_denom : 0 ≤ (gcf.of v).denominators n := by { rw [denom_eq_conts_b, nth_cont_eq_succ_nth_cont_aux], exact zero_le_of_continuants_aux_b } lemma le_of_succ_succ_nth_continuants_aux_b {b : K} (nth_part_denom_eq : (gcf.of v).partial_denominators.nth n = some b) : b * ((gcf.of v).continuants_aux $ n + 1).b ≤ ((gcf.of v).continuants_aux $ n + 2).b := begin set g := gcf.of v with g_eq, obtain ⟨gp_n, nth_s_eq, ⟨_⟩⟩ : ∃ gp_n, g.s.nth n = some gp_n ∧ gp_n.b = b, from exists_s_b_of_part_denom nth_part_denom_eq, let conts := g.continuants_aux (n + 2), set pconts := g.continuants_aux (n + 1) with pconts_eq, set ppconts := g.continuants_aux n with ppconts_eq, -- use the recurrence of continuants_aux and the fact that gp_n.a = 1 suffices : gp_n.b * pconts.b ≤ ppconts.b + gp_n.b * pconts.b, by { have : gp_n.a = 1, from of_part_num_eq_one (part_num_eq_s_a nth_s_eq), finish [(gcf.continuants_aux_recurrence nth_s_eq ppconts_eq pconts_eq)] }, have : 0 ≤ ppconts.b, from zero_le_of_continuants_aux_b, solve_by_elim [le_add_of_nonneg_of_le, le_refl] end /-- Shows that `Bₙ₊₁ ≥ bₙ * Bₙ`, where `bₙ` is the `n`th partial denominator and `Bₙ₊₁` and `Bₙ` are the `n + 1`th and `n`th denominator of the continued fraction. -/ theorem le_of_succ_nth_denom {b : K} (nth_part_denom_eq : (gcf.of v).partial_denominators.nth n = some b) : b * (gcf.of v).denominators n ≤ (gcf.of v).denominators (n + 1) := begin rw [denom_eq_conts_b, nth_cont_eq_succ_nth_cont_aux], exact (le_of_succ_succ_nth_continuants_aux_b nth_part_denom_eq) end /-- Shows that the sequence of denominators is monotonically increasing, that is `Bₙ ≤ Bₙ₊₁`. -/ theorem of_denom_mono : (gcf.of v).denominators n ≤ (gcf.of v).denominators (n + 1) := begin let g := gcf.of v, cases (decidable.em $ g.partial_denominators.terminated_at n) with terminated not_terminated, { have : g.partial_denominators.nth n = none, by rwa seq.terminated_at at terminated, have : g.terminated_at n, from terminated_at_iff_part_denom_none.elim_right (by rwa seq.terminated_at at terminated), have : (gcf.of v).denominators (n + 1) = (gcf.of v).denominators n, from denominators_stable_of_terminated n.le_succ this, rw this }, { obtain ⟨b, nth_part_denom_eq⟩ : ∃ b, g.partial_denominators.nth n = some b, from option.ne_none_iff_exists'.mp not_terminated, have : 1 ≤ b, from of_one_le_nth_part_denom nth_part_denom_eq, calc (gcf.of v).denominators n ≤ b * (gcf.of v).denominators n : by simpa using mul_le_mul_of_nonneg_right this zero_le_of_denom ... ≤ (gcf.of v).denominators (n + 1) : le_of_succ_nth_denom nth_part_denom_eq } end end generalized_continued_fraction
68bc91bf212f2e3acc4ce971d69bfc18dcda6dcd
bd12a817ba941113eb7fdb7ddf0979d9ed9386a0
/src/category_theory/instances/CommRing/adjunctions.lean
481d24ad3182116211c153065eb926c6af330e8c
[ "Apache-2.0" ]
permissive
flypitch/mathlib
563d9c3356c2885eb6cefaa704d8d86b89b74b15
70cd00bc20ad304f2ac0886b2291b44261787607
refs/heads/master
1,590,167,818,658
1,557,762,121,000
1,557,762,121,000
186,450,076
0
0
Apache-2.0
1,557,762,289,000
1,557,762,288,000
null
UTF-8
Lean
false
false
2,746
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johannes Hölzl Multivariable polynomials on a type is the left adjoint of the forgetful functor from commutative rings to types. -/ import category_theory.instances.CommRing.basic import category_theory.adjunction import data.mv_polynomial universe u open mv_polynomial open category_theory open category_theory.instances namespace category_theory.instances.CommRing local attribute [instance, priority 0] subtype.fintype set_fintype classical.prop_decidable noncomputable def polynomial_ring : Type u ⥤ CommRing.{u} := { obj := λ α, ⟨mv_polynomial α ℤ, by apply_instance⟩, map := λ α β f, ⟨eval₂ C (X ∘ f), by apply_instance⟩, map_id' := λ α, subtype.ext.mpr $ funext $ eval₂_eta, map_comp' := λ α β γ f g, subtype.ext.mpr $ funext $ λ p, by apply mv_polynomial.induction_on p; intros; simp only [*, eval₂_add, eval₂_mul, eval₂_C, eval₂_X, comp_val, eq_self_iff_true, function.comp_app, types_comp] at * } @[simp] lemma polynomial_ring_obj_α {α : Type u} : (polynomial_ring.obj α).α = mv_polynomial α ℤ := rfl @[simp] lemma polynomial_ring_map_val {α β : Type u} {f : α → β} : (polynomial_ring.map f).val = eval₂ C (X ∘ f) := rfl lemma hom_coe_app' {R S : CommRing} (f : R ⟶ S) (r : R) : f r = f.val r := rfl -- this is usually a bad idea, as it forgets that `f` is ring homomorphism local attribute [simp] hom_coe_app' noncomputable def adj : adjunction polynomial_ring (forget : CommRing ⥤ Type u) := adjunction.mk_of_hom_equiv _ _ { hom_equiv := λ α R, { to_fun := λ f, f ∘ X, inv_fun := λ f, ⟨eval₂ int.cast f, by apply_instance⟩, left_inv := λ f, subtype.ext.mpr $ funext $ λ p, begin have H0 := λ n, (congr (int.eq_cast' (f.val ∘ C)) (rfl : n = n)).symm, have H1 := λ p₁ p₂, (@is_ring_hom.map_add _ _ _ _ f.val f.2 p₁ p₂).symm, have H2 := λ p₁ p₂, (@is_ring_hom.map_mul _ _ _ _ f.val f.2 p₁ p₂).symm, apply mv_polynomial.induction_on p; intros; simp only [*, eval₂_add, eval₂_mul, eval₂_C, eval₂_X, eq_self_iff_true, function.comp_app, hom_coe_app'] at * end, right_inv := by tidy }, hom_equiv_naturality_left_symm' := λ X' X Y f g, subtype.ext.mpr $ funext $ λ p, begin apply mv_polynomial.induction_on p; intros; simp only [*, eval₂_mul, eval₂_add, eval₂_C, eval₂_X, comp_val, equiv.coe_fn_symm_mk, hom_coe_app, polynomial_ring_map_val, eq_self_iff_true, function.comp_app, add_right_inj, types_comp] at * end } end category_theory.instances.CommRing
da686cbfac21d8e25e198faa048d99318393d1a1
c777c32c8e484e195053731103c5e52af26a25d1
/src/analysis/complex/upper_half_plane/topology.lean
c441e99fcfe93c2d0e24fc2545eeacef411bb5f0
[ "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
2,859
lean
/- Copyright (c) 2022 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import analysis.complex.upper_half_plane.basic import analysis.convex.contractible import analysis.convex.normed import analysis.convex.complex import analysis.complex.re_im_topology import topology.homotopy.contractible import geometry.manifold.cont_mdiff_mfderiv /-! # Topology on the upper half plane In this file we introduce a `topological_space` structure on the upper half plane and provide various instances. -/ noncomputable theory open set filter function topological_space complex open_locale filter topology upper_half_plane manifold namespace upper_half_plane instance : topological_space ℍ := subtype.topological_space lemma open_embedding_coe : open_embedding (coe : ℍ → ℂ) := is_open.open_embedding_subtype_coe $ is_open_lt continuous_const complex.continuous_im lemma embedding_coe : embedding (coe : ℍ → ℂ) := embedding_subtype_coe lemma continuous_coe : continuous (coe : ℍ → ℂ) := embedding_coe.continuous lemma continuous_re : continuous re := complex.continuous_re.comp continuous_coe lemma continuous_im : continuous im := complex.continuous_im.comp continuous_coe instance : topological_space.second_countable_topology ℍ := topological_space.subtype.second_countable_topology _ _ instance : t3_space ℍ := subtype.t3_space instance : normal_space ℍ := normal_space_of_t3_second_countable ℍ instance : contractible_space ℍ := (convex_halfspace_im_gt 0).contractible_space ⟨I, one_pos.trans_eq I_im.symm⟩ instance : loc_path_connected_space ℍ := loc_path_connected_of_is_open $ is_open_lt continuous_const complex.continuous_im instance : noncompact_space ℍ := begin refine ⟨λ h, _⟩, have : is_compact (complex.im ⁻¹' Ioi 0), from is_compact_iff_is_compact_univ.2 h, replace := this.is_closed.closure_eq, rw [closure_preimage_im, closure_Ioi, set.ext_iff] at this, exact absurd ((this 0).1 left_mem_Ici) (lt_irrefl _) end instance : locally_compact_space ℍ := open_embedding_coe.locally_compact_space instance upper_half_plane.charted_space : charted_space ℂ ℍ := upper_half_plane.open_embedding_coe.singleton_charted_space instance upper_half_plane.smooth_manifold_with_corners : smooth_manifold_with_corners 𝓘(ℂ) ℍ := upper_half_plane.open_embedding_coe.singleton_smooth_manifold_with_corners 𝓘(ℂ) /-- The inclusion map `ℍ → ℂ` is a smooth map of manifolds. -/ lemma smooth_coe : smooth 𝓘(ℂ) 𝓘(ℂ) (coe : ℍ → ℂ) := λ x, cont_mdiff_at_ext_chart_at /-- The inclusion map `ℍ → ℂ` is a differentiable map of manifolds. -/ lemma mdifferentiable_coe : mdifferentiable 𝓘(ℂ) 𝓘(ℂ) (coe : ℍ → ℂ) := smooth_coe.mdifferentiable end upper_half_plane
d6f8447e8e2cc13b8aef50064448fa21c88a5550
bb31430994044506fa42fd667e2d556327e18dfe
/src/algebra/group/basic.lean
6740b509dae66f2821c84b1bb0355ada13e87f93
[ "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
21,588
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, Simon Hudon, Mario Carneiro -/ import algebra.group.defs /-! # Basic lemmas about semigroups, monoids, and groups > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file lists various basic lemmas about semigroups, monoids, and groups. Most proofs are one-liners from the corresponding axioms. For the definitions of semigroups, monoids and groups, see `algebra/group/defs.lean`. -/ open function universe u variables {α β G : Type*} section associative variables (f : α → α → α) [is_associative α f] (x y : α) /-- Composing two associative operations of `f : α → α → α` on the left is equal to an associative operation on the left. -/ lemma comp_assoc_left : (f x) ∘ (f y) = (f (f x y)) := by { ext z, rw [function.comp_apply, @is_associative.assoc _ f] } /-- Composing two associative operations of `f : α → α → α` on the right is equal to an associative operation on the right. -/ lemma comp_assoc_right : (λ z, f z x) ∘ (λ z, f z y) = (λ z, f z (f y x)) := by { ext z, rw [function.comp_apply, @is_associative.assoc _ f] } end associative section semigroup /-- Composing two multiplications on the left by `y` then `x` is equal to a multiplication on the left by `x * y`. -/ @[simp, to_additive "Composing two additions on the left by `y` then `x` is equal to a addition on the left by `x + y`."] lemma comp_mul_left [semigroup α] (x y : α) : ((*) x) ∘ ((*) y) = ((*) (x * y)) := comp_assoc_left _ _ _ /-- Composing two multiplications on the right by `y` and `x` is equal to a multiplication on the right by `y * x`. -/ @[simp, to_additive "Composing two additions on the right by `y` and `x` is equal to a addition on the right by `y + x`."] lemma comp_mul_right [semigroup α] (x y : α) : (* x) ∘ (* y) = (* (y * x)) := comp_assoc_right _ _ _ end semigroup section mul_one_class variables {M : Type u} [mul_one_class M] @[to_additive] lemma ite_mul_one {P : Prop} [decidable P] {a b : M} : ite P (a * b) 1 = ite P a 1 * ite P b 1 := by { by_cases h : P; simp [h], } @[to_additive] lemma ite_one_mul {P : Prop} [decidable P] {a b : M} : ite P 1 (a * b) = ite P 1 a * ite P 1 b := by { by_cases h : P; simp [h], } @[to_additive] lemma eq_one_iff_eq_one_of_mul_eq_one {a b : M} (h : a * b = 1) : a = 1 ↔ b = 1 := by split; { rintro rfl, simpa using h } @[to_additive] lemma one_mul_eq_id : ((*) (1 : M)) = id := funext one_mul @[to_additive] lemma mul_one_eq_id : (* (1 : M)) = id := funext mul_one end mul_one_class section comm_semigroup variables [comm_semigroup G] @[no_rsimp, to_additive] lemma mul_left_comm : ∀ a b c : G, a * (b * c) = b * (a * c) := left_comm has_mul.mul mul_comm mul_assoc @[to_additive] lemma mul_right_comm : ∀ a b c : G, a * b * c = a * c * b := right_comm has_mul.mul mul_comm mul_assoc @[to_additive] theorem mul_mul_mul_comm (a b c d : G) : (a * b) * (c * d) = (a * c) * (b * d) := by simp only [mul_left_comm, mul_assoc] @[to_additive] lemma mul_rotate (a b c : G) : a * b * c = b * c * a := by simp only [mul_left_comm, mul_comm] @[to_additive] lemma mul_rotate' (a b c : G) : a * (b * c) = b * (c * a) := by simp only [mul_left_comm, mul_comm] end comm_semigroup section add_comm_semigroup variables {M : Type u} [add_comm_semigroup M] lemma bit0_add (a b : M) : bit0 (a + b) = bit0 a + bit0 b := add_add_add_comm _ _ _ _ lemma bit1_add [has_one M] (a b : M) : bit1 (a + b) = bit0 a + bit1 b := (congr_arg (+ (1 : M)) $ bit0_add a b : _).trans (add_assoc _ _ _) lemma bit1_add' [has_one M] (a b : M) : bit1 (a + b) = bit1 a + bit0 b := by rw [add_comm, bit1_add, add_comm] end add_comm_semigroup local attribute [simp] mul_assoc sub_eq_add_neg section add_monoid variables {M : Type u} [add_monoid M] {a b c : M} @[simp] lemma bit0_zero : bit0 (0 : M) = 0 := add_zero _ @[simp] lemma bit1_zero [has_one M] : bit1 (0 : M) = 1 := by rw [bit1, bit0_zero, zero_add] end add_monoid section comm_monoid variables {M : Type u} [comm_monoid M] {x y z : M} @[to_additive] lemma inv_unique (hy : x * y = 1) (hz : x * z = 1) : y = z := left_inv_eq_right_inv (trans (mul_comm _ _) hy) hz end comm_monoid section left_cancel_monoid variables {M : Type u} [left_cancel_monoid M] {a b : M} @[simp, to_additive] lemma mul_right_eq_self : a * b = a ↔ b = 1 := calc a * b = a ↔ a * b = a * 1 : by rw mul_one ... ↔ b = 1 : mul_left_cancel_iff @[simp, to_additive] lemma self_eq_mul_right : a = a * b ↔ b = 1 := eq_comm.trans mul_right_eq_self end left_cancel_monoid section right_cancel_monoid variables {M : Type u} [right_cancel_monoid M] {a b : M} @[simp, to_additive] lemma mul_left_eq_self : a * b = b ↔ a = 1 := calc a * b = b ↔ a * b = 1 * b : by rw one_mul ... ↔ a = 1 : mul_right_cancel_iff @[simp, to_additive] lemma self_eq_mul_left : b = a * b ↔ a = 1 := eq_comm.trans mul_left_eq_self end right_cancel_monoid section has_involutive_inv variables [has_involutive_inv G] {a b : G} @[simp, to_additive] lemma inv_involutive : function.involutive (has_inv.inv : G → G) := inv_inv @[simp, to_additive] lemma inv_surjective : function.surjective (has_inv.inv : G → G) := inv_involutive.surjective @[to_additive] lemma inv_injective : function.injective (has_inv.inv : G → G) := inv_involutive.injective @[simp, to_additive] theorem inv_inj {a b : G} : a⁻¹ = b⁻¹ ↔ a = b := inv_injective.eq_iff @[to_additive] lemma eq_inv_of_eq_inv (h : a = b⁻¹) : b = a⁻¹ := by simp [h] @[to_additive] theorem eq_inv_iff_eq_inv : a = b⁻¹ ↔ b = a⁻¹ := ⟨eq_inv_of_eq_inv, eq_inv_of_eq_inv⟩ @[to_additive] theorem inv_eq_iff_inv_eq : a⁻¹ = b ↔ b⁻¹ = a := eq_comm.trans $ eq_inv_iff_eq_inv.trans eq_comm variables (G) @[simp, to_additive] lemma inv_comp_inv : has_inv.inv ∘ has_inv.inv = @id G := inv_involutive.comp_self @[to_additive] lemma left_inverse_inv : left_inverse (λ a : G, a⁻¹) (λ a, a⁻¹) := inv_inv @[to_additive] lemma right_inverse_inv : left_inverse (λ a : G, a⁻¹) (λ a, a⁻¹) := inv_inv end has_involutive_inv section div_inv_monoid variables [div_inv_monoid G] {a b c : G} @[to_additive, field_simps] -- The attributes are out of order on purpose lemma inv_eq_one_div (x : G) : x⁻¹ = 1 / x := by rw [div_eq_mul_inv, one_mul] @[to_additive] lemma mul_one_div (x y : G) : x * (1 / y) = x / y := by rw [div_eq_mul_inv, one_mul, div_eq_mul_inv] @[to_additive] lemma mul_div_assoc (a b c : G) : a * b / c = a * (b / c) := by rw [div_eq_mul_inv, div_eq_mul_inv, mul_assoc _ _ _] @[to_additive, field_simps] -- The attributes are out of order on purpose lemma mul_div_assoc' (a b c : G) : a * (b / c) = (a * b) / c := (mul_div_assoc _ _ _).symm @[simp, to_additive] lemma one_div (a : G) : 1 / a = a⁻¹ := (inv_eq_one_div a).symm @[to_additive] lemma mul_div (a b c : G) : a * (b / c) = a * b / c := by simp only [mul_assoc, div_eq_mul_inv] @[to_additive] lemma div_eq_mul_one_div (a b : G) : a / b = a * (1 / b) := by rw [div_eq_mul_inv, one_div] end div_inv_monoid section div_inv_one_monoid variables [div_inv_one_monoid G] @[simp, to_additive] lemma div_one (a : G) : a / 1 = a := by simp [div_eq_mul_inv] @[to_additive] lemma one_div_one : (1 : G) / 1 = 1 := div_one _ end div_inv_one_monoid section division_monoid variables [division_monoid α] {a b c : α} local attribute [simp] mul_assoc div_eq_mul_inv @[to_additive] lemma inv_eq_of_mul_eq_one_left (h : a * b = 1) : b⁻¹ = a := by rw [←inv_eq_of_mul_eq_one_right h, inv_inv] @[to_additive] lemma eq_inv_of_mul_eq_one_left (h : a * b = 1) : a = b⁻¹ := (inv_eq_of_mul_eq_one_left h).symm @[to_additive] lemma eq_inv_of_mul_eq_one_right (h : a * b = 1) : b = a⁻¹ := (inv_eq_of_mul_eq_one_right h).symm @[to_additive] lemma eq_one_div_of_mul_eq_one_left (h : b * a = 1) : b = 1 / a := by rw [eq_inv_of_mul_eq_one_left h, one_div] @[to_additive] lemma eq_one_div_of_mul_eq_one_right (h : a * b = 1) : b = 1 / a := by rw [eq_inv_of_mul_eq_one_right h, one_div] @[to_additive] lemma eq_of_div_eq_one (h : a / b = 1) : a = b := inv_injective $ inv_eq_of_mul_eq_one_right $ by rwa ←div_eq_mul_inv @[to_additive] lemma div_ne_one_of_ne : a ≠ b → a / b ≠ 1 := mt eq_of_div_eq_one variables (a b c) @[to_additive] lemma one_div_mul_one_div_rev : (1 / a) * (1 / b) = 1 / (b * a) := by simp @[to_additive] lemma inv_div_left : a⁻¹ / b = (b * a)⁻¹ := by simp @[simp, to_additive] lemma inv_div : (a / b)⁻¹ = b / a := by simp @[simp, to_additive] lemma one_div_div : 1 / (a / b) = b / a := by simp @[to_additive] lemma one_div_one_div : 1 / (1 / a) = a := by simp @[priority 100, to_additive subtraction_monoid.to_sub_neg_zero_monoid] instance division_monoid.to_div_inv_one_monoid : div_inv_one_monoid α := { inv_one := by simpa only [one_div, inv_inv] using (inv_div (1 : α) 1).symm, ..division_monoid.to_div_inv_monoid α } variables {a b c} @[simp, to_additive] lemma inv_eq_one : a⁻¹ = 1 ↔ a = 1 := inv_injective.eq_iff' inv_one @[simp, to_additive] lemma one_eq_inv : 1 = a⁻¹ ↔ a = 1 := eq_comm.trans inv_eq_one @[to_additive] lemma inv_ne_one : a⁻¹ ≠ 1 ↔ a ≠ 1 := inv_eq_one.not @[to_additive] lemma eq_of_one_div_eq_one_div (h : 1 / a = 1 / b) : a = b := by rw [←one_div_one_div a, h, one_div_one_div] variables (a b c) -- The attributes are out of order on purpose @[to_additive, field_simps] lemma div_div_eq_mul_div : a / (b / c) = a * c / b := by simp @[simp, to_additive] lemma div_inv_eq_mul : a / b⁻¹ = a * b := by simp @[to_additive] lemma div_mul_eq_div_div_swap : a / (b * c) = a / c / b := by simp only [mul_assoc, mul_inv_rev, div_eq_mul_inv] end division_monoid lemma bit0_neg [subtraction_monoid α] (a : α) : bit0 (-a) = -bit0 a := (neg_add_rev _ _).symm section division_comm_monoid variables [division_comm_monoid α] (a b c d : α) local attribute [simp] mul_assoc mul_comm mul_left_comm div_eq_mul_inv @[to_additive neg_add] lemma mul_inv : (a * b)⁻¹ = a⁻¹ * b⁻¹ := by simp @[to_additive] lemma inv_div' : (a / b)⁻¹ = a⁻¹ / b⁻¹ := by simp @[to_additive] lemma div_eq_inv_mul : a / b = b⁻¹ * a := by simp @[to_additive] lemma inv_mul_eq_div : a⁻¹ * b = b / a := by simp @[to_additive] lemma inv_mul' : (a * b)⁻¹ = a⁻¹ / b := by simp @[simp, to_additive] lemma inv_div_inv : (a⁻¹ / b⁻¹) = b / a := by simp @[to_additive] lemma inv_inv_div_inv : (a⁻¹ / b⁻¹)⁻¹ = a / b := by simp @[to_additive] lemma one_div_mul_one_div : (1 / a) * (1 / b) = 1 / (a * b) := by simp @[to_additive] lemma div_right_comm : a / b / c = a / c / b := by simp @[to_additive, field_simps] lemma div_div : a / b / c = a / (b * c) := by simp @[to_additive] lemma div_mul : a / b * c = a / (b / c) := by simp @[to_additive] lemma mul_div_left_comm : a * (b / c) = b * (a / c) := by simp @[to_additive] lemma mul_div_right_comm : a * b / c = a / c * b := by simp @[to_additive] lemma div_mul_eq_div_div : a / (b * c) = a / b / c := by simp @[to_additive, field_simps] lemma div_mul_eq_mul_div : a / b * c = a * c / b := by simp @[to_additive] lemma mul_comm_div : a / b * c = a * (c / b) := by simp @[to_additive] lemma div_mul_comm : a / b * c = c / b * a := by simp @[to_additive] lemma div_mul_eq_div_mul_one_div : a / (b * c) = (a / b) * (1 / c) := by simp @[to_additive] lemma div_div_div_eq : a / b / (c / d) = a * d / (b * c) := by simp @[to_additive] lemma div_div_div_comm : a / b / (c / d) = a / c / (b / d) := by simp @[to_additive] lemma div_mul_div_comm : a / b * (c / d) = a * c / (b * d) := by simp @[to_additive] lemma mul_div_mul_comm : a * b / (c * d) = a / c * (b / d) := by simp end division_comm_monoid section group variables [group G] {a b c d : G} @[simp, to_additive] theorem div_eq_inv_self : a / b = b⁻¹ ↔ a = 1 := by rw [div_eq_mul_inv, mul_left_eq_self] @[to_additive] theorem mul_left_surjective (a : G) : function.surjective ((*) a) := λ x, ⟨a⁻¹ * x, mul_inv_cancel_left a x⟩ @[to_additive] theorem mul_right_surjective (a : G) : function.surjective (λ x, x * a) := λ x, ⟨x * a⁻¹, inv_mul_cancel_right x a⟩ @[to_additive] lemma eq_mul_inv_of_mul_eq (h : a * c = b) : a = b * c⁻¹ := by simp [h.symm] @[to_additive] lemma eq_inv_mul_of_mul_eq (h : b * a = c) : a = b⁻¹ * c := by simp [h.symm] @[to_additive] lemma inv_mul_eq_of_eq_mul (h : b = a * c) : a⁻¹ * b = c := by simp [h] @[to_additive] lemma mul_inv_eq_of_eq_mul (h : a = c * b) : a * b⁻¹ = c := by simp [h] @[to_additive] lemma eq_mul_of_mul_inv_eq (h : a * c⁻¹ = b) : a = b * c := by simp [h.symm] @[to_additive] lemma eq_mul_of_inv_mul_eq (h : b⁻¹ * a = c) : a = b * c := by simp [h.symm, mul_inv_cancel_left] @[to_additive] lemma mul_eq_of_eq_inv_mul (h : b = a⁻¹ * c) : a * b = c := by rw [h, mul_inv_cancel_left] @[to_additive] lemma mul_eq_of_eq_mul_inv (h : a = c * b⁻¹) : a * b = c := by simp [h] @[to_additive] theorem mul_eq_one_iff_eq_inv : a * b = 1 ↔ a = b⁻¹ := ⟨eq_inv_of_mul_eq_one_left, λ h, by rw [h, mul_left_inv]⟩ @[to_additive] theorem mul_eq_one_iff_inv_eq : a * b = 1 ↔ a⁻¹ = b := by rw [mul_eq_one_iff_eq_inv, eq_inv_iff_eq_inv, eq_comm] @[to_additive] theorem eq_inv_iff_mul_eq_one : a = b⁻¹ ↔ a * b = 1 := mul_eq_one_iff_eq_inv.symm @[to_additive] theorem inv_eq_iff_mul_eq_one : a⁻¹ = b ↔ a * b = 1 := mul_eq_one_iff_inv_eq.symm @[to_additive] theorem eq_mul_inv_iff_mul_eq : a = b * c⁻¹ ↔ a * c = b := ⟨λ h, by rw [h, inv_mul_cancel_right], λ h, by rw [← h, mul_inv_cancel_right]⟩ @[to_additive] theorem eq_inv_mul_iff_mul_eq : a = b⁻¹ * c ↔ b * a = c := ⟨λ h, by rw [h, mul_inv_cancel_left], λ h, by rw [← h, inv_mul_cancel_left]⟩ @[to_additive] theorem inv_mul_eq_iff_eq_mul : a⁻¹ * b = c ↔ b = a * c := ⟨λ h, by rw [← h, mul_inv_cancel_left], λ h, by rw [h, inv_mul_cancel_left]⟩ @[to_additive] theorem mul_inv_eq_iff_eq_mul : a * b⁻¹ = c ↔ a = c * b := ⟨λ h, by rw [← h, inv_mul_cancel_right], λ h, by rw [h, mul_inv_cancel_right]⟩ @[to_additive] theorem mul_inv_eq_one : a * b⁻¹ = 1 ↔ a = b := by rw [mul_eq_one_iff_eq_inv, inv_inv] @[to_additive] theorem inv_mul_eq_one : a⁻¹ * b = 1 ↔ a = b := by rw [mul_eq_one_iff_eq_inv, inv_inj] @[to_additive] lemma div_left_injective : function.injective (λ a, a / b) := by simpa only [div_eq_mul_inv] using λ a a' h, mul_left_injective (b⁻¹) h @[to_additive] lemma div_right_injective : function.injective (λ a, b / a) := by simpa only [div_eq_mul_inv] using λ a a' h, inv_injective (mul_right_injective b h) @[simp, to_additive sub_add_cancel] lemma div_mul_cancel' (a b : G) : a / b * b = a := by rw [div_eq_mul_inv, inv_mul_cancel_right a b] @[simp, to_additive sub_self] lemma div_self' (a : G) : a / a = 1 := by rw [div_eq_mul_inv, mul_right_inv a] @[simp, to_additive add_sub_cancel] lemma mul_div_cancel'' (a b : G) : a * b / b = a := by rw [div_eq_mul_inv, mul_inv_cancel_right a b] @[simp, to_additive] lemma mul_div_mul_right_eq_div (a b c : G) : (a * c) / (b * c) = a / b := by rw [div_mul_eq_div_div_swap]; simp only [mul_left_inj, eq_self_iff_true, mul_div_cancel''] @[to_additive eq_sub_of_add_eq] lemma eq_div_of_mul_eq' (h : a * c = b) : a = b / c := by simp [← h] @[to_additive sub_eq_of_eq_add] lemma div_eq_of_eq_mul'' (h : a = c * b) : a / b = c := by simp [h] @[to_additive] lemma eq_mul_of_div_eq (h : a / c = b) : a = b * c := by simp [← h] @[to_additive] lemma mul_eq_of_eq_div (h : a = c / b) : a * b = c := by simp [h] @[simp, to_additive] lemma div_right_inj : a / b = a / c ↔ b = c := div_right_injective.eq_iff @[simp, to_additive] lemma div_left_inj : b / a = c / a ↔ b = c := by { rw [div_eq_mul_inv, div_eq_mul_inv], exact mul_left_inj _ } @[simp, to_additive sub_add_sub_cancel] lemma div_mul_div_cancel' (a b c : G) : (a / b) * (b / c) = a / c := by rw [← mul_div_assoc, div_mul_cancel'] @[simp, to_additive sub_sub_sub_cancel_right] lemma div_div_div_cancel_right' (a b c : G) : (a / c) / (b / c) = a / b := by rw [← inv_div c b, div_inv_eq_mul, div_mul_div_cancel'] @[to_additive] theorem div_eq_one : a / b = 1 ↔ a = b := ⟨eq_of_div_eq_one, λ h, by rw [h, div_self']⟩ alias div_eq_one ↔ _ div_eq_one_of_eq alias sub_eq_zero ↔ _ sub_eq_zero_of_eq @[to_additive] theorem div_ne_one : a / b ≠ 1 ↔ a ≠ b := not_congr div_eq_one @[simp, to_additive] theorem div_eq_self : a / b = a ↔ b = 1 := by rw [div_eq_mul_inv, mul_right_eq_self, inv_eq_one] @[to_additive eq_sub_iff_add_eq] theorem eq_div_iff_mul_eq' : a = b / c ↔ a * c = b := by rw [div_eq_mul_inv, eq_mul_inv_iff_mul_eq] @[to_additive] theorem div_eq_iff_eq_mul : a / b = c ↔ a = c * b := by rw [div_eq_mul_inv, mul_inv_eq_iff_eq_mul] @[to_additive] theorem eq_iff_eq_of_div_eq_div (H : a / b = c / d) : a = b ↔ c = d := by rw [← div_eq_one, H, div_eq_one] @[to_additive] theorem left_inverse_div_mul_left (c : G) : function.left_inverse (λ x, x / c) (λ x, x * c) := assume x, mul_div_cancel'' x c @[to_additive] theorem left_inverse_mul_left_div (c : G) : function.left_inverse (λ x, x * c) (λ x, x / c) := assume x, div_mul_cancel' x c @[to_additive] theorem left_inverse_mul_right_inv_mul (c : G) : function.left_inverse (λ x, c * x) (λ x, c⁻¹ * x) := assume x, mul_inv_cancel_left c x @[to_additive] theorem left_inverse_inv_mul_mul_right (c : G) : function.left_inverse (λ x, c⁻¹ * x) (λ x, c * x) := assume x, inv_mul_cancel_left c x @[to_additive] lemma exists_npow_eq_one_of_zpow_eq_one {n : ℤ} (hn : n ≠ 0) {x : G} (h : x ^ n = 1) : ∃ n : ℕ, 0 < n ∧ x ^ n = 1 := begin cases n with n n, { rw zpow_of_nat at h, refine ⟨n, nat.pos_of_ne_zero (λ n0, hn _), h⟩, rw n0, refl }, { rw [zpow_neg_succ_of_nat, inv_eq_one] at h, refine ⟨n + 1, n.succ_pos, h⟩ } end end group section comm_group variables [comm_group G] {a b c d : G} local attribute [simp] mul_assoc mul_comm mul_left_comm div_eq_mul_inv @[to_additive] lemma div_eq_of_eq_mul' {a b c : G} (h : a = b * c) : a / b = c := by rw [h, div_eq_mul_inv, mul_comm, inv_mul_cancel_left] @[simp, to_additive] lemma mul_div_mul_left_eq_div (a b c : G) : (c * a) / (c * b) = a / b := by simp @[to_additive eq_sub_of_add_eq'] lemma eq_div_of_mul_eq'' (h : c * a = b) : a = b / c := by simp [h.symm] @[to_additive] lemma eq_mul_of_div_eq' (h : a / b = c) : a = b * c := by simp [h.symm] @[to_additive] lemma mul_eq_of_eq_div' (h : b = c / a) : a * b = c := begin simp [h], rw [mul_comm c, mul_inv_cancel_left] end @[to_additive sub_sub_self] lemma div_div_self' (a b : G) : a / (a / b) = b := by simpa using mul_inv_cancel_left a b @[to_additive] lemma div_eq_div_mul_div (a b c : G) : a / b = c / b * (a / c) := by simp [mul_left_comm c] @[simp, to_additive] lemma div_div_cancel (a b : G) : a / (a / b) = b := div_div_self' a b @[simp, to_additive] lemma div_div_cancel_left (a b : G) : a / b / a = b⁻¹ := by simp @[to_additive eq_sub_iff_add_eq'] lemma eq_div_iff_mul_eq'' : a = b / c ↔ c * a = b := by rw [eq_div_iff_mul_eq', mul_comm] @[to_additive] lemma div_eq_iff_eq_mul' : a / b = c ↔ a = b * c := by rw [div_eq_iff_eq_mul, mul_comm] @[simp, to_additive add_sub_cancel'] lemma mul_div_cancel''' (a b : G) : a * b / a = b := by rw [div_eq_inv_mul, inv_mul_cancel_left] @[simp, to_additive] lemma mul_div_cancel'_right (a b : G) : a * (b / a) = b := by rw [← mul_div_assoc, mul_div_cancel'''] @[simp, to_additive sub_add_cancel'] lemma div_mul_cancel'' (a b : G) : a / (a * b) = b⁻¹ := by rw [← inv_div, mul_div_cancel'''] -- This lemma is in the `simp` set under the name `mul_inv_cancel_comm_assoc`, -- along with the additive version `add_neg_cancel_comm_assoc`, -- defined in `algebra/group/commute` @[to_additive] lemma mul_mul_inv_cancel'_right (a b : G) : a * (b * a⁻¹) = b := by rw [← div_eq_mul_inv, mul_div_cancel'_right a b] @[simp, to_additive] lemma mul_mul_div_cancel (a b c : G) : (a * c) * (b / c) = a * b := by rw [mul_assoc, mul_div_cancel'_right] @[simp, to_additive] lemma div_mul_mul_cancel (a b c : G) : (a / c) * (b * c) = a * b := by rw [mul_left_comm, div_mul_cancel', mul_comm] @[simp, to_additive sub_add_sub_cancel'] lemma div_mul_div_cancel'' (a b c : G) : (a / b) * (c / a) = c / b := by rw mul_comm; apply div_mul_div_cancel' @[simp, to_additive] lemma mul_div_div_cancel (a b c : G) : (a * b) / (a / c) = b * c := by rw [← div_mul, mul_div_cancel'''] @[simp, to_additive] lemma div_div_div_cancel_left (a b c : G) : (c / a) / (c / b) = b / a := by rw [← inv_div b c, div_inv_eq_mul, mul_comm, div_mul_div_cancel'] @[to_additive] lemma div_eq_div_iff_mul_eq_mul : a / b = c / d ↔ a * d = c * b := begin rw [div_eq_iff_eq_mul, div_mul_eq_mul_div, eq_comm, div_eq_iff_eq_mul'], simp only [mul_comm, eq_comm] end @[to_additive] lemma div_eq_div_iff_div_eq_div : a / b = c / d ↔ a / c = b / d := by rw [div_eq_iff_eq_mul, div_mul_eq_mul_div, div_eq_iff_eq_mul', mul_div_assoc] end comm_group section subtraction_comm_monoid variables {M : Type u} [subtraction_comm_monoid M] lemma bit0_sub (a b : M) : bit0 (a - b) = bit0 a - bit0 b := sub_add_sub_comm _ _ _ _ lemma bit1_sub [has_one M] (a b : M) : bit1 (a - b) = bit1 a - bit0 b := (congr_arg (+ (1 : M)) $ bit0_sub a b : _).trans $ sub_add_eq_add_sub _ _ _ end subtraction_comm_monoid
0ad795589635c1e65c4708654e784da4f0fe2e46
a6b711a4e8db20755026231f7ed529a9014b2b6d
/ZZ_IGNORE/S17/class-s17/FinalExam/FinalExam.lean
6a540b924a84d71cde4f77b53e446e3c774ab5b5
[]
no_license
chaseboettner/cs-dm-1
b67d4a7e86f56bce59d2af115503769749d423b2
80b35f2957ffaa45b8b7a4479a3570a2d6eb4db0
refs/heads/master
1,585,367,603,488
1,536,235,675,000
1,536,235,675,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,071
lean
/- PART I: LOGIC AND PROOF USING THE LEAN PROVER This section is worth 20% of the exam grade. There are five problems and one extra credit problem. -/ /- PROBLEM #1 Complete the proof by replacing the "sorry" stub with code to construct a proof of the proposition. You may want to start by replacing sorry with a hole (underscore), then hover over the underscore to see what assumptions you've got and what you have left to prove. You will see that you've assumed that P and Q are arbitrary propositions and that you have a proof of P ∧ Q. Apply the appropriate elimination and introduction rules, first to obtain the "smaller proofs" you'll need to construct the desired proof (by using elimination rules), then apply an introduction rule to get the result you need. The conclusion is a conjunction. That tells you what kind of introduction rule you need to use to complete the proof. It's not a bad idea to work "top down" by incrementally filling in the hole with expressions that can include new holes, continuing this "refinement" process until your proof is done. -/ theorem and_commutes: forall P Q: Prop, P ∧ Q → Q ∧ P := λ P Q pf, sorry /- PROBLEM #2 As you can see from the definition of and_commutes it is basically a function (see the lambda!) that takes two propositions, P and Q, as arguments, and that then returns a proof of P ∧ Q → Q ∧ P. No other proofs are needed as arguments in this case, as the proposition to be proved is logically valid. In this problem you use the fact that and_commutes can be "called" to build the two proofs that you will need to construct a proof of the stated equivalence. Recall than equivalence is written as both iff and as ↔. Read and understand the statement of the theorem first, then read the partial proof. Complete it by replacing "sorry" with a proof of the required type. You might again want to start by replacing sorry with a hole, then check to see the context and goal for that hole. And finally fill it with an expression that produces the required proof. Hint: The rule you need to apply is of course the introduction rule for ↔, also known as "iff". -/ theorem and_commutes_2: forall X Y: Prop, X ∧ Y ↔ Y ∧ X := λ (X Y: Prop), iff.intro (and_commutes X Y) _ /- PROBLEM #3 Replace sorry to complete the proof that if p, q, and r are arbitrary natural numbers, and p < q and q < r then p < r. Very strong hint: Lean already has a theorem (a rule), called lt.trans, that when given a proof of p < q and a proof of q < r generates a proof of p < r. -/ theorem my_lt_trans: ∀ p q r: nat, p < q → q < r → p < r := λ p q r pq qr, lt.trans pq qr /- PROBLEM #4 In Lean, write and prove the proposition that, for all natural numbers, p, q, and r, if p is equal to q then if q is equal to r, then r is equal to p. Don't reverse the order of variables in this expression when writing it in Lean. If we were just restating transitivity of equality, the third proposition would have been p = r, but here it's r = p. Hint: compose inference rules for two different properties of equality. Give your theorem the name, eq_rev_trans -/ -- your answer here theorem eq_rev_trans: ∀ p q r: nat, p=q → q=r → r=p := λ p q r pq qr, _ /- PROBLEM #5 In Lean, write and prove a theorem, called pqp, that states that, for any propositions, P and Q, P → Q → P. -/ -- your answer here theorem pqp: forall P Q: Prop, P → Q → P := _ /- EXTRA CREDIT. One of the inference rules that we did validate in the propositional logic unit, and that was described in intro.lean, but that we did not have time to talk about in class, states that false implies anything at all, even absurdities. Your task here is, in Lean, first to write and prove that false implies 0=1 (call the theorem f01) and then to write and prove a second theorem asserting that from false you can prove any proposition, Q, whatsoever (call it fQ). -/ theorem f01: false → 0=1 := sorry -- Your answer for the second part here
4ee9d3d45d1d49a799322d0f20191f21c17863fa
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/analysis/calculus/parametric_interval_integral.lean
f454383ad8c77f735ddfd16dda2695f213c664ae
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
6,702
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import analysis.calculus.parametric_integral import measure_theory.integral.interval_integral /-! # Derivatives of interval integrals depending on parameters In this file we restate theorems about derivatives of integrals depending on parameters for interval integrals. -/ open topological_space measure_theory filter metric open_locale topological_space filter interval variables {α 𝕜 : Type*} [measurable_space α] [linear_order α] [topological_space α] [order_topology α] [opens_measurable_space α] {μ : measure α} [is_R_or_C 𝕜] {E : Type*} [normed_group E] [normed_space ℝ E] [normed_space 𝕜 E] [complete_space E] [second_countable_topology E] [measurable_space E] [borel_space E] {H : Type*} [normed_group H] [normed_space 𝕜 H] [second_countable_topology $ H →L[𝕜] E] {a b : α} {bound : α → ℝ} {ε : ℝ} namespace interval_integral /-- Differentiation under integral of `x ↦ ∫ t in a..b, F x t` at a given point `x₀`, assuming `F x₀` is integrable, `x ↦ F x a` is locally Lipschitz on a ball around `x₀` for ae `a` (with a ball radius independent of `a`) with integrable Lipschitz bound, and `F x` is ae-measurable for `x` in a possibly smaller neighborhood of `x₀`. -/ lemma has_fderiv_at_integral_of_dominated_loc_of_lip {F : H → α → E} {F' : α → (H →L[𝕜] E)} {x₀ : H} (ε_pos : 0 < ε) (hF_meas : ∀ᶠ x in 𝓝 x₀, ae_measurable (F x) (μ.restrict (Ι a b))) (hF_int : interval_integrable (F x₀) μ a b) (hF'_meas : ae_measurable F' (μ.restrict (Ι a b))) (h_lip : ∀ᵐ t ∂μ, t ∈ Ι a b → lipschitz_on_with (real.nnabs $ bound t) (λ x, F x t) (ball x₀ ε)) (bound_integrable : interval_integrable bound μ a b) (h_diff : ∀ᵐ t ∂μ, t ∈ Ι a b → has_fderiv_at (λ x, F x t) (F' t) x₀) : interval_integrable F' μ a b ∧ has_fderiv_at (λ x, ∫ t in a..b, F x t ∂μ) (∫ t in a..b, F' t ∂μ) x₀ := begin simp only [interval_integrable_iff, interval_integral_eq_integral_interval_oc, ← ae_restrict_iff' measurable_set_interval_oc] at *, have := has_fderiv_at_integral_of_dominated_loc_of_lip ε_pos hF_meas hF_int hF'_meas h_lip bound_integrable h_diff, exact ⟨this.1, this.2.const_smul _⟩ end /-- Differentiation under integral of `x ↦ ∫ F x a` at a given point `x₀`, assuming `F x₀` is integrable, `x ↦ F x a` is differentiable on a ball around `x₀` for ae `a` with derivative norm uniformly bounded by an integrable function (the ball radius is independent of `a`), and `F x` is ae-measurable for `x` in a possibly smaller neighborhood of `x₀`. -/ lemma has_fderiv_at_integral_of_dominated_of_fderiv_le {F : H → α → E} {F' : H → α → (H →L[𝕜] E)} {x₀ : H} (ε_pos : 0 < ε) (hF_meas : ∀ᶠ x in 𝓝 x₀, ae_measurable (F x) (μ.restrict (Ι a b))) (hF_int : interval_integrable (F x₀) μ a b) (hF'_meas : ae_measurable (F' x₀) (μ.restrict (Ι a b))) (h_bound : ∀ᵐ t ∂μ, t ∈ Ι a b → ∀ x ∈ ball x₀ ε, ∥F' x t∥ ≤ bound t) (bound_integrable : interval_integrable bound μ a b) (h_diff : ∀ᵐ t ∂μ, t ∈ Ι a b → ∀ x ∈ ball x₀ ε, has_fderiv_at (λ x, F x t) (F' x t) x) : has_fderiv_at (λ x, ∫ t in a..b, F x t ∂μ) (∫ t in a..b, F' x₀ t ∂μ) x₀ := begin simp only [interval_integrable_iff, interval_integral_eq_integral_interval_oc, ← ae_restrict_iff' measurable_set_interval_oc] at *, exact (has_fderiv_at_integral_of_dominated_of_fderiv_le ε_pos hF_meas hF_int hF'_meas h_bound bound_integrable h_diff).const_smul _ end /-- Derivative under integral of `x ↦ ∫ F x a` at a given point `x₀ : 𝕜`, `𝕜 = ℝ` or `𝕜 = ℂ`, assuming `F x₀` is integrable, `x ↦ F x a` is locally Lipschitz on a ball around `x₀` for ae `a` (with ball radius independent of `a`) with integrable Lipschitz bound, and `F x` is ae-measurable for `x` in a possibly smaller neighborhood of `x₀`. -/ lemma has_deriv_at_integral_of_dominated_loc_of_lip {F : 𝕜 → α → E} {F' : α → E} {x₀ : 𝕜} (ε_pos : 0 < ε) (hF_meas : ∀ᶠ x in 𝓝 x₀, ae_measurable (F x) (μ.restrict (Ι a b))) (hF_int : interval_integrable (F x₀) μ a b) (hF'_meas : ae_measurable F' (μ.restrict (Ι a b))) (h_lipsch : ∀ᵐ t ∂μ, t ∈ Ι a b → lipschitz_on_with (real.nnabs $ bound t) (λ x, F x t) (ball x₀ ε)) (bound_integrable : interval_integrable (bound : α → ℝ) μ a b) (h_diff : ∀ᵐ t ∂μ, t ∈ Ι a b → has_deriv_at (λ x, F x t) (F' t) x₀) : (interval_integrable F' μ a b) ∧ has_deriv_at (λ x, ∫ t in a..b, F x t ∂μ) (∫ t in a..b, F' t ∂μ) x₀ := begin simp only [interval_integrable_iff, interval_integral_eq_integral_interval_oc, ← ae_restrict_iff' measurable_set_interval_oc] at *, have := has_deriv_at_integral_of_dominated_loc_of_lip ε_pos hF_meas hF_int hF'_meas h_lipsch bound_integrable h_diff, exact ⟨this.1, this.2.const_smul _⟩ end /-- Derivative under integral of `x ↦ ∫ F x a` at a given point `x₀ : 𝕜`, `𝕜 = ℝ` or `𝕜 = ℂ`, assuming `F x₀` is integrable, `x ↦ F x a` is differentiable on an interval around `x₀` for ae `a` (with interval radius independent of `a`) with derivative uniformly bounded by an integrable function, and `F x` is ae-measurable for `x` in a possibly smaller neighborhood of `x₀`. -/ lemma has_deriv_at_integral_of_dominated_loc_of_deriv_le {F : 𝕜 → α → E} {F' : 𝕜 → α → E} {x₀ : 𝕜} (ε_pos : 0 < ε) (hF_meas : ∀ᶠ x in 𝓝 x₀, ae_measurable (F x) (μ.restrict (Ι a b))) (hF_int : interval_integrable (F x₀) μ a b) (hF'_meas : ae_measurable (F' x₀) (μ.restrict (Ι a b))) (h_bound : ∀ᵐ t ∂μ, t ∈ Ι a b → ∀ x ∈ ball x₀ ε, ∥F' x t∥ ≤ bound t) (bound_integrable : interval_integrable bound μ a b) (h_diff : ∀ᵐ t ∂μ, t ∈ Ι a b → ∀ x ∈ ball x₀ ε, has_deriv_at (λ x, F x t) (F' x t) x) : (interval_integrable (F' x₀) μ a b) ∧ has_deriv_at (λ x, ∫ t in a..b, F x t ∂μ) (∫ t in a..b, F' x₀ t ∂μ) x₀ := begin simp only [interval_integrable_iff, interval_integral_eq_integral_interval_oc, ← ae_restrict_iff' measurable_set_interval_oc] at *, have := has_deriv_at_integral_of_dominated_loc_of_deriv_le ε_pos hF_meas hF_int hF'_meas h_bound bound_integrable h_diff, exact ⟨this.1, this.2.const_smul _⟩ end end interval_integral
7d9ce01ee037a4b189098deafd8ce5677ff2fb2b
5756a081670ba9c1d1d3fca7bd47cb4e31beae66
/Mathport/Syntax/Translate/Tactic/Mathlib/Linarith.lean
b05ae4933eacdb23db078816e02c7f4deb1065f2
[ "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
1,946
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathport.Syntax.Translate.Tactic.Basic import Mathport.Syntax.Translate.Tactic.Lean3 open Lean namespace Mathport.Translate.Tactic open AST3 Parser -- # tactic.linarith @[tr_tactic linarith] def trLinarith : TacM Syntax.Tactic := do let bang ← parse (tk "!")? let o := optTk (← parse onlyFlag) let args := (← (← parse optPExprList).mapM (trExpr ·)).asNonempty let cfg ← mkConfigStx? (← (← expr?).mapM (trExpr ·)) match bang with | none => `(tactic| linarith $(cfg)? $[only%$o]? $[[$args,*]]?) | some _ => `(tactic| linarith! $(cfg)? $[only%$o]? $[[$args,*]]?) @[tr_tactic nlinarith] def trNLinarith : TacM Syntax.Tactic := do let bang ← parse (tk "!")? let o := optTk (← parse onlyFlag) let args := (← (← parse optPExprList).mapM (trExpr ·)).asNonempty let cfg ← mkConfigStx? (← (← expr?).mapM (trExpr ·)) match bang with | none => `(tactic| nlinarith $(cfg)? $[only%$o]? $[[$args,*]]?) | some _ => `(tactic| nlinarith! $(cfg)? $[only%$o]? $[[$args,*]]?) -- # tactic.zify @[tr_user_attr zify] def trZifyAttr := tagAttr `zify @[tr_tactic zify] def trZify : TacM Syntax.Tactic := do let hs := (← trSimpArgs (← parse simpArgList)).asNonempty `(tactic| zify $[[$hs,*]]? $(← trLoc (← parse location))?) -- # tactic.qify @[tr_user_attr qify] def trQifyAttr := tagAttr `qify @[tr_tactic qify] def trQify : TacM Syntax.Tactic := do let hs := (← trSimpArgs (← parse simpArgList)).asNonempty `(tactic| qify $[[$hs,*]]? $(← trLoc (← parse location))?) -- # tactic.polyrith @[tr_tactic polyrith] def trPolyrith : TacM Syntax.Tactic := do let o := optTk (← parse onlyFlag) let args := (← (← parse optPExprList).mapM (trExpr ·)).asNonempty `(tactic| polyrith $[only%$o]? $[[$args,*]]?)
9b1e4c038ee28131016e2c16d210e229568b3349
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/library/init/sigma.lean
82961d6349d67738eab10cd11ec511a195ebb9f0
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
881
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Jeremy Avigad, Floris van Doorn -/ prelude import init.datatypes init.num init.wf init.logic definition dpair := @sigma.mk notation `Σ` binders `, ` r:(scoped P, sigma P) := r universe variables u v lemma ex_of_sig {A : Type u} {P : A → Prop} : (Σ x, P x) → ∃ x, P x | ⟨x, hx⟩ := ⟨x, hx⟩ namespace sigma notation `pr₁` := pr1 notation `pr₂` := pr2 namespace ops postfix `.1`:(max+1) := pr1 postfix `.2`:(max+1) := pr2 end ops open ops variables {A : Type u} {B : A → Type v} protected theorem eq : ∀ {p₁ p₂ : Σ a : A, B a} (H₁ : p₁.1 = p₂.1), (eq.rec_on H₁ p₁.2 : B p₂.1) = p₂.2 → p₁ = p₂ | ⟨a, b⟩ ⟨.a, .b⟩ rfl rfl := rfl end sigma
fc1f3926da9395b7848f25b0b6fcc08d1bc6816d
853df553b1d6ca524e3f0a79aedd32dde5d27ec3
/src/ring_theory/adjoin_root.lean
85f73107e904a690d53c76f4612313b84e59b783
[ "Apache-2.0" ]
permissive
DanielFabian/mathlib
efc3a50b5dde303c59eeb6353ef4c35a345d7112
f520d07eba0c852e96fe26da71d85bf6d40fcc2a
refs/heads/master
1,668,739,922,971
1,595,201,756,000
1,595,201,756,000
279,469,476
0
0
null
1,594,696,604,000
1,594,696,604,000
null
UTF-8
Lean
false
false
4,940
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Chris Hughes Adjoining roots of polynomials -/ import data.polynomial import ring_theory.adjoin import ring_theory.principal_ideal_domain /-! # Adjoining roots of polynomials This file defines the commutative ring `adjoin_root f`, the ring R[X]/(f) obtained from a commutative ring `R` and a polynomial `f : R[X]`. If furthermore `R` is a field and `f` is irreducible, the field structure on `adjoin_root f` is constructed. ## Main definitions and results The main definitions are in the `adjoin_root` namespace. * `mk f : polynomial R →+* adjoin_root f`, the natural ring homomorphism. * `of f : R →+* adjoin_root f`, the natural ring homomorphism. * `root f : adjoin_root f`, the image of X in R[X]/(f). * `lift (i : R →+* S) (x : S) (h : f.eval₂ i x = 0) : (adjoin_root f) →+* S`, the ring homomorphism from R[X]/(f) to S extending `i : R →+* S` and sending `X` to `x`. -/ noncomputable theory open_locale big_operators universes u v w variables {R : Type u} {S : Type v} {K : Type w} open polynomial ideal /-- Adjoin a root of a polynomial `f` to a commutative ring `R`. We define the new ring as the quotient of `R` by the principal ideal of `f`. -/ def adjoin_root [comm_ring R] (f : polynomial R) : Type u := ideal.quotient (span {f} : ideal (polynomial R)) namespace adjoin_root section comm_ring variables [comm_ring R] (f : polynomial R) instance : comm_ring (adjoin_root f) := ideal.quotient.comm_ring _ instance : inhabited (adjoin_root f) := ⟨0⟩ instance : decidable_eq (adjoin_root f) := classical.dec_eq _ /-- Ring homomorphism from `R[x]` to `adjoin_root f` sending `X` to the `root`. -/ def mk : polynomial R →+* adjoin_root f := ideal.quotient.mk_hom _ @[elab_as_eliminator] theorem induction_on {C : adjoin_root f → Prop} (x : adjoin_root f) (ih : ∀ p : polynomial R, C (mk f p)) : C x := quotient.induction_on' x ih /-- Embedding of the original ring `R` into `adjoin_root f`. -/ def of : R →+* adjoin_root f := (mk f).comp (ring_hom.of C) instance : algebra R (adjoin_root f) := (of f).to_algebra @[simp] lemma algebra_map_eq : algebra_map R (adjoin_root f) = of f := rfl /-- The adjoined root. -/ def root : adjoin_root f := mk f X variables {f} instance adjoin_root.has_coe_t : has_coe_t R (adjoin_root f) := ⟨of f⟩ @[simp] lemma mk_self : mk f f = 0 := quotient.sound' (mem_span_singleton.2 $ by simp) @[simp] lemma mk_C (x : R) : mk f (C x) = x := rfl @[simp] lemma mk_X : mk f X = root f := rfl @[simp] lemma aeval_eq (p : polynomial R) : aeval R (adjoin_root f) (root f) p = mk f p := polynomial.induction_on p (λ x, by { rw aeval_C, refl }) (λ p q ihp ihq, by rw [alg_hom.map_add, ring_hom.map_add, ihp, ihq]) (λ n x ih, by { rw [alg_hom.map_mul, aeval_C, alg_hom.map_pow, aeval_X, ring_hom.map_mul, mk_C, ring_hom.map_pow, mk_X], refl }) theorem adjoin_root_eq_top : algebra.adjoin R ({root f} : set (adjoin_root f)) = ⊤ := algebra.eq_top_iff.2 $ λ x, induction_on f x $ λ p, (algebra.adjoin_singleton_eq_range R (root f)).symm ▸ ⟨p, aeval_eq p⟩ @[simp] lemma eval₂_root (f : polynomial R) : f.eval₂ (of f) (root f) = 0 := by rw [← algebra_map_eq, ← aeval_def, aeval_eq, mk_self] lemma is_root_root (f : polynomial R) : is_root (f.map (of f)) (root f) := by rw [is_root, eval_map, eval₂_root] variables [comm_ring S] /-- Lift a ring homomorphism `i : R →+* S` to `adjoin_root f →+* S`. -/ def lift (i : R →+* S) (x : S) (h : f.eval₂ i x = 0) : (adjoin_root f) →+* S := begin apply ideal.quotient.lift _ (eval₂_ring_hom i x), intros g H, rcases mem_span_singleton.1 H with ⟨y, hy⟩, rw [hy, ring_hom.map_mul, coe_eval₂_ring_hom, h, zero_mul] end variables {i : R →+* S} {a : S} {h : f.eval₂ i a = 0} @[simp] lemma lift_mk {g : polynomial R} : lift i a h (mk f g) = g.eval₂ i a := ideal.quotient.lift_mk _ _ _ @[simp] lemma lift_root : lift i a h (root f) = a := by rw [root, lift_mk, eval₂_X] @[simp] lemma lift_of {x : R} : lift i a h x = i x := by rw [← mk_C x, lift_mk, eval₂_C] @[simp] lemma lift_comp_of : (lift i a h).comp (of f) = i := ring_hom.ext $ λ _, @lift_of _ _ _ _ _ _ _ h _ end comm_ring variables [field K] {f : polynomial K} [irreducible f] instance is_maximal_span : is_maximal (span {f} : ideal (polynomial K)) := principal_ideal_ring.is_maximal_of_irreducible ‹irreducible f› noncomputable instance field : field (adjoin_root f) := ideal.quotient.field (span {f} : ideal (polynomial K)) lemma coe_injective : function.injective (coe : K → adjoin_root f) := (of f).injective variable (f) lemma mul_div_root_cancel : ((X - C (root f)) * (f.map (of f) / (X - C (root f))) : polynomial (adjoin_root f)) = f.map (of f) := mul_div_eq_iff_is_root.2 $ is_root_root _ end adjoin_root
1134a650b266f549b735f35045c20ce85b510b33
94e33a31faa76775069b071adea97e86e218a8ee
/src/data/nat/lattice.lean
9317ef18245566e2117e7d16e84cd8fe071f9586
[ "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
7,260
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, Floris van Doorn, Gabriel Ebner, Yury Kudryashov -/ import data.nat.part_enat import order.conditionally_complete_lattice /-! # Conditionally complete linear order structure on `ℕ` In this file we * define a `conditionally_complete_linear_order_bot` structure on `ℕ`; * define a `complete_linear_order` structure on `part_enat`; * prove a few lemmas about `supr`/`infi`/`set.Union`/`set.Inter` and natural numbers. -/ open set namespace nat open_locale classical noncomputable instance : has_Inf ℕ := ⟨λs, if h : ∃n, n ∈ s then @nat.find (λn, n ∈ s) _ h else 0⟩ noncomputable instance : has_Sup ℕ := ⟨λs, if h : ∃n, ∀a∈s, a ≤ n then @nat.find (λn, ∀a∈s, a ≤ n) _ h else 0⟩ lemma Inf_def {s : set ℕ} (h : s.nonempty) : Inf s = @nat.find (λn, n ∈ s) _ h := dif_pos _ lemma Sup_def {s : set ℕ} (h : ∃n, ∀a∈s, a ≤ n) : Sup s = @nat.find (λn, ∀a∈s, a ≤ n) _ h := dif_pos _ lemma _root_.set.infinite.nat.Sup_eq_zero {s : set ℕ} (h : s.infinite) : Sup s = 0 := dif_neg $ λ ⟨n, hn⟩, let ⟨k, hks, hk⟩ := h.exists_nat_lt n in (hn k hks).not_lt hk @[simp] lemma Inf_eq_zero {s : set ℕ} : Inf s = 0 ↔ 0 ∈ s ∨ s = ∅ := begin cases eq_empty_or_nonempty s, { subst h, simp only [or_true, eq_self_iff_true, iff_true, Inf, has_Inf.Inf, mem_empty_eq, exists_false, dif_neg, not_false_iff] }, { have := ne_empty_iff_nonempty.mpr h, simp only [this, or_false, nat.Inf_def, h, nat.find_eq_zero] } end @[simp] lemma Inf_empty : Inf ∅ = 0 := by { rw Inf_eq_zero, right, refl } @[simp] lemma infi_of_empty {ι : Sort*} [is_empty ι] (f : ι → ℕ) : infi f = 0 := by rw [infi_of_empty', Inf_empty] lemma Inf_mem {s : set ℕ} (h : s.nonempty) : Inf s ∈ s := by { rw [nat.Inf_def h], exact nat.find_spec h } lemma not_mem_of_lt_Inf {s : set ℕ} {m : ℕ} (hm : m < Inf s) : m ∉ s := begin cases eq_empty_or_nonempty s, { subst h, apply not_mem_empty }, { rw [nat.Inf_def h] at hm, exact nat.find_min h hm } end protected lemma Inf_le {s : set ℕ} {m : ℕ} (hm : m ∈ s) : Inf s ≤ m := by { rw [nat.Inf_def ⟨m, hm⟩], exact nat.find_min' ⟨m, hm⟩ hm } lemma nonempty_of_pos_Inf {s : set ℕ} (h : 0 < Inf s) : s.nonempty := begin by_contradiction contra, rw set.not_nonempty_iff_eq_empty at contra, have h' : Inf s ≠ 0, { exact ne_of_gt h, }, apply h', rw nat.Inf_eq_zero, right, assumption, end lemma nonempty_of_Inf_eq_succ {s : set ℕ} {k : ℕ} (h : Inf s = k + 1) : s.nonempty := nonempty_of_pos_Inf (h.symm ▸ (succ_pos k) : Inf s > 0) lemma eq_Ici_of_nonempty_of_upward_closed {s : set ℕ} (hs : s.nonempty) (hs' : ∀ (k₁ k₂ : ℕ), k₁ ≤ k₂ → k₁ ∈ s → k₂ ∈ s) : s = Ici (Inf s) := ext (λ n, ⟨λ H, nat.Inf_le H, λ H, hs' (Inf s) n H (Inf_mem hs)⟩) lemma Inf_upward_closed_eq_succ_iff {s : set ℕ} (hs : ∀ (k₁ k₂ : ℕ), k₁ ≤ k₂ → k₁ ∈ s → k₂ ∈ s) (k : ℕ) : Inf s = k + 1 ↔ k + 1 ∈ s ∧ k ∉ s := begin split, { intro H, rw [eq_Ici_of_nonempty_of_upward_closed (nonempty_of_Inf_eq_succ H) hs, H, mem_Ici, mem_Ici], exact ⟨le_rfl, k.not_succ_le_self⟩, }, { rintro ⟨H, H'⟩, rw [Inf_def (⟨_, H⟩ : s.nonempty), find_eq_iff], exact ⟨H, λ n hnk hns, H' $ hs n k (lt_succ_iff.mp hnk) hns⟩, }, end /-- This instance is necessary, otherwise the lattice operations would be derived via conditionally_complete_linear_order_bot and marked as noncomputable. -/ instance : lattice ℕ := linear_order.to_lattice noncomputable instance : conditionally_complete_linear_order_bot ℕ := { Sup := Sup, Inf := Inf, le_cSup := assume s a hb ha, by rw [Sup_def hb]; revert a ha; exact @nat.find_spec _ _ hb, cSup_le := assume s a hs ha, by rw [Sup_def ⟨a, ha⟩]; exact nat.find_min' _ ha, le_cInf := assume s a hs hb, by rw [Inf_def hs]; exact hb (@nat.find_spec (λn, n ∈ s) _ _), cInf_le := assume s a hb ha, by rw [Inf_def ⟨a, ha⟩]; exact nat.find_min' _ ha, cSup_empty := begin simp only [Sup_def, set.mem_empty_eq, forall_const, forall_prop_of_false, not_false_iff, exists_const], apply bot_unique (nat.find_min' _ _), trivial end, .. (infer_instance : order_bot ℕ), .. (linear_order.to_lattice : lattice ℕ), .. (infer_instance : linear_order ℕ) } lemma Sup_mem {s : set ℕ} (h₁ : s.nonempty) (h₂ : bdd_above s) : Sup s ∈ s := let ⟨k, hk⟩ := h₂ in h₁.cSup_mem ((finite_le_nat k).subset hk) lemma Inf_add {n : ℕ} {p : ℕ → Prop} (hn : n ≤ Inf {m | p m}) : Inf {m | p (m + n)} + n = Inf {m | p m} := begin obtain h | ⟨m, hm⟩ := {m | p (m + n)}.eq_empty_or_nonempty, { rw [h, nat.Inf_empty, zero_add], obtain hnp | hnp := hn.eq_or_lt, { exact hnp }, suffices hp : p (Inf {m | p m} - n + n), { exact (h.subset hp).elim }, rw tsub_add_cancel_of_le hn, exact Inf_mem (nonempty_of_pos_Inf $ n.zero_le.trans_lt hnp) }, { have hp : ∃ n, n ∈ {m | p m} := ⟨_, hm⟩, rw [nat.Inf_def ⟨m, hm⟩, nat.Inf_def hp], rw [nat.Inf_def hp] at hn, exact find_add hn } end lemma Inf_add' {n : ℕ} {p : ℕ → Prop} (h : 0 < Inf {m | p m}) : Inf {m | p m} + n = Inf {m | p (m - n)} := begin convert Inf_add _, { simp_rw add_tsub_cancel_right }, obtain ⟨m, hm⟩ := nonempty_of_pos_Inf h, refine le_cInf ⟨m + n, _⟩ (λ b hb, le_of_not_lt $ λ hbn, ne_of_mem_of_not_mem _ (not_mem_of_lt_Inf h) (tsub_eq_zero_of_le hbn.le)), { dsimp, rwa add_tsub_cancel_right }, { exact hb } end section variables {α : Type*} [complete_lattice α] lemma supr_lt_succ (u : ℕ → α) (n : ℕ) : (⨆ k < n + 1, u k) = (⨆ k < n, u k) ⊔ u n := by simp [nat.lt_succ_iff_lt_or_eq, supr_or, supr_sup_eq] lemma supr_lt_succ' (u : ℕ → α) (n : ℕ) : (⨆ k < n + 1, u k) = u 0 ⊔ (⨆ k < n, u (k + 1)) := by { rw ← sup_supr_nat_succ, simp } lemma infi_lt_succ (u : ℕ → α) (n : ℕ) : (⨅ k < n + 1, u k) = (⨅ k < n, u k) ⊓ u n := @supr_lt_succ αᵒᵈ _ _ _ lemma infi_lt_succ' (u : ℕ → α) (n : ℕ) : (⨅ k < n + 1, u k) = u 0 ⊓ (⨅ k < n, u (k + 1)) := @supr_lt_succ' αᵒᵈ _ _ _ end end nat namespace set variable {α : Type*} lemma bUnion_lt_succ (u : ℕ → set α) (n : ℕ) : (⋃ k < n + 1, u k) = (⋃ k < n, u k) ∪ u n := nat.supr_lt_succ u n lemma bUnion_lt_succ' (u : ℕ → set α) (n : ℕ) : (⋃ k < n + 1, u k) = u 0 ∪ (⋃ k < n, u (k + 1)) := nat.supr_lt_succ' u n lemma bInter_lt_succ (u : ℕ → set α) (n : ℕ) : (⋂ k < n + 1, u k) = (⋂ k < n, u k) ∩ u n := nat.infi_lt_succ u n lemma bInter_lt_succ' (u : ℕ → set α) (n : ℕ) : (⋂ k < n + 1, u k) = u 0 ∩ (⋂ k < n, u (k + 1)) := nat.infi_lt_succ' u n end set namespace part_enat open_locale classical noncomputable instance : complete_linear_order part_enat := { inf := (⊓), sup := (⊔), top := ⊤, bot := ⊥, le := (≤), lt := (<), .. part_enat.lattice, .. with_top_order_iso.symm.to_galois_insertion.lift_complete_lattice, .. part_enat.linear_order, } end part_enat
912e2fb1e5d58fc704872a7ff5330803b3fc8c38
94637389e03c919023691dcd05bd4411b1034aa5
/src/inClassNotes/typeclasses/functor.lean
adf5652419ebf1347060e1d07abe3b6aefa41dde
[]
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
7,603
lean
/- Lean provides the following functor typeclass. class functor (c : Type u → Type v) : Type := (map : Π {α β : Type u}, (α → β) → c α → c β) Key observation: polymorphic type builders, such as list α and option α, are of type, Type → Type. -/ #check @list #check @option /- So reasonable values for (c : Type → Type) here are values such as list, box, and option. It can be useful to think of such arguments as "container" types, or more generally as types that surround values of other types with some kind of "context." -/ /- Second key observation: the functor typeclass extends our interface to list, option, and other such types to include a new mapping operator, map. -/ /- Consider our list, option, and box types as types that augment values with surrounding context. For example, the nat, 3, can be put in a list context as [3], in an option context as (some 3), or in a box context as (box.mk 3). -/ universes u v structure box (α : Type u) : Type u := mk :: (val : α) /- We've already seen that we can define higher order functions that can *map* other function over such "containers". For example, given a function of type ℕ → bool, we can map it over a list of ℕ values to get a corresponding list of bool values, over an option ℕ, or over a box ℕ. In each case, we transform the value in the context without changing the structure of the context. So, for example, mapping over a list always returns a list of the same shape (length). -/ def box_map {α β : Type u} : (α → β) → box α → box β | f (box.mk a) := box.mk (f a) def list_map {α β : Type u} : (α → β) → list α → list β | f list.nil := [] | f (h::t) := f h::list_map f t def option_map {α β : Type u} : (α → β) → option α → option β | f none := none | f (some a) := some (f a) /- Now comes a key insight: we can characterize *fmap* as an overloaded operator, that takes a function and *some kind of* container (list, box, option, etc.), and that then maps that function over the contents of the container to produce a new container of exactly the same "shape" as the original (e.g., a list of the same length), with values derived by applying f to the values in the original "context/container". We can now really make sense of the type of any such overloaded "map" function: (fmap : Π {α β : Type u}, (α → β) → c α → c β) Given types α and β, a function, f : α → β, and a "container/context", (c α) -- such as list α or option α -- map will return a "context", of type (c β): the same kind of container, and one of the same shape, but now containing a value or a set of values as transformed by f. -/ /- As we see above, we can define implementations of such a map function for completely different types of context/container objects: lists, boxes, options, etc. We can abstract this collection of types (or here, polymorphic type builders) as a typeclass. One name we might give it is has_map. So a type builder such as list would be "in" this typeclass if it provided an instance of has_map, and that instance would hold the implementation of map for lists, namely list_map. -/ /- has_map typeclass -/ class has_map (c : Type u → Type v) : Type (max (u+1) v) := (map : Π {α β : Type u}, (α → β) → c α → c β) /- instances for box, list, option -/ #check list_map #check @list_map instance list_has_map : has_map list := ⟨ @list_map ⟩ instance option_has_map : has_map option := ⟨ @option_map ⟩ instance box_has_map : has_map box := ⟨ @box_map ⟩ /- In practice, the name "functor" is typically used for this typeclass. We can use Lean's definition of this typeclass. That said Lean's definition of functor requires an implementation of a second function, which we'll just ignore here. -/ #print functor instance option_as_function : functor option := ⟨ @option_map, sorry⟩ instance list_as_functor : functor list := ⟨ @list_map, sorry ⟩ instance box_as_functor : functor box := ⟨ @box_map, sorry ⟩ /- We can now define an overloaded map operator, usually called fmap. -/ -- first version def fmap' ( c : Type → Type ) [functor c] { α β : Type} (f : α → β) (l : c α) := functor.map f l #check @fmap' list #check @fmap' box #check @fmap' option #eval fmap' list nat.succ [1,2,3] #eval fmap' option nat.succ (some 1) #reduce fmap' box nat.succ (box.mk 3) -- final version open functor def fmap { c : Type → Type } [functor c] { α β : Type} (f : α → β) (l : c α) := map f l -- map over list #reduce fmap nat.succ [1,2,3,4,5] #reduce fmap nat.succ (some 1) #reduce fmap nat.succ (box.mk 3) #reduce fmap (λ n, n * n) [1,2,3,4,5] #reduce fmap (λ (s : string), s.length) ["Hello", "there", "how", "are", "you?"] /- So functor is a typeclass. You can think of it as an kind of abstract interface that can be used to extend the native interface of a type and its associated functions. To extend the interface of a particular type, you define a new instance. It defines how each element of the abstract interface is implemented for that type. Here, the instances define how the new abstract "map" function is impleented for each of the list, box, and option types. We can apply all of this machinery in several ways. One is to define overloaded operators. Here we define fmap as an overloaded map function. Given a type for which there is a typeclass, it finds the relevant instance, grabs the value in its map field, and uses it. -/ /- From here, you'll want to learn about the typeclasses called applicative and monad. Each one just defines abstract interfaces than can be implemented for various polymorphic type builders. As an example, you might define a general interface for applying a function *that is itself in a context* to a value that is in the same kind of context to get a result also in that context. To be more concrete, suppose you have a value of type (option α → β) and a value of type (option α). You can surely now come up with a way to "apply" the former to the latter to get a value of type option β. Now do the same thing for list and box. Now generalize from option and box to any kind of "container/context" type, and, voila, you have an interesting new typeclass, and what you need to create a instances for various container types. If this were a class just in functional programming, we'd go ahead and continue in this vein to cover the important monad typeclass, which has diverse applications involving compositions of operations and encapsulation of results produced by non purely functional means (e.g., I/O), so that non-functional "effects" can be safely integrated with functional code. I recommend that you now go read the typeclass chapters in Learn You a Haskell, if you're intersted. Rather, we now turn to another use of typeclasses, and that is to formalize abstract interfaces in mathematics. E.g., the concept of a group is really an interface concept! Something is a group if it *implements* the group "interface", which requires (1) a set of objects, (2) a binary operator, (3) an identity element, and (4) evidence that rules for being a group are followed. Many, many different structures can implement this interface. We'll now look at one, the Dihedral group on a square. This work, in turn, will set us up to make the transition to mathematial logic and proof construction -- because the kind of evidence we'll seek will come exactly in the form of *proofs* that specified rules are followed. -/
768d558bc0aeb29996c0e14e4b1fa51d31725159
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/topology/algebra/group.lean
a22f00ec3fc5d2510c0a9d6e6b31583dca500cf4
[ "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
28,728
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, Patrick Massot -/ import order.filter.pointwise import group_theory.quotient_group import topology.algebra.monoid import topology.homeomorph /-! # Theory of topological groups This file defines the following typeclasses: * `topological_group`, `topological_add_group`: multiplicative and additive topological groups, i.e., groups with continuous `(*)` and `(⁻¹)` / `(+)` and `(-)`; * `has_continuous_sub G` means that `G` has a continuous subtraction operation. There is an instance deducing `has_continuous_sub` from `topological_group` but we use a separate typeclass because, e.g., `ℕ` and `ℝ≥0` have continuous subtraction but are not additive groups. We also define `homeomorph` versions of several `equiv`s: `homeomorph.mul_left`, `homeomorph.mul_right`, `homeomorph.inv`, and prove a few facts about neighbourhood filters in groups. ## Tags topological space, group, topological group -/ open classical set filter topological_space function open_locale classical topological_space filter universes u v w x variables {α : Type u} {β : Type v} {G : Type w} {H : Type x} section continuous_mul_group /-! ### Groups with continuous multiplication In this section we prove a few statements about groups with continuous `(*)`. -/ variables [topological_space G] [group G] [has_continuous_mul G] /-- Multiplication from the left in a topological group as a homeomorphism. -/ @[to_additive "Addition from the left in a topological additive group as a homeomorphism."] protected def homeomorph.mul_left (a : G) : G ≃ₜ G := { continuous_to_fun := continuous_const.mul continuous_id, continuous_inv_fun := continuous_const.mul continuous_id, .. equiv.mul_left a } @[simp, to_additive] lemma homeomorph.coe_mul_left (a : G) : ⇑(homeomorph.mul_left a) = (*) a := rfl @[to_additive] lemma homeomorph.mul_left_symm (a : G) : (homeomorph.mul_left a).symm = homeomorph.mul_left a⁻¹ := by { ext, refl } @[to_additive] lemma is_open_map_mul_left (a : G) : is_open_map (λ x, a * x) := (homeomorph.mul_left a).is_open_map @[to_additive] lemma is_closed_map_mul_left (a : G) : is_closed_map (λ x, a * x) := (homeomorph.mul_left a).is_closed_map /-- Multiplication from the right in a topological group as a homeomorphism. -/ @[to_additive "Addition from the right in a topological additive group as a homeomorphism."] protected def homeomorph.mul_right (a : G) : G ≃ₜ G := { continuous_to_fun := continuous_id.mul continuous_const, continuous_inv_fun := continuous_id.mul continuous_const, .. equiv.mul_right a } @[to_additive] lemma is_open_map_mul_right (a : G) : is_open_map (λ x, x * a) := (homeomorph.mul_right a).is_open_map @[to_additive] lemma is_closed_map_mul_right (a : G) : is_closed_map (λ x, x * a) := (homeomorph.mul_right a).is_closed_map @[to_additive] lemma is_open_map_div_right (a : G) : is_open_map (λ x, x / a) := by simpa only [div_eq_mul_inv] using is_open_map_mul_right (a⁻¹) @[to_additive] lemma is_closed_map_div_right (a : G) : is_closed_map (λ x, x / a) := by simpa only [div_eq_mul_inv] using is_closed_map_mul_right (a⁻¹) @[to_additive] lemma discrete_topology_of_open_singleton_one (h : is_open ({1} : set G)) : discrete_topology G := begin rw ← singletons_open_iff_discrete, intro g, suffices : {g} = (λ (x : G), g⁻¹ * x) ⁻¹' {1}, { rw this, exact (continuous_mul_left (g⁻¹)).is_open_preimage _ h, }, simp only [mul_one, set.preimage_mul_left_singleton, eq_self_iff_true, inv_inv, set.singleton_eq_singleton_iff], end end continuous_mul_group section topological_group /-! ### Topological groups A topological group is a group in which the multiplication and inversion operations are continuous. Topological additive groups are defined in the same way. Equivalently, we can require that the division operation `λ x y, x * y⁻¹` (resp., subtraction) is continuous. -/ /-- A topological (additive) group is a group in which the addition and negation operations are continuous. -/ class topological_add_group (G : Type u) [topological_space G] [add_group G] extends has_continuous_add G : Prop := (continuous_neg : continuous (λa:G, -a)) /-- A topological group is a group in which the multiplication and inversion operations are continuous. -/ @[to_additive] class topological_group (G : Type*) [topological_space G] [group G] extends has_continuous_mul G : Prop := (continuous_inv : continuous (has_inv.inv : G → G)) variables [topological_space G] [group G] [topological_group G] export topological_group (continuous_inv) export topological_add_group (continuous_neg) @[to_additive] lemma continuous_on_inv {s : set G} : continuous_on has_inv.inv s := continuous_inv.continuous_on @[to_additive] lemma continuous_within_at_inv {s : set G} {x : G} : continuous_within_at has_inv.inv s x := continuous_inv.continuous_within_at @[to_additive] lemma continuous_at_inv {x : G} : continuous_at has_inv.inv x := continuous_inv.continuous_at @[to_additive] lemma tendsto_inv (a : G) : tendsto has_inv.inv (𝓝 a) (𝓝 (a⁻¹)) := continuous_at_inv /-- If a function converges to a value in a multiplicative topological group, then its inverse converges to the inverse of this value. For the version in normed fields assuming additionally that the limit is nonzero, use `tendsto.inv'`. -/ @[to_additive] lemma filter.tendsto.inv {f : α → G} {l : filter α} {y : G} (h : tendsto f l (𝓝 y)) : tendsto (λ x, (f x)⁻¹) l (𝓝 y⁻¹) := (continuous_inv.tendsto y).comp h variables [topological_space α] {f : α → G} {s : set α} {x : α} @[continuity, to_additive] lemma continuous.inv (hf : continuous f) : continuous (λx, (f x)⁻¹) := continuous_inv.comp hf attribute [continuity] continuous.neg -- TODO @[to_additive] lemma continuous_on.inv (hf : continuous_on f s) : continuous_on (λx, (f x)⁻¹) s := continuous_inv.comp_continuous_on hf @[to_additive] lemma continuous_within_at.inv (hf : continuous_within_at f s x) : continuous_within_at (λ x, (f x)⁻¹) s x := hf.inv @[instance, to_additive] instance [topological_space H] [group H] [topological_group H] : topological_group (G × H) := { continuous_inv := continuous_inv.prod_map continuous_inv } @[to_additive] instance pi.topological_group {C : β → Type*} [∀ b, topological_space (C b)] [∀ b, group (C b)] [∀ b, topological_group (C b)] : topological_group (Π b, C b) := { continuous_inv := continuous_pi (λ i, (continuous_apply i).inv) } variable (G) /-- Inversion in a topological group as a homeomorphism. -/ @[to_additive "Negation in a topological group as a homeomorphism."] protected def homeomorph.inv : G ≃ₜ G := { continuous_to_fun := continuous_inv, continuous_inv_fun := continuous_inv, .. equiv.inv G } @[to_additive] lemma nhds_one_symm : comap has_inv.inv (𝓝 (1 : G)) = 𝓝 (1 : G) := ((homeomorph.inv G).comap_nhds_eq _).trans (congr_arg nhds one_inv) /-- The map `(x, y) ↦ (x, xy)` as a homeomorphism. This is a shear mapping. -/ @[to_additive "The map `(x, y) ↦ (x, x + y)` as a homeomorphism. This is a shear mapping."] protected def homeomorph.shear_mul_right : G × G ≃ₜ G × G := { continuous_to_fun := continuous_fst.prod_mk continuous_mul, continuous_inv_fun := continuous_fst.prod_mk $ continuous_fst.inv.mul continuous_snd, .. equiv.prod_shear (equiv.refl _) equiv.mul_left } @[simp, to_additive] lemma homeomorph.shear_mul_right_coe : ⇑(homeomorph.shear_mul_right G) = λ z : G × G, (z.1, z.1 * z.2) := rfl @[simp, to_additive] lemma homeomorph.shear_mul_right_symm_coe : ⇑(homeomorph.shear_mul_right G).symm = λ z : G × G, (z.1, z.1⁻¹ * z.2) := rfl variable {G} @[to_additive] lemma inv_closure (s : set G) : (closure s)⁻¹ = closure s⁻¹ := (homeomorph.inv G).preimage_closure s /-- The (topological-space) closure of a subgroup of a space `M` with `has_continuous_mul` is itself a subgroup. -/ @[to_additive "The (topological-space) closure of an additive subgroup of a space `M` with `has_continuous_add` is itself an additive subgroup."] def subgroup.topological_closure (s : subgroup G) : subgroup G := { carrier := closure (s : set G), inv_mem' := λ g m, by simpa [←mem_inv, inv_closure] using m, ..s.to_submonoid.topological_closure } @[to_additive] instance subgroup.topological_closure_topological_group (s : subgroup G) : topological_group (s.topological_closure) := { continuous_inv := begin apply continuous_induced_rng, change continuous (λ p : s.topological_closure, (p : G)⁻¹), continuity, end ..s.to_submonoid.topological_closure_has_continuous_mul} lemma subgroup.subgroup_topological_closure (s : subgroup G) : s ≤ s.topological_closure := subset_closure lemma subgroup.is_closed_topological_closure (s : subgroup G) : is_closed (s.topological_closure : set G) := by convert is_closed_closure lemma subgroup.topological_closure_minimal (s : subgroup G) {t : subgroup G} (h : s ≤ t) (ht : is_closed (t : set G)) : s.topological_closure ≤ t := closure_minimal h ht @[to_additive exists_nhds_half_neg] lemma exists_nhds_split_inv {s : set G} (hs : s ∈ 𝓝 (1 : G)) : ∃ V ∈ 𝓝 (1 : G), ∀ (v ∈ V) (w ∈ V), v / w ∈ s := have ((λp : G × G, p.1 * p.2⁻¹) ⁻¹' s) ∈ 𝓝 ((1, 1) : G × G), from continuous_at_fst.mul continuous_at_snd.inv (by simpa), by simpa only [div_eq_mul_inv, nhds_prod_eq, mem_prod_self_iff, prod_subset_iff, mem_preimage] using this @[to_additive] lemma nhds_translation_mul_inv (x : G) : comap (λ y : G, y * x⁻¹) (𝓝 1) = 𝓝 x := ((homeomorph.mul_right x⁻¹).comap_nhds_eq 1).trans $ show 𝓝 (1 * x⁻¹⁻¹) = 𝓝 x, by simp @[simp, to_additive] lemma map_mul_left_nhds (x y : G) : map ((*) x) (𝓝 y) = 𝓝 (x * y) := (homeomorph.mul_left x).map_nhds_eq y @[to_additive] lemma map_mul_left_nhds_one (x : G) : map ((*) x) (𝓝 1) = 𝓝 x := by simp @[to_additive] lemma topological_group.ext {G : Type*} [group G] {t t' : topological_space G} (tg : @topological_group G t _) (tg' : @topological_group G t' _) (h : @nhds G t 1 = @nhds G t' 1) : t = t' := eq_of_nhds_eq_nhds $ λ x, by rw [← @nhds_translation_mul_inv G t _ _ x , ← @nhds_translation_mul_inv G t' _ _ x , ← h] @[to_additive] lemma topological_group.of_nhds_aux {G : Type*} [group G] [topological_space G] (hinv : tendsto (λ (x : G), x⁻¹) (𝓝 1) (𝓝 1)) (hleft : ∀ (x₀ : G), 𝓝 x₀ = map (λ (x : G), x₀ * x) (𝓝 1)) (hconj : ∀ (x₀ : G), map (λ (x : G), x₀ * x * x₀⁻¹) (𝓝 1) ≤ 𝓝 1) : continuous (λ x : G, x⁻¹) := begin rw continuous_iff_continuous_at, rintros x₀, have key : (λ x, (x₀*x)⁻¹) = (λ x, x₀⁻¹*x) ∘ (λ x, x₀*x*x₀⁻¹) ∘ (λ x, x⁻¹), by {ext ; simp[mul_assoc] }, calc map (λ x, x⁻¹) (𝓝 x₀) = map (λ x, x⁻¹) (map (λ x, x₀*x) $ 𝓝 1) : by rw hleft ... = map (λ x, (x₀*x)⁻¹) (𝓝 1) : by rw filter.map_map ... = map (((λ x, x₀⁻¹*x) ∘ (λ x, x₀*x*x₀⁻¹)) ∘ (λ x, x⁻¹)) (𝓝 1) : by rw key ... = map ((λ x, x₀⁻¹*x) ∘ (λ x, x₀*x*x₀⁻¹)) _ : by rw ← filter.map_map ... ≤ map ((λ x, x₀⁻¹ * x) ∘ λ x, x₀ * x * x₀⁻¹) (𝓝 1) : map_mono hinv ... = map (λ x, x₀⁻¹ * x) (map (λ x, x₀ * x * x₀⁻¹) (𝓝 1)) : filter.map_map ... ≤ map (λ x, x₀⁻¹ * x) (𝓝 1) : map_mono (hconj x₀) ... = 𝓝 x₀⁻¹ : (hleft _).symm end @[to_additive] lemma topological_group.of_nhds_one' {G : Type*} [group G] [topological_space G] (hmul : tendsto (uncurry ((*) : G → G → G)) ((𝓝 1) ×ᶠ 𝓝 1) (𝓝 1)) (hinv : tendsto (λ x : G, x⁻¹) (𝓝 1) (𝓝 1)) (hleft : ∀ x₀ : G, 𝓝 x₀ = map (λ x, x₀*x) (𝓝 1)) (hright : ∀ x₀ : G, 𝓝 x₀ = map (λ x, x*x₀) (𝓝 1)) : topological_group G := begin refine { continuous_mul := (has_continuous_mul.of_nhds_one hmul hleft hright).continuous_mul, continuous_inv := topological_group.of_nhds_aux hinv hleft _ }, intros x₀, suffices : map (λ (x : G), x₀ * x * x₀⁻¹) (𝓝 1) = 𝓝 1, by simp [this, le_refl], rw [show (λ x, x₀ * x * x₀⁻¹) = (λ x, x₀ * x) ∘ λ x, x*x₀⁻¹, by {ext, simp [mul_assoc] }, ← filter.map_map, ← hright, hleft x₀⁻¹, filter.map_map], convert map_id, ext, simp end @[to_additive] lemma topological_group.of_nhds_one {G : Type u} [group G] [topological_space G] (hmul : tendsto (uncurry ((*) : G → G → G)) ((𝓝 1) ×ᶠ 𝓝 1) (𝓝 1)) (hinv : tendsto (λ x : G, x⁻¹) (𝓝 1) (𝓝 1)) (hleft : ∀ x₀ : G, 𝓝 x₀ = map (λ x, x₀*x) (𝓝 1)) (hconj : ∀ x₀ : G, tendsto (λ x, x₀*x*x₀⁻¹) (𝓝 1) (𝓝 1)) : topological_group G := { continuous_mul := begin rw continuous_iff_continuous_at, rintros ⟨x₀, y₀⟩, have key : (λ (p : G × G), x₀ * p.1 * (y₀ * p.2)) = ((λ x, x₀*y₀*x) ∘ (uncurry (*)) ∘ (prod.map (λ x, y₀⁻¹*x*y₀) id)), by { ext, simp [uncurry, prod.map, mul_assoc] }, specialize hconj y₀⁻¹, rw inv_inv at hconj, calc map (λ (p : G × G), p.1 * p.2) (𝓝 (x₀, y₀)) = map (λ (p : G × G), p.1 * p.2) ((𝓝 x₀) ×ᶠ 𝓝 y₀) : by rw nhds_prod_eq ... = map (λ (p : G × G), x₀ * p.1 * (y₀ * p.2)) ((𝓝 1) ×ᶠ (𝓝 1)) : by rw [hleft x₀, hleft y₀, prod_map_map_eq, filter.map_map] ... = map (((λ x, x₀*y₀*x) ∘ (uncurry (*))) ∘ (prod.map (λ x, y₀⁻¹*x*y₀) id))((𝓝 1) ×ᶠ (𝓝 1)) : by rw key ... = map ((λ x, x₀*y₀*x) ∘ (uncurry (*))) ((map (λ x, y₀⁻¹*x*y₀) $ 𝓝 1) ×ᶠ (𝓝 1)) : by rw [← filter.map_map, ← prod_map_map_eq', map_id] ... ≤ map ((λ x, x₀*y₀*x) ∘ (uncurry (*))) ((𝓝 1) ×ᶠ (𝓝 1)) : map_mono (filter.prod_mono hconj $ le_refl _) ... = map (λ x, x₀*y₀*x) (map (uncurry (*)) ((𝓝 1) ×ᶠ (𝓝 1))) : by rw filter.map_map ... ≤ map (λ x, x₀*y₀*x) (𝓝 1) : map_mono hmul ... = 𝓝 (x₀*y₀) : (hleft _).symm end, continuous_inv := topological_group.of_nhds_aux hinv hleft hconj} @[to_additive] lemma topological_group.of_comm_of_nhds_one {G : Type*} [comm_group G] [topological_space G] (hmul : tendsto (uncurry ((*) : G → G → G)) ((𝓝 1) ×ᶠ 𝓝 1) (𝓝 1)) (hinv : tendsto (λ x : G, x⁻¹) (𝓝 1) (𝓝 1)) (hleft : ∀ x₀ : G, 𝓝 x₀ = map (λ x, x₀*x) (𝓝 1)) : topological_group G := topological_group.of_nhds_one hmul hinv hleft (by simpa using tendsto_id) end topological_group section quotient_topological_group variables [topological_space G] [group G] [topological_group G] (N : subgroup G) (n : N.normal) @[to_additive] instance {G : Type*} [group G] [topological_space G] (N : subgroup G) : topological_space (quotient_group.quotient N) := quotient.topological_space open quotient_group @[to_additive] lemma quotient_group.is_open_map_coe : is_open_map (coe : G → quotient N) := begin intros s s_op, change is_open ((coe : G → quotient N) ⁻¹' (coe '' s)), rw quotient_group.preimage_image_coe N s, exact is_open_Union (λ n, (continuous_mul_right _).is_open_preimage s s_op) end @[to_additive] instance topological_group_quotient [N.normal] : topological_group (quotient N) := { continuous_mul := begin have cont : continuous ((coe : G → quotient N) ∘ (λ (p : G × G), p.fst * p.snd)) := continuous_quot_mk.comp continuous_mul, have quot : quotient_map (λ p : G × G, ((p.1:quotient N), (p.2:quotient N))), { apply is_open_map.to_quotient_map, { exact (quotient_group.is_open_map_coe N).prod (quotient_group.is_open_map_coe N) }, { exact continuous_quot_mk.prod_map continuous_quot_mk }, { exact (surjective_quot_mk _).prod_map (surjective_quot_mk _) } }, exact (quotient_map.continuous_iff quot).2 cont, end, continuous_inv := begin have : continuous ((coe : G → quotient N) ∘ (λ (a : G), a⁻¹)) := continuous_quot_mk.comp continuous_inv, convert continuous_quotient_lift _ this, end } attribute [instance] topological_add_group_quotient end quotient_topological_group /-- A typeclass saying that `λ p : G × G, p.1 - p.2` is a continuous function. This property automatically holds for topological additive groups but it also holds, e.g., for `ℝ≥0`. -/ class has_continuous_sub (G : Type*) [topological_space G] [has_sub G] : Prop := (continuous_sub : continuous (λ p : G × G, p.1 - p.2)) @[priority 100] -- see Note [lower instance priority] instance topological_add_group.to_has_continuous_sub [topological_space G] [add_group G] [topological_add_group G] : has_continuous_sub G := ⟨by { simp only [sub_eq_add_neg], exact continuous_fst.add continuous_snd.neg }⟩ export has_continuous_sub (continuous_sub) section has_continuous_sub variables [topological_space G] [has_sub G] [has_continuous_sub G] lemma filter.tendsto.sub {f g : α → G} {l : filter α} {a b : G} (hf : tendsto f l (𝓝 a)) (hg : tendsto g l (𝓝 b)) : tendsto (λx, f x - g x) l (𝓝 (a - b)) := (continuous_sub.tendsto (a, b)).comp (hf.prod_mk_nhds hg) variables [topological_space α] {f g : α → G} {s : set α} {x : α} @[continuity] lemma continuous.sub (hf : continuous f) (hg : continuous g) : continuous (λ x, f x - g x) := continuous_sub.comp (hf.prod_mk hg : _) lemma continuous_within_at.sub (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) : continuous_within_at (λ x, f x - g x) s x := hf.sub hg lemma continuous_on.sub (hf : continuous_on f s) (hg : continuous_on g s) : continuous_on (λx, f x - g x) s := λ x hx, (hf x hx).sub (hg x hx) end has_continuous_sub lemma nhds_translation [topological_space G] [add_group G] [topological_add_group G] (x : G) : comap (λy:G, y - x) (𝓝 0) = 𝓝 x := by simpa only [sub_eq_add_neg] using nhds_translation_add_neg x /-- additive group with a neighbourhood around 0. Only used to construct a topology and uniform space. This is currently only available for commutative groups, but it can be extended to non-commutative groups too. -/ class add_group_with_zero_nhd (G : Type u) extends add_comm_group G := (Z [] : filter G) (zero_Z : pure 0 ≤ Z) (sub_Z : tendsto (λp:G×G, p.1 - p.2) (Z ×ᶠ Z) Z) namespace add_group_with_zero_nhd variables (G) [add_group_with_zero_nhd G] local notation `Z` := add_group_with_zero_nhd.Z @[priority 100] -- see Note [lower instance priority] instance : topological_space G := topological_space.mk_of_nhds $ λa, map (λx, x + a) (Z G) variables {G} lemma neg_Z : tendsto (λa:G, - a) (Z G) (Z G) := have tendsto (λa, (0:G)) (Z G) (Z G), by refine le_trans (assume h, _) zero_Z; simp [univ_mem_sets'] {contextual := tt}, have tendsto (λa:G, 0 - a) (Z G) (Z G), from sub_Z.comp (tendsto.prod_mk this tendsto_id), by simpa lemma add_Z : tendsto (λp:G×G, p.1 + p.2) (Z G ×ᶠ Z G) (Z G) := suffices tendsto (λp:G×G, p.1 - -p.2) (Z G ×ᶠ Z G) (Z G), by simpa [sub_eq_add_neg], sub_Z.comp (tendsto.prod_mk tendsto_fst (neg_Z.comp tendsto_snd)) lemma exists_Z_half {s : set G} (hs : s ∈ Z G) : ∃ V ∈ Z G, ∀ (v ∈ V) (w ∈ V), v + w ∈ s := begin have : ((λa:G×G, a.1 + a.2) ⁻¹' s) ∈ Z G ×ᶠ Z G := add_Z (by simpa using hs), rcases mem_prod_self_iff.1 this with ⟨V, H, H'⟩, exact ⟨V, H, prod_subset_iff.1 H'⟩ end lemma nhds_eq (a : G) : 𝓝 a = map (λx, x + a) (Z G) := topological_space.nhds_mk_of_nhds _ _ (assume a, calc pure a = map (λx, x + a) (pure 0) : by simp ... ≤ _ : map_mono zero_Z) (assume b s hs, let ⟨t, ht, eqt⟩ := exists_Z_half hs in have t0 : (0:G) ∈ t, by simpa using zero_Z ht, begin refine ⟨(λx:G, x + b) '' t, image_mem_map ht, _, _⟩, { refine set.image_subset_iff.2 (assume b hbt, _), simpa using eqt 0 t0 b hbt }, { rintros _ ⟨c, hb, rfl⟩, refine (Z G).sets_of_superset ht (assume x hxt, _), simpa [add_assoc] using eqt _ hxt _ hb } end) lemma nhds_zero_eq_Z : 𝓝 0 = Z G := by simp [nhds_eq]; exact filter.map_id @[priority 100] -- see Note [lower instance priority] instance : has_continuous_add G := ⟨ continuous_iff_continuous_at.2 $ assume ⟨a, b⟩, begin rw [continuous_at, nhds_prod_eq, nhds_eq, nhds_eq, nhds_eq, filter.prod_map_map_eq, tendsto_map'_iff], suffices : tendsto ((λx:G, (a + b) + x) ∘ (λp:G×G,p.1 + p.2)) (Z G ×ᶠ Z G) (map (λx:G, (a + b) + x) (Z G)), { simpa [(∘), add_comm, add_left_comm] }, exact tendsto_map.comp add_Z end ⟩ @[priority 100] -- see Note [lower instance priority] instance : topological_add_group G := ⟨continuous_iff_continuous_at.2 $ assume a, begin rw [continuous_at, nhds_eq, nhds_eq, tendsto_map'_iff], suffices : tendsto ((λx:G, x - a) ∘ (λx:G, -x)) (Z G) (map (λx:G, x - a) (Z G)), { simpa [(∘), add_comm, sub_eq_add_neg] using this }, exact tendsto_map.comp neg_Z end⟩ end add_group_with_zero_nhd section filter_mul section variables [topological_space G] [group G] [topological_group G] @[to_additive] lemma is_open.mul_left {s t : set G} : is_open t → is_open (s * t) := λ ht, begin have : ∀a, is_open ((λ (x : G), a * x) '' t) := assume a, is_open_map_mul_left a t ht, rw ← Union_mul_left_image, exact is_open_Union (λa, is_open_Union $ λha, this _), end @[to_additive] lemma is_open.mul_right {s t : set G} : is_open s → is_open (s * t) := λ hs, begin have : ∀a, is_open ((λ (x : G), x * a) '' s), assume a, apply is_open_map_mul_right, exact hs, rw ← Union_mul_right_image, exact is_open_Union (λa, is_open_Union $ λha, this _), end variables (G) lemma topological_group.t1_space (h : @is_closed G _ {1}) : t1_space G := ⟨assume x, by { convert is_closed_map_mul_right x _ h, simp }⟩ lemma topological_group.regular_space [t1_space G] : regular_space G := ⟨assume s a hs ha, let f := λ p : G × G, p.1 * (p.2)⁻¹ in have hf : continuous f := continuous_fst.mul continuous_snd.inv, -- a ∈ -s implies f (a, 1) ∈ -s, and so (a, 1) ∈ f⁻¹' (-s); -- and so can find t₁ t₂ open such that a ∈ t₁ × t₂ ⊆ f⁻¹' (-s) let ⟨t₁, t₂, ht₁, ht₂, a_mem_t₁, one_mem_t₂, t_subset⟩ := is_open_prod_iff.1 ((is_open_compl_iff.2 hs).preimage hf) a (1:G) (by simpa [f]) in begin use [s * t₂, ht₂.mul_left, λ x hx, ⟨x, 1, hx, one_mem_t₂, mul_one _⟩], rw [nhds_within, inf_principal_eq_bot, mem_nhds_iff], refine ⟨t₁, _, ht₁, a_mem_t₁⟩, rintros x hx ⟨y, z, hy, hz, yz⟩, have : x * z⁻¹ ∈ sᶜ := (prod_subset_iff.1 t_subset) x hx z hz, have : x * z⁻¹ ∈ s, rw ← yz, simpa, contradiction end⟩ local attribute [instance] topological_group.regular_space lemma topological_group.t2_space [t1_space G] : t2_space G := regular_space.t2_space G end section /-! Some results about an open set containing the product of two sets in a topological group. -/ variables [topological_space G] [group G] [topological_group G] /-- Given a compact set `K` inside an open set `U`, there is a open neighborhood `V` of `1` such that `KV ⊆ U`. -/ @[to_additive "Given a compact set `K` inside an open set `U`, there is a open neighborhood `V` of `0` such that `K + V ⊆ U`."] lemma compact_open_separated_mul {K U : set G} (hK : is_compact K) (hU : is_open U) (hKU : K ⊆ U) : ∃ V : set G, is_open V ∧ (1 : G) ∈ V ∧ K * V ⊆ U := begin let W : G → set G := λ x, (λ y, x * y) ⁻¹' U, have h1W : ∀ x, is_open (W x) := λ x, hU.preimage (continuous_mul_left x), have h2W : ∀ x ∈ K, (1 : G) ∈ W x := λ x hx, by simp only [mem_preimage, mul_one, hKU hx], choose V hV using λ x : K, exists_open_nhds_one_mul_subset ((h1W x).mem_nhds (h2W x.1 x.2)), let X : K → set G := λ x, (λ y, (x : G)⁻¹ * y) ⁻¹' (V x), obtain ⟨t, ht⟩ : ∃ t : finset ↥K, K ⊆ ⋃ i ∈ t, X i, { refine hK.elim_finite_subcover X (λ x, (hV x).1.preimage (continuous_mul_left x⁻¹)) _, intros x hx, rw [mem_Union], use ⟨x, hx⟩, rw [mem_preimage], convert (hV _).2.1, simp only [mul_left_inv, subtype.coe_mk] }, refine ⟨⋂ x ∈ t, V x, is_open_bInter (finite_mem_finset _) (λ x hx, (hV x).1), _, _⟩, { simp only [mem_Inter], intros x hx, exact (hV x).2.1 }, rintro _ ⟨x, y, hx, hy, rfl⟩, simp only [mem_Inter] at hy, have := ht hx, simp only [mem_Union, mem_preimage] at this, rcases this with ⟨z, h1z, h2z⟩, have : (z : G)⁻¹ * x * y ∈ W z := (hV z).2.2 (mul_mem_mul h2z (hy z h1z)), rw [mem_preimage] at this, convert this using 1, simp only [mul_assoc, mul_inv_cancel_left] end /-- A compact set is covered by finitely many left multiplicative translates of a set with non-empty interior. -/ @[to_additive "A compact set is covered by finitely many left additive translates of a set with non-empty interior."] lemma compact_covered_by_mul_left_translates {K V : set G} (hK : is_compact K) (hV : (interior V).nonempty) : ∃ t : finset G, K ⊆ ⋃ g ∈ t, (λ h, g * h) ⁻¹' V := begin obtain ⟨t, ht⟩ : ∃ t : finset G, K ⊆ ⋃ x ∈ t, interior (((*) x) ⁻¹' V), { refine hK.elim_finite_subcover (λ x, interior $ ((*) x) ⁻¹' V) (λ x, is_open_interior) _, cases hV with g₀ hg₀, refine λ g hg, mem_Union.2 ⟨g₀ * g⁻¹, _⟩, refine preimage_interior_subset_interior_preimage (continuous_const.mul continuous_id) _, rwa [mem_preimage, inv_mul_cancel_right] }, exact ⟨t, subset.trans ht $ bUnion_subset_bUnion_right $ λ g hg, interior_subset⟩ end /-- Every locally compact separable topological group is σ-compact. Note: this is not true if we drop the topological group hypothesis. -/ @[priority 100] instance separable_locally_compact_group.sigma_compact_space [separable_space G] [locally_compact_space G] : sigma_compact_space G := begin obtain ⟨L, hLc, hL1⟩ := exists_compact_mem_nhds (1 : G), refine ⟨⟨λ n, (λ x, x * dense_seq G n) ⁻¹' L, _, _⟩⟩, { intro n, exact (homeomorph.mul_right _).compact_preimage.mpr hLc }, { refine Union_eq_univ_iff.2 (λ x, _), obtain ⟨_, ⟨n, rfl⟩, hn⟩ : (range (dense_seq G) ∩ (λ y, x * y) ⁻¹' L).nonempty, { rw [← (homeomorph.mul_left x).apply_symm_apply 1] at hL1, exact (dense_range_dense_seq G).inter_nhds_nonempty ((homeomorph.mul_left x).continuous.continuous_at $ hL1) }, exact ⟨n, hn⟩ } end end section variables [topological_space G] [comm_group G] [topological_group G] @[to_additive] lemma nhds_mul (x y : G) : 𝓝 (x * y) = 𝓝 x * 𝓝 y := filter_eq $ set.ext $ assume s, begin rw [← nhds_translation_mul_inv x, ← nhds_translation_mul_inv y, ← nhds_translation_mul_inv (x*y)], split, { rintros ⟨t, ht, ts⟩, rcases exists_nhds_one_split ht with ⟨V, V1, h⟩, refine ⟨(λa, a * x⁻¹) ⁻¹' V, (λa, a * y⁻¹) ⁻¹' V, ⟨V, V1, subset.refl _⟩, ⟨V, V1, subset.refl _⟩, _⟩, rintros a ⟨v, w, v_mem, w_mem, rfl⟩, apply ts, simpa [mul_comm, mul_assoc, mul_left_comm] using h (v * x⁻¹) v_mem (w * y⁻¹) w_mem }, { rintros ⟨a, c, ⟨b, hb, ba⟩, ⟨d, hd, dc⟩, ac⟩, refine ⟨b ∩ d, inter_mem_sets hb hd, assume v, _⟩, simp only [preimage_subset_iff, mul_inv_rev, mem_preimage] at *, rintros ⟨vb, vd⟩, refine ac ⟨v * y⁻¹, y, _, _, _⟩, { rw ← mul_assoc _ _ _ at vb, exact ba _ vb }, { apply dc y, rw mul_right_inv, exact mem_of_mem_nhds hd }, { simp only [inv_mul_cancel_right] } } end @[to_additive] lemma nhds_is_mul_hom : is_mul_hom (λx:G, 𝓝 x) := ⟨λ_ _, nhds_mul _ _⟩ end end filter_mul instance additive.topological_add_group {G} [h : topological_space G] [group G] [topological_group G] : @topological_add_group (additive G) h _ := { continuous_neg := @continuous_inv G _ _ _ } instance multiplicative.topological_group {G} [h : topological_space G] [add_group G] [topological_add_group G] : @topological_group (multiplicative G) h _ := { continuous_inv := @continuous_neg G _ _ _ } namespace units variables [monoid α] [topological_space α] [has_continuous_mul α] instance : topological_group (units α) := { continuous_inv := continuous_induced_rng ((continuous_unop.comp (continuous_snd.comp (@continuous_embed_product α _ _))).prod_mk (continuous_op.comp continuous_coe)) } end units
afd3bee674c039223da53d2d89e70be9a1324fc7
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/category/Group/preadditive.lean
04317799f588e0b34b70655922ee74e8feb198d0
[ "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
608
lean
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import algebra.category.Group.basic import category_theory.preadditive.basic /-! # The category of additive commutative groups is preadditive. -/ open category_theory universe u namespace AddCommGroup instance : preadditive AddCommGroup := { add_comp' := λ P Q R f f' g, show (f + f') ≫ g = f ≫ g + f' ≫ g, by { ext, simp }, comp_add' := λ P Q R f g g', show f ≫ (g + g') = f ≫ g + f ≫ g', by { ext, simp } } end AddCommGroup
7ba1a5ee9acaa1a14f42dbfee7ea3424b5577a60
94e33a31faa76775069b071adea97e86e218a8ee
/archive/imo/imo1988_q6.lean
66f901652cc15c61db50b5aaa117fd40e3eb8468
[ "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
13,863
lean
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import data.nat.prime import data.rat.defs import order.well_founded import tactic.linarith /-! # IMO 1988 Q6 and constant descent Vieta jumping Question 6 of IMO1988 is somewhat (in)famous. Several expert problem solvers could not tackle the question within the given time limit. The problem lead to the introduction of a new proof technique, so called “Vieta jumping”. In this file we formalise constant descent Vieta jumping, and apply this to prove Q6 of IMO1988. To illustrate the technique, we also prove a similar result. -/ -- open_locale classical local attribute [instance] classical.prop_decidable local attribute [simp] sq /-- Constant descent Vieta jumping. This proof technique allows one to prove an arbitrary proposition `claim`, by running a descent argument on a hyperbola `H` in the first quadrant of the plane, under the following conditions: * `h₀` : There exists an integral point `(x,y)` on the hyperbola `H`. * `H_symm` : The hyperbola has a symmetry along the diagonal in the plane. * `H_zero` : If an integral point `(x,0)` lies on the hyperbola `H`, then `claim` is true. * `H_diag` : If an integral point `(x,x)` lies on the hyperbola `H`, then `claim` is true. * `H_desc` : If `(x,y)` is an integral point on the hyperbola `H`, with `x < y` then there exists a “smaller” point on `H`: a point `(x',y')` with `x' < y' ≤ x`. For reasons of usability, the hyperbola `H` is implemented as an arbitrary predicate. (In question 6 of IMO1988, where this proof technique was first developped, the predicate `claim` would be `∃ (d : ℕ), d ^ 2 = k` for some natural number `k`, and the predicate `H` would be `λ a b, a * a + b * b = (a * b + 1) * k`.) To ensure that the predicate `H` actually describes a hyperbola, the user must provide arguments `B` and `C` that are used as coefficients for a quadratic equation. Finally, `H_quad` is the proof obligation that the quadratic equation `(y:ℤ) * y - B x * y + C x = 0` describes the same hyperbola as the predicate `H`. For extra flexibility, one must provide a predicate `base` on the integral points in the plane. In the descent step `H_desc` this will give the user the additional assumption that the point `(x,y)` does not lie in this base locus. The user must provide a proof that the proposition `claim` is true if there exists an integral point `(x,y)` on the hyperbola `H` that lies in the base locus. If such a base locus is not necessary, once can simply let it be `λ x y, false`. -/ lemma constant_descent_vieta_jumping (x y : ℕ) {claim : Prop} {H : ℕ → ℕ → Prop} (h₀ : H x y) (B : ℕ → ℤ) (C : ℕ → ℤ) (base : ℕ → ℕ → Prop) (H_quad : ∀ {x y}, H x y ↔ (y:ℤ) * y - B x * y + C x = 0) (H_symm : ∀ {x y}, H x y ↔ H y x) (H_zero : ∀ {x}, H x 0 → claim) (H_diag : ∀ {x}, H x x → claim) (H_desc : ∀ {x y}, 0 < x → x < y → ¬base x y → H x y → ∀ y', y' * y' - B x * y' + C x = 0 → y' = B x - y → y' * y = C x → 0 ≤ y' ∧ y' ≤ x) (H_base : ∀ {x y}, H x y → base x y → claim) : claim := begin -- First of all, we may assume that x ≤ y. -- We justify this using H_symm. wlog hxy : x ≤ y, swap, { rw H_symm at h₀, solve_by_elim }, -- In fact, we can easily deal with the case x = y. by_cases x_eq_y : x = y, {subst x_eq_y, exact H_diag h₀}, -- Hence we may assume that x < y. replace hxy : x < y := lt_of_le_of_ne hxy x_eq_y, clear x_eq_y, -- Consider the upper branch of the hyperbola defined by H. let upper_branch : set (ℕ × ℕ) := {p | H p.1 p.2 ∧ p.1 < p.2}, -- Note that the point p = (x,y) lies on the upper branch. let p : ℕ × ℕ := ⟨x,y⟩, have hp : p ∈ upper_branch := ⟨h₀, hxy⟩, -- We also consider the exceptional set of solutions (a,b) that satisfy -- a = 0 or a = b or B a = b or B a = b + a or that lie in the base locus. let exceptional : set (ℕ × ℕ) := {p | H p.1 p.2 ∧ (base p.1 p.2 ∨ p.1 = 0 ∨ p.1 = p.2 ∨ B p.1 = p.2 ∨ B p.1 = p.2 + p.1) }, -- Let S be the projection of the upper branch on to the y-axis -- after removing the exceptional locus. let S : set ℕ := prod.snd '' (upper_branch \ exceptional), -- The strategy is to show that the exceptional locus in nonempty -- by running a descent argument that starts with the given point p = (x,y). -- Our assumptions ensure that we can then prove the claim. suffices exc : exceptional.nonempty, { -- Suppose that there exists an element in the exceptional locus. simp [exceptional, -add_comm, set.nonempty] at exc, -- Let (a,b) be such an element, and consider all the possible cases. rcases exc with ⟨a, b, hH, hb⟩, rcases hb with _|rfl|rfl|hB|hB, -- The first three cases are rather easy to solve. { solve_by_elim }, { rw H_symm at hH, solve_by_elim }, { solve_by_elim }, -- The final two cases are very similar. all_goals { -- Consider the quadratic equation that (a,b) satisfies. rw H_quad at hH, -- We find the other root of the equation, and Vieta's formulas. rcases Vieta_formula_quadratic hH with ⟨c, h_root, hV₁, hV₂⟩, -- By substitutions we find that b = 0 or b = a. simp [hB] at hV₁, subst hV₁, rw [← int.coe_nat_zero] at *, rw ← H_quad at h_root, -- And hence we are done by H_zero and H_diag. solve_by_elim } }, -- To finish the main proof, we need to show that the exceptional locus is nonempty. -- So we assume that the exceptional locus is empty, and work towards dering a contradiction. rw ← set.ne_empty_iff_nonempty, assume exceptional_empty, -- Observe that S is nonempty. have S_nonempty : S.nonempty, { -- It contains the image of p. use p.2, apply set.mem_image_of_mem, -- After all, we assumed that the exceptional locus is empty. rwa [exceptional_empty, set.diff_empty], }, -- We are now set for an infinite descent argument. -- Let m be the smallest element of the nonempty set S. let m : ℕ := well_founded.min nat.lt_wf S S_nonempty, have m_mem : m ∈ S := well_founded.min_mem nat.lt_wf S S_nonempty, have m_min : ∀ k ∈ S, ¬ k < m := λ k hk, well_founded.not_lt_min nat.lt_wf S S_nonempty hk, -- It suffices to show that there is point (a,b) with b ∈ S and b < m. suffices hp' : ∃ p' : ℕ × ℕ, p'.2 ∈ S ∧ p'.2 < m, { rcases hp' with ⟨p', p'_mem, p'_small⟩, solve_by_elim }, -- Let (m_x, m_y) be a point on the upper branch that projects to m ∈ S -- and that does not lie in the exceptional locus. rcases m_mem with ⟨⟨mx, my⟩, ⟨⟨hHm, mx_lt_my⟩, h_base⟩, m_eq⟩, -- This means that m_y = m, -- and the conditions H(m_x, m_y) and m_x < m_y are satisfied. simp [exceptional, hHm] at mx_lt_my h_base m_eq, push_neg at h_base, -- Finally, it also means that (m_x, m_y) does not lie in the base locus, -- that m_x ≠ 0, m_x ≠ m_y, B(m_x) ≠ m_y, and B(m_x) ≠ m_x + m_y. rcases h_base with ⟨h_base, hmx, hm_diag, hm_B₁, hm_B₂⟩, replace hmx : 0 < mx := pos_iff_ne_zero.mpr hmx, -- Consider the quadratic equation that (m_x, m_y) satisfies. have h_quad := hHm, rw H_quad at h_quad, -- We find the other root of the equation, and Vieta's formulas. rcases Vieta_formula_quadratic h_quad with ⟨c, h_root, hV₁, hV₂⟩, -- No we rewrite Vietas formulas a bit, and apply the descent step. replace hV₁ : c = B mx - my := eq_sub_of_add_eq' hV₁, rw mul_comm at hV₂, have Hc := H_desc hmx mx_lt_my h_base hHm c h_root hV₁ hV₂, -- This means that we may assume that c ≥ 0 and c ≤ m_x. cases Hc with c_nonneg c_lt, -- In other words, c is a natural number. lift c to ℕ using c_nonneg, -- Recall that we are trying find a point (a,b) such that b ∈ S and b < m. -- We claim that p' = (c, m_x) does the job. let p' : ℕ × ℕ := ⟨c, mx⟩, use p', -- The second condition is rather easy to check, so we do that first. split, swap, { rwa m_eq at mx_lt_my }, -- Now we need to show that p' projects onto S. In other words, that c ∈ S. -- We do that, by showing that it lies in the upper branch -- (which is sufficient, because we assumed that the exceptional locus is empty). apply set.mem_image_of_mem, rw [exceptional_empty, set.diff_empty], -- Now we are ready to prove that p' = (c, m_x) lies on the upper branch. -- We need to check two conditions: H(c, m_x) and c < m_x. split; dsimp only, { -- The first condition is not so hard. After all, c is the other root of the quadratic equation. rw [H_symm, H_quad], simpa using h_root, }, { -- For the second condition, we note that it suffices to check that c ≠ m_x. suffices hc : c ≠ mx, { refine lt_of_le_of_ne _ hc, exact_mod_cast c_lt, }, -- However, recall that B(m_x) ≠ m_x + m_y. -- If c = m_x, we can prove B(m_x) = m_x + m_y. contrapose! hm_B₂, subst c, simp [hV₁], } -- Hence p' = (c, m_x) lies on the upper branch, and we are done. end /--Question 6 of IMO1988. If a and b are two natural numbers such that a*b+1 divides a^2 + b^2, show that their quotient is a perfect square.-/ lemma imo1988_q6 {a b : ℕ} (h : (a*b+1) ∣ a^2 + b^2) : ∃ d, d^2 = (a^2 + b^2)/(a*b + 1) := begin rcases h with ⟨k, hk⟩, rw [hk, nat.mul_div_cancel_left _ (nat.succ_pos (a*b))], simp only [sq] at hk, apply constant_descent_vieta_jumping a b hk (λ x, k * x) (λ x, x*x - k) (λ x y, false); clear hk a b, { -- We will now show that the fibers of the solution set are described by a quadratic equation. intros x y, dsimp only, rw [← int.coe_nat_inj', ← sub_eq_zero], apply eq_iff_eq_cancel_right.2, norm_cast, simp, ring, }, { -- Show that the solution set is symmetric in a and b. intros x y, simp [add_comm (x*x), mul_comm x], }, { -- Show that the claim is true if b = 0. suffices : ∀ a, a * a = k → ∃ d, d * d = k, by simpa, rintros x rfl, use x }, { -- Show that the claim is true if a = b. intros x hx, suffices : k ≤ 1, { rw [nat.le_add_one_iff, nat.le_zero_iff] at this, rcases this with rfl|rfl, { use 0, simp }, { use 1, simp } }, contrapose! hx with k_lt_one, apply ne_of_lt, calc x*x + x*x = x*x * 2 : by rw mul_two ... ≤ x*x * k : nat.mul_le_mul_left (x*x) k_lt_one ... < (x*x + 1) * k : by linarith }, { -- Show the descent step. intros x y hx x_lt_y hxky h z h_root hV₁ hV₀, split, { dsimp [-sub_eq_add_neg] at *, have hpos : z*z + x*x > 0, { apply add_pos_of_nonneg_of_pos, { apply mul_self_nonneg }, { apply mul_pos; exact_mod_cast hx }, }, have hzx : z*z + x*x = (z * x + 1) * k, { rw [← sub_eq_zero, ← h_root], ring, }, rw hzx at hpos, replace hpos : z * x + 1 > 0 := pos_of_mul_pos_left hpos (int.coe_zero_le k), replace hpos : z * x ≥ 0 := int.le_of_lt_add_one hpos, apply nonneg_of_mul_nonneg_left hpos (by exact_mod_cast hx), }, { contrapose! hV₀ with x_lt_z, apply ne_of_gt, calc z * y > x*x : by apply mul_lt_mul'; linarith ... ≥ x*x - k : sub_le_self _ (int.coe_zero_le k) }, }, { -- There is no base case in this application of Vieta jumping. simp }, end /- The following example illustrates the use of constant descent Vieta jumping in the presence of a non-trivial base case. -/ example {a b : ℕ} (h : a*b ∣ a^2 + b^2 + 1) : 3*a*b = a^2 + b^2 + 1 := begin rcases h with ⟨k, hk⟩, suffices : k = 3, { simp * at *, ring, }, simp only [sq] at hk, apply constant_descent_vieta_jumping a b hk (λ x, k * x) (λ x, x*x + 1) (λ x y, x ≤ 1); clear hk a b, { -- We will now show that the fibers of the solution set are described by a quadratic equation. intros x y, dsimp only, rw [← int.coe_nat_inj', ← sub_eq_zero], apply eq_iff_eq_cancel_right.2, simp, ring, }, { -- Show that the solution set is symmetric in a and b. cc }, { -- Show that the claim is true if b = 0. simp }, { -- Show that the claim is true if a = b. intros x hx, have x_sq_dvd : x*x ∣ x*x*k := dvd_mul_right (x*x) k, rw ← hx at x_sq_dvd, obtain ⟨y, hy⟩ : x * x ∣ 1 := by simpa only [nat.dvd_add_self_left, add_assoc] using x_sq_dvd, obtain ⟨rfl,rfl⟩ : x = 1 ∧ y = 1 := by simpa [nat.mul_eq_one_iff] using hy.symm, simpa using hx.symm, }, { -- Show the descent step. intros x y x_lt_y hx h_base h z h_root hV₁ hV₀, split, { have zy_pos : z * y ≥ 0, { rw hV₀, exact_mod_cast (nat.zero_le _) }, apply nonneg_of_mul_nonneg_left zy_pos, linarith }, { contrapose! hV₀ with x_lt_z, apply ne_of_gt, push_neg at h_base, calc z * y > x * y : by apply mul_lt_mul_of_pos_right; linarith ... ≥ x * (x + 1) : by apply mul_le_mul; linarith ... > x * x + 1 : begin rw [mul_add, mul_one], apply add_lt_add_left, assumption_mod_cast end, } }, { -- Show the base case. intros x y h h_base, obtain rfl|rfl : x = 0 ∨ x = 1 := by rwa [nat.le_add_one_iff, nat.le_zero_iff] at h_base, { simpa using h, }, { simp only [mul_one, one_mul, add_comm, zero_add] at h, have y_dvd : y ∣ y * k := dvd_mul_right y k, rw [← h, ← add_assoc, nat.dvd_add_left (dvd_mul_left y y)] at y_dvd, obtain rfl|rfl := (nat.dvd_prime nat.prime_two).mp y_dvd; apply nat.eq_of_mul_eq_mul_left, exacts [zero_lt_one, h.symm, zero_lt_two, h.symm] } } end
a411853920e66b1cf90093f95646931443613f4e
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/tests/lean/run/eval_expr_partial.lean
0d2a9f0f4b274fb6edc510970f6e3b1fdcb5a6ad
[ "Apache-2.0" ]
permissive
bre7k30/lean
de893411bcfa7b3c5572e61b9e1c52951b310aa4
5a924699d076dab1bd5af23a8f910b433e598d7a
refs/heads/master
1,610,900,145,817
1,488,006,845,000
1,488,006,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
78
lean
open tactic run_command to_expr `(bit0 1) >>= eval_expr nat >>= tactic.trace
31639294105d1a5977f5988a7d1c442aad23422f
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/data/set/disjointed.lean
34c9083ef133afae36bc437d2ebd47bfe8052b46
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
5,371
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.set.lattice import tactic.wlog /-! # Relations holding pairwise and consecutive differences of sets This file defines pairwise relations and a way to make a sequence of sets into a sequence of disjoint sets. ## Main declarations * `pairwise p`: States that `p i j` for all `i ≠ j`. * `disjointed f`: Yields the sequence of sets `f 0`, `f 1 \ f 0`, `f 2 \ (f 0 ∪ f 1)`, ... This sequence has the same union as `f 0`, `f 1`, `f 2` but with disjoint sets. -/ open set 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 set.pairwise_on_univ {r : α → α → Prop} : (univ : set α).pairwise_on r ↔ pairwise r := by simp only [pairwise_on, pairwise, mem_univ, forall_const] theorem set.pairwise_on.on_injective {s : set α} {r : α → α → Prop} (hs : pairwise_on s r) {f : β → α} (hf : function.injective f) (hfs : ∀ x, f x ∈ s) : pairwise (r on f) := λ i j hij, hs _ (hfs i) _ (hfs j) (hf.ne hij) theorem pairwise.mono {p q : α → α → Prop} (h : ∀ ⦃i j⦄, p i j → q i j) (hp : pairwise p) : pairwise q := λ i j hij, h (hp i j hij) theorem pairwise_on_bool {r} (hr : symmetric r) {a b : α} : pairwise (r on (λ c, cond c a b)) ↔ r a b := by simpa [pairwise, function.on_fun] using @hr a b 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 theorem pairwise_on_nat {r} (hr : symmetric r) (f : ℕ → α) : pairwise (r on f) ↔ ∀ (m n) (h : m < n), r (f m) (f n) := ⟨λ p m n w, p m n w.ne, λ p m n w, by { wlog w' : m ≤ n, exact p m n ((ne.le_iff_lt w).mp w'), }⟩ theorem pairwise_disjoint_on_nat [semilattice_inf_bot α] (f : ℕ → α) : pairwise (disjoint on f) ↔ ∀ (m n) (h : m < n), disjoint (f m) (f n) := pairwise_on_nat disjoint.symm f theorem pairwise.pairwise_on {p : α → α → Prop} (h : pairwise p) (s : set α) : s.pairwise_on p := λ x hx y hy, h x y theorem pairwise_disjoint_fiber (f : α → β) : pairwise (disjoint on (λ y : β, f ⁻¹' {y})) := set.pairwise_on_univ.1 $ pairwise_on_disjoint_fiber f univ 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)ᶜ) variables {f : ℕ → set α} {n : ℕ} lemma disjoint_disjointed : 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 -- a more useful version might be `∀ i j x, x ∈ disjointed f i → x ∈ disjointed f j → i = j` lemma disjoint_disjointed' : ∀ i j, i ≠ j → (disjointed f i) ∩ (disjointed f j) = ∅ := λ i j hij, disjoint_iff.1 $ disjoint_disjointed i j hij lemma disjointed_subset : disjointed f n ⊆ f n := inter_subset_left _ _ lemma Union_lt_succ : (⋃ 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 : (⋂ 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 disjointed_induct {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 (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 $ λ i, le_infi $ λ hi, compl_le_compl $ hf $ nat.le_of_succ_le_succ hi), by simp [disjointed, this, diff_eq] open_locale classical lemma subset_Union_disjointed : f n ⊆ ⋃ i < n.succ, disjointed f i := λ x hx, have ∃ k, x ∈ f k, from ⟨n, hx⟩, have hn : ∀ (i : ℕ), i < nat.find this → x ∉ f i, from λ i, nat.find_min this, have hlt : nat.find this < n.succ, from (nat.find_min' this hx).trans_lt n.lt_succ_self, mem_bUnion hlt ⟨nat.find_spec this, mem_bInter hn⟩ lemma Union_disjointed : (⋃ n, disjointed f n) = (⋃ n, f n) := subset.antisymm (Union_subset_Union $ λ i, inter_subset_left _ _) (Union_subset $ λ n, subset.trans subset_Union_disjointed (bUnion_subset_Union _ _)) lemma Union_disjointed_of_mono (hf : monotone f) (n : ℕ) : (⋃ i < n.succ, disjointed f i) = f n := subset.antisymm (bUnion_subset $ λ k hk, subset.trans disjointed_subset $ hf $ nat.lt_succ_iff.1 hk) subset_Union_disjointed end set
0755b9ecb8a9ae48e390cec8e765ad8b32c45d5c
38bf3fd2bb651ab70511408fcf70e2029e2ba310
/src/category_theory/limits/shapes/binary_products.lean
6187c6d6f2ef2e4e92e7f640a7b06aab02bbcf2d
[ "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
7,623
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.finite_products import category_theory.limits.shapes.terminal import category_theory.discrete_category /-! # Binary (co)products We define a category `walking_pair`, which is the index category for a binary (co)product diagram. A convenience method `pair X Y` constructs the functor from the walking pair, hitting the given objects. We define `prod X Y` and `coprod X Y` as limits and colimits of such functors. Typeclasses `has_binary_products` and `has_binary_coproducts` assert the existence of (co)limits shaped as walking pairs. -/ universes v u open category_theory namespace category_theory.limits /-- The type of objects for the diagram indexing a binary (co)product. -/ @[derive decidable_eq] inductive walking_pair : Type v | left | right instance fintype_walking_pair : fintype walking_pair := { elems := [walking_pair.left, walking_pair.right].to_finset, complete := λ x, by { cases x; simp } } def pair_function {C : Type u} (X Y : C) : walking_pair → C | walking_pair.left := X | walking_pair.right := Y variables {C : Type u} [𝒞 : category.{v} C] include 𝒞 def pair (X Y : C) : discrete walking_pair ⥤ C := functor.of_function (pair_function X Y) @[simp] lemma pair_obj_left (X Y : C) : (pair X Y).obj walking_pair.left = X := rfl @[simp] lemma pair_obj_right (X Y : C) : (pair X Y).obj walking_pair.right = Y := rfl def map_pair {W X Y Z : C} (f : W ⟶ Y) (g : X ⟶ Z) : pair W X ⟶ pair Y Z := { app := λ j, match j with | walking_pair.left := f | walking_pair.right := g end } @[simp] lemma map_pair_left {W X Y Z : C} (f : W ⟶ Y) (g : X ⟶ Z) : (map_pair f g).app walking_pair.left = f := rfl @[simp] lemma map_pair_right {W X Y Z : C} (f : W ⟶ Y) (g : X ⟶ Z) : (map_pair f g).app walking_pair.right = g := rfl abbreviation binary_fan (X Y : C) := cone (pair X Y) abbreviation binary_cofan (X Y : C) := cocone (pair X Y) variables {X Y : C} def binary_fan.mk {P : C} (π₁ : P ⟶ X) (π₂ : P ⟶ Y) : binary_fan X Y := { X := P, π := { app := λ j, walking_pair.cases_on j π₁ π₂ }} def binary_cofan.mk {P : C} (ι₁ : X ⟶ P) (ι₂ : Y ⟶ P) : binary_cofan X Y := { X := P, ι := { app := λ j, walking_pair.cases_on j ι₁ ι₂ }} @[simp] lemma binary_fan.mk_π_app_left {P : C} (π₁ : P ⟶ X) (π₂ : P ⟶ Y) : (binary_fan.mk π₁ π₂).π.app walking_pair.left = π₁ := rfl @[simp] lemma binary_fan.mk_π_app_right {P : C} (π₁ : P ⟶ X) (π₂ : P ⟶ Y) : (binary_fan.mk π₁ π₂).π.app walking_pair.right = π₂ := rfl @[simp] lemma binary_cofan.mk_π_app_left {P : C} (ι₁ : X ⟶ P) (ι₂ : Y ⟶ P) : (binary_cofan.mk ι₁ ι₂).ι.app walking_pair.left = ι₁ := rfl @[simp] lemma binary_cofan.mk_π_app_right {P : C} (ι₁ : X ⟶ P) (ι₂ : Y ⟶ P) : (binary_cofan.mk ι₁ ι₂).ι.app walking_pair.right = ι₂ := rfl abbreviation prod (X Y : C) [has_limit (pair X Y)] := limit (pair X Y) abbreviation coprod (X Y : C) [has_colimit (pair X Y)] := colimit (pair X Y) notation X `⨯`:20 Y:20 := prod X Y notation X `⨿`:20 Y:20 := coprod X Y abbreviation prod.fst {X Y : C} [has_limit (pair X Y)] : X ⨯ Y ⟶ X := limit.π (pair X Y) walking_pair.left abbreviation prod.snd {X Y : C} [has_limit (pair X Y)] : X ⨯ Y ⟶ Y := limit.π (pair X Y) walking_pair.right abbreviation coprod.inl {X Y : C} [has_colimit (pair X Y)] : X ⟶ X ⨿ Y := colimit.ι (pair X Y) walking_pair.left abbreviation coprod.inr {X Y : C} [has_colimit (pair X Y)] : Y ⟶ X ⨿ Y := colimit.ι (pair X Y) walking_pair.right abbreviation prod.lift {W X Y : C} [has_limit (pair X Y)] (f : W ⟶ X) (g : W ⟶ Y) : W ⟶ X ⨯ Y := limit.lift _ (binary_fan.mk f g) abbreviation coprod.desc {W X Y : C} [has_colimit (pair X Y)] (f : X ⟶ W) (g : Y ⟶ W) : X ⨿ Y ⟶ W := colimit.desc _ (binary_cofan.mk f g) abbreviation prod.map {W X Y Z : C} [has_limits_of_shape.{v} (discrete walking_pair) C] (f : W ⟶ Y) (g : X ⟶ Z) : W ⨯ X ⟶ Y ⨯ Z := lim.map (map_pair f g) abbreviation coprod.map {W X Y Z : C} [has_colimits_of_shape.{v} (discrete walking_pair) C] (f : W ⟶ Y) (g : X ⟶ Z) : W ⨿ X ⟶ Y ⨿ Z := colim.map (map_pair f g) variables (C) class has_binary_products := (has_limits_of_shape : has_limits_of_shape.{v} (discrete walking_pair) C) class has_binary_coproducts := (has_colimits_of_shape : has_colimits_of_shape.{v} (discrete walking_pair) C) attribute [instance] has_binary_products.has_limits_of_shape has_binary_coproducts.has_colimits_of_shape instance [has_finite_products.{v} C] : has_binary_products.{v} C := { has_limits_of_shape := by apply_instance } instance [has_finite_coproducts.{v} C] : has_binary_coproducts.{v} C := { has_colimits_of_shape := by apply_instance } section -- TODO The `@[simp] def`s below should probably instead have appropriate simp lemmas written. variables {C} [has_binary_products.{v} C] local attribute [tidy] tactic.case_bash /-- The braiding isomorphism which swaps a binary product. -/ @[simp] def prod.braiding (P Q : C) : P ⨯ Q ≅ Q ⨯ P := { hom := prod.lift prod.snd prod.fst, inv := prod.lift prod.snd prod.fst } /-- The braiding isomorphism is symmetric. -/ @[simp] lemma prod.symmetry (P Q : C) : (prod.braiding P Q).hom ≫ (prod.braiding Q P).hom = 𝟙 _ := by tidy /-- The associator isomorphism for binary products. -/ @[simp] def prod.associator (P Q R : C) : (P ⨯ Q) ⨯ R ≅ P ⨯ (Q ⨯ R) := { hom := prod.lift (prod.fst ≫ prod.fst) (prod.lift (prod.fst ≫ prod.snd) prod.snd), inv := prod.lift (prod.lift prod.fst (prod.snd ≫ prod.fst)) (prod.snd ≫ prod.snd) } variables [has_terminal.{v} C] /-- The left unitor isomorphism for binary products with the terminal object. -/ @[simp] def prod.left_unitor (P : C) : ⊤_ C ⨯ P ≅ P := { hom := prod.snd, inv := prod.lift (terminal.from P) (𝟙 _) } /-- The right unitor isomorphism for binary products with the terminal object. -/ @[simp] def prod.right_unitor (P : C) : P ⨯ ⊤_ C ≅ P := { hom := prod.fst, inv := prod.lift (𝟙 _) (terminal.from P) } end section variables {C} [has_binary_coproducts.{v} C] local attribute [tidy] tactic.case_bash /-- The braiding isomorphism which swaps a binary coproduct. -/ @[simp] def coprod.braiding (P Q : C) : P ⨿ Q ≅ Q ⨿ P := { hom := coprod.desc coprod.inr coprod.inl, inv := coprod.desc coprod.inr coprod.inl } /-- The braiding isomorphism is symmetric. -/ @[simp] lemma coprod.symmetry (P Q : C) : (coprod.braiding P Q).hom ≫ (coprod.braiding Q P).hom = 𝟙 _ := by tidy /-- The associator isomorphism for binary coproducts. -/ @[simp] def coprod.associator (P Q R : C) : (P ⨿ Q) ⨿ R ≅ P ⨿ (Q ⨿ R) := { hom := coprod.desc (coprod.desc coprod.inl (coprod.inl ≫ coprod.inr)) (coprod.inr ≫ coprod.inr), inv := coprod.desc (coprod.inl ≫ coprod.inl) (coprod.desc (coprod.inr ≫ coprod.inl) coprod.inr) } variables [has_initial.{v} C] /-- The left unitor isomorphism for binary coproducts with the initial object. -/ @[simp] def coprod.left_unitor (P : C) : ⊥_ C ⨿ P ≅ P := { hom := coprod.desc (initial.to P) (𝟙 _), inv := coprod.inr } /-- The right unitor isomorphism for binary coproducts with the initial object. -/ @[simp] def coprod.right_unitor (P : C) : P ⨿ ⊥_ C ≅ P := { hom := coprod.desc (𝟙 _) (initial.to P), inv := coprod.inl } end end category_theory.limits
a2074da94e2aecb469840829b70aa0a4c7637f95
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/algebra/star.lean
d05c702d09460f8d89a8bd92b04ac28c047a98fe
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
3,167
lean
/- Copyright (c) 2022 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import algebra.star.pi import algebra.star.prod import topology.algebra.constructions import topology.continuous_function.basic /-! # Continuity of `star` > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines the `has_continuous_star` typeclass, along with instances on `pi`, `prod`, `mul_opposite`, and `units`. -/ open_locale filter topology open filter universes u variables {ι α R S : Type*} /-- Basic hypothesis to talk about a topological space with a continuous `star` operator. -/ class has_continuous_star (R : Type u) [topological_space R] [has_star R] : Prop := (continuous_star : continuous (star : R → R)) export has_continuous_star (continuous_star) section continuity variables [topological_space R] [has_star R] [has_continuous_star R] lemma continuous_on_star {s : set R} : continuous_on star s := continuous_star.continuous_on lemma continuous_within_at_star {s : set R} {x : R} : continuous_within_at star s x := continuous_star.continuous_within_at lemma continuous_at_star {x : R} : continuous_at star x := continuous_star.continuous_at lemma tendsto_star (a : R) : tendsto star (𝓝 a) (𝓝 (star a)) := continuous_at_star lemma filter.tendsto.star {f : α → R} {l : filter α} {y : R} (h : tendsto f l (𝓝 y)) : tendsto (λ x, star (f x)) l (𝓝 (star y)) := (continuous_star.tendsto y).comp h variables [topological_space α] {f : α → R} {s : set α} {x : α} @[continuity] lemma continuous.star (hf : continuous f) : continuous (λ x, star (f x)) := continuous_star.comp hf lemma continuous_at.star (hf : continuous_at f x) : continuous_at (λ x, star (f x)) x := continuous_at_star.comp hf lemma continuous_on.star (hf : continuous_on f s) : continuous_on (λ x, star (f x)) s := continuous_star.comp_continuous_on hf lemma continuous_within_at.star (hf : continuous_within_at f s x) : continuous_within_at (λ x, star (f x)) s x := hf.star /-- The star operation bundled as a continuous map. -/ @[simps] def star_continuous_map : C(R, R) := ⟨star, continuous_star⟩ end continuity section instances instance [has_star R] [has_star S] [topological_space R] [topological_space S] [has_continuous_star R] [has_continuous_star S] : has_continuous_star (R × S) := ⟨(continuous_star.comp continuous_fst).prod_mk (continuous_star.comp continuous_snd)⟩ instance {C : ι → Type*} [∀ i, topological_space (C i)] [∀ i, has_star (C i)] [∀ i, has_continuous_star (C i)] : has_continuous_star (Π i, C i) := { continuous_star := continuous_pi (λ i, continuous.star (continuous_apply i)) } instance [has_star R] [topological_space R] [has_continuous_star R] : has_continuous_star Rᵐᵒᵖ := ⟨mul_opposite.continuous_op.comp $ mul_opposite.continuous_unop.star⟩ instance [monoid R] [star_semigroup R] [topological_space R] [has_continuous_star R] : has_continuous_star Rˣ := ⟨continuous_induced_rng.2 units.continuous_embed_product.star⟩ end instances
f7bc432f54bf043bfe26e3472c079d74bdd5a6e8
7cdf3413c097e5d36492d12cdd07030eb991d394
/world_experiments/world8/level10.lean
fe8a3560dc68a4d3d7f3c6aaec6b4bc259a5bc8e
[]
no_license
alreadydone/natural_number_game
3135b9385a9f43e74cfbf79513fc37e69b99e0b3
1a39e693df4f4e871eb449890d3c7715a25c2ec9
refs/heads/master
1,599,387,390,105
1,573,200,587,000
1,573,200,691,000
220,397,084
0
0
null
1,573,192,734,000
1,573,192,733,000
null
UTF-8
Lean
false
false
671
lean
import game.world3.level9andahalf -- hide import game.world2.level11 -- random import -- succ ne zero -- hide import game.world2.level13 -- add_left_eq_zero -- hide namespace mynat -- hide /- # Multiplication World ## Level 10: `eq_zero_or_eq_zero_of_mul_eq_zero` A variant on the previous level. -/ /- Theorem If $a * b = 0$, then at least one of $a$ or $b$ is equal to zero. -/ theorem eq_zero_or_eq_zero_of_mul_eq_zero ⦃a b : mynat⦄ (h : a * b = 0) : a = 0 ∨ b = 0 := begin [less_leaky] cases a with d, left, refl, cases b with e he, right, refl, exfalso, rw mul_succ at h, rw add_succ at h, exact succ_ne_zero h, end end mynat -- hide
cf4c2c4d47d68420a785543cd5a0f122111bf6f9
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/geometry/euclidean/basic.lean
d0a575eb917102196166fb8c8f000a8fa80e0dfe
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
45,287
lean
/- Copyright (c) 2020 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers -/ import analysis.normed_space.inner_product import algebra.quadratic_discriminant import analysis.normed_space.add_torsor import data.matrix.notation import linear_algebra.affine_space.finite_dimensional import tactic.fin_cases noncomputable theory open_locale big_operators open_locale classical open_locale real open_locale real_inner_product_space /-! # Euclidean spaces This file makes some definitions and proves very basic geometrical results about real inner product spaces and Euclidean affine spaces. Results about real inner product spaces that involve the norm and inner product but not angles generally go in `analysis.normed_space.inner_product`. Results with longer proofs or more geometrical content generally go in separate files. ## Main definitions * `inner_product_geometry.angle` is the undirected angle between two vectors. * `euclidean_geometry.angle`, with notation `∠`, is the undirected angle determined by three points. * `euclidean_geometry.orthogonal_projection` is the orthogonal projection of a point onto an affine subspace. * `euclidean_geometry.reflection` is the reflection of a point in an affine subspace. ## Implementation notes To declare `P` as the type of points in a Euclidean affine space with `V` as the type of vectors, use `[inner_product_space ℝ V] [metric_space P] [normed_add_torsor V P]`. This works better with `out_param` to make `V` implicit in most cases than having a separate type alias for Euclidean affine spaces. Rather than requiring Euclidean affine spaces to be finite-dimensional (as in the definition on Wikipedia), this is specified only for those theorems that need it. ## References * https://en.wikipedia.org/wiki/Euclidean_space -/ namespace inner_product_geometry /-! ### Geometrical results on real inner product spaces This section develops some geometrical definitions and results on real inner product spaces, where those definitions and results can most conveniently be developed in terms of vectors and then used to deduce corresponding results for Euclidean affine spaces. -/ variables {V : Type*} [inner_product_space ℝ V] /-- The undirected angle between two vectors. If either vector is 0, this is π/2. -/ def angle (x y : V) : ℝ := real.arccos (inner x y / (∥x∥ * ∥y∥)) /-- The cosine of the angle between two vectors. -/ lemma cos_angle (x y : V) : real.cos (angle x y) = inner x y / (∥x∥ * ∥y∥) := real.cos_arccos (abs_le.mp (abs_real_inner_div_norm_mul_norm_le_one x y)).1 (abs_le.mp (abs_real_inner_div_norm_mul_norm_le_one x y)).2 /-- The angle between two vectors does not depend on their order. -/ lemma angle_comm (x y : V) : angle x y = angle y x := begin unfold angle, rw [real_inner_comm, mul_comm] end /-- The angle between the negation of two vectors. -/ @[simp] lemma angle_neg_neg (x y : V) : angle (-x) (-y) = angle x y := begin unfold angle, rw [inner_neg_neg, norm_neg, norm_neg] end /-- The angle between two vectors is nonnegative. -/ lemma angle_nonneg (x y : V) : 0 ≤ angle x y := real.arccos_nonneg _ /-- The angle between two vectors is at most π. -/ lemma angle_le_pi (x y : V) : angle x y ≤ π := real.arccos_le_pi _ /-- The angle between a vector and the negation of another vector. -/ lemma angle_neg_right (x y : V) : angle x (-y) = π - angle x y := begin unfold angle, rw [←real.arccos_neg, norm_neg, inner_neg_right, neg_div] end /-- The angle between the negation of a vector and another vector. -/ lemma angle_neg_left (x y : V) : angle (-x) y = π - angle x y := by rw [←angle_neg_neg, neg_neg, angle_neg_right] /-- The angle between the zero vector and a vector. -/ @[simp] lemma angle_zero_left (x : V) : angle 0 x = π / 2 := begin unfold angle, rw [inner_zero_left, zero_div, real.arccos_zero] end /-- The angle between a vector and the zero vector. -/ @[simp] lemma angle_zero_right (x : V) : angle x 0 = π / 2 := begin unfold angle, rw [inner_zero_right, zero_div, real.arccos_zero] end /-- The angle between a nonzero vector and itself. -/ @[simp] lemma angle_self {x : V} (hx : x ≠ 0) : angle x x = 0 := begin unfold angle, rw [←real_inner_self_eq_norm_square, div_self (λ h, hx (inner_self_eq_zero.1 h)), real.arccos_one] end /-- The angle between a nonzero vector and its negation. -/ @[simp] lemma angle_self_neg_of_nonzero {x : V} (hx : x ≠ 0) : angle x (-x) = π := by rw [angle_neg_right, angle_self hx, sub_zero] /-- The angle between the negation of a nonzero vector and that vector. -/ @[simp] lemma angle_neg_self_of_nonzero {x : V} (hx : x ≠ 0) : angle (-x) x = π := by rw [angle_comm, angle_self_neg_of_nonzero hx] /-- The angle between a vector and a positive multiple of a vector. -/ @[simp] lemma angle_smul_right_of_pos (x y : V) {r : ℝ} (hr : 0 < r) : angle x (r • y) = angle x y := begin unfold angle, rw [inner_smul_right, norm_smul, real.norm_eq_abs, abs_of_nonneg (le_of_lt hr), ←mul_assoc, mul_comm _ r, mul_assoc, mul_div_mul_left _ _ (ne_of_gt hr)] end /-- The angle between a positive multiple of a vector and a vector. -/ @[simp] lemma angle_smul_left_of_pos (x y : V) {r : ℝ} (hr : 0 < r) : angle (r • x) y = angle x y := by rw [angle_comm, angle_smul_right_of_pos y x hr, angle_comm] /-- The angle between a vector and a negative multiple of a vector. -/ @[simp] lemma angle_smul_right_of_neg (x y : V) {r : ℝ} (hr : r < 0) : angle x (r • y) = angle x (-y) := by rw [←neg_neg r, neg_smul, angle_neg_right, angle_smul_right_of_pos x y (neg_pos_of_neg hr), angle_neg_right] /-- The angle between a negative multiple of a vector and a vector. -/ @[simp] lemma angle_smul_left_of_neg (x y : V) {r : ℝ} (hr : r < 0) : angle (r • x) y = angle (-x) y := by rw [angle_comm, angle_smul_right_of_neg y x hr, angle_comm] /-- The cosine of the angle between two vectors, multiplied by the product of their norms. -/ lemma cos_angle_mul_norm_mul_norm (x y : V) : real.cos (angle x y) * (∥x∥ * ∥y∥) = inner x y := begin rw [cos_angle, div_mul_cancel_of_imp], simp [or_imp_distrib] { contextual := tt }, end /-- The sine of the angle between two vectors, multiplied by the product of their norms. -/ lemma sin_angle_mul_norm_mul_norm (x y : V) : real.sin (angle x y) * (∥x∥ * ∥y∥) = real.sqrt (inner x x * inner y y - inner x y * inner x y) := begin unfold angle, rw [real.sin_arccos (abs_le.mp (abs_real_inner_div_norm_mul_norm_le_one x y)).1 (abs_le.mp (abs_real_inner_div_norm_mul_norm_le_one x y)).2, ←real.sqrt_mul_self (mul_nonneg (norm_nonneg x) (norm_nonneg y)), ←real.sqrt_mul' _ (mul_self_nonneg _), pow_two, real.sqrt_mul_self (mul_nonneg (norm_nonneg x) (norm_nonneg y)), real_inner_self_eq_norm_square, real_inner_self_eq_norm_square], by_cases h : (∥x∥ * ∥y∥) = 0, { rw [(show ∥x∥ * ∥x∥ * (∥y∥ * ∥y∥) = (∥x∥ * ∥y∥) * (∥x∥ * ∥y∥), by ring), h, mul_zero, mul_zero, zero_sub], cases eq_zero_or_eq_zero_of_mul_eq_zero h with hx hy, { rw norm_eq_zero at hx, rw [hx, inner_zero_left, zero_mul, neg_zero] }, { rw norm_eq_zero at hy, rw [hy, inner_zero_right, zero_mul, neg_zero] } }, { field_simp [h], ring_nf, ring_nf, } end /-- The angle between two vectors is zero if and only if they are nonzero and one is a positive multiple of the other. -/ lemma angle_eq_zero_iff {x y : V} : angle x y = 0 ↔ (x ≠ 0 ∧ ∃ (r : ℝ), 0 < r ∧ y = r • x) := begin rw [angle, ← real_inner_div_norm_mul_norm_eq_one_iff, real.arccos_eq_zero, has_le.le.le_iff_eq, eq_comm], exact (abs_le.mp (abs_real_inner_div_norm_mul_norm_le_one x y)).2 end /-- The angle between two vectors is π if and only if they are nonzero and one is a negative multiple of the other. -/ lemma angle_eq_pi_iff {x y : V} : angle x y = π ↔ (x ≠ 0 ∧ ∃ (r : ℝ), r < 0 ∧ y = r • x) := begin rw [angle, ← real_inner_div_norm_mul_norm_eq_neg_one_iff, real.arccos_eq_pi, has_le.le.le_iff_eq], exact (abs_le.mp (abs_real_inner_div_norm_mul_norm_le_one x y)).1 end /-- If the angle between two vectors is π, the angles between those vectors and a third vector add to π. -/ lemma angle_add_angle_eq_pi_of_angle_eq_pi {x y : V} (z : V) (h : angle x y = π) : angle x z + angle y z = π := begin rcases angle_eq_pi_iff.1 h with ⟨hx, ⟨r, ⟨hr, rfl⟩⟩⟩, rw [angle_smul_left_of_neg x z hr, angle_neg_left, add_sub_cancel'_right] end /-- Two vectors have inner product 0 if and only if the angle between them is π/2. -/ lemma inner_eq_zero_iff_angle_eq_pi_div_two (x y : V) : ⟪x, y⟫ = 0 ↔ angle x y = π / 2 := iff.symm $ by simp [angle, or_imp_distrib] { contextual := tt } end inner_product_geometry namespace euclidean_geometry /-! ### Geometrical results on Euclidean affine spaces This section develops some geometrical definitions and results on Euclidean affine spaces. -/ open inner_product_geometry variables {V : Type*} {P : Type*} [inner_product_space ℝ V] [metric_space P] [normed_add_torsor V P] local notation `⟪`x`, `y`⟫` := @inner ℝ V _ x y include V /-- The undirected angle at `p2` between the line segments to `p1` and `p3`. If either of those points equals `p2`, this is π/2. Use `open_locale euclidean_geometry` to access the `∠ p1 p2 p3` notation. -/ def angle (p1 p2 p3 : P) : ℝ := angle (p1 -ᵥ p2 : V) (p3 -ᵥ p2) localized "notation `∠` := euclidean_geometry.angle" in euclidean_geometry /-- The angle at a point does not depend on the order of the other two points. -/ lemma angle_comm (p1 p2 p3 : P) : ∠ p1 p2 p3 = ∠ p3 p2 p1 := angle_comm _ _ /-- The angle at a point is nonnegative. -/ lemma angle_nonneg (p1 p2 p3 : P) : 0 ≤ ∠ p1 p2 p3 := angle_nonneg _ _ /-- The angle at a point is at most π. -/ lemma angle_le_pi (p1 p2 p3 : P) : ∠ p1 p2 p3 ≤ π := angle_le_pi _ _ /-- The angle ∠AAB at a point. -/ lemma angle_eq_left (p1 p2 : P) : ∠ p1 p1 p2 = π / 2 := begin unfold angle, rw vsub_self, exact angle_zero_left _ end /-- The angle ∠ABB at a point. -/ lemma angle_eq_right (p1 p2 : P) : ∠ p1 p2 p2 = π / 2 := by rw [angle_comm, angle_eq_left] /-- The angle ∠ABA at a point. -/ lemma angle_eq_of_ne {p1 p2 : P} (h : p1 ≠ p2) : ∠ p1 p2 p1 = 0 := angle_self (λ he, h (vsub_eq_zero_iff_eq.1 he)) /-- If the angle ∠ABC at a point is π, the angle ∠BAC is 0. -/ lemma angle_eq_zero_of_angle_eq_pi_left {p1 p2 p3 : P} (h : ∠ p1 p2 p3 = π) : ∠ p2 p1 p3 = 0 := begin unfold angle at h, rw angle_eq_pi_iff at h, rcases h with ⟨hp1p2, ⟨r, ⟨hr, hpr⟩⟩⟩, unfold angle, rw angle_eq_zero_iff, rw [←neg_vsub_eq_vsub_rev, neg_ne_zero] at hp1p2, use [hp1p2, -r + 1, add_pos (neg_pos_of_neg hr) zero_lt_one], rw [add_smul, ←neg_vsub_eq_vsub_rev p1 p2, smul_neg], simp [←hpr] end /-- If the angle ∠ABC at a point is π, the angle ∠BCA is 0. -/ lemma angle_eq_zero_of_angle_eq_pi_right {p1 p2 p3 : P} (h : ∠ p1 p2 p3 = π) : ∠ p2 p3 p1 = 0 := begin rw angle_comm at h, exact angle_eq_zero_of_angle_eq_pi_left h end /-- If ∠BCD = π, then ∠ABC = ∠ABD. -/ lemma angle_eq_angle_of_angle_eq_pi (p1 : P) {p2 p3 p4 : P} (h : ∠ p2 p3 p4 = π) : ∠ p1 p2 p3 = ∠ p1 p2 p4 := begin unfold angle at *, rcases angle_eq_pi_iff.1 h with ⟨hp2p3, ⟨r, ⟨hr, hpr⟩⟩⟩, rw [eq_comm], convert angle_smul_right_of_pos (p1 -ᵥ p2) (p3 -ᵥ p2) (add_pos (neg_pos_of_neg hr) zero_lt_one), rw [add_smul, ← neg_vsub_eq_vsub_rev p2 p3, smul_neg, neg_smul, ← hpr], simp end /-- If ∠BCD = π, then ∠ACB + ∠ACD = π. -/ lemma angle_add_angle_eq_pi_of_angle_eq_pi (p1 : P) {p2 p3 p4 : P} (h : ∠ p2 p3 p4 = π) : ∠ p1 p3 p2 + ∠ p1 p3 p4 = π := begin unfold angle at h, rw [angle_comm p1 p3 p2, angle_comm p1 p3 p4], unfold angle, exact angle_add_angle_eq_pi_of_angle_eq_pi _ h end /-- The inner product of two vectors given with `weighted_vsub`, in terms of the pairwise distances. -/ lemma inner_weighted_vsub {ι₁ : Type*} {s₁ : finset ι₁} {w₁ : ι₁ → ℝ} (p₁ : ι₁ → P) (h₁ : ∑ i in s₁, w₁ i = 0) {ι₂ : Type*} {s₂ : finset ι₂} {w₂ : ι₂ → ℝ} (p₂ : ι₂ → P) (h₂ : ∑ i in s₂, w₂ i = 0) : inner (s₁.weighted_vsub p₁ w₁) (s₂.weighted_vsub p₂ w₂) = (-∑ i₁ in s₁, ∑ i₂ in s₂, w₁ i₁ * w₂ i₂ * (dist (p₁ i₁) (p₂ i₂) * dist (p₁ i₁) (p₂ i₂))) / 2 := begin rw [finset.weighted_vsub_apply, finset.weighted_vsub_apply, inner_sum_smul_sum_smul_of_sum_eq_zero _ h₁ _ h₂], simp_rw [vsub_sub_vsub_cancel_right], rcongr i₁ i₂; rw dist_eq_norm_vsub V (p₁ i₁) (p₂ i₂) end /-- The distance between two points given with `affine_combination`, in terms of the pairwise distances between the points in that combination. -/ lemma dist_affine_combination {ι : Type*} {s : finset ι} {w₁ w₂ : ι → ℝ} (p : ι → P) (h₁ : ∑ i in s, w₁ i = 1) (h₂ : ∑ i in s, w₂ i = 1) : dist (s.affine_combination p w₁) (s.affine_combination p w₂) * dist (s.affine_combination p w₁) (s.affine_combination p w₂) = (-∑ i₁ in s, ∑ i₂ in s, (w₁ - w₂) i₁ * (w₁ - w₂) i₂ * (dist (p i₁) (p i₂) * dist (p i₁) (p i₂))) / 2 := begin rw [dist_eq_norm_vsub V (s.affine_combination p w₁) (s.affine_combination p w₂), ←inner_self_eq_norm_square, finset.affine_combination_vsub], have h : ∑ i in s, (w₁ - w₂) i = 0, { simp_rw [pi.sub_apply, finset.sum_sub_distrib, h₁, h₂, sub_self] }, exact inner_weighted_vsub p h p h end /-- Suppose that `c₁` is equidistant from `p₁` and `p₂`, and the same applies to `c₂`. Then the vector between `c₁` and `c₂` is orthogonal to that between `p₁` and `p₂`. (In two dimensions, this says that the diagonals of a kite are orthogonal.) -/ lemma inner_vsub_vsub_of_dist_eq_of_dist_eq {c₁ c₂ p₁ p₂ : P} (hc₁ : dist p₁ c₁ = dist p₂ c₁) (hc₂ : dist p₁ c₂ = dist p₂ c₂) : ⟪c₂ -ᵥ c₁, p₂ -ᵥ p₁⟫ = 0 := begin have h : ⟪(c₂ -ᵥ c₁) + (c₂ -ᵥ c₁), p₂ -ᵥ p₁⟫ = 0, { conv_lhs { congr, congr, rw ←vsub_sub_vsub_cancel_right c₂ c₁ p₁, skip, rw ←vsub_sub_vsub_cancel_right c₂ c₁ p₂ }, rw [←add_sub_comm, inner_sub_left], conv_lhs { congr, rw ←vsub_sub_vsub_cancel_right p₂ p₁ c₂, skip, rw ←vsub_sub_vsub_cancel_right p₂ p₁ c₁ }, rw [dist_comm p₁, dist_comm p₂, dist_eq_norm_vsub V _ p₁, dist_eq_norm_vsub V _ p₂, ←real_inner_add_sub_eq_zero_iff] at hc₁ hc₂, simp_rw [←neg_vsub_eq_vsub_rev c₁, ←neg_vsub_eq_vsub_rev c₂, sub_neg_eq_add, neg_add_eq_sub, hc₁, hc₂, sub_zero] }, simpa [inner_add_left, ←mul_two, (by norm_num : (2 : ℝ) ≠ 0)] using h end /-- The squared distance between points on a line (expressed as a multiple of a fixed vector added to a point) and another point, expressed as a quadratic. -/ lemma dist_smul_vadd_square (r : ℝ) (v : V) (p₁ p₂ : P) : dist (r • v +ᵥ p₁) p₂ * dist (r • v +ᵥ p₁) p₂ = ⟪v, v⟫ * r * r + 2 * ⟪v, p₁ -ᵥ p₂⟫ * r + ⟪p₁ -ᵥ p₂, p₁ -ᵥ p₂⟫ := begin rw [dist_eq_norm_vsub V _ p₂, ←real_inner_self_eq_norm_square, vadd_vsub_assoc, real_inner_add_add_self, real_inner_smul_left, real_inner_smul_left, real_inner_smul_right], ring end /-- The condition for two points on a line to be equidistant from another point. -/ lemma dist_smul_vadd_eq_dist {v : V} (p₁ p₂ : P) (hv : v ≠ 0) (r : ℝ) : dist (r • v +ᵥ p₁) p₂ = dist p₁ p₂ ↔ (r = 0 ∨ r = -2 * ⟪v, p₁ -ᵥ p₂⟫ / ⟪v, v⟫) := begin conv_lhs { rw [←mul_self_inj_of_nonneg dist_nonneg dist_nonneg, dist_smul_vadd_square, ←sub_eq_zero, add_sub_assoc, dist_eq_norm_vsub V p₁ p₂, ←real_inner_self_eq_norm_square, sub_self] }, have hvi : ⟪v, v⟫ ≠ 0, by simpa using hv, have hd : discrim ⟪v, v⟫ (2 * ⟪v, p₁ -ᵥ p₂⟫) 0 = (2 * inner v (p₁ -ᵥ p₂)) * (2 * inner v (p₁ -ᵥ p₂)), { rw discrim, ring }, rw [quadratic_eq_zero_iff hvi hd, add_left_neg, zero_div, neg_mul_eq_neg_mul, ←mul_sub_right_distrib, sub_eq_add_neg, ←mul_two, mul_assoc, mul_div_assoc, mul_div_mul_left, mul_div_assoc], norm_num end open affine_subspace finite_dimensional /-- Distances `r₁` `r₂` of `p` from two different points `c₁` `c₂` determine at most two points `p₁` `p₂` in a two-dimensional subspace containing those points (two circles intersect in at most two points). -/ lemma eq_of_dist_eq_of_dist_eq_of_mem_of_findim_eq_two {s : affine_subspace ℝ P} [finite_dimensional ℝ s.direction] (hd : findim ℝ s.direction = 2) {c₁ c₂ p₁ p₂ p : P} (hc₁s : c₁ ∈ s) (hc₂s : c₂ ∈ s) (hp₁s : p₁ ∈ s) (hp₂s : p₂ ∈ s) (hps : p ∈ s) {r₁ r₂ : ℝ} (hc : c₁ ≠ c₂) (hp : p₁ ≠ p₂) (hp₁c₁ : dist p₁ c₁ = r₁) (hp₂c₁ : dist p₂ c₁ = r₁) (hpc₁ : dist p c₁ = r₁) (hp₁c₂ : dist p₁ c₂ = r₂) (hp₂c₂ : dist p₂ c₂ = r₂) (hpc₂ : dist p c₂ = r₂) : p = p₁ ∨ p = p₂ := begin have ho : ⟪c₂ -ᵥ c₁, p₂ -ᵥ p₁⟫ = 0 := inner_vsub_vsub_of_dist_eq_of_dist_eq (hp₁c₁.trans hp₂c₁.symm) (hp₁c₂.trans hp₂c₂.symm), have hop : ⟪c₂ -ᵥ c₁, p -ᵥ p₁⟫ = 0 := inner_vsub_vsub_of_dist_eq_of_dist_eq (hp₁c₁.trans hpc₁.symm) (hp₁c₂.trans hpc₂.symm), let b : fin 2 → V := ![c₂ -ᵥ c₁, p₂ -ᵥ p₁], have hb : linear_independent ℝ b, { refine linear_independent_of_ne_zero_of_inner_eq_zero _ _, { intro i, fin_cases i; simp [b, hc.symm, hp.symm], }, { intros i j hij, fin_cases i; fin_cases j; try { exact false.elim (hij rfl) }, { exact ho }, { rw real_inner_comm, exact ho } } }, have hbs : submodule.span ℝ (set.range b) = s.direction, { refine eq_of_le_of_findim_eq _ _, { rw [submodule.span_le, set.range_subset_iff], intro i, fin_cases i, { exact vsub_mem_direction hc₂s hc₁s }, { exact vsub_mem_direction hp₂s hp₁s } }, { rw [findim_span_eq_card hb, fintype.card_fin, hd] } }, have hv : ∀ v ∈ s.direction, ∃ t₁ t₂ : ℝ, v = t₁ • (c₂ -ᵥ c₁) + t₂ • (p₂ -ᵥ p₁), { intros v hv, have hr : set.range b = {c₂ -ᵥ c₁, p₂ -ᵥ p₁}, { have hu : (finset.univ : finset (fin 2)) = {0, 1}, by dec_trivial, rw [←fintype.coe_image_univ, hu], simp, refl }, rw [←hbs, hr, submodule.mem_span_insert] at hv, rcases hv with ⟨t₁, v', hv', hv⟩, rw submodule.mem_span_singleton at hv', rcases hv' with ⟨t₂, rfl⟩, exact ⟨t₁, t₂, hv⟩ }, rcases hv (p -ᵥ p₁) (vsub_mem_direction hps hp₁s) with ⟨t₁, t₂, hpt⟩, simp only [hpt, inner_add_right, inner_smul_right, ho, mul_zero, add_zero, mul_eq_zero, inner_self_eq_zero, vsub_eq_zero_iff_eq, hc.symm, or_false] at hop, rw [hop, zero_smul, zero_add, ←eq_vadd_iff_vsub_eq] at hpt, subst hpt, have hp' : (p₂ -ᵥ p₁ : V) ≠ 0, { simp [hp.symm] }, have hp₂ : dist ((1 : ℝ) • (p₂ -ᵥ p₁) +ᵥ p₁) c₁ = r₁, { simp [hp₂c₁] }, rw [←hp₁c₁, dist_smul_vadd_eq_dist _ _ hp'] at hpc₁ hp₂, simp only [one_ne_zero, false_or] at hp₂, rw hp₂.symm at hpc₁, cases hpc₁; simp [hpc₁] end /-- Distances `r₁` `r₂` of `p` from two different points `c₁` `c₂` determine at most two points `p₁` `p₂` in two-dimensional space (two circles intersect in at most two points). -/ lemma eq_of_dist_eq_of_dist_eq_of_findim_eq_two [finite_dimensional ℝ V] (hd : findim ℝ V = 2) {c₁ c₂ p₁ p₂ p : P} {r₁ r₂ : ℝ} (hc : c₁ ≠ c₂) (hp : p₁ ≠ p₂) (hp₁c₁ : dist p₁ c₁ = r₁) (hp₂c₁ : dist p₂ c₁ = r₁) (hpc₁ : dist p c₁ = r₁) (hp₁c₂ : dist p₁ c₂ = r₂) (hp₂c₂ : dist p₂ c₂ = r₂) (hpc₂ : dist p c₂ = r₂) : p = p₁ ∨ p = p₂ := begin have hd' : findim ℝ (⊤ : affine_subspace ℝ P).direction = 2, { rw [direction_top, findim_top], exact hd }, exact eq_of_dist_eq_of_dist_eq_of_mem_of_findim_eq_two hd' (mem_top ℝ V _) (mem_top ℝ V _) (mem_top ℝ V _) (mem_top ℝ V _) (mem_top ℝ V _) hc hp hp₁c₁ hp₂c₁ hpc₁ hp₁c₂ hp₂c₂ hpc₂ end variables {V} /-- The orthogonal projection of a point onto a nonempty affine subspace, whose direction is complete, as an unbundled function. This definition is only intended for use in setting up the bundled version `orthogonal_projection` and should not be used once that is defined. -/ def orthogonal_projection_fn (s : affine_subspace ℝ P) [nonempty s] [complete_space s.direction] (p : P) : P := classical.some $ inter_eq_singleton_of_nonempty_of_is_compl (nonempty_subtype.mp ‹_›) (mk'_nonempty p s.directionᗮ) begin convert submodule.is_compl_orthogonal_of_is_complete (complete_space_coe_iff_is_complete.mp ‹_›), exact direction_mk' p s.directionᗮ end /-- The intersection of the subspace and the orthogonal subspace through the given point is the `orthogonal_projection_fn` of that point onto the subspace. This lemma is only intended for use in setting up the bundled version and should not be used once that is defined. -/ lemma inter_eq_singleton_orthogonal_projection_fn {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] (p : P) : (s : set P) ∩ (mk' p s.directionᗮ) = {orthogonal_projection_fn s p} := classical.some_spec $ inter_eq_singleton_of_nonempty_of_is_compl (nonempty_subtype.mp ‹_›) (mk'_nonempty p s.directionᗮ) begin convert submodule.is_compl_orthogonal_of_is_complete (complete_space_coe_iff_is_complete.mp ‹_›), exact direction_mk' p s.directionᗮ end /-- The `orthogonal_projection_fn` lies in the given subspace. This lemma is only intended for use in setting up the bundled version and should not be used once that is defined. -/ lemma orthogonal_projection_fn_mem {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] (p : P) : orthogonal_projection_fn s p ∈ s := begin rw [←mem_coe, ←set.singleton_subset_iff, ←inter_eq_singleton_orthogonal_projection_fn], exact set.inter_subset_left _ _ end /-- The `orthogonal_projection_fn` lies in the orthogonal subspace. This lemma is only intended for use in setting up the bundled version and should not be used once that is defined. -/ lemma orthogonal_projection_fn_mem_orthogonal {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] (p : P) : orthogonal_projection_fn s p ∈ mk' p s.directionᗮ := begin rw [←mem_coe, ←set.singleton_subset_iff, ←inter_eq_singleton_orthogonal_projection_fn], exact set.inter_subset_right _ _ end /-- Subtracting `p` from its `orthogonal_projection_fn` produces a result in the orthogonal direction. This lemma is only intended for use in setting up the bundled version and should not be used once that is defined. -/ lemma orthogonal_projection_fn_vsub_mem_direction_orthogonal {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] (p : P) : orthogonal_projection_fn s p -ᵥ p ∈ s.directionᗮ := direction_mk' p s.directionᗮ ▸ vsub_mem_direction (orthogonal_projection_fn_mem_orthogonal p) (self_mem_mk' _ _) /-- The orthogonal projection of a point onto a nonempty affine subspace, whose direction is complete. The corresponding linear map (mapping a vector to the difference between the projections of two points whose difference is that vector) is the `orthogonal_projection` for real inner product spaces, onto the direction of the affine subspace being projected onto. -/ def orthogonal_projection (s : affine_subspace ℝ P) [nonempty s] [complete_space s.direction] : P →ᵃ[ℝ] s := { to_fun := λ p, ⟨orthogonal_projection_fn s p, orthogonal_projection_fn_mem p⟩, linear := orthogonal_projection s.direction, map_vadd' := λ p v, begin have hs : ((orthogonal_projection s.direction) v : V) +ᵥ orthogonal_projection_fn s p ∈ s := vadd_mem_of_mem_direction (orthogonal_projection s.direction v).2 (orthogonal_projection_fn_mem p), have ho : ((orthogonal_projection s.direction) v : V) +ᵥ orthogonal_projection_fn s p ∈ mk' (v +ᵥ p) s.directionᗮ, { rw [←vsub_right_mem_direction_iff_mem (self_mem_mk' _ _) _, direction_mk', vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, add_comm, add_sub_assoc], refine submodule.add_mem _ (orthogonal_projection_fn_vsub_mem_direction_orthogonal p) _, rw submodule.mem_orthogonal', intros w hw, rw [←neg_sub, inner_neg_left, orthogonal_projection_inner_eq_zero _ w hw, neg_zero], }, have hm : ((orthogonal_projection s.direction) v : V) +ᵥ orthogonal_projection_fn s p ∈ ({orthogonal_projection_fn s (v +ᵥ p)} : set P), { rw ←inter_eq_singleton_orthogonal_projection_fn (v +ᵥ p), exact set.mem_inter hs ho }, rw set.mem_singleton_iff at hm, ext, exact hm.symm end } @[simp] lemma orthogonal_projection_fn_eq {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] (p : P) : orthogonal_projection_fn s p = orthogonal_projection s p := rfl /-- The linear map corresponding to `orthogonal_projection`. -/ @[simp] lemma orthogonal_projection_linear {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] : (orthogonal_projection s).linear = _root_.orthogonal_projection s.direction := rfl /-- The intersection of the subspace and the orthogonal subspace through the given point is the `orthogonal_projection` of that point onto the subspace. -/ lemma inter_eq_singleton_orthogonal_projection {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] (p : P) : (s : set P) ∩ (mk' p s.directionᗮ) = {orthogonal_projection s p} := begin rw ←orthogonal_projection_fn_eq, exact inter_eq_singleton_orthogonal_projection_fn p end /-- The `orthogonal_projection` lies in the given subspace. -/ lemma orthogonal_projection_mem {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] (p : P) : ↑(orthogonal_projection s p) ∈ s := (orthogonal_projection s p).2 /-- The `orthogonal_projection` lies in the orthogonal subspace. -/ lemma orthogonal_projection_mem_orthogonal (s : affine_subspace ℝ P) [nonempty s] [complete_space s.direction] (p : P) : ↑(orthogonal_projection s p) ∈ mk' p s.directionᗮ := orthogonal_projection_fn_mem_orthogonal p /-- Subtracting a point in the given subspace from the `orthogonal_projection` produces a result in the direction of the given subspace. -/ lemma orthogonal_projection_vsub_mem_direction {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] {p1 : P} (p2 : P) (hp1 : p1 ∈ s) : ↑(orthogonal_projection s p2 -ᵥ ⟨p1, hp1⟩ : s.direction) ∈ s.direction := (orthogonal_projection s p2 -ᵥ ⟨p1, hp1⟩ : s.direction).2 /-- Subtracting the `orthogonal_projection` from a point in the given subspace produces a result in the direction of the given subspace. -/ lemma vsub_orthogonal_projection_mem_direction {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] {p1 : P} (p2 : P) (hp1 : p1 ∈ s) : ↑((⟨p1, hp1⟩ : s) -ᵥ orthogonal_projection s p2 : s.direction) ∈ s.direction := ((⟨p1, hp1⟩ : s) -ᵥ orthogonal_projection s p2 : s.direction).2 /-- A point equals its orthogonal projection if and only if it lies in the subspace. -/ lemma orthogonal_projection_eq_self_iff {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] {p : P} : ↑(orthogonal_projection s p) = p ↔ p ∈ s := begin split, { exact λ h, h ▸ orthogonal_projection_mem p }, { intro h, have hp : p ∈ ((s : set P) ∩ mk' p s.directionᗮ) := ⟨h, self_mem_mk' p _⟩, rw [inter_eq_singleton_orthogonal_projection p] at hp, symmetry, exact hp } end /-- Orthogonal projection is idempotent. -/ @[simp] lemma orthogonal_projection_orthogonal_projection (s : affine_subspace ℝ P) [nonempty s] [complete_space s.direction] (p : P) : orthogonal_projection s (orthogonal_projection s p) = orthogonal_projection s p := begin ext, rw orthogonal_projection_eq_self_iff, exact orthogonal_projection_mem p, end lemma eq_orthogonal_projection_of_eq_subspace {s s' : affine_subspace ℝ P} [nonempty s] [nonempty s'] [complete_space s.direction] [complete_space s'.direction] (h : s = s') (p : P) : (orthogonal_projection s p : P) = (orthogonal_projection s' p : P) := begin change orthogonal_projection_fn s p = orthogonal_projection_fn s' p, congr, exact h end /-- The distance to a point's orthogonal projection is 0 iff it lies in the subspace. -/ lemma dist_orthogonal_projection_eq_zero_iff {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] {p : P} : dist p (orthogonal_projection s p) = 0 ↔ p ∈ s := by rw [dist_comm, dist_eq_zero, orthogonal_projection_eq_self_iff] /-- The distance between a point and its orthogonal projection is nonzero if it does not lie in the subspace. -/ lemma dist_orthogonal_projection_ne_zero_of_not_mem {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] {p : P} (hp : p ∉ s) : dist p (orthogonal_projection s p) ≠ 0 := mt dist_orthogonal_projection_eq_zero_iff.mp hp /-- Subtracting `p` from its `orthogonal_projection` produces a result in the orthogonal direction. -/ lemma orthogonal_projection_vsub_mem_direction_orthogonal (s : affine_subspace ℝ P) [nonempty s] [complete_space s.direction] (p : P) : (orthogonal_projection s p : P) -ᵥ p ∈ s.directionᗮ := orthogonal_projection_fn_vsub_mem_direction_orthogonal p /-- Subtracting the `orthogonal_projection` from `p` produces a result in the orthogonal direction. -/ lemma vsub_orthogonal_projection_mem_direction_orthogonal (s : affine_subspace ℝ P) [nonempty s] [complete_space s.direction] (p : P) : p -ᵥ orthogonal_projection s p ∈ s.directionᗮ := direction_mk' p s.directionᗮ ▸ vsub_mem_direction (self_mem_mk' _ _) (orthogonal_projection_mem_orthogonal s p) /-- Adding a vector to a point in the given subspace, then taking the orthogonal projection, produces the original point if the vector was in the orthogonal direction. -/ lemma orthogonal_projection_vadd_eq_self {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] {p : P} (hp : p ∈ s) {v : V} (hv : v ∈ s.directionᗮ) : orthogonal_projection s (v +ᵥ p) = ⟨p, hp⟩ := begin have h := vsub_orthogonal_projection_mem_direction_orthogonal s (v +ᵥ p), rw [vadd_vsub_assoc, submodule.add_mem_iff_right _ hv] at h, refine (eq_of_vsub_eq_zero _).symm, ext, refine submodule.disjoint_def.1 s.direction.orthogonal_disjoint _ _ h, exact (_ : s.direction).2 end /-- Adding a vector to a point in the given subspace, then taking the orthogonal projection, produces the original point if the vector is a multiple of the result of subtracting a point's orthogonal projection from that point. -/ lemma orthogonal_projection_vadd_smul_vsub_orthogonal_projection {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] {p1 : P} (p2 : P) (r : ℝ) (hp : p1 ∈ s) : orthogonal_projection s (r • (p2 -ᵥ orthogonal_projection s p2 : V) +ᵥ p1) = ⟨p1, hp⟩ := orthogonal_projection_vadd_eq_self hp (submodule.smul_mem _ _ (vsub_orthogonal_projection_mem_direction_orthogonal s _)) /-- The square of the distance from a point in `s` to `p2` equals the sum of the squares of the distances of the two points to the `orthogonal_projection`. -/ lemma dist_square_eq_dist_orthogonal_projection_square_add_dist_orthogonal_projection_square {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] {p1 : P} (p2 : P) (hp1 : p1 ∈ s) : dist p1 p2 * dist p1 p2 = dist p1 (orthogonal_projection s p2) * dist p1 (orthogonal_projection s p2) + dist p2 (orthogonal_projection s p2) * dist p2 (orthogonal_projection s p2) := begin rw [pseudo_metric_space.dist_comm p2 _, dist_eq_norm_vsub V p1 _, dist_eq_norm_vsub V p1 _, dist_eq_norm_vsub V _ p2, ← vsub_add_vsub_cancel p1 (orthogonal_projection s p2) p2, norm_add_square_eq_norm_square_add_norm_square_iff_real_inner_eq_zero], exact submodule.inner_right_of_mem_orthogonal (vsub_orthogonal_projection_mem_direction p2 hp1) (orthogonal_projection_vsub_mem_direction_orthogonal s p2), end /-- The square of the distance between two points constructed by adding multiples of the same orthogonal vector to points in the same subspace. -/ lemma dist_square_smul_orthogonal_vadd_smul_orthogonal_vadd {s : affine_subspace ℝ P} {p1 p2 : P} (hp1 : p1 ∈ s) (hp2 : p2 ∈ s) (r1 r2 : ℝ) {v : V} (hv : v ∈ s.directionᗮ) : dist (r1 • v +ᵥ p1) (r2 • v +ᵥ p2) * dist (r1 • v +ᵥ p1) (r2 • v +ᵥ p2) = dist p1 p2 * dist p1 p2 + (r1 - r2) * (r1 - r2) * (∥v∥ * ∥v∥) := calc dist (r1 • v +ᵥ p1) (r2 • v +ᵥ p2) * dist (r1 • v +ᵥ p1) (r2 • v +ᵥ p2) = ∥(p1 -ᵥ p2) + (r1 - r2) • v∥ * ∥(p1 -ᵥ p2) + (r1 - r2) • v∥ : by { rw [dist_eq_norm_vsub V (r1 • v +ᵥ p1), vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, sub_smul], abel } ... = ∥p1 -ᵥ p2∥ * ∥p1 -ᵥ p2∥ + ∥(r1 - r2) • v∥ * ∥(r1 - r2) • v∥ : norm_add_square_eq_norm_square_add_norm_square_real (submodule.inner_right_of_mem_orthogonal (vsub_mem_direction hp1 hp2) (submodule.smul_mem _ _ hv)) ... = ∥(p1 -ᵥ p2 : V)∥ * ∥(p1 -ᵥ p2 : V)∥ + abs (r1 - r2) * abs (r1 - r2) * ∥v∥ * ∥v∥ : by { rw [norm_smul, real.norm_eq_abs], ring } ... = dist p1 p2 * dist p1 p2 + (r1 - r2) * (r1 - r2) * (∥v∥ * ∥v∥) : by { rw [dist_eq_norm_vsub V p1, abs_mul_abs_self, mul_assoc] } /-- Reflection in an affine subspace, which is expected to be nonempty and complete. The word "reflection" is sometimes understood to mean specifically reflection in a codimension-one subspace, and sometimes more generally to cover operations such as reflection in a point. The definition here, of reflection in an affine subspace, is a more general sense of the word that includes both those common cases. If the subspace is empty or not complete, `orthogonal_projection` is defined as the identity map, which results in `reflection` being the identity map in that case as well. -/ def reflection (s : affine_subspace ℝ P) [nonempty s] [complete_space s.direction] : P ≃ᵢ P := { to_fun := λ p, (↑(orthogonal_projection s p) -ᵥ p) +ᵥ orthogonal_projection s p, inv_fun := λ p, (↑(orthogonal_projection s p) -ᵥ p) +ᵥ orthogonal_projection s p, left_inv := λ p, by simp [vsub_vadd_eq_vsub_sub, -orthogonal_projection_linear], right_inv := λ p, by simp [vsub_vadd_eq_vsub_sub, -orthogonal_projection_linear], isometry_to_fun := begin dsimp only, rw isometry_emetric_iff_metric, intros p₁ p₂, rw [←mul_self_inj_of_nonneg dist_nonneg dist_nonneg, dist_eq_norm_vsub V ((↑(orthogonal_projection s p₁) -ᵥ p₁) +ᵥ ↑(orthogonal_projection s p₁)), dist_eq_norm_vsub V p₁, ←inner_self_eq_norm_square, ←inner_self_eq_norm_square], calc ⟪((orthogonal_projection s p₁ : P) -ᵥ p₁ +ᵥ (orthogonal_projection s p₁ : P) -ᵥ ((orthogonal_projection s p₂ : P) -ᵥ p₂ +ᵥ orthogonal_projection s p₂)), ((orthogonal_projection s p₁ : P) -ᵥ p₁ +ᵥ (orthogonal_projection s p₁ : P) -ᵥ ((orthogonal_projection s p₂ : P) -ᵥ p₂ +ᵥ orthogonal_projection s p₂))⟫ = ⟪(_root_.orthogonal_projection s.direction (p₁ -ᵥ p₂)) + _root_.orthogonal_projection s.direction (p₁ -ᵥ p₂) - (p₁ -ᵥ p₂), _root_.orthogonal_projection s.direction (p₁ -ᵥ p₂) + _root_.orthogonal_projection s.direction (p₁ -ᵥ p₂) - (p₁ -ᵥ p₂)⟫ : by { rw [vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, add_comm, add_sub_assoc, ←vsub_vadd_eq_vsub_sub, vsub_vadd_comm, vsub_vadd_eq_vsub_sub, ←add_sub_assoc, ←coe_vsub, ←affine_map.linear_map_vsub], simp } ... = -4 * inner (p₁ -ᵥ p₂ - (_root_.orthogonal_projection s.direction (p₁ -ᵥ p₂) : V)) (_root_.orthogonal_projection s.direction (p₁ -ᵥ p₂)) + ⟪p₁ -ᵥ p₂, p₁ -ᵥ p₂⟫ : by { simp [inner_sub_left, inner_sub_right, inner_add_left, inner_add_right, real_inner_comm (p₁ -ᵥ p₂)], ring } ... = ⟪p₁ -ᵥ p₂, p₁ -ᵥ p₂⟫ : by simp, end } /-- The result of reflecting. -/ lemma reflection_apply (s : affine_subspace ℝ P) [nonempty s] [complete_space s.direction] (p : P) : reflection s p = (↑(orthogonal_projection s p) -ᵥ p) +ᵥ orthogonal_projection s p := rfl lemma eq_reflection_of_eq_subspace {s s' : affine_subspace ℝ P} [nonempty s] [nonempty s'] [complete_space s.direction] [complete_space s'.direction] (h : s = s') (p : P) : (reflection s p : P) = (reflection s' p : P) := by unfreezingI { subst h } /-- Reflection is its own inverse. -/ @[simp] lemma reflection_symm (s : affine_subspace ℝ P) [nonempty s] [complete_space s.direction] : (reflection s).symm = reflection s := rfl /-- Reflecting twice in the same subspace. -/ @[simp] lemma reflection_reflection (s : affine_subspace ℝ P) [nonempty s] [complete_space s.direction] (p : P) : reflection s (reflection s p) = p := (reflection s).left_inv p /-- Reflection is involutive. -/ lemma reflection_involutive (s : affine_subspace ℝ P) [nonempty s] [complete_space s.direction] : function.involutive (reflection s) := reflection_reflection s /-- A point is its own reflection if and only if it is in the subspace. -/ lemma reflection_eq_self_iff {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] (p : P) : reflection s p = p ↔ p ∈ s := begin rw [←orthogonal_projection_eq_self_iff, reflection_apply], split, { intro h, rw [←@vsub_eq_zero_iff_eq V, vadd_vsub_assoc, ←two_smul ℝ (↑(orthogonal_projection s p) -ᵥ p), smul_eq_zero] at h, norm_num at h, exact h }, { intro h, simp [h] } end /-- Reflecting a point in two subspaces produces the same result if and only if the point has the same orthogonal projection in each of those subspaces. -/ lemma reflection_eq_iff_orthogonal_projection_eq (s₁ s₂ : affine_subspace ℝ P) [nonempty s₁] [nonempty s₂] [complete_space s₁.direction] [complete_space s₂.direction] (p : P) : reflection s₁ p = reflection s₂ p ↔ (orthogonal_projection s₁ p : P) = orthogonal_projection s₂ p := begin rw [reflection_apply, reflection_apply], split, { intro h, rw [←@vsub_eq_zero_iff_eq V, vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, add_comm, add_sub_assoc, vsub_sub_vsub_cancel_right, ←two_smul ℝ ((orthogonal_projection s₁ p : P) -ᵥ orthogonal_projection s₂ p), smul_eq_zero] at h, norm_num at h, exact h }, { intro h, rw h } end /-- The distance between `p₁` and the reflection of `p₂` equals that between the reflection of `p₁` and `p₂`. -/ lemma dist_reflection (s : affine_subspace ℝ P) [nonempty s] [complete_space s.direction] (p₁ p₂ : P) : dist p₁ (reflection s p₂) = dist (reflection s p₁) p₂ := begin conv_lhs { rw ←reflection_reflection s p₁ }, exact (reflection s).dist_eq _ _ end /-- A point in the subspace is equidistant from another point and its reflection. -/ lemma dist_reflection_eq_of_mem (s : affine_subspace ℝ P) [nonempty s] [complete_space s.direction] {p₁ : P} (hp₁ : p₁ ∈ s) (p₂ : P) : dist p₁ (reflection s p₂) = dist p₁ p₂ := begin rw ←reflection_eq_self_iff p₁ at hp₁, convert (reflection s).dist_eq p₁ p₂, rw hp₁ end /-- The reflection of a point in a subspace is contained in any larger subspace containing both the point and the subspace reflected in. -/ lemma reflection_mem_of_le_of_mem {s₁ s₂ : affine_subspace ℝ P} [nonempty s₁] [complete_space s₁.direction] (hle : s₁ ≤ s₂) {p : P} (hp : p ∈ s₂) : reflection s₁ p ∈ s₂ := begin rw [reflection_apply], have ho : ↑(orthogonal_projection s₁ p) ∈ s₂ := hle (orthogonal_projection_mem p), exact vadd_mem_of_mem_direction (vsub_mem_direction ho hp) ho end /-- Reflecting an orthogonal vector plus a point in the subspace produces the negation of that vector plus the point. -/ lemma reflection_orthogonal_vadd {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] {p : P} (hp : p ∈ s) {v : V} (hv : v ∈ s.directionᗮ) : reflection s (v +ᵥ p) = -v +ᵥ p := begin rw [reflection_apply, orthogonal_projection_vadd_eq_self hp hv, vsub_vadd_eq_vsub_sub], simp end /-- Reflecting a vector plus a point in the subspace produces the negation of that vector plus the point if the vector is a multiple of the result of subtracting a point's orthogonal projection from that point. -/ lemma reflection_vadd_smul_vsub_orthogonal_projection {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] {p₁ : P} (p₂ : P) (r : ℝ) (hp₁ : p₁ ∈ s) : reflection s (r • (p₂ -ᵥ orthogonal_projection s p₂) +ᵥ p₁) = -(r • (p₂ -ᵥ orthogonal_projection s p₂)) +ᵥ p₁ := reflection_orthogonal_vadd hp₁ (submodule.smul_mem _ _ (vsub_orthogonal_projection_mem_direction_orthogonal s _)) omit V /-- A set of points is cospherical if they are equidistant from some point. In two dimensions, this is the same thing as being concyclic. -/ def cospherical (ps : set P) : Prop := ∃ (center : P) (radius : ℝ), ∀ p ∈ ps, dist p center = radius /-- The definition of `cospherical`. -/ lemma cospherical_def (ps : set P) : cospherical ps ↔ ∃ (center : P) (radius : ℝ), ∀ p ∈ ps, dist p center = radius := iff.rfl /-- A subset of a cospherical set is cospherical. -/ lemma cospherical_subset {ps₁ ps₂ : set P} (hs : ps₁ ⊆ ps₂) (hc : cospherical ps₂) : cospherical ps₁ := begin rcases hc with ⟨c, r, hcr⟩, exact ⟨c, r, λ p hp, hcr p (hs hp)⟩ end include V /-- The empty set is cospherical. -/ lemma cospherical_empty : cospherical (∅ : set P) := begin use add_torsor.nonempty.some, simp, end omit V /-- A single point is cospherical. -/ lemma cospherical_singleton (p : P) : cospherical ({p} : set P) := begin use p, simp end include V /-- Two points are cospherical. -/ lemma cospherical_insert_singleton (p₁ p₂ : P) : cospherical ({p₁, p₂} : set P) := begin use [(2⁻¹ : ℝ) • (p₂ -ᵥ p₁) +ᵥ p₁, (2⁻¹ : ℝ) * (dist p₂ p₁)], intro p, rw [set.mem_insert_iff, set.mem_singleton_iff], rintro ⟨_|_⟩, { rw [dist_eq_norm_vsub V p₁, vsub_vadd_eq_vsub_sub, vsub_self, zero_sub, norm_neg, norm_smul, dist_eq_norm_vsub V p₂], simp }, { rw [H, dist_eq_norm_vsub V p₂, vsub_vadd_eq_vsub_sub, dist_eq_norm_vsub V p₂], conv_lhs { congr, congr, rw ←one_smul ℝ (p₂ -ᵥ p₁ : V) }, rw [←sub_smul, norm_smul], norm_num } end /-- Any three points in a cospherical set are affinely independent. -/ lemma cospherical.affine_independent {s : set P} (hs : cospherical s) {p : fin 3 → P} (hps : set.range p ⊆ s) (hpi : function.injective p) : affine_independent ℝ p := begin rw affine_independent_iff_not_collinear, intro hc, rw collinear_iff_of_mem ℝ (set.mem_range_self (0 : fin 3)) at hc, rcases hc with ⟨v, hv⟩, rw set.forall_range_iff at hv, have hv0 : v ≠ 0, { intro h, have he : p 1 = p 0, by simpa [h] using hv 1, exact (dec_trivial : (1 : fin 3) ≠ 0) (hpi he) }, rcases hs with ⟨c, r, hs⟩, have hs' := λ i, hs (p i) (set.mem_of_mem_of_subset (set.mem_range_self _) hps), choose f hf using hv, have hsd : ∀ i, dist ((f i • v) +ᵥ p 0) c = r, { intro i, rw ←hf, exact hs' i }, have hf0 : f 0 = 0, { have hf0' := hf 0, rw [eq_comm, ←@vsub_eq_zero_iff_eq V, vadd_vsub, smul_eq_zero] at hf0', simpa [hv0] using hf0' }, have hfi : function.injective f, { intros i j h, have hi := hf i, rw [h, ←hf j] at hi, exact hpi hi }, simp_rw [←hsd 0, hf0, zero_smul, zero_vadd, dist_smul_vadd_eq_dist (p 0) c hv0] at hsd, have hfn0 : ∀ i, i ≠ 0 → f i ≠ 0 := λ i, (hfi.ne_iff' hf0).2, have hfn0' : ∀ i, i ≠ 0 → f i = (-2) * ⟪v, (p 0 -ᵥ c)⟫ / ⟪v, v⟫, { intros i hi, have hsdi := hsd i, simpa [hfn0, hi] using hsdi }, have hf12 : f 1 = f 2, { rw [hfn0' 1 dec_trivial, hfn0' 2 dec_trivial] }, exact (dec_trivial : (1 : fin 3) ≠ 2) (hfi hf12) end end euclidean_geometry
9dfe1d67f25b2e51be6766efaad8501f6eb0782e
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/category_theory/sites/types.lean
98968d43ab3b71815bddea2e43a50e51b4440dce
[ "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
7,539
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import category_theory.sites.canonical import category_theory.sites.sheaf_of_types /-! # Grothendieck Topology and Sheaves on the Category of Types In this file we define a Grothendieck topology on the category of types, and construct the canonical functor that sends a type to a sheaf over the category of types, and make this an equivalence of categories. Then we prove that the topology defined is the canonical topology. -/ universe u namespace category_theory open_locale category_theory.Type /-- A Grothendieck topology associated to the category of all types. A sieve is a covering iff it is jointly surjective. -/ def types_grothendieck_topology : grothendieck_topology (Type u) := { sieves := λ α S, ∀ x : α, S (λ _ : punit, x), top_mem' := λ α x, trivial, pullback_stable' := λ α β S f hs x, hs (f x), transitive' := λ α S hs R hr x, hr (hs x) punit.star } /-- The discrete sieve on a type, which only includes arrows whose image is a subsingleton. -/ @[simps] def discrete_sieve (α : Type u) : sieve α := { arrows := λ β f, ∃ x, ∀ y, f y = x, downward_closed' := λ β γ f ⟨x, hx⟩ g, ⟨x, λ y, hx $ g y⟩ } lemma discrete_sieve_mem (α : Type u) : discrete_sieve α ∈ types_grothendieck_topology α := λ x, ⟨x, λ y, rfl⟩ /-- The discrete presieve on a type, which only includes arrows whose domain is a singleton. -/ def discrete_presieve (α : Type u) : presieve α := λ β f, ∃ x : β, ∀ y : β, y = x lemma generate_discrete_presieve_mem (α : Type u) : sieve.generate (discrete_presieve α) ∈ types_grothendieck_topology α := λ x, ⟨punit, id, λ _, x, ⟨punit.star, λ _, subsingleton.elim _ _⟩, rfl⟩ open presieve theorem is_sheaf_yoneda' {α : Type u} : is_sheaf types_grothendieck_topology (yoneda.obj α) := λ β S hs x hx, ⟨λ y, x _ (hs y) punit.star, λ γ f h, funext $ λ z, have _ := congr_fun (hx (𝟙 _) (λ _, z) (hs $ f z) h rfl) punit.star, by { convert this, exact rfl }, λ f hf, funext $ λ y, by convert congr_fun (hf _ (hs y)) punit.star⟩ /-- The yoneda functor that sends a type to a sheaf over the category of types -/ @[simps] def yoneda' : Type u ⥤ SheafOfTypes types_grothendieck_topology := { obj := λ α, ⟨yoneda.obj α, is_sheaf_yoneda'⟩, map := λ α β f, yoneda.map f } @[simp] lemma yoneda'_comp : yoneda'.{u} ⋙ induced_functor _ = yoneda := rfl open opposite /-- Given a presheaf `P` on the category of types, construct a map `P(α) → (α → P(*))` for all type `α`. -/ def eval (P : (Type u)ᵒᵖ ⥤ Type u) (α : Type u) (s : P.obj (op α)) (x : α) : P.obj (op punit) := P.map (↾λ _, x).op s /-- Given a sheaf `S` on the category of types, construct a map `(α → S(*)) → S(α)` that is inverse to `eval`. -/ noncomputable def types_glue (S : (Type u)ᵒᵖ ⥤ Type u) (hs : is_sheaf types_grothendieck_topology S) (α : Type u) (f : α → S.obj (op punit)) : S.obj (op α) := (hs.is_sheaf_for _ _ (generate_discrete_presieve_mem α)).amalgamate (λ β g hg, S.map (↾λ x, punit.star).op $ f $ g $ classical.some hg) (λ β γ δ g₁ g₂ f₁ f₂ hf₁ hf₂ h, (hs.is_sheaf_for _ _ (generate_discrete_presieve_mem δ)).is_separated_for.ext $ λ ε g ⟨x, hx⟩, have f₁ (classical.some hf₁) = f₂ (classical.some hf₂), from classical.some_spec hf₁ (g₁ $ g x) ▸ classical.some_spec hf₂ (g₂ $ g x) ▸ congr_fun h _, by { simp_rw [← functor_to_types.map_comp_apply, this, ← op_comp], refl }) lemma eval_types_glue {S hs α} (f) : eval.{u} S α (types_glue S hs α f) = f := funext $ λ x, (is_sheaf_for.valid_glue _ _ _ $ by exact ⟨punit.star, λ _, subsingleton.elim _ _⟩).trans $ by { convert functor_to_types.map_id_apply _ _, rw ← op_id, congr } lemma types_glue_eval {S hs α} (s) : types_glue.{u} S hs α (eval S α s) = s := (hs.is_sheaf_for _ _ (generate_discrete_presieve_mem α)).is_separated_for.ext $ λ β f hf, (is_sheaf_for.valid_glue _ _ _ hf).trans $ (functor_to_types.map_comp_apply _ _ _ _).symm.trans $ by { rw ← op_comp, congr' 2, exact funext (λ x, congr_arg f (classical.some_spec hf x).symm) } /-- Given a sheaf `S`, construct an equivalence `S(α) ≃ (α → S(*))`. -/ @[simps] noncomputable def eval_equiv (S : (Type u)ᵒᵖ ⥤ Type u) (hs : is_sheaf types_grothendieck_topology S) (α : Type u) : S.obj (op α) ≃ (α → S.obj (op punit)) := { to_fun := eval S α, inv_fun := types_glue S hs α, left_inv := types_glue_eval, right_inv := eval_types_glue } lemma eval_map (S : (Type u)ᵒᵖ ⥤ Type u) (α β) (f : β ⟶ α) (s x) : eval S β (S.map f.op s) x = eval S α s (f x) := by { simp_rw [eval, ← functor_to_types.map_comp_apply, ← op_comp], refl } /-- Given a sheaf `S`, construct an isomorphism `S ≅ [-, S(*)]`. -/ @[simps] noncomputable def equiv_yoneda (S : (Type u)ᵒᵖ ⥤ Type u) (hs : is_sheaf types_grothendieck_topology S) : S ≅ yoneda.obj (S.obj (op punit)) := nat_iso.of_components (λ α, equiv.to_iso $ eval_equiv S hs $ unop α) $ λ α β f, funext $ λ s, funext $ λ x, eval_map S (unop α) (unop β) f.unop _ _ /-- Given a sheaf `S`, construct an isomorphism `S ≅ [-, S(*)]`. -/ @[simps] noncomputable def equiv_yoneda' (S : SheafOfTypes types_grothendieck_topology) : S ≅ yoneda'.obj (S.1.obj (op punit)) := { hom := (equiv_yoneda S.1 S.2).hom, inv := (equiv_yoneda S.1 S.2).inv, hom_inv_id' := (equiv_yoneda S.1 S.2).hom_inv_id, inv_hom_id' := (equiv_yoneda S.1 S.2).inv_hom_id } lemma eval_app (S₁ S₂ : SheafOfTypes.{u} types_grothendieck_topology) (f : S₁ ⟶ S₂) (α : Type u) (s : S₁.1.obj (op α)) (x : α) : eval S₂.1 α (f.app (op α) s) x = f.app (op punit) (eval S₁.1 α s x) := (congr_fun (f.2 (↾λ _ : punit, x).op) s).symm /-- `yoneda'` induces an equivalence of category between `Type u` and `Sheaf types_grothendieck_topology`. -/ @[simps] noncomputable def type_equiv : Type u ≌ SheafOfTypes types_grothendieck_topology := equivalence.mk yoneda' (induced_functor _ ⋙ (evaluation _ _).obj (op punit)) (nat_iso.of_components (λ α, /- α ≅ punit ⟶ α -/ { hom := λ x _, x, inv := λ f, f punit.star, hom_inv_id' := funext $ λ x, rfl, inv_hom_id' := funext $ λ f, funext $ λ y, punit.cases_on y rfl }) (λ α β f, rfl)) (iso.symm $ nat_iso.of_components (λ S, equiv_yoneda' S) (λ S₁ S₂ f, nat_trans.ext _ _ $ funext $ λ α, funext $ λ s, funext $ λ x, eval_app S₁ S₂ f (unop α) s x)) lemma subcanonical_types_grothendieck_topology : sheaf.subcanonical types_grothendieck_topology.{u} := sheaf.subcanonical.of_yoneda_is_sheaf _ (λ X, is_sheaf_yoneda') lemma types_grothendieck_topology_eq_canonical : types_grothendieck_topology.{u} = sheaf.canonical_topology (Type u) := le_antisymm subcanonical_types_grothendieck_topology $ Inf_le ⟨yoneda.obj (ulift bool), ⟨_, rfl⟩, grothendieck_topology.ext $ funext $ λ α, set.ext $ λ S, ⟨λ hs x, classical.by_contradiction $ λ hsx, have (λ _, ulift.up tt : (yoneda.obj (ulift bool)).obj (op punit)) = λ _, ulift.up ff := (hs punit (λ _, x)).is_separated_for.ext $ λ β f hf, funext $ λ y, hsx.elim $ S.2 hf $ λ _, y, bool.no_confusion $ ulift.up.inj $ (congr_fun this punit.star : _), λ hs β f, is_sheaf_yoneda' _ $ λ y, hs _⟩⟩ end category_theory
f4d55f56ab4d05decd26d5ac070e21cfcab2061e
07c6143268cfb72beccd1cc35735d424ebcb187b
/src/data/real/nnreal.lean
1bb44eae3357fd94d9b184d6f7cf7a55ac2d0946
[ "Apache-2.0" ]
permissive
khoek/mathlib
bc49a842910af13a3c372748310e86467d1dc766
aa55f8b50354b3e11ba64792dcb06cccb2d8ee28
refs/heads/master
1,588,232,063,837
1,587,304,803,000
1,587,304,803,000
176,688,517
0
0
Apache-2.0
1,553,070,585,000
1,553,070,585,000
null
UTF-8
Lean
false
false
25,066
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin Nonnegative real numbers. -/ import data.real.basic order.lattice algebra.field noncomputable theory open_locale classical /-- Nonnegative real numbers. -/ def nnreal := {r : ℝ // 0 ≤ r} localized "notation ` ℝ≥0 ` := nnreal" in nnreal namespace nnreal instance : has_coe ℝ≥0 ℝ := ⟨subtype.val⟩ /- Simp lemma to put back `n.val` into the normal form given by the coercion. -/ @[simp] lemma val_eq_coe (n : nnreal) : n.val = n := rfl instance : can_lift ℝ nnreal := { coe := coe, cond := λ r, r ≥ 0, prf := λ x hx, ⟨⟨x, hx⟩, rfl⟩ } protected lemma eq {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) → n = m := subtype.eq protected lemma eq_iff {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) ↔ n = m := iff.intro nnreal.eq (congr_arg coe) lemma ne_iff {x y : ℝ≥0} : (x : ℝ) ≠ (y : ℝ) ↔ x ≠ y := not_iff_not_of_iff $ nnreal.eq_iff protected def of_real (r : ℝ) : ℝ≥0 := ⟨max r 0, le_max_right _ _⟩ lemma coe_of_real (r : ℝ) (hr : 0 ≤ r) : (nnreal.of_real r : ℝ) = r := max_eq_left hr lemma le_coe_of_real (r : ℝ) : r ≤ nnreal.of_real r := le_max_left r 0 lemma coe_nonneg (r : nnreal) : (0 : ℝ) ≤ r := r.2 @[norm_cast] theorem coe_mk (a : ℝ) (ha) : ((⟨a, ha⟩ : ℝ≥0) : ℝ) = a := rfl instance : has_zero ℝ≥0 := ⟨⟨0, le_refl 0⟩⟩ instance : has_one ℝ≥0 := ⟨⟨1, zero_le_one⟩⟩ instance : has_add ℝ≥0 := ⟨λa b, ⟨a + b, add_nonneg a.2 b.2⟩⟩ instance : has_sub ℝ≥0 := ⟨λa b, nnreal.of_real (a - b)⟩ instance : has_mul ℝ≥0 := ⟨λa b, ⟨a * b, mul_nonneg a.2 b.2⟩⟩ instance : has_inv ℝ≥0 := ⟨λa, ⟨(a.1)⁻¹, inv_nonneg.2 a.2⟩⟩ instance : has_div ℝ≥0 := ⟨λa b, ⟨a.1 / b.1, div_nonneg' a.2 b.2⟩⟩ instance : has_le ℝ≥0 := ⟨λ r s, (r:ℝ) ≤ s⟩ instance : has_bot ℝ≥0 := ⟨0⟩ instance : inhabited ℝ≥0 := ⟨0⟩ @[simp, norm_cast] protected lemma coe_eq {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) = r₂ ↔ r₁ = r₂ := subtype.ext.symm @[simp, norm_cast] protected lemma coe_zero : ((0 : ℝ≥0) : ℝ) = 0 := rfl @[simp, norm_cast] protected lemma coe_one : ((1 : ℝ≥0) : ℝ) = 1 := rfl @[simp, norm_cast] protected lemma coe_add (r₁ r₂ : ℝ≥0) : ((r₁ + r₂ : ℝ≥0) : ℝ) = r₁ + r₂ := rfl @[simp, norm_cast] protected lemma coe_mul (r₁ r₂ : ℝ≥0) : ((r₁ * r₂ : ℝ≥0) : ℝ) = r₁ * r₂ := rfl @[simp, norm_cast] protected lemma coe_div (r₁ r₂ : ℝ≥0) : ((r₁ / r₂ : ℝ≥0) : ℝ) = r₁ / r₂ := rfl @[simp, norm_cast] protected lemma coe_inv (r : ℝ≥0) : ((r⁻¹ : ℝ≥0) : ℝ) = r⁻¹ := rfl @[simp, norm_cast] protected lemma coe_bit0 (r : ℝ≥0) : ((bit0 r : ℝ≥0) : ℝ) = bit0 r := by unfold bit0; norm_cast @[simp, norm_cast] protected lemma coe_bit1 (r : ℝ≥0) : ((bit1 r : ℝ≥0) : ℝ) = bit1 r := by unfold bit1; norm_cast @[simp, norm_cast] protected lemma coe_sub {r₁ r₂ : ℝ≥0} (h : r₂ ≤ r₁) : ((r₁ - r₂ : ℝ≥0) : ℝ) = r₁ - r₂ := max_eq_left $ le_sub.2 $ by simp [show (r₂ : ℝ) ≤ r₁, from h] -- TODO: setup semifield! @[simp] protected lemma zero_div (r : ℝ≥0) : 0 / r = 0 := nnreal.eq (zero_div _) @[simp] protected lemma coe_eq_zero (r : ℝ≥0) : ↑r = (0 : ℝ) ↔ r = 0 := by norm_cast lemma coe_ne_zero {r : ℝ≥0} : (r : ℝ) ≠ 0 ↔ r ≠ 0 := by norm_cast instance : comm_semiring ℝ≥0 := begin refine { zero := 0, add := (+), one := 1, mul := (*), ..}; { intros; apply nnreal.eq; simp [mul_comm, mul_assoc, add_comm_monoid.add, left_distrib, right_distrib, add_comm_monoid.zero, add_comm, add_left_comm] } end instance : is_semiring_hom (coe : ℝ≥0 → ℝ) := by refine_struct {..}; intros; refl instance : comm_group_with_zero ℝ≥0 := { zero_ne_one := assume h, zero_ne_one $ nnreal.eq_iff.2 h, inv_zero := nnreal.eq $ inv_zero, mul_inv_cancel := assume x h, nnreal.eq $ mul_inv_cancel $ ne_iff.2 h, .. (by apply_instance : has_inv ℝ≥0), .. (_ : comm_semiring ℝ≥0), .. (_ : semiring ℝ≥0) } @[norm_cast] lemma coe_pow (r : ℝ≥0) (n : ℕ) : ((r^n : ℝ≥0) : ℝ) = r^n := is_monoid_hom.map_pow coe r n @[norm_cast] lemma coe_list_sum (l : list ℝ≥0) : ((l.sum : ℝ≥0) : ℝ) = (l.map coe).sum := eq.symm $ l.sum_hom coe @[norm_cast] lemma coe_list_prod (l : list ℝ≥0) : ((l.prod : ℝ≥0) : ℝ) = (l.map coe).prod := eq.symm $ l.prod_hom coe @[norm_cast] lemma coe_multiset_sum (s : multiset ℝ≥0) : ((s.sum : ℝ≥0) : ℝ) = (s.map coe).sum := eq.symm $ s.sum_hom coe @[norm_cast] lemma coe_multiset_prod (s : multiset ℝ≥0) : ((s.prod : ℝ≥0) : ℝ) = (s.map coe).prod := eq.symm $ s.prod_hom coe @[norm_cast] lemma coe_sum {α} {s : finset α} {f : α → ℝ≥0} : ↑(s.sum f) = s.sum (λa, (f a : ℝ)) := eq.symm $ s.sum_hom coe @[norm_cast] lemma coe_prod {α} {s : finset α} {f : α → ℝ≥0} : ↑(s.prod f) = s.prod (λa, (f a : ℝ)) := eq.symm $ s.prod_hom coe @[norm_cast] lemma smul_coe (r : ℝ≥0) (n : ℕ) : ↑(add_monoid.smul n r) = add_monoid.smul n (r:ℝ) := is_add_monoid_hom.map_smul coe r n @[simp, norm_cast] protected lemma coe_nat_cast (n : ℕ) : (↑(↑n : ℝ≥0) : ℝ) = n := (ring_hom.of (coe : ℝ≥0 → ℝ)).map_nat_cast n instance : decidable_linear_order ℝ≥0 := decidable_linear_order.lift (coe : ℝ≥0 → ℝ) subtype.val_injective (by apply_instance) @[norm_cast] protected lemma coe_le_coe {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) ≤ r₂ ↔ r₁ ≤ r₂ := iff.rfl @[norm_cast] protected lemma coe_lt_coe {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) < r₂ ↔ r₁ < r₂ := iff.rfl protected lemma coe_pos {r : ℝ≥0} : (0 : ℝ) < r ↔ 0 < r := iff.rfl protected lemma coe_mono : monotone (coe : ℝ≥0 → ℝ) := λ _ _, nnreal.coe_le_coe.2 protected lemma of_real_mono : monotone nnreal.of_real := λ x y h, max_le_max h (le_refl 0) @[simp] lemma of_real_coe {r : ℝ≥0} : nnreal.of_real r = r := nnreal.eq $ max_eq_left r.2 /-- `nnreal.of_real` and `coe : ℝ≥0 → ℝ` form a Galois insertion. -/ protected def gi : galois_insertion nnreal.of_real coe := galois_insertion.monotone_intro nnreal.coe_mono nnreal.of_real_mono le_coe_of_real (λ _, of_real_coe) instance : order_bot ℝ≥0 := { bot := ⊥, bot_le := assume ⟨a, h⟩, h, .. nnreal.decidable_linear_order } instance : canonically_ordered_add_monoid ℝ≥0 := { add_le_add_left := assume a b h c, @add_le_add_left ℝ _ a b h c, lt_of_add_lt_add_left := assume a b c, @lt_of_add_lt_add_left ℝ _ a b c, le_iff_exists_add := assume ⟨a, ha⟩ ⟨b, hb⟩, iff.intro (assume h : a ≤ b, ⟨⟨b - a, le_sub_iff_add_le.2 $ by simp [h]⟩, nnreal.eq $ show b = a + (b - a), by rw [add_sub_cancel'_right]⟩) (assume ⟨⟨c, hc⟩, eq⟩, eq.symm ▸ show a ≤ a + c, from (le_add_iff_nonneg_right a).2 hc), ..nnreal.comm_semiring, ..nnreal.order_bot, ..nnreal.decidable_linear_order } instance : distrib_lattice ℝ≥0 := by apply_instance instance : semilattice_inf_bot ℝ≥0 := { .. nnreal.order_bot, .. nnreal.distrib_lattice } instance : semilattice_sup_bot ℝ≥0 := { .. nnreal.order_bot, .. nnreal.distrib_lattice } instance : linear_ordered_semiring ℝ≥0 := { add_left_cancel := assume a b c h, nnreal.eq $ @add_left_cancel ℝ _ a b c (nnreal.eq_iff.2 h), add_right_cancel := assume a b c h, nnreal.eq $ @add_right_cancel ℝ _ a b c (nnreal.eq_iff.2 h), le_of_add_le_add_left := assume a b c, @le_of_add_le_add_left ℝ _ a b c, mul_lt_mul_of_pos_left := assume a b c, @mul_lt_mul_of_pos_left ℝ _ a b c, mul_lt_mul_of_pos_right := assume a b c, @mul_lt_mul_of_pos_right ℝ _ a b c, zero_lt_one := @zero_lt_one ℝ _, .. nnreal.decidable_linear_order, .. nnreal.canonically_ordered_add_monoid, .. nnreal.comm_semiring } instance : canonically_ordered_comm_semiring ℝ≥0 := { zero_ne_one := assume h, @zero_ne_one ℝ _ $ congr_arg subtype.val $ h, mul_eq_zero_iff := assume a b, nnreal.eq_iff.symm.trans $ mul_eq_zero.trans $ by simp, .. nnreal.linear_ordered_semiring, .. nnreal.canonically_ordered_add_monoid, .. nnreal.comm_semiring } instance : densely_ordered ℝ≥0 := ⟨assume a b (h : (a : ℝ) < b), let ⟨c, hac, hcb⟩ := dense h in ⟨⟨c, le_trans a.property $ le_of_lt $ hac⟩, hac, hcb⟩⟩ instance : no_top_order ℝ≥0 := ⟨assume a, let ⟨b, hb⟩ := no_top (a:ℝ) in ⟨⟨b, le_trans a.property $ le_of_lt $ hb⟩, hb⟩⟩ lemma bdd_above_coe {s : set ℝ≥0} : bdd_above ((coe : nnreal → ℝ) '' s) ↔ bdd_above s := iff.intro (assume ⟨b, hb⟩, ⟨nnreal.of_real b, assume ⟨y, hy⟩ hys, show y ≤ max b 0, from le_max_left_of_le $ hb $ set.mem_image_of_mem _ hys⟩) (assume ⟨b, hb⟩, ⟨b, assume y ⟨x, hx, eq⟩, eq ▸ hb hx⟩) lemma bdd_below_coe (s : set ℝ≥0) : bdd_below ((coe : nnreal → ℝ) '' s) := ⟨0, assume r ⟨q, _, eq⟩, eq ▸ q.2⟩ instance : has_Sup ℝ≥0 := ⟨λs, ⟨Sup ((coe : nnreal → ℝ) '' s), begin cases s.eq_empty_or_nonempty with h h, { simp [h, set.image_empty, real.Sup_empty] }, rcases h with ⟨⟨b, hb⟩, hbs⟩, by_cases h' : bdd_above s, { exact le_cSup_of_le (bdd_above_coe.2 h') (set.mem_image_of_mem _ hbs) hb }, { rw [real.Sup_of_not_bdd_above], rwa [bdd_above_coe] } end⟩⟩ instance : has_Inf ℝ≥0 := ⟨λs, ⟨Inf ((coe : nnreal → ℝ) '' s), begin cases s.eq_empty_or_nonempty with h h, { simp [h, set.image_empty, real.Inf_empty] }, exact le_cInf (h.image _) (assume r ⟨q, _, eq⟩, eq ▸ q.2) end⟩⟩ lemma coe_Sup (s : set nnreal) : (↑(Sup s) : ℝ) = Sup ((coe : nnreal → ℝ) '' s) := rfl lemma coe_Inf (s : set nnreal) : (↑(Inf s) : ℝ) = Inf ((coe : nnreal → ℝ) '' s) := rfl instance : conditionally_complete_linear_order_bot ℝ≥0 := { Sup := Sup, Inf := Inf, le_cSup := assume s a hs ha, le_cSup (bdd_above_coe.2 hs) (set.mem_image_of_mem _ ha), cSup_le := assume s a hs h,show Sup ((coe : nnreal → ℝ) '' s) ≤ a, from cSup_le (by simp [hs]) $ assume r ⟨b, hb, eq⟩, eq ▸ h hb, cInf_le := assume s a _ has, cInf_le (bdd_below_coe s) (set.mem_image_of_mem _ has), le_cInf := assume s a hs h, show (↑a : ℝ) ≤ Inf ((coe : nnreal → ℝ) '' s), from le_cInf (by simp [hs]) $ assume r ⟨b, hb, eq⟩, eq ▸ h hb, cSup_empty := nnreal.eq $ by simp [coe_Sup, real.Sup_empty]; refl, decidable_le := begin assume x y, apply classical.dec end, .. nnreal.linear_ordered_semiring, .. lattice_of_decidable_linear_order, .. nnreal.order_bot } instance : archimedean nnreal := ⟨ assume x y pos_y, let ⟨n, hr⟩ := archimedean.arch (x:ℝ) (pos_y : (0 : ℝ) < y) in ⟨n, show (x:ℝ) ≤ (add_monoid.smul n y : nnreal), by simp [*, smul_coe]⟩ ⟩ lemma le_of_forall_epsilon_le {a b : nnreal} (h : ∀ε, ε > 0 → a ≤ b + ε) : a ≤ b := le_of_forall_le_of_dense $ assume x hxb, begin rcases le_iff_exists_add.1 (le_of_lt hxb) with ⟨ε, rfl⟩, exact h _ ((lt_add_iff_pos_right b).1 hxb) end lemma lt_iff_exists_rat_btwn (a b : nnreal) : a < b ↔ (∃q:ℚ, 0 ≤ q ∧ a < nnreal.of_real q ∧ nnreal.of_real q < b) := iff.intro (assume (h : (↑a:ℝ) < (↑b:ℝ)), let ⟨q, haq, hqb⟩ := exists_rat_btwn h in have 0 ≤ (q : ℝ), from le_trans a.2 $ le_of_lt haq, ⟨q, rat.cast_nonneg.1 this, by simp [coe_of_real _ this, nnreal.coe_lt_coe.symm, haq, hqb]⟩) (assume ⟨q, _, haq, hqb⟩, lt_trans haq hqb) lemma bot_eq_zero : (⊥ : nnreal) = 0 := rfl lemma mul_sup (a b c : ℝ≥0) : a * (b ⊔ c) = (a * b) ⊔ (a * c) := begin cases le_total b c with h h, { simp [sup_eq_max, max_eq_right h, max_eq_right (mul_le_mul_of_nonneg_left h (zero_le a))] }, { simp [sup_eq_max, max_eq_left h, max_eq_left (mul_le_mul_of_nonneg_left h (zero_le a))] }, end lemma mul_finset_sup {α} {f : α → ℝ≥0} {s : finset α} (r : ℝ≥0) : r * s.sup f = s.sup (λa, r * f a) := begin refine s.induction_on _ _, { simp [bot_eq_zero] }, { assume a s has ih, simp [has, ih, mul_sup], } end @[simp, norm_cast] lemma coe_max (x y : nnreal) : ((max x y : nnreal) : ℝ) = max (x : ℝ) (y : ℝ) := by { delta max, split_ifs; refl } @[simp, norm_cast] lemma coe_min (x y : nnreal) : ((min x y : nnreal) : ℝ) = min (x : ℝ) (y : ℝ) := by { delta min, split_ifs; refl } section of_real @[simp] lemma zero_le_coe {q : nnreal} : 0 ≤ (q : ℝ) := q.2 @[simp] lemma of_real_zero : nnreal.of_real 0 = 0 := by simp [nnreal.of_real]; refl @[simp] lemma of_real_one : nnreal.of_real 1 = 1 := by simp [nnreal.of_real, max_eq_left (zero_le_one : (0 :ℝ) ≤ 1)]; refl @[simp] lemma of_real_pos {r : ℝ} : 0 < nnreal.of_real r ↔ 0 < r := by simp [nnreal.of_real, nnreal.coe_lt_coe.symm, lt_irrefl] @[simp] lemma of_real_eq_zero {r : ℝ} : nnreal.of_real r = 0 ↔ r ≤ 0 := by simpa [-of_real_pos] using (not_iff_not.2 (@of_real_pos r)) lemma of_real_of_nonpos {r : ℝ} : r ≤ 0 → nnreal.of_real r = 0 := of_real_eq_zero.2 @[simp] lemma of_real_le_of_real_iff {r p : ℝ} (hp : 0 ≤ p) : nnreal.of_real r ≤ nnreal.of_real p ↔ r ≤ p := by simp [nnreal.coe_le_coe.symm, nnreal.of_real, hp] @[simp] lemma of_real_lt_of_real_iff' {r p : ℝ} : nnreal.of_real r < nnreal.of_real p ↔ r < p ∧ 0 < p := by simp [nnreal.coe_lt_coe.symm, nnreal.of_real, lt_irrefl] lemma of_real_lt_of_real_iff {r p : ℝ} (h : 0 < p) : nnreal.of_real r < nnreal.of_real p ↔ r < p := of_real_lt_of_real_iff'.trans (and_iff_left h) lemma of_real_lt_of_real_iff_of_nonneg {r p : ℝ} (hr : 0 ≤ r) : nnreal.of_real r < nnreal.of_real p ↔ r < p := of_real_lt_of_real_iff'.trans ⟨and.left, λ h, ⟨h, lt_of_le_of_lt hr h⟩⟩ @[simp] lemma of_real_add {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : nnreal.of_real (r + p) = nnreal.of_real r + nnreal.of_real p := nnreal.eq $ by simp [nnreal.of_real, hr, hp, add_nonneg] lemma of_real_add_of_real {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : nnreal.of_real r + nnreal.of_real p = nnreal.of_real (r + p) := (of_real_add hr hp).symm lemma of_real_le_of_real {r p : ℝ} (h : r ≤ p) : nnreal.of_real r ≤ nnreal.of_real p := nnreal.of_real_mono h lemma of_real_add_le {r p : ℝ} : nnreal.of_real (r + p) ≤ nnreal.of_real r + nnreal.of_real p := nnreal.coe_le_coe.1 $ max_le (add_le_add (le_max_left _ _) (le_max_left _ _)) nnreal.zero_le_coe lemma of_real_le_iff_le_coe {r : ℝ} {p : nnreal} : nnreal.of_real r ≤ p ↔ r ≤ ↑p := nnreal.gi.gc r p lemma le_of_real_iff_coe_le {r : nnreal} {p : ℝ} (hp : p ≥ 0) : r ≤ nnreal.of_real p ↔ ↑r ≤ p := by rw [← nnreal.coe_le_coe, nnreal.coe_of_real p hp] lemma of_real_lt_iff_lt_coe {r : ℝ} {p : nnreal} (ha : r ≥ 0) : nnreal.of_real r < p ↔ r < ↑p := by rw [← nnreal.coe_lt_coe, nnreal.coe_of_real r ha] lemma lt_of_real_iff_coe_lt {r : nnreal} {p : ℝ} : r < nnreal.of_real p ↔ ↑r < p := begin cases le_total 0 p, { rw [← nnreal.coe_lt_coe, nnreal.coe_of_real p h] }, { rw [of_real_eq_zero.2 h], split, intro, have := not_lt_of_le (zero_le r), contradiction, intro rp, have : ¬(p ≤ 0) := not_le_of_lt (lt_of_le_of_lt (coe_nonneg _) rp), contradiction } end end of_real section mul lemma mul_eq_mul_left {a b c : nnreal} (h : a ≠ 0) : (a * b = a * c ↔ b = c) := begin rw [← nnreal.eq_iff, ← nnreal.eq_iff, nnreal.coe_mul, nnreal.coe_mul], split, { exact eq_of_mul_eq_mul_left (mt (@nnreal.eq_iff a 0).1 h) }, { assume h, rw [h] } end lemma of_real_mul {p q : ℝ} (hp : 0 ≤ p) : nnreal.of_real (p * q) = nnreal.of_real p * nnreal.of_real q := begin cases le_total 0 q with hq hq, { apply nnreal.eq, have := max_eq_left (mul_nonneg hp hq), simpa [nnreal.of_real, hp, hq, max_eq_left] }, { have hpq := mul_nonpos_of_nonneg_of_nonpos hp hq, rw [of_real_eq_zero.2 hq, of_real_eq_zero.2 hpq, mul_zero] } end @[field_simps] theorem mul_ne_zero' {a b : nnreal} (h₁ : a ≠ 0) (h₂ : b ≠ 0) : a * b ≠ 0 := mul_ne_zero'' h₁ h₂ end mul section sub lemma sub_def {r p : ℝ≥0} : r - p = nnreal.of_real (r - p) := rfl lemma sub_eq_zero {r p : ℝ≥0} (h : r ≤ p) : r - p = 0 := nnreal.eq $ max_eq_right $ sub_le_iff_le_add.2 $ by simpa [nnreal.coe_le_coe] using h @[simp] lemma sub_self {r : ℝ≥0} : r - r = 0 := sub_eq_zero $ le_refl r @[simp] lemma sub_zero {r : ℝ≥0} : r - 0 = r := by rw [sub_def, nnreal.coe_zero, sub_zero, nnreal.of_real_coe] lemma sub_pos {r p : ℝ≥0} : 0 < r - p ↔ p < r := of_real_pos.trans $ sub_pos.trans $ nnreal.coe_lt_coe protected lemma sub_lt_self {r p : nnreal} : 0 < r → 0 < p → r - p < r := assume hr hp, begin cases le_total r p, { rwa [sub_eq_zero h] }, { rw [← nnreal.coe_lt_coe, nnreal.coe_sub h], exact sub_lt_self _ hp } end @[simp] lemma sub_le_iff_le_add {r p q : nnreal} : r - p ≤ q ↔ r ≤ q + p := match le_total p r with | or.inl h := by rw [← nnreal.coe_le_coe, ← nnreal.coe_le_coe, nnreal.coe_sub h, nnreal.coe_add, sub_le_iff_le_add] | or.inr h := have r ≤ p + q, from le_add_right h, by simpa [nnreal.coe_le_coe, nnreal.coe_le_coe, sub_eq_zero h, add_comm] end @[simp] lemma sub_le_self {r p : ℝ≥0} : r - p ≤ r := sub_le_iff_le_add.2 $ le_add_right $ le_refl r lemma add_sub_cancel {r p : nnreal} : (p + r) - r = p := nnreal.eq $ by rw [nnreal.coe_sub, nnreal.coe_add, add_sub_cancel]; exact le_add_left (le_refl _) lemma add_sub_cancel' {r p : nnreal} : (r + p) - r = p := by rw [add_comm, add_sub_cancel] @[simp] lemma sub_add_cancel_of_le {a b : nnreal} (h : b ≤ a) : (a - b) + b = a := nnreal.eq $ by rw [nnreal.coe_add, nnreal.coe_sub h, sub_add_cancel] lemma sub_sub_cancel_of_le {r p : ℝ≥0} (h : r ≤ p) : p - (p - r) = r := by rw [nnreal.sub_def, nnreal.sub_def, nnreal.coe_of_real _ $ sub_nonneg.2 h, sub_sub_cancel, nnreal.of_real_coe] lemma lt_sub_iff_add_lt {p q r : nnreal} : p < q - r ↔ p + r < q := begin split, { assume H, have : (((q - r) : nnreal) : ℝ) = (q : ℝ) - (r : ℝ) := nnreal.coe_sub (le_of_lt (sub_pos.1 (lt_of_le_of_lt (zero_le _) H))), rwa [← nnreal.coe_lt_coe, this, lt_sub_iff_add_lt, ← nnreal.coe_add] at H }, { assume H, have : r ≤ q := le_trans (le_add_left (le_refl _)) (le_of_lt H), rwa [← nnreal.coe_lt_coe, nnreal.coe_sub this, lt_sub_iff_add_lt, ← nnreal.coe_add] } end end sub section inv lemma div_def {r p : nnreal} : r / p = r * p⁻¹ := rfl @[simp] lemma inv_zero : (0 : nnreal)⁻¹ = 0 := nnreal.eq inv_zero @[simp] lemma inv_eq_zero {r : nnreal} : (r : nnreal)⁻¹ = 0 ↔ r = 0 := inv_eq_zero @[simp] lemma inv_pos {r : nnreal} : 0 < r⁻¹ ↔ 0 < r := by simp [zero_lt_iff_ne_zero] lemma div_pos {r p : ℝ≥0} (hr : 0 < r) (hp : 0 < p) : 0 < r / p := mul_pos hr (inv_pos.2 hp) @[simp] lemma inv_one : (1:ℝ≥0)⁻¹ = 1 := nnreal.eq $ inv_one @[simp] lemma div_one {r : ℝ≥0} : r / 1 = r := by rw [div_def, inv_one, mul_one] protected lemma mul_inv {r p : ℝ≥0} : (r * p)⁻¹ = p⁻¹ * r⁻¹ := nnreal.eq $ mul_inv' _ _ protected lemma inv_pow {r : ℝ≥0} {n : ℕ} : (r^n)⁻¹ = (r⁻¹)^n := nnreal.eq $ by { push_cast, exact (inv_pow' _ _).symm } @[simp] lemma inv_mul_cancel {r : ℝ≥0} (h : r ≠ 0) : r⁻¹ * r = 1 := nnreal.eq $ inv_mul_cancel $ mt (@nnreal.eq_iff r 0).1 h @[simp] lemma mul_inv_cancel {r : ℝ≥0} (h : r ≠ 0) : r * r⁻¹ = 1 := by rw [mul_comm, inv_mul_cancel h] @[simp] lemma div_self {r : ℝ≥0} (h : r ≠ 0) : r / r = 1 := mul_inv_cancel h @[simp] lemma div_mul_cancel {r p : ℝ≥0} (h : p ≠ 0) : r / p * p = r := by rw [div_def, mul_assoc, inv_mul_cancel h, mul_one] @[simp] lemma mul_div_cancel {r p : ℝ≥0} (h : p ≠ 0) : r * p / p = r := by rw [div_def, mul_assoc, mul_inv_cancel h, mul_one] @[simp] lemma mul_div_cancel' {r p : ℝ≥0} (h : r ≠ 0) : r * (p / r) = p := by rw [mul_comm, div_mul_cancel h] @[simp] lemma inv_inv {r : ℝ≥0} : r⁻¹⁻¹ = r := nnreal.eq (inv_inv' _) @[simp] lemma inv_le {r p : ℝ≥0} (h : r ≠ 0) : r⁻¹ ≤ p ↔ 1 ≤ r * p := by rw [← mul_le_mul_left (zero_lt_iff_ne_zero.2 h), mul_inv_cancel h] lemma inv_le_of_le_mul {r p : ℝ≥0} (h : 1 ≤ r * p) : r⁻¹ ≤ p := by by_cases r = 0; simp [*, inv_le] @[simp] lemma le_inv_iff_mul_le {r p : ℝ≥0} (h : p ≠ 0) : (r ≤ p⁻¹ ↔ r * p ≤ 1) := by rw [← mul_le_mul_left (zero_lt_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] @[simp] lemma lt_inv_iff_mul_lt {r p : ℝ≥0} (h : p ≠ 0) : (r < p⁻¹ ↔ r * p < 1) := by rw [← mul_lt_mul_left (zero_lt_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] lemma mul_le_iff_le_inv {a b r : ℝ≥0} (hr : r ≠ 0) : r * a ≤ b ↔ a ≤ r⁻¹ * b := have 0 < r, from lt_of_le_of_ne (zero_le r) hr.symm, by rw [← @mul_le_mul_left _ _ a _ r this, ← mul_assoc, mul_inv_cancel hr, one_mul] lemma le_div_iff_mul_le {a b r : ℝ≥0} (hr : r ≠ 0) : a ≤ b / r ↔ a * r ≤ b := by rw [div_def, mul_comm, ← mul_le_iff_le_inv hr, mul_comm] lemma le_of_forall_lt_one_mul_lt {x y : ℝ≥0} (h : ∀a<1, a * x ≤ y) : x ≤ y := le_of_forall_ge_of_dense $ assume a ha, have hx : x ≠ 0 := zero_lt_iff_ne_zero.1 (lt_of_le_of_lt (zero_le _) ha), have hx' : x⁻¹ ≠ 0, by rwa [(≠), inv_eq_zero], have a * x⁻¹ < 1, by rwa [← lt_inv_iff_mul_lt hx', inv_inv], have (a * x⁻¹) * x ≤ y, from h _ this, by rwa [mul_assoc, inv_mul_cancel hx, mul_one] at this lemma div_add_div_same (a b c : ℝ≥0) : a / c + b / c = (a + b) / c := eq.symm $ right_distrib a b (c⁻¹) lemma half_pos {a : ℝ≥0} (h : 0 < a) : 0 < a / 2 := div_pos h zero_lt_two lemma add_halves (a : ℝ≥0) : a / 2 + a / 2 = a := nnreal.eq (add_halves a) lemma half_lt_self {a : ℝ≥0} (h : a ≠ 0) : a / 2 < a := by rw [← nnreal.coe_lt_coe, nnreal.coe_div]; exact half_lt_self (bot_lt_iff_ne_bot.2 h) lemma two_inv_lt_one : (2⁻¹:ℝ≥0) < 1 := by simpa [div_def] using half_lt_self zero_ne_one.symm lemma div_lt_iff {a b c : ℝ≥0} (hc : c ≠ 0) : b / c < a ↔ b < a * c := begin rw [← nnreal.coe_lt_coe, ← nnreal.coe_lt_coe, nnreal.coe_div, nnreal.coe_mul], exact div_lt_iff (zero_lt_iff_ne_zero.mpr hc) end lemma div_lt_one_of_lt {a b : ℝ≥0} (h : a < b) : a / b < 1 := begin rwa [div_lt_iff, one_mul], exact ne_of_gt (lt_of_le_of_lt (zero_le _) h) end @[field_simps] theorem div_pow {a b : ℝ≥0} (n : ℕ) : (a / b) ^ n = a ^ n / b ^ n := div_pow _ _ _ @[field_simps] lemma mul_div_assoc' (a b c : ℝ≥0) : a * (b / c) = (a * b) / c := by rw [div_def, div_def, mul_assoc] @[field_simps] lemma div_add_div (a : ℝ≥0) {b : ℝ≥0} (c : ℝ≥0) {d : ℝ≥0} (hb : b ≠ 0) (hd : d ≠ 0) : a / b + c / d = (a * d + b * c) / (b * d) := begin rw ← nnreal.eq_iff, simp only [nnreal.coe_add, nnreal.coe_div, nnreal.coe_mul], exact div_add_div _ _ (coe_ne_zero.2 hb) (coe_ne_zero.2 hd) end @[field_simps] lemma inv_eq_one_div (a : ℝ≥0) : a⁻¹ = 1/a := by rw [div_def, one_mul] @[field_simps] lemma div_mul_eq_mul_div (a b c : ℝ≥0) : (a / b) * c = (a * c) / b := by { rw [div_def, div_def], ac_refl } @[field_simps] lemma add_div' (a b c : ℝ≥0) (hc : c ≠ 0) : b + a / c = (b * c + a) / c := by simpa using div_add_div b a one_ne_zero hc @[field_simps] lemma div_add' (a b c : ℝ≥0) (hc : c ≠ 0) : a / c + b = (a + b * c) / c := by rwa [add_comm, add_div', add_comm] lemma one_div_eq_inv (a : ℝ≥0) : 1 / a = a⁻¹ := one_mul a⁻¹ lemma one_div_div (a b : ℝ≥0) : 1 / (a / b) = b / a := by { rw ← nnreal.eq_iff, simp [one_div_div] } lemma div_eq_mul_one_div (a b : ℝ≥0) : a / b = a * (1 / b) := by rw [div_def, div_def, one_mul] @[field_simps] lemma div_div_eq_mul_div (a b c : ℝ≥0) : a / (b / c) = (a * c) / b := by { rw ← nnreal.eq_iff, simp [div_div_eq_mul_div] } @[field_simps] lemma div_div_eq_div_mul (a b c : ℝ≥0) : (a / b) / c = a / (b * c) := by { rw ← nnreal.eq_iff, simp [div_div_eq_div_mul] } @[field_simps] lemma div_eq_div_iff {a b c d : ℝ≥0} (hb : b ≠ 0) (hd : d ≠ 0) : a / b = c / d ↔ a * d = c * b := div_eq_div_iff hb hd @[field_simps] lemma div_eq_iff {a b c : ℝ≥0} (hb : b ≠ 0) : a / b = c ↔ a = c * b := by simpa using @div_eq_div_iff a b c 1 hb one_ne_zero @[field_simps] lemma eq_div_iff {a b c : ℝ≥0} (hb : b ≠ 0) : c = a / b ↔ c * b = a := by simpa using @div_eq_div_iff c 1 a b one_ne_zero hb end inv section pow theorem pow_eq_zero {a : ℝ≥0} {n : ℕ} (h : a^n = 0) : a = 0 := begin rw ← nnreal.eq_iff, rw [← nnreal.eq_iff, coe_pow] at h, exact pow_eq_zero h end @[field_simps] theorem pow_ne_zero {a : ℝ≥0} (n : ℕ) (h : a ≠ 0) : a ^ n ≠ 0 := mt pow_eq_zero h end pow end nnreal
12c920082ffbc7b4d9e55f1c86189bfe2bf228d4
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Lean/Meta/Tactic/Injection.lean
511e8f1f0b9a2acb3d8e8fda3c0f8929b60272dd
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
4,941
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.AppBuilder import Lean.Meta.MatchUtil import Lean.Meta.Tactic.Clear import Lean.Meta.Tactic.Subst import Lean.Meta.Tactic.Assert import Lean.Meta.Tactic.Intro namespace Lean.Meta def getCtorNumPropFields (ctorInfo : ConstructorVal) : MetaM Nat := do forallTelescopeReducing ctorInfo.type fun xs _ => do let mut numProps := 0 for i in [:ctorInfo.numFields] do if (← isProp (← inferType xs[ctorInfo.numParams + i]!)) then numProps := numProps + 1 return numProps inductive InjectionResultCore where | solved | subgoal (mvarId : MVarId) (numNewEqs : Nat) def injectionCore (mvarId : MVarId) (fvarId : FVarId) : MetaM InjectionResultCore := mvarId.withContext do mvarId.checkNotAssigned `injection let decl ← fvarId.getDecl let type ← whnf decl.type let go (type prf : Expr) : MetaM InjectionResultCore := do match type.eq? with | none => throwTacticEx `injection mvarId "equality expected" | some (_, a, b) => let a ← whnf a let b ← whnf b let target ← mvarId.getType let env ← getEnv match a.isConstructorApp? env, b.isConstructorApp? env with | some aCtor, some bCtor => let val ← mkNoConfusion target prf if aCtor.name != bCtor.name then mvarId.assign val return InjectionResultCore.solved else let valType ← inferType val let valType ← whnf valType match valType with | Expr.forallE _ newTarget _ _ => let newTarget := newTarget.headBeta let tag ← mvarId.getTag let newMVar ← mkFreshExprSyntheticOpaqueMVar newTarget tag mvarId.assign (mkApp val newMVar) let mvarId ← newMVar.mvarId!.tryClear fvarId /- Recall that `noConfusion` does not include equalities for propositions since they are trivial due to proof irrelevance. -/ let numPropFields ← getCtorNumPropFields aCtor return InjectionResultCore.subgoal mvarId (aCtor.numFields - numPropFields) | _ => throwTacticEx `injection mvarId "ill-formed noConfusion auxiliary construction" | _, _ => throwTacticEx `injection mvarId "equality of constructor applications expected" let prf := mkFVar fvarId if let some (α, a, β, b) := type.heq? then if (← isDefEq α β) then go (← mkEq a b) (← mkEqOfHEq prf) else go type prf else go type prf inductive InjectionResult where | solved | subgoal (mvarId : MVarId) (newEqs : Array FVarId) (remainingNames : List Name) def injectionIntro (mvarId : MVarId) (numEqs : Nat) (newNames : List Name) (tryToClear := true) : MetaM InjectionResult := let rec go : Nat → MVarId → Array FVarId → List Name → MetaM InjectionResult | 0, mvarId, fvarIds, remainingNames => return InjectionResult.subgoal mvarId fvarIds remainingNames | n+1, mvarId, fvarIds, name::remainingNames => do let (fvarId, mvarId) ← mvarId.intro name let (fvarId, mvarId) ← heqToEq mvarId fvarId tryToClear go n mvarId (fvarIds.push fvarId) remainingNames | n+1, mvarId, fvarIds, [] => do let (fvarId, mvarId) ← mvarId.intro1 let (fvarId, mvarId) ← heqToEq mvarId fvarId tryToClear go n mvarId (fvarIds.push fvarId) [] go numEqs mvarId #[] newNames def injection (mvarId : MVarId) (fvarId : FVarId) (newNames : List Name := []) : MetaM InjectionResult := do match (← injectionCore mvarId fvarId) with | InjectionResultCore.solved => pure InjectionResult.solved | InjectionResultCore.subgoal mvarId numEqs => injectionIntro mvarId numEqs newNames partial def injections (mvarId : MVarId) (maxDepth : Nat := 5) : MetaM (Option MVarId) := mvarId.withContext do let fvarIds := (← getLCtx).getFVarIds go maxDepth fvarIds.toList mvarId where go : Nat → List FVarId → MVarId → MetaM (Option MVarId) | 0, _, _ => throwTacticEx `injections mvarId "recursion depth exceeded" | _, [], mvarId => return mvarId | d+1, fvarId :: fvarIds, mvarId => do let cont := do go (d+1) fvarIds mvarId if let some (_, lhs, rhs) ← matchEqHEq? (← fvarId.getType) then let lhs ← whnf lhs let rhs ← whnf rhs if lhs.isNatLit && rhs.isNatLit then cont else try match (← injection mvarId fvarId) with | InjectionResult.solved => return none | InjectionResult.subgoal mvarId newEqs _ => mvarId.withContext <| go d (newEqs.toList ++ fvarIds) mvarId catch _ => cont else cont end Lean.Meta
8744de264ecc69ee5df0ab64e87655760632b228
32c054a763e4aa96bcb6e8bc87775e0f403a1804
/src/spec/freevar.lean
864a7b168fca2632b9e0a3f3b502f32fd8c1c9ef
[ "LicenseRef-scancode-generic-cla", "MIT" ]
permissive
Claudiusgonzo/AliveInLean
7fac3f82722c27acc5551260ea12a36519f6e24e
a21bfb90dee0c6c6e00a955b6de92c631198c5ba
refs/heads/master
1,635,381,727,801
1,555,783,536,000
1,555,783,536,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
43,544
lean
-- Copyright (c) Microsoft Corporation. All rights reserved. -- Licensed under the MIT license. import ..smtexpr import ..bitvector import ..irsem import .spec import .lemmas import .irstate import smt2.syntax import system.io import init.meta.tactic import init.meta.interactive namespace spec open irsem def env.added2 (η:freevar.env) (n1 n2:string) (η':freevar.env):= (∀ n, n ∉ η → n ≠ n1 ∧ n ≠ n2 → n ∉ η') ∧ (∀ n, n ∈ η ∨ n = n1 ∨ n = n2 → n ∈ η') def env.has_only (η:freevar.env) (names:list string) := ∀ name, name ∈ names ↔ name ∈ η universes u v @[reducible] def apply {α:Type u} (f:α → α) : option α → option α | (some x) := some (f x) | none := none lemma apply_none: ∀ {α:Type u} (f:α → α) o, apply f o = none ↔ o = none := begin intros, split, { intros H, cases o, refl, unfold apply at H, cases H }, { intros H, rw H } end lemma apply_some: ∀ {α:Type u} {f:α → α} {o} {v} (H:apply f o = some v), ∃ v', o = some v' := begin intros, cases o with v0, { unfold apply at H, cases H }, { unfold apply at H, apply exists.intro v0, refl } end notation η `⟦` s `⟧'` := apply (freevar.env.replace η) s notation η `⟦` s `⟧'` := apply (freevar.env.replace_valty η) s notation η `⟦` s `⟧'` := apply (freevar.env.replace_sbv η) s notation η `⟦` s `⟧'` := apply (freevar.env.replace_sb η) s -- Induction principles for sbool & sbitvec which are mutually defined section @[reducible] parameters (P:sbool → Prop) (P':Π {sz:size}, sbitvec sz → Prop) (Hbtt: P sbool.tt) (Hbff: P sbool.ff) (Hbvar: ∀ s, P (sbool.var s)) (Hband: ∀ b1 b2, P b1 → P b2 → P (sbool.and b1 b2)) (Hbor: ∀ b1 b2, P b1 → P b2 → P (sbool.or b1 b2)) (Hbxor: ∀ b1 b2, P b1 → P b2 → P (sbool.xor b1 b2)) (Hbeqb: ∀ b1 b2, P b1 → P b2 → P (sbool.eqb b1 b2)) (Hbneb: ∀ b1 b2, P b1 → P b2 → P (sbool.neb b1 b2)) (Hbite: ∀ b1 b2 b3, P b1 → P b2 → P b3 → P (sbool.ite b1 b2 b3)) (Hbnot: ∀ b, P b → P (sbool.not b)) (Hbeqbv: ∀ {sz} (v1 v2:sbitvec sz), P' v1 → P' v2 → P (sbool.eqbv v1 v2)) (Hbnebv: ∀ {sz} (v1 v2:sbitvec sz), P' v1 → P' v2 → P (sbool.nebv v1 v2)) (Hbsle: ∀ {sz} (v1 v2:sbitvec sz), P' v1 → P' v2 → P (sbool.sle v1 v2)) (Hbslt: ∀ {sz} (v1 v2:sbitvec sz), P' v1 → P' v2 → P (sbool.slt v1 v2)) (Hbule: ∀ {sz} (v1 v2:sbitvec sz), P' v1 → P' v2 → P (sbool.ule v1 v2)) (Hbult: ∀ {sz} (v1 v2:sbitvec sz), P' v1 → P' v2 → P (sbool.ult v1 v2)) (Hvconst: ∀ sz n, P' (sbitvec.const sz n)) (Hvvar: ∀ sz n, P' (sbitvec.var sz n)) (Hvadd: ∀ {sz} (v1 v2:sbitvec sz), P' v1 → P' v2 → P' (sbitvec.add v1 v2)) (Hvsub: ∀ {sz} (v1 v2:sbitvec sz), P' v1 → P' v2 → P' (sbitvec.sub v1 v2)) (Hvmul: ∀ {sz} (v1 v2:sbitvec sz), P' v1 → P' v2 → P' (sbitvec.mul v1 v2)) (Hvudiv: ∀ {sz} (v1 v2:sbitvec sz), P' v1 → P' v2 → P' (sbitvec.udiv v1 v2)) (Hvurem: ∀ {sz} (v1 v2:sbitvec sz), P' v1 → P' v2 → P' (sbitvec.urem v1 v2)) (Hvsdiv: ∀ {sz} (v1 v2:sbitvec sz), P' v1 → P' v2 → P' (sbitvec.sdiv v1 v2)) (Hvsrem: ∀ {sz} (v1 v2:sbitvec sz), P' v1 → P' v2 → P' (sbitvec.srem v1 v2)) (Hvand: ∀ {sz} (v1 v2:sbitvec sz), P' v1 → P' v2 → P' (sbitvec.and v1 v2)) (Hvor: ∀ {sz} (v1 v2:sbitvec sz), P' v1 → P' v2 → P' (sbitvec.or v1 v2)) (Hvxor: ∀ {sz} (v1 v2:sbitvec sz), P' v1 → P' v2 → P' (sbitvec.xor v1 v2)) (Hvshl: ∀ {sz} (v1 v2:sbitvec sz), P' v1 → P' v2 → P' (sbitvec.shl v1 v2)) (Hvlshr: ∀ {sz} (v1 v2:sbitvec sz), P' v1 → P' v2 → P' (sbitvec.lshr v1 v2)) (Hvashr: ∀ {sz} (v1 v2:sbitvec sz), P' v1 → P' v2 → P' (sbitvec.ashr v1 v2)) (Hvzext: ∀ {sz} (v:sbitvec sz) sz', P' v → P' (sbitvec.zext sz' v)) (Hvsext:∀ {sz} (v:sbitvec sz) sz', P' v → P' (sbitvec.sext sz' v)) (Hvtrunc: ∀ {sz} (v:sbitvec sz) sz', P' v → P' (sbitvec.trunc sz' v)) (Hvextract: ∀ {sz sz':size} (v:sbitvec sz) highbit lowbit (H:sz'.val = highbit - lowbit + 1), P' v → P' (sbitvec.extract highbit lowbit H v)) (Hvite: ∀ {sz} (b:sbool) (v1 v2:sbitvec sz), P b → P' v1 → P' v2 → P' (sbitvec.ite b v1 v2)) @[reducible] mutual def sbool.induction, sbitvec.induction with sbool.induction : ∀ b, P b | sbool.tt := Hbtt | sbool.ff := Hbff | (sbool.var b) := Hbvar b | (sbool.and b1 b2) := Hband b1 b2 (sbool.induction b1) (sbool.induction b2) | (sbool.or b1 b2) := Hbor b1 b2 (sbool.induction b1) (sbool.induction b2) | (sbool.xor b1 b2) := Hbxor b1 b2 (sbool.induction b1) (sbool.induction b2) | (sbool.eqb b1 b2) := Hbeqb b1 b2 (sbool.induction b1) (sbool.induction b2) | (sbool.neb b1 b2) := Hbneb b1 b2 (sbool.induction b1) (sbool.induction b2) | (sbool.ite b1 b2 b3) := Hbite b1 b2 b3 (sbool.induction b1) (sbool.induction b2) (sbool.induction b3) | (sbool.not b) := Hbnot b (sbool.induction b) | (@sbool.eqbv sz v1 v2) := have 0 < sbitvec.sizeof sz v1, by apply sbitvec.decr_sbitvec, have 0 < sbitvec.sizeof sz v2, by apply sbitvec.decr_sbitvec, Hbeqbv v1 v2 (sbitvec.induction v1) (sbitvec.induction v2) | (@sbool.nebv sz v1 v2) := have 0 < sbitvec.sizeof sz v1, by apply sbitvec.decr_sbitvec, have 0 < sbitvec.sizeof sz v2, by apply sbitvec.decr_sbitvec, Hbnebv v1 v2 (sbitvec.induction v1) (sbitvec.induction v2) | (@sbool.sle sz v1 v2) := have 0 < sbitvec.sizeof sz v1, by apply sbitvec.decr_sbitvec, have 0 < sbitvec.sizeof sz v2, by apply sbitvec.decr_sbitvec, Hbsle v1 v2 (sbitvec.induction v1) (sbitvec.induction v2) | (@sbool.slt sz v1 v2) := have 0 < sbitvec.sizeof sz v1, by apply sbitvec.decr_sbitvec, have 0 < sbitvec.sizeof sz v2, by apply sbitvec.decr_sbitvec, Hbslt v1 v2 (sbitvec.induction v1) (sbitvec.induction v2) | (@sbool.ule sz v1 v2) := have 0 < sbitvec.sizeof sz v1, by apply sbitvec.decr_sbitvec, have 0 < sbitvec.sizeof sz v2, by apply sbitvec.decr_sbitvec, Hbule v1 v2 (sbitvec.induction v1) (sbitvec.induction v2) | (@sbool.ult sz v1 v2) := have 0 < sbitvec.sizeof sz v1, by apply sbitvec.decr_sbitvec, have 0 < sbitvec.sizeof sz v2, by apply sbitvec.decr_sbitvec, Hbult v1 v2 (sbitvec.induction v1) (sbitvec.induction v2) with sbitvec.induction: ∀ {sz:size} (v:sbitvec sz), P' v | _ (sbitvec.const sz n) := Hvconst sz n | _ (sbitvec.var sz n) := Hvvar sz n | _ (sbitvec.add v1 v2) := Hvadd v1 v2 (sbitvec.induction v1) (sbitvec.induction v2) | _ (sbitvec.sub v1 v2) := Hvsub v1 v2 (sbitvec.induction v1) (sbitvec.induction v2) | _ (sbitvec.mul v1 v2) := Hvmul v1 v2 (sbitvec.induction v1) (sbitvec.induction v2) | _ (sbitvec.udiv v1 v2) := Hvudiv v1 v2 (sbitvec.induction v1) (sbitvec.induction v2) | _ (sbitvec.urem v1 v2) := Hvurem v1 v2 (sbitvec.induction v1) (sbitvec.induction v2) | _ (sbitvec.sdiv v1 v2) := Hvsdiv v1 v2 (sbitvec.induction v1) (sbitvec.induction v2) | _ (sbitvec.srem v1 v2) := Hvsrem v1 v2 (sbitvec.induction v1) (sbitvec.induction v2) | _ (sbitvec.and v1 v2) := Hvand v1 v2 (sbitvec.induction v1) (sbitvec.induction v2) | _ (sbitvec.or v1 v2) := Hvor v1 v2 (sbitvec.induction v1) (sbitvec.induction v2) | _ (sbitvec.xor v1 v2) := Hvxor v1 v2 (sbitvec.induction v1) (sbitvec.induction v2) | _ (sbitvec.shl v1 v2) := Hvshl v1 v2 (sbitvec.induction v1) (sbitvec.induction v2) | _ (sbitvec.lshr v1 v2) := Hvlshr v1 v2 (sbitvec.induction v1) (sbitvec.induction v2) | _ (sbitvec.ashr v1 v2) := Hvashr v1 v2 (sbitvec.induction v1) (sbitvec.induction v2) | _ (sbitvec.zext sz' v) := Hvzext v sz' (sbitvec.induction v) | _ (sbitvec.sext sz' v) := Hvsext v sz' (sbitvec.induction v) | _ (sbitvec.trunc sz' v) := Hvtrunc v sz' (sbitvec.induction v) | _ (sbitvec.extract h l H v) := Hvextract v h l H (sbitvec.induction v) | _ (sbitvec.ite b v1 v2) := Hvite b v1 v2 (sbool.induction b) (sbitvec.induction v1) (sbitvec.induction v2) end lemma env.replace_sb_and0: ∀ (b1 b2:sbool) (η:freevar.env), η⟦sbool.and b1 b2⟧ = sbool.and (η⟦b1⟧) (η⟦b2⟧) := begin intros, unfold freevar.env.replace_sb end lemma env.replace_sb_and: ∀ (b1 b2:sbool) (η:freevar.env), η⟦b1 & b2⟧ = η⟦b1⟧ & η⟦b2⟧ := begin intros, apply env.replace_sb_and0 end lemma env.replace_sb_or0: ∀ (b1 b2:sbool) (η:freevar.env), η⟦sbool.or b1 b2⟧ = sbool.or (η⟦b1⟧) (η⟦b2⟧) := begin intros, unfold freevar.env.replace_sb end lemma env.replace_sb_or: ∀ (b1 b2:sbool) (η:freevar.env), η⟦b1 |b b2⟧ = η⟦b1⟧ |b η⟦b2⟧ := begin intros, apply env.replace_sb_or0 end lemma env.replace_sb_not0: ∀ (b:sbool) (η:freevar.env), η⟦sbool.not b⟧ = sbool.not (η⟦b⟧) := begin intros, unfold freevar.env.replace_sb end lemma env.replace_sb_not: ∀ (b:sbool) (η:freevar.env), η⟦~ b⟧ = ~ η⟦b⟧ := begin intros, apply env.replace_sb_not0 end lemma env.replace_sb_eqbv: ∀ {sz:size} (a b:sbitvec sz) (η:freevar.env), η⟦sbool.eqbv a b⟧ = sbool.eqbv (η⟦a⟧) (η⟦b⟧) := begin intros, unfold freevar.env.replace_sb end lemma env.replace_sb_nebv: ∀ {sz:size} (a b:sbitvec sz) (η:freevar.env), η⟦sbool.nebv a b⟧ = sbool.nebv (η⟦a⟧) (η⟦b⟧) := begin intros, unfold freevar.env.replace_sb end lemma env.replace_sb_sle: ∀ {sz:size} (a b:sbitvec sz) (η:freevar.env), η⟦sbool.sle a b⟧ = sbool.sle (η⟦a⟧) (η⟦b⟧) := begin intros, unfold freevar.env.replace_sb end lemma env.replace_sb_slt: ∀ {sz:size} (a b:sbitvec sz) (η:freevar.env), η⟦sbool.slt a b⟧ = sbool.slt (η⟦a⟧) (η⟦b⟧) := begin intros, unfold freevar.env.replace_sb end lemma env.replace_sb_ule: ∀ {sz:size} (a b:sbitvec sz) (η:freevar.env), η⟦sbool.ule a b⟧ = sbool.ule (η⟦a⟧) (η⟦b⟧) := begin intros, unfold freevar.env.replace_sb end lemma env.replace_sb_ult: ∀ {sz:size} (a b:sbitvec sz) (η:freevar.env), η⟦sbool.ult a b⟧ = sbool.ult (η⟦a⟧) (η⟦b⟧) := begin intros, unfold freevar.env.replace_sb end lemma env.replace_sb_ite: ∀ (c:sbool) (a b:sbool) (η:freevar.env), η⟦sbool.ite c a b⟧ = sbool.ite (η⟦c⟧) (η⟦a⟧) (η⟦b⟧) := begin intros, unfold freevar.env.replace_sb end lemma env.replace_b2p: ∀ (b:sbool) (η:freevar.env), η⟦b2p irsem_smt b⟧ = b2p irsem_smt (η⟦b⟧) := begin intros, unfold b2p, unfold has_ite.ite, unfold freevar.env.replace_sb, refl end lemma env.replace_sb_of_bool: ∀ (a:bool) (η:freevar.env), η⟦sbool.of_bool a⟧ = sbool.of_bool a := begin intros, cases a; refl end lemma env.replace_eq2p: ∀ {sz:size} (v1:sbitvec sz) (v2:sbitvec sz) (η:freevar.env), η⟦irsem.eq2p irsem_smt v1 v2⟧ = (irsem.eq2p irsem_smt (η⟦v1⟧) (η⟦v2⟧)) := begin intros, unfold eq2p, unfold has_ne.ne, unfold has_comp.ne, have HNE: sbool.nebv (η⟦v1⟧) (η⟦v2⟧) = η⟦sbool.nebv v1 v2⟧, { unfold freevar.env.replace_sb }, rw HNE, apply env.replace_b2p, end lemma env.replace_neq2p: ∀ {sz:size} (v1:sbitvec sz) (v2:sbitvec sz) (η:freevar.env), η⟦irsem.neq2p irsem_smt v1 v2⟧ = (irsem.neq2p irsem_smt (η⟦v1⟧) (η⟦v2⟧)) := begin intros, unfold neq2p, unfold has_eq.eq, unfold has_comp.eq, have HNE: sbool.eqbv (η⟦v1⟧) (η⟦v2⟧) = η⟦sbool.eqbv v1 v2⟧, { unfold freevar.env.replace_sb }, rw HNE, apply env.replace_b2p, end lemma env.replace_eq2p': ∀ {sz:size} (v1:sbitvec sz) (v2:sbitvec sz) (η:freevar.env) (H:v2 = η⟦v2⟧), (irsem.eq2p irsem_smt (η⟦v1⟧) v2) = η⟦irsem.eq2p irsem_smt v1 v2⟧ := begin intros, have H0: irsem.eq2p irsem_smt (η⟦v1⟧) v2 = irsem.eq2p irsem_smt (η⟦v1⟧) (η⟦v2⟧), { rw ← H }, rw H0, rw ← env.replace_eq2p end -- replace_sbv lemma env.replace_sbv_add: ∀ {sz:size} (a b:sbitvec sz) (η:freevar.env), η⟦sbitvec.add a b⟧ = sbitvec.add (η⟦a⟧) (η⟦b⟧) := begin intros, unfold freevar.env.replace_sbv end lemma env.replace_sbv_sub: ∀ {sz:size} (a b:sbitvec sz) (η:freevar.env), η⟦sbitvec.sub a b⟧ = sbitvec.sub (η⟦a⟧) (η⟦b⟧) := begin intros, unfold freevar.env.replace_sbv end lemma env.replace_sbv_mul: ∀ {sz:size} (a b:sbitvec sz) (η:freevar.env), η⟦sbitvec.mul a b⟧ = sbitvec.mul (η⟦a⟧) (η⟦b⟧) := begin intros, unfold freevar.env.replace_sbv end lemma env.replace_sbv_udiv: ∀ {sz:size} (a b:sbitvec sz) (η:freevar.env), η⟦sbitvec.udiv a b⟧ = sbitvec.udiv (η⟦a⟧) (η⟦b⟧) := begin intros, unfold freevar.env.replace_sbv end lemma env.replace_sbv_urem: ∀ {sz:size} (a b:sbitvec sz) (η:freevar.env), η⟦sbitvec.urem a b⟧ = sbitvec.urem (η⟦a⟧) (η⟦b⟧) := begin intros, unfold freevar.env.replace_sbv end lemma env.replace_sbv_sdiv: ∀ {sz:size} (a b:sbitvec sz) (η:freevar.env), η⟦sbitvec.sdiv a b⟧ = sbitvec.sdiv (η⟦a⟧) (η⟦b⟧) := begin intros, unfold freevar.env.replace_sbv end lemma env.replace_sbv_srem: ∀ {sz:size} (a b:sbitvec sz) (η:freevar.env), η⟦sbitvec.srem a b⟧ = sbitvec.srem (η⟦a⟧) (η⟦b⟧) := begin intros, unfold freevar.env.replace_sbv end lemma env.replace_sbv_shl: ∀ {sz:size} (a b:sbitvec sz) (η:freevar.env), η⟦sbitvec.shl a b⟧ = sbitvec.shl (η⟦a⟧) (η⟦b⟧) := begin intros, unfold freevar.env.replace_sbv end lemma env.replace_sbv_lshr: ∀ {sz:size} (a b:sbitvec sz) (η:freevar.env), η⟦sbitvec.lshr a b⟧ = sbitvec.lshr (η⟦a⟧) (η⟦b⟧) := begin intros, unfold freevar.env.replace_sbv end lemma env.replace_sbv_ashr: ∀ {sz:size} (a b:sbitvec sz) (η:freevar.env), η⟦sbitvec.ashr a b⟧ = sbitvec.ashr (η⟦a⟧) (η⟦b⟧) := begin intros, unfold freevar.env.replace_sbv end lemma env.replace_sbv_mk_zext: ∀ {sz sz2:size} (a:sbitvec sz) (η:freevar.env), η⟦sbitvec.mk_zext sz2 a⟧ = sbitvec.mk_zext sz2 (η⟦a⟧) := begin intros, unfold sbitvec.mk_zext, unfold freevar.env.replace_sbv end lemma env.replace_sbv_mk_sext: ∀ {sz sz2:size} (a:sbitvec sz) (η:freevar.env), η⟦sbitvec.mk_sext sz2 a⟧ = sbitvec.mk_sext sz2 (η⟦a⟧) := begin intros, unfold sbitvec.mk_sext, unfold freevar.env.replace_sbv end lemma env.replace_sbv_trunc: ∀ {sz sz':size} (b:sbitvec sz) (η:freevar.env), η⟦sbitvec.trunc sz' b⟧ = sbitvec.trunc sz' (η⟦b⟧) := begin intros, unfold freevar.env.replace_sbv end lemma env.replace_sbv_zext: ∀ {sz sz':size} (b:sbitvec sz) (η:freevar.env), η⟦sbitvec.zext sz' b⟧ = sbitvec.zext sz' (η⟦b⟧) := begin intros, unfold freevar.env.replace_sbv end lemma env.replace_sbv_sext: ∀ {sz sz':size} (b:sbitvec sz) (η:freevar.env), η⟦sbitvec.sext sz' b⟧ = sbitvec.sext sz' (η⟦b⟧) := begin intros, unfold freevar.env.replace_sbv end lemma env.replace_sbv_extract: ∀ {sz sz':size} (b:sbitvec sz) (η:freevar.env) h l (H:sz'.val = h - l + 1), η⟦sbitvec.extract h l H b⟧ = sbitvec.extract h l H (η⟦b⟧) := begin intros, unfold freevar.env.replace_sbv end lemma env.replace_sbv_ite: ∀ {sz:size} (c:sbool) (a b:sbitvec sz) (η:freevar.env), η⟦sbitvec.ite c a b⟧ = sbitvec.ite (η⟦c⟧) (η⟦a⟧) (η⟦b⟧) := begin intros, unfold freevar.env.replace_sbv end lemma env.replace_sbv_of_int: ∀ sz z (η:freevar.env), η⟦sbitvec.of_int sz z⟧ = sbitvec.of_int sz z := begin intros, cases sz, cases z; { unfold sbitvec.of_int, unfold freevar.env.replace_sbv } end lemma env.replace_sbv_of_bool: ∀ (a:sbool) (η:freevar.env), η⟦sbitvec.of_bool a⟧ = sbitvec.of_bool (η⟦a⟧) := begin intros, unfold sbitvec.of_bool, rw env.replace_sbv_ite, unfold sbitvec.zero, unfold sbitvec.one, rw env.replace_sbv_of_int, rw env.replace_sbv_of_int end lemma env.replace_idemp : (∀ (η:freevar.env) (b:sbool), η⟦η⟦b⟧⟧ = η⟦b⟧) ∧ (∀ {sz:size} (η:freevar.env) (b:sbitvec sz), η⟦η⟦b⟧⟧ = η⟦b⟧) := begin split; intros; revert b, apply (sbool.induction (λ b, η⟦η⟦b⟧⟧ = η⟦b⟧) (λ {sz:size} (v:sbitvec sz), η⟦η⟦v⟧⟧ = η⟦v⟧)), any_goals { apply (sbitvec.induction (λ b, η⟦η⟦b⟧⟧ = η⟦b⟧) (λ {sz:size} (v:sbitvec sz), η⟦η⟦v⟧⟧ = η⟦v⟧)) }, any_goals { refl }, -- sbool any_goals { intros b1 b2 H1 H2, unfold freevar.env.replace_sb, rw [H1, H2], done }, any_goals { intros, unfold freevar.env.replace_sb, generalize Hb': η.b s = b', cases b', { unfold freevar.env.replace_sb._match_1, unfold freevar.env.replace_sb, rw Hb', refl }, { unfold freevar.env.replace_sb._match_1, cases b'; refl } }, any_goals { intros b1 b2 b3 H1 H2 H3, unfold freevar.env.replace_sb, rw [H1, H2, H3], done }, any_goals { intros b H, unfold freevar.env.replace_sb, rw H, done }, any_goals { intros sz v1 v2 H1 H2, unfold freevar.env.replace_sb, rw H1, rw H2, done }, -- sbitvec any_goals { intros, unfold freevar.env.replace_sbv, done }, any_goals { intros sz v1 v2 H1 H2, unfold freevar.env.replace_sbv, rw [H1, H2], done }, any_goals { intros, unfold freevar.env.replace_sbv, generalize Hb': η.bv n = b', cases b'; unfold freevar.env.replace_sbv._match_1, { unfold freevar.env.replace_sbv, rw Hb', refl }, { rw env.replace_sbv_of_int }, done }, any_goals { intros sz v' sz' H, unfold freevar.env.replace_sbv, rw H, done }, any_goals { intros sz sz' v highbit lowbit H H', unfold freevar.env.replace_sbv, rw H', done }, any_goals { intros sz b v1 v2 H H1 H2, unfold freevar.env.replace_sbv, rw [H, H1, H2] } end lemma env.replace_sb_idemp: ∀ (η:freevar.env) (b:sbool), η⟦η⟦b⟧⟧ = η⟦b⟧ := begin apply (and.elim_left env.replace_idemp) end lemma env.replace_sbv_idemp: ∀ {sz} (η:freevar.env) (b:sbitvec sz), η⟦η⟦b⟧⟧ = η⟦b⟧ := begin intros, apply (and.elim_right env.replace_idemp) end lemma env.empty_replace: (∀ (b:sbool), freevar.env.empty⟦b⟧ = b) ∧ (∀ {sz} (b:sbitvec sz), freevar.env.empty⟦b⟧ = b) := begin intros, split; intros; revert b, apply (sbool.induction (λ b, freevar.env.empty⟦b⟧ = b) (λ {sz:size} (v:sbitvec sz), freevar.env.empty⟦v⟧ = v)), any_goals { apply (sbitvec.induction (λ b, freevar.env.empty⟦b⟧ = b) (λ {sz:size} (v:sbitvec sz), freevar.env.empty⟦v⟧ = v)), }, any_goals { refl }, any_goals { intros, unfold freevar.env.replace_sb, unfold freevar.env.empty, simp, done }, any_goals { intros b H, unfold freevar.env.replace_sb, rw H }, any_goals { intros b1 b2 H1 H2, unfold freevar.env.replace_sb, rw [H1, H2], done }, any_goals { intros b b1 b2 H H1 H2, unfold freevar.env.replace_sb, rw [H, H1, H2], done }, any_goals { intros sz b1 b2 H1 H2, unfold freevar.env.replace_sb, rw [H1, H2], done }, -- sbitvec any_goals { intros, unfold freevar.env.replace_sbv, done }, any_goals { intros sz v1 v2 H1 H2, unfold freevar.env.replace_sbv, rw [H1, H2], done }, any_goals { intros, unfold freevar.env.replace_sbv, unfold freevar.env.empty, simp, done }, any_goals { intros sz v' sz' H, unfold freevar.env.replace_sbv, rw H, done }, any_goals { intros sz sz' v highbit lowbit H H', unfold freevar.env.replace_sbv, rw H', done }, any_goals { intros sz b v1 v2 H H1 H2, unfold freevar.env.replace_sbv, rw [H, H1, H2] } end lemma env.empty_replace_sb: ∀ (b:sbool), freevar.env.empty⟦b⟧ = b := begin apply (and.elim_left env.empty_replace) end lemma env.empty_replace_sbv {sz:size} : ∀ (b:sbitvec sz), freevar.env.empty⟦b⟧ = b := begin apply (and.elim_right env.empty_replace) end lemma env.replace_sbv_cast: ∀ {sz sz':size} {H:sz = sz'} {H':sbitvec sz = sbitvec sz'} (b:sbitvec sz) (η:freevar.env), η⟦cast H' b⟧ = cast H' (η⟦b⟧) := begin intros, induction H, unfold cast end lemma env.replace_sb_overflowchk_add: ∀ {sz1:size} (sz2:size) (v1 v2:sbitvec sz1) nsw (η:freevar.env), η⟦sbitvec.overflow_chk @sbitvec.add sz2 nsw v1 v2⟧ = sbitvec.overflow_chk @sbitvec.add sz2 nsw (η⟦v1⟧) (η⟦v2⟧) := begin intros, unfold sbitvec.overflow_chk, cases nsw; simp, rw env.replace_sbv_of_int end lemma env.replace_sb_overflowchk_sub: ∀ {sz1:size} (sz2:size) (v1 v2:sbitvec sz1) nsw (η:freevar.env), η⟦sbitvec.overflow_chk @sbitvec.sub sz2 nsw v1 v2⟧ = sbitvec.overflow_chk @sbitvec.sub sz2 nsw (η⟦v1⟧) (η⟦v2⟧) := begin intros, unfold sbitvec.overflow_chk, cases nsw; simp, rw env.replace_sbv_of_int end lemma env.replace_sb_overflowchk_mul: ∀ {sz1:size} (sz2:size) (v1 v2:sbitvec sz1) nsw (η:freevar.env), η⟦sbitvec.overflow_chk @sbitvec.mul sz2 nsw v1 v2⟧ = sbitvec.overflow_chk @sbitvec.mul sz2 nsw (η⟦v1⟧) (η⟦v2⟧) := begin intros, unfold sbitvec.overflow_chk, cases nsw; simp, rw env.replace_sbv_of_int end lemma env.replace_sb_overflowchk_shl: ∀ {sz1:size} (sz2:size) (v1 v2:sbitvec sz1) nsw (η:freevar.env), η⟦sbitvec.shl_overflow_chk sz2 nsw v1 v2⟧ = sbitvec.shl_overflow_chk sz2 nsw (η⟦v1⟧) (η⟦v2⟧) := begin intros, unfold sbitvec.shl_overflow_chk, unfold sbitvec.overflow_chk, cases nsw; simp, { repeat { rw env.replace_sbv_of_int }, tauto }, { rw env.replace_sbv_of_int, tauto } end -- irstate lemma getreg_replace_none: ∀ ss ss' (η:freevar.env) (name name':string) (H:irstate.getreg irsem_smt ss name = none) (H':ss' = η⟦ss⟧), irstate.getreg irsem_smt ss' name = none := begin intros, unfold freevar.env.replace at H', rw H', rw ← irstate.getreg_apply_none_smt, assumption end lemma getreg_replace_none_inv: ∀ ss ss' (η:freevar.env) (name name':string) (H:irstate.getreg irsem_smt ss' name = none) (H':ss' = η⟦ss⟧), irstate.getreg irsem_smt ss name = none := begin intros, unfold freevar.env.replace at H', rw H' at H, rw irstate.getreg_apply_none_smt, assumption end lemma getreg_replace: ∀ {ss:irstate_smt} {name:string} {η:freevar.env} {ov} (HV:irstate.getreg irsem_smt ss name = ov), irstate.getreg irsem_smt (η⟦ss⟧) name = η⟦ov⟧' := begin intros, revert ov, unfold irstate.getreg at *, unfold freevar.env.replace at *, unfold irstate.apply_to_values at *, cases ss with ss_ub ss_rf, apply regfile.induction ss_rf, { intros ov H, unfold irstate.setub, simp at *, rw regfile.empty_apply_empty, rw regfile.empty_get_none at *, rw ← H }, { intros rf Hind n v ov H, unfold irstate.setub, simp at *, rw regfile.apply_update_comm, have HNAME: decidable (n = name), apply_instance, cases HNAME, { rw regfile.update_get_nomatch, apply Hind, { rw regfile.update_get_nomatch at H, apply H, apply neq_symm, assumption }, { apply neq_symm, assumption } }, { rw regfile.update_get_match, rw regfile.update_get_match at H, rw ← H, rw HNAME, rw HNAME } } end lemma replace_updatereg: ∀ (ss:irstate_smt) (name:string) (η:freevar.env) v, η⟦irstate.updatereg irsem_smt ss name v⟧ = irstate.updatereg irsem_smt (η⟦ss⟧) name (η⟦v⟧) := begin intros, unfold irstate.updatereg, unfold freevar.env.replace, unfold irstate.apply_to_values, congr end lemma replace_updateub: ∀ (ss:irstate_smt) (η:freevar.env) ub, η⟦irstate.updateub irsem_smt ss ub⟧ = irstate.updateub irsem_smt (η⟦ss⟧) (η⟦ub⟧) := begin intros, unfold irstate.updateub, unfold freevar.env.replace, unfold irstate.apply_to_values, unfold irstate.setub, unfold irstate.getub, simp, congr, rw env.replace_sb_and end lemma replace_getub: ∀ (ss:irstate_smt) (η:freevar.env), η⟦irstate.getub irsem_smt ss⟧ = irstate.getub irsem_smt (η⟦ss⟧) := begin intros, unfold irstate.getub, cases ss, refl end lemma empty_replace_st: ∀ (ss:irstate_smt), freevar.env.empty⟦ss⟧ = ss := begin intros, unfold freevar.env.replace, cases ss, unfold irstate.apply_to_values, unfold irstate.setub, unfold irstate.getub, simp, rw prod_inj, split, { rw env.empty_replace_sb }, { simp, revert ss_snd, apply regfile.induction, { rw regfile.empty_apply_empty }, { intros rf H, intros, rw regfile.apply_update_comm, congr, assumption, cases v, unfold freevar.env.replace_valty, congr, rw env.empty_replace_sbv, rw env.empty_replace_sb } } end -- ∉ lemma env.not_in_split: ∀ (η:freevar.env) n, n ∉ η ↔ η.b n = none ∧ η.bv n = none := begin intros, split; intros H, { unfold has_mem.mem at H, rw decidable.not_or_iff_and_not at H, cases H, rw ne.def at H_left, rw ne.def at H_right, rw decidable.not_not_iff at H_left, rw decidable.not_not_iff at H_right, split; assumption }, { unfold has_mem.mem, rw decidable.not_or_iff_and_not, cases H, rw ne.def, rw ne.def, rw decidable.not_not_iff, rw decidable.not_not_iff, split; assumption } end lemma env.in_not_in: ∀ n (η:freevar.env), n ∉ η ∨ n ∈ η := begin intros, rw env.not_in_split, unfold has_mem.mem, generalize Hb: η.b n = b', generalize Hb: η.bv n = bv', cases b'; cases bv', { left, split; refl }, { right, right, intros H, cases H }, { right, left, intros H, cases H }, { right, left, intros H, cases H }, end lemma env.not_in_add_bv: ∀ (η:freevar.env) n1 n2 z (HNEQ:n1 ≠ n2) (HNOTIN:n1 ∉ η), n1 ∉ (η.add_bv n2 z) := begin intros, rw env.not_in_split at *, cases HNOTIN, unfold freevar.env.add_bv, split, { simp, assumption }, { simp, rw if_neg; assumption } end lemma env.not_in_add_b: ∀ (η:freevar.env) n1 n2 z (HNEQ:n1 ≠ n2) (HNOTIN:n1 ∉ η), n1 ∉ (η.add_b n2 z) := begin intros, rw env.not_in_split at *, cases HNOTIN, unfold freevar.env.add_b, split, { simp, rw if_neg; assumption }, { simp, assumption } end lemma env.not_in_empty: ∀ n, n ∉ freevar.env.empty := begin intros, rw env.not_in_split, split; refl end -- replace_sb, repalce_sbv lemma env.not_in_replace_sb: ∀ (η:freevar.env) n (HNOTIN:n ∉ η), η⟦sbool.var n⟧ = sbool.var n := begin intros, rw env.not_in_split at *, cases HNOTIN, unfold freevar.env.replace_sb, generalize H:η.b n = g, cases g, { refl }, { rw HNOTIN_left at H, cases H } end lemma env.not_in_replace_sbv: ∀ (η:freevar.env) n sz (HNOTIN:n ∉ η), η⟦sbitvec.var sz n⟧ = sbitvec.var sz n := begin intros, rw env.not_in_split at *, cases HNOTIN, unfold freevar.env.replace_sbv, generalize H:η.bv n = g, cases g, { refl }, { rw HNOTIN_right at H, cases H } end lemma env.in_replace_sbv: ∀ (η:freevar.env) n sz (HIN:∃ z, η.bv n = some z), ∃ z, η⟦sbitvec.var sz n⟧ = sbitvec.const sz z := begin intros, cases HIN with z HIN, unfold freevar.env.replace_sbv, rw HIN, unfold freevar.env.replace_sbv._match_1, cases z; unfold sbitvec.of_int, apply exists.intro, refl, apply exists.intro, refl end lemma env.sbv_var_or_const: ∀ (η:freevar.env) sz n, η⟦sbitvec.var sz n⟧ = sbitvec.var sz n ∨ ∃ z, η⟦sbitvec.var sz n⟧ = sbitvec.const sz z := begin intros, unfold freevar.env.replace_sbv, generalize H: (η.bv n) = b, cases b, { unfold freevar.env.replace_sbv._match_1, left, refl }, { unfold freevar.env.replace_sbv._match_1, right, cases b; unfold sbitvec.of_int, apply exists.intro, refl, apply exists.intro, refl } end lemma env.sb_var_or_const: ∀ (η:freevar.env) n, η⟦sbool.var n⟧ = sbool.var n ∨ η⟦sbool.var n⟧ = sbool.tt ∨ η⟦sbool.var n⟧ = sbool.ff := begin intros, unfold freevar.env.replace_sb, generalize H: (η.b n) = b, cases b, { unfold freevar.env.replace_sb._match_1, left, refl }, { unfold freevar.env.replace_sb._match_1, right, cases b, right, refl, left, refl } end lemma env.not_in_add_bv_replace: ∀ (η:freevar.env) n n2 sz z (HNEQ:n ≠ n2), (η.add_bv n2 z)⟦sbitvec.var sz n⟧ = η⟦sbitvec.var sz n⟧ := begin intros, unfold freevar.env.replace_sbv, unfold freevar.env.add_bv, simp, rw if_neg, assumption end lemma env.not_in_add_b_replace: ∀ (η:freevar.env) n n2 z (HNEQ:n ≠ n2), (η.add_b n2 z)⟦sbool.var n⟧ = η⟦sbool.var n⟧ := begin intros, unfold freevar.env.replace_sb, unfold freevar.env.add_b, simp, rw if_neg, assumption end lemma env.add_b_replace_match: ∀ (η:freevar.env) n b, (freevar.env.add_b η n b)⟦sbool.var n⟧ = sbool.of_bool b := begin intros, unfold freevar.env.replace_sb, unfold freevar.env.add_b, simp end lemma env.add_bv_replace_match: ∀ (η:freevar.env) n z sz, (freevar.env.add_bv η n z)⟦sbitvec.var sz n⟧ = sbitvec.of_int sz z := begin intros, unfold freevar.env.replace_sbv, unfold freevar.env.add_bv, simp end lemma env.add_bv_replace_sb: ∀ (η:freevar.env) n n' z, (freevar.env.add_bv η n z)⟦sbool.var n'⟧ = η⟦sbool.var n'⟧ := begin intros, unfold freevar.env.replace_sb, unfold freevar.env.add_bv end lemma env.add_b_replace_sbv: ∀ (η:freevar.env) n n' b sz, (freevar.env.add_b η n b)⟦sbitvec.var sz n'⟧ = η⟦sbitvec.var sz n'⟧ := begin intros, unfold freevar.env.replace_sbv, unfold freevar.env.add_b end lemma env.replace_sb_cases: ∀ s (η:freevar.env), η⟦sbool.var s⟧ = sbool.var s ∨ η⟦sbool.var s⟧ = sbool.tt ∨ η⟦sbool.var s⟧ = sbool.ff := begin intros, unfold freevar.env.replace_sb, generalize Hb: η.b s = b', rw Hb at *, cases b'; unfold freevar.env.replace_sb._match_1, { left, refl }, { right, cases b'; unfold sbool.of_bool; simp } end lemma env.replace_sbv_cases: ∀ sz s (η:freevar.env), η⟦sbitvec.var sz s⟧ = sbitvec.var sz s ∨ ∃ n, η⟦sbitvec.var sz s⟧ = sbitvec.const sz n := begin intros, unfold freevar.env.replace_sbv, generalize Hb: η.bv s = b', rw Hb at *, cases b'; unfold freevar.env.replace_sbv._match_1, { left, refl }, { right, cases b'; unfold sbitvec.of_int, apply exists.intro, refl, apply exists.intro, refl } end lemma env.not_in_add_b_bv_bv_comm: ∀ (η:freevar.env) z, (∀ (v:sbool) n, n ∉ η → (η.add_bv n z)⟦v⟧ = (freevar.env.empty.add_bv n z)⟦η⟦v⟧⟧) ∧ (∀ {sz} (v:sbitvec sz) n, n ∉ η → (η.add_bv n z)⟦v⟧ = (freevar.env.empty.add_bv n z)⟦η⟦v⟧⟧) := begin intros, split, apply sbool.induction (λ v, ∀ n, n ∉ η → (η.add_bv n z)⟦v⟧ = (freevar.env.empty.add_bv n z)⟦η⟦v⟧⟧) (λ {sz} (v:sbitvec sz), ∀ n, n ∉ η → (η.add_bv n z)⟦v⟧ = (freevar.env.empty.add_bv n z)⟦η⟦v⟧⟧), any_goals { apply sbitvec.induction (λ v, ∀ n, n ∉ η → (η.add_bv n z)⟦v⟧ = (freevar.env.empty.add_bv n z)⟦η⟦v⟧⟧) (λ {sz} (v:sbitvec sz), ∀ n, n ∉ η → (η.add_bv n z)⟦v⟧ = (freevar.env.empty.add_bv n z)⟦η⟦v⟧⟧), }, any_goals { intros, refl }, any_goals { intros, unfold freevar.env.add_bv, unfold freevar.env.replace_sb, simp, unfold freevar.env.empty, generalize HG: η.b s = b', cases b', { unfold freevar.env.replace_sb._match_1, unfold freevar.env.replace_sb, }, { unfold freevar.env.replace_sb._match_1, rw env.replace_sb_of_bool, done } }, any_goals { intros b1 b2 H1 H2 n' HNOTIN', unfold freevar.env.replace_sb at *, rw [H1, H2]; assumption }, any_goals { intros b1 b2 b3 H1 H2 H3 n' HNOTIN', unfold freevar.env.replace_sb at *, rw [H1, H2, H3]; assumption }, any_goals { intros b H n' HNOTIN', unfold freevar.env.replace_sb at *, rw [H], assumption }, any_goals { intros sz b1 b2 H1 H2 n' HNOTIN', unfold freevar.env.replace_sb at *, rw [H1, H2]; assumption }, any_goals { intros, unfold freevar.env.replace_sbv at *, done }, any_goals { intros sz n', intros n'' HNOTIN', have HEQ:decidable (n' = n''), apply_instance, cases HEQ, { have H:= env.sbv_var_or_const η sz n', cases H, { rw env.not_in_add_bv_replace, rw H, rw env.not_in_add_bv_replace, rw env.empty_replace_sbv, assumption, assumption }, { cases H with z' H, rw env.not_in_add_bv_replace, rw H, unfold freevar.env.replace_sbv, assumption } }, { rw HEQ, rw env.add_bv_replace_match, rw env.not_in_replace_sbv, rw env.add_bv_replace_match, assumption } }, any_goals { intros sz v1 v2 H1 H2 n' HNOTIN', unfold freevar.env.replace_sbv at *, rw [H1, H2]; assumption }, any_goals { intros sz v sz' H n' HNOTIN', unfold freevar.env.replace_sbv at *, rw H, assumption }, any_goals { intros sz sz' v highbit lowbit HH H n' HNOTIN', unfold freevar.env.replace_sbv at *, rw H, assumption }, any_goals { intros sz v1 v2 v3 H1 H2 H3 n' HNOTIN', unfold freevar.env.replace_sbv at *, rw [H1, H2, H3]; assumption } end lemma env.not_in_add_bv_b_comm: ∀ (η:freevar.env) z (v:sbool) n, n ∉ η → (η.add_bv n z)⟦v⟧ = (freevar.env.empty.add_bv n z)⟦η⟦v⟧⟧ := begin intros, apply (and.elim_left (env.not_in_add_b_bv_bv_comm η z)), assumption end lemma env.not_in_add_bv_bv_comm: ∀ (η:freevar.env) z {sz} (v:sbitvec sz) n, n ∉ η → (η.add_bv n z)⟦v⟧ = (freevar.env.empty.add_bv n z)⟦η⟦v⟧⟧ := begin intros, apply (and.elim_right (env.not_in_add_b_bv_bv_comm η z)), assumption end lemma env.not_in_add_b_bv_b_comm: ∀ (η:freevar.env) z, (∀ (v:sbool) n, n ∉ η → (η.add_b n z)⟦v⟧ = (freevar.env.empty.add_b n z)⟦η⟦v⟧⟧) ∧ (∀ {sz} (v:sbitvec sz) n, n ∉ η → (η.add_b n z)⟦v⟧ = (freevar.env.empty.add_b n z)⟦η⟦v⟧⟧) := begin intros, split, apply sbool.induction (λ v, ∀ n, n ∉ η → (η.add_b n z)⟦v⟧ = (freevar.env.empty.add_b n z)⟦η⟦v⟧⟧) (λ {sz} (v:sbitvec sz), ∀ n, n ∉ η → (η.add_b n z)⟦v⟧ = (freevar.env.empty.add_b n z)⟦η⟦v⟧⟧), any_goals { apply sbitvec.induction (λ v, ∀ n, n ∉ η → (η.add_b n z)⟦v⟧ = (freevar.env.empty.add_b n z)⟦η⟦v⟧⟧) (λ {sz} (v:sbitvec sz), ∀ n, n ∉ η → (η.add_b n z)⟦v⟧ = (freevar.env.empty.add_b n z)⟦η⟦v⟧⟧), }, any_goals { intros, refl }, any_goals { intros n', intros n'' HNOTIN', have HEQ:decidable (n' = n''), apply_instance, cases HEQ, { have H:= env.sb_var_or_const η n', cases H, { rw env.not_in_add_b_replace, rw H, rw env.not_in_add_b_replace, rw env.empty_replace_sb, assumption, assumption }, { cases H, any_goals {rw env.not_in_add_b_replace, rw H, unfold freevar.env.replace_sb, assumption } } }, { rw HEQ, rw env.add_b_replace_match, rw env.not_in_replace_sb, rw env.add_b_replace_match, assumption } }, any_goals { intros b1 b2 H1 H2 n' HNOTIN', unfold freevar.env.replace_sb at *, rw [H1, H2]; assumption }, any_goals { intros b1 b2 b3 H1 H2 H3 n' HNOTIN', unfold freevar.env.replace_sb at *, rw [H1, H2, H3]; assumption }, any_goals { intros b H n' HNOTIN', unfold freevar.env.replace_sb at *, rw [H], assumption }, any_goals { intros sz b1 b2 H1 H2 n' HNOTIN', unfold freevar.env.replace_sb at *, rw [H1, H2]; assumption }, any_goals { intros, unfold freevar.env.replace_sbv at *, done }, any_goals { intros, unfold freevar.env.add_b, unfold freevar.env.replace_sbv, simp, unfold freevar.env.empty, generalize HG: η.bv n = b', cases b', { unfold freevar.env.replace_sbv._match_1, unfold freevar.env.replace_sbv, }, { unfold freevar.env.replace_sbv._match_1, simp, rw env.replace_sbv_of_int, done } }, any_goals { intros sz v1 v2 H1 H2 n' HNOTIN', unfold freevar.env.replace_sbv at *, rw [H1, H2]; assumption }, any_goals { intros sz v sz' H n' HNOTIN', unfold freevar.env.replace_sbv at *, rw H, assumption }, any_goals { intros sz sz' v highbit lowbit HH H n' HNOTIN', unfold freevar.env.replace_sbv at *, rw H, assumption }, any_goals { intros sz v1 v2 v3 H1 H2 H3 n' HNOTIN', unfold freevar.env.replace_sbv at *, rw [H1, H2, H3]; assumption } end lemma env.not_in_add_b_b_comm: ∀ (η:freevar.env) z (v:sbool) n, n ∉ η → (η.add_b n z)⟦v⟧ = (freevar.env.empty.add_b n z)⟦η⟦v⟧⟧ := begin intros, apply (and.elim_left (env.not_in_add_b_bv_b_comm η z)), assumption end lemma env.not_in_add_b_bv_comm: ∀ (η:freevar.env) z {sz} (v:sbitvec sz) n, n ∉ η → (η.add_b n z)⟦v⟧ = (freevar.env.empty.add_b n z)⟦η⟦v⟧⟧ := begin intros, apply (and.elim_right (env.not_in_add_b_bv_b_comm η z)), assumption end lemma env.not_in_add_bv_valty_comm: ∀ (η:freevar.env) (vv:valty_smt) n z, n ∉ η → (η.add_bv n z)⟦vv⟧ = (freevar.env.empty.add_bv n z)⟦η⟦vv⟧⟧ := begin intros, cases vv with sz iv pv, unfold freevar.env.replace_valty, simp, rw env.not_in_add_bv_bv_comm, rw env.not_in_add_bv_b_comm, split, refl, refl, assumption, assumption end lemma env.not_in_add_b_valty_comm: ∀ (η:freevar.env) (vv:valty_smt) n z, n ∉ η → (η.add_b n z)⟦vv⟧ = (freevar.env.empty.add_b n z)⟦η⟦vv⟧⟧ := begin intros, cases vv with sz iv pv, unfold freevar.env.replace_valty, simp, rw env.not_in_add_b_bv_comm, rw env.not_in_add_b_b_comm, split, refl, refl, assumption, assumption end lemma env.not_in_add_bv_irstate_comm: ∀ (η:freevar.env) (ss:irstate irsem_smt) n z, n ∉ η → (η.add_bv n z)⟦ss⟧ = (freevar.env.empty.add_bv n z)⟦η⟦ss⟧⟧ := begin intros, cases ss, unfold freevar.env.replace, rw ← irstate.setub_apply_to_values, rw ← irstate.setub_apply_to_values, rw ← irstate.setub_apply_to_values, rw ← irstate.setub_apply_to_values, unfold irstate.getub, unfold irstate.setub, unfold irstate.apply_to_values, unfold regfile.apply_to_values, simp, rw prod_inj, simp, split, { rw env.not_in_add_bv_b_comm, assumption }, { induction ss_snd, { refl }, { simp, rw ss_snd_ih, cases ss_snd_hd with rn v, cases v with sz iv pv, simp, rw env.not_in_add_bv_bv_comm, rw env.not_in_add_bv_b_comm, assumption, assumption } } end lemma env.not_in_add_b_irstate_comm: ∀ (η:freevar.env) (ss:irstate irsem_smt) n z, n ∉ η → (η.add_b n z)⟦ss⟧ = (freevar.env.empty.add_b n z)⟦η⟦ss⟧⟧ := begin intros, cases ss, unfold freevar.env.replace, rw ← irstate.setub_apply_to_values, rw ← irstate.setub_apply_to_values, rw ← irstate.setub_apply_to_values, rw ← irstate.setub_apply_to_values, unfold irstate.getub, unfold irstate.setub, unfold irstate.apply_to_values, unfold regfile.apply_to_values, simp, rw prod_inj, simp, split, { rw env.not_in_add_b_b_comm, assumption }, { induction ss_snd, { refl }, { simp, rw ss_snd_ih, cases ss_snd_hd with rn v, cases v with sz iv pv, simp, rw env.not_in_add_b_bv_comm, rw env.not_in_add_b_b_comm, assumption, assumption } } end lemma env.has_only_shuffle: ∀ {η:freevar.env} (s:string) (l1 l2:list string), env.has_only η (s::(l1 ++ l2)) ↔ env.has_only η (l1 ++ s::l2) := begin intros, rw ← list.cons_append, unfold env.has_only, split, { intros, simp, simp at a, rw or.comm, rw or.assoc, rw @or.comm (name ∈ l2) (name ∈ l1), apply a }, { intros, simp, simp at a, rw or.comm, rw or.assoc, rw @or.comm (name ∈ l2) (name = s), apply a } end lemma env.has_only_shuffle2: ∀ {η:freevar.env} (s1 s2:string) (l:list string), env.has_only η (s1::s2::l) ↔ env.has_only η (s2::s1::l) := begin intros, unfold env.has_only, split, { intros, simp, simp at a, rw or.comm, rw or.assoc, rw @or.comm (name ∈ l) (name = s2), apply a }, { intros, simp, simp at a, rw or.comm, rw or.assoc, rw @or.comm (name ∈ l) (name = s1), apply a } end lemma env.has_only_added2: ∀ {η η':freevar.env} {n1 n2:string} {l:list string} (H1: env.has_only η l) (H2: env.added2 η n1 n2 η'), env.has_only η' (n1::n2::l) := begin intros, unfold env.added2 at H2, cases H2, unfold env.has_only at *, intros, simp, rw H1, split, { intros, apply H2_right, rw or.comm, rw or.assoc, assumption }, { intros, rw ← decidable.not_not_iff (name ∈ η') at a, rw ← decidable.not_not_iff (name = n1 ∨ name = n2 ∨ name ∈ η), intros H0, rw decidable.not_or_iff_and_not at H0, rw decidable.not_or_iff_and_not at H0, cases H0, cases H0_right, apply a, apply H2_left, assumption, split; assumption } end lemma env.has_only_not_in: ∀ {η:freevar.env} {l n} (H:env.has_only η l) (HNOTIN:n ∉ l), n ∉ η := begin intros, unfold env.has_only at H, intros H0, apply HNOTIN, rw H, assumption end -- get_value lemma get_value_replace: ∀ (ss:irstate_smt) (op:operand) (η:freevar.env) (t:ty), get_value irsem_smt (η⟦ss⟧) op t = η⟦get_value irsem_smt ss op t⟧' := begin intros, cases op, { -- operand.reg cases op, unfold get_value, apply getreg_replace, refl }, { cases op, cases t, { unfold get_value, have H0: decidable (0 < t), apply_instance, cases H0, { rw dif_neg, assumption }, { rw dif_pos, simp, have H1: decidable (within_signed_range ⟨t, H0⟩ op), apply_instance, cases H1, { rw if_neg, { have H2: decidable (↑(int_min_nat ⟨t, H0⟩) ≤ op ∧ op ≤ ↑(all_one_nat ⟨t, H0⟩)), apply_instance, cases H2, { rw if_neg, apply H2 }, { rw if_pos, unfold uint_like.from_z, unfold apply, unfold freevar.env.replace_valty, generalize HCONST: op + -↑(2^t) = c, cases c, { unfold sbitvec.of_int, unfold freevar.env.replace_sbv, refl }, { unfold sbitvec.of_int, unfold freevar.env.replace_sbv, refl }, assumption } }, { assumption } }, { rw if_pos, unfold uint_like.from_z, unfold apply, unfold freevar.env.replace_valty, cases op, { unfold sbitvec.of_int, unfold freevar.env.replace_sbv, refl }, { unfold sbitvec.of_int, unfold freevar.env.replace_sbv, refl }, assumption } } }, { unfold get_value } } end end spec
0265bb95a80e2d7145fd57e8550fc108131d77af
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/obtain_tac.lean
fff5fd55b14a5d9f6fb4c724d9b1a72851dd8c93
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
132
lean
example (a b : Prop) : a ∧ b → b ∧ a := begin intro Hab, obtain Ha Hb, from Hab, show b ∧ a, from and.intro Hb Ha end
7f891f63b9ff88356358b9b13b451a4094f8864a
94e33a31faa76775069b071adea97e86e218a8ee
/src/analysis/hofer.lean
f428bb649b91311f8ec604455ac5fba6a92488a1
[ "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
4,602
lean
/- Copyright (c) 2020 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import analysis.specific_limits.basic /-! # Hofer's lemma This is an elementary lemma about complete metric spaces. It is motivated by an application to the bubbling-off analysis for holomorphic curves in symplectic topology. We are *very* far away from having these applications, but the proof here is a nice example of a proof needing to construct a sequence by induction in the middle of the proof. ## References: * H. Hofer and C. Viterbo, *The Weinstein conjecture in the presence of holomorphic spheres* -/ open_locale classical topological_space big_operators open filter finset local notation `d` := dist lemma hofer {X: Type*} [metric_space X] [complete_space X] (x : X) (ε : ℝ) (ε_pos : 0 < ε) {ϕ : X → ℝ} (cont : continuous ϕ) (nonneg : ∀ y, 0 ≤ ϕ y) : ∃ (ε' > 0) (x' : X), ε' ≤ ε ∧ d x' x ≤ 2*ε ∧ ε * ϕ(x) ≤ ε' * ϕ x' ∧ ∀ y, d x' y ≤ ε' → ϕ y ≤ 2*ϕ x' := begin by_contradiction H, have reformulation : ∀ x' (k : ℕ), ε * ϕ x ≤ ε / 2 ^ k * ϕ x' ↔ 2^k * ϕ x ≤ ϕ x', { intros x' k, rw [div_mul_eq_mul_div, le_div_iff, mul_assoc, mul_le_mul_left ε_pos, mul_comm], exact pow_pos (by norm_num) k, }, -- Now let's specialize to `ε/2^k` replace H : ∀ k : ℕ, ∀ x', d x' x ≤ 2 * ε ∧ 2^k * ϕ x ≤ ϕ x' → ∃ y, d x' y ≤ ε/2^k ∧ 2 * ϕ x' < ϕ y, { intros k x', push_neg at H, simpa [reformulation] using H (ε/2^k) (by simp [ε_pos, zero_lt_two]) x' (by simp [ε_pos, zero_lt_two, one_le_two]) }, clear reformulation, haveI : nonempty X := ⟨x⟩, choose! F hF using H, -- Use the axiom of choice -- Now define u by induction starting at x, with u_{n+1} = F(n, u_n) let u : ℕ → X := λ n, nat.rec_on n x F, have hu0 : u 0 = x := rfl, -- The properties of F translate to properties of u have hu : ∀ n, d (u n) x ≤ 2 * ε ∧ 2^n * ϕ x ≤ ϕ (u n) → d (u n) (u $ n + 1) ≤ ε / 2 ^ n ∧ 2 * ϕ (u n) < ϕ (u $ n + 1), { intro n, exact hF n (u n) }, clear hF, -- Key properties of u, to be proven by induction have key : ∀ n, d (u n) (u (n + 1)) ≤ ε / 2 ^ n ∧ 2 * ϕ (u n) < ϕ (u (n + 1)), { intro n, induction n using nat.case_strong_induction_on with n IH, { specialize hu 0, simpa [hu0, mul_nonneg_iff, zero_le_one, ε_pos.le, le_refl] using hu }, have A : d (u (n+1)) x ≤ 2 * ε, { rw [dist_comm], let r := range (n+1), -- range (n+1) = {0, ..., n} calc d (u 0) (u (n + 1)) ≤ ∑ i in r, d (u i) (u $ i+1) : dist_le_range_sum_dist u (n + 1) ... ≤ ∑ i in r, ε/2^i : sum_le_sum (λ i i_in, (IH i $ nat.lt_succ_iff.mp $ finset.mem_range.mp i_in).1) ... = ∑ i in r, (1/2)^i*ε : by { congr' with i, field_simp } ... = (∑ i in r, (1/2)^i)*ε : finset.sum_mul.symm ... ≤ 2*ε : mul_le_mul_of_nonneg_right (sum_geometric_two_le _) (le_of_lt ε_pos), }, have B : 2^(n+1) * ϕ x ≤ ϕ (u (n + 1)), { refine @geom_le (ϕ ∘ u) _ zero_le_two (n + 1) (λ m hm, _), exact (IH _ $ nat.lt_add_one_iff.1 hm).2.le }, exact hu (n+1) ⟨A, B⟩, }, cases forall_and_distrib.mp key with key₁ key₂, clear hu key, -- Hence u is Cauchy have cauchy_u : cauchy_seq u, { refine cauchy_seq_of_le_geometric _ ε one_half_lt_one (λ n, _), simpa only [one_div, inv_pow] using key₁ n }, -- So u converges to some y obtain ⟨y, limy⟩ : ∃ y, tendsto u at_top (𝓝 y), from complete_space.complete cauchy_u, -- And ϕ ∘ u goes to +∞ have lim_top : tendsto (ϕ ∘ u) at_top at_top, { let v := λ n, (ϕ ∘ u) (n+1), suffices : tendsto v at_top at_top, by rwa tendsto_add_at_top_iff_nat at this, have hv₀ : 0 < v 0, { have : 0 ≤ ϕ (u 0) := nonneg x, calc 0 ≤ 2 * ϕ (u 0) : by linarith ... < ϕ (u (0 + 1)) : key₂ 0 }, apply tendsto_at_top_of_geom_le hv₀ one_lt_two, exact λ n, (key₂ (n+1)).le }, -- But ϕ ∘ u also needs to go to ϕ(y) have lim : tendsto (ϕ ∘ u) at_top (𝓝 (ϕ y)), from tendsto.comp cont.continuous_at limy, -- So we have our contradiction! exact not_tendsto_at_top_of_tendsto_nhds lim lim_top, end
bd8fefec396066963e8d196aab28fa7905683f13
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/ring_theory/polynomial/symmetric.lean
b39b8900e6982c85bda17c85d37d67f7eda5ac09
[ "Apache-2.0" ]
permissive
jumpy4/mathlib
d3829e75173012833e9f15ac16e481e17596de0f
af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13
refs/heads/master
1,693,508,842,818
1,636,203,271,000
1,636,203,271,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,172
lean
/- Copyright (c) 2020 Hanting Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Hanting Zhang, Johan Commelin -/ import data.fintype.card import data.mv_polynomial.rename import data.mv_polynomial.comm_ring import algebra.algebra.subalgebra /-! # Symmetric Polynomials and Elementary Symmetric Polynomials This file defines symmetric `mv_polynomial`s and elementary symmetric `mv_polynomial`s. We also prove some basic facts about them. ## Main declarations * `mv_polynomial.is_symmetric` * `mv_polynomial.symmetric_subalgebra` * `mv_polynomial.esymm` ## Notation + `esymm σ R n`, is the `n`th elementary symmetric polynomial in `mv_polynomial σ R`. 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) + `r : R` elements of the coefficient ring + `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians + `φ ψ : mv_polynomial σ R` -/ open equiv (perm) open_locale big_operators noncomputable theory namespace mv_polynomial variables {σ : Type*} {R : Type*} variables {τ : Type*} {S : Type*} /-- A `mv_polynomial φ` is symmetric if it is invariant under permutations of its variables by the `rename` operation -/ def is_symmetric [comm_semiring R] (φ : mv_polynomial σ R) : Prop := ∀ e : perm σ, rename e φ = φ variables (σ R) /-- The subalgebra of symmetric `mv_polynomial`s. -/ def symmetric_subalgebra [comm_semiring R] : subalgebra R (mv_polynomial σ R) := { carrier := set_of is_symmetric, algebra_map_mem' := λ r e, rename_C e r, mul_mem' := λ a b ha hb e, by rw [alg_hom.map_mul, ha, hb], add_mem' := λ a b ha hb e, by rw [alg_hom.map_add, ha, hb] } variables {σ R} @[simp] lemma mem_symmetric_subalgebra [comm_semiring R] (p : mv_polynomial σ R) : p ∈ symmetric_subalgebra σ R ↔ p.is_symmetric := iff.rfl namespace is_symmetric section comm_semiring variables [comm_semiring R] [comm_semiring S] {φ ψ : mv_polynomial σ R} @[simp] lemma C (r : R) : is_symmetric (C r : mv_polynomial σ R) := (symmetric_subalgebra σ R).algebra_map_mem r @[simp] lemma zero : is_symmetric (0 : mv_polynomial σ R) := (symmetric_subalgebra σ R).zero_mem @[simp] lemma one : is_symmetric (1 : mv_polynomial σ R) := (symmetric_subalgebra σ R).one_mem lemma add (hφ : is_symmetric φ) (hψ : is_symmetric ψ) : is_symmetric (φ + ψ) := (symmetric_subalgebra σ R).add_mem hφ hψ lemma mul (hφ : is_symmetric φ) (hψ : is_symmetric ψ) : is_symmetric (φ * ψ) := (symmetric_subalgebra σ R).mul_mem hφ hψ lemma smul (r : R) (hφ : is_symmetric φ) : is_symmetric (r • φ) := (symmetric_subalgebra σ R).smul_mem hφ r @[simp] lemma map (hφ : is_symmetric φ) (f : R →+* S) : is_symmetric (map f φ) := λ e, by rw [← map_rename, hφ] end comm_semiring section comm_ring variables [comm_ring R] {φ ψ : mv_polynomial σ R} lemma neg (hφ : is_symmetric φ) : is_symmetric (-φ) := (symmetric_subalgebra σ R).neg_mem hφ lemma sub (hφ : is_symmetric φ) (hψ : is_symmetric ψ) : is_symmetric (φ - ψ) := (symmetric_subalgebra σ R).sub_mem hφ hψ end comm_ring end is_symmetric section elementary_symmetric open finset variables (σ R) [comm_semiring R] [comm_semiring S] [fintype σ] [fintype τ] /-- The `n`th elementary symmetric `mv_polynomial σ R`. -/ def esymm (n : ℕ) : mv_polynomial σ R := ∑ t in powerset_len n univ, ∏ i in t, X i /-- We can define `esymm σ R n` by summing over a subtype instead of over `powerset_len`. -/ lemma esymm_eq_sum_subtype (n : ℕ) : esymm σ R n = ∑ t : {s : finset σ // s.card = n}, ∏ i in (t : finset σ), X i := begin rw esymm, let i : Π (a : finset σ), a ∈ powerset_len n univ → {s : finset σ // s.card = n} := λ a ha, ⟨_, (mem_powerset_len.mp ha).2⟩, refine sum_bij i (λ a ha, mem_univ (i a ha)) _ (λ _ _ _ _ hi, subtype.ext_iff_val.mp hi) _, { intros, apply prod_congr, simp only [subtype.coe_mk], intros, refl,}, { refine (λ b H, ⟨b.val, mem_powerset_len.mpr ⟨subset_univ b.val, b.property⟩, _⟩), simp [i] }, end /-- We can define `esymm σ R n` as a sum over explicit monomials -/ lemma esymm_eq_sum_monomial (n : ℕ) : esymm σ R n = ∑ t in powerset_len n univ, monomial (∑ i in t, finsupp.single i 1) 1 := begin refine sum_congr rfl (λ x hx, _), rw monic_monomial_eq, rw finsupp.prod_pow, rw ← prod_subset (λ y _, finset.mem_univ y : x ⊆ univ) (λ y _ hy, _), { refine prod_congr rfl (λ x' hx', _), convert (pow_one _).symm, convert (finsupp.apply_add_hom x' : (σ →₀ ℕ) →+ ℕ).map_sum _ x, classical, simp [finsupp.single_apply, finset.filter_eq', apply_ite, apply_ite finset.card], rw if_pos hx', }, { convert pow_zero _, convert (finsupp.apply_add_hom y : (σ →₀ ℕ) →+ ℕ).map_sum _ x, classical, simp [finsupp.single_apply, finset.filter_eq', apply_ite, apply_ite finset.card], rw if_neg hy, } end @[simp] lemma esymm_zero : esymm σ R 0 = 1 := by simp only [esymm, powerset_len_zero, sum_singleton, prod_empty] lemma map_esymm (n : ℕ) (f : R →+* S) : map f (esymm σ R n) = esymm σ S n := begin rw [esymm, (map f).map_sum], refine sum_congr rfl (λ x hx, _), rw (map f).map_prod, simp, end lemma rename_esymm (n : ℕ) (e : σ ≃ τ) : rename e (esymm σ R n) = esymm τ R n := begin rw [esymm_eq_sum_subtype, esymm_eq_sum_subtype, (rename ⇑e).map_sum], let e' : {s : finset σ // s.card = n} ≃ {s : finset τ // s.card = n} := equiv.subtype_equiv (equiv.finset_congr e) (by simp), rw ← equiv.sum_comp e'.symm, apply fintype.sum_congr, intro, calc _ = (∏ i in (e'.symm a : finset σ), (rename e) (X i)) : (rename e).map_prod _ _ ... = (∏ i in (a : finset τ), (rename e) (X (e.symm i))) : prod_map (a : finset τ) _ _ ... = _ : _, apply finset.prod_congr rfl, intros, simp, end lemma esymm_is_symmetric (n : ℕ) : is_symmetric (esymm σ R n) := by { intro, rw rename_esymm } lemma support_esymm'' (n : ℕ) [decidable_eq σ] [nontrivial R] : (esymm σ R n).support = (powerset_len n (univ : finset σ)).bUnion (λ t, (finsupp.single (∑ (i : σ) in t, finsupp.single i 1) (1:R)).support) := begin rw esymm_eq_sum_monomial, simp only [← single_eq_monomial], convert finsupp.support_sum_eq_bUnion (powerset_len n (univ : finset σ)) _, intros s t hst d, simp only [finsupp.support_single_ne_zero one_ne_zero, and_imp, inf_eq_inter, mem_inter, mem_singleton], rintro h rfl, have := congr_arg finsupp.support h, rw [finsupp.support_sum_eq_bUnion, finsupp.support_sum_eq_bUnion] at this, { simp only [finsupp.support_single_ne_zero one_ne_zero, bUnion_singleton_eq_self] at this, exact absurd this hst.symm }, all_goals { intros x y, simp [finsupp.support_single_disjoint] } end lemma support_esymm' (n : ℕ) [decidable_eq σ] [nontrivial R] : (esymm σ R n).support = (powerset_len n (univ : finset σ)).bUnion (λ t, {∑ (i : σ) in t, finsupp.single i 1}) := begin rw support_esymm'', congr, funext, exact finsupp.support_single_ne_zero one_ne_zero end lemma support_esymm (n : ℕ) [decidable_eq σ] [nontrivial R] : (esymm σ R n).support = (powerset_len n (univ : finset σ)).image (λ t, ∑ (i : σ) in t, finsupp.single i 1) := by { rw support_esymm', exact bUnion_singleton } lemma degrees_esymm [nontrivial R] (n : ℕ) (hpos : 0 < n) (hn : n ≤ fintype.card σ) : (esymm σ R n).degrees = (univ : finset σ).val := begin classical, have : (finsupp.to_multiset ∘ λ (t : finset σ), ∑ (i : σ) in t, finsupp.single i 1) = finset.val, { funext, simp [finsupp.to_multiset_sum_single] }, rw [degrees, support_esymm, sup_finset_image, this, ←comp_sup_eq_sup_comp], { obtain ⟨k, rfl⟩ := nat.exists_eq_succ_of_ne_zero hpos.ne', simpa using powerset_len_sup _ _ (nat.lt_of_succ_le hn) }, { intros, simp only [union_val, sup_eq_union], congr }, { refl } end end elementary_symmetric end mv_polynomial
52af078a542ece01e899af77541f24560ce6e6db
e61a235b8468b03aee0120bf26ec615c045005d2
/stage0/src/Init/Lean/Util/MonadCache.lean
9db30b47650ab12a4295ae8a0e956d3374f48917
[ "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
4,919
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Control.Reader import Init.Control.EState import Init.Data.HashMap namespace Lean /-- Interface for caching results. -/ class MonadCache (α β : Type) (m : Type → Type) := (findCached? : α → m (Option β)) (cache : α → β → m Unit) /-- If entry `a := b` is already in the cache, then return `b`. Otherwise, execute `b ← f a`, store `a := b` in the cache and return `b`. -/ @[inline] def checkCache {α β : Type} {m : Type → Type} [MonadCache α β m] [Monad m] (a : α) (f : α → m β) : m β := do b? ← MonadCache.findCached? a; match b? with | some b => pure b | none => do b ← f a; MonadCache.cache a b; pure b instance readerLift {α β ρ : Type} {m : Type → Type} [MonadCache α β m] : MonadCache α β (ReaderT ρ m) := { findCached? := fun a r => MonadCache.findCached? a, cache := fun a b r => MonadCache.cache a b } instance exceptLift {α β ε : Type} {m : Type → Type} [MonadCache α β m] [Monad m] : MonadCache α β (ExceptT ε m) := { findCached? := fun a => ExceptT.lift $ MonadCache.findCached? a, cache := fun a b => ExceptT.lift $ MonadCache.cache a b } /-- Adapter for implementing `MonadCache` interface using `HashMap`s. We just have to specify how to extract/modify the `HashMap`. -/ class MonadHashMapCacheAdapter (α β : Type) (m : Type → Type) [HasBeq α] [Hashable α] := (getCache : m (HashMap α β)) (modifyCache : (HashMap α β → HashMap α β) → m Unit) namespace MonadHashMapCacheAdapter @[inline] def findCached? {α β : Type} {m : Type → Type} [HasBeq α] [Hashable α] [Monad m] [MonadHashMapCacheAdapter α β m] (a : α) : m (Option β) := do c ← getCache; pure (c.find? a) @[inline] def cache {α β : Type} {m : Type → Type} [HasBeq α] [Hashable α] [MonadHashMapCacheAdapter α β m] (a : α) (b : β) : m Unit := modifyCache $ fun s => s.insert a b instance {α β : Type} {m : Type → Type} [HasBeq α] [Hashable α] [Monad m] [MonadHashMapCacheAdapter α β m] : MonadCache α β m := { findCached? := MonadHashMapCacheAdapter.findCached?, cache := MonadHashMapCacheAdapter.cache } end MonadHashMapCacheAdapter /-- Auxiliary structure for "adding" a `HashMap` to a state object. -/ structure WithHashMapCache (α β σ : Type) [HasBeq α] [Hashable α] := (state : σ) (cache : HashMap α β := {}) namespace WithHashMapCache @[inline] def getCache {α β σ : Type} [HasBeq α] [Hashable α] : StateM (WithHashMapCache α β σ) (HashMap α β) := do s ← get; pure s.cache @[inline] def modifyCache {α β σ : Type} [HasBeq α] [Hashable α] (f : HashMap α β → HashMap α β) : StateM (WithHashMapCache α β σ) Unit := modify $ fun s => { s with cache := f s.cache } instance stateAdapter (α β σ : Type) [HasBeq α] [Hashable α] : MonadHashMapCacheAdapter α β (StateM (WithHashMapCache α β σ)) := { getCache := WithHashMapCache.getCache, modifyCache := WithHashMapCache.modifyCache } @[inline] def getCacheE {α β ε σ : Type} [HasBeq α] [Hashable α] : EStateM ε (WithHashMapCache α β σ) (HashMap α β) := do s ← get; pure s.cache @[inline] def modifyCacheE {α β ε σ : Type} [HasBeq α] [Hashable α] (f : HashMap α β → HashMap α β) : EStateM ε (WithHashMapCache α β σ) Unit := modify $ fun s => { s with cache := f s.cache } instance estateAdapter (α β ε σ : Type) [HasBeq α] [Hashable α] : MonadHashMapCacheAdapter α β (EStateM ε (WithHashMapCache α β σ)) := { getCache := WithHashMapCache.getCacheE, modifyCache := WithHashMapCache.modifyCacheE } @[inline] def fromState {α β σ δ : Type} [HasBeq α] [Hashable α] (x : StateM σ δ) : StateM (WithHashMapCache α β σ) δ := adaptState (fun (s : WithHashMapCache α β σ) => (s.state, s.cache)) (fun (s : σ) (cache : HashMap α β) => { state := s, cache := cache }) x @[inline] def toState {α β σ δ : Type} [HasBeq α] [Hashable α] (x : StateM (WithHashMapCache α β σ) δ) : StateM σ δ := adaptState' (fun (s : σ) => ({ state := s } : WithHashMapCache α β σ)) (fun (s : WithHashMapCache α β σ) => s.state) x @[inline] def fromEState {α β σ ε δ : Type} [HasBeq α] [Hashable α] (x : EStateM ε σ δ) : EStateM ε (WithHashMapCache α β σ) δ := adaptState (fun (s : WithHashMapCache α β σ) => (s.state, s.cache)) (fun (s : σ) (cache : HashMap α β) => { state := s, cache := cache }) x @[inline] def toEState {α β σ ε δ : Type} [HasBeq α] [Hashable α] (x : EStateM ε (WithHashMapCache α β σ) δ) : EStateM ε σ δ := adaptState' (fun (s : σ) => ({ state := s } : WithHashMapCache α β σ)) (fun (s : WithHashMapCache α β σ) => s.state) x end WithHashMapCache end Lean
fa3029caecaabe66a023ba63ca50f1c0253889dc
3dc4623269159d02a444fe898d33e8c7e7e9461b
/.github/workflows/project_1_a_decrire/foncteur/structure_comax.lean
f82e94fa362917ba3bec2d518cf7416fd370df2a
[]
no_license
Or7ando/lean
cc003e6c41048eae7c34aa6bada51c9e9add9e66
d41169cf4e416a0d42092fb6bdc14131cee9dd15
refs/heads/master
1,650,600,589,722
1,587,262,906,000
1,587,262,906,000
255,387,160
0
0
null
null
null
null
UTF-8
Lean
false
false
8,158
lean
import tactic.ring import tactic.ring_exp import data.finset import data.finsupp import data.nat.choose import algebra.category.CommRing.basic import data.fin import data.finset open CommRing open finset --- Goal : understand the notion of structure with a little exemple ! namespace co_maxi structure comax {R : Type*}[comm_ring R] (a b : R) := --- ici c'est est ce que a et b sont comaximaux (u : R)(v : R) (certificat : a * u + b * v = (1 : R)) #print comax local infix ⊥ := comax --- notation structure comax_hom (A : Type*) (B : Type*)[comm_ring A] [comm_ring B] (a1 a2: A)(b1 b2 : B) := (hom : ring_hom A B) (hom_comp_point : hom a1 = b1 ∧ (hom a2 = b2)) open is_ring_hom --- to have acces map_mul map_one def comp (A B: Type)(φ : A → B)[comm_ring A][comm_ring B][is_ring_hom φ] (a b : A) : (comax a b) → comax (φ a) (φ b) := λ ⟨u_ab,v_ab,certificat_ab⟩, --- {} or ⟨ ⟩ λ OBJET and then OBJET.u etc begin have certificat : (φ a) * (φ u_ab) + (φ b) * (φ v_ab) = 1, -- It's trivial ring identity, but i have to control ! rw [ ← map_mul φ, ← map_mul φ , ← map_add φ,certificat_ab], apply map_one, exact {u := φ u_ab,v := φ v_ab,certificat := certificat}, --- the constructor of structure : i thinck better than ⟨ ⟩ end end co_maxi namespace Exemple --- This is a closed universe #print Ring --- but we have access to the primitive structure #print co_maxi.comax -- we have to co_maxi. first to acces comax ! end Exemple open co_maxi namespace comax section parameters {R : Type}[comm_ring R] parameters (a b c : R) local infix ⊥ := comax --- notation def symm : (a) ⊥ (b) → (b) ⊥ (a) := --- a u + b v = 1 → b v + a u = 1 λ ⟨u,v,certificat⟩, begin have t : b * v + a * u = 1, rw add_comm, assumption, use ⟨v,u,t⟩, end lemma one_perp : 1 ⊥ a := --- with 1 * 1 + a * 0 = 1 begin have h: 1 * 1 + a * 0 = 1, rw [one_mul,mul_zero,add_zero], use ⟨1,0,h⟩ end lemma abab_trick : (a ⊥ c) → (b ⊥ c) → (a * b) ⊥ c := --- Trick to simplify proof ! if a ⊥ c and b ⊥ c then ab ⊥ c from calculus ! λ ⟨ua,va,ka⟩ ⟨ub,vb,kb⟩, begin have J : (a * b) * ( ua * ub) + c * ( a * ua * vb + va * b * ub + va * c * vb) = 1, by calc (a * b) * ( ua * ub) + c * ( a * ua * vb + va * b * ub + va * c * vb) = (a * ua + c * va) * (b * ub + c * vb) : by ring_exp ... = 1 : by rw [ka,kb, one_mul], use ⟨ ua * ub, a * ua * vb + va * b * ub + va * c * vb , J⟩, end open nat lemma star (a b : R) (n : nat): (a ⊥ b) → ((a^n) ⊥ b) := λ u, nat.rec_on n (show (a^0) ⊥ b, {rw pow_zero a, apply one_perp, }) (assume n, assume re : ((a^n) ⊥ b), show (a^(n+1)) ⊥ b, {rw pow_succ a n,apply abab_trick, assumption,assumption}) theorem My_favorite_localisation_lemma (n m : nat) : (a ⊥ b) → (a^n) ⊥ (b^m) := --- the goals λ u, begin apply star, apply symm, -- is there a repeat method ? How to programme such method ? apply star, apply symm, assumption, end ---- --- We want to proof 𝔸 is a local functor : a scheaf for global Zariski for Affᵒᵖ. ---- ( Note 𝔸 is structural for Ring so if you do the job for 𝔸 you do the job for all ring i.e Spec R := Hom(R,•) is a scheme (in sense of functorial geometry) --- (ref Jantzen : 'algebraic group and representation' the first chapter) for all ring R : i can explain) ! --- for the moment only with 2-covering famillies --- There is two axioms : --- 1/ Separation : (for two elements ONLY) --- let R : comm_ring --- Let f,g ∈ R : f ⊥ g. --- Let a ∈ R : --- ∃ m n : ℕ, f^m a = 0 ∧ g^n a = 0 --- i.e a = 0 in localisation {f^k k ∈ N⋆} and {g^k k ∈ N⋆} --- Since f ⊥ g , we have f^m ⊥ b^n --- Have (u,v) s.t f^m u + g^n v = 1 --- multipliying by a give f^m au + g^n a v = a so 0 = a ! --- Note : i don't use Localisation library for the moment (i have to study) ! parameters (f g : R) structure localy_zero (a : R) extends comax(f)(g) := (m : ℕ)(n : ℕ) (proof_localy_zero : f^m * a = 0 ∧ g^n * a = 0) theorem Separation_axiom (a : R) : f ⊥ g → localy_zero (a) → a = 0 := λ coma ⟨t,m,n,proof_localy_zero ⟩, begin have H : (f^m) ⊥ (g^n), apply My_favorite_localisation_lemma, assumption, rcases H with ⟨ua,va,ka⟩, apply eq.symm, have H : 0 = (f ^ m * a* ua + g ^ n *a * va), rw [proof_localy_zero.1,proof_localy_zero.2], apply eq.symm, rw [zero_mul,zero_mul,add_zero], have G : (f ^ m * a* ua + g ^ n *a * va) = (f ^ m * ua + g ^ n * va) * a, ring, rewrite [H,G,ka,one_mul a], end --- Gluing_axiome : --- structure descent_data (s_f s_g : R)(n : ℕ) extends f ⊥ g := --- comment est structuré la notion de desc (m : ℕ) --- F : N →₀ R + certiticat F^⊥ : N →₀ R co-max (proof_m : f^m * g^(n+m) * s_f = g^m * f^(n+m) * s_g ) --- là j'ai accées a des fonctions touts faites --- ---> F, ::: s_F : ℕ → R et n_F : N → ℕ --- ' ζ_f := s_f / f^n_f ' (data) parameters s_f s_g : R --- descent data + ∃ m_f t q (f,g) ---> certificat f g parameters n : ℕ --- structure effective_descente_data extends descent_data s_f s_g n:= ( s : R) (N_f : ℕ) (N_g : ℕ) (proof_n_plus_m_f_g : f^(N_f+n) * s = f^N_f * s_f ∧ g^(N_g+n) * s = g^N_g * s_g) set_option class.instance_max_depth 20 theorem gluing_data : f ⊥ g → descent_data s_f s_g n → effective_descente_data := λ comma,λ y, begin rcases comma with ⟨u,v,proof_of_comax⟩, rcases y with ⟨t,m,proof_m⟩, have H: (f^(n+m)) ⊥ (g^(n+m)), apply My_favorite_localisation_lemma, assumption, rcases H with ⟨vf,vg,proof_n_plus_m_f_g⟩, have H1 : f ^ (m + n) * (vf * s_f * f ^ m + vg * s_g * g ^ m) = ( f ^ (n + m)* vf * s_f * f ^ m + g ^ m *f ^ (n + m) *s_g * vg), ring_exp, have B1 : f ^ (n + m) * vf * s_f * f ^ m + f ^ m * g ^ (n + m) * s_f * vg = f^m *s_f *(f ^ (n + m) * vf + g ^ (n + m) * vg), ring, have H2 : g ^ (m + n) * (vf * s_f * f ^ m + vg * s_g * g ^ m) = f ^ m *g ^ (n+m) *s_f *vf + g ^ m* g ^ (n + m) * vg * s_g, ring_exp, have B2 : g ^ m * f ^ (n + m) * s_g * vf + g ^ m * g ^ (n + m) * vg * s_g = g^m * s_g *(f ^ (n + m) * vf + g ^ (n + m) * vg ), ring, have Y : f^(m+n) * (vf *s_f * f^m+ vg*s_g*g^m) = f^m * s_f ∧ g^(m+n) * (vf *s_f * f^m+ vg*s_g*g^m) = g^m * s_g, split, rw [H1,eq.symm proof_m,B1,proof_n_plus_m_f_g,mul_one], rw [H2,proof_m,B2, proof_n_plus_m_f_g,mul_one], exact {to_descent_data := ⟨t,m,proof_m⟩, s := vf *s_f * f^m+ vg*s_g*g^m, N_f := m, N_g := m, proof_n_plus_m_f_g := Y}, end #check gluing_data end co_maxi
5f5f2693bdfa760d6195cebcba6149aab66d26f7
c777c32c8e484e195053731103c5e52af26a25d1
/src/algebraic_geometry/open_immersion.lean
87d243476e371d239696af651d23b47d888a2b6e
[ "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
86,250
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 algebraic_geometry.presheafed_space.has_colimits import category_theory.limits.shapes.binary_products import category_theory.limits.preserves.shapes.pullbacks import topology.sheaves.functors import topology.category.Top.limits.pullbacks import algebraic_geometry.Scheme import category_theory.limits.shapes.strict_initial import category_theory.limits.shapes.comm_sq import algebra.category.Ring.instances /-! # Open immersions of structured spaces We say that a morphism of presheafed spaces `f : X ⟶ Y` is an open immersions if the underlying map of spaces is an open embedding `f : X ⟶ U ⊆ Y`, and the sheaf map `Y(V) ⟶ f _* X(V)` is an iso for each `V ⊆ U`. Abbreviations are also provided for `SheafedSpace`, `LocallyRingedSpace` and `Scheme`. ## Main definitions * `algebraic_geometry.PresheafedSpace.is_open_immersion`: the `Prop`-valued typeclass asserting that a PresheafedSpace hom `f` is an open_immersion. * `algebraic_geometry.is_open_immersion`: the `Prop`-valued typeclass asserting that a Scheme morphism `f` is an open_immersion. * `algebraic_geometry.PresheafedSpace.is_open_immersion.iso_restrict`: The source of an open immersion is isomorphic to the restriction of the target onto the image. * `algebraic_geometry.PresheafedSpace.is_open_immersion.lift`: Any morphism whose range is contained in an open immersion factors though the open immersion. * `algebraic_geometry.PresheafedSpace.is_open_immersion.to_SheafedSpace`: If `f : X ⟶ Y` is an open immersion of presheafed spaces, and `Y` is a sheafed space, then `X` is also a sheafed space. The morphism as morphisms of sheafed spaces is given by `to_SheafedSpace_hom`. * `algebraic_geometry.PresheafedSpace.is_open_immersion.to_LocallyRingedSpace`: If `f : X ⟶ Y` is an open immersion of presheafed spaces, and `Y` is a locally ringed space, then `X` is also a locally ringed space. The morphism as morphisms of locally ringed spaces is given by `to_LocallyRingedSpace_hom`. ## Main results * `algebraic_geometry.PresheafedSpace.is_open_immersion.comp`: The composition of two open immersions is an open immersion. * `algebraic_geometry.PresheafedSpace.is_open_immersion.of_iso`: An iso is an open immersion. * `algebraic_geometry.PresheafedSpace.is_open_immersion.to_iso`: A surjective open immersion is an isomorphism. * `algebraic_geometry.PresheafedSpace.is_open_immersion.stalk_iso`: An open immersion induces an isomorphism on stalks. * `algebraic_geometry.PresheafedSpace.is_open_immersion.has_pullback_of_left`: If `f` is an open immersion, then the pullback `(f, g)` exists (and the forgetful functor to `Top` preserves it). * `algebraic_geometry.PresheafedSpace.is_open_immersion.pullback_snd_of_left`: Open immersions are stable under pullbacks. * `algebraic_geometry.SheafedSpace.is_open_immersion.of_stalk_iso` An (topological) open embedding between two sheafed spaces is an open immersion if all the stalk maps are isomorphisms. -/ open topological_space category_theory opposite open category_theory.limits namespace algebraic_geometry universes v v₁ v₂ u variables {C : Type u} [category.{v} C] /-- An open immersion of PresheafedSpaces is an open embedding `f : X ⟶ U ⊆ Y` of the underlying spaces, such that the sheaf map `Y(V) ⟶ f _* X(V)` is an iso for each `V ⊆ U`. -/ class PresheafedSpace.is_open_immersion {X Y : PresheafedSpace.{v} C} (f : X ⟶ Y) : Prop := (base_open : open_embedding f.base) (c_iso : ∀ U : opens X, is_iso (f.c.app (op (base_open.is_open_map.functor.obj U)))) /-- A morphism of SheafedSpaces is an open immersion if it is an open immersion as a morphism of PresheafedSpaces -/ abbreviation SheafedSpace.is_open_immersion {X Y : SheafedSpace.{v} C} (f : X ⟶ Y) : Prop := PresheafedSpace.is_open_immersion f /-- A morphism of LocallyRingedSpaces is an open immersion if it is an open immersion as a morphism of SheafedSpaces -/ abbreviation LocallyRingedSpace.is_open_immersion {X Y : LocallyRingedSpace} (f : X ⟶ Y) : Prop := SheafedSpace.is_open_immersion f.1 /-- A morphism of Schemes is an open immersion if it is an open immersion as a morphism of LocallyRingedSpaces -/ abbreviation is_open_immersion {X Y : Scheme} (f : X ⟶ Y) : Prop := LocallyRingedSpace.is_open_immersion f namespace PresheafedSpace.is_open_immersion open PresheafedSpace local notation `is_open_immersion` := PresheafedSpace.is_open_immersion attribute [instance] is_open_immersion.c_iso section variables {X Y : PresheafedSpace.{v} C} {f : X ⟶ Y} (H : is_open_immersion f) /-- The functor `opens X ⥤ opens Y` associated with an open immersion `f : X ⟶ Y`. -/ abbreviation open_functor := H.base_open.is_open_map.functor /-- An open immersion `f : X ⟶ Y` induces an isomorphism `X ≅ Y|_{f(X)}`. -/ @[simps hom_c_app] noncomputable def iso_restrict : X ≅ Y.restrict H.base_open := PresheafedSpace.iso_of_components (iso.refl _) begin symmetry, fapply nat_iso.of_components, intro U, refine as_iso (f.c.app (op (H.open_functor.obj (unop U)))) ≪≫ X.presheaf.map_iso (eq_to_iso _), { induction U using opposite.rec, cases U, dsimp only [is_open_map.functor, functor.op, opens.map], congr' 2, erw set.preimage_image_eq _ H.base_open.inj, refl }, { intros U V i, simp only [category_theory.eq_to_iso.hom, Top.presheaf.pushforward_obj_map, category.assoc, functor.op_map, iso.trans_hom, as_iso_hom, functor.map_iso_hom, ←X.presheaf.map_comp], erw [f.c.naturality_assoc, ←X.presheaf.map_comp], congr } end @[simp] lemma iso_restrict_hom_of_restrict : H.iso_restrict.hom ≫ Y.of_restrict _ = f := begin ext, { simp only [comp_c_app, iso_restrict_hom_c_app, nat_trans.comp_app, eq_to_hom_refl, of_restrict_c_app, category.assoc, whisker_right_id'], erw [category.comp_id, f.c.naturality_assoc, ←X.presheaf.map_comp], transitivity f.c.app x ≫ X.presheaf.map (𝟙 _), { congr }, { erw [X.presheaf.map_id, category.comp_id] } }, { refl, } end @[simp] lemma iso_restrict_inv_of_restrict : H.iso_restrict.inv ≫ f = Y.of_restrict _ := by { rw [iso.inv_comp_eq, iso_restrict_hom_of_restrict] } instance mono [H : is_open_immersion f] : mono f := by { rw ← H.iso_restrict_hom_of_restrict, apply mono_comp } /-- The composition of two open immersions is an open immersion. -/ instance comp {Z : PresheafedSpace C} (f : X ⟶ Y) [hf : is_open_immersion f] (g : Y ⟶ Z) [hg : is_open_immersion g] : is_open_immersion (f ≫ g) := { base_open := hg.base_open.comp hf.base_open, c_iso := λ U, begin generalize_proofs h, dsimp only [algebraic_geometry.PresheafedSpace.comp_c_app, unop_op, functor.op, comp_base, Top.presheaf.pushforward_obj_obj, opens.map_comp_obj], apply_with is_iso.comp_is_iso { instances := ff }, swap, { have : (opens.map g.base).obj (h.functor.obj U) = hf.open_functor.obj U, { ext1, dsimp only [opens.map_coe, is_open_map.functor_obj_coe, comp_base], rw [coe_comp, ← set.image_image, set.preimage_image_eq _ hg.base_open.inj] }, rw this, apply_instance }, { have : h.functor.obj U = hg.open_functor.obj (hf.open_functor.obj U), { ext1, dsimp only [is_open_map.functor_obj_coe], rw [comp_base, coe_comp, ←set.image_image] }, rw this, apply_instance } end } /-- For an open immersion `f : X ⟶ Y` and an open set `U ⊆ X`, we have the map `X(U) ⟶ Y(U)`. -/ noncomputable def inv_app (U : opens X) : X.presheaf.obj (op U) ⟶ Y.presheaf.obj (op (H.open_functor.obj U)) := X.presheaf.map (eq_to_hom (by simp [opens.map, set.preimage_image_eq _ H.base_open.inj])) ≫ inv (f.c.app (op (H.open_functor.obj U))) @[simp, reassoc] lemma inv_naturality {U V : (opens X)ᵒᵖ} (i : U ⟶ V) : X.presheaf.map i ≫ H.inv_app (unop V) = H.inv_app (unop U) ≫ Y.presheaf.map (H.open_functor.op.map i) := begin simp only [inv_app, ←category.assoc], rw [is_iso.comp_inv_eq], simp only [category.assoc, f.c.naturality, is_iso.inv_hom_id_assoc, ← X.presheaf.map_comp], erw ← X.presheaf.map_comp, congr end instance (U : opens X) : is_iso (H.inv_app U) := by { delta inv_app, apply_instance } lemma inv_inv_app (U : opens X) : inv (H.inv_app U) = f.c.app (op (H.open_functor.obj U)) ≫ X.presheaf.map (eq_to_hom (by simp [opens.map, set.preimage_image_eq _ H.base_open.inj])) := begin rw ← cancel_epi (H.inv_app U), rw is_iso.hom_inv_id, delta inv_app, simp [← functor.map_comp] end @[simp, reassoc, elementwise] lemma inv_app_app (U : opens X) : H.inv_app U ≫ f.c.app (op (H.open_functor.obj U)) = X.presheaf.map (eq_to_hom (by simp [opens.map, set.preimage_image_eq _ H.base_open.inj])) := by rw [inv_app, category.assoc, is_iso.inv_hom_id, category.comp_id] @[simp, reassoc] lemma app_inv_app (U : opens Y) : f.c.app (op U) ≫ H.inv_app ((opens.map f.base).obj U) = Y.presheaf.map ((hom_of_le (by exact set.image_preimage_subset f.base U)).op : op U ⟶ op (H.open_functor.obj ((opens.map f.base).obj U))) := by { erw ← category.assoc, rw [is_iso.comp_inv_eq, f.c.naturality], congr } /-- A variant of `app_inv_app` that gives an `eq_to_hom` instead of `hom_of_le`. -/ @[reassoc] lemma app_inv_app' (U : opens Y) (hU : (U : set Y) ⊆ set.range f.base) : f.c.app (op U) ≫ H.inv_app ((opens.map f.base).obj U) = Y.presheaf.map (eq_to_hom (by { apply le_antisymm, { exact set.image_preimage_subset f.base U.1 }, { rw [← set_like.coe_subset_coe], refine has_le.le.trans_eq _ (@set.image_preimage_eq_inter_range _ _ f.base U.1).symm, exact set.subset_inter_iff.mpr ⟨λ _ h, h, hU⟩ } })).op := by { erw ← category.assoc, rw [is_iso.comp_inv_eq, f.c.naturality], congr } /-- An isomorphism is an open immersion. -/ instance of_iso {X Y : PresheafedSpace.{v} C} (H : X ≅ Y) : is_open_immersion H.hom := { base_open := (Top.homeo_of_iso ((forget C).map_iso H)).open_embedding, c_iso := λ _, infer_instance } @[priority 100] instance of_is_iso {X Y : PresheafedSpace.{v} C} (f : X ⟶ Y) [is_iso f] : is_open_immersion f := algebraic_geometry.PresheafedSpace.is_open_immersion.of_iso (as_iso f) instance of_restrict {X : Top} (Y : PresheafedSpace C) {f : X ⟶ Y.carrier} (hf : open_embedding f) : is_open_immersion (Y.of_restrict hf) := { base_open := hf, c_iso := λ U, begin dsimp, have : (opens.map f).obj (hf.is_open_map.functor.obj U) = U, { ext1, exact set.preimage_image_eq _ hf.inj }, convert (show is_iso (Y.presheaf.map (𝟙 _)), from infer_instance), { apply subsingleton.helim, rw this }, { rw Y.presheaf.map_id, apply_instance } end } @[elementwise, simp] lemma of_restrict_inv_app {C : Type*} [category C] (X : PresheafedSpace C) {Y : Top} {f : Y ⟶ Top.of X.carrier} (h : open_embedding f) (U : opens (X.restrict h).carrier) : (PresheafedSpace.is_open_immersion.of_restrict X h).inv_app U = 𝟙 _ := begin delta PresheafedSpace.is_open_immersion.inv_app, rw [is_iso.comp_inv_eq, category.id_comp], change X.presheaf.map _ = X.presheaf.map _, congr end /-- An open immersion is an iso if the underlying continuous map is epi. -/ lemma to_iso (f : X ⟶ Y) [h : is_open_immersion f] [h' : epi f.base] : is_iso f := begin apply_with is_iso_of_components { instances := ff }, { let : X ≃ₜ Y := (homeomorph.of_embedding _ h.base_open.to_embedding).trans { to_fun := subtype.val, inv_fun := λ x, ⟨x, by { rw set.range_iff_surjective.mpr ((Top.epi_iff_surjective _).mp h'), trivial }⟩, left_inv := λ ⟨_,_⟩, rfl, right_inv := λ _, rfl }, convert is_iso.of_iso (Top.iso_of_homeo this), { ext, refl } }, { apply_with nat_iso.is_iso_of_is_iso_app { instances := ff }, intro U, have : U = op (h.open_functor.obj ((opens.map f.base).obj (unop U))), { induction U using opposite.rec, cases U, dsimp only [functor.op, opens.map], congr, exact (set.image_preimage_eq _ ((Top.epi_iff_surjective _).mp h')).symm }, convert @@is_open_immersion.c_iso _ h ((opens.map f.base).obj (unop U)) } end instance stalk_iso [has_colimits C] [H : is_open_immersion f] (x : X) : is_iso (stalk_map f x) := begin rw ← H.iso_restrict_hom_of_restrict, rw PresheafedSpace.stalk_map.comp, apply_instance end end section pullback noncomputable theory variables {X Y Z : PresheafedSpace.{v} C} (f : X ⟶ Z) [hf : is_open_immersion f] (g : Y ⟶ Z) include hf /-- (Implementation.) The projection map when constructing the pullback along an open immersion. -/ def pullback_cone_of_left_fst : Y.restrict (Top.snd_open_embedding_of_left_open_embedding hf.base_open g.base) ⟶ X := { base := pullback.fst, c := { app := λ U, hf.inv_app (unop U) ≫ g.c.app (op (hf.base_open.is_open_map.functor.obj (unop U))) ≫ Y.presheaf.map (eq_to_hom (begin simp only [is_open_map.functor, subtype.mk_eq_mk, unop_op, op_inj_iff, opens.map, subtype.coe_mk, functor.op_obj, subtype.val_eq_coe], apply has_le.le.antisymm, { rintros _ ⟨_, h₁, h₂⟩, use (Top.pullback_iso_prod_subtype _ _).inv ⟨⟨_, _⟩, h₂⟩, simpa using h₁ }, { rintros _ ⟨x, h₁, rfl⟩, exact ⟨_, h₁, concrete_category.congr_hom pullback.condition x⟩ } end)), naturality' := begin intros U V i, induction U using opposite.rec, induction V using opposite.rec, simp only [quiver.hom.unop_op, Top.presheaf.pushforward_obj_map, category.assoc, nat_trans.naturality_assoc, functor.op_map, inv_naturality_assoc, ← Y.presheaf.map_comp], erw ← Y.presheaf.map_comp, congr end } } lemma pullback_cone_of_left_condition : pullback_cone_of_left_fst f g ≫ f = Y.of_restrict _ ≫ g := begin ext U, { induction U using opposite.rec, dsimp only [comp_c_app, nat_trans.comp_app, unop_op, whisker_right_app, pullback_cone_of_left_fst], simp only [quiver.hom.unop_op, Top.presheaf.pushforward_obj_map, app_inv_app_assoc, eq_to_hom_app, eq_to_hom_unop, category.assoc, nat_trans.naturality_assoc, functor.op_map], erw [← Y.presheaf.map_comp, ← Y.presheaf.map_comp], congr }, { simpa using pullback.condition } end /-- We construct the pullback along an open immersion via restricting along the pullback of the maps of underlying spaces (which is also an open embedding). -/ def pullback_cone_of_left : pullback_cone f g := pullback_cone.mk (pullback_cone_of_left_fst f g) (Y.of_restrict _) (pullback_cone_of_left_condition f g) variable (s : pullback_cone f g) /-- (Implementation.) Any cone over `cospan f g` indeed factors through the constructed cone. -/ def pullback_cone_of_left_lift : s.X ⟶ (pullback_cone_of_left f g).X := { base := pullback.lift s.fst.base s.snd.base (congr_arg (λ x, PresheafedSpace.hom.base x) s.condition), c := { app := λ U, s.snd.c.app _ ≫ s.X.presheaf.map (eq_to_hom (begin dsimp only [opens.map, is_open_map.functor, functor.op], congr' 2, let s' : pullback_cone f.base g.base := pullback_cone.mk s.fst.base s.snd.base _, have : _ = s.snd.base := limit.lift_π s' walking_cospan.right, conv_lhs { erw ← this, rw coe_comp, erw ← set.preimage_preimage }, erw set.preimage_image_eq _ (Top.snd_open_embedding_of_left_open_embedding hf.base_open g.base).inj, end)), naturality' := λ U V i, begin erw s.snd.c.naturality_assoc, rw category.assoc, erw [← s.X.presheaf.map_comp, ← s.X.presheaf.map_comp], congr end } } -- this lemma is not a `simp` lemma, because it is an implementation detail lemma pullback_cone_of_left_lift_fst : pullback_cone_of_left_lift f g s ≫ (pullback_cone_of_left f g).fst = s.fst := begin ext x, { induction x using opposite.rec, change ((_ ≫ _) ≫ _ ≫ _) ≫ _ = _, simp_rw [category.assoc], erw ← s.X.presheaf.map_comp, erw s.snd.c.naturality_assoc, have := congr_app s.condition (op (hf.open_functor.obj x)), dsimp only [comp_c_app, unop_op] at this, rw ← is_iso.comp_inv_eq at this, reassoc! this, erw [← this, hf.inv_app_app_assoc, s.fst.c.naturality_assoc], simpa [eq_to_hom_map], }, { change pullback.lift _ _ _ ≫ pullback.fst = _, simp } end -- this lemma is not a `simp` lemma, because it is an implementation detail lemma pullback_cone_of_left_lift_snd : pullback_cone_of_left_lift f g s ≫ (pullback_cone_of_left f g).snd = s.snd := begin ext x, { change (_ ≫ _ ≫ _) ≫ _ = _, simp_rw category.assoc, erw s.snd.c.naturality_assoc, erw [← s.X.presheaf.map_comp, ← s.X.presheaf.map_comp], transitivity s.snd.c.app x ≫ s.X.presheaf.map (𝟙 _), { congr }, { rw s.X.presheaf.map_id, erw category.comp_id } }, { change pullback.lift _ _ _ ≫ pullback.snd = _, simp } end instance pullback_cone_snd_is_open_immersion : is_open_immersion (pullback_cone_of_left f g).snd := begin erw category_theory.limits.pullback_cone.mk_snd, apply_instance end /-- The constructed pullback cone is indeed the pullback. -/ def pullback_cone_of_left_is_limit : is_limit (pullback_cone_of_left f g) := begin apply pullback_cone.is_limit_aux', intro s, use pullback_cone_of_left_lift f g s, use pullback_cone_of_left_lift_fst f g s, use pullback_cone_of_left_lift_snd f g s, intros m h₁ h₂, rw ← cancel_mono (pullback_cone_of_left f g).snd, exact (h₂.trans (pullback_cone_of_left_lift_snd f g s).symm) end instance has_pullback_of_left : has_pullback f g := ⟨⟨⟨_, pullback_cone_of_left_is_limit f g⟩⟩⟩ instance has_pullback_of_right : has_pullback g f := has_pullback_symmetry f g /-- Open immersions are stable under base-change. -/ instance pullback_snd_of_left : is_open_immersion (pullback.snd : pullback f g ⟶ _) := begin delta pullback.snd, rw ← limit.iso_limit_cone_hom_π ⟨_, pullback_cone_of_left_is_limit f g⟩ walking_cospan.right, apply_instance end /-- Open immersions are stable under base-change. -/ instance pullback_fst_of_right : is_open_immersion (pullback.fst : pullback g f ⟶ _) := begin rw ← pullback_symmetry_hom_comp_snd, apply_instance end instance pullback_to_base_is_open_immersion [is_open_immersion g] : is_open_immersion (limit.π (cospan f g) walking_cospan.one) := begin rw [←limit.w (cospan f g) walking_cospan.hom.inl, cospan_map_inl], apply_instance end instance forget_preserves_limits_of_left : preserves_limit (cospan f g) (forget C) := preserves_limit_of_preserves_limit_cone (pullback_cone_of_left_is_limit f g) begin apply (is_limit.postcompose_hom_equiv (diagram_iso_cospan.{v} _) _).to_fun, refine (is_limit.equiv_iso_limit _).to_fun (limit.is_limit (cospan f.base g.base)), fapply cones.ext, exact (iso.refl _), change ∀ j, _ = 𝟙 _ ≫ _ ≫ _, simp_rw category.id_comp, rintros (_|_|_); symmetry, { erw category.comp_id, exact limit.w (cospan f.base g.base) walking_cospan.hom.inl }, { exact category.comp_id _ }, { exact category.comp_id _ }, end instance forget_preserves_limits_of_right : preserves_limit (cospan g f) (forget C) := preserves_pullback_symmetry (forget C) f g lemma pullback_snd_is_iso_of_range_subset (H : set.range g.base ⊆ set.range f.base) : is_iso (pullback.snd : pullback f g ⟶ _) := begin haveI := Top.snd_iso_of_left_embedding_range_subset hf.base_open.to_embedding g.base H, haveI : is_iso (pullback.snd : pullback f g ⟶ _).base, { delta pullback.snd, rw ← limit.iso_limit_cone_hom_π ⟨_, pullback_cone_of_left_is_limit f g⟩ walking_cospan.right, change is_iso (_ ≫ pullback.snd), apply_instance }, apply to_iso end /-- The universal property of open immersions: For an open immersion `f : X ⟶ Z`, given any morphism of schemes `g : Y ⟶ Z` whose topological image is contained in the image of `f`, we can lift this morphism to a unique `Y ⟶ X` that commutes with these maps. -/ def lift (H : set.range g.base ⊆ set.range f.base) : Y ⟶ X := begin haveI := pullback_snd_is_iso_of_range_subset f g H, exact inv (pullback.snd : pullback f g ⟶ _) ≫ pullback.fst, end @[simp, reassoc] lemma lift_fac (H : set.range g.base ⊆ set.range f.base) : lift f g H ≫ f = g := by { erw category.assoc, rw is_iso.inv_comp_eq, exact pullback.condition } lemma lift_uniq (H : set.range g.base ⊆ set.range f.base) (l : Y ⟶ X) (hl : l ≫ f = g) : l = lift f g H := by rw [← cancel_mono f, hl, lift_fac] /-- Two open immersions with equal range is isomorphic. -/ @[simps] def iso_of_range_eq [is_open_immersion g] (e : set.range f.base = set.range g.base) : X ≅ Y := { hom := lift g f (le_of_eq e), inv := lift f g (le_of_eq e.symm), hom_inv_id' := by { rw ← cancel_mono f, simp }, inv_hom_id' := by { rw ← cancel_mono g, simp } } end pullback open category_theory.limits.walking_cospan section to_SheafedSpace variables {X : PresheafedSpace.{v} C} (Y : SheafedSpace C) variables (f : X ⟶ Y.to_PresheafedSpace) [H : is_open_immersion f] include H /-- If `X ⟶ Y` is an open immersion, and `Y` is a SheafedSpace, then so is `X`. -/ def to_SheafedSpace : SheafedSpace C := { is_sheaf := begin apply Top.presheaf.is_sheaf_of_iso (sheaf_iso_of_iso H.iso_restrict.symm).symm, apply Top.sheaf.pushforward_sheaf_of_sheaf, exact (Y.restrict H.base_open).is_sheaf end, to_PresheafedSpace := X } @[simp] lemma to_SheafedSpace_to_PresheafedSpace : (to_SheafedSpace Y f).to_PresheafedSpace = X := rfl /-- If `X ⟶ Y` is an open immersion of PresheafedSpaces, and `Y` is a SheafedSpace, we can upgrade it into a morphism of SheafedSpaces. -/ def to_SheafedSpace_hom : to_SheafedSpace Y f ⟶ Y := f @[simp] lemma to_SheafedSpace_hom_base : (to_SheafedSpace_hom Y f).base = f.base := rfl @[simp] lemma to_SheafedSpace_hom_c : (to_SheafedSpace_hom Y f).c = f.c := rfl instance to_SheafedSpace_is_open_immersion : SheafedSpace.is_open_immersion (to_SheafedSpace_hom Y f) := H omit H @[simp] lemma SheafedSpace_to_SheafedSpace {X Y : SheafedSpace.{v} C} (f : X ⟶ Y) [is_open_immersion f] : to_SheafedSpace Y f = X := by unfreezingI { cases X, refl } end to_SheafedSpace section to_LocallyRingedSpace variables {X : PresheafedSpace.{u} CommRing.{u}} (Y : LocallyRingedSpace.{u}) variables (f : X ⟶ Y.to_PresheafedSpace) [H : is_open_immersion f] include H /-- If `X ⟶ Y` is an open immersion, and `Y` is a LocallyRingedSpace, then so is `X`. -/ def to_LocallyRingedSpace : LocallyRingedSpace := { to_SheafedSpace := to_SheafedSpace Y.to_SheafedSpace f, local_ring := λ x, begin haveI : local_ring (Y.to_SheafedSpace.to_PresheafedSpace.stalk (f.base x)) := Y.local_ring _, exact (as_iso (stalk_map f x)).CommRing_iso_to_ring_equiv.local_ring end } @[simp] lemma to_LocallyRingedSpace_to_SheafedSpace : (to_LocallyRingedSpace Y f).to_SheafedSpace = (to_SheafedSpace Y.1 f) := rfl /-- If `X ⟶ Y` is an open immersion of PresheafedSpaces, and `Y` is a LocallyRingedSpace, we can upgrade it into a morphism of LocallyRingedSpace. -/ def to_LocallyRingedSpace_hom : to_LocallyRingedSpace Y f ⟶ Y := ⟨f, λ x, infer_instance⟩ @[simp] lemma to_LocallyRingedSpace_hom_val : (to_LocallyRingedSpace_hom Y f).val = f := rfl instance to_LocallyRingedSpace_is_open_immersion : LocallyRingedSpace.is_open_immersion (to_LocallyRingedSpace_hom Y f) := H omit H @[simp] lemma LocallyRingedSpace_to_LocallyRingedSpace {X Y : LocallyRingedSpace} (f : X ⟶ Y) [LocallyRingedSpace.is_open_immersion f] : to_LocallyRingedSpace Y f.1 = X := by unfreezingI { cases X, delta to_LocallyRingedSpace, simp } end to_LocallyRingedSpace lemma is_iso_of_subset {X Y : PresheafedSpace.{v} C} (f : X ⟶ Y) [H : PresheafedSpace.is_open_immersion f] (U : opens Y.carrier) (hU : (U : set Y.carrier) ⊆ set.range f.base) : is_iso (f.c.app $ op U) := begin have : U = H.base_open.is_open_map.functor.obj ((opens.map f.base).obj U), { ext1, exact (set.inter_eq_left_iff_subset.mpr hU).symm.trans set.image_preimage_eq_inter_range.symm }, convert PresheafedSpace.is_open_immersion.c_iso ((opens.map f.base).obj U), end end PresheafedSpace.is_open_immersion namespace SheafedSpace.is_open_immersion @[priority 100] instance of_is_iso {X Y : SheafedSpace.{v} C} (f : X ⟶ Y) [is_iso f] : SheafedSpace.is_open_immersion f := @@PresheafedSpace.is_open_immersion.of_is_iso _ f (SheafedSpace.forget_to_PresheafedSpace.map_is_iso _) instance comp {X Y Z : SheafedSpace C} (f : X ⟶ Y) (g : Y ⟶ Z) [SheafedSpace.is_open_immersion f] [SheafedSpace.is_open_immersion g] : SheafedSpace.is_open_immersion (f ≫ g) := PresheafedSpace.is_open_immersion.comp f g section pullback variables {X Y Z : SheafedSpace C} (f : X ⟶ Z) (g : Y ⟶ Z) variable [H : SheafedSpace.is_open_immersion f] include H local notation `forget` := SheafedSpace.forget_to_PresheafedSpace open category_theory.limits.walking_cospan instance : mono f := forget .mono_of_mono_map (show @mono (PresheafedSpace C) _ _ _ f, by apply_instance) instance forget_map_is_open_immersion : PresheafedSpace.is_open_immersion (forget .map f) := ⟨H.base_open, H.c_iso⟩ instance has_limit_cospan_forget_of_left : has_limit (cospan f g ⋙ forget) := begin apply has_limit_of_iso (diagram_iso_cospan.{v} _).symm, change has_limit (cospan (forget .map f) (forget .map g)), apply_instance end instance has_limit_cospan_forget_of_left' : has_limit (cospan ((cospan f g ⋙ forget).map hom.inl) ((cospan f g ⋙ forget).map hom.inr)) := show has_limit (cospan (forget .map f) (forget .map g)), from infer_instance instance has_limit_cospan_forget_of_right : has_limit (cospan g f ⋙ forget) := begin apply has_limit_of_iso (diagram_iso_cospan.{v} _).symm, change has_limit (cospan (forget .map g) (forget .map f)), apply_instance end instance has_limit_cospan_forget_of_right' : has_limit (cospan ((cospan g f ⋙ forget).map hom.inl) ((cospan g f ⋙ forget).map hom.inr)) := show has_limit (cospan (forget .map g) (forget .map f)), from infer_instance instance forget_creates_pullback_of_left : creates_limit (cospan f g) forget := creates_limit_of_fully_faithful_of_iso (PresheafedSpace.is_open_immersion.to_SheafedSpace Y (@pullback.snd (PresheafedSpace C) _ _ _ _ f g _)) (eq_to_iso (show pullback _ _ = pullback _ _, by congr) ≪≫ has_limit.iso_of_nat_iso (diagram_iso_cospan _).symm) instance forget_creates_pullback_of_right : creates_limit (cospan g f) forget := creates_limit_of_fully_faithful_of_iso (PresheafedSpace.is_open_immersion.to_SheafedSpace Y (@pullback.fst (PresheafedSpace C) _ _ _ _ g f _)) (eq_to_iso (show pullback _ _ = pullback _ _, by congr) ≪≫ has_limit.iso_of_nat_iso (diagram_iso_cospan _).symm) instance SheafedSpace_forget_preserves_of_left : preserves_limit (cospan f g) (SheafedSpace.forget C) := @@limits.comp_preserves_limit _ _ _ _ forget (PresheafedSpace.forget C) _ begin apply_with (preserves_limit_of_iso_diagram _ (diagram_iso_cospan.{v} _).symm) { instances := tt }, dsimp, apply_instance end instance SheafedSpace_forget_preserves_of_right : preserves_limit (cospan g f) (SheafedSpace.forget C) := preserves_pullback_symmetry _ _ _ instance SheafedSpace_has_pullback_of_left : has_pullback f g := has_limit_of_created (cospan f g) forget instance SheafedSpace_has_pullback_of_right : has_pullback g f := has_limit_of_created (cospan g f) forget /-- Open immersions are stable under base-change. -/ instance SheafedSpace_pullback_snd_of_left : SheafedSpace.is_open_immersion (pullback.snd : pullback f g ⟶ _) := begin delta pullback.snd, have : _ = limit.π (cospan f g) right := preserves_limits_iso_hom_π forget (cospan f g) right, rw ← this, have := has_limit.iso_of_nat_iso_hom_π (diagram_iso_cospan.{v} (cospan f g ⋙ forget)) right, erw category.comp_id at this, rw ← this, dsimp, apply_instance end instance SheafedSpace_pullback_fst_of_right : SheafedSpace.is_open_immersion (pullback.fst : pullback g f ⟶ _) := begin delta pullback.fst, have : _ = limit.π (cospan g f) left := preserves_limits_iso_hom_π forget (cospan g f) left, rw ← this, have := has_limit.iso_of_nat_iso_hom_π (diagram_iso_cospan.{v} (cospan g f ⋙ forget)) left, erw category.comp_id at this, rw ← this, dsimp, apply_instance end instance SheafedSpace_pullback_to_base_is_open_immersion [SheafedSpace.is_open_immersion g] : SheafedSpace.is_open_immersion (limit.π (cospan f g) one : pullback f g ⟶ Z) := begin rw [←limit.w (cospan f g) hom.inl, cospan_map_inl], apply_instance end end pullback section of_stalk_iso variables [has_limits C] [has_colimits C] [concrete_category.{v} C] variables [reflects_isomorphisms (forget C)] [preserves_limits (forget C)] variables [preserves_filtered_colimits (forget C)] /-- Suppose `X Y : SheafedSpace C`, where `C` is a concrete category, whose forgetful functor reflects isomorphisms, preserves limits and filtered colimits. Then a morphism `X ⟶ Y` that is a topological open embedding is an open immersion iff every stalk map is an iso. -/ lemma of_stalk_iso {X Y : SheafedSpace C} (f : X ⟶ Y) (hf : open_embedding f.base) [H : ∀ x : X, is_iso (PresheafedSpace.stalk_map f x)] : SheafedSpace.is_open_immersion f := { base_open := hf, c_iso := λ U, begin apply_with (Top.presheaf.app_is_iso_of_stalk_functor_map_iso (show Y.sheaf ⟶ (Top.sheaf.pushforward f.base).obj X.sheaf, from ⟨f.c⟩)) { instances := ff }, rintros ⟨_, y, hy, rfl⟩, specialize H y, delta PresheafedSpace.stalk_map at H, haveI H' := Top.presheaf.stalk_pushforward.stalk_pushforward_iso_of_open_embedding C hf X.presheaf y, have := @@is_iso.comp_is_iso _ H (@@is_iso.inv_is_iso _ H'), rw [category.assoc, is_iso.hom_inv_id, category.comp_id] at this, exact this end } end of_stalk_iso section prod variables [has_limits C] {ι : Type v} (F : discrete ι ⥤ SheafedSpace C) [has_colimit F] (i : discrete ι) lemma sigma_ι_open_embedding : open_embedding (colimit.ι F i).base := begin rw ← (show _ = (colimit.ι F i).base, from ι_preserves_colimits_iso_inv (SheafedSpace.forget C) F i), have : _ = _ ≫ colimit.ι (discrete.functor ((F ⋙ SheafedSpace.forget C).obj ∘ discrete.mk)) i := has_colimit.iso_of_nat_iso_ι_hom discrete.nat_iso_functor i, rw ← iso.eq_comp_inv at this, rw this, have : colimit.ι _ _ ≫ _ = _ := Top.sigma_iso_sigma_hom_ι.{v v} ((F ⋙ SheafedSpace.forget C).obj ∘ discrete.mk) i.as, rw ← iso.eq_comp_inv at this, cases i, rw this, simp_rw [← category.assoc, Top.open_embedding_iff_comp_is_iso, Top.open_embedding_iff_is_iso_comp], dsimp, exact open_embedding_sigma_mk end lemma image_preimage_is_empty (j : discrete ι) (h : i ≠ j) (U : opens (F.obj i)) : (opens.map (colimit.ι (F ⋙ SheafedSpace.forget_to_PresheafedSpace) j).base).obj ((opens.map (preserves_colimit_iso SheafedSpace.forget_to_PresheafedSpace F).inv.base).obj ((sigma_ι_open_embedding F i).is_open_map.functor.obj U)) = ⊥ := begin ext, apply iff_false_intro, rintro ⟨y, hy, eq⟩, replace eq := concrete_category.congr_arg (preserves_colimit_iso (SheafedSpace.forget C) F ≪≫ has_colimit.iso_of_nat_iso discrete.nat_iso_functor ≪≫ Top.sigma_iso_sigma.{v} _).hom eq, simp_rw [category_theory.iso.trans_hom, ← Top.comp_app, ← PresheafedSpace.comp_base] at eq, rw ι_preserves_colimits_iso_inv at eq, change ((SheafedSpace.forget C).map (colimit.ι F i) ≫ _) y = ((SheafedSpace.forget C).map (colimit.ι F j) ≫ _) x at eq, cases i, cases j, rw [ι_preserves_colimits_iso_hom_assoc, ι_preserves_colimits_iso_hom_assoc, has_colimit.iso_of_nat_iso_ι_hom_assoc, has_colimit.iso_of_nat_iso_ι_hom_assoc, Top.sigma_iso_sigma_hom_ι.{v}, Top.sigma_iso_sigma_hom_ι.{v}] at eq, exact h (congr_arg discrete.mk (congr_arg sigma.fst eq)), end instance sigma_ι_is_open_immersion [has_strict_terminal_objects C] : SheafedSpace.is_open_immersion (colimit.ι F i) := { base_open := sigma_ι_open_embedding F i, c_iso := λ U, begin have e : colimit.ι F i = _ := (ι_preserves_colimits_iso_inv SheafedSpace.forget_to_PresheafedSpace F i).symm, have H : open_embedding (colimit.ι (F ⋙ SheafedSpace.forget_to_PresheafedSpace) i ≫ (preserves_colimit_iso SheafedSpace.forget_to_PresheafedSpace F).inv).base := e ▸ sigma_ι_open_embedding F i, suffices : is_iso ((colimit.ι (F ⋙ SheafedSpace.forget_to_PresheafedSpace) i ≫ (preserves_colimit_iso SheafedSpace.forget_to_PresheafedSpace F).inv).c.app (op (H.is_open_map.functor.obj U))), { convert this }, rw [PresheafedSpace.comp_c_app, ← PresheafedSpace.colimit_presheaf_obj_iso_componentwise_limit_hom_π], rsufficesI : is_iso (limit.π (PresheafedSpace.componentwise_diagram (F ⋙ SheafedSpace.forget_to_PresheafedSpace) ((opens.map (preserves_colimit_iso SheafedSpace.forget_to_PresheafedSpace F).inv.base).obj (unop $ op $ H.is_open_map.functor.obj U))) (op i)), { apply_instance }, apply limit_π_is_iso_of_is_strict_terminal, intros j hj, induction j using opposite.rec, dsimp, convert (F.obj j).sheaf.is_terminal_of_empty, convert image_preimage_is_empty F i j (λ h, hj (congr_arg op h.symm)) U, exact (congr_arg PresheafedSpace.hom.base e).symm end } end prod end SheafedSpace.is_open_immersion namespace LocallyRingedSpace.is_open_immersion section pullback variables {X Y Z : LocallyRingedSpace.{u}} (f : X ⟶ Z) (g : Y ⟶ Z) variable [H : LocallyRingedSpace.is_open_immersion f] @[priority 100] instance of_is_iso [is_iso g] : LocallyRingedSpace.is_open_immersion g := @@PresheafedSpace.is_open_immersion.of_is_iso _ g.1 ⟨⟨(inv g).1, by { erw ← LocallyRingedSpace.comp_val, rw is_iso.hom_inv_id, erw ← LocallyRingedSpace.comp_val, rw is_iso.inv_hom_id, split; simpa }⟩⟩ include H instance comp (g : Z ⟶ Y) [LocallyRingedSpace.is_open_immersion g] : LocallyRingedSpace.is_open_immersion (f ≫ g) := PresheafedSpace.is_open_immersion.comp f.1 g.1 instance mono : mono f := LocallyRingedSpace.forget_to_SheafedSpace.mono_of_mono_map (show mono f.1, by apply_instance) instance : SheafedSpace.is_open_immersion (LocallyRingedSpace.forget_to_SheafedSpace.map f) := H /-- An explicit pullback cone over `cospan f g` if `f` is an open immersion. -/ def pullback_cone_of_left : pullback_cone f g := begin refine pullback_cone.mk _ (Y.of_restrict (Top.snd_open_embedding_of_left_open_embedding H.base_open g.1.base)) _, { use PresheafedSpace.is_open_immersion.pullback_cone_of_left_fst f.1 g.1, intro x, have := PresheafedSpace.stalk_map.congr_hom _ _ (PresheafedSpace.is_open_immersion.pullback_cone_of_left_condition f.1 g.1) x, rw [PresheafedSpace.stalk_map.comp, PresheafedSpace.stalk_map.comp] at this, rw ← is_iso.eq_inv_comp at this, rw this, apply_instance }, { exact LocallyRingedSpace.hom.ext _ _ (PresheafedSpace.is_open_immersion.pullback_cone_of_left_condition _ _) }, end instance : LocallyRingedSpace.is_open_immersion (pullback_cone_of_left f g).snd := show PresheafedSpace.is_open_immersion (Y.to_PresheafedSpace.of_restrict _), by apply_instance /-- The constructed `pullback_cone_of_left` is indeed limiting. -/ def pullback_cone_of_left_is_limit : is_limit (pullback_cone_of_left f g) := pullback_cone.is_limit_aux' _ $ λ s, begin use PresheafedSpace.is_open_immersion.pullback_cone_of_left_lift f.1 g.1 (pullback_cone.mk s.fst.1 s.snd.1 (congr_arg LocallyRingedSpace.hom.val s.condition)), { intro x, have := PresheafedSpace.stalk_map.congr_hom _ _ (PresheafedSpace.is_open_immersion.pullback_cone_of_left_lift_snd f.1 g.1 (pullback_cone.mk s.fst.1 s.snd.1 (congr_arg LocallyRingedSpace.hom.val s.condition))) x, change _ = _ ≫ PresheafedSpace.stalk_map s.snd.1 x at this, rw [PresheafedSpace.stalk_map.comp, ← is_iso.eq_inv_comp] at this, rw this, apply_instance }, split, { exact LocallyRingedSpace.hom.ext _ _ (PresheafedSpace.is_open_immersion.pullback_cone_of_left_lift_fst f.1 g.1 _) }, split, { exact LocallyRingedSpace.hom.ext _ _ (PresheafedSpace.is_open_immersion.pullback_cone_of_left_lift_snd f.1 g.1 _) }, intros m h₁ h₂, rw ← cancel_mono (pullback_cone_of_left f g).snd, exact (h₂.trans (LocallyRingedSpace.hom.ext _ _ (PresheafedSpace.is_open_immersion.pullback_cone_of_left_lift_snd f.1 g.1 (pullback_cone.mk s.fst.1 s.snd.1 (congr_arg LocallyRingedSpace.hom.val s.condition))).symm)) end instance has_pullback_of_left : has_pullback f g := ⟨⟨⟨_, pullback_cone_of_left_is_limit f g⟩⟩⟩ instance has_pullback_of_right : has_pullback g f := has_pullback_symmetry f g /-- Open immersions are stable under base-change. -/ instance pullback_snd_of_left : LocallyRingedSpace.is_open_immersion (pullback.snd : pullback f g ⟶ _) := begin delta pullback.snd, rw ← limit.iso_limit_cone_hom_π ⟨_, pullback_cone_of_left_is_limit f g⟩ walking_cospan.right, apply_instance end /-- Open immersions are stable under base-change. -/ instance pullback_fst_of_right : LocallyRingedSpace.is_open_immersion (pullback.fst : pullback g f ⟶ _) := begin rw ← pullback_symmetry_hom_comp_snd, apply_instance end instance pullback_to_base_is_open_immersion [LocallyRingedSpace.is_open_immersion g] : LocallyRingedSpace.is_open_immersion (limit.π (cospan f g) walking_cospan.one) := begin rw [←limit.w (cospan f g) walking_cospan.hom.inl, cospan_map_inl], apply_instance end instance forget_preserves_pullback_of_left : preserves_limit (cospan f g) LocallyRingedSpace.forget_to_SheafedSpace := preserves_limit_of_preserves_limit_cone (pullback_cone_of_left_is_limit f g) begin apply (is_limit_map_cone_pullback_cone_equiv _ _).symm.to_fun, apply is_limit_of_is_limit_pullback_cone_map SheafedSpace.forget_to_PresheafedSpace, exact PresheafedSpace.is_open_immersion.pullback_cone_of_left_is_limit f.1 g.1 end instance forget_to_PresheafedSpace_preserves_pullback_of_left : preserves_limit (cospan f g) (LocallyRingedSpace.forget_to_SheafedSpace ⋙ SheafedSpace.forget_to_PresheafedSpace) := preserves_limit_of_preserves_limit_cone (pullback_cone_of_left_is_limit f g) begin apply (is_limit_map_cone_pullback_cone_equiv _ _).symm.to_fun, exact PresheafedSpace.is_open_immersion.pullback_cone_of_left_is_limit f.1 g.1 end instance forget_to_PresheafedSpace_preserves_open_immersion : PresheafedSpace.is_open_immersion ((LocallyRingedSpace.forget_to_SheafedSpace ⋙ SheafedSpace.forget_to_PresheafedSpace).map f) := H instance forget_to_Top_preserves_pullback_of_left : preserves_limit (cospan f g) (LocallyRingedSpace.forget_to_SheafedSpace ⋙ SheafedSpace.forget _) := begin change preserves_limit _ ((LocallyRingedSpace.forget_to_SheafedSpace ⋙ SheafedSpace.forget_to_PresheafedSpace) ⋙ PresheafedSpace.forget _), apply_with limits.comp_preserves_limit { instances := ff }, apply_instance, apply preserves_limit_of_iso_diagram _ (diagram_iso_cospan.{u} _).symm, dsimp [SheafedSpace.forget_to_PresheafedSpace], apply_instance, end instance forget_reflects_pullback_of_left : reflects_limit (cospan f g) LocallyRingedSpace.forget_to_SheafedSpace := reflects_limit_of_reflects_isomorphisms _ _ instance forget_preserves_pullback_of_right : preserves_limit (cospan g f) LocallyRingedSpace.forget_to_SheafedSpace := preserves_pullback_symmetry _ _ _ instance forget_to_PresheafedSpace_preserves_pullback_of_right : preserves_limit (cospan g f) (LocallyRingedSpace.forget_to_SheafedSpace ⋙ SheafedSpace.forget_to_PresheafedSpace) := preserves_pullback_symmetry _ _ _ instance forget_reflects_pullback_of_right : reflects_limit (cospan g f) LocallyRingedSpace.forget_to_SheafedSpace := reflects_limit_of_reflects_isomorphisms _ _ instance forget_to_PresheafedSpace_reflects_pullback_of_left : reflects_limit (cospan f g) (LocallyRingedSpace.forget_to_SheafedSpace ⋙ SheafedSpace.forget_to_PresheafedSpace) := reflects_limit_of_reflects_isomorphisms _ _ instance forget_to_PresheafedSpace_reflects_pullback_of_right : reflects_limit (cospan g f) (LocallyRingedSpace.forget_to_SheafedSpace ⋙ SheafedSpace.forget_to_PresheafedSpace) := reflects_limit_of_reflects_isomorphisms _ _ lemma pullback_snd_is_iso_of_range_subset (H' : set.range g.1.base ⊆ set.range f.1.base) : is_iso (pullback.snd : pullback f g ⟶ _) := begin apply_with (reflects_isomorphisms.reflects LocallyRingedSpace.forget_to_SheafedSpace) { instances := ff }, apply_with (reflects_isomorphisms.reflects SheafedSpace.forget_to_PresheafedSpace) { instances := ff }, erw ← preserves_pullback.iso_hom_snd (LocallyRingedSpace.forget_to_SheafedSpace ⋙ SheafedSpace.forget_to_PresheafedSpace) f g, haveI := PresheafedSpace.is_open_immersion.pullback_snd_is_iso_of_range_subset _ _ H', apply_instance, apply_instance end /-- The universal property of open immersions: For an open immersion `f : X ⟶ Z`, given any morphism of schemes `g : Y ⟶ Z` whose topological image is contained in the image of `f`, we can lift this morphism to a unique `Y ⟶ X` that commutes with these maps. -/ def lift (H' : set.range g.1.base ⊆ set.range f.1.base) : Y ⟶ X := begin haveI := pullback_snd_is_iso_of_range_subset f g H', exact inv (pullback.snd : pullback f g ⟶ _) ≫ pullback.fst, end @[simp, reassoc] lemma lift_fac (H' : set.range g.1.base ⊆ set.range f.1.base) : lift f g H' ≫ f = g := by { erw category.assoc, rw is_iso.inv_comp_eq, exact pullback.condition } lemma lift_uniq (H' : set.range g.1.base ⊆ set.range f.1.base) (l : Y ⟶ X) (hl : l ≫ f = g) : l = lift f g H' := by rw [← cancel_mono f, hl, lift_fac] lemma lift_range (H' : set.range g.1.base ⊆ set.range f.1.base) : set.range (lift f g H').1.base = f.1.base ⁻¹' (set.range g.1.base) := begin haveI := pullback_snd_is_iso_of_range_subset f g H', dsimp only [lift], have : _ = (pullback.fst : pullback f g ⟶ _).val.base := preserves_pullback.iso_hom_fst (LocallyRingedSpace.forget_to_SheafedSpace ⋙ SheafedSpace.forget _) f g, rw [LocallyRingedSpace.comp_val, SheafedSpace.comp_base, ← this, ← category.assoc, coe_comp], rw [set.range_comp, set.range_iff_surjective.mpr, set.image_univ, Top.pullback_fst_range], ext, split, { rintros ⟨y, eq⟩, exact ⟨y, eq.symm⟩ }, { rintros ⟨y, eq⟩, exact ⟨y, eq.symm⟩ }, { rw ← Top.epi_iff_surjective, rw (show (inv (pullback.snd : pullback f g ⟶ _)).val.base = _, from (LocallyRingedSpace.forget_to_SheafedSpace ⋙ SheafedSpace.forget _).map_inv _), apply_instance } end end pullback /-- An open immersion is isomorphic to the induced open subscheme on its image. -/ def iso_restrict {X Y : LocallyRingedSpace} {f : X ⟶ Y} (H : LocallyRingedSpace.is_open_immersion f) : X ≅ Y.restrict H.base_open := begin apply LocallyRingedSpace.iso_of_SheafedSpace_iso, refine SheafedSpace.forget_to_PresheafedSpace.preimage_iso _, exact H.iso_restrict end /-- To show that a locally ringed space is a scheme, it suffices to show that it has a jointly surjective family of open immersions from affine schemes. -/ protected def Scheme (X : LocallyRingedSpace) (h : ∀ (x : X), ∃ (R : CommRing) (f : Spec.to_LocallyRingedSpace.obj (op R) ⟶ X), (x ∈ set.range f.1.base : _) ∧ LocallyRingedSpace.is_open_immersion f) : Scheme := { to_LocallyRingedSpace := X, local_affine := begin intro x, obtain ⟨R, f, h₁, h₂⟩ := h x, refine ⟨⟨⟨_, h₂.base_open.open_range⟩, h₁⟩, R, ⟨_⟩⟩, apply LocallyRingedSpace.iso_of_SheafedSpace_iso, refine SheafedSpace.forget_to_PresheafedSpace.preimage_iso _, resetI, apply PresheafedSpace.is_open_immersion.iso_of_range_eq (PresheafedSpace.of_restrict _ _) f.1, { exact subtype.range_coe_subtype }, { apply_instance } end } end LocallyRingedSpace.is_open_immersion lemma is_open_immersion.open_range {X Y : Scheme} (f : X ⟶ Y) [H : is_open_immersion f] : is_open (set.range f.1.base) := H.base_open.open_range section open_cover namespace Scheme /-- An open cover of `X` consists of a family of open immersions into `X`, and for each `x : X` an open immersion (indexed by `f x`) that covers `x`. This is merely a coverage in the Zariski pretopology, and it would be optimal if we could reuse the existing API about pretopologies, However, the definitions of sieves and grothendieck topologies uses `Prop`s, so that the actual open sets and immersions are hard to obtain. Also, since such a coverage in the pretopology usually contains a proper class of immersions, it is quite hard to glue them, reason about finite covers, etc. -/ -- TODO: provide API to and from a presieve. structure open_cover (X : Scheme.{u}) := (J : Type v) (obj : Π (j : J), Scheme) (map : Π (j : J), obj j ⟶ X) (f : X.carrier → J) (covers : ∀ x, x ∈ set.range ((map (f x)).1.base)) (is_open : ∀ x, is_open_immersion (map x) . tactic.apply_instance) attribute [instance] open_cover.is_open variables {X Y Z : Scheme.{u}} (𝒰 : open_cover X) (f : X ⟶ Z) (g : Y ⟶ Z) variables [∀ x, has_pullback (𝒰.map x ≫ f) g] /-- The affine cover of a scheme. -/ def affine_cover (X : Scheme) : open_cover X := { J := X.carrier, obj := λ x, Spec.obj $ opposite.op (X.local_affine x).some_spec.some, map := λ x, ((X.local_affine x).some_spec.some_spec.some.inv ≫ X.to_LocallyRingedSpace.of_restrict _ : _), f := λ x, x, is_open := λ x, begin apply_with PresheafedSpace.is_open_immersion.comp { instances := ff }, apply_instance, apply PresheafedSpace.is_open_immersion.of_restrict, end, covers := begin intro x, erw coe_comp, rw [set.range_comp, set.range_iff_surjective.mpr, set.image_univ], erw subtype.range_coe_subtype, exact (X.local_affine x).some.2, rw ← Top.epi_iff_surjective, change epi ((SheafedSpace.forget _).map (LocallyRingedSpace.forget_to_SheafedSpace.map _)), apply_instance end } instance : inhabited X.open_cover := ⟨X.affine_cover⟩ /-- Given an open cover `{ Uᵢ }` of `X`, and for each `Uᵢ` an open cover, we may combine these open covers to form an open cover of `X`. -/ @[simps J obj map] def open_cover.bind (f : Π (x : 𝒰.J), open_cover (𝒰.obj x)) : open_cover X := { J := Σ (i : 𝒰.J), (f i).J, obj := λ x, (f x.1).obj x.2, map := λ x, (f x.1).map x.2 ≫ 𝒰.map x.1, f := λ x, ⟨_, (f _).f (𝒰.covers x).some⟩, covers := λ x, begin let y := (𝒰.covers x).some, have hy : (𝒰.map (𝒰.f x)).val.base y = x := (𝒰.covers x).some_spec, rcases (f (𝒰.f x)).covers y with ⟨z, hz⟩, change x ∈ set.range (((f (𝒰.f x)).map ((f (𝒰.f x)).f y) ≫ 𝒰.map (𝒰.f x)).1.base), use z, erw comp_apply, rw [hz, hy], end } /-- An isomorphism `X ⟶ Y` is an open cover of `Y`. -/ @[simps J obj map] def open_cover_of_is_iso {X Y : Scheme.{u}} (f : X ⟶ Y) [is_iso f] : open_cover Y := { J := punit.{v+1}, obj := λ _, X, map := λ _, f, f := λ _, punit.star, covers := λ x, by { rw set.range_iff_surjective.mpr, { trivial }, rw ← Top.epi_iff_surjective, apply_instance } } /-- We construct an open cover from another, by providing the needed fields and showing that the provided fields are isomorphic with the original open cover. -/ @[simps J obj map] def open_cover.copy {X : Scheme} (𝒰 : open_cover X) (J : Type*) (obj : J → Scheme) (map : ∀ i, obj i ⟶ X) (e₁ : J ≃ 𝒰.J) (e₂ : ∀ i, obj i ≅ 𝒰.obj (e₁ i)) (e₂ : ∀ i, map i = (e₂ i).hom ≫ 𝒰.map (e₁ i)) : open_cover X := { J := J, obj := obj, map := map, f := λ x, e₁.symm (𝒰.f x), covers := λ x, begin rw [e₂, Scheme.comp_val_base, coe_comp, set.range_comp, set.range_iff_surjective.mpr, set.image_univ, e₁.right_inverse_symm], { exact 𝒰.covers x }, { rw ← Top.epi_iff_surjective, apply_instance } end, is_open := λ i, by { rw e₂, apply_instance } } /-- The pushforward of an open cover along an isomorphism. -/ @[simps J obj map] def open_cover.pushforward_iso {X Y : Scheme} (𝒰 : open_cover X) (f : X ⟶ Y) [is_iso f] : open_cover Y := ((open_cover_of_is_iso f).bind (λ _, 𝒰)).copy 𝒰.J _ _ ((equiv.punit_prod _).symm.trans (equiv.sigma_equiv_prod punit 𝒰.J).symm) (λ _, iso.refl _) (λ _, (category.id_comp _).symm) /-- Adding an open immersion into an open cover gives another open cover. -/ @[simps] def open_cover.add {X : Scheme} (𝒰 : X.open_cover) {Y : Scheme} (f : Y ⟶ X) [is_open_immersion f] : X.open_cover := { J := option 𝒰.J, obj := λ i, option.rec Y 𝒰.obj i, map := λ i, option.rec f 𝒰.map i, f := λ x, some (𝒰.f x), covers := 𝒰.covers, is_open := by rintro (_|_); dsimp; apply_instance } -- Related result : `open_cover.pullback_cover`, where we pullback an open cover on `X` along a -- morphism `W ⟶ X`. This is provided at the end of the file since it needs some more results -- about open immersion (which in turn needs the open cover API). local attribute [reducible] CommRing.of CommRing.of_hom instance val_base_is_iso {X Y : Scheme} (f : X ⟶ Y) [is_iso f] : is_iso f.1.base := Scheme.forget_to_Top.map_is_iso f instance basic_open_is_open_immersion {R : CommRing} (f : R) : algebraic_geometry.is_open_immersion (Scheme.Spec.map (CommRing.of_hom (algebra_map R (localization.away f))).op) := begin apply_with SheafedSpace.is_open_immersion.of_stalk_iso { instances := ff }, any_goals { apply_instance }, any_goals { apply_instance }, exact (prime_spectrum.localization_away_open_embedding (localization.away f) f : _), intro x, exact Spec_map_localization_is_iso R (submonoid.powers f) x, end /-- The basic open sets form an affine open cover of `Spec R`. -/ def affine_basis_cover_of_affine (R : CommRing) : open_cover (Spec.obj (opposite.op R)) := { J := R, obj := λ r, Spec.obj (opposite.op $ CommRing.of $ localization.away r), map := λ r, Spec.map (quiver.hom.op (algebra_map R (localization.away r) : _)), f := λ x, 1, covers := λ r, begin rw set.range_iff_surjective.mpr ((Top.epi_iff_surjective _).mp _), { exact trivial }, { apply_instance } end, is_open := λ x, algebraic_geometry.Scheme.basic_open_is_open_immersion x } /-- We may bind the basic open sets of an open affine cover to form a affine cover that is also a basis. -/ def affine_basis_cover (X : Scheme) : open_cover X := X.affine_cover.bind (λ x, affine_basis_cover_of_affine _) /-- The coordinate ring of a component in the `affine_basis_cover`. -/ def affine_basis_cover_ring (X : Scheme) (i : X.affine_basis_cover.J) : CommRing := CommRing.of $ @localization.away (X.local_affine i.1).some_spec.some _ i.2 lemma affine_basis_cover_obj (X : Scheme) (i : X.affine_basis_cover.J) : X.affine_basis_cover.obj i = Spec.obj (op $ X.affine_basis_cover_ring i) := rfl lemma affine_basis_cover_map_range (X : Scheme) (x : X.carrier) (r : (X.local_affine x).some_spec.some) : set.range (X.affine_basis_cover.map ⟨x, r⟩).1.base = (X.affine_cover.map x).1.base '' (prime_spectrum.basic_open r).1 := begin erw [coe_comp, set.range_comp], congr, exact (prime_spectrum.localization_away_comap_range (localization.away r) r : _) end lemma affine_basis_cover_is_basis (X : Scheme) : topological_space.is_topological_basis { x : set X.carrier | ∃ a : X.affine_basis_cover.J, x = set.range ((X.affine_basis_cover.map a).1.base) } := begin apply topological_space.is_topological_basis_of_open_of_nhds, { rintros _ ⟨a, rfl⟩, exact is_open_immersion.open_range (X.affine_basis_cover.map a) }, { rintros a U haU hU, rcases X.affine_cover.covers a with ⟨x, e⟩, let U' := (X.affine_cover.map (X.affine_cover.f a)).1.base ⁻¹' U, have hxU' : x ∈ U' := by { rw ← e at haU, exact haU }, rcases prime_spectrum.is_basis_basic_opens.exists_subset_of_mem_open hxU' ((X.affine_cover.map (X.affine_cover.f a)).1.base.continuous_to_fun.is_open_preimage _ hU) with ⟨_,⟨_,⟨s,rfl⟩,rfl⟩,hxV,hVU⟩, refine ⟨_,⟨⟨_,s⟩,rfl⟩,_,_⟩; erw affine_basis_cover_map_range, { exact ⟨x,hxV,e⟩ }, { rw set.image_subset_iff, exact hVU } } end /-- Every open cover of a quasi-compact scheme can be refined into a finite subcover. -/ @[simps obj map] def open_cover.finite_subcover {X : Scheme} (𝒰 : open_cover X) [H : compact_space X.carrier] : open_cover X := begin have := @@compact_space.elim_nhds_subcover _ H (λ (x : X.carrier), set.range ((𝒰.map (𝒰.f x)).1.base)) (λ x, (is_open_immersion.open_range (𝒰.map (𝒰.f x))).mem_nhds (𝒰.covers x)), let t := this.some, have h : ∀ (x : X.carrier), ∃ (y : t), x ∈ set.range ((𝒰.map (𝒰.f y)).1.base), { intro x, have h' : x ∈ (⊤ : set X.carrier) := trivial, rw [← classical.some_spec this, set.mem_Union] at h', rcases h' with ⟨y,_,⟨hy,rfl⟩,hy'⟩, exact ⟨⟨y,hy⟩,hy'⟩ }, exact { J := t, obj := λ x, 𝒰.obj (𝒰.f x.1), map := λ x, 𝒰.map (𝒰.f x.1), f := λ x, (h x).some, covers := λ x, (h x).some_spec } end instance [H : compact_space X.carrier] : fintype 𝒰.finite_subcover.J := by { delta open_cover.finite_subcover, apply_instance } end Scheme end open_cover namespace PresheafedSpace.is_open_immersion section to_Scheme variables {X : PresheafedSpace.{u} CommRing.{u}} (Y : Scheme.{u}) variables (f : X ⟶ Y.to_PresheafedSpace) [H : PresheafedSpace.is_open_immersion f] include H /-- If `X ⟶ Y` is an open immersion, and `Y` is a scheme, then so is `X`. -/ def to_Scheme : Scheme := begin apply LocallyRingedSpace.is_open_immersion.Scheme (to_LocallyRingedSpace _ f), intro x, obtain ⟨_,⟨i,rfl⟩,hx,hi⟩ := Y.affine_basis_cover_is_basis.exists_subset_of_mem_open (set.mem_range_self x) H.base_open.open_range, use Y.affine_basis_cover_ring i, use LocallyRingedSpace.is_open_immersion.lift (to_LocallyRingedSpace_hom _ f) _ hi, split, { rw LocallyRingedSpace.is_open_immersion.lift_range, exact hx }, { delta LocallyRingedSpace.is_open_immersion.lift, apply_instance } end @[simp] lemma to_Scheme_to_LocallyRingedSpace : (to_Scheme Y f).to_LocallyRingedSpace = (to_LocallyRingedSpace Y.1 f) := rfl /-- If `X ⟶ Y` is an open immersion of PresheafedSpaces, and `Y` is a Scheme, we can upgrade it into a morphism of Schemes. -/ def to_Scheme_hom : to_Scheme Y f ⟶ Y := to_LocallyRingedSpace_hom _ f @[simp] lemma to_Scheme_hom_val : (to_Scheme_hom Y f).val = f := rfl instance to_Scheme_hom_is_open_immersion : is_open_immersion (to_Scheme_hom Y f) := H omit H lemma Scheme_eq_of_LocallyRingedSpace_eq {X Y : Scheme} (H : X.to_LocallyRingedSpace = Y.to_LocallyRingedSpace) : X = Y := by { cases X, cases Y, congr, exact H } lemma Scheme_to_Scheme {X Y : Scheme} (f : X ⟶ Y) [is_open_immersion f] : to_Scheme Y f.1 = X := begin apply Scheme_eq_of_LocallyRingedSpace_eq, exact LocallyRingedSpace_to_LocallyRingedSpace f end end to_Scheme end PresheafedSpace.is_open_immersion /-- The restriction of a Scheme along an open embedding. -/ @[simps] def Scheme.restrict {U : Top} (X : Scheme) {f : U ⟶ Top.of X.carrier} (h : open_embedding f) : Scheme := { to_PresheafedSpace := X.to_PresheafedSpace.restrict h, ..(PresheafedSpace.is_open_immersion.to_Scheme X (X.to_PresheafedSpace.of_restrict h)) } /-- The canonical map from the restriction to the supspace. -/ @[simps] def Scheme.of_restrict {U : Top} (X : Scheme) {f : U ⟶ Top.of X.carrier} (h : open_embedding f) : X.restrict h ⟶ X := X.to_LocallyRingedSpace.of_restrict h instance is_open_immersion.of_restrict {U : Top} (X : Scheme) {f : U ⟶ Top.of X.carrier} (h : open_embedding f) : is_open_immersion (X.of_restrict h) := show PresheafedSpace.is_open_immersion (X.to_PresheafedSpace.of_restrict h), by apply_instance namespace is_open_immersion variables {X Y Z : Scheme.{u}} (f : X ⟶ Z) (g : Y ⟶ Z) variable [H : is_open_immersion f] @[priority 100] instance of_is_iso [is_iso g] : is_open_immersion g := @@LocallyRingedSpace.is_open_immersion.of_is_iso _ (show is_iso ((induced_functor _).map g), by apply_instance) lemma to_iso {X Y : Scheme} (f : X ⟶ Y) [h : is_open_immersion f] [epi f.1.base] : is_iso f := @@is_iso_of_reflects_iso _ _ f (Scheme.forget_to_LocallyRingedSpace ⋙ LocallyRingedSpace.forget_to_SheafedSpace ⋙ SheafedSpace.forget_to_PresheafedSpace) (@@PresheafedSpace.is_open_immersion.to_iso _ f.1 h _) _ lemma of_stalk_iso {X Y : Scheme} (f : X ⟶ Y) (hf : open_embedding f.1.base) [∀ x, is_iso (PresheafedSpace.stalk_map f.1 x)] : is_open_immersion f := SheafedSpace.is_open_immersion.of_stalk_iso f.1 hf lemma iff_stalk_iso {X Y : Scheme} (f : X ⟶ Y) : is_open_immersion f ↔ open_embedding f.1.base ∧ ∀ x, is_iso (PresheafedSpace.stalk_map f.1 x) := ⟨λ H, ⟨H.1, by exactI infer_instance⟩, λ ⟨h₁, h₂⟩, @@is_open_immersion.of_stalk_iso f h₁ h₂⟩ lemma _root_.algebraic_geometry.is_iso_iff_is_open_immersion {X Y : Scheme} (f : X ⟶ Y) : is_iso f ↔ is_open_immersion f ∧ epi f.1.base := ⟨λ H, by exactI ⟨infer_instance, infer_instance⟩, λ ⟨h₁, h₂⟩, @@is_open_immersion.to_iso f h₁ h₂⟩ lemma _root_.algebraic_geometry.is_iso_iff_stalk_iso {X Y : Scheme} (f : X ⟶ Y) : is_iso f ↔ is_iso f.1.base ∧ ∀ x, is_iso (PresheafedSpace.stalk_map f.1 x) := begin rw [is_iso_iff_is_open_immersion, is_open_immersion.iff_stalk_iso, and_comm, ← and_assoc], refine and_congr ⟨_, _⟩ iff.rfl, { rintro ⟨h₁, h₂⟩, convert_to is_iso (Top.iso_of_homeo (homeomorph.homeomorph_of_continuous_open (equiv.of_bijective _ ⟨h₂.inj, (Top.epi_iff_surjective _).mp h₁⟩) h₂.continuous h₂.is_open_map)).hom, { ext, refl }, { apply_instance } }, { intro H, exactI ⟨infer_instance, (Top.homeo_of_iso (as_iso f.1.base)).open_embedding⟩ } end /-- A open immersion induces an isomorphism from the domain onto the image -/ def iso_restrict : X ≅ (Z.restrict H.base_open : _) := ⟨H.iso_restrict.hom, H.iso_restrict.inv, H.iso_restrict.hom_inv_id, H.iso_restrict.inv_hom_id⟩ include H local notation `forget` := Scheme.forget_to_LocallyRingedSpace instance mono : mono f := (induced_functor _).mono_of_mono_map (show @mono LocallyRingedSpace _ _ _ f, by apply_instance) instance forget_map_is_open_immersion : LocallyRingedSpace.is_open_immersion (forget .map f) := ⟨H.base_open, H.c_iso⟩ instance has_limit_cospan_forget_of_left : has_limit (cospan f g ⋙ Scheme.forget_to_LocallyRingedSpace) := begin apply has_limit_of_iso (diagram_iso_cospan.{u} _).symm, change has_limit (cospan (forget .map f) (forget .map g)), apply_instance end open category_theory.limits.walking_cospan instance has_limit_cospan_forget_of_left' : has_limit (cospan ((cospan f g ⋙ forget).map hom.inl) ((cospan f g ⋙ forget).map hom.inr)) := show has_limit (cospan (forget .map f) (forget .map g)), from infer_instance instance has_limit_cospan_forget_of_right : has_limit (cospan g f ⋙ forget) := begin apply has_limit_of_iso (diagram_iso_cospan.{u} _).symm, change has_limit (cospan (forget .map g) (forget .map f)), apply_instance end instance has_limit_cospan_forget_of_right' : has_limit (cospan ((cospan g f ⋙ forget).map hom.inl) ((cospan g f ⋙ forget).map hom.inr)) := show has_limit (cospan (forget .map g) (forget .map f)), from infer_instance instance forget_creates_pullback_of_left : creates_limit (cospan f g) forget := creates_limit_of_fully_faithful_of_iso (PresheafedSpace.is_open_immersion.to_Scheme Y (@pullback.snd LocallyRingedSpace _ _ _ _ f g _).1) (eq_to_iso (by simp) ≪≫ has_limit.iso_of_nat_iso (diagram_iso_cospan _).symm) instance forget_creates_pullback_of_right : creates_limit (cospan g f) forget := creates_limit_of_fully_faithful_of_iso (PresheafedSpace.is_open_immersion.to_Scheme Y (@pullback.fst LocallyRingedSpace _ _ _ _ g f _).1) (eq_to_iso (by simp) ≪≫ has_limit.iso_of_nat_iso (diagram_iso_cospan _).symm) instance forget_preserves_of_left : preserves_limit (cospan f g) forget := category_theory.preserves_limit_of_creates_limit_and_has_limit _ _ instance forget_preserves_of_right : preserves_limit (cospan g f) forget := preserves_pullback_symmetry _ _ _ instance has_pullback_of_left : has_pullback f g := has_limit_of_created (cospan f g) forget instance has_pullback_of_right : has_pullback g f := has_limit_of_created (cospan g f) forget instance pullback_snd_of_left : is_open_immersion (pullback.snd : pullback f g ⟶ _) := begin have := preserves_pullback.iso_hom_snd forget f g, dsimp only [Scheme.forget_to_LocallyRingedSpace, induced_functor_map] at this, rw ← this, change LocallyRingedSpace.is_open_immersion _, apply_instance end instance pullback_fst_of_right : is_open_immersion (pullback.fst : pullback g f ⟶ _) := begin rw ← pullback_symmetry_hom_comp_snd, apply_instance end instance pullback_to_base [is_open_immersion g] : is_open_immersion (limit.π (cospan f g) walking_cospan.one) := begin rw ← limit.w (cospan f g) walking_cospan.hom.inl, change is_open_immersion (_ ≫ f), apply_instance end instance forget_to_Top_preserves_of_left : preserves_limit (cospan f g) Scheme.forget_to_Top := begin apply_with limits.comp_preserves_limit { instances := ff }, apply_instance, apply preserves_limit_of_iso_diagram _ (diagram_iso_cospan.{u} _).symm, dsimp [LocallyRingedSpace.forget_to_Top], apply_instance end instance forget_to_Top_preserves_of_right : preserves_limit (cospan g f) Scheme.forget_to_Top := preserves_pullback_symmetry _ _ _ lemma range_pullback_snd_of_left : set.range (pullback.snd : pullback f g ⟶ Y).1.base = (opens.map g.1.base).obj ⟨set.range f.1.base, H.base_open.open_range⟩ := begin rw [← (show _ = (pullback.snd : pullback f g ⟶ _).1.base, from preserves_pullback.iso_hom_snd Scheme.forget_to_Top f g), coe_comp, set.range_comp, set.range_iff_surjective.mpr, ← @set.preimage_univ _ _ (pullback.fst : pullback f.1.base g.1.base ⟶ _), Top.pullback_snd_image_fst_preimage, set.image_univ], refl, rw ← Top.epi_iff_surjective, apply_instance end lemma range_pullback_fst_of_right : set.range (pullback.fst : pullback g f ⟶ Y).1.base = (opens.map g.1.base).obj ⟨set.range f.1.base, H.base_open.open_range⟩ := begin rw [← (show _ = (pullback.fst : pullback g f ⟶ _).1.base, from preserves_pullback.iso_hom_fst Scheme.forget_to_Top g f), coe_comp, set.range_comp, set.range_iff_surjective.mpr, ← @set.preimage_univ _ _ (pullback.snd : pullback g.1.base f.1.base ⟶ _), Top.pullback_fst_image_snd_preimage, set.image_univ], refl, rw ← Top.epi_iff_surjective, apply_instance end lemma range_pullback_to_base_of_left : set.range (pullback.fst ≫ f : pullback f g ⟶ Z).1.base = set.range f.1.base ∩ set.range g.1.base := begin rw [pullback.condition, Scheme.comp_val_base, coe_comp, set.range_comp, range_pullback_snd_of_left, opens.map_obj, opens.coe_mk, set.image_preimage_eq_inter_range, set.inter_comm], end lemma range_pullback_to_base_of_right : set.range (pullback.fst ≫ g : pullback g f ⟶ Z).1.base = set.range g.1.base ∩ set.range f.1.base := begin rw [Scheme.comp_val_base, coe_comp, set.range_comp, range_pullback_fst_of_right, opens.map_obj, opens.coe_mk, set.image_preimage_eq_inter_range, set.inter_comm], end /-- The universal property of open immersions: For an open immersion `f : X ⟶ Z`, given any morphism of schemes `g : Y ⟶ Z` whose topological image is contained in the image of `f`, we can lift this morphism to a unique `Y ⟶ X` that commutes with these maps. -/ def lift (H' : set.range g.1.base ⊆ set.range f.1.base) : Y ⟶ X := LocallyRingedSpace.is_open_immersion.lift f g H' @[simp, reassoc] lemma lift_fac (H' : set.range g.1.base ⊆ set.range f.1.base) : lift f g H' ≫ f = g := LocallyRingedSpace.is_open_immersion.lift_fac f g H' lemma lift_uniq (H' : set.range g.1.base ⊆ set.range f.1.base) (l : Y ⟶ X) (hl : l ≫ f = g) : l = lift f g H' := LocallyRingedSpace.is_open_immersion.lift_uniq f g H' l hl /-- Two open immersions with equal range are isomorphic. -/ @[simps] def iso_of_range_eq [is_open_immersion g] (e : set.range f.1.base = set.range g.1.base) : X ≅ Y := { hom := lift g f (le_of_eq e), inv := lift f g (le_of_eq e.symm), hom_inv_id' := by { rw ← cancel_mono f, simp }, inv_hom_id' := by { rw ← cancel_mono g, simp } } /-- The functor `opens X ⥤ opens Y` associated with an open immersion `f : X ⟶ Y`. -/ abbreviation _root_.algebraic_geometry.Scheme.hom.opens_functor {X Y : Scheme} (f : X ⟶ Y) [H : is_open_immersion f] : opens X.carrier ⥤ opens Y.carrier := H.open_functor /-- The isomorphism `Γ(X, U) ⟶ Γ(Y, f(U))` induced by an open immersion `f : X ⟶ Y`. -/ def _root_.algebraic_geometry.Scheme.hom.inv_app {X Y : Scheme} (f : X ⟶ Y) [H : is_open_immersion f] (U) : X.presheaf.obj (op U) ⟶ Y.presheaf.obj (op (f.opens_functor.obj U)) := H.inv_app U lemma app_eq_inv_app_app_of_comp_eq_aux {X Y U : Scheme} (f : Y ⟶ U) (g : U ⟶ X) (fg : Y ⟶ X) (H : fg = f ≫ g) [h : is_open_immersion g] (V : opens U.carrier) : (opens.map f.1.base).obj V = (opens.map fg.1.base).obj (g.opens_functor.obj V) := begin subst H, rw [Scheme.comp_val_base, opens.map_comp_obj], congr' 1, ext1, exact (set.preimage_image_eq _ h.base_open.inj).symm end /-- The `fg` argument is to avoid nasty stuff about dependent types. -/ lemma app_eq_inv_app_app_of_comp_eq {X Y U : Scheme} (f : Y ⟶ U) (g : U ⟶ X) (fg : Y ⟶ X) (H : fg = f ≫ g) [h : is_open_immersion g] (V : opens U.carrier) : f.1.c.app (op V) = g.inv_app _ ≫ fg.1.c.app _ ≫ Y.presheaf.map (eq_to_hom $ is_open_immersion.app_eq_inv_app_app_of_comp_eq_aux f g fg H V).op := begin subst H, rw [Scheme.comp_val_c_app, category.assoc, Scheme.hom.inv_app, PresheafedSpace.is_open_immersion.inv_app_app_assoc, f.val.c.naturality_assoc, Top.presheaf.pushforward_obj_map, ← functor.map_comp], convert (category.comp_id _).symm, convert Y.presheaf.map_id _, end lemma lift_app {X Y U : Scheme} (f : U ⟶ Y) (g : X ⟶ Y) [h : is_open_immersion f] (H) (V : opens U.carrier) : (is_open_immersion.lift f g H).1.c.app (op V) = f.inv_app _ ≫ g.1.c.app _ ≫ X.presheaf.map (eq_to_hom $ is_open_immersion.app_eq_inv_app_app_of_comp_eq_aux _ _ _ (is_open_immersion.lift_fac f g H).symm V).op := is_open_immersion.app_eq_inv_app_app_of_comp_eq _ _ _ _ _ end is_open_immersion namespace Scheme lemma image_basic_open {X Y : Scheme} (f : X ⟶ Y) [H : is_open_immersion f] {U : opens X.carrier} (r : X.presheaf.obj (op U)) : f.opens_functor.obj (X.basic_open r) = Y.basic_open (f.inv_app U r) := begin have e := Scheme.preimage_basic_open f (f.inv_app U r), rw [Scheme.hom.inv_app, PresheafedSpace.is_open_immersion.inv_app_app_apply, Scheme.basic_open_res, inf_eq_right.mpr _] at e, rw ← e, ext1, refine set.image_preimage_eq_inter_range.trans _, erw set.inter_eq_left_iff_subset, refine set.subset.trans (Scheme.basic_open_le _ _) (set.image_subset_range _ _), refine le_trans (Scheme.basic_open_le _ _) (le_of_eq _), ext1, exact (set.preimage_image_eq _ H.base_open.inj).symm end /-- The image of an open immersion as an open set. -/ @[simps] def hom.opens_range {X Y : Scheme} (f : X ⟶ Y) [H : is_open_immersion f] : opens Y.carrier := ⟨_, H.base_open.open_range⟩ end Scheme section variable (X : Scheme) /-- The functor taking open subsets of `X` to open subschemes of `X`. -/ @[simps obj_left obj_hom map_left] def Scheme.restrict_functor : opens X.carrier ⥤ over X := { obj := λ U, over.mk (X.of_restrict U.open_embedding), map := λ U V i, over.hom_mk (is_open_immersion.lift (X.of_restrict _) (X.of_restrict _) (by { change set.range coe ⊆ set.range coe, simp_rw [subtype.range_coe], exact i.le })) (is_open_immersion.lift_fac _ _ _), map_id' := λ U, by begin ext1, dsimp only [over.hom_mk_left, over.id_left], rw [← cancel_mono (X.of_restrict U.open_embedding), category.id_comp, is_open_immersion.lift_fac], end, map_comp' := λ U V W i j, begin ext1, dsimp only [over.hom_mk_left, over.comp_left], rw [← cancel_mono (X.of_restrict W.open_embedding), category.assoc], iterate 3 { rw [is_open_immersion.lift_fac] } end } @[reassoc] lemma Scheme.restrict_functor_map_of_restrict {U V : opens X.carrier} (i : U ⟶ V) : (X.restrict_functor.map i).1 ≫ X.of_restrict _ = X.of_restrict _ := is_open_immersion.lift_fac _ _ _ lemma Scheme.restrict_functor_map_base {U V : opens X.carrier} (i : U ⟶ V) : (X.restrict_functor.map i).1.1.base = (opens.to_Top _).map i := begin ext a, exact (congr_arg (λ f : X.restrict U.open_embedding ⟶ X, by exact f.1.base a) (X.restrict_functor_map_of_restrict i) : _), end lemma Scheme.restrict_functor_map_app_aux {U V : opens X.carrier} (i : U ⟶ V) (W : opens V) : U.open_embedding.is_open_map.functor.obj ((opens.map (X.restrict_functor.map i).1.val.base).obj W) ≤ V.open_embedding.is_open_map.functor.obj W := begin simp only [← set_like.coe_subset_coe, is_open_map.functor_obj_coe, set.image_subset_iff, Scheme.restrict_functor_map_base, opens.map_coe, opens.inclusion_apply], rintros _ h, exact ⟨_, h, rfl⟩, end lemma Scheme.restrict_functor_map_app {U V : opens X.carrier} (i : U ⟶ V) (W : opens V) : (X.restrict_functor.map i).1.1.c.app (op W) = X.presheaf.map (hom_of_le $ X.restrict_functor_map_app_aux i W).op := begin have e₁ := Scheme.congr_app (X.restrict_functor_map_of_restrict i) (op $ V.open_embedding.is_open_map.functor.obj W), rw Scheme.comp_val_c_app at e₁, have e₂ := (X.restrict_functor.map i).1.val.c.naturality (eq_to_hom W.map_functor_eq).op, rw ← is_iso.eq_inv_comp at e₂, dsimp at e₁ e₂ ⊢, rw [e₂, W.adjunction_counit_map_functor, ← is_iso.eq_inv_comp, is_iso.inv_comp_eq, ← is_iso.eq_comp_inv] at e₁, simp_rw [eq_to_hom_map (opens.map _), eq_to_hom_map (is_open_map.functor _), ← functor.map_inv, ← functor.map_comp] at e₁, rw e₁, congr' 1, end /-- The functor that restricts to open subschemes and then takes global section is isomorphic to the structure sheaf. -/ @[simps] def Scheme.restrict_functor_Γ : X.restrict_functor.op ⋙ (over.forget X).op ⋙ Scheme.Γ ≅ X.presheaf := nat_iso.of_components (λ U, X.presheaf.map_iso ((eq_to_iso (unop U).open_embedding_obj_top).symm.op : _)) begin intros U V i, dsimp [-subtype.val_eq_coe, -Scheme.restrict_functor_map_left], rw [X.restrict_functor_map_app, ← functor.map_comp, ← functor.map_comp], congr' 1 end end /-- The restriction of an isomorphism onto an open set. -/ noncomputable abbreviation Scheme.restrict_map_iso {X Y : Scheme} (f : X ⟶ Y) [is_iso f] (U : opens Y.carrier) : X.restrict ((opens.map f.1.base).obj U).open_embedding ≅ Y.restrict U.open_embedding := begin refine is_open_immersion.iso_of_range_eq (X.of_restrict _ ≫ f) (Y.of_restrict _) _, dsimp [opens.inclusion], rw [coe_comp, set.range_comp], dsimp, rw [subtype.range_coe, subtype.range_coe], refine @set.image_preimage_eq _ _ f.1.base U.1 _, rw ← Top.epi_iff_surjective, apply_instance end /-- Given an open cover on `X`, we may pull them back along a morphism `W ⟶ X` to obtain an open cover of `W`. -/ @[simps] def Scheme.open_cover.pullback_cover {X : Scheme} (𝒰 : X.open_cover) {W : Scheme} (f : W ⟶ X) : W.open_cover := { J := 𝒰.J, obj := λ x, pullback f (𝒰.map x), map := λ x, pullback.fst, f := λ x, 𝒰.f (f.1.base x), covers := λ x, begin rw ← (show _ = (pullback.fst : pullback f (𝒰.map (𝒰.f (f.1.base x))) ⟶ _).1.base, from preserves_pullback.iso_hom_fst Scheme.forget_to_Top f (𝒰.map (𝒰.f (f.1.base x)))), rw [coe_comp, set.range_comp, set.range_iff_surjective.mpr, set.image_univ, Top.pullback_fst_range], obtain ⟨y, h⟩ := 𝒰.covers (f.1.base x), exact ⟨y, h.symm⟩, { rw ← Top.epi_iff_surjective, apply_instance } end } lemma Scheme.open_cover.Union_range {X : Scheme} (𝒰 : X.open_cover) : (⋃ i, set.range (𝒰.map i).1.base) = set.univ := begin rw set.eq_univ_iff_forall, intros x, rw set.mem_Union, exact ⟨𝒰.f x, 𝒰.covers x⟩ end lemma Scheme.open_cover.supr_opens_range {X : Scheme} (𝒰 : X.open_cover) : (⨆ i, (𝒰.map i).opens_range) = ⊤ := opens.ext $ by { rw opens.coe_supr, exact 𝒰.Union_range } lemma Scheme.open_cover.compact_space {X : Scheme} (𝒰 : X.open_cover) [finite 𝒰.J] [H : ∀ i, compact_space (𝒰.obj i).carrier] : compact_space X.carrier := begin casesI nonempty_fintype 𝒰.J, rw [← is_compact_univ_iff, ← 𝒰.Union_range], apply is_compact_Union, intro i, rw is_compact_iff_compact_space, exact @@homeomorph.compact_space _ _ (H i) (Top.homeo_of_iso (as_iso (is_open_immersion.iso_of_range_eq (𝒰.map i) (X.of_restrict (opens.open_embedding ⟨_, (𝒰.is_open i).base_open.open_range⟩)) subtype.range_coe.symm).hom.1.base)) end /-- Given open covers `{ Uᵢ }` and `{ Uⱼ }`, we may form the open cover `{ Uᵢ ∩ Uⱼ }`. -/ def Scheme.open_cover.inter {X : Scheme.{u}} (𝒰₁ : Scheme.open_cover.{v₁} X) (𝒰₂ : Scheme.open_cover.{v₂} X) : X.open_cover := { J := 𝒰₁.J × 𝒰₂.J, obj := λ ij, pullback (𝒰₁.map ij.1) (𝒰₂.map ij.2), map := λ ij, pullback.fst ≫ 𝒰₁.map ij.1, f := λ x, ⟨𝒰₁.f x, 𝒰₂.f x⟩, covers := λ x, by { rw is_open_immersion.range_pullback_to_base_of_left, exact ⟨𝒰₁.covers x, 𝒰₂.covers x⟩ } } /-- If `U` is a family of open sets that covers `X`, then `X.restrict U` forms an `X.open_cover`. -/ @[simps J obj map] def Scheme.open_cover_of_supr_eq_top {s : Type*} (X : Scheme) (U : s → opens X.carrier) (hU : (⨆ i, U i) = ⊤) : X.open_cover := { J := s, obj := λ i, X.restrict (U i).open_embedding, map := λ i, X.of_restrict (U i).open_embedding, f := λ x, begin have : x ∈ ⨆ i, U i := hU.symm ▸ (show x ∈ (⊤ : opens X.carrier), by triv), exact (opens.mem_supr.mp this).some, end, covers := λ x, begin erw subtype.range_coe, have : x ∈ ⨆ i, U i := hU.symm ▸ (show x ∈ (⊤ : opens X.carrier), by triv), exact (opens.mem_supr.mp this).some_spec, end } section morphism_restrict /-- Given a morphism `f : X ⟶ Y` and an open set `U ⊆ Y`, we have `X ×[Y] U ≅ X |_{f ⁻¹ U}` -/ def pullback_restrict_iso_restrict {X Y : Scheme} (f : X ⟶ Y) (U : opens Y.carrier) : pullback f (Y.of_restrict U.open_embedding) ≅ X.restrict ((opens.map f.1.base).obj U).open_embedding := begin refine is_open_immersion.iso_of_range_eq pullback.fst (X.of_restrict _) _, rw is_open_immersion.range_pullback_fst_of_right, dsimp [opens.inclusion], rw [subtype.range_coe, subtype.range_coe], refl, end @[simp, reassoc] lemma pullback_restrict_iso_restrict_inv_fst {X Y : Scheme} (f : X ⟶ Y) (U : opens Y.carrier) : (pullback_restrict_iso_restrict f U).inv ≫ pullback.fst = X.of_restrict _ := by { delta pullback_restrict_iso_restrict, simp } @[simp, reassoc] lemma pullback_restrict_iso_restrict_hom_restrict {X Y : Scheme} (f : X ⟶ Y) (U : opens Y.carrier) : (pullback_restrict_iso_restrict f U).hom ≫ X.of_restrict _ = pullback.fst := by { delta pullback_restrict_iso_restrict, simp } /-- The restriction of a morphism `X ⟶ Y` onto `X |_{f ⁻¹ U} ⟶ Y |_ U`. -/ def morphism_restrict {X Y : Scheme} (f : X ⟶ Y) (U : opens Y.carrier) : X.restrict ((opens.map f.1.base).obj U).open_embedding ⟶ Y.restrict U.open_embedding := (pullback_restrict_iso_restrict f U).inv ≫ pullback.snd infix ` ∣_ `: 80 := morphism_restrict @[simp, reassoc] lemma pullback_restrict_iso_restrict_hom_morphism_restrict {X Y : Scheme} (f : X ⟶ Y) (U : opens Y.carrier) : (pullback_restrict_iso_restrict f U).hom ≫ f ∣_ U = pullback.snd := iso.hom_inv_id_assoc _ _ @[simp, reassoc] lemma morphism_restrict_ι {X Y : Scheme} (f : X ⟶ Y) (U : opens Y.carrier) : f ∣_ U ≫ Y.of_restrict U.open_embedding = X.of_restrict _ ≫ f := by { delta morphism_restrict, rw [category.assoc, pullback.condition.symm, pullback_restrict_iso_restrict_inv_fst_assoc] } lemma is_pullback_morphism_restrict {X Y : Scheme} (f : X ⟶ Y) (U : opens Y.carrier) : is_pullback (f ∣_ U) (X.of_restrict _) (Y.of_restrict _) f := begin delta morphism_restrict, nth_rewrite 0 ← category.id_comp f, refine (is_pullback.of_horiz_is_iso ⟨_⟩).paste_horiz (is_pullback.of_has_pullback f (Y.of_restrict U.open_embedding)).flip, rw [pullback_restrict_iso_restrict_inv_fst, category.comp_id], end lemma morphism_restrict_comp {X Y Z : Scheme} (f : X ⟶ Y) (g : Y ⟶ Z) (U : opens Z.carrier) : (f ≫ g) ∣_ U = (f ∣_ ((opens.map g.val.base).obj U) ≫ g ∣_ U : _) := begin delta morphism_restrict, rw ← pullback_right_pullback_fst_iso_inv_snd_snd, simp_rw ← category.assoc, congr' 1, rw ← cancel_mono pullback.fst, simp_rw category.assoc, rw [pullback_restrict_iso_restrict_inv_fst, pullback_right_pullback_fst_iso_inv_snd_fst, ← pullback.condition, pullback_restrict_iso_restrict_inv_fst_assoc, pullback_restrict_iso_restrict_inv_fst_assoc], refl, apply_instance end instance {X Y : Scheme} (f : X ⟶ Y) [is_iso f] (U : opens Y.carrier) : is_iso (f ∣_ U) := by { delta morphism_restrict, apply_instance } lemma morphism_restrict_base_coe {X Y : Scheme} (f : X ⟶ Y) (U : opens Y.carrier) (x) : @coe U Y.carrier _ ((f ∣_ U).1.base x) = f.1.base x.1 := congr_arg (λ f, PresheafedSpace.hom.base (LocallyRingedSpace.hom.val f) x) (morphism_restrict_ι f U) lemma morphism_restrict_val_base {X Y : Scheme} (f : X ⟶ Y) (U : opens Y.carrier) : ⇑(f ∣_ U).1.base = U.1.restrict_preimage f.1.base := funext (λ x, subtype.ext (morphism_restrict_base_coe f U x)) lemma image_morphism_restrict_preimage {X Y : Scheme} (f : X ⟶ Y) (U : opens Y.carrier) (V : opens U) : ((opens.map f.val.base).obj U).open_embedding.is_open_map.functor.obj ((opens.map (f ∣_ U).val.base).obj V) = (opens.map f.val.base).obj (U.open_embedding.is_open_map.functor.obj V) := begin ext1, ext x, split, { rintro ⟨⟨x, hx⟩, (hx' : (f ∣_ U).1.base _ ∈ _), rfl⟩, refine ⟨⟨_, hx⟩, _, rfl⟩, convert hx', ext1, exact (morphism_restrict_base_coe f U ⟨x, hx⟩).symm }, { rintro ⟨⟨x, hx⟩, hx', (rfl : x = _)⟩, refine ⟨⟨_, hx⟩, (_: ((f ∣_ U).1.base ⟨x, hx⟩) ∈ V.1), rfl⟩, convert hx', ext1, exact morphism_restrict_base_coe f U ⟨x, hx⟩ } end lemma morphism_restrict_c_app {X Y : Scheme} (f : X ⟶ Y) (U : opens Y.carrier) (V : opens U) : (f ∣_ U).1.c.app (op V) = f.1.c.app (op (U.open_embedding.is_open_map.functor.obj V)) ≫ X.presheaf.map (eq_to_hom (image_morphism_restrict_preimage f U V)).op := begin have := Scheme.congr_app (morphism_restrict_ι f U) (op (U.open_embedding.is_open_map.functor.obj V)), rw [Scheme.comp_val_c_app, Scheme.comp_val_c_app_assoc] at this, have e : (opens.map U.inclusion).obj (U.open_embedding.is_open_map.functor.obj V) = V, { ext1, exact set.preimage_image_eq _ subtype.coe_injective }, have : _ ≫ X.presheaf.map _ = _ := (((f ∣_ U).1.c.naturality (eq_to_hom e).op).symm.trans _).trans this, swap, { change Y.presheaf.map _ ≫ _ = Y.presheaf.map _ ≫ _, congr, }, rw [← is_iso.eq_comp_inv, ← functor.map_inv, category.assoc] at this, rw this, congr' 1, erw [← X.presheaf.map_comp, ← X.presheaf.map_comp], congr' 1, end lemma Γ_map_morphism_restrict {X Y : Scheme} (f : X ⟶ Y) (U : opens Y.carrier) : Scheme.Γ.map (f ∣_ U).op = Y.presheaf.map (eq_to_hom $ U.open_embedding_obj_top.symm).op ≫ f.1.c.app (op U) ≫ X.presheaf.map (eq_to_hom $ ((opens.map f.val.base).obj U).open_embedding_obj_top).op := begin rw [Scheme.Γ_map_op, morphism_restrict_c_app f U ⊤, f.val.c.naturality_assoc], erw ← X.presheaf.map_comp, congr, end /-- Restricting a morphism onto the the image of an open immersion is isomorphic to the base change along the immersion. -/ def morphism_restrict_opens_range {X Y U : Scheme} (f : X ⟶ Y) (g : U ⟶ Y) [hg : is_open_immersion g] : arrow.mk (f ∣_ g.opens_range) ≅ arrow.mk (pullback.snd : pullback f g ⟶ _) := begin let V : opens Y.carrier := g.opens_range, let e := is_open_immersion.iso_of_range_eq g (Y.of_restrict V.open_embedding) (by exact subtype.range_coe.symm), let t : pullback f g ⟶ pullback f (Y.of_restrict V.open_embedding) := pullback.map _ _ _ _ (𝟙 _) e.hom (𝟙 _) (by rw [category.comp_id, category.id_comp]) (by rw [category.comp_id, is_open_immersion.iso_of_range_eq_hom, is_open_immersion.lift_fac]), symmetry, refine arrow.iso_mk (as_iso t ≪≫ pullback_restrict_iso_restrict f V) e _, rw [iso.trans_hom, as_iso_hom, ← iso.comp_inv_eq, ← cancel_mono g, arrow.mk_hom, arrow.mk_hom, is_open_immersion.iso_of_range_eq_inv, category.assoc, category.assoc, category.assoc, is_open_immersion.lift_fac, ← pullback.condition, morphism_restrict_ι, pullback_restrict_iso_restrict_hom_restrict_assoc, pullback.lift_fst_assoc, category.comp_id], end /-- The restrictions onto two equal open sets are isomorphic. This currently has bad defeqs when unfolded, but it should not matter for now. Replace this definition if better defeqs are needed. -/ def morphism_restrict_eq {X Y : Scheme} (f : X ⟶ Y) {U V : opens Y.carrier} (e : U = V) : arrow.mk (f ∣_ U) ≅ arrow.mk (f ∣_ V) := eq_to_iso (by subst e) /-- Restricting a morphism twice is isomorpic to one restriction. -/ def morphism_restrict_restrict {X Y : Scheme} (f : X ⟶ Y) (U : opens Y.carrier) (V : opens U) : arrow.mk (f ∣_ U ∣_ V) ≅ arrow.mk (f ∣_ (U.open_embedding.is_open_map.functor.obj V)) := begin have : (f ∣_ U ∣_ V) ≫ (iso.refl _).hom = (as_iso $ (pullback_restrict_iso_restrict (f ∣_ U) V).inv ≫ (pullback_symmetry _ _).hom ≫ pullback.map _ _ _ _ (𝟙 _) ((pullback_restrict_iso_restrict f U).inv ≫ (pullback_symmetry _ _).hom) (𝟙 _) ((category.comp_id _).trans (category.id_comp _).symm) (by simpa) ≫ (pullback_right_pullback_fst_iso _ _ _).hom ≫ (pullback_symmetry _ _).hom).hom ≫ pullback.snd, { simpa only [category.comp_id, pullback_right_pullback_fst_iso_hom_fst, iso.refl_hom, category.assoc, pullback_symmetry_hom_comp_snd, as_iso_hom, pullback.lift_fst, pullback_symmetry_hom_comp_fst] }, refine arrow.iso_mk' _ _ _ _ this.symm ≪≫ (morphism_restrict_opens_range _ _).symm ≪≫ morphism_restrict_eq _ _, ext1, dsimp, rw [coe_comp, set.range_comp], congr, exact subtype.range_coe, end /-- Restricting a morphism twice onto a basic open set is isomorphic to one restriction. -/ def morphism_restrict_restrict_basic_open {X Y : Scheme} (f : X ⟶ Y) (U : opens Y.carrier) (r : Y.presheaf.obj (op U)) : arrow.mk (f ∣_ U ∣_ (Y.restrict _).basic_open (Y.presheaf.map (eq_to_hom U.open_embedding_obj_top).op r)) ≅ arrow.mk (f ∣_ Y.basic_open r) := begin refine morphism_restrict_restrict _ _ _ ≪≫ morphism_restrict_eq _ _, have e := Scheme.preimage_basic_open (Y.of_restrict U.open_embedding) r, erw [Scheme.of_restrict_val_c_app, opens.adjunction_counit_app_self, eq_to_hom_op] at e, rw [← (Y.restrict U.open_embedding).basic_open_res_eq _ (eq_to_hom U.inclusion_map_eq_top).op, ← comp_apply], erw ← Y.presheaf.map_comp, rw [eq_to_hom_op, eq_to_hom_op, eq_to_hom_map, eq_to_hom_trans], erw ← e, ext1, dsimp [opens.map, opens.inclusion], rw [set.image_preimage_eq_inter_range, set.inter_eq_left_iff_subset, subtype.range_coe], exact Y.basic_open_le r end /-- The stalk map of a restriction of a morphism is isomorphic to the stalk map of the original map. -/ def morphism_restrict_stalk_map {X Y : Scheme} (f : X ⟶ Y) (U : opens Y.carrier) (x) : arrow.mk (PresheafedSpace.stalk_map (f ∣_ U).1 x) ≅ arrow.mk (PresheafedSpace.stalk_map f.1 x.1) := begin fapply arrow.iso_mk', { refine Y.restrict_stalk_iso U.open_embedding ((f ∣_ U).1 x) ≪≫ Top.presheaf.stalk_congr _ _, apply inseparable.of_eq, exact morphism_restrict_base_coe f U x }, { exact X.restrict_stalk_iso _ _ }, { apply Top.presheaf.stalk_hom_ext, intros V hxV, simp only [Top.presheaf.stalk_congr_hom, category_theory.category.assoc, category_theory.iso.trans_hom], erw PresheafedSpace.restrict_stalk_iso_hom_eq_germ_assoc, erw PresheafedSpace.stalk_map_germ_assoc _ _ ⟨_, _⟩, rw [Top.presheaf.germ_stalk_specializes'_assoc], erw PresheafedSpace.stalk_map_germ _ _ ⟨_, _⟩, erw PresheafedSpace.restrict_stalk_iso_hom_eq_germ, rw [morphism_restrict_c_app, category.assoc, Top.presheaf.germ_res], refl } end instance {X Y : Scheme} (f : X ⟶ Y) (U : opens Y.carrier) [is_open_immersion f] : is_open_immersion (f ∣_ U) := by { delta morphism_restrict, apply_instance } end morphism_restrict end algebraic_geometry
f302508fc2c3fb21fb51da869604b86ecaac3546
29cc89d6158dd3b90acbdbcab4d2c7eb9a7dbf0f
/Exercises 7/24_homework_sheet.lean
737b83240fe94554152cabe9c8e5081eecb1a3e7
[]
no_license
KjellZijlemaker/Logical_Verification_VU
ced0ba95316a30e3c94ba8eebd58ea004fa6f53b
4578b93bf1615466996157bb333c84122b201d99
refs/heads/master
1,585,966,086,108
1,549,187,704,000
1,549,187,704,000
155,690,284
0
0
null
null
null
null
UTF-8
Lean
false
false
7,522
lean
/- Homework 2.4: Functional Programming — Metaprogramming -/ open expr open tactic open declaration /- Question 1: A `safe` tactic -/ /- We develop a tactic that applies all safe introduction and elimination rules for the connectives and quantifiers exhaustively. A rule is said to be _safe_ if it always gives rise to provable subgoals. In addition, we will require that safe rules do not introduce metavariables (which can easily be instantiated accidentally with the wrong terms.) We proceed in three steps. -/ /- 1.1. Develop a `safe_intros` tactic that applies the introduction rules for `true', `¬`, `∧`, `↔`, and `→`/`∀`. (Hint: You can use `tactic.intro` or `tactic.intro1` for some of these.) -/ meta def safe_intros : tactic unit := do tactic.intros, repeat (applyc `and.intro), tactic.intro `a2, repeat (applyc `iff.intro) example {a b c d e f : Prop} {p : ℕ → Prop} : a → ¬ b ∧ (c ↔ d) := begin safe_intros, /- The proof state should be roughly as follows: a b c d e f : Prop, p : ℕ → Prop, a_1 : a, a_2 : b ⊢ false a b c d e f : Prop, p : ℕ → Prop, a_1 : a, a_2 : c ⊢ d a b c d e f : Prop, p : ℕ → Prop, a_1 : a, a_2 : d ⊢ c -/ repeat { sorry } end /- 1.2. Develop a `safe_destructs` tactic that eliminates `false`, `∧`, `∨`, `↔`, and `∃`. -/ meta def safe_destructs : tactic unit := (do hs ← local_context, h ← hs.mfirst (λh, do `(false ) ← infer_type h, pure h), cases h, safe_destructs, skip) <|> (do hs ← local_context, h ← hs.mfirst (λh, do `(_ ∧ _) ← infer_type h, pure h), cases h, safe_destructs, skip) <|> (do hs ← local_context, h ← hs.mfirst (λh, do `(_ ∨ _) ← infer_type h, pure h), cases h, safe_destructs, skip) <|> repeat(do hs ← local_context, h ← hs.mfirst (λh, do `(_ ↔ _) ← infer_type h, pure h), cases h, safe_destructs, skip) <|> skip example {a b c d e f : Prop} {p : ℕ → Prop} (hneg: ¬ a) (hand : a ∧ b ∧ c) (hor : c ∨ d) (himp : b → e) (hiff : e ↔ f) (hex : ∃x, p x) : false := begin safe_destructs, /- The proof state should be roughly as follows: 2 goals a b c d e f : Prop, p : ℕ → Prop, hneg : ¬a, himp : b → e, hand_left : a, hor : c, hiff_mp : e → f, hiff_mpr : f → e, hex_w : ℕ, hex_h : p hex_w, hand_right_left : b, hand_right_right : c ⊢ false a b c d e f : Prop, p : ℕ → Prop, hneg : ¬a, himp : b → e, hand_left : a, hor : d, hiff_mp : e → f, hiff_mpr : f → e, hex_w : ℕ, hex_h : p hex_w, hand_right_left : b, hand_right_right : c ⊢ false -/ repeat { sorry } end /- 1.3. Implement a `safe` tactic that first performs introduction, then elimination, and finally proves all the subgoals that can be discharged directly by `assumption`. Hint: The `try` tactic combinator might come in handy. -/ meta def safe : tactic unit := do applyc (`safe_intros), applyc (`safe_destructs), assumption example {a b c d e f : Prop} {p : ℕ → Prop} (hneg: ¬ a) (hand : a ∧ b ∧ c) (hor : c ∨ d) (himp : b → e) (hiff : e ↔ f) (hex : ∃x, p x) : a → ¬ b ∧ (c ↔ d) := begin safe, /- The proof state should be roughly as follows: 3 goals a b c d e f : Prop, p : ℕ → Prop, hneg : ¬a, himp : b → e, a_1 : a, a_2 : b, hand_left : a, hor : c, hiff_mp : e → f, hiff_mpr : f → e, hex_w : ℕ, hex_h : p hex_w, hand_right_left : b, hand_right_right : c ⊢ false a b c d e f : Prop, p : ℕ → Prop, hneg : ¬a, himp : b → e, a_1 : a, a_2 : b, hand_left : a, hor : d, hiff_mp : e → f, hiff_mpr : f → e, hex_w : ℕ, hex_h : p hex_w, hand_right_left : b, hand_right_right : c ⊢ false a b c d e f : Prop, p : ℕ → Prop, hneg : ¬a, himp : b → e, a_1 : a, a_2 : c, hand_left : a, hor : c, hiff_mp : e → f, hiff_mpr : f → e, hex_w : ℕ, hex_h : p hex_w, hand_right_left : b, hand_right_right : c ⊢ d -/ repeat { sorry } end /- Question 2: A `solve_direct` advisor -/ /- It often happens that a user states a new lemma, proves it, and later realizes that the lemma already exists in the library. To help prevent this, we want to implement a `solve_direct` tactic, which goes through all lemmas in the database and checks whether one of them can be used to fully solve the statement. We implement it in steps. -/ /- 2.1. Develop a function that returns `tt` if a `declaration` is a theorem (`declaration.thm`) or an axiom (`declaration.ax`) and `ff` otherwise. -/ meta def is_theorem : declaration → bool | (declaration.thm s us ty body) := tt | (declaration.ax n us ty) := tt | otherwise := ff /- 2.2. Develop a function that returns the list of all theorem names (theorem in the sense of `is_theorem`). Here `get_env` and `environment.fold` are very helpful. See also Question 3 of the exercise. -/ meta def get_all_theorems : tactic (list name) := do env ← get_env, opts ← get_options, environment.fold env [] $ λdecl lst, if term_contains_all ns decl.type then decl.to_name :: lst else lst ) /- 2.3. Develop a tactic that (fully) solves the goal using a theorem `n`. Hints: * `mk_const n` can be used to produce an `expr` (representing the proof) from a name `n` (the theorem name). * `apply` applies an `expr` to the current goal. For speed reasons one might want to add the c onfiguration `apply c { md := transparency.reducible, unify := ff }`, where `c : expr` is the current theorem, and the parameters in `{ ... }` tell `apply` to apply less computational unfolding. * `all_goals` in combination with `assumption` can be used to ensure that the hypothesis from the local context are used to fill in all remaining subgoals. * `done` can be used to check that no subgoal is left. -/ meta def solve (n : name) : tactic unit := sorry /- 2.4. Implement `solve_direct`. Now `solve_direct` should go through `get_all_theorems` and succeed with the first theorem solving the current goal. You can use `list.mfirst` to apply a tactic to each element of a list until one application succeeds. Use `trace` to output the successful theorem to the user. -/ meta def solve_direct : tactic unit := sorry /- 2.5. Develop a version of `solve_direct` that also looks for equalities stated in symmetric form (e.g., if the goal is `l = r` but the theorem is `r = l`). -/ #check eq.symm meta def solve_direct_symm : tactic unit := sorry example {n : ℕ} : n + 0 = n := by solve_direct_symm example {n : ℕ} : n = n + 0 := by solve_direct_symm /- Question 3 **optional**: An `auto` tactic -/ /- 3.1. Develop an Isabelle-style `auto` tactic. This tactic would apply all safe introduction and elimination rules. In addition, it would try unsafe rules (such as `or.intro_left` and `false.elim`) but backtrack at some point (or try several possibilities in parallel). Iterative deepening may be a valid approach, or best-first search, or breadth-first search. The tactic should also attempt to apply assumptions whose conclusion matches the goal, but backtrack if necessary. See also "Automatic Proof and Disproof in Isabelle/HOL" (https://www.cs.vu.nl/~jbe248/frocos2011-dis-proof.pdf) by Blanchette, Bulwahn, and Nipkow, and the references they give. -/ /- 3.2. Test your tactic on some benchmarks. You can try your tactic on logical puzzles of the kinds we proved in exercise 1.3 and howework 1.3. Please include these below. -/
ffa7971152eeac0e332d35518b51c50fe92b4d15
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/multiset/basic.lean
f58ff719fad968dc3f44eb2473941d9f25fba270
[ "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
98,072
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.set.list import data.list.perm /-! # Multisets > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. These are implemented as the quotient of a list by permutations. ## Notation We define the global infix notation `::ₘ` for `multiset.cons`. -/ open list subtype nat variables {α : Type*} {β : Type*} {γ : Type*} /-- `multiset α` is the quotient of `list α` by list permutation. The result is a type of finite sets with duplicates allowed. -/ def {u} multiset (α : Type u) : Type u := quotient (list.is_setoid α) namespace multiset instance : has_coe (list α) (multiset α) := ⟨quot.mk _⟩ @[simp] theorem quot_mk_to_coe (l : list α) : @eq (multiset α) ⟦l⟧ l := rfl @[simp] theorem quot_mk_to_coe' (l : list α) : @eq (multiset α) (quot.mk (≈) l) l := rfl @[simp] theorem quot_mk_to_coe'' (l : list α) : @eq (multiset α) (quot.mk setoid.r l) l := rfl @[simp] theorem coe_eq_coe {l₁ l₂ : list α} : (l₁ : multiset α) = l₂ ↔ l₁ ~ l₂ := quotient.eq instance has_decidable_eq [decidable_eq α] : decidable_eq (multiset α) | s₁ s₂ := quotient.rec_on_subsingleton₂ s₁ s₂ $ λ l₁ l₂, decidable_of_iff' _ quotient.eq /-- defines a size for a multiset by referring to the size of the underlying list -/ protected def sizeof [has_sizeof α] (s : multiset α) : ℕ := quot.lift_on s sizeof $ λ l₁ l₂, perm.sizeof_eq_sizeof instance has_sizeof [has_sizeof α] : has_sizeof (multiset α) := ⟨multiset.sizeof⟩ /-! ### Empty multiset -/ /-- `0 : multiset α` is the empty set -/ protected def zero : multiset α := @nil α instance : has_zero (multiset α) := ⟨multiset.zero⟩ instance : has_emptyc (multiset α) := ⟨0⟩ instance inhabited_multiset : inhabited (multiset α) := ⟨0⟩ @[simp] theorem coe_nil : (@nil α : multiset α) = 0 := rfl @[simp] theorem empty_eq_zero : (∅ : multiset α) = 0 := rfl @[simp] theorem coe_eq_zero (l : list α) : (l : multiset α) = 0 ↔ l = [] := iff.trans coe_eq_coe perm_nil lemma coe_eq_zero_iff_empty (l : list α) : (l : multiset α) = 0 ↔ l.empty := iff.trans (coe_eq_zero l) (empty_iff_eq_nil).symm /-! ### `multiset.cons` -/ /-- `cons a s` is the multiset which contains `s` plus one more instance of `a`. -/ def cons (a : α) (s : multiset α) : multiset α := quot.lift_on s (λ l, (a :: l : multiset α)) (λ l₁ l₂ p, quot.sound (p.cons a)) infixr ` ::ₘ `:67 := multiset.cons instance : has_insert α (multiset α) := ⟨cons⟩ @[simp] theorem insert_eq_cons (a : α) (s : multiset α) : insert a s = a ::ₘ s := rfl @[simp] theorem cons_coe (a : α) (l : list α) : (a ::ₘ l : multiset α) = (a::l : list α) := rfl @[simp] theorem cons_inj_left {a b : α} (s : multiset α) : a ::ₘ s = b ::ₘ s ↔ a = b := ⟨quot.induction_on s $ λ l e, have [a] ++ l ~ [b] ++ l, from quotient.exact e, singleton_perm_singleton.1 $ (perm_append_right_iff _).1 this, congr_arg _⟩ @[simp] theorem cons_inj_right (a : α) : ∀{s t : multiset α}, a ::ₘ s = a ::ₘ t ↔ s = t := by rintros ⟨l₁⟩ ⟨l₂⟩; simp @[recursor 5] protected theorem induction {p : multiset α → Prop} (h₁ : p 0) (h₂ : ∀ ⦃a : α⦄ {s : multiset α}, p s → p (a ::ₘ s)) : ∀s, p s := by rintros ⟨l⟩; induction l with _ _ ih; [exact h₁, exact h₂ ih] @[elab_as_eliminator] protected theorem induction_on {p : multiset α → Prop} (s : multiset α) (h₁ : p 0) (h₂ : ∀ ⦃a : α⦄ {s : multiset α}, p s → p (a ::ₘ s)) : p s := multiset.induction h₁ h₂ s theorem cons_swap (a b : α) (s : multiset α) : a ::ₘ b ::ₘ s = b ::ₘ a ::ₘ s := quot.induction_on s $ λ l, quotient.sound $ perm.swap _ _ _ section rec variables {C : multiset α → Sort*} /-- Dependent recursor on multisets. TODO: should be @[recursor 6], but then the definition of `multiset.pi` fails with a stack overflow in `whnf`. -/ protected def rec (C_0 : C 0) (C_cons : Πa m, C m → C (a ::ₘ m)) (C_cons_heq : ∀ a a' m b, C_cons a (a' ::ₘ m) (C_cons a' m b) == C_cons a' (a ::ₘ m) (C_cons a m b)) (m : multiset α) : C m := quotient.hrec_on m (@list.rec α (λl, C ⟦l⟧) C_0 (λa l b, C_cons a ⟦l⟧ b)) $ assume l l' h, h.rec_heq (assume a l l' b b' hl, have ⟦l⟧ = ⟦l'⟧, from quot.sound hl, by cc) (assume a a' l, C_cons_heq a a' ⟦l⟧) /-- Companion to `multiset.rec` with more convenient argument order. -/ @[elab_as_eliminator] protected def rec_on (m : multiset α) (C_0 : C 0) (C_cons : Πa m, C m → C (a ::ₘ m)) (C_cons_heq : ∀a a' m b, C_cons a (a' ::ₘ m) (C_cons a' m b) == C_cons a' (a ::ₘ m) (C_cons a m b)) : C m := multiset.rec C_0 C_cons C_cons_heq m variables {C_0 : C 0} {C_cons : Πa m, C m → C (a ::ₘ m)} {C_cons_heq : ∀a a' m b, C_cons a (a' ::ₘ m) (C_cons a' m b) == C_cons a' (a ::ₘ m) (C_cons a m b)} @[simp] lemma rec_on_0 : @multiset.rec_on α C (0:multiset α) C_0 C_cons C_cons_heq = C_0 := rfl @[simp] lemma rec_on_cons (a : α) (m : multiset α) : (a ::ₘ m).rec_on C_0 C_cons C_cons_heq = C_cons a m (m.rec_on C_0 C_cons C_cons_heq) := quotient.induction_on m $ assume l, rfl end rec section mem /-- `a ∈ s` means that `a` has nonzero multiplicity in `s`. -/ def mem (a : α) (s : multiset α) : Prop := quot.lift_on s (λ l, a ∈ l) (λ l₁ l₂ (e : l₁ ~ l₂), propext $ e.mem_iff) instance : has_mem α (multiset α) := ⟨mem⟩ @[simp] lemma mem_coe {a : α} {l : list α} : a ∈ (l : multiset α) ↔ a ∈ l := iff.rfl instance decidable_mem [decidable_eq α] (a : α) (s : multiset α) : decidable (a ∈ s) := quot.rec_on_subsingleton s $ list.decidable_mem a @[simp] theorem mem_cons {a b : α} {s : multiset α} : a ∈ b ::ₘ s ↔ a = b ∨ a ∈ s := quot.induction_on s $ λ l, iff.rfl lemma mem_cons_of_mem {a b : α} {s : multiset α} (h : a ∈ s) : a ∈ b ::ₘ s := mem_cons.2 $ or.inr h @[simp] theorem mem_cons_self (a : α) (s : multiset α) : a ∈ a ::ₘ s := mem_cons.2 (or.inl rfl) theorem forall_mem_cons {p : α → Prop} {a : α} {s : multiset α} : (∀ x ∈ (a ::ₘ s), p x) ↔ p a ∧ ∀ x ∈ s, p x := quotient.induction_on' s $ λ L, list.forall_mem_cons theorem exists_cons_of_mem {s : multiset α} {a : α} : a ∈ s → ∃ t, s = a ::ₘ t := quot.induction_on s $ λ l (h : a ∈ l), let ⟨l₁, l₂, e⟩ := mem_split h in e.symm ▸ ⟨(l₁++l₂ : list α), quot.sound perm_middle⟩ @[simp] theorem not_mem_zero (a : α) : a ∉ (0 : multiset α) := id theorem eq_zero_of_forall_not_mem {s : multiset α} : (∀x, x ∉ s) → s = 0 := quot.induction_on s $ λ l H, by rw eq_nil_iff_forall_not_mem.mpr H; refl theorem eq_zero_iff_forall_not_mem {s : multiset α} : s = 0 ↔ ∀ a, a ∉ s := ⟨λ h, h.symm ▸ λ _, not_false, eq_zero_of_forall_not_mem⟩ theorem exists_mem_of_ne_zero {s : multiset α} : s ≠ 0 → ∃ a : α, a ∈ s := quot.induction_on s $ assume l hl, match l, hl with | [] := assume h, false.elim $ h rfl | (a :: l) := assume _, ⟨a, by simp⟩ end lemma empty_or_exists_mem (s : multiset α) : s = 0 ∨ ∃ a, a ∈ s := or_iff_not_imp_left.mpr multiset.exists_mem_of_ne_zero @[simp] lemma zero_ne_cons {a : α} {m : multiset α} : 0 ≠ a ::ₘ m := assume h, have a ∈ (0:multiset α), from h.symm ▸ mem_cons_self _ _, not_mem_zero _ this @[simp] lemma cons_ne_zero {a : α} {m : multiset α} : a ::ₘ m ≠ 0 := zero_ne_cons.symm lemma cons_eq_cons {a b : α} {as bs : multiset α} : a ::ₘ as = b ::ₘ bs ↔ ((a = b ∧ as = bs) ∨ (a ≠ b ∧ ∃cs, as = b ::ₘ cs ∧ bs = a ::ₘ cs)) := begin haveI : decidable_eq α := classical.dec_eq α, split, { assume eq, by_cases a = b, { subst h, simp * at * }, { have : a ∈ b ::ₘ bs, from eq ▸ mem_cons_self _ _, have : a ∈ bs, by simpa [h], rcases exists_cons_of_mem this with ⟨cs, hcs⟩, simp [h, hcs], have : a ::ₘ as = b ::ₘ a ::ₘ cs, by simp [eq, hcs], have : a ::ₘ as = a ::ₘ b ::ₘ cs, by rwa [cons_swap], simpa using this } }, { assume h, rcases h with ⟨eq₁, eq₂⟩ | ⟨h, cs, eq₁, eq₂⟩, { simp * }, { simp [*, cons_swap a b] } } end end mem /-! ### Singleton -/ instance : has_singleton α (multiset α) := ⟨λ a, a ::ₘ 0⟩ instance : is_lawful_singleton α (multiset α) := ⟨λ a, rfl⟩ @[simp] theorem cons_zero (a : α) : a ::ₘ 0 = {a} := rfl @[simp, norm_cast] theorem coe_singleton (a : α) : ([a] : multiset α) = {a} := rfl @[simp] theorem mem_singleton {a b : α} : b ∈ ({a} : multiset α) ↔ b = a := by simp only [←cons_zero, mem_cons, iff_self, or_false, not_mem_zero] theorem mem_singleton_self (a : α) : a ∈ ({a} : multiset α) := by { rw ←cons_zero, exact mem_cons_self _ _ } @[simp] theorem singleton_inj {a b : α} : ({a} : multiset α) = {b} ↔ a = b := by { simp_rw [←cons_zero], exact cons_inj_left _ } @[simp, norm_cast] lemma coe_eq_singleton {l : list α} {a : α} : (l : multiset α) = {a} ↔ l = [a] := by rw [←coe_singleton, coe_eq_coe, list.perm_singleton] @[simp] lemma singleton_eq_cons_iff {a b : α} (m : multiset α) : {a} = b ::ₘ m ↔ a = b ∧ m = 0 := by { rw [←cons_zero, cons_eq_cons], simp [eq_comm] } theorem pair_comm (x y : α) : ({x, y} : multiset α) = {y, x} := cons_swap x y 0 /-! ### `multiset.subset` -/ section subset /-- `s ⊆ t` is the lift of the list subset relation. It means that any element with nonzero multiplicity in `s` has nonzero multiplicity in `t`, but it does not imply that the multiplicity of `a` in `s` is less or equal than in `t`; see `s ≤ t` for this relation. -/ protected def subset (s t : multiset α) : Prop := ∀ ⦃a : α⦄, a ∈ s → a ∈ t instance : has_subset (multiset α) := ⟨multiset.subset⟩ instance : has_ssubset (multiset α) := ⟨λ s t, s ⊆ t ∧ ¬ t ⊆ s⟩ @[simp] theorem coe_subset {l₁ l₂ : list α} : (l₁ : multiset α) ⊆ l₂ ↔ l₁ ⊆ l₂ := iff.rfl @[simp] theorem subset.refl (s : multiset α) : s ⊆ s := λ a h, h theorem subset.trans {s t u : multiset α} : s ⊆ t → t ⊆ u → s ⊆ u := λ h₁ h₂ a m, h₂ (h₁ m) theorem subset_iff {s t : multiset α} : s ⊆ t ↔ (∀⦃x⦄, x ∈ s → x ∈ t) := iff.rfl theorem mem_of_subset {s t : multiset α} {a : α} (h : s ⊆ t) : a ∈ s → a ∈ t := @h _ @[simp] theorem zero_subset (s : multiset α) : 0 ⊆ s := λ a, (not_mem_nil a).elim lemma subset_cons (s : multiset α) (a : α) : s ⊆ a ::ₘ s := λ _, mem_cons_of_mem lemma ssubset_cons {s : multiset α} {a : α} (ha : a ∉ s) : s ⊂ a ::ₘ s := ⟨subset_cons _ _, λ h, ha $ h $ mem_cons_self _ _⟩ @[simp] theorem cons_subset {a : α} {s t : multiset α} : (a ::ₘ s) ⊆ t ↔ a ∈ t ∧ s ⊆ t := by simp [subset_iff, or_imp_distrib, forall_and_distrib] lemma cons_subset_cons {a : α} {s t : multiset α} : s ⊆ t → a ::ₘ s ⊆ a ::ₘ t := quotient.induction_on₂ s t $ λ _ _, cons_subset_cons _ theorem eq_zero_of_subset_zero {s : multiset α} (h : s ⊆ 0) : s = 0 := eq_zero_of_forall_not_mem h theorem subset_zero {s : multiset α} : s ⊆ 0 ↔ s = 0 := ⟨eq_zero_of_subset_zero, λ xeq, xeq.symm ▸ subset.refl 0⟩ lemma induction_on' {p : multiset α → Prop} (S : multiset α) (h₁ : p 0) (h₂ : ∀ {a s}, a ∈ S → s ⊆ S → p s → p (insert a s)) : p S := @multiset.induction_on α (λ T, T ⊆ S → p T) S (λ _, h₁) (λ a s hps hs, let ⟨hS, sS⟩ := cons_subset.1 hs in h₂ hS sS (hps sS)) (subset.refl S) end subset /-! ### `multiset.to_list` -/ section to_list /-- Produces a list of the elements in the multiset using choice. -/ noncomputable def to_list (s : multiset α) := s.out' @[simp, norm_cast] lemma coe_to_list (s : multiset α) : (s.to_list : multiset α) = s := s.out_eq' @[simp] lemma to_list_eq_nil {s : multiset α} : s.to_list = [] ↔ s = 0 := by rw [← coe_eq_zero, coe_to_list] @[simp] lemma empty_to_list {s : multiset α} : s.to_list.empty ↔ s = 0 := empty_iff_eq_nil.trans to_list_eq_nil @[simp] lemma to_list_zero : (multiset.to_list 0 : list α) = [] := to_list_eq_nil.mpr rfl @[simp] lemma mem_to_list {a : α} {s : multiset α} : a ∈ s.to_list ↔ a ∈ s := by rw [← mem_coe, coe_to_list] @[simp] lemma to_list_eq_singleton_iff {a : α} {m : multiset α} : m.to_list = [a] ↔ m = {a} := by rw [←perm_singleton, ←coe_eq_coe, coe_to_list, coe_singleton] @[simp] lemma to_list_singleton (a : α) : ({a} : multiset α).to_list = [a] := multiset.to_list_eq_singleton_iff.2 rfl end to_list /-! ### Partial order on `multiset`s -/ /-- `s ≤ t` means that `s` is a sublist of `t` (up to permutation). Equivalently, `s ≤ t` means that `count a s ≤ count a t` for all `a`. -/ protected def le (s t : multiset α) : Prop := quotient.lift_on₂ s t (<+~) $ λ v₁ v₂ w₁ w₂ p₁ p₂, propext (p₂.subperm_left.trans p₁.subperm_right) instance : partial_order (multiset α) := { le := multiset.le, le_refl := by rintros ⟨l⟩; exact subperm.refl _, le_trans := by rintros ⟨l₁⟩ ⟨l₂⟩ ⟨l₃⟩; exact @subperm.trans _ _ _ _, le_antisymm := by rintros ⟨l₁⟩ ⟨l₂⟩ h₁ h₂; exact quot.sound (subperm.antisymm h₁ h₂) } instance decidable_le [decidable_eq α] : decidable_rel ((≤) : multiset α → multiset α → Prop) := λ s t, quotient.rec_on_subsingleton₂ s t list.decidable_subperm section variables {s t : multiset α} {a : α} lemma subset_of_le : s ≤ t → s ⊆ t := quotient.induction_on₂ s t $ λ l₁ l₂, subperm.subset alias subset_of_le ← le.subset lemma mem_of_le (h : s ≤ t) : a ∈ s → a ∈ t := mem_of_subset (subset_of_le h) lemma not_mem_mono (h : s ⊆ t) : a ∉ t → a ∉ s := mt $ @h _ @[simp] theorem coe_le {l₁ l₂ : list α} : (l₁ : multiset α) ≤ l₂ ↔ l₁ <+~ l₂ := iff.rfl @[elab_as_eliminator] theorem le_induction_on {C : multiset α → multiset α → Prop} {s t : multiset α} (h : s ≤ t) (H : ∀ {l₁ l₂ : list α}, l₁ <+ l₂ → C l₁ l₂) : C s t := quotient.induction_on₂ s t (λ l₁ l₂ ⟨l, p, s⟩, (show ⟦l⟧ = ⟦l₁⟧, from quot.sound p) ▸ H s) h theorem zero_le (s : multiset α) : 0 ≤ s := quot.induction_on s $ λ l, (nil_sublist l).subperm instance : order_bot (multiset α) := ⟨0, zero_le⟩ /-- This is a `rfl` and `simp` version of `bot_eq_zero`. -/ @[simp] theorem bot_eq_zero : (⊥ : multiset α) = 0 := rfl lemma le_zero : s ≤ 0 ↔ s = 0 := le_bot_iff theorem lt_cons_self (s : multiset α) (a : α) : s < a ::ₘ s := quot.induction_on s $ λ l, suffices l <+~ a :: l ∧ (¬l ~ a :: l), by simpa [lt_iff_le_and_ne], ⟨(sublist_cons _ _).subperm, λ p, ne_of_lt (lt_succ_self (length l)) p.length_eq⟩ theorem le_cons_self (s : multiset α) (a : α) : s ≤ a ::ₘ s := le_of_lt $ lt_cons_self _ _ lemma cons_le_cons_iff (a : α) : a ::ₘ s ≤ a ::ₘ t ↔ s ≤ t := quotient.induction_on₂ s t $ λ l₁ l₂, subperm_cons a lemma cons_le_cons (a : α) : s ≤ t → a ::ₘ s ≤ a ::ₘ t := (cons_le_cons_iff a).2 lemma le_cons_of_not_mem (m : a ∉ s) : s ≤ a ::ₘ t ↔ s ≤ t := begin refine ⟨_, λ h, le_trans h $ le_cons_self _ _⟩, suffices : ∀ {t'} (_ : s ≤ t') (_ : a ∈ t'), a ::ₘ s ≤ t', { exact λ h, (cons_le_cons_iff a).1 (this h (mem_cons_self _ _)) }, introv h, revert m, refine le_induction_on h _, introv s m₁ m₂, rcases mem_split m₂ with ⟨r₁, r₂, rfl⟩, exact perm_middle.subperm_left.2 ((subperm_cons _).2 $ ((sublist_or_mem_of_sublist s).resolve_right m₁).subperm) end @[simp] theorem singleton_ne_zero (a : α) : ({a} : multiset α) ≠ 0 := ne_of_gt (lt_cons_self _ _) @[simp] theorem singleton_le {a : α} {s : multiset α} : {a} ≤ s ↔ a ∈ s := ⟨λ h, mem_of_le h (mem_singleton_self _), λ h, let ⟨t, e⟩ := exists_cons_of_mem h in e.symm ▸ cons_le_cons _ (zero_le _)⟩ end /-! ### Additive monoid -/ /-- The sum of two multisets is the lift of the list append operation. This adds the multiplicities of each element, i.e. `count a (s + t) = count a s + count a t`. -/ protected def add (s₁ s₂ : multiset α) : multiset α := quotient.lift_on₂ s₁ s₂ (λ l₁ l₂, ((l₁ ++ l₂ : list α) : multiset α)) $ λ v₁ v₂ w₁ w₂ p₁ p₂, quot.sound $ p₁.append p₂ instance : has_add (multiset α) := ⟨multiset.add⟩ @[simp] theorem coe_add (s t : list α) : (s + t : multiset α) = (s ++ t : list α) := rfl @[simp] theorem singleton_add (a : α) (s : multiset α) : {a} + s = a ::ₘ s := rfl private theorem add_le_add_iff_left' {s t u : multiset α} : s + t ≤ s + u ↔ t ≤ u := quotient.induction_on₃ s t u $ λ l₁ l₂ l₃, subperm_append_left _ instance : covariant_class (multiset α) (multiset α) (+) (≤) := ⟨λ s t u, add_le_add_iff_left'.2⟩ instance : contravariant_class (multiset α) (multiset α) (+) (≤) := ⟨λ s t u, add_le_add_iff_left'.1⟩ instance : ordered_cancel_add_comm_monoid (multiset α) := { zero := 0, add := (+), add_comm := λ s t, quotient.induction_on₂ s t $ λ l₁ l₂, quot.sound perm_append_comm, add_assoc := λ s₁ s₂ s₃, quotient.induction_on₃ s₁ s₂ s₃ $ λ l₁ l₂ l₃, congr_arg coe $ append_assoc l₁ l₂ l₃, zero_add := λ s, quot.induction_on s $ λ l, rfl, add_zero := λ s, quotient.induction_on s $ λ l, congr_arg coe $ append_nil l, add_le_add_left := λ s₁ s₂, add_le_add_left, le_of_add_le_add_left := λ s₁ s₂ s₃, le_of_add_le_add_left, ..@multiset.partial_order α } theorem le_add_right (s t : multiset α) : s ≤ s + t := by simpa using add_le_add_left (zero_le t) s theorem le_add_left (s t : multiset α) : s ≤ t + s := by simpa using add_le_add_right (zero_le t) s theorem le_iff_exists_add {s t : multiset α} : s ≤ t ↔ ∃ u, t = s + u := ⟨λ h, le_induction_on h $ λ l₁ l₂ s, let ⟨l, p⟩ := s.exists_perm_append in ⟨l, quot.sound p⟩, λ ⟨u, e⟩, e.symm ▸ le_add_right _ _⟩ instance : canonically_ordered_add_monoid (multiset α) := { le_self_add := le_add_right, exists_add_of_le := λ a b h, le_induction_on h $ λ l₁ l₂ s, let ⟨l, p⟩ := s.exists_perm_append in ⟨l, quot.sound p⟩, ..multiset.order_bot, ..multiset.ordered_cancel_add_comm_monoid } @[simp] theorem cons_add (a : α) (s t : multiset α) : a ::ₘ s + t = a ::ₘ (s + t) := by rw [← singleton_add, ← singleton_add, add_assoc] @[simp] theorem add_cons (a : α) (s t : multiset α) : s + a ::ₘ t = a ::ₘ (s + t) := by rw [add_comm, cons_add, add_comm] @[simp] theorem mem_add {a : α} {s t : multiset α} : a ∈ s + t ↔ a ∈ s ∨ a ∈ t := quotient.induction_on₂ s t $ λ l₁ l₂, mem_append lemma mem_of_mem_nsmul {a : α} {s : multiset α} {n : ℕ} (h : a ∈ n • s) : a ∈ s := begin induction n with n ih, { rw zero_nsmul at h, exact absurd h (not_mem_zero _) }, { rw [succ_nsmul, mem_add] at h, exact h.elim id ih }, end @[simp] lemma mem_nsmul {a : α} {s : multiset α} {n : ℕ} (h0 : n ≠ 0) : a ∈ n • s ↔ a ∈ s := begin refine ⟨mem_of_mem_nsmul, λ h, _⟩, obtain ⟨n, rfl⟩ := exists_eq_succ_of_ne_zero h0, rw [succ_nsmul, mem_add], exact or.inl h end lemma nsmul_cons {s : multiset α} (n : ℕ) (a : α) : n • (a ::ₘ s) = n • {a} + n • s := by rw [←singleton_add, nsmul_add] /-! ### Cardinality -/ /-- The cardinality of a multiset is the sum of the multiplicities of all its elements, or simply the length of the underlying list. -/ def card : multiset α →+ ℕ := { to_fun := λ s, quot.lift_on s length $ λ l₁ l₂, perm.length_eq, map_zero' := rfl, map_add' := λ s t, quotient.induction_on₂ s t length_append } @[simp] theorem coe_card (l : list α) : card (l : multiset α) = length l := rfl @[simp] theorem length_to_list (s : multiset α) : s.to_list.length = s.card := by rw [← coe_card, coe_to_list] @[simp] theorem card_zero : @card α 0 = 0 := rfl theorem card_add (s t : multiset α) : card (s + t) = card s + card t := card.map_add s t lemma card_nsmul (s : multiset α) (n : ℕ) : (n • s).card = n * s.card := by rw [card.map_nsmul s n, nat.nsmul_eq_mul] @[simp] theorem card_cons (a : α) (s : multiset α) : card (a ::ₘ s) = card s + 1 := quot.induction_on s $ λ l, rfl @[simp] theorem card_singleton (a : α) : card ({a} : multiset α) = 1 := by simp only [←cons_zero, card_zero, eq_self_iff_true, zero_add, card_cons] lemma card_pair (a b : α) : ({a, b} : multiset α).card = 2 := by rw [insert_eq_cons, card_cons, card_singleton] theorem card_eq_one {s : multiset α} : card s = 1 ↔ ∃ a, s = {a} := ⟨quot.induction_on s $ λ l h, (list.length_eq_one.1 h).imp $ λ a, congr_arg coe, λ ⟨a, e⟩, e.symm ▸ rfl⟩ theorem card_le_of_le {s t : multiset α} (h : s ≤ t) : card s ≤ card t := le_induction_on h $ λ l₁ l₂, sublist.length_le @[mono] theorem card_mono : monotone (@card α) := λ a b, card_le_of_le theorem eq_of_le_of_card_le {s t : multiset α} (h : s ≤ t) : card t ≤ card s → s = t := le_induction_on h $ λ l₁ l₂ s h₂, congr_arg coe $ s.eq_of_length_le h₂ theorem card_lt_of_lt {s t : multiset α} (h : s < t) : card s < card t := lt_of_not_ge $ λ h₂, ne_of_lt h $ eq_of_le_of_card_le (le_of_lt h) h₂ theorem lt_iff_cons_le {s t : multiset α} : s < t ↔ ∃ a, a ::ₘ s ≤ t := ⟨quotient.induction_on₂ s t $ λ l₁ l₂ h, subperm.exists_of_length_lt (le_of_lt h) (card_lt_of_lt h), λ ⟨a, h⟩, lt_of_lt_of_le (lt_cons_self _ _) h⟩ @[simp] theorem card_eq_zero {s : multiset α} : card s = 0 ↔ s = 0 := ⟨λ h, (eq_of_le_of_card_le (zero_le _) (le_of_eq h)).symm, λ e, by simp [e]⟩ theorem card_pos {s : multiset α} : 0 < card s ↔ s ≠ 0 := pos_iff_ne_zero.trans $ not_congr card_eq_zero theorem card_pos_iff_exists_mem {s : multiset α} : 0 < card s ↔ ∃ a, a ∈ s := quot.induction_on s $ λ l, length_pos_iff_exists_mem lemma card_eq_two {s : multiset α} : s.card = 2 ↔ ∃ x y, s = {x, y} := ⟨quot.induction_on s (λ l h, (list.length_eq_two.mp h).imp (λ a, Exists.imp (λ b, congr_arg coe))), λ ⟨a, b, e⟩, e.symm ▸ rfl⟩ lemma card_eq_three {s : multiset α} : s.card = 3 ↔ ∃ x y z, s = {x, y, z} := ⟨quot.induction_on s (λ l h, (list.length_eq_three.mp h).imp (λ a, Exists.imp (λ b, Exists.imp (λ c, congr_arg coe)))), λ ⟨a, b, c, e⟩, e.symm ▸ rfl⟩ /-! ### Induction principles -/ /-- A strong induction principle for multisets: If you construct a value for a particular multiset given values for all strictly smaller multisets, you can construct a value for any multiset. -/ @[elab_as_eliminator] def strong_induction_on {p : multiset α → Sort*} : ∀ (s : multiset α), (∀ s, (∀t < s, p t) → p s) → p s | s := λ ih, ih s $ λ t h, have card t < card s, from card_lt_of_lt h, strong_induction_on t ih using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf card⟩]} theorem strong_induction_eq {p : multiset α → Sort*} (s : multiset α) (H) : @strong_induction_on _ p s H = H s (λ t h, @strong_induction_on _ p t H) := by rw [strong_induction_on] @[elab_as_eliminator] lemma case_strong_induction_on {p : multiset α → Prop} (s : multiset α) (h₀ : p 0) (h₁ : ∀ a s, (∀t ≤ s, p t) → p (a ::ₘ s)) : p s := multiset.strong_induction_on s $ assume s, multiset.induction_on s (λ _, h₀) $ λ a s _ ih, h₁ _ _ $ λ t h, ih _ $ lt_of_le_of_lt h $ lt_cons_self _ _ /-- Suppose that, given that `p t` can be defined on all supersets of `s` of cardinality less than `n`, one knows how to define `p s`. Then one can inductively define `p s` for all multisets `s` of cardinality less than `n`, starting from multisets of card `n` and iterating. This can be used either to define data, or to prove properties. -/ def strong_downward_induction {p : multiset α → Sort*} {n : ℕ} (H : ∀ t₁, (∀ {t₂ : multiset α}, t₂.card ≤ n → t₁ < t₂ → p t₂) → t₁.card ≤ n → p t₁) : ∀ (s : multiset α), s.card ≤ n → p s | s := H s (λ t ht h, have n - card t < n - card s, from (tsub_lt_tsub_iff_left_of_le ht).2 (card_lt_of_lt h), strong_downward_induction t ht) using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ (t : multiset α), n - t.card)⟩]} lemma strong_downward_induction_eq {p : multiset α → Sort*} {n : ℕ} (H : ∀ t₁, (∀ {t₂ : multiset α}, t₂.card ≤ n → t₁ < t₂ → p t₂) → t₁.card ≤ n → p t₁) (s : multiset α) : strong_downward_induction H s = H s (λ t ht hst, strong_downward_induction H t ht) := by rw strong_downward_induction /-- Analogue of `strong_downward_induction` with order of arguments swapped. -/ @[elab_as_eliminator] def strong_downward_induction_on {p : multiset α → Sort*} {n : ℕ} : ∀ (s : multiset α), (∀ t₁, (∀ {t₂ : multiset α}, t₂.card ≤ n → t₁ < t₂ → p t₂) → t₁.card ≤ n → p t₁) → s.card ≤ n → p s := λ s H, strong_downward_induction H s lemma strong_downward_induction_on_eq {p : multiset α → Sort*} (s : multiset α) {n : ℕ} (H : ∀ t₁, (∀ {t₂ : multiset α}, t₂.card ≤ n → t₁ < t₂ → p t₂) → t₁.card ≤ n → p t₁) : s.strong_downward_induction_on H = H s (λ t ht h, t.strong_downward_induction_on H ht) := by { dunfold strong_downward_induction_on, rw strong_downward_induction } /-- Another way of expressing `strong_induction_on`: the `(<)` relation is well-founded. -/ lemma well_founded_lt : well_founded ((<) : multiset α → multiset α → Prop) := subrelation.wf (λ _ _, multiset.card_lt_of_lt) (measure_wf multiset.card) instance is_well_founded_lt : _root_.well_founded_lt (multiset α) := ⟨well_founded_lt⟩ /-! ### `multiset.replicate` -/ /-- `replicate n a` is the multiset containing only `a` with multiplicity `n`. -/ def replicate (n : ℕ) (a : α) : multiset α := replicate n a lemma coe_replicate (n : ℕ) (a : α) : (list.replicate n a : multiset α) = replicate n a := rfl @[simp] lemma replicate_zero (a : α) : replicate 0 a = 0 := rfl @[simp] lemma replicate_succ (a : α) (n) : replicate (n + 1) a = a ::ₘ replicate n a := rfl lemma replicate_add (m n : ℕ) (a : α) : replicate (m + n) a = replicate m a + replicate n a := congr_arg _ $ list.replicate_add _ _ _ /-- `multiset.replicate` as an `add_monoid_hom`. -/ @[simps] def replicate_add_monoid_hom (a : α) : ℕ →+ multiset α := { to_fun := λ n, replicate n a, map_zero' := replicate_zero a, map_add' := λ _ _, replicate_add _ _ a } lemma replicate_one (a : α) : replicate 1 a = {a} := rfl @[simp] lemma card_replicate : ∀ n (a : α), card (replicate n a) = n := length_replicate lemma mem_replicate {a b : α} {n : ℕ} : b ∈ replicate n a ↔ n ≠ 0 ∧ b = a := mem_replicate theorem eq_of_mem_replicate {a b : α} {n} : b ∈ replicate n a → b = a := eq_of_mem_replicate theorem eq_replicate_card {a : α} {s : multiset α} : s = replicate s.card a ↔ ∀ b ∈ s, b = a := quot.induction_on s $ λ l, coe_eq_coe.trans $ perm_replicate.trans eq_replicate_length alias eq_replicate_card ↔ _ eq_replicate_of_mem theorem eq_replicate {a : α} {n} {s : multiset α} : s = replicate n a ↔ card s = n ∧ ∀ b ∈ s, b = a := ⟨λ h, h.symm ▸ ⟨card_replicate _ _, λ b, eq_of_mem_replicate⟩, λ ⟨e, al⟩, e ▸ eq_replicate_of_mem al⟩ lemma replicate_right_injective {n : ℕ} (hn : n ≠ 0) : function.injective (replicate n : α → multiset α) := λ a b h, (eq_replicate.1 h).2 _ $ mem_replicate.2 ⟨hn, rfl⟩ @[simp] lemma replicate_right_inj {a b : α} {n : ℕ} (h : n ≠ 0) : replicate n a = replicate n b ↔ a = b := (replicate_right_injective h).eq_iff theorem replicate_left_injective (a : α) : function.injective (λ n, replicate n a) := λ m n h, by rw [← (eq_replicate.1 h).1, card_replicate] theorem replicate_subset_singleton : ∀ n (a : α), replicate n a ⊆ {a} := replicate_subset_singleton theorem replicate_le_coe {a : α} {n} {l : list α} : replicate n a ≤ l ↔ list.replicate n a <+ l := ⟨λ ⟨l', p, s⟩, (perm_replicate.1 p) ▸ s, sublist.subperm⟩ theorem nsmul_singleton (a : α) (n) : n • ({a} : multiset α) = replicate n a := begin refine eq_replicate.mpr ⟨_, λ b hb, mem_singleton.mp (mem_of_mem_nsmul hb)⟩, rw [card_nsmul, card_singleton, mul_one] end lemma nsmul_replicate {a : α} (n m : ℕ) : n • replicate m a = replicate (n * m) a := ((replicate_add_monoid_hom a).map_nsmul _ _).symm lemma replicate_le_replicate (a : α) {k n : ℕ} : replicate k a ≤ replicate n a ↔ k ≤ n := replicate_le_coe.trans $ list.replicate_sublist_replicate _ lemma le_replicate_iff {m : multiset α} {a : α} {n : ℕ} : m ≤ replicate n a ↔ ∃ (k ≤ n), m = replicate k a := ⟨λ h, ⟨m.card, (card_mono h).trans_eq (card_replicate _ _), eq_replicate_card.2 $ λ b hb, eq_of_mem_replicate $ subset_of_le h hb⟩, λ ⟨k, hkn, hm⟩, hm.symm ▸ (replicate_le_replicate _).2 hkn⟩ lemma lt_replicate_succ {m : multiset α} {x : α} {n : ℕ} : m < replicate (n + 1) x ↔ m ≤ replicate n x := begin rw lt_iff_cons_le, split, { rintros ⟨x', hx'⟩, have := eq_of_mem_replicate (mem_of_le hx' (mem_cons_self _ _)), rwa [this, replicate_succ, cons_le_cons_iff] at hx' }, { intro h, rw replicate_succ, exact ⟨x, cons_le_cons _ h⟩ } end /-! ### Erasing one copy of an element -/ section erase variables [decidable_eq α] {s t : multiset α} {a b : α} /-- `erase s a` is the multiset that subtracts 1 from the multiplicity of `a`. -/ def erase (s : multiset α) (a : α) : multiset α := quot.lift_on s (λ l, (l.erase a : multiset α)) (λ l₁ l₂ p, quot.sound (p.erase a)) @[simp] theorem coe_erase (l : list α) (a : α) : erase (l : multiset α) a = l.erase a := rfl @[simp] theorem erase_zero (a : α) : (0 : multiset α).erase a = 0 := rfl @[simp] theorem erase_cons_head (a : α) (s : multiset α) : (a ::ₘ s).erase a = s := quot.induction_on s $ λ l, congr_arg coe $ erase_cons_head a l @[simp, priority 990] theorem erase_cons_tail {a b : α} (s : multiset α) (h : b ≠ a) : (b ::ₘ s).erase a = b ::ₘ s.erase a := quot.induction_on s $ λ l, congr_arg coe $ erase_cons_tail l h @[simp] theorem erase_singleton (a : α) : ({a} : multiset α).erase a = 0 := erase_cons_head a 0 @[simp, priority 980] theorem erase_of_not_mem {a : α} {s : multiset α} : a ∉ s → s.erase a = s := quot.induction_on s $ λ l h, congr_arg coe $ erase_of_not_mem h @[simp, priority 980] theorem cons_erase {s : multiset α} {a : α} : a ∈ s → a ::ₘ s.erase a = s := quot.induction_on s $ λ l h, quot.sound (perm_cons_erase h).symm theorem le_cons_erase (s : multiset α) (a : α) : s ≤ a ::ₘ s.erase a := if h : a ∈ s then le_of_eq (cons_erase h).symm else by rw erase_of_not_mem h; apply le_cons_self lemma add_singleton_eq_iff {s t : multiset α} {a : α} : s + {a} = t ↔ a ∈ t ∧ s = t.erase a := begin rw [add_comm, singleton_add], split, { rintro rfl, exact ⟨s.mem_cons_self a, (s.erase_cons_head a).symm⟩ }, { rintro ⟨h, rfl⟩, exact cons_erase h }, end theorem erase_add_left_pos {a : α} {s : multiset α} (t) : a ∈ s → (s + t).erase a = s.erase a + t := quotient.induction_on₂ s t $ λ l₁ l₂ h, congr_arg coe $ erase_append_left l₂ h theorem erase_add_right_pos {a : α} (s) {t : multiset α} (h : a ∈ t) : (s + t).erase a = s + t.erase a := by rw [add_comm, erase_add_left_pos s h, add_comm] theorem erase_add_right_neg {a : α} {s : multiset α} (t) : a ∉ s → (s + t).erase a = s + t.erase a := quotient.induction_on₂ s t $ λ l₁ l₂ h, congr_arg coe $ erase_append_right l₂ h theorem erase_add_left_neg {a : α} (s) {t : multiset α} (h : a ∉ t) : (s + t).erase a = s.erase a + t := by rw [add_comm, erase_add_right_neg s h, add_comm] theorem erase_le (a : α) (s : multiset α) : s.erase a ≤ s := quot.induction_on s $ λ l, (erase_sublist a l).subperm @[simp] theorem erase_lt {a : α} {s : multiset α} : s.erase a < s ↔ a ∈ s := ⟨λ h, not_imp_comm.1 erase_of_not_mem (ne_of_lt h), λ h, by simpa [h] using lt_cons_self (s.erase a) a⟩ theorem erase_subset (a : α) (s : multiset α) : s.erase a ⊆ s := subset_of_le (erase_le a s) theorem mem_erase_of_ne {a b : α} {s : multiset α} (ab : a ≠ b) : a ∈ s.erase b ↔ a ∈ s := quot.induction_on s $ λ l, list.mem_erase_of_ne ab theorem mem_of_mem_erase {a b : α} {s : multiset α} : a ∈ s.erase b → a ∈ s := mem_of_subset (erase_subset _ _) theorem erase_comm (s : multiset α) (a b : α) : (s.erase a).erase b = (s.erase b).erase a := quot.induction_on s $ λ l, congr_arg coe $ l.erase_comm a b theorem erase_le_erase {s t : multiset α} (a : α) (h : s ≤ t) : s.erase a ≤ t.erase a := le_induction_on h $ λ l₁ l₂ h, (h.erase _).subperm theorem erase_le_iff_le_cons {s t : multiset α} {a : α} : s.erase a ≤ t ↔ s ≤ a ::ₘ t := ⟨λ h, le_trans (le_cons_erase _ _) (cons_le_cons _ h), λ h, if m : a ∈ s then by rw ← cons_erase m at h; exact (cons_le_cons_iff _).1 h else le_trans (erase_le _ _) ((le_cons_of_not_mem m).1 h)⟩ @[simp] theorem card_erase_of_mem {a : α} {s : multiset α} : a ∈ s → card (s.erase a) = pred (card s) := quot.induction_on s $ λ l, length_erase_of_mem @[simp] lemma card_erase_add_one {a : α} {s : multiset α} : a ∈ s → (s.erase a).card + 1 = s.card := quot.induction_on s $ λ l, length_erase_add_one theorem card_erase_lt_of_mem {a : α} {s : multiset α} : a ∈ s → card (s.erase a) < card s := λ h, card_lt_of_lt (erase_lt.mpr h) theorem card_erase_le {a : α} {s : multiset α} : card (s.erase a) ≤ card s := card_le_of_le (erase_le a s) theorem card_erase_eq_ite {a : α} {s : multiset α} : card (s.erase a) = if a ∈ s then pred (card s) else card s := begin by_cases h : a ∈ s, { rwa [card_erase_of_mem h, if_pos] }, { rwa [erase_of_not_mem h, if_neg] } end end erase @[simp] theorem coe_reverse (l : list α) : (reverse l : multiset α) = l := quot.sound $ reverse_perm _ /-! ### `multiset.map` -/ /-- `map f s` is the lift of the list `map` operation. The multiplicity of `b` in `map f s` is the number of `a ∈ s` (counting multiplicity) such that `f a = b`. -/ def map (f : α → β) (s : multiset α) : multiset β := quot.lift_on s (λ l : list α, (l.map f : multiset β)) (λ l₁ l₂ p, quot.sound (p.map f)) @[congr] theorem map_congr {f g : α → β} {s t : multiset α} : s = t → (∀ x ∈ t, f x = g x) → map f s = map g t := begin rintros rfl h, induction s using quot.induction_on, exact congr_arg coe (map_congr h) end lemma map_hcongr {β' : Type*} {m : multiset α} {f : α → β} {f' : α → β'} (h : β = β') (hf : ∀a∈m, f a == f' a) : map f m == map f' m := begin subst h, simp at hf, simp [map_congr rfl hf] end theorem forall_mem_map_iff {f : α → β} {p : β → Prop} {s : multiset α} : (∀ y ∈ s.map f, p y) ↔ (∀ x ∈ s, p (f x)) := quotient.induction_on' s $ λ L, list.forall_mem_map_iff @[simp] theorem coe_map (f : α → β) (l : list α) : map f ↑l = l.map f := rfl @[simp] theorem map_zero (f : α → β) : map f 0 = 0 := rfl @[simp] theorem map_cons (f : α → β) (a s) : map f (a ::ₘ s) = f a ::ₘ map f s := quot.induction_on s $ λ l, rfl theorem map_comp_cons (f : α → β) (t) : map f ∘ cons t = cons (f t) ∘ map f := by { ext, simp } @[simp] theorem map_singleton (f : α → β) (a : α) : ({a} : multiset α).map f = {f a} := rfl @[simp] theorem map_replicate (f : α → β) (a : α) (k : ℕ) : (replicate k a).map f = replicate k (f a) := by simp only [← coe_replicate, coe_map, map_replicate] @[simp] theorem map_add (f : α → β) (s t) : map f (s + t) = map f s + map f t := quotient.induction_on₂ s t $ λ l₁ l₂, congr_arg coe $ map_append _ _ _ /-- If each element of `s : multiset α` can be lifted to `β`, then `s` can be lifted to `multiset β`. -/ instance can_lift (c) (p) [can_lift α β c p] : can_lift (multiset α) (multiset β) (map c) (λ s, ∀ x ∈ s, p x) := { prf := by { rintro ⟨l⟩ hl, lift l to list β using hl, exact ⟨l, coe_map _ _⟩ } } /-- `multiset.map` as an `add_monoid_hom`. -/ def map_add_monoid_hom (f : α → β) : multiset α →+ multiset β := { to_fun := map f, map_zero' := map_zero _, map_add' := map_add _ } @[simp] lemma coe_map_add_monoid_hom (f : α → β) : (map_add_monoid_hom f : multiset α → multiset β) = map f := rfl theorem map_nsmul (f : α → β) (n : ℕ) (s) : map f (n • s) = n • (map f s) := (map_add_monoid_hom f).map_nsmul _ _ @[simp] theorem mem_map {f : α → β} {b : β} {s : multiset α} : b ∈ map f s ↔ ∃ a, a ∈ s ∧ f a = b := quot.induction_on s $ λ l, mem_map @[simp] theorem card_map (f : α → β) (s) : card (map f s) = card s := quot.induction_on s $ λ l, length_map _ _ @[simp] theorem map_eq_zero {s : multiset α} {f : α → β} : s.map f = 0 ↔ s = 0 := by rw [← multiset.card_eq_zero, multiset.card_map, multiset.card_eq_zero] theorem mem_map_of_mem (f : α → β) {a : α} {s : multiset α} (h : a ∈ s) : f a ∈ map f s := mem_map.2 ⟨_, h, rfl⟩ lemma map_eq_singleton {f : α → β} {s : multiset α} {b : β} : map f s = {b} ↔ ∃ a : α, s = {a} ∧ f a = b := begin split, { intro h, obtain ⟨a, ha⟩ : ∃ a, s = {a}, { rw [←card_eq_one, ←card_map, h, card_singleton] }, refine ⟨a, ha, _⟩, rw [←mem_singleton, ←h, ha, map_singleton, mem_singleton] }, { rintro ⟨a, rfl, rfl⟩, simp } end lemma map_eq_cons [decidable_eq α] (f : α → β) (s : multiset α) (t : multiset β) (b : β) : (∃ a ∈ s, f a = b ∧ (s.erase a).map f = t) ↔ s.map f = b ::ₘ t := begin split, { rintro ⟨a, ha, rfl, rfl⟩, rw [←map_cons, multiset.cons_erase ha] }, { intro h, have : b ∈ s.map f, { rw h, exact mem_cons_self _ _ }, obtain ⟨a, h1, rfl⟩ := mem_map.mp this, obtain ⟨u, rfl⟩ := exists_cons_of_mem h1, rw [map_cons, cons_inj_right] at h, refine ⟨a, mem_cons_self _ _, rfl, _⟩, rw [multiset.erase_cons_head, h] } end theorem mem_map_of_injective {f : α → β} (H : function.injective f) {a : α} {s : multiset α} : f a ∈ map f s ↔ a ∈ s := quot.induction_on s $ λ l, mem_map_of_injective H @[simp] theorem map_map (g : β → γ) (f : α → β) (s : multiset α) : map g (map f s) = map (g ∘ f) s := quot.induction_on s $ λ l, congr_arg coe $ list.map_map _ _ _ theorem map_id (s : multiset α) : map id s = s := quot.induction_on s $ λ l, congr_arg coe $ map_id _ @[simp] lemma map_id' (s : multiset α) : map (λx, x) s = s := map_id s @[simp] theorem map_const (s : multiset α) (b : β) : map (function.const α b) s = replicate s.card b := quot.induction_on s $ λ l, congr_arg coe $ map_const _ _ -- Not a `simp` lemma because `function.const` is reducibel in Lean 3 theorem map_const' (s : multiset α) (b : β) : map (λ _, b) s = replicate s.card b := map_const s b theorem eq_of_mem_map_const {b₁ b₂ : β} {l : list α} (h : b₁ ∈ map (function.const α b₂) l) : b₁ = b₂ := eq_of_mem_replicate $ by rwa map_const at h @[simp] theorem map_le_map {f : α → β} {s t : multiset α} (h : s ≤ t) : map f s ≤ map f t := le_induction_on h $ λ l₁ l₂ h, (h.map f).subperm @[simp] lemma map_lt_map {f : α → β} {s t : multiset α} (h : s < t) : s.map f < t.map f := begin refine (map_le_map h.le).lt_of_not_le (λ H, h.ne $ eq_of_le_of_card_le h.le _), rw [←s.card_map f, ←t.card_map f], exact card_le_of_le H, end lemma map_mono (f : α → β) : monotone (map f) := λ _ _, map_le_map lemma map_strict_mono (f : α → β) : strict_mono (map f) := λ _ _, map_lt_map @[simp] theorem map_subset_map {f : α → β} {s t : multiset α} (H : s ⊆ t) : map f s ⊆ map f t := λ b m, let ⟨a, h, e⟩ := mem_map.1 m in mem_map.2 ⟨a, H h, e⟩ lemma map_erase [decidable_eq α] [decidable_eq β] (f : α → β) (hf : function.injective f) (x : α) (s : multiset α) : (s.erase x).map f = (s.map f).erase (f x) := begin induction s using multiset.induction_on with y s ih, { simp }, by_cases hxy : y = x, { cases hxy, simp }, { rw [s.erase_cons_tail hxy, map_cons, map_cons, (s.map f).erase_cons_tail (hf.ne hxy), ih] } end lemma map_surjective_of_surjective {f : α → β} (hf : function.surjective f) : function.surjective (map f) := begin intro s, induction s using multiset.induction_on with x s ih, { exact ⟨0, map_zero _⟩ }, { obtain ⟨y, rfl⟩ := hf x, obtain ⟨t, rfl⟩ := ih, exact ⟨y ::ₘ t, map_cons _ _ _⟩ } end /-! ### `multiset.fold` -/ /-- `foldl f H b s` is the lift of the list operation `foldl f b l`, which folds `f` over the multiset. It is well defined when `f` is right-commutative, that is, `f (f b a₁) a₂ = f (f b a₂) a₁`. -/ def foldl (f : β → α → β) (H : right_commutative f) (b : β) (s : multiset α) : β := quot.lift_on s (λ l, foldl f b l) (λ l₁ l₂ p, p.foldl_eq H b) @[simp] theorem foldl_zero (f : β → α → β) (H b) : foldl f H b 0 = b := rfl @[simp] theorem foldl_cons (f : β → α → β) (H b a s) : foldl f H b (a ::ₘ s) = foldl f H (f b a) s := quot.induction_on s $ λ l, rfl @[simp] theorem foldl_add (f : β → α → β) (H b s t) : foldl f H b (s + t) = foldl f H (foldl f H b s) t := quotient.induction_on₂ s t $ λ l₁ l₂, foldl_append _ _ _ _ /-- `foldr f H b s` is the lift of the list operation `foldr f b l`, which folds `f` over the multiset. It is well defined when `f` is left-commutative, that is, `f a₁ (f a₂ b) = f a₂ (f a₁ b)`. -/ def foldr (f : α → β → β) (H : left_commutative f) (b : β) (s : multiset α) : β := quot.lift_on s (λ l, foldr f b l) (λ l₁ l₂ p, p.foldr_eq H b) @[simp] theorem foldr_zero (f : α → β → β) (H b) : foldr f H b 0 = b := rfl @[simp] theorem foldr_cons (f : α → β → β) (H b a s) : foldr f H b (a ::ₘ s) = f a (foldr f H b s) := quot.induction_on s $ λ l, rfl @[simp] theorem foldr_singleton (f : α → β → β) (H b a) : foldr f H b ({a} : multiset α) = f a b := rfl @[simp] theorem foldr_add (f : α → β → β) (H b s t) : foldr f H b (s + t) = foldr f H (foldr f H b t) s := quotient.induction_on₂ s t $ λ l₁ l₂, foldr_append _ _ _ _ @[simp] theorem coe_foldr (f : α → β → β) (H : left_commutative f) (b : β) (l : list α) : foldr f H b l = l.foldr f b := rfl @[simp] theorem coe_foldl (f : β → α → β) (H : right_commutative f) (b : β) (l : list α) : foldl f H b l = l.foldl f b := rfl theorem coe_foldr_swap (f : α → β → β) (H : left_commutative f) (b : β) (l : list α) : foldr f H b l = l.foldl (λ x y, f y x) b := (congr_arg (foldr f H b) (coe_reverse l)).symm.trans $ foldr_reverse _ _ _ theorem foldr_swap (f : α → β → β) (H : left_commutative f) (b : β) (s : multiset α) : foldr f H b s = foldl (λ x y, f y x) (λ x y z, (H _ _ _).symm) b s := quot.induction_on s $ λ l, coe_foldr_swap _ _ _ _ theorem foldl_swap (f : β → α → β) (H : right_commutative f) (b : β) (s : multiset α) : foldl f H b s = foldr (λ x y, f y x) (λ x y z, (H _ _ _).symm) b s := (foldr_swap _ _ _ _).symm lemma foldr_induction' (f : α → β → β) (H : left_commutative f) (x : β) (q : α → Prop) (p : β → Prop) (s : multiset α) (hpqf : ∀ a b, q a → p b → p (f a b)) (px : p x) (q_s : ∀ a ∈ s, q a) : p (foldr f H x s) := begin revert s, refine multiset.induction (by simp [px]) _, intros a s hs hsa, rw foldr_cons, have hps : ∀ (x : α), x ∈ s → q x, from λ x hxs, hsa x (mem_cons_of_mem hxs), exact hpqf a (foldr f H x s) (hsa a (mem_cons_self a s)) (hs hps), end lemma foldr_induction (f : α → α → α) (H : left_commutative f) (x : α) (p : α → Prop) (s : multiset α) (p_f : ∀ a b, p a → p b → p (f a b)) (px : p x) (p_s : ∀ a ∈ s, p a) : p (foldr f H x s) := foldr_induction' f H x p p s p_f px p_s lemma foldl_induction' (f : β → α → β) (H : right_commutative f) (x : β) (q : α → Prop) (p : β → Prop) (s : multiset α) (hpqf : ∀ a b, q a → p b → p (f b a)) (px : p x) (q_s : ∀ a ∈ s, q a) : p (foldl f H x s) := begin rw foldl_swap, exact foldr_induction' (λ x y, f y x) (λ x y z, (H _ _ _).symm) x q p s hpqf px q_s, end lemma foldl_induction (f : α → α → α) (H : right_commutative f) (x : α) (p : α → Prop) (s : multiset α) (p_f : ∀ a b, p a → p b → p (f b a)) (px : p x) (p_s : ∀ a ∈ s, p a) : p (foldl f H x s) := foldl_induction' f H x p p s p_f px p_s /-! ### Map for partial functions -/ /-- Lift of the list `pmap` operation. Map a partial function `f` over a multiset `s` whose elements are all in the domain of `f`. -/ def pmap {p : α → Prop} (f : Π a, p a → β) (s : multiset α) : (∀ a ∈ s, p a) → multiset β := quot.rec_on s (λ l H, ↑(pmap f l H)) $ λ l₁ l₂ (pp : l₁ ~ l₂), funext $ λ (H₂ : ∀ a ∈ l₂, p a), have H₁ : ∀ a ∈ l₁, p a, from λ a h, H₂ a (pp.subset h), have ∀ {s₂ e H}, @eq.rec (multiset α) l₁ (λ s, (∀ a ∈ s, p a) → multiset β) (λ _, ↑(pmap f l₁ H₁)) s₂ e H = ↑(pmap f l₁ H₁), by intros s₂ e _; subst e, this.trans $ quot.sound $ pp.pmap f @[simp] theorem coe_pmap {p : α → Prop} (f : Π a, p a → β) (l : list α) (H : ∀ a ∈ l, p a) : pmap f l H = l.pmap f H := rfl @[simp] lemma pmap_zero {p : α → Prop} (f : Π a, p a → β) (h : ∀a∈(0:multiset α), p a) : pmap f 0 h = 0 := rfl @[simp] lemma pmap_cons {p : α → Prop} (f : Π a, p a → β) (a : α) (m : multiset α) : ∀(h : ∀b∈a ::ₘ m, p b), pmap f (a ::ₘ m) h = f a (h a (mem_cons_self a m)) ::ₘ pmap f m (λa ha, h a $ mem_cons_of_mem ha) := quotient.induction_on m $ assume l h, rfl /-- "Attach" a proof that `a ∈ s` to each element `a` in `s` to produce a multiset on `{x // x ∈ s}`. -/ def attach (s : multiset α) : multiset {x // x ∈ s} := pmap subtype.mk s (λ a, id) @[simp] theorem coe_attach (l : list α) : @eq (multiset {x // x ∈ l}) (@attach α l) l.attach := rfl theorem sizeof_lt_sizeof_of_mem [has_sizeof α] {x : α} {s : multiset α} (hx : x ∈ s) : sizeof x < sizeof s := by { induction s with l a b, exact list.sizeof_lt_sizeof_of_mem hx, refl } theorem pmap_eq_map (p : α → Prop) (f : α → β) (s : multiset α) : ∀ H, @pmap _ _ p (λ a _, f a) s H = map f s := quot.induction_on s $ λ l H, congr_arg coe $ pmap_eq_map p f l H theorem pmap_congr {p q : α → Prop} {f : Π a, p a → β} {g : Π a, q a → β} (s : multiset α) {H₁ H₂} : (∀ (a ∈ s) h₁ h₂, f a h₁ = g a h₂) → pmap f s H₁ = pmap g s H₂ := quot.induction_on s (λ l H₁ H₂ h, congr_arg coe $ pmap_congr l h) H₁ H₂ theorem map_pmap {p : α → Prop} (g : β → γ) (f : Π a, p a → β) (s) : ∀ H, map g (pmap f s H) = pmap (λ a h, g (f a h)) s H := quot.induction_on s $ λ l H, congr_arg coe $ map_pmap g f l H theorem pmap_eq_map_attach {p : α → Prop} (f : Π a, p a → β) (s) : ∀ H, pmap f s H = s.attach.map (λ x, f x (H _ x.prop)) := quot.induction_on s $ λ l H, congr_arg coe $ pmap_eq_map_attach f l H @[simp] lemma attach_map_coe' (s : multiset α) (f : α → β) : s.attach.map (λ i, f i) = s.map f := quot.induction_on s $ λ l, congr_arg coe $ attach_map_coe' l f lemma attach_map_val' (s : multiset α) (f : α → β) : s.attach.map (λ i, f i.val) = s.map f := attach_map_coe' _ _ @[simp] lemma attach_map_coe (s : multiset α) : s.attach.map (coe : _ → α) = s := (attach_map_coe' _ _).trans s.map_id lemma attach_map_val (s : multiset α) : s.attach.map subtype.val = s := attach_map_coe _ @[simp] theorem mem_attach (s : multiset α) : ∀ x, x ∈ s.attach := quot.induction_on s $ λ l, mem_attach _ @[simp] theorem mem_pmap {p : α → Prop} {f : Π a, p a → β} {s H b} : b ∈ pmap f s H ↔ ∃ a (h : a ∈ s), f a (H a h) = b := quot.induction_on s (λ l H, mem_pmap) H @[simp] theorem card_pmap {p : α → Prop} (f : Π a, p a → β) (s H) : card (pmap f s H) = card s := quot.induction_on s (λ l H, length_pmap) H @[simp] theorem card_attach {m : multiset α} : card (attach m) = card m := card_pmap _ _ _ @[simp] lemma attach_zero : (0 : multiset α).attach = 0 := rfl lemma attach_cons (a : α) (m : multiset α) : (a ::ₘ m).attach = ⟨a, mem_cons_self a m⟩ ::ₘ (m.attach.map $ λp, ⟨p.1, mem_cons_of_mem p.2⟩) := quotient.induction_on m $ assume l, congr_arg coe $ congr_arg (list.cons _) $ by rw [list.map_pmap]; exact list.pmap_congr _ (λ _ _ _ _, subtype.eq rfl) section decidable_pi_exists variables {m : multiset α} /-- If `p` is a decidable predicate, so is the predicate that all elements of a multiset satisfy `p`. -/ protected def decidable_forall_multiset {p : α → Prop} [hp : ∀ a, decidable (p a)] : decidable (∀ a ∈ m, p a) := quotient.rec_on_subsingleton m (λl, decidable_of_iff (∀a ∈ l, p a) $ by simp) instance decidable_dforall_multiset {p : Π a ∈ m, Prop} [hp : ∀ a (h : a ∈ m), decidable (p a h)] : decidable (∀ a (h : a ∈ m), p a h) := decidable_of_decidable_of_iff (@multiset.decidable_forall_multiset {a // a ∈ m} m.attach (λa, p a.1 a.2) _) (iff.intro (assume h a ha, h ⟨a, ha⟩ (mem_attach _ _)) (assume h ⟨a, ha⟩ _, h _ _)) /-- decidable equality for functions whose domain is bounded by multisets -/ instance decidable_eq_pi_multiset {β : α → Type*} [h : ∀ a, decidable_eq (β a)] : decidable_eq (Π a ∈ m, β a) := assume f g, decidable_of_iff (∀ a (h : a ∈ m), f a h = g a h) (by simp [function.funext_iff]) /-- If `p` is a decidable predicate, so is the existence of an element in a multiset satisfying `p`. -/ protected def decidable_exists_multiset {p : α → Prop} [decidable_pred p] : decidable (∃ x ∈ m, p x) := quotient.rec_on_subsingleton m (λl, decidable_of_iff (∃ a ∈ l, p a) $ by simp) instance decidable_dexists_multiset {p : Π a ∈ m, Prop} [hp : ∀ a (h : a ∈ m), decidable (p a h)] : decidable (∃ a (h : a ∈ m), p a h) := decidable_of_decidable_of_iff (@multiset.decidable_exists_multiset {a // a ∈ m} m.attach (λa, p a.1 a.2) _) (iff.intro (λ ⟨⟨a, ha₁⟩, _, ha₂⟩, ⟨a, ha₁, ha₂⟩) (λ ⟨a, ha₁, ha₂⟩, ⟨⟨a, ha₁⟩, mem_attach _ _, ha₂⟩)) end decidable_pi_exists /-! ### Subtraction -/ section variables [decidable_eq α] {s t u : multiset α} {a b : α} /-- `s - t` is the multiset such that `count a (s - t) = count a s - count a t` for all `a` (note that it is truncated subtraction, so it is `0` if `count a t ≥ count a s`). -/ protected def sub (s t : multiset α) : multiset α := quotient.lift_on₂ s t (λ l₁ l₂, (l₁.diff l₂ : multiset α)) $ λ v₁ v₂ w₁ w₂ p₁ p₂, quot.sound $ p₁.diff p₂ instance : has_sub (multiset α) := ⟨multiset.sub⟩ @[simp] theorem coe_sub (s t : list α) : (s - t : multiset α) = (s.diff t : list α) := rfl /-- This is a special case of `tsub_zero`, which should be used instead of this. This is needed to prove `has_ordered_sub (multiset α)`. -/ protected theorem sub_zero (s : multiset α) : s - 0 = s := quot.induction_on s $ λ l, rfl @[simp] theorem sub_cons (a : α) (s t : multiset α) : s - a ::ₘ t = s.erase a - t := quotient.induction_on₂ s t $ λ l₁ l₂, congr_arg coe $ diff_cons _ _ _ /-- This is a special case of `tsub_le_iff_right`, which should be used instead of this. This is needed to prove `has_ordered_sub (multiset α)`. -/ protected theorem sub_le_iff_le_add : s - t ≤ u ↔ s ≤ u + t := by revert s; exact multiset.induction_on t (by simp [multiset.sub_zero]) (λ a t IH s, by simp [IH, erase_le_iff_le_cons]) instance : has_ordered_sub (multiset α) := ⟨λ n m k, multiset.sub_le_iff_le_add⟩ lemma cons_sub_of_le (a : α) {s t : multiset α} (h : t ≤ s) : a ::ₘ s - t = a ::ₘ (s - t) := by rw [←singleton_add, ←singleton_add, add_tsub_assoc_of_le h] theorem sub_eq_fold_erase (s t : multiset α) : s - t = foldl erase erase_comm s t := quotient.induction_on₂ s t $ λ l₁ l₂, show ↑(l₁.diff l₂) = foldl erase erase_comm ↑l₁ ↑l₂, by { rw diff_eq_foldl l₁ l₂, symmetry, exact foldl_hom _ _ _ _ _ (λ x y, rfl) } @[simp] theorem card_sub {s t : multiset α} (h : t ≤ s) : card (s - t) = card s - card t := (tsub_eq_of_eq_add_rev $ by rw [add_comm, ← card_add, tsub_add_cancel_of_le h]).symm /-! ### Union -/ /-- `s ∪ t` is the lattice join operation with respect to the multiset `≤`. The multiplicity of `a` in `s ∪ t` is the maximum of the multiplicities in `s` and `t`. -/ def union (s t : multiset α) : multiset α := s - t + t instance : has_union (multiset α) := ⟨union⟩ theorem union_def (s t : multiset α) : s ∪ t = s - t + t := rfl theorem le_union_left (s t : multiset α) : s ≤ s ∪ t := le_tsub_add theorem le_union_right (s t : multiset α) : t ≤ s ∪ t := le_add_left _ _ theorem eq_union_left : t ≤ s → s ∪ t = s := tsub_add_cancel_of_le theorem union_le_union_right (h : s ≤ t) (u) : s ∪ u ≤ t ∪ u := add_le_add_right (tsub_le_tsub_right h _) u theorem union_le (h₁ : s ≤ u) (h₂ : t ≤ u) : s ∪ t ≤ u := by rw ← eq_union_left h₂; exact union_le_union_right h₁ t @[simp] theorem mem_union : a ∈ s ∪ t ↔ a ∈ s ∨ a ∈ t := ⟨λ h, (mem_add.1 h).imp_left (mem_of_le tsub_le_self), or.rec (mem_of_le $ le_union_left _ _) (mem_of_le $ le_union_right _ _)⟩ @[simp] theorem map_union [decidable_eq β] {f : α → β} (finj : function.injective f) {s t : multiset α} : map f (s ∪ t) = map f s ∪ map f t := quotient.induction_on₂ s t $ λ l₁ l₂, congr_arg coe (by rw [list.map_append f, list.map_diff finj]) /-! ### Intersection -/ /-- `s ∩ t` is the lattice meet operation with respect to the multiset `≤`. The multiplicity of `a` in `s ∩ t` is the minimum of the multiplicities in `s` and `t`. -/ def inter (s t : multiset α) : multiset α := quotient.lift_on₂ s t (λ l₁ l₂, (l₁.bag_inter l₂ : multiset α)) $ λ v₁ v₂ w₁ w₂ p₁ p₂, quot.sound $ p₁.bag_inter p₂ instance : has_inter (multiset α) := ⟨inter⟩ @[simp] theorem inter_zero (s : multiset α) : s ∩ 0 = 0 := quot.induction_on s $ λ l, congr_arg coe l.bag_inter_nil @[simp] theorem zero_inter (s : multiset α) : 0 ∩ s = 0 := quot.induction_on s $ λ l, congr_arg coe l.nil_bag_inter @[simp] theorem cons_inter_of_pos {a} (s : multiset α) {t} : a ∈ t → (a ::ₘ s) ∩ t = a ::ₘ s ∩ t.erase a := quotient.induction_on₂ s t $ λ l₁ l₂ h, congr_arg coe $ cons_bag_inter_of_pos _ h @[simp] theorem cons_inter_of_neg {a} (s : multiset α) {t} : a ∉ t → (a ::ₘ s) ∩ t = s ∩ t := quotient.induction_on₂ s t $ λ l₁ l₂ h, congr_arg coe $ cons_bag_inter_of_neg _ h theorem inter_le_left (s t : multiset α) : s ∩ t ≤ s := quotient.induction_on₂ s t $ λ l₁ l₂, (bag_inter_sublist_left _ _).subperm theorem inter_le_right (s : multiset α) : ∀ t, s ∩ t ≤ t := multiset.induction_on s (λ t, (zero_inter t).symm ▸ zero_le _) $ λ a s IH t, if h : a ∈ t then by simpa [h] using cons_le_cons a (IH (t.erase a)) else by simp [h, IH] theorem le_inter (h₁ : s ≤ t) (h₂ : s ≤ u) : s ≤ t ∩ u := begin revert s u, refine multiset.induction_on t _ (λ a t IH, _); intros, { simp [h₁] }, by_cases a ∈ u, { rw [cons_inter_of_pos _ h, ← erase_le_iff_le_cons], exact IH (erase_le_iff_le_cons.2 h₁) (erase_le_erase _ h₂) }, { rw cons_inter_of_neg _ h, exact IH ((le_cons_of_not_mem $ mt (mem_of_le h₂) h).1 h₁) h₂ } end @[simp] theorem mem_inter : a ∈ s ∩ t ↔ a ∈ s ∧ a ∈ t := ⟨λ h, ⟨mem_of_le (inter_le_left _ _) h, mem_of_le (inter_le_right _ _) h⟩, λ ⟨h₁, h₂⟩, by rw [← cons_erase h₁, cons_inter_of_pos _ h₂]; apply mem_cons_self⟩ instance : lattice (multiset α) := { sup := (∪), sup_le := @union_le _ _, le_sup_left := le_union_left, le_sup_right := le_union_right, inf := (∩), le_inf := @le_inter _ _, inf_le_left := inter_le_left, inf_le_right := inter_le_right, ..@multiset.partial_order α } @[simp] theorem sup_eq_union (s t : multiset α) : s ⊔ t = s ∪ t := rfl @[simp] theorem inf_eq_inter (s t : multiset α) : s ⊓ t = s ∩ t := rfl @[simp] theorem le_inter_iff : s ≤ t ∩ u ↔ s ≤ t ∧ s ≤ u := le_inf_iff @[simp] theorem union_le_iff : s ∪ t ≤ u ↔ s ≤ u ∧ t ≤ u := sup_le_iff theorem union_comm (s t : multiset α) : s ∪ t = t ∪ s := sup_comm theorem inter_comm (s t : multiset α) : s ∩ t = t ∩ s := inf_comm theorem eq_union_right (h : s ≤ t) : s ∪ t = t := by rw [union_comm, eq_union_left h] theorem union_le_union_left (h : s ≤ t) (u) : u ∪ s ≤ u ∪ t := sup_le_sup_left h _ theorem union_le_add (s t : multiset α) : s ∪ t ≤ s + t := union_le (le_add_right _ _) (le_add_left _ _) theorem union_add_distrib (s t u : multiset α) : (s ∪ t) + u = (s + u) ∪ (t + u) := by simpa [(∪), union, eq_comm, add_assoc] using show s + u - (t + u) = s - t, by rw [add_comm t, tsub_add_eq_tsub_tsub, add_tsub_cancel_right] theorem add_union_distrib (s t u : multiset α) : s + (t ∪ u) = (s + t) ∪ (s + u) := by rw [add_comm, union_add_distrib, add_comm s, add_comm s] theorem cons_union_distrib (a : α) (s t : multiset α) : a ::ₘ (s ∪ t) = (a ::ₘ s) ∪ (a ::ₘ t) := by simpa using add_union_distrib (a ::ₘ 0) s t theorem inter_add_distrib (s t u : multiset α) : (s ∩ t) + u = (s + u) ∩ (t + u) := begin by_contra h, cases lt_iff_cons_le.1 (lt_of_le_of_ne (le_inter (add_le_add_right (inter_le_left s t) u) (add_le_add_right (inter_le_right s t) u)) h) with a hl, rw ← cons_add at hl, exact not_le_of_lt (lt_cons_self (s ∩ t) a) (le_inter (le_of_add_le_add_right (le_trans hl (inter_le_left _ _))) (le_of_add_le_add_right (le_trans hl (inter_le_right _ _)))) end theorem add_inter_distrib (s t u : multiset α) : s + (t ∩ u) = (s + t) ∩ (s + u) := by rw [add_comm, inter_add_distrib, add_comm s, add_comm s] theorem cons_inter_distrib (a : α) (s t : multiset α) : a ::ₘ (s ∩ t) = (a ::ₘ s) ∩ (a ::ₘ t) := by simp theorem union_add_inter (s t : multiset α) : s ∪ t + s ∩ t = s + t := begin apply le_antisymm, { rw union_add_distrib, refine union_le (add_le_add_left (inter_le_right _ _) _) _, rw add_comm, exact add_le_add_right (inter_le_left _ _) _ }, { rw [add_comm, add_inter_distrib], refine le_inter (add_le_add_right (le_union_right _ _) _) _, rw add_comm, exact add_le_add_right (le_union_left _ _) _ } end theorem sub_add_inter (s t : multiset α) : s - t + s ∩ t = s := begin rw [inter_comm], revert s, refine multiset.induction_on t (by simp) (λ a t IH s, _), by_cases a ∈ s, { rw [cons_inter_of_pos _ h, sub_cons, add_cons, IH, cons_erase h] }, { rw [cons_inter_of_neg _ h, sub_cons, erase_of_not_mem h, IH] } end theorem sub_inter (s t : multiset α) : s - (s ∩ t) = s - t := add_right_cancel $ by rw [sub_add_inter s t, tsub_add_cancel_of_le (inter_le_left s t)] end /-! ### `multiset.filter` -/ section variables (p : α → Prop) [decidable_pred p] /-- `filter p s` returns the elements in `s` (with the same multiplicities) which satisfy `p`, and removes the rest. -/ def filter (s : multiset α) : multiset α := quot.lift_on s (λ l, (filter p l : multiset α)) (λ l₁ l₂ h, quot.sound $ h.filter p) @[simp] theorem coe_filter (l : list α) : filter p (↑l) = l.filter p := rfl @[simp] theorem filter_zero : filter p 0 = 0 := rfl lemma filter_congr {p q : α → Prop} [decidable_pred p] [decidable_pred q] {s : multiset α} : (∀ x ∈ s, p x ↔ q x) → filter p s = filter q s := quot.induction_on s $ λ l h, congr_arg coe $ filter_congr' h @[simp] theorem filter_add (s t : multiset α) : filter p (s + t) = filter p s + filter p t := quotient.induction_on₂ s t $ λ l₁ l₂, congr_arg coe $ filter_append _ _ @[simp] theorem filter_le (s : multiset α) : filter p s ≤ s := quot.induction_on s $ λ l, (filter_sublist _).subperm @[simp] theorem filter_subset (s : multiset α) : filter p s ⊆ s := subset_of_le $ filter_le _ _ theorem filter_le_filter {s t} (h : s ≤ t) : filter p s ≤ filter p t := le_induction_on h $ λ l₁ l₂ h, (h.filter p).subperm lemma monotone_filter_left : monotone (filter p) := λ s t, filter_le_filter p lemma monotone_filter_right (s : multiset α) ⦃p q : α → Prop⦄ [decidable_pred p] [decidable_pred q] (h : p ≤ q) : s.filter p ≤ s.filter q := quotient.induction_on s (λ l, (l.monotone_filter_right h).subperm) variable {p} @[simp] theorem filter_cons_of_pos {a : α} (s) : p a → filter p (a ::ₘ s) = a ::ₘ filter p s := quot.induction_on s $ λ l h, congr_arg coe $ filter_cons_of_pos l h @[simp] theorem filter_cons_of_neg {a : α} (s) : ¬ p a → filter p (a ::ₘ s) = filter p s := quot.induction_on s $ λ l h, @congr_arg _ _ _ _ coe $ filter_cons_of_neg l h @[simp] theorem mem_filter {a : α} {s} : a ∈ filter p s ↔ a ∈ s ∧ p a := quot.induction_on s $ λ l, mem_filter theorem of_mem_filter {a : α} {s} (h : a ∈ filter p s) : p a := (mem_filter.1 h).2 theorem mem_of_mem_filter {a : α} {s} (h : a ∈ filter p s) : a ∈ s := (mem_filter.1 h).1 theorem mem_filter_of_mem {a : α} {l} (m : a ∈ l) (h : p a) : a ∈ filter p l := mem_filter.2 ⟨m, h⟩ theorem filter_eq_self {s} : filter p s = s ↔ ∀ a ∈ s, p a := quot.induction_on s $ λ l, iff.trans ⟨λ h, (filter_sublist _).eq_of_length (@congr_arg _ _ _ _ card h), congr_arg coe⟩ filter_eq_self theorem filter_eq_nil {s} : filter p s = 0 ↔ ∀ a ∈ s, ¬p a := quot.induction_on s $ λ l, iff.trans ⟨λ h, eq_nil_of_length_eq_zero (@congr_arg _ _ _ _ card h), congr_arg coe⟩ filter_eq_nil theorem le_filter {s t} : s ≤ filter p t ↔ s ≤ t ∧ ∀ a ∈ s, p a := ⟨λ h, ⟨le_trans h (filter_le _ _), λ a m, of_mem_filter (mem_of_le h m)⟩, λ ⟨h, al⟩, filter_eq_self.2 al ▸ filter_le_filter p h⟩ theorem filter_cons {a : α} (s : multiset α) : filter p (a ::ₘ s) = (if p a then {a} else 0) + filter p s := begin split_ifs with h, { rw [filter_cons_of_pos _ h, singleton_add] }, { rw [filter_cons_of_neg _ h, zero_add] }, end lemma filter_singleton {a : α} (p : α → Prop) [decidable_pred p] : filter p {a} = if p a then {a} else ∅ := by simp only [singleton, filter_cons, filter_zero, add_zero, empty_eq_zero] lemma filter_nsmul (s : multiset α) (n : ℕ) : filter p (n • s) = n • filter p s := begin refine s.induction_on _ _, { simp only [filter_zero, nsmul_zero] }, { intros a ha ih, rw [nsmul_cons, filter_add, ih, filter_cons, nsmul_add], congr, split_ifs with hp; { simp only [filter_eq_self, nsmul_zero, filter_eq_nil], intros b hb, rwa (mem_singleton.mp (mem_of_mem_nsmul hb)) } } end variable (p) @[simp] theorem filter_sub [decidable_eq α] (s t : multiset α) : filter p (s - t) = filter p s - filter p t := begin revert s, refine multiset.induction_on t (by simp) (λ a t IH s, _), rw [sub_cons, IH], by_cases p a, { rw [filter_cons_of_pos _ h, sub_cons], congr, by_cases m : a ∈ s, { rw [← cons_inj_right a, ← filter_cons_of_pos _ h, cons_erase (mem_filter_of_mem m h), cons_erase m] }, { rw [erase_of_not_mem m, erase_of_not_mem (mt mem_of_mem_filter m)] } }, { rw [filter_cons_of_neg _ h], by_cases m : a ∈ s, { rw [(by rw filter_cons_of_neg _ h : filter p (erase s a) = filter p (a ::ₘ erase s a)), cons_erase m] }, { rw [erase_of_not_mem m] } } end @[simp] theorem filter_union [decidable_eq α] (s t : multiset α) : filter p (s ∪ t) = filter p s ∪ filter p t := by simp [(∪), union] @[simp] theorem filter_inter [decidable_eq α] (s t : multiset α) : filter p (s ∩ t) = filter p s ∩ filter p t := le_antisymm (le_inter (filter_le_filter _ $ inter_le_left _ _) (filter_le_filter _ $ inter_le_right _ _)) $ le_filter.2 ⟨inf_le_inf (filter_le _ _) (filter_le _ _), λ a h, of_mem_filter (mem_of_le (inter_le_left _ _) h)⟩ @[simp] theorem filter_filter (q) [decidable_pred q] (s : multiset α) : filter p (filter q s) = filter (λ a, p a ∧ q a) s := quot.induction_on s $ λ l, congr_arg coe $ filter_filter p q l theorem filter_add_filter (q) [decidable_pred q] (s : multiset α) : filter p s + filter q s = filter (λ a, p a ∨ q a) s + filter (λ a, p a ∧ q a) s := multiset.induction_on s rfl $ λ a s IH, by by_cases p a; by_cases q a; simp * theorem filter_add_not (s : multiset α) : filter p s + filter (λ a, ¬ p a) s = s := by rw [filter_add_filter, filter_eq_self.2, filter_eq_nil.2]; simp [decidable.em] theorem map_filter (f : β → α) (s : multiset β) : filter p (map f s) = map f (filter (p ∘ f) s) := quot.induction_on s (λ l, by simp [map_filter]) /-! ### Simultaneously filter and map elements of a multiset -/ /-- `filter_map f s` is a combination filter/map operation on `s`. The function `f : α → option β` is applied to each element of `s`; if `f a` is `some b` then `b` is added to the result, otherwise `a` is removed from the resulting multiset. -/ def filter_map (f : α → option β) (s : multiset α) : multiset β := quot.lift_on s (λ l, (filter_map f l : multiset β)) (λ l₁ l₂ h, quot.sound $ h.filter_map f) @[simp] theorem coe_filter_map (f : α → option β) (l : list α) : filter_map f l = l.filter_map f := rfl @[simp] theorem filter_map_zero (f : α → option β) : filter_map f 0 = 0 := rfl @[simp] theorem filter_map_cons_none {f : α → option β} (a : α) (s : multiset α) (h : f a = none) : filter_map f (a ::ₘ s) = filter_map f s := quot.induction_on s $ λ l, @congr_arg _ _ _ _ coe $ filter_map_cons_none a l h @[simp] theorem filter_map_cons_some (f : α → option β) (a : α) (s : multiset α) {b : β} (h : f a = some b) : filter_map f (a ::ₘ s) = b ::ₘ filter_map f s := quot.induction_on s $ λ l, @congr_arg _ _ _ _ coe $ filter_map_cons_some f a l h theorem filter_map_eq_map (f : α → β) : filter_map (some ∘ f) = map f := funext $ λ s, quot.induction_on s $ λ l, @congr_arg _ _ _ _ coe $ congr_fun (filter_map_eq_map f) l theorem filter_map_eq_filter : filter_map (option.guard p) = filter p := funext $ λ s, quot.induction_on s $ λ l, @congr_arg _ _ _ _ coe $ congr_fun (filter_map_eq_filter p) l theorem filter_map_filter_map (f : α → option β) (g : β → option γ) (s : multiset α) : filter_map g (filter_map f s) = filter_map (λ x, (f x).bind g) s := quot.induction_on s $ λ l, congr_arg coe $ filter_map_filter_map f g l theorem map_filter_map (f : α → option β) (g : β → γ) (s : multiset α) : map g (filter_map f s) = filter_map (λ x, (f x).map g) s := quot.induction_on s $ λ l, congr_arg coe $ map_filter_map f g l theorem filter_map_map (f : α → β) (g : β → option γ) (s : multiset α) : filter_map g (map f s) = filter_map (g ∘ f) s := quot.induction_on s $ λ l, congr_arg coe $ filter_map_map f g l theorem filter_filter_map (f : α → option β) (p : β → Prop) [decidable_pred p] (s : multiset α) : filter p (filter_map f s) = filter_map (λ x, (f x).filter p) s := quot.induction_on s $ λ l, congr_arg coe $ filter_filter_map f p l theorem filter_map_filter (f : α → option β) (s : multiset α) : filter_map f (filter p s) = filter_map (λ x, if p x then f x else none) s := quot.induction_on s $ λ l, congr_arg coe $ filter_map_filter p f l @[simp] theorem filter_map_some (s : multiset α) : filter_map some s = s := quot.induction_on s $ λ l, congr_arg coe $ filter_map_some l @[simp] theorem mem_filter_map (f : α → option β) (s : multiset α) {b : β} : b ∈ filter_map f s ↔ ∃ a, a ∈ s ∧ f a = some b := quot.induction_on s $ λ l, mem_filter_map f l theorem map_filter_map_of_inv (f : α → option β) (g : β → α) (H : ∀ x : α, (f x).map g = some x) (s : multiset α) : map g (filter_map f s) = s := quot.induction_on s $ λ l, congr_arg coe $ map_filter_map_of_inv f g H l theorem filter_map_le_filter_map (f : α → option β) {s t : multiset α} (h : s ≤ t) : filter_map f s ≤ filter_map f t := le_induction_on h $ λ l₁ l₂ h, (h.filter_map _).subperm /-! ### countp -/ /-- `countp p s` counts the number of elements of `s` (with multiplicity) that satisfy `p`. -/ def countp (s : multiset α) : ℕ := quot.lift_on s (countp p) (λ l₁ l₂, perm.countp_eq p) @[simp] theorem coe_countp (l : list α) : countp p l = l.countp p := rfl @[simp] theorem countp_zero : countp p 0 = 0 := rfl variable {p} @[simp] theorem countp_cons_of_pos {a : α} (s) : p a → countp p (a ::ₘ s) = countp p s + 1 := quot.induction_on s $ countp_cons_of_pos p @[simp] theorem countp_cons_of_neg {a : α} (s) : ¬ p a → countp p (a ::ₘ s) = countp p s := quot.induction_on s $ countp_cons_of_neg p variable (p) theorem countp_cons (b : α) (s) : countp p (b ::ₘ s) = countp p s + (if p b then 1 else 0) := quot.induction_on s $ by simp [list.countp_cons] theorem countp_eq_card_filter (s) : countp p s = card (filter p s) := quot.induction_on s $ λ l, l.countp_eq_length_filter p theorem countp_le_card (s) : countp p s ≤ card s := quot.induction_on s $ λ l, countp_le_length p @[simp] theorem countp_add (s t) : countp p (s + t) = countp p s + countp p t := by simp [countp_eq_card_filter] @[simp] theorem countp_nsmul (s) (n : ℕ) : countp p (n • s) = n * countp p s := by induction n; simp [*, succ_nsmul', succ_mul, zero_nsmul] theorem card_eq_countp_add_countp (s) : card s = countp p s + countp (λ x, ¬ p x) s := quot.induction_on s $ λ l, by simp [l.length_eq_countp_add_countp p] /-- `countp p`, the number of elements of a multiset satisfying `p`, promoted to an `add_monoid_hom`. -/ def countp_add_monoid_hom : multiset α →+ ℕ := { to_fun := countp p, map_zero' := countp_zero _, map_add' := countp_add _ } @[simp] lemma coe_countp_add_monoid_hom : (countp_add_monoid_hom p : multiset α → ℕ) = countp p := rfl @[simp] theorem countp_sub [decidable_eq α] {s t : multiset α} (h : t ≤ s) : countp p (s - t) = countp p s - countp p t := by simp [countp_eq_card_filter, h, filter_le_filter] theorem countp_le_of_le {s t} (h : s ≤ t) : countp p s ≤ countp p t := by simpa [countp_eq_card_filter] using card_le_of_le (filter_le_filter p h) @[simp] theorem countp_filter (q) [decidable_pred q] (s : multiset α) : countp p (filter q s) = countp (λ a, p a ∧ q a) s := by simp [countp_eq_card_filter] theorem countp_eq_countp_filter_add (s) (p q : α → Prop) [decidable_pred p] [decidable_pred q] : countp p s = (filter q s).countp p + (filter (λ a, ¬ q a) s).countp p := quot.induction_on s $ λ l, l.countp_eq_countp_filter_add _ _ @[simp] lemma countp_true {s : multiset α} : countp (λ _, true) s = card s := quot.induction_on s $ λ l, list.countp_true @[simp] lemma countp_false {s : multiset α} : countp (λ _, false) s = 0 := quot.induction_on s $ λ l, list.countp_false theorem countp_map (f : α → β) (s : multiset α) (p : β → Prop) [decidable_pred p] : countp p (map f s) = (s.filter (λ a, p (f a))).card := begin refine multiset.induction_on s _ (λ a t IH, _), { rw [map_zero, countp_zero, filter_zero, card_zero] }, { rw [map_cons, countp_cons, IH, filter_cons, card_add, apply_ite card, card_zero, card_singleton, add_comm] }, end variable {p} theorem countp_pos {s} : 0 < countp p s ↔ ∃ a ∈ s, p a := quot.induction_on s $ λ l, list.countp_pos p theorem countp_eq_zero {s} : countp p s = 0 ↔ ∀ a ∈ s, ¬ p a := quot.induction_on s $ λ l, list.countp_eq_zero p theorem countp_eq_card {s} : countp p s = card s ↔ ∀ a ∈ s, p a := quot.induction_on s $ λ l, list.countp_eq_length p theorem countp_pos_of_mem {s a} (h : a ∈ s) (pa : p a) : 0 < countp p s := countp_pos.2 ⟨_, h, pa⟩ theorem countp_congr {s s' : multiset α} (hs : s = s') {p p' : α → Prop} [decidable_pred p] [decidable_pred p'] (hp : ∀ x ∈ s, p x = p' x) : s.countp p = s'.countp p' := quot.induction_on₂ s s' (λ l l' hs hp, begin simp only [quot_mk_to_coe'', coe_eq_coe] at hs, exact hs.countp_congr hp, end) hs hp end /-! ### Multiplicity of an element -/ section variable [decidable_eq α] /-- `count a s` is the multiplicity of `a` in `s`. -/ def count (a : α) : multiset α → ℕ := countp (eq a) @[simp] theorem coe_count (a : α) (l : list α) : count a (↑l) = l.count a := coe_countp _ _ @[simp] theorem count_zero (a : α) : count a 0 = 0 := rfl @[simp] theorem count_cons_self (a : α) (s : multiset α) : count a (a ::ₘ s) = succ (count a s) := countp_cons_of_pos _ rfl @[simp, priority 990] theorem count_cons_of_ne {a b : α} (h : a ≠ b) (s : multiset α) : count a (b ::ₘ s) = count a s := countp_cons_of_neg _ h theorem count_le_card (a : α) (s) : count a s ≤ card s := countp_le_card _ _ theorem count_le_of_le (a : α) {s t} : s ≤ t → count a s ≤ count a t := countp_le_of_le _ theorem count_le_count_cons (a b : α) (s : multiset α) : count a s ≤ count a (b ::ₘ s) := count_le_of_le _ (le_cons_self _ _) theorem count_cons (a b : α) (s : multiset α) : count a (b ::ₘ s) = count a s + (if a = b then 1 else 0) := countp_cons _ _ _ theorem count_singleton_self (a : α) : count a ({a} : multiset α) = 1 := count_eq_one_of_mem (nodup_singleton a) $ mem_singleton_self a theorem count_singleton (a b : α) : count a ({b} : multiset α) = if a = b then 1 else 0 := by simp only [count_cons, ←cons_zero, count_zero, zero_add] @[simp] theorem count_add (a : α) : ∀ s t, count a (s + t) = count a s + count a t := countp_add _ /-- `count a`, the multiplicity of `a` in a multiset, promoted to an `add_monoid_hom`. -/ def count_add_monoid_hom (a : α) : multiset α →+ ℕ := countp_add_monoid_hom (eq a) @[simp] lemma coe_count_add_monoid_hom {a : α} : (count_add_monoid_hom a : multiset α → ℕ) = count a := rfl @[simp] theorem count_nsmul (a : α) (n s) : count a (n • s) = n * count a s := by induction n; simp [*, succ_nsmul', succ_mul, zero_nsmul] theorem count_pos {a : α} {s : multiset α} : 0 < count a s ↔ a ∈ s := by simp [count, countp_pos] theorem one_le_count_iff_mem {a : α} {s : multiset α} : 1 ≤ count a s ↔ a ∈ s := by rw [succ_le_iff, count_pos] @[simp, priority 980] theorem count_eq_zero_of_not_mem {a : α} {s : multiset α} (h : a ∉ s) : count a s = 0 := by_contradiction $ λ h', h $ count_pos.1 (nat.pos_of_ne_zero h') @[simp] theorem count_eq_zero {a : α} {s : multiset α} : count a s = 0 ↔ a ∉ s := iff_not_comm.1 $ count_pos.symm.trans pos_iff_ne_zero theorem count_ne_zero {a : α} {s : multiset α} : count a s ≠ 0 ↔ a ∈ s := by simp [ne.def, count_eq_zero] theorem count_eq_card {a : α} {s} : count a s = card s ↔ ∀ (x ∈ s), a = x := countp_eq_card @[simp] theorem count_replicate_self (a : α) (n : ℕ) : count a (replicate n a) = n := count_replicate_self _ _ theorem count_replicate (a b : α) (n : ℕ) : count a (replicate n b) = if (a = b) then n else 0 := count_replicate _ _ _ @[simp] theorem count_erase_self (a : α) (s : multiset α) : count a (erase s a) = pred (count a s) := quotient.induction_on s $ count_erase_self a @[simp, priority 980] theorem count_erase_of_ne {a b : α} (ab : a ≠ b) (s : multiset α) : count a (erase s b) = count a s := quotient.induction_on s $ count_erase_of_ne ab @[simp] theorem count_sub (a : α) (s t : multiset α) : count a (s - t) = count a s - count a t := begin revert s, refine multiset.induction_on t (by simp) (λ b t IH s, _), rw [sub_cons, IH], by_cases ab : a = b, { subst b, rw [count_erase_self, count_cons_self, sub_succ, pred_sub] }, { rw [count_erase_of_ne ab, count_cons_of_ne ab] } end @[simp] theorem count_union (a : α) (s t : multiset α) : count a (s ∪ t) = max (count a s) (count a t) := by simp [(∪), union, tsub_add_eq_max, -add_comm] @[simp] theorem count_inter (a : α) (s t : multiset α) : count a (s ∩ t) = min (count a s) (count a t) := begin apply @nat.add_left_cancel (count a (s - t)), rw [← count_add, sub_add_inter, count_sub, tsub_add_min], end theorem le_count_iff_replicate_le {a : α} {s : multiset α} {n : ℕ} : n ≤ count a s ↔ replicate n a ≤ s := quot.induction_on s $ λ l, le_count_iff_replicate_sublist.trans replicate_le_coe.symm @[simp] theorem count_filter_of_pos {p} [decidable_pred p] {a} {s : multiset α} (h : p a) : count a (filter p s) = count a s := quot.induction_on s $ λ l, count_filter h @[simp] theorem count_filter_of_neg {p} [decidable_pred p] {a} {s : multiset α} (h : ¬ p a) : count a (filter p s) = 0 := multiset.count_eq_zero_of_not_mem (λ t, h (of_mem_filter t)) theorem count_filter {p} [decidable_pred p] {a} {s : multiset α} : count a (filter p s) = if p a then count a s else 0 := begin split_ifs with h, { exact count_filter_of_pos h }, { exact count_filter_of_neg h }, end theorem ext {s t : multiset α} : s = t ↔ ∀ a, count a s = count a t := quotient.induction_on₂ s t $ λ l₁ l₂, quotient.eq.trans perm_iff_count @[ext] theorem ext' {s t : multiset α} : (∀ a, count a s = count a t) → s = t := ext.2 @[simp] theorem coe_inter (s t : list α) : (s ∩ t : multiset α) = (s.bag_inter t : list α) := by ext; simp theorem le_iff_count {s t : multiset α} : s ≤ t ↔ ∀ a, count a s ≤ count a t := ⟨λ h a, count_le_of_le a h, λ al, by rw ← (ext.2 (λ a, by simp [max_eq_right (al a)]) : s ∪ t = t); apply le_union_left⟩ instance : distrib_lattice (multiset α) := { le_sup_inf := λ s t u, le_of_eq $ eq.symm $ ext.2 $ λ a, by simp only [max_min_distrib_left, multiset.count_inter, multiset.sup_eq_union, multiset.count_union, multiset.inf_eq_inter], ..multiset.lattice } theorem count_map {α β : Type*} (f : α → β) (s : multiset α) [decidable_eq β] (b : β) : count b (map f s) = (s.filter (λ a, b = f a)).card := countp_map _ _ _ /-- `multiset.map f` preserves `count` if `f` is injective on the set of elements contained in the multiset -/ theorem count_map_eq_count [decidable_eq β] (f : α → β) (s : multiset α) (hf : set.inj_on f {x : α | x ∈ s}) (x ∈ s) : (s.map f).count (f x) = s.count x := begin suffices : (filter (λ (a : α), f x = f a) s).count x = card (filter (λ (a : α), f x = f a) s), { rw [count, countp_map, ← this], exact count_filter_of_pos rfl }, { rw [eq_replicate_card.2 (λ b hb, ((hf H (mem_filter.1 hb).left) (mem_filter.1 hb).2).symm), count_replicate_self, card_replicate] } end /-- `multiset.map f` preserves `count` if `f` is injective -/ theorem count_map_eq_count' [decidable_eq β] (f : α → β) (s : multiset α) (hf : function.injective f) (x : α) : (s.map f).count (f x) = s.count x := begin by_cases H : x ∈ s, { exact count_map_eq_count f _ (set.inj_on_of_injective hf _) _ H, }, { rw [count_eq_zero_of_not_mem H, count_eq_zero, mem_map], rintro ⟨k, hks, hkx⟩, rw hf hkx at *, contradiction } end @[simp] lemma attach_count_eq_count_coe (m : multiset α) (a) : m.attach.count a = m.count (a : α) := calc m.attach.count a = (m.attach.map (coe : _ → α)).count (a : α) : (multiset.count_map_eq_count' _ _ subtype.coe_injective _).symm ... = m.count (a : α) : congr_arg _ m.attach_map_coe lemma filter_eq' (s : multiset α) (b : α) : s.filter (= b) = replicate (count b s) b := quotient.induction_on s $ λ l, congr_arg coe $ filter_eq' l b lemma filter_eq (s : multiset α) (b : α) : s.filter (eq b) = replicate (count b s) b := by simp_rw [←filter_eq', eq_comm] @[simp] lemma replicate_inter (n : ℕ) (x : α) (s : multiset α) : replicate n x ∩ s = replicate (min n (s.count x)) x := begin ext y, rw [count_inter, count_replicate, count_replicate], by_cases y = x, { simp only [h, if_pos rfl] }, { simp only [h, if_false, zero_min] } end @[simp] lemma inter_replicate (s : multiset α) (x : α) (n : ℕ) : s ∩ replicate n x = replicate (min (s.count x) n) x := by rw [inter_comm, replicate_inter, min_comm] end @[ext] lemma add_hom_ext [add_zero_class β] ⦃f g : multiset α →+ β⦄ (h : ∀ x, f {x} = g {x}) : f = g := begin ext s, induction s using multiset.induction_on with a s ih, { simp only [_root_.map_zero] }, { simp only [←singleton_add, _root_.map_add, ih, h] } end section embedding @[simp] lemma map_le_map_iff {f : α → β} (hf : function.injective f) {s t : multiset α} : s.map f ≤ t.map f ↔ s ≤ t := begin classical, refine ⟨λ h, le_iff_count.mpr (λ a, _), map_le_map⟩, simpa [count_map_eq_count' f _ hf] using le_iff_count.mp h (f a), end /-- Associate to an embedding `f` from `α` to `β` the order embedding that maps a multiset to its image under `f`. -/ @[simps] def map_embedding (f : α ↪ β) : multiset α ↪o multiset β := order_embedding.of_map_le_iff (map f) (λ _ _, map_le_map_iff f.inj') end embedding lemma count_eq_card_filter_eq [decidable_eq α] (s : multiset α) (a : α) : s.count a = (s.filter (eq a)).card := by rw [count, countp_eq_card_filter] /-- Mapping a multiset through a predicate and counting the `true`s yields the cardinality of the set filtered by the predicate. Note that this uses the notion of a multiset of `Prop`s - due to the decidability requirements of `count`, the decidability instance on the LHS is different from the RHS. In particular, the decidability instance on the left leaks `classical.dec_eq`. See [here](https://github.com/leanprover-community/mathlib/pull/11306#discussion_r782286812) for more discussion. -/ @[simp] lemma map_count_true_eq_filter_card (s : multiset α) (p : α → Prop) [decidable_pred p] : (s.map p).count true = (s.filter p).card := by simp only [count_eq_card_filter_eq, map_filter, card_map, function.comp.left_id, eq_true_eq_id] /-! ### Lift a relation to `multiset`s -/ section rel /-- `rel r s t` -- lift the relation `r` between two elements to a relation between `s` and `t`, s.t. there is a one-to-one mapping betweem elements in `s` and `t` following `r`. -/ @[mk_iff] inductive rel (r : α → β → Prop) : multiset α → multiset β → Prop | zero : rel 0 0 | cons {a b as bs} : r a b → rel as bs → rel (a ::ₘ as) (b ::ₘ bs) variables {δ : Type*} {r : α → β → Prop} {p : γ → δ → Prop} private lemma rel_flip_aux {s t} (h : rel r s t) : rel (flip r) t s := rel.rec_on h rel.zero (assume _ _ _ _ h₀ h₁ ih, rel.cons h₀ ih) lemma rel_flip {s t} : rel (flip r) s t ↔ rel r t s := ⟨rel_flip_aux, rel_flip_aux⟩ lemma rel_refl_of_refl_on {m : multiset α} {r : α → α → Prop} : (∀ x ∈ m, r x x) → rel r m m := begin apply m.induction_on, { intros, apply rel.zero }, { intros a m ih h, exact rel.cons (h _ (mem_cons_self _ _)) (ih (λ _ ha, h _ (mem_cons_of_mem ha))) } end lemma rel_eq_refl {s : multiset α} : rel (=) s s := rel_refl_of_refl_on (λ x hx, rfl) lemma rel_eq {s t : multiset α} : rel (=) s t ↔ s = t := begin split, { assume h, induction h; simp * }, { assume h, subst h, exact rel_eq_refl } end lemma rel.mono {r p : α → β → Prop} {s t} (hst : rel r s t) (h : ∀(a ∈ s) (b ∈ t), r a b → p a b) : rel p s t := begin induction hst, case rel.zero { exact rel.zero }, case rel.cons : a b s t hab hst ih { apply rel.cons (h a (mem_cons_self _ _) b (mem_cons_self _ _) hab), exact ih (λ a' ha' b' hb' h', h a' (mem_cons_of_mem ha') b' (mem_cons_of_mem hb') h') } end lemma rel.add {s t u v} (hst : rel r s t) (huv : rel r u v) : rel r (s + u) (t + v) := begin induction hst, case rel.zero { simpa using huv }, case rel.cons : a b s t hab hst ih { simpa using ih.cons hab } end lemma rel_flip_eq {s t : multiset α} : rel (λa b, b = a) s t ↔ s = t := show rel (flip (=)) s t ↔ s = t, by rw [rel_flip, rel_eq, eq_comm] @[simp] lemma rel_zero_left {b : multiset β} : rel r 0 b ↔ b = 0 := by rw [rel_iff]; simp @[simp] lemma rel_zero_right {a : multiset α} : rel r a 0 ↔ a = 0 := by rw [rel_iff]; simp lemma rel_cons_left {a as bs} : rel r (a ::ₘ as) bs ↔ (∃b bs', r a b ∧ rel r as bs' ∧ bs = b ::ₘ bs') := begin split, { generalize hm : a ::ₘ as = m, assume h, induction h generalizing as, case rel.zero { simp at hm, contradiction }, case rel.cons : a' b as' bs ha'b h ih { rcases cons_eq_cons.1 hm with ⟨eq₁, eq₂⟩ | ⟨h, cs, eq₁, eq₂⟩, { subst eq₁, subst eq₂, exact ⟨b, bs, ha'b, h, rfl⟩ }, { rcases ih eq₂.symm with ⟨b', bs', h₁, h₂, eq⟩, exact ⟨b', b ::ₘ bs', h₁, eq₁.symm ▸ rel.cons ha'b h₂, eq.symm ▸ cons_swap _ _ _⟩ } } }, { exact assume ⟨b, bs', hab, h, eq⟩, eq.symm ▸ rel.cons hab h } end lemma rel_cons_right {as b bs} : rel r as (b ::ₘ bs) ↔ (∃a as', r a b ∧ rel r as' bs ∧ as = a ::ₘ as') := begin rw [← rel_flip, rel_cons_left], refine exists₂_congr (λ a as', _), rw [rel_flip, flip] end lemma rel_add_left {as₀ as₁} : ∀{bs}, rel r (as₀ + as₁) bs ↔ (∃bs₀ bs₁, rel r as₀ bs₀ ∧ rel r as₁ bs₁ ∧ bs = bs₀ + bs₁) := multiset.induction_on as₀ (by simp) begin assume a s ih bs, simp only [ih, cons_add, rel_cons_left], split, { assume h, rcases h with ⟨b, bs', hab, h, rfl⟩, rcases h with ⟨bs₀, bs₁, h₀, h₁, rfl⟩, exact ⟨b ::ₘ bs₀, bs₁, ⟨b, bs₀, hab, h₀, rfl⟩, h₁, by simp⟩ }, { assume h, rcases h with ⟨bs₀, bs₁, h, h₁, rfl⟩, rcases h with ⟨b, bs, hab, h₀, rfl⟩, exact ⟨b, bs + bs₁, hab, ⟨bs, bs₁, h₀, h₁, rfl⟩, by simp⟩ } end lemma rel_add_right {as bs₀ bs₁} : rel r as (bs₀ + bs₁) ↔ (∃as₀ as₁, rel r as₀ bs₀ ∧ rel r as₁ bs₁ ∧ as = as₀ + as₁) := by rw [← rel_flip, rel_add_left]; simp [rel_flip] lemma rel_map_left {s : multiset γ} {f : γ → α} : ∀{t}, rel r (s.map f) t ↔ rel (λa b, r (f a) b) s t := multiset.induction_on s (by simp) (by simp [rel_cons_left] {contextual := tt}) lemma rel_map_right {s : multiset α} {t : multiset γ} {f : γ → β} : rel r s (t.map f) ↔ rel (λa b, r a (f b)) s t := by rw [← rel_flip, rel_map_left, ← rel_flip]; refl lemma rel_map {s : multiset α} {t : multiset β} {f : α → γ} {g : β → δ} : rel p (s.map f) (t.map g) ↔ rel (λa b, p (f a) (g b)) s t := rel_map_left.trans rel_map_right lemma card_eq_card_of_rel {r : α → β → Prop} {s : multiset α} {t : multiset β} (h : rel r s t) : card s = card t := by induction h; simp [*] lemma exists_mem_of_rel_of_mem {r : α → β → Prop} {s : multiset α} {t : multiset β} (h : rel r s t) : ∀ {a : α} (ha : a ∈ s), ∃ b ∈ t, r a b := begin induction h with x y s t hxy hst ih, { simp }, { assume a ha, cases mem_cons.1 ha with ha ha, { exact ⟨y, mem_cons_self _ _, ha.symm ▸ hxy⟩ }, { rcases ih ha with ⟨b, hbt, hab⟩, exact ⟨b, mem_cons.2 (or.inr hbt), hab⟩ } } end lemma rel_of_forall {m1 m2 : multiset α} {r : α → α → Prop} (h : ∀ a b, a ∈ m1 → b ∈ m2 → r a b) (hc : card m1 = card m2) : m1.rel r m2 := begin revert m1, apply m2.induction_on, { intros m h hc, rw [rel_zero_right, ← card_eq_zero, hc, card_zero] }, { intros a t ih m h hc, rw card_cons at hc, obtain ⟨b, hb⟩ := card_pos_iff_exists_mem.1 (show 0 < card m, from hc.symm ▸ (nat.succ_pos _)), obtain ⟨m', rfl⟩ := exists_cons_of_mem hb, refine rel_cons_right.mpr ⟨b, m', h _ _ hb (mem_cons_self _ _), ih _ _, rfl⟩, { exact λ _ _ ha hb, h _ _ (mem_cons_of_mem ha) (mem_cons_of_mem hb) }, { simpa using hc } } end lemma rel_replicate_left {m : multiset α} {a : α} {r : α → α → Prop} {n : ℕ} : (replicate n a).rel r m ↔ m.card = n ∧ ∀ x, x ∈ m → r a x := ⟨λ h, ⟨(card_eq_card_of_rel h).symm.trans (card_replicate _ _), λ x hx, begin obtain ⟨b, hb1, hb2⟩ := exists_mem_of_rel_of_mem (rel_flip.2 h) hx, rwa eq_of_mem_replicate hb1 at hb2, end⟩, λ h, rel_of_forall (λ x y hx hy, (eq_of_mem_replicate hx).symm ▸ (h.2 _ hy)) (eq.trans (card_replicate _ _) h.1.symm)⟩ lemma rel_replicate_right {m : multiset α} {a : α} {r : α → α → Prop} {n : ℕ} : m.rel r (replicate n a) ↔ m.card = n ∧ ∀ x, x ∈ m → r x a := rel_flip.trans rel_replicate_left lemma rel.trans (r : α → α → Prop) [is_trans α r] {s t u : multiset α} (r1 : rel r s t) (r2 : rel r t u) : rel r s u := begin induction t using multiset.induction_on with x t ih generalizing s u, { rw [rel_zero_right.mp r1, rel_zero_left.mp r2, rel_zero_left] }, { obtain ⟨a, as, ha1, ha2, rfl⟩ := rel_cons_right.mp r1, obtain ⟨b, bs, hb1, hb2, rfl⟩ := rel_cons_left.mp r2, exact multiset.rel.cons (trans ha1 hb1) (ih ha2 hb2) } end lemma rel.countp_eq (r : α → α → Prop) [is_trans α r] [is_symm α r] {s t : multiset α} (x : α) [decidable_pred (r x)] (h : rel r s t) : countp (r x) s = countp (r x) t := begin induction s using multiset.induction_on with y s ih generalizing t, { rw rel_zero_left.mp h, }, { obtain ⟨b, bs, hb1, hb2, rfl⟩ := rel_cons_left.mp h, rw [countp_cons, countp_cons, ih hb2], exact congr_arg _ (if_congr ⟨λ h, trans h hb1, λ h, trans h (symm hb1)⟩ rfl rfl) }, end end rel section map theorem map_eq_map {f : α → β} (hf : function.injective f) {s t : multiset α} : s.map f = t.map f ↔ s = t := by { rw [← rel_eq, ← rel_eq, rel_map], simp only [hf.eq_iff] } theorem map_injective {f : α → β} (hf : function.injective f) : function.injective (multiset.map f) := assume x y, (map_eq_map hf).1 end map section quot theorem map_mk_eq_map_mk_of_rel {r : α → α → Prop} {s t : multiset α} (hst : s.rel r t) : s.map (quot.mk r) = t.map (quot.mk r) := rel.rec_on hst rfl $ assume a b s t hab hst ih, by simp [ih, quot.sound hab] theorem exists_multiset_eq_map_quot_mk {r : α → α → Prop} (s : multiset (quot r)) : ∃t:multiset α, s = t.map (quot.mk r) := multiset.induction_on s ⟨0, rfl⟩ $ assume a s ⟨t, ht⟩, quot.induction_on a $ assume a, ht.symm ▸ ⟨a ::ₘ t, (map_cons _ _ _).symm⟩ theorem induction_on_multiset_quot {r : α → α → Prop} {p : multiset (quot r) → Prop} (s : multiset (quot r)) : (∀s:multiset α, p (s.map (quot.mk r))) → p s := match s, exists_multiset_eq_map_quot_mk s with _, ⟨t, rfl⟩ := assume h, h _ end end quot /-! ### Disjoint multisets -/ /-- `disjoint s t` means that `s` and `t` have no elements in common. -/ def disjoint (s t : multiset α) : Prop := ∀ ⦃a⦄, a ∈ s → a ∈ t → false @[simp] theorem coe_disjoint (l₁ l₂ : list α) : @disjoint α l₁ l₂ ↔ l₁.disjoint l₂ := iff.rfl theorem disjoint.symm {s t : multiset α} (d : disjoint s t) : disjoint t s | a i₂ i₁ := d i₁ i₂ theorem disjoint_comm {s t : multiset α} : disjoint s t ↔ disjoint t s := ⟨disjoint.symm, disjoint.symm⟩ theorem disjoint_left {s t : multiset α} : disjoint s t ↔ ∀ {a}, a ∈ s → a ∉ t := iff.rfl theorem disjoint_right {s t : multiset α} : disjoint s t ↔ ∀ {a}, a ∈ t → a ∉ s := disjoint_comm theorem disjoint_iff_ne {s t : multiset α} : disjoint s t ↔ ∀ a ∈ s, ∀ b ∈ t, a ≠ b := by simp [disjoint_left, imp_not_comm] theorem disjoint_of_subset_left {s t u : multiset α} (h : s ⊆ u) (d : disjoint u t) : disjoint s t | x m₁ := d (h m₁) theorem disjoint_of_subset_right {s t u : multiset α} (h : t ⊆ u) (d : disjoint s u) : disjoint s t | x m m₁ := d m (h m₁) theorem disjoint_of_le_left {s t u : multiset α} (h : s ≤ u) : disjoint u t → disjoint s t := disjoint_of_subset_left (subset_of_le h) theorem disjoint_of_le_right {s t u : multiset α} (h : t ≤ u) : disjoint s u → disjoint s t := disjoint_of_subset_right (subset_of_le h) @[simp] theorem zero_disjoint (l : multiset α) : disjoint 0 l | a := (not_mem_nil a).elim @[simp, priority 1100] theorem singleton_disjoint {l : multiset α} {a : α} : disjoint {a} l ↔ a ∉ l := by simp [disjoint]; refl @[simp, priority 1100] theorem disjoint_singleton {l : multiset α} {a : α} : disjoint l {a} ↔ a ∉ l := by rw [disjoint_comm, singleton_disjoint] @[simp] theorem disjoint_add_left {s t u : multiset α} : disjoint (s + t) u ↔ disjoint s u ∧ disjoint t u := by simp [disjoint, or_imp_distrib, forall_and_distrib] @[simp] theorem disjoint_add_right {s t u : multiset α} : disjoint s (t + u) ↔ disjoint s t ∧ disjoint s u := by rw [disjoint_comm, disjoint_add_left]; tauto @[simp] theorem disjoint_cons_left {a : α} {s t : multiset α} : disjoint (a ::ₘ s) t ↔ a ∉ t ∧ disjoint s t := (@disjoint_add_left _ {a} s t).trans $ by rw singleton_disjoint @[simp] theorem disjoint_cons_right {a : α} {s t : multiset α} : disjoint s (a ::ₘ t) ↔ a ∉ s ∧ disjoint s t := by rw [disjoint_comm, disjoint_cons_left]; tauto theorem inter_eq_zero_iff_disjoint [decidable_eq α] {s t : multiset α} : s ∩ t = 0 ↔ disjoint s t := by rw ← subset_zero; simp [subset_iff, disjoint] @[simp] theorem disjoint_union_left [decidable_eq α] {s t u : multiset α} : disjoint (s ∪ t) u ↔ disjoint s u ∧ disjoint t u := by simp [disjoint, or_imp_distrib, forall_and_distrib] @[simp] theorem disjoint_union_right [decidable_eq α] {s t u : multiset α} : disjoint s (t ∪ u) ↔ disjoint s t ∧ disjoint s u := by simp [disjoint, or_imp_distrib, forall_and_distrib] lemma add_eq_union_iff_disjoint [decidable_eq α] {s t : multiset α} : s + t = s ∪ t ↔ disjoint s t := by simp_rw [←inter_eq_zero_iff_disjoint, ext, count_add, count_union, count_inter, count_zero, nat.min_eq_zero_iff, nat.add_eq_max_iff] lemma disjoint_map_map {f : α → γ} {g : β → γ} {s : multiset α} {t : multiset β} : disjoint (s.map f) (t.map g) ↔ (∀a∈s, ∀b∈t, f a ≠ g b) := by { simp [disjoint, @eq_comm _ (f _) (g _)], refl } /-- `pairwise r m` states that there exists a list of the elements s.t. `r` holds pairwise on this list. -/ def pairwise (r : α → α → Prop) (m : multiset α) : Prop := ∃l:list α, m = l ∧ l.pairwise r @[simp] lemma pairwise_zero (r : α → α → Prop) : multiset.pairwise r 0 := ⟨[], rfl, list.pairwise.nil⟩ lemma pairwise_coe_iff {r : α → α → Prop} {l : list α} : multiset.pairwise r l ↔ ∃ l' : list α, l ~ l' ∧ l'.pairwise r := exists_congr $ by simp lemma pairwise_coe_iff_pairwise {r : α → α → Prop} (hr : symmetric r) {l : list α} : multiset.pairwise r l ↔ l.pairwise r := iff.intro (assume ⟨l', eq, h⟩, ((quotient.exact eq).pairwise_iff hr).2 h) (assume h, ⟨l, rfl, h⟩) lemma map_set_pairwise {f : α → β} {r : β → β → Prop} {m : multiset α} (h : {a | a ∈ m}.pairwise $ λ a₁ a₂, r (f a₁) (f a₂)) : {b | b ∈ m.map f}.pairwise r := λ b₁ h₁ b₂ h₂ hn, begin obtain ⟨⟨a₁, H₁, rfl⟩, a₂, H₂, rfl⟩ := ⟨multiset.mem_map.1 h₁, multiset.mem_map.1 h₂⟩, exact h H₁ H₂ (mt (congr_arg f) hn), end end multiset namespace multiset section choose variables (p : α → Prop) [decidable_pred p] (l : multiset α) /-- Given a proof `hp` that there exists a unique `a ∈ l` such that `p a`, `choose_x p l hp` returns that `a` together with proofs of `a ∈ l` and `p a`. -/ def choose_x : Π hp : (∃! a, a ∈ l ∧ p a), { a // a ∈ l ∧ p a } := quotient.rec_on l (λ l' ex_unique, list.choose_x p l' (exists_of_exists_unique ex_unique)) begin intros, funext hp, suffices all_equal : ∀ x y : { t // t ∈ b ∧ p t }, x = y, { apply all_equal }, { rintros ⟨x, px⟩ ⟨y, py⟩, rcases hp with ⟨z, ⟨z_mem_l, pz⟩, z_unique⟩, congr, calc x = z : z_unique x px ... = y : (z_unique y py).symm } end /-- Given a proof `hp` that there exists a unique `a ∈ l` such that `p a`, `choose p l hp` returns that `a`. -/ def choose (hp : ∃! a, a ∈ l ∧ p a) : α := choose_x p l hp lemma choose_spec (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (choose_x p l hp).property lemma choose_mem (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 lemma choose_property (hp : ∃! a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end choose variable (α) /-- The equivalence between lists and multisets of a subsingleton type. -/ def subsingleton_equiv [subsingleton α] : list α ≃ multiset α := { to_fun := coe, inv_fun := quot.lift id $ λ (a b : list α) (h : a ~ b), list.ext_le h.length_eq $ λ n h₁ h₂, subsingleton.elim _ _, left_inv := λ l, rfl, right_inv := λ m, quot.induction_on m $ λ l, rfl } variable {α} @[simp] lemma coe_subsingleton_equiv [subsingleton α] : (subsingleton_equiv α : list α → multiset α) = coe := rfl end multiset
d7d72707648f47727040f71ba335c5ec201a01e0
59aed81a2ce7741e690907fc374be338f4f88b6f
/src/math-688/lectures/lec-18.lean
888f9abc3d41f48d914bd953a88cbaea4649f9b2
[]
no_license
agusakov/math-688-lean
c84d5e1423eb208a0281135f0214b91b30d0ef48
67dc27ebff55a74c6b5a1c469ba04e7981d2e550
refs/heads/main
1,679,699,340,788
1,616,602,782,000
1,616,602,782,000
332,894,454
0
0
null
null
null
null
UTF-8
Lean
false
false
85
lean
/- 9 Oct 2019 -/ -- equitable partitions -- quotient matrix -- characteristic matrix
04d060ce69569020e57aa4080bf4bef812b9407f
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/logic/unnamed_482.lean
fa182bd41a7e28eaa6e2519fbee9f5e1dccf0e5a
[]
no_license
jamescheuk91/mathematics_in_lean
09f1f87d2b0dce53464ff0cbe592c568ff59cf5e
4452499264e2975bca2f42565c0925506ba5dda3
refs/heads/master
1,679,716,410,967
1,613,957,947,000
1,613,957,947,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
265
lean
variables {α : Type*} (r s t : set α) example : s ⊆ s := by { intros x xs, exact xs } theorem subset.refl : s ⊆ s := λ x xs, xs example : r ⊆ s → s ⊆ t → r ⊆ t := begin sorry end theorem subset.trans : r ⊆ s → s ⊆ t → r ⊆ t := sorry
e691ade6df12b96cdceae25e1057d1d13b2c937d
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/blast_cc1.lean
0abd9f4540ae8cb8946ef7473c861dacadca73d0
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
569
lean
import data.list constant f {A : Type} : A → A → A constant g : nat → nat set_option blast.strategy "cc" example (a b c : nat) : a = b → g a == g b := by blast example (a b c : nat) : a = b → c = b → f (f a b) (g c) = f (f c a) (g b) := by blast example (a b c d e x y : nat) : a = b → a = x → b = y → c = d → c = e → c = b → a = e := by blast open perm example (a b c d : list nat) : a ~ b → c ~ b → d ~ c → a ~ d := by blast set_option trace.cc true example (a b c d : list nat) : a ~ b → c ~ b → d = c → a ~ d := by blast
b087b5b048de01459de255e865dd6131cdb873ab
4727251e0cd73359b15b664c3170e5d754078599
/src/data/num/bitwise.lean
7f3d7c9324e8f993ae065f3d1b50af721514d202
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
10,971
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.num.basic import data.bitvec.core /-! # Bitwise operations using binary representation of integers ## Definitions * bitwise operations for `pos_num` and `num`, * `snum`, a type that represents integers as a bit string with a sign bit at the end, * arithmetic operations for `snum`. -/ namespace pos_num /-- Bitwise "or" for `pos_num`. -/ def lor : pos_num → pos_num → pos_num | 1 (bit0 q) := bit1 q | 1 q := q | (bit0 p) 1 := bit1 p | p 1 := p | (bit0 p) (bit0 q) := bit0 (lor p q) | (bit0 p) (bit1 q) := bit1 (lor p q) | (bit1 p) (bit0 q) := bit1 (lor p q) | (bit1 p) (bit1 q) := bit1 (lor p q) /-- Bitwise "and" for `pos_num`. -/ def land : pos_num → pos_num → num | 1 (bit0 q) := 0 | 1 _ := 1 | (bit0 p) 1 := 0 | _ 1 := 1 | (bit0 p) (bit0 q) := num.bit0 (land p q) | (bit0 p) (bit1 q) := num.bit0 (land p q) | (bit1 p) (bit0 q) := num.bit0 (land p q) | (bit1 p) (bit1 q) := num.bit1 (land p q) /-- Bitwise `λ a b, a && !b` for `pos_num`. For example, `ldiff 5 9 = 4`: 101 1001 ---- 100 -/ def ldiff : pos_num → pos_num → num | 1 (bit0 q) := 1 | 1 _ := 0 | (bit0 p) 1 := num.pos (bit0 p) | (bit1 p) 1 := num.pos (bit0 p) | (bit0 p) (bit0 q) := num.bit0 (ldiff p q) | (bit0 p) (bit1 q) := num.bit0 (ldiff p q) | (bit1 p) (bit0 q) := num.bit1 (ldiff p q) | (bit1 p) (bit1 q) := num.bit0 (ldiff p q) /-- Bitwise "xor" for `pos_num`. -/ def lxor : pos_num → pos_num → num | 1 1 := 0 | 1 (bit0 q) := num.pos (bit1 q) | 1 (bit1 q) := num.pos (bit0 q) | (bit0 p) 1 := num.pos (bit1 p) | (bit1 p) 1 := num.pos (bit0 p) | (bit0 p) (bit0 q) := num.bit0 (lxor p q) | (bit0 p) (bit1 q) := num.bit1 (lxor p q) | (bit1 p) (bit0 q) := num.bit1 (lxor p q) | (bit1 p) (bit1 q) := num.bit0 (lxor p q) /-- `a.test_bit n` is `tt` iff the `n`-th bit (starting from the LSB) in the binary representation of `a` is active. If the size of `a` is less than `n`, this evaluates to `ff`. -/ def test_bit : pos_num → nat → bool | 1 0 := tt | 1 (n+1) := ff | (bit0 p) 0 := ff | (bit0 p) (n+1) := test_bit p n | (bit1 p) 0 := tt | (bit1 p) (n+1) := test_bit p n /-- `n.one_bits 0` is the list of indices of active bits in the binary representation of `n`. -/ def one_bits : pos_num → nat → list nat | 1 d := [d] | (bit0 p) d := one_bits p (d+1) | (bit1 p) d := d :: one_bits p (d+1) /-- Left-shift the binary representation of a `pos_num`. -/ def shiftl (p : pos_num) : nat → pos_num | 0 := p | (n+1) := bit0 (shiftl n) /-- Right-shift the binary representation of a `pos_num`. -/ def shiftr : pos_num → nat → num | p 0 := num.pos p | 1 (n+1) := 0 | (bit0 p) (n+1) := shiftr p n | (bit1 p) (n+1) := shiftr p n end pos_num namespace num /-- Bitwise "or" for `num`. -/ def lor : num → num → num | 0 q := q | p 0 := p | (pos p) (pos q) := pos (p.lor q) /-- Bitwise "and" for `num`. -/ def land : num → num → num | 0 q := 0 | p 0 := 0 | (pos p) (pos q) := p.land q /-- Bitwise `λ a b, a && !b` for `num`. For example, `ldiff 5 9 = 4`: 101 1001 ---- 100 -/ def ldiff : num → num → num | 0 q := 0 | p 0 := p | (pos p) (pos q) := p.ldiff q /-- Bitwise "xor" for `num`. -/ def lxor : num → num → num | 0 q := q | p 0 := p | (pos p) (pos q) := p.lxor q /-- Left-shift the binary representation of a `num`. -/ def shiftl : num → nat → num | 0 n := 0 | (pos p) n := pos (p.shiftl n) /-- Right-shift the binary representation of a `pos_num`. -/ def shiftr : num → nat → num | 0 n := 0 | (pos p) n := p.shiftr n /-- `a.test_bit n` is `tt` iff the `n`-th bit (starting from the LSB) in the binary representation of `a` is active. If the size of `a` is less than `n`, this evaluates to `ff`. -/ def test_bit : num → nat → bool | 0 n := ff | (pos p) n := p.test_bit n /-- `n.one_bits` is the list of indices of active bits in the binary representation of `n`. -/ def one_bits : num → list nat | 0 := [] | (pos p) := p.one_bits 0 end num /-- This is a nonzero (and "non minus one") version of `snum`. See the documentation of `snum` for more details. -/ @[derive has_reflect, derive decidable_eq] inductive nzsnum : Type | msb : bool → nzsnum | bit : bool → nzsnum → nzsnum /-- Alternative representation of integers using a sign bit at the end. The convention on sign here is to have the argument to `msb` denote the sign of the MSB itself, with all higher bits set to the negation of this sign. The result is interpreted in two's complement. 13 = ..0001101(base 2) = nz (bit1 (bit0 (bit1 (msb tt)))) -13 = ..1110011(base 2) = nz (bit1 (bit1 (bit0 (msb ff)))) As with `num`, a special case must be added for zero, which has no msb, but by two's complement symmetry there is a second special case for -1. Here the `bool` field indicates the sign of the number. 0 = ..0000000(base 2) = zero ff -1 = ..1111111(base 2) = zero tt -/ @[derive has_reflect, derive decidable_eq] inductive snum : Type | zero : bool → snum | nz : nzsnum → snum instance : has_coe nzsnum snum := ⟨snum.nz⟩ instance : has_zero snum := ⟨snum.zero ff⟩ instance : has_one nzsnum := ⟨nzsnum.msb tt⟩ instance : has_one snum := ⟨snum.nz 1⟩ instance : inhabited nzsnum := ⟨1⟩ instance : inhabited snum := ⟨0⟩ /-! The `snum` representation uses a bit string, essentially a list of 0 (`ff`) and 1 (`tt`) bits, and the negation of the MSB is sign-extended to all higher bits. -/ namespace nzsnum notation a :: b := bit a b /-- Sign of a `nzsnum`. -/ def sign : nzsnum → bool | (msb b) := bnot b | (b :: p) := sign p /-- Bitwise `not` for `nzsnum`. -/ @[pattern] def not : nzsnum → nzsnum | (msb b) := msb (bnot b) | (b :: p) := bnot b :: not p prefix `~`:100 := not /-- Add an inactive bit at the end of a `nzsnum`. This mimics `pos_num.bit0`. -/ def bit0 : nzsnum → nzsnum := bit ff /-- Add an active bit at the end of a `nzsnum`. This mimics `pos_num.bit1`. -/ def bit1 : nzsnum → nzsnum := bit tt /-- The `head` of a `nzsnum` is the boolean value of its LSB. -/ def head : nzsnum → bool | (msb b) := b | (b :: p) := b /-- The `tail` of a `nzsnum` is the `snum` obtained by removing the LSB. Edge cases: `tail 1 = 0` and `tail (-2) = -1`. -/ def tail : nzsnum → snum | (msb b) := snum.zero (bnot b) | (b :: p) := p end nzsnum namespace snum open nzsnum /-- Sign of a `snum`. -/ def sign : snum → bool | (zero z) := z | (nz p) := p.sign /-- Bitwise `not` for `snum`. -/ @[pattern] def not : snum → snum | (zero z) := zero (bnot z) | (nz p) := ~p prefix ~ := not /-- Add a bit at the end of a `snum`. This mimics `nzsnum.bit`. -/ @[pattern] def bit : bool → snum → snum | b (zero z) := if b = z then zero b else msb b | b (nz p) := p.bit b notation a :: b := bit a b /-- Add an inactive bit at the end of a `snum`. This mimics `znum.bit0`. -/ def bit0 : snum → snum := bit ff /-- Add an active bit at the end of a `snum`. This mimics `znum.bit1`. -/ def bit1 : snum → snum := bit tt theorem bit_zero (b) : b :: zero b = zero b := by cases b; refl theorem bit_one (b) : b :: zero (bnot b) = msb b := by cases b; refl end snum namespace nzsnum open snum /-- A dependent induction principle for `nzsnum`, with base cases `0 : snum` and `(-1) : snum`. -/ def drec' {C : snum → Sort*} (z : Π b, C (snum.zero b)) (s : Π b p, C p → C (b :: p)) : Π p : nzsnum, C p | (msb b) := by rw ←bit_one; exact s b (snum.zero (bnot b)) (z (bnot b)) | (bit b p) := s b p (drec' p) end nzsnum namespace snum open nzsnum /-- The `head` of a `snum` is the boolean value of its LSB. -/ def head : snum → bool | (zero z) := z | (nz p) := p.head /-- The `tail` of a `snum` is obtained by removing the LSB. Edge cases: `tail 1 = 0`, `tail (-2) = -1`, `tail 0 = 0` and `tail (-1) = -1`. -/ def tail : snum → snum | (zero z) := zero z | (nz p) := p.tail /-- A dependent induction principle for `snum` which avoids relying on `nzsnum`. -/ def drec' {C : snum → Sort*} (z : Π b, C (snum.zero b)) (s : Π b p, C p → C (b :: p)) : Π p, C p | (zero b) := z b | (nz p) := p.drec' z s /-- An induction principle for `snum` which avoids relying on `nzsnum`. -/ def rec' {α} (z : bool → α) (s : bool → snum → α → α) : snum → α := drec' z s /-- `snum.test_bit n a` is `tt` iff the `n`-th bit (starting from the LSB) of `a` is active. If the size of `a` is less than `n`, this evaluates to `ff`. -/ def test_bit : nat → snum → bool | 0 p := head p | (n+1) p := test_bit n (tail p) /-- The successor of a `snum` (i.e. the operation adding one). -/ def succ : snum → snum := rec' (λ b, cond b 0 1) (λb p succp, cond b (ff :: succp) (tt :: p)) /-- The predecessor of a `snum` (i.e. the operation of removing one). -/ def pred : snum → snum := rec' (λ b, cond b (~1) ~0) (λb p predp, cond b (ff :: p) (tt :: predp)) /-- The opposite of a `snum`. -/ protected def neg (n : snum) : snum := succ ~n instance : has_neg snum := ⟨snum.neg⟩ /-- `snum.czadd a b n` is `n + a - b` (where `a` and `b` should be read as either 0 or 1). This is useful to implement the carry system in `cadd`. -/ def czadd : bool → bool → snum → snum | ff ff p := p | ff tt p := pred p | tt ff p := succ p | tt tt p := p end snum namespace snum /-- `a.bits n` is the vector of the `n` first bits of `a` (starting from the LSB). -/ def bits : snum → Π n, vector bool n | p 0 := vector.nil | p (n+1) := head p ::ᵥ bits (tail p) n def cadd : snum → snum → bool → snum := rec' (λ a p c, czadd c a p) $ λa p IH, rec' (λb c, czadd c b (a :: p)) $ λb q _ c, bitvec.xor3 a b c :: IH q (bitvec.carry a b c) /-- Add two `snum`s. -/ protected def add (a b : snum) : snum := cadd a b ff instance : has_add snum := ⟨snum.add⟩ /-- Substract two `snum`s. -/ protected def sub (a b : snum) : snum := a + -b instance : has_sub snum := ⟨snum.sub⟩ /-- Multiply two `snum`s. -/ protected def mul (a : snum) : snum → snum := rec' (λ b, cond b (-a) 0) $ λb q IH, cond b (bit0 IH + a) (bit0 IH) instance : has_mul snum := ⟨snum.mul⟩ end snum
9cf0b554756f631d6c421b0aefd42e5d4dbc001f
6e44fda625e48340c6ffc7b1109a9e3b208e5384
/src/metric_spaces/lattice.lean
08098a8265882f695e0ac792382331f0a0202be3
[]
no_license
JasonKYi/learn_mspaces
9f998a265b907af6be6a54061637fcf1f6d1ee9d
54083e81da420d2d362a7024a8c86bea8529fe66
refs/heads/master
1,619,008,842,896
1,609,897,382,000
1,609,897,382,000
249,780,600
5
0
null
null
null
null
UTF-8
Lean
false
false
1,783
lean
import metric_spaces.basic /-- In this file we showed that the `closed_set` form a Galois insertion with `Closure` being its Galois connection, and with that, we conclude `closed_set` form a `complete_lattice`. - `closed_set` is defined to be a closed subset of a metric space - `Closure` is defined to be a `closed_set` with `closure' _` as its carrier -/ variables {X : Type*} [metric_space X] variables {Y : Type*} [metric_space Y] open definitions set namespace closed_set open open_closed_sets.closure' /- Defining the structure of a closed set -/ structure closed_set (X : Type*) [metric_space X] := (carrier : set X) (is_closed : is_closed' carrier) instance : has_coe (closed_set X) (set X) := ⟨closed_set.carrier⟩ def Closure (S : set X) : closed_set X := { carrier := closure' S, is_closed := closure_closed S } theorem ext' {S T : closed_set X} (h : (S : set X) = T) : S = T := by cases S; cases T; congr' /- Closed sets form a partial order -/ instance : partial_order (closed_set X) := {.. partial_order.lift (coe : closed_set X → set X) (λ a b, ext') (by apply_instance)} /- The closure of a closed_set is itself -/ lemma Closure_self (T : closed_set X) : T = Closure T.1 := ext' $ show ↑T = closure' T.carrier, by {rw closure_self, refl, from T.2} /- Closed sets form a Galois insertion -/ def gi : @galois_insertion (set X) (closed_set X) _ _ Closure closed_set.carrier := { choice := λ S h, Closure S, gc := λ S T, ⟨λ h, set.subset.trans subset_closure' h, λ h, by rw Closure_self T; from closure_mono' h⟩, le_l_u := λ S, subset_closure', choice_eq := λ _ _, rfl } /- Closed sets form a complete lattice -/ instance : complete_lattice (closed_set X) := { .. galois_insertion.lift_complete_lattice gi} end closed_set
c1068f0bce55c0e226e8cd8511f7eca02c9b019f
618003631150032a5676f229d13a079ac875ff77
/src/category_theory/limits/shapes/images.lean
c13d4a15a6ec75aedbab95126c3522872a051b29
[ "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
18,931
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Markus Himmel -/ import category_theory.limits.shapes.equalizers import category_theory.limits.shapes.strong_epi /-! # Categorical images We define the categorical image of `f` as a factorisation `f = e ≫ m` through a monomorphism `m`, so that `m` factors through the `m'` in any other such factorisation. ## Main definitions * A `mono_factorisation` is a factorisation `f = e ≫ m`, where `m` is a monomorphism * `is_image F` means that a given mono factorisation `F` has the universal property of the image. * `has_image f` means that we have chosen an image for the morphism `f : X ⟶ Y`. * In this case, `image f` is the image object, `image.ι f : image f ⟶ Y` is the monomorphism `m` of the factorisation and `factor_thru_image f : X ⟶ image f` is the morphism `e`. * `has_images C` means that every morphism in `C` has an image. * Let `f : X ⟶ Y` and `g : P ⟶ Q` be morphisms in `C`, which we will represent as objects of the arrow category `arrow C`. Then `sq : f ⟶ g` is a commutative square in `C`. If `f` and `g` have images, then `has_image_map sq` represents the fact that there is a morphism `i : image f ⟶ image g` making the diagram X ----→ image f ----→ Y | | | | | | ↓ ↓ ↓ P ----→ image g ----→ Q commute, where the top row is the image factorisation of `f`, the bottom row is the image factorisation of `g`, and the outer rectangle is the commutative square `sq`. * If a category `has_images`, then `has_image_maps` means that every commutative square admits an image map. * If a category `has_images`, then `has_strong_epi_images` means that the morphism to the image is always a strong epimorphism. ## Main statements * When `C` has equalizers, the morphism `e` appearing in an image factorisation is an epimorphism. * When `C` has strong epi images, then these images admit image maps. ## Future work * TODO: coimages, and abelian categories. * TODO: connect this with existing working in the group theory and ring theory libraries. -/ universes v u open category_theory open category_theory.limits.walking_parallel_pair namespace category_theory.limits variables {C : Type u} [category.{v} C] variables {X Y : C} (f : X ⟶ Y) /-- A factorisation of a morphism `f = e ≫ m`, with `m` monic. -/ structure mono_factorisation (f : X ⟶ Y) := (I : C) (m : I ⟶ Y) [m_mono : mono.{v} m] (e : X ⟶ I) (fac' : e ≫ m = f . obviously) restate_axiom mono_factorisation.fac' attribute [simp, reassoc] mono_factorisation.fac attribute [instance] mono_factorisation.m_mono attribute [instance] mono_factorisation.m_mono namespace mono_factorisation /-- The obvious factorisation of a monomorphism through itself. -/ def self [mono f] : mono_factorisation f := { I := X, m := f, e := 𝟙 X } -- I'm not sure we really need this, but the linter says that an inhabited instance ought to exist... instance [mono f] : inhabited (mono_factorisation f) := ⟨self f⟩ /-- The morphism `m` in a factorisation `f = e ≫ m` through a monomorphism is uniquely determined. -/ @[ext] lemma ext {F F' : mono_factorisation f} (hI : F.I = F'.I) (hm : F.m = (eq_to_hom hI) ≫ F'.m) : F = F' := begin cases F, cases F', cases hI, simp at hm, dsimp at F_fac' F'_fac', congr, { assumption }, { resetI, apply (cancel_mono F_m).1, rw [F_fac', hm, F'_fac'], } end end mono_factorisation variable {f} /-- Data exhibiting that a given factorisation through a mono is initial. -/ structure is_image (F : mono_factorisation f) := (lift : Π (F' : mono_factorisation f), F.I ⟶ F'.I) (lift_fac' : Π (F' : mono_factorisation f), lift F' ≫ F'.m = F.m . obviously) restate_axiom is_image.lift_fac' attribute [simp, reassoc] is_image.lift_fac @[simp, reassoc] lemma is_image.fac_lift {F : mono_factorisation f} (hF : is_image F) (F' : mono_factorisation f) : F.e ≫ hF.lift F' = F'.e := (cancel_mono F'.m).1 $ by simp variable (f) namespace is_image /-- The trivial factorisation of a monomorphism satisfies the universal property. -/ @[simps] def self [mono f] : is_image (mono_factorisation.self f) := { lift := λ F', F'.e } instance [mono f] : inhabited (is_image (mono_factorisation.self f)) := ⟨self f⟩ variable {f} /-- Two factorisations through monomorphisms satisfying the universal property must factor through isomorphic objects. -/ -- TODO this is another good candidate for a future `unique_up_to_canonical_iso`. @[simps] def iso_ext {F F' : mono_factorisation f} (hF : is_image F) (hF' : is_image F') : F.I ≅ F'.I := { hom := hF.lift F', inv := hF'.lift F, hom_inv_id' := (cancel_mono F.m).1 (by simp), inv_hom_id' := (cancel_mono F'.m).1 (by simp) } end is_image /-- Data exhibiting that a morphism `f` has an image. -/ class has_image (f : X ⟶ Y) := (F : mono_factorisation f) (is_image : is_image F) section variable [has_image f] /-- The chosen factorisation of `f` through a monomorphism. -/ def image.mono_factorisation : mono_factorisation f := has_image.F /-- The witness of the universal property for the chosen factorisation of `f` through a monomorphism. -/ def image.is_image : is_image (image.mono_factorisation f) := has_image.is_image /-- The categorical image of a morphism. -/ def image : C := (image.mono_factorisation f).I /-- The inclusion of the image of a morphism into the target. -/ def image.ι : image f ⟶ Y := (image.mono_factorisation f).m @[simp] lemma image.as_ι : (image.mono_factorisation f).m = image.ι f := rfl instance : mono (image.ι f) := (image.mono_factorisation f).m_mono /-- The map from the source to the image of a morphism. -/ def factor_thru_image : X ⟶ image f := (image.mono_factorisation f).e /-- Rewrite in terms of the `factor_thru_image` interface. -/ @[simp] lemma as_factor_thru_image : (image.mono_factorisation f).e = factor_thru_image f := rfl @[simp, reassoc] lemma image.fac : factor_thru_image f ≫ image.ι f = f := (image.mono_factorisation f).fac' variable {f} /-- Any other factorisation of the morphism `f` through a monomorphism receives a map from the image. -/ def image.lift (F' : mono_factorisation f) : image f ⟶ F'.I := (image.is_image f).lift F' @[simp, reassoc] lemma image.lift_fac (F' : mono_factorisation f) : image.lift F' ≫ F'.m = image.ι f := (image.is_image f).lift_fac' F' @[simp, reassoc] lemma image.fac_lift (F' : mono_factorisation f) : factor_thru_image f ≫ image.lift F' = F'.e := (image.is_image f).fac_lift F' -- TODO we could put a category structure on `mono_factorisation f`, -- with the morphisms being `g : I ⟶ I'` commuting with the `m`s -- (they then automatically commute with the `e`s) -- and show that an `image_of f` gives an initial object there -- (uniqueness of the lift comes for free). instance lift_mono (F' : mono_factorisation f) : mono.{v} (image.lift F') := begin split, intros Z a b w, have w' : a ≫ image.ι f = b ≫ image.ι f := calc a ≫ image.ι f = a ≫ (image.lift F' ≫ F'.m) : by simp ... = (a ≫ image.lift F') ≫ F'.m : by rw [category.assoc] ... = (b ≫ image.lift F') ≫ F'.m : by rw w ... = b ≫ (image.lift F' ≫ F'.m) : by rw [←category.assoc] ... = b ≫ image.ι f : by simp, exact (cancel_mono (image.ι f)).1 w', end lemma has_image.uniq (F' : mono_factorisation f) (l : image f ⟶ F'.I) (w : l ≫ F'.m = image.ι f) : l = image.lift F' := (cancel_mono F'.m).1 (by simp [w]) end section variables (C) /-- `has_images` represents a choice of image for every morphism -/ class has_images := (has_image : Π {X Y : C} (f : X ⟶ Y), has_image.{v} f) attribute [instance, priority 100] has_images.has_image end section variables (f) [has_image f] /-- The image of a monomorphism is isomorphic to the source. -/ def image_mono_iso_source [mono f] : image f ≅ X := is_image.iso_ext (image.is_image f) (is_image.self f) @[simp, reassoc] lemma image_mono_iso_source_inv_ι [mono f] : (image_mono_iso_source f).inv ≫ image.ι f = f := by simp [image_mono_iso_source] @[simp, reassoc] lemma image_mono_iso_source_hom_self [mono f] : (image_mono_iso_source f).hom ≫ f = image.ι f := begin conv { to_lhs, congr, skip, rw ←image_mono_iso_source_inv_ι f, }, rw [←category.assoc, iso.hom_inv_id, category.id_comp], end -- This is the proof from https://en.wikipedia.org/wiki/Image_(category_theory), which is taken from: -- Mitchell, Barry (1965), Theory of categories, MR 0202787, p.12, Proposition 10.1 instance [Π {Z : C} (g h : image f ⟶ Z), has_limit.{v} (parallel_pair g h)] : epi (factor_thru_image f) := ⟨λ Z g h w, begin let q := equalizer.ι g h, let e' := equalizer.lift _ w, let F' : mono_factorisation f := { I := equalizer g h, m := q ≫ image.ι f, m_mono := by apply mono_comp, e := e' }, let v := image.lift F', have t₀ : v ≫ q ≫ image.ι f = image.ι f := image.lift_fac F', have t : v ≫ q = 𝟙 (image f) := (cancel_mono_id (image.ι f)).1 (by { convert t₀ using 1, rw category.assoc }), -- The proof from wikipedia next proves `q ≫ v = 𝟙 _`, -- and concludes that `equalizer g h ≅ image f`, -- but this isn't necessary. calc g = 𝟙 (image f) ≫ g : by rw [category.id_comp] ... = v ≫ q ≫ g : by rw [←t, category.assoc] ... = v ≫ q ≫ h : by rw [equalizer.condition g h] ... = 𝟙 (image f) ≫ h : by rw [←category.assoc, t] ... = h : by rw [category.id_comp] end⟩ end section variables {f} {f' : X ⟶ Y} [has_image f] [has_image f'] /-- An equation between morphisms gives a comparison map between the images (which momentarily we prove is an iso). -/ def image.eq_to_hom (h : f = f') : image f ⟶ image f' := image.lift.{v} { I := image f', m := image.ι f', e := factor_thru_image f', }. instance (h : f = f') : is_iso (image.eq_to_hom h) := { inv := image.eq_to_hom h.symm, hom_inv_id' := (cancel_mono (image.ι f)).1 (by simp [image.eq_to_hom]), inv_hom_id' := (cancel_mono (image.ι f')).1 (by simp [image.eq_to_hom]), } /-- An equation between morphisms gives an isomorphism between the images. -/ def image.eq_to_iso (h : f = f') : image f ≅ image f' := as_iso (image.eq_to_hom h) end section variables {Z : C} (g : Y ⟶ Z) /-- The comparison map `image (f ≫ g) ⟶ image g`. -/ def image.pre_comp [has_image g] [has_image (f ≫ g)] : image (f ≫ g) ⟶ image g := image.lift.{v} { I := image g, m := image.ι g, e := f ≫ factor_thru_image g } /-- The two step comparison map `image (f ≫ (g ≫ h)) ⟶ image (g ≫ h) ⟶ image h` agrees with the one step comparison map `image (f ≫ (g ≫ h)) ≅ image ((f ≫ g) ≫ h) ⟶ image h`. -/ lemma image.pre_comp_comp {W : C} (h : Z ⟶ W) [has_image (g ≫ h)] [has_image (f ≫ g ≫ h)] [has_image h] [has_image ((f ≫ g) ≫ h)] : image.pre_comp f (g ≫ h) ≫ image.pre_comp g h = image.eq_to_hom (category.assoc f g h).symm ≫ (image.pre_comp (f ≫ g) h) := begin apply (cancel_mono (image.ι h)).1, simp [image.pre_comp, image.eq_to_hom], end -- Note that in general we don't have the other comparison map you might expect -- `image f ⟶ image (f ≫ g)`. end end category_theory.limits namespace category_theory.limits variables {C : Type u} [category.{v} C] section instance {X Y : C} (f : X ⟶ Y) [has_image f] : has_image (arrow.mk f).hom := show has_image f, by apply_instance end section has_image_map /-- An image map is a morphism `image f → image g` fitting into a commutative square and satisfying the obvious commutativity conditions. -/ class has_image_map {f g : arrow C} [has_image f.hom] [has_image g.hom] (sq : f ⟶ g) := (map : image f.hom ⟶ image g.hom) (map_ι' : map ≫ image.ι g.hom = image.ι f.hom ≫ sq.right . obviously) restate_axiom has_image_map.map_ι' attribute [simp, reassoc] has_image_map.map_ι @[simp, reassoc] lemma has_image_map.factor_map {f g : arrow C} [has_image f.hom] [has_image g.hom] (sq : f ⟶ g) [has_image_map sq] : factor_thru_image f.hom ≫ has_image_map.map sq = sq.left ≫ factor_thru_image g.hom := (cancel_mono (image.ι g.hom)).1 $ by simp [arrow.w] variables {f g : arrow C} [has_image f.hom] [has_image g.hom] (sq : f ⟶ g) section local attribute [ext] has_image_map instance : subsingleton (has_image_map sq) := subsingleton.intro $ λ a b, has_image_map.ext a b $ (cancel_mono (image.ι g.hom)).1 $ by simp only [has_image_map.map_ι] end variable [has_image_map sq] /-- The map on images induced by a commutative square. -/ abbreviation image.map : image f.hom ⟶ image g.hom := has_image_map.map sq lemma image.factor_map : factor_thru_image f.hom ≫ image.map sq = sq.left ≫ factor_thru_image g.hom := by simp lemma image.map_ι : image.map sq ≫ image.ι g.hom = image.ι f.hom ≫ sq.right := by simp lemma image.map_hom_mk'_ι {X Y P Q : C} {k : X ⟶ Y} [has_image k] {l : P ⟶ Q} [has_image l] {m : X ⟶ P} {n : Y ⟶ Q} (w : m ≫ l = k ≫ n) [has_image_map (arrow.hom_mk' w)] : image.map (arrow.hom_mk' w) ≫ image.ι l = image.ι k ≫ n := image.map_ι _ section variables {h : arrow C} [has_image h.hom] (sq' : g ⟶ h) variables [has_image_map sq'] /-- Image maps for composable commutative squares induce an image map in the composite square. -/ def has_image_map_comp : has_image_map (sq ≫ sq') := { map := image.map sq ≫ image.map sq' } @[simp] lemma image.map_comp [has_image_map (sq ≫ sq')] : image.map (sq ≫ sq') = image.map sq ≫ image.map sq' := show (has_image_map.map (sq ≫ sq')) = (has_image_map_comp sq sq').map, by congr end section variables (f) /-- The identity `image f ⟶ image f` fits into the commutative square represented by the identity morphism `𝟙 f` in the arrow category. -/ def has_image_map_id : has_image_map (𝟙 f) := { map := 𝟙 (image f.hom) } @[simp] lemma image.map_id [has_image_map (𝟙 f)] : image.map (𝟙 f) = 𝟙 (image f.hom) := show (image.map (𝟙 f)) = (has_image_map_id f).map, by congr end end has_image_map section variables (C) [has_images.{v} C] /-- If a category `has_image_maps`, then all commutative squares induce morphisms on images. -/ class has_image_maps := (has_image_map : Π {f g : arrow C} (st : f ⟶ g), has_image_map st) attribute [instance, priority 100] has_image_maps.has_image_map end section has_image_maps variables [has_images.{v} C] [has_image_maps.{v} C] /-- The functor from the arrow category of `C` to `C` itself that maps a morphism to its image and a commutative square to the induced morphism on images. -/ @[simps] def im : arrow C ⥤ C := { obj := λ f, image f.hom, map := λ _ _ st, image.map st } end has_image_maps section strong_epi_mono_factorisation /-- A strong epi-mono factorisation is a decomposition `f = e ≫ m` with `e` a strong epimorphism and `m` a monomorphism. -/ structure strong_epi_mono_factorisation {X Y : C} (f : X ⟶ Y) extends mono_factorisation.{v} f := [e_strong_epi : strong_epi e] attribute [instance] strong_epi_mono_factorisation.e_strong_epi /-- Satisfying the inhabited linter -/ instance strong_epi_mono_factorisation_inhabited {X Y : C} (f : X ⟶ Y) [strong_epi f] : inhabited (strong_epi_mono_factorisation f) := ⟨⟨⟨Y, 𝟙 Y, f, by simp⟩⟩⟩ /-- A mono factorisation coming from a strong epi-mono factorisation always has the universal property of the image. -/ def strong_epi_mono_factorisation.to_mono_is_image {X Y : C} {f : X ⟶ Y} (F : strong_epi_mono_factorisation f) : is_image F.to_mono_factorisation := { lift := λ G, arrow.lift $ arrow.hom_mk' $ show G.e ≫ G.m = F.e ≫ F.m, by rw [F.to_mono_factorisation.fac, G.fac] } variable (C) /-- A category has strong epi-mono factorisations if every morphism admits a strong epi-mono factorisation. -/ class has_strong_epi_mono_factorisations := (has_fac : Π {X Y : C} (f : X ⟶ Y), strong_epi_mono_factorisation.{v} f) @[priority 100] instance has_images_of_has_strong_epi_mono_factorisations [has_strong_epi_mono_factorisations.{v} C] : has_images.{v} C := { has_image := λ X Y f, let F' := has_strong_epi_mono_factorisations.has_fac f in { F := F'.to_mono_factorisation, is_image := F'.to_mono_is_image } } end strong_epi_mono_factorisation section has_strong_epi_images variables (C) [has_images.{v} C] /-- A category has strong epi images if it has all images and `factor_thru_image f` is a strong epimorphism for all `f`. -/ class has_strong_epi_images := (strong_factor_thru_image : Π {X Y : C} (f : X ⟶ Y), strong_epi.{v} (factor_thru_image f)) attribute [instance] has_strong_epi_images.strong_factor_thru_image end has_strong_epi_images section has_strong_epi_images /-- If we constructed our images from strong epi-mono factorisations, then these images are strong epi images. -/ @[priority 100] instance has_strong_epi_images_of_has_strong_epi_mono_factorisations [has_strong_epi_mono_factorisations.{v} C] : has_strong_epi_images.{v} C := { strong_factor_thru_image := λ X Y f, (has_strong_epi_mono_factorisations.has_fac f).e_strong_epi } end has_strong_epi_images section has_strong_epi_images variables [has_images.{v} C] /-- A category with strong epi images has image maps. The construction is taken from Borceux, Handbook of Categorical Algebra 1, Proposition 4.4.5. -/ @[priority 100] instance has_image_maps_of_has_strong_epi_images [has_strong_epi_images.{v} C] : has_image_maps.{v} C := { has_image_map := λ f g st, let I := image (image.ι f.hom ≫ st.right) in let I' := image (st.left ≫ factor_thru_image g.hom) in let upper : strong_epi_mono_factorisation (f.hom ≫ st.right) := { I := I, e := factor_thru_image f.hom ≫ factor_thru_image (image.ι f.hom ≫ st.right), m := image.ι (image.ι f.hom ≫ st.right), e_strong_epi := strong_epi_comp _ _, m_mono := by apply_instance } in let lower : strong_epi_mono_factorisation (f.hom ≫ st.right) := { I := I', e := factor_thru_image (st.left ≫ factor_thru_image g.hom), m := image.ι (st.left ≫ factor_thru_image g.hom) ≫ image.ι g.hom, fac' := by simp [arrow.w], e_strong_epi := by apply_instance, m_mono := mono_comp _ _ } in let s : I ⟶ I' := is_image.lift upper.to_mono_is_image lower.to_mono_factorisation in { map := factor_thru_image (image.ι f.hom ≫ st.right) ≫ s ≫ image.ι (st.left ≫ factor_thru_image g.hom), map_ι' := by rw [category.assoc, category.assoc, is_image.lift_fac upper.to_mono_is_image lower.to_mono_factorisation, image.fac] } } end has_strong_epi_images end category_theory.limits
84cce86c513fba9466a4bb466a4b91fbfafcc4b2
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/data/list/forall2.lean
7dae13805a178dea2323c3ce1bb0eda16b42b070
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,621
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Johannes Hölzl -/ import data.list.basic /-! # Double universal quantification on a list This file provides an API for `list.forall₂` (definition in `data.list.defs`). `forall₂ r l₁ l₂` means that `∀ a ∈ l₁, ∀ b ∈ l₂, r a b`, where `l₁`, `l₂` are lists. -/ open nat function namespace list variables {α β γ δ : Type*} {r : α → β → Prop} {p : γ → δ → Prop} open relator mk_iff_of_inductive_prop list.forall₂ list.forall₂_iff @[simp] theorem forall₂_cons {R : α → β → Prop} {a b l₁ l₂} : forall₂ R (a :: l₁) (b :: l₂) ↔ R a b ∧ forall₂ R l₁ l₂ := ⟨λ h, by cases h with h₁ h₂; split; assumption, λ ⟨h₁, h₂⟩, forall₂.cons h₁ h₂⟩ theorem forall₂.imp {R S : α → β → Prop} (H : ∀ a b, R a b → S a b) {l₁ l₂} (h : forall₂ R l₁ l₂) : forall₂ S l₁ l₂ := by induction h; constructor; solve_by_elim lemma forall₂.mp {r q s : α → β → Prop} (h : ∀ a b, r a b → q a b → s a b) : ∀ {l₁ l₂}, forall₂ r l₁ l₂ → forall₂ q l₁ l₂ → forall₂ s l₁ l₂ | [] [] forall₂.nil forall₂.nil := forall₂.nil | (a :: l₁) (b :: l₂) (forall₂.cons hr hrs) (forall₂.cons hq hqs) := forall₂.cons (h a b hr hq) (forall₂.mp hrs hqs) lemma forall₂.flip : ∀ {a b}, forall₂ (flip r) b a → forall₂ r a b | _ _ forall₂.nil := forall₂.nil | (a :: as) (b :: bs) (forall₂.cons h₁ h₂) := forall₂.cons h₁ h₂.flip lemma forall₂_same {r : α → α → Prop} : ∀ {l}, (∀ x∈l, r x x) → forall₂ r l l | [] _ := forall₂.nil | (a :: as) h := forall₂.cons (h _ (mem_cons_self _ _)) (forall₂_same $ λ a ha, h a $ mem_cons_of_mem _ ha) lemma forall₂_refl {r} [is_refl α r] (l : list α) : forall₂ r l l := forall₂_same $ λ a h, is_refl.refl _ lemma forall₂_eq_eq_eq : forall₂ ((=) : α → α → Prop) = (=) := begin funext a b, apply propext, split, { intro h, induction h, {refl}, simp only [*]; split; refl }, { intro h, subst h, exact forall₂_refl _ } end @[simp, priority 900] lemma forall₂_nil_left_iff {l} : forall₂ r nil l ↔ l = nil := ⟨λ H, by cases H; refl, by rintro rfl; exact forall₂.nil⟩ @[simp, priority 900] lemma forall₂_nil_right_iff {l} : forall₂ r l nil ↔ l = nil := ⟨λ H, by cases H; refl, by rintro rfl; exact forall₂.nil⟩ lemma forall₂_cons_left_iff {a l u} : forall₂ r (a :: l) u ↔ (∃b u', r a b ∧ forall₂ r l u' ∧ u = b :: u') := iff.intro (λ h, match u, h with (b :: u'), forall₂.cons h₁ h₂ := ⟨b, u', h₁, h₂, rfl⟩ end) (λ h, match u, h with _, ⟨b, u', h₁, h₂, rfl⟩ := forall₂.cons h₁ h₂ end) lemma forall₂_cons_right_iff {b l u} : forall₂ r u (b :: l) ↔ (∃a u', r a b ∧ forall₂ r u' l ∧ u = a :: u') := iff.intro (λ h, match u, h with (b :: u'), forall₂.cons h₁ h₂ := ⟨b, u', h₁, h₂, rfl⟩ end) (λ h, match u, h with _, ⟨b, u', h₁, h₂, rfl⟩ := forall₂.cons h₁ h₂ end) lemma forall₂_and_left {r : α → β → Prop} {p : α → Prop} : ∀ l u, forall₂ (λa b, p a ∧ r a b) l u ↔ (∀ a∈l, p a) ∧ forall₂ r l u | [] u := by simp only [forall₂_nil_left_iff, forall_prop_of_false (not_mem_nil _), imp_true_iff, true_and] | (a :: l) u := by simp only [forall₂_and_left l, forall₂_cons_left_iff, forall_mem_cons, and_assoc, and_comm, and.left_comm, exists_and_distrib_left.symm] @[simp] lemma forall₂_map_left_iff {f : γ → α} : ∀ {l u}, forall₂ r (map f l) u ↔ forall₂ (λc b, r (f c) b) l u | [] _ := by simp only [map, forall₂_nil_left_iff] | (a :: l) _ := by simp only [map, forall₂_cons_left_iff, forall₂_map_left_iff] @[simp] lemma forall₂_map_right_iff {f : γ → β} : ∀ {l u}, forall₂ r l (map f u) ↔ forall₂ (λa c, r a (f c)) l u | _ [] := by simp only [map, forall₂_nil_right_iff] | _ (b :: u) := by simp only [map, forall₂_cons_right_iff, forall₂_map_right_iff] lemma left_unique_forall₂' (hr : left_unique r) : ∀ {a b c}, forall₂ r a c → forall₂ r b c → a = b | a₀ nil a₁ forall₂.nil forall₂.nil := rfl | (a₀ :: l₀) (b :: l) (a₁ :: l₁) (forall₂.cons ha₀ h₀) (forall₂.cons ha₁ h₁) := hr ha₀ ha₁ ▸ left_unique_forall₂' h₀ h₁ ▸ rfl lemma _root_.relator.left_unique.forall₂ (hr : left_unique r) : left_unique (forall₂ r) := @left_unique_forall₂' _ _ _ hr lemma right_unique_forall₂' (hr : right_unique r) : ∀ {a b c}, forall₂ r a b → forall₂ r a c → b = c | nil a₀ a₁ forall₂.nil forall₂.nil := rfl | (b :: l) (a₀ :: l₀) (a₁ :: l₁) (forall₂.cons ha₀ h₀) (forall₂.cons ha₁ h₁) := hr ha₀ ha₁ ▸ right_unique_forall₂' h₀ h₁ ▸ rfl lemma _root_.relator.right_unique.forall₂ (hr : right_unique r) : right_unique (forall₂ r) := @right_unique_forall₂' _ _ _ hr lemma _root_.relator.bi_unique.forall₂ (hr : bi_unique r) : bi_unique (forall₂ r) := ⟨hr.left.forall₂, hr.right.forall₂⟩ theorem forall₂_length_eq {R : α → β → Prop} : ∀ {l₁ l₂}, forall₂ R l₁ l₂ → length l₁ = length l₂ | _ _ forall₂.nil := rfl | _ _ (forall₂.cons h₁ h₂) := congr_arg succ (forall₂_length_eq h₂) theorem forall₂_zip {R : α → β → Prop} : ∀ {l₁ l₂}, forall₂ R l₁ l₂ → ∀ {a b}, (a, b) ∈ zip l₁ l₂ → R a b | _ _ (forall₂.cons h₁ h₂) x y (or.inl rfl) := h₁ | _ _ (forall₂.cons h₁ h₂) x y (or.inr h₃) := forall₂_zip h₂ h₃ theorem forall₂_iff_zip {R : α → β → Prop} {l₁ l₂} : forall₂ R l₁ l₂ ↔ length l₁ = length l₂ ∧ ∀ {a b}, (a, b) ∈ zip l₁ l₂ → R a b := ⟨λ h, ⟨forall₂_length_eq h, @forall₂_zip _ _ _ _ _ h⟩, λ h, begin cases h with h₁ h₂, induction l₁ with a l₁ IH generalizing l₂, { cases length_eq_zero.1 h₁.symm, constructor }, { cases l₂ with b l₂; injection h₁ with h₁, exact forall₂.cons (h₂ $ or.inl rfl) (IH h₁ $ λ a b h, h₂ $ or.inr h) } end⟩ theorem forall₂_take {R : α → β → Prop} : ∀ n {l₁ l₂}, forall₂ R l₁ l₂ → forall₂ R (take n l₁) (take n l₂) | 0 _ _ _ := by simp only [forall₂.nil, take] | (n+1) _ _ (forall₂.nil) := by simp only [forall₂.nil, take] | (n+1) _ _ (forall₂.cons h₁ h₂) := by simp [and.intro h₁ h₂, forall₂_take n] theorem forall₂_drop {R : α → β → Prop} : ∀ n {l₁ l₂}, forall₂ R l₁ l₂ → forall₂ R (drop n l₁) (drop n l₂) | 0 _ _ h := by simp only [drop, h] | (n+1) _ _ (forall₂.nil) := by simp only [forall₂.nil, drop] | (n+1) _ _ (forall₂.cons h₁ h₂) := by simp [and.intro h₁ h₂, forall₂_drop n] theorem forall₂_take_append {R : α → β → Prop} (l : list α) (l₁ : list β) (l₂ : list β) (h : forall₂ R l (l₁ ++ l₂)) : forall₂ R (list.take (length l₁) l) l₁ := have h': forall₂ R (take (length l₁) l) (take (length l₁) (l₁ ++ l₂)), from forall₂_take (length l₁) h, by rwa [take_left] at h' theorem forall₂_drop_append {R : α → β → Prop} (l : list α) (l₁ : list β) (l₂ : list β) (h : forall₂ R l (l₁ ++ l₂)) : forall₂ R (list.drop (length l₁) l) l₂ := have h': forall₂ R (drop (length l₁) l) (drop (length l₁) (l₁ ++ l₂)), from forall₂_drop (length l₁) h, by rwa [drop_left] at h' lemma rel_mem (hr : bi_unique r) : (r ⇒ forall₂ r ⇒ iff) (∈) (∈) | a b h [] [] forall₂.nil := by simp only [not_mem_nil] | a b h (a' :: as) (b' :: bs) (forall₂.cons h₁ h₂) := rel_or (rel_eq hr h h₁) (rel_mem h h₂) lemma rel_map : ((r ⇒ p) ⇒ forall₂ r ⇒ forall₂ p) map map | f g h [] [] forall₂.nil := forall₂.nil | f g h (a :: as) (b :: bs) (forall₂.cons h₁ h₂) := forall₂.cons (h h₁) (rel_map @h h₂) lemma rel_append : (forall₂ r ⇒ forall₂ r ⇒ forall₂ r) append append | [] [] h l₁ l₂ hl := hl | (a :: as) (b :: bs) (forall₂.cons h₁ h₂) l₁ l₂ hl := forall₂.cons h₁ (rel_append h₂ hl) lemma rel_reverse : (forall₂ r ⇒ forall₂ r) reverse reverse | [] [] forall₂.nil := forall₂.nil | (a :: as) (b :: bs) (forall₂.cons h₁ h₂) := begin simp only [reverse_cons], exact rel_append (rel_reverse h₂) (forall₂.cons h₁ forall₂.nil) end @[simp] lemma forall₂_reverse_iff {l₁ l₂} : forall₂ r (reverse l₁) (reverse l₂) ↔ forall₂ r l₁ l₂ := iff.intro (λ h, by { rw [← reverse_reverse l₁, ← reverse_reverse l₂], exact rel_reverse h }) (λ h, rel_reverse h) lemma rel_join : (forall₂ (forall₂ r) ⇒ forall₂ r) join join | [] [] forall₂.nil := forall₂.nil | (a :: as) (b :: bs) (forall₂.cons h₁ h₂) := rel_append h₁ (rel_join h₂) lemma rel_bind : (forall₂ r ⇒ (r ⇒ forall₂ p) ⇒ forall₂ p) list.bind list.bind := λ a b h₁ f g h₂, rel_join (rel_map @h₂ h₁) lemma rel_foldl : ((p ⇒ r ⇒ p) ⇒ p ⇒ forall₂ r ⇒ p) foldl foldl | f g hfg _ _ h _ _ forall₂.nil := h | f g hfg x y hxy _ _ (forall₂.cons hab hs) := rel_foldl @hfg (hfg hxy hab) hs lemma rel_foldr : ((r ⇒ p ⇒ p) ⇒ p ⇒ forall₂ r ⇒ p) foldr foldr | f g hfg _ _ h _ _ forall₂.nil := h | f g hfg x y hxy _ _ (forall₂.cons hab hs) := hfg hab (rel_foldr @hfg hxy hs) lemma rel_filter {p : α → Prop} {q : β → Prop} [decidable_pred p] [decidable_pred q] (hpq : (r ⇒ (↔)) p q) : (forall₂ r ⇒ forall₂ r) (filter p) (filter q) | _ _ forall₂.nil := forall₂.nil | (a :: as) (b :: bs) (forall₂.cons h₁ h₂) := begin by_cases p a, { have : q b, { rwa [← hpq h₁] }, simp only [filter_cons_of_pos _ h, filter_cons_of_pos _ this, forall₂_cons, h₁, rel_filter h₂, and_true], }, { have : ¬ q b, { rwa [← hpq h₁] }, simp only [filter_cons_of_neg _ h, filter_cons_of_neg _ this, rel_filter h₂], }, end theorem filter_map_cons (f : α → option β) (a : α) (l : list α) : filter_map f (a :: l) = option.cases_on (f a) (filter_map f l) (λb, b :: filter_map f l) := begin generalize eq : f a = b, cases b, { rw filter_map_cons_none _ _ eq }, { rw filter_map_cons_some _ _ _ eq }, end lemma rel_filter_map : ((r ⇒ option.rel p) ⇒ forall₂ r ⇒ forall₂ p) filter_map filter_map | f g hfg _ _ forall₂.nil := forall₂.nil | f g hfg (a :: as) (b :: bs) (forall₂.cons h₁ h₂) := by rw [filter_map_cons, filter_map_cons]; from match f a, g b, hfg h₁ with | _, _, option.rel.none := rel_filter_map @hfg h₂ | _, _, option.rel.some h := forall₂.cons h (rel_filter_map @hfg h₂) end @[to_additive] lemma rel_prod [monoid α] [monoid β] (h : r 1 1) (hf : (r ⇒ r ⇒ r) (*) (*)) : (forall₂ r ⇒ r) prod prod := rel_foldl hf h /-- Given a relation `r`, `sublist_forall₂ r l₁ l₂` indicates that there is a sublist of `l₂` such that `forall₂ r l₁ l₂`. -/ inductive sublist_forall₂ (r : α → β → Prop) : list α → list β → Prop | nil {l} : sublist_forall₂ [] l | cons {a₁ a₂ l₁ l₂} : r a₁ a₂ → sublist_forall₂ l₁ l₂ → sublist_forall₂ (a₁ :: l₁) (a₂ :: l₂) | cons_right {a l₁ l₂} : sublist_forall₂ l₁ l₂ → sublist_forall₂ l₁ (a :: l₂) lemma sublist_forall₂_iff {l₁ : list α} {l₂ : list β} : sublist_forall₂ r l₁ l₂ ↔ ∃ l, forall₂ r l₁ l ∧ l <+ l₂ := begin split; intro h, { induction h with _ a b l1 l2 rab rll ih b l1 l2 hl ih, { exact ⟨nil, forall₂.nil, nil_sublist _⟩ }, { obtain ⟨l, hl1, hl2⟩ := ih, refine ⟨b :: l, forall₂.cons rab hl1, cons_sublist_cons b hl2⟩ }, { obtain ⟨l, hl1, hl2⟩ := ih, exact ⟨l, hl1, hl2.trans (sublist.cons _ _ _ (sublist.refl _))⟩ } }, { obtain ⟨l, hl1, hl2⟩ := h, revert l₁, induction hl2 with _ _ _ _ ih _ _ _ _ ih; intros l₁ hl1, { rw [forall₂_nil_right_iff.1 hl1], exact sublist_forall₂.nil }, { exact sublist_forall₂.cons_right (ih hl1) }, { cases hl1 with _ _ _ _ hr hl _, exact sublist_forall₂.cons hr (ih hl) } } end variable {ra : α → α → Prop} instance sublist_forall₂.is_refl [is_refl α ra] : is_refl (list α) (sublist_forall₂ ra) := ⟨λ l, sublist_forall₂_iff.2 ⟨l, forall₂_refl l, sublist.refl l⟩⟩ instance sublist_forall₂.is_trans [is_trans α ra] : is_trans (list α) (sublist_forall₂ ra) := ⟨λ a b c, begin revert a b, induction c with _ _ ih, { rintros _ _ h1 (_ | _ | _), exact h1 }, { rintros a b h1 h2, cases h2 with _ _ _ _ _ hbc tbc _ _ y1 btc, { cases h1, exact sublist_forall₂.nil }, { cases h1 with _ _ _ _ _ hab tab _ _ _ atb, { exact sublist_forall₂.nil }, { exact sublist_forall₂.cons (trans hab hbc) (ih _ _ tab tbc) }, { exact sublist_forall₂.cons_right (ih _ _ atb tbc) } }, { exact sublist_forall₂.cons_right (ih _ _ h1 btc), } } end⟩ lemma sublist.sublist_forall₂ {l₁ l₂ : list α} (h : l₁ <+ l₂) (r : α → α → Prop) [is_refl α r] : sublist_forall₂ r l₁ l₂ := sublist_forall₂_iff.2 ⟨l₁, forall₂_refl l₁, h⟩ lemma tail_sublist_forall₂_self [is_refl α ra] (l : list α) : sublist_forall₂ ra l.tail l := l.tail_sublist.sublist_forall₂ ra end list
3af4c787ad48f64e5759c8bd5ef7a071d4d8484a
8cb37a089cdb4af3af9d8bf1002b417e407a8e9e
/tests/lean/run/coe_fn_mvar.lean
5322fdb0ecdb0f1191ddcec78ee72f357596effd
[ "Apache-2.0" ]
permissive
kbuzzard/lean
ae3c3db4bb462d750dbf7419b28bafb3ec983ef7
ed1788fd674bb8991acffc8fca585ec746711928
refs/heads/master
1,620,983,366,617
1,618,937,600,000
1,618,937,600,000
359,886,396
1
0
Apache-2.0
1,618,936,987,000
1,618,936,987,000
null
UTF-8
Lean
false
false
1,035
lean
structure hom (α β : Type*) := (f : α → β) instance {α β} : has_coe_to_fun (hom α β) := ⟨_, hom.f⟩ def frob {α β} (a : α) : hom β (α × β) := ⟨λ b, (a, b)⟩ -- `(frob 1 : hom ?m_1 (?m_2 × ?m_1))` has metavariables in the type def foo : ℤ × ℤ := frob 1 2 example : foo = (1, 2) := rfl -- backport elabissues/Reid1.lean from Lean 4 structure constantFunction (α β : Type) := (f : α → β) (h : ∀ a₁ a₂, f a₁ = f a₂) instance {α β : Type} : has_coe_to_fun (constantFunction α β) := ⟨_, constantFunction.f⟩ def myFun {α : Type} : constantFunction α (option α) := { f := fun a, none, h := fun a₁ a₂, rfl } def myFun' (α : Type) : constantFunction α (option α) := { f := fun a, none, h := fun a₁ a₂, rfl } #check myFun 3 #check @myFun nat 3 -- works #check myFun' _ 3 #check myFun' nat 3 -- works /- The single, double, and serif uparrows don't really work in Lean 3. #check ⇑myFun 3 #check ⇑(myFun' _) 3 #check ⇑(myFun' Nat) 3 -/
2717487de2889d3c2b03995d0cb3689bf45dd489
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/finset/interval.lean
3f1181ef4939170d58e35b7f2bbc6082a8e0d516
[ "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
3,938
lean
/- Copyright (c) 2021 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import data.finset.locally_finite /-! # Intervals of finsets as finsets This file provides the `locally_finite_order` instance for `finset α` and calculates the cardinality of finite intervals of finsets. If `s t : finset α`, then `finset.Icc s t` is the finset of finsets which include `s` and are included in `t`. For example, `finset.Icc {0, 1} {0, 1, 2, 3} = {{0, 1}, {0, 1, 2}, {0, 1, 3}, {0, 1, 2, 3}}` and `finset.Icc {0, 1, 2} {0, 1, 3} = {}`. -/ variables {α : Type*} namespace finset variables [decidable_eq α] (s t : finset α) instance : locally_finite_order (finset α) := { finset_Icc := λ s t, t.powerset.filter ((⊆) s), finset_Ico := λ s t, t.ssubsets.filter ((⊆) s), finset_Ioc := λ s t, t.powerset.filter ((⊂) s), finset_Ioo := λ s t, t.ssubsets.filter ((⊂) s), finset_mem_Icc := λ s t u, by {rw [mem_filter, mem_powerset], exact and_comm _ _ }, finset_mem_Ico := λ s t u, by {rw [mem_filter, mem_ssubsets], exact and_comm _ _ }, finset_mem_Ioc := λ s t u, by {rw [mem_filter, mem_powerset], exact and_comm _ _ }, finset_mem_Ioo := λ s t u, by {rw [mem_filter, mem_ssubsets], exact and_comm _ _ } } lemma Icc_eq_filter_powerset : Icc s t = t.powerset.filter ((⊆) s) := rfl lemma Ico_eq_filter_ssubsets : Ico s t = t.ssubsets.filter ((⊆) s) := rfl lemma Ioc_eq_filter_powerset : Ioc s t = t.powerset.filter ((⊂) s) := rfl lemma Ioo_eq_filter_ssubsets : Ioo s t = t.ssubsets.filter ((⊂) s) := rfl lemma Iic_eq_powerset : Iic s = s.powerset := filter_true_of_mem $ λ t _, empty_subset t lemma Iio_eq_ssubsets : Iio s = s.ssubsets := filter_true_of_mem $ λ t _, empty_subset t variables {s t} lemma Icc_eq_image_powerset (h : s ⊆ t) : Icc s t = (t \ s).powerset.image ((∪) s) := begin ext u, simp_rw [mem_Icc, mem_image, exists_prop, mem_powerset], split, { rintro ⟨hs, ht⟩, exact ⟨u \ s, sdiff_le_sdiff_right ht, sup_sdiff_cancel_right hs⟩ }, { rintro ⟨v, hv, rfl⟩, exact ⟨le_sup_left, union_subset h $ hv.trans $ sdiff_subset _ _⟩ } end lemma Ico_eq_image_ssubsets (h : s ⊆ t) : Ico s t = (t \ s).ssubsets.image ((∪) s) := begin ext u, simp_rw [mem_Ico, mem_image, exists_prop, mem_ssubsets], split, { rintro ⟨hs, ht⟩, exact ⟨u \ s, sdiff_lt_sdiff_right ht hs, sup_sdiff_cancel_right hs⟩ }, { rintro ⟨v, hv, rfl⟩, exact ⟨le_sup_left, sup_lt_of_lt_sdiff_left hv h⟩ } end /-- Cardinality of a non-empty `Icc` of finsets. -/ lemma card_Icc_finset (h : s ⊆ t) : (Icc s t).card = 2 ^ (t.card - s.card) := begin rw [←card_sdiff h, ←card_powerset, Icc_eq_image_powerset h, finset.card_image_iff], rintro u hu v hv (huv : s ⊔ u = s ⊔ v), rw [mem_coe, mem_powerset] at hu hv, rw [←(disjoint_sdiff.mono_right hu : disjoint s u).sup_sdiff_cancel_left, ←(disjoint_sdiff.mono_right hv : disjoint s v).sup_sdiff_cancel_left, huv], end /-- Cardinality of an `Ico` of finsets. -/ lemma card_Ico_finset (h : s ⊆ t) : (Ico s t).card = 2 ^ (t.card - s.card) - 1 := by rw [card_Ico_eq_card_Icc_sub_one, card_Icc_finset h] /-- Cardinality of an `Ioc` of finsets. -/ lemma card_Ioc_finset (h : s ⊆ t) : (Ioc s t).card = 2 ^ (t.card - s.card) - 1 := by rw [card_Ioc_eq_card_Icc_sub_one, card_Icc_finset h] /-- Cardinality of an `Ioo` of finsets. -/ lemma card_Ioo_finset (h : s ⊆ t) : (Ioo s t).card = 2 ^ (t.card - s.card) - 2 := by rw [card_Ioo_eq_card_Icc_sub_two, card_Icc_finset h] /-- Cardinality of an `Iic` of finsets. -/ lemma card_Iic_finset : (Iic s).card = 2 ^ s.card := by rw [Iic_eq_powerset, card_powerset] /-- Cardinality of an `Iio` of finsets. -/ lemma card_Iio_finset : (Iio s).card = 2 ^ s.card - 1 := by rw [Iio_eq_ssubsets, ssubsets, card_erase_of_mem (mem_powerset_self _), card_powerset] end finset
3edf9f1f5ea4a7bedcfbd966c85ffa67b63daa5d
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Init/Hints.lean
e8a2904e63bb280d8ee484f9117a7f6575fbce87
[ "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
371
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.NotationExtra /- Hint for making sure `Not p` is definitionally equal to `p → False` even when `TransparencyMode.reducible` -/ unif_hint (p : Prop) where |- Not p =?= p → False
b9284cbeb2f1f0ae517b3655683a958529687573
3b15c7b0b62d8ada1399c112ad88a529e6bfa115
/stage0/src/Lean/Meta/Tactic/Cleanup.lean
a970fb57f3b41f1351d7c732c717658cb8be4ddd
[ "Apache-2.0" ]
permissive
stephenbrady/lean4
74bf5cae8a433e9c815708ce96c9e54a5caf2115
b1bd3fc304d0f7bc6810ec78bfa4c51476d263f9
refs/heads/master
1,692,621,473,161
1,634,308,743,000
1,634,310,749,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,563
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.CollectFVars import Lean.Meta.Tactic.Clear namespace Lean.Meta /-- Auxiliary tactic for cleaning the local context. It removes local declarations (aka hypotheses) that are *not* relevant. We say a variable `x` is "relevant" if - It occurs in the target type, or - There is a relevant variable `y` that depends on `x`, or - The type of `x` is a proposition and it depends on a relevant variable `y`. -/ partial def cleanup (mvarId : MVarId) : MetaM MVarId := do withMVarContext mvarId do checkNotAssigned mvarId `cleanup let used ← collectUsed |>.run' (false, {}) let mut lctx ← getLCtx for localDecl in lctx do unless used.contains localDecl.fvarId do lctx := lctx.erase localDecl.fvarId let localInsts := (← getLocalInstances).filter fun inst => used.contains inst.fvar.fvarId! let mvarNew ← mkFreshExprMVarAt lctx localInsts (← getMVarType' mvarId) MetavarKind.syntheticOpaque (← getMVarTag mvarId) assignExprMVar mvarId mvarNew return mvarNew.mvarId! where addUsedFVars (e : Expr) : StateRefT (Bool × FVarIdSet) MetaM Unit := do let (_, s) ← collectUsedFVars (← instantiateMVars e) |>.run {} for fvarId in s.fvarSet do addUsedFVar fvarId addDeps (fvarId : FVarId) : StateRefT (Bool × FVarIdSet) MetaM Unit := do let localDecl ← getLocalDecl fvarId addUsedFVars localDecl.type if let some val := localDecl.value? then addUsedFVars val addUsedFVar (fvarId : FVarId) : StateRefT (Bool × FVarIdSet) MetaM Unit := do unless (← get).2.contains fvarId do modify fun (modified, s) => (true, s.insert fvarId) addDeps fvarId /- We include `p` in the used-set, if `p` is a proposition that contains a `x` that is in the used-set. -/ collectPropsStep : StateRefT (Bool × FVarIdSet) MetaM Unit := do let usedSet := (← get).2 for localDecl in (← getLCtx) do if (← isProp localDecl.type) then if (← dependsOnPred localDecl.type usedSet.contains) then addUsedFVar localDecl.fvarId collectProps : StateRefT (Bool × FVarIdSet) MetaM Unit := do modify fun s => (false, s.2) collectPropsStep if (← get).1 then collectProps collectUsed : StateRefT (Bool × FVarIdSet) MetaM FVarIdSet := do addUsedFVars (← getMVarType' mvarId) collectProps return (← get).2 end Lean.Meta
4fb1da1446807a690b75555a0d1692e9c403de9e
ac89c256db07448984849346288e0eeffe8b20d0
/tests/lean/run/simp7.lean
8e10267936f4759852f6537417bf9de81db79112
[ "Apache-2.0" ]
permissive
chepinzhang/lean4
002cc667f35417a418f0ebc9cb4a44559bb0ccac
24fe2875c68549b5481f07c57eab4ad4a0ae5305
refs/heads/master
1,688,942,838,326
1,628,801,942,000
1,628,801,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,404
lean
def f (x : α) := x theorem ex1 (a : α) (b : List α) : f (a::b = []) = False := by simp [f] def length : List α → Nat | [] => 0 | a::as => length as + 1 theorem ex2 (a b c : α) (as : List α) : length (a :: b :: as) > length as := by simp [length] apply Nat.lt.step apply Nat.lt_succ_self def fact : Nat → Nat | 0 => 1 | x+1 => (x+1) * fact x theorem ex3 : fact x > 0 := by induction x with | zero => rfl | succ x ih => simp [fact] apply Nat.mul_pos apply Nat.zero_lt_succ apply ih def head [Inhabited α] : List α → α | [] => arbitrary | a::_ => a theorem ex4 [Inhabited α] (a : α) (as : List α) : head (a::as) = a := by simp [head] def foo := 10 theorem ex5 (x : Nat) : foo + x = 10 + x := by simp [foo] done def g (x : Nat) : Nat := do let x := x return x theorem ex6 : g x = x := by simp [g, bind, pure] def f1 : StateM Nat Unit := do modify fun x => g x def f2 : StateM Nat Unit := do let s ← get set <| g s theorem ex7 : f1 = f2 := by simp [f1, f2, bind, StateT.bind, get, getThe, MonadStateOf.get, StateT.get, pure, set, StateT.set, modify, modifyGet, MonadStateOf.modifyGet, StateT.modifyGet] def h (x : Nat) : Sum (Nat × Nat) Nat := Sum.inl (x, x) def bla (x : Nat) := match h x with | Sum.inl (y, z) => y + z | Sum.inr _ => 0 theorem ex8 (x : Nat) : bla x = x + x := by simp [bla, h]
88d01d11ed636ac58999bb67dde5dcbbf25654a9
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/no_coe.lean
cca344d3e6ef2799295213023aad8fb8ed59dd58
[ "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
126
lean
#check if tt then 1 else 0 set_option elaborator.coercions false #check if tt then 1 else 0 -- Error coercions are disabled
c35366d899a5abb32f49520c64464b59996ee9fd
957a80ea22c5abb4f4670b250d55534d9db99108
/library/init/meta/name.lean
f146b933758a8a5e7004564aa9a0fcbffc07ddec
[ "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
3,401
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.data.ordering init.coe init.data.to_string /-- Reflect a C++ name object. The VM replaces it with the C++ implementation. -/ inductive name | anonymous : name | mk_string : string → name → name | mk_numeral : unsigned → name → name /-- Gadget for automatic parameter support. This is similar to the opt_param gadget, but it uses the tactic declaration names tac_name to synthesize the argument. Like opt_param, this gadget only affects elaboration. For example, the tactic will *not* be invoked during type class resolution. -/ @[reducible] def {u} auto_param (α : Sort u) (tac_name : name) : Sort u := α instance : inhabited name := ⟨name.anonymous⟩ def mk_str_name (n : name) (s : string) : name := name.mk_string s n def mk_num_name (n : name) (v : nat) : name := name.mk_numeral (unsigned.of_nat' v) n def mk_simple_name (s : string) : name := mk_str_name name.anonymous s instance string_to_name : has_coe string name := ⟨mk_simple_name⟩ infix ` <.> `:65 := mk_str_name open name def name.get_prefix : name → name | anonymous := anonymous | (mk_string s p) := p | (mk_numeral s p) := p def name.update_prefix : name → name → name | anonymous new_p := anonymous | (mk_string s p) new_p := mk_string s new_p | (mk_numeral s p) new_p := mk_numeral s new_p def name.to_string_with_sep (sep : string) : name → string | anonymous := "[anonymous]" | (mk_string s anonymous) := s | (mk_numeral v anonymous) := repr v | (mk_string s n) := name.to_string_with_sep n ++ sep ++ s | (mk_numeral v n) := name.to_string_with_sep n ++ sep ++ repr v private def name.components' : name -> list name | anonymous := [] | (mk_string s n) := mk_string s anonymous :: name.components' n | (mk_numeral v n) := mk_numeral v anonymous :: name.components' n def name.components (n : name) : list name := (name.components' n).reverse protected def name.to_string : name → string := name.to_string_with_sep "." instance : has_to_string name := ⟨name.to_string⟩ /- TODO(Leo): provide a definition in Lean. -/ meta constant name.has_decidable_eq : decidable_eq name /- Both cmp and lex_cmp are total orders, but lex_cmp implements a lexicographical order. -/ meta constant name.cmp : name → name → ordering meta constant name.lex_cmp : name → name → ordering meta constant name.append : name → name → name meta constant name.is_internal : name → bool attribute [instance] name.has_decidable_eq meta instance : has_ordering name := ⟨name.cmp⟩ meta instance : has_append name := ⟨name.append⟩ /-- `name.append_after n i` return a name of the form n_i -/ meta constant name.append_after : name → nat → name meta def name.is_prefix_of : name → name → bool | p name.anonymous := ff | p n := if p = n then tt else name.is_prefix_of p n.get_prefix meta def name.replace_prefix : name → name → name → name | anonymous p p' := anonymous | (mk_string s c) p p' := if c = p then mk_string s p' else mk_string s (name.replace_prefix c p p') | (mk_numeral v c) p p' := if c = p then mk_numeral v p' else mk_numeral v (name.replace_prefix c p p')
98319a3115e5e04ee9088966d1e2339d614382a7
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/1253.lean
b3f1543b7a8d4064a2863b9807d58af6b147942d
[ "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
471
lean
open list lemma induction₂ {α₁ α₂ : Type*} (p : list α₁ → list α₂ → Prop) (h_base : p [] []) (h_step : ∀ {xs₁ xs₂}, p xs₁ xs₂ → (∀ x₁ x₂, p (x₁::xs₁) (x₂::xs₂))) : Π (xs₁ : list α₁) (xs₂ : list α₂) (H_same_len : length xs₁ = length xs₂), p xs₁ xs₂ | [] [] h := h_base | (x₁::xs₁) (x₂::xs₂) h := h_step (induction₂ xs₁ xs₂ sorry) x₁ x₂
9e8a3ed25f312d1320d13e4a1c920f1fe0360538
5756a081670ba9c1d1d3fca7bd47cb4e31beae66
/Mathport/Syntax/Translate/Tactic/Mathlib/Clear.lean
142544d2ef6f37612c4c258c1d8e601c28b80b4c
[ "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
512
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathport.Syntax.Translate.Tactic.Basic import Mathport.Syntax.Translate.Tactic.Lean3 open Lean namespace Mathport.Translate.Tactic open Parser -- # tactic.clear attribute [tr_tactic clear'] trClear @[tr_tactic clear_dependent] def trClearDependent : TacM Syntax.Tactic := do `(tactic| clear! $[$((← parse ident*).map Lean.mkIdent)]*)
6950e3de0abd8c930b6d535b94d07f77dde4b797
61ccc57f9d72048e493dd6969b56ebd7f0a8f9e8
/src/topology/metric_space/antilipschitz.lean
b95a2ca6e4997121acffc973c7bf189945ca1781
[ "Apache-2.0" ]
permissive
jtristan/mathlib
375b3c8682975df28f79f53efcb7c88840118467
8fa8f175271320d675277a672f59ec53abd62f10
refs/heads/master
1,651,072,765,551
1,588,255,641,000
1,588,255,641,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,457
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 topology.metric_space.lipschitz /-! # Antilipschitz functions We say that a map `f : α → β` between two (extended) metric spaces is `antilipschitz_with K`, `K ≥ 0`, if for all `x, y` we have `edist x y ≤ K * edist (f x) (f y)`. For a metric space, the latter inequality is equivalent to `dist x y ≤ K * dist (f x) (f y)`. ## Implementation notes The parameter `K` has type `nnreal`. This way we avoid conjuction in the definition and have coercions both to `ℝ` and `ennreal`. We do not require `0 < K` in the definition, mostly because we do not have a `posreal` type. -/ variables {α : Type*} {β : Type*} {γ : Type*} open_locale nnreal open set /-- We say that `f : α → β` is `antilipschitz_with K` if for any two points `x`, `y` we have `K * edist x y ≤ edist (f x) (f y)`. -/ def antilipschitz_with [emetric_space α] [emetric_space β] (K : ℝ≥0) (f : α → β) := ∀ x y, edist x y ≤ K * edist (f x) (f y) lemma antilipschitz_with_iff_le_mul_dist [metric_space α] [metric_space β] {K : ℝ≥0} {f : α → β} : antilipschitz_with K f ↔ ∀ x y, dist x y ≤ K * dist (f x) (f y) := by { simp only [antilipschitz_with, edist_nndist, dist_nndist], norm_cast } alias antilipschitz_with_iff_le_mul_dist ↔ antilipschitz_with.le_mul_dist antilipschitz_with.of_le_mul_dist lemma antilipschitz_with.mul_le_dist [metric_space α] [metric_space β] {K : ℝ≥0} {f : α → β} (hf : antilipschitz_with K f) (x y : α) : ↑K⁻¹ * dist x y ≤ dist (f x) (f y) := begin by_cases hK : K = 0, by simp [hK, dist_nonneg], rw [nnreal.coe_inv, ← div_eq_inv_mul'], apply div_le_of_le_mul (nnreal.coe_pos.2 $ zero_lt_iff_ne_zero.2 hK), exact hf.le_mul_dist x y end namespace antilipschitz_with variables [emetric_space α] [emetric_space β] [emetric_space γ] {K : ℝ≥0} {f : α → β} /-- Extract the constant from `hf : antilipschitz_with K f`. This is useful, e.g., if `K` is given by a long formula, and we want to reuse this value. -/ @[nolint unused_arguments] -- uses neither `f` nor `hf` protected def K (hf : antilipschitz_with K f) : ℝ≥0 := K protected lemma injective (hf : antilipschitz_with K f) : function.injective f := λ x y h, by simpa only [h, edist_self, mul_zero, edist_le_zero] using hf x y lemma mul_le_edist (hf : antilipschitz_with K f) (x y : α) : ↑K⁻¹ * edist x y ≤ edist (f x) (f y) := begin by_cases hK : K = 0, by simp [hK], rw [ennreal.coe_inv hK, mul_comm, ← ennreal.div_def], apply ennreal.div_le_of_le_mul, rw mul_comm, exact hf x y end protected lemma id : antilipschitz_with 1 (id : α → α) := λ x y, by simp only [ennreal.coe_one, one_mul, id, le_refl] lemma comp {Kg : ℝ≥0} {g : β → γ} (hg : antilipschitz_with Kg g) {Kf : ℝ≥0} {f : α → β} (hf : antilipschitz_with Kf f) : antilipschitz_with (Kf * Kg) (g ∘ f) := λ x y, calc edist x y ≤ Kf * edist (f x) (f y) : hf x y ... ≤ Kf * (Kg * edist (g (f x)) (g (f y))) : ennreal.mul_left_mono (hg _ _) ... = _ : by rw [ennreal.coe_mul, mul_assoc] lemma restrict (hf : antilipschitz_with K f) (s : set α) : antilipschitz_with K (s.restrict f) := λ x y, hf x y lemma cod_restrict (hf : antilipschitz_with K f) {s : set β} (hs : ∀ x, f x ∈ s) : antilipschitz_with K (s.cod_restrict f hs) := λ x y, hf x y lemma to_right_inv_on' {s : set α} (hf : antilipschitz_with K (s.restrict f)) {g : β → α} {t : set β} (g_maps : maps_to g t s) (g_inv : right_inv_on g f t) : lipschitz_with K (t.restrict g) := λ x y, by simpa only [restrict_apply, g_inv x.mem, g_inv y.mem, subtype.edist_eq, subtype.coe_mk] using hf ⟨g x, g_maps x.mem⟩ ⟨g y, g_maps y.mem⟩ lemma to_right_inv_on (hf : antilipschitz_with K f) {g : β → α} {t : set β} (h : right_inv_on g f t) : lipschitz_with K (t.restrict g) := (hf.restrict univ).to_right_inv_on' (maps_to_univ g t) h lemma to_right_inverse (hf : antilipschitz_with K f) {g : β → α} (hg : function.right_inverse g f) : lipschitz_with K g := begin intros x y, have := hf (g x) (g y), rwa [hg x, hg y] at this end lemma uniform_embedding (hf : antilipschitz_with K f) (hfc : uniform_continuous f) : uniform_embedding f := begin refine emetric.uniform_embedding_iff.2 ⟨hf.injective, hfc, λ δ δ0, _⟩, by_cases hK : K = 0, { refine ⟨1, ennreal.zero_lt_one, λ x y _, lt_of_le_of_lt _ δ0⟩, simpa only [hK, ennreal.coe_zero, zero_mul] using hf x y }, { refine ⟨K⁻¹ * δ, _, λ x y hxy, lt_of_le_of_lt (hf x y) _⟩, { exact canonically_ordered_semiring.mul_pos.2 ⟨ennreal.inv_pos.2 ennreal.coe_ne_top, δ0⟩ }, { rw [mul_comm, ← ennreal.div_def] at hxy, have := ennreal.mul_lt_of_lt_div hxy, rwa mul_comm } } end lemma subtype_coe (s : set α) : antilipschitz_with 1 (coe : s → α) := antilipschitz_with.id.restrict s lemma of_subsingleton [subsingleton α] {K : ℝ≥0} : antilipschitz_with K f := λ x y, by simp only [subsingleton.elim x y, edist_self, zero_le] end antilipschitz_with lemma lipschitz_with.to_right_inverse [emetric_space α] [emetric_space β] {K : ℝ≥0} {f : α → β} (hf : lipschitz_with K f) {g : β → α} (hg : function.right_inverse g f) : antilipschitz_with K g := λ x y, by simpa only [hg _] using hf (g x) (g y)
9bbb82f84bbcfe9984b56b81e69cf20c75efc0f4
94e33a31faa76775069b071adea97e86e218a8ee
/src/category_theory/limits/constructions/over/default.lean
09025a23cc049bc4eb3b804e37d32ed50ab27221
[ "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
2,779
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Reid Barton, Bhavik Mehta -/ import category_theory.limits.connected import category_theory.limits.constructions.over.products import category_theory.limits.constructions.over.connected import category_theory.limits.constructions.limits_of_products_and_equalizers import category_theory.limits.constructions.equalizers /-! # Limits in the over category Declare instances for limits in the over category: If `C` has finite wide pullbacks, `over B` has finite limits, and if `C` has arbitrary wide pullbacks then `over B` has limits. -/ universes w v u -- morphism levels before object levels. See note [category_theory universes]. open category_theory category_theory.limits variables {C : Type u} [category.{v} C] variable {X : C} namespace category_theory.over /-- Make sure we can derive pullbacks in `over B`. -/ instance {B : C} [has_pullbacks C] : has_pullbacks (over B) := begin letI : has_limits_of_shape (ulift_hom.{v} (ulift.{v} walking_cospan)) C := has_limits_of_shape_of_equivalence (ulift_hom_ulift_category.equiv.{v} _), letI : category (ulift_hom.{v} (ulift.{v} walking_cospan)) := infer_instance, exact has_limits_of_shape_of_equivalence (ulift_hom_ulift_category.equiv.{v v} _).symm, end /-- Make sure we can derive equalizers in `over B`. -/ instance {B : C} [has_equalizers C] : has_equalizers (over B) := begin letI : has_limits_of_shape (ulift_hom.{v} (ulift.{v} walking_parallel_pair)) C := has_limits_of_shape_of_equivalence (ulift_hom_ulift_category.equiv.{v} _), letI : category (ulift_hom.{v} (ulift.{v} walking_parallel_pair)) := infer_instance, exact has_limits_of_shape_of_equivalence (ulift_hom_ulift_category.equiv.{v v} _).symm, end instance has_finite_limits {B : C} [has_finite_wide_pullbacks C] : has_finite_limits (over B) := begin apply @finite_limits_from_equalizers_and_finite_products _ _ _ _, { exact construct_products.over_finite_products_of_finite_wide_pullbacks, }, { apply @has_equalizers_of_pullbacks_and_binary_products _ _ _ _, { haveI : has_pullbacks C := ⟨by apply_instance⟩, exact construct_products.over_binary_product_of_pullback }, { apply_instance, } } end instance has_limits {B : C} [has_wide_pullbacks.{w} C] : has_limits_of_size.{w} (over B) := begin apply @limits_from_equalizers_and_products _ _ _ _, { exact construct_products.over_products_of_wide_pullbacks }, { apply @has_equalizers_of_pullbacks_and_binary_products _ _ _ _, { haveI : has_pullbacks C := ⟨infer_instance⟩, exact construct_products.over_binary_product_of_pullback }, { apply_instance, } } end end category_theory.over
86181798cef04d03b685189306abf790e6546046
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/analysis/asymptotics/specific_asymptotics.lean
ef8af93edbb0a260e69b741969c537b650ab14dd
[ "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
6,913
lean
/- Copyright (c) 2021 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker -/ import analysis.normed.order.basic import analysis.asymptotics.asymptotics /-! # A collection of specific asymptotic results This file contains specific lemmas about asymptotics which don't have their place in the general theory developped in `analysis.asymptotics.asymptotics`. -/ open filter asymptotics open_locale topological_space section normed_field /-- If `f : 𝕜 → E` is bounded in a punctured neighborhood of `a`, then `f(x) = o((x - a)⁻¹)` as `x → a`, `x ≠ a`. -/ lemma filter.is_bounded_under.is_o_sub_self_inv {𝕜 E : Type*} [normed_field 𝕜] [has_norm E] {a : 𝕜} {f : 𝕜 → E} (h : is_bounded_under (≤) (𝓝[≠] a) (norm ∘ f)) : f =o[𝓝[≠] a] (λ x, (x - a)⁻¹) := begin refine (h.is_O_const (one_ne_zero' ℝ)).trans_is_o (is_o_const_left.2 $ or.inr _), simp only [(∘), norm_inv], exact (tendsto_norm_sub_self_punctured_nhds a).inv_tendsto_zero end end normed_field section linear_ordered_field variables {𝕜 : Type*} [linear_ordered_field 𝕜] lemma pow_div_pow_eventually_eq_at_top {p q : ℕ} : (λ x : 𝕜, x^p / x^q) =ᶠ[at_top] (λ x, x^((p : ℤ) -q)) := begin apply ((eventually_gt_at_top (0 : 𝕜)).mono (λ x hx, _)), simp [zpow_sub₀ hx.ne'], end lemma pow_div_pow_eventually_eq_at_bot {p q : ℕ} : (λ x : 𝕜, x^p / x^q) =ᶠ[at_bot] (λ x, x^((p : ℤ) -q)) := begin apply ((eventually_lt_at_bot (0 : 𝕜)).mono (λ x hx, _)), simp [zpow_sub₀ hx.ne], end lemma tendsto_zpow_at_top_at_top {n : ℤ} (hn : 0 < n) : tendsto (λ x : 𝕜, x^n) at_top at_top := begin lift n to ℕ using hn.le, simp only [zpow_coe_nat], exact tendsto_pow_at_top (nat.cast_pos.mp hn).ne' end lemma tendsto_pow_div_pow_at_top_at_top {p q : ℕ} (hpq : q < p) : tendsto (λ x : 𝕜, x^p / x^q) at_top at_top := begin rw tendsto_congr' pow_div_pow_eventually_eq_at_top, apply tendsto_zpow_at_top_at_top, linarith end lemma tendsto_pow_div_pow_at_top_zero [topological_space 𝕜] [order_topology 𝕜] {p q : ℕ} (hpq : p < q) : tendsto (λ x : 𝕜, x^p / x^q) at_top (𝓝 0) := begin rw tendsto_congr' pow_div_pow_eventually_eq_at_top, apply tendsto_zpow_at_top_zero, linarith end end linear_ordered_field section normed_linear_ordered_field variables {𝕜 : Type*} [normed_linear_ordered_field 𝕜] lemma asymptotics.is_o_pow_pow_at_top_of_lt [order_topology 𝕜] {p q : ℕ} (hpq : p < q) : (λ x : 𝕜, x^p) =o[at_top] (λ x, x^q) := begin refine (is_o_iff_tendsto' _).mpr (tendsto_pow_div_pow_at_top_zero hpq), exact (eventually_gt_at_top 0).mono (λ x hx hxq, (pow_ne_zero q hx.ne' hxq).elim), end lemma asymptotics.is_O.trans_tendsto_norm_at_top {α : Type*} {u v : α → 𝕜} {l : filter α} (huv : u =O[l] v) (hu : tendsto (λ x, ‖u x‖) l at_top) : tendsto (λ x, ‖v x‖) l at_top := begin rcases huv.exists_pos with ⟨c, hc, hcuv⟩, rw is_O_with at hcuv, convert tendsto.at_top_div_const hc (tendsto_at_top_mono' l hcuv hu), ext x, rw mul_div_cancel_left _ hc.ne.symm, end end normed_linear_ordered_field section real open_locale big_operators open finset lemma asymptotics.is_o.sum_range {α : Type*} [normed_add_comm_group α] {f : ℕ → α} {g : ℕ → ℝ} (h : f =o[at_top] g) (hg : 0 ≤ g) (h'g : tendsto (λ n, ∑ i in range n, g i) at_top at_top) : (λ n, ∑ i in range n, f i) =o[at_top] (λ n, ∑ i in range n, g i) := begin have A : ∀ i, ‖g i‖ = g i := λ i, real.norm_of_nonneg (hg i), have B : ∀ n, ‖∑ i in range n, g i‖ = ∑ i in range n, g i, from λ n, by rwa [real.norm_eq_abs, abs_sum_of_nonneg'], apply is_o_iff.2 (λ ε εpos, _), obtain ⟨N, hN⟩ : ∃ (N : ℕ), ∀ (b : ℕ), N ≤ b → ‖f b‖ ≤ ε / 2 * g b, by simpa only [A, eventually_at_top] using is_o_iff.mp h (half_pos εpos), have : (λ (n : ℕ), ∑ i in range N, f i) =o[at_top] (λ (n : ℕ), ∑ i in range n, g i), { apply is_o_const_left.2, exact or.inr (h'g.congr (λ n, (B n).symm)) }, filter_upwards [is_o_iff.1 this (half_pos εpos), Ici_mem_at_top N] with n hn Nn, calc ‖∑ i in range n, f i‖ = ‖∑ i in range N, f i + ∑ i in Ico N n, f i‖ : by rw sum_range_add_sum_Ico _ Nn ... ≤ ‖∑ i in range N, f i‖ + ‖∑ i in Ico N n, f i‖ : norm_add_le _ _ ... ≤ ‖∑ i in range N, f i‖ + ∑ i in Ico N n, (ε / 2) * g i : add_le_add le_rfl (norm_sum_le_of_le _ (λ i hi, hN _ (mem_Ico.1 hi).1)) ... ≤ ‖∑ i in range N, f i‖ + ∑ i in range n, (ε / 2) * g i : begin refine add_le_add le_rfl _, apply sum_le_sum_of_subset_of_nonneg, { rw range_eq_Ico, exact Ico_subset_Ico (zero_le _) le_rfl }, { assume i hi hident, exact mul_nonneg (half_pos εpos).le (hg i) } end ... ≤ (ε / 2) * ‖∑ i in range n, g i‖ + (ε / 2) * (∑ i in range n, g i) : begin rw ← mul_sum, exact add_le_add hn (mul_le_mul_of_nonneg_left le_rfl (half_pos εpos).le), end ... = ε * ‖(∑ i in range n, g i)‖ : by { simp [B], ring } end lemma asymptotics.is_o_sum_range_of_tendsto_zero {α : Type*} [normed_add_comm_group α] {f : ℕ → α} (h : tendsto f at_top (𝓝 0)) : (λ n, ∑ i in range n, f i) =o[at_top] (λ n, (n : ℝ)) := begin have := ((is_o_one_iff ℝ).2 h).sum_range (λ i, zero_le_one), simp only [sum_const, card_range, nat.smul_one_eq_coe] at this, exact this tendsto_coe_nat_at_top_at_top end /-- The Cesaro average of a converging sequence converges to the same limit. -/ lemma filter.tendsto.cesaro_smul {E : Type*} [normed_add_comm_group E] [normed_space ℝ E] {u : ℕ → E} {l : E} (h : tendsto u at_top (𝓝 l)) : tendsto (λ (n : ℕ), (n ⁻¹ : ℝ) • (∑ i in range n, u i)) at_top (𝓝 l) := begin rw [← tendsto_sub_nhds_zero_iff, ← is_o_one_iff ℝ], have := asymptotics.is_o_sum_range_of_tendsto_zero (tendsto_sub_nhds_zero_iff.2 h), apply ((is_O_refl (λ (n : ℕ), (n : ℝ) ⁻¹) at_top).smul_is_o this).congr' _ _, { filter_upwards [Ici_mem_at_top 1] with n npos, have nposℝ : (0 : ℝ) < n := nat.cast_pos.2 npos, simp only [smul_sub, sum_sub_distrib, sum_const, card_range, sub_right_inj], rw [nsmul_eq_smul_cast ℝ, smul_smul, inv_mul_cancel nposℝ.ne', one_smul] }, { filter_upwards [Ici_mem_at_top 1] with n npos, have nposℝ : (0 : ℝ) < n := nat.cast_pos.2 npos, rw [algebra.id.smul_eq_mul, inv_mul_cancel nposℝ.ne'] } end /-- The Cesaro average of a converging sequence converges to the same limit. -/ lemma filter.tendsto.cesaro {u : ℕ → ℝ} {l : ℝ} (h : tendsto u at_top (𝓝 l)) : tendsto (λ (n : ℕ), (n ⁻¹ : ℝ) * (∑ i in range n, u i)) at_top (𝓝 l) := h.cesaro_smul end real
699a4481ad6c02dcb8bf8b7def9a175b337ec0a3
3dc4623269159d02a444fe898d33e8c7e7e9461b
/.github/workflows/geo/group_objet/group_obj_def.lean
c7ee0a67761d53feea769c8f4c43ddcbbfdf0558
[]
no_license
Or7ando/lean
cc003e6c41048eae7c34aa6bada51c9e9add9e66
d41169cf4e416a0d42092fb6bdc14131cee9dd15
refs/heads/master
1,650,600,589,722
1,587,262,906,000
1,587,262,906,000
255,387,160
0
0
null
null
null
null
UTF-8
Lean
false
false
9,768
lean
import category_theory.limits.limits import category_theory.limits.shapes import category_theory.yoneda import category_theory.opposites import category_theory.types import category_theory.limits.types run_cmd mk_simp_attr `PRODUCT ----- BOF BOF meta def PRODUCT_CAT : tactic unit := `[ try {simp only with PRODUCT}] run_cmd add_interactive [`PRODUCT_CAT] universes v u open category_theory open category_theory.limits open category_theory.category open opposite namespace lem variables {C : Type u} variables [𝒞 : category.{v} C] variables [has_binary_products.{v} C][has_terminal.{v} C] include 𝒞 attribute [PRODUCT] category.assoc category.id_comp category.comp_id @[PRODUCT] lemma prod_left_def {X Y : C} : limit.π (pair X Y) walking_pair.left = limits.prod.fst := rfl @[PRODUCT] lemma prod_right_def {X Y : C} : limit.π (pair X Y) walking_pair.right = limits.prod.snd := rfl lemma prod.hom_ext {A X Y : C} {a b : A ⟶ X ⨯ Y} (h1 : a ≫ limits.prod.fst = b ≫ limits.prod.fst) (h2 : a ≫ limits.prod.snd = b ≫ limits.prod.snd) : a = b := begin apply limit.hom_ext, rintros (_ | _), rw prod_left_def, exact h1, rw prod_right_def, exact h2, end @[PRODUCT]lemma prod.lift_fst {Y A B : C} (f : Y ⟶ A) (g : Y ⟶ B) : prod.lift f g ≫ category_theory.limits.prod.fst = f := limit.lift_π (binary_fan.mk f g) _ @[PRODUCT]lemma prod.lift_snd {Y A B : C} (f : Y ⟶ A) (g : Y ⟶ B) : prod.lift f g ≫ category_theory.limits.prod.snd = g := limit.lift_π (binary_fan.mk f g) _ end lem notation f ` ⊗ `:20 g :20 := category_theory.limits.prod.map f g ---- 20 notation `T`C :20 := (terminal C) notation `T`X : 20 := (terminal.from X) notation f ` | `:20 g :20 := prod.lift f g notation `π1` := limits.prod.fst notation `π2` := limits.prod.snd variables {C : Type u} variables [𝒞 : category.{v} C] variables [has_binary_products.{v} C][has_terminal.{v} C] include 𝒞 variables (X :C) open lem /- π notation for projection -/ @[PRODUCT]lemma ex_1 {Y A B : C} (f : Y ⟶ A) (g : Y ⟶ B) : ( f | g) ≫ π1 = f := prod.lift_fst f g /- we can type π : A ⨯ B ⟶ B if we need -/ @[PRODUCT]lemma ex_2 {Y A B : C} (f : Y ⟶ A) (g : Y ⟶ B) : ( f | g) ≫ (π2 : A ⨯ B ⟶ B) = g := prod.lift_snd f g @[PRODUCT]lemma ex_3 {A X Y : C} {a b : A ⟶ X ⨯ Y} (h1 : a ≫ π1 = b ≫ π1 ) (h2 : a ≫ π2 = b ≫ π2) : a = b := prod.hom_ext h1 h2 @[PRODUCT]lemma prod.left_composition{Z' Z A B : C}(h : Z' ⟶ Z)(f : Z ⟶ A)(g : Z ⟶ B) : h ≫ (f | g) = (h ≫ f | h ≫ g) := begin apply prod.hom_ext, --- Le right member is of the form ( | ) composition π1 π2 PRODUCT_CAT, PRODUCT_CAT, --- here assoc -- rw assoc, -- rw prod.lift_fst, -- rw prod.lift_fst, -- rw prod.lift_snd, -- rw assoc, -- rw prod.lift_snd, end @[PRODUCT]lemma prod.map_first{X Y Z W : C}(f : X ⟶ Y)(g : Z ⟶ W) : (f ⊗ g) ≫ (π1 : Y ⨯ W ⟶ Y) = π1 ≫ f := begin exact limit.map_π (map_pair f g) walking_pair.left, end @[PRODUCT]lemma prod.map_second{X Y Z W : C}(f : X ⟶ Y)(g : Z ⟶ W) : (f ⊗ g) ≫ π2 = π2 ≫ g := begin exact limit.map_π (map_pair f g) walking_pair.right, end @[PRODUCT]lemma prod.otimes_is_prod {X Y Z W : C}(f : X ⟶ Y)(g : Z ⟶ W) : (f ⊗ g) = ( π1 ≫ f | π2 ≫ g ) := begin apply prod.hom_ext, PRODUCT_CAT, PRODUCT_CAT, -- rw prod.lift_fst, -- rw prod.map_first, -- rw prod.lift_snd, -- rw prod.map_second, end -- notation π1`(`X `x` Y`)` := (limits.prod.fst : X⨯Y ⟶ X) @[PRODUCT]lemma prod.map_ext{X Y Z W : C}(f1 f2 : X ⟶ Y)(g1 g2 : Z ⟶ W) : (f1 ⊗ g1) = (f2 ⊗ g2) → (π1 : X ⨯ Z ⟶ X) ≫ f1 = (π1 : X ⨯ Z ⟶ X) ≫ f2 := λ certif, begin iterate 2 {rw prod.otimes_is_prod at certif}, rw ← prod.map_first ( f1) (g1), rw ← prod.map_first ( f2) (g2), iterate 2 {rw prod.otimes_is_prod}, rw certif, end @[PRODUCT]lemma destruction {X Y Z : C} (f : Y ⟶ X) (g : X ⟶ Z ) : (f | 𝟙 Y) ≫ (g ⊗ (𝟙 Y)) = (f ≫ g | 𝟙 Y) := begin apply prod.hom_ext, -- PRODUCT_CAT,PRODUCT_CAT, ---------------------- PROBLEME With the tatict HEEEEEEERRRRRRE rw [prod.lift_fst], rw assoc, rw prod.map_first, rw ← assoc, ----- ← assoc here Problem ? rw prod.lift_fst, tidy, -- super - power tidy end -- def Y (R : C)(A :C) := (yoneda.obj A).obj (op R) -- def Y_ (R : C) {A B : C}(φ : A ⟶ B) := ((yoneda.map φ).app (op R) : Y R A ⟶ Y R B) -- -- Good notation for yoneda stuff : -- -- We fix V : C and we denote by -- -- R[X] := yoneda.obj X).obj (op R) and φ : A ⟶ B (in C) R ⟦ φ ⟧ : R[A] → R[B] in type v -- local notation R`[`A`]`:20 := Y R A -- notation ?? -- local notation R`<`φ`>` :20 := Y_ R φ -- -- def Yoneda_preserve_product (Y : C)(A B : C) : -- Y[A ⨯ B] ≅ Y[A] ⨯ Y[B] := -- { hom := prod.lift -- (λ f, f ≫ π1) -- (λ f, f ≫ π2), -- inv := λ f : (Y ⟶ A) ⨯ (Y ⟶ B), -- (prod.lift -- ((@category_theory.limits.prod.fst _ _ (Y ⟶ A) (Y ⟶ B) _ : ((Y ⟶ A) ⨯ (Y ⟶ B)) → (Y ⟶ A)) f) -- ((@category_theory.limits.prod.snd _ _ (Y ⟶ A) _ _ : ((Y ⟶ A) ⨯ (Y ⟶ B)) → (Y ⟶ B)) f : Y ⟶ B)), -- hom_inv_id' := begin -- ext f, -- cases j, -- { simp, refl}, -- { simp, refl} -- end, -- inv_hom_id' := begin -- apply lem.prod.hom_ext, -- { rw assoc, rw lem.prod.lift_fst, obviously}, -- { rw assoc, rw lem.prod.lift_snd, obviously} -- end -- } -- --- Here it just sugar -- @[PRODUCT]lemma yoneda_sugar.composition (R : C) {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) : R < f ≫ g > =( R< f >) ≫ (R < g >) -- := begin -- unfold Y_, -- simp, -- end -- def yoneda_sugar.conv {R : C}{A : C}(g : R[A]) : R ⟶ A := g -- def yoneda_sugar.prod (R : C)(A B : C) : R[A ⨯ B] ≅ R[A] ⨯ R[B] := begin -- exact Yoneda_preserve_product R A B, -- end -- @[PRODUCT]lemma yoneda_sugar.prod.hom (R : C)(A B : C) : -- (yoneda_sugar.prod R A B).hom = (R < (limits.prod.fst : A ⨯ B ⟶ A) > | R < (limits.prod.snd : A ⨯ B ⟶ B)> ) := rfl -- @[PRODUCT]lemma yoneda_sugar.prod.first (R : C)(A B : C) : -- (yoneda_sugar.prod R A B).hom ≫ limits.prod.fst = (R < limits.prod.fst >) := -- begin -- exact rfl, -- end -- @[PRODUCT]lemma yoneda_sugar.prod.hom_inv (R : C)(A B : C) : -- (yoneda_sugar.prod R A B).hom ≫ (yoneda_sugar.prod R A B).inv = 𝟙 (R[ A ⨯ B]) := -- (Yoneda_preserve_product R A B).hom_inv_id' -- @[PRODUCT]lemma yoneda_sugar.prod.inv_hom (R : C)(A B : C) : -- (yoneda_sugar.prod R A B).inv ≫ (yoneda_sugar.prod R A B).hom = 𝟙 ( R [A] ⨯ R[B]) := -- (Yoneda_preserve_product R A B).inv_hom_id' -- @[PRODUCT]lemma yoneda_sugar.prod.second (R : C)(A B : C) : -- (yoneda_sugar.prod R A B).hom ≫ limits.prod.snd = (R < limits.prod.snd >) := rfl -- @[PRODUCT]lemma yoneda_sugar.id (R : C)(A : C) : R < 𝟙 A > = 𝟙 ( R [A] ) := begin -- funext, -- exact comp_id C g, -- -- have T : ((yoneda.map (𝟙 A)).app (op R)) g = (g ≫ (𝟙 A)), -- end -- lemma yoneda_sugar_prod (R : C)(A B : C)(X :C)(f : X ⟶ A)(g : X ⟶ B) : -- R < (f | g) > ≫ (yoneda_sugar.prod R A B).hom = (R < f > | R < g > ) := -- the ≫ is :/ -- begin -- PRODUCT_CAT, -- -- rw yoneda_sugar.prod.hom R A B, -- -- rw prod.left_composition, -- iterate 2 {rw ← yoneda_sugar.composition}, -- rw ← is the problem ? -- rw lem.prod.lift_fst, -- rw lem.prod.lift_snd, -- end -- @[PRODUCT]lemma yoneda_sugar_prod_inv (R : C)(A B : C)(X :C)(f : X ⟶ A)(g : X ⟶ B) : -- R < (f | g) > = (R < f > | R < g > ) ≫ (yoneda_sugar.prod R A B).inv := -- begin -- PRODUCT_CAT, -- noting -- rw ← yoneda_sugar_prod, -- rw assoc, -- rw yoneda_sugar.prod.hom_inv, -- exact rfl, -- end -- lemma yoneda_sugar.otimes (R : C){Y Z K :C}(f : X ⟶ Y )(g : Z ⟶ K) : -- ( R < (f ⊗ g) > ) = (yoneda_sugar.prod _ _ _).hom ≫ ((R<f>) ⊗ R<g>) ≫ (yoneda_sugar.prod _ _ _ ).inv := begin -- PRODUCT_CAT, -- -- iterate 2 {rw prod.otimes_is_prod}, -- -- rw yoneda_sugar.prod.hom, -- -- iterate 1 {rw yoneda_sugar_prod_inv}, -- rw ← assoc, -- rw prod.left_composition, -- rw ← assoc, -- rw prod.lift_fst, -- rw ← assoc, -- rw prod.lift_snd, -- -- rw yoneda_sugar.composition, -- -- rw yoneda_sugar.composition, -- end -- @[PRODUCT]lemma yonega_sugar.one_otimes (R :C)(X Y Z: C) (f : X ⟶ Y) : -- (((yoneda_sugar.prod R Z X).inv) ≫ (R <(𝟙 Z ⊗ f ) > ) ≫ (yoneda_sugar.prod R Z Y).hom) = (𝟙 (R[Z]) ⊗ R < f >) := begin -- rw yoneda_sugar.otimes, -- iterate 3 {rw ← assoc}, -- rw yoneda_sugar.prod.inv_hom, -- rw id_comp, -- rw assoc, -- rw yoneda_sugar.prod.inv_hom, -- rw ← yoneda_sugar.id, -- simp, -- end -- lemma yonega_sugar.one_otimes' (R :C)(X Y Z: C) (f : X ⟶ Y) : -- ( (R <(𝟙 Z ⊗ f ) > ) ≫ (yoneda_sugar.prod R Z Y).hom) = ((yoneda_sugar.prod R Z X).hom) ≫ (𝟙 (R[Z]) ⊗ R < f >) := begin -- iterate 2{ rw yoneda_sugar.prod.hom}, -- rw prod.left_composition, -- iterate 2{ rw ← yoneda_sugar.composition}, -- rw prod.map_first, -- rw prod.map_second, -- rw comp_id, -- rw prod.otimes_is_prod,rw prod.left_composition,rw ← assoc, -- rw prod.lift_fst,rw ← assoc,rw prod.lift_snd,rw comp_id, -- rw yoneda_sugar.composition, -- end
3a4b8f2c393435208eaa6235a6110a80a747d365
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/data/mv_polynomial.lean
55c7a8b47f75a0b2d83a25312fa33742d1604d1b
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
29,516
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 Multivariate Polynomial -/ import algebra.ring import data.finsupp data.polynomial data.equiv.algebra open set function finsupp lattice universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} /-- Multivariate polynomial, where `σ` is the index set of the variables and `α` is the coefficient ring -/ def mv_polynomial (σ : Type*) (α : Type*) [comm_semiring α] := (σ →₀ ℕ) →₀ α namespace mv_polynomial variables {σ : Type*} {a a' a₁ a₂ : α} {e : ℕ} {n m : σ} {s : σ →₀ ℕ} variables [decidable_eq σ] [decidable_eq α] section comm_semiring variables [comm_semiring α] {p q : mv_polynomial σ α} instance : decidable_eq (mv_polynomial σ α) := finsupp.decidable_eq instance : has_zero (mv_polynomial σ α) := finsupp.has_zero instance : has_one (mv_polynomial σ α) := finsupp.has_one instance : has_add (mv_polynomial σ α) := finsupp.has_add instance : has_mul (mv_polynomial σ α) := finsupp.has_mul instance : comm_semiring (mv_polynomial σ α) := finsupp.to_comm_semiring /-- `monomial s a` is the monomial `a * X^s` -/ def monomial (s : σ →₀ ℕ) (a : α) : mv_polynomial σ α := single s a /-- `C a` is the constant polynomial with value `a` -/ def C (a : α) : mv_polynomial σ α := monomial 0 a /-- `X n` is the polynomial with value X_n -/ def X (n : σ) : mv_polynomial σ α := monomial (single n 1) 1 @[simp] lemma C_0 : C 0 = (0 : mv_polynomial σ α) := by simp [C, monomial]; refl @[simp] lemma C_1 : C 1 = (1 : mv_polynomial σ α) := rfl lemma C_mul_monomial : C a * monomial s a' = monomial s (a * a') := by simp [C, monomial, single_mul_single] @[simp] lemma C_add : (C (a + a') : mv_polynomial σ α) = C a + C a' := single_add @[simp] lemma C_mul : (C (a * a') : mv_polynomial σ α) = C a * C a' := C_mul_monomial.symm instance : is_semiring_hom (C : α → mv_polynomial σ α) := { map_zero := C_0, map_one := C_1, map_add := λ a a', C_add, map_mul := λ a a', C_mul } lemma X_pow_eq_single : X n ^ e = monomial (single n e) (1 : α) := begin induction e, { simp [X], refl }, { simp [pow_succ, e_ih], simp [X, monomial, single_mul_single, nat.succ_eq_add_one] } end lemma monomial_add_single : monomial (s + single n e) a = (monomial s a * X n ^ e):= by rw [X_pow_eq_single, monomial, monomial, monomial, single_mul_single]; simp lemma monomial_single_add : monomial (single n e + s) a = (X n ^ e * monomial s a):= by rw [X_pow_eq_single, monomial, monomial, monomial, single_mul_single]; simp lemma monomial_eq : monomial s a = C a * (s.prod $ λn e, X n ^ e : mv_polynomial σ α) := begin apply @finsupp.induction σ ℕ _ _ _ _ s, { simp [C, prod_zero_index]; exact (mul_one _).symm }, { assume n e s hns he ih, simp [prod_add_index, prod_single_index, pow_zero, pow_add, (mul_assoc _ _ _).symm, ih.symm, monomial_add_single] } end @[recursor 7] lemma induction_on {M : mv_polynomial σ α → Prop} (p : mv_polynomial σ α) (h_C : ∀a, M (C a)) (h_add : ∀p q, M p → M q → M (p + q)) (h_X : ∀p n, M p → M (p * X n)) : M p := have ∀s a, M (monomial s a), begin assume s a, apply @finsupp.induction σ ℕ _ _ _ _ s, { show M (monomial 0 a), from h_C a, }, { assume n e p hpn he ih, have : ∀e:ℕ, M (monomial p a * X n ^ e), { intro e, induction e, { simp [ih] }, { simp [ih, pow_succ', (mul_assoc _ _ _).symm, h_X, e_ih] } }, simp [monomial_add_single, this] } end, finsupp.induction p (by have : M (C 0) := h_C 0; rwa [C_0] at this) (assume s a p hsp ha hp, h_add _ _ (this s a) hp) lemma hom_eq_hom [semiring γ] (f g : mv_polynomial σ α → γ) (hf : is_semiring_hom f) (hg : is_semiring_hom g) (hC : ∀a:α, f (C a) = g (C a)) (hX : ∀n:σ, f (X n) = g (X n)) (p : mv_polynomial σ α) : f p = g p := mv_polynomial.induction_on p hC begin assume p q hp hq, rw [is_semiring_hom.map_add f, is_semiring_hom.map_add g, hp, hq] end begin assume p n hp, rw [is_semiring_hom.map_mul f, is_semiring_hom.map_mul g, hp, hX] end lemma is_id (f : mv_polynomial σ α → mv_polynomial σ α) (hf : is_semiring_hom f) (hC : ∀a:α, f (C a) = (C a)) (hX : ∀n:σ, f (X n) = (X n)) (p : mv_polynomial σ α) : f p = p := hom_eq_hom f id hf is_semiring_hom.id hC hX p section eval₂ variables [comm_semiring β] variables (f : α → β) (g : σ → β) /-- Evaluate a polynomial `p` given a valuation `g` of all the variables and a ring hom `f` from the scalar ring to the target -/ def eval₂ (p : mv_polynomial σ α) : β := p.sum (λs a, f a * s.prod (λn e, g n ^ e)) @[simp] lemma eval₂_zero : (0 : mv_polynomial σ α).eval₂ f g = 0 := finsupp.sum_zero_index variables [is_semiring_hom f] @[simp] lemma eval₂_add : (p + q).eval₂ f g = p.eval₂ f g + q.eval₂ f g := finsupp.sum_add_index (by simp [is_semiring_hom.map_zero f]) (by simp [add_mul, is_semiring_hom.map_add f]) @[simp] lemma eval₂_monomial : (monomial s a).eval₂ f g = f a * s.prod (λn e, g n ^ e) := finsupp.sum_single_index (by simp [is_semiring_hom.map_zero f]) @[simp] lemma eval₂_C (a) : (C a).eval₂ f g = f a := by simp [eval₂_monomial, C, prod_zero_index] @[simp] lemma eval₂_one : (1 : mv_polynomial σ α).eval₂ f g = 1 := (eval₂_C _ _ _).trans (is_semiring_hom.map_one f) @[simp] lemma eval₂_X (n) : (X n).eval₂ f g = g n := by simp [eval₂_monomial, is_semiring_hom.map_one f, X, prod_single_index, pow_one] lemma eval₂_mul_monomial : ∀{s a}, (p * monomial s a).eval₂ f g = p.eval₂ f g * f a * s.prod (λn e, g n ^ e) := begin apply mv_polynomial.induction_on p, { assume a' s a, simp [C_mul_monomial, eval₂_monomial, is_semiring_hom.map_mul f] }, { assume p q ih_p ih_q, simp [add_mul, eval₂_add, ih_p, ih_q] }, { assume p n ih s a, from calc (p * X n * monomial s a).eval₂ f g = (p * monomial (single n 1 + s) a).eval₂ f g : by simp [monomial_single_add, -add_comm, pow_one, mul_assoc] ... = (p * monomial (single n 1) 1).eval₂ f g * f a * s.prod (λn e, g n ^ e) : by simp [ih, prod_single_index, prod_add_index, pow_one, pow_add, mul_assoc, mul_left_comm, is_semiring_hom.map_one f, -add_comm] } end lemma eval₂_mul : ∀{p}, (p * q).eval₂ f g = p.eval₂ f g * q.eval₂ f g := begin apply mv_polynomial.induction_on q, { simp [C, eval₂_monomial, eval₂_mul_monomial, prod_zero_index] }, { simp [mul_add, eval₂_add] {contextual := tt} }, { simp [X, eval₂_monomial, eval₂_mul_monomial, (mul_assoc _ _ _).symm] { contextual := tt} } end lemma eval₂_pow {p:mv_polynomial σ α} : ∀{n:ℕ}, (p ^ n).eval₂ f g = (p.eval₂ f g)^n | 0 := eval₂_one _ _ | (n + 1) := by rw [pow_add, pow_one, pow_add, pow_one, eval₂_mul, eval₂_pow] instance eval₂.is_semiring_hom : is_semiring_hom (eval₂ f g) := { map_zero := eval₂_zero _ _, map_one := eval₂_one _ _, map_add := λ p q, eval₂_add _ _, map_mul := λ p q, eval₂_mul _ _ } lemma eval₂_comp_left {γ} [comm_semiring γ] (k : β → γ) [is_semiring_hom k] (f : α → β) [is_semiring_hom f] (g : σ → β) (p) : k (eval₂ f g p) = eval₂ (k ∘ f) (k ∘ g) p := by apply mv_polynomial.induction_on p; simp [ eval₂_add, is_semiring_hom.map_add k, eval₂_mul, is_semiring_hom.map_mul k] {contextual := tt} lemma eval₂_eta (p : mv_polynomial σ α) : eval₂ C X p = p := by apply mv_polynomial.induction_on p; simp [eval₂_add, eval₂_mul] {contextual := tt} end eval₂ section eval variables {f : σ → α} /-- Evaluate a polynomial `p` given a valuation `f` of all the variables -/ def eval (f : σ → α) : mv_polynomial σ α → α := eval₂ id f @[simp] lemma eval_zero : (0 : mv_polynomial σ α).eval f = 0 := eval₂_zero _ _ @[simp] lemma eval_add : (p + q).eval f = p.eval f + q.eval f := eval₂_add _ _ lemma eval_monomial : (monomial s a).eval f = a * s.prod (λn e, f n ^ e) := eval₂_monomial _ _ @[simp] lemma eval_C : ∀ a, (C a).eval f = a := eval₂_C _ _ @[simp] lemma eval_X : ∀ n, (X n).eval f = f n := eval₂_X _ _ @[simp] lemma eval_mul : (p * q).eval f = p.eval f * q.eval f := eval₂_mul _ _ instance eval.is_semiring_hom : is_semiring_hom (eval f) := eval₂.is_semiring_hom _ _ theorem eval_assoc {τ} [decidable_eq τ] (f : σ → mv_polynomial τ α) (g : τ → α) (p : mv_polynomial σ α) : p.eval (eval g ∘ f) = (eval₂ C f p).eval g := begin rw eval₂_comp_left (eval g), unfold eval, congr; funext a; simp end end eval section map variables [comm_semiring β] [decidable_eq β] variables (f : α → β) [is_semiring_hom f] /-- `map f p` maps a polynomial `p` across a ring hom `f` -/ def map : mv_polynomial σ α → mv_polynomial σ β := eval₂ (C ∘ f) X @[simp] theorem map_monomial (s : σ →₀ ℕ) (a : α) : map f (monomial s a) = monomial s (f a) := (eval₂_monomial _ _).trans monomial_eq.symm @[simp] theorem map_C : ∀ (a : α), map f (C a : mv_polynomial σ α) = C (f a) := map_monomial _ _ @[simp] theorem map_X : ∀ (n : σ), map f (X n : mv_polynomial σ α) = X n := eval₂_X _ _ @[simp] theorem map_one : map f (1 : mv_polynomial σ α) = 1 := eval₂_one _ _ @[simp] theorem map_add (p q : mv_polynomial σ α) : map f (p + q) = map f p + map f q := eval₂_add _ _ @[simp] theorem map_mul (p q : mv_polynomial σ α) : map f (p * q) = map f p * map f q := eval₂_mul _ _ instance map.is_semiring_hom : is_semiring_hom (map f : mv_polynomial σ α → mv_polynomial σ β) := eval₂.is_semiring_hom _ _ theorem map_id : ∀ (p : mv_polynomial σ α), map id p = p := eval₂_eta theorem map_map [comm_semiring γ] [decidable_eq γ] (g : β → γ) [is_semiring_hom g] (p : mv_polynomial σ α) : map g (map f p) = map (g ∘ f) p := (eval₂_comp_left (map g) (C ∘ f) X p).trans $ by congr; funext a; simp theorem eval₂_eq_eval_map (g : σ → β) (p : mv_polynomial σ α) : p.eval₂ f g = (map f p).eval g := begin unfold map eval, rw eval₂_comp_left (eval₂ id g), congr; funext a; simp end end map section degrees section comm_semiring def degrees (p : mv_polynomial σ α) : multiset σ := p.support.sup (λs:σ →₀ ℕ, s.to_multiset) lemma degrees_monomial (s : σ →₀ ℕ) (a : α) : degrees (monomial s a) ≤ s.to_multiset := finset.sup_le $ assume t h, begin have := finsupp.support_single_subset h, rw [finset.singleton_eq_singleton, finset.mem_singleton] at this, rw this end lemma degrees_monomial_eq (s : σ →₀ ℕ) (a : α) (ha : a ≠ 0) : degrees (monomial s a) = s.to_multiset := le_antisymm (degrees_monomial s a) $ finset.le_sup $ by rw [monomial, finsupp.support_single_ne_zero ha, finset.singleton_eq_singleton, finset.mem_singleton] lemma degrees_C (a : α) : degrees (C a : mv_polynomial σ α) = 0 := multiset.le_zero.1 $ degrees_monomial _ _ lemma degrees_X (n : σ) : degrees (X n : mv_polynomial σ α) ≤ {n} := le_trans (degrees_monomial _ _) $ le_of_eq $ to_multiset_single _ _ lemma degrees_zero : degrees (0 : mv_polynomial σ α) = 0 := degrees_C 0 lemma degrees_one : degrees (1 : mv_polynomial σ α) = 0 := degrees_C 1 lemma degrees_add (p q : mv_polynomial σ α) : (p + q).degrees ≤ p.degrees ⊔ q.degrees := begin refine finset.sup_le (assume b hb, _), cases finset.mem_union.1 (finsupp.support_add hb), { exact le_sup_left_of_le (finset.le_sup h) }, { exact le_sup_right_of_le (finset.le_sup h) }, end lemma degrees_sum {ι : Type*} [decidable_eq ι] (s : finset ι) (f : ι → mv_polynomial σ α) : (s.sum f).degrees ≤ s.sup (λi, (f i).degrees) := begin refine s.induction _ _, { simp only [finset.sum_empty, finset.sup_empty, degrees_zero], exact le_refl _ }, { assume i s his ih, rw [finset.sup_insert, finset.sum_insert his], exact le_trans (degrees_add _ _) (sup_le_sup_left ih _) } end lemma degrees_mul (p q : mv_polynomial σ α) : (p * q).degrees ≤ p.degrees + q.degrees := begin refine finset.sup_le (assume b hb, _), have := support_mul p q hb, simp only [finset.mem_bind, finset.singleton_eq_singleton, finset.mem_singleton] at this, rcases this with ⟨a₁, h₁, a₂, h₂, rfl⟩, rw [finsupp.to_multiset_add], exact add_le_add (finset.le_sup h₁) (finset.le_sup h₂) end lemma degrees_prod {ι : Type*} [decidable_eq ι] (s : finset ι) (f : ι → mv_polynomial σ α) : (s.prod f).degrees ≤ s.sum (λi, (f i).degrees) := begin refine s.induction _ _, { simp only [finset.prod_empty, finset.sum_empty, degrees_one] }, { assume i s his ih, rw [finset.prod_insert his, finset.sum_insert his], exact le_trans (degrees_mul _ _) (add_le_add_left ih _) } end lemma degrees_pow (p : mv_polynomial σ α) : ∀(n : ℕ), (p^n).degrees ≤ add_monoid.smul n p.degrees | 0 := begin rw [pow_zero, degrees_one], exact multiset.zero_le _ end | (n + 1) := le_trans (degrees_mul _ _) (add_le_add_left (degrees_pow n) _) end comm_semiring end degrees section vars /-- `vars p` is the set of variables appearing in the polynomial `p` -/ def vars (p : mv_polynomial σ α) : finset σ := p.degrees.to_finset @[simp] lemma vars_0 : (0 : mv_polynomial σ α).vars = ∅ := by rw [vars, degrees_zero, multiset.to_finset_zero] @[simp] lemma vars_monomial (h : a ≠ 0) : (monomial s a).vars = s.support := by rw [vars, degrees_monomial_eq _ _ h, finsupp.to_finset_to_multiset] @[simp] lemma vars_C : (C a : mv_polynomial σ α).vars = ∅ := by rw [vars, degrees_C, multiset.to_finset_zero] @[simp] lemma vars_X (h : 0 ≠ (1 : α)) : (X n : mv_polynomial σ α).vars = {n} := by rw [X, vars_monomial h.symm, finsupp.support_single_ne_zero zero_ne_one.symm] end vars section degree_of /-- `degree_of n p` gives the highest power of X_n that appears in `p` -/ def degree_of (n : σ) (p : mv_polynomial σ α) : ℕ := p.degrees.count n end degree_of section total_degree /-- `total_degree p` gives the maximum |s| over the monomials X^s in `p` -/ def total_degree (p : mv_polynomial σ α) : ℕ := p.support.sup (λs, s.sum $ λn e, e) lemma total_degree_eq (p : mv_polynomial σ α) : p.total_degree = p.support.sup (λm, m.to_multiset.card) := begin rw [total_degree], congr, funext m, exact (finsupp.card_to_multiset _).symm end lemma total_degree_le_degrees_card (p : mv_polynomial σ α) : p.total_degree ≤ p.degrees.card := begin rw [total_degree_eq], exact finset.sup_le (assume s hs, multiset.card_le_of_le $ finset.le_sup hs) end lemma total_degree_C (a : α) : (C a : mv_polynomial σ α).total_degree = 0 := nat.eq_zero_of_le_zero $ finset.sup_le $ assume n hn, have _ := finsupp.support_single_subset hn, begin rw [finset.singleton_eq_singleton, finset.mem_singleton] at this, subst this, exact le_refl _ end lemma total_degree_zero : (0 : mv_polynomial σ α).total_degree = 0 := by rw [← C_0]; exact total_degree_C (0 : α) lemma total_degree_one : (1 : mv_polynomial σ α).total_degree = 0 := total_degree_C (1 : α) lemma total_degree_add (a b : mv_polynomial σ α) : (a + b).total_degree ≤ max a.total_degree b.total_degree := finset.sup_le $ assume n hn, have _ := finsupp.support_add hn, begin rcases finset.mem_union.1 this, { exact le_max_left_of_le (finset.le_sup h) }, { exact le_max_right_of_le (finset.le_sup h) } end lemma total_degree_mul (a b : mv_polynomial σ α) : (a * b).total_degree ≤ a.total_degree + b.total_degree := finset.sup_le $ assume n hn, have _ := finsupp.support_mul a b hn, begin simp only [finset.mem_bind, finset.mem_singleton, finset.singleton_eq_singleton] at this, rcases this with ⟨a₁, h₁, a₂, h₂, rfl⟩, rw [finsupp.sum_add_index], { exact add_le_add (finset.le_sup h₁) (finset.le_sup h₂) }, { assume a, refl }, { assume a b₁ b₂, refl } end lemma total_degree_list_prod : ∀(s : list (mv_polynomial σ α)), s.prod.total_degree ≤ (s.map mv_polynomial.total_degree).sum | [] := by rw [@list.prod_nil (mv_polynomial σ α) _, total_degree_one]; refl | (p :: ps) := begin rw [@list.prod_cons (mv_polynomial σ α) _, list.map, list.sum_cons], exact le_trans (total_degree_mul _ _) (add_le_add_left (total_degree_list_prod ps) _) end lemma total_degree_multiset_prod (s : multiset (mv_polynomial σ α)) : s.prod.total_degree ≤ (s.map mv_polynomial.total_degree).sum := begin refine quotient.induction_on s (assume l, _), rw [multiset.quot_mk_to_coe, multiset.coe_prod, multiset.coe_map, multiset.coe_sum], exact total_degree_list_prod l end lemma total_degree_finset_prod {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ α) : (s.prod f).total_degree ≤ s.sum (λi, (f i).total_degree) := begin refine le_trans (total_degree_multiset_prod _) _, rw [multiset.map_map], refl end end total_degree end comm_semiring section comm_ring variable [comm_ring α] variables {p q : mv_polynomial σ α} instance : ring (mv_polynomial σ α) := finsupp.to_ring instance : comm_ring (mv_polynomial σ α) := finsupp.to_comm_ring instance : has_scalar α (mv_polynomial σ α) := finsupp.to_has_scalar instance : module α (mv_polynomial σ α) := finsupp.to_module _ α instance C.is_ring_hom : is_ring_hom (C : α → mv_polynomial σ α) := by apply is_ring_hom.of_semiring variables (σ a a') lemma C_sub : (C (a - a') : mv_polynomial σ α) = C a - C a' := is_ring_hom.map_sub _ @[simp] lemma C_neg : (C (-a) : mv_polynomial σ α) = -C a := is_ring_hom.map_neg _ variables {σ} (p) theorem C_mul' : mv_polynomial.C a * p = a • p := begin apply finsupp.induction p, { exact (mul_zero $ mv_polynomial.C a).trans (@smul_zero α (mv_polynomial σ α) _ _ _ a).symm }, intros p b f haf hb0 ih, rw [mul_add, ih, @smul_add α (mv_polynomial σ α) _ _ _ a], congr' 1, rw [finsupp.mul_def, finsupp.smul_single, mv_polynomial.C, mv_polynomial.monomial], rw [finsupp.sum_single_index, finsupp.sum_single_index, zero_add, smul_eq_mul], { rw [mul_zero, finsupp.single_zero] }, { rw finsupp.sum_single_index, all_goals { rw [zero_mul, finsupp.single_zero] } } end lemma smul_eq_C_mul (p : mv_polynomial σ α) (a : α) : a • p = C a * p := begin rw [← finsupp.sum_single p, @finsupp.smul_sum (σ →₀ ℕ) α α, finsupp.mul_sum], refine finset.sum_congr rfl (assume n _, _), simp only [finsupp.smul_single], exact C_mul_monomial.symm end @[simp] lemma smul_eval (x) (p : mv_polynomial σ α) (s) : (s • p).eval x = s * p.eval x := by rw [smul_eq_C_mul, eval_mul, eval_C] section degrees lemma degrees_neg [comm_ring α] (p : mv_polynomial σ α) : (- p).degrees = p.degrees := by rw [degrees, finsupp.support_neg]; refl lemma degrees_sub [comm_ring α] (p q : mv_polynomial σ α) : (p - q).degrees ≤ p.degrees ⊔ q.degrees := le_trans (degrees_add p (-q)) $ by rw [degrees_neg] end degrees section eval₂ variables [comm_ring β] variables (f : α → β) [is_ring_hom f] (g : σ → β) instance eval₂.is_ring_hom : is_ring_hom (eval₂ f g) := by apply is_ring_hom.of_semiring lemma eval₂_sub : (p - q).eval₂ f g = p.eval₂ f g - q.eval₂ f g := is_ring_hom.map_sub _ @[simp] lemma eval₂_neg : (-p).eval₂ f g = -(p.eval₂ f g) := is_ring_hom.map_neg _ end eval₂ section eval variables (f : σ → α) instance eval.is_ring_hom : is_ring_hom (eval f) := eval₂.is_ring_hom _ _ lemma eval_sub : (p - q).eval f = p.eval f - q.eval f := is_ring_hom.map_sub _ @[simp] lemma eval_neg : (-p).eval f = -(p.eval f) := is_ring_hom.map_neg _ end eval section map variables [decidable_eq β] [comm_ring β] variables (f : α → β) [is_ring_hom f] instance map.is_ring_hom : is_ring_hom (map f : mv_polynomial σ α → mv_polynomial σ β) := eval₂.is_ring_hom _ _ lemma map_sub : (p - q).map f = p.map f - q.map f := is_ring_hom.map_sub _ @[simp] lemma map_neg : (-p).map f = -(p.map f) := is_ring_hom.map_neg _ end map end comm_ring section rename variables {α} [comm_semiring α] [decidable_eq α] [decidable_eq β] [decidable_eq γ] [decidable_eq δ] def rename (f : β → γ) : mv_polynomial β α → mv_polynomial γ α := eval₂ C (X ∘ f) instance rename.is_semiring_hom (f : β → γ) : is_semiring_hom (rename f : mv_polynomial β α → mv_polynomial γ α) := by unfold rename; apply_instance @[simp] lemma rename_C (f : β → γ) (a : α) : rename f (C a) = C a := eval₂_C _ _ _ @[simp] lemma rename_X (f : β → γ) (b : β) : rename f (X b : mv_polynomial β α) = X (f b) := eval₂_X _ _ _ lemma rename_rename (f : β → γ) (g : γ → δ) (p : mv_polynomial β α) : rename g (rename f p) = rename (g ∘ f) p := show rename g (eval₂ C (X ∘ f) p) = _, by simp only [eval₂_comp_left (rename g) C (X ∘ f) p, (∘), rename_C, rename_X]; refl lemma rename_id (p : mv_polynomial β α) : rename id p = p := eval₂_eta p lemma rename_monomial (f : β → γ) (p : β →₀ ℕ) (a : α) : rename f (monomial p a) = monomial (p.map_domain f) a := begin rw [rename, eval₂_monomial, monomial_eq, finsupp.prod_map_domain_index], { exact assume n, pow_zero _ }, { exact assume n i₁ i₂, pow_add _ _ _ } end lemma rename_eq (f : β → γ) (p : mv_polynomial β α) : rename f p = finsupp.map_domain (finsupp.map_domain f) p := begin simp only [rename, eval₂, finsupp.map_domain], congr, ext s a : 2, rw [← monomial, monomial_eq, finsupp.prod_sum_index], congr, ext n i : 2, rw [finsupp.prod_single_index], exact pow_zero _, exact assume a, pow_zero _, exact assume a b c, pow_add _ _ _ end lemma injective_rename (f : β → γ) (hf : function.injective f) : function.injective (rename f : mv_polynomial β α → mv_polynomial γ α) := have (rename f : mv_polynomial β α → mv_polynomial γ α) = finsupp.map_domain (finsupp.map_domain f) := funext (rename_eq f), begin rw this, exact finsupp.injective_map_domain (finsupp.injective_map_domain hf) end lemma total_degree_rename_le (f : β → γ) (p : mv_polynomial β α) : (p.rename f).total_degree ≤ p.total_degree := finset.sup_le $ assume b, begin assume h, rw rename_eq at h, have h' := finsupp.map_domain_support h, rcases finset.mem_image.1 h' with ⟨s, hs, rfl⟩, rw finsupp.sum_map_domain_index, exact le_trans (le_refl _) (finset.le_sup hs), exact assume _, rfl, exact assume _ _ _, rfl end end rename instance rename.is_ring_hom {α} [comm_ring α] [decidable_eq α] [decidable_eq β] [decidable_eq γ] (f : β → γ) : is_ring_hom (rename f : mv_polynomial β α → mv_polynomial γ α) := @is_ring_hom.of_semiring (mv_polynomial β α) (mv_polynomial γ α) _ _ (rename f) (rename.is_semiring_hom f) section equiv variables (α) [comm_ring α] variables [decidable_eq β] [decidable_eq γ] [decidable_eq δ] def pempty_ring_equiv : mv_polynomial pempty α ≃r α := { to_fun := mv_polynomial.eval₂ id $ pempty.elim, inv_fun := C, left_inv := is_id _ (by apply_instance) (assume a, by rw [eval₂_C]; refl) (assume a, a.elim), right_inv := λ r, eval₂_C _ _ _, hom := eval₂.is_ring_hom _ _ } def punit_ring_equiv : mv_polynomial punit α ≃r polynomial α := { to_fun := eval₂ polynomial.C (λu:punit, polynomial.X), inv_fun := polynomial.eval₂ mv_polynomial.C (X punit.star), left_inv := begin refine is_id _ _ _ _, apply is_semiring_hom.comp (eval₂ polynomial.C (λu:punit, polynomial.X)) _; apply_instance, { assume a, rw [eval₂_C, polynomial.eval₂_C] }, { rintros ⟨⟩, rw [eval₂_X, polynomial.eval₂_X] } end, right_inv := assume p, polynomial.induction_on p (assume a, by rw [polynomial.eval₂_C, mv_polynomial.eval₂_C]) (assume p q hp hq, by rw [polynomial.eval₂_add, mv_polynomial.eval₂_add, hp, hq]) (assume p n hp, by rw [polynomial.eval₂_mul, polynomial.eval₂_pow, polynomial.eval₂_X, polynomial.eval₂_C, eval₂_mul, eval₂_C, eval₂_pow, eval₂_X]), hom := eval₂.is_ring_hom _ _ } def ring_equiv_of_equiv (e : β ≃ γ) : mv_polynomial β α ≃r mv_polynomial γ α := { to_fun := rename e, inv_fun := rename e.symm, left_inv := λ p, by simp only [rename_rename, (∘), e.symm_apply_apply]; exact rename_id p, right_inv := λ p, by simp only [rename_rename, (∘), e.apply_symm_apply]; exact rename_id p, hom := rename.is_ring_hom e } def ring_equiv_congr [comm_ring γ] (e : α ≃r γ) : mv_polynomial β α ≃r mv_polynomial β γ := { to_fun := map e.to_fun, inv_fun := map e.symm.to_fun, left_inv := assume p, have (e.symm.to_equiv.to_fun ∘ e.to_equiv.to_fun) = id, { ext a, exact e.to_equiv.symm_apply_apply a }, by simp only [map_map, this, map_id], right_inv := assume p, have (e.to_equiv.to_fun ∘ e.symm.to_equiv.to_fun) = id, { ext a, exact e.to_equiv.apply_symm_apply a }, by simp only [map_map, this, map_id], hom := map.is_ring_hom e.to_fun } section variables (β γ δ) instance ring_on_sum : ring (mv_polynomial (β ⊕ γ) α) := by apply_instance instance ring_on_iter : ring (mv_polynomial β (mv_polynomial γ α)) := by apply_instance def sum_to_iter : mv_polynomial (β ⊕ γ) α → mv_polynomial β (mv_polynomial γ α) := eval₂ (C ∘ C) (λbc, sum.rec_on bc X (C ∘ X)) instance is_semiring_hom_C_C : is_semiring_hom (C ∘ C : α → mv_polynomial β (mv_polynomial γ α)) := @is_semiring_hom.comp _ _ _ _ C mv_polynomial.is_semiring_hom _ _ C mv_polynomial.is_semiring_hom instance is_semiring_hom_sum_to_iter : is_semiring_hom (sum_to_iter α β γ) := eval₂.is_semiring_hom _ _ lemma sum_to_iter_C (a : α) : sum_to_iter α β γ (C a) = C (C a) := eval₂_C _ _ a lemma sum_to_iter_Xl (b : β) : sum_to_iter α β γ (X (sum.inl b)) = X b := eval₂_X _ _ (sum.inl b) lemma sum_to_iter_Xr (c : γ) : sum_to_iter α β γ (X (sum.inr c)) = C (X c) := eval₂_X _ _ (sum.inr c) def iter_to_sum : mv_polynomial β (mv_polynomial γ α) → mv_polynomial (β ⊕ γ) α := eval₂ (eval₂ C (X ∘ sum.inr)) (X ∘ sum.inl) section instance is_semiring_hom_iter_to_sum : is_semiring_hom (iter_to_sum α β γ) := eval₂.is_semiring_hom _ _ end lemma iter_to_sum_C_C (a : α) : iter_to_sum α β γ (C (C a)) = C a := eq.trans (eval₂_C _ _ (C a)) (eval₂_C _ _ _) lemma iter_to_sum_X (b : β) : iter_to_sum α β γ (X b) = X (sum.inl b) := eval₂_X _ _ _ lemma iter_to_sum_C_X (c : γ) : iter_to_sum α β γ (C (X c)) = X (sum.inr c) := eq.trans (eval₂_C _ _ (X c)) (eval₂_X _ _ _) def mv_polynomial_equiv_mv_polynomial [comm_ring δ] (f : mv_polynomial β α → mv_polynomial γ δ) (hf : is_semiring_hom f) (g : mv_polynomial γ δ → mv_polynomial β α) (hg : is_semiring_hom g) (hfgC : ∀a, f (g (C a)) = C a) (hfgX : ∀n, f (g (X n)) = X n) (hgfC : ∀a, g (f (C a)) = C a) (hgfX : ∀n, g (f (X n)) = X n) : mv_polynomial β α ≃r mv_polynomial γ δ := { to_fun := f, inv_fun := g, left_inv := is_id _ (is_semiring_hom.comp _ _) hgfC hgfX, right_inv := is_id _ (is_semiring_hom.comp _ _) hfgC hfgX, hom := is_ring_hom.of_semiring f } def sum_ring_equiv : mv_polynomial (β ⊕ γ) α ≃r mv_polynomial β (mv_polynomial γ α) := begin apply @mv_polynomial_equiv_mv_polynomial α (β ⊕ γ) _ _ _ _ _ _ _ _ (sum_to_iter α β γ) _ (iter_to_sum α β γ) _, { assume p, apply @hom_eq_hom _ _ _ _ _ _ _ _ _ _ _ _ _ p, apply_instance, { apply @is_semiring_hom.comp _ _ _ _ _ _ _ _ _ _, apply_instance, apply @is_semiring_hom.comp _ _ _ _ _ _ _ _ _ _, apply_instance, { apply @mv_polynomial.is_semiring_hom }, { apply mv_polynomial.is_semiring_hom_iter_to_sum α β γ }, { apply mv_polynomial.is_semiring_hom_sum_to_iter α β γ } }, { apply mv_polynomial.is_semiring_hom }, { assume a, rw [iter_to_sum_C_C α β γ, sum_to_iter_C α β γ] }, { assume c, rw [iter_to_sum_C_X α β γ, sum_to_iter_Xr α β γ] } }, { assume b, rw [iter_to_sum_X α β γ, sum_to_iter_Xl α β γ] }, { assume a, rw [sum_to_iter_C α β γ, iter_to_sum_C_C α β γ] }, { assume n, cases n with b c, { rw [sum_to_iter_Xl, iter_to_sum_X] }, { rw [sum_to_iter_Xr, iter_to_sum_C_X] } }, { apply mv_polynomial.is_semiring_hom_sum_to_iter α β γ }, { apply mv_polynomial.is_semiring_hom_iter_to_sum α β γ } end instance option_ring : ring (mv_polynomial (option β) α) := mv_polynomial.ring instance polynomial_ring : ring (polynomial (mv_polynomial β α)) := @comm_ring.to_ring _ polynomial.comm_ring instance polynomial_ring2 : ring (mv_polynomial β (polynomial α)) := by apply_instance def option_equiv_left : mv_polynomial (option β) α ≃r polynomial (mv_polynomial β α) := (ring_equiv_of_equiv α $ (equiv.option_equiv_sum_punit β).trans (equiv.sum_comm _ _)).trans $ (sum_ring_equiv α _ _).trans $ punit_ring_equiv _ def option_equiv_right : mv_polynomial (option β) α ≃r mv_polynomial β (polynomial α) := (ring_equiv_of_equiv α $ equiv.option_equiv_sum_punit.{0} β).trans $ (sum_ring_equiv α β unit).trans $ ring_equiv_congr (mv_polynomial unit α) (punit_ring_equiv α) end end equiv end mv_polynomial
92769ecc187eb075002fb4a8a9419b97e7562fa6
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/theories/number_theory/irrational_roots.lean
ed95e7a714e16e746830bedb09dc4b7bba371f69
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
7,629
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad A proof that if n > 1 and a > 0, then the nth root of a is irrational, unless a is a perfect nth power. -/ import data.rat .prime_factorization open eq.ops /- First, a textbook proof that sqrt 2 is irrational. -/ section open nat theorem sqrt_two_irrational {a b : ℕ} (co : coprime a b) : a^2 ≠ 2 * b^2 := assume H : a^2 = 2 * b^2, have even (a^2), from even_of_exists (exists.intro _ H), have even a, from even_of_even_pow this, obtain (c : nat) (aeq : a = 2 * c), from exists_of_even this, have 2 * (2 * c^2) = 2 * b^2, by rewrite [-H, aeq, *pow_two, mul.assoc, mul.left_comm c], have 2 * c^2 = b^2, from eq_of_mul_eq_mul_left dec_trivial this, have even (b^2), from even_of_exists (exists.intro _ (eq.symm this)), have even b, from even_of_even_pow this, have 2 ∣ gcd a b, from dvd_gcd (dvd_of_even `even a`) (dvd_of_even `even b`), have (2:nat) ∣ 1, begin rewrite [gcd_eq_one_of_coprime co at this], exact this end, show false, from absurd `2 ∣ 1` dec_trivial end /- Replacing 2 by an arbitrary prime and the power 2 by any n ≥ 1 yields the stronger result that the nth root of an integer is irrational, unless the integer is already a perfect nth power. -/ section open nat decidable theorem root_irrational {a b c n : ℕ} (npos : n > 0) (apos : a > 0) (co : coprime a b) (H : a^n = c * b^n) : b = 1 := have bpos : b > 0, from pos_of_ne_zero (suppose b = 0, have a^n = 0, by rewrite [H, this, zero_pow npos], have a = 0, from eq_zero_of_pow_eq_zero this, show false, from ne_of_lt `0 < a` this⁻¹), have H₁ : ∀ p, prime p → ¬ p ∣ b, from take p, suppose prime p, suppose p ∣ b, have p ∣ b^n, from dvd_pow_of_dvd_of_pos `p ∣ b` `n > 0`, have p ∣ a^n, by rewrite H; apply dvd_mul_of_dvd_right this, have p ∣ a, from dvd_of_prime_of_dvd_pow `prime p` this, have ¬ coprime a b, from not_coprime_of_dvd_of_dvd (gt_one_of_prime `prime p`) `p ∣ a` `p ∣ b`, show false, from this `coprime a b`, have blt2 : b < 2, from by_contradiction (suppose ¬ b < 2, have b ≥ 2, from le_of_not_gt this, obtain p [primep pdvdb], from exists_prime_and_dvd this, show false, from H₁ p primep pdvdb), show b = 1, from (le.antisymm (le_of_lt_succ blt2) (succ_le_of_lt bpos)) end /- Here we state this in terms of the rationals, ℚ. The main difficulty is casting between ℕ, ℤ, and ℚ. -/ section open rat int nat decidable theorem denom_eq_one_of_pow_eq {q : ℚ} {n : ℕ} {c : ℤ} (npos : n > 0) (H : q^n = c) : denom q = 1 := let a := num q, b := denom q in have b ≠ 0, from ne_of_gt (denom_pos q), have bnz : b ≠ (0 : ℚ), from assume H, `b ≠ 0` (of_int.inj H), have bnnz : (b : rat)^n ≠ 0, from assume bneqz, bnz (_root_.eq_zero_of_pow_eq_zero bneqz), have a^n /[rat] (b:rat)^n = c, begin rewrite [*of_int_pow, -div_pow, -eq_num_div_denom, -H] end, have (a^n : rat) = c * (b:rat)^n, from eq.symm (!mul_eq_of_eq_div bnnz (eq.symm this)), have a^n = c * b^n, -- int version by rewrite [-of_int_pow at this, -of_int_mul at this]; exact of_int.inj this, have (abs a)^n = abs c * (abs b)^n, by rewrite [-abs_pow, this, abs_mul, abs_pow], have H₁ : (nat_abs a)^n = nat_abs c * (nat_abs b)^n, begin apply int.of_nat.inj, rewrite [int.of_nat_mul, +int.of_nat_pow, +of_nat_nat_abs], exact this end, have H₂ : nat.coprime (nat_abs a) (nat_abs b), from of_nat.inj !coprime_num_denom, have nat_abs b = 1, from by_cases (suppose q = 0, by rewrite this) (suppose qne0 : q ≠ 0, begin have ane0 : a ≠ 0, from suppose aeq0 : a = 0, have qeq0 : q = 0, begin rewrite [eq_num_div_denom q, aeq0, of_int_zero, zero_div] end, show false, from qne0 qeq0, have nat_abs a ≠ 0, from suppose nat_abs a = 0, have aeq0 : a = 0, from eq_zero_of_nat_abs_eq_zero this, show false, from ane0 aeq0, show nat_abs b = 1, from (root_irrational npos (pos_of_ne_zero this) H₂ H₁) end), show b = 1, begin rewrite [-of_nat_nat_abs_of_nonneg (le_of_lt !denom_pos), this] end theorem eq_num_pow_of_pow_eq {q : ℚ} {n : ℕ} {c : ℤ} (npos : n > 0) (H : q^n = c) : c = (num q)^n := have denom q = 1, from denom_eq_one_of_pow_eq npos H, have of_int c = of_int ((num q)^n), by rewrite [-H, eq_num_div_denom q at {1}, this, of_int_one, div_one, of_int_pow], show c = (num q)^n , from of_int.inj this end /- As a corollary, for n > 1, the nth root of a prime is irrational. -/ section open nat theorem not_eq_pow_of_prime {p n : ℕ} (a : ℕ) (ngt1 : n > 1) (primep : prime p) : p ≠ a^n := assume peq : p = a^n, have npos : n > 0, from lt.trans dec_trivial ngt1, have pnez : p ≠ 0, from (suppose p = 0, show false, by note H := (pos_of_prime primep); rewrite this at H; exfalso; exact !lt.irrefl H), have agtz : a > 0, from pos_of_ne_zero (suppose a = 0, show false, by revert peq; rewrite [this, zero_pow npos]; exact pnez), have n * mult p a = 1, from calc n * mult p a = mult p (a^n) : begin rewrite [mult_pow n agtz primep] end ... = mult p p : peq ... = 1 : mult_self (gt_one_of_prime primep), have n ∣ 1, from dvd_of_mul_right_eq this, have n = 1, from eq_one_of_dvd_one this, show false, by rewrite this at ngt1; exact !lt.irrefl ngt1 open int rat theorem root_prime_irrational {p n : ℕ} {q : ℚ} (qnonneg : q ≥ 0) (ngt1 : n > 1) (primep : prime p) : q^n ≠ p := have numq : num q ≥ 0, from num_nonneg_of_nonneg qnonneg, have npos : n > 0, from lt.trans dec_trivial ngt1, suppose q^n = p, have p = (num q)^n, from eq_num_pow_of_pow_eq npos this, have p = (nat_abs (num q))^n, by apply of_nat.inj; rewrite [this, of_nat_pow, of_nat_nat_abs_of_nonneg numq], show false, from not_eq_pow_of_prime _ ngt1 primep this end /- Thaetetus, who lives in the fourth century BC, is said to have proved the irrationality of square roots up to seventeen. In Chapter 4 of /Why Prove it Again/, John Dawson notes that Thaetetus may have used an approach similar to the one below. (See data/nat/gcd.lean for the key theorem, "div_gcd_eq_div_gcd".) -/ section open int example {a b c : ℤ} (co : coprime a b) (apos : a > 0) (bpos : b > 0) (H : a * a = c * (b * b)) : b = 1 := have H₁ : gcd (c * b) a = gcd c a, from gcd_mul_right_cancel_of_coprime _ (coprime_swap co), have a * a = c * b * b, by rewrite -mul.assoc at H; apply H, have a / (gcd a b) = c * b / gcd (c * b) a, from div_gcd_eq_div_gcd this bpos apos, have a = c * b / gcd c a, by revert this; rewrite [↑coprime at co, co, int.div_one, H₁]; intros; assumption, have a = b * (c / gcd c a), by revert this; rewrite [mul.comm, !int.mul_div_assoc !gcd_dvd_left]; intros; assumption, have b ∣ a, from dvd_of_mul_right_eq this⁻¹, have b ∣ gcd a b, from dvd_gcd this !dvd.refl, have b ∣ 1, by rewrite [↑coprime at co, co at this]; apply this, show b = 1, from eq_one_of_dvd_one (le_of_lt bpos) this end
208af555a3422f9c54630981f865e80bb0745c32
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/calculus/fderiv_measurable.lean
d3471ac69dd07e7bd346343e4f6a3884651c2ee8
[ "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
41,149
lean
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Yury Kudryashov -/ import analysis.calculus.deriv.basic import measure_theory.constructions.borel_space.continuous_linear_map import measure_theory.function.strongly_measurable.basic /-! # Derivative is measurable > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we prove that the derivative of any function with complete codomain is a measurable function. Namely, we prove: * `measurable_set_of_differentiable_at`: the set `{x | differentiable_at 𝕜 f x}` is measurable; * `measurable_fderiv`: the function `fderiv 𝕜 f` is measurable; * `measurable_fderiv_apply_const`: for a fixed vector `y`, the function `λ x, fderiv 𝕜 f x y` is measurable; * `measurable_deriv`: the function `deriv f` is measurable (for `f : 𝕜 → F`). We also show the same results for the right derivative on the real line (see `measurable_deriv_within_Ici` and ``measurable_deriv_within_Ioi`), following the same proof strategy. ## Implementation We give a proof that avoids second-countability issues, by expressing the differentiability set as a function of open sets in the following way. Define `A (L, r, ε)` to be the set of points where, on a ball of radius roughly `r` around `x`, the function is uniformly approximated by the linear map `L`, up to `ε r`. It is an open set. Let also `B (L, r, s, ε) = A (L, r, ε) ∩ A (L, s, ε)`: we require that at two possibly different scales `r` and `s`, the function is well approximated by the linear map `L`. It is also open. We claim that the differentiability set of `f` is exactly `D = ⋂ ε > 0, ⋃ δ > 0, ⋂ r, s < δ, ⋃ L, B (L, r, s, ε)`. In other words, for any `ε > 0`, we require that there is a size `δ` such that, for any two scales below this size, the function is well approximated by a linear map, common to the two scales. The set `⋃ L, B (L, r, s, ε)` is open, as a union of open sets. Converting the intersections and unions to countable ones (using real numbers of the form `2 ^ (-n)`), it follows that the differentiability set is measurable. To prove the claim, there are two inclusions. One is trivial: if the function is differentiable at `x`, then `x` belongs to `D` (just take `L` to be the derivative, and use that the differentiability exactly says that the map is well approximated by `L`). This is proved in `mem_A_of_differentiable` and `differentiable_set_subset_D`. For the other direction, the difficulty is that `L` in the union may depend on `ε, r, s`. The key point is that, in fact, it doesn't depend too much on them. First, if `x` belongs both to `A (L, r, ε)` and `A (L', r, ε)`, then `L` and `L'` have to be close on a shell, and thus `‖L - L'‖` is bounded by `ε` (see `norm_sub_le_of_mem_A`). Assume now `x ∈ D`. If one has two maps `L` and `L'` such that `x` belongs to `A (L, r, ε)` and to `A (L', r', ε')`, one deduces that `L` is close to `L'` by arguing as follows. Consider another scale `s` smaller than `r` and `r'`. Take a linear map `L₁` that approximates `f` around `x` both at scales `r` and `s` w.r.t. `ε` (it exists as `x` belongs to `D`). Take also `L₂` that approximates `f` around `x` both at scales `r'` and `s` w.r.t. `ε'`. Then `L₁` is close to `L` (as they are close on a shell of radius `r`), and `L₂` is close to `L₁` (as they are close on a shell of radius `s`), and `L'` is close to `L₂` (as they are close on a shell of radius `r'`). It follows that `L` is close to `L'`, as we claimed. It follows that the different approximating linear maps that show up form a Cauchy sequence when `ε` tends to `0`. When the target space is complete, this sequence converges, to a limit `f'`. With the same kind of arguments, one checks that `f` is differentiable with derivative `f'`. To show that the derivative itself is measurable, add in the definition of `B` and `D` a set `K` of continuous linear maps to which `L` should belong. Then, when `K` is complete, the set `D K` is exactly the set of points where `f` is differentiable with a derivative in `K`. ## Tags derivative, measurable function, Borel σ-algebra -/ noncomputable theory open set metric asymptotics filter continuous_linear_map open topological_space (second_countable_topology) measure_theory open_locale topology namespace continuous_linear_map variables {𝕜 E F : Type*} [nontrivially_normed_field 𝕜] [normed_add_comm_group E] [normed_space 𝕜 E] [normed_add_comm_group F] [normed_space 𝕜 F] lemma measurable_apply₂ [measurable_space E] [opens_measurable_space E] [second_countable_topology E] [second_countable_topology (E →L[𝕜] F)] [measurable_space F] [borel_space F] : measurable (λ p : (E →L[𝕜] F) × E, p.1 p.2) := is_bounded_bilinear_map_apply.continuous.measurable end continuous_linear_map section fderiv variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] variables {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] variables {F : Type*} [normed_add_comm_group F] [normed_space 𝕜 F] variables {f : E → F} (K : set (E →L[𝕜] F)) namespace fderiv_measurable_aux /-- The set `A f L r ε` is the set of points `x` around which the function `f` is well approximated at scale `r` by the linear map `L`, up to an error `ε`. We tweak the definition to make sure that this is an open set.-/ def A (f : E → F) (L : E →L[𝕜] F) (r ε : ℝ) : set E := {x | ∃ r' ∈ Ioc (r/2) r, ∀ y z ∈ ball x r', ‖f z - f y - L (z-y)‖ ≤ ε * r} /-- The set `B f K r s ε` is the set of points `x` around which there exists a continuous linear map `L` belonging to `K` (a given set of continuous linear maps) that approximates well the function `f` (up to an error `ε`), simultaneously at scales `r` and `s`. -/ def B (f : E → F) (K : set (E →L[𝕜] F)) (r s ε : ℝ) : set E := ⋃ (L ∈ K), (A f L r ε) ∩ (A f L s ε) /-- The set `D f K` is a complicated set constructed using countable intersections and unions. Its main use is that, when `K` is complete, it is exactly the set of points where `f` is differentiable, with a derivative in `K`. -/ def D (f : E → F) (K : set (E →L[𝕜] F)) : set E := ⋂ (e : ℕ), ⋃ (n : ℕ), ⋂ (p ≥ n) (q ≥ n), B f K ((1/2) ^ p) ((1/2) ^ q) ((1/2) ^ e) lemma is_open_A (L : E →L[𝕜] F) (r ε : ℝ) : is_open (A f L r ε) := begin rw metric.is_open_iff, rintros x ⟨r', r'_mem, hr'⟩, obtain ⟨s, s_gt, s_lt⟩ : ∃ (s : ℝ), r / 2 < s ∧ s < r' := exists_between r'_mem.1, have : s ∈ Ioc (r/2) r := ⟨s_gt, le_of_lt (s_lt.trans_le r'_mem.2)⟩, refine ⟨r' - s, by linarith, λ x' hx', ⟨s, this, _⟩⟩, have B : ball x' s ⊆ ball x r' := ball_subset (le_of_lt hx'), assume y hy z hz, exact hr' y (B hy) z (B hz) end lemma is_open_B {K : set (E →L[𝕜] F)} {r s ε : ℝ} : is_open (B f K r s ε) := by simp [B, is_open_Union, is_open.inter, is_open_A] lemma A_mono (L : E →L[𝕜] F) (r : ℝ) {ε δ : ℝ} (h : ε ≤ δ) : A f L r ε ⊆ A f L r δ := begin rintros x ⟨r', r'r, hr'⟩, refine ⟨r', r'r, λ y hy z hz, (hr' y hy z hz).trans (mul_le_mul_of_nonneg_right h _)⟩, linarith [mem_ball.1 hy, r'r.2, @dist_nonneg _ _ y x], end lemma le_of_mem_A {r ε : ℝ} {L : E →L[𝕜] F} {x : E} (hx : x ∈ A f L r ε) {y z : E} (hy : y ∈ closed_ball x (r/2)) (hz : z ∈ closed_ball x (r/2)) : ‖f z - f y - L (z-y)‖ ≤ ε * r := begin rcases hx with ⟨r', r'mem, hr'⟩, exact hr' _ ((mem_closed_ball.1 hy).trans_lt r'mem.1) _ ((mem_closed_ball.1 hz).trans_lt r'mem.1) end lemma mem_A_of_differentiable {ε : ℝ} (hε : 0 < ε) {x : E} (hx : differentiable_at 𝕜 f x) : ∃ R > 0, ∀ r ∈ Ioo (0 : ℝ) R, x ∈ A f (fderiv 𝕜 f x) r ε := begin have := hx.has_fderiv_at, simp only [has_fderiv_at, has_fderiv_at_filter, is_o_iff] at this, rcases eventually_nhds_iff_ball.1 (this (half_pos hε)) with ⟨R, R_pos, hR⟩, refine ⟨R, R_pos, λ r hr, _⟩, have : r ∈ Ioc (r/2) r := ⟨half_lt_self hr.1, le_rfl⟩, refine ⟨r, this, λ y hy z hz, _⟩, calc ‖f z - f y - (fderiv 𝕜 f x) (z - y)‖ = ‖(f z - f x - (fderiv 𝕜 f x) (z - x)) - (f y - f x - (fderiv 𝕜 f x) (y - x))‖ : by { congr' 1, simp only [continuous_linear_map.map_sub], abel } ... ≤ ‖(f z - f x - (fderiv 𝕜 f x) (z - x))‖ + ‖f y - f x - (fderiv 𝕜 f x) (y - x)‖ : norm_sub_le _ _ ... ≤ ε / 2 * ‖z - x‖ + ε / 2 * ‖y - x‖ : add_le_add (hR _ (lt_trans (mem_ball.1 hz) hr.2)) (hR _ (lt_trans (mem_ball.1 hy) hr.2)) ... ≤ ε / 2 * r + ε / 2 * r : add_le_add (mul_le_mul_of_nonneg_left (le_of_lt (mem_ball_iff_norm.1 hz)) (le_of_lt (half_pos hε))) (mul_le_mul_of_nonneg_left (le_of_lt (mem_ball_iff_norm.1 hy)) (le_of_lt (half_pos hε))) ... = ε * r : by ring end lemma norm_sub_le_of_mem_A {c : 𝕜} (hc : 1 < ‖c‖) {r ε : ℝ} (hε : 0 < ε) (hr : 0 < r) {x : E} {L₁ L₂ : E →L[𝕜] F} (h₁ : x ∈ A f L₁ r ε) (h₂ : x ∈ A f L₂ r ε) : ‖L₁ - L₂‖ ≤ 4 * ‖c‖ * ε := begin have : 0 ≤ 4 * ‖c‖ * ε := mul_nonneg (mul_nonneg (by norm_num : (0 : ℝ) ≤ 4) (norm_nonneg _)) hε.le, refine op_norm_le_of_shell (half_pos hr) this hc _, assume y ley ylt, rw [div_div, div_le_iff' (mul_pos (by norm_num : (0 : ℝ) < 2) (zero_lt_one.trans hc))] at ley, calc ‖(L₁ - L₂) y‖ = ‖(f (x + y) - f x - L₂ ((x + y) - x)) - (f (x + y) - f x - L₁ ((x + y) - x))‖ : by simp ... ≤ ‖(f (x + y) - f x - L₂ ((x + y) - x))‖ + ‖(f (x + y) - f x - L₁ ((x + y) - x))‖ : norm_sub_le _ _ ... ≤ ε * r + ε * r : begin apply add_le_add, { apply le_of_mem_A h₂, { simp only [le_of_lt (half_pos hr), mem_closed_ball, dist_self] }, { simp only [dist_eq_norm, add_sub_cancel', mem_closed_ball, ylt.le], } }, { apply le_of_mem_A h₁, { simp only [le_of_lt (half_pos hr), mem_closed_ball, dist_self] }, { simp only [dist_eq_norm, add_sub_cancel', mem_closed_ball, ylt.le] } }, end ... = 2 * ε * r : by ring ... ≤ 2 * ε * (2 * ‖c‖ * ‖y‖) : mul_le_mul_of_nonneg_left ley (mul_nonneg (by norm_num) hε.le) ... = 4 * ‖c‖ * ε * ‖y‖ : by ring end /-- Easy inclusion: a differentiability point with derivative in `K` belongs to `D f K`. -/ lemma differentiable_set_subset_D : {x | differentiable_at 𝕜 f x ∧ fderiv 𝕜 f x ∈ K} ⊆ D f K := begin assume x hx, rw [D, mem_Inter], assume e, have : (0 : ℝ) < (1/2) ^ e := pow_pos (by norm_num) _, rcases mem_A_of_differentiable this hx.1 with ⟨R, R_pos, hR⟩, obtain ⟨n, hn⟩ : ∃ (n : ℕ), (1/2) ^ n < R := exists_pow_lt_of_lt_one R_pos (by norm_num : (1 : ℝ)/2 < 1), simp only [mem_Union, mem_Inter, B, mem_inter_iff], refine ⟨n, λ p hp q hq, ⟨fderiv 𝕜 f x, hx.2, ⟨_, _⟩⟩⟩; { refine hR _ ⟨pow_pos (by norm_num) _, lt_of_le_of_lt _ hn⟩, exact pow_le_pow_of_le_one (by norm_num) (by norm_num) (by assumption) } end /-- Harder inclusion: at a point in `D f K`, the function `f` has a derivative, in `K`. -/ lemma D_subset_differentiable_set {K : set (E →L[𝕜] F)} (hK : is_complete K) : D f K ⊆ {x | differentiable_at 𝕜 f x ∧ fderiv 𝕜 f x ∈ K} := begin have P : ∀ {n : ℕ}, (0 : ℝ) < (1/2) ^ n := pow_pos (by norm_num), rcases normed_field.exists_one_lt_norm 𝕜 with ⟨c, hc⟩, have cpos : 0 < ‖c‖ := lt_trans zero_lt_one hc, assume x hx, have : ∀ (e : ℕ), ∃ (n : ℕ), ∀ p q, n ≤ p → n ≤ q → ∃ L ∈ K, x ∈ A f L ((1/2) ^ p) ((1/2) ^ e) ∩ A f L ((1/2) ^ q) ((1/2) ^ e), { assume e, have := mem_Inter.1 hx e, rcases mem_Union.1 this with ⟨n, hn⟩, refine ⟨n, λ p q hp hq, _⟩, simp only [mem_Inter, ge_iff_le] at hn, rcases mem_Union.1 (hn p hp q hq) with ⟨L, hL⟩, exact ⟨L, mem_Union.1 hL⟩, }, /- Recast the assumptions: for each `e`, there exist `n e` and linear maps `L e p q` in `K` such that, for `p, q ≥ n e`, then `f` is well approximated by `L e p q` at scale `2 ^ (-p)` and `2 ^ (-q)`, with an error `2 ^ (-e)`. -/ choose! n L hn using this, /- All the operators `L e p q` that show up are close to each other. To prove this, we argue that `L e p q` is close to `L e p r` (where `r` is large enough), as both approximate `f` at scale `2 ^(- p)`. And `L e p r` is close to `L e' p' r` as both approximate `f` at scale `2 ^ (- r)`. And `L e' p' r` is close to `L e' p' q'` as both approximate `f` at scale `2 ^ (- p')`. -/ have M : ∀ e p q e' p' q', n e ≤ p → n e ≤ q → n e' ≤ p' → n e' ≤ q' → e ≤ e' → ‖L e p q - L e' p' q'‖ ≤ 12 * ‖c‖ * (1/2) ^ e, { assume e p q e' p' q' hp hq hp' hq' he', let r := max (n e) (n e'), have I : ((1:ℝ)/2)^e' ≤ (1/2)^e := pow_le_pow_of_le_one (by norm_num) (by norm_num) he', have J1 : ‖L e p q - L e p r‖ ≤ 4 * ‖c‖ * (1/2)^e, { have I1 : x ∈ A f (L e p q) ((1 / 2) ^ p) ((1/2)^e) := (hn e p q hp hq).2.1, have I2 : x ∈ A f (L e p r) ((1 / 2) ^ p) ((1/2)^e) := (hn e p r hp (le_max_left _ _)).2.1, exact norm_sub_le_of_mem_A hc P P I1 I2 }, have J2 : ‖L e p r - L e' p' r‖ ≤ 4 * ‖c‖ * (1/2)^e, { have I1 : x ∈ A f (L e p r) ((1 / 2) ^ r) ((1/2)^e) := (hn e p r hp (le_max_left _ _)).2.2, have I2 : x ∈ A f (L e' p' r) ((1 / 2) ^ r) ((1/2)^e') := (hn e' p' r hp' (le_max_right _ _)).2.2, exact norm_sub_le_of_mem_A hc P P I1 (A_mono _ _ I I2) }, have J3 : ‖L e' p' r - L e' p' q'‖ ≤ 4 * ‖c‖ * (1/2)^e, { have I1 : x ∈ A f (L e' p' r) ((1 / 2) ^ p') ((1/2)^e') := (hn e' p' r hp' (le_max_right _ _)).2.1, have I2 : x ∈ A f (L e' p' q') ((1 / 2) ^ p') ((1/2)^e') := (hn e' p' q' hp' hq').2.1, exact norm_sub_le_of_mem_A hc P P (A_mono _ _ I I1) (A_mono _ _ I I2) }, calc ‖L e p q - L e' p' q'‖ = ‖(L e p q - L e p r) + (L e p r - L e' p' r) + (L e' p' r - L e' p' q')‖ : by { congr' 1, abel } ... ≤ ‖L e p q - L e p r‖ + ‖L e p r - L e' p' r‖ + ‖L e' p' r - L e' p' q'‖ : le_trans (norm_add_le _ _) (add_le_add_right (norm_add_le _ _) _) ... ≤ 4 * ‖c‖ * (1/2)^e + 4 * ‖c‖ * (1/2)^e + 4 * ‖c‖ * (1/2)^e : by apply_rules [add_le_add] ... = 12 * ‖c‖ * (1/2)^e : by ring }, /- For definiteness, use `L0 e = L e (n e) (n e)`, to have a single sequence. We claim that this is a Cauchy sequence. -/ let L0 : ℕ → (E →L[𝕜] F) := λ e, L e (n e) (n e), have : cauchy_seq L0, { rw metric.cauchy_seq_iff', assume ε εpos, obtain ⟨e, he⟩ : ∃ (e : ℕ), (1/2) ^ e < ε / (12 * ‖c‖) := exists_pow_lt_of_lt_one (div_pos εpos (mul_pos (by norm_num) cpos)) (by norm_num), refine ⟨e, λ e' he', _⟩, rw [dist_comm, dist_eq_norm], calc ‖L0 e - L0 e'‖ ≤ 12 * ‖c‖ * (1/2)^e : M _ _ _ _ _ _ le_rfl le_rfl le_rfl le_rfl he' ... < 12 * ‖c‖ * (ε / (12 * ‖c‖)) : mul_lt_mul' le_rfl he (le_of_lt P) (mul_pos (by norm_num) cpos) ... = ε : by { field_simp [(by norm_num : (12 : ℝ) ≠ 0), ne_of_gt cpos], ring } }, /- As it is Cauchy, the sequence `L0` converges, to a limit `f'` in `K`.-/ obtain ⟨f', f'K, hf'⟩ : ∃ f' ∈ K, tendsto L0 at_top (𝓝 f') := cauchy_seq_tendsto_of_is_complete hK (λ e, (hn e (n e) (n e) le_rfl le_rfl).1) this, have Lf' : ∀ e p, n e ≤ p → ‖L e (n e) p - f'‖ ≤ 12 * ‖c‖ * (1/2)^e, { assume e p hp, apply le_of_tendsto (tendsto_const_nhds.sub hf').norm, rw eventually_at_top, exact ⟨e, λ e' he', M _ _ _ _ _ _ le_rfl hp le_rfl le_rfl he'⟩ }, /- Let us show that `f` has derivative `f'` at `x`. -/ have : has_fderiv_at f f' x, { simp only [has_fderiv_at_iff_is_o_nhds_zero, is_o_iff], /- to get an approximation with a precision `ε`, we will replace `f` with `L e (n e) m` for some large enough `e` (yielding a small error by uniform approximation). As one can vary `m`, this makes it possible to cover all scales, and thus to obtain a good linear approximation in the whole ball of radius `(1/2)^(n e)`. -/ assume ε εpos, have pos : 0 < 4 + 12 * ‖c‖ := add_pos_of_pos_of_nonneg (by norm_num) (mul_nonneg (by norm_num) (norm_nonneg _)), obtain ⟨e, he⟩ : ∃ (e : ℕ), (1 / 2) ^ e < ε / (4 + 12 * ‖c‖) := exists_pow_lt_of_lt_one (div_pos εpos pos) (by norm_num), rw eventually_nhds_iff_ball, refine ⟨(1/2) ^ (n e + 1), P, λ y hy, _⟩, -- We need to show that `f (x + y) - f x - f' y` is small. For this, we will work at scale -- `k` where `k` is chosen with `‖y‖ ∼ 2 ^ (-k)`. by_cases y_pos : y = 0, {simp [y_pos] }, have yzero : 0 < ‖y‖ := norm_pos_iff.mpr y_pos, have y_lt : ‖y‖ < (1/2) ^ (n e + 1), by simpa using mem_ball_iff_norm.1 hy, have yone : ‖y‖ ≤ 1 := le_trans (y_lt.le) (pow_le_one _ (by norm_num) (by norm_num)), -- define the scale `k`. obtain ⟨k, hk, h'k⟩ : ∃ (k : ℕ), (1/2) ^ (k + 1) < ‖y‖ ∧ ‖y‖ ≤ (1/2) ^ k := exists_nat_pow_near_of_lt_one yzero yone (by norm_num : (0 : ℝ) < 1/2) (by norm_num : (1 : ℝ)/2 < 1), -- the scale is large enough (as `y` is small enough) have k_gt : n e < k, { have : ((1:ℝ)/2) ^ (k + 1) < (1/2) ^ (n e + 1) := lt_trans hk y_lt, rw pow_lt_pow_iff_of_lt_one (by norm_num : (0 : ℝ) < 1/2) (by norm_num) at this, linarith }, set m := k - 1 with hl, have m_ge : n e ≤ m := nat.le_pred_of_lt k_gt, have km : k = m + 1 := (nat.succ_pred_eq_of_pos (lt_of_le_of_lt (zero_le _) k_gt)).symm, rw km at hk h'k, -- `f` is well approximated by `L e (n e) k` at the relevant scale -- (in fact, we use `m = k - 1` instead of `k` because of the precise definition of `A`). have J1 : ‖f (x + y) - f x - L e (n e) m ((x + y) - x)‖ ≤ (1/2) ^ e * (1/2) ^ m, { apply le_of_mem_A (hn e (n e) m le_rfl m_ge).2.2, { simp only [mem_closed_ball, dist_self], exact div_nonneg (le_of_lt P) (zero_le_two) }, { simpa only [dist_eq_norm, add_sub_cancel', mem_closed_ball, pow_succ', mul_one_div] using h'k } }, have J2 : ‖f (x + y) - f x - L e (n e) m y‖ ≤ 4 * (1/2) ^ e * ‖y‖ := calc ‖f (x + y) - f x - L e (n e) m y‖ ≤ (1/2) ^ e * (1/2) ^ m : by simpa only [add_sub_cancel'] using J1 ... = 4 * (1/2) ^ e * (1/2) ^ (m + 2) : by { field_simp, ring_exp } ... ≤ 4 * (1/2) ^ e * ‖y‖ : mul_le_mul_of_nonneg_left (le_of_lt hk) (mul_nonneg (by norm_num) (le_of_lt P)), -- use the previous estimates to see that `f (x + y) - f x - f' y` is small. calc ‖f (x + y) - f x - f' y‖ = ‖(f (x + y) - f x - L e (n e) m y) + (L e (n e) m - f') y‖ : congr_arg _ (by simp) ... ≤ 4 * (1/2) ^ e * ‖y‖ + 12 * ‖c‖ * (1/2) ^ e * ‖y‖ : norm_add_le_of_le J2 ((le_op_norm _ _).trans (mul_le_mul_of_nonneg_right (Lf' _ _ m_ge) (norm_nonneg _))) ... = (4 + 12 * ‖c‖) * ‖y‖ * (1/2) ^ e : by ring ... ≤ (4 + 12 * ‖c‖) * ‖y‖ * (ε / (4 + 12 * ‖c‖)) : mul_le_mul_of_nonneg_left he.le (mul_nonneg (add_nonneg (by norm_num) (mul_nonneg (by norm_num) (norm_nonneg _))) (norm_nonneg _)) ... = ε * ‖y‖ : by { field_simp [ne_of_gt pos], ring } }, rw ← this.fderiv at f'K, exact ⟨this.differentiable_at, f'K⟩ end theorem differentiable_set_eq_D (hK : is_complete K) : {x | differentiable_at 𝕜 f x ∧ fderiv 𝕜 f x ∈ K} = D f K := subset.antisymm (differentiable_set_subset_D _) (D_subset_differentiable_set hK) end fderiv_measurable_aux open fderiv_measurable_aux variables [measurable_space E] [opens_measurable_space E] variables (𝕜 f) /-- The set of differentiability points of a function, with derivative in a given complete set, is Borel-measurable. -/ theorem measurable_set_of_differentiable_at_of_is_complete {K : set (E →L[𝕜] F)} (hK : is_complete K) : measurable_set {x | differentiable_at 𝕜 f x ∧ fderiv 𝕜 f x ∈ K} := by simp [differentiable_set_eq_D K hK, D, is_open_B.measurable_set, measurable_set.Inter, measurable_set.Union] variable [complete_space F] /-- The set of differentiability points of a function taking values in a complete space is Borel-measurable. -/ theorem measurable_set_of_differentiable_at : measurable_set {x | differentiable_at 𝕜 f x} := begin have : is_complete (univ : set (E →L[𝕜] F)) := complete_univ, convert measurable_set_of_differentiable_at_of_is_complete 𝕜 f this, simp end @[measurability] lemma measurable_fderiv : measurable (fderiv 𝕜 f) := begin refine measurable_of_is_closed (λ s hs, _), have : fderiv 𝕜 f ⁻¹' s = {x | differentiable_at 𝕜 f x ∧ fderiv 𝕜 f x ∈ s} ∪ ({x | ¬differentiable_at 𝕜 f x} ∩ {x | (0 : E →L[𝕜] F) ∈ s}) := set.ext (λ x, mem_preimage.trans fderiv_mem_iff), rw this, exact (measurable_set_of_differentiable_at_of_is_complete _ _ hs.is_complete).union ((measurable_set_of_differentiable_at _ _).compl.inter (measurable_set.const _)) end @[measurability] lemma measurable_fderiv_apply_const [measurable_space F] [borel_space F] (y : E) : measurable (λ x, fderiv 𝕜 f x y) := (continuous_linear_map.measurable_apply y).comp (measurable_fderiv 𝕜 f) variable {𝕜} @[measurability] lemma measurable_deriv [measurable_space 𝕜] [opens_measurable_space 𝕜] [measurable_space F] [borel_space F] (f : 𝕜 → F) : measurable (deriv f) := by simpa only [fderiv_deriv] using measurable_fderiv_apply_const 𝕜 f 1 lemma strongly_measurable_deriv [measurable_space 𝕜] [opens_measurable_space 𝕜] [second_countable_topology F] (f : 𝕜 → F) : strongly_measurable (deriv f) := by { borelize F, exact (measurable_deriv f).strongly_measurable } lemma ae_measurable_deriv [measurable_space 𝕜] [opens_measurable_space 𝕜] [measurable_space F] [borel_space F] (f : 𝕜 → F) (μ : measure 𝕜) : ae_measurable (deriv f) μ := (measurable_deriv f).ae_measurable lemma ae_strongly_measurable_deriv [measurable_space 𝕜] [opens_measurable_space 𝕜] [second_countable_topology F] (f : 𝕜 → F) (μ : measure 𝕜) : ae_strongly_measurable (deriv f) μ := (strongly_measurable_deriv f).ae_strongly_measurable end fderiv section right_deriv variables {F : Type*} [normed_add_comm_group F] [normed_space ℝ F] variables {f : ℝ → F} (K : set F) namespace right_deriv_measurable_aux /-- The set `A f L r ε` is the set of points `x` around which the function `f` is well approximated at scale `r` by the linear map `h ↦ h • L`, up to an error `ε`. We tweak the definition to make sure that this is open on the right. -/ def A (f : ℝ → F) (L : F) (r ε : ℝ) : set ℝ := {x | ∃ r' ∈ Ioc (r/2) r, ∀ y z ∈ Icc x (x + r'), ‖f z - f y - (z-y) • L‖ ≤ ε * r} /-- The set `B f K r s ε` is the set of points `x` around which there exists a vector `L` belonging to `K` (a given set of vectors) such that `h • L` approximates well `f (x + h)` (up to an error `ε`), simultaneously at scales `r` and `s`. -/ def B (f : ℝ → F) (K : set F) (r s ε : ℝ) : set ℝ := ⋃ (L ∈ K), (A f L r ε) ∩ (A f L s ε) /-- The set `D f K` is a complicated set constructed using countable intersections and unions. Its main use is that, when `K` is complete, it is exactly the set of points where `f` is differentiable, with a derivative in `K`. -/ def D (f : ℝ → F) (K : set F) : set ℝ := ⋂ (e : ℕ), ⋃ (n : ℕ), ⋂ (p ≥ n) (q ≥ n), B f K ((1/2) ^ p) ((1/2) ^ q) ((1/2) ^ e) lemma A_mem_nhds_within_Ioi {L : F} {r ε x : ℝ} (hx : x ∈ A f L r ε) : A f L r ε ∈ 𝓝[>] x := begin rcases hx with ⟨r', rr', hr'⟩, rw mem_nhds_within_Ioi_iff_exists_Ioo_subset, obtain ⟨s, s_gt, s_lt⟩ : ∃ (s : ℝ), r / 2 < s ∧ s < r' := exists_between rr'.1, have : s ∈ Ioc (r/2) r := ⟨s_gt, le_of_lt (s_lt.trans_le rr'.2)⟩, refine ⟨x + r' - s, by { simp only [mem_Ioi], linarith }, λ x' hx', ⟨s, this, _⟩⟩, have A : Icc x' (x' + s) ⊆ Icc x (x + r'), { apply Icc_subset_Icc hx'.1.le, linarith [hx'.2] }, assume y hy z hz, exact hr' y (A hy) z (A hz) end lemma B_mem_nhds_within_Ioi {K : set F} {r s ε x : ℝ} (hx : x ∈ B f K r s ε) : B f K r s ε ∈ 𝓝[>] x := begin obtain ⟨L, LK, hL₁, hL₂⟩ : ∃ (L : F), L ∈ K ∧ x ∈ A f L r ε ∧ x ∈ A f L s ε, by simpa only [B, mem_Union, mem_inter_iff, exists_prop] using hx, filter_upwards [A_mem_nhds_within_Ioi hL₁, A_mem_nhds_within_Ioi hL₂] with y hy₁ hy₂, simp only [B, mem_Union, mem_inter_iff, exists_prop], exact ⟨L, LK, hy₁, hy₂⟩ end lemma measurable_set_B {K : set F} {r s ε : ℝ} : measurable_set (B f K r s ε) := measurable_set_of_mem_nhds_within_Ioi (λ x hx, B_mem_nhds_within_Ioi hx) lemma A_mono (L : F) (r : ℝ) {ε δ : ℝ} (h : ε ≤ δ) : A f L r ε ⊆ A f L r δ := begin rintros x ⟨r', r'r, hr'⟩, refine ⟨r', r'r, λ y hy z hz, (hr' y hy z hz).trans (mul_le_mul_of_nonneg_right h _)⟩, linarith [hy.1, hy.2, r'r.2], end lemma le_of_mem_A {r ε : ℝ} {L : F} {x : ℝ} (hx : x ∈ A f L r ε) {y z : ℝ} (hy : y ∈ Icc x (x + r/2)) (hz : z ∈ Icc x (x + r/2)) : ‖f z - f y - (z-y) • L‖ ≤ ε * r := begin rcases hx with ⟨r', r'mem, hr'⟩, have A : x + r / 2 ≤ x + r', by linarith [r'mem.1], exact hr' _ ((Icc_subset_Icc le_rfl A) hy) _ ((Icc_subset_Icc le_rfl A) hz), end lemma mem_A_of_differentiable {ε : ℝ} (hε : 0 < ε) {x : ℝ} (hx : differentiable_within_at ℝ f (Ici x) x) : ∃ R > 0, ∀ r ∈ Ioo (0 : ℝ) R, x ∈ A f (deriv_within f (Ici x) x) r ε := begin have := hx.has_deriv_within_at, simp_rw [has_deriv_within_at_iff_is_o, is_o_iff] at this, rcases mem_nhds_within_Ici_iff_exists_Ico_subset.1 (this (half_pos hε)) with ⟨m, xm, hm⟩, refine ⟨m - x, by linarith [show x < m, from xm], λ r hr, _⟩, have : r ∈ Ioc (r/2) r := ⟨half_lt_self hr.1, le_rfl⟩, refine ⟨r, this, λ y hy z hz, _⟩, calc ‖f z - f y - (z - y) • deriv_within f (Ici x) x‖ = ‖(f z - f x - (z - x) • deriv_within f (Ici x) x) - (f y - f x - (y - x) • deriv_within f (Ici x) x)‖ : by { congr' 1, simp only [sub_smul], abel } ... ≤ ‖f z - f x - (z - x) • deriv_within f (Ici x) x‖ + ‖f y - f x - (y - x) • deriv_within f (Ici x) x‖ : norm_sub_le _ _ ... ≤ ε / 2 * ‖z - x‖ + ε / 2 * ‖y - x‖ : add_le_add (hm ⟨hz.1, hz.2.trans_lt (by linarith [hr.2])⟩) (hm ⟨hy.1, hy.2.trans_lt (by linarith [hr.2])⟩) ... ≤ ε / 2 * r + ε / 2 * r : begin apply add_le_add, { apply mul_le_mul_of_nonneg_left _ (le_of_lt (half_pos hε)), rw [real.norm_of_nonneg]; linarith [hz.1, hz.2] }, { apply mul_le_mul_of_nonneg_left _ (le_of_lt (half_pos hε)), rw [real.norm_of_nonneg]; linarith [hy.1, hy.2] }, end ... = ε * r : by ring end lemma norm_sub_le_of_mem_A {r x : ℝ} (hr : 0 < r) (ε : ℝ) {L₁ L₂ : F} (h₁ : x ∈ A f L₁ r ε) (h₂ : x ∈ A f L₂ r ε) : ‖L₁ - L₂‖ ≤ 4 * ε := begin suffices H : ‖(r/2) • (L₁ - L₂)‖ ≤ (r / 2) * (4 * ε), by rwa [norm_smul, real.norm_of_nonneg (half_pos hr).le, mul_le_mul_left (half_pos hr)] at H, calc ‖(r/2) • (L₁ - L₂)‖ = ‖(f (x + r/2) - f x - (x + r/2 - x) • L₂) - (f (x + r/2) - f x - (x + r/2 - x) • L₁)‖ : by simp [smul_sub] ... ≤ ‖f (x + r/2) - f x - (x + r/2 - x) • L₂‖ + ‖f (x + r/2) - f x - (x + r/2 - x) • L₁‖ : norm_sub_le _ _ ... ≤ ε * r + ε * r : begin apply add_le_add, { apply le_of_mem_A h₂; simp [(half_pos hr).le] }, { apply le_of_mem_A h₁; simp [(half_pos hr).le] }, end ... = (r / 2) * (4 * ε) : by ring end /-- Easy inclusion: a differentiability point with derivative in `K` belongs to `D f K`. -/ lemma differentiable_set_subset_D : {x | differentiable_within_at ℝ f (Ici x) x ∧ deriv_within f (Ici x) x ∈ K} ⊆ D f K := begin assume x hx, rw [D, mem_Inter], assume e, have : (0 : ℝ) < (1/2) ^ e := pow_pos (by norm_num) _, rcases mem_A_of_differentiable this hx.1 with ⟨R, R_pos, hR⟩, obtain ⟨n, hn⟩ : ∃ (n : ℕ), (1/2) ^ n < R := exists_pow_lt_of_lt_one R_pos (by norm_num : (1 : ℝ)/2 < 1), simp only [mem_Union, mem_Inter, B, mem_inter_iff], refine ⟨n, λ p hp q hq, ⟨deriv_within f (Ici x) x, hx.2, ⟨_, _⟩⟩⟩; { refine hR _ ⟨pow_pos (by norm_num) _, lt_of_le_of_lt _ hn⟩, exact pow_le_pow_of_le_one (by norm_num) (by norm_num) (by assumption) } end /-- Harder inclusion: at a point in `D f K`, the function `f` has a derivative, in `K`. -/ lemma D_subset_differentiable_set {K : set F} (hK : is_complete K) : D f K ⊆ {x | differentiable_within_at ℝ f (Ici x) x ∧ deriv_within f (Ici x) x ∈ K} := begin have P : ∀ {n : ℕ}, (0 : ℝ) < (1/2) ^ n := pow_pos (by norm_num), assume x hx, have : ∀ (e : ℕ), ∃ (n : ℕ), ∀ p q, n ≤ p → n ≤ q → ∃ L ∈ K, x ∈ A f L ((1/2) ^ p) ((1/2) ^ e) ∩ A f L ((1/2) ^ q) ((1/2) ^ e), { assume e, have := mem_Inter.1 hx e, rcases mem_Union.1 this with ⟨n, hn⟩, refine ⟨n, λ p q hp hq, _⟩, simp only [mem_Inter, ge_iff_le] at hn, rcases mem_Union.1 (hn p hp q hq) with ⟨L, hL⟩, exact ⟨L, mem_Union.1 hL⟩, }, /- Recast the assumptions: for each `e`, there exist `n e` and linear maps `L e p q` in `K` such that, for `p, q ≥ n e`, then `f` is well approximated by `L e p q` at scale `2 ^ (-p)` and `2 ^ (-q)`, with an error `2 ^ (-e)`. -/ choose! n L hn using this, /- All the operators `L e p q` that show up are close to each other. To prove this, we argue that `L e p q` is close to `L e p r` (where `r` is large enough), as both approximate `f` at scale `2 ^(- p)`. And `L e p r` is close to `L e' p' r` as both approximate `f` at scale `2 ^ (- r)`. And `L e' p' r` is close to `L e' p' q'` as both approximate `f` at scale `2 ^ (- p')`. -/ have M : ∀ e p q e' p' q', n e ≤ p → n e ≤ q → n e' ≤ p' → n e' ≤ q' → e ≤ e' → ‖L e p q - L e' p' q'‖ ≤ 12 * (1/2) ^ e, { assume e p q e' p' q' hp hq hp' hq' he', let r := max (n e) (n e'), have I : ((1:ℝ)/2)^e' ≤ (1/2)^e := pow_le_pow_of_le_one (by norm_num) (by norm_num) he', have J1 : ‖L e p q - L e p r‖ ≤ 4 * (1/2)^e, { have I1 : x ∈ A f (L e p q) ((1 / 2) ^ p) ((1/2)^e) := (hn e p q hp hq).2.1, have I2 : x ∈ A f (L e p r) ((1 / 2) ^ p) ((1/2)^e) := (hn e p r hp (le_max_left _ _)).2.1, exact norm_sub_le_of_mem_A P _ I1 I2 }, have J2 : ‖L e p r - L e' p' r‖ ≤ 4 * (1/2)^e, { have I1 : x ∈ A f (L e p r) ((1 / 2) ^ r) ((1/2)^e) := (hn e p r hp (le_max_left _ _)).2.2, have I2 : x ∈ A f (L e' p' r) ((1 / 2) ^ r) ((1/2)^e') := (hn e' p' r hp' (le_max_right _ _)).2.2, exact norm_sub_le_of_mem_A P _ I1 (A_mono _ _ I I2) }, have J3 : ‖L e' p' r - L e' p' q'‖ ≤ 4 * (1/2)^e, { have I1 : x ∈ A f (L e' p' r) ((1 / 2) ^ p') ((1/2)^e') := (hn e' p' r hp' (le_max_right _ _)).2.1, have I2 : x ∈ A f (L e' p' q') ((1 / 2) ^ p') ((1/2)^e') := (hn e' p' q' hp' hq').2.1, exact norm_sub_le_of_mem_A P _ (A_mono _ _ I I1) (A_mono _ _ I I2) }, calc ‖L e p q - L e' p' q'‖ = ‖(L e p q - L e p r) + (L e p r - L e' p' r) + (L e' p' r - L e' p' q')‖ : by { congr' 1, abel } ... ≤ ‖L e p q - L e p r‖ + ‖L e p r - L e' p' r‖ + ‖L e' p' r - L e' p' q'‖ : le_trans (norm_add_le _ _) (add_le_add_right (norm_add_le _ _) _) ... ≤ 4 * (1/2)^e + 4 * (1/2)^e + 4 * (1/2)^e : by apply_rules [add_le_add] ... = 12 * (1/2)^e : by ring }, /- For definiteness, use `L0 e = L e (n e) (n e)`, to have a single sequence. We claim that this is a Cauchy sequence. -/ let L0 : ℕ → F := λ e, L e (n e) (n e), have : cauchy_seq L0, { rw metric.cauchy_seq_iff', assume ε εpos, obtain ⟨e, he⟩ : ∃ (e : ℕ), (1/2) ^ e < ε / 12 := exists_pow_lt_of_lt_one (div_pos εpos (by norm_num)) (by norm_num), refine ⟨e, λ e' he', _⟩, rw [dist_comm, dist_eq_norm], calc ‖L0 e - L0 e'‖ ≤ 12 * (1/2)^e : M _ _ _ _ _ _ le_rfl le_rfl le_rfl le_rfl he' ... < 12 * (ε / 12) : mul_lt_mul' le_rfl he (le_of_lt P) (by norm_num) ... = ε : by { field_simp [(by norm_num : (12 : ℝ) ≠ 0)], ring } }, /- As it is Cauchy, the sequence `L0` converges, to a limit `f'` in `K`.-/ obtain ⟨f', f'K, hf'⟩ : ∃ f' ∈ K, tendsto L0 at_top (𝓝 f') := cauchy_seq_tendsto_of_is_complete hK (λ e, (hn e (n e) (n e) le_rfl le_rfl).1) this, have Lf' : ∀ e p, n e ≤ p → ‖L e (n e) p - f'‖ ≤ 12 * (1/2)^e, { assume e p hp, apply le_of_tendsto (tendsto_const_nhds.sub hf').norm, rw eventually_at_top, exact ⟨e, λ e' he', M _ _ _ _ _ _ le_rfl hp le_rfl le_rfl he'⟩ }, /- Let us show that `f` has right derivative `f'` at `x`. -/ have : has_deriv_within_at f f' (Ici x) x, { simp only [has_deriv_within_at_iff_is_o, is_o_iff], /- to get an approximation with a precision `ε`, we will replace `f` with `L e (n e) m` for some large enough `e` (yielding a small error by uniform approximation). As one can vary `m`, this makes it possible to cover all scales, and thus to obtain a good linear approximation in the whole interval of length `(1/2)^(n e)`. -/ assume ε εpos, obtain ⟨e, he⟩ : ∃ (e : ℕ), (1 / 2) ^ e < ε / 16 := exists_pow_lt_of_lt_one (div_pos εpos (by norm_num)) (by norm_num), have xmem : x ∈ Ico x (x + (1/2)^(n e + 1)), by simp only [one_div, left_mem_Ico, lt_add_iff_pos_right, inv_pos, pow_pos, zero_lt_bit0, zero_lt_one], filter_upwards [Icc_mem_nhds_within_Ici xmem] with y hy, -- We need to show that `f y - f x - f' (y - x)` is small. For this, we will work at scale -- `k` where `k` is chosen with `‖y - x‖ ∼ 2 ^ (-k)`. rcases eq_or_lt_of_le hy.1 with rfl|xy, { simp only [sub_self, zero_smul, norm_zero, mul_zero]}, have yzero : 0 < y - x := sub_pos.2 xy, have y_le : y - x ≤ (1/2) ^ (n e + 1), by linarith [hy.2], have yone : y - x ≤ 1 := le_trans y_le (pow_le_one _ (by norm_num) (by norm_num)), -- define the scale `k`. obtain ⟨k, hk, h'k⟩ : ∃ (k : ℕ), (1/2) ^ (k + 1) < y - x ∧ y - x ≤ (1/2) ^ k := exists_nat_pow_near_of_lt_one yzero yone (by norm_num : (0 : ℝ) < 1/2) (by norm_num : (1 : ℝ)/2 < 1), -- the scale is large enough (as `y - x` is small enough) have k_gt : n e < k, { have : ((1:ℝ)/2) ^ (k + 1) < (1/2) ^ (n e + 1) := lt_of_lt_of_le hk y_le, rw pow_lt_pow_iff_of_lt_one (by norm_num : (0 : ℝ) < 1/2) (by norm_num) at this, linarith }, set m := k - 1 with hl, have m_ge : n e ≤ m := nat.le_pred_of_lt k_gt, have km : k = m + 1 := (nat.succ_pred_eq_of_pos (lt_of_le_of_lt (zero_le _) k_gt)).symm, rw km at hk h'k, -- `f` is well approximated by `L e (n e) k` at the relevant scale -- (in fact, we use `m = k - 1` instead of `k` because of the precise definition of `A`). have J : ‖f y - f x - (y - x) • L e (n e) m‖ ≤ 4 * (1/2) ^ e * ‖y - x‖ := calc ‖f y - f x - (y - x) • L e (n e) m‖ ≤ (1/2) ^ e * (1/2) ^ m : begin apply le_of_mem_A (hn e (n e) m le_rfl m_ge).2.2, { simp only [one_div, inv_pow, left_mem_Icc, le_add_iff_nonneg_right], exact div_nonneg (inv_nonneg.2 (pow_nonneg zero_le_two _)) zero_le_two }, { simp only [pow_add, tsub_le_iff_left] at h'k, simpa only [hy.1, mem_Icc, true_and, one_div, pow_one] using h'k } end ... = 4 * (1/2) ^ e * (1/2) ^ (m + 2) : by { field_simp, ring_exp } ... ≤ 4 * (1/2) ^ e * (y - x) : mul_le_mul_of_nonneg_left (le_of_lt hk) (mul_nonneg (by norm_num) (le_of_lt P)) ... = 4 * (1/2) ^ e * ‖y - x‖ : by rw [real.norm_of_nonneg yzero.le], calc ‖f y - f x - (y - x) • f'‖ = ‖(f y - f x - (y - x) • L e (n e) m) + (y - x) • (L e (n e) m - f')‖ : by simp only [smul_sub, sub_add_sub_cancel] ... ≤ 4 * (1/2) ^ e * ‖y - x‖ + ‖y - x‖ * (12 * (1/2) ^ e) : norm_add_le_of_le J (by { rw [norm_smul], exact mul_le_mul_of_nonneg_left (Lf' _ _ m_ge) (norm_nonneg _) }) ... = 16 * ‖y - x‖ * (1/2) ^ e : by ring ... ≤ 16 * ‖y - x‖ * (ε / 16) : mul_le_mul_of_nonneg_left he.le (mul_nonneg (by norm_num) (norm_nonneg _)) ... = ε * ‖y - x‖ : by ring }, rw ← this.deriv_within (unique_diff_on_Ici x x le_rfl) at f'K, exact ⟨this.differentiable_within_at, f'K⟩, end theorem differentiable_set_eq_D (hK : is_complete K) : {x | differentiable_within_at ℝ f (Ici x) x ∧ deriv_within f (Ici x) x ∈ K} = D f K := subset.antisymm (differentiable_set_subset_D _) (D_subset_differentiable_set hK) end right_deriv_measurable_aux open right_deriv_measurable_aux variables (f) /-- The set of right differentiability points of a function, with derivative in a given complete set, is Borel-measurable. -/ theorem measurable_set_of_differentiable_within_at_Ici_of_is_complete {K : set F} (hK : is_complete K) : measurable_set {x | differentiable_within_at ℝ f (Ici x) x ∧ deriv_within f (Ici x) x ∈ K} := by simp [differentiable_set_eq_D K hK, D, measurable_set_B, measurable_set.Inter, measurable_set.Union] variable [complete_space F] /-- The set of right differentiability points of a function taking values in a complete space is Borel-measurable. -/ theorem measurable_set_of_differentiable_within_at_Ici : measurable_set {x | differentiable_within_at ℝ f (Ici x) x} := begin have : is_complete (univ : set F) := complete_univ, convert measurable_set_of_differentiable_within_at_Ici_of_is_complete f this, simp end @[measurability] lemma measurable_deriv_within_Ici [measurable_space F] [borel_space F] : measurable (λ x, deriv_within f (Ici x) x) := begin refine measurable_of_is_closed (λ s hs, _), have : (λ x, deriv_within f (Ici x) x) ⁻¹' s = {x | differentiable_within_at ℝ f (Ici x) x ∧ deriv_within f (Ici x) x ∈ s} ∪ ({x | ¬differentiable_within_at ℝ f (Ici x) x} ∩ {x | (0 : F) ∈ s}) := set.ext (λ x, mem_preimage.trans deriv_within_mem_iff), rw this, exact (measurable_set_of_differentiable_within_at_Ici_of_is_complete _ hs.is_complete).union ((measurable_set_of_differentiable_within_at_Ici _).compl.inter (measurable_set.const _)) end lemma strongly_measurable_deriv_within_Ici [second_countable_topology F] : strongly_measurable (λ x, deriv_within f (Ici x) x) := by { borelize F, exact (measurable_deriv_within_Ici f).strongly_measurable } lemma ae_measurable_deriv_within_Ici [measurable_space F] [borel_space F] (μ : measure ℝ) : ae_measurable (λ x, deriv_within f (Ici x) x) μ := (measurable_deriv_within_Ici f).ae_measurable lemma ae_strongly_measurable_deriv_within_Ici [second_countable_topology F] (μ : measure ℝ) : ae_strongly_measurable (λ x, deriv_within f (Ici x) x) μ := (strongly_measurable_deriv_within_Ici f).ae_strongly_measurable /-- The set of right differentiability points of a function taking values in a complete space is Borel-measurable. -/ theorem measurable_set_of_differentiable_within_at_Ioi : measurable_set {x | differentiable_within_at ℝ f (Ioi x) x} := by simpa [differentiable_within_at_Ioi_iff_Ici] using measurable_set_of_differentiable_within_at_Ici f @[measurability] lemma measurable_deriv_within_Ioi [measurable_space F] [borel_space F] : measurable (λ x, deriv_within f (Ioi x) x) := by simpa [deriv_within_Ioi_eq_Ici] using measurable_deriv_within_Ici f lemma strongly_measurable_deriv_within_Ioi [second_countable_topology F] : strongly_measurable (λ x, deriv_within f (Ioi x) x) := by { borelize F, exact (measurable_deriv_within_Ioi f).strongly_measurable } lemma ae_measurable_deriv_within_Ioi [measurable_space F] [borel_space F] (μ : measure ℝ) : ae_measurable (λ x, deriv_within f (Ioi x) x) μ := (measurable_deriv_within_Ioi f).ae_measurable lemma ae_strongly_measurable_deriv_within_Ioi [second_countable_topology F] (μ : measure ℝ) : ae_strongly_measurable (λ x, deriv_within f (Ioi x) x) μ := (strongly_measurable_deriv_within_Ioi f).ae_strongly_measurable end right_deriv
003b2f71be04ccd44c9a8748852d8dfdd4d43ec3
4fa161becb8ce7378a709f5992a594764699e268
/src/data/padics/padic_norm.lean
b51e129584ca289d1b220260ea03ad3a05c9bef9
[ "Apache-2.0" ]
permissive
laughinggas/mathlib
e4aa4565ae34e46e834434284cb26bd9d67bc373
86dcd5cda7a5017c8b3c8876c89a510a19d49aad
refs/heads/master
1,669,496,232,688
1,592,831,995,000
1,592,831,995,000
274,155,979
0
0
Apache-2.0
1,592,835,190,000
1,592,835,189,000
null
UTF-8
Lean
false
false
18,611
lean
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis -/ import data.rat import algebra.gcd_domain import algebra.field_power import ring_theory.multiplicity import data.real.cau_seq /-! # p-adic norm This file defines the p-adic valuation and the p-adic norm on ℚ. The p-adic valuation on ℚ is the difference of the multiplicities of `p` in the numerator and denominator of `q`. This function obeys the standard properties of a valuation, with the appropriate assumptions on p. The valuation induces a norm on ℚ. This norm is a nonarchimedean absolute value. It takes values in {0} ∪ {1/p^k | k ∈ ℤ}. ## Notations This file uses the local notation `/.` for `rat.mk`. ## Implementation notes Much, but not all, of this file assumes that `p` is prime. This assumption is inferred automatically by taking `[fact (prime p)]` as a type class argument. ## References * [F. Q. Gouêva, *p-adic numbers*][gouvea1997] * [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019] * <https://en.wikipedia.org/wiki/P-adic_number> ## Tags p-adic, p adic, padic, norm, valuation -/ universe u open nat open_locale rat open multiplicity /-- For `p ≠ 1`, the p-adic valuation of an integer `z ≠ 0` is the largest natural number `n` such that p^n divides z. `padic_val_rat` defines the valuation of a rational `q` to be the valuation of `q.num` minus the valuation of `q.denom`. If `q = 0` or `p = 1`, then `padic_val_rat p q` defaults to 0. -/ def padic_val_rat (p : ℕ) (q : ℚ) : ℤ := if h : q ≠ 0 ∧ p ≠ 1 then (multiplicity (p : ℤ) q.num).get (multiplicity.finite_int_iff.2 ⟨h.2, rat.num_ne_zero_of_ne_zero h.1⟩) - (multiplicity (p : ℤ) q.denom).get (multiplicity.finite_int_iff.2 ⟨h.2, by exact_mod_cast rat.denom_ne_zero _⟩) else 0 /-- A simplification of the definition of `padic_val_rat p q` when `q ≠ 0` and `p` is prime. -/ lemma padic_val_rat_def (p : ℕ) [hp : fact p.prime] {q : ℚ} (hq : q ≠ 0) : padic_val_rat p q = (multiplicity (p : ℤ) q.num).get (finite_int_iff.2 ⟨hp.ne_one, rat.num_ne_zero_of_ne_zero hq⟩) - (multiplicity (p : ℤ) q.denom).get (finite_int_iff.2 ⟨hp.ne_one, by exact_mod_cast rat.denom_ne_zero _⟩) := dif_pos ⟨hq, hp.ne_one⟩ namespace padic_val_rat open multiplicity section padic_val_rat variables {p : ℕ} /-- `padic_val_rat p q` is symmetric in `q`. -/ @[simp] protected lemma neg (q : ℚ) : padic_val_rat p (-q) = padic_val_rat p q := begin unfold padic_val_rat, split_ifs, { simp [-add_comm]; refl }, { exfalso, simp * at * }, { exfalso, simp * at * }, { refl } end /-- `padic_val_rat p 1` is 0 for any `p`. -/ @[simp] protected lemma one : padic_val_rat p 1 = 0 := by unfold padic_val_rat; split_ifs; simp * /-- For `p ≠ 0, p ≠ 1, `padic_val_rat p p` is 1. -/ @[simp] lemma padic_val_rat_self (hp : 1 < p) : padic_val_rat p p = 1 := by unfold padic_val_rat; split_ifs; simp [*, nat.one_lt_iff_ne_zero_and_ne_one] at * /-- The p-adic value of an integer `z ≠ 0` is the multiplicity of `p` in `z`. -/ lemma padic_val_rat_of_int (z : ℤ) (hp : p ≠ 1) (hz : z ≠ 0) : padic_val_rat p (z : ℚ) = (multiplicity (p : ℤ) z).get (finite_int_iff.2 ⟨hp, hz⟩) := by rw [padic_val_rat, dif_pos]; simp *; refl end padic_val_rat section padic_val_nat /-- A convenience function for the case of `padic_val_rat` when both inputs are natural numbers. -/ def padic_val_nat (p : ℕ) (n : ℕ) : ℕ := int.to_nat (padic_val_rat p n) /-- `padic_val_nat` is defined as an `int.to_nat` cast; this lemma ensures that the cast is well-behaved. -/ lemma zero_le_padic_val_rat_of_nat (p n : ℕ) : 0 ≤ padic_val_rat p n := begin unfold padic_val_rat, split_ifs, { simp, }, { trivial, }, end /-- `padic_val_rat` coincides with `padic_val_nat`. -/ @[simp, norm_cast] lemma padic_val_rat_of_nat (p n : ℕ) : ↑(padic_val_nat p n) = padic_val_rat p n := begin unfold padic_val_nat, rw int.to_nat_of_nonneg (zero_le_padic_val_rat_of_nat p n), end /-- A simplification of `padic_val_nat` when one input is prime, by analogy with `padic_val_rat_def`. -/ lemma padic_val_nat_def {p : ℕ} [hp : fact p.prime] {n : ℕ} (hn : n ≠ 0) : padic_val_nat p n = (multiplicity p n).get (multiplicity.finite_nat_iff.2 ⟨nat.prime.ne_one hp, bot_lt_iff_ne_bot.mpr hn⟩) := begin have n_nonzero : (n : ℚ) ≠ 0, by simpa only [cast_eq_zero, ne.def], -- Infinite loop with @simp padic_val_rat_of_nat unless we restrict the available lemmas here, -- hence the very long list simpa only [ int.coe_nat_multiplicity p n, rat.coe_nat_denom n, (padic_val_rat_of_nat p n).symm, int.coe_nat_zero, int.coe_nat_inj', sub_zero, get_one_right, int.coe_nat_succ, zero_add, rat.coe_nat_num ] using padic_val_rat_def p n_nonzero, end end padic_val_nat section padic_val_rat open multiplicity variables (p : ℕ) [p_prime : fact p.prime] include p_prime /-- The multiplicity of `p : ℕ` in `a : ℤ` is finite exactly when `a ≠ 0`. -/ lemma finite_int_prime_iff {p : ℕ} [p_prime : fact p.prime] {a : ℤ} : finite (p : ℤ) a ↔ a ≠ 0 := by simp [finite_int_iff, ne.symm (ne_of_lt (p_prime.one_lt))] /-- A rewrite lemma for `padic_val_rat p q` when `q` is expressed in terms of `rat.mk`. -/ protected lemma defn {q : ℚ} {n d : ℤ} (hqz : q ≠ 0) (qdf : q = n /. d) : padic_val_rat p q = (multiplicity (p : ℤ) n).get (finite_int_iff.2 ⟨ne.symm $ ne_of_lt p_prime.one_lt, λ hn, by simp * at *⟩) - (multiplicity (p : ℤ) d).get (finite_int_iff.2 ⟨ne.symm $ ne_of_lt p_prime.one_lt, λ hd, by simp * at *⟩) := have hn : n ≠ 0, from rat.mk_num_ne_zero_of_ne_zero hqz qdf, have hd : d ≠ 0, from rat.mk_denom_ne_zero_of_ne_zero hqz qdf, let ⟨c, hc1, hc2⟩ := rat.num_denom_mk hn hd qdf in by rw [padic_val_rat, dif_pos]; simp [hc1, hc2, multiplicity.mul' (nat.prime_iff_prime_int.1 p_prime), (ne.symm (ne_of_lt p_prime.one_lt)), hqz] /-- A rewrite lemma for `padic_val_rat p (q * r)` with conditions `q ≠ 0`, `r ≠ 0`. -/ protected lemma mul {q r : ℚ} (hq : q ≠ 0) (hr : r ≠ 0) : padic_val_rat p (q * r) = padic_val_rat p q + padic_val_rat p r := have q*r = (q.num * r.num) /. (↑q.denom * ↑r.denom), by rw_mod_cast rat.mul_num_denom, have hq' : q.num /. q.denom ≠ 0, by rw rat.num_denom; exact hq, have hr' : r.num /. r.denom ≠ 0, by rw rat.num_denom; exact hr, have hp' : _root_.prime (p : ℤ), from nat.prime_iff_prime_int.1 p_prime, begin rw [padic_val_rat.defn p (mul_ne_zero hq hr) this], conv_rhs { rw [←(@rat.num_denom q), padic_val_rat.defn p hq', ←(@rat.num_denom r), padic_val_rat.defn p hr'] }, rw [multiplicity.mul' hp', multiplicity.mul' hp']; simp [add_comm, add_left_comm, sub_eq_add_neg] end /-- A rewrite lemma for `padic_val_rat p (q^k) with condition `q ≠ 0`. -/ protected lemma pow {q : ℚ} (hq : q ≠ 0) {k : ℕ} : padic_val_rat p (q ^ k) = k * padic_val_rat p q := by induction k; simp [*, padic_val_rat.mul _ hq (pow_ne_zero _ hq), pow_succ, add_mul, add_comm] /-- A rewrite lemma for `padic_val_rat p (q⁻¹)` with condition `q ≠ 0`. -/ protected lemma inv {q : ℚ} (hq : q ≠ 0) : padic_val_rat p (q⁻¹) = -padic_val_rat p q := by rw [eq_neg_iff_add_eq_zero, ← padic_val_rat.mul p (inv_ne_zero hq) hq, inv_mul_cancel hq, padic_val_rat.one] /-- A rewrite lemma for `padic_val_rat p (q / r)` with conditions `q ≠ 0`, `r ≠ 0`. -/ protected lemma div {q r : ℚ} (hq : q ≠ 0) (hr : r ≠ 0) : padic_val_rat p (q / r) = padic_val_rat p q - padic_val_rat p r := by rw [div_eq_mul_inv, padic_val_rat.mul p hq (inv_ne_zero hr), padic_val_rat.inv p hr, sub_eq_add_neg] /-- A condition for `padic_val_rat p (n₁ / d₁) ≤ padic_val_rat p (n₂ / d₂), in terms of divisibility by `p^n`. -/ lemma padic_val_rat_le_padic_val_rat_iff {n₁ n₂ d₁ d₂ : ℤ} (hn₁ : n₁ ≠ 0) (hn₂ : n₂ ≠ 0) (hd₁ : d₁ ≠ 0) (hd₂ : d₂ ≠ 0) : padic_val_rat p (n₁ /. d₁) ≤ padic_val_rat p (n₂ /. d₂) ↔ ∀ (n : ℕ), ↑p ^ n ∣ n₁ * d₂ → ↑p ^ n ∣ n₂ * d₁ := have hf1 : finite (p : ℤ) (n₁ * d₂), from finite_int_prime_iff.2 (mul_ne_zero hn₁ hd₂), have hf2 : finite (p : ℤ) (n₂ * d₁), from finite_int_prime_iff.2 (mul_ne_zero hn₂ hd₁), by conv { to_lhs, rw [padic_val_rat.defn p (rat.mk_ne_zero_of_ne_zero hn₁ hd₁) rfl, padic_val_rat.defn p (rat.mk_ne_zero_of_ne_zero hn₂ hd₂) rfl, sub_le_iff_le_add', ← add_sub_assoc, le_sub_iff_add_le], norm_cast, rw [← multiplicity.mul' (nat.prime_iff_prime_int.1 p_prime) hf1, add_comm, ← multiplicity.mul' (nat.prime_iff_prime_int.1 p_prime) hf2, enat.get_le_get, multiplicity_le_multiplicity_iff] } /-- Sufficient conditions to show that the p-adic valuation of `q` is less than or equal to the p-adic vlauation of `q + r`. -/ theorem le_padic_val_rat_add_of_le {q r : ℚ} (hq : q ≠ 0) (hr : r ≠ 0) (hqr : q + r ≠ 0) (h : padic_val_rat p q ≤ padic_val_rat p r) : padic_val_rat p q ≤ padic_val_rat p (q + r) := have hqn : q.num ≠ 0, from rat.num_ne_zero_of_ne_zero hq, have hqd : (q.denom : ℤ) ≠ 0, by exact_mod_cast rat.denom_ne_zero _, have hrn : r.num ≠ 0, from rat.num_ne_zero_of_ne_zero hr, have hrd : (r.denom : ℤ) ≠ 0, by exact_mod_cast rat.denom_ne_zero _, have hqdv : q.num /. q.denom ≠ 0, from rat.mk_ne_zero_of_ne_zero hqn hqd, have hrdv : r.num /. r.denom ≠ 0, from rat.mk_ne_zero_of_ne_zero hrn hrd, have hqreq : q + r = (((q.num * r.denom + q.denom * r.num : ℤ)) /. (↑q.denom * ↑r.denom : ℤ)), from rat.add_num_denom _ _, have hqrd : q.num * ↑(r.denom) + ↑(q.denom) * r.num ≠ 0, from rat.mk_num_ne_zero_of_ne_zero hqr hqreq, begin conv_lhs { rw ←(@rat.num_denom q) }, rw [hqreq, padic_val_rat_le_padic_val_rat_iff p hqn hqrd hqd (mul_ne_zero hqd hrd), ← multiplicity_le_multiplicity_iff, mul_left_comm, multiplicity.mul (nat.prime_iff_prime_int.1 p_prime), add_mul], rw [←(@rat.num_denom q), ←(@rat.num_denom r), padic_val_rat_le_padic_val_rat_iff p hqn hrn hqd hrd, ← multiplicity_le_multiplicity_iff] at h, calc _ ≤ min (multiplicity ↑p (q.num * ↑(r.denom) * ↑(q.denom))) (multiplicity ↑p (↑(q.denom) * r.num * ↑(q.denom))) : (le_min (by rw [@multiplicity.mul _ _ _ _ (_ * _) _ (nat.prime_iff_prime_int.1 p_prime), add_comm]) (by rw [mul_assoc, @multiplicity.mul _ _ _ _ (q.denom : ℤ) (_ * _) (nat.prime_iff_prime_int.1 p_prime)]; exact add_le_add_left' h)) ... ≤ _ : min_le_multiplicity_add end /-- The minimum of the valuations of `q` and `r` is less than or equal to the valuation of `q + r`. -/ theorem min_le_padic_val_rat_add {q r : ℚ} (hq : q ≠ 0) (hr : r ≠ 0) (hqr : q + r ≠ 0) : min (padic_val_rat p q) (padic_val_rat p r) ≤ padic_val_rat p (q + r) := (le_total (padic_val_rat p q) (padic_val_rat p r)).elim (λ h, by rw [min_eq_left h]; exact le_padic_val_rat_add_of_le _ hq hr hqr h) (λ h, by rw [min_eq_right h, add_comm]; exact le_padic_val_rat_add_of_le _ hr hq (by rwa add_comm) h) end padic_val_rat end padic_val_rat /-- If `q ≠ 0`, the p-adic norm of a rational `q` is `p ^ (-(padic_val_rat p q))`. If `q = 0`, the p-adic norm of `q` is 0. -/ def padic_norm (p : ℕ) (q : ℚ) : ℚ := if q = 0 then 0 else (↑p : ℚ) ^ (-(padic_val_rat p q)) namespace padic_norm section padic_norm open padic_val_rat variables (p : ℕ) /-- Unfolds the definition of the p-adic norm of `q` when `q ≠ 0`. -/ @[simp] protected lemma eq_fpow_of_nonzero {q : ℚ} (hq : q ≠ 0) : padic_norm p q = p ^ (-(padic_val_rat p q)) := by simp [hq, padic_norm] /-- The p-adic norm is nonnegative. -/ protected lemma nonneg (q : ℚ) : 0 ≤ padic_norm p q := if hq : q = 0 then by simp [hq, padic_norm] else begin unfold padic_norm; split_ifs, apply fpow_nonneg_of_nonneg, exact_mod_cast nat.zero_le _ end /-- The p-adic norm of 0 is 0. -/ @[simp] protected lemma zero : padic_norm p 0 = 0 := by simp [padic_norm] /-- The p-adic norm of 1 is 1. -/ @[simp] protected lemma one : padic_norm p 1 = 1 := by simp [padic_norm] /-- The image of `padic_norm p` is {0} ∪ {p^(-n) | n ∈ ℤ}. -/ protected theorem image {q : ℚ} (hq : q ≠ 0) : ∃ n : ℤ, padic_norm p q = p ^ (-n) := ⟨ (padic_val_rat p q), by simp [padic_norm, hq] ⟩ variable [hp : fact p.prime] include hp /-- If `q ≠ 0`, then `padic_norm p q ≠ 0`. -/ protected lemma nonzero {q : ℚ} (hq : q ≠ 0) : padic_norm p q ≠ 0 := begin rw padic_norm.eq_fpow_of_nonzero p hq, apply fpow_ne_zero_of_ne_zero, exact_mod_cast ne_of_gt hp.pos end /-- `padic_norm p` is symmetric. -/ @[simp] protected lemma neg (q : ℚ) : padic_norm p (-q) = padic_norm p q := if hq : q = 0 then by simp [hq] else by simp [padic_norm, hq, hp.one_lt] /-- If the p-adic norm of `q` is 0, then `q` is 0. -/ lemma zero_of_padic_norm_eq_zero {q : ℚ} (h : padic_norm p q = 0) : q = 0 := begin apply by_contradiction, intro hq, unfold padic_norm at h, rw if_neg hq at h, apply absurd h, apply fpow_ne_zero_of_ne_zero, exact_mod_cast hp.ne_zero end /-- The p-adic norm is multiplicative. -/ @[simp] protected theorem mul (q r : ℚ) : padic_norm p (q*r) = padic_norm p q * padic_norm p r := if hq : q = 0 then by simp [hq] else if hr : r = 0 then by simp [hr] else have q*r ≠ 0, from mul_ne_zero hq hr, have (↑p : ℚ) ≠ 0, by simp [hp.ne_zero], by simp [padic_norm, *, padic_val_rat.mul, fpow_add this, mul_comm] /-- The p-adic norm respects division. -/ @[simp] protected theorem div (q r : ℚ) : padic_norm p (q / r) = padic_norm p q / padic_norm p r := if hr : r = 0 then by simp [hr] else eq_div_of_mul_eq _ _ (padic_norm.nonzero _ hr) (by rw [←padic_norm.mul, div_mul_cancel _ hr]) /-- The p-adic norm of an integer is at most 1. -/ protected theorem of_int (z : ℤ) : padic_norm p ↑z ≤ 1 := if hz : z = 0 then by simp [hz, zero_le_one] else begin unfold padic_norm, rw [if_neg _], { refine fpow_le_one_of_nonpos _ _, { exact_mod_cast le_of_lt hp.one_lt, }, { rw [padic_val_rat_of_int _ hp.ne_one hz, neg_nonpos], norm_cast, simp }}, exact_mod_cast hz end private lemma nonarchimedean_aux {q r : ℚ} (h : padic_val_rat p q ≤ padic_val_rat p r) : padic_norm p (q + r) ≤ max (padic_norm p q) (padic_norm p r) := have hnqp : padic_norm p q ≥ 0, from padic_norm.nonneg _ _, have hnrp : padic_norm p r ≥ 0, from padic_norm.nonneg _ _, if hq : q = 0 then by simp [hq, max_eq_right hnrp, le_max_right] else if hr : r = 0 then by simp [hr, max_eq_left hnqp, le_max_left] else if hqr : q + r = 0 then le_trans (by simpa [hqr] using hnqp) (le_max_left _ _) else begin unfold padic_norm, split_ifs, apply le_max_iff.2, left, apply fpow_le_of_le, { exact_mod_cast le_of_lt hp.one_lt }, { apply neg_le_neg, have : padic_val_rat p q = min (padic_val_rat p q) (padic_val_rat p r), from (min_eq_left h).symm, rw this, apply min_le_padic_val_rat_add; assumption } end /-- The p-adic norm is nonarchimedean: the norm of `p + q` is at most the max of the norm of `p` and the norm of `q`. -/ protected theorem nonarchimedean {q r : ℚ} : padic_norm p (q + r) ≤ max (padic_norm p q) (padic_norm p r) := begin wlog hle := le_total (padic_val_rat p q) (padic_val_rat p r) using [q r], exact nonarchimedean_aux p hle end /-- The p-adic norm respects the triangle inequality: the norm of `p + q` is at most the norm of `p` plus the norm of `q`. -/ theorem triangle_ineq (q r : ℚ) : padic_norm p (q + r) ≤ padic_norm p q + padic_norm p r := calc padic_norm p (q + r) ≤ max (padic_norm p q) (padic_norm p r) : padic_norm.nonarchimedean p ... ≤ padic_norm p q + padic_norm p r : max_le_add_of_nonneg (padic_norm.nonneg p _) (padic_norm.nonneg p _) /-- The p-adic norm of a difference is at most the max of each component. Restates the archimedean property of the p-adic norm. -/ protected theorem sub {q r : ℚ} : padic_norm p (q - r) ≤ max (padic_norm p q) (padic_norm p r) := by rw [sub_eq_add_neg, ←padic_norm.neg p r]; apply padic_norm.nonarchimedean /-- If the p-adic norms of `q` and `r` are different, then the norm of `q + r` is equal to the max of the norms of `q` and `r`. -/ lemma add_eq_max_of_ne {q r : ℚ} (hne : padic_norm p q ≠ padic_norm p r) : padic_norm p (q + r) = max (padic_norm p q) (padic_norm p r) := begin wlog hle := le_total (padic_norm p r) (padic_norm p q) using [q r], have hlt : padic_norm p r < padic_norm p q, from lt_of_le_of_ne hle hne.symm, have : padic_norm p q ≤ max (padic_norm p (q + r)) (padic_norm p r), from calc padic_norm p q = padic_norm p (q + r - r) : by congr; ring ... ≤ max (padic_norm p (q + r)) (padic_norm p (-r)) : padic_norm.nonarchimedean p ... = max (padic_norm p (q + r)) (padic_norm p r) : by simp, have hnge : padic_norm p r ≤ padic_norm p (q + r), { apply le_of_not_gt, intro hgt, rw max_eq_right_of_lt hgt at this, apply not_lt_of_ge this, assumption }, have : padic_norm p q ≤ padic_norm p (q + r), by rwa [max_eq_left hnge] at this, apply _root_.le_antisymm, { apply padic_norm.nonarchimedean p }, { rw max_eq_left_of_lt hlt, assumption } end /-- The p-adic norm is an absolute value: positive-definite and multiplicative, satisfying the triangle inequality. -/ instance : is_absolute_value (padic_norm p) := { abv_nonneg := padic_norm.nonneg p, abv_eq_zero := begin intros, constructor; intro, { apply zero_of_padic_norm_eq_zero p, assumption }, { simp [*] } end, abv_add := padic_norm.triangle_ineq p, abv_mul := padic_norm.mul p } /-- If `p^n` divides an integer `z`, then the p-adic norm of `z` is at most `p^(-n)`. -/ lemma le_of_dvd {n : ℕ} {z : ℤ} (hd : ↑(p^n) ∣ z) : padic_norm p z ≤ ↑p ^ (-n : ℤ) := begin unfold padic_norm, split_ifs with hz hz, { apply fpow_nonneg_of_nonneg, exact_mod_cast le_of_lt hp.pos }, { apply fpow_le_of_le, exact_mod_cast le_of_lt hp.one_lt, apply neg_le_neg, rw padic_val_rat_of_int _ hp.ne_one _, { norm_cast, rw [← enat.coe_le_coe, enat.coe_get], apply multiplicity.le_multiplicity_of_pow_dvd, exact_mod_cast hd }, { exact_mod_cast hz }}, end end padic_norm end padic_norm
b8457237f8e42f16e565b8f79ef8812c5c5c8592
9dc8cecdf3c4634764a18254e94d43da07142918
/src/topology/tietze_extension.lean
8a5e96357448b03f82157fda00c4c16f233ddf88
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
22,782
lean
/- Copyright (c) 2021 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import data.set.intervals.monotone import topology.algebra.order.monotone_continuity import topology.urysohns_bounded /-! # Tietze extension theorem In this file we prove a few version of the Tietze extension theorem. The theorem says that a continuous function `s → ℝ` defined on a closed set in a normal topological space `Y` can be extended to a continuous function on the whole space. Moreover, if all values of the original function belong to some (finite or infinite, open or closed) interval, then the extension can be chosen so that it takes values in the same interval. In particular, if the original function is a bounded function, then there exists a bounded extension of the same norm. The proof mostly follows <https://ncatlab.org/nlab/show/Tietze+extension+theorem>. We patch a small gap in the proof for unbounded functions, see `exists_extension_forall_exists_le_ge_of_closed_embedding`. ## Implementation notes We first prove the theorems for a closed embedding `e : X → Y` of a topological space into a normal topological space, then specialize them to the case `X = s : set Y`, `e = coe`. ## Tags Tietze extension theorem, Urysohn's lemma, normal topological space -/ variables {X Y : Type*} [topological_space X] [topological_space Y] [normal_space Y] open metric set filter open_locale bounded_continuous_function topological_space noncomputable theory namespace bounded_continuous_function /-- One step in the proof of the Tietze extension theorem. If `e : C(X, Y)` is a closed embedding of a topological space into a normal topological space and `f : X →ᵇ ℝ` is a bounded continuous function, then there exists a bounded continuous function `g : Y →ᵇ ℝ` of the norm `∥g∥ ≤ ∥f∥ / 3` such that the distance between `g ∘ e` and `f` is at most `(2 / 3) * ∥f∥`. -/ lemma tietze_extension_step (f : X →ᵇ ℝ) (e : C(X, Y)) (he : closed_embedding e) : ∃ g : Y →ᵇ ℝ, ∥g∥ ≤ ∥f∥ / 3 ∧ dist (g.comp_continuous e) f ≤ (2 / 3) * ∥f∥ := begin have h3 : (0 : ℝ) < 3 := by norm_num1, have h23 : 0 < (2 / 3 : ℝ) := by norm_num1, -- In the trivial case `f = 0`, we take `g = 0` rcases eq_or_ne f 0 with (rfl|hf), { use 0, simp }, replace hf : 0 < ∥f∥ := norm_pos_iff.2 hf, /- Otherwise, the closed sets `e '' (f ⁻¹' (Iic (-∥f∥ / 3)))` and `e '' (f ⁻¹' (Ici (∥f∥ / 3)))` are disjoint, hence by Urysohn's lemma there exists a function `g` that is equal to `-∥f∥ / 3` on the former set and is equal to `∥f∥ / 3` on the latter set. This function `g` satisfies the assertions of the lemma. -/ have hf3 : -∥f∥ / 3 < ∥f∥ / 3, from (div_lt_div_right h3).2 (left.neg_lt_self hf), have hc₁ : is_closed (e '' (f ⁻¹' (Iic (-∥f∥ / 3)))), from he.is_closed_map _ (is_closed_Iic.preimage f.continuous), have hc₂ : is_closed (e '' (f ⁻¹' (Ici (∥f∥ / 3)))), from he.is_closed_map _ (is_closed_Ici.preimage f.continuous), have hd : disjoint (e '' (f ⁻¹' (Iic (-∥f∥ / 3)))) (e '' (f ⁻¹' (Ici (∥f∥ / 3)))), { refine disjoint_image_of_injective he.inj (disjoint.preimage _ _), rwa [Iic_disjoint_Ici, not_le] }, rcases exists_bounded_mem_Icc_of_closed_of_le hc₁ hc₂ hd hf3.le with ⟨g, hg₁, hg₂, hgf⟩, refine ⟨g, _, _⟩, { refine (norm_le $ div_nonneg hf.le h3.le).mpr (λ y, _), simpa [abs_le, neg_div] using hgf y }, { refine (dist_le $ mul_nonneg h23.le hf.le).mpr (λ x, _), have hfx : -∥f∥ ≤ f x ∧ f x ≤ ∥f∥, by simpa only [real.norm_eq_abs, abs_le] using f.norm_coe_le_norm x, cases le_total (f x) (-∥f∥ / 3) with hle₁ hle₁, { calc |g (e x) - f x| = -∥f∥ / 3 - f x: by rw [hg₁ (mem_image_of_mem _ hle₁), abs_of_nonneg (sub_nonneg.2 hle₁)] ... ≤ (2 / 3) * ∥f∥ : by linarith }, { cases le_total (f x) (∥f∥ / 3) with hle₂ hle₂, { simp only [neg_div] at *, calc dist (g (e x)) (f x) ≤ |g (e x)| + |f x| : dist_le_norm_add_norm _ _ ... ≤ ∥f∥ / 3 + ∥f∥ / 3 : add_le_add (abs_le.2 $ hgf _) (abs_le.2 ⟨hle₁, hle₂⟩) ... = (2 / 3) * ∥f∥ : by linarith }, { calc |g (e x) - f x| = f x - ∥f∥ / 3 : by rw [hg₂ (mem_image_of_mem _ hle₂), abs_sub_comm, abs_of_nonneg (sub_nonneg.2 hle₂)] ... ≤ (2 / 3) * ∥f∥ : by linarith } } } end /-- **Tietze extension theorem** for real-valued bounded continuous maps, a version with a closed embedding and bundled composition. If `e : C(X, Y)` is a closed embedding of a topological space into a normal topological space and `f : X →ᵇ ℝ` is a bounded continuous function, then there exists a bounded continuous function `g : Y →ᵇ ℝ` of the same norm such that `g ∘ e = f`. -/ lemma exists_extension_norm_eq_of_closed_embedding' (f : X →ᵇ ℝ) (e : C(X, Y)) (he : closed_embedding e) : ∃ g : Y →ᵇ ℝ, ∥g∥ = ∥f∥ ∧ g.comp_continuous e = f := begin /- For the proof, we iterate `tietze_extension_step`. Each time we apply it to the difference between the previous approximation and `f`. -/ choose F hF_norm hF_dist using λ f : X →ᵇ ℝ, tietze_extension_step f e he, set g : ℕ → Y →ᵇ ℝ := λ n, (λ g, g + F (f - g.comp_continuous e))^[n] 0, have g0 : g 0 = 0 := rfl, have g_succ : ∀ n, g (n + 1) = g n + F (f - (g n).comp_continuous e), from λ n, function.iterate_succ_apply' _ _ _, have hgf : ∀ n, dist ((g n).comp_continuous e) f ≤ (2 / 3) ^ n * ∥f∥, { intro n, induction n with n ihn, { simp [g0] }, { rw [g_succ n, add_comp_continuous, ← dist_sub_right, add_sub_cancel', pow_succ, mul_assoc], refine (hF_dist _).trans (mul_le_mul_of_nonneg_left _ (by norm_num1)), rwa ← dist_eq_norm' } }, have hg_dist : ∀ n, dist (g n) (g (n + 1)) ≤ 1 / 3 * ∥f∥ * (2 / 3) ^ n, { intro n, calc dist (g n) (g (n + 1)) = ∥F (f - (g n).comp_continuous e)∥ : by rw [g_succ, dist_eq_norm', add_sub_cancel'] ... ≤ ∥f - (g n).comp_continuous e∥ / 3 : hF_norm _ ... = (1 / 3) * dist ((g n).comp_continuous e) f : by rw [dist_eq_norm', one_div, div_eq_inv_mul] ... ≤ (1 / 3) * ((2 / 3) ^ n * ∥f∥) : mul_le_mul_of_nonneg_left (hgf n) (by norm_num1) ... = 1 / 3 * ∥f∥ * (2 / 3) ^ n : by ac_refl }, have hg_cau : cauchy_seq g, from cauchy_seq_of_le_geometric _ _ (by norm_num1) hg_dist, have : tendsto (λ n, (g n).comp_continuous e) at_top (𝓝 $ (lim at_top g).comp_continuous e), from ((continuous_comp_continuous e).tendsto _).comp hg_cau.tendsto_lim, have hge : (lim at_top g).comp_continuous e = f, { refine tendsto_nhds_unique this (tendsto_iff_dist_tendsto_zero.2 _), refine squeeze_zero (λ _, dist_nonneg) hgf _, rw ← zero_mul (∥f∥), refine (tendsto_pow_at_top_nhds_0_of_lt_1 _ _).mul tendsto_const_nhds; norm_num1 }, refine ⟨lim at_top g, le_antisymm _ _, hge⟩, { rw [← dist_zero_left, ← g0], refine (dist_le_of_le_geometric_of_tendsto₀ _ _ (by norm_num1) hg_dist hg_cau.tendsto_lim).trans_eq _, field_simp [show (3 - 2 : ℝ) = 1, by norm_num1] }, { rw ← hge, exact norm_comp_continuous_le _ _ } end /-- **Tietze extension theorem** for real-valued bounded continuous maps, a version with a closed embedding and unbundled composition. If `e : C(X, Y)` is a closed embedding of a topological space into a normal topological space and `f : X →ᵇ ℝ` is a bounded continuous function, then there exists a bounded continuous function `g : Y →ᵇ ℝ` of the same norm such that `g ∘ e = f`. -/ lemma exists_extension_norm_eq_of_closed_embedding (f : X →ᵇ ℝ) {e : X → Y} (he : closed_embedding e) : ∃ g : Y →ᵇ ℝ, ∥g∥ = ∥f∥ ∧ g ∘ e = f := begin rcases exists_extension_norm_eq_of_closed_embedding' f ⟨e, he.continuous⟩ he with ⟨g, hg, rfl⟩, exact ⟨g, hg, rfl⟩ end /-- **Tietze extension theorem** for real-valued bounded continuous maps, a version for a closed set. If `f` is a bounded continuous real-valued function defined on a closed set in a normal topological space, then it can be extended to a bounded continuous function of the same norm defined on the whole space. -/ lemma exists_norm_eq_restrict_eq_of_closed {s : set Y} (f : s →ᵇ ℝ) (hs : is_closed s) : ∃ g : Y →ᵇ ℝ, ∥g∥ = ∥f∥ ∧ g.restrict s = f := exists_extension_norm_eq_of_closed_embedding' f ((continuous_map.id _).restrict s) (closed_embedding_subtype_coe hs) /-- **Tietze extension theorem** for real-valued bounded continuous maps, a version for a closed embedding and a bounded continuous function that takes values in a non-trivial closed interval. See also `exists_extension_forall_mem_of_closed_embedding` for a more general statement that works for any interval (finite or infinite, open or closed). If `e : X → Y` is a closed embedding and `f : X →ᵇ ℝ` is a bounded continuous function such that `f x ∈ [a, b]` for all `x`, where `a ≤ b`, then there exists a bounded continuous function `g : Y →ᵇ ℝ` such that `g y ∈ [a, b]` for all `y` and `g ∘ e = f`. -/ lemma exists_extension_forall_mem_Icc_of_closed_embedding (f : X →ᵇ ℝ) {a b : ℝ} {e : X → Y} (hf : ∀ x, f x ∈ Icc a b) (hle : a ≤ b) (he : closed_embedding e) : ∃ g : Y →ᵇ ℝ, (∀ y, g y ∈ Icc a b) ∧ g ∘ e = f := begin rcases exists_extension_norm_eq_of_closed_embedding (f - const X ((a + b) / 2)) he with ⟨g, hgf, hge⟩, refine ⟨const Y ((a + b) / 2) + g, λ y, _, _⟩, { suffices : ∥f - const X ((a + b) / 2)∥ ≤ (b - a) / 2, by simpa [real.Icc_eq_closed_ball, add_mem_closed_ball_iff_norm] using (norm_coe_le_norm g y).trans (hgf.trans_le this), refine (norm_le $ div_nonneg (sub_nonneg.2 hle) zero_le_two).2 (λ x, _), simpa only [real.Icc_eq_closed_ball] using hf x }, { ext x, have : g (e x) = f x - (a + b) / 2 := congr_fun hge x, simp [this] } end /-- **Tietze extension theorem** for real-valued bounded continuous maps, a version for a closed embedding. Let `e` be a closed embedding of a nonempty topological space `X` into a normal topological space `Y`. Let `f` be a bounded continuous real-valued function on `X`. Then there exists a bounded continuous function `g : Y →ᵇ ℝ` such that `g ∘ e = f` and each value `g y` belongs to a closed interval `[f x₁, f x₂]` for some `x₁` and `x₂`. -/ lemma exists_extension_forall_exists_le_ge_of_closed_embedding [nonempty X] (f : X →ᵇ ℝ) {e : X → Y} (he : closed_embedding e) : ∃ g : Y →ᵇ ℝ, (∀ y, ∃ x₁ x₂, g y ∈ Icc (f x₁) (f x₂)) ∧ g ∘ e = f := begin inhabit X, -- Put `a = ⨅ x, f x` and `b = ⨆ x, f x` obtain ⟨a, ha⟩ : ∃ a, is_glb (range f) a, from ⟨_, is_glb_cinfi (real.bounded_iff_bdd_below_bdd_above.1 f.bounded_range).1⟩, obtain ⟨b, hb⟩ : ∃ b, is_lub (range f) b, from ⟨_, is_lub_csupr (real.bounded_iff_bdd_below_bdd_above.1 f.bounded_range).2⟩, -- Then `f x ∈ [a, b]` for all `x` have hmem : ∀ x, f x ∈ Icc a b, from λ x, ⟨ha.1 ⟨x, rfl⟩, hb.1 ⟨x, rfl⟩⟩, -- Rule out the trivial case `a = b` have hle : a ≤ b := (hmem default).1.trans (hmem default).2, rcases hle.eq_or_lt with (rfl|hlt), { have : ∀ x, f x = a, by simpa using hmem, use const Y a, simp [this, function.funext_iff] }, -- Put `c = (a + b) / 2`. Then `a < c < b` and `c - a = b - c`. set c := (a + b) / 2, have hac : a < c := left_lt_add_div_two.2 hlt, have hcb : c < b := add_div_two_lt_right.2 hlt, have hsub : c - a = b - c, by { simp only [c], field_simp, ring }, /- Due to `exists_extension_forall_mem_Icc_of_closed_embedding`, there exists an extension `g` such that `g y ∈ [a, b]` for all `y`. However, if `a` and/or `b` do not belong to the range of `f`, then we need to ensure that these points do not belong to the range of `g`. This is done in two almost identical steps. First we deal with the case `∀ x, f x ≠ a`. -/ obtain ⟨g, hg_mem, hgf⟩ : ∃ g : Y →ᵇ ℝ, (∀ y, ∃ x, g y ∈ Icc (f x) b) ∧ g ∘ e = f, { rcases exists_extension_forall_mem_Icc_of_closed_embedding f hmem hle he with ⟨g, hg_mem, hgf⟩, -- If `a ∈ range f`, then we are done. rcases em (∃ x, f x = a) with ⟨x, rfl⟩|ha', { exact ⟨g, λ y, ⟨x, hg_mem _⟩, hgf⟩ }, /- Otherwise, `g ⁻¹' {a}` is disjoint with `range e ∪ g ⁻¹' (Ici c)`, hence there exists a function `dg : Y → ℝ` such that `dg ∘ e = 0`, `dg y = 0` whenever `c ≤ g y`, `dg y = c - a` whenever `g y = a`, and `0 ≤ dg y ≤ c - a` for all `y`. -/ have hd : disjoint (range e ∪ g ⁻¹' (Ici c)) (g ⁻¹' {a}), { refine disjoint_union_left.2 ⟨_, disjoint.preimage _ _⟩, { rintro _ ⟨⟨x, rfl⟩, rfl : g (e x) = a⟩, exact ha' ⟨x, (congr_fun hgf x).symm⟩ }, { exact set.disjoint_singleton_right.2 hac.not_le } }, rcases exists_bounded_mem_Icc_of_closed_of_le (he.closed_range.union $ is_closed_Ici.preimage g.continuous) (is_closed_singleton.preimage g.continuous) hd (sub_nonneg.2 hac.le) with ⟨dg, dg0, dga, dgmem⟩, replace hgf : ∀ x, (g + dg) (e x) = f x, { intro x, simp [dg0 (or.inl $ mem_range_self _), ← hgf] }, refine ⟨g + dg, λ y, _, funext hgf⟩, { have hay : a < (g + dg) y, { rcases (hg_mem y).1.eq_or_lt with rfl|hlt, { refine (lt_add_iff_pos_right _).2 _, calc 0 < c - g y : sub_pos.2 hac ... = dg y : (dga rfl).symm }, { exact hlt.trans_le ((le_add_iff_nonneg_right _).2 $ (dgmem y).1) } }, rcases ha.exists_between hay with ⟨_, ⟨x, rfl⟩, hax, hxy⟩, refine ⟨x, hxy.le, _⟩, cases le_total c (g y) with hc hc, { simp [dg0 (or.inr hc), (hg_mem y).2] }, { calc g y + dg y ≤ c + (c - a) : add_le_add hc (dgmem _).2 ... = b : by rw [hsub, add_sub_cancel'_right] } } }, /- Now we deal with the case `∀ x, f x ≠ b`. The proof is the same as in the first case, with minor modifications that make it hard to deduplicate code. -/ choose xl hxl hgb using hg_mem, rcases em (∃ x, f x = b) with ⟨x, rfl⟩|hb', { exact ⟨g, λ y, ⟨xl y, x, hxl y, hgb y⟩, hgf⟩ }, have hd : disjoint (range e ∪ g ⁻¹' (Iic c)) (g ⁻¹' {b}), { refine disjoint_union_left.2 ⟨_, disjoint.preimage _ _⟩, { rintro _ ⟨⟨x, rfl⟩, rfl : g (e x) = b⟩, exact hb' ⟨x, (congr_fun hgf x).symm⟩ }, { exact set.disjoint_singleton_right.2 hcb.not_le } }, rcases exists_bounded_mem_Icc_of_closed_of_le (he.closed_range.union $ is_closed_Iic.preimage g.continuous) (is_closed_singleton.preimage g.continuous) hd (sub_nonneg.2 hcb.le) with ⟨dg, dg0, dgb, dgmem⟩, replace hgf : ∀ x, (g - dg) (e x) = f x, { intro x, simp [dg0 (or.inl $ mem_range_self _), ← hgf] }, refine ⟨g - dg, λ y, _, funext hgf⟩, { have hyb : (g - dg) y < b, { rcases (hgb y).eq_or_lt with rfl|hlt, { refine (sub_lt_self_iff _).2 _, calc 0 < g y - c : sub_pos.2 hcb ... = dg y : (dgb rfl).symm }, { exact ((sub_le_self_iff _).2 (dgmem _).1).trans_lt hlt } }, rcases hb.exists_between hyb with ⟨_, ⟨xu, rfl⟩, hyxu, hxub⟩, cases lt_or_le c (g y) with hc hc, { rcases em (a ∈ range f) with ⟨x, rfl⟩|ha', { refine ⟨x, xu, _, hyxu.le⟩, calc f x = c - (b - c) : by rw [← hsub, sub_sub_cancel] ... ≤ g y - dg y : sub_le_sub hc.le (dgmem _).2 }, { have hay : a < (g - dg) y, { calc a = c - (b - c) : by rw [← hsub, sub_sub_cancel] ... < g y - (b - c) : sub_lt_sub_right hc _ ... ≤ g y - dg y : sub_le_sub_left (dgmem _).2 _ }, rcases ha.exists_between hay with ⟨_, ⟨x, rfl⟩, ha, hxy⟩, exact ⟨x, xu, hxy.le, hyxu.le⟩ } }, { refine ⟨xl y, xu, _, hyxu.le⟩, simp [dg0 (or.inr hc), hxl] } }, end /-- **Tietze extension theorem** for real-valued bounded continuous maps, a version for a closed embedding. Let `e` be a closed embedding of a nonempty topological space `X` into a normal topological space `Y`. Let `f` be a bounded continuous real-valued function on `X`. Let `t` be a nonempty convex set of real numbers (we use `ord_connected` instead of `convex` to automatically deduce this argument by typeclass search) such that `f x ∈ t` for all `x`. Then there exists a bounded continuous real-valued function `g : Y →ᵇ ℝ` such that `g y ∈ t` for all `y` and `g ∘ e = f`. -/ lemma exists_extension_forall_mem_of_closed_embedding (f : X →ᵇ ℝ) {t : set ℝ} {e : X → Y} [hs : ord_connected t] (hf : ∀ x, f x ∈ t) (hne : t.nonempty) (he : closed_embedding e) : ∃ g : Y →ᵇ ℝ, (∀ y, g y ∈ t) ∧ g ∘ e = f := begin casesI is_empty_or_nonempty X, { rcases hne with ⟨c, hc⟩, refine ⟨const Y c, λ y, hc, funext $ λ x, is_empty_elim x⟩ }, rcases exists_extension_forall_exists_le_ge_of_closed_embedding f he with ⟨g, hg, hgf⟩, refine ⟨g, λ y, _, hgf⟩, rcases hg y with ⟨xl, xu, h⟩, exact hs.out (hf _) (hf _) h end /-- **Tietze extension theorem** for real-valued bounded continuous maps, a version for a closed set. Let `s` be a closed set in a normal topological space `Y`. Let `f` be a bounded continuous real-valued function on `s`. Let `t` be a nonempty convex set of real numbers (we use `ord_connected` instead of `convex` to automatically deduce this argument by typeclass search) such that `f x ∈ t` for all `x : s`. Then there exists a bounded continuous real-valued function `g : Y →ᵇ ℝ` such that `g y ∈ t` for all `y` and `g.restrict s = f`. -/ lemma exists_forall_mem_restrict_eq_of_closed {s : set Y} (f : s →ᵇ ℝ) (hs : is_closed s) {t : set ℝ} [ord_connected t] (hf : ∀ x, f x ∈ t) (hne : t.nonempty) : ∃ g : Y →ᵇ ℝ, (∀ y, g y ∈ t) ∧ g.restrict s = f := begin rcases exists_extension_forall_mem_of_closed_embedding f hf hne (closed_embedding_subtype_coe hs) with ⟨g, hg, hgf⟩, exact ⟨g, hg, fun_like.coe_injective hgf⟩ end end bounded_continuous_function namespace continuous_map /-- **Tietze extension theorem** for real-valued continuous maps, a version for a closed embedding. Let `e` be a closed embedding of a nonempty topological space `X` into a normal topological space `Y`. Let `f` be a continuous real-valued function on `X`. Let `t` be a nonempty convex set of real numbers (we use `ord_connected` instead of `convex` to automatically deduce this argument by typeclass search) such that `f x ∈ t` for all `x`. Then there exists a continuous real-valued function `g : C(Y, ℝ)` such that `g y ∈ t` for all `y` and `g ∘ e = f`. -/ lemma exists_extension_forall_mem_of_closed_embedding (f : C(X, ℝ)) {t : set ℝ} {e : X → Y} [hs : ord_connected t] (hf : ∀ x, f x ∈ t) (hne : t.nonempty) (he : closed_embedding e) : ∃ g : C(Y, ℝ), (∀ y, g y ∈ t) ∧ g ∘ e = f := begin have h : ℝ ≃o Ioo (-1 : ℝ) 1 := order_iso_Ioo_neg_one_one ℝ, set F : X →ᵇ ℝ := { to_fun := coe ∘ (h ∘ f), continuous_to_fun := continuous_subtype_coe.comp (h.continuous.comp f.continuous), map_bounded' := bounded_range_iff.1 ((bounded_Ioo (-1 : ℝ) 1).mono $ forall_range_iff.2 $ λ x, (h (f x)).2) }, set t' : set ℝ := (coe ∘ h) '' t, have ht_sub : t' ⊆ Ioo (-1 : ℝ) 1 := image_subset_iff.2 (λ x hx, (h x).2), haveI : ord_connected t', { constructor, rintros _ ⟨x, hx, rfl⟩ _ ⟨y, hy, rfl⟩ z hz, lift z to Ioo (-1 : ℝ) 1 using (Icc_subset_Ioo (h x).2.1 (h y).2.2 hz), change z ∈ Icc (h x) (h y) at hz, rw [← h.image_Icc] at hz, rcases hz with ⟨z, hz, rfl⟩, exact ⟨z, hs.out hx hy hz, rfl⟩ }, have hFt : ∀ x, F x ∈ t', from λ x, mem_image_of_mem _ (hf x), rcases F.exists_extension_forall_mem_of_closed_embedding hFt (hne.image _) he with ⟨G, hG, hGF⟩, set g : C(Y, ℝ) := ⟨h.symm ∘ cod_restrict G _ (λ y, ht_sub (hG y)), h.symm.continuous.comp $ G.continuous.subtype_mk _⟩, have hgG : ∀ {y a}, g y = a ↔ G y = h a, from λ y a, h.to_equiv.symm_apply_eq.trans subtype.ext_iff, refine ⟨g, λ y, _, _⟩, { rcases hG y with ⟨a, ha, hay⟩, convert ha, exact hgG.2 hay.symm }, { ext x, exact hgG.2 (congr_fun hGF _) } end /-- **Tietze extension theorem** for real-valued continuous maps, a version for a closed embedding. Let `e` be a closed embedding of a nonempty topological space `X` into a normal topological space `Y`. Let `f` be a continuous real-valued function on `X`. Then there exists a continuous real-valued function `g : C(Y, ℝ)` such that `g ∘ e = f`. -/ lemma exists_extension_of_closed_embedding (f : C(X, ℝ)) (e : X → Y) (he : closed_embedding e) : ∃ g : C(Y, ℝ), g ∘ e = f := (exists_extension_forall_mem_of_closed_embedding f (λ x, mem_univ _) univ_nonempty he).imp $ λ g, and.right /-- **Tietze extension theorem** for real-valued continuous maps, a version for a closed set. Let `s` be a closed set in a normal topological space `Y`. Let `f` be a continuous real-valued function on `s`. Let `t` be a nonempty convex set of real numbers (we use `ord_connected` instead of `convex` to automatically deduce this argument by typeclass search) such that `f x ∈ t` for all `x : s`. Then there exists a continuous real-valued function `g : C(Y, ℝ)` such that `g y ∈ t` for all `y` and `g.restrict s = f`. -/ lemma exists_restrict_eq_forall_mem_of_closed {s : set Y} (f : C(s, ℝ)) {t : set ℝ} [ord_connected t] (ht : ∀ x, f x ∈ t) (hne : t.nonempty) (hs : is_closed s) : ∃ g : C(Y, ℝ), (∀ y, g y ∈ t) ∧ g.restrict s = f := let ⟨g, hgt, hgf⟩ := exists_extension_forall_mem_of_closed_embedding f ht hne (closed_embedding_subtype_coe hs) in ⟨g, hgt, coe_injective hgf⟩ /-- **Tietze extension theorem** for real-valued continuous maps, a version for a closed set. Let `s` be a closed set in a normal topological space `Y`. Let `f` be a continuous real-valued function on `s`. Then there exists a continuous real-valued function `g : C(Y, ℝ)` such that `g.restrict s = f`. -/ lemma exists_restrict_eq_of_closed {s : set Y} (f : C(s, ℝ)) (hs : is_closed s) : ∃ g : C(Y, ℝ), g.restrict s = f := let ⟨g, hg, hgf⟩ := exists_restrict_eq_forall_mem_of_closed f (λ _, mem_univ _) univ_nonempty hs in ⟨g, hgf⟩ end continuous_map
120f1a844370eb1be3b490c6e12b9e0be196bbb6
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/data/nat/basic.lean
5f2b22ec4c8e0c27a26198d836c5554988982284
[ "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
59,711
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura, Jeremy Avigad, Mario Carneiro -/ import logic.basic algebra.ordered_ring data.option.basic algebra.order_functions /-! # Basic operations on the natural numbers This files has some basic lemmas about natural numbers, definition of the `choice` function, and extra recursors: * `le_rec_on`, `le_induction`: recursion and induction principles starting at non-zero numbers. * `decreasing_induction` : recursion gowing downwards. * `strong_rec'` : recursion based on strong inequalities. -/ universes u v namespace nat variables {m n k : ℕ} -- Sometimes a bare `nat.add` or similar appears as a consequence of unfolding -- during pattern matching. These lemmas package them back up as typeclass -- mediated operations. @[simp] theorem add_def {a b : ℕ} : nat.add a b = a + b := rfl @[simp] theorem mul_def {a b : ℕ} : nat.mul a b = a * b := rfl attribute [simp] nat.add_sub_cancel nat.add_sub_cancel_left attribute [simp] nat.sub_self @[simp] lemma succ_pos' {n : ℕ} : 0 < succ n := succ_pos n theorem succ_inj' {n m : ℕ} : succ n = succ m ↔ n = m := ⟨succ_inj, congr_arg _⟩ theorem succ_le_succ_iff {m n : ℕ} : succ m ≤ succ n ↔ m ≤ n := ⟨le_of_succ_le_succ, succ_le_succ⟩ lemma zero_max {m : nat} : max 0 m = m := max_eq_right (zero_le _) theorem max_succ_succ {m n : ℕ} : max (succ m) (succ n) = succ (max m n) := begin by_cases h1 : m ≤ n, rw [max_eq_right h1, max_eq_right (succ_le_succ h1)], { rw not_le at h1, have h2 := le_of_lt h1, rw [max_eq_left h2, max_eq_left (succ_le_succ h2)] } end lemma not_succ_lt_self {n : ℕ} : ¬succ n < n := not_lt_of_ge (nat.le_succ _) theorem lt_succ_iff {m n : ℕ} : m < succ n ↔ m ≤ n := succ_le_succ_iff lemma succ_le_iff {m n : ℕ} : succ m ≤ n ↔ m < n := ⟨lt_of_succ_le, succ_le_of_lt⟩ lemma lt_iff_add_one_le {m n : ℕ} : m < n ↔ m + 1 ≤ n := by rw succ_le_iff -- Just a restatement of `nat.lt_succ_iff` using `+1`. lemma lt_add_one_iff {a b : ℕ} : a < b + 1 ↔ a ≤ b := lt_succ_iff -- A flipped version of `lt_add_one_iff`. lemma lt_one_add_iff {a b : ℕ} : a < 1 + b ↔ a ≤ b := by simp only [add_comm, lt_succ_iff] -- This is true reflexively, by the definition of `≤` on ℕ, -- but it's still useful to have, to convince Lean to change the syntactic type. lemma add_one_le_iff {a b : ℕ} : a + 1 ≤ b ↔ a < b := iff.refl _ lemma one_add_le_iff {a b : ℕ} : 1 + a ≤ b ↔ a < b := by simp only [add_comm, add_one_le_iff] theorem of_le_succ {n m : ℕ} (H : n ≤ m.succ) : n ≤ m ∨ n = m.succ := (lt_or_eq_of_le H).imp le_of_lt_succ id /-- Recursion starting at a non-zero number: given a map `C k → C (k+1)` for each `k`, there is a map from `C n` to each `C m`, `n ≤ m`. -/ @[elab_as_eliminator] def le_rec_on {C : ℕ → Sort u} {n : ℕ} : Π {m : ℕ}, n ≤ m → (Π {k}, C k → C (k+1)) → C n → C m | 0 H next x := eq.rec_on (eq_zero_of_le_zero H) x | (m+1) H next x := or.by_cases (of_le_succ H) (λ h : n ≤ m, next $ le_rec_on h @next x) (λ h : n = m + 1, eq.rec_on h x) theorem le_rec_on_self {C : ℕ → Sort u} {n} {h : n ≤ n} {next} (x : C n) : (le_rec_on h next x : C n) = x := by cases n; unfold le_rec_on or.by_cases; rw [dif_neg n.not_succ_le_self, dif_pos rfl] theorem le_rec_on_succ {C : ℕ → Sort u} {n m} (h1 : n ≤ m) {h2 : n ≤ m+1} {next} (x : C n) : (le_rec_on h2 @next x : C (m+1)) = next (le_rec_on h1 @next x : C m) := by conv { to_lhs, rw [le_rec_on, or.by_cases, dif_pos h1] } theorem le_rec_on_succ' {C : ℕ → Sort u} {n} {h : n ≤ n+1} {next} (x : C n) : (le_rec_on h next x : C (n+1)) = next x := by rw [le_rec_on_succ (le_refl n), le_rec_on_self] theorem le_rec_on_trans {C : ℕ → Sort u} {n m k} (hnm : n ≤ m) (hmk : m ≤ k) {next} (x : C n) : (le_rec_on (le_trans hnm hmk) @next x : C k) = le_rec_on hmk @next (le_rec_on hnm @next x) := begin induction hmk with k hmk ih, { rw le_rec_on_self }, rw [le_rec_on_succ (le_trans hnm hmk), ih, le_rec_on_succ] end theorem le_rec_on_succ_left {C : ℕ → Sort u} {n m} (h1 : n ≤ m) (h2 : n+1 ≤ m) {next : Π{{k}}, C k → C (k+1)} (x : C n) : (le_rec_on h2 next (next x) : C m) = (le_rec_on h1 next x : C m) := begin rw [subsingleton.elim h1 (le_trans (le_succ n) h2), le_rec_on_trans (le_succ n) h2, le_rec_on_succ'] end theorem le_rec_on_injective {C : ℕ → Sort u} {n m} (hnm : n ≤ m) (next : Π n, C n → C (n+1)) (Hnext : ∀ n, function.injective (next n)) : function.injective (le_rec_on hnm next) := begin induction hnm with m hnm ih, { intros x y H, rwa [le_rec_on_self, le_rec_on_self] at H }, intros x y H, rw [le_rec_on_succ hnm, le_rec_on_succ hnm] at H, exact ih (Hnext _ H) end theorem le_rec_on_surjective {C : ℕ → Sort u} {n m} (hnm : n ≤ m) (next : Π n, C n → C (n+1)) (Hnext : ∀ n, function.surjective (next n)) : function.surjective (le_rec_on hnm next) := begin induction hnm with m hnm ih, { intros x, use x, rw le_rec_on_self }, intros x, rcases Hnext _ x with ⟨w, rfl⟩, rcases ih w with ⟨x, rfl⟩, use x, rw le_rec_on_succ end theorem pred_eq_of_eq_succ {m n : ℕ} (H : m = n.succ) : m.pred = n := by simp [H] @[simp] lemma pred_eq_succ_iff {n m : ℕ} : pred n = succ m ↔ n = m + 2 := by cases n; split; rintro ⟨⟩; refl theorem pred_sub (n m : ℕ) : pred n - m = pred (n - m) := by rw [← sub_one, nat.sub_sub, one_add]; refl @[simp] lemma add_succ_sub_one (n m : ℕ) : (n + succ m) - 1 = n + m := by rw [add_succ, succ_sub_one] @[simp] lemma succ_add_sub_one (n m : ℕ) : (succ n + m) - 1 = n + m := by rw [succ_add, succ_sub_one] lemma pred_eq_sub_one (n : ℕ) : pred n = n - 1 := rfl lemma one_le_of_lt {n m : ℕ} (h : n < m) : 1 ≤ m := lt_of_le_of_lt (nat.zero_le _) h lemma le_pred_of_lt {n m : ℕ} (h : m < n) : m ≤ n - 1 := nat.sub_le_sub_right h 1 lemma le_of_pred_lt {m n : ℕ} : pred m < n → m ≤ n := match m with | 0 := le_of_lt | m+1 := id end /-- This ensures that `simp` succeeds on `pred (n + 1) = n`. -/ @[simp] lemma pred_one_add (n : ℕ) : pred (1 + n) = n := by rw [add_comm, add_one, pred_succ] theorem pos_iff_ne_zero : 0 < n ↔ n ≠ 0 := ⟨ne_of_gt, nat.pos_of_ne_zero⟩ lemma one_lt_iff_ne_zero_and_ne_one : ∀ {n : ℕ}, 1 < n ↔ n ≠ 0 ∧ n ≠ 1 | 0 := dec_trivial | 1 := dec_trivial | (n+2) := dec_trivial theorem eq_of_lt_succ_of_not_lt {a b : ℕ} (h1 : a < b + 1) (h2 : ¬ a < b) : a = b := have h3 : a ≤ b, from le_of_lt_succ h1, or.elim (eq_or_lt_of_not_lt h2) (λ h, h) (λ h, absurd h (not_lt_of_ge h3)) protected theorem le_sub_add (n m : ℕ) : n ≤ n - m + m := or.elim (le_total n m) (assume : n ≤ m, begin rw [sub_eq_zero_of_le this, zero_add], exact this end) (assume : m ≤ n, begin rw (nat.sub_add_cancel this) end) theorem sub_add_eq_max (n m : ℕ) : n - m + m = max n m := eq_max (nat.le_sub_add _ _) (le_add_left _ _) $ λ k h₁ h₂, by rw ← nat.sub_add_cancel h₂; exact add_le_add_right (nat.sub_le_sub_right h₁ _) _ theorem sub_add_min (n m : ℕ) : n - m + min n m = n := (le_total n m).elim (λ h, by rw [min_eq_left h, sub_eq_zero_of_le h, zero_add]) (λ h, by rw [min_eq_right h, nat.sub_add_cancel h]) protected theorem add_sub_cancel' {n m : ℕ} (h : m ≤ n) : m + (n - m) = n := by rw [add_comm, nat.sub_add_cancel h] protected theorem sub_eq_of_eq_add (h : k = m + n) : k - m = n := begin rw [h, nat.add_sub_cancel_left] end theorem sub_cancel {a b c : ℕ} (h₁ : a ≤ b) (h₂ : a ≤ c) (w : b - a = c - a) : b = c := by rw [←nat.sub_add_cancel h₁, ←nat.sub_add_cancel h₂, w] lemma sub_sub_sub_cancel_right {a b c : ℕ} (h₂ : c ≤ b) : (a - c) - (b - c) = a - b := by rw [nat.sub_sub, ←nat.add_sub_assoc h₂, nat.add_sub_cancel_left] lemma add_sub_cancel_right (n m k : ℕ) : n + (m + k) - k = n + m := by { rw [nat.add_sub_assoc, nat.add_sub_cancel], apply k.le_add_left } protected lemma sub_add_eq_add_sub {a b c : ℕ} (h : b ≤ a) : (a - b) + c = (a + c) - b := by rw [add_comm a, nat.add_sub_assoc h, add_comm] theorem sub_min (n m : ℕ) : n - min n m = n - m := nat.sub_eq_of_eq_add $ by rw [add_comm, sub_add_min] theorem sub_sub_assoc {a b c : ℕ} (h₁ : b ≤ a) (h₂ : c ≤ b) : a - (b - c) = a - b + c := (nat.sub_eq_iff_eq_add (le_trans (nat.sub_le _ _) h₁)).2 $ by rw [add_right_comm, add_assoc, nat.sub_add_cancel h₂, nat.sub_add_cancel h₁] protected theorem lt_of_sub_pos (h : 0 < n - m) : m < n := lt_of_not_ge (assume : n ≤ m, have n - m = 0, from sub_eq_zero_of_le this, begin rw this at h, exact lt_irrefl _ h end) protected theorem lt_of_sub_lt_sub_right : m - k < n - k → m < n := lt_imp_lt_of_le_imp_le (λ h, nat.sub_le_sub_right h _) protected theorem lt_of_sub_lt_sub_left : m - n < m - k → k < n := lt_imp_lt_of_le_imp_le (nat.sub_le_sub_left _) protected theorem sub_lt_self (h₁ : 0 < m) (h₂ : 0 < n) : m - n < m := calc m - n = succ (pred m) - succ (pred n) : by rw [succ_pred_eq_of_pos h₁, succ_pred_eq_of_pos h₂] ... = pred m - pred n : by rw succ_sub_succ ... ≤ pred m : sub_le _ _ ... < succ (pred m) : lt_succ_self _ ... = m : succ_pred_eq_of_pos h₁ protected theorem le_sub_right_of_add_le (h : m + k ≤ n) : m ≤ n - k := by rw ← nat.add_sub_cancel m k; exact nat.sub_le_sub_right h k protected theorem le_sub_left_of_add_le (h : k + m ≤ n) : m ≤ n - k := nat.le_sub_right_of_add_le (by rwa add_comm at h) protected theorem lt_sub_right_of_add_lt (h : m + k < n) : m < n - k := lt_of_succ_le $ nat.le_sub_right_of_add_le $ by rw succ_add; exact succ_le_of_lt h protected theorem lt_sub_left_of_add_lt (h : k + m < n) : m < n - k := nat.lt_sub_right_of_add_lt (by rwa add_comm at h) protected theorem add_lt_of_lt_sub_right (h : m < n - k) : m + k < n := @nat.lt_of_sub_lt_sub_right _ _ k (by rwa nat.add_sub_cancel) protected theorem add_lt_of_lt_sub_left (h : m < n - k) : k + m < n := by rw add_comm; exact nat.add_lt_of_lt_sub_right h protected theorem le_add_of_sub_le_right : n - k ≤ m → n ≤ m + k := le_imp_le_of_lt_imp_lt nat.lt_sub_right_of_add_lt protected theorem le_add_of_sub_le_left : n - k ≤ m → n ≤ k + m := le_imp_le_of_lt_imp_lt nat.lt_sub_left_of_add_lt protected theorem lt_add_of_sub_lt_right : n - k < m → n < m + k := lt_imp_lt_of_le_imp_le nat.le_sub_right_of_add_le protected theorem lt_add_of_sub_lt_left : n - k < m → n < k + m := lt_imp_lt_of_le_imp_le nat.le_sub_left_of_add_le protected theorem sub_le_left_of_le_add : n ≤ k + m → n - k ≤ m := le_imp_le_of_lt_imp_lt nat.add_lt_of_lt_sub_left protected theorem sub_le_right_of_le_add : n ≤ m + k → n - k ≤ m := le_imp_le_of_lt_imp_lt nat.add_lt_of_lt_sub_right protected theorem sub_lt_left_iff_lt_add (H : n ≤ k) : k - n < m ↔ k < n + m := ⟨nat.lt_add_of_sub_lt_left, λ h₁, have succ k ≤ n + m, from succ_le_of_lt h₁, have succ (k - n) ≤ m, from calc succ (k - n) = succ k - n : by rw (succ_sub H) ... ≤ n + m - n : nat.sub_le_sub_right this n ... = m : by rw nat.add_sub_cancel_left, lt_of_succ_le this⟩ protected theorem le_sub_left_iff_add_le (H : m ≤ k) : n ≤ k - m ↔ m + n ≤ k := le_iff_le_iff_lt_iff_lt.2 (nat.sub_lt_left_iff_lt_add H) protected theorem le_sub_right_iff_add_le (H : n ≤ k) : m ≤ k - n ↔ m + n ≤ k := by rw [nat.le_sub_left_iff_add_le H, add_comm] protected theorem lt_sub_left_iff_add_lt : n < k - m ↔ m + n < k := ⟨nat.add_lt_of_lt_sub_left, nat.lt_sub_left_of_add_lt⟩ protected theorem lt_sub_right_iff_add_lt : m < k - n ↔ m + n < k := by rw [nat.lt_sub_left_iff_add_lt, add_comm] theorem sub_le_left_iff_le_add : m - n ≤ k ↔ m ≤ n + k := le_iff_le_iff_lt_iff_lt.2 nat.lt_sub_left_iff_add_lt theorem sub_le_right_iff_le_add : m - k ≤ n ↔ m ≤ n + k := by rw [nat.sub_le_left_iff_le_add, add_comm] protected theorem sub_lt_right_iff_lt_add (H : k ≤ m) : m - k < n ↔ m < n + k := by rw [nat.sub_lt_left_iff_lt_add H, add_comm] protected theorem sub_le_sub_left_iff (H : k ≤ m) : m - n ≤ m - k ↔ k ≤ n := ⟨λ h, have k + (m - k) - n ≤ m - k, by rwa nat.add_sub_cancel' H, nat.le_of_add_le_add_right (nat.le_add_of_sub_le_left this), nat.sub_le_sub_left _⟩ protected theorem sub_lt_sub_right_iff (H : k ≤ m) : m - k < n - k ↔ m < n := lt_iff_lt_of_le_iff_le (nat.sub_le_sub_right_iff _ _ _ H) protected theorem sub_lt_sub_left_iff (H : n ≤ m) : m - n < m - k ↔ k < n := lt_iff_lt_of_le_iff_le (nat.sub_le_sub_left_iff H) protected theorem sub_le_iff : m - n ≤ k ↔ m - k ≤ n := nat.sub_le_left_iff_le_add.trans nat.sub_le_right_iff_le_add.symm protected lemma sub_le_self (n m : ℕ) : n - m ≤ n := nat.sub_le_left_of_le_add (nat.le_add_left _ _) protected theorem sub_lt_iff (h₁ : n ≤ m) (h₂ : k ≤ m) : m - n < k ↔ m - k < n := (nat.sub_lt_left_iff_lt_add h₁).trans (nat.sub_lt_right_iff_lt_add h₂).symm lemma pred_le_iff {n m : ℕ} : pred n ≤ m ↔ n ≤ succ m := @nat.sub_le_right_iff_le_add n m 1 lemma lt_pred_iff {n m : ℕ} : n < pred m ↔ succ n < m := @nat.lt_sub_right_iff_add_lt n 1 m lemma lt_of_lt_pred {a b : ℕ} (h : a < b - 1) : a < b := lt_of_succ_lt (lt_pred_iff.1 h) protected theorem mul_ne_zero {n m : ℕ} (n0 : n ≠ 0) (m0 : m ≠ 0) : n * m ≠ 0 | nm := (eq_zero_of_mul_eq_zero nm).elim n0 m0 @[simp] protected theorem mul_eq_zero {a b : ℕ} : a * b = 0 ↔ a = 0 ∨ b = 0 := iff.intro eq_zero_of_mul_eq_zero (by simp [or_imp_distrib] {contextual := tt}) @[simp] protected theorem zero_eq_mul {a b : ℕ} : 0 = a * b ↔ a = 0 ∨ b = 0 := by rw [eq_comm, nat.mul_eq_zero] lemma eq_zero_of_double_le {a : ℕ} (h : 2 * a ≤ a) : a = 0 := nat.eq_zero_of_le_zero $ by rwa [two_mul, nat.add_le_to_le_sub, nat.sub_self] at h; refl lemma eq_zero_of_mul_le {a b : ℕ} (hb : 2 ≤ b) (h : b * a ≤ a) : a = 0 := eq_zero_of_double_le $ le_trans (nat.mul_le_mul_right _ hb) h lemma le_mul_of_pos_left {m n : ℕ} (h : 0 < n) : m ≤ n * m := begin conv {to_lhs, rw [← one_mul(m)]}, exact mul_le_mul_of_nonneg_right (nat.succ_le_of_lt h) dec_trivial, end lemma le_mul_of_pos_right {m n : ℕ} (h : 0 < n) : m ≤ m * n := begin conv {to_lhs, rw [← mul_one(m)]}, exact mul_le_mul_of_nonneg_left (nat.succ_le_of_lt h) dec_trivial, end theorem two_mul_ne_two_mul_add_one {n m} : 2 * n ≠ 2 * m + 1 := mt (congr_arg (%2)) (by rw [add_comm, add_mul_mod_self_left, mul_mod_right]; exact dec_trivial) /-- Recursion principle based on `<`. -/ @[elab_as_eliminator] protected def strong_rec' {p : ℕ → Sort u} (H : ∀ n, (∀ m, m < n → p m) → p n) : ∀ (n : ℕ), p n | n := H n (λ m hm, strong_rec' m) attribute [simp] nat.div_self protected lemma div_le_of_le_mul' {m n : ℕ} {k} (h : m ≤ k * n) : m / k ≤ n := (eq_zero_or_pos k).elim (λ k0, by rw [k0, nat.div_zero]; apply zero_le) (λ k0, (decidable.mul_le_mul_left k0).1 $ calc k * (m / k) ≤ m % k + k * (m / k) : le_add_left _ _ ... = m : mod_add_div _ _ ... ≤ k * n : h) protected lemma div_le_self' (m n : ℕ) : m / n ≤ m := (eq_zero_or_pos n).elim (λ n0, by rw [n0, nat.div_zero]; apply zero_le) (λ n0, nat.div_le_of_le_mul' $ calc m = 1 * m : (one_mul _).symm ... ≤ n * m : mul_le_mul_right _ n0) theorem le_div_iff_mul_le' {x y : ℕ} {k : ℕ} (k0 : 0 < k) : x ≤ y / k ↔ x * k ≤ y := begin revert x, refine nat.strong_rec' _ y, clear y, intros y IH x, cases decidable.lt_or_le y k with h h, { rw [div_eq_of_lt h], cases x with x, { simp [zero_mul, zero_le] }, { rw succ_mul, exact iff_of_false (not_succ_le_zero _) (not_le_of_lt $ lt_of_lt_of_le h (le_add_left _ _)) } }, { rw [div_eq_sub_div k0 h], cases x with x, { simp [zero_mul, zero_le] }, { rw [← add_one, nat.add_le_add_iff_le_right, succ_mul, IH _ (sub_lt_of_pos_le _ _ k0 h), add_le_to_le_sub _ h] } } end theorem div_mul_le_self' (m n : ℕ) : m / n * n ≤ m := (nat.eq_zero_or_pos n).elim (λ n0, by simp [n0, zero_le]) $ λ n0, (le_div_iff_mul_le' n0).1 (le_refl _) theorem div_lt_iff_lt_mul' {x y : ℕ} {k : ℕ} (k0 : 0 < k) : x / k < y ↔ x < y * k := lt_iff_lt_of_le_iff_le $ le_div_iff_mul_le' k0 protected theorem div_le_div_right {n m : ℕ} (h : n ≤ m) {k : ℕ} : n / k ≤ m / k := (nat.eq_zero_or_pos k).elim (λ k0, by simp [k0]) $ λ hk, (le_div_iff_mul_le' hk).2 $ le_trans (nat.div_mul_le_self' _ _) h lemma lt_of_div_lt_div {m n k : ℕ} (h : m / k < n / k) : m < n := by_contradiction $ λ h₁, absurd h (not_lt_of_ge (nat.div_le_div_right (not_lt.1 h₁))) protected theorem eq_mul_of_div_eq_right {a b c : ℕ} (H1 : b ∣ a) (H2 : a / b = c) : a = b * c := by rw [← H2, nat.mul_div_cancel' H1] protected theorem div_eq_iff_eq_mul_right {a b c : ℕ} (H : 0 < b) (H' : b ∣ a) : a / b = c ↔ a = b * c := ⟨nat.eq_mul_of_div_eq_right H', nat.div_eq_of_eq_mul_right H⟩ protected theorem div_eq_iff_eq_mul_left {a b c : ℕ} (H : 0 < b) (H' : b ∣ a) : a / b = c ↔ a = c * b := by rw mul_comm; exact nat.div_eq_iff_eq_mul_right H H' protected theorem eq_mul_of_div_eq_left {a b c : ℕ} (H1 : b ∣ a) (H2 : a / b = c) : a = c * b := by rw [mul_comm, nat.eq_mul_of_div_eq_right H1 H2] protected theorem mul_div_cancel_left' {a b : ℕ} (Hd : a ∣ b) : a * (b / a) = b := by rw [mul_comm,nat.div_mul_cancel Hd] protected theorem div_mod_unique {n k m d : ℕ} (h : 0 < k) : n / k = d ∧ n % k = m ↔ m + k * d = n ∧ m < k := ⟨λ ⟨e₁, e₂⟩, e₁ ▸ e₂ ▸ ⟨mod_add_div _ _, mod_lt _ h⟩, λ ⟨h₁, h₂⟩, h₁ ▸ by rw [add_mul_div_left _ _ h, add_mul_mod_self_left]; simp [div_eq_of_lt, mod_eq_of_lt, h₂]⟩ lemma two_mul_odd_div_two {n : ℕ} (hn : n % 2 = 1) : 2 * (n / 2) = n - 1 := by conv {to_rhs, rw [← nat.mod_add_div n 2, hn, nat.add_sub_cancel_left]} lemma div_dvd_of_dvd {a b : ℕ} (h : b ∣ a) : (a / b) ∣ a := ⟨b, (nat.div_mul_cancel h).symm⟩ protected lemma div_pos {a b : ℕ} (hba : b ≤ a) (hb : 0 < b) : 0 < a / b := nat.pos_of_ne_zero (λ h, lt_irrefl a (calc a = a % b : by simpa [h] using (mod_add_div a b).symm ... < b : nat.mod_lt a hb ... ≤ a : hba)) protected theorem mul_right_inj {a b c : ℕ} (ha : 0 < a) : b * a = c * a ↔ b = c := ⟨nat.eq_of_mul_eq_mul_right ha, λ e, e ▸ rfl⟩ protected theorem mul_left_inj {a b c : ℕ} (ha : 0 < a) : a * b = a * c ↔ b = c := ⟨nat.eq_of_mul_eq_mul_left ha, λ e, e ▸ rfl⟩ protected lemma div_div_self : ∀ {a b : ℕ}, b ∣ a → 0 < a → a / (a / b) = b | a 0 h₁ h₂ := by rw eq_zero_of_zero_dvd h₁; refl | 0 b h₁ h₂ := absurd h₂ dec_trivial | (a+1) (b+1) h₁ h₂ := (nat.mul_right_inj (nat.div_pos (le_of_dvd (succ_pos a) h₁) (succ_pos b))).1 $ by rw [nat.div_mul_cancel (div_dvd_of_dvd h₁), nat.mul_div_cancel' h₁] protected lemma div_lt_of_lt_mul {m n k : ℕ} (h : m < n * k) : m / n < k := lt_of_mul_lt_mul_left (calc n * (m / n) ≤ m % n + n * (m / n) : nat.le_add_left _ _ ... = m : mod_add_div _ _ ... < n * k : h) (nat.zero_le n) lemma lt_mul_of_div_lt {a b c : ℕ} (h : a / c < b) (w : 0 < c) : a < b * c := lt_of_not_ge $ not_le_of_gt h ∘ (nat.le_div_iff_mul_le _ _ w).2 protected lemma div_eq_zero_iff {a b : ℕ} (hb : 0 < b) : a / b = 0 ↔ a < b := ⟨λ h, by rw [← mod_add_div a b, h, mul_zero, add_zero]; exact mod_lt _ hb, λ h, by rw [← nat.mul_left_inj hb, ← @add_left_cancel_iff _ _ (a % b), mod_add_div, mod_eq_of_lt h, mul_zero, add_zero]⟩ lemma eq_zero_of_le_div {a b : ℕ} (hb : 2 ≤ b) (h : a ≤ a / b) : a = 0 := eq_zero_of_mul_le hb $ by rw mul_comm; exact (nat.le_div_iff_mul_le' (lt_of_lt_of_le dec_trivial hb)).1 h lemma mul_div_le_mul_div_assoc (a b c : ℕ) : a * (b / c) ≤ (a * b) / c := if hc0 : c = 0 then by simp [hc0] else (nat.le_div_iff_mul_le _ _ (nat.pos_of_ne_zero hc0)).2 (by rw [mul_assoc]; exact mul_le_mul_left _ (nat.div_mul_le_self _ _)) lemma div_mul_div_le_div (a b c : ℕ) : ((a / c) * b) / a ≤ b / c := if ha0 : a = 0 then by simp [ha0] else calc a / c * b / a ≤ b * a / c / a : nat.div_le_div_right (by rw [mul_comm]; exact mul_div_le_mul_div_assoc _ _ _) ... = b / c : by rw [nat.div_div_eq_div_mul, mul_comm b, mul_comm c, nat.mul_div_mul _ _ (nat.pos_of_ne_zero ha0)] lemma eq_zero_of_le_half {a : ℕ} (h : a ≤ a / 2) : a = 0 := eq_zero_of_le_div (le_refl _) h lemma mod_mul_right_div_self (a b c : ℕ) : a % (b * c) / b = (a / b) % c := if hb : b = 0 then by simp [hb] else if hc : c = 0 then by simp [hc] else by conv {to_rhs, rw ← mod_add_div a (b * c)}; rw [mul_assoc, nat.add_mul_div_left _ _ (nat.pos_of_ne_zero hb), add_mul_mod_self_left, mod_eq_of_lt (nat.div_lt_of_lt_mul (mod_lt _ (mul_pos (nat.pos_of_ne_zero hb) (nat.pos_of_ne_zero hc))))] lemma mod_mul_left_div_self (a b c : ℕ) : a % (c * b) / b = (a / b) % c := by rw [mul_comm c, mod_mul_right_div_self] /- The `n+1`-st triangle number is `n` more than the `n`-th triangle number -/ lemma triangle_succ (n : ℕ) : (n + 1) * ((n + 1) - 1) / 2 = n * (n - 1) / 2 + n := begin rw [← add_mul_div_left, mul_comm 2 n, ← mul_add, nat.add_sub_cancel, mul_comm], cases n; refl, apply zero_lt_succ end @[simp] protected theorem dvd_one {n : ℕ} : n ∣ 1 ↔ n = 1 := ⟨eq_one_of_dvd_one, λ e, e.symm ▸ dvd_refl _⟩ protected theorem dvd_add_left {k m n : ℕ} (h : k ∣ n) : k ∣ m + n ↔ k ∣ m := (nat.dvd_add_iff_left h).symm protected theorem dvd_add_right {k m n : ℕ} (h : k ∣ m) : k ∣ m + n ↔ k ∣ n := (nat.dvd_add_iff_right h).symm /-- A natural number m divides the sum m + n if and only if m divides b.-/ @[simp] protected lemma dvd_add_self_left {m n : ℕ} : m ∣ m + n ↔ m ∣ n := nat.dvd_add_right (dvd_refl m) /-- A natural number m divides the sum n + m if and only if m divides b.-/ @[simp] protected lemma dvd_add_self_right {m n : ℕ} : m ∣ n + m ↔ m ∣ n := nat.dvd_add_left (dvd_refl m) protected theorem mul_dvd_mul_iff_left {a b c : ℕ} (ha : 0 < a) : a * b ∣ a * c ↔ b ∣ c := exists_congr $ λ d, by rw [mul_assoc, nat.mul_left_inj ha] protected theorem mul_dvd_mul_iff_right {a b c : ℕ} (hc : 0 < c) : a * c ∣ b * c ↔ a ∣ b := exists_congr $ λ d, by rw [mul_right_comm, nat.mul_right_inj hc] lemma succ_div : ∀ (a b : ℕ), (a + 1) / b = a / b + if b ∣ a + 1 then 1 else 0 | a 0 := by simp | 0 1 := rfl | 0 (b+2) := have hb2 : b + 2 > 1, from dec_trivial, by simp [ne_of_gt hb2, div_eq_of_lt hb2] | (a+1) (b+1) := begin rw [nat.div_def], conv_rhs { rw nat.div_def }, by_cases hb_eq_a : b = a + 1, { simp [hb_eq_a, le_refl] }, by_cases hb_le_a1 : b ≤ a + 1, { have hb_le_a : b ≤ a, from le_of_lt_succ (lt_of_le_of_ne hb_le_a1 hb_eq_a), have h₁ : (0 < b + 1 ∧ b + 1 ≤ a + 1 + 1), from ⟨succ_pos _, (add_le_add_iff_right _).2 hb_le_a1⟩, have h₂ : (0 < b + 1 ∧ b + 1 ≤ a + 1), from ⟨succ_pos _, (add_le_add_iff_right _).2 hb_le_a⟩, have dvd_iff : b + 1 ∣ a - b + 1 ↔ b + 1 ∣ a + 1 + 1, { rw [nat.dvd_add_iff_left (dvd_refl (b + 1)), ← nat.add_sub_add_right a 1 b, add_comm (_ - _), add_assoc, nat.sub_add_cancel (succ_le_succ hb_le_a), add_comm 1] }, have wf : a - b < a + 1, from lt_succ_of_le (nat.sub_le_self _ _), rw [if_pos h₁, if_pos h₂, nat.add_sub_add_right, nat.sub_add_comm hb_le_a, by exact have _ := wf, succ_div (a - b), nat.add_sub_add_right], simp [dvd_iff, succ_eq_add_one, add_comm 1] }, { have hba : ¬ b ≤ a, from not_le_of_gt (lt_trans (lt_succ_self a) (lt_of_not_ge hb_le_a1)), have hb_dvd_a : ¬ b + 1 ∣ a + 2, from λ h, hb_le_a1 (le_of_succ_le_succ (le_of_dvd (succ_pos _) h)), simp [hba, hb_le_a1, hb_dvd_a], } end lemma succ_div_of_dvd {a b : ℕ} (hba : b ∣ a + 1) : (a + 1) / b = a / b + 1 := by rw [succ_div, if_pos hba] lemma succ_div_of_not_dvd {a b : ℕ} (hba : ¬ b ∣ a + 1) : (a + 1) / b = a / b := by rw [succ_div, if_neg hba, add_zero] @[simp] theorem mod_mod_of_dvd (n : nat) {m k : nat} (h : m ∣ k) : n % k % m = n % m := begin conv { to_rhs, rw ←mod_add_div n k }, rcases h with ⟨t, rfl⟩, rw [mul_assoc, add_mul_mod_self_left] end @[simp] theorem mod_mod (a n : ℕ) : (a % n) % n = a % n := (eq_zero_or_pos n).elim (λ n0, by simp [n0]) (λ npos, mod_eq_of_lt (mod_lt _ npos)) theorem add_pos_left {m : ℕ} (h : 0 < m) (n : ℕ) : 0 < m + n := calc m + n > 0 + n : nat.add_lt_add_right h n ... = n : nat.zero_add n ... ≥ 0 : zero_le n theorem add_pos_right (m : ℕ) {n : ℕ} (h : 0 < n) : 0 < m + n := begin rw add_comm, exact add_pos_left h m end theorem add_pos_iff_pos_or_pos (m n : ℕ) : 0 < m + n ↔ 0 < m ∨ 0 < n := iff.intro begin intro h, cases m with m, {simp [zero_add] at h, exact or.inr h}, exact or.inl (succ_pos _) end begin intro h, cases h with mpos npos, { apply add_pos_left mpos }, apply add_pos_right _ npos end lemma add_eq_one_iff : ∀ {a b : ℕ}, a + b = 1 ↔ (a = 0 ∧ b = 1) ∨ (a = 1 ∧ b = 0) | 0 0 := dec_trivial | 0 1 := dec_trivial | 1 0 := dec_trivial | 1 1 := dec_trivial | (a+2) _ := by rw add_right_comm; exact dec_trivial | _ (b+2) := by rw [← add_assoc]; simp only [nat.succ_inj', nat.succ_ne_zero]; simp lemma mul_eq_one_iff : ∀ {a b : ℕ}, a * b = 1 ↔ a = 1 ∧ b = 1 | 0 0 := dec_trivial | 0 1 := dec_trivial | 1 0 := dec_trivial | (a+2) 0 := by simp | 0 (b+2) := by simp | (a+1) (b+1) := ⟨λ h, by simp only [add_mul, mul_add, mul_add, one_mul, mul_one, (add_assoc _ _ _).symm, nat.succ_inj', add_eq_zero_iff] at h; simp [h.1.2, h.2], by clear_aux_decl; finish⟩ lemma mul_right_eq_self_iff {a b : ℕ} (ha : 0 < a) : a * b = a ↔ b = 1 := suffices a * b = a * 1 ↔ b = 1, by rwa mul_one at this, nat.mul_left_inj ha lemma mul_left_eq_self_iff {a b : ℕ} (hb : 0 < b) : a * b = b ↔ a = 1 := by rw [mul_comm, nat.mul_right_eq_self_iff hb] lemma lt_succ_iff_lt_or_eq {n i : ℕ} : n < i.succ ↔ (n < i ∨ n = i) := lt_succ_iff.trans le_iff_lt_or_eq theorem le_zero_iff {i : ℕ} : i ≤ 0 ↔ i = 0 := ⟨nat.eq_zero_of_le_zero, assume h, h ▸ le_refl i⟩ theorem le_add_one_iff {i j : ℕ} : i ≤ j + 1 ↔ (i ≤ j ∨ i = j + 1) := ⟨assume h, match nat.eq_or_lt_of_le h with | or.inl h := or.inr h | or.inr h := or.inl $ nat.le_of_succ_le_succ h end, or.rec (assume h, le_trans h $ nat.le_add_right _ _) le_of_eq⟩ theorem mul_self_inj {n m : ℕ} : n * n = m * m ↔ n = m := le_antisymm_iff.trans (le_antisymm_iff.trans (and_congr mul_self_le_mul_self_iff mul_self_le_mul_self_iff)).symm instance decidable_ball_lt (n : nat) (P : Π k < n, Prop) : ∀ [H : ∀ n h, decidable (P n h)], decidable (∀ n h, P n h) := begin induction n with n IH; intro; resetI, { exact is_true (λ n, dec_trivial) }, cases IH (λ k h, P k (lt_succ_of_lt h)) with h, { refine is_false (mt _ h), intros hn k h, apply hn }, by_cases p : P n (lt_succ_self n), { exact is_true (λ k h', (lt_or_eq_of_le $ le_of_lt_succ h').elim (h _) (λ e, match k, e, h' with _, rfl, h := p end)) }, { exact is_false (mt (λ hn, hn _ _) p) } end instance decidable_forall_fin {n : ℕ} (P : fin n → Prop) [H : decidable_pred P] : decidable (∀ i, P i) := decidable_of_iff (∀ k h, P ⟨k, h⟩) ⟨λ a ⟨k, h⟩, a k h, λ a k h, a ⟨k, h⟩⟩ instance decidable_ball_le (n : ℕ) (P : Π k ≤ n, Prop) [H : ∀ n h, decidable (P n h)] : decidable (∀ n h, P n h) := decidable_of_iff (∀ k (h : k < succ n), P k (le_of_lt_succ h)) ⟨λ a k h, a k (lt_succ_of_le h), λ a k h, a k _⟩ instance decidable_lo_hi (lo hi : ℕ) (P : ℕ → Prop) [H : decidable_pred P] : decidable (∀x, lo ≤ x → x < hi → P x) := decidable_of_iff (∀ x < hi - lo, P (lo + x)) ⟨λal x hl hh, by have := al (x - lo) (lt_of_not_ge $ (not_congr (nat.sub_le_sub_right_iff _ _ _ hl)).2 $ not_le_of_gt hh); rwa [nat.add_sub_of_le hl] at this, λal x h, al _ (nat.le_add_right _ _) (nat.add_lt_of_lt_sub_left h)⟩ instance decidable_lo_hi_le (lo hi : ℕ) (P : ℕ → Prop) [H : decidable_pred P] : decidable (∀x, lo ≤ x → x ≤ hi → P x) := decidable_of_iff (∀x, lo ≤ x → x < hi + 1 → P x) $ ball_congr $ λ x hl, imp_congr lt_succ_iff iff.rfl protected theorem bit0_le {n m : ℕ} (h : n ≤ m) : bit0 n ≤ bit0 m := add_le_add h h protected theorem bit1_le {n m : ℕ} (h : n ≤ m) : bit1 n ≤ bit1 m := succ_le_succ (add_le_add h h) theorem bit_le : ∀ (b : bool) {n m : ℕ}, n ≤ m → bit b n ≤ bit b m | tt n m h := nat.bit1_le h | ff n m h := nat.bit0_le h theorem bit_ne_zero (b) {n} (h : n ≠ 0) : bit b n ≠ 0 := by cases b; [exact nat.bit0_ne_zero h, exact nat.bit1_ne_zero _] theorem bit0_le_bit : ∀ (b) {m n : ℕ}, m ≤ n → bit0 m ≤ bit b n | tt m n h := le_of_lt $ nat.bit0_lt_bit1 h | ff m n h := nat.bit0_le h theorem bit_le_bit1 : ∀ (b) {m n : ℕ}, m ≤ n → bit b m ≤ bit1 n | ff m n h := le_of_lt $ nat.bit0_lt_bit1 h | tt m n h := nat.bit1_le h theorem bit_lt_bit0 : ∀ (b) {n m : ℕ}, n < m → bit b n < bit0 m | tt n m h := nat.bit1_lt_bit0 h | ff n m h := nat.bit0_lt h theorem bit_lt_bit (a b) {n m : ℕ} (h : n < m) : bit a n < bit b m := lt_of_lt_of_le (bit_lt_bit0 _ h) (bit0_le_bit _ (le_refl _)) @[simp] lemma bit0_le_bit1_iff : bit0 k ≤ bit1 n ↔ k ≤ n := ⟨λ h, by rwa [← nat.lt_succ_iff, n.bit1_eq_succ_bit0, ← n.bit0_succ_eq, bit0_lt_bit0, nat.lt_succ_iff] at h, λ h, le_of_lt (nat.bit0_lt_bit1 h)⟩ @[simp] lemma bit0_lt_bit1_iff : bit0 k < bit1 n ↔ k ≤ n := ⟨λ h, bit0_le_bit1_iff.1 (le_of_lt h), nat.bit0_lt_bit1⟩ @[simp] lemma bit1_le_bit0_iff : bit1 k ≤ bit0 n ↔ k < n := ⟨λ h, by rwa [k.bit1_eq_succ_bit0, succ_le_iff, bit0_lt_bit0] at h, λ h, le_of_lt (nat.bit1_lt_bit0 h)⟩ @[simp] lemma bit1_lt_bit0_iff : bit1 k < bit0 n ↔ k < n := ⟨λ h, bit1_le_bit0_iff.1 (le_of_lt h), nat.bit1_lt_bit0⟩ @[simp] lemma one_le_bit0_iff : 1 ≤ bit0 n ↔ 0 < n := by { convert bit1_le_bit0_iff, refl, } @[simp] lemma one_lt_bit0_iff : 1 < bit0 n ↔ 1 ≤ n := by { convert bit1_lt_bit0_iff, refl, } @[simp] lemma bit_le_bit_iff : ∀ {b : bool}, bit b k ≤ bit b n ↔ k ≤ n | ff := bit0_le_bit0 | tt := bit1_le_bit1 @[simp] lemma bit_lt_bit_iff : ∀ {b : bool}, bit b k < bit b n ↔ k < n | ff := bit0_lt_bit0 | tt := bit1_lt_bit1 @[simp] lemma bit_le_bit1_iff : ∀ {b : bool}, bit b k ≤ bit1 n ↔ k ≤ n | ff := bit0_le_bit1_iff | tt := bit1_le_bit1 lemma pos_of_bit0_pos {n : ℕ} (h : 0 < bit0 n) : 0 < n := by { cases n, cases h, apply succ_pos, } /-- Define a function on `ℕ` depending on parity of the argument. -/ @[elab_as_eliminator] def bit_cases {C : ℕ → Sort u} (H : Π b n, C (bit b n)) (n : ℕ) : C n := eq.rec_on n.bit_decomp (H (bodd n) (div2 n)) /- partial subtraction -/ /-- Partial predecessor operation. Returns `ppred n = some m` if `n = m + 1`, otherwise `none`. -/ @[simp] def ppred : ℕ → option ℕ | 0 := none | (n+1) := some n /-- Partial subtraction operation. Returns `psub m n = some k` if `m = n + k`, otherwise `none`. -/ @[simp] def psub (m : ℕ) : ℕ → option ℕ | 0 := some m | (n+1) := psub n >>= ppred theorem pred_eq_ppred (n : ℕ) : pred n = (ppred n).get_or_else 0 := by cases n; refl theorem sub_eq_psub (m : ℕ) : ∀ n, m - n = (psub m n).get_or_else 0 | 0 := rfl | (n+1) := (pred_eq_ppred (m-n)).trans $ by rw [sub_eq_psub, psub]; cases psub m n; refl @[simp] theorem ppred_eq_some {m : ℕ} : ∀ {n}, ppred n = some m ↔ succ m = n | 0 := by split; intro h; contradiction | (n+1) := by dsimp; split; intro h; injection h; subst n @[simp] theorem ppred_eq_none : ∀ {n : ℕ}, ppred n = none ↔ n = 0 | 0 := by simp | (n+1) := by dsimp; split; contradiction theorem psub_eq_some {m : ℕ} : ∀ {n k}, psub m n = some k ↔ k + n = m | 0 k := by simp [eq_comm] | (n+1) k := begin dsimp, apply option.bind_eq_some.trans, simp [psub_eq_some, add_comm, add_left_comm, nat.succ_eq_add_one] end theorem psub_eq_none (m n : ℕ) : psub m n = none ↔ m < n := begin cases s : psub m n; simp [eq_comm], { show m < n, refine lt_of_not_ge (λ h, _), cases le.dest h with k e, injection s.symm.trans (psub_eq_some.2 $ (add_comm _ _).trans e) }, { show n ≤ m, rw ← psub_eq_some.1 s, apply le_add_left } end theorem ppred_eq_pred {n} (h : 0 < n) : ppred n = some (pred n) := ppred_eq_some.2 $ succ_pred_eq_of_pos h theorem psub_eq_sub {m n} (h : n ≤ m) : psub m n = some (m - n) := psub_eq_some.2 $ nat.sub_add_cancel h theorem psub_add (m n k) : psub m (n + k) = do x ← psub m n, psub x k := by induction k; simp [*, add_succ, bind_assoc] /- pow -/ attribute [simp] nat.pow_zero nat.pow_one @[simp] lemma one_pow : ∀ n : ℕ, 1 ^ n = 1 | 0 := rfl | (k+1) := show 1^k * 1 = 1, by rw [mul_one, one_pow] theorem pow_add (a m n : ℕ) : a^(m + n) = a^m * a^n := by induction n; simp [*, pow_succ, mul_assoc] theorem pow_two (a : ℕ) : a ^ 2 = a * a := show (1 * a) * a = _, by rw one_mul theorem pow_dvd_pow (a : ℕ) {m n : ℕ} (h : m ≤ n) : a^m ∣ a^n := by rw [← nat.add_sub_cancel' h, pow_add]; apply dvd_mul_right theorem pow_dvd_pow_of_dvd {a b : ℕ} (h : a ∣ b) : ∀ n:ℕ, a^n ∣ b^n | 0 := dvd_refl _ | (n+1) := mul_dvd_mul (pow_dvd_pow_of_dvd n) h theorem mul_pow (a b n : ℕ) : (a * b) ^ n = a ^ n * b ^ n := by induction n; simp [*, nat.pow_succ, mul_comm, mul_assoc, mul_left_comm] protected theorem pow_mul (a b n : ℕ) : n ^ (a * b) = (n ^ a) ^ b := by induction b; simp [*, nat.succ_eq_add_one, nat.pow_add, mul_add, mul_comm] theorem pow_pos {p : ℕ} (hp : 0 < p) : ∀ n : ℕ, 0 < p ^ n | 0 := by simp | (k+1) := mul_pos (pow_pos _) hp lemma pow_eq_mul_pow_sub (p : ℕ) {m n : ℕ} (h : m ≤ n) : p ^ m * p ^ (n - m) = p ^ n := by rw [←nat.pow_add, nat.add_sub_cancel' h] lemma pow_lt_pow_succ {p : ℕ} (h : 1 < p) (n : ℕ) : p^n < p^(n+1) := suffices p^n*1 < p^n*p, by simpa, nat.mul_lt_mul_of_pos_left h (nat.pow_pos (lt_of_succ_lt h) n) lemma lt_pow_self {p : ℕ} (h : 1 < p) : ∀ n : ℕ, n < p ^ n | 0 := by simp [zero_lt_one] | (n+1) := calc n + 1 < p^n + 1 : nat.add_lt_add_right (lt_pow_self _) _ ... ≤ p ^ (n+1) : pow_lt_pow_succ h _ lemma pow_right_strict_mono {x : ℕ} (k : 2 ≤ x) : strict_mono (nat.pow x) := λ _ _, pow_lt_pow_of_lt_right k lemma pow_le_iff_le_right {x m n : ℕ} (k : 2 ≤ x) : x^m ≤ x^n ↔ m ≤ n := strict_mono.le_iff_le (pow_right_strict_mono k) lemma pow_lt_iff_lt_right {x m n : ℕ} (k : 2 ≤ x) : x^m < x^n ↔ m < n := strict_mono.lt_iff_lt (pow_right_strict_mono k) lemma pow_right_injective {x : ℕ} (k : 2 ≤ x) : function.injective (nat.pow x) := strict_mono.injective (pow_right_strict_mono k) lemma pow_left_strict_mono {m : ℕ} (k : 1 ≤ m) : strict_mono (λ (x : ℕ), x^m) := λ _ _ h, pow_lt_pow_of_lt_left h k lemma pow_le_iff_le_left {m x y : ℕ} (k : 1 ≤ m) : x^m ≤ y^m ↔ x ≤ y := strict_mono.le_iff_le (pow_left_strict_mono k) lemma pow_lt_iff_lt_left {m x y : ℕ} (k : 1 ≤ m) : x^m < y^m ↔ x < y := strict_mono.lt_iff_lt (pow_left_strict_mono k) lemma pow_left_injective {m : ℕ} (k : 1 ≤ m) : function.injective (λ (x : ℕ), x^m) := strict_mono.injective (pow_left_strict_mono k) lemma not_pos_pow_dvd : ∀ {p k : ℕ} (hp : 1 < p) (hk : 1 < k), ¬ p^k ∣ p | (succ p) (succ k) hp hk h := have (succ p)^k * succ p ∣ 1 * succ p, by simpa, have (succ p) ^ k ∣ 1, from dvd_of_mul_dvd_mul_right (succ_pos _) this, have he : (succ p) ^ k = 1, from eq_one_of_dvd_one this, have k < (succ p) ^ k, from lt_pow_self hp k, have k < 1, by rwa [he] at this, have k = 0, from eq_zero_of_le_zero $ le_of_lt_succ this, have 1 < 1, by rwa [this] at hk, absurd this dec_trivial @[simp] theorem bodd_div2_eq (n : ℕ) : bodd_div2 n = (bodd n, div2 n) := by unfold bodd div2; cases bodd_div2 n; refl @[simp] lemma bodd_bit0 (n) : bodd (bit0 n) = ff := bodd_bit ff n @[simp] lemma bodd_bit1 (n) : bodd (bit1 n) = tt := bodd_bit tt n @[simp] lemma div2_bit0 (n) : div2 (bit0 n) = n := div2_bit ff n @[simp] lemma div2_bit1 (n) : div2 (bit1 n) = n := div2_bit tt n /- iterate -/ section variables {α : Sort*} (op : α → α) @[simp] theorem iterate_zero (a : α) : op^[0] a = a := rfl @[simp] theorem iterate_succ (n : ℕ) (a : α) : op^[succ n] a = (op^[n]) (op a) := rfl theorem iterate_add : ∀ (m n : ℕ) (a : α), op^[m + n] a = (op^[m]) (op^[n] a) | m 0 a := rfl | m (succ n) a := iterate_add m n _ theorem iterate_succ' (n : ℕ) (a : α) : op^[succ n] a = op (op^[n] a) := by rw [← one_add, iterate_add]; refl theorem iterate₀ {α : Type u} {op : α → α} {x : α} (H : op x = x) {n : ℕ} : op^[n] x = x := by induction n; [simp only [iterate_zero], simp only [iterate_succ', H, *]] theorem iterate₁ {α : Type u} {β : Type v} {op : α → α} {op' : β → β} {op'' : α → β} (H : ∀ x, op' (op'' x) = op'' (op x)) {n : ℕ} {x : α} : op'^[n] (op'' x) = op'' (op^[n] x) := by induction n; [simp only [iterate_zero], simp only [iterate_succ', H, *]] theorem iterate₂ {α : Type u} {op : α → α} {op' : α → α → α} (H : ∀ x y, op (op' x y) = op' (op x) (op y)) {n : ℕ} {x y : α} : op^[n] (op' x y) = op' (op^[n] x) (op^[n] y) := by induction n; [simp only [iterate_zero], simp only [iterate_succ', H, *]] theorem iterate_cancel {α : Type u} {op op' : α → α} (H : ∀ x, op (op' x) = x) {n : ℕ} {x : α} : op^[n] (op'^[n] x) = x := by induction n; [refl, rwa [iterate_succ, iterate_succ', H]] theorem iterate_inj {α : Type u} {op : α → α} (Hinj : function.injective op) (n : ℕ) (x y : α) (H : (op^[n] x) = (op^[n] y)) : x = y := by induction n with n ih; simp only [iterate_zero, iterate_succ'] at H; [exact H, exact ih (Hinj H)] end /- size and shift -/ theorem shiftl'_ne_zero_left (b) {m} (h : m ≠ 0) (n) : shiftl' b m n ≠ 0 := by induction n; simp [shiftl', bit_ne_zero, *] theorem shiftl'_tt_ne_zero (m) : ∀ {n} (h : n ≠ 0), shiftl' tt m n ≠ 0 | 0 h := absurd rfl h | (succ n) _ := nat.bit1_ne_zero _ @[simp] theorem size_zero : size 0 = 0 := rfl @[simp] theorem size_bit {b n} (h : bit b n ≠ 0) : size (bit b n) = succ (size n) := begin rw size, conv { to_lhs, rw [binary_rec], simp [h] }, rw div2_bit, end @[simp] theorem size_bit0 {n} (h : n ≠ 0) : size (bit0 n) = succ (size n) := @size_bit ff n (nat.bit0_ne_zero h) @[simp] theorem size_bit1 (n) : size (bit1 n) = succ (size n) := @size_bit tt n (nat.bit1_ne_zero n) @[simp] theorem size_one : size 1 = 1 := by apply size_bit1 0 @[simp] theorem size_shiftl' {b m n} (h : shiftl' b m n ≠ 0) : size (shiftl' b m n) = size m + n := begin induction n with n IH; simp [shiftl'] at h ⊢, rw [size_bit h, nat.add_succ], by_cases s0 : shiftl' b m n = 0; [skip, rw [IH s0]], rw s0 at h ⊢, cases b, {exact absurd rfl h}, have : shiftl' tt m n + 1 = 1 := congr_arg (+1) s0, rw [shiftl'_tt_eq_mul_pow] at this, have m0 := succ_inj (eq_one_of_dvd_one ⟨_, this.symm⟩), subst m0, simp at this, have : n = 0 := eq_zero_of_le_zero (le_of_not_gt $ λ hn, ne_of_gt (pow_lt_pow_of_lt_right dec_trivial hn) this), subst n, refl end @[simp] theorem size_shiftl {m} (h : m ≠ 0) (n) : size (shiftl m n) = size m + n := size_shiftl' (shiftl'_ne_zero_left _ h _) theorem lt_size_self (n : ℕ) : n < 2^size n := begin rw [← one_shiftl], have : ∀ {n}, n = 0 → n < shiftl 1 (size n) := λ n e, by subst e; exact dec_trivial, apply binary_rec _ _ n, {apply this rfl}, intros b n IH, by_cases bit b n = 0, {apply this h}, rw [size_bit h, shiftl_succ], exact bit_lt_bit0 _ IH end theorem size_le {m n : ℕ} : size m ≤ n ↔ m < 2^n := ⟨λ h, lt_of_lt_of_le (lt_size_self _) (pow_le_pow_of_le_right dec_trivial h), begin rw [← one_shiftl], revert n, apply binary_rec _ _ m, { intros n h, apply zero_le }, { intros b m IH n h, by_cases e : bit b m = 0, { rw e, apply zero_le }, rw [size_bit e], cases n with n, { exact e.elim (eq_zero_of_le_zero (le_of_lt_succ h)) }, { apply succ_le_succ (IH _), apply lt_imp_lt_of_le_imp_le (λ h', bit0_le_bit _ h') h } } end⟩ theorem lt_size {m n : ℕ} : m < size n ↔ 2^m ≤ n := by rw [← not_lt, iff_not_comm, not_lt, size_le] theorem size_pos {n : ℕ} : 0 < size n ↔ 0 < n := by rw lt_size; refl theorem size_eq_zero {n : ℕ} : size n = 0 ↔ n = 0 := by have := @size_pos n; simp [pos_iff_ne_zero] at this; exact not_iff_not.1 this theorem size_pow {n : ℕ} : size (2^n) = n+1 := le_antisymm (size_le.2 $ pow_lt_pow_of_lt_right dec_trivial (lt_succ_self _)) (lt_size.2 $ le_refl _) theorem size_le_size {m n : ℕ} (h : m ≤ n) : size m ≤ size n := size_le.2 $ lt_of_le_of_lt h (lt_size_self _) /- factorial -/ /-- `fact n` is the factorial of `n`. -/ @[simp] def fact : nat → nat | 0 := 1 | (succ n) := succ n * fact n @[simp] theorem fact_zero : fact 0 = 1 := rfl @[simp] theorem fact_succ (n) : fact (succ n) = succ n * fact n := rfl @[simp] theorem fact_one : fact 1 = 1 := rfl theorem fact_pos : ∀ n, 0 < fact n | 0 := zero_lt_one | (succ n) := mul_pos (succ_pos _) (fact_pos n) theorem fact_ne_zero (n : ℕ) : fact n ≠ 0 := ne_of_gt (fact_pos _) theorem fact_dvd_fact {m n} (h : m ≤ n) : fact m ∣ fact n := begin induction n with n IH; simp, { have := eq_zero_of_le_zero h, subst m, simp }, { cases eq_or_lt_of_le h with he hl, { subst m, simp }, { apply dvd_mul_of_dvd_right (IH (le_of_lt_succ hl)) } } end theorem dvd_fact : ∀ {m n}, 0 < m → m ≤ n → m ∣ fact n | (succ m) n _ h := dvd_of_mul_right_dvd (fact_dvd_fact h) theorem fact_le {m n} (h : m ≤ n) : fact m ≤ fact n := le_of_dvd (fact_pos _) (fact_dvd_fact h) lemma fact_mul_pow_le_fact : ∀ {m n : ℕ}, m.fact * m.succ ^ n ≤ (m + n).fact | m 0 := by simp | m (n+1) := by rw [← add_assoc, nat.fact_succ, mul_comm (nat.succ _), nat.pow_succ, ← mul_assoc]; exact mul_le_mul fact_mul_pow_le_fact (nat.succ_le_succ (nat.le_add_right _ _)) (nat.zero_le _) (nat.zero_le _) lemma monotone_fact : monotone fact := λ n m, fact_le lemma fact_lt (h0 : 0 < n) : n.fact < m.fact ↔ n < m := begin split; intro h, { rw [← not_le], intro hmn, apply not_le_of_lt h (fact_le hmn) }, { have : ∀(n : ℕ), 0 < n → n.fact < n.succ.fact, { intros k hk, rw [fact_succ, succ_mul, lt_add_iff_pos_left], apply mul_pos hk (fact_pos k) }, induction h generalizing h0, { exact this _ h0, }, { refine lt_trans (h_ih h0) (this _ _), exact lt_trans h0 (lt_of_succ_le h_a) }} end lemma one_lt_fact : 1 < n.fact ↔ 1 < n := by { convert fact_lt _, refl, exact one_pos } lemma fact_eq_one : n.fact = 1 ↔ n ≤ 1 := begin split; intro h, { rw [← not_lt, ← one_lt_fact, h], apply lt_irrefl }, { cases h with h h, refl, cases h, refl } end lemma fact_inj (h0 : 1 < n.fact) : n.fact = m.fact ↔ n = m := begin split; intro h, { rcases lt_trichotomy n m with hnm|hnm|hnm, { exfalso, rw [← fact_lt, h] at hnm, exact lt_irrefl _ hnm, rw [one_lt_fact] at h0, exact lt_trans one_pos h0 }, { exact hnm }, { exfalso, rw [← fact_lt, h] at hnm, exact lt_irrefl _ hnm, rw [h, one_lt_fact] at h0, exact lt_trans one_pos h0 }}, { rw h } end /- choose -/ /-- `choose n k` is the number of `k`-element subsets in an `n`-element set. Also known as binomial coefficients. -/ def choose : ℕ → ℕ → ℕ | _ 0 := 1 | 0 (k + 1) := 0 | (n + 1) (k + 1) := choose n k + choose n (k + 1) @[simp] lemma choose_zero_right (n : ℕ) : choose n 0 = 1 := by cases n; refl @[simp] lemma choose_zero_succ (k : ℕ) : choose 0 (succ k) = 0 := rfl lemma choose_succ_succ (n k : ℕ) : choose (succ n) (succ k) = choose n k + choose n (succ k) := rfl lemma choose_eq_zero_of_lt : ∀ {n k}, n < k → choose n k = 0 | _ 0 hk := absurd hk dec_trivial | 0 (k + 1) hk := choose_zero_succ _ | (n + 1) (k + 1) hk := have hnk : n < k, from lt_of_succ_lt_succ hk, have hnk1 : n < k + 1, from lt_of_succ_lt hk, by rw [choose_succ_succ, choose_eq_zero_of_lt hnk, choose_eq_zero_of_lt hnk1] @[simp] lemma choose_self (n : ℕ) : choose n n = 1 := by induction n; simp [*, choose, choose_eq_zero_of_lt (lt_succ_self _)] @[simp] lemma choose_succ_self (n : ℕ) : choose n (succ n) = 0 := choose_eq_zero_of_lt (lt_succ_self _) @[simp] lemma choose_one_right (n : ℕ) : choose n 1 = n := by induction n; simp [*, choose, add_comm] /-- `choose n 2` is the `n`-th triangle number. -/ lemma choose_two_right (n : ℕ) : choose n 2 = n * (n - 1) / 2 := begin induction n with n ih, simp, {rw triangle_succ n, simp [choose, ih], rw add_comm}, end lemma choose_pos : ∀ {n k}, k ≤ n → 0 < choose n k | 0 _ hk := by rw [eq_zero_of_le_zero hk]; exact dec_trivial | (n + 1) 0 hk := by simp; exact dec_trivial | (n + 1) (k + 1) hk := by rw choose_succ_succ; exact add_pos_of_pos_of_nonneg (choose_pos (le_of_succ_le_succ hk)) (nat.zero_le _) lemma succ_mul_choose_eq : ∀ n k, succ n * choose n k = choose (succ n) (succ k) * succ k | 0 0 := dec_trivial | 0 (k + 1) := by simp [choose] | (n + 1) 0 := by simp | (n + 1) (k + 1) := by rw [choose_succ_succ (succ n) (succ k), add_mul, ←succ_mul_choose_eq, mul_succ, ←succ_mul_choose_eq, add_right_comm, ←mul_add, ←choose_succ_succ, ←succ_mul] lemma choose_mul_fact_mul_fact : ∀ {n k}, k ≤ n → choose n k * fact k * fact (n - k) = fact n | 0 _ hk := by simp [eq_zero_of_le_zero hk] | (n + 1) 0 hk := by simp | (n + 1) (succ k) hk := begin cases lt_or_eq_of_le hk with hk₁ hk₁, { have h : choose n k * fact (succ k) * fact (n - k) = succ k * fact n := by rw ← choose_mul_fact_mul_fact (le_of_succ_le_succ hk); simp [fact_succ, mul_comm, mul_left_comm], have h₁ : fact (n - k) = (n - k) * fact (n - succ k) := by rw [← succ_sub_succ, succ_sub (le_of_lt_succ hk₁), fact_succ], have h₂ : choose n (succ k) * fact (succ k) * ((n - k) * fact (n - succ k)) = (n - k) * fact n := by rw ← choose_mul_fact_mul_fact (le_of_lt_succ hk₁); simp [fact_succ, mul_comm, mul_left_comm, mul_assoc], have h₃ : k * fact n ≤ n * fact n := mul_le_mul_right _ (le_of_succ_le_succ hk), rw [choose_succ_succ, add_mul, add_mul, succ_sub_succ, h, h₁, h₂, ← add_one, add_mul, nat.mul_sub_right_distrib, fact_succ, ← nat.add_sub_assoc h₃, add_assoc, ← add_mul, nat.add_sub_cancel_left, add_comm] }, { simp [hk₁, mul_comm, choose, nat.sub_self] } end theorem choose_eq_fact_div_fact {n k : ℕ} (hk : k ≤ n) : choose n k = fact n / (fact k * fact (n - k)) := begin have : fact n = choose n k * (fact k * fact (n - k)) := by rw ← mul_assoc; exact (choose_mul_fact_mul_fact hk).symm, exact (nat.div_eq_of_eq_mul_left (mul_pos (fact_pos _) (fact_pos _)) this).symm end theorem fact_mul_fact_dvd_fact {n k : ℕ} (hk : k ≤ n) : fact k * fact (n - k) ∣ fact n := by rw [←choose_mul_fact_mul_fact hk, mul_assoc]; exact dvd_mul_left _ _ @[simp] lemma choose_symm {n k : ℕ} (hk : k ≤ n) : choose n (n-k) = choose n k := by rw [choose_eq_fact_div_fact hk, choose_eq_fact_div_fact (sub_le _ _), nat.sub_sub_self hk, mul_comm] lemma choose_succ_right_eq (n k : ℕ) : choose n (k + 1) * (k + 1) = choose n k * (n - k) := begin have e : (n+1) * choose n k = choose n k * (k+1) + choose n (k+1) * (k+1), rw [← right_distrib, ← choose_succ_succ, succ_mul_choose_eq], rw [← nat.sub_eq_of_eq_add e, mul_comm, ← nat.mul_sub_left_distrib, nat.add_sub_add_right] end @[simp] lemma choose_succ_self_right : ∀ (n:ℕ), (n+1).choose n = n+1 | 0 := rfl | (n+1) := by rw [choose_succ_succ, choose_succ_self_right, choose_self] lemma choose_mul_succ_eq (n k : ℕ) : (n.choose k) * (n + 1) = ((n+1).choose k) * (n + 1 - k) := begin induction k with k ih, { simp }, by_cases hk : n < k + 1, { rw [choose_eq_zero_of_lt hk, sub_eq_zero_of_le hk, zero_mul, mul_zero] }, push_neg at hk, replace hk : k + 1 ≤ n + 1 := _root_.le_add_right hk, rw [choose_succ_succ], rw [add_mul, succ_sub_succ], rw [← choose_succ_right_eq], rw [← succ_sub_succ, nat.mul_sub_left_distrib], symmetry, apply nat.add_sub_cancel', exact mul_le_mul_left _ hk, end section find_greatest /-- `find_greatest P b` is the largest `i ≤ bound` such that `P i` holds, or `0` if no such `i` exists -/ protected def find_greatest (P : ℕ → Prop) [decidable_pred P] : ℕ → ℕ | 0 := 0 | (n + 1) := if P (n + 1) then n + 1 else find_greatest n variables {P : ℕ → Prop} [decidable_pred P] @[simp] lemma find_greatest_zero : nat.find_greatest P 0 = 0 := rfl @[simp] lemma find_greatest_eq : ∀{b}, P b → nat.find_greatest P b = b | 0 h := rfl | (n + 1) h := by simp [nat.find_greatest, h] @[simp] lemma find_greatest_of_not {b} (h : ¬ P (b + 1)) : nat.find_greatest P (b + 1) = nat.find_greatest P b := by simp [nat.find_greatest, h] lemma find_greatest_spec_and_le : ∀{b m}, m ≤ b → P m → P (nat.find_greatest P b) ∧ m ≤ nat.find_greatest P b | 0 m hm hP := have m = 0, from le_antisymm hm (nat.zero_le _), show P 0 ∧ m ≤ 0, from this ▸ ⟨hP, le_refl _⟩ | (b + 1) m hm hP := begin by_cases h : P (b + 1), { simp [h, hm] }, { have : m ≠ b + 1 := assume this, h $ this ▸ hP, have : m ≤ b := (le_of_not_gt $ assume h : b + 1 ≤ m, this $ le_antisymm hm h), have : P (nat.find_greatest P b) ∧ m ≤ nat.find_greatest P b := find_greatest_spec_and_le this hP, simp [h, this] } end lemma find_greatest_spec {b} : (∃m, m ≤ b ∧ P m) → P (nat.find_greatest P b) | ⟨m, hmb, hm⟩ := (find_greatest_spec_and_le hmb hm).1 lemma find_greatest_le : ∀ {b}, nat.find_greatest P b ≤ b | 0 := le_refl _ | (b + 1) := have nat.find_greatest P b ≤ b + 1, from le_trans find_greatest_le (nat.le_succ b), by by_cases P (b + 1); simp [h, this] lemma le_find_greatest {b m} (hmb : m ≤ b) (hm : P m) : m ≤ nat.find_greatest P b := (find_greatest_spec_and_le hmb hm).2 lemma find_greatest_is_greatest {P : ℕ → Prop} [decidable_pred P] {b} : (∃ m, m ≤ b ∧ P m) → ∀ k, nat.find_greatest P b < k ∧ k ≤ b → ¬ P k | ⟨m, hmb, hP⟩ k ⟨hk, hkb⟩ hPk := lt_irrefl k $ lt_of_le_of_lt (le_find_greatest hkb hPk) hk lemma find_greatest_eq_zero {P : ℕ → Prop} [decidable_pred P] : ∀ {b}, (∀ n ≤ b, ¬ P n) → nat.find_greatest P b = 0 | 0 h := find_greatest_zero | (n + 1) h := begin have := nat.find_greatest_of_not (h (n + 1) (le_refl _)), rw this, exact find_greatest_eq_zero (assume k hk, h k (le_trans hk $ nat.le_succ _)) end lemma find_greatest_of_ne_zero {P : ℕ → Prop} [decidable_pred P] : ∀ {b m}, nat.find_greatest P b = m → m ≠ 0 → P m | 0 m rfl h := by { have := @find_greatest_zero P _, contradiction } | (b + 1) m rfl h := decidable.by_cases (assume hb : P (b + 1), by { have := find_greatest_eq hb, rw this, exact hb }) (assume hb : ¬ P (b + 1), find_greatest_of_ne_zero (find_greatest_of_not hb).symm h) end find_greatest section div lemma dvd_div_of_mul_dvd {a b c : ℕ} (h : a * b ∣ c) : b ∣ c / a := if ha : a = 0 then by simp [ha] else have ha : 0 < a, from nat.pos_of_ne_zero ha, have h1 : ∃ d, c = a * b * d, from h, let ⟨d, hd⟩ := h1 in have hac : a ∣ c, from dvd_of_mul_right_dvd h, have h2 : c / a = b * d, from nat.div_eq_of_eq_mul_right ha (by simpa [mul_assoc] using hd), show ∃ d, c / a = b * d, from ⟨d, h2⟩ lemma mul_dvd_of_dvd_div {a b c : ℕ} (hab : c ∣ b) (h : a ∣ b / c) : c * a ∣ b := have h1 : ∃ d, b / c = a * d, from h, have h2 : ∃ e, b = c * e, from hab, let ⟨d, hd⟩ := h1, ⟨e, he⟩ := h2 in have h3 : b = a * d * c, from nat.eq_mul_of_div_eq_left hab hd, show ∃ d, b = c * a * d, from ⟨d, by cc⟩ lemma div_mul_div {a b c d : ℕ} (hab : b ∣ a) (hcd : d ∣ c) : (a / b) * (c / d) = (a * c) / (b * d) := have exi1 : ∃ x, a = b * x, from hab, have exi2 : ∃ y, c = d * y, from hcd, if hb : b = 0 then by simp [hb] else have 0 < b, from nat.pos_of_ne_zero hb, if hd : d = 0 then by simp [hd] else have 0 < d, from nat.pos_of_ne_zero hd, begin cases exi1 with x hx, cases exi2 with y hy, rw [hx, hy, nat.mul_div_cancel_left, nat.mul_div_cancel_left], symmetry, apply nat.div_eq_of_eq_mul_left, apply mul_pos, repeat {assumption}, cc end lemma pow_dvd_of_le_of_pow_dvd {p m n k : ℕ} (hmn : m ≤ n) (hdiv : p ^ n ∣ k) : p ^ m ∣ k := have p ^ m ∣ p ^ n, from pow_dvd_pow _ hmn, dvd_trans this hdiv lemma dvd_of_pow_dvd {p k m : ℕ} (hk : 1 ≤ k) (hpk : p^k ∣ m) : p ∣ m := by rw ←nat.pow_one p; exact pow_dvd_of_le_of_pow_dvd hk hpk lemma eq_of_dvd_of_div_eq_one {a b : ℕ} (w : a ∣ b) (h : b / a = 1) : a = b := by rw [←nat.div_mul_cancel w, h, one_mul] lemma eq_zero_of_dvd_of_div_eq_zero {a b : ℕ} (w : a ∣ b) (h : b / a = 0) : b = 0 := by rw [←nat.div_mul_cancel w, h, zero_mul] lemma div_le_div_left {a b c : ℕ} (h₁ : c ≤ b) (h₂ : 0 < c) : a / b ≤ a / c := (nat.le_div_iff_mul_le _ _ h₂).2 $ le_trans (mul_le_mul_left _ h₁) (div_mul_le_self _ _) lemma div_eq_self {a b : ℕ} : a / b = a ↔ a = 0 ∨ b = 1 := begin split, { intro, cases b, { simp * at * }, { cases b, { right, refl }, { left, have : a / (b + 2) ≤ a / 2 := div_le_div_left (by simp) dec_trivial, refine eq_zero_of_le_half _, simp * at * } } }, { rintros (rfl|rfl); simp } end end div lemma exists_eq_add_of_le : ∀ {m n : ℕ}, m ≤ n → ∃ k : ℕ, n = m + k | 0 0 h := ⟨0, by simp⟩ | 0 (n+1) h := ⟨n+1, by simp⟩ | (m+1) (n+1) h := let ⟨k, hk⟩ := exists_eq_add_of_le (nat.le_of_succ_le_succ h) in ⟨k, by simp [hk, add_comm, add_left_comm]⟩ lemma exists_eq_add_of_lt : ∀ {m n : ℕ}, m < n → ∃ k : ℕ, n = m + k + 1 | 0 0 h := false.elim $ lt_irrefl _ h | 0 (n+1) h := ⟨n, by simp⟩ | (m+1) (n+1) h := let ⟨k, hk⟩ := exists_eq_add_of_le (nat.le_of_succ_le_succ h) in ⟨k, by simp [hk]⟩ lemma with_bot.add_eq_zero_iff : ∀ {n m : with_bot ℕ}, n + m = 0 ↔ n = 0 ∧ m = 0 | none m := iff_of_false dec_trivial (λ h, absurd h.1 dec_trivial) | n none := iff_of_false (by cases n; exact dec_trivial) (λ h, absurd h.2 dec_trivial) | (some n) (some m) := show (n + m : with_bot ℕ) = (0 : ℕ) ↔ (n : with_bot ℕ) = (0 : ℕ) ∧ (m : with_bot ℕ) = (0 : ℕ), by rw [← with_bot.coe_add, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, add_eq_zero_iff' (nat.zero_le _) (nat.zero_le _)] lemma with_bot.add_eq_one_iff : ∀ {n m : with_bot ℕ}, n + m = 1 ↔ (n = 0 ∧ m = 1) ∨ (n = 1 ∧ m = 0) | none none := dec_trivial | none (some m) := dec_trivial | (some n) none := iff_of_false dec_trivial (λ h, h.elim (λ h, absurd h.2 dec_trivial) (λ h, absurd h.2 dec_trivial)) | (some n) (some 0) := by erw [with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe]; simp | (some n) (some (m + 1)) := by erw [with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe]; simp [nat.add_succ, nat.succ_inj', nat.succ_ne_zero] -- induction /-- Induction principle starting at a non-zero number. For maps to a `Sort*` see `le_rec_on`. -/ @[elab_as_eliminator] lemma le_induction {P : nat → Prop} {m} (h0 : P m) (h1 : ∀ n, m ≤ n → P n → P (n + 1)) : ∀ n, m ≤ n → P n := by apply nat.less_than_or_equal.rec h0; exact h1 /-- Decreasing induction: if `P (k+1)` implies `P k`, then `P n` implies `P m` for all `m ≤ n`. Also works for functions to `Sort*`. -/ @[elab_as_eliminator] def decreasing_induction {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {m n : ℕ} (mn : m ≤ n) (hP : P n) : P m := le_rec_on mn (λ k ih hsk, ih $ h k hsk) (λ h, h) hP @[simp] lemma decreasing_induction_self {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {n : ℕ} (nn : n ≤ n) (hP : P n) : (decreasing_induction h nn hP : P n) = hP := by { dunfold decreasing_induction, rw [le_rec_on_self] } lemma decreasing_induction_succ {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {m n : ℕ} (mn : m ≤ n) (msn : m ≤ n + 1) (hP : P (n+1)) : (decreasing_induction h msn hP : P m) = decreasing_induction h mn (h n hP) := by { dunfold decreasing_induction, rw [le_rec_on_succ] } @[simp] lemma decreasing_induction_succ' {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {m : ℕ} (msm : m ≤ m + 1) (hP : P (m+1)) : (decreasing_induction h msm hP : P m) = h m hP := by { dunfold decreasing_induction, rw [le_rec_on_succ'] } lemma decreasing_induction_trans {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {m n k : ℕ} (mn : m ≤ n) (nk : n ≤ k) (hP : P k) : (decreasing_induction h (le_trans mn nk) hP : P m) = decreasing_induction h mn (decreasing_induction h nk hP) := by { induction nk with k nk ih, rw [decreasing_induction_self], rw [decreasing_induction_succ h (le_trans mn nk), ih, decreasing_induction_succ] } lemma decreasing_induction_succ_left {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {m n : ℕ} (smn : m + 1 ≤ n) (mn : m ≤ n) (hP : P n) : (decreasing_induction h mn hP : P m) = h m (decreasing_induction h smn hP) := by { rw [subsingleton.elim mn (le_trans (le_succ m) smn), decreasing_induction_trans, decreasing_induction_succ'] } end nat namespace monoid_hom variables {M : Type*} {G : Type*} [monoid M] [group G] @[simp, to_additive] theorem iterate_map_one (f : M →* M) (n : ℕ) : f^[n] 1 = 1 := nat.iterate₀ f.map_one @[simp, to_additive] theorem iterate_map_mul (f : M →* M) (n : ℕ) (x y) : f^[n] (x * y) = (f^[n] x) * (f^[n] y) := nat.iterate₂ f.map_mul @[simp, to_additive] theorem iterate_map_inv (f : G →* G) (n : ℕ) (x) : f^[n] (x⁻¹) = (f^[n] x)⁻¹ := nat.iterate₁ f.map_inv @[simp] theorem iterate_map_sub {A : Type*} [add_group A] (f : A →+ A) (n : ℕ) (x y) : f^[n] (x - y) = (f^[n] x) - (f^[n] y) := nat.iterate₂ f.map_sub end monoid_hom namespace ring_hom variables {R : Type*} [semiring R] {S : Type*} [ring S] @[simp] theorem iterate_map_one (f : R →+* R) (n : ℕ) : f^[n] 1 = 1 := nat.iterate₀ f.map_one @[simp] theorem iterate_map_zero (f : R →+* R) (n : ℕ) : f^[n] 0 = 0 := nat.iterate₀ f.map_zero @[simp] theorem iterate_map_mul (f : R →+* R) (n : ℕ) (x y) : f^[n] (x * y) = (f^[n] x) * (f^[n] y) := nat.iterate₂ f.map_mul @[simp] theorem iterate_map_add (f : R →+* R) (n : ℕ) (x y) : f^[n] (x + y) = (f^[n] x) + (f^[n] y) := nat.iterate₂ f.map_add @[simp] theorem iterate_map_neg (f : S →+* S) (n : ℕ) (x) : f^[n] (-x) = -(f^[n] x) := nat.iterate₁ f.map_neg @[simp] theorem iterate_map_sub (f : S →+* S) (n : ℕ) (x y) : f^[n] (x - y) = (f^[n] x) - (f^[n] y) := nat.iterate₂ f.map_sub end ring_hom
171f62a5872a8bfb296080d1d428934c37c14cd1
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/linear_algebra/affine_space/combination.lean
146882b11c8a866ec865711e56ed0feb90ad6c88
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
30,044
lean
/- Copyright (c) 2020 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Joseph Myers. -/ import algebra.invertible import data.indicator_function import linear_algebra.affine_space.basic import linear_algebra.finsupp noncomputable theory open_locale big_operators open_locale classical /-! # Affine combinations of points This file defines affine combinations of points. ## Main definitions * `weighted_vsub_of_point` is a general weighted combination of subtractions with an explicit base point, yielding a vector. * `weighted_vsub` uses an arbitrary choice of base point and is intended to be used when the sum of weights is 0, in which case the result is independent of the choice of base point. * `affine_combination` adds the weighted combination to the arbitrary base point, yielding a point rather than a vector, and is intended to be used when the sum of weights is 1, in which case the result is independent of the choice of base point. These definitions are for sums over a `finset`; versions for a `fintype` may be obtained using `finset.univ`, while versions for a `finsupp` may be obtained using `finsupp.support`. ## References * https://en.wikipedia.org/wiki/Affine_space -/ namespace finset variables {k : Type*} {V : Type*} {P : Type*} [ring k] [add_comm_group V] [module k V] variables [S : affine_space V P] include S variables {ι : Type*} (s : finset ι) variables {ι₂ : Type*} (s₂ : finset ι₂) /-- A weighted sum of the results of subtracting a base point from the given points, as a linear map on the weights. The main cases of interest are where the sum of the weights is 0, in which case the sum is independent of the choice of base point, and where the sum of the weights is 1, in which case the sum added to the base point is independent of the choice of base point. -/ def weighted_vsub_of_point (p : ι → P) (b : P) : (ι → k) →ₗ[k] V := ∑ i in s, (linear_map.proj i : (ι → k) →ₗ[k] k).smul_right (p i -ᵥ b) @[simp] lemma weighted_vsub_of_point_apply (w : ι → k) (p : ι → P) (b : P) : s.weighted_vsub_of_point p b w = ∑ i in s, w i • (p i -ᵥ b) := by simp [weighted_vsub_of_point, linear_map.sum_apply] /-- The weighted sum is independent of the base point when the sum of the weights is 0. -/ lemma weighted_vsub_of_point_eq_of_sum_eq_zero (w : ι → k) (p : ι → P) (h : ∑ i in s, w i = 0) (b₁ b₂ : P) : s.weighted_vsub_of_point p b₁ w = s.weighted_vsub_of_point p b₂ w := begin apply eq_of_sub_eq_zero, rw [weighted_vsub_of_point_apply, weighted_vsub_of_point_apply, ←sum_sub_distrib], conv_lhs { congr, skip, funext, rw [←smul_sub, vsub_sub_vsub_cancel_left] }, rw [←sum_smul, h, zero_smul] end /-- The weighted sum, added to the base point, is independent of the base point when the sum of the weights is 1. -/ lemma weighted_vsub_of_point_vadd_eq_of_sum_eq_one (w : ι → k) (p : ι → P) (h : ∑ i in s, w i = 1) (b₁ b₂ : P) : s.weighted_vsub_of_point p b₁ w +ᵥ b₁ = s.weighted_vsub_of_point p b₂ w +ᵥ b₂ := begin erw [weighted_vsub_of_point_apply, weighted_vsub_of_point_apply, ←@vsub_eq_zero_iff_eq V, vadd_vsub_assoc, vsub_vadd_eq_vsub_sub, ←add_sub_assoc, add_comm, add_sub_assoc, ←sum_sub_distrib], conv_lhs { congr, skip, congr, skip, funext, rw [←smul_sub, vsub_sub_vsub_cancel_left] }, rw [←sum_smul, h, one_smul, vsub_add_vsub_cancel, vsub_self] end /-- The weighted sum is unaffected by removing the base point, if present, from the set of points. -/ @[simp] lemma weighted_vsub_of_point_erase (w : ι → k) (p : ι → P) (i : ι) : (s.erase i).weighted_vsub_of_point p (p i) w = s.weighted_vsub_of_point p (p i) w := begin rw [weighted_vsub_of_point_apply, weighted_vsub_of_point_apply], apply sum_erase, rw [vsub_self, smul_zero] end /-- The weighted sum is unaffected by adding the base point, whether or not present, to the set of points. -/ @[simp] lemma weighted_vsub_of_point_insert (w : ι → k) (p : ι → P) (i : ι) : (insert i s).weighted_vsub_of_point p (p i) w = s.weighted_vsub_of_point p (p i) w := begin rw [weighted_vsub_of_point_apply, weighted_vsub_of_point_apply], apply sum_insert_zero, rw [vsub_self, smul_zero] end /-- The weighted sum is unaffected by changing the weights to the corresponding indicator function and adding points to the set. -/ lemma weighted_vsub_of_point_indicator_subset (w : ι → k) (p : ι → P) (b : P) {s₁ s₂ : finset ι} (h : s₁ ⊆ s₂) : s₁.weighted_vsub_of_point p b w = s₂.weighted_vsub_of_point p b (set.indicator ↑s₁ w) := begin rw [weighted_vsub_of_point_apply, weighted_vsub_of_point_apply], exact set.sum_indicator_subset_of_eq_zero w (λ i wi, wi • (p i -ᵥ b : V)) h (λ i, zero_smul k _) end /-- A weighted sum, over the image of an embedding, equals a weighted sum with the same points and weights over the original `finset`. -/ lemma weighted_vsub_of_point_map (e : ι₂ ↪ ι) (w : ι → k) (p : ι → P) (b : P) : (s₂.map e).weighted_vsub_of_point p b w = s₂.weighted_vsub_of_point (p ∘ e) b (w ∘ e) := begin simp_rw [weighted_vsub_of_point_apply], exact finset.sum_map _ _ _ end /-- A weighted sum of the results of subtracting a default base point from the given points, as a linear map on the weights. This is intended to be used when the sum of the weights is 0; that condition is specified as a hypothesis on those lemmas that require it. -/ def weighted_vsub (p : ι → P) : (ι → k) →ₗ[k] V := s.weighted_vsub_of_point p (classical.choice S.nonempty) /-- Applying `weighted_vsub` with given weights. This is for the case where a result involving a default base point is OK (for example, when that base point will cancel out later); a more typical use case for `weighted_vsub` would involve selecting a preferred base point with `weighted_vsub_eq_weighted_vsub_of_point_of_sum_eq_zero` and then using `weighted_vsub_of_point_apply`. -/ lemma weighted_vsub_apply (w : ι → k) (p : ι → P) : s.weighted_vsub p w = ∑ i in s, w i • (p i -ᵥ (classical.choice S.nonempty)) := by simp [weighted_vsub, linear_map.sum_apply] /-- `weighted_vsub` gives the sum of the results of subtracting any base point, when the sum of the weights is 0. -/ lemma weighted_vsub_eq_weighted_vsub_of_point_of_sum_eq_zero (w : ι → k) (p : ι → P) (h : ∑ i in s, w i = 0) (b : P) : s.weighted_vsub p w = s.weighted_vsub_of_point p b w := s.weighted_vsub_of_point_eq_of_sum_eq_zero w p h _ _ /-- The `weighted_vsub` for an empty set is 0. -/ @[simp] lemma weighted_vsub_empty (w : ι → k) (p : ι → P) : (∅ : finset ι).weighted_vsub p w = (0:V) := by simp [weighted_vsub_apply] /-- The weighted sum is unaffected by changing the weights to the corresponding indicator function and adding points to the set. -/ lemma weighted_vsub_indicator_subset (w : ι → k) (p : ι → P) {s₁ s₂ : finset ι} (h : s₁ ⊆ s₂) : s₁.weighted_vsub p w = s₂.weighted_vsub p (set.indicator ↑s₁ w) := weighted_vsub_of_point_indicator_subset _ _ _ h /-- A weighted subtraction, over the image of an embedding, equals a weighted subtraction with the same points and weights over the original `finset`. -/ lemma weighted_vsub_map (e : ι₂ ↪ ι) (w : ι → k) (p : ι → P) : (s₂.map e).weighted_vsub p w = s₂.weighted_vsub (p ∘ e) (w ∘ e) := s₂.weighted_vsub_of_point_map _ _ _ _ /-- A weighted sum of the results of subtracting a default base point from the given points, added to that base point, as an affine map on the weights. This is intended to be used when the sum of the weights is 1, in which case it is an affine combination (barycenter) of the points with the given weights; that condition is specified as a hypothesis on those lemmas that require it. -/ def affine_combination (p : ι → P) : affine_map k (ι → k) P := { to_fun := λ w, s.weighted_vsub_of_point p (classical.choice S.nonempty) w +ᵥ (classical.choice S.nonempty), linear := s.weighted_vsub p, map_vadd' := λ w₁ w₂, by simp_rw [vadd_assoc, weighted_vsub, vadd_eq_add, linear_map.map_add] } /-- The linear map corresponding to `affine_combination` is `weighted_vsub`. -/ @[simp] lemma affine_combination_linear (p : ι → P) : (s.affine_combination p : affine_map k (ι → k) P).linear = s.weighted_vsub p := rfl /-- Applying `affine_combination` with given weights. This is for the case where a result involving a default base point is OK (for example, when that base point will cancel out later); a more typical use case for `affine_combination` would involve selecting a preferred base point with `affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one` and then using `weighted_vsub_of_point_apply`. -/ lemma affine_combination_apply (w : ι → k) (p : ι → P) : s.affine_combination p w = s.weighted_vsub_of_point p (classical.choice S.nonempty) w +ᵥ (classical.choice S.nonempty) := rfl /-- `affine_combination` gives the sum with any base point, when the sum of the weights is 1. -/ lemma affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one (w : ι → k) (p : ι → P) (h : ∑ i in s, w i = 1) (b : P) : s.affine_combination p w = s.weighted_vsub_of_point p b w +ᵥ b := s.weighted_vsub_of_point_vadd_eq_of_sum_eq_one w p h _ _ /-- Adding a `weighted_vsub` to an `affine_combination`. -/ lemma weighted_vsub_vadd_affine_combination (w₁ w₂ : ι → k) (p : ι → P) : s.weighted_vsub p w₁ +ᵥ s.affine_combination p w₂ = s.affine_combination p (w₁ + w₂) := by rw [←vadd_eq_add, affine_map.map_vadd, affine_combination_linear] /-- Subtracting two `affine_combination`s. -/ lemma affine_combination_vsub (w₁ w₂ : ι → k) (p : ι → P) : s.affine_combination p w₁ -ᵥ s.affine_combination p w₂ = s.weighted_vsub p (w₁ - w₂) := by rw [←affine_map.linear_map_vsub, affine_combination_linear, vsub_eq_sub] /-- An `affine_combination` equals a point if that point is in the set and has weight 1 and the other points in the set have weight 0. -/ @[simp] lemma affine_combination_of_eq_one_of_eq_zero (w : ι → k) (p : ι → P) {i : ι} (his : i ∈ s) (hwi : w i = 1) (hw0 : ∀ i2 ∈ s, i2 ≠ i → w i2 = 0) : s.affine_combination p w = p i := begin have h1 : ∑ i in s, w i = 1 := hwi ▸ sum_eq_single i hw0 (λ h, false.elim (h his)), rw [s.affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one w p h1 (p i), weighted_vsub_of_point_apply], convert zero_vadd V (p i), convert sum_eq_zero _, intros i2 hi2, by_cases h : i2 = i, { simp [h] }, { simp [hw0 i2 hi2 h] } end /-- An affine combination is unaffected by changing the weights to the corresponding indicator function and adding points to the set. -/ lemma affine_combination_indicator_subset (w : ι → k) (p : ι → P) {s₁ s₂ : finset ι} (h : s₁ ⊆ s₂) : s₁.affine_combination p w = s₂.affine_combination p (set.indicator ↑s₁ w) := by rw [affine_combination_apply, affine_combination_apply, weighted_vsub_of_point_indicator_subset _ _ _ h] /-- An affine combination, over the image of an embedding, equals an affine combination with the same points and weights over the original `finset`. -/ lemma affine_combination_map (e : ι₂ ↪ ι) (w : ι → k) (p : ι → P) : (s₂.map e).affine_combination p w = s₂.affine_combination (p ∘ e) (w ∘ e) := by simp_rw [affine_combination_apply, weighted_vsub_of_point_map] variables {V} /-- Suppose an indexed family of points is given, along with a subset of the index type. A vector can be expressed as `weighted_vsub_of_point` using a `finset` lying within that subset and with a given sum of weights if and only if it can be expressed as `weighted_vsub_of_point` with that sum of weights for the corresponding indexed family whose index type is the subtype corresponding to that subset. -/ lemma eq_weighted_vsub_of_point_subset_iff_eq_weighted_vsub_of_point_subtype {v : V} {x : k} {s : set ι} {p : ι → P} {b : P} : (∃ (fs : finset ι) (hfs : ↑fs ⊆ s) (w : ι → k) (hw : ∑ i in fs, w i = x), v = fs.weighted_vsub_of_point p b w) ↔ ∃ (fs : finset s) (w : s → k) (hw : ∑ i in fs, w i = x), v = fs.weighted_vsub_of_point (λ (i : s), p i) b w := begin simp_rw weighted_vsub_of_point_apply, split, { rintros ⟨fs, hfs, w, rfl, rfl⟩, use [fs.subtype s, λ i, w i, sum_subtype_of_mem _ hfs, (sum_subtype_of_mem _ hfs).symm] }, { rintros ⟨fs, w, rfl, rfl⟩, refine ⟨fs.map (function.embedding.subtype _), map_subtype_subset _, λ i, if h : i ∈ s then w ⟨i, h⟩ else 0, _, _⟩; simp } end variables (k) /-- Suppose an indexed family of points is given, along with a subset of the index type. A vector can be expressed as `weighted_vsub` using a `finset` lying within that subset and with sum of weights 0 if and only if it can be expressed as `weighted_vsub` with sum of weights 0 for the corresponding indexed family whose index type is the subtype corresponding to that subset. -/ lemma eq_weighted_vsub_subset_iff_eq_weighted_vsub_subtype {v : V} {s : set ι} {p : ι → P} : (∃ (fs : finset ι) (hfs : ↑fs ⊆ s) (w : ι → k) (hw : ∑ i in fs, w i = 0), v = fs.weighted_vsub p w) ↔ ∃ (fs : finset s) (w : s → k) (hw : ∑ i in fs, w i = 0), v = fs.weighted_vsub (λ (i : s), p i) w := eq_weighted_vsub_of_point_subset_iff_eq_weighted_vsub_of_point_subtype variables (V) /-- Suppose an indexed family of points is given, along with a subset of the index type. A point can be expressed as an `affine_combination` using a `finset` lying within that subset and with sum of weights 1 if and only if it can be expressed an `affine_combination` with sum of weights 1 for the corresponding indexed family whose index type is the subtype corresponding to that subset. -/ lemma eq_affine_combination_subset_iff_eq_affine_combination_subtype {p0 : P} {s : set ι} {p : ι → P} : (∃ (fs : finset ι) (hfs : ↑fs ⊆ s) (w : ι → k) (hw : ∑ i in fs, w i = 1), p0 = fs.affine_combination p w) ↔ ∃ (fs : finset s) (w : s → k) (hw : ∑ i in fs, w i = 1), p0 = fs.affine_combination (λ (i : s), p i) w := begin simp_rw [affine_combination_apply, eq_vadd_iff_vsub_eq], exact eq_weighted_vsub_of_point_subset_iff_eq_weighted_vsub_of_point_subtype end end finset namespace finset variables (k : Type*) {V : Type*} {P : Type*} [division_ring k] [add_comm_group V] [module k V] variables [affine_space V P] {ι : Type*} (s : finset ι) {ι₂ : Type*} (s₂ : finset ι₂) /-- The weights for the centroid of some points. -/ def centroid_weights : ι → k := function.const ι (card s : k) ⁻¹ /-- `centroid_weights` at any point. -/ @[simp] lemma centroid_weights_apply (i : ι) : s.centroid_weights k i = (card s : k) ⁻¹ := rfl /-- `centroid_weights` equals a constant function. -/ lemma centroid_weights_eq_const : s.centroid_weights k = function.const ι ((card s : k) ⁻¹) := rfl variables {k} /-- The weights in the centroid sum to 1, if the number of points, converted to `k`, is not zero. -/ lemma sum_centroid_weights_eq_one_of_cast_card_ne_zero (h : (card s : k) ≠ 0) : ∑ i in s, s.centroid_weights k i = 1 := by simp [h] variables (k) /-- In the characteristic zero case, the weights in the centroid sum to 1 if the number of points is not zero. -/ lemma sum_centroid_weights_eq_one_of_card_ne_zero [char_zero k] (h : card s ≠ 0) : ∑ i in s, s.centroid_weights k i = 1 := by simp [h] /-- In the characteristic zero case, the weights in the centroid sum to 1 if the set is nonempty. -/ lemma sum_centroid_weights_eq_one_of_nonempty [char_zero k] (h : s.nonempty) : ∑ i in s, s.centroid_weights k i = 1 := s.sum_centroid_weights_eq_one_of_card_ne_zero k (ne_of_gt (card_pos.2 h)) /-- In the characteristic zero case, the weights in the centroid sum to 1 if the number of points is `n + 1`. -/ lemma sum_centroid_weights_eq_one_of_card_eq_add_one [char_zero k] {n : ℕ} (h : card s = n + 1) : ∑ i in s, s.centroid_weights k i = 1 := s.sum_centroid_weights_eq_one_of_card_ne_zero k (h.symm ▸ nat.succ_ne_zero n) include V /-- The centroid of some points. Although defined for any `s`, this is intended to be used in the case where the number of points, converted to `k`, is not zero. -/ def centroid (p : ι → P) : P := s.affine_combination p (s.centroid_weights k) /-- The definition of the centroid. -/ lemma centroid_def (p : ι → P) : s.centroid k p = s.affine_combination p (s.centroid_weights k) := rfl /-- The centroid of a single point. -/ @[simp] lemma centroid_singleton (p : ι → P) (i : ι) : ({i} : finset ι).centroid k p = p i := by simp [centroid_def, affine_combination_apply] /-- The centroid of two points, expressed directly as adding a vector to a point. -/ lemma centroid_insert_singleton [invertible (2 : k)] (p : ι → P) (i₁ i₂ : ι) : ({i₁, i₂} : finset ι).centroid k p = (2 ⁻¹ : k) • (p i₂ -ᵥ p i₁) +ᵥ p i₁ := begin by_cases h : i₁ = i₂, { simp [h] }, { have hc : (card ({i₁, i₂} : finset ι) : k) ≠ 0, { rw [card_insert_of_not_mem (not_mem_singleton.2 h), card_singleton], norm_num, exact nonzero_of_invertible _ }, rw [centroid_def, affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one _ _ _ (sum_centroid_weights_eq_one_of_cast_card_ne_zero _ hc) (p i₁)], simp [h], norm_num } end /-- The centroid of two points indexed by `fin 2`, expressed directly as adding a vector to the first point. -/ lemma centroid_insert_singleton_fin [invertible (2 : k)] (p : fin 2 → P) : univ.centroid k p = (2 ⁻¹ : k) • (p 1 -ᵥ p 0) +ᵥ p 0 := begin have hu : (finset.univ : finset (fin 2)) = {0, 1}, by dec_trivial, rw hu, convert centroid_insert_singleton k p 0 1 end /-- A centroid, over the image of an embedding, equals a centroid with the same points and weights over the original `finset`. -/ lemma centroid_map (e : ι₂ ↪ ι) (p : ι → P) : (s₂.map e).centroid k p = s₂.centroid k (p ∘ e) := by simp [centroid_def, affine_combination_map, centroid_weights] omit V /-- `centroid_weights` gives the weights for the centroid as a constant function, which is suitable when summing over the points whose centroid is being taken. This function gives the weights in a form suitable for summing over a larger set of points, as an indicator function that is zero outside the set whose centroid is being taken. In the case of a `fintype`, the sum may be over `univ`. -/ def centroid_weights_indicator : ι → k := set.indicator ↑s (s.centroid_weights k) /-- The definition of `centroid_weights_indicator`. -/ lemma centroid_weights_indicator_def : s.centroid_weights_indicator k = set.indicator ↑s (s.centroid_weights k) := rfl /-- The sum of the weights for the centroid indexed by a `fintype`. -/ lemma sum_centroid_weights_indicator [fintype ι] : ∑ i, s.centroid_weights_indicator k i = ∑ i in s, s.centroid_weights k i := (set.sum_indicator_subset _ (subset_univ _)).symm /-- In the characteristic zero case, the weights in the centroid indexed by a `fintype` sum to 1 if the number of points is not zero. -/ lemma sum_centroid_weights_indicator_eq_one_of_card_ne_zero [char_zero k] [fintype ι] (h : card s ≠ 0) : ∑ i, s.centroid_weights_indicator k i = 1 := begin rw sum_centroid_weights_indicator, exact s.sum_centroid_weights_eq_one_of_card_ne_zero k h end /-- In the characteristic zero case, the weights in the centroid indexed by a `fintype` sum to 1 if the set is nonempty. -/ lemma sum_centroid_weights_indicator_eq_one_of_nonempty [char_zero k] [fintype ι] (h : s.nonempty) : ∑ i, s.centroid_weights_indicator k i = 1 := begin rw sum_centroid_weights_indicator, exact s.sum_centroid_weights_eq_one_of_nonempty k h end /-- In the characteristic zero case, the weights in the centroid indexed by a `fintype` sum to 1 if the number of points is `n + 1`. -/ lemma sum_centroid_weights_indicator_eq_one_of_card_eq_add_one [char_zero k] [fintype ι] {n : ℕ} (h : card s = n + 1) : ∑ i, s.centroid_weights_indicator k i = 1 := begin rw sum_centroid_weights_indicator, exact s.sum_centroid_weights_eq_one_of_card_eq_add_one k h end include V /-- The centroid as an affine combination over a `fintype`. -/ lemma centroid_eq_affine_combination_fintype [fintype ι] (p : ι → P) : s.centroid k p = univ.affine_combination p (s.centroid_weights_indicator k) := affine_combination_indicator_subset _ _ (subset_univ _) end finset section affine_space' variables {k : Type*} {V : Type*} {P : Type*} [ring k] [add_comm_group V] [module k V] [affine_space V P] variables {ι : Type*} include V /-- A `weighted_vsub` with sum of weights 0 is in the `vector_span` of an indexed family. -/ lemma weighted_vsub_mem_vector_span {s : finset ι} {w : ι → k} (h : ∑ i in s, w i = 0) (p : ι → P) : s.weighted_vsub p w ∈ vector_span k (set.range p) := begin by_cases hn : nonempty ι, { cases hn with i0, rw [vector_span_range_eq_span_range_vsub_right k p i0, ←set.image_univ, finsupp.mem_span_iff_total, finset.weighted_vsub_eq_weighted_vsub_of_point_of_sum_eq_zero s w p h (p i0), finset.weighted_vsub_of_point_apply], let w' := set.indicator ↑s w, have hwx : ∀ i, w' i ≠ 0 → i ∈ s := λ i, set.mem_of_indicator_ne_zero, use [finsupp.on_finset s w' hwx, set.subset_univ _], rw [finsupp.total_apply, finsupp.on_finset_sum hwx], { apply finset.sum_congr rfl, intros i hi, simp [w', set.indicator_apply, if_pos hi] }, { exact λ _, zero_smul k _ } }, { simp [finset.eq_empty_of_not_nonempty hn s] } end /-- An `affine_combination` with sum of weights 1 is in the `affine_span` of an indexed family, if the underlying ring is nontrivial. -/ lemma affine_combination_mem_affine_span [nontrivial k] {s : finset ι} {w : ι → k} (h : ∑ i in s, w i = 1) (p : ι → P) : s.affine_combination p w ∈ affine_span k (set.range p) := begin have hnz : ∑ i in s, w i ≠ 0 := h.symm ▸ one_ne_zero, have hn : s.nonempty := finset.nonempty_of_sum_ne_zero hnz, cases hn with i1 hi1, let w1 : ι → k := function.update (function.const ι 0) i1 1, have hw1 : ∑ i in s, w1 i = 1, { rw [finset.sum_update_of_mem hi1, finset.sum_const_zero, add_zero] }, have hw1s : s.affine_combination p w1 = p i1 := s.affine_combination_of_eq_one_of_eq_zero w1 p hi1 (function.update_same _ _ _) (λ _ _ hne, function.update_noteq hne _ _), have hv : s.affine_combination p w -ᵥ p i1 ∈ (affine_span k (set.range p)).direction, { rw [direction_affine_span, ←hw1s, finset.affine_combination_vsub], apply weighted_vsub_mem_vector_span, simp [pi.sub_apply, h, hw1] }, rw ←vsub_vadd (s.affine_combination p w) (p i1), exact affine_subspace.vadd_mem_of_mem_direction hv (mem_affine_span k (set.mem_range_self _)) end variables (k) {V} /-- A vector is in the `vector_span` of an indexed family if and only if it is a `weighted_vsub` with sum of weights 0. -/ lemma mem_vector_span_iff_eq_weighted_vsub {v : V} {p : ι → P} : v ∈ vector_span k (set.range p) ↔ ∃ (s : finset ι) (w : ι → k) (h : ∑ i in s, w i = 0), v = s.weighted_vsub p w := begin split, { by_cases hn : nonempty ι, { cases hn with i0, rw [vector_span_range_eq_span_range_vsub_right k p i0, ←set.image_univ, finsupp.mem_span_iff_total], rintros ⟨l, hl, hv⟩, use insert i0 l.support, set w := (l : ι → k) - function.update (function.const ι 0 : ι → k) i0 (∑ i in l.support, l i) with hwdef, use w, have hw : ∑ i in insert i0 l.support, w i = 0, { rw hwdef, simp_rw [pi.sub_apply, finset.sum_sub_distrib, finset.sum_update_of_mem (finset.mem_insert_self _ _), finset.sum_const_zero, finset.sum_insert_of_eq_zero_if_not_mem finsupp.not_mem_support_iff.1, add_zero, sub_self] }, use hw, have hz : w i0 • (p i0 -ᵥ p i0 : V) = 0 := (vsub_self (p i0)).symm ▸ smul_zero _, change (λ i, w i • (p i -ᵥ p i0 : V)) i0 = 0 at hz, rw [finset.weighted_vsub_eq_weighted_vsub_of_point_of_sum_eq_zero _ w p hw (p i0), finset.weighted_vsub_of_point_apply, ←hv, finsupp.total_apply, finset.sum_insert_zero hz], change ∑ i in l.support, l i • _ = _, congr' with i, by_cases h : i = i0, { simp [h] }, { simp [hwdef, h] } }, { rw [set.range_eq_empty.2 hn, vector_span_empty, submodule.mem_bot], intro hv, use [∅], simp [hv] } }, { rintros ⟨s, w, hw, rfl⟩, exact weighted_vsub_mem_vector_span hw p } end variables {k} /-- A point in the `affine_span` of an indexed family is an `affine_combination` with sum of weights 1. -/ lemma eq_affine_combination_of_mem_affine_span {p1 : P} {p : ι → P} (h : p1 ∈ affine_span k (set.range p)) : ∃ (s : finset ι) (w : ι → k) (hw : ∑ i in s, w i = 1), p1 = s.affine_combination p w := begin have hn : ((affine_span k (set.range p)) : set P).nonempty := ⟨p1, h⟩, rw [affine_span_nonempty, set.range_nonempty_iff_nonempty] at hn, cases hn with i0, have h0 : p i0 ∈ affine_span k (set.range p) := mem_affine_span k (set.mem_range_self i0), have hd : p1 -ᵥ p i0 ∈ (affine_span k (set.range p)).direction := affine_subspace.vsub_mem_direction h h0, rw [direction_affine_span, mem_vector_span_iff_eq_weighted_vsub] at hd, rcases hd with ⟨s, w, h, hs⟩, let s' := insert i0 s, let w' := set.indicator ↑s w, have h' : ∑ i in s', w' i = 0, { rw [←h, set.sum_indicator_subset _ (finset.subset_insert i0 s)] }, have hs' : s'.weighted_vsub p w' = p1 -ᵥ p i0, { rw hs, exact (finset.weighted_vsub_indicator_subset _ _ (finset.subset_insert i0 s)).symm }, let w0 : ι → k := function.update (function.const ι 0) i0 1, have hw0 : ∑ i in s', w0 i = 1, { rw [finset.sum_update_of_mem (finset.mem_insert_self _ _), finset.sum_const_zero, add_zero] }, have hw0s : s'.affine_combination p w0 = p i0 := s'.affine_combination_of_eq_one_of_eq_zero w0 p (finset.mem_insert_self _ _) (function.update_same _ _ _) (λ _ _ hne, function.update_noteq hne _ _), use [s', w0 + w'], split, { simp [pi.add_apply, finset.sum_add_distrib, hw0, h'] }, { rw [add_comm, ←finset.weighted_vsub_vadd_affine_combination, hw0s, hs', vsub_vadd] } end variables (k V) /-- A point is in the `affine_span` of an indexed family if and only if it is an `affine_combination` with sum of weights 1, provided the underlying ring is nontrivial. -/ lemma mem_affine_span_iff_eq_affine_combination [nontrivial k] {p1 : P} {p : ι → P} : p1 ∈ affine_span k (set.range p) ↔ ∃ (s : finset ι) (w : ι → k) (hw : ∑ i in s, w i = 1), p1 = s.affine_combination p w := begin split, { exact eq_affine_combination_of_mem_affine_span }, { rintros ⟨s, w, hw, rfl⟩, exact affine_combination_mem_affine_span hw p } end end affine_space' section division_ring variables {k : Type*} {V : Type*} {P : Type*} [division_ring k] [add_comm_group V] [module k V] variables [affine_space V P] {ι : Type*} include V open set finset /-- The centroid lies in the affine span if the number of points, converted to `k`, is not zero. -/ lemma centroid_mem_affine_span_of_cast_card_ne_zero {s : finset ι} (p : ι → P) (h : (card s : k) ≠ 0) : s.centroid k p ∈ affine_span k (range p) := affine_combination_mem_affine_span (s.sum_centroid_weights_eq_one_of_cast_card_ne_zero h) p variables (k) /-- In the characteristic zero case, the centroid lies in the affine span if the number of points is not zero. -/ lemma centroid_mem_affine_span_of_card_ne_zero [char_zero k] {s : finset ι} (p : ι → P) (h : card s ≠ 0) : s.centroid k p ∈ affine_span k (range p) := affine_combination_mem_affine_span (s.sum_centroid_weights_eq_one_of_card_ne_zero k h) p /-- In the characteristic zero case, the centroid lies in the affine span if the set is nonempty. -/ lemma centroid_mem_affine_span_of_nonempty [char_zero k] {s : finset ι} (p : ι → P) (h : s.nonempty) : s.centroid k p ∈ affine_span k (range p) := affine_combination_mem_affine_span (s.sum_centroid_weights_eq_one_of_nonempty k h) p /-- In the characteristic zero case, the centroid lies in the affine span if the number of points is `n + 1`. -/ lemma centroid_mem_affine_span_of_card_eq_add_one [char_zero k] {s : finset ι} (p : ι → P) {n : ℕ} (h : card s = n + 1) : s.centroid k p ∈ affine_span k (range p) := affine_combination_mem_affine_span (s.sum_centroid_weights_eq_one_of_card_eq_add_one k h) p end division_ring namespace affine_map variables {k : Type*} {V : Type*} (P : Type*) [comm_ring k] [add_comm_group V] [module k V] variables [affine_space V P] {ι : Type*} (s : finset ι) include V -- TODO: define `affine_map.proj`, `affine_map.fst`, `affine_map.snd` /-- A weighted sum, as an affine map on the points involved. -/ def weighted_vsub_of_point (w : ι → k) : affine_map k ((ι → P) × P) V := { to_fun := λ p, s.weighted_vsub_of_point p.fst p.snd w, linear := ∑ i in s, w i • ((linear_map.proj i).comp (linear_map.fst _ _ _) - linear_map.snd _ _ _), map_vadd' := begin rintros ⟨p, b⟩ ⟨v, b'⟩, simp [linear_map.sum_apply, finset.weighted_vsub_of_point, vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, add_sub, ← sub_add_eq_add_sub, smul_add, finset.sum_add_distrib] end } end affine_map
f73e3773b665531365686516842d316d7bf39058
b00eb947a9c4141624aa8919e94ce6dcd249ed70
/tests/lean/run/meta7.lean
9b86b1d6aea8c939d347c2cb6ad7d789fa4d69e6
[ "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,859
lean
import Lean.Meta open Lean open Lean.Meta partial def fact : Nat → Nat | 0 => 1 | n+1 => (n+1)*fact n set_option trace.Meta.debug true set_option trace.Meta.check false def print (msg : MessageData) : MetaM Unit := trace[Meta.debug] msg def checkM (x : MetaM Bool) : MetaM Unit := unless (← x) do throwError "check failed" def ex (x_1 x_2 x_3 : Nat) : Nat × Nat := let x := fact (10 + x_1 + x_2 + x_3); let ty := Nat → Nat; let f : ty := fun x => x; let n := 20; let z := f 10; (let y : { v : Nat // v = n } := ⟨20, rfl⟩; y.1 + n + f x, z + 10) def tst1 : MetaM Unit := do print "----- tst1 -----"; let c ← getConstInfo `ex; lambdaTelescope c.value?.get! fun xs body => withTrackingZeta do check body; let ys ← getZetaFVarIds; let ys := ys.toList.map mkFVar; print ys; checkM $ pure $ ys.length == 2; let c ← mkAuxDefinitionFor `foo body; print c; check c; pure () #eval tst1 #print foo def tst2 : MetaM Unit := do print "----- tst2 -----"; let nat := mkConst `Nat; let t0 := mkApp (mkConst `IO) nat; let t := mkForall `_ BinderInfo.default nat t0; print t; check t; forallBoundedTelescope t (some 1) fun xs b => do print b; checkM $ pure $ xs.size == 1; checkM $ pure $ b == t0; pure () #eval tst2 def tst3 : MetaM Unit := do print "----- tst2 -----"; let nat := mkConst `Nat; let t0 := mkApp (mkConst `IO) nat; let t := t0; print t; check t; forallBoundedTelescope t (some 0) fun xs b => do print b; checkM $ pure $ xs.size == 0; checkM $ pure $ b == t0; pure () #eval tst3 def tst4 : MetaM Unit := do print "----- tst4 -----"; let nat := mkConst `Nat; withLocalDeclD `x nat fun x => withLocalDeclD `y nat fun y => do let m ← mkFreshExprMVar nat; print (← ppGoal m.mvarId!); let val ← mkAppM `Add.add #[mkNatLit 10, y]; let ⟨zId, nId, subst⟩ ← assertAfter m.mvarId! x.fvarId! `z nat val; print m; print (← ppGoal nId); withMVarContext nId do { print m!"{subst.apply x} {subst.apply y} {mkFVar zId}"; assignExprMVar nId (← mkAppM `Add.add #[subst.apply x, mkFVar zId]); print (mkMVar nId) }; print m; let expected ← mkAppM `Add.add #[x, val]; checkM (isDefEq m expected); pure () #eval tst4 def tst5 : MetaM Unit := do print "----- tst5 -----"; let prop := mkSort levelZero; withLocalDeclD `p prop fun p => withLocalDeclD `q prop fun q => do withLocalDeclD `h₁ p fun h₁ => do let eq ← mkEq p q; withLocalDeclD `h₂ eq fun h₂ => do let m ← mkFreshExprMVar q; let r ← replaceLocalDecl m.mvarId! h₁.fvarId! q h₂; print (← ppGoal r.mvarId); assignExprMVar r.mvarId (mkFVar r.fvarId); print m; check m; pure () #eval tst5 def tst6 : MetaM Unit := do print "----- tst6 -----"; let nat := mkConst `Nat; withLocalDeclD `x nat fun x => withLocalDeclD `y nat fun y => do let m ← mkFreshExprMVar nat; print (← ppGoal m.mvarId!); let val ← mkAppM `Add.add #[mkNatLit 10, y]; let ⟨zId, nId, subst⟩ ← assertAfter m.mvarId! y.fvarId! `z nat val; print m; print (← ppGoal nId); withMVarContext nId do { print m!"{subst.apply x} {subst.apply y} {mkFVar zId}"; assignExprMVar nId (← mkAppM `Add.add #[subst.apply x, mkFVar zId]); print (mkMVar nId) }; print m; let expected ← mkAppM `Add.add #[x, val]; checkM (isDefEq m expected); pure () #eval tst6 def tst7 : MetaM Unit := do print "----- tst7 -----"; let nat := mkConst `Nat; withLocalDeclD `x nat fun x => withLocalDeclD `y nat fun y => do let val ← mkAppM `Add.add #[x, y]; print val; let val := val.replaceFVars #[x, y] #[mkNatLit 0, mkNatLit 1]; print val; let expected ← mkAppM `Add.add #[mkNatLit 0, mkNatLit 1]; print expected; checkM (pure $ val == expected); pure () #eval tst7 def aux := [1, 2, 3].isEmpty def tst8 : MetaM Unit := do print "----- tst8 -----" let t := mkConst `aux let some t ← unfoldDefinition? t | throwError "unexpected" let some t ← unfoldDefinition? t | throwError "unexpected" print t let t ← whnfCore t print t pure () #eval tst8 def tst9 : MetaM Unit := do print "----- tst9 -----" let defInsts ← getDefaultInstances `OfNat print (toString defInsts) pure () #eval tst9 mutual inductive Foo (α : Type) where | mk : List (Bla α) → Foo α | leaf : α → Foo α inductive Bla (α : Type) where | nil : Bla α | cons : Foo α → Bla α → Bla α end def tst10 : MetaM Unit := do assert! !(← getConstInfoInduct `List).isNested assert! (← getConstInfoInduct `Bla).isNested assert! (← getConstInfoInduct `Foo).isNested assert! !(← getConstInfoInduct `Prod).isNested #eval tst10 def tst11 : MetaM Unit := do print "----- tst11 -----" withLocalDeclD `x (mkConst ``True) fun x => withLocalDeclD `y (mkConst ``True) fun y => do checkM (isDefEq x y) checkM (withoutProofIrrelevance do return !(← isDefEq x y)) pure () #eval tst11
158d1382ff15a878c56544092bbf72eebe8fd8a5
0845ae2ca02071debcfd4ac24be871236c01784f
/library/init/lean/compiler/ir/default.lean
739b39043cd58683d6ffe2e1ed9c0bf4bbf6f37c
[ "Apache-2.0" ]
permissive
GaloisInc/lean4
74c267eb0e900bfaa23df8de86039483ecbd60b7
228ddd5fdcd98dd4e9c009f425284e86917938aa
refs/heads/master
1,643,131,356,301
1,562,715,572,000
1,562,715,572,000
192,390,898
0
0
null
1,560,792,750,000
1,560,792,749,000
null
UTF-8
Lean
false
false
1,842
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.lean.compiler.ir.basic import init.lean.compiler.ir.format import init.lean.compiler.ir.compilerm import init.lean.compiler.ir.pushproj import init.lean.compiler.ir.elimdead import init.lean.compiler.ir.simpcase import init.lean.compiler.ir.resetreuse import init.lean.compiler.ir.normids import init.lean.compiler.ir.checker import init.lean.compiler.ir.borrow import init.lean.compiler.ir.boxing import init.lean.compiler.ir.rc import init.lean.compiler.ir.expandresetreuse import init.lean.compiler.ir.emitcpp namespace Lean namespace IR private def compileAux (decls : Array Decl) : CompilerM Unit := do logDecls `init decls; checkDecls decls; let decls := decls.map Decl.pushProj; logDecls `push_proj decls; let decls := decls.map Decl.insertResetReuse; logDecls `reset_reuse decls; let decls := decls.map Decl.elimDead; logDecls `elim_dead decls; let decls := decls.map Decl.simpCase; logDecls `simp_case decls; let decls := decls.map Decl.normalizeIds; decls ← inferBorrow decls; logDecls `borrow decls; decls ← explicitBoxing decls; logDecls `boxing decls; decls ← explicitRC decls; logDecls `rc decls; let decls := decls.map Decl.expandResetReuse; logDecls `expand_reset_reuse decls; let decls := decls.map Decl.pushProj; logDecls `push_proj decls; logDecls `result decls; checkDecls decls; addDecls decls; pure () @[export lean.ir.compile_core] def compile (env : Environment) (opts : Options) (decls : Array Decl) : Log × (Except String Environment) := match (compileAux decls opts).run { env := env } with | EState.Result.ok _ s => (s.log, Except.ok s.env) | EState.Result.error msg s => (s.log, Except.error msg) end IR end Lean
ddb5d4029cfb9d674276d08090aac0bb02b50148
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/library/data/set/finite.lean
2a01c6c06687bdc9708840ed53ad532ba605594b
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
8,856
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad The notion of "finiteness" for sets. This approach is not computational: for example, just because an element s : set A satsifies finite s doesn't mean that we can compute the cardinality. For a computational representation, use the finset type. -/ import data.finset.to_set .classical_inverse open nat classical variable {A : Type} namespace set attribute [class] definition finite (s : set A) : Prop := ∃ (s' : finset A), s = finset.to_set s' attribute [instance] theorem finite_finset (s : finset A) : finite (finset.to_set s) := exists.intro s rfl /- to finset: casts every set to a finite set -/ noncomputable definition to_finset (s : set A) : finset A := if fins : finite s then some fins else finset.empty theorem to_finset_of_not_finite {s : set A} (nfins : ¬ finite s) : to_finset s = (#finset ∅) := by rewrite [↑to_finset, dif_neg nfins] theorem to_set_to_finset (s : set A) [fins : finite s] : finset.to_set (to_finset s) = s := by rewrite [↑to_finset, dif_pos fins]; exact eq.symm (some_spec fins) theorem mem_to_finset_eq (a : A) (s : set A) [finite s] : (#finset a ∈ to_finset s) = (a ∈ s) := by rewrite [-to_set_to_finset at {2}] theorem to_set_to_finset_of_not_finite {s : set A} (nfins : ¬ finite s) : finset.to_set (to_finset s) = ∅ := by rewrite [to_finset_of_not_finite nfins] theorem to_finset_to_set (s : finset A) : to_finset (finset.to_set s) = s := by rewrite [finset.eq_eq_to_set_eq, to_set_to_finset (finset.to_set s)] theorem to_finset_eq_of_to_set_eq {s : set A} {t : finset A} (H : finset.to_set t = s) : to_finset s = t := finset.eq_of_to_set_eq_to_set (by subst [s]; rewrite to_finset_to_set) /- finiteness -/ theorem finite_of_to_set_to_finset_eq {s : set A} (H : finset.to_set (to_finset s) = s) : finite s := by rewrite -H; apply finite_finset attribute [instance] theorem finite_empty : finite (∅ : set A) := by rewrite [-finset.to_set_empty]; apply finite_finset theorem to_finset_empty : to_finset (∅ : set A) = (#finset ∅) := to_finset_eq_of_to_set_eq !finset.to_set_empty theorem to_finset_eq_empty_of_eq_empty {s : set A} [fins : finite s] (H : s = ∅) : to_finset s = finset.empty := by rewrite [H, to_finset_empty] theorem eq_empty_of_to_finset_eq_empty {s : set A} [fins : finite s] (H : to_finset s = finset.empty) : s = ∅ := by rewrite [-finset.to_set_empty, -H, to_set_to_finset] theorem to_finset_eq_empty (s : set A) [fins : finite s] : (to_finset s = finset.empty) ↔ (s = ∅) := iff.intro eq_empty_of_to_finset_eq_empty to_finset_eq_empty_of_eq_empty attribute [instance] theorem finite_insert (a : A) (s : set A) [finite s] : finite (insert a s) := exists.intro (finset.insert a (to_finset s)) (by rewrite [finset.to_set_insert, to_set_to_finset]) theorem to_finset_insert (a : A) (s : set A) [finite s] : to_finset (insert a s) = finset.insert a (to_finset s) := by apply to_finset_eq_of_to_set_eq; rewrite [finset.to_set_insert, to_set_to_finset] attribute [instance] theorem finite_union (s t : set A) [finite s] [finite t] : finite (s ∪ t) := exists.intro (#finset to_finset s ∪ to_finset t) (by rewrite [finset.to_set_union, *to_set_to_finset]) theorem to_finset_union (s t : set A) [finite s] [finite t] : to_finset (s ∪ t) = (#finset to_finset s ∪ to_finset t) := by apply to_finset_eq_of_to_set_eq; rewrite [finset.to_set_union, *to_set_to_finset] attribute [instance] theorem finite_inter (s t : set A) [finite s] [finite t] : finite (s ∩ t) := exists.intro (#finset to_finset s ∩ to_finset t) (by rewrite [finset.to_set_inter, *to_set_to_finset]) theorem to_finset_inter (s t : set A) [finite s] [finite t] : to_finset (s ∩ t) = (#finset to_finset s ∩ to_finset t) := by apply to_finset_eq_of_to_set_eq; rewrite [finset.to_set_inter, *to_set_to_finset] attribute [instance] theorem finite_sep (s : set A) (p : A → Prop) [finite s] : finite {x ∈ s | p x} := exists.intro (finset.sep p (to_finset s)) (by rewrite [finset.to_set_sep, *to_set_to_finset]) theorem to_finset_sep (s : set A) (p : A → Prop) [finite s] : to_finset {x ∈ s | p x} = (#finset {x ∈ to_finset s | p x}) := by apply to_finset_eq_of_to_set_eq; rewrite [finset.to_set_sep, to_set_to_finset] attribute [instance] theorem finite_image {B : Type} (f : A → B) (s : set A) [finite s] : finite (f ' s) := exists.intro (finset.image f (to_finset s)) (by rewrite [finset.to_set_image, *to_set_to_finset]) theorem to_finset_image {B : Type} (f : A → B) (s : set A) [fins : finite s] : to_finset (f ' s) = (#finset f ' (to_finset s)) := by apply to_finset_eq_of_to_set_eq; rewrite [finset.to_set_image, to_set_to_finset] attribute [instance] theorem finite_diff (s t : set A) [finite s] : finite (s \ t) := !finite_sep theorem to_finset_diff (s t : set A) [finite s] [finite t] : to_finset (s \ t) = (#finset to_finset s \ to_finset t) := by apply to_finset_eq_of_to_set_eq; rewrite [finset.to_set_diff, *to_set_to_finset] theorem finite_subset {s t : set A} [finite t] (ssubt : s ⊆ t) : finite s := by rewrite (eq_sep_of_subset ssubt); apply finite_sep theorem to_finset_subset_to_finset_eq (s t : set A) [finite s] [finite t] : (#finset to_finset s ⊆ to_finset t) = (s ⊆ t) := by rewrite [finset.subset_eq_to_set_subset, *to_set_to_finset] theorem finite_of_finite_insert {s : set A} {a : A} (finias : finite (insert a s)) : finite s := finite_subset (subset_insert a s) attribute [instance] theorem finite_upto (n : ℕ) : finite {i | i < n} := by rewrite [-finset.to_set_upto n]; apply finite_finset theorem to_finset_upto (n : ℕ) : to_finset {i | i < n} = finset.upto n := by apply (to_finset_eq_of_to_set_eq !finset.to_set_upto) theorem finite_of_surj_on {B : Type} {f : A → B} {s : set A} [finite s] {t : set B} (H : surj_on f s t) : finite t := finite_subset H theorem finite_of_inj_on {B : Type} {f : A → B} {s : set A} {t : set B} [finite t] (mapsto : maps_to f s t) (injf : inj_on f s) : finite s := if H : s = ∅ then by rewrite H; apply _ else obtain (dflt : A) (xs : dflt ∈ s), from exists_mem_of_ne_empty H, let finv := inv_fun f s dflt in have surj_on finv t s, from surj_on_inv_fun_of_inj_on dflt mapsto injf, finite_of_surj_on this theorem finite_of_bij_on {B : Type} {f : A → B} {s : set A} {t : set B} [finite s] (bijf : bij_on f s t) : finite t := finite_of_surj_on (surj_on_of_bij_on bijf) theorem finite_of_bij_on' {B : Type} {f : A → B} {s : set A} {t : set B} [finite t] (bijf : bij_on f s t) : finite s := finite_of_inj_on (maps_to_of_bij_on bijf) (inj_on_of_bij_on bijf) theorem finite_iff_finite_of_bij_on {B : Type} {f : A → B} {s : set A} {t : set B} (bijf : bij_on f s t) : finite s ↔ finite t := iff.intro (assume fs, finite_of_bij_on bijf) (assume ft, finite_of_bij_on' bijf) theorem finite_powerset (s : set A) [finite s] : finite 𝒫 s := have H : 𝒫 s = finset.to_set ' (finset.to_set (#finset 𝒫 (to_finset s))), from ext (take t, iff.intro (suppose t ∈ 𝒫 s, have t ⊆ s, from this, have finite t, from finite_subset this, have (#finset to_finset t ∈ 𝒫 (to_finset s)), by rewrite [finset.mem_powerset_iff_subset, to_finset_subset_to_finset_eq]; apply `t ⊆ s`, have to_finset t ∈ (finset.to_set (finset.powerset (to_finset s))), from this, mem_image this (by rewrite to_set_to_finset)) (assume H', obtain t' [(tmem : (#finset t' ∈ 𝒫 (to_finset s))) (teq : finset.to_set t' = t)], from H', show t ⊆ s, begin rewrite [-teq, finset.mem_powerset_iff_subset at tmem, -to_set_to_finset s], rewrite -finset.subset_eq_to_set_subset, assumption end)), by rewrite H; apply finite_image /- induction for finite sets -/ attribute [recursor 6] theorem induction_finite {P : set A → Prop} (H1 : P ∅) (H2 : ∀ ⦃a : A⦄, ∀ {s : set A} [finite s], a ∉ s → P s → P (insert a s)) : ∀ (s : set A) [finite s], P s := begin intro s fins, rewrite [-to_set_to_finset s], generalize to_finset s, intro s', induction s' using finset.induction with a s' nains ih, {rewrite finset.to_set_empty, apply H1}, rewrite [finset.to_set_insert], apply H2, {rewrite -finset.mem_eq_mem_to_set, assumption}, exact ih end theorem induction_on_finite {P : set A → Prop} (s : set A) [finite s] (H1 : P ∅) (H2 : ∀ ⦃a : A⦄, ∀ {s : set A} [finite s], a ∉ s → P s → P (insert a s)) : P s := induction_finite H1 H2 s end set
5d98ad786a1e210cfa9b6b4be650771f7dad25eb
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/data/real/golden_ratio.lean
1f3615465fb26ef1d8608b7a791bfacea37ee3c0
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
5,573
lean
/- Copyright (c) 2020 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker, Alexey Soloyev, Junyan Xu -/ import data.real.irrational import data.nat.fib import data.matrix.notation import tactic.ring_exp import algebra.linear_recurrence /-! # The golden ratio and its conjugate This file defines the golden ratio `φ := (1 + √5)/2` and its conjugate `ψ := (1 - √5)/2`, which are the two real roots of `X² - X - 1`. Along with various computational facts about them, we prove their irrationality, and we link them to the Fibonacci sequence by proving Binet's formula. -/ noncomputable theory /-- The golden ratio `φ := (1 + √5)/2`. -/ @[reducible] def golden_ratio := (1 + real.sqrt 5)/2 /-- The conjugate of the golden ratio `ψ := (1 - √5)/2`. -/ @[reducible] def golden_conj := (1 - real.sqrt 5)/2 localized "notation `φ` := golden_ratio" in real localized "notation `ψ` := golden_conj" in real /-- The inverse of the golden ratio is the opposite of its conjugate. -/ lemma inv_gold : φ⁻¹ = -ψ := begin have : 1 + real.sqrt 5 ≠ 0, from ne_of_gt (add_pos (by norm_num) $ real.sqrt_pos.mpr (by norm_num)), field_simp [sub_mul, mul_add], norm_num end /-- The opposite of the golden ratio is the inverse of its conjugate. -/ lemma inv_gold_conj : ψ⁻¹ = -φ := begin rw [inv_eq_iff, ← neg_inv, neg_eq_iff_neg_eq], exact inv_gold.symm, end @[simp] lemma gold_mul_gold_conj : φ * ψ = -1 := by {field_simp, rw ← sq_sub_sq, norm_num} @[simp] lemma gold_conj_mul_gold : ψ * φ = -1 := by {rw mul_comm, exact gold_mul_gold_conj} @[simp] lemma gold_add_gold_conj : φ + ψ = 1 := by {rw [golden_ratio, golden_conj], ring} lemma one_sub_gold_conj : 1 - φ = ψ := by linarith [gold_add_gold_conj] lemma one_sub_gold : 1 - ψ = φ := by linarith [gold_add_gold_conj] @[simp] lemma gold_sub_gold_conj : φ - ψ = real.sqrt 5 := by {rw [golden_ratio, golden_conj], ring} @[simp] lemma gold_sq : φ^2 = φ + 1 := begin rw [golden_ratio, ←sub_eq_zero], ring_exp, rw real.sq_sqrt; norm_num, end @[simp] lemma gold_conj_sq : ψ^2 = ψ + 1 := begin rw [golden_conj, ←sub_eq_zero], ring_exp, rw real.sq_sqrt; norm_num, end lemma gold_pos : 0 < φ := mul_pos (by apply add_pos; norm_num) $ inv_pos.2 zero_lt_two lemma gold_ne_zero : φ ≠ 0 := ne_of_gt gold_pos lemma one_lt_gold : 1 < φ := begin refine lt_of_mul_lt_mul_left _ (le_of_lt gold_pos), simp [← sq, gold_pos, zero_lt_one] end lemma gold_conj_neg : ψ < 0 := by linarith [one_sub_gold_conj, one_lt_gold] lemma gold_conj_ne_zero : ψ ≠ 0 := ne_of_lt gold_conj_neg lemma neg_one_lt_gold_conj : -1 < ψ := begin rw [neg_lt, ← inv_gold], exact inv_lt_one one_lt_gold, end /-! ## Irrationality -/ /-- The golden ratio is irrational. -/ theorem gold_irrational : irrational φ := begin have := nat.prime.irrational_sqrt (show nat.prime 5, by norm_num), have := this.rat_add 1, have := this.rat_mul (show (0.5 : ℚ) ≠ 0, by norm_num), convert this, field_simp end /-- The conjugate of the golden ratio is irrational. -/ theorem gold_conj_irrational : irrational ψ := begin have := nat.prime.irrational_sqrt (show nat.prime 5, by norm_num), have := this.rat_sub 1, have := this.rat_mul (show (0.5 : ℚ) ≠ 0, by norm_num), convert this, field_simp end /-! ## Links with Fibonacci sequence -/ section fibrec variables {α : Type*} [comm_semiring α] /-- The recurrence relation satisfied by the Fibonacci sequence. -/ def fib_rec : linear_recurrence α := { order := 2, coeffs := ![1, 1]} section poly open polynomial /-- The characteristic polynomial of `fib_rec` is `X² - (X + 1)`. -/ lemma fib_rec_char_poly_eq {β : Type*} [comm_ring β] : fib_rec.char_poly = X^2 - (X + (1 : polynomial β)) := begin rw [fib_rec, linear_recurrence.char_poly], simp [finset.sum_fin_eq_sum_range, finset.sum_range_succ', monomial_eq_smul_X] end end poly /-- As expected, the Fibonacci sequence is a solution of `fib_rec`. -/ lemma fib_is_sol_fib_rec : fib_rec.is_solution (λ x, x.fib : ℕ → α) := begin rw fib_rec, intros n, simp only, rw [nat.fib_succ_succ, add_comm], simp [finset.sum_fin_eq_sum_range, finset.sum_range_succ'], end /-- The geometric sequence `λ n, φ^n` is a solution of `fib_rec`. -/ lemma geom_gold_is_sol_fib_rec : fib_rec.is_solution (pow φ) := begin rw [fib_rec.geom_sol_iff_root_char_poly, fib_rec_char_poly_eq], simp [sub_eq_zero] end /-- The geometric sequence `λ n, ψ^n` is a solution of `fib_rec`. -/ lemma geom_gold_conj_is_sol_fib_rec : fib_rec.is_solution (pow ψ) := begin rw [fib_rec.geom_sol_iff_root_char_poly, fib_rec_char_poly_eq], simp [sub_eq_zero] end end fibrec /-- Binet's formula as a function equality. -/ theorem real.coe_fib_eq' : (λ n, nat.fib n : ℕ → ℝ) = λ n, (φ^n - ψ^n) / real.sqrt 5 := begin rw fib_rec.sol_eq_of_eq_init, { intros i hi, fin_cases hi, { simp }, { simp only [golden_ratio, golden_conj], ring_exp, rw mul_inv_cancel; norm_num } }, { exact fib_is_sol_fib_rec }, { ring_nf, exact (@fib_rec ℝ _).sol_space.sub_mem (submodule.smul_mem fib_rec.sol_space (real.sqrt 5)⁻¹ geom_gold_is_sol_fib_rec) (submodule.smul_mem fib_rec.sol_space (real.sqrt 5)⁻¹ geom_gold_conj_is_sol_fib_rec) } end /-- Binet's formula as a dependent equality. -/ theorem real.coe_fib_eq : ∀ n, (nat.fib n : ℝ) = (φ^n - ψ^n) / real.sqrt 5 := by rw [← function.funext_iff, real.coe_fib_eq']
29729111d06578a64307c35ec8c05a2e3e9cc688
206422fb9edabf63def0ed2aa3f489150fb09ccb
/src/algebra/category/Module/limits.lean
32a7fb7a96aac7993b04bd0660175b6453609dc0
[ "Apache-2.0" ]
permissive
hamdysalah1/mathlib
b915f86b2503feeae268de369f1b16932321f097
95454452f6b3569bf967d35aab8d852b1ddf8017
refs/heads/master
1,677,154,116,545
1,611,797,994,000
1,611,797,994,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,275
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.category.Module.basic import algebra.category.Group.limits import algebra.direct_limit /-! # The category of R-modules has all limits Further, these limits are preserved by the forgetful functor --- that is, the underlying types are just the limits in the category of types. -/ open category_theory open category_theory.limits universes u v noncomputable theory namespace Module variables {R : Type u} [ring R] variables {J : Type v} [small_category J] instance add_comm_group_obj (F : J ⥤ Module.{v} R) (j) : add_comm_group ((F ⋙ forget (Module R)).obj j) := by { change add_comm_group (F.obj j), apply_instance } instance module_obj (F : J ⥤ Module.{v} R) (j) : module R ((F ⋙ forget (Module R)).obj j) := by { change module R (F.obj j), apply_instance } /-- The flat sections of a functor into `Module R` form a submodule of all sections. -/ def sections_submodule (F : J ⥤ Module R) : submodule R (Π j, F.obj j) := { carrier := (F ⋙ forget (Module R)).sections, smul_mem' := λ r s sh j j' f, begin simp only [forget_map_eq_coe, functor.comp_map, pi.smul_apply, linear_map.map_smul], dsimp [functor.sections] at sh, rw sh f, end, ..(AddGroup.sections_add_subgroup (F ⋙ forget₂ (Module R) AddCommGroup.{v} ⋙ forget₂ AddCommGroup AddGroup.{v})) } instance limit_add_comm_group (F : J ⥤ Module R) : add_comm_group (types.limit_cone (F ⋙ forget (Module.{v} R))).X := begin change add_comm_group (sections_submodule F), apply_instance, end instance limit_module (F : J ⥤ Module R) : module R (types.limit_cone (F ⋙ forget (Module.{v} R))).X := begin change module R (sections_submodule F), apply_instance, end /-- `limit.π (F ⋙ forget Ring) j` as a `ring_hom`. -/ def limit_π_linear_map (F : J ⥤ Module R) (j) : (types.limit_cone (F ⋙ forget (Module.{v} R))).X →ₗ[R] (F ⋙ forget (Module R)).obj j := { to_fun := (types.limit_cone (F ⋙ forget (Module R))).π.app j, map_smul' := λ x y, rfl, map_add' := λ x y, rfl } namespace has_limits -- The next two definitions are used in the construction of `has_limits (Module R)`. -- After that, the limits should be constructed using the generic limits API, -- e.g. `limit F`, `limit.cone F`, and `limit.is_limit F`. /-- Construction of a limit cone in `Module R`. (Internal use only; use the limits API.) -/ def limit_cone (F : J ⥤ Module R) : cone F := { X := Module.of R (types.limit_cone (F ⋙ forget _)).X, π := { app := limit_π_linear_map F, naturality' := λ j j' f, linear_map.coe_injective ((types.limit_cone (F ⋙ forget _)).π.naturality f) } } /-- Witness that the limit cone in `Module R` is a limit cone. (Internal use only; use the limits API.) -/ def limit_cone_is_limit (F : J ⥤ Module R) : is_limit (limit_cone F) := begin refine is_limit.of_faithful (forget (Module R)) (types.limit_cone_is_limit _) (λ s, ⟨_, _, _⟩) (λ s, rfl); tidy end end has_limits open has_limits /-- The category of R-modules has all limits. -/ @[irreducible] instance has_limits : has_limits (Module.{v} R) := { has_limits_of_shape := λ J 𝒥, by exactI { has_limit := λ F, has_limit.mk { cone := limit_cone F, is_limit := limit_cone_is_limit F } } } /-- An auxiliary declaration to speed up typechecking. -/ def forget₂_AddCommGroup_preserves_limits_aux (F : J ⥤ Module R) : is_limit ((forget₂ (Module R) AddCommGroup).map_cone (limit_cone F)) := AddCommGroup.limit_cone_is_limit (F ⋙ forget₂ (Module R) AddCommGroup) /-- The forgetful functor from R-modules to abelian groups preserves all limits. -/ instance forget₂_AddCommGroup_preserves_limits : preserves_limits (forget₂ (Module R) AddCommGroup.{v}) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ F, preserves_limit_of_preserves_limit_cone (limit_cone_is_limit F) (forget₂_AddCommGroup_preserves_limits_aux F) } } /-- The forgetful functor from R-modules to types preserves all limits. -/ instance forget_preserves_limits : preserves_limits (forget (Module R)) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ F, preserves_limit_of_preserves_limit_cone (limit_cone_is_limit F) (types.limit_cone_is_limit (F ⋙ forget _)) } } section direct_limit open module variables {ι : Type v} variables [dec_ι : decidable_eq ι] [directed_order ι] variables (G : ι → Type v) variables [Π i, add_comm_group (G i)] [Π i, module R (G i)] variables (f : Π i j, i ≤ j → G i →ₗ[R] G j) [module.directed_system G f] /-- The diagram (in the sense of `category_theory`) of an unbundled `direct_limit` of modules. -/ @[simps] def direct_limit_diagram : ι ⥤ Module R := { obj := λ i, Module.of R (G i), map := λ i j hij, f i j (le_of_hom hij), map_id' := λ i, by { ext x, apply module.directed_system.map_self }, map_comp' := λ i j k hij hjk, begin ext x, symmetry, apply module.directed_system.map_map end } variables [decidable_eq ι] /-- The `cocone` on `direct_limit_diagram` corresponding to the unbundled `direct_limit` of modules. In `direct_limit_is_colimit` we show that it is a colimit cocone. -/ @[simps] def direct_limit_cocone : cocone (direct_limit_diagram G f) := { X := Module.of R $ direct_limit G f, ι := { app := module.direct_limit.of R ι G f, naturality' := λ i j hij, by { ext x, exact direct_limit.of_f } } } /-- The unbundled `direct_limit` of modules is a colimit in the sense of `category_theory`. -/ @[simps] def direct_limit_is_colimit [nonempty ι] : is_colimit (direct_limit_cocone G f) := { desc := λ s, direct_limit.lift R ι G f s.ι.app $ λ i j h x, by { rw [←s.w (hom_of_le h)], refl }, fac' := λ s i, begin ext x, dsimp, exact direct_limit.lift_of s.ι.app _ x, end, uniq' := λ s m h, begin have : s.ι.app = λ i, linear_map.comp m (direct_limit.of R ι (λ i, G i) (λ i j H, f i j H) i), { funext i, rw ← h, refl }, ext x, simp only [this], apply module.direct_limit.lift_unique end } end direct_limit end Module
7f9cba81b616f77eaa7725b88ec7c7c38518232c
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/field_theory/abel_ruffini.lean
724a5a46777a808b8f3f8dd8129f415369bff2b8
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,327
lean
/- Copyright (c) 2020 Thomas Browning and Patrick Lutz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning, Patrick Lutz -/ import group_theory.solvable import field_theory.polynomial_galois_group import ring_theory.roots_of_unity /-! # The Abel-Ruffini Theorem This file proves one direction of the Abel-Ruffini theorem, namely that if an element is solvable by radicals, then its minimal polynomial has solvable Galois group. ## Main definitions * `SBF F E` : the intermediate field of solvable-by-radicals elements ## Main results * `solvable_gal_of_solvable_by_rad` : the minimal polynomial of an element of `SBF F E` has solvable Galois group -/ noncomputable theory open_locale classical open polynomial intermediate_field section abel_ruffini variables {F : Type*} [field F] {E : Type*} [field E] [algebra F E] lemma gal_zero_is_solvable : is_solvable (0 : polynomial F).gal := by apply_instance lemma gal_one_is_solvable : is_solvable (1 : polynomial F).gal := by apply_instance lemma gal_C_is_solvable (x : F) : is_solvable (C x).gal := by apply_instance lemma gal_X_is_solvable : is_solvable (X : polynomial F).gal := by apply_instance lemma gal_X_sub_C_is_solvable (x : F) : is_solvable (X - C x).gal := by apply_instance lemma gal_X_pow_is_solvable (n : ℕ) : is_solvable (X ^ n : polynomial F).gal := by apply_instance lemma gal_mul_is_solvable {p q : polynomial F} (hp : is_solvable p.gal) (hq : is_solvable q.gal) : is_solvable (p * q).gal := solvable_of_solvable_injective (gal.restrict_prod_injective p q) lemma gal_prod_is_solvable {s : multiset (polynomial F)} (hs : ∀ p ∈ s, is_solvable (gal p)) : is_solvable s.prod.gal := begin apply multiset.induction_on' s, { exact gal_one_is_solvable }, { intros p t hps hts ht, rw [multiset.insert_eq_cons, multiset.prod_cons], exact gal_mul_is_solvable (hs p hps) ht }, end lemma gal_is_solvable_of_splits {p q : polynomial F} (hpq : fact (p.splits (algebra_map F q.splitting_field))) (hq : is_solvable q.gal) : is_solvable p.gal := begin haveI : is_solvable (q.splitting_field ≃ₐ[F] q.splitting_field) := hq, exact solvable_of_surjective (alg_equiv.restrict_normal_hom_surjective q.splitting_field), end lemma gal_is_solvable_tower (p q : polynomial F) (hpq : p.splits (algebra_map F q.splitting_field)) (hp : is_solvable p.gal) (hq : is_solvable (q.map (algebra_map F p.splitting_field)).gal) : is_solvable q.gal := begin let K := p.splitting_field, let L := q.splitting_field, haveI : fact (p.splits (algebra_map F L)) := ⟨hpq⟩, let ϕ : (L ≃ₐ[K] L) ≃* (q.map (algebra_map F K)).gal := (is_splitting_field.alg_equiv L (q.map (algebra_map F K))).aut_congr, have ϕ_inj : function.injective ϕ.to_monoid_hom := ϕ.injective, haveI : is_solvable (K ≃ₐ[F] K) := hp, haveI : is_solvable (L ≃ₐ[K] L) := solvable_of_solvable_injective ϕ_inj, exact is_solvable_of_is_scalar_tower F p.splitting_field q.splitting_field, end section gal_X_pow_sub_C lemma gal_X_pow_sub_one_is_solvable (n : ℕ) : is_solvable (X ^ n - 1 : polynomial F).gal := begin by_cases hn : n = 0, { rw [hn, pow_zero, sub_self], exact gal_zero_is_solvable }, have hn' : 0 < n := pos_iff_ne_zero.mpr hn, have hn'' : (X ^ n - 1 : polynomial F) ≠ 0 := λ h, one_ne_zero ((leading_coeff_X_pow_sub_one hn').symm.trans (congr_arg leading_coeff h)), apply is_solvable_of_comm, intros σ τ, ext a ha, rw [mem_root_set hn'', alg_hom.map_sub, aeval_X_pow, aeval_one, sub_eq_zero] at ha, have key : ∀ σ : (X ^ n - 1 : polynomial F).gal, ∃ m : ℕ, σ a = a ^ m, { intro σ, obtain ⟨m, hm⟩ := σ.to_alg_hom.to_ring_hom.map_root_of_unity_eq_pow_self ⟨is_unit.unit (is_unit_of_pow_eq_one a n ha hn'), by { ext, rwa [units.coe_pow, is_unit.unit_spec, subtype.coe_mk n hn'] }⟩, use m, convert hm, all_goals { exact (is_unit.unit_spec _).symm } }, obtain ⟨c, hc⟩ := key σ, obtain ⟨d, hd⟩ := key τ, rw [σ.mul_apply, τ.mul_apply, hc, τ.map_pow, hd, σ.map_pow, hc, ←pow_mul, pow_mul'], end lemma gal_X_pow_sub_C_is_solvable_aux (n : ℕ) (a : F) (h : (X ^ n - 1 : polynomial F).splits (ring_hom.id F)) : is_solvable (X ^ n - C a).gal := begin by_cases ha : a = 0, { rw [ha, C_0, sub_zero], exact gal_X_pow_is_solvable n }, have ha' : algebra_map F (X ^ n - C a).splitting_field a ≠ 0 := mt ((ring_hom.injective_iff _).mp (ring_hom.injective _) a) ha, by_cases hn : n = 0, { rw [hn, pow_zero, ←C_1, ←C_sub], exact gal_C_is_solvable (1 - a) }, have hn' : 0 < n := pos_iff_ne_zero.mpr hn, have hn'' : X ^ n - C a ≠ 0 := λ h, one_ne_zero ((leading_coeff_X_pow_sub_C hn').symm.trans (congr_arg leading_coeff h)), have hn''' : (X ^ n - 1 : polynomial F) ≠ 0 := λ h, one_ne_zero ((leading_coeff_X_pow_sub_one hn').symm.trans (congr_arg leading_coeff h)), have mem_range : ∀ {c}, c ^ n = 1 → ∃ d, algebra_map F (X ^ n - C a).splitting_field d = c := λ c hc, ring_hom.mem_range.mp (minpoly.mem_range_of_degree_eq_one F c (or.resolve_left h hn''' (minpoly.irreducible ((splitting_field.normal (X ^ n - C a)).is_integral c)) (minpoly.dvd F c (by rwa [map_id, alg_hom.map_sub, sub_eq_zero, aeval_X_pow, aeval_one])))), apply is_solvable_of_comm, intros σ τ, ext b hb, rw [mem_root_set hn'', alg_hom.map_sub, aeval_X_pow, aeval_C, sub_eq_zero] at hb, have hb' : b ≠ 0, { intro hb', rw [hb', zero_pow hn'] at hb, exact ha' hb.symm }, have key : ∀ σ : (X ^ n - C a).gal, ∃ c, σ b = b * algebra_map F _ c, { intro σ, have key : (σ b / b) ^ n = 1 := by rw [div_pow, ←σ.map_pow, hb, σ.commutes, div_self ha'], obtain ⟨c, hc⟩ := mem_range key, use c, rw [hc, mul_div_cancel' (σ b) hb'] }, obtain ⟨c, hc⟩ := key σ, obtain ⟨d, hd⟩ := key τ, rw [σ.mul_apply, τ.mul_apply, hc, τ.map_mul, τ.commutes, hd, σ.map_mul, σ.commutes, hc], rw [mul_assoc, mul_assoc, mul_right_inj' hb', mul_comm], end lemma splits_X_pow_sub_one_of_X_pow_sub_C {F : Type*} [field F] {E : Type*} [field E] (i : F →+* E) (n : ℕ) {a : F} (ha : a ≠ 0) (h : (X ^ n - C a).splits i) : (X ^ n - 1).splits i := begin have ha' : i a ≠ 0 := mt (i.injective_iff.mp (i.injective) a) ha, by_cases hn : n = 0, { rw [hn, pow_zero, sub_self], exact splits_zero i }, have hn' : 0 < n := pos_iff_ne_zero.mpr hn, have hn'' : (X ^ n - C a).degree ≠ 0 := ne_of_eq_of_ne (degree_X_pow_sub_C hn' a) (mt with_bot.coe_eq_coe.mp hn), obtain ⟨b, hb⟩ := exists_root_of_splits i h hn'', rw [eval₂_sub, eval₂_X_pow, eval₂_C, sub_eq_zero] at hb, have hb' : b ≠ 0, { intro hb', rw [hb', zero_pow hn'] at hb, exact ha' hb.symm }, let s := ((X ^ n - C a).map i).roots, have hs : _ = _ * (s.map _).prod := eq_prod_roots_of_splits h, rw [leading_coeff_X_pow_sub_C hn', ring_hom.map_one, C_1, one_mul] at hs, have hs' : s.card = n := (nat_degree_eq_card_roots h).symm.trans nat_degree_X_pow_sub_C, apply @splits_of_exists_multiset F E _ _ i (X ^ n - 1) (s.map (λ c : E, c / b)), rw [leading_coeff_X_pow_sub_one hn', ring_hom.map_one, C_1, one_mul, multiset.map_map], have C_mul_C : (C (i a⁻¹)) * (C (i a)) = 1, { rw [←C_mul, ←i.map_mul, inv_mul_cancel ha, i.map_one, C_1] }, have key1 : (X ^ n - 1).map i = C (i a⁻¹) * ((X ^ n - C a).map i).comp (C b * X), { rw [map_sub, map_sub, map_pow, map_X, map_C, map_one, sub_comp, pow_comp, X_comp, C_comp, mul_pow, ←C_pow, hb, mul_sub, ←mul_assoc, C_mul_C, one_mul] }, have key2 : (λ q : polynomial E, q.comp (C b * X)) ∘ (λ c : E, X - C c) = (λ c : E, C b * (X - C (c / b))), { ext1 c, change (X - C c).comp (C b * X) = C b * (X - C (c / b)), rw [sub_comp, X_comp, C_comp, mul_sub, ←C_mul, mul_div_cancel' c hb'] }, rw [key1, hs, prod_comp, multiset.map_map, key2, multiset.prod_map_mul, multiset.map_const, multiset.prod_repeat, hs', ←C_pow, hb, ←mul_assoc, C_mul_C, one_mul], all_goals { exact field.to_nontrivial F }, end lemma gal_X_pow_sub_C_is_solvable (n : ℕ) (x : F) : is_solvable (X ^ n - C x).gal := begin by_cases hx : x = 0, { rw [hx, C_0, sub_zero], exact gal_X_pow_is_solvable n }, apply gal_is_solvable_tower (X ^ n - 1) (X ^ n - C x), { exact splits_X_pow_sub_one_of_X_pow_sub_C _ n hx (splitting_field.splits _) }, { exact gal_X_pow_sub_one_is_solvable n }, { rw [map_sub, map_pow, map_X, map_C], apply gal_X_pow_sub_C_is_solvable_aux, have key := splitting_field.splits (X ^ n - 1 : polynomial F), rwa [←splits_id_iff_splits, map_sub, map_pow, map_X, map_one] at key }, end end gal_X_pow_sub_C variables (F) /-- Inductive definition of solvable by radicals -/ inductive is_solvable_by_rad : E → Prop | base (a : F) : is_solvable_by_rad (algebra_map F E a) | add (a b : E) : is_solvable_by_rad a → is_solvable_by_rad b → is_solvable_by_rad (a + b) | neg (α : E) : is_solvable_by_rad α → is_solvable_by_rad (-α) | mul (α β : E) : is_solvable_by_rad α → is_solvable_by_rad β → is_solvable_by_rad (α * β) | inv (α : E) : is_solvable_by_rad α → is_solvable_by_rad α⁻¹ | rad (α : E) (n : ℕ) (hn : n ≠ 0) : is_solvable_by_rad (α^n) → is_solvable_by_rad α variables (E) /-- The intermediate field of solvable-by-radicals elements -/ def solvable_by_rad : intermediate_field F E := { carrier := is_solvable_by_rad F, zero_mem' := by { convert is_solvable_by_rad.base (0 : F), rw ring_hom.map_zero }, add_mem' := is_solvable_by_rad.add, neg_mem' := is_solvable_by_rad.neg, one_mem' := by { convert is_solvable_by_rad.base (1 : F), rw ring_hom.map_one }, mul_mem' := is_solvable_by_rad.mul, inv_mem' := is_solvable_by_rad.inv, algebra_map_mem' := is_solvable_by_rad.base } namespace solvable_by_rad variables {F} {E} {α : E} lemma induction (P : solvable_by_rad F E → Prop) (base : ∀ α : F, P (algebra_map F (solvable_by_rad F E) α)) (add : ∀ α β : solvable_by_rad F E, P α → P β → P (α + β)) (neg : ∀ α : solvable_by_rad F E, P α → P (-α)) (mul : ∀ α β : solvable_by_rad F E, P α → P β → P (α * β)) (inv : ∀ α : solvable_by_rad F E, P α → P α⁻¹) (rad : ∀ α : solvable_by_rad F E, ∀ n : ℕ, n ≠ 0 → P (α^n) → P α) (α : solvable_by_rad F E) : P α := begin revert α, suffices : ∀ (α : E), is_solvable_by_rad F α → (∃ β : solvable_by_rad F E, ↑β = α ∧ P β), { intro α, obtain ⟨α₀, hα₀, Pα⟩ := this α (subtype.mem α), convert Pα, exact subtype.ext hα₀.symm }, apply is_solvable_by_rad.rec, { exact λ α, ⟨algebra_map F (solvable_by_rad F E) α, rfl, base α⟩ }, { intros α β hα hβ Pα Pβ, obtain ⟨⟨α₀, hα₀, Pα⟩, β₀, hβ₀, Pβ⟩ := ⟨Pα, Pβ⟩, exact ⟨α₀ + β₀, by {rw [←hα₀, ←hβ₀], refl }, add α₀ β₀ Pα Pβ⟩ }, { intros α hα Pα, obtain ⟨α₀, hα₀, Pα⟩ := Pα, exact ⟨-α₀, by {rw ←hα₀, refl }, neg α₀ Pα⟩ }, { intros α β hα hβ Pα Pβ, obtain ⟨⟨α₀, hα₀, Pα⟩, β₀, hβ₀, Pβ⟩ := ⟨Pα, Pβ⟩, exact ⟨α₀ * β₀, by {rw [←hα₀, ←hβ₀], refl }, mul α₀ β₀ Pα Pβ⟩ }, { intros α hα Pα, obtain ⟨α₀, hα₀, Pα⟩ := Pα, exact ⟨α₀⁻¹, by {rw ←hα₀, refl }, inv α₀ Pα⟩ }, { intros α n hn hα Pα, obtain ⟨α₀, hα₀, Pα⟩ := Pα, refine ⟨⟨α, is_solvable_by_rad.rad α n hn hα⟩, rfl, rad _ n hn _⟩, convert Pα, exact subtype.ext (eq.trans ((solvable_by_rad F E).coe_pow _ n) hα₀.symm) } end theorem is_integral (α : solvable_by_rad F E) : is_integral F α := begin revert α, apply solvable_by_rad.induction, { exact λ _, is_integral_algebra_map }, { exact λ _ _, is_integral_add }, { exact λ _, is_integral_neg }, { exact λ _ _, is_integral_mul }, { exact λ α hα, subalgebra.inv_mem_of_algebraic (integral_closure F (solvable_by_rad F E)) (show is_algebraic F ↑(⟨α, hα⟩ : integral_closure F (solvable_by_rad F E)), by exact (is_algebraic_iff_is_integral F).mpr hα) }, { intros α n hn hα, obtain ⟨p, h1, h2⟩ := (is_algebraic_iff_is_integral F).mpr hα, refine (is_algebraic_iff_is_integral F).mp ⟨p.comp (X ^ n), ⟨λ h, h1 (leading_coeff_eq_zero.mp _), by rw [aeval_comp, aeval_X_pow, h2]⟩⟩, rwa [←leading_coeff_eq_zero, leading_coeff_comp, leading_coeff_X_pow, one_pow, mul_one] at h, rwa nat_degree_X_pow } end /-- The statement to be proved inductively -/ def P (α : solvable_by_rad F E) : Prop := is_solvable (minpoly F α).gal /-- An auxiliary induction lemma, which is generalized by `solvable_by_rad.is_solvable`. -/ lemma induction3 {α : solvable_by_rad F E} {n : ℕ} (hn : n ≠ 0) (hα : P (α ^ n)) : P α := begin let p := minpoly F (α ^ n), have hp : p.comp (X ^ n) ≠ 0, { intro h, cases (comp_eq_zero_iff.mp h) with h' h', { exact minpoly.ne_zero (is_integral (α ^ n)) h' }, { exact hn (by rw [←nat_degree_C _, ←h'.2, nat_degree_X_pow]) } }, apply gal_is_solvable_of_splits, { exact ⟨splits_of_splits_of_dvd _ hp (splitting_field.splits (p.comp (X ^ n))) (minpoly.dvd F α (by rw [aeval_comp, aeval_X_pow, minpoly.aeval]))⟩ }, { refine gal_is_solvable_tower p (p.comp (X ^ n)) _ hα _, { exact gal.splits_in_splitting_field_of_comp _ _ (by rwa [nat_degree_X_pow]) }, { obtain ⟨s, hs⟩ := exists_multiset_of_splits _ (splitting_field.splits p), rw [map_comp, map_pow, map_X, hs, mul_comp, C_comp], apply gal_mul_is_solvable (gal_C_is_solvable _), rw prod_comp, apply gal_prod_is_solvable, intros q hq, rw multiset.mem_map at hq, obtain ⟨q, hq, rfl⟩ := hq, rw multiset.mem_map at hq, obtain ⟨q, hq, rfl⟩ := hq, rw [sub_comp, X_comp, C_comp], exact gal_X_pow_sub_C_is_solvable n q } }, end /-- An auxiliary induction lemma, which is generalized by `solvable_by_rad.is_solvable`. -/ lemma induction2 {α β γ : solvable_by_rad F E} (hγ : γ ∈ F⟮α, β⟯) (hα : P α) (hβ : P β) : P γ := begin let p := (minpoly F α), let q := (minpoly F β), have hpq := polynomial.splits_of_splits_mul _ (mul_ne_zero (minpoly.ne_zero (is_integral α)) (minpoly.ne_zero (is_integral β))) (splitting_field.splits (p * q)), let f : F⟮α, β⟯ →ₐ[F] (p * q).splitting_field := classical.choice (alg_hom_mk_adjoin_splits begin intros x hx, cases hx, rw hx, exact ⟨is_integral α, hpq.1⟩, cases hx, exact ⟨is_integral β, hpq.2⟩, end), have key : minpoly F γ = minpoly F (f ⟨γ, hγ⟩) := minpoly.eq_of_irreducible_of_monic (minpoly.irreducible (is_integral γ)) begin suffices : aeval (⟨γ, hγ⟩ : F ⟮α, β⟯) (minpoly F γ) = 0, { rw [aeval_alg_hom_apply, this, alg_hom.map_zero] }, apply (algebra_map F⟮α, β⟯ (solvable_by_rad F E)).injective, rw [ring_hom.map_zero, is_scalar_tower.algebra_map_aeval], exact minpoly.aeval F γ, end (minpoly.monic (is_integral γ)), rw [P, key], exact gal_is_solvable_of_splits ⟨normal.splits (splitting_field.normal _) _⟩ (gal_mul_is_solvable hα hβ), end /-- An auxiliary induction lemma, which is generalized by `solvable_by_rad.is_solvable`. -/ lemma induction1 {α β : solvable_by_rad F E} (hβ : β ∈ F⟮α⟯) (hα : P α) : P β := induction2 (adjoin.mono F _ _ (ge_of_eq (set.pair_eq_singleton α)) hβ) hα hα theorem is_solvable (α : solvable_by_rad F E) : is_solvable (minpoly F α).gal := begin revert α, apply solvable_by_rad.induction, { exact λ α, by { rw minpoly.eq_X_sub_C, exact gal_X_sub_C_is_solvable α } }, { exact λ α β, induction2 (add_mem _ (subset_adjoin F _ (set.mem_insert α _)) (subset_adjoin F _ (set.mem_insert_of_mem α (set.mem_singleton β)))) }, { exact λ α, induction1 (neg_mem _ (mem_adjoin_simple_self F α)) }, { exact λ α β, induction2 (mul_mem _ (subset_adjoin F _ (set.mem_insert α _)) (subset_adjoin F _ (set.mem_insert_of_mem α (set.mem_singleton β)))) }, { exact λ α, induction1 (inv_mem _ (mem_adjoin_simple_self F α)) }, { exact λ α n, induction3 }, end /-- **Abel-Ruffini Theorem** (one direction): An irreducible polynomial with an `is_solvable_by_rad` root has solvable Galois group -/ lemma is_solvable' {α : E} {q : polynomial F} (q_irred : irreducible q) (q_aeval : aeval α q = 0) (hα : is_solvable_by_rad F α) : _root_.is_solvable q.gal := begin haveI : _root_.is_solvable (q * C q.leading_coeff⁻¹).gal, { rw [minpoly.eq_of_irreducible q_irred q_aeval, ←show minpoly F (⟨α, hα⟩ : solvable_by_rad F E) = minpoly F α, from minpoly.eq_of_algebra_map_eq (ring_hom.injective _) (is_integral ⟨α, hα⟩) rfl], exact is_solvable ⟨α, hα⟩ }, refine solvable_of_surjective (gal.restrict_dvd_surjective ⟨C q.leading_coeff⁻¹, rfl⟩ _), rw [mul_ne_zero_iff, ne, ne, C_eq_zero, inv_eq_zero], exact ⟨q_irred.ne_zero, leading_coeff_ne_zero.mpr q_irred.ne_zero⟩, end end solvable_by_rad end abel_ruffini
d2d8af823188efd49980b3d2ed24a2d2280ba15e
be5348f86d661459153802318209304b793c0e2a
/src/insertion.lean
22740f1663c2c93d66f13b98ca3da3d0492fc5c9
[]
no_license
reglayass/lean-avl
6b758c7708bdb3316b1b97ada3e3f259b49da58a
c7bffa75d7548e5ff8cdd7d69f5a58499f883df1
refs/heads/master
1,692,297,536,477
1,633,946,864,000
1,633,946,864,000
340,881,572
4
0
null
null
null
null
UTF-8
Lean
false
false
9,737
lean
import definitions rotations forall_keys tactic.linarith tactic.omega set_option pp.generalized_field_notation false universe u namespace insertion_balanced_lemmas open btree rotation_lemmas forall_keys_lemmas variables {α : Type u} /- Auxilary lemma to show that a previously existing key has the same relation to the tree after insertion -/ lemma forall_insert (k x : nat) (t : btree α) (a : α) (p : nat → nat → Prop) (h : p x k) : forall_keys p x t → forall_keys p x (insert k a t) := begin intro h₁, induction t, case empty { unfold forall_keys, intros k' h₂, simp [btree.insert, bound] at h₂, subst h₂, exact h, }, case node : tl tk ta tr ihl ihr { unfold forall_keys at ihl ihr h₁, simp only [btree.insert], by_cases c₁ : (k < tk), { simp only [if_pos c₁], by_cases c₂ : (height (insert k a tl) > height tr + 1), { simp only [if_pos c₂], apply forall_rotate_right, rw ← forall_keys_char, unfold forall_keys, repeat { split }, { apply ihl, intros k' h₂, apply h₁, simp [bound], tauto, }, { apply h₁, simp [bound], }, { intros k' h₂, apply h₁, simp [bound], tauto, }, }, { simp only [if_neg c₂], rw ← forall_keys_char, repeat { split }, { apply ihl, intros k' h₂, apply h₁, simp [bound], tauto, }, { apply h₁, simp [bound], }, { intros k' h₂, apply h₁, simp [bound], tauto, }, }, }, { simp only [if_neg c₁], by_cases c₂ : (k > tk), { simp only [if_pos c₂], by_cases c₃ : (height (insert k a tr) > height tl + 1), { simp only [if_pos c₃], apply forall_rotate_left, rw ← forall_keys_char, unfold forall_keys, repeat { split }, { intros k' h₂, apply h₁, simp [bound], tauto, }, { apply h₁, simp [bound], }, { apply ihr, intros k' h₂, apply h₁, simp [bound], tauto, }, }, { simp only [if_neg c₃], rw ← forall_keys_char, unfold forall_keys, repeat { split }, { intros k' h₂, apply h₁, simp [bound], tauto, }, { apply h₁, simp [bound], }, { apply ihr, intros k' h₂, apply h₁, simp [bound], tauto, }, }, }, { simp only [if_neg c₂], have h : k = tk := by linarith, subst h, exact h₁, }, }, }, end -- /- Insertion preserves tree order -/ lemma insert_ordered (t : btree α) (k : nat) (v : α) : ordered t → ordered (insert k v t) := begin intro h₁, induction t, case empty { simp [btree.insert, ordered, forall_keys], split, repeat { intros k' h₂, simp [bound] at h₂, contradiction, }, }, case node : tl tk ta tr ihl ihr { simp only [btree.insert], by_cases c₁ : (k < tk), { simp only [if_pos c₁], by_cases c₂ : (height (insert k v tl) > height tr + 1), { simp only [if_pos c₂], apply rotate_right_ordered, simp only [ordered] at h₁ ⊢, cases_matching* (_ ∧ _), repeat { split }; try { assumption }, { apply ihl, exact h₁_left, }, { apply forall_insert; assumption, }, }, { simp only [if_neg c₂], simp only [ordered] at h₁ ⊢, cases_matching* (_ ∧ _), repeat { split }; try { assumption }, { apply ihl, exact h₁_left, }, { apply forall_insert; assumption, }, }, }, { simp only [if_neg c₁], by_cases c₂ : (k > tk), { simp only [if_pos c₂], by_cases c₃ : (height (insert k v tr) > height tl + 1), { simp only [if_pos c₃], apply rotate_left_ordered, simp only [ordered] at h₁ ⊢, cases_matching* (_ ∧ _), repeat { split }; try { assumption }, { apply ihr, exact h₁_right_left, }, { apply forall_insert; assumption, } }, { simp only [if_neg c₃], simp only [ordered] at h₁ ⊢, cases_matching* (_ ∧ _), repeat { split }; try { assumption }, { apply ihr, exact h₁_right_left, }, { apply forall_insert; assumption, }, }, }, { simp only [if_neg c₂], have h : k = tk := by linarith, subst h, exact h₁, }, }, }, end /- A key is bound to the tree immediately after insertion -/ lemma insert_bound (t : btree α) (k : nat) (v : α) : bound k (insert k v t) = tt := begin induction t, case empty { simp [btree.insert, bound], }, case node : tl tk ta tr ihl ihr { simp [btree.insert], by_cases c₁ : (k < tk), { simp only [if_pos c₁], by_cases c₂ : (height tr + 1 < height (insert k v tl)), { simp only [if_pos c₂], rw ← rotate_right_keys, simp [bound], tauto, }, { simp only [if_neg c₂], simp [bound], tauto, }, }, { simp only [if_neg c₁], by_cases c₂ : (tk < k), { simp only [if_pos c₂], by_cases c₃ : (height tl + 1 < height (insert k v tr)), { simp only [if_pos c₃], rw ← rotate_left_keys, simp [bound], tauto, }, { simp only [if_neg c₃], simp [bound], tauto, }, }, { simp [if_neg c₂, bound], }, }, }, end /- Insertion does not lose any other keys after insertion -/ lemma insert_diff_bound (t : btree α) (k x : nat) (v : α) : bound x t = tt → bound x (insert k v t) = tt := begin intro h₁, induction t, case empty { simp [btree.insert, bound], simp [bound] at h₁, contradiction, }, case node : tl tk ta tr ihl ihr { simp only [btree.insert], by_cases c₁ : (k < tk), { simp only [if_pos c₁], by_cases c₂ : (height (insert k v tl) > height tr + 1), { simp only [if_pos c₂], rw ← rotate_right_keys, simp [bound], simp [bound] at h₁, tauto, }, { simp only [if_neg c₂], simp [bound], simp [bound] at h₁, tauto, }, }, { simp only [if_neg c₁], by_cases c₂ : (k > tk), { simp only [if_pos c₂], by_cases c₃ : (height (insert k v tr) > height tl + 1), { simp only [if_pos c₃], rw ← rotate_left_keys, simp [bound], simp [bound] at h₁, tauto, }, { simp only [if_neg c₃], simp [bound], simp [bound] at h₁, tauto, }, }, { simp only [if_neg c₂], simp [bound], simp [bound] at h₁, have h : k = tk := by linarith, subst h, exact h₁, }, }, }, end /- Insertion doesn't create any new keys except the ones specified -/ lemma insert_nbound (t : btree α) (k x : nat) (v : α) : (bound x t = ff ∧ x ≠ k) → bound x (insert k v t) = ff := begin intro h₁, induction t, case empty { simp [btree.insert, bound], finish, }, case node : tl tk ta tr ihl ihr { simp only [btree.insert], by_cases c₁ : (k < tk), { simp only [if_pos c₁], by_cases c₂ : (height (insert k v tl) > height tr + 1), { simp only [if_pos c₂], rw ← rotate_right_keys, simp [bound] at *, tauto, }, { simp only [if_neg c₂], simp [bound] at *, tauto, }, }, { simp only [if_neg c₁], by_cases c₂ : (tk < k), { simp only [if_pos c₂], by_cases c₃ : (height (insert k v tr) > height tl + 1), { simp only [if_pos c₃], rw ← rotate_left_keys, simp [bound] at *, tauto, }, { simp only [if_neg c₃], simp [bound] at *, tauto, }, }, { simp only [if_neg c₂], simp [bound] at *, tauto, }, }, }, end /- Insertion preserves balance -/ lemma insert_balanced (t : btree α) (k : nat) (v : α) : balanced t = tt → balanced (insert k v t) = tt := begin intro h₁, cases t, case empty { simp [btree.insert, balanced], }, case node : tl tk ta tr { simp only [btree.insert], by_cases c₁ : (k < tk), { simp only [if_pos c₁], by_cases c₂ : (height (insert k v tl) > height tr + 1), { simp only [if_pos c₂], apply rotate_right_balanced, sorry, }, { simp only [if_neg c₂], simp [balanced, height] at h₁ ⊢, sorry, }, }, { simp only [if_neg c₁], by_cases c₂ : (k > tk), { simp only [if_pos c₂], by_cases c₃ : (height (insert k v tr) > height tl + 1), { simp only [if_pos c₃], apply rotate_left_balanced, sorry, }, { simp only [if_neg c₃], simp [balanced], sorry, }, }, { simp only [if_neg c₂], have h : k = tk := by linarith, subst h, exact h₁, }, }, }, end end insertion_balanced_lemmas
412cf9190e79f72a3ccff1037bd03192e71ed8b5
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/logic/function/basic.lean
b0a9ccf46b085765d61baf737379f81491dc6652
[ "Apache-2.0" ]
permissive
dupuisf/mathlib
62de4ec6544bf3b79086afd27b6529acfaf2c1bb
8582b06b0a5d06c33ee07d0bdf7c646cae22cf36
refs/heads/master
1,669,494,854,016
1,595,692,409,000
1,595,692,409,000
272,046,630
0
0
Apache-2.0
1,592,066,143,000
1,592,066,142,000
null
UTF-8
Lean
false
false
16,131
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 u} {β : Sort v} {f : α → β} 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 lemma comp_apply {α : Sort u} {β : Sort v} {φ : Sort w} (f : β → φ) (g : α → β) (a : α) : (f ∘ g) a = f (g a) := rfl @[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 {γ : Sort w} {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 {γ : Sort w} {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 right_inverse.comp_eq_id {f : α → β} {g : β → α} (h : right_inverse f g) : g ∘ f = id := funext h 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 @[simp] lemma update_same (a : α) (v : β a) (f : Πa, β a) : update f a v a = v := dif_pos rfl @[simp] lemma update_noteq {a a' : α} (h : a ≠ a') (v : β a') (f : Πa, β a) : update f a' v a = f a := dif_neg h @[simp] lemma update_eq_self (a : α) (f : Πa, β a) : update f a (f a) = f := begin refine funext (λi, _), by_cases h : i = a, { rw h, simp }, { simp [h] } end lemma update_comp {β : Sort v} (f : α → β) {g : α' → α} (hg : injective g) (a : α') (v : β) : (update f (g a) v) ∘ g = update (f ∘ g) a v := begin refine funext (λi, _), by_cases h : i = a, { rw h, simp }, { simp [h, hg.ne] } end lemma comp_update {α' : Sort*} {β : Sort*} (f : α' → β) (g : α → α') (i : α) (v : α') : f ∘ (update g i v) = update (f ∘ g) i (f v) := begin refine funext (λj, _), by_cases h : j = i, { rw h, simp }, { simp [h] } end 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 [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 lemma uncurry_def {α β γ} (f : α → β → γ) : uncurry f = (λp, f p.1 p.2) := rfl section bicomp variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {ε : 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 /-- 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) 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⟩ 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 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