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
b947c5372a7f113d21c3ae9a5758ce0f2260f9b2
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/data/fun_like/basic.lean
802503b21e806add6841f8fb4f6d372106c62c31
[ "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,629
lean
/- Copyright (c) 2021 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import logic.function.basic import tactic.lint import tactic.norm_cast /-! # Typeclass for a type `F` with an injective map to `A → B` This typeclass is primarily for use by homomorphisms like `monoid_hom` and `linear_map`. ## Basic usage of `fun_like` A typical type of morphisms should be declared as: ``` structure my_hom (A B : Type*) [my_class A] [my_class B] := (to_fun : A → B) (map_op' : ∀ {x y : A}, to_fun (my_class.op x y) = my_class.op (to_fun x) (to_fun y)) namespace my_hom variables (A B : Type*) [my_class A] [my_class B] -- This instance is optional if you follow the "morphism class" design below: instance : fun_like (my_hom A B) A (λ _, B) := { coe := my_hom.to_fun, coe_injective' := λ f g h, by cases f; cases g; congr' } /-- Helper instance for when there's too many metavariables to apply `to_fun.to_coe_fn` directly. -/ instance : has_coe_to_fun (my_hom A B) (λ _, A → B) := to_fun.to_coe_fn @[simp] lemma to_fun_eq_coe {f : my_hom A B} : f.to_fun = (f : A → B) := rfl @[ext] theorem ext {f g : my_hom A B} (h : ∀ x, f x = g x) : f = g := fun_like.ext f g h /-- Copy of a `my_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : my_hom A B) (f' : A → B) (h : f' = ⇑f) : my_hom A B := { to_fun := f', map_op' := h.symm ▸ f.map_op' } end my_hom ``` This file will then provide a `has_coe_to_fun` instance and various extensionality and simp lemmas. ## Morphism classes extending `fun_like` The `fun_like` design provides further benefits if you put in a bit more work. The first step is to extend `fun_like` to create a class of those types satisfying the axioms of your new type of morphisms. Continuing the example above: ``` /-- `my_hom_class F A B` states that `F` is a type of `my_class.op`-preserving morphisms. You should extend this class when you extend `my_hom`. -/ class my_hom_class (F : Type*) (A B : out_param $ Type*) [my_class A] [my_class B] extends fun_like F A (λ _, B) := (map_op : ∀ (f : F) (x y : A), f (my_class.op x y) = my_class.op (f x) (f y)) @[simp] lemma map_op {F A B : Type*} [my_class A] [my_class B] [my_hom_class F A B] (f : F) (x y : A) : f (my_class.op x y) = my_class.op (f x) (f y) := my_hom_class.map_op -- You can replace `my_hom.fun_like` with the below instance: instance : my_hom_class (my_hom A B) A B := { coe := my_hom.to_fun, coe_injective' := λ f g h, by cases f; cases g; congr', map_op := my_hom.map_op' } -- [Insert `has_coe_to_fun`, `to_fun_eq_coe`, `ext` and `copy` here] ``` The second step is to add instances of your new `my_hom_class` for all types extending `my_hom`. Typically, you can just declare a new class analogous to `my_hom_class`: ``` structure cooler_hom (A B : Type*) [cool_class A] [cool_class B] extends my_hom A B := (map_cool' : to_fun cool_class.cool = cool_class.cool) class cooler_hom_class (F : Type*) (A B : out_param $ Type*) [cool_class A] [cool_class B] extends my_hom_class F A B := (map_cool : ∀ (f : F), f cool_class.cool = cool_class.cool) @[simp] lemma map_cool {F A B : Type*} [cool_class A] [cool_class B] [cooler_hom_class F A B] (f : F) : f cool_class.cool = cool_class.cool := my_hom_class.map_op -- You can also replace `my_hom.fun_like` with the below instance: instance : cool_hom_class (cool_hom A B) A B := { coe := cool_hom.to_fun, coe_injective' := λ f g h, by cases f; cases g; congr', map_op := cool_hom.map_op', map_cool := cool_hom.map_cool' } -- [Insert `has_coe_to_fun`, `to_fun_eq_coe`, `ext` and `copy` here] ``` Then any declaration taking a specific type of morphisms as parameter can instead take the class you just defined: ``` -- Compare with: lemma do_something (f : my_hom A B) : sorry := sorry lemma do_something {F : Type*} [my_hom_class F A B] (f : F) : sorry := sorry ``` This means anything set up for `my_hom`s will automatically work for `cool_hom_class`es, and defining `cool_hom_class` only takes a constant amount of effort, instead of linearly increasing the work per `my_hom`-related declaration. -/ -- This instance should have low priority, to ensure we follow the chain -- `fun_like → has_coe_to_fun` attribute [instance, priority 10] coe_fn_trans /-- The class `fun_like F α β` expresses that terms of type `F` have an injective coercion to functions from `α` to `β`. This typeclass is used in the definition of the homomorphism typeclasses, such as `zero_hom_class`, `mul_hom_class`, `monoid_hom_class`, .... -/ class fun_like (F : Sort*) (α : out_param Sort*) (β : out_param $ α → Sort*) := (coe : F → Π a : α, β a) (coe_injective' : function.injective coe) section dependent /-! ### `fun_like F α β` where `β` depends on `a : α` -/ variables (F α : Sort*) (β : α → Sort*) namespace fun_like variables {F α β} [i : fun_like F α β] include i @[priority 100, -- Give this a priority between `coe_fn_trans` and the default priority nolint dangerous_instance] -- `α` and `β` are out_params, so this instance should not be dangerous instance : has_coe_to_fun F (λ _, Π a : α, β a) := { coe := fun_like.coe } @[simp] lemma coe_eq_coe_fn : (fun_like.coe : F → Π a : α, β a) = coe_fn := rfl theorem coe_injective : function.injective (coe_fn : F → Π a : α, β a) := fun_like.coe_injective' @[simp, norm_cast] theorem coe_fn_eq {f g : F} : (f : Π a : α, β a) = (g : Π a : α, β a) ↔ f = g := ⟨λ h, @coe_injective _ _ _ i _ _ h, λ h, by cases h; refl⟩ theorem ext' {f g : F} (h : (f : Π a : α, β a) = (g : Π a : α, β a)) : f = g := coe_injective h theorem ext'_iff {f g : F} : f = g ↔ ((f : Π a : α, β a) = (g : Π a : α, β a)) := coe_fn_eq.symm theorem ext (f g : F) (h : ∀ (x : α), f x = g x) : f = g := coe_injective (funext h) theorem ext_iff {f g : F} : f = g ↔ (∀ x, f x = g x) := coe_fn_eq.symm.trans function.funext_iff protected lemma congr_fun {f g : F} (h₁ : f = g) (x : α) : f x = g x := congr_fun (congr_arg _ h₁) x end fun_like end dependent section non_dependent /-! ### `fun_like F α (λ _, β)` where `β` does not depend on `a : α` -/ variables {F α β : Sort*} [i : fun_like F α (λ _, β)] include i namespace fun_like protected lemma congr {f g : F} {x y : α} (h₁ : f = g) (h₂ : x = y) : f x = g y := congr (congr_arg _ h₁) h₂ protected lemma congr_arg (f : F) {x y : α} (h₂ : x = y) : f x = f y := congr_arg _ h₂ end fun_like end non_dependent
ce4e2dcb740ed838ee087746103b2e311b86bbd1
63abd62053d479eae5abf4951554e1064a4c45b4
/src/data/finset/lattice.lean
a40cba8bfc5a1bd6f3ef568a545c4e3c85c2df60
[ "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
24,935
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import data.finset.fold import data.multiset.lattice import order.order_dual /-! # Lattice operations on finsets -/ variables {α β γ : Type*} namespace finset open multiset order_dual /-! ### sup -/ section sup variables [semilattice_sup_bot α] /-- Supremum of a finite set: `sup {a, b, c} f = f a ⊔ f b ⊔ f c` -/ def sup (s : finset β) (f : β → α) : α := s.fold (⊔) ⊥ f variables {s s₁ s₂ : finset β} {f : β → α} lemma sup_def : s.sup f = (s.1.map f).sup := rfl @[simp] lemma sup_empty : (∅ : finset β).sup f = ⊥ := fold_empty @[simp] lemma sup_insert [decidable_eq β] {b : β} : (insert b s : finset β).sup f = f b ⊔ s.sup f := fold_insert_idem @[simp] lemma sup_singleton {b : β} : ({b} : finset β).sup f = f b := sup_singleton lemma sup_union [decidable_eq β] : (s₁ ∪ s₂).sup f = s₁.sup f ⊔ s₂.sup f := finset.induction_on s₁ (by rw [empty_union, sup_empty, bot_sup_eq]) $ λ a s has ih, by rw [insert_union, sup_insert, sup_insert, ih, sup_assoc] theorem sup_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀a∈s₂, f a = g a) : s₁.sup f = s₂.sup g := by subst hs; exact finset.fold_congr hfg @[simp] lemma sup_le_iff {a : α} : s.sup f ≤ a ↔ (∀b ∈ s, f b ≤ a) := begin apply iff.trans multiset.sup_le, simp only [multiset.mem_map, and_imp, exists_imp_distrib], exact ⟨λ k b hb, k _ _ hb rfl, λ k a' b hb h, h ▸ k _ hb⟩, end lemma sup_le {a : α} : (∀b ∈ s, f b ≤ a) → s.sup f ≤ a := sup_le_iff.2 lemma le_sup {b : β} (hb : b ∈ s) : f b ≤ s.sup f := sup_le_iff.1 (le_refl _) _ hb lemma sup_mono_fun {g : β → α} (h : ∀b∈s, f b ≤ g b) : s.sup f ≤ s.sup g := sup_le (λ b hb, le_trans (h b hb) (le_sup hb)) lemma sup_mono (h : s₁ ⊆ s₂) : s₁.sup f ≤ s₂.sup f := sup_le $ assume b hb, le_sup (h hb) @[simp] lemma sup_lt_iff [is_total α (≤)] {a : α} (ha : ⊥ < a) : s.sup f < a ↔ (∀b ∈ s, f b < a) := by letI := classical.dec_eq β; from ⟨ λh b hb, lt_of_le_of_lt (le_sup hb) h, finset.induction_on s (by simp [ha]) (by simp {contextual := tt}) ⟩ lemma comp_sup_eq_sup_comp [semilattice_sup_bot γ] {s : finset β} {f : β → α} (g : α → γ) (g_sup : ∀ x y, g (x ⊔ y) = g x ⊔ g y) (bot : g ⊥ = ⊥) : g (s.sup f) = s.sup (g ∘ f) := by letI := classical.dec_eq β; from finset.induction_on s (by simp [bot]) (by simp [g_sup] {contextual := tt}) lemma comp_sup_eq_sup_comp_of_is_total [is_total α (≤)] {γ : Type} [semilattice_sup_bot γ] (g : α → γ) (mono_g : monotone g) (bot : g ⊥ = ⊥) : g (s.sup f) = s.sup (g ∘ f) := comp_sup_eq_sup_comp g mono_g.map_sup bot /-- Computating `sup` in a subtype (closed under `sup`) is the same as computing it in `α`. -/ lemma sup_coe {P : α → Prop} {Pbot : P ⊥} {Psup : ∀{{x y}}, P x → P y → P (x ⊔ y)} (t : finset β) (f : β → {x : α // P x}) : (@sup _ _ (subtype.semilattice_sup_bot Pbot Psup) t f : α) = t.sup (λ x, f x) := by { classical, rw [comp_sup_eq_sup_comp coe]; intros; refl } theorem subset_range_sup_succ (s : finset ℕ) : s ⊆ range (s.sup id).succ := λ n hn, mem_range.2 $ nat.lt_succ_of_le $ le_sup hn theorem exists_nat_subset_range (s : finset ℕ) : ∃n : ℕ, s ⊆ range n := ⟨_, s.subset_range_sup_succ⟩ lemma mem_sup {α β} [decidable_eq β] {s : finset α} {f : α → multiset β} {x : β} : x ∈ s.sup f ↔ ∃ v ∈ s, x ∈ f v := begin classical, apply s.induction_on, { simp }, { intros a s has hxs, rw [finset.sup_insert, multiset.sup_eq_union, multiset.mem_union], split, { intro hxi, cases hxi with hf hf, { refine ⟨a, _, hf⟩, simp only [true_or, eq_self_iff_true, finset.mem_insert] }, { rcases hxs.mp hf with ⟨v, hv, hfv⟩, refine ⟨v, _, hfv⟩, simp only [hv, or_true, finset.mem_insert] } }, { rintros ⟨v, hv, hfv⟩, rw [finset.mem_insert] at hv, rcases hv with rfl | hv, { exact or.inl hfv }, { refine or.inr (hxs.mpr ⟨v, hv, hfv⟩) } } }, end lemma sup_subset {α β} [semilattice_sup_bot β] {s t : finset α} (hst : s ⊆ t) (f : α → β) : s.sup f ≤ t.sup f := by classical; calc t.sup f = (s ∪ t).sup f : by rw [finset.union_eq_right_iff_subset.mpr hst] ... = s.sup f ⊔ t.sup f : by rw finset.sup_union ... ≥ s.sup f : le_sup_left end sup lemma sup_eq_supr [complete_lattice β] (s : finset α) (f : α → β) : s.sup f = (⨆a∈s, f a) := le_antisymm (finset.sup_le $ assume a ha, le_supr_of_le a $ le_supr _ ha) (supr_le $ assume a, supr_le $ assume ha, le_sup ha) /-! ### inf -/ section inf variables [semilattice_inf_top α] /-- Infimum of a finite set: `inf {a, b, c} f = f a ⊓ f b ⊓ f c` -/ def inf (s : finset β) (f : β → α) : α := s.fold (⊓) ⊤ f variables {s s₁ s₂ : finset β} {f : β → α} lemma inf_def : s.inf f = (s.1.map f).inf := rfl @[simp] lemma inf_empty : (∅ : finset β).inf f = ⊤ := fold_empty lemma le_inf_iff {a : α} : a ≤ s.inf f ↔ ∀b ∈ s, a ≤ f b := @sup_le_iff (order_dual α) _ _ _ _ _ @[simp] lemma inf_insert [decidable_eq β] {b : β} : (insert b s : finset β).inf f = f b ⊓ s.inf f := fold_insert_idem @[simp] lemma inf_singleton {b : β} : ({b} : finset β).inf f = f b := inf_singleton lemma inf_union [decidable_eq β] : (s₁ ∪ s₂).inf f = s₁.inf f ⊓ s₂.inf f := @sup_union (order_dual α) _ _ _ _ _ _ theorem inf_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀a∈s₂, f a = g a) : s₁.inf f = s₂.inf g := by subst hs; exact finset.fold_congr hfg lemma inf_le {b : β} (hb : b ∈ s) : s.inf f ≤ f b := le_inf_iff.1 (le_refl _) _ hb lemma le_inf {a : α} : (∀b ∈ s, a ≤ f b) → a ≤ s.inf f := le_inf_iff.2 lemma inf_mono_fun {g : β → α} (h : ∀b∈s, f b ≤ g b) : s.inf f ≤ s.inf g := le_inf (λ b hb, le_trans (inf_le hb) (h b hb)) lemma inf_mono (h : s₁ ⊆ s₂) : s₂.inf f ≤ s₁.inf f := le_inf $ assume b hb, inf_le (h hb) lemma lt_inf_iff [h : is_total α (≤)] {a : α} (ha : a < ⊤) : a < s.inf f ↔ (∀b ∈ s, a < f b) := @sup_lt_iff (order_dual α) _ _ _ _ (@is_total.swap α _ h) _ ha lemma comp_inf_eq_inf_comp [semilattice_inf_top γ] {s : finset β} {f : β → α} (g : α → γ) (g_inf : ∀ x y, g (x ⊓ y) = g x ⊓ g y) (top : g ⊤ = ⊤) : g (s.inf f) = s.inf (g ∘ f) := @comp_sup_eq_sup_comp (order_dual α) _ (order_dual γ) _ _ _ _ _ g_inf top lemma comp_inf_eq_inf_comp_of_is_total [h : is_total α (≤)] {γ : Type} [semilattice_inf_top γ] (g : α → γ) (mono_g : monotone g) (top : g ⊤ = ⊤) : g (s.inf f) = s.inf (g ∘ f) := comp_inf_eq_inf_comp g mono_g.map_inf top /-- Computating `inf` in a subtype (closed under `inf`) is the same as computing it in `α`. -/ lemma inf_coe {P : α → Prop} {Ptop : P ⊤} {Pinf : ∀{{x y}}, P x → P y → P (x ⊓ y)} (t : finset β) (f : β → {x : α // P x}) : (@inf _ _ (subtype.semilattice_inf_top Ptop Pinf) t f : α) = t.inf (λ x, f x) := by { classical, rw [comp_inf_eq_inf_comp coe]; intros; refl } end inf lemma inf_eq_infi [complete_lattice β] (s : finset α) (f : α → β) : s.inf f = (⨅a∈s, f a) := @sup_eq_supr _ (order_dual β) _ _ _ /-! ### max and min of finite sets -/ section max_min variables [linear_order α] /-- Let `s` be a finset in a linear order. Then `s.max` is the maximum of `s` if `s` is not empty, and `none` otherwise. It belongs to `option α`. If you want to get an element of `α`, see `s.max'`. -/ protected def max : finset α → option α := fold (option.lift_or_get max) none some theorem max_eq_sup_with_bot (s : finset α) : s.max = @sup (with_bot α) α _ s some := rfl @[simp] theorem max_empty : (∅ : finset α).max = none := rfl @[simp] theorem max_insert {a : α} {s : finset α} : (insert a s).max = option.lift_or_get max (some a) s.max := fold_insert_idem @[simp] theorem max_singleton {a : α} : finset.max {a} = some a := by { rw [← insert_emptyc_eq], exact max_insert } theorem max_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b, b ∈ s.max := (@le_sup (with_bot α) _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst theorem max_of_nonempty {s : finset α} (h : s.nonempty) : ∃ a, a ∈ s.max := let ⟨a, ha⟩ := h in max_of_mem ha theorem max_eq_none {s : finset α} : s.max = none ↔ s = ∅ := ⟨λ h, s.eq_empty_or_nonempty.elim id (λ H, let ⟨a, ha⟩ := max_of_nonempty H in by rw h at ha; cases ha), λ h, h.symm ▸ max_empty⟩ theorem mem_of_max {s : finset α} : ∀ {a : α}, a ∈ s.max → a ∈ s := finset.induction_on s (λ _ H, by cases H) (λ b s _ (ih : ∀ {a}, a ∈ s.max → a ∈ s) a (h : a ∈ (insert b s).max), begin by_cases p : b = a, { induction p, exact mem_insert_self b s }, { cases option.lift_or_get_choice max_choice (some b) s.max with q q; rw [max_insert, q] at h, { cases h, cases p rfl }, { exact mem_insert_of_mem (ih h) } } end) theorem le_max_of_mem {s : finset α} {a b : α} (h₁ : a ∈ s) (h₂ : b ∈ s.max) : a ≤ b := by rcases @le_sup (with_bot α) _ _ _ _ _ h₁ _ rfl with ⟨b', hb, ab⟩; cases h₂.symm.trans hb; assumption /-- Let `s` be a finset in a linear order. Then `s.min` is the minimum of `s` if `s` is not empty, and `none` otherwise. It belongs to `option α`. If you want to get an element of `α`, see `s.min'`. -/ protected def min : finset α → option α := fold (option.lift_or_get min) none some theorem min_eq_inf_with_top (s : finset α) : s.min = @inf (with_top α) α _ s some := rfl @[simp] theorem min_empty : (∅ : finset α).min = none := rfl @[simp] theorem min_insert {a : α} {s : finset α} : (insert a s).min = option.lift_or_get min (some a) s.min := fold_insert_idem @[simp] theorem min_singleton {a : α} : finset.min {a} = some a := by { rw ← insert_emptyc_eq, exact min_insert } theorem min_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b, b ∈ s.min := (@inf_le (with_top α) _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst theorem min_of_nonempty {s : finset α} (h : s.nonempty) : ∃ a, a ∈ s.min := let ⟨a, ha⟩ := h in min_of_mem ha theorem min_eq_none {s : finset α} : s.min = none ↔ s = ∅ := ⟨λ h, s.eq_empty_or_nonempty.elim id (λ H, let ⟨a, ha⟩ := min_of_nonempty H in by rw h at ha; cases ha), λ h, h.symm ▸ min_empty⟩ theorem mem_of_min {s : finset α} : ∀ {a : α}, a ∈ s.min → a ∈ s := finset.induction_on s (λ _ H, by cases H) $ λ b s _ (ih : ∀ {a}, a ∈ s.min → a ∈ s) a (h : a ∈ (insert b s).min), begin by_cases p : b = a, { induction p, exact mem_insert_self b s }, { cases option.lift_or_get_choice min_choice (some b) s.min with q q; rw [min_insert, q] at h, { cases h, cases p rfl }, { exact mem_insert_of_mem (ih h) } } end theorem min_le_of_mem {s : finset α} {a b : α} (h₁ : b ∈ s) (h₂ : a ∈ s.min) : a ≤ b := by rcases @inf_le (with_top α) _ _ _ _ _ h₁ _ rfl with ⟨b', hb, ab⟩; cases h₂.symm.trans hb; assumption /-- Given a nonempty finset `s` in a linear order `α `, then `s.min' h` is its minimum, as an element of `α`, where `h` is a proof of nonemptiness. Without this assumption, use instead `s.min`, taking values in `option α`. -/ def min' (s : finset α) (H : s.nonempty) : α := @option.get _ s.min $ let ⟨k, hk⟩ := H in let ⟨b, hb⟩ := min_of_mem hk in by simp at hb; simp [hb] /-- Given a nonempty finset `s` in a linear order `α `, then `s.max' h` is its maximum, as an element of `α`, where `h` is a proof of nonemptiness. Without this assumption, use instead `s.max`, taking values in `option α`. -/ def max' (s : finset α) (H : s.nonempty) : α := @option.get _ s.max $ let ⟨k, hk⟩ := H in let ⟨b, hb⟩ := max_of_mem hk in by simp at hb; simp [hb] variables (s : finset α) (H : s.nonempty) theorem min'_mem : s.min' H ∈ s := mem_of_min $ by simp [min'] theorem min'_le (x) (H2 : x ∈ s) : s.min' ⟨x, H2⟩ ≤ x := min_le_of_mem H2 $ option.get_mem _ theorem le_min' (x) (H2 : ∀ y ∈ s, x ≤ y) : x ≤ s.min' H := H2 _ $ min'_mem _ _ /-- `{a}.min' _` is `a`. -/ @[simp] lemma min'_singleton (a : α) : ({a} : finset α).min' (singleton_nonempty _) = a := by simp [min'] theorem max'_mem : s.max' H ∈ s := mem_of_max $ by simp [max'] theorem le_max' (x) (H2 : x ∈ s) : x ≤ s.max' ⟨x, H2⟩ := le_max_of_mem H2 $ option.get_mem _ theorem max'_le (x) (H2 : ∀ y ∈ s, y ≤ x) : s.max' H ≤ x := H2 _ $ max'_mem _ _ /-- `{a}.max' _` is `a`. -/ @[simp] lemma max'_singleton (a : α) : ({a} : finset α).max' (singleton_nonempty _) = a := by simp [max'] theorem min'_lt_max' {i j} (H1 : i ∈ s) (H2 : j ∈ s) (H3 : i ≠ j) : s.min' ⟨i, H1⟩ < s.max' ⟨i, H1⟩ := begin rcases lt_trichotomy i j with H4 | H4 | H4, { have H5 := min'_le s i H1, have H6 := le_max' s j H2, apply lt_of_le_of_lt H5, apply lt_of_lt_of_le H4 H6 }, { cc }, { have H5 := min'_le s j H2, have H6 := le_max' s i H1, apply lt_of_le_of_lt H5, apply lt_of_lt_of_le H4 H6 } end /-- If there's more than 1 element, the min' is less than the max'. An alternate version of `min'_lt_max'` which is sometimes more convenient. -/ lemma min'_lt_max'_of_card (h₂ : 1 < card s) : s.min' (finset.card_pos.mp $ lt_trans zero_lt_one h₂) < s.max' (finset.card_pos.mp $ lt_trans zero_lt_one h₂) := begin apply lt_of_not_ge, intro a, apply not_le_of_lt h₂ (le_of_eq _), rw card_eq_one, use (max' s (finset.card_pos.mp $ lt_trans zero_lt_one h₂)), rw eq_singleton_iff_unique_mem, refine ⟨max'_mem _ _, λ t Ht, le_antisymm (le_max' s t Ht) (le_trans a (min'_le s t Ht))⟩, end lemma max'_eq_of_dual_min' {s : finset α} (hs : s.nonempty) : max' s hs = of_dual (min' (image to_dual s) (nonempty.image hs to_dual)) := begin rw [of_dual, to_dual, equiv.coe_fn_mk, equiv.coe_fn_symm_mk, id.def], simp_rw (@image_id (order_dual α) (s : finset (order_dual α))), refl, end lemma min'_eq_of_dual_max' {s : finset α} (hs : s.nonempty) : min' s hs = of_dual (max' (image to_dual s) (nonempty.image hs to_dual)) := begin rw [of_dual, to_dual, equiv.coe_fn_mk, equiv.coe_fn_symm_mk, id.def], simp_rw (@image_id (order_dual α) (s : finset (order_dual α))), refl, end @[simp] lemma of_dual_max_eq_min_of_dual {a b : α} : of_dual (max a b) = min (of_dual a) (of_dual b) := rfl @[simp] lemma of_dual_min_eq_max_of_dual {a b : α} : of_dual (min a b) = max (of_dual a) (of_dual b) := rfl end max_min section exists_max_min variables [linear_order α] lemma exists_max_image (s : finset β) (f : β → α) (h : s.nonempty) : ∃ x ∈ s, ∀ x' ∈ s, f x' ≤ f x := begin cases max_of_nonempty (h.image f) with y hy, rcases mem_image.mp (mem_of_max hy) with ⟨x, hx, rfl⟩, exact ⟨x, hx, λ x' hx', le_max_of_mem (mem_image_of_mem f hx') hy⟩, end lemma exists_min_image (s : finset β) (f : β → α) (h : s.nonempty) : ∃ x ∈ s, ∀ x' ∈ s, f x ≤ f x' := @exists_max_image (order_dual α) β _ s f h end exists_max_min end finset namespace multiset lemma count_sup [decidable_eq β] (s : finset α) (f : α → multiset β) (b : β) : count b (s.sup f) = s.sup (λa, count b (f a)) := begin letI := classical.dec_eq α, refine s.induction _ _, { exact count_zero _ }, { assume i s his ih, rw [finset.sup_insert, sup_eq_union, count_union, finset.sup_insert, ih], refl } end end multiset section lattice variables {ι : Type*} {ι' : Sort*} [complete_lattice α] /-- Supremum of `s i`, `i : ι`, is equal to the supremum over `t : finset ι` of suprema `⨆ i ∈ t, s i`. This version assumes `ι` is a `Type*`. See `supr_eq_supr_finset'` for a version that works for `ι : Sort*`. -/ lemma supr_eq_supr_finset (s : ι → α) : (⨆i, s i) = (⨆t:finset ι, ⨆i∈t, s i) := begin classical, exact le_antisymm (supr_le $ assume b, le_supr_of_le {b} $ le_supr_of_le b $ le_supr_of_le (by simp) $ le_refl _) (supr_le $ assume t, supr_le $ assume b, supr_le $ assume hb, le_supr _ _) end /-- Supremum of `s i`, `i : ι`, is equal to the supremum over `t : finset ι` of suprema `⨆ i ∈ t, s i`. This version works for `ι : Sort*`. See `supr_eq_supr_finset` for a version that assumes `ι : Type*` but has no `plift`s. -/ lemma supr_eq_supr_finset' (s : ι' → α) : (⨆i, s i) = (⨆t:finset (plift ι'), ⨆i∈t, s (plift.down i)) := by rw [← supr_eq_supr_finset, ← equiv.plift.surjective.supr_comp]; refl /-- Infimum of `s i`, `i : ι`, is equal to the infimum over `t : finset ι` of infima `⨆ i ∈ t, s i`. This version assumes `ι` is a `Type*`. See `infi_eq_infi_finset'` for a version that works for `ι : Sort*`. -/ lemma infi_eq_infi_finset (s : ι → α) : (⨅i, s i) = (⨅t:finset ι, ⨅i∈t, s i) := @supr_eq_supr_finset (order_dual α) _ _ _ /-- Infimum of `s i`, `i : ι`, is equal to the infimum over `t : finset ι` of infima `⨆ i ∈ t, s i`. This version works for `ι : Sort*`. See `infi_eq_infi_finset` for a version that assumes `ι : Type*` but has no `plift`s. -/ lemma infi_eq_infi_finset' (s : ι' → α) : (⨅i, s i) = (⨅t:finset (plift ι'), ⨅i∈t, s (plift.down i)) := @supr_eq_supr_finset' (order_dual α) _ _ _ end lattice namespace set variables {ι : Type*} {ι' : Sort*} /-- Union of an indexed family of sets `s : ι → set α` is equal to the union of the unions of finite subfamilies. This version assumes `ι : Type*`. See also `Union_eq_Union_finset'` for a version that works for `ι : Sort*`. -/ lemma Union_eq_Union_finset (s : ι → set α) : (⋃i, s i) = (⋃t:finset ι, ⋃i∈t, s i) := supr_eq_supr_finset s /-- Union of an indexed family of sets `s : ι → set α` is equal to the union of the unions of finite subfamilies. This version works for `ι : Sort*`. See also `Union_eq_Union_finset` for a version that assumes `ι : Type*` but avoids `plift`s in the right hand side. -/ lemma Union_eq_Union_finset' (s : ι' → set α) : (⋃i, s i) = (⋃t:finset (plift ι'), ⋃i∈t, s (plift.down i)) := supr_eq_supr_finset' s /-- Intersection of an indexed family of sets `s : ι → set α` is equal to the intersection of the intersections of finite subfamilies. This version assumes `ι : Type*`. See also `Inter_eq_Inter_finset'` for a version that works for `ι : Sort*`. -/ lemma Inter_eq_Inter_finset (s : ι → set α) : (⋂i, s i) = (⋂t:finset ι, ⋂i∈t, s i) := infi_eq_infi_finset s /-- Intersection of an indexed family of sets `s : ι → set α` is equal to the intersection of the intersections of finite subfamilies. This version works for `ι : Sort*`. See also `Inter_eq_Inter_finset` for a version that assumes `ι : Type*` but avoids `plift`s in the right hand side. -/ lemma Inter_eq_Inter_finset' (s : ι' → set α) : (⋂i, s i) = (⋂t:finset (plift ι'), ⋂i∈t, s (plift.down i)) := infi_eq_infi_finset' s end set namespace finset open function /-! ### Interaction with big lattice/set operations -/ section lattice lemma supr_coe [has_Sup β] (f : α → β) (s : finset α) : (⨆ x ∈ (↑s : set α), f x) = ⨆ x ∈ s, f x := rfl lemma infi_coe [has_Inf β] (f : α → β) (s : finset α) : (⨅ x ∈ (↑s : set α), f x) = ⨅ x ∈ s, f x := rfl variables [complete_lattice β] theorem supr_singleton (a : α) (s : α → β) : (⨆ x ∈ ({a} : finset α), s x) = s a := by simp theorem infi_singleton (a : α) (s : α → β) : (⨅ x ∈ ({a} : finset α), s x) = s a := by simp lemma supr_option_to_finset (o : option α) (f : α → β) : (⨆ x ∈ o.to_finset, f x) = ⨆ x ∈ o, f x := by { congr, ext, rw [option.mem_to_finset] } lemma infi_option_to_finset (o : option α) (f : α → β) : (⨅ x ∈ o.to_finset, f x) = ⨅ x ∈ o, f x := @supr_option_to_finset _ (order_dual β) _ _ _ variables [decidable_eq α] theorem supr_union {f : α → β} {s t : finset α} : (⨆ x ∈ s ∪ t, f x) = (⨆x∈s, f x) ⊔ (⨆x∈t, f x) := by simp [supr_or, supr_sup_eq] theorem infi_union {f : α → β} {s t : finset α} : (⨅ x ∈ s ∪ t, f x) = (⨅ x ∈ s, f x) ⊓ (⨅ x ∈ t, f x) := by simp [infi_or, infi_inf_eq] lemma supr_insert (a : α) (s : finset α) (t : α → β) : (⨆ x ∈ insert a s, t x) = t a ⊔ (⨆ x ∈ s, t x) := by { rw insert_eq, simp only [supr_union, finset.supr_singleton] } lemma infi_insert (a : α) (s : finset α) (t : α → β) : (⨅ x ∈ insert a s, t x) = t a ⊓ (⨅ x ∈ s, t x) := by { rw insert_eq, simp only [infi_union, finset.infi_singleton] } lemma supr_finset_image {f : γ → α} {g : α → β} {s : finset γ} : (⨆ x ∈ s.image f, g x) = (⨆ y ∈ s, g (f y)) := by rw [← supr_coe, coe_image, supr_image, supr_coe] lemma infi_finset_image {f : γ → α} {g : α → β} {s : finset γ} : (⨅ x ∈ s.image f, g x) = (⨅ y ∈ s, g (f y)) := by rw [← infi_coe, coe_image, infi_image, infi_coe] lemma supr_insert_update {x : α} {t : finset α} (f : α → β) {s : β} (hx : x ∉ t) : (⨆ (i ∈ insert x t), function.update f x s i) = (s ⊔ ⨆ (i ∈ t), f i) := begin simp only [finset.supr_insert, update_same], rcongr i hi, apply update_noteq, rintro rfl, exact hx hi end lemma infi_insert_update {x : α} {t : finset α} (f : α → β) {s : β} (hx : x ∉ t) : (⨅ (i ∈ insert x t), update f x s i) = (s ⊓ ⨅ (i ∈ t), f i) := @supr_insert_update α (order_dual β) _ _ _ _ f _ hx lemma supr_bind (s : finset γ) (t : γ → finset α) (f : α → β) : (⨆ y ∈ s.bind t, f y) = ⨆ (x ∈ s) (y ∈ t x), f y := calc (⨆ y ∈ s.bind t, f y) = ⨆ y (hy : ∃ x ∈ s, y ∈ t x), f y : congr_arg _ $ funext $ λ y, by rw [mem_bind] ... = _ : by simp only [supr_exists, @supr_comm _ α] lemma infi_bind (s : finset γ) (t : γ → finset α) (f : α → β) : (⨅ y ∈ s.bind t, f y) = ⨅ (x ∈ s) (y ∈ t x), f y := @supr_bind _ (order_dual β) _ _ _ _ _ _ end lattice @[simp] theorem bUnion_coe (s : finset α) (t : α → set β) : (⋃ x ∈ (↑s : set α), t x) = ⋃ x ∈ s, t x := rfl @[simp] theorem bInter_coe (s : finset α) (t : α → set β) : (⋂ x ∈ (↑s : set α), t x) = ⋂ x ∈ s, t x := rfl @[simp] theorem bUnion_singleton (a : α) (s : α → set β) : (⋃ x ∈ ({a} : finset α), s x) = s a := supr_singleton a s @[simp] theorem bInter_singleton (a : α) (s : α → set β) : (⋂ x ∈ ({a} : finset α), s x) = s a := infi_singleton a s @[simp] lemma bUnion_preimage_singleton (f : α → β) (s : finset β) : (⋃ y ∈ s, f ⁻¹' {y}) = f ⁻¹' ↑s := set.bUnion_preimage_singleton f ↑s @[simp] lemma bUnion_option_to_finset (o : option α) (f : α → set β) : (⋃ x ∈ o.to_finset, f x) = ⋃ x ∈ o, f x := supr_option_to_finset o f @[simp] lemma bInter_option_to_finset (o : option α) (f : α → set β) : (⋂ x ∈ o.to_finset, f x) = ⋂ x ∈ o, f x := infi_option_to_finset o f variables [decidable_eq α] lemma bUnion_union (s t : finset α) (u : α → set β) : (⋃ x ∈ s ∪ t, u x) = (⋃ x ∈ s, u x) ∪ (⋃ x ∈ t, u x) := supr_union lemma bInter_inter (s t : finset α) (u : α → set β) : (⋂ x ∈ s ∪ t, u x) = (⋂ x ∈ s, u x) ∩ (⋂ x ∈ t, u x) := infi_union @[simp] lemma bUnion_insert (a : α) (s : finset α) (t : α → set β) : (⋃ x ∈ insert a s, t x) = t a ∪ (⋃ x ∈ s, t x) := supr_insert a s t @[simp] lemma bInter_insert (a : α) (s : finset α) (t : α → set β) : (⋂ x ∈ insert a s, t x) = t a ∩ (⋂ x ∈ s, t x) := infi_insert a s t @[simp] lemma bUnion_finset_image {f : γ → α} {g : α → set β} {s : finset γ} : (⋃x ∈ s.image f, g x) = (⋃y ∈ s, g (f y)) := supr_finset_image @[simp] lemma bInter_finset_image {f : γ → α} {g : α → set β} {s : finset γ} : (⋂ x ∈ s.image f, g x) = (⋂ y ∈ s, g (f y)) := infi_finset_image lemma bUnion_insert_update {x : α} {t : finset α} (f : α → set β) {s : set β} (hx : x ∉ t) : (⋃ (i ∈ insert x t), @update _ _ _ f x s i) = (s ∪ ⋃ (i ∈ t), f i) := supr_insert_update f hx lemma bInter_insert_update {x : α} {t : finset α} (f : α → set β) {s : set β} (hx : x ∉ t) : (⋂ (i ∈ insert x t), @update _ _ _ f x s i) = (s ∩ ⋂ (i ∈ t), f i) := infi_insert_update f hx @[simp] lemma bUnion_bind (s : finset γ) (t : γ → finset α) (f : α → set β) : (⋃ y ∈ s.bind t, f y) = ⋃ (x ∈ s) (y ∈ t x), f y := supr_bind s t f @[simp] lemma bInter_bind (s : finset γ) (t : γ → finset α) (f : α → set β) : (⋂ y ∈ s.bind t, f y) = ⋂ (x ∈ s) (y ∈ t x), f y := infi_bind s t f end finset
cfb0485d51ad0e6bb52d76ff6621c203e7d5efdf
4fa161becb8ce7378a709f5992a594764699e268
/src/logic/relation.lean
067e1d9e7a71ea09d08c3ded18837a0e0d87f989
[ "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
13,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 Transitive reflexive as well as reflexive closure of relations. -/ import tactic.basic variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} namespace relation section comp variables {r : α → β → Prop} {p : β → γ → Prop} {q : γ → δ → Prop} def comp (r : α → β → Prop) (p : β → γ → Prop) (a : α) (c : γ) : Prop := ∃b, r a b ∧ p b c local infixr ` ∘r ` : 80 := relation.comp lemma comp_eq : r ∘r (=) = r := funext $ assume a, funext $ assume b, propext $ iff.intro (assume ⟨c, h, eq⟩, eq ▸ h) (assume h, ⟨b, h, rfl⟩) lemma eq_comp : (=) ∘r r = r := funext $ assume a, funext $ assume b, propext $ iff.intro (assume ⟨c, eq, h⟩, eq.symm ▸ h) (assume h, ⟨a, rfl, h⟩) lemma iff_comp {r : Prop → α → Prop} : (↔) ∘r r = r := have (↔) = (=), by funext a b; exact iff_eq_eq, by rw [this, eq_comp] lemma comp_iff {r : α → Prop → Prop} : r ∘r (↔) = r := have (↔) = (=), by funext a b; exact iff_eq_eq, by rw [this, comp_eq] lemma comp_assoc : (r ∘r p) ∘r q = r ∘r p ∘r q := begin funext a d, apply propext, split, exact assume ⟨c, ⟨b, hab, hbc⟩, hcd⟩, ⟨b, hab, c, hbc, hcd⟩, exact assume ⟨b, hab, c, hbc, hcd⟩, ⟨c, ⟨b, hab, hbc⟩, hcd⟩ end lemma flip_comp : flip (r ∘r p) = (flip p) ∘r (flip r) := begin funext c a, apply propext, split, exact assume ⟨b, hab, hbc⟩, ⟨b, hbc, hab⟩, exact assume ⟨b, hbc, hab⟩, ⟨b, hab, hbc⟩ end end comp protected def map (r : α → β → Prop) (f : α → γ) (g : β → δ) : γ → δ → Prop := λc d, ∃a b, r a b ∧ f a = c ∧ g b = d variables {r : α → α → Prop} {a b c d : α} /-- `refl_trans_gen r`: reflexive transitive closure of `r` -/ inductive refl_trans_gen (r : α → α → Prop) (a : α) : α → Prop | refl : refl_trans_gen a | tail {b c} : refl_trans_gen b → r b c → refl_trans_gen c attribute [refl] refl_trans_gen.refl mk_iff_of_inductive_prop relation.refl_trans_gen relation.refl_trans_gen.cases_tail_iff /-- `refl_gen r`: reflexive closure of `r` -/ inductive refl_gen (r : α → α → Prop) (a : α) : α → Prop | refl : refl_gen a | single {b} : r a b → refl_gen b mk_iff_of_inductive_prop relation.refl_gen relation.refl_gen_iff /-- `trans_gen r`: transitive closure of `r` -/ inductive trans_gen (r : α → α → Prop) (a : α) : α → Prop | single {b} : r a b → trans_gen b | tail {b c} : trans_gen b → r b c → trans_gen c mk_iff_of_inductive_prop relation.trans_gen relation.trans_gen_iff attribute [refl] refl_gen.refl lemma refl_gen.to_refl_trans_gen : ∀{a b}, refl_gen r a b → refl_trans_gen r a b | a _ refl_gen.refl := by refl | a b (refl_gen.single h) := refl_trans_gen.tail refl_trans_gen.refl h namespace refl_trans_gen @[trans] lemma trans (hab : refl_trans_gen r a b) (hbc : refl_trans_gen r b c) : refl_trans_gen r a c := begin induction hbc, case refl_trans_gen.refl { assumption }, case refl_trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end lemma single (hab : r a b) : refl_trans_gen r a b := refl.tail hab lemma head (hab : r a b) (hbc : refl_trans_gen r b c) : refl_trans_gen r a c := begin induction hbc, case refl_trans_gen.refl { exact refl.tail hab }, case refl_trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end lemma symmetric (h : symmetric r) : symmetric (refl_trans_gen r) := begin intros x y h, induction h with z w a b c, { refl }, { apply relation.refl_trans_gen.head (h b) c } end lemma cases_tail : refl_trans_gen r a b → b = a ∨ (∃c, refl_trans_gen r a c ∧ r c b) := (cases_tail_iff r a b).1 @[elab_as_eliminator] lemma head_induction_on {P : ∀(a:α), refl_trans_gen r a b → Prop} {a : α} (h : refl_trans_gen r a b) (refl : P b refl) (head : ∀{a c} (h' : r a c) (h : refl_trans_gen r c b), P c h → P a (h.head h')) : P a h := begin induction h generalizing P, case refl_trans_gen.refl { exact refl }, case refl_trans_gen.tail : b c hab hbc ih { apply ih, show P b _, from head hbc _ refl, show ∀a a', r a a' → refl_trans_gen r a' b → P a' _ → P a _, from assume a a' hab hbc, head hab _ } end @[elab_as_eliminator] lemma trans_induction_on {P : ∀{a b : α}, refl_trans_gen r a b → Prop} {a b : α} (h : refl_trans_gen r a b) (ih₁ : ∀a, @P a a refl) (ih₂ : ∀{a b} (h : r a b), P (single h)) (ih₃ : ∀{a b c} (h₁ : refl_trans_gen r a b) (h₂ : refl_trans_gen r b c), P h₁ → P h₂ → P (h₁.trans h₂)) : P h := begin induction h, case refl_trans_gen.refl { exact ih₁ a }, case refl_trans_gen.tail : b c hab hbc ih { exact ih₃ hab (single hbc) ih (ih₂ hbc) } end lemma cases_head (h : refl_trans_gen r a b) : a = b ∨ (∃c, r a c ∧ refl_trans_gen r c b) := begin induction h using relation.refl_trans_gen.head_induction_on, { left, refl }, { right, existsi _, split; assumption } end lemma cases_head_iff : refl_trans_gen r a b ↔ a = b ∨ (∃c, r a c ∧ refl_trans_gen r c b) := begin split, { exact cases_head }, { assume h, rcases h with rfl | ⟨c, hac, hcb⟩, { refl }, { exact head hac hcb } } end lemma total_of_right_unique (U : relator.right_unique r) (ab : refl_trans_gen r a b) (ac : refl_trans_gen r a c) : refl_trans_gen r b c ∨ refl_trans_gen r c b := begin induction ab with b d ab bd IH, { exact or.inl ac }, { rcases IH with IH | IH, { rcases cases_head IH with rfl | ⟨e, be, ec⟩, { exact or.inr (single bd) }, { cases U bd be, exact or.inl ec } }, { exact or.inr (IH.tail bd) } } end end refl_trans_gen namespace trans_gen lemma to_refl {a b} (h : trans_gen r a b) : refl_trans_gen r a b := begin induction h with b h b c _ bc ab, exact refl_trans_gen.single h, exact refl_trans_gen.tail ab bc end @[trans] lemma trans_left (hab : trans_gen r a b) (hbc : refl_trans_gen r b c) : trans_gen r a c := begin induction hbc, case refl_trans_gen.refl : { assumption }, case refl_trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end @[trans] lemma trans (hab : trans_gen r a b) (hbc : trans_gen r b c) : trans_gen r a c := trans_left hab hbc.to_refl lemma head' (hab : r a b) (hbc : refl_trans_gen r b c) : trans_gen r a c := trans_left (single hab) hbc lemma tail' (hab : refl_trans_gen r a b) (hbc : r b c) : trans_gen r a c := begin induction hab generalizing c, case refl_trans_gen.refl : c hac { exact single hac }, case refl_trans_gen.tail : d b hab hdb IH { exact tail (IH hdb) hbc } end @[trans] lemma trans_right (hab : refl_trans_gen r a b) (hbc : trans_gen r b c) : trans_gen r a c := begin induction hbc, case trans_gen.single : c hbc { exact tail' hab hbc }, case trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end lemma head (hab : r a b) (hbc : trans_gen r b c) : trans_gen r a c := head' hab hbc.to_refl lemma tail'_iff : trans_gen r a c ↔ ∃ b, refl_trans_gen r a b ∧ r b c := begin refine ⟨λ h, _, λ ⟨b, hab, hbc⟩, tail' hab hbc⟩, cases h with _ hac b _ hab hbc, { exact ⟨_, by refl, hac⟩ }, { exact ⟨_, hab.to_refl, hbc⟩ } end lemma head'_iff : trans_gen r a c ↔ ∃ b, r a b ∧ refl_trans_gen r b c := begin refine ⟨λ h, _, λ ⟨b, hab, hbc⟩, head' hab hbc⟩, induction h, case trans_gen.single : c hac { exact ⟨_, hac, by refl⟩ }, case trans_gen.tail : b c hab hbc IH { rcases IH with ⟨d, had, hdb⟩, exact ⟨_, had, hdb.tail hbc⟩ } end end trans_gen section refl_trans_gen open refl_trans_gen lemma refl_trans_gen_iff_eq (h : ∀b, ¬ r a b) : refl_trans_gen r a b ↔ b = a := by rw [cases_head_iff]; simp [h, eq_comm] lemma refl_trans_gen_iff_eq_or_trans_gen : refl_trans_gen r a b ↔ b = a ∨ trans_gen r a b := begin refine ⟨λ h, _, λ h, _⟩, { cases h with c _ hac hcb, { exact or.inl rfl }, { exact or.inr (trans_gen.tail' hac hcb) } }, { rcases h with rfl | h, {refl}, {exact h.to_refl} } end lemma refl_trans_gen_lift {p : β → β → Prop} {a b : α} (f : α → β) (h : ∀a b, r a b → p (f a) (f b)) (hab : refl_trans_gen r a b) : refl_trans_gen p (f a) (f b) := refl_trans_gen.trans_induction_on hab (assume a, refl) (assume a b, refl_trans_gen.single ∘ h _ _) (assume a b c _ _, trans) lemma refl_trans_gen_mono {p : α → α → Prop} : (∀a b, r a b → p a b) → refl_trans_gen r a b → refl_trans_gen p a b := refl_trans_gen_lift id lemma refl_trans_gen_eq_self (refl : reflexive r) (trans : transitive r) : refl_trans_gen r = r := funext $ λ a, funext $ λ b, propext $ ⟨λ h, begin induction h with b c h₁ h₂ IH, {apply refl}, exact trans IH h₂, end, single⟩ lemma reflexive_refl_trans_gen : reflexive (refl_trans_gen r) := assume a, refl lemma transitive_refl_trans_gen : transitive (refl_trans_gen r) := assume a b c, trans lemma refl_trans_gen_idem : refl_trans_gen (refl_trans_gen r) = refl_trans_gen r := refl_trans_gen_eq_self reflexive_refl_trans_gen transitive_refl_trans_gen lemma refl_trans_gen_lift' {p : β → β → Prop} {a b : α} (f : α → β) (h : ∀a b, r a b → refl_trans_gen p (f a) (f b)) (hab : refl_trans_gen r a b) : refl_trans_gen p (f a) (f b) := by simpa [refl_trans_gen_idem] using refl_trans_gen_lift f h hab lemma refl_trans_gen_closed {p : α → α → Prop} : (∀ a b, r a b → refl_trans_gen p a b) → refl_trans_gen r a b → refl_trans_gen p a b := refl_trans_gen_lift' id end refl_trans_gen def join (r : α → α → Prop) : α → α → Prop := λa b, ∃c, r a c ∧ r b c section join open refl_trans_gen refl_gen lemma church_rosser (h : ∀a b c, r a b → r a c → ∃d, refl_gen r b d ∧ refl_trans_gen r c d) (hab : refl_trans_gen r a b) (hac : refl_trans_gen r a c) : join (refl_trans_gen r) b c := begin induction hab, case refl_trans_gen.refl { exact ⟨c, hac, refl⟩ }, case refl_trans_gen.tail : d e had hde ih { clear hac had a, rcases ih with ⟨b, hdb, hcb⟩, have : ∃a, refl_trans_gen r e a ∧ refl_gen r b a, { clear hcb, induction hdb, case refl_trans_gen.refl { exact ⟨e, refl, refl_gen.single hde⟩ }, case refl_trans_gen.tail : f b hdf hfb ih { rcases ih with ⟨a, hea, hfa⟩, cases hfa with _ hfa, { exact ⟨b, hea.tail hfb, refl_gen.refl⟩ }, { rcases h _ _ _ hfb hfa with ⟨c, hbc, hac⟩, exact ⟨c, hea.trans hac, hbc⟩ } } }, rcases this with ⟨a, hea, hba⟩, cases hba with _ hba, { exact ⟨b, hea, hcb⟩ }, { exact ⟨a, hea, hcb.tail hba⟩ } } end lemma join_of_single (h : reflexive r) (hab : r a b) : join r a b := ⟨b, hab, h b⟩ lemma symmetric_join : symmetric (join r) := assume a b ⟨c, hac, hcb⟩, ⟨c, hcb, hac⟩ lemma reflexive_join (h : reflexive r) : reflexive (join r) := assume a, ⟨a, h a, h a⟩ lemma transitive_join (ht : transitive r) (h : ∀a b c, r a b → r a c → join r b c) : transitive (join r) := assume a b c ⟨x, hax, hbx⟩ ⟨y, hby, hcy⟩, let ⟨z, hxz, hyz⟩ := h b x y hbx hby in ⟨z, ht hax hxz, ht hcy hyz⟩ lemma equivalence_join (hr : reflexive r) (ht : transitive r) (h : ∀a b c, r a b → r a c → join r b c) : equivalence (join r) := ⟨reflexive_join hr, symmetric_join, transitive_join ht h⟩ lemma equivalence_join_refl_trans_gen (h : ∀a b c, r a b → r a c → ∃d, refl_gen r b d ∧ refl_trans_gen r c d) : equivalence (join (refl_trans_gen r)) := equivalence_join reflexive_refl_trans_gen transitive_refl_trans_gen (assume a b c, church_rosser h) lemma join_of_equivalence {r' : α → α → Prop} (hr : equivalence r) (h : ∀a b, r' a b → r a b) : join r' a b → r a b | ⟨c, hac, hbc⟩ := hr.2.2 (h _ _ hac) (hr.2.1 $ h _ _ hbc) lemma refl_trans_gen_of_transitive_reflexive {r' : α → α → Prop} (hr : reflexive r) (ht : transitive r) (h : ∀a b, r' a b → r a b) (h' : refl_trans_gen r' a b) : r a b := begin induction h' with b c hab hbc ih, { exact hr _ }, { exact ht ih (h _ _ hbc) } end lemma refl_trans_gen_of_equivalence {r' : α → α → Prop} (hr : equivalence r) : (∀a b, r' a b → r a b) → refl_trans_gen r' a b → r a b := refl_trans_gen_of_transitive_reflexive hr.1 hr.2.2 end join section eqv_gen lemma eqv_gen_iff_of_equivalence (h : equivalence r) : eqv_gen r a b ↔ r a b := iff.intro begin assume h, induction h, case eqv_gen.rel { assumption }, case eqv_gen.refl { exact h.1 _ }, case eqv_gen.symm { apply h.2.1, assumption }, case eqv_gen.trans : a b c _ _ hab hbc { exact h.2.2 hab hbc } end (eqv_gen.rel a b) lemma eqv_gen_mono {r p : α → α → Prop} (hrp : ∀a b, r a b → p a b) (h : eqv_gen r a b) : eqv_gen p a b := begin induction h, case eqv_gen.rel : a b h { exact eqv_gen.rel _ _ (hrp _ _ h) }, case eqv_gen.refl : { exact eqv_gen.refl _ }, case eqv_gen.symm : a b h ih { exact eqv_gen.symm _ _ ih }, case eqv_gen.trans : a b c ih1 ih2 hab hbc { exact eqv_gen.trans _ _ _ hab hbc } end end eqv_gen end relation
03bba40952dbd1de8610e9dab22c076e1087a5d3
63abd62053d479eae5abf4951554e1064a4c45b4
/test/ring.lean
bf1bbffcf67458757a66a1ca8782a81e78e22175
[ "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
2,153
lean
import tactic.ring import data.real.basic example (x y : ℕ) : x + y = y + x := by ring example (x y : ℕ) : x + y + y = 2 * y + x := by ring example (x y : ℕ) : x + id y = y + id x := by ring! example {α} [comm_ring α] (x y : α) : x + y + y - x = 2 * y := by ring example (x y : ℚ) : x / 2 + x / 2 = x := by ring example (x y : ℚ) : (x + y) ^ 3 = x ^ 3 + y ^ 3 + 3 * (x * y ^ 2 + x ^ 2 * y) := by ring example (x y : ℝ) : (x + y) ^ 3 = x ^ 3 + y ^ 3 + 3 * (x * y ^ 2 + x ^ 2 * y) := by ring example {α} [comm_semiring α] (x : α) : (x + 1) ^ 6 = (1 + x) ^ 6 := by try_for 15000 {ring} example (n : ℕ) : (n / 2) + (n / 2) = 2 * (n / 2) := by ring example {α} [field α] [char_zero α] (a : α) : a / 2 = a / 2 := by ring example {α} [linear_ordered_field α] (a b c : α) : a * (-c / b) * (-c / b) + -c + c = a * (c / b * (c / b)) := by ring example {α} [linear_ordered_field α] (a b c : α) : b ^ 2 - 4 * c * a = -(4 * c * a) + b ^ 2 := by ring example (x : ℚ) : x ^ (2 + 2) = x^4 := by ring example {α} [comm_ring α] (x : α) : x ^ 2 = x * x := by ring example {α} [linear_ordered_field α] (a b c : α) : b ^ 2 - 4 * c * a = -(4 * c * a) + b ^ 2 := by ring example {α} [linear_ordered_field α] (a b c : α) : b ^ 2 - 4 * a * c = 4 * a * 0 + b * b - 4 * a * c := by ring example {α} [comm_semiring α] (x y z : α) (n : ℕ) : (x + y) * (z * (y * y) + (x * x ^ n + (1 + ↑n) * x ^ n * y)) = x * (x * x ^ n) + ((2 + ↑n) * (x * x ^ n) * y + (x * z + (z * y + (1 + ↑n) * x ^ n)) * (y * y)) := by ring example {α} [comm_ring α] (a b c d e : α) : (-(a * b) + c + d) * e = (c + (d + -a * b)) * e := by ring example (a n s: ℕ) : a * (n - s) = (n - s) * a := by ring example (x y z : ℚ) (hx : x ≠ 0) (hy : y ≠ 0) (hz : z ≠ 0) : x / (y / z) + y ⁻¹ + 1 / (y * -x) = -1/ (x * y) + (x * z + 1) / y := begin field_simp [hx, hy, hz], ring end example (a b c d x y : ℚ) (hx : x ≠ 0) (hy : y ≠ 0) : a + b / x - c / x^2 + d / x^3 = a + x⁻¹ * (y * b / y + (d / x - c) / x) := begin field_simp [hx, hy], ring end example : (876544 : ℤ) * -1 + (1000000 - 123456) = 0 := by ring
c45eb2495fde1f6e2ce2a5a497591f0c19a330ec
b9dd0f3640eda42cdd8d97060417cb19d5c00eaf
/src/game.lean
fb3fb7a3d036760c70a9de3daa438c6fed6bf80b
[]
no_license
foxthomson/guessgame
20e3e5854d0a5c1a26335914446c827a711ee53e
96d542b574949a883a9ed216a4f78e195d61878a
refs/heads/master
1,670,822,903,666
1,599,562,332,000
1,599,562,332,000
293,782,243
0
0
null
null
null
null
UTF-8
Lean
false
false
472
lean
import basic /-- # How to play The tactic `mk_random_game` will generate a random number between 1 and 100. You can then use the tactic `guess n` to guess a random number. If you get it right the goal will be solved otherwise it will add a hypothesis saying whether you were too high or too low. You have 7 guesses. -/ example : true := begin mk_random_game, { guess 10, guess 20, guess 30, guess 40, guess 50, guess 60, }, trivial, end
ef07e5f92987f548eac3a7192d90f549f8260dc7
05b503addd423dd68145d68b8cde5cd595d74365
/src/algebra/ordered_ring.lean
4f0c561f8b741e04f10621a5ee883521a16facb6
[ "Apache-2.0" ]
permissive
aestriplex/mathlib
77513ff2b176d74a3bec114f33b519069788811d
e2fa8b2b1b732d7c25119229e3cdfba8370cb00f
refs/heads/master
1,621,969,960,692
1,586,279,279,000
1,586,279,279,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
23,617
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 tactic.split_ifs order.basic algebra.order algebra.ordered_group algebra.ring data.nat.cast universe u variable {α : Type u} -- `mul_nonneg` and `mul_pos` in core are stated in terms of `≥` and `>`, so we restate them here -- for use in syntactic tactics (e.g. `simp` and `rw`). lemma mul_nonneg' [ordered_semiring α] {a b : α} : 0 ≤ a → 0 ≤ b → 0 ≤ a * b := mul_nonneg lemma mul_pos' [ordered_semiring α] {a b : α} (ha : 0 < a) (hb : 0 < b) : 0 < a * b := mul_pos ha hb section linear_ordered_semiring variable [linear_ordered_semiring α] /-- `0 < 2`: an alternative version of `two_pos` that only assumes `linear_ordered_semiring`. -/ lemma zero_lt_two : (0:α) < 2 := by { rw [← zero_add (0:α), bit0], exact add_lt_add zero_lt_one zero_lt_one } @[simp] lemma mul_le_mul_left {a b c : α} (h : 0 < c) : c * a ≤ c * b ↔ a ≤ b := ⟨λ h', le_of_mul_le_mul_left h' h, λ h', mul_le_mul_of_nonneg_left h' (le_of_lt h)⟩ @[simp] lemma mul_le_mul_right {a b c : α} (h : 0 < c) : a * c ≤ b * c ↔ a ≤ b := ⟨λ h', le_of_mul_le_mul_right h' h, λ h', mul_le_mul_of_nonneg_right h' (le_of_lt h)⟩ @[simp] lemma mul_lt_mul_left {a b c : α} (h : 0 < c) : c * a < c * b ↔ a < b := ⟨lt_imp_lt_of_le_imp_le $ λ h', mul_le_mul_of_nonneg_left h' (le_of_lt h), λ h', mul_lt_mul_of_pos_left h' h⟩ @[simp] lemma mul_lt_mul_right {a b c : α} (h : 0 < c) : a * c < b * c ↔ a < b := ⟨lt_imp_lt_of_le_imp_le $ λ h', mul_le_mul_of_nonneg_right h' (le_of_lt h), λ h', mul_lt_mul_of_pos_right h' h⟩ @[simp] lemma zero_le_mul_left {b c : α} (h : 0 < c) : 0 ≤ c * b ↔ 0 ≤ b := by { convert mul_le_mul_left h, simp } @[simp] lemma zero_le_mul_right {b c : α} (h : 0 < c) : 0 ≤ b * c ↔ 0 ≤ b := by { convert mul_le_mul_right h, simp } @[simp] lemma zero_lt_mul_left {b c : α} (h : 0 < c) : 0 < c * b ↔ 0 < b := by { convert mul_lt_mul_left h, simp } @[simp] lemma zero_lt_mul_right {b c : α} (h : 0 < c) : 0 < b * c ↔ 0 < b := by { convert mul_lt_mul_right h, simp } @[simp] lemma bit0_le_bit0 {a b : α} : bit0 a ≤ bit0 b ↔ a ≤ b := by rw [bit0, bit0, ← two_mul, ← two_mul, mul_le_mul_left zero_lt_two] @[simp] lemma bit0_lt_bit0 {a b : α} : bit0 a < bit0 b ↔ a < b := by rw [bit0, bit0, ← two_mul, ← two_mul, mul_lt_mul_left zero_lt_two] @[simp] lemma bit1_le_bit1 {a b : α} : bit1 a ≤ bit1 b ↔ a ≤ b := (add_le_add_iff_right 1).trans bit0_le_bit0 @[simp] lemma bit1_lt_bit1 {a b : α} : bit1 a < bit1 b ↔ a < b := (add_lt_add_iff_right 1).trans bit0_lt_bit0 @[simp] lemma one_le_bit1 {a : α} : (1 : α) ≤ bit1 a ↔ 0 ≤ a := by rw [bit1, le_add_iff_nonneg_left, bit0, ← two_mul, zero_le_mul_left zero_lt_two] @[simp] lemma one_lt_bit1 {a : α} : (1 : α) < bit1 a ↔ 0 < a := by rw [bit1, lt_add_iff_pos_left, bit0, ← two_mul, zero_lt_mul_left zero_lt_two] @[simp] lemma zero_le_bit0 {a : α} : (0 : α) ≤ bit0 a ↔ 0 ≤ a := by rw [bit0, ← two_mul, zero_le_mul_left zero_lt_two] @[simp] lemma zero_lt_bit0 {a : α} : (0 : α) < bit0 a ↔ 0 < a := by rw [bit0, ← two_mul, zero_lt_mul_left zero_lt_two] lemma mul_lt_mul'' {a b c d : α} (h1 : a < c) (h2 : b < d) (h3 : 0 ≤ a) (h4 : 0 ≤ b) : a * b < c * d := (lt_or_eq_of_le h4).elim (λ b0, mul_lt_mul h1 (le_of_lt h2) b0 (le_trans h3 (le_of_lt h1))) (λ b0, by rw [← b0, mul_zero]; exact mul_pos (lt_of_le_of_lt h3 h1) (lt_of_le_of_lt h4 h2)) lemma le_mul_iff_one_le_left {a b : α} (hb : 0 < b) : b ≤ a * b ↔ 1 ≤ a := suffices 1 * b ≤ a * b ↔ 1 ≤ a, by rwa one_mul at this, mul_le_mul_right hb lemma lt_mul_iff_one_lt_left {a b : α} (hb : 0 < b) : b < a * b ↔ 1 < a := suffices 1 * b < a * b ↔ 1 < a, by rwa one_mul at this, mul_lt_mul_right hb lemma le_mul_iff_one_le_right {a b : α} (hb : 0 < b) : b ≤ b * a ↔ 1 ≤ a := suffices b * 1 ≤ b * a ↔ 1 ≤ a, by rwa mul_one at this, mul_le_mul_left hb lemma lt_mul_iff_one_lt_right {a b : α} (hb : 0 < b) : b < b * a ↔ 1 < a := suffices b * 1 < b * a ↔ 1 < a, by rwa mul_one at this, mul_lt_mul_left hb lemma lt_mul_of_one_lt_right' {a b : α} (hb : 0 < b) : 1 < a → b < b * a := (lt_mul_iff_one_lt_right hb).2 lemma le_mul_of_one_le_right' {a b : α} (hb : 0 ≤ b) (h : 1 ≤ a) : b ≤ b * a := suffices b * 1 ≤ b * a, by rwa mul_one at this, mul_le_mul_of_nonneg_left h hb lemma le_mul_of_one_le_left' {a b : α} (hb : 0 ≤ b) (h : 1 ≤ a) : b ≤ a * b := suffices 1 * b ≤ a * b, by rwa one_mul at this, mul_le_mul_of_nonneg_right h hb theorem mul_nonneg_iff_right_nonneg_of_pos {a b : α} (h : 0 < a) : 0 ≤ b * a ↔ 0 ≤ b := ⟨assume : 0 ≤ b * a, nonneg_of_mul_nonneg_right this h, assume : 0 ≤ b, mul_nonneg this $ le_of_lt h⟩ lemma bit1_pos {a : α} (h : 0 ≤ a) : 0 < bit1 a := lt_add_of_le_of_pos (add_nonneg h h) zero_lt_one lemma bit1_pos' {a : α} (h : 0 < a) : 0 < bit1 a := bit1_pos (le_of_lt h) lemma lt_add_one (a : α) : a < a + 1 := lt_add_of_le_of_pos (le_refl _) zero_lt_one lemma lt_one_add (a : α) : a < 1 + a := by { rw [add_comm], apply lt_add_one } lemma one_lt_two : 1 < (2 : α) := lt_add_one _ lemma one_lt_mul {a b : α} (ha : 1 ≤ a) (hb : 1 < b) : 1 < a * b := (one_mul (1 : α)) ▸ mul_lt_mul' ha hb zero_le_one (lt_of_lt_of_le zero_lt_one ha) lemma mul_le_one {a b : α} (ha : a ≤ 1) (hb' : 0 ≤ b) (hb : b ≤ 1) : a * b ≤ 1 := begin rw ← one_mul (1 : α), apply mul_le_mul; {assumption <|> apply zero_le_one} end lemma one_lt_mul_of_le_of_lt {a b : α} (ha : 1 ≤ a) (hb : 1 < b) : 1 < a * b := calc 1 = 1 * 1 : by rw one_mul ... < a * b : mul_lt_mul' ha hb zero_le_one (lt_of_lt_of_le zero_lt_one ha) lemma one_lt_mul_of_lt_of_le {a b : α} (ha : 1 < a) (hb : 1 ≤ b) : 1 < a * b := calc 1 = 1 * 1 : by rw one_mul ... < a * b : mul_lt_mul ha hb zero_lt_one (le_trans zero_le_one (le_of_lt ha)) lemma mul_le_of_le_one_right {a b : α} (ha : 0 ≤ a) (hb1 : b ≤ 1) : a * b ≤ a := calc a * b ≤ a * 1 : mul_le_mul_of_nonneg_left hb1 ha ... = a : mul_one a lemma mul_le_of_le_one_left {a b : α} (hb : 0 ≤ b) (ha1 : a ≤ 1) : a * b ≤ b := calc a * b ≤ 1 * b : mul_le_mul ha1 (le_refl b) hb zero_le_one ... = b : one_mul b lemma mul_lt_one_of_nonneg_of_lt_one_left {a b : α} (ha0 : 0 ≤ a) (ha : a < 1) (hb : b ≤ 1) : a * b < 1 := calc a * b ≤ a : mul_le_of_le_one_right ha0 hb ... < 1 : ha lemma mul_lt_one_of_nonneg_of_lt_one_right {a b : α} (ha : a ≤ 1) (hb0 : 0 ≤ b) (hb : b < 1) : a * b < 1 := calc a * b ≤ b : mul_le_of_le_one_left hb0 ha ... < 1 : hb lemma mul_le_iff_le_one_left {a b : α} (hb : 0 < b) : a * b ≤ b ↔ a ≤ 1 := ⟨ λ h, le_of_not_lt (mt (lt_mul_iff_one_lt_left hb).2 (not_lt_of_ge h)), λ h, le_of_not_lt (mt (lt_mul_iff_one_lt_left hb).1 (not_lt_of_ge h)) ⟩ lemma mul_lt_iff_lt_one_left {a b : α} (hb : 0 < b) : a * b < b ↔ a < 1 := ⟨ λ h, lt_of_not_ge (mt (le_mul_iff_one_le_left hb).2 (not_le_of_gt h)), λ h, lt_of_not_ge (mt (le_mul_iff_one_le_left hb).1 (not_le_of_gt h)) ⟩ lemma mul_le_iff_le_one_right {a b : α} (hb : 0 < b) : b * a ≤ b ↔ a ≤ 1 := ⟨ λ h, le_of_not_lt (mt (lt_mul_iff_one_lt_right hb).2 (not_lt_of_ge h)), λ h, le_of_not_lt (mt (lt_mul_iff_one_lt_right hb).1 (not_lt_of_ge h)) ⟩ lemma mul_lt_iff_lt_one_right {a b : α} (hb : 0 < b) : b * a < b ↔ a < 1 := ⟨ λ h, lt_of_not_ge (mt (le_mul_iff_one_le_right hb).2 (not_le_of_gt h)), λ h, lt_of_not_ge (mt (le_mul_iff_one_le_right hb).1 (not_le_of_gt h)) ⟩ lemma nonpos_of_mul_nonneg_left {a b : α} (h : 0 ≤ a * b) (hb : b < 0) : a ≤ 0 := le_of_not_gt (λ ha, absurd h (not_le_of_gt (mul_neg_of_pos_of_neg ha hb))) lemma nonpos_of_mul_nonneg_right {a b : α} (h : 0 ≤ a * b) (ha : a < 0) : b ≤ 0 := le_of_not_gt (λ hb, absurd h (not_le_of_gt (mul_neg_of_neg_of_pos ha hb))) lemma neg_of_mul_pos_left {a b : α} (h : 0 < a * b) (hb : b ≤ 0) : a < 0 := lt_of_not_ge (λ ha, absurd h (not_lt_of_ge (mul_nonpos_of_nonneg_of_nonpos ha hb))) lemma neg_of_mul_pos_right {a b : α} (h : 0 < a * b) (ha : a ≤ 0) : b < 0 := lt_of_not_ge (λ hb, absurd h (not_lt_of_ge (mul_nonpos_of_nonpos_of_nonneg ha hb))) end linear_ordered_semiring section decidable_linear_ordered_semiring variable [decidable_linear_ordered_semiring α] @[simp] lemma decidable.mul_le_mul_left {a b c : α} (h : 0 < c) : c * a ≤ c * b ↔ a ≤ b := decidable.le_iff_le_iff_lt_iff_lt.2 $ mul_lt_mul_left h @[simp] lemma decidable.mul_le_mul_right {a b c : α} (h : 0 < c) : a * c ≤ b * c ↔ a ≤ b := decidable.le_iff_le_iff_lt_iff_lt.2 $ mul_lt_mul_right h end decidable_linear_ordered_semiring -- The proof doesn't need commutativity but we have no `decidable_linear_ordered_ring` @[simp] lemma abs_two [decidable_linear_ordered_comm_ring α] : abs (2:α) = 2 := abs_of_pos $ by refine zero_lt_two @[priority 100] -- see Note [lower instance priority] instance linear_ordered_semiring.to_no_top_order {α : Type*} [linear_ordered_semiring α] : no_top_order α := ⟨assume a, ⟨a + 1, lt_add_of_pos_right _ zero_lt_one⟩⟩ @[priority 100] -- see Note [lower instance priority] instance linear_ordered_semiring.to_no_bot_order {α : Type*} [linear_ordered_ring α] : no_bot_order α := ⟨assume a, ⟨a - 1, sub_lt_iff_lt_add.mpr $ lt_add_of_pos_right _ zero_lt_one⟩⟩ @[priority 100] -- see Note [lower instance priority] instance linear_ordered_ring.to_domain [s : linear_ordered_ring α] : domain α := { eq_zero_or_eq_zero_of_mul_eq_zero := @linear_ordered_ring.eq_zero_or_eq_zero_of_mul_eq_zero α s, ..s } section linear_ordered_ring variable [linear_ordered_ring α] @[simp] lemma mul_le_mul_left_of_neg {a b c : α} (h : c < 0) : c * a ≤ c * b ↔ b ≤ a := ⟨le_imp_le_of_lt_imp_lt $ λ h', mul_lt_mul_of_neg_left h' h, λ h', mul_le_mul_of_nonpos_left h' (le_of_lt h)⟩ @[simp] lemma mul_le_mul_right_of_neg {a b c : α} (h : c < 0) : a * c ≤ b * c ↔ b ≤ a := ⟨le_imp_le_of_lt_imp_lt $ λ h', mul_lt_mul_of_neg_right h' h, λ h', mul_le_mul_of_nonpos_right h' (le_of_lt h)⟩ @[simp] lemma mul_lt_mul_left_of_neg {a b c : α} (h : c < 0) : c * a < c * b ↔ b < a := lt_iff_lt_of_le_iff_le (mul_le_mul_left_of_neg h) @[simp] lemma mul_lt_mul_right_of_neg {a b c : α} (h : c < 0) : a * c < b * c ↔ b < a := lt_iff_lt_of_le_iff_le (mul_le_mul_right_of_neg h) lemma sub_one_lt (a : α) : a - 1 < a := sub_lt_iff_lt_add.2 (lt_add_one a) lemma mul_self_pos {a : α} (ha : a ≠ 0) : 0 < a * a := by rcases lt_trichotomy a 0 with h|h|h; [exact mul_pos_of_neg_of_neg h h, exact (ha h).elim, exact mul_pos h h] lemma mul_self_le_mul_self_of_le_of_neg_le {x y : α} (h₁ : x ≤ y) (h₂ : -x ≤ y) : x * x ≤ y * y := begin cases le_total 0 x, { exact mul_self_le_mul_self h h₁ }, { rw ← neg_mul_neg, exact mul_self_le_mul_self (neg_nonneg_of_nonpos h) h₂ } end lemma nonneg_of_mul_nonpos_left {a b : α} (h : a * b ≤ 0) (hb : b < 0) : 0 ≤ a := le_of_not_gt (λ ha, absurd h (not_le_of_gt (mul_pos_of_neg_of_neg ha hb))) lemma nonneg_of_mul_nonpos_right {a b : α} (h : a * b ≤ 0) (ha : a < 0) : 0 ≤ b := le_of_not_gt (λ hb, absurd h (not_le_of_gt (mul_pos_of_neg_of_neg ha hb))) lemma pos_of_mul_neg_left {a b : α} (h : a * b < 0) (hb : b ≤ 0) : 0 < a := lt_of_not_ge (λ ha, absurd h (not_lt_of_ge (mul_nonneg_of_nonpos_of_nonpos ha hb))) lemma pos_of_mul_neg_right {a b : α} (h : a * b < 0) (ha : a ≤ 0) : 0 < b := lt_of_not_ge (λ hb, absurd h (not_lt_of_ge (mul_nonneg_of_nonpos_of_nonpos ha hb))) /- The sum of two squares is zero iff both elements are zero. -/ lemma mul_self_add_mul_self_eq_zero {x y : α} : x * x + y * y = 0 ↔ x = 0 ∧ y = 0 := begin split; intro h, swap, { rcases h with ⟨rfl, rfl⟩, simp }, have : y * y ≤ 0, { rw [← h], apply le_add_of_nonneg_left (mul_self_nonneg x) }, have : y * y = 0 := le_antisymm this (mul_self_nonneg y), have hx : x = 0, { rwa [this, add_zero, mul_self_eq_zero] at h }, rw mul_self_eq_zero at this, split; assumption end end linear_ordered_ring set_option old_structure_cmd true section prio set_option default_priority 100 -- see Note [default priority] /-- Extend `nonneg_comm_group` to support ordered rings specified by their nonnegative elements -/ class nonneg_ring (α : Type*) extends ring α, zero_ne_one_class α, nonneg_comm_group α := (mul_nonneg : ∀ {a b}, nonneg a → nonneg b → nonneg (a * b)) (mul_pos : ∀ {a b}, pos a → pos b → pos (a * b)) /-- Extend `nonneg_comm_group` to support linearly ordered rings specified by their nonnegative elements -/ class linear_nonneg_ring (α : Type*) extends domain α, nonneg_comm_group α := (mul_nonneg : ∀ {a b}, nonneg a → nonneg b → nonneg (a * b)) (nonneg_total : ∀ a, nonneg a ∨ nonneg (-a)) end prio namespace nonneg_ring open nonneg_comm_group variable [s : nonneg_ring α] @[priority 100] -- see Note [lower instance priority] instance to_ordered_ring : ordered_ring α := { le := (≤), lt := (<), lt_iff_le_not_le := @lt_iff_le_not_le _ _, le_refl := @le_refl _ _, le_trans := @le_trans _ _, le_antisymm := @le_antisymm _ _, add_lt_add_left := @add_lt_add_left _ _, add_le_add_left := @add_le_add_left _ _, mul_nonneg := λ a b, by simp [nonneg_def.symm]; exact mul_nonneg, mul_pos := λ a b, by simp [pos_def.symm]; exact mul_pos, ..s } def to_linear_nonneg_ring (nonneg_total : ∀ a : α, nonneg a ∨ nonneg (-a)) : linear_nonneg_ring α := { nonneg_total := nonneg_total, eq_zero_or_eq_zero_of_mul_eq_zero := suffices ∀ {a} b : α, nonneg a → a * b = 0 → a = 0 ∨ b = 0, from λ a b, (nonneg_total a).elim (this b) (λ na, by simpa using this b na), suffices ∀ {a b : α}, nonneg a → nonneg b → a * b = 0 → a = 0 ∨ b = 0, from λ a b na, (nonneg_total b).elim (this na) (λ nb, by simpa using this na nb), λ a b na nb z, classical.by_cases (λ nna : nonneg (-a), or.inl (nonneg_antisymm na nna)) (λ pa, classical.by_cases (λ nnb : nonneg (-b), or.inr (nonneg_antisymm nb nnb)) (λ pb, absurd z $ ne_of_gt $ pos_def.1 $ mul_pos ((pos_iff _ _).2 ⟨na, pa⟩) ((pos_iff _ _).2 ⟨nb, pb⟩))), ..s } end nonneg_ring namespace linear_nonneg_ring open nonneg_comm_group variable [s : linear_nonneg_ring α] @[priority 100] -- see Note [lower instance priority] instance to_nonneg_ring : nonneg_ring α := { mul_pos := λ a b pa pb, let ⟨a1, a2⟩ := (pos_iff α a).1 pa, ⟨b1, b2⟩ := (pos_iff α b).1 pb in have ab : nonneg (a * b), from mul_nonneg a1 b1, (pos_iff α _).2 ⟨ab, λ hn, have a * b = 0, from nonneg_antisymm ab hn, (eq_zero_or_eq_zero_of_mul_eq_zero _ _ this).elim (ne_of_gt (pos_def.1 pa)) (ne_of_gt (pos_def.1 pb))⟩, ..s } @[priority 100] -- see Note [lower instance priority] instance to_linear_order : linear_order α := { le := (≤), lt := (<), lt_iff_le_not_le := @lt_iff_le_not_le _ _, le_refl := @le_refl _ _, le_trans := @le_trans _ _, le_antisymm := @le_antisymm _ _, le_total := nonneg_total_iff.1 nonneg_total, ..s } @[priority 100] -- see Note [lower instance priority] instance to_linear_ordered_ring : linear_ordered_ring α := { le := (≤), lt := (<), lt_iff_le_not_le := @lt_iff_le_not_le _ _, le_refl := @le_refl _ _, le_trans := @le_trans _ _, le_antisymm := @le_antisymm _ _, le_total := @le_total _ _, add_lt_add_left := @add_lt_add_left _ _, add_le_add_left := @add_le_add_left _ _, mul_nonneg := by simp [nonneg_def.symm]; exact @mul_nonneg _ _, mul_pos := by simp [pos_def.symm]; exact @nonneg_ring.mul_pos _ _, zero_lt_one := lt_of_not_ge $ λ (h : nonneg (0 - 1)), begin rw [zero_sub] at h, have := mul_nonneg h h, simp at this, exact zero_ne_one _ (nonneg_antisymm this h).symm end, ..s } /-- Convert a `linear_nonneg_ring` with a commutative multiplication and decidable non-negativity into a `decidable_linear_ordered_comm_ring` -/ def to_decidable_linear_ordered_comm_ring [decidable_pred (@nonneg α _)] [comm : @is_commutative α (*)] : decidable_linear_ordered_comm_ring α := { decidable_le := by apply_instance, decidable_lt := by apply_instance, mul_comm := is_commutative.comm (*), ..@linear_nonneg_ring.to_linear_ordered_ring _ s } end linear_nonneg_ring section prio set_option default_priority 100 -- see Note [default priority] class canonically_ordered_comm_semiring (α : Type*) extends canonically_ordered_monoid α, comm_semiring α, zero_ne_one_class α := (mul_eq_zero_iff (a b : α) : a * b = 0 ↔ a = 0 ∨ b = 0) end prio namespace canonically_ordered_semiring open canonically_ordered_monoid variable [canonically_ordered_comm_semiring α] lemma mul_le_mul {a b c d : α} (hab : a ≤ b) (hcd : c ≤ d) : a * c ≤ b * d := begin rcases (le_iff_exists_add _ _).1 hab with ⟨b, rfl⟩, rcases (le_iff_exists_add _ _).1 hcd with ⟨d, rfl⟩, suffices : a * c ≤ a * c + (a * d + b * c + b * d), by simpa [mul_add, add_mul], exact (le_iff_exists_add _ _).2 ⟨_, rfl⟩ end /-- A version of `zero_lt_one : 0 < 1` for a `canonically_ordered_comm_semiring`. -/ lemma zero_lt_one : (0:α) < 1 := lt_of_le_of_ne (zero_le 1) zero_ne_one lemma mul_pos {a b : α} : 0 < a * b ↔ (0 < a) ∧ (0 < b) := by simp only [zero_lt_iff_ne_zero, ne.def, canonically_ordered_comm_semiring.mul_eq_zero_iff, not_or_distrib] end canonically_ordered_semiring instance : canonically_ordered_comm_semiring ℕ := { le_iff_exists_add := assume a b, ⟨assume h, let ⟨c, hc⟩ := nat.le.dest h in ⟨c, hc.symm⟩, assume ⟨c, hc⟩, hc.symm ▸ nat.le_add_right _ _⟩, zero_ne_one := ne_of_lt zero_lt_one, mul_eq_zero_iff := assume a b, iff.intro nat.eq_zero_of_mul_eq_zero (by simp [or_imp_distrib] {contextual := tt}), bot := 0, bot_le := nat.zero_le, .. (infer_instance : ordered_comm_monoid ℕ), .. (infer_instance : linear_ordered_semiring ℕ), .. (infer_instance : comm_semiring ℕ) } namespace with_top variables [canonically_ordered_comm_semiring α] [decidable_eq α] instance : mul_zero_class (with_top α) := { zero := 0, mul := λm n, if m = 0 ∨ n = 0 then 0 else m.bind (λa, n.bind $ λb, ↑(a * b)), zero_mul := assume a, if_pos $ or.inl rfl, mul_zero := assume a, if_pos $ or.inr rfl } instance : has_one (with_top α) := ⟨↑(1:α)⟩ lemma mul_def {a b : with_top α} : a * b = if a = 0 ∨ b = 0 then 0 else a.bind (λa, b.bind $ λb, ↑(a * b)) := rfl @[simp] theorem top_ne_zero [partial_order α] : ⊤ ≠ (0 : with_top α) . @[simp] theorem zero_ne_top [partial_order α] : (0 : with_top α) ≠ ⊤ . @[simp] theorem coe_eq_zero [partial_order α] {a : α} : (a : with_top α) = 0 ↔ a = 0 := iff.intro (assume h, match a, h with _, rfl := rfl end) (assume h, h.symm ▸ rfl) @[simp] theorem zero_eq_coe [partial_order α] {a : α} : 0 = (a : with_top α) ↔ a = 0 := by rw [eq_comm, coe_eq_zero] @[simp] theorem coe_zero [partial_order α] : ↑(0 : α) = (0 : with_top α) := rfl @[simp] lemma mul_top {a : with_top α} (h : a ≠ 0) : a * ⊤ = ⊤ := by cases a; simp [mul_def, h]; refl @[simp] lemma top_mul {a : with_top α} (h : a ≠ 0) : ⊤ * a = ⊤ := by cases a; simp [mul_def, h]; refl @[simp] lemma top_mul_top : (⊤ * ⊤ : with_top α) = ⊤ := top_mul top_ne_zero lemma coe_mul {a b : α} : (↑(a * b) : with_top α) = a * b := decidable.by_cases (assume : a = 0, by simp [this]) $ assume ha, decidable.by_cases (assume : b = 0, by simp [this]) $ assume hb, by simp [*, mul_def]; refl lemma mul_coe {b : α} (hb : b ≠ 0) : ∀{a : with_top α}, a * b = a.bind (λa:α, ↑(a * b)) | none := show (if (⊤:with_top α) = 0 ∨ (b:with_top α) = 0 then 0 else ⊤ : with_top α) = ⊤, by simp [hb] | (some a) := show ↑a * ↑b = ↑(a * b), from coe_mul.symm private lemma comm (a b : with_top α) : a * b = b * a := begin by_cases ha : a = 0, { simp [ha] }, by_cases hb : b = 0, { simp [hb] }, simp [ha, hb, mul_def, option.bind_comm a b, mul_comm] end @[simp] lemma mul_eq_top_iff {a b : with_top α} : a * b = ⊤ ↔ (a ≠ 0 ∧ b = ⊤) ∨ (a = ⊤ ∧ b ≠ 0) := begin have H : ∀x:α, (¬x = 0) ↔ (⊤ : with_top α) * ↑x = ⊤ := λx, ⟨λhx, by simp [top_mul, hx], λhx f, by simpa [f] using hx⟩, cases a; cases b; simp [none_eq_top, top_mul, coe_ne_top, some_eq_coe, coe_mul.symm], { rw [H b] }, { rw [H a, comm] } end private lemma distrib' (a b c : with_top α) : (a + b) * c = a * c + b * c := begin cases c, { show (a + b) * ⊤ = a * ⊤ + b * ⊤, by_cases ha : a = 0; simp [ha] }, { show (a + b) * c = a * c + b * c, by_cases hc : c = 0, { simp [hc] }, simp [mul_coe hc], cases a; cases b, repeat { refl <|> exact congr_arg some (add_mul _ _ _) } } end private lemma mul_eq_zero (a b : with_top α) : a * b = 0 ↔ a = 0 ∨ b = 0 := by cases a; cases b; dsimp [mul_def]; split_ifs; simp [*, none_eq_top, some_eq_coe, canonically_ordered_comm_semiring.mul_eq_zero_iff] at * private lemma assoc (a b c : with_top α) : (a * b) * c = a * (b * c) := begin cases a, { by_cases hb : b = 0; by_cases hc : c = 0; simp [*, none_eq_top, mul_eq_zero b c] }, cases b, { by_cases ha : a = 0; by_cases hc : c = 0; simp [*, none_eq_top, some_eq_coe, mul_eq_zero ↑a c] }, cases c, { by_cases ha : a = 0; by_cases hb : b = 0; simp [*, none_eq_top, some_eq_coe, mul_eq_zero ↑a ↑b] }, simp [some_eq_coe, coe_mul.symm, mul_assoc] end private lemma one_mul' : ∀a : with_top α, 1 * a = a | none := show ((1:α) : with_top α) * ⊤ = ⊤, by simp [-with_bot.coe_one] | (some a) := show ((1:α) : with_top α) * a = a, by simp [coe_mul.symm, -with_bot.coe_one] instance [canonically_ordered_comm_semiring α] [decidable_eq α] : canonically_ordered_comm_semiring (with_top α) := { one := (1 : α), right_distrib := distrib', left_distrib := assume a b c, by rw [comm, distrib', comm b, comm c]; refl, mul_assoc := assoc, mul_comm := comm, mul_eq_zero_iff := mul_eq_zero, one_mul := one_mul', mul_one := assume a, by rw [comm, one_mul'], zero_ne_one := assume h, @zero_ne_one α _ $ option.some.inj h, .. with_top.add_comm_monoid, .. with_top.mul_zero_class, .. with_top.canonically_ordered_monoid } @[simp] lemma coe_nat : ∀(n : nat), ((n : α) : with_top α) = n | 0 := rfl | (n+1) := have (((1 : nat) : α) : with_top α) = ((1 : nat) : with_top α) := rfl, by rw [nat.cast_add, coe_add, nat.cast_add, coe_nat n, this] @[simp] lemma nat_ne_top (n : nat) : (n : with_top α ) ≠ ⊤ := by rw [←coe_nat n]; apply coe_ne_top @[simp] lemma top_ne_nat (n : nat) : (⊤ : with_top α) ≠ n := by rw [←coe_nat n]; apply top_ne_coe lemma add_one_le_of_lt {i n : with_top ℕ} (h : i < n) : i + 1 ≤ n := begin cases n, { exact le_top }, cases i, { exact (not_le_of_lt h le_top).elim }, exact with_top.coe_le_coe.2 (with_top.coe_lt_coe.1 h) end @[elab_as_eliminator] lemma nat_induction {P : with_top ℕ → Prop} (a : with_top ℕ) (h0 : P 0) (hsuc : ∀n:ℕ, P n → P n.succ) (htop : (∀n : ℕ, P n) → P ⊤) : P a := begin have A : ∀n:ℕ, P n, { assume n, induction n with n IH, { exact h0 }, { exact hsuc n IH } }, cases a, { exact htop A }, { exact A a } end end with_top
de725bb289223364523507e48ee9be2cff5eb93a
ad0c7d243dc1bd563419e2767ed42fb323d7beea
/order/order_iso.lean
6e85c019047201d66cf0351921556576f6e136b7
[ "Apache-2.0" ]
permissive
sebzim4500/mathlib
e0b5a63b1655f910dee30badf09bd7e191d3cf30
6997cafbd3a7325af5cb318561768c316ceb7757
refs/heads/master
1,585,549,958,618
1,538,221,723,000
1,538,221,723,000
150,869,076
0
0
Apache-2.0
1,538,229,323,000
1,538,229,323,000
null
UTF-8
Lean
false
false
12,873
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import order.basic logic.embedding data.nat.basic open function universes u v w variables {α : Type*} {β : Type*} {γ : Type*} {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} structure order_embedding {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends α ↪ β := (ord : ∀ {a b}, r a b ↔ s (to_embedding a) (to_embedding b)) infix ` ≼o `:50 := order_embedding /-- the induced order on a subtype is an embedding under the natural inclusion. -/ definition subtype.order_embedding {X : Type*} (r : X → X → Prop) (p : X → Prop) : ((subtype.val : subtype p → X) ⁻¹'o r) ≼o r := ⟨⟨subtype.val,subtype.val_injective⟩,by intros;refl⟩ namespace order_embedding instance : has_coe_to_fun (r ≼o s) := ⟨λ _, α → β, λ o, o.to_embedding⟩ theorem ord' : ∀ (f : r ≼o s) {a b}, r a b ↔ s (f a) (f b) | ⟨f, o⟩ := @o @[simp] theorem coe_fn_mk (f : α ↪ β) (o) : (@order_embedding.mk _ _ r s f o : α → β) = f := rfl @[simp] theorem coe_fn_to_embedding (f : r ≼o s) : (f.to_embedding : α → β) = f := rfl theorem eq_of_to_fun_eq : ∀ {e₁ e₂ : r ≼o s}, (e₁ : α → β) = e₂ → e₁ = e₂ | ⟨⟨f₁, h₁⟩, o₁⟩ ⟨⟨f₂, h₂⟩, o₂⟩ h := by congr; exact h @[refl] protected def refl (r : α → α → Prop) : r ≼o r := ⟨embedding.refl _, λ a b, iff.rfl⟩ @[trans] protected def trans : r ≼o s → s ≼o t → r ≼o t | ⟨f₁, o₁⟩ ⟨f₂, o₂⟩ := ⟨f₁.trans f₂, λ a b, by rw [o₁, o₂]; simp⟩ @[simp] theorem refl_apply (x : α) : order_embedding.refl r x = x := rfl @[simp] theorem trans_apply : ∀ (f : r ≼o s) (g : s ≼o t) (a : α), (f.trans g) a = g (f a) | ⟨f₁, o₁⟩ ⟨f₂, o₂⟩ a := rfl /-- An order embedding is also an order embedding between dual orders. -/ def rsymm (f : r ≼o s) : swap r ≼o swap s := ⟨f.to_embedding, λ a b, f.ord'⟩ /-- If `f` is injective, then it is an order embedding from the preimage order of `s` to `s`. -/ def preimage (f : α ↪ β) (s : β → β → Prop) : f ⁻¹'o s ≼o s := ⟨f, λ a b, iff.rfl⟩ theorem eq_preimage (f : r ≼o s) : r = f ⁻¹'o s := by funext a b; exact propext f.ord' protected theorem is_irrefl : ∀ (f : r ≼o s) [is_irrefl β s], is_irrefl α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a h, H _ (o.1 h)⟩ protected theorem is_refl : ∀ (f : r ≼o s) [is_refl β s], is_refl α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a, o.2 (H _)⟩ protected theorem is_symm : ∀ (f : r ≼o s) [is_symm β s], is_symm α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h, o.2 (H _ _ (o.1 h))⟩ protected theorem is_asymm : ∀ (f : r ≼o s) [is_asymm β s], is_asymm α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h₁ h₂, H _ _ (o.1 h₁) (o.1 h₂)⟩ protected theorem is_antisymm : ∀ (f : r ≼o s) [is_antisymm β s], is_antisymm α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h₁ h₂, f.inj' (H _ _ (o.1 h₁) (o.1 h₂))⟩ protected theorem is_trans : ∀ (f : r ≼o s) [is_trans β s], is_trans α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b c h₁ h₂, o.2 (H _ _ _ (o.1 h₁) (o.1 h₂))⟩ protected theorem is_total : ∀ (f : r ≼o s) [is_total β s], is_total α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b, (or_congr o o).2 (H _ _)⟩ protected theorem is_preorder : ∀ (f : r ≼o s) [is_preorder β s], is_preorder α r | f H := by exactI {..f.is_refl, ..f.is_trans} protected theorem is_partial_order : ∀ (f : r ≼o s) [is_partial_order β s], is_partial_order α r | f H := by exactI {..f.is_preorder, ..f.is_antisymm} protected theorem is_linear_order : ∀ (f : r ≼o s) [is_linear_order β s], is_linear_order α r | f H := by exactI {..f.is_partial_order, ..f.is_total} protected theorem is_strict_order : ∀ (f : r ≼o s) [is_strict_order β s], is_strict_order α r | f H := by exactI {..f.is_irrefl, ..f.is_trans} protected theorem is_trichotomous : ∀ (f : r ≼o s) [is_trichotomous β s], is_trichotomous α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b, (or_congr o (or_congr f.inj'.eq_iff.symm o)).2 (H _ _)⟩ protected theorem is_strict_total_order' : ∀ (f : r ≼o s) [is_strict_total_order' β s], is_strict_total_order' α r | f H := by exactI {..f.is_trichotomous, ..f.is_strict_order} protected theorem acc (f : r ≼o s) (a : α) : acc s (f a) → acc r a := begin generalize h : f a = b, intro ac, induction ac with _ H IH generalizing a, subst h, exact ⟨_, λ a' h, IH (f a') (f.ord'.1 h) _ rfl⟩ end protected theorem well_founded : ∀ (f : r ≼o s) (h : well_founded s), well_founded r | f ⟨H⟩ := ⟨λ a, f.acc _ (H _)⟩ protected theorem is_well_order : ∀ (f : r ≼o s) [is_well_order β s], is_well_order α r | f H := by exactI {wf := f.well_founded H.wf, ..f.is_strict_total_order'} /-- It suffices to prove `f` is monotone between strict orders to show it is an order embedding. -/ def of_monotone [is_trichotomous α r] [is_asymm β s] (f : α → β) (H : ∀ a b, r a b → s (f a) (f b)) : r ≼o s := begin haveI := @is_irrefl_of_is_asymm β s _, refine ⟨⟨f, λ a b e, _⟩, λ a b, ⟨H _ _, λ h, _⟩⟩, { refine ((@trichotomous _ r _ a b).resolve_left _).resolve_right _; exact λ h, @irrefl _ s _ _ (by simpa [e] using H _ _ h) }, { refine (@trichotomous _ r _ a b).resolve_right (or.rec (λ e, _) (λ h', _)), { subst e, exact irrefl _ h }, { exact asymm (H _ _ h') h } } end @[simp] theorem of_monotone_coe [is_trichotomous α r] [is_asymm β s] (f : α → β) (H) : (@of_monotone _ _ r s _ _ f H : α → β) = f := rfl -- If le is preserved by an order embedding of preorders, then lt is too def lt_embedding_of_le_embedding [preorder α] [preorder β] (f : (has_le.le : α → α → Prop) ≼o (has_le.le : β → β → Prop)) : (has_lt.lt : α → α → Prop) ≼o (has_lt.lt : β → β → Prop) := { to_fun := f, inj := f.inj, ord := by intros; simp [lt_iff_le_not_le,f.ord] } theorem nat_lt [is_strict_order α r] (f : ℕ → α) (H : ∀ n:ℕ, r (f n) (f (n+1))) : ((<) : ℕ → ℕ → Prop) ≼o r := of_monotone f $ λ a b h, begin induction b with b IH, {exact (nat.not_lt_zero _ h).elim}, cases nat.lt_succ_iff_lt_or_eq.1 h with h e, { exact trans (IH h) (H _) }, { subst b, apply H } end theorem nat_gt [is_strict_order α r] (f : ℕ → α) (H : ∀ n:ℕ, r (f (n+1)) (f n)) : ((>) : ℕ → ℕ → Prop) ≼o r := by haveI := is_strict_order.swap r; exact rsymm (nat_lt f H) theorem well_founded_iff_no_descending_seq [is_strict_order α r] : well_founded r ↔ ¬ nonempty (((>) : ℕ → ℕ → Prop) ≼o r) := ⟨λ ⟨h⟩ ⟨⟨f, o⟩⟩, suffices ∀ a, acc r a → ∀ n, a ≠ f n, from this (f 0) (h _) 0 rfl, λ a ac, begin induction ac with a _ IH, intros n h, subst a, exact IH (f (n+1)) (o.1 (nat.lt_succ_self _)) _ rfl end, λ N, ⟨λ a, classical.by_contradiction $ λ na, let ⟨f, h⟩ := classical.axiom_of_choice $ show ∀ x : {a // ¬ acc r a}, ∃ y : {a // ¬ acc r a}, r y.1 x.1, from λ ⟨x, h⟩, classical.by_contradiction $ λ hn, h $ ⟨_, λ y h, classical.by_contradiction $ λ na, hn ⟨⟨y, na⟩, h⟩⟩ in N ⟨nat_gt (λ n, (f^[n] ⟨a, na⟩).1) $ λ n, by rw nat.iterate_succ'; apply h⟩⟩⟩ end order_embedding /-- The inclusion map `fin n → ℕ` is an order embedding. -/ def fin.val.order_embedding (n) : @order_embedding (fin n) ℕ (<) (<) := ⟨⟨fin.val, @fin.eq_of_veq _⟩, λ a b, iff.rfl⟩ /-- The inclusion map `fin m → fin n` is an order embedding. -/ def fin_fin.order_embedding {m n} (h : m ≤ n) : @order_embedding (fin m) (fin n) (<) (<) := ⟨⟨λ ⟨x, h'⟩, ⟨x, lt_of_lt_of_le h' h⟩, λ ⟨a, _⟩ ⟨b, _⟩ h, by congr; injection h⟩, by intros; cases a; cases b; refl⟩ instance fin.lt.is_well_order (n) : is_well_order (fin n) (<) := (fin.val.order_embedding _).is_well_order /-- An order isomorphism is an equivalence that is also an order embedding. -/ structure order_iso {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends α ≃ β := (ord : ∀ {a b}, r a b ↔ s (to_equiv a) (to_equiv b)) infix ` ≃o `:50 := order_iso namespace order_iso def to_order_embedding (f : r ≃o s) : r ≼o s := ⟨f.to_equiv.to_embedding, f.ord⟩ instance : has_coe (r ≃o s) (r ≼o s) := ⟨to_order_embedding⟩ theorem coe_coe_fn (f : r ≃o s) : ((f : r ≼o s) : α → β) = f := rfl theorem ord' : ∀ (f : r ≃o s) {a b}, r a b ↔ s (f a) (f b) | ⟨f, o⟩ := @o @[simp] theorem coe_fn_mk (f : α ≃ β) (o) : (@order_iso.mk _ _ r s f o : α → β) = f := rfl @[simp] theorem coe_fn_to_equiv (f : r ≃o s) : (f.to_equiv : α → β) = f := rfl theorem eq_of_to_fun_eq : ∀ {e₁ e₂ : r ≃o s}, (e₁ : α → β) = e₂ → e₁ = e₂ | ⟨e₁, o₁⟩ ⟨e₂, o₂⟩ h := by congr; exact equiv.eq_of_to_fun_eq h @[refl] protected def refl (r : α → α → Prop) : r ≃o r := ⟨equiv.refl _, λ a b, iff.rfl⟩ @[symm] protected def symm (f : r ≃o s) : s ≃o r := ⟨f.to_equiv.symm, λ a b, by cases f with f o; rw o; simp⟩ @[trans] protected def trans (f₁ : r ≃o s) (f₂ : s ≃o t) : r ≃o t := ⟨f₁.to_equiv.trans f₂.to_equiv, λ a b, by cases f₁ with f₁ o₁; cases f₂ with f₂ o₂; rw [o₁, o₂]; simp⟩ @[simp] theorem coe_fn_symm_mk (f o) : ((@order_iso.mk _ _ r s f o).symm : β → α) = f.symm := rfl @[simp] theorem refl_apply (x : α) : order_iso.refl r x = x := rfl @[simp] theorem trans_apply : ∀ (f : r ≃o s) (g : s ≃o t) (a : α), (f.trans g) a = g (f a) | ⟨f₁, o₁⟩ ⟨f₂, o₂⟩ a := equiv.trans_apply _ _ _ @[simp] theorem apply_inverse_apply : ∀ (e : r ≃o s) (x : β), e (e.symm x) = x | ⟨f₁, o₁⟩ x := by simp @[simp] theorem inverse_apply_apply : ∀ (e : r ≃o s) (x : α), e.symm (e x) = x | ⟨f₁, o₁⟩ x := by simp /-- Any equivalence lifts to an order isomorphism between `s` and its preimage. -/ def preimage (f : α ≃ β) (s : β → β → Prop) : f ⁻¹'o s ≃o s := ⟨f, λ a b, iff.rfl⟩ noncomputable def of_surjective (f : r ≼o s) (H : surjective f) : r ≃o s := ⟨equiv.of_bijective ⟨f.inj, H⟩, by simp [f.ord']⟩ @[simp] theorem of_surjective_coe (f : r ≼o s) (H) : (of_surjective f H : α → β) = f := by delta of_surjective; simp theorem sum_lex_congr {α₁ α₂ β₁ β₂ r₁ r₂ s₁ s₂} (e₁ : @order_iso α₁ α₂ r₁ r₂) (e₂ : @order_iso β₁ β₂ s₁ s₂) : sum.lex r₁ s₁ ≃o sum.lex r₂ s₂ := ⟨equiv.sum_congr e₁.to_equiv e₂.to_equiv, λ a b, by cases e₁ with f hf; cases e₂ with g hg; cases a; cases b; simp [hf, hg]⟩ theorem prod_lex_congr {α₁ α₂ β₁ β₂ r₁ r₂ s₁ s₂} (e₁ : @order_iso α₁ α₂ r₁ r₂) (e₂ : @order_iso β₁ β₂ s₁ s₂) : prod.lex r₁ s₁ ≃o prod.lex r₂ s₂ := ⟨equiv.prod_congr e₁.to_equiv e₂.to_equiv, λ a b, begin cases e₁ with f hf; cases e₂ with g hg, cases a with a₁ a₂; cases b with b₁ b₂, suffices : prod.lex r₁ s₁ (a₁, a₂) (b₁, b₂) ↔ prod.lex r₂ s₂ (f a₁, g a₂) (f b₁, g b₂), {simpa [hf, hg]}, split, { intro h, cases h with _ _ _ _ h _ _ _ h, { left, exact hf.1 h }, { right, exact hg.1 h } }, { generalize e : f b₁ = fb₁, intro h, cases h with _ _ _ _ h _ _ _ h, { subst e, left, exact hf.2 h }, { have := f.bijective.1 e, subst b₁, right, exact hg.2 h } } end⟩ end order_iso /-- A subset `p : set α` embeds into `α` -/ def set_coe_embedding {α : Type*} (p : set α) : p ↪ α := ⟨subtype.val, @subtype.eq _ _⟩ /-- `subrel r p` is the inherited relation on a subset. -/ def subrel (r : α → α → Prop) (p : set α) : p → p → Prop := @subtype.val _ p ⁻¹'o r @[simp] theorem subrel_val (r : α → α → Prop) (p : set α) {a b} : subrel r p a b ↔ r a.1 b.1 := iff.rfl namespace subrel protected def order_embedding (r : α → α → Prop) (p : set α) : subrel r p ≼o r := ⟨set_coe_embedding _, λ a b, iff.rfl⟩ @[simp] theorem order_embedding_apply (r : α → α → Prop) (p a) : subrel.order_embedding r p a = a.1 := rfl instance (r : α → α → Prop) [is_well_order α r] (p : set α) : is_well_order p (subrel r p) := order_embedding.is_well_order (subrel.order_embedding r p) end subrel /-- Restrict the codomain of an order embedding -/ def order_embedding.cod_restrict (p : set β) (f : r ≼o s) (H : ∀ a, f a ∈ p) : r ≼o subrel s p := ⟨f.to_embedding.cod_restrict p H, f.ord⟩ @[simp] theorem order_embedding.cod_restrict_apply (p) (f : r ≼o s) (H a) : order_embedding.cod_restrict p f H a = ⟨f a, H a⟩ := rfl
5eae429d024a17d27431c1f721dd89f89aa1bebc
df561f413cfe0a88b1056655515399c546ff32a5
/3-multiplication-world/l9.lean
a75dad341b714cd6d7cf1c256f586afed55fb931
[]
no_license
nicholaspun/natural-number-game-solutions
31d5158415c6f582694680044c5c6469032c2a06
1e2aed86d2e76a3f4a275c6d99e795ad30cf6df0
refs/heads/main
1,675,123,625,012
1,607,633,548,000
1,607,633,548,000
318,933,860
3
1
null
null
null
null
UTF-8
Lean
false
false
192
lean
lemma mul_left_comm (a b c : mynat) : a * (b * c) = b * (a * c) := begin induction b with k Pk, repeat { rw zero_mul }, rw mul_zero, refl, repeat { rw succ_mul }, rw mul_add, rw Pk, refl, end
0a270d877a5452265e3b59ebdd3acdc28b9035b3
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/group/hom_instances.lean
672efaf836ffbeac75d690243dadc2889ccb9dc6
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
9,640
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Kevin Buzzard, Scott Morrison, Johan Commelin, Chris Hughes, Johannes Hölzl, Yury Kudryashov -/ import algebra.group_power.basic /-! # Instances on spaces of monoid and group morphisms We endow the space of monoid morphisms `M →* N` with a `comm_monoid` structure when the target is commutative, through pointwise multiplication, and with a `comm_group` structure when the target is a commutative group. We also prove the same instances for additive situations. Since these structures permit morphisms of morphisms, we also provide some composition-like operations. Finally, we provide the `ring` structure on `add_monoid.End`. -/ universes uM uN uP uQ variables {M : Type uM} {N : Type uN} {P : Type uP} {Q : Type uQ} /-- `(M →* N)` is a `comm_monoid` if `N` is commutative. -/ @[to_additive "`(M →+ N)` is an `add_comm_monoid` if `N` is commutative."] instance [mul_one_class M] [comm_monoid N] : comm_monoid (M →* N) := { mul := (*), mul_assoc := by intros; ext; apply mul_assoc, one := 1, one_mul := by intros; ext; apply one_mul, mul_one := by intros; ext; apply mul_one, mul_comm := by intros; ext; apply mul_comm, npow := λ n f, { to_fun := λ x, (f x) ^ n, map_one' := by simp, map_mul' := λ x y, by simp [mul_pow] }, npow_zero' := λ f, by { ext x, simp }, npow_succ' := λ n f, by { ext x, simp [pow_succ] } } /-- If `G` is a commutative group, then `M →* G` is a commutative group too. -/ @[to_additive "If `G` is an additive commutative group, then `M →+ G` is an additive commutative group too."] instance {M G} [mul_one_class M] [comm_group G] : comm_group (M →* G) := { inv := has_inv.inv, div := has_div.div, div_eq_mul_inv := by { intros, ext, apply div_eq_mul_inv }, mul_left_inv := by intros; ext; apply mul_left_inv, zpow := λ n f, { to_fun := λ x, (f x) ^ n, map_one' := by simp, map_mul' := λ x y, by simp [mul_zpow] }, zpow_zero' := λ f, by { ext x, simp }, zpow_succ' := λ n f, by { ext x, simp [zpow_of_nat, pow_succ] }, zpow_neg' := λ n f, by { ext x, simp }, ..monoid_hom.comm_monoid } instance [add_comm_monoid M] : semiring (add_monoid.End M) := { zero_mul := λ x, add_monoid_hom.ext $ λ i, rfl, mul_zero := λ x, add_monoid_hom.ext $ λ i, add_monoid_hom.map_zero _, left_distrib := λ x y z, add_monoid_hom.ext $ λ i, add_monoid_hom.map_add _ _ _, right_distrib := λ x y z, add_monoid_hom.ext $ λ i, rfl, .. add_monoid.End.monoid M, .. add_monoid_hom.add_comm_monoid } instance [add_comm_group M] : ring (add_monoid.End M) := { .. add_monoid.End.semiring, .. add_monoid_hom.add_comm_group } /-! ### Morphisms of morphisms The structures above permit morphisms that themselves produce morphisms, provided the codomain is commutative. -/ namespace monoid_hom @[to_additive] lemma ext_iff₂ {mM : mul_one_class M} {mN : mul_one_class N} {mP : comm_monoid P} {f g : M →* N →* P} : f = g ↔ (∀ x y, f x y = g x y) := monoid_hom.ext_iff.trans $ forall_congr $ λ _, monoid_hom.ext_iff /-- `flip` arguments of `f : M →* N →* P` -/ @[to_additive "`flip` arguments of `f : M →+ N →+ P`"] def flip {mM : mul_one_class M} {mN : mul_one_class N} {mP : comm_monoid P} (f : M →* N →* P) : N →* M →* P := { to_fun := λ y, ⟨λ x, f x y, by rw [f.map_one, one_apply], λ x₁ x₂, by rw [f.map_mul, mul_apply]⟩, map_one' := ext $ λ x, (f x).map_one, map_mul' := λ y₁ y₂, ext $ λ x, (f x).map_mul y₁ y₂ } @[simp, to_additive] lemma flip_apply {mM : mul_one_class M} {mN : mul_one_class N} {mP : comm_monoid P} (f : M →* N →* P) (x : M) (y : N) : f.flip y x = f x y := rfl @[to_additive] lemma map_one₂ {mM : mul_one_class M} {mN : mul_one_class N} {mP : comm_monoid P} (f : M →* N →* P) (n : N) : f 1 n = 1 := (flip f n).map_one @[to_additive] lemma map_mul₂ {mM : mul_one_class M} {mN : mul_one_class N} {mP : comm_monoid P} (f : M →* N →* P) (m₁ m₂ : M) (n : N) : f (m₁ * m₂) n = f m₁ n * f m₂ n := (flip f n).map_mul _ _ @[to_additive] lemma map_inv₂ {mM : group M} {mN : mul_one_class N} {mP : comm_group P} (f : M →* N →* P) (m : M) (n : N) : f m⁻¹ n = (f m n)⁻¹ := (flip f n).map_inv _ @[to_additive] lemma map_div₂ {mM : group M} {mN : mul_one_class N} {mP : comm_group P} (f : M →* N →* P) (m₁ m₂ : M) (n : N) : f (m₁ / m₂) n = f m₁ n / f m₂ n := (flip f n).map_div _ _ /-- Evaluation of a `monoid_hom` at a point as a monoid homomorphism. See also `monoid_hom.apply` for the evaluation of any function at a point. -/ @[to_additive "Evaluation of an `add_monoid_hom` at a point as an additive monoid homomorphism. See also `add_monoid_hom.apply` for the evaluation of any function at a point.", simps] def eval [mul_one_class M] [comm_monoid N] : M →* (M →* N) →* N := (monoid_hom.id (M →* N)).flip /-- The expression `λ g m, g (f m)` as a `monoid_hom`. Equivalently, `(λ g, monoid_hom.comp g f)` as a `monoid_hom`. -/ @[to_additive "The expression `λ g m, g (f m)` as a `add_monoid_hom`. Equivalently, `(λ g, monoid_hom.comp g f)` as a `add_monoid_hom`. This also exists in a `linear_map` version, `linear_map.lcomp`.", simps] def comp_hom' [mul_one_class M] [mul_one_class N] [comm_monoid P] (f : M →* N) : (N →* P) →* M →* P := flip $ eval.comp f /-- Composition of monoid morphisms (`monoid_hom.comp`) as a monoid morphism. Note that unlike `monoid_hom.comp_hom'` this requires commutativity of `N`. -/ @[to_additive "Composition of additive monoid morphisms (`add_monoid_hom.comp`) as an additive monoid morphism. Note that unlike `add_monoid_hom.comp_hom'` this requires commutativity of `N`. This also exists in a `linear_map` version, `linear_map.llcomp`.", simps] def comp_hom [mul_one_class M] [comm_monoid N] [comm_monoid P] : (N →* P) →* (M →* N) →* (M →* P) := { to_fun := λ g, { to_fun := g.comp, map_one' := comp_one g, map_mul' := comp_mul g }, map_one' := by { ext1 f, exact one_comp f }, map_mul' := λ g₁ g₂, by { ext1 f, exact mul_comp g₁ g₂ f } } /-- Flipping arguments of monoid morphisms (`monoid_hom.flip`) as a monoid morphism. -/ @[to_additive "Flipping arguments of additive monoid morphisms (`add_monoid_hom.flip`) as an additive monoid morphism.", simps] def flip_hom {mM : mul_one_class M} {mN : mul_one_class N} {mP : comm_monoid P} : (M →* N →* P) →* (N →* M →* P) := { to_fun := monoid_hom.flip, map_one' := rfl, map_mul' := λ f g, rfl } /-- The expression `λ m q, f m (g q)` as a `monoid_hom`. Note that the expression `λ q n, f (g q) n` is simply `monoid_hom.comp`. -/ @[to_additive "The expression `λ m q, f m (g q)` as an `add_monoid_hom`. Note that the expression `λ q n, f (g q) n` is simply `add_monoid_hom.comp`. This also exists as a `linear_map` version, `linear_map.compl₂`"] def compl₂ [mul_one_class M] [mul_one_class N] [comm_monoid P] [comm_monoid Q] (f : M →* N →* P) (g : Q →* N) : M →* Q →* P := (comp_hom' g).comp f @[simp, to_additive] lemma compl₂_apply [mul_one_class M] [mul_one_class N] [comm_monoid P] [comm_monoid Q] (f : M →* N →* P) (g : Q →* N) (m : M) (q : Q) : (compl₂ f g) m q = f m (g q) := rfl /-- The expression `λ m n, g (f m n)` as a `monoid_hom`. -/ @[to_additive "The expression `λ m n, g (f m n)` as an `add_monoid_hom`. This also exists as a linear_map version, `linear_map.compr₂`"] def compr₂ [mul_one_class M] [mul_one_class N] [comm_monoid P] [comm_monoid Q] (f : M →* N →* P) (g : P →* Q) : M →* N →* Q := (comp_hom g).comp f @[simp, to_additive] lemma compr₂_apply [mul_one_class M] [mul_one_class N] [comm_monoid P] [comm_monoid Q] (f : M →* N →* P) (g : P →* Q) (m : M) (n : N) : (compr₂ f g) m n = g (f m n) := rfl end monoid_hom /-! ### Miscellaneous definitions Due to the fact this file imports `algebra.group_power.basic`, it is not possible to import it in some of the lower-level files like `algebra.ring.basic`. The following lemmas should be rehomed if the import structure permits them to be. -/ section semiring variables {R S : Type*} [non_unital_non_assoc_semiring R] [non_unital_non_assoc_semiring S] /-- Multiplication of an element of a (semi)ring is an `add_monoid_hom` in both arguments. This is a more-strongly bundled version of `add_monoid_hom.mul_left` and `add_monoid_hom.mul_right`. A stronger version of this exists for algebras as `algebra.lmul`. -/ def add_monoid_hom.mul : R →+ R →+ R := { to_fun := add_monoid_hom.mul_left, map_zero' := add_monoid_hom.ext $ zero_mul, map_add' := λ a b, add_monoid_hom.ext $ add_mul a b } lemma add_monoid_hom.mul_apply (x y : R) : add_monoid_hom.mul x y = x * y := rfl @[simp] lemma add_monoid_hom.coe_mul : ⇑(add_monoid_hom.mul : R →+ R →+ R) = add_monoid_hom.mul_left := rfl @[simp] lemma add_monoid_hom.coe_flip_mul : ⇑(add_monoid_hom.mul : R →+ R →+ R).flip = add_monoid_hom.mul_right := rfl /-- An `add_monoid_hom` preserves multiplication if pre- and post- composition with `add_monoid_hom.mul` are equivalent. By converting the statement into an equality of `add_monoid_hom`s, this lemma allows various specialized `ext` lemmas about `→+` to then be applied. -/ lemma add_monoid_hom.map_mul_iff (f : R →+ S) : (∀ x y, f (x * y) = f x * f y) ↔ (add_monoid_hom.mul : R →+ R →+ R).compr₂ f = (add_monoid_hom.mul.comp f).compl₂ f := iff.symm add_monoid_hom.ext_iff₂ end semiring
aec5f43d780f1254335f5310928c21b4e139c55a
6fca17f8d5025f89be1b2d9d15c9e0c4b4900cbf
/src/game/world2/level2.lean
4b46ba4156067b30a7cd2fa89d1899e91391baa9
[ "Apache-2.0" ]
permissive
arolihas/natural_number_game
4f0c93feefec93b8824b2b96adff8b702b8b43ce
8e4f7b4b42888a3b77429f90cce16292bd288138
refs/heads/master
1,621,872,426,808
1,586,270,467,000
1,586,270,467,000
253,648,466
0
0
null
1,586,219,694,000
1,586,219,694,000
null
UTF-8
Lean
false
false
1,683
lean
import mynat.definition -- hide import mynat.add -- hide import game.world2.level1 -- hide namespace mynat -- hide /- # Addition world Don't forget to use the drop down boxes on the left to see your tactics and what you have proved so far. ## Level 2: `add_assoc` -- associativity of addition. It's well-known that (1 + 2) + 3 = 1 + (2 + 3) -- if we have three numbers to add up, it doesn't matter which of the additions we do first. This fact is called *associativity of addition* by mathematicians, and it is *not* obvious. For example, subtraction really is not associative: $(6 - 2) - 1$ is really not equal to $6 - (2 - 1)$. We are going to have to prove that addition, as defined the way we've defined it, is associative. See if you can prove associativity of addition. Hint: because addition was defined by recursion on the right-most variable, use induction on the right-most variable (try other variables at your peril!). Note that when Lean writes `a + b + c`, it means `(a + b) + c`. If it wants to talk about `a + (b + c)` it will put the brackets in explictly. Reminder: you are done when you see "Proof complete!" in the top right, and an empty box (no errors) in the bottom right. You can move between levels and worlds (i.e. you can go back and review old stuff) without losing anything. Once you're done with associativity (sub-boss), we can move on to commutativity (boss). -/ /- Lemma On the set of natural numbers, addition is associative. In other words, for all natural numbers $a, b$ and $c$, we have $$ (a + b) + c = a + (b + c). $$ -/ lemma add_assoc (a b c : mynat) : (a + b) + c = a + (b + c) := begin [nat_num_game] end end mynat -- hide
b77150c7a5aba527c30a951c08beccf8b4cb8419
4727251e0cd73359b15b664c3170e5d754078599
/src/data/list/alist.lean
37ca52e6a9c6c0558dc431c2c5b346cdd0f36ae8
[ "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
11,991
lean
/- Copyright (c) 2018 Sean Leather. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sean Leather, Mario Carneiro -/ import data.list.sigma /-! # Association Lists This file defines association lists. An association list is a list where every element consists of a key and a value, and no two entries have the same key. The type of the value is allowed to be dependent on the type of the key. This type dependence is implemented using `sigma`: The elements of the list are of type `sigma β`, for some type index `β`. ## Main definitions Association lists are represented by the `alist` structure. This file defines this structure and provides ways to access, modify, and combine `alist`s. * `alist.keys` returns a list of keys of the alist. * `alist.has_mem` returns membership in the set of keys. * `alist.erase` removes a certain key. * `alist.insert` adds a key-value mapping to the list. * `alist.union` combines two association lists. ## References * <https://en.wikipedia.org/wiki/Association_list> -/ universes u v w open list variables {α : Type u} {β : α → Type v} /-- `alist β` is a key-value map stored as a `list` (i.e. a linked list). It is a wrapper around certain `list` functions with the added constraint that the list have unique keys. -/ structure alist (β : α → Type v) : Type (max u v) := (entries : list (sigma β)) (nodupkeys : entries.nodupkeys) /-- Given `l : list (sigma β)`, create a term of type `alist β` by removing entries with duplicate keys. -/ def list.to_alist [decidable_eq α] {β : α → Type v} (l : list (sigma β)) : alist β := { entries := _, nodupkeys := nodupkeys_dedupkeys l } namespace alist @[ext] theorem ext : ∀ {s t : alist β}, s.entries = t.entries → s = t | ⟨l₁, h₁⟩ ⟨l₂, h₂⟩ H := by congr' lemma ext_iff {s t : alist β} : s = t ↔ s.entries = t.entries := ⟨congr_arg _, ext⟩ instance [decidable_eq α] [∀ a, decidable_eq (β a)] : decidable_eq (alist β) := λ xs ys, by rw ext_iff; apply_instance /-! ### keys -/ /-- The list of keys of an association list. -/ def keys (s : alist β) : list α := s.entries.keys theorem keys_nodup (s : alist β) : s.keys.nodup := s.nodupkeys /-! ### mem -/ /-- The predicate `a ∈ s` means that `s` has a value associated to the key `a`. -/ instance : has_mem α (alist β) := ⟨λ a s, a ∈ s.keys⟩ theorem mem_keys {a : α} {s : alist β} : a ∈ s ↔ a ∈ s.keys := iff.rfl theorem mem_of_perm {a : α} {s₁ s₂ : alist β} (p : s₁.entries ~ s₂.entries) : a ∈ s₁ ↔ a ∈ s₂ := (p.map sigma.fst).mem_iff /-! ### empty -/ /-- The empty association list. -/ instance : has_emptyc (alist β) := ⟨⟨[], nodupkeys_nil⟩⟩ instance : inhabited (alist β) := ⟨∅⟩ theorem not_mem_empty (a : α) : a ∉ (∅ : alist β) := not_mem_nil a @[simp] theorem empty_entries : (∅ : alist β).entries = [] := rfl @[simp] theorem keys_empty : (∅ : alist β).keys = [] := rfl /-! ### singleton -/ /-- The singleton association list. -/ def singleton (a : α) (b : β a) : alist β := ⟨[⟨a, b⟩], nodupkeys_singleton _⟩ @[simp] theorem singleton_entries (a : α) (b : β a) : (singleton a b).entries = [sigma.mk a b] := rfl @[simp] theorem keys_singleton (a : α) (b : β a) : (singleton a b).keys = [a] := rfl /-! ### lookup -/ section variables [decidable_eq α] /-- Look up the value associated to a key in an association list. -/ def lookup (a : α) (s : alist β) : option (β a) := s.entries.lookup a @[simp] theorem lookup_empty (a) : lookup a (∅ : alist β) = none := rfl theorem lookup_is_some {a : α} {s : alist β} : (s.lookup a).is_some ↔ a ∈ s := lookup_is_some theorem lookup_eq_none {a : α} {s : alist β} : lookup a s = none ↔ a ∉ s := lookup_eq_none theorem perm_lookup {a : α} {s₁ s₂ : alist β} (p : s₁.entries ~ s₂.entries) : s₁.lookup a = s₂.lookup a := perm_lookup _ s₁.nodupkeys s₂.nodupkeys p instance (a : α) (s : alist β) : decidable (a ∈ s) := decidable_of_iff _ lookup_is_some /-! ### replace -/ /-- Replace a key with a given value in an association list. If the key is not present it does nothing. -/ def replace (a : α) (b : β a) (s : alist β) : alist β := ⟨kreplace a b s.entries, (kreplace_nodupkeys a b).2 s.nodupkeys⟩ @[simp] theorem keys_replace (a : α) (b : β a) (s : alist β) : (replace a b s).keys = s.keys := keys_kreplace _ _ _ @[simp] theorem mem_replace {a a' : α} {b : β a} {s : alist β} : a' ∈ replace a b s ↔ a' ∈ s := by rw [mem_keys, keys_replace, ←mem_keys] theorem perm_replace {a : α} {b : β a} {s₁ s₂ : alist β} : s₁.entries ~ s₂.entries → (replace a b s₁).entries ~ (replace a b s₂).entries := perm.kreplace s₁.nodupkeys end /-- Fold a function over the key-value pairs in the map. -/ def foldl {δ : Type w} (f : δ → Π a, β a → δ) (d : δ) (m : alist β) : δ := m.entries.foldl (λ r a, f r a.1 a.2) d /-! ### erase -/ section variables [decidable_eq α] /-- Erase a key from the map. If the key is not present, do nothing. -/ def erase (a : α) (s : alist β) : alist β := ⟨s.entries.kerase a, s.nodupkeys.kerase a⟩ @[simp] lemma keys_erase (a : α) (s : alist β) : (erase a s).keys = s.keys.erase a := keys_kerase @[simp] theorem mem_erase {a a' : α} {s : alist β} : a' ∈ erase a s ↔ a' ≠ a ∧ a' ∈ s := by rw [mem_keys, keys_erase, s.keys_nodup.mem_erase_iff, ←mem_keys] theorem perm_erase {a : α} {s₁ s₂ : alist β} : s₁.entries ~ s₂.entries → (erase a s₁).entries ~ (erase a s₂).entries := perm.kerase s₁.nodupkeys @[simp] theorem lookup_erase (a) (s : alist β) : lookup a (erase a s) = none := lookup_kerase a s.nodupkeys @[simp] theorem lookup_erase_ne {a a'} {s : alist β} (h : a ≠ a') : lookup a (erase a' s) = lookup a s := lookup_kerase_ne h theorem erase_erase (a a' : α) (s : alist β) : (s.erase a).erase a' = (s.erase a').erase a := ext $ kerase_kerase /-! ### insert -/ /-- Insert a key-value pair into an association list and erase any existing pair with the same key. -/ def insert (a : α) (b : β a) (s : alist β) : alist β := ⟨kinsert a b s.entries, kinsert_nodupkeys a b s.nodupkeys⟩ @[simp] theorem insert_entries {a} {b : β a} {s : alist β} : (insert a b s).entries = sigma.mk a b :: kerase a s.entries := rfl theorem insert_entries_of_neg {a} {b : β a} {s : alist β} (h : a ∉ s) : (insert a b s).entries = ⟨a, b⟩ :: s.entries := by rw [insert_entries, kerase_of_not_mem_keys h] @[simp] theorem mem_insert {a a'} {b' : β a'} (s : alist β) : a ∈ insert a' b' s ↔ a = a' ∨ a ∈ s := mem_keys_kinsert @[simp] theorem keys_insert {a} {b : β a} (s : alist β) : (insert a b s).keys = a :: s.keys.erase a := by simp [insert, keys, keys_kerase] theorem perm_insert {a} {b : β a} {s₁ s₂ : alist β} (p : s₁.entries ~ s₂.entries) : (insert a b s₁).entries ~ (insert a b s₂).entries := by simp only [insert_entries]; exact p.kinsert s₁.nodupkeys @[simp] theorem lookup_insert {a} {b : β a} (s : alist β) : lookup a (insert a b s) = some b := by simp only [lookup, insert, lookup_kinsert] @[simp] theorem lookup_insert_ne {a a'} {b' : β a'} {s : alist β} (h : a ≠ a') : lookup a (insert a' b' s) = lookup a s := lookup_kinsert_ne h @[simp] theorem lookup_to_alist {a} (s : list (sigma β)) : lookup a s.to_alist = s.lookup a := by rw [list.to_alist,lookup,lookup_dedupkeys] @[simp] theorem insert_insert {a} {b b' : β a} (s : alist β) : (s.insert a b).insert a b' = s.insert a b' := by ext : 1; simp only [alist.insert_entries, list.kerase_cons_eq]; constructor_matching* [_ ∧ _]; refl theorem insert_insert_of_ne {a a'} {b : β a} {b' : β a'} (s : alist β) (h : a ≠ a') : ((s.insert a b).insert a' b').entries ~ ((s.insert a' b').insert a b).entries := by simp only [insert_entries]; rw [kerase_cons_ne,kerase_cons_ne,kerase_comm]; [apply perm.swap, exact h, exact h.symm] @[simp] lemma insert_singleton_eq {a : α} {b b' : β a} : insert a b (singleton a b') = singleton a b := ext $ by simp only [alist.insert_entries, list.kerase_cons_eq, and_self, alist.singleton_entries, heq_iff_eq, eq_self_iff_true] @[simp] theorem entries_to_alist (xs : list (sigma β)) : (list.to_alist xs).entries = dedupkeys xs := rfl theorem to_alist_cons (a : α) (b : β a) (xs : list (sigma β)) : list.to_alist (⟨a,b⟩ :: xs) = insert a b xs.to_alist := rfl /-! ### extract -/ /-- Erase a key from the map, and return the corresponding value, if found. -/ def extract (a : α) (s : alist β) : option (β a) × alist β := have (kextract a s.entries).2.nodupkeys, by rw [kextract_eq_lookup_kerase]; exact s.nodupkeys.kerase _, match kextract a s.entries, this with | (b, l), h := (b, ⟨l, h⟩) end @[simp] theorem extract_eq_lookup_erase (a : α) (s : alist β) : extract a s = (lookup a s, erase a s) := by simp [extract]; split; refl /-! ### union -/ /-- `s₁ ∪ s₂` is the key-based union of two association lists. It is left-biased: if there exists an `a ∈ s₁`, `lookup a (s₁ ∪ s₂) = lookup a s₁`. -/ def union (s₁ s₂ : alist β) : alist β := ⟨s₁.entries.kunion s₂.entries, s₁.nodupkeys.kunion s₂.nodupkeys⟩ instance : has_union (alist β) := ⟨union⟩ @[simp] theorem union_entries {s₁ s₂ : alist β} : (s₁ ∪ s₂).entries = kunion s₁.entries s₂.entries := rfl @[simp] theorem empty_union {s : alist β} : (∅ : alist β) ∪ s = s := ext rfl @[simp] theorem union_empty {s : alist β} : s ∪ (∅ : alist β) = s := ext $ by simp @[simp] theorem mem_union {a} {s₁ s₂ : alist β} : a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂ := mem_keys_kunion theorem perm_union {s₁ s₂ s₃ s₄ : alist β} (p₁₂ : s₁.entries ~ s₂.entries) (p₃₄ : s₃.entries ~ s₄.entries) : (s₁ ∪ s₃).entries ~ (s₂ ∪ s₄).entries := by simp [p₁₂.kunion s₃.nodupkeys p₃₄] theorem union_erase (a : α) (s₁ s₂ : alist β) : erase a (s₁ ∪ s₂) = erase a s₁ ∪ erase a s₂ := ext kunion_kerase.symm @[simp] theorem lookup_union_left {a} {s₁ s₂ : alist β} : a ∈ s₁ → lookup a (s₁ ∪ s₂) = lookup a s₁ := lookup_kunion_left @[simp] theorem lookup_union_right {a} {s₁ s₂ : alist β} : a ∉ s₁ → lookup a (s₁ ∪ s₂) = lookup a s₂ := lookup_kunion_right @[simp] theorem mem_lookup_union {a} {b : β a} {s₁ s₂ : alist β} : b ∈ lookup a (s₁ ∪ s₂) ↔ b ∈ lookup a s₁ ∨ a ∉ s₁ ∧ b ∈ lookup a s₂ := mem_lookup_kunion theorem mem_lookup_union_middle {a} {b : β a} {s₁ s₂ s₃ : alist β} : b ∈ lookup a (s₁ ∪ s₃) → a ∉ s₂ → b ∈ lookup a (s₁ ∪ s₂ ∪ s₃) := mem_lookup_kunion_middle theorem insert_union {a} {b : β a} {s₁ s₂ : alist β} : insert a b (s₁ ∪ s₂) = insert a b s₁ ∪ s₂ := by ext; simp theorem union_assoc {s₁ s₂ s₃ : alist β} : ((s₁ ∪ s₂) ∪ s₃).entries ~ (s₁ ∪ (s₂ ∪ s₃)).entries := lookup_ext (alist.nodupkeys _) (alist.nodupkeys _) (by simp [decidable.not_or_iff_and_not,or_assoc,and_or_distrib_left,and_assoc]) end /-! ### disjoint -/ /-- Two associative lists are disjoint if they have no common keys. -/ def disjoint (s₁ s₂ : alist β) : Prop := ∀ k ∈ s₁.keys, ¬ k ∈ s₂.keys variables [decidable_eq α] theorem union_comm_of_disjoint {s₁ s₂ : alist β} (h : disjoint s₁ s₂) : (s₁ ∪ s₂).entries ~ (s₂ ∪ s₁).entries := lookup_ext (alist.nodupkeys _) (alist.nodupkeys _) (begin intros, simp, split; intro h', cases h', { right, refine ⟨_,h'⟩, apply h, rw [keys,← list.lookup_is_some,h'], exact rfl }, { left, rw h'.2 }, cases h', { right, refine ⟨_,h'⟩, intro h'', apply h _ h'', rw [keys,← list.lookup_is_some,h'], exact rfl }, { left, rw h'.2 }, end) end alist
b031d61762cece6b2d0c05fd48d0c82ef6bfc5ab
ee8cdbabf07f77e7be63a449b8483ce308d37218
/lean/src/valid/mathd-algebra-48.lean
61e4f5ffb07e799c54180fc0b73e66a9bc62814b
[ "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
307
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.complex.basic example (q e : ℂ) (h₀ : q = 9 - 4 * complex.I) (h₁ : e = -3 - 4 * complex.I) : q - e = 12 := begin rw [h₀, h₁], ring, end
bb1bb7a1c434c67c68eede13707d8119ca4a608e
b70031c8e2c5337b91d7e70f1e0c5f528f7b0e77
/src/group_theory/group_action/group.lean
bd17a40cd2f6fb7e57e322598ec32572061d72f0
[ "Apache-2.0" ]
permissive
molodiuc/mathlib
cae2ba3ef1601c1f42ca0b625c79b061b63fef5b
98ebe5a6739fbe254f9ee9d401882d4388f91035
refs/heads/master
1,674,237,127,059
1,606,353,533,000
1,606,353,533,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,875
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import group_theory.group_action.defs import algebra.group.units import algebra.group_with_zero import data.equiv.mul_add /-! # Group actions applied to various types of group This file contains lemmas about `smul` on `units`, `group_with_zero`, and `group`. -/ universes u v w variables {α : Type u} {β : Type v} {γ : Type w} section mul_action section units variables [monoid α] [mul_action α β] @[simp] lemma units.inv_smul_smul (u : units α) (x : β) : (↑u⁻¹:α) • (u:α) • x = x := by rw [smul_smul, u.inv_mul, one_smul] @[simp] lemma units.smul_inv_smul (u : units α) (x : β) : (u:α) • (↑u⁻¹:α) • x = x := by rw [smul_smul, u.mul_inv, one_smul] /-- If a monoid `α` acts on `β`, then each `u : units α` defines a permutation of `β`. -/ def units.smul_perm_hom : units α →* equiv.perm β := { to_fun := λ u, ⟨λ x, (u:α) • x, λ x, (↑u⁻¹:α) • x, u.inv_smul_smul, u.smul_inv_smul⟩, map_one' := equiv.ext $ one_smul α, map_mul' := λ u₁ u₂, equiv.ext $ mul_smul (u₁:α) u₂ } @[simp] lemma units.smul_left_cancel (u : units α) {x y : β} : (u:α) • x = (u:α) • y ↔ x = y := u.smul_perm_hom.apply_eq_iff_eq lemma units.smul_eq_iff_eq_inv_smul (u : units α) {x y : β} : (u:α) • x = y ↔ x = (↑u⁻¹:α) • y := u.smul_perm_hom.apply_eq_iff_eq_symm_apply lemma is_unit.smul_left_cancel {a : α} (ha : is_unit a) {x y : β} : a • x = a • y ↔ x = y := let ⟨u, hu⟩ := ha in hu ▸ u.smul_left_cancel end units section gwz variables [group_with_zero α] [mul_action α β] @[simp] lemma inv_smul_smul' {c : α} (hc : c ≠ 0) (x : β) : c⁻¹ • c • x = x := (units.mk0 c hc).inv_smul_smul x @[simp] lemma smul_inv_smul' {c : α} (hc : c ≠ 0) (x : β) : c • c⁻¹ • x = x := (units.mk0 c hc).smul_inv_smul x lemma inv_smul_eq_iff' {a : α} (ha : a ≠ 0) {x y : β} : a⁻¹ • x = y ↔ x = a • y := by { split; intro h, rw [← h, smul_inv_smul' ha], rw [h, inv_smul_smul' ha] } lemma eq_inv_smul_iff' {a : α} (ha : a ≠ 0) {x y : β} : x = a⁻¹ • y ↔ a • x = y := by { split; intro h, rw [h, smul_inv_smul' ha], rw [← h, inv_smul_smul' ha] } end gwz section group variables [group α] [mul_action α β] @[simp] lemma inv_smul_smul (c : α) (x : β) : c⁻¹ • c • x = x := (to_units c).inv_smul_smul x @[simp] lemma smul_inv_smul (c : α) (x : β) : c • c⁻¹ • x = x := (to_units c).smul_inv_smul x lemma inv_smul_eq_iff {a : α} {x y : β} : a⁻¹ • x = y ↔ x = a • y := begin split; rintro rfl, {rw smul_inv_smul}, {rw inv_smul_smul}, end lemma eq_inv_smul_iff {a : α} {x y : β} : x = a⁻¹ • y ↔ a • x = y := begin split; rintro rfl, {rw smul_inv_smul}, {rw inv_smul_smul}, end variables (α) (β) /-- Given an action of a group `α` on a set `β`, each `g : α` defines a permutation of `β`. -/ def mul_action.to_perm : α →* equiv.perm β := units.smul_perm_hom.comp to_units.to_monoid_hom variables {α} {β} protected lemma mul_action.bijective (g : α) : function.bijective (λ b : β, g • b) := (mul_action.to_perm α β g).bijective end group end mul_action section distrib_mul_action variables [monoid α] [add_monoid β] [distrib_mul_action α β] theorem units.smul_eq_zero (u : units α) {x : β} : (u : α) • x = 0 ↔ x = 0 := ⟨λ h, by rw [← u.inv_smul_smul x, h, smul_zero], λ h, h.symm ▸ smul_zero _⟩ theorem units.smul_ne_zero (u : units α) {x : β} : (u : α) • x ≠ 0 ↔ x ≠ 0 := not_congr u.smul_eq_zero @[simp] theorem is_unit.smul_eq_zero {u : α} (hu : is_unit u) {x : β} : u • x = 0 ↔ x = 0 := exists.elim hu $ λ u hu, hu ▸ u.smul_eq_zero end distrib_mul_action
4ac6d9a314904e04bb5698fbf3e06e41f084b0d1
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/order/pfilter.lean
62df6bcdc89a1d7110e59fc2203c8046e9d05dc1
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
4,159
lean
/- Copyright (c) 2020 Mathieu Guay-Paquet. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mathieu Guay-Paquet -/ import order.ideal /-! # Order filters ## Main definitions Throughout this file, `P` is at least a preorder, but some sections require more structure, such as a bottom element, a top element, or a join-semilattice structure. - `order.pfilter P`: The type of nonempty, downward directed, upward closed subsets of `P`. This is dual to `order.ideal`, so it simply wraps `order.ideal (order_dual P)`. - `order.is_pfilter P`: a predicate for when a `set P` is a filter. Note the relation between `order/filter` and `order/pfilter`: for any type `α`, `filter α` represents the same mathematical object as `pfilter (set α)`. ## References - <https://en.wikipedia.org/wiki/Filter_(mathematics)> ## Tags pfilter, filter, ideal, dual -/ namespace order variables {P : Type*} /-- A filter on a preorder `P` is a subset of `P` that is - nonempty - downward directed - upward closed. -/ structure pfilter (P) [preorder P] := (dual : ideal (order_dual P)) /-- A predicate for when a subset of `P` is a filter. -/ def is_pfilter [preorder P] (F : set P) : Prop := @is_ideal (order_dual P) _ F lemma is_pfilter.of_def [preorder P] {F : set P} (nonempty : F.nonempty) (directed : directed_on (≥) F) (mem_of_le : ∀ {x y : P}, x ≤ y → x ∈ F → y ∈ F) : is_pfilter F := by { use [nonempty, directed], exact λ _ _ _ _, mem_of_le ‹_› ‹_› } /-- Create an element of type `order.pfilter` from a set satisfying the predicate `order.is_pfilter`. -/ def is_pfilter.to_pfilter [preorder P] {F : set P} (h : is_pfilter F) : pfilter P := ⟨h.to_ideal⟩ namespace pfilter section preorder variables [preorder P] {x y : P} (F : pfilter P) /-- A filter on `P` is a subset of `P`. -/ instance : has_coe (pfilter P) (set P) := ⟨λ F, F.dual.carrier⟩ /-- For the notation `x ∈ F`. -/ instance : has_mem P (pfilter P) := ⟨λ x F, x ∈ (F : set P)⟩ @[simp] lemma mem_coe : x ∈ (F : set P) ↔ x ∈ F := iff_of_eq rfl lemma is_pfilter : is_pfilter (F : set P) := F.dual.is_ideal lemma nonempty : (F : set P).nonempty := F.dual.nonempty lemma directed : directed_on (≥) (F : set P) := F.dual.directed lemma mem_of_le {F : pfilter P} : x ≤ y → x ∈ F → y ∈ F := λ h, F.dual.mem_of_le h /-- The smallest filter containing a given element. -/ def principal (p : P) : pfilter P := ⟨ideal.principal p⟩ instance [inhabited P] : inhabited (pfilter P) := ⟨⟨default _⟩⟩ /-- Two filters are equal when their underlying sets are equal. -/ @[ext] lemma ext : ∀ (F G : pfilter P), (F : set P) = G → F = G | ⟨⟨_, _, _, _⟩⟩ ⟨⟨_, _, _, _⟩⟩ rfl := rfl /-- The partial ordering by subset inclusion, inherited from `set P`. -/ instance : partial_order (pfilter P) := partial_order.lift coe ext @[trans] lemma mem_of_mem_of_le {F G : pfilter P} : x ∈ F → F ≤ G → x ∈ G := ideal.mem_of_mem_of_le @[simp] lemma principal_le_iff {F : pfilter P} : principal x ≤ F ↔ x ∈ F := ideal.principal_le_iff end preorder section order_top variables [order_top P] {F : pfilter P} /-- A specific witness of `pfilter.nonempty` when `P` has a top element. -/ @[simp] lemma top_mem : ⊤ ∈ F := ideal.bot_mem /-- There is a bottom filter when `P` has a top element. -/ instance : order_bot (pfilter P) := { bot := ⟨⊥⟩, bot_le := λ F, (bot_le : ⊥ ≤ F.dual), .. pfilter.partial_order } end order_top /-- There is a top filter when `P` has a bottom element. -/ instance {P} [order_bot P] : order_top (pfilter P) := { top := ⟨⊤⟩, le_top := λ F, (le_top : F.dual ≤ ⊤), .. pfilter.partial_order } section semilattice_inf variables [semilattice_inf P] {x y : P} {F : pfilter P} /-- A specific witness of `pfilter.directed` when `P` has meets. -/ lemma inf_mem (x y ∈ F) : x ⊓ y ∈ F := ideal.sup_mem x y ‹x ∈ F› ‹y ∈ F› @[simp] lemma inf_mem_iff : x ⊓ y ∈ F ↔ x ∈ F ∧ y ∈ F := ideal.sup_mem_iff end semilattice_inf end pfilter end order
cfdc9fefe3e442488102c15e596273202cc28ee0
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/algebraic_geometry/stalks.lean
a4e003619fd0a7cfe702c14c38440ea1df2f4de2
[ "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
7,222
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 algebraic_geometry.presheafed_space import category_theory.limits.final import topology.sheaves.stalks /-! # Stalks for presheaved spaces This file lifts constructions of stalks and pushforwards of stalks to work with the category of presheafed spaces. Additionally, we prove that restriction of presheafed spaces does not change the stalks. -/ noncomputable theory universes v u v' u' open category_theory open category_theory.limits category_theory.category category_theory.functor open algebraic_geometry open topological_space open opposite variables {C : Type u} [category.{v} C] [has_colimits C] local attribute [tidy] tactic.op_induction' open Top.presheaf namespace algebraic_geometry.PresheafedSpace /-- The stalk at `x` of a `PresheafedSpace`. -/ def stalk (X : PresheafedSpace C) (x : X) : C := X.presheaf.stalk x /-- A morphism of presheafed spaces induces a morphism of stalks. -/ def stalk_map {X Y : PresheafedSpace C} (α : X ⟶ Y) (x : X) : Y.stalk (α.base x) ⟶ X.stalk x := (stalk_functor C (α.base x)).map (α.c) ≫ X.presheaf.stalk_pushforward C α.base x @[simp, elementwise, reassoc] lemma stalk_map_germ {X Y : PresheafedSpace C} (α : X ⟶ Y) (U : opens Y.carrier) (x : (opens.map α.base).obj U) : Y.presheaf.germ ⟨α.base x, x.2⟩ ≫ stalk_map α ↑x = α.c.app (op U) ≫ X.presheaf.germ x := by rw [stalk_map, stalk_functor_map_germ_assoc, stalk_pushforward_germ] section restrict /-- For an open embedding `f : U ⟶ X` and a point `x : U`, we get an isomorphism between the stalk of `X` at `f x` and the stalk of the restriction of `X` along `f` at t `x`. -/ def restrict_stalk_iso {U : Top} (X : PresheafedSpace C) {f : U ⟶ (X : Top.{v})} (h : open_embedding f) (x : U) : (X.restrict h).stalk x ≅ X.stalk (f x) := begin -- As a left adjoint, the functor `h.is_open_map.functor_nhds x` is initial. haveI := initial_of_adjunction (h.is_open_map.adjunction_nhds x), -- Typeclass resolution knows that the opposite of an initial functor is final. The result -- follows from the general fact that postcomposing with a final functor doesn't change colimits. exact final.colimit_iso (h.is_open_map.functor_nhds x).op ((open_nhds.inclusion (f x)).op ⋙ X.presheaf), end @[simp, elementwise, reassoc] lemma restrict_stalk_iso_hom_eq_germ {U : Top} (X : PresheafedSpace C) {f : U ⟶ (X : Top.{v})} (h : open_embedding f) (V : opens U) (x : U) (hx : x ∈ V) : (X.restrict h).presheaf.germ ⟨x, hx⟩ ≫ (restrict_stalk_iso X h x).hom = X.presheaf.germ ⟨f x, show f x ∈ h.is_open_map.functor.obj V, from ⟨x, hx, rfl⟩⟩ := colimit.ι_pre ((open_nhds.inclusion (f x)).op ⋙ X.presheaf) (h.is_open_map.functor_nhds x).op (op ⟨V, hx⟩) @[simp, elementwise, reassoc] lemma restrict_stalk_iso_inv_eq_germ {U : Top} (X : PresheafedSpace C) {f : U ⟶ (X : Top.{v})} (h : open_embedding f) (V : opens U) (x : U) (hx : x ∈ V) : X.presheaf.germ ⟨f x, show f x ∈ h.is_open_map.functor.obj V, from ⟨x, hx, rfl⟩⟩ ≫ (restrict_stalk_iso X h x).inv = (X.restrict h).presheaf.germ ⟨x, hx⟩ := by rw [← restrict_stalk_iso_hom_eq_germ, category.assoc, iso.hom_inv_id, category.comp_id] end restrict namespace stalk_map @[simp] lemma id (X : PresheafedSpace C) (x : X) : stalk_map (𝟙 X) x = 𝟙 (X.stalk x) := begin dsimp [stalk_map], simp only [stalk_pushforward.id], rw [←map_comp], convert (stalk_functor C x).map_id X.presheaf, tidy, end -- TODO understand why this proof is still gross (i.e. requires using `erw`) @[simp] lemma comp {X Y Z : PresheafedSpace C} (α : X ⟶ Y) (β : Y ⟶ Z) (x : X) : stalk_map (α ≫ β) x = (stalk_map β (α.base x) : Z.stalk (β.base (α.base x)) ⟶ Y.stalk (α.base x)) ≫ (stalk_map α x : Y.stalk (α.base x) ⟶ X.stalk x) := begin dsimp [stalk_map, stalk_functor, stalk_pushforward], ext U, induction U using opposite.rec, cases U, simp only [colimit.ι_map_assoc, colimit.ι_pre_assoc, colimit.ι_pre, whisker_left_app, whisker_right_app, assoc, id_comp, map_id, map_comp], dsimp, simp only [map_id, assoc, pushforward.comp_inv_app], -- FIXME Why doesn't simp do this: erw [category_theory.functor.map_id], erw [category_theory.functor.map_id], erw [id_comp, id_comp], end /-- If `α = β` and `x = x'`, we would like to say that `stalk_map α x = stalk_map β x'`. Unfortunately, this equality is not well-formed, as their types are not _definitionally_ the same. To get a proper congruence lemma, we therefore have to introduce these `eq_to_hom` arrows on either side of the equality. -/ lemma congr {X Y : PresheafedSpace C} (α β : X ⟶ Y) (h₁ : α = β) (x x': X) (h₂ : x = x') : stalk_map α x ≫ eq_to_hom (show X.stalk x = X.stalk x', by rw h₂) = eq_to_hom (show Y.stalk (α.base x) = Y.stalk (β.base x'), by rw [h₁, h₂]) ≫ stalk_map β x' := stalk_hom_ext _ $ λ U hx, by { subst h₁, subst h₂, simp } lemma congr_hom {X Y : PresheafedSpace C} (α β : X ⟶ Y) (h : α = β) (x : X) : stalk_map α x = eq_to_hom (show Y.stalk (α.base x) = Y.stalk (β.base x), by rw h) ≫ stalk_map β x := by rw [← stalk_map.congr α β h x x rfl, eq_to_hom_refl, category.comp_id] lemma congr_point {X Y : PresheafedSpace C} (α : X ⟶ Y) (x x' : X) (h : x = x') : stalk_map α x ≫ eq_to_hom (show X.stalk x = X.stalk x', by rw h) = eq_to_hom (show Y.stalk (α.base x) = Y.stalk (α.base x'), by rw h) ≫ stalk_map α x' := by rw stalk_map.congr α α rfl x x' h instance is_iso {X Y : PresheafedSpace C} (α : X ⟶ Y) [is_iso α] (x : X) : is_iso (stalk_map α x) := { out := begin let β : Y ⟶ X := category_theory.inv α, have h_eq : (α ≫ β).base x = x, { rw [is_iso.hom_inv_id α, id_base, Top.id_app] }, -- Intuitively, the inverse of the stalk map of `α` at `x` should just be the stalk map of `β` -- at `α x`. Unfortunately, we have a problem with dependent type theory here: Because `x` -- is not *definitionally* equal to `β (α x)`, the map `stalk_map β (α x)` has not the correct -- type for an inverse. -- To get a proper inverse, we need to compose with the `eq_to_hom` arrow -- `X.stalk x ⟶ X.stalk ((α ≫ β).base x)`. refine ⟨eq_to_hom (show X.stalk x = X.stalk ((α ≫ β).base x), by rw h_eq) ≫ (stalk_map β (α.base x) : _), _, _⟩, { rw [← category.assoc, congr_point α x ((α ≫ β).base x) h_eq.symm, category.assoc], erw ← stalk_map.comp β α (α.base x), rw [congr_hom _ _ (is_iso.inv_hom_id α), stalk_map.id, eq_to_hom_trans_assoc, eq_to_hom_refl, category.id_comp] }, { rw [category.assoc, ← stalk_map.comp, congr_hom _ _ (is_iso.hom_inv_id α), stalk_map.id, eq_to_hom_trans_assoc, eq_to_hom_refl, category.id_comp] }, end } /-- An isomorphism between presheafed spaces induces an isomorphism of stalks. -/ def stalk_iso {X Y : PresheafedSpace C} (α : X ≅ Y) (x : X) : Y.stalk (α.hom.base x) ≅ X.stalk x := as_iso (stalk_map α.hom x) end stalk_map end algebraic_geometry.PresheafedSpace
88cdbd672076a02dfdde6e820b9352cb9a45b895
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/endofunctor/algebra.lean
a43e576f522758fad281ff634b882253fdb2a2c8
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
6,470
lean
/- Copyright (c) 2022 Joseph Hua. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Bhavik Mehta, Johan Commelin, Reid Barton, Rob Lewis, Joseph Hua -/ import category_theory.limits.final import category_theory.functor.reflects_isomorphisms /-! # Algebras of endofunctors This file defines algebras of an endofunctor, and provides the category instance for them. This extends to Eilenberg-Moore (co)algebras for a (co)monad. It also defines the forgetful functor from the category of algebras. It is shown that the structure map of the initial algebra of an endofunctor is an isomorphism. -/ universes v u namespace category_theory namespace endofunctor variables {C : Type u} [category.{v} C] /-- An algebra of an endofunctor; `str` stands for "structure morphism" -/ structure algebra (F : C ⥤ C) := (A : C) (str : F.obj A ⟶ A) instance [inhabited C] : inhabited (algebra (𝟭 C)) := ⟨⟨ default , 𝟙 _ ⟩⟩ namespace algebra variables {F : C ⥤ C} (A : algebra F) {A₀ A₁ A₂ : algebra F} /- ``` str F A₀ -----> A₀ | | F f | | f V V F A₁ -----> A₁ str ``` -/ /-- A morphism between algebras of endofunctor `F` -/ @[ext] structure hom (A₀ A₁ : algebra F) := (f : A₀.1 ⟶ A₁.1) (h' : F.map f ≫ A₁.str = A₀.str ≫ f . obviously) restate_axiom hom.h' attribute [simp, reassoc] hom.h namespace hom /-- The identity morphism of an algebra of endofunctor `F` -/ def id : hom A A := { f := 𝟙 _ } instance : inhabited (hom A A) := ⟨{ f := 𝟙 _ }⟩ /-- The composition of morphisms between algebras of endofunctor `F` -/ def comp (f : hom A₀ A₁) (g : hom A₁ A₂) : hom A₀ A₂ := { f := f.1 ≫ g.1 } end hom instance (F : C ⥤ C) : category_struct (algebra F) := { hom := hom, id := hom.id, comp := @hom.comp _ _ _ } @[simp] lemma id_eq_id : algebra.hom.id A = 𝟙 A := rfl @[simp] lemma id_f : (𝟙 _ : A ⟶ A).1 = 𝟙 A.1 := rfl variables {A₀ A₁ A₂} (f : A₀ ⟶ A₁) (g : A₁ ⟶ A₂) @[simp] lemma comp_eq_comp : algebra.hom.comp f g = f ≫ g := rfl @[simp] lemma comp_f : (f ≫ g).1 = f.1 ≫ g.1 := rfl /-- Algebras of an endofunctor `F` form a category -/ instance (F : C ⥤ C) : category (algebra F) := {} /-- To construct an isomorphism of algebras, it suffices to give an isomorphism of the As which commutes with the structure morphisms. -/ @[simps] def iso_mk (h : A₀.1 ≅ A₁.1) (w : F.map h.hom ≫ A₁.str = A₀.str ≫ h.hom) : A₀ ≅ A₁ := { hom := { f := h.hom }, inv := { f := h.inv, h' := by { rw [h.eq_comp_inv, category.assoc, ←w, ←functor.map_comp_assoc], simp } } } /-- The forgetful functor from the category of algebras, forgetting the algebraic structure. -/ @[simps] def forget (F : C ⥤ C) : algebra F ⥤ C := { obj := λ A, A.1, map := λ A B f, f.1 } /-- An algebra morphism with an underlying isomorphism hom in `C` is an algebra isomorphism. -/ lemma iso_of_iso (f : A₀ ⟶ A₁) [is_iso f.1] : is_iso f := ⟨⟨{ f := inv f.1, h' := by { rw [is_iso.eq_comp_inv f.1, category.assoc, ← f.h], simp } }, by tidy⟩⟩ instance forget_reflects_iso : reflects_isomorphisms (forget F) := { reflects := λ A B, iso_of_iso } instance forget_faithful : faithful (forget F) := {} /-- From a natural transformation `α : G → F` we get a functor from algebras of `F` to algebras of `G`. -/ @[simps] def functor_of_nat_trans {F G : C ⥤ C} (α : G ⟶ F) : algebra F ⥤ algebra G := { obj := λ A, { A := A.1, str := α.app A.1 ≫ A.str }, map := λ A₀ A₁ f, { f := f.1 } } /-- The identity transformation induces the identity endofunctor on the category of algebras. -/ @[simps {rhs_md := semireducible}] def functor_of_nat_trans_id : functor_of_nat_trans (𝟙 F) ≅ 𝟭 _ := nat_iso.of_components (λ X, iso_mk (iso.refl _) (by { dsimp, simp, })) (λ X Y f, by { ext, dsimp, simp }) /-- A composition of natural transformations gives the composition of corresponding functors. -/ @[simps {rhs_md := semireducible}] def functor_of_nat_trans_comp {F₀ F₁ F₂ : C ⥤ C} (α : F₀ ⟶ F₁) (β : F₁ ⟶ F₂) : functor_of_nat_trans (α ≫ β) ≅ functor_of_nat_trans β ⋙ functor_of_nat_trans α := nat_iso.of_components (λ X, iso_mk (iso.refl _) (by { dsimp, simp })) (λ X Y f, by { ext, dsimp, simp }) /-- If `α` and `β` are two equal natural transformations, then the functors of algebras induced by them are isomorphic. We define it like this as opposed to using `eq_to_iso` so that the components are nicer to prove lemmas about. -/ @[simps {rhs_md := semireducible}] def functor_of_nat_trans_eq {F G : C ⥤ C} {α β : F ⟶ G} (h : α = β) : functor_of_nat_trans α ≅ functor_of_nat_trans β := nat_iso.of_components (λ X, iso_mk (iso.refl _) (by { dsimp, simp [h] })) (λ X Y f, by { ext, dsimp, simp }) /-- Naturally isomorphic endofunctors give equivalent categories of algebras. Furthermore, they are equivalent as categories over `C`, that is, we have `equiv_of_nat_iso h ⋙ forget = forget`. -/ @[simps] def equiv_of_nat_iso {F G : C ⥤ C} (α : F ≅ G) : algebra F ≌ algebra G := { functor := functor_of_nat_trans α.inv, inverse := functor_of_nat_trans α.hom, unit_iso := functor_of_nat_trans_id.symm ≪≫ functor_of_nat_trans_eq (by simp) ≪≫ functor_of_nat_trans_comp _ _, counit_iso := (functor_of_nat_trans_comp _ _).symm ≪≫ functor_of_nat_trans_eq (by simp) ≪≫ functor_of_nat_trans_id }. namespace initial variables {A} (h : limits.is_initial A) /-- The inverse of the structure map of an initial algebra -/ @[simp] def str_inv : A.1 ⟶ F.obj A.1 := (h.to ⟨ F.obj A.1 , F.map A.str ⟩).1 lemma left_inv' : (⟨str_inv h ≫ A.str⟩ : A ⟶ A) = 𝟙 A := limits.is_initial.hom_ext h _ (𝟙 A) lemma left_inv : str_inv h ≫ A.str = 𝟙 _ := congr_arg hom.f (left_inv' h) lemma right_inv : A.str ≫ str_inv h = 𝟙 _ := by { rw [str_inv, ← (h.to ⟨ F.obj A.1 , F.map A.str ⟩).h, ← F.map_id, ← F.map_comp], congr, exact (left_inv h) } /-- The structure map of the inital algebra is an isomorphism, hence endofunctors preserve their initial algebras -/ lemma str_is_iso (h : limits.is_initial A) : is_iso A.str := { out := ⟨ str_inv h, right_inv _ , left_inv _ ⟩ } end initial end algebra end endofunctor end category_theory
bdd2e40c45e8c967bb4d76c4a898f2f78c936c15
6c56e3fbe5d7711b66a3b46f1810b44cfa109768
/src/week1.lean
0277077b4010d6ac2a5ba374887530e63d0c396f
[]
no_license
kodyvajjha/lean-mmc2021
015e7ba9d2f43397582d42ad282e88c8ce876dc3
cc774208f9dc041cad4def289357301b4a0a8173
refs/heads/master
1,689,416,888,246
1,630,161,054,000
1,630,161,054,000
384,140,257
1
0
null
null
null
null
UTF-8
Lean
false
false
4,270
lean
/- This exercise will guide you through a proof that there are infinitely many primes. NOTE: You will need to install mathlib for this. A good way to confirm if mathlib is installed is to see if the following imports work. This tells Lean to import facts about prime numbers. -/ import tactic import data.nat.prime open nat /- We shall break up the proof into a bunch of lemmas and use these lemmas in the final proof. The first lemma `not_dvd_succ` says that if a ≠ 1 divides b, then it does not divide b+1. Lemmas/Hints which may be useful for this exercise: * nat.dvd_add_right * nat.dvd_one * Try to use the rewrite tactic. (Of course, this is not the only way to prove this exercise. Look at the file data.nat.prime to see if you can find other exercises which may help you. You can browse through https://leanprover-community.github.io/mathlib_docs/data/nat/prime.html for more facts.) -/ lemma not_dvd_succ {a b : ℕ} (ha : a ≠ 1) (hab : a ∣ b) : ¬ (a ∣ b + 1) := begin sorry, end /- Before we move on to the main theorem, it is good to understand how the `linarith` tactic works. `linarith` is a high-powered tactic which can be used to prove linear (in)equalities. Read : https://leanprover-community.github.io/mathlib_docs/tactics.html#linarith -/ example (x : ℕ) (hx₁ : x > 15) (hx₂ : x ≤ 20) : (x < 25) := begin linarith, end example (x y z : ℕ) (h₁ : x > y + 1) (h₂ : y > 2*z + 2): (x > z) := begin linarith, end /- Shorter proofs can simply use the `by` keyword. -/ example (x y z : ℚ) (h1 : 2*x < 3*y) (h2 : -4*x + 2*z < 0) (h3 : 12*y - 4* z < 0) : false := by linarith example(a b d : ℕ) (h: 2*a ≤ 3*b)(h': 1≤ a)(h'': d = 2): d + a ≤ 5*b := by linarith /- Let us try to recall how the proof works: The factorial n! of a positive integer n is divisible by very integer from 2 to n, as it is the product of all of them. Hence, n! + 1 is not divisible by any of the integers from 2 to n, inclusive (it gives a remainder of 1 when divided by each). Hence n! + 1 is either prime or divisible by a prime larger than n. In either case, for every positive integer n, there is at least one prime bigger than n. The conclusion is that the number of primes is infinite. We break up the above sketch into little steps in the proof below. -/ theorem inf_many_primes : ∀ n : ℕ, ∃ p, p ≥ n ∧ prime p := begin -- assume an arbitrary n assume n, -- define N = n! + 1 let N := n.factorial + 1, /- Prove first that N ≥ 2. The tactic `linarith` and lemmas `factorial_le` and `zero_le` may be helpful.-/ have Nge2 : N ≥ 2, { sorry, }, /- Next we prove that the numbers from 2 to n divide n!. The tactic `linarith` and lemmas `dvd_factorial` may be helpful. -/ have dvd₁ : ∀ k n : ℕ, 2 ≤ k ∧ k ≤ n → k ∣ n.factorial, { sorry, }, /- Next we prove that the numbers from 2 to n do NOT divide n! + 1. Use the previous lemmas we proved: `dvd₁` and `not_dvd_succ`. -/ have dvd₂ : ∀ k n : ℕ, 2 ≤ k ∧ k ≤ n → ¬ (k ∣ n.factorial + 1), { sorry, }, -- clear some hypothesis to avoid clutter clear dvd₁, /- The number N := n! + 1 is either prime or not. Note for nerds: This tactic uses *classical* logic. It is good to keep this in mind even if you don't care about it. -/ by_cases (prime N), { /- in the case where N is prime, we can prove the main goal easily. Try using `self_le_factorial` and `linarith`. -/ use N, sorry, }, /- In the case where N is not prime, we first add the fact that there is a prime which divides it. This is the fact `exists_prime_and_dvd`. -/ have HN := exists_prime_and_dvd Nge2, /- Here is another tactic which makes breaking up hypotheses easier. -/ rcases HN with ⟨p,⟨Hp₁,Hp₂⟩⟩, -- Use p now to finish the proof. use p, sorry, end /- Final words of advice: * Keep the tactic cheatsheet open next to you if you are lost: https://leanprover-community.github.io//img/lean-tactics.pdf * Search for lemmas in mathlib. * Always try to get to the goal on paper first. Once that is clear, then move onto formalizing. -/
9fc645b2c589b96c1a658f1d58e281ed98bf4858
e21db629d2e37a833531fdcb0b37ce4d71825408
/src/use_cases/arrcp.lean
48452762cef91670a3a83efce413550b0bf1697a
[]
no_license
fischerman/GPU-transformation-verifier
614a28cb4606a05a0eb27e8d4eab999f4f5ea60c
75a5016f05382738ff93ce5859c4cfa47ccb63c1
refs/heads/master
1,586,985,789,300
1,579,290,514,000
1,579,290,514,000
165,031,073
1
0
null
null
null
null
UTF-8
Lean
false
false
4,400
lean
/- import rel_hoare namespace arrcp open parlang open parlang.kernel def tstate := string → ℕ def state.update (name : string) (val : ℕ) (s : tstate) : tstate := λn, if n = name then val else s n notation s ` & ` n ` ::= ` v := state.update n v s def init := λ n : ℕ, λ name, if name = "tid" then n else 0 def arrcp₁ : program tstate (λ n : ℕ, ℕ) := program.intro (λ m : memory (λ n : ℕ, ℕ), m 0) ( kernel.load (λ s, ⟨0, λ a, s & "n" ::= a⟩) ;; -- load n to thread tlocal kernel.load (λ s, ⟨1 + s "tid", λ a, s & "temp" ::= a⟩) ;; -- load from array a kernel.store (λ s, ⟨1 + s "n" + s "tid", s "temp"⟩) -- store to array b ) def arrcp₂ : program tstate (λ n : ℕ, ℕ) := program.intro (λ m : memory (λ n : ℕ, ℕ), 10) ( kernel.load (λ s, ⟨0, λ a, s & "n" ::= a⟩) ;; -- load n to thread tlocal kernel.compute (λ s, s & "i" ::= s "tid") ;; kernel.loop (λ s, s "i" < s "n") ( kernel.load (λ s, ⟨1 + s "i", λ a, s & "temp" ::= a⟩) ;;-- load from array a kernel.store (λ s, ⟨1 + s "n" + s "i", s "temp"⟩) ;; -- store to array b kernel.compute (λ s, s & "i" ::= s "i" + 10) ) ) lemma arrcprel : rel_hoare_program init init eq arrcp₁ arrcp₂ eq := begin unfold arrcp₁ arrcp₂, apply rel_kernel_to_program, apply single_step_left ((λ (n₁ : ℕ) (s₁ : state n₁ (string → ℕ) (λ (n : ℕ), ℕ)) (ac₁ : vector bool n₁) (n₂ : ℕ) (s₂ : state n₂ (string → ℕ) (λ (n : ℕ), ℕ)) (ac₂ : vector bool n₂), ∃ (m₁ m₂ : memory (λ (n : ℕ), ℕ)), state.syncable s₁ m₁ ∧ state.syncable s₂ m₂ ∧ n₁ = m₁ 0 ∧ n₂ = 10 ∧ (∀ (i : fin n₁), vector.nth (s₁.threads) i = {tlocal := (init ↑i) & "n" ::= m₁ 0, shared := m₁, loads := insert 0 ((vector.nth (s₁.threads) i).loads), stores := ∅}) ∧ (∀ (i : fin n₂), vector.nth (s₂.threads) i = {tlocal := init ↑i, shared := m₂, loads := ∅, stores := ∅}) ∧ m₁ = m₂ ∧ ↥(all_threads_active ac₁) ∧ ↥(all_threads_active ac₂))), { intros n₁ n₂ s₁ s₁' s₂ ac₁ ac₂ hp hek₁, cases hp with m₁ hp, cases hp with m₂ hp, apply exists.intro s₂, apply and.intro, { suffices h : exec_state (compute id) ac₂ s₂ (state.map_active_threads ac₂ (thread_state.map id) s₂), { rw ← state.map_active_threads_id s₂ ac₂ at h, assumption, }, apply exec_state.compute, }, { simp, apply exists.intro m₁, cases hek₁, apply and.intro, { sorry -- memory does not change }, { apply exists.intro m₂, apply and.intro, { apply hp.right.left, }, { apply and.intro hp.right.right.left (and.intro hp.right.right.right.left (and.intro _ hp.right.right.right.right.right)), intro i, -- rw ← hp.right.right.right.right.left, simp, have haa: ↥(vector.nth ac₁ i) := begin apply all_threads_active_nth, exact hp.right.right.right.right.right.right.right.left, end, simp [haa], rw thread_state.load, rw thread_state.load._match_1, have : m₁ = (vector.nth (s₁.threads) i).shared := begin rw hp.right.right.right.right.left, end, subst this, simp, have : (vector.nth (s₁.threads) i).tlocal = (init i) := begin rw hp.right.right.right.right.left, end, rw this, rw memory.get, simp, rw hp.right.right.right.right.left, } } } }, { } end end arrcp -/
3114f4547c98b77e6317ecb1c5cc6964775c2fba
60bf3fa4185ec5075eaea4384181bfbc7e1dc319
/src/game/order/max_API_test.lean
747275c4aee266ca62539457d522fc510ea3e38e
[ "Apache-2.0" ]
permissive
anrddh/real-number-game
660f1127d03a78fd35986c771d65c3132c5f4025
c708c4e02ec306c657e1ea67862177490db041b0
refs/heads/master
1,668,214,277,092
1,593,105,075,000
1,593,105,075,000
264,269,218
0
0
null
1,589,567,264,000
1,589,567,264,000
null
UTF-8
Lean
false
false
4,822
lean
import tactic open_locale classical -- def of max a b is "if a <= b then b else a" noncomputable theory -- same reason -- A linear order is a reflexive, transitive, antisymmetric and total relation `≤` variables {X : Type} [linear_order X] {a b c : X} -- Note in the below that sometimes we have to supply the variable names and other times not. -- I am scared after my NNG experience to break with tradition and start -- changing mathlib conventions. They are there for a reason (you don't need -- them when there are implications involved, because you can guess the -- variables from the hypotheses) -- I think that the user would be happy to accept all of these as axioms, -- even though strictly speaking one could define a < b to be a ≤ b ∧ a ≠ b -- and then prove the last two. example : a ≤ a := le_refl a example : a ≤ b → b ≤ c → a ≤ c := le_trans example : a ≤ b → b ≤ a → a = b := le_antisymm -- I think this is more useful in practice than trichotomy example : a ≤ b ∨ b ≤ a := le_total a b example : a < b ↔ a ≤ b ∧ a ≠ b := lt_iff_le_and_ne example : a ≤ b → b < c → a < c := lt_of_le_of_lt example : a < b → b ≤ c → a < c := lt_of_lt_of_le -- max is already defined so we work in a namespace namespace test -- I think this definition might be hard to work with def max (a b : X) := if a ≤ b then b else a -- need if_pos to do this one theorem max_eq_right (hab : a ≤ b) : max a b = b := begin unfold max, rw if_pos hab, end -- need if_neg to do this one theorem max_eq_left (hba : b ≤ a) : max a b = a := begin by_cases hab : a ≤ b, { rw max_eq_right hab, exact le_antisymm hba hab, }, { unfold max, rw if_neg hab, } end -- but now things work really nicely. Proposal: tell them that -- max_eq_left and max_eq_right are axioms of max, and don't -- tell them the definition. Note that max_eq_left and max_eq_right -- together are enough to deduce that max a b is what it is, -- because of le_antisymm theorem max_choice (a b : X) : max a b = a ∨ max a b = b := begin cases le_total a b with hab hba, { right, exact max_eq_right hab }, { left, exact max_eq_left hba } end theorem max_comm (a b : X) : max a b = max b a := begin cases le_total a b with hab hba, { rw max_eq_right hab, rw max_eq_left hab, }, { rw max_eq_left hba, rw max_eq_right hba } end theorem le_max_left (a b : X) : a ≤ max a b := begin cases le_total a b with hab hba, { rw max_eq_right hab, assumption }, { rw max_eq_left hba, -- Lean closes a ≤ a automatically because ≤ is reflexive } end theorem le_max_right (a b : X) : b ≤ max a b := begin rw max_comm, apply le_max_left end -- this comes out nicely theorem max_le (hac : a ≤ c) (hbc : b ≤ c) : max a b ≤ c := begin cases max_choice a b with ha hb, { rw ha, assumption }, { rw hb, assumption } end -- so does this theorem max_lt (hac : a < c) (hbc : b < c) : max a b < c := begin cases max_choice a b with ha hb, { rw ha, assumption }, { rw hb, assumption } end -- and this, if we can teach `apply le_trans _ habc` theorem max_le_iff : a ≤ c ∧ b ≤ c ↔ max a b ≤ c := begin split, { intro h, cases h with hac hbc, exact max_le hac hbc }, { intro habc, split, { apply le_trans _ habc, apply le_max_left}, { apply le_trans _ habc, apply le_max_right } }, end theorem max_lt_iff : a < c ∧ b < c ↔ max a b < c := begin split, { intro h, cases h with hac hbc, exact max_lt hac hbc }, { intro habc, split, { apply lt_of_le_of_lt _ habc, apply le_max_left}, { apply lt_of_le_of_lt _ habc, apply le_max_right } }, end -- long but fun theorem le_max_iff : a ≤ max b c ↔ a ≤ b ∨ a ≤ c := begin split, { intro ha, cases le_total b c with hbc hcb, { rw max_eq_right hbc at ha, right, assumption, }, { rw max_eq_left hcb at ha, left, assumption } }, { intro habc, cases habc with hab hac, { apply le_trans hab, apply le_max_left}, { apply le_trans hac, apply le_max_right}, } end -- same as previous one theorem lt_max_iff : a < max b c ↔ a < b ∨ a < c := begin split, { intro ha, cases le_total b c with hbc hcb, { rw max_eq_right hbc at ha, right, assumption, }, { rw max_eq_left hcb at ha, left, assumption } }, { intro habc, cases habc with hab hac, { apply lt_of_lt_of_le hab, apply le_max_left}, { apply lt_of_lt_of_le hac, apply le_max_right}, } end -- I think that's a good API for max. Let's test this hypothesis -- by seeing how easy it is to make a good API for abs. end test
ff5425bce0b4e4764817ddaabcb0d1bb6b08d1d8
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/algebra/group/compact.lean
0c223c744d154af653d14ae35ee359a6388d772a
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
2,413
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 topology.algebra.group.basic import topology.compact_open import topology.sets.compacts /-! # Additional results on topological groups Two results on topological groups that have been separated out as they require more substantial imports developing either positive compacts or the compact open topology. -/ open classical set filter topological_space function open_locale classical topological_space filter pointwise universes u v w x variables {α : Type u} {β : Type v} {G : Type w} {H : Type x} 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] /-- Every separated topological group in which there exists a compact set with nonempty interior is locally compact. -/ @[to_additive "Every separated topological group in which there exists a compact set with nonempty interior is locally compact."] lemma topological_space.positive_compacts.locally_compact_space_of_group [t2_space G] (K : positive_compacts G) : locally_compact_space G := begin refine locally_compact_of_compact_nhds (λ x, _), obtain ⟨y, hy⟩ := K.interior_nonempty, let F := homeomorph.mul_left (x * y⁻¹), refine ⟨F '' K, _, K.is_compact.image F.continuous⟩, suffices : F.symm ⁻¹' K ∈ 𝓝 x, by { convert this, apply equiv.image_eq_preimage }, apply continuous_at.preimage_mem_nhds F.symm.continuous.continuous_at, have : F.symm x = y, by simp [F, homeomorph.mul_left_symm], rw this, exact mem_interior_iff_mem_nhds.1 hy end end section quotient variables [group G] [topological_space G] [topological_group G] {Γ : subgroup G} @[to_additive] instance quotient_group.has_continuous_smul [locally_compact_space G] : has_continuous_smul G (G ⧸ Γ) := { continuous_smul := begin let F : G × G ⧸ Γ → G ⧸ Γ := λ p, p.1 • p.2, change continuous F, have H : continuous (F ∘ (λ p : G × G, (p.1, quotient_group.mk p.2))), { change continuous (λ p : G × G, quotient_group.mk (p.1 * p.2)), refine continuous_coinduced_rng.comp continuous_mul }, exact quotient_map.continuous_lift_prod_right quotient_map_quotient_mk H, end } end quotient
3569bdad2833ef9ff027efbaac5b72275345ad68
d1a52c3f208fa42c41df8278c3d280f075eb020c
/tests/lean/run/discrRefinement3.lean
20774361b66b73bdbf6e4cce071f26c7b4624441
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
1,261
lean
inductive Mem (a : α) : List α → Prop where | head {as} : Mem a (a::as) | tail {as} : Mem a as → Mem a (a'::as) infix:50 " ∈ " => Mem example (a b : Nat) (h : a ∈ [b]) : b = a := match h with | Mem.head => rfl example {as : List α} (h : a ∈ b :: as) : b = a ∨ a ∈ as := match h with | Mem.head => Or.inl rfl | Mem.tail h' => Or.inr h' example (a b : Nat) (h : a.succ.succ = b.succ.succ.succ) : a = b.succ := match h with | rfl => rfl inductive Vec (α : Type u) : Nat → Type u where | nil : Vec α 0 | cons : α → Vec α n → Vec α (n + 1) def hv (xs : Vec Nat (n+1)) : Nat := match xs with | Vec.cons a .. => a def addHead (p : Vec Nat n × Vec Nat n) : Nat := match p with | (Vec.cons a _, Vec.cons b _) => a + b | (Vec.nil, Vec.nil) => 0 inductive HVec : {n : Nat} → Vec (Type u) n → Type (u+1) | nil : HVec Vec.nil | cons : {αs : Vec (Type u) n} → α → HVec αs → HVec (Vec.cons α αs) abbrev HVec.TypeHead {αs : Vec (Type u) n} (xs : HVec αs) : Type u := match xs with | HVec.nil => PUnit | HVec.cons (α := α) .. => α def HVec.head {αs : Vec (Type u) n} (xs : HVec αs) : TypeHead xs := match xs with | HVec.cons a _ => a | HVec.nil => PUnit.unit
9d0a148772eaf74437c2969a724ce45015e08048
491068d2ad28831e7dade8d6dff871c3e49d9431
/library/data/set/card.lean
01681f03ae0855474358b53991c32cb3e5293e68
[ "Apache-2.0" ]
permissive
davidmueller13/lean
65a3ed141b4088cd0a268e4de80eb6778b21a0e9
c626e2e3c6f3771e07c32e82ee5b9e030de5b050
refs/heads/master
1,611,278,313,401
1,444,021,177,000
1,444,021,177,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,055
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad Cardinality of finite sets. -/ import .finite data.finset.card open nat classical namespace set variable {A : Type} noncomputable definition card (s : set A) := finset.card (set.to_finset s) theorem card_to_set (s : finset A) : card (finset.to_set s) = finset.card s := by rewrite [↑card, to_finset_to_set] theorem card_of_not_finite {s : set A} (nfins : ¬ finite s) : card s = 0 := by rewrite [↑card, to_finset_of_not_finite nfins] theorem card_empty : card (∅ : set A) = 0 := by rewrite [-finset.to_set_empty, card_to_set] theorem card_insert_of_mem {a : A} {s : set A} (H : a ∈ s) : card (insert a s) = card s := if fins : finite s then (by rewrite [↑card, to_finset_insert, -mem_to_finset_eq at H, finset.card_insert_of_mem H]) else (assert ¬ finite (insert a s), from suppose _, absurd (!finite_of_finite_insert this) fins, by rewrite [card_of_not_finite fins, card_of_not_finite this]) theorem card_insert_of_not_mem {a : A} {s : set A} [fins : finite s] (H : a ∉ s) : card (insert a s) = card s + 1 := by rewrite [↑card, to_finset_insert, -mem_to_finset_eq at H, finset.card_insert_of_not_mem H] theorem card_insert_le (a : A) (s : set A) [fins : finite s] : card (insert a s) ≤ card s + 1 := if H : a ∈ s then by rewrite [card_insert_of_mem H]; apply le_succ else by rewrite [card_insert_of_not_mem H] theorem card_singleton (a : A) : card '{a} = 1 := by rewrite [card_insert_of_not_mem !not_mem_empty, card_empty] /- Note: the induction tactic does not work well with the set induction principle with the extra predicate "finite". -/ theorem eq_empty_of_card_eq_zero {s : set A} [fins : finite s] : card s = 0 → s = ∅ := induction_on_finite s (by intro H; exact rfl) (begin intro a s' fins' anins IH H, rewrite (card_insert_of_not_mem anins) at H, apply nat.no_confusion H end) theorem card_upto (n : ℕ) : card {i | i < n} = n := by rewrite [↑card, to_finset_upto, finset.card_upto] theorem card_add_card (s₁ s₂ : set A) [fins₁ : finite s₁] [fins₂ : finite s₂] : card s₁ + card s₂ = card (s₁ ∪ s₂) + card (s₁ ∩ s₂) := begin rewrite [-to_set_to_finset s₁, -to_set_to_finset s₂], rewrite [-finset.to_set_union, -finset.to_set_inter, *card_to_set], apply finset.card_add_card end theorem card_union (s₁ s₂ : set A) [fins₁ : finite s₁] [fins₂ : finite s₂] : card (s₁ ∪ s₂) = card s₁ + card s₂ - card (s₁ ∩ s₂) := calc card (s₁ ∪ s₂) = card (s₁ ∪ s₂) + card (s₁ ∩ s₂) - card (s₁ ∩ s₂) : add_sub_cancel ... = card s₁ + card s₂ - card (s₁ ∩ s₂) : card_add_card s₁ s₂ theorem card_union_of_disjoint {s₁ s₂ : set A} [fins₁ : finite s₁] [fins₂ : finite s₂] (H : s₁ ∩ s₂ = ∅) : card (s₁ ∪ s₂) = card s₁ + card s₂ := by rewrite [card_union, H, card_empty] theorem card_eq_card_add_card_diff {s₁ s₂ : set A} [fins₁ : finite s₁] [fins₂ : finite s₂] (H : s₁ ⊆ s₂) : card s₂ = card s₁ + card (s₂ \ s₁) := have H1 : s₁ ∩ (s₂ \ s₁) = ∅, from eq_empty_of_forall_not_mem (take x, assume H, (and.right (and.right H)) (and.left H)), have s₂ = s₁ ∪ (s₂ \ s₁), from eq.symm (union_diff_cancel H), calc card s₂ = card (s₁ ∪ (s₂ \ s₁)) : {this} ... = card s₁ + card (s₂ \ s₁) : card_union_of_disjoint H1 theorem card_le_card_of_subset {s₁ s₂ : set A} [fins₁ : finite s₁] [fins₂ : finite s₂] (H : s₁ ⊆ s₂) : card s₁ ≤ card s₂ := calc card s₂ = card s₁ + card (s₂ \ s₁) : card_eq_card_add_card_diff H ... ≥ card s₁ : le_add_right variable {B : Type} theorem card_image_eq_of_inj_on {f : A → B} {s : set A} [fins : finite s] (injfs : inj_on f s) : card (image f s) = card s := begin rewrite [↑card, to_finset_image]; apply finset.card_image_eq_of_inj_on, rewrite to_set_to_finset, apply injfs end theorem card_le_of_inj_on (a : set A) (b : set B) [finb : finite b] (Pex : ∃ f : A → B, inj_on f a ∧ (image f a ⊆ b)) : card a ≤ card b := by_cases (assume fina : finite a, obtain f H, from Pex, finset.card_le_of_inj_on (to_finset a) (to_finset b) (exists.intro f begin rewrite [finset.subset_eq_to_set_subset, finset.to_set_image, *to_set_to_finset], exact H end)) (assume nfina : ¬ finite a, by rewrite [card_of_not_finite nfina]; exact !zero_le) theorem card_image_le (f : A → B) (s : set A) [fins : finite s] : card (image f s) ≤ card s := by rewrite [↑card, to_finset_image]; apply finset.card_image_le theorem inj_on_of_card_image_eq {f : A → B} {s : set A} [fins : finite s] (H : card (image f s) = card s) : inj_on f s := begin rewrite -to_set_to_finset, apply finset.inj_on_of_card_image_eq, rewrite [-to_finset_to_set (finset.image _ _), finset.to_set_image, to_set_to_finset], exact H end theorem card_pos_of_mem {a : A} {s : set A} [fins : finite s] (H : a ∈ s) : card s > 0 := have (#finset a ∈ to_finset s), by rewrite [finset.mem_eq_mem_to_set, to_set_to_finset]; apply H, finset.card_pos_of_mem this theorem eq_of_card_eq_of_subset {s₁ s₂ : set A} [fins₁ : finite s₁] [fins₂ : finite s₂] (Hcard : card s₁ = card s₂) (Hsub : s₁ ⊆ s₂) : s₁ = s₂ := begin rewrite [-to_set_to_finset s₁, -to_set_to_finset s₂, -finset.eq_eq_to_set_eq], apply finset.eq_of_card_eq_of_subset Hcard, rewrite [to_finset_subset_to_finset_eq], exact Hsub end theorem exists_two_of_card_gt_one {s : set A} (H : 1 < card s) : ∃ a b, a ∈ s ∧ b ∈ s ∧ a ≠ b := assert fins : finite s, from by_contradiction (assume nfins, by rewrite [card_of_not_finite nfins at H]; apply !not_succ_le_zero H), by rewrite [-to_set_to_finset s]; apply finset.exists_two_of_card_gt_one H end set
8e52266588f6c2ba0d0018d07938efc22f396fe7
271e26e338b0c14544a889c31c30b39c989f2e0f
/stage0/src/Init/Lean/MetavarContext.lean
053cf2a2502a143c2c58a1c562e27fdaa248ed03
[ "Apache-2.0" ]
permissive
dgorokho/lean4
805f99b0b60c545b64ac34ab8237a8504f89d7d4
e949a052bad59b1c7b54a82d24d516a656487d8a
refs/heads/master
1,607,061,363,851
1,578,006,086,000
1,578,006,086,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
40,217
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.Data.Nat import Init.Data.Option import Init.Lean.Data.NameGenerator import Init.Lean.Util.MonadCache import Init.Lean.LocalContext namespace Lean /- The metavariable context stores metavariable declarations and their assignments. It is used in the elaborator, tactic framework, unifier (aka `isDefEq`), and type class resolution (TC). First, we list all the requirements imposed by these modules. - We may invoke TC while executing `isDefEq`. We need this feature to be able to solve unification problems such as: ``` f ?a (ringHasAdd ?s) ?x ?y =?= f Int intHasAdd n m ``` where `(?a : Type) (?s : Ring ?a) (?x ?y : ?a)` During `isDefEq` (i.e., unification), it will need to solve the constrain ``` ringHasAdd ?s =?= intHasAdd ``` We say `ringHasAdd ?s` is stuck because it cannot be reduced until we synthesize the term `?s : Ring ?a` using TC. This can be done since we have assigned `?a := Int` when solving `?a =?= Int`. - TC uses `isDefEq`, and `isDefEq` may create TC problems as shown aaa. Thus, we may have nested TC problems. - `isDefEq` extends the local context when going inside binders. Thus, the local context for nested TC may be an extension of the local context for outer TC. - TC should not assign metavariables created by the elaborator, simp, tactic framework, and outer TC problems. Reason: TC commits to the first solution it finds. Consider the TC problem `HasCoe Nat ?x`, where `?x` is a metavariable created by the caller. There are many solutions to this problem (e.g., `?x := Int`, `?x := Real`, ...), and it doesn’t make sense to commit to the first one since TC does not know the the constraints the caller may impose on `?x` after the TC problem is solved. Remark: we claim it is not feasible to make the whole system backtrackable, and allow the caller to backtrack back to TC and ask it for another solution if the first one found did not work. We claim it would be too inefficient. - TC metavariables should not leak outside of TC. Reason: we want to get rid of them after we synthesize the instance. - `simp` invokes `isDefEq` for matching the left-hand-side of equations to terms in our goal. Thus, it may invoke TC indirectly. - In Lean3, we didn’t have to create a fresh pattern for trying to match the left-hand-side of equations when executing `simp`. We had a mechanism called tmp metavariables. It avoided this overhead, but it created many problems since `simp` may indirectly call TC which may recursively call TC. Moreover, we want to allow TC to invoke tactics. Thus, when `simp` invokes `isDefEq`, it may indirectly invoke a tactic and `simp` itself. The Lean3 approach assumed that metavariables were short-lived, this is not true in Lean4, and to some extent was also not true in Lean3 since `simp`, in principle, could trigger an arbitrary number of nested TC problems. - Here are some possible call stack traces we could have in Lean3 (and Lean4). ``` Elaborator (-> TC -> isDefEq)+ Elaborator -> isDefEq (-> TC -> isDefEq)* Elaborator -> simp -> isDefEq (-> TC -> isDefEq)* ``` In Lean4, TC may also invoke tactics. - In Lean3 and Lean4, TC metavariables are not really short-lived. We solve an arbitrary number of unification problems, and we may have nested TC invocations. - TC metavariables do not share the same local context even in the same invocation. In the C++ and Lean implementations we use a trick to ensure they do: https://github.com/leanprover/lean/blob/92826917a252a6092cffaf5fc5f1acb1f8cef379/src/library/type_context.cpp#L3583-L3594 - Metavariables may be natural, synthetic or syntheticOpaque. a) Natural metavariables may be assigned by unification (i.e., `isDefEq`). b) Synthetic metavariables may still be assigned by unification, but whenever possible `isDefEq` will avoid the assignment. For example, if we have the unification constaint `?m =?= ?n`, where `?m` is synthetic, but `?n` is not, `isDefEq` solves it by using the assignment `?n := ?m`. We use synthetic metavariables for type class resolution. Any module that creates synthetic metavariables, must also check whether they have been assigned by `isDefEq`, and then still synthesize them, and check whether the sythesized result is compatible with the one assigned by `isDefEq`. c) SyntheticOpaque metavariables are never assigned by `isDefEq`. That is, the constraint `?n =?= Nat.succ Nat.zero` always fail if `?n` is a syntheticOpaque metavariable. This kind of metavariable is created by tactics such as `intro`. Reason: in the tactic framework, subgoals as represented as metavariables, and a subgoal `?n` is considered as solved whenever the metavariable is assigned. This distinction was not precise in Lean3 and produced counterintuitive behavior. For example, the following hack was added in Lean3 to work around one of these issues: https://github.com/leanprover/lean/blob/92826917a252a6092cffaf5fc5f1acb1f8cef379/src/library/type_context.cpp#L2751 - When creating lambda/forall expressions, we need to convert/abstract free variables and convert them to bound variables. Now, suppose we a trying to create a lambda/forall expression by abstracting free variables `xs` and a term `t[?m]` which contains a metavariable `?m`, and the local context of `?m` contains `xs`. The term ``` fun xs => t[?m] ``` will be ill-formed if we later assign a term `s` to `?m`, and `s` contains free variables in `xs`. We address this issue by changing the free variable abstraction procedure. We consider two cases: `?m` is natural, `?m` is synthetic. Assume the type of `?m` is `A[xs]`. Then, in both cases we create an auxiliary metavariable `?n` with type `forall xs => A[xs]`, and local context := local context of `?m` - `xs`. In both cases, we produce the term `fun xs => t[?n xs]` 1- If `?m` is natural or synthetic, then we assign `?m := ?n xs`, and we produce the term `fun xs => t[?n xs]` 2- If `?m` is syntheticOpaque, then we mark `?n` as a syntheticOpaque variable. However, `?n` is managed by the metavariable context itself. We say we have a "delayed assignment" `?n xs := ?m`. That is, after a term `s` is assigned to `?m`, and `s` does not contain metavariables, we replace any occurrence `?n ts` with `s[xs := ts]`. Gruesome details: - When we create the type `forall xs => A` for `?n`, we may encounter the same issue if `A` contains metavariables. So, the process above is recursive. We claim it terminates because we keep creating new metavariables with smaller local contexts. - Suppose, we have `t[?m]` and we want to create a let-expression by abstracting a let-decl free variable `x`, and the local context of `?m` contatins `x`. Similarly to the previous case ``` let x : T := v; t[?m] ``` will be ill-formed if we later assign a term `s` to `?m`, and `s` contains free variable `x`. Again, assume the type of `?m` is `A[x]`. 1- If `?m` is natural or synthetic, then we create `?n : (let x : T := v; A[x])` with and local context := local context of `?m` - `x`, we assign `?m := ?n`, and produce the term `let x : T := v; t[?n]`. That is, we are just making sure `?n` must never be assigned to a term containing `x`. 2- If `?m` is syntheticOpaque, we create a fresh syntheticOpaque `?n` with type `?n : T -> (let x : T := v; A[x])` and local context := local context of `?m` - `x`, create the delayed assignment `?n #[x] := ?m`, and produce the term `let x : T := v; t[?n x]`. Now suppose we assign `s` to `?m`. We do not assign the term `fun (x : T) => s` to `?n`, since `fun (x : T) => s` may not even be type correct. Instead, we just replace applications `?n r` with `s[x/r]`. The term `r` may not necessarily be a bound variable. For example, a tactic may have reduced `let x : T := v; t[?n x]` into `t[?n v]`. We are essentially using the pair "delayed assignment + application" to implement a delayed substitution. - We use TC for implementing coercions. Both Joe Hendrix and Reid Barton reported a nasty limitation. In Lean3, TC will not be used if there are metavariables in the TC problem. For example, the elaborator will not try to synthesize `HasCoe Nat ?x`. This is good, but this constraint is too strict for problems such as `HasCoe (Vector Bool ?n) (BV ?n)`. The coercion exists independently of `?n`. Thus, during TC, we want `isDefEq` to throw an exception instead of return `false` whenever it tries to assign a metavariable owned by its caller. The idea is to sign to the caller that it cannot solve the TC problem at this point, and more information is needed. That is, the caller must make progress an assign its metavariables before trying to invoke TC again. In Lean4, we are using a simpler design for the `MetavarContext`. - No distinction betwen temporary and regular metavariables. - Metavariables have a `depth` Nat field. - MetavarContext also has a `depth` field. - We bump the `MetavarContext` depth when we create a nested problem. Example: Elaborator (depth = 0) -> Simplifier matcher (depth = 1) -> TC (level = 2) -> TC (level = 3) -> ... - When `MetavarContext` is at depth N, `isDefEq` does not assign variables from `depth < N`. - Metavariables from depth N+1 must be fully assigned before we return to level N. - New design even allows us to invoke tactics from TC. * Main concern We don't have tmp metavariables anymore in Lean4. Thus, before trying to match the left-hand-side of an equation in `simp`. We first must bump the level of the `MetavarContext`, create fresh metavariables, then create a new pattern by replacing the free variable on the left-hand-side with these metavariables. We are hoping to minimize this overhead by - Using better indexing data structures in `simp`. They should reduce the number of time `simp` must invoke `isDefEq`. - Implementing `isDefEqApprox` which ignores metavariables and returns only `false` or `undef`. It is a quick filter that allows us to fail quickly and avoid the creation of new fresh metavariables, and a new pattern. - Adding built-in support for arithmetic, Logical connectives, etc. Thus, we avoid a bunch of lemmas in the simp set. - Adding support for AC-rewriting. In Lean3, users use AC lemmas as rewriting rules for "sorting" terms. This is inefficient, requires a quadratic number of rewrite steps, and does not preserve the structure of the goal. The temporary metavariables were also used in the "app builder" module used in Lean3. The app builder uses `isDefEq`. So, it could, in principle, invoke an arbitrary number of nested TC problems. However, in Lean3, all app builder uses are controlled. That is, it is mainly used to synthesize implicit arguments using very simple unification and/or non-nested TC. So, if the "app builder" becomes a bottleneck without tmp metavars, we may solve the issue by implementing `isDefEqCheap` that never invokes TC and uses tmp metavars. -/ structure LocalInstance := (className : Name) (fvar : Expr) abbrev LocalInstances := Array LocalInstance def LocalInstance.beq (i₁ i₂ : LocalInstance) : Bool := i₁.fvar == i₂.fvar instance LocalInstance.hasBeq : HasBeq LocalInstance := ⟨LocalInstance.beq⟩ inductive MetavarKind | natural | synthetic | syntheticOpaque def MetavarKind.isSyntheticOpaque : MetavarKind → Bool | MetavarKind.syntheticOpaque => true | _ => false structure MetavarDecl := (userName : Name := Name.anonymous) (lctx : LocalContext) (type : Expr) (depth : Nat) (localInstances : LocalInstances) (kind : MetavarKind) namespace MetavarDecl instance : Inhabited MetavarDecl := ⟨{ lctx := arbitrary _, type := arbitrary _, depth := 0, localInstances := #[], kind := MetavarKind.natural }⟩ end MetavarDecl /-- A delayed assignment for a metavariable `?m`. It represents an assignment of the form `?m := (fun fvars => val)`. The local context `lctx` provides the declarations for `fvars`. Note that `fvars` may not be defined in the local context for `?m`. - TODO: after we delete the old frontend, we can remove the field `lctx`. This field is only used in old C++ implementation. -/ structure DelayedMetavarAssignment := (lctx : LocalContext) (fvars : Array Expr) (val : Expr) structure MetavarContext := (depth : Nat := 0) (lDepth : PersistentHashMap MVarId Nat := {}) (decls : PersistentHashMap MVarId MetavarDecl := {}) (lAssignment : PersistentHashMap MVarId Level := {}) (eAssignment : PersistentHashMap MVarId Expr := {}) (dAssignment : PersistentHashMap MVarId DelayedMetavarAssignment := {}) namespace MetavarContext instance : Inhabited MetavarContext := ⟨{}⟩ @[export lean_mk_metavar_ctx] def mkMetavarContext : Unit → MetavarContext := fun _ => {} /- Low level API for adding/declaring metavariable declarations. It is used to implement actions in the monads `MetaM`, `ElabM` and `TacticM`. It should not be used directly since the argument `(mvarId : MVarId)` is assumed to be "unique". -/ @[export lean_metavar_ctx_mk_decl] def addExprMVarDecl (mctx : MetavarContext) (mvarId : MVarId) (userName : Name) (lctx : LocalContext) (localInstances : LocalInstances) (type : Expr) (kind : MetavarKind := MetavarKind.natural) : MetavarContext := { decls := mctx.decls.insert mvarId { userName := userName, lctx := lctx, localInstances := localInstances, type := type, depth := mctx.depth, kind := kind }, .. mctx } /- Low level API for adding/declaring universe level metavariable declarations. It is used to implement actions in the monads `MetaM`, `ElabM` and `TacticM`. It should not be used directly since the argument `(mvarId : MVarId)` is assumed to be "unique". -/ def addLevelMVarDecl (mctx : MetavarContext) (mvarId : MVarId) : MetavarContext := { lDepth := mctx.lDepth.insert mvarId mctx.depth, .. mctx } @[export lean_metavar_ctx_find_decl] def findDecl? (mctx : MetavarContext) (mvarId : MVarId) : Option MetavarDecl := mctx.decls.find? mvarId def getDecl (mctx : MetavarContext) (mvarId : MVarId) : MetavarDecl := match mctx.decls.find? mvarId with | some decl => decl | none => panic! "unknown metavariable" def findLevelDepth? (mctx : MetavarContext) (mvarId : MVarId) : Option Nat := mctx.lDepth.find? mvarId def getLevelDepth (mctx : MetavarContext) (mvarId : MVarId) : Nat := match mctx.findLevelDepth? mvarId with | some d => d | none => panic! "unknown metavariable" @[export lean_metavar_ctx_assign_level] def assignLevel (m : MetavarContext) (mvarId : MVarId) (val : Level) : MetavarContext := { lAssignment := m.lAssignment.insert mvarId val, .. m } @[export lean_metavar_ctx_assign_expr] def assignExprCore (m : MetavarContext) (mvarId : MVarId) (val : Expr) : MetavarContext := { eAssignment := m.eAssignment.insert mvarId val, .. m } def assignExpr (m : MetavarContext) (mvarId : MVarId) (val : Expr) : MetavarContext := { eAssignment := m.eAssignment.insert mvarId val, .. m } @[export lean_metavar_ctx_assign_delayed] def assignDelayed (m : MetavarContext) (mvarId : MVarId) (lctx : LocalContext) (fvars : Array Expr) (val : Expr) : MetavarContext := { dAssignment := m.dAssignment.insert mvarId { lctx := lctx, fvars := fvars, val := val }, .. m } @[export lean_metavar_ctx_get_level_assignment] def getLevelAssignment? (m : MetavarContext) (mvarId : MVarId) : Option Level := m.lAssignment.find? mvarId @[export lean_metavar_ctx_get_expr_assignment] def getExprAssignment? (m : MetavarContext) (mvarId : MVarId) : Option Expr := m.eAssignment.find? mvarId @[export lean_metavar_ctx_get_delayed_assignment] def getDelayedAssignment? (m : MetavarContext) (mvarId : MVarId) : Option DelayedMetavarAssignment := m.dAssignment.find? mvarId @[export lean_metavar_ctx_is_level_assigned] def isLevelAssigned (m : MetavarContext) (mvarId : MVarId) : Bool := m.lAssignment.contains mvarId @[export lean_metavar_ctx_is_expr_assigned] def isExprAssigned (m : MetavarContext) (mvarId : MVarId) : Bool := m.eAssignment.contains mvarId @[export lean_metavar_ctx_is_delayed_assigned] def isDelayedAssigned (m : MetavarContext) (mvarId : MVarId) : Bool := m.dAssignment.contains mvarId @[export lean_metavar_ctx_erase_delayed] def eraseDelayed (m : MetavarContext) (mvarId : MVarId) : MetavarContext := { dAssignment := m.dAssignment.erase mvarId, .. m } def isLevelAssignable (mctx : MetavarContext) (mvarId : MVarId) : Bool := match mctx.lDepth.find? mvarId with | some d => d == mctx.depth | _ => panic! "unknown universe metavariable" def isExprAssignable (mctx : MetavarContext) (mvarId : MVarId) : Bool := let decl := mctx.getDecl mvarId; decl.depth == mctx.depth def incDepth (mctx : MetavarContext) : MetavarContext := { depth := mctx.depth + 1, .. mctx } /-- Return true iff the given level contains an assigned metavariable. -/ def hasAssignedLevelMVar (mctx : MetavarContext) : Level → Bool | Level.succ lvl _ => lvl.hasMVar && hasAssignedLevelMVar lvl | Level.max lvl₁ lvl₂ _ => (lvl₁.hasMVar && hasAssignedLevelMVar lvl₁) || (lvl₂.hasMVar && hasAssignedLevelMVar lvl₂) | Level.imax lvl₁ lvl₂ _ => (lvl₁.hasMVar && hasAssignedLevelMVar lvl₁) || (lvl₂.hasMVar && hasAssignedLevelMVar lvl₂) | Level.mvar mvarId _ => mctx.isLevelAssigned mvarId | Level.zero _ => false | Level.param _ _ => false /-- Return `true` iff expression contains assigned (level/expr) metavariables -/ def hasAssignedMVar (mctx : MetavarContext) : Expr → Bool | Expr.const _ lvls _ => lvls.any (hasAssignedLevelMVar mctx) | Expr.sort lvl _ => hasAssignedLevelMVar mctx lvl | Expr.app f a _ => (f.hasMVar && hasAssignedMVar f) || (a.hasMVar && hasAssignedMVar a) | Expr.letE _ t v b _ => (t.hasMVar && hasAssignedMVar t) || (v.hasMVar && hasAssignedMVar v) || (b.hasMVar && hasAssignedMVar b) | Expr.forallE _ d b _ => (d.hasMVar && hasAssignedMVar d) || (b.hasMVar && hasAssignedMVar b) | Expr.lam _ d b _ => (d.hasMVar && hasAssignedMVar d) || (b.hasMVar && hasAssignedMVar b) | Expr.fvar _ _ => false | Expr.bvar _ _ => false | Expr.lit _ _ => false | Expr.mdata _ e _ => e.hasMVar && hasAssignedMVar e | Expr.proj _ _ e _ => e.hasMVar && hasAssignedMVar e | Expr.mvar mvarId _ => mctx.isExprAssigned mvarId | Expr.localE _ _ _ _ => unreachable! /-- Return true iff the given level contains a metavariable that can be assigned. -/ def hasAssignableLevelMVar (mctx : MetavarContext) : Level → Bool | Level.succ lvl _ => lvl.hasMVar && hasAssignableLevelMVar lvl | Level.max lvl₁ lvl₂ _ => (lvl₁.hasMVar && hasAssignableLevelMVar lvl₁) || (lvl₂.hasMVar && hasAssignableLevelMVar lvl₂) | Level.imax lvl₁ lvl₂ _ => (lvl₁.hasMVar && hasAssignableLevelMVar lvl₁) || (lvl₂.hasMVar && hasAssignableLevelMVar lvl₂) | Level.mvar mvarId _ => mctx.isLevelAssignable mvarId | Level.zero _ => false | Level.param _ _ => false /-- Return `true` iff expression contains a metavariable that can be assigned. -/ def hasAssignableMVar (mctx : MetavarContext) : Expr → Bool | Expr.const _ lvls _ => lvls.any (hasAssignableLevelMVar mctx) | Expr.sort lvl _ => hasAssignableLevelMVar mctx lvl | Expr.app f a _ => (f.hasMVar && hasAssignableMVar f) || (a.hasMVar && hasAssignableMVar a) | Expr.letE _ t v b _ => (t.hasMVar && hasAssignableMVar t) || (v.hasMVar && hasAssignableMVar v) || (b.hasMVar && hasAssignableMVar b) | Expr.forallE _ d b _ => (d.hasMVar && hasAssignableMVar d) || (b.hasMVar && hasAssignableMVar b) | Expr.lam _ d b _ => (d.hasMVar && hasAssignableMVar d) || (b.hasMVar && hasAssignableMVar b) | Expr.fvar _ _ => false | Expr.bvar _ _ => false | Expr.lit _ _ => false | Expr.mdata _ e _ => e.hasMVar && hasAssignableMVar e | Expr.proj _ _ e _ => e.hasMVar && hasAssignableMVar e | Expr.mvar mvarId _ => mctx.isExprAssignable mvarId | Expr.localE _ _ _ _ => unreachable! partial def instantiateLevelMVars : Level → StateM MetavarContext Level | lvl@(Level.succ lvl₁ _) => do lvl₁ ← instantiateLevelMVars lvl₁; pure (Level.updateSucc! lvl lvl₁) | lvl@(Level.max lvl₁ lvl₂ _) => do lvl₁ ← instantiateLevelMVars lvl₁; lvl₂ ← instantiateLevelMVars lvl₂; pure (Level.updateMax! lvl lvl₁ lvl₂) | lvl@(Level.imax lvl₁ lvl₂ _) => do lvl₁ ← instantiateLevelMVars lvl₁; lvl₂ ← instantiateLevelMVars lvl₂; pure (Level.updateIMax! lvl lvl₁ lvl₂) | lvl@(Level.mvar mvarId _) => do mctx ← get; match getLevelAssignment? mctx mvarId with | some newLvl => if !newLvl.hasMVar then pure newLvl else do newLvl' ← instantiateLevelMVars newLvl; modify $ fun mctx => mctx.assignLevel mvarId newLvl'; pure newLvl' | none => pure lvl | lvl => pure lvl namespace InstantiateExprMVars private abbrev M := StateM (WithHashMapCache Expr Expr MetavarContext) @[inline] def instantiateLevelMVars (lvl : Level) : M Level := WithHashMapCache.fromState $ MetavarContext.instantiateLevelMVars lvl @[inline] private def visit (f : Expr → M Expr) (e : Expr) : M Expr := if !e.hasMVar then pure e else checkCache e f @[inline] private def getMCtx : M MetavarContext := do s ← get; pure s.state @[inline] private def modifyCtx (f : MetavarContext → MetavarContext) : M Unit := modify $ fun s => { state := f s.state, .. s } /-- instantiateExprMVars main function -/ partial def main : Expr → M Expr | e@(Expr.proj _ _ s _) => do s ← visit main s; pure (e.updateProj! s) | e@(Expr.forallE _ d b _) => do d ← visit main d; b ← visit main b; pure (e.updateForallE! d b) | e@(Expr.lam _ d b _) => do d ← visit main d; b ← visit main b; pure (e.updateLambdaE! d b) | e@(Expr.letE _ t v b _) => do t ← visit main t; v ← visit main v; b ← visit main b; pure (e.updateLet! t v b) | e@(Expr.const _ lvls _) => do lvls ← lvls.mapM instantiateLevelMVars; pure (e.updateConst! lvls) | e@(Expr.sort lvl _) => do lvl ← instantiateLevelMVars lvl; pure (e.updateSort! lvl) | e@(Expr.mdata _ b _) => do b ← visit main b; pure (e.updateMData! b) | e@(Expr.app _ _ _) => e.withApp $ fun f args => do let instArgs (f : Expr) : M Expr := do { args ← args.mapM (visit main); pure (mkAppN f args) }; let instApp : M Expr := do { let wasMVar := f.isMVar; f ← visit main f; if wasMVar && f.isLambda then /- Some of the arguments in args are irrelevant after we beta reduce. -/ visit main (f.betaRev args.reverse) else instArgs f }; match f with | Expr.mvar mvarId _ => do mctx ← getMCtx; match mctx.getDelayedAssignment? mvarId with | none => instApp | some { fvars := fvars, val := val, .. } => /- Apply "delayed substitution" (i.e., delayed assignment + application). That is, `f` is some metavariable `?m`, that is delayed assigned to `val`. If after instantiating `val`, we obtain `newVal`, and `newVal` does not contain metavariables, we replace the free variables `fvars` in `newVal` with the first `fvars.size` elements of `args`. -/ if fvars.size > args.size then /- We don't have sufficient arguments for instantiating the free variables `fvars`. This can only happy if a tactic or elaboration function is not implemented correctly. We decided to not use `panic!` here and report it as an error in the frontend when we are checking for unassigned metavariables in an elaborated term. -/ instArgs f else do newVal ← visit main val; if newVal.hasMVar then instArgs f else do args ← args.mapM (visit main); /- Example: suppose we have `?m t1 t2 t3` That is, `f := ?m` and `args := #[t1, t2, t3]` Morever, `?m` is delayed assigned `?m #[x, y] := f x y` where, `fvars := #[x, y]` and `newVal := f x y`. After abstracting `newVal`, we have `f (Expr.bvar 0) (Expr.bvar 1)`. After `instantiaterRevRange 0 2 args`, we have `f t1 t2`. After `mkAppRange 2 3`, we have `f t1 t2 t3` -/ let newVal := newVal.abstract fvars; let result := newVal.instantiateRevRange 0 fvars.size args; let result := mkAppRange result fvars.size args.size args; pure $ result | _ => instApp | e@(Expr.mvar mvarId _) => checkCache e $ fun e => do mctx ← getMCtx; match mctx.getExprAssignment? mvarId with | some newE => do newE' ← visit main newE; modifyCtx $ fun mctx => mctx.assignExpr mvarId newE'; pure newE' | none => pure e | e => pure e end InstantiateExprMVars def instantiateMVars (mctx : MetavarContext) (e : Expr) : Expr × MetavarContext := if !e.hasMVar then (e, mctx) else (WithHashMapCache.toState $ InstantiateExprMVars.main e).run mctx namespace DependsOn private abbrev M := StateM ExprSet private def visit? (e : Expr) : M Bool := if !e.hasMVar && !e.hasFVar then pure false else do s ← get; if s.contains e then pure false else do modify $ fun s => s.insert e; pure true @[inline] private def visit (main : Expr → M Bool) (e : Expr) : M Bool := condM (visit? e) (main e) (pure false) @[specialize] private partial def dep (mctx : MetavarContext) (p : FVarId → Bool) : Expr → M Bool | e@(Expr.proj _ _ s _) => visit dep s | e@(Expr.forallE _ d b _) => visit dep d <||> visit dep b | e@(Expr.lam _ d b _) => visit dep d <||> visit dep b | e@(Expr.letE _ t v b _) => visit dep t <||> visit dep v <||> visit dep b | e@(Expr.mdata _ b _) => visit dep b | e@(Expr.app f a _) => visit dep a <||> if f.isApp then dep f else visit dep f | e@(Expr.mvar mvarId _) => match mctx.getExprAssignment? mvarId with | some a => visit dep a | none => let lctx := (mctx.getDecl mvarId).lctx; pure $ lctx.any $ fun decl => p decl.fvarId | e@(Expr.fvar fvarId _) => pure $ p fvarId | e => pure false @[inline] partial def main (mctx : MetavarContext) (p : FVarId → Bool) (e : Expr) : M Bool := if !e.hasFVar && !e.hasMVar then pure false else dep mctx p e end DependsOn /-- Return `true` iff `e` depends on a free variable `x` s.t. `p x` is `true`. For each metavariable `?m` occurring in `x` 1- If `?m := t`, then we visit `t` looking for `x` 2- If `?m` is unassigned, then we consider the worst case and check whether `x` is in the local context of `?m`. This case is a "may dependency". That is, we may assign a term `t` to `?m` s.t. `t` contains `x`. -/ @[inline] def exprDependsOn (mctx : MetavarContext) (p : FVarId → Bool) (e : Expr) : Bool := (DependsOn.main mctx p e).run' {} /-- Similar to `exprDependsOn`, but checks the expressions in the given local declaration depends on a free variable `x` s.t. `p x` is `true`. -/ @[inline] def localDeclDependsOn (mctx : MetavarContext) (p : FVarId → Bool) : LocalDecl → Bool | LocalDecl.cdecl _ _ _ type _ => exprDependsOn mctx p type | LocalDecl.ldecl _ _ _ type value => (DependsOn.main mctx p type <||> DependsOn.main mctx p value).run' {} namespace MkBinding inductive Exception | revertFailure (mctx : MetavarContext) (lctx : LocalContext) (toRevert : Array Expr) (decl : LocalDecl) def Exception.toString : Exception → String | Exception.revertFailure _ lctx toRevert decl => "failed to revert " ++ toString (toRevert.map (fun x => "'" ++ toString (lctx.getFVar! x).userName ++ "'")) ++ ", '" ++ toString decl.userName ++ "' depends on them, and it is an auxiliary declaration created by the elaborator" ++ " (possible solution: use tactic 'clear' to remove '" ++ toString decl.userName ++ "' from local context)" instance Exception.hasToString : HasToString Exception := ⟨Exception.toString⟩ /-- `MkBinding` and `elimMVarDepsAux` are mutually recursive, but `cache` is only used at `elimMVarDepsAux`. We use a single state object for convenience. We have a `NameGenerator` because we need to generate fresh auxiliary metavariables. -/ structure State := (mctx : MetavarContext) (ngen : NameGenerator) (cache : HashMap Expr Expr := {}) -- abbrev M := EStateM Exception State instance : MonadHashMapCacheAdapter Expr Expr M := { getCache := do s ← get; pure s.cache, modifyCache := fun f => modify $ fun s => { cache := f s.cache, .. s } } /-- Return the local declaration of the free variable `x` in `xs` with the smallest index -/ private def getLocalDeclWithSmallestIdx (lctx : LocalContext) (xs : Array Expr) : LocalDecl := let d : LocalDecl := lctx.getFVar! $ xs.get! 0; xs.foldlFrom (fun d x => let decl := lctx.getFVar! x; if decl.index < d.index then decl else d) d 1 /-- Given `toRevert` an array of free variables s.t. `lctx` contains their declarations, return a new array of free variables that contains `toRevert` and all free variables in `lctx` that may depend on `toRevert`. Remark: the result is sorted by `LocalDecl` indices. -/ private def collectDeps (mctx : MetavarContext) (lctx : LocalContext) (toRevert : Array Expr) : Except Exception (Array Expr) := if toRevert.size == 0 then pure toRevert else let minDecl := getLocalDeclWithSmallestIdx lctx toRevert; lctx.foldlFromM (fun newToRevert decl => if toRevert.any (fun x => decl.fvarId == x.fvarId!) then pure (newToRevert.push decl.toExpr) else if localDeclDependsOn mctx (fun fvarId => newToRevert.any $ fun x => x.fvarId! == fvarId) decl then if decl.binderInfo.isAuxDecl then throw (Exception.revertFailure mctx lctx toRevert decl) else pure (newToRevert.push decl.toExpr) else pure newToRevert) (Array.mkEmpty toRevert.size) minDecl /-- Create a new `LocalContext` by removing the free variables in `toRevert` from `lctx`. We use this function when we create auxiliary metavariables at `elimMVarDepsAux`. -/ private def reduceLocalContext (lctx : LocalContext) (toRevert : Array Expr) : LocalContext := toRevert.foldr (fun x lctx => lctx.erase x.fvarId!) lctx @[inline] private def visit (f : Expr → M Expr) (e : Expr) : M Expr := if !e.hasMVar then pure e else checkCache e f @[inline] private def getMCtx : M MetavarContext := do s ← get; pure s.mctx /-- Return free variables in `xs` that are in the local context `lctx` -/ private def getInScope (lctx : LocalContext) (xs : Array Expr) : Array Expr := xs.foldl (fun scope x => if lctx.contains x.fvarId! then scope.push x else scope) #[] /-- Execute `x` with an empty cache, and then restore the original cache. -/ @[inline] private def withFreshCache {α} (x : M α) : M α := do cache ← modifyGet $ fun s => (s.cache, { cache := {}, .. s }); a ← x; modify $ fun s => { cache := cache, .. s }; pure a @[inline] private def abstractRangeAux (elimMVarDeps : Expr → M Expr) (xs : Array Expr) (i : Nat) (e : Expr) : M Expr := do e ← elimMVarDeps e; pure (e.abstractRange i xs) private def mkAuxMVarType (elimMVarDeps : Expr → M Expr) (lctx : LocalContext) (xs : Array Expr) (kind : MetavarKind) (e : Expr) : M Expr := do e ← abstractRangeAux elimMVarDeps xs xs.size e; xs.size.foldRevM (fun i e => let x := xs.get! i; match lctx.getFVar! x with | LocalDecl.cdecl _ _ n type bi => do type ← abstractRangeAux elimMVarDeps xs i type; pure $ Lean.mkForall n bi type e | LocalDecl.ldecl _ _ n type value => do type ← abstractRangeAux elimMVarDeps xs i type; value ← abstractRangeAux elimMVarDeps xs i value; let e := mkLet n type value e; match kind with | MetavarKind.syntheticOpaque => -- See "Gruesome details" section in the beginning of the file let e := e.liftLooseBVars 0 1; pure $ mkForall n BinderInfo.default type e | _ => pure e) e /-- Create an application `mvar ys` where `ys` are the free variables. See "Gruesome details" in the beginning of the file for understanding how let-decl free variables are handled. -/ private def mkMVarApp (lctx : LocalContext) (mvar : Expr) (xs : Array Expr) (kind : MetavarKind) : Expr := xs.foldl (fun e x => match kind with | MetavarKind.syntheticOpaque => mkApp e x | _ => if (lctx.getFVar! x).isLet then e else mkApp e x) mvar private def mkAuxMVar (lctx : LocalContext) (localInsts : LocalInstances) (type : Expr) (kind : MetavarKind) : M MVarId := do s ← get; let mvarId := s.ngen.curr; modify $ fun s => { mctx := s.mctx.addExprMVarDecl mvarId Name.anonymous lctx localInsts type kind, ngen := s.ngen.next, .. s }; pure mvarId /-- Return true iff some `e` in `es` depends on `fvarId` -/ private def anyDependsOn (mctx : MetavarContext) (es : Array Expr) (fvarId : FVarId) : Bool := es.any $ fun e => exprDependsOn mctx (fun fvarId' => fvarId == fvarId') e private partial def elimMVarDepsApp (elimMVarDepsAux : Expr → M Expr) (xs : Array Expr) : Expr → Array Expr → M Expr | f, args => match f with | Expr.mvar mvarId _ => do let processDefault (newF : Expr) : M Expr := do { if newF.isLambda then do args ← args.mapM (visit elimMVarDepsAux); pure $ newF.betaRev args.reverse else if newF == f then do args ← args.mapM (visit elimMVarDepsAux); pure $ mkAppN newF args else elimMVarDepsApp newF args }; mctx ← getMCtx; match mctx.getExprAssignment? mvarId with | some val => processDefault val | _ => let mvarDecl := mctx.getDecl mvarId; let mvarLCtx := mvarDecl.lctx; let toRevert := getInScope mvarLCtx xs; if toRevert.size == 0 then processDefault f else let newMVarKind := if !mctx.isExprAssignable mvarId then MetavarKind.syntheticOpaque else mvarDecl.kind; /- If `mvarId` is the lhs of a delayed assignment `?m #[x_1, ... x_n] := val`, then `nestedFVars` is `#[x_1, ..., x_n]`. In this case, we produce a new `syntheticOpaque` metavariable `?n` and a delayed assignment ``` ?n #[y_1, ..., y_m, x_1, ... x_n] := ?m x_1 ... x_n ``` where `#[y_1, ..., y_m]` is `toRevert` after `collectDeps`. Remark: `newMVarKind != MetavarKind.syntheticOpaque ==> nestedFVars == #[]` -/ let continue (nestedFVars : Array Expr) : M Expr := do { args ← args.mapM (visit elimMVarDepsAux); match collectDeps mctx mvarLCtx toRevert with | Except.error ex => throw ex | Except.ok toRevert => do let newMVarLCtx := reduceLocalContext mvarLCtx toRevert; let newLocalInsts := mvarDecl.localInstances.filter $ fun inst => toRevert.all $ fun x => inst.fvar != x; newMVarType ← mkAuxMVarType elimMVarDepsAux mvarLCtx toRevert newMVarKind mvarDecl.type; newMVarId ← mkAuxMVar newMVarLCtx newLocalInsts newMVarType newMVarKind; let newMVar := mkMVar newMVarId; let result := mkMVarApp mvarLCtx newMVar toRevert newMVarKind; match newMVarKind with | MetavarKind.syntheticOpaque => modify $ fun s => { mctx := assignDelayed s.mctx newMVarId mvarLCtx (toRevert ++ nestedFVars) (mkAppN f nestedFVars), .. s } | _ => modify $ fun s => { mctx := assignExpr s.mctx mvarId result, .. s }; pure (mkAppN result args) }; if !mvarDecl.kind.isSyntheticOpaque then continue #[] else match mctx.getDelayedAssignment? mvarId with | none => continue #[] | some { fvars := fvars, .. } => continue fvars | _ => do f ← visit elimMVarDepsAux f; args ← args.mapM (visit elimMVarDepsAux); pure (mkAppN f args) private partial def elimMVarDepsAux (xs : Array Expr) : Expr → M Expr | e@(Expr.proj _ _ s _) => do s ← visit elimMVarDepsAux s; pure (e.updateProj! s) | e@(Expr.forallE _ d b _) => do d ← visit elimMVarDepsAux d; b ← visit elimMVarDepsAux b; pure (e.updateForallE! d b) | e@(Expr.lam _ d b _) => do d ← visit elimMVarDepsAux d; b ← visit elimMVarDepsAux b; pure (e.updateLambdaE! d b) | e@(Expr.letE _ t v b _) => do t ← visit elimMVarDepsAux t; v ← visit elimMVarDepsAux v; b ← visit elimMVarDepsAux b; pure (e.updateLet! t v b) | e@(Expr.mdata _ b _) => do b ← visit elimMVarDepsAux b; pure (e.updateMData! b) | e@(Expr.app _ _ _) => e.withApp $ fun f args => elimMVarDepsApp elimMVarDepsAux xs f args | e@(Expr.mvar mvarId _) => elimMVarDepsApp elimMVarDepsAux xs e #[] | e => pure e partial def elimMVarDeps (xs : Array Expr) (e : Expr) : M Expr := if !e.hasMVar then pure e else withFreshCache $ elimMVarDepsAux xs e /-- Similar to `Expr.abstractRange`, but handles metavariables correctly. It uses `elimMVarDeps` to ensure `e` and the type of the free variables `xs` do not contain a metavariable `?m` s.t. local context of `?m` contains a free variable in `xs`. `elimMVarDeps` is defined later in this file. -/ @[inline] private def abstractRange (xs : Array Expr) (i : Nat) (e : Expr) : M Expr := do e ← elimMVarDeps xs e; pure (e.abstractRange i xs) /-- Similar to `LocalContext.mkBinding`, but handles metavariables correctly. -/ @[specialize] def mkBinding (isLambda : Bool) (lctx : LocalContext) (xs : Array Expr) (e : Expr) : M Expr := do e ← abstractRange xs xs.size e; xs.size.foldRevM (fun i e => let x := xs.get! i; match lctx.getFVar! x with | LocalDecl.cdecl _ _ n type bi => do type ← abstractRange xs i type; if isLambda then pure $ Lean.mkLambda n bi type e else pure $ Lean.mkForall n bi type e | LocalDecl.ldecl _ _ n type value => do if e.hasLooseBVar 0 then do type ← abstractRange xs i type; value ← abstractRange xs i value; pure $ mkLet n type value e else pure e) e end MkBinding abbrev MkBindingM := ReaderT LocalContext MkBinding.M def mkBinding (isLambda : Bool) (xs : Array Expr) (e : Expr) : MkBindingM Expr := fun lctx => MkBinding.mkBinding isLambda lctx xs e @[inline] def mkLambda (xs : Array Expr) (e : Expr) : MkBindingM Expr := mkBinding true xs e @[inline] def mkForall (xs : Array Expr) (e : Expr) : MkBindingM Expr := mkBinding false xs e /-- `isWellFormed mctx lctx e` return true if - All locals in `e` are declared in `lctx` - All metavariables `?m` in `e` have a local context which is a subprefix of `lctx` or are assigned, and the assignment is well-formed. -/ partial def isWellFormed (mctx : MetavarContext) (lctx : LocalContext) : Expr → Bool | Expr.mdata _ e _ => isWellFormed e | Expr.proj _ _ e _ => isWellFormed e | e@(Expr.app f a _) => (e.hasExprMVar || e.hasFVar) && isWellFormed f && isWellFormed a | e@(Expr.lam _ d b _) => (e.hasExprMVar || e.hasFVar) && isWellFormed d && isWellFormed b | e@(Expr.forallE _ d b _) => (e.hasExprMVar || e.hasFVar) && isWellFormed d && isWellFormed b | e@(Expr.letE _ t v b _) => (e.hasExprMVar || e.hasFVar) && isWellFormed t && isWellFormed v && isWellFormed b | Expr.const _ _ _ => true | Expr.bvar _ _ => true | Expr.sort _ _ => true | Expr.lit _ _ => true | Expr.mvar mvarId _ => let mvarDecl := mctx.getDecl mvarId; if mvarDecl.lctx.isSubPrefixOf lctx then true else match mctx.getExprAssignment? mvarId with | none => false | some v => isWellFormed v | Expr.fvar fvarId _ => lctx.contains fvarId | Expr.localE _ _ _ _ => unreachable! end MetavarContext end Lean
13c505ab764b51c1a840c80fb510120b74d5abdd
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/linear_algebra/free_module/basic.lean
25c1a2c1321fecc9927e71a48f4c2801db69c00e
[ "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,325
lean
/- Copyright (c) 2021 Riccardo Brasca. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Riccardo Brasca -/ import linear_algebra.direct_sum.finsupp import logic.small.basic import linear_algebra.std_basis import linear_algebra.finsupp_vector_space /-! # Free modules We introduce a class `module.free R M`, for `R` a `semiring` and `M` an `R`-module and we provide several basic instances for this class. Use `finsupp.total_id_surjective` to prove that any module is the quotient of a free module. ## Main definition * `module.free R M` : the class of free `R`-modules. -/ universes u v w z variables {ι : Type*} (R : Type u) (M : Type v) (N : Type z) open_locale tensor_product direct_sum big_operators section basic variables [semiring R] [add_comm_monoid M] [module R M] /-- `module.free R M` is the statement that the `R`-module `M` is free.-/ class module.free : Prop := (exists_basis [] : nonempty (Σ (I : Type v), basis I R M)) /- If `M` fits in universe `w`, then freeness is equivalent to existence of a basis in that universe. Note that if `M` does not fit in `w`, the reverse direction of this implication is still true as `module.free.of_basis`. -/ lemma module.free_def [small.{w} M] : module.free R M ↔ ∃ (I : Type w), nonempty (basis I R M) := ⟨ λ h, ⟨shrink (set.range h.exists_basis.some.2), ⟨(basis.reindex_range h.exists_basis.some.2).reindex (equiv_shrink _)⟩⟩, λ h, ⟨(nonempty_sigma.2 h).map $ λ ⟨i, b⟩, ⟨set.range b, b.reindex_range⟩⟩⟩ lemma module.free_iff_set : module.free R M ↔ ∃ (S : set M), nonempty (basis S R M) := ⟨λ h, ⟨set.range h.exists_basis.some.2, ⟨basis.reindex_range h.exists_basis.some.2⟩⟩, λ ⟨S, hS⟩, ⟨nonempty_sigma.2 ⟨S, hS⟩⟩⟩ variables {R M} lemma module.free.of_basis {ι : Type w} (b : basis ι R M) : module.free R M := (module.free_def R M).2 ⟨set.range b, ⟨b.reindex_range⟩⟩ end basic namespace module.free section semiring variables (R M) [semiring R] [add_comm_monoid M] [module R M] [module.free R M] variables [add_comm_monoid N] [module R N] /-- If `module.free R M` then `choose_basis_index R M` is the `ι` which indexes the basis `ι → M`. -/ @[nolint has_nonempty_instance] def choose_basis_index := (exists_basis R M).some.1 /-- If `module.free R M` then `choose_basis : ι → M` is the basis. Here `ι = choose_basis_index R M`. -/ noncomputable def choose_basis : basis (choose_basis_index R M) R M := (exists_basis R M).some.2 /-- The isomorphism `M ≃ₗ[R] (choose_basis_index R M →₀ R)`. -/ noncomputable def repr : M ≃ₗ[R] (choose_basis_index R M →₀ R) := (choose_basis R M).repr /-- The universal property of free modules: giving a functon `(choose_basis_index R M) → N`, for `N` an `R`-module, is the same as giving an `R`-linear map `M →ₗ[R] N`. This definition is parameterized over an extra `semiring S`, such that `smul_comm_class R S M'` holds. If `R` is commutative, you can set `S := R`; if `R` is not commutative, you can recover an `add_equiv` by setting `S := ℕ`. See library note [bundled maps over different rings]. -/ noncomputable def constr {S : Type z} [semiring S] [module S N] [smul_comm_class R S N] : ((choose_basis_index R M) → N) ≃ₗ[S] M →ₗ[R] N := basis.constr (choose_basis R M) S @[priority 100] instance no_zero_smul_divisors [no_zero_divisors R] : no_zero_smul_divisors R M := let ⟨⟨_, b⟩⟩ := exists_basis R M in b.no_zero_smul_divisors variables {R M N} lemma of_equiv (e : M ≃ₗ[R] N) : module.free R N := of_basis $ (choose_basis R M).map e /-- A variation of `of_equiv`: the assumption `module.free R P` here is explicit rather than an instance. -/ lemma of_equiv' {P : Type v} [add_comm_monoid P] [module R P] (h : module.free R P) (e : P ≃ₗ[R] N) : module.free R N := of_equiv e variables (R M N) /-- The module structure provided by `semiring.to_module` is free. -/ instance self : module.free R R := of_basis (basis.singleton unit R) instance prod [module.free R N] : module.free R (M × N) := of_basis $ (choose_basis R M).prod (choose_basis R N) /-- The product of finitely many free modules is free. -/ instance pi (M : ι → Type*) [finite ι] [Π (i : ι), add_comm_monoid (M i)] [Π (i : ι), module R (M i)] [Π (i : ι), module.free R (M i)] : module.free R (Π i, M i) := let ⟨_⟩ := nonempty_fintype ι in by exactI (of_basis $ pi.basis $ λ i, choose_basis R (M i)) /-- The module of finite matrices is free. -/ instance matrix {m n : Type*} [finite m] [finite n] : module.free R (matrix m n M) := module.free.pi R _ variables (ι) /-- The product of finitely many free modules is free (non-dependent version to help with typeclass search). -/ instance function [finite ι] : module.free R (ι → M) := free.pi _ _ instance finsupp : module.free R (ι →₀ M) := of_basis (finsupp.basis $ λ i, choose_basis R M) variables {ι} @[priority 100] instance of_subsingleton [subsingleton N] : module.free R N := of_basis (basis.empty N : basis pempty R N) @[priority 100] instance of_subsingleton' [subsingleton R] : module.free R N := by letI := module.subsingleton R N; exact module.free.of_subsingleton R N instance dfinsupp {ι : Type*} (M : ι → Type*) [Π (i : ι), add_comm_monoid (M i)] [Π (i : ι), module R (M i)] [Π (i : ι), module.free R (M i)] : module.free R (Π₀ i, M i) := of_basis $ dfinsupp.basis $ λ i, choose_basis R (M i) instance direct_sum {ι : Type*} (M : ι → Type*) [Π (i : ι), add_comm_monoid (M i)] [Π (i : ι), module R (M i)] [Π (i : ι), module.free R (M i)] : module.free R (⨁ i, M i) := module.free.dfinsupp R M end semiring section comm_ring variables [comm_ring R] [add_comm_group M] [module R M] [module.free R M] variables [add_comm_group N] [module R N] [module.free R N] instance tensor : module.free R (M ⊗[R] N) := of_equiv' (of_equiv' (free.finsupp _ R _) (finsupp_tensor_finsupp' R _ _).symm) (tensor_product.congr (choose_basis R M).repr (choose_basis R N).repr).symm end comm_ring section division_ring variables [division_ring R] [add_comm_group M] [module R M] @[priority 100] instance of_division_ring : module.free R M := of_basis (basis.of_vector_space R M) end division_ring end module.free
09f4d38657d53ee44fea4672d7c54c5d573effeb
5883d9218e6f144e20eee6ca1dab8529fa1a97c0
/src/exp/subst/type.lean
fdabc5f70beab3e99366ce860f3071a251cd22c7
[]
no_license
spl/alpha-conversion-is-easy
0d035bc570e52a6345d4890e4d0c9e3f9b8126c1
ed937fe85d8495daffd9412a5524c77b9fcda094
refs/heads/master
1,607,649,280,020
1,517,380,240,000
1,517,380,240,000
52,174,747
4
0
null
1,456,052,226,000
1,456,001,163,000
Lean
UTF-8
Lean
false
false
873
lean
/- This files defines the substitution type `subst`. -/ import exp.type namespace acie ----------------------------------------------------------------- namespace exp ------------------------------------------------------------------ variables {V : Type} [decidable_eq V] -- Type of variable names variables {vs : Type → Type} [vset vs V] -- Type of variable name sets -- The type of a substitution @[reducible] def subst (X Y : vs V) : Type := ν∈ X → exp Y -- Lift a function to a substitution @[reducible] def subst.lift {X Y : vs V} : (X →ν Y) → subst X Y := function.comp var -- Identity substitution construction @[reducible] protected def subst.id (X : vs V) : subst X X := var end /- namespace -/ exp -------------------------------------------------------- end /- namespace -/ acie -------------------------------------------------------
38ad0d5213c6fc708b91254c812ade737478079e
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/combinatorics/young/semistandard_tableau.lean
993b41bef0539efa8a901361affafc7c63b1af60
[ "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
4,794
lean
/- Copyright (c) 2022 Jake Levinson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jake Levinson -/ import combinatorics.young.young_diagram /-! # Semistandard Young tableaux A semistandard Young tableau is a filling of a Young diagram by natural numbers, such that the entries are weakly increasing left-to-right along rows (i.e. for fixed `i`), and strictly-increasing top-to-bottom along columns (i.e. for fixed `j`). An example of an SSYT of shape `μ = [4, 2, 1]` is: ```text 0 0 0 2 1 1 2 ``` We represent an SSYT as a function `ℕ → ℕ → ℕ`, which is required to be zero for all pairs `(i, j) ∉ μ` and to satisfy the row-weak and column-strict conditions on `μ`. ## Main definitions - `ssyt (μ : young_diagram)` : semistandard Young tableaux of shape `μ`. There is a `has_coe_to_fun` instance such that `T i j` is value of the `(i, j)` entry of the SSYT `T`. - `ssyt.highest_weight (μ : young_diagram)`: the semistandard Young tableau whose `i`th row consists entirely of `i`s, for each `i`. ## Tags Semistandard Young tableau ## References <https://en.wikipedia.org/wiki/Young_tableau> -/ /-- A semistandard Young tableau (SSYT) is a filling of the cells of a Young diagram by natural numbers, such that the entries in each row are weakly increasing (left to right), and the entries in each column are strictly increasing (top to bottom). Here, an SSYT is represented as an unrestricted function `ℕ → ℕ → ℕ` that, for reasons of extensionality, is required to vanish outside `μ`. -/ structure ssyt (μ : young_diagram) := (entry : ℕ → ℕ → ℕ) (row_weak' : ∀ {i j1 j2 : ℕ}, j1 < j2 → (i, j2) ∈ μ → entry i j1 ≤ entry i j2) (col_strict' : ∀ {i1 i2 j : ℕ}, i1 < i2 → (i2, j) ∈ μ → entry i1 j < entry i2 j) (zeros' : ∀ {i j}, (i, j) ∉ μ → entry i j = 0) namespace ssyt instance fun_like {μ : young_diagram} : fun_like (ssyt μ) ℕ (λ _, ℕ → ℕ) := { coe := ssyt.entry, coe_injective' := λ T T' h, by { cases T, cases T', congr' } } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance {μ : young_diagram} : has_coe_to_fun (ssyt μ) (λ _, ℕ → ℕ → ℕ) := fun_like.has_coe_to_fun @[simp] lemma to_fun_eq_coe {μ : young_diagram} {T : ssyt μ} : T.entry = (T : ℕ → ℕ → ℕ) := rfl @[ext] theorem ext {μ : young_diagram} {T T' : ssyt μ} (h : ∀ i j, T i j = T' i j) : T = T' := fun_like.ext T T' (λ x, by { funext, apply h }) /-- Copy of an `ssyt μ` with a new `entry` equal to the old one. Useful to fix definitional equalities. -/ protected def copy {μ : young_diagram} (T : ssyt μ) (entry' : ℕ → ℕ → ℕ) (h : entry' = T) : ssyt μ := { entry := entry', row_weak' := λ _ _ _, h.symm ▸ T.row_weak', col_strict' := λ _ _ _, h.symm ▸ T.col_strict', zeros' := λ _ _, h.symm ▸ T.zeros' } @[simp] lemma coe_copy {μ : young_diagram} (T : ssyt μ) (entry' : ℕ → ℕ → ℕ) (h : entry' = T) : ⇑(T.copy entry' h) = entry' := rfl lemma copy_eq {μ : young_diagram} (T : ssyt μ) (entry' : ℕ → ℕ → ℕ) (h : entry' = T) : T.copy entry' h = T := fun_like.ext' h lemma row_weak {μ : young_diagram} (T : ssyt μ) {i j1 j2 : ℕ} (hj : j1 < j2) (hcell : (i, j2) ∈ μ) : T i j1 ≤ T i j2 := T.row_weak' hj hcell lemma col_strict {μ : young_diagram} (T : ssyt μ) {i1 i2 j : ℕ} (hi : i1 < i2) (hcell : (i2, j) ∈ μ) : T i1 j < T i2 j := T.col_strict' hi hcell lemma zeros {μ : young_diagram} (T : ssyt μ) {i j : ℕ} (not_cell : (i, j) ∉ μ) : T i j = 0 := T.zeros' not_cell lemma row_weak_of_le {μ : young_diagram} (T : ssyt μ) {i j1 j2 : ℕ} (hj : j1 ≤ j2) (cell : (i, j2) ∈ μ) : T i j1 ≤ T i j2 := by { cases eq_or_lt_of_le hj, subst h, exact T.row_weak h cell } lemma col_weak {μ : young_diagram} (T : ssyt μ) {i1 i2 j : ℕ} (hi : i1 ≤ i2) (cell : (i2, j) ∈ μ) : T i1 j ≤ T i2 j := by { cases eq_or_lt_of_le hi, subst h, exact le_of_lt (T.col_strict h cell) } /-- The "highest weight" SSYT of a given shape is has all i's in row i, for each i. -/ def highest_weight (μ : young_diagram) : ssyt μ := { entry := λ i j, if (i, j) ∈ μ then i else 0, row_weak' := λ i j1 j2 hj hcell, by rw [if_pos hcell, if_pos (μ.up_left_mem (by refl) (le_of_lt hj) hcell)], col_strict' := λ i1 i2 j hi hcell, by rwa [if_pos hcell, if_pos (μ.up_left_mem (le_of_lt hi) (by refl) hcell)], zeros' := λ i j not_cell, if_neg not_cell } @[simp] lemma highest_weight_apply {μ : young_diagram} {i j : ℕ} : highest_weight μ i j = if (i, j) ∈ μ then i else 0 := rfl instance {μ : young_diagram} : inhabited (ssyt μ) := ⟨ssyt.highest_weight μ⟩ end ssyt
214c66a18d074d73fa7f1846d193ca8f0b350923
856e2e1615a12f95b551ed48fa5b03b245abba44
/docs/tutorial/category_theory/calculating_colimits_in_Top.lean
5c9df7216803d9efd32218b64b086c0478d67210
[ "Apache-2.0" ]
permissive
pimsp/mathlib
8b77e1ccfab21703ba8fbe65988c7de7765aa0e5
913318ca9d6979686996e8d9b5ebf7e74aae1c63
refs/heads/master
1,669,812,465,182
1,597,133,610,000
1,597,133,610,000
281,890,685
1
0
null
1,595,491,577,000
1,595,491,576,000
null
UTF-8
Lean
false
false
3,637
lean
import topology.category.Top.limits import category_theory.limits.shapes import topology.instances.real /-! This file contains some demos of using the (co)limits API to do topology. -/ noncomputable theory open category_theory open category_theory.limits def R : Top := Top.of ℝ def I : Top := Top.of (set.Icc 0 1 : set ℝ) def pt : Top := Top.of unit section MappingCylinder -- Let's construct the mapping cylinder. def to_pt (X : Top) : X ⟶ pt := { to_fun := λ _, unit.star, continuous_to_fun := continuous_const } def I₀ : pt ⟶ I := { to_fun := λ _, ⟨(0 : ℝ), by norm_num [set.left_mem_Icc]⟩, continuous_to_fun := continuous_const } def I₁ : pt ⟶ I := { to_fun := λ _, ⟨(1 : ℝ), by norm_num [set.right_mem_Icc]⟩, continuous_to_fun := continuous_const } def cylinder (X : Top) : Top := prod X I -- To define a map to the cylinder, we give a map to each factor. -- `prod.lift` is a helper method, providing a wrapper around `limit.lift` for binary products. def cylinder₀ (X : Top) : X ⟶ cylinder X := prod.lift (𝟙 X) (to_pt X ≫ I₀) def cylinder₁ (X : Top) : X ⟶ cylinder X := prod.lift (𝟙 X) (to_pt X ≫ I₁) -- The mapping cylinder is the pushout of the diagram -- X -- ↙ ↘ -- Y (X x I) -- (`pushout` is implemented just as a wrapper around `colimit`) is def mapping_cylinder {X Y : Top} (f : X ⟶ Y) : Top := pushout f (cylinder₁ X) /-- We construct the map from `X` into the "bottom" of the mapping cylinder for `f : X ⟶ Y`, as the composition of the inclusion of `X` into the bottom of the cylinder `prod X I`, followed by the map `pushout.inr` of `prod X I` into `mapping_cylinder f`. -/ def mapping_cylinder₀ {X Y : Top} (f : X ⟶ Y) : X ⟶ mapping_cylinder f := cylinder₀ X ≫ pushout.inr /-- The mapping cone is defined as the pushout of ``` X ↙ ↘ (Cyl f) pt ``` (where the left arrow is `mapping_cylinder₀`). This makes it an iterated colimit; one could also define it in one step as the colimit of ``` -- X X -- ↙ ↘ ↙ ↘ -- Y (X x I) pt ``` -/ def mapping_cone {X Y : Top} (f : X ⟶ Y) : Top := pushout (mapping_cylinder₀ f) (to_pt X) -- TODO Hopefully someone will write a nice tactic for generating diagrams quickly, -- and we'll be able to verify that this iterated construction is the same as the colimit -- over a single diagram. end MappingCylinder section Gluing -- Here's two copies of the real line glued together at a point. def f : pt ⟶ R := { to_fun := λ _, (0 : ℝ), continuous_to_fun := continuous_const } /-- Two copies of the real line glued together at 0. -/ def X : Top := pushout f f -- To define a map out of it, we define maps out of each copy of the line, -- and check the maps agree at 0. def g : X ⟶ R := pushout.desc (𝟙 _) (𝟙 _) rfl end Gluing universes v u w section Products /-- The countably infinite product of copies of `ℝ`. -/ def Y : Top := ∏ (λ n : ℕ, R) /-- We can define a point in this infinite product by specifying its coordinates. Let's define the point whose `n`-th coordinate is `n + 1` (as a real number). -/ def q : pt ⟶ Y := pi.lift (λ (n : ℕ), ⟨λ (_ : pt), (n + 1 : ℝ), continuous_const⟩) -- "Looking under the hood", we see that `q` is a `subtype`, whose `val` is a function `unit → Y.α`. -- #check q.val -- q.val : pt.α → Y.α -- `q.property` is the fact this function is continuous (i.e. no content, since `pt` is a singleton) -- We can check that this function is definitionally just the function we specified. example : (q ()).val (9 : ℕ) = ((10 : ℕ) : ℝ) := rfl end Products
349863a6bb56ea3a3bcb90ddce1cb0c8a9f63d97
b7f22e51856f4989b970961f794f1c435f9b8f78
/hott/algebra/ordered_field.hlean
f05877ee6c02d8c3646353359e9a5b77ffcb037e
[ "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
20,339
hlean
/- Copyright (c) 2014 Robert Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Lewis -/ import algebra.ordered_ring algebra.field open eq eq.ops algebra set_option class.force_new true namespace algebra structure linear_ordered_field [class] (A : Type) extends linear_ordered_ring A, field A section linear_ordered_field variable {A : Type} variables [s : linear_ordered_field A] {a b c d : A} include s -- helpers for following theorem mul_zero_lt_mul_inv_of_pos (H : 0 < a) : a * 0 < a * (1 / a) := calc a * 0 = 0 : mul_zero ... < 1 : zero_lt_one ... = a * a⁻¹ : mul_inv_cancel (ne.symm (ne_of_lt H)) ... = a * (1 / a) : inv_eq_one_div theorem mul_zero_lt_mul_inv_of_neg (H : a < 0) : a * 0 < a * (1 / a) := calc a * 0 = 0 : mul_zero ... < 1 : zero_lt_one ... = a * a⁻¹ : mul_inv_cancel (ne_of_lt H) ... = a * (1 / a) : inv_eq_one_div theorem one_div_pos_of_pos (H : 0 < a) : 0 < 1 / a := lt_of_mul_lt_mul_left (mul_zero_lt_mul_inv_of_pos H) (le_of_lt H) theorem one_div_neg_of_neg (H : a < 0) : 1 / a < 0 := gt_of_mul_lt_mul_neg_left (mul_zero_lt_mul_inv_of_neg H) (le_of_lt H) theorem le_mul_of_ge_one_right (Hb : b ≥ 0) (H : a ≥ 1) : b ≤ b * a := mul_one _ ▸ (mul_le_mul_of_nonneg_left H Hb) theorem lt_mul_of_gt_one_right (Hb : b > 0) (H : a > 1) : b < b * a := mul_one _ ▸ (mul_lt_mul_of_pos_left H Hb) theorem one_le_div_iff_le (a : A) {b : A} (Hb : b > 0) : 1 ≤ a / b ↔ b ≤ a := have Hb' : b ≠ 0, from ne.symm (ne_of_lt Hb), iff.intro (assume H : 1 ≤ a / b, calc b = b : refl ... ≤ b * (a / b) : le_mul_of_ge_one_right (le_of_lt Hb) H ... = a : mul_div_cancel' Hb') (assume H : b ≤ a, have Hbinv : 1 / b > 0, from one_div_pos_of_pos Hb, calc 1 = b * (1 / b) : mul_one_div_cancel Hb' ... ≤ a * (1 / b) : mul_le_mul_of_nonneg_right H (le_of_lt Hbinv) ... = a / b : div_eq_mul_one_div) theorem le_of_one_le_div (Hb : b > 0) (H : 1 ≤ a / b) : b ≤ a := (iff.mp (!one_le_div_iff_le Hb)) H theorem one_le_div_of_le (Hb : b > 0) (H : b ≤ a) : 1 ≤ a / b := (iff.mpr (!one_le_div_iff_le Hb)) H theorem one_lt_div_iff_lt (a : A) {b : A} (Hb : b > 0) : 1 < a / b ↔ b < a := have Hb' : b ≠ 0, from ne.symm (ne_of_lt Hb), iff.intro (assume H : 1 < a / b, calc b < b * (a / b) : lt_mul_of_gt_one_right Hb H ... = a : mul_div_cancel' Hb') (assume H : b < a, have Hbinv : 1 / b > 0, from one_div_pos_of_pos Hb, calc 1 = b * (1 / b) : mul_one_div_cancel Hb' ... < a * (1 / b) : mul_lt_mul_of_pos_right H Hbinv ... = a / b : div_eq_mul_one_div) theorem lt_of_one_lt_div (Hb : b > 0) (H : 1 < a / b) : b < a := (iff.mp (!one_lt_div_iff_lt Hb)) H theorem one_lt_div_of_lt (Hb : b > 0) (H : b < a) : 1 < a / b := (iff.mpr (!one_lt_div_iff_lt Hb)) H theorem exists_lt (a : A) : Σ x, x < a := have H : a - 1 < a, from add_lt_of_le_of_neg (le.refl _) zero_gt_neg_one, sigma.mk _ H theorem exists_gt (a : A) : Σ x, x > a := have H : a + 1 > a, from lt_add_of_le_of_pos (le.refl _) zero_lt_one, sigma.mk _ H -- the following theorems amount to four iffs, for <, ≤, ≥, >. theorem mul_le_of_le_div (Hc : 0 < c) (H : a ≤ b / c) : a * c ≤ b := !div_mul_cancel (ne.symm (ne_of_lt Hc)) ▸ mul_le_mul_of_nonneg_right H (le_of_lt Hc) theorem le_div_of_mul_le (Hc : 0 < c) (H : a * c ≤ b) : a ≤ b / c := calc a = a * c * (1 / c) : !mul_mul_div (ne.symm (ne_of_lt Hc)) ... ≤ b * (1 / c) : mul_le_mul_of_nonneg_right H (le_of_lt (one_div_pos_of_pos Hc)) ... = b / c : div_eq_mul_one_div theorem mul_lt_of_lt_div (Hc : 0 < c) (H : a < b / c) : a * c < b := !div_mul_cancel (ne.symm (ne_of_lt Hc)) ▸ mul_lt_mul_of_pos_right H Hc theorem lt_div_of_mul_lt (Hc : 0 < c) (H : a * c < b) : a < b / c := calc a = a * c * (1 / c) : !mul_mul_div (ne.symm (ne_of_lt Hc)) ... < b * (1 / c) : mul_lt_mul_of_pos_right H (one_div_pos_of_pos Hc) ... = b / c : div_eq_mul_one_div theorem mul_le_of_div_le_of_neg (Hc : c < 0) (H : b / c ≤ a) : a * c ≤ b := !div_mul_cancel (ne_of_lt Hc) ▸ mul_le_mul_of_nonpos_right H (le_of_lt Hc) theorem div_le_of_mul_le_of_neg (Hc : c < 0) (H : a * c ≤ b) : b / c ≤ a := calc a = a * c * (1 / c) : !mul_mul_div (ne_of_lt Hc) ... ≥ b * (1 / c) : mul_le_mul_of_nonpos_right H (le_of_lt (one_div_neg_of_neg Hc)) ... = b / c : div_eq_mul_one_div theorem mul_lt_of_gt_div_of_neg (Hc : c < 0) (H : a > b / c) : a * c < b := !div_mul_cancel (ne_of_lt Hc) ▸ mul_lt_mul_of_neg_right H Hc theorem div_lt_of_mul_gt_of_neg (Hc : c < 0) (H : a * c < b) : b / c < a := calc a = a * c * (1 / c) : !mul_mul_div (ne_of_lt Hc) ... > b * (1 / c) : mul_lt_mul_of_neg_right H (one_div_neg_of_neg Hc) ... = b / c : div_eq_mul_one_div theorem div_le_of_le_mul (Hb : b > 0) (H : a ≤ b * c) : a / b ≤ c := calc a / b = a * (1 / b) : div_eq_mul_one_div ... ≤ (b * c) * (1 / b) : mul_le_mul_of_nonneg_right H (le_of_lt (one_div_pos_of_pos Hb)) ... = (b * c) / b : div_eq_mul_one_div ... = c : mul_div_cancel_left (ne.symm (ne_of_lt Hb)) theorem le_mul_of_div_le (Hc : c > 0) (H : a / c ≤ b) : a ≤ b * c := calc a = a / c * c : !div_mul_cancel (ne.symm (ne_of_lt Hc)) ... ≤ b * c : mul_le_mul_of_nonneg_right H (le_of_lt Hc) -- following these in the isabelle file, there are 8 biconditionals for the above with - signs -- skipping for now theorem mul_sub_mul_div_mul_neg (Hc : c ≠ 0) (Hd : d ≠ 0) (H : a / c < b / d) : (a * d - b * c) / (c * d) < 0 := have H1 : a / c - b / d < 0, from calc a / c - b / d < b / d - b / d : sub_lt_sub_right H ... = 0 : sub_self, calc 0 > a / c - b / d : H1 ... = (a * d - c * b) / (c * d) : !div_sub_div Hc Hd ... = (a * d - b * c) / (c * d) : mul.comm theorem mul_sub_mul_div_mul_nonpos (Hc : c ≠ 0) (Hd : d ≠ 0) (H : a / c ≤ b / d) : (a * d - b * c) / (c * d) ≤ 0 := have H1 : a / c - b / d ≤ 0, from calc a / c - b / d ≤ b / d - b / d : sub_le_sub_right H ... = 0 : sub_self, calc 0 ≥ a / c - b / d : H1 ... = (a * d - c * b) / (c * d) : !div_sub_div Hc Hd ... = (a * d - b * c) / (c * d) : mul.comm theorem div_lt_div_of_mul_sub_mul_div_neg (Hc : c ≠ 0) (Hd : d ≠ 0) (H : (a * d - b * c) / (c * d) < 0) : a / c < b / d := have H1 : (a * d - c * b) / (c * d) < 0, by rewrite [mul.comm c b]; exact H, have H2 : a / c - b / d < 0, by rewrite [!div_sub_div Hc Hd]; exact H1, have H3 : a / c - b / d + b / d < 0 + b / d, from add_lt_add_right H2 _, begin rewrite [zero_add at H3, sub_eq_add_neg at H3, neg_add_cancel_right at H3], exact H3 end theorem div_le_div_of_mul_sub_mul_div_nonpos (Hc : c ≠ 0) (Hd : d ≠ 0) (H : (a * d - b * c) / (c * d) ≤ 0) : a / c ≤ b / d := have H1 : (a * d - c * b) / (c * d) ≤ 0, by rewrite [mul.comm c b]; exact H, have H2 : a / c - b / d ≤ 0, by rewrite [!div_sub_div Hc Hd]; exact H1, have H3 : a / c - b / d + b / d ≤ 0 + b / d, from add_le_add_right H2 _, begin rewrite [zero_add at H3, sub_eq_add_neg at H3, neg_add_cancel_right at H3], exact H3 end theorem div_pos_of_pos_of_pos (Ha : 0 < a) (Hb : 0 < b) : 0 < a / b := begin rewrite div_eq_mul_one_div, apply mul_pos, exact Ha, apply one_div_pos_of_pos, exact Hb end theorem div_nonneg_of_nonneg_of_pos (Ha : 0 ≤ a) (Hb : 0 < b) : 0 ≤ a / b := begin rewrite div_eq_mul_one_div, apply mul_nonneg, exact Ha, apply le_of_lt, apply one_div_pos_of_pos, exact Hb end theorem div_neg_of_neg_of_pos (Ha : a < 0) (Hb : 0 < b) : a / b < 0:= begin rewrite div_eq_mul_one_div, apply mul_neg_of_neg_of_pos, exact Ha, apply one_div_pos_of_pos, exact Hb end theorem div_nonpos_of_nonpos_of_pos (Ha : a ≤ 0) (Hb : 0 < b) : a / b ≤ 0 := begin rewrite div_eq_mul_one_div, apply mul_nonpos_of_nonpos_of_nonneg, exact Ha, apply le_of_lt, apply one_div_pos_of_pos, exact Hb end theorem div_neg_of_pos_of_neg (Ha : 0 < a) (Hb : b < 0) : a / b < 0 := begin rewrite div_eq_mul_one_div, apply mul_neg_of_pos_of_neg, exact Ha, apply one_div_neg_of_neg, exact Hb end theorem div_nonpos_of_nonneg_of_neg (Ha : 0 ≤ a) (Hb : b < 0) : a / b ≤ 0 := begin rewrite div_eq_mul_one_div, apply mul_nonpos_of_nonneg_of_nonpos, exact Ha, apply le_of_lt, apply one_div_neg_of_neg, exact Hb end theorem div_pos_of_neg_of_neg (Ha : a < 0) (Hb : b < 0) : 0 < a / b := begin rewrite div_eq_mul_one_div, apply mul_pos_of_neg_of_neg, exact Ha, apply one_div_neg_of_neg, exact Hb end theorem div_nonneg_of_nonpos_of_neg (Ha : a ≤ 0) (Hb : b < 0) : 0 ≤ a / b := begin rewrite div_eq_mul_one_div, apply mul_nonneg_of_nonpos_of_nonpos, exact Ha, apply le_of_lt, apply one_div_neg_of_neg, exact Hb end theorem div_lt_div_of_lt_of_pos (H : a < b) (Hc : 0 < c) : a / c < b / c := begin rewrite [{a/c}div_eq_mul_one_div, {b/c}div_eq_mul_one_div], exact mul_lt_mul_of_pos_right H (one_div_pos_of_pos Hc) end theorem div_le_div_of_le_of_pos (H : a ≤ b) (Hc : 0 < c) : a / c ≤ b / c := begin rewrite [{a/c}div_eq_mul_one_div, {b/c}div_eq_mul_one_div], exact mul_le_mul_of_nonneg_right H (le_of_lt (one_div_pos_of_pos Hc)) end theorem div_lt_div_of_lt_of_neg (H : b < a) (Hc : c < 0) : a / c < b / c := begin rewrite [{a/c}div_eq_mul_one_div, {b/c}div_eq_mul_one_div], exact mul_lt_mul_of_neg_right H (one_div_neg_of_neg Hc) end theorem div_le_div_of_le_of_neg (H : b ≤ a) (Hc : c < 0) : a / c ≤ b / c := begin rewrite [{a/c}div_eq_mul_one_div, {b/c}div_eq_mul_one_div], exact mul_le_mul_of_nonpos_right H (le_of_lt (one_div_neg_of_neg Hc)) end theorem two_pos : (1 : A) + 1 > 0 := add_pos zero_lt_one zero_lt_one theorem one_add_one_ne_zero : 1 + 1 ≠ (0:A) := ne.symm (ne_of_lt two_pos) theorem two_ne_zero : 2 ≠ (0:A) := by unfold bit0; apply one_add_one_ne_zero theorem add_halves (a : A) : a / 2 + a / 2 = a := calc a / 2 + a / 2 = (a + a) / 2 : by rewrite div_add_div_same ... = (a * 1 + a * 1) / 2 : by rewrite mul_one ... = (a * (1 + 1)) / 2 : by rewrite left_distrib ... = (a * 2) / 2 : by rewrite one_add_one_eq_two ... = a : by rewrite [@mul_div_cancel A _ _ _ two_ne_zero] theorem sub_self_div_two (a : A) : a - a / 2 = a / 2 := by rewrite [-{a}add_halves at {1}, add_sub_cancel] theorem add_midpoint {a b : A} (H : a < b) : a + (b - a) / 2 < b := begin rewrite [-div_sub_div_same, sub_eq_add_neg, {b / 2 + _}add.comm, -add.assoc, -sub_eq_add_neg], apply add_lt_of_lt_sub_right, rewrite *sub_self_div_two, apply div_lt_div_of_lt_of_pos H two_pos end theorem div_two_sub_self (a : A) : a / 2 - a = - (a / 2) := by rewrite [-{a}add_halves at {2}, sub_add_eq_sub_sub, sub_self, zero_sub] theorem add_self_div_two (a : A) : (a + a) / 2 = a := symm (iff.mpr (!eq_div_iff_mul_eq (ne_of_gt (add_pos zero_lt_one zero_lt_one))) (by krewrite [left_distrib, *mul_one])) theorem two_ge_one : (2:A) ≥ 1 := calc (2:A) = 1+1 : one_add_one_eq_two ... ≥ 1+0 : add_le_add_left (le_of_lt zero_lt_one) ... = 1 : add_zero theorem mul_le_mul_of_mul_div_le (H : a * (b / c) ≤ d) (Hc : c > 0) : b * a ≤ d * c := begin rewrite [-mul_div_assoc at H, mul.comm b], apply le_mul_of_div_le Hc H end theorem div_two_lt_of_pos (H : a > 0) : a / (1 + 1) < a := have Ha : a / (1 + 1) > 0, from div_pos_of_pos_of_pos H (add_pos zero_lt_one zero_lt_one), calc a / (1 + 1) < a / (1 + 1) + a / (1 + 1) : lt_add_of_pos_left Ha ... = a : add_halves theorem div_mul_le_div_mul_of_div_le_div_pos {e : A} (Hb : b ≠ 0) (Hd : d ≠ 0) (H : a / b ≤ c / d) (He : e > 0) : a / (b * e) ≤ c / (d * e) := begin rewrite [!field.div_mul_eq_div_mul_one_div Hb (ne_of_gt He), !field.div_mul_eq_div_mul_one_div Hd (ne_of_gt He)], apply mul_le_mul_of_nonneg_right H, apply le_of_lt, apply one_div_pos_of_pos He end theorem exists_add_lt_prod_pos_of_lt (H : b < a) : Σ c : A, b + c < a × c > 0 := sigma.mk ((a - b) / (1 + 1)) (pair (have H2 : a + a > (b + b) + (a - b), from calc a + a > b + a : add_lt_add_right H ... = b + a + b - b : by rewrite add_sub_cancel ... = b + b + a - b : by rewrite add.right_comm ... = (b + b) + (a - b) : by rewrite add_sub, have H3 : (a + a) / 2 > ((b + b) + (a - b)) / 2, from div_lt_div_of_lt_of_pos H2 two_pos, by rewrite [one_add_one_eq_two, sub_eq_add_neg, add_self_div_two at H3, -div_add_div_same at H3, add_self_div_two at H3]; exact H3) (div_pos_of_pos_of_pos (iff.mpr !sub_pos_iff_lt H) two_pos)) theorem ge_of_forall_ge_sub {a b : A} (H : Π ε : A, ε > 0 → a ≥ b - ε) : a ≥ b := begin apply le_of_not_gt, intro Hb, cases exists_add_lt_prod_pos_of_lt Hb with [c, Hc], let Hc' := H c (prod.pr2 Hc), apply (not_le_of_gt (prod.pr1 Hc)) (iff.mpr !le_add_iff_sub_right_le Hc') end end linear_ordered_field structure discrete_linear_ordered_field [class] (A : Type) extends linear_ordered_field A, decidable_linear_ordered_comm_ring A := (inv_zero : inv zero = zero) section discrete_linear_ordered_field variable {A : Type} variables [s : discrete_linear_ordered_field A] {a b c : A} include s definition dec_eq_of_dec_lt : Π x y : A, decidable (x = y) := take x y, decidable.by_cases (assume H : x < y, decidable.inr (ne_of_lt H)) (assume H : ¬ x < y, decidable.by_cases (assume H' : y < x, decidable.inr (ne.symm (ne_of_lt H'))) (assume H' : ¬ y < x, decidable.inl (le.antisymm (le_of_not_gt H') (le_of_not_gt H)))) definition discrete_linear_ordered_field.to_discrete_field [trans_instance] : discrete_field A := ⦃ discrete_field, s, has_decidable_eq := dec_eq_of_dec_lt⦄ theorem pos_of_one_div_pos (H : 0 < 1 / a) : 0 < a := have H1 : 0 < 1 / (1 / a), from one_div_pos_of_pos H, have H2 : 1 / a ≠ 0, from (assume H3 : 1 / a = 0, have H4 : 1 / (1 / a) = 0, from H3⁻¹ ▸ !div_zero, absurd H4 (ne.symm (ne_of_lt H1))), (division_ring.one_div_one_div (ne_zero_of_one_div_ne_zero H2)) ▸ H1 theorem neg_of_one_div_neg (H : 1 / a < 0) : a < 0 := have H1 : 0 < - (1 / a), from neg_pos_of_neg H, have Ha : a ≠ 0, from ne_zero_of_one_div_ne_zero (ne_of_lt H), have H2 : 0 < 1 / (-a), from (division_ring.one_div_neg_eq_neg_one_div Ha)⁻¹ ▸ H1, have H3 : 0 < -a, from pos_of_one_div_pos H2, neg_of_neg_pos H3 theorem le_of_one_div_le_one_div (H : 0 < a) (Hl : 1 / a ≤ 1 / b) : b ≤ a := have Hb : 0 < b, from pos_of_one_div_pos (calc 0 < 1 / a : one_div_pos_of_pos H ... ≤ 1 / b : Hl), have H' : 1 ≤ a / b, from (calc 1 = a / a : div_self (ne.symm (ne_of_lt H)) ... = a * (1 / a) : div_eq_mul_one_div ... ≤ a * (1 / b) : mul_le_mul_of_nonneg_left Hl (le_of_lt H) ... = a / b : div_eq_mul_one_div ), le_of_one_le_div Hb H' theorem le_of_one_div_le_one_div_of_neg (H : b < 0) (Hl : 1 / a ≤ 1 / b) : b ≤ a := have Ha : a ≠ 0, from ne_of_lt (neg_of_one_div_neg (calc 1 / a ≤ 1 / b : Hl ... < 0 : one_div_neg_of_neg H)), have H' : -b > 0, from neg_pos_of_neg H, have Hl' : - (1 / b) ≤ - (1 / a), from neg_le_neg Hl, have Hl'' : 1 / - b ≤ 1 / - a, from calc 1 / -b = - (1 / b) : by rewrite [division_ring.one_div_neg_eq_neg_one_div (ne_of_lt H)] ... ≤ - (1 / a) : Hl' ... = 1 / -a : by rewrite [division_ring.one_div_neg_eq_neg_one_div Ha], le_of_neg_le_neg (le_of_one_div_le_one_div H' Hl'') theorem lt_of_one_div_lt_one_div (H : 0 < a) (Hl : 1 / a < 1 / b) : b < a := have Hb : 0 < b, from pos_of_one_div_pos (calc 0 < 1 / a : one_div_pos_of_pos H ... < 1 / b : Hl), have H : 1 < a / b, from (calc 1 = a / a : div_self (ne.symm (ne_of_lt H)) ... = a * (1 / a) : div_eq_mul_one_div ... < a * (1 / b) : mul_lt_mul_of_pos_left Hl H ... = a / b : div_eq_mul_one_div), lt_of_one_lt_div Hb H theorem lt_of_one_div_lt_one_div_of_neg (H : b < 0) (Hl : 1 / a < 1 / b) : b < a := have H1 : b ≤ a, from le_of_one_div_le_one_div_of_neg H (le_of_lt Hl), have Hn : b ≠ a, from (assume Hn' : b = a, have Hl' : 1 / a = 1 / b, from Hn' ▸ refl _, absurd Hl' (ne_of_lt Hl)), lt_of_le_of_ne H1 Hn theorem one_div_lt_one_div_of_lt (Ha : 0 < a) (H : a < b) : 1 / b < 1 / a := lt_of_not_ge (assume H', absurd H (not_lt_of_ge (le_of_one_div_le_one_div Ha H'))) theorem one_div_le_one_div_of_le (Ha : 0 < a) (H : a ≤ b) : 1 / b ≤ 1 / a := le_of_not_gt (assume H', absurd H (not_le_of_gt (lt_of_one_div_lt_one_div Ha H'))) theorem one_div_lt_one_div_of_lt_of_neg (Hb : b < 0) (H : a < b) : 1 / b < 1 / a := lt_of_not_ge (assume H', absurd H (not_lt_of_ge (le_of_one_div_le_one_div_of_neg Hb H'))) theorem one_div_le_one_div_of_le_of_neg (Hb : b < 0) (H : a ≤ b) : 1 / b ≤ 1 / a := le_of_not_gt (assume H', absurd H (not_le_of_gt (lt_of_one_div_lt_one_div_of_neg Hb H'))) theorem one_lt_one_div (H1 : 0 < a) (H2 : a < 1) : 1 < 1 / a := one_div_one ▸ one_div_lt_one_div_of_lt H1 H2 theorem one_le_one_div (H1 : 0 < a) (H2 : a ≤ 1) : 1 ≤ 1 / a := one_div_one ▸ one_div_le_one_div_of_le H1 H2 theorem one_div_lt_neg_one (H1 : a < 0) (H2 : -1 < a) : 1 / a < -1 := one_div_neg_one_eq_neg_one ▸ one_div_lt_one_div_of_lt_of_neg H1 H2 theorem one_div_le_neg_one (H1 : a < 0) (H2 : -1 ≤ a) : 1 / a ≤ -1 := one_div_neg_one_eq_neg_one ▸ one_div_le_one_div_of_le_of_neg H1 H2 theorem div_lt_div_of_pos_of_lt_of_pos (Hb : 0 < b) (H : b < a) (Hc : 0 < c) : c / a < c / b := begin apply iff.mp !sub_neg_iff_lt, rewrite [div_eq_mul_one_div, {c / b}div_eq_mul_one_div, -mul_sub_left_distrib], apply mul_neg_of_pos_of_neg, exact Hc, apply iff.mpr !sub_neg_iff_lt, apply one_div_lt_one_div_of_lt, repeat assumption end theorem div_mul_le_div_mul_of_div_le_div_pos' {d e : A} (H : a / b ≤ c / d) (He : e > 0) : a / (b * e) ≤ c / (d * e) := begin rewrite [2 div_mul_eq_div_mul_one_div], apply mul_le_mul_of_nonneg_right H, apply le_of_lt, apply one_div_pos_of_pos He end theorem abs_one_div (a : A) : abs (1 / a) = 1 / abs a := if H : a > 0 then by rewrite [abs_of_pos H, abs_of_pos (one_div_pos_of_pos H)] else (if H' : a < 0 then by rewrite [abs_of_neg H', abs_of_neg (one_div_neg_of_neg H'), -(division_ring.one_div_neg_eq_neg_one_div (ne_of_lt H'))] else have Heq : a = 0, from eq_of_le_of_ge (le_of_not_gt H) (le_of_not_gt H'), by rewrite [Heq, div_zero, *abs_zero, div_zero]) theorem sign_eq_div_abs (a : A) : sign a = a / (abs a) := decidable.by_cases (suppose a = 0, by subst a; rewrite [zero_div, sign_zero]) (suppose a ≠ 0, have abs a ≠ 0, from assume H, this (eq_zero_of_abs_eq_zero H), !eq_div_of_mul_eq this !eq_sign_mul_abs⁻¹) end discrete_linear_ordered_field end algebra
8c78f5d421d7d4bfdea304e3b149c1002e75c931
46125763b4dbf50619e8846a1371029346f4c3db
/src/analysis/convex/specific_functions.lean
68b6ced1a717525488e6668789fcccd10281044e
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
4,325
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 analysis.calculus.mean_value data.nat.parity analysis.complex.exponential /-! # Collection of convex functions In this file we prove that the following functions are convex: * `convex_on_exp` : the exponential function is convex on $(-∞, +∞)$; * `convex_on_pow_of_even` : given an even natural number $n$, the function $f(x)=x^n$ is convex on $(-∞, +∞)$; * `convex_on_pow` : for a natural $n$, the function $f(x)=x^n$ is convex on $[0, +∞)$; * `convex_on_fpow` : for an integer $m$, the function $f(x)=x^m$ is convex on $(0, +∞)$. ## TODO * `convex_on_rpow : ∀ r : ℝ, (r ≤ 0 ∨ 1 ≤ r) → convex_on (Ioi 0) (λ x, x ^ r)` -/ open real set /-- `exp` is convex on the whole real line -/ lemma convex_on_exp : convex_on univ exp := convex_on_univ_of_deriv2_nonneg differentiable_exp (deriv_exp.symm ▸ differentiable_exp) (assume x, (iter_deriv_exp 2).symm ▸ le_of_lt (exp_pos x)) /-- `x^n`, `n : ℕ` is convex on the whole real line whenever `n` is even -/ lemma convex_on_pow_of_even {n : ℕ} (hn : n.even) : convex_on set.univ (λ x, x^n) := begin apply convex_on_univ_of_deriv2_nonneg differentiable_pow, { simp only [deriv_pow', differentiable.mul, differentiable_const, differentiable_pow] }, { intro x, rcases hn.sub (nat.even_bit0 1) with ⟨k, hk⟩, simp only [iter_deriv_pow, finset.prod_range_succ, finset.prod_range_zero, nat.sub_zero, mul_one, hk, pow_mul', pow_two], exact mul_nonneg (nat.cast_nonneg _) (mul_self_nonneg _) } end /-- `x^n`, `n : ℕ` is convex on `[0, +∞)` for all `n` -/ lemma convex_on_pow (n : ℕ) : convex_on (Ici 0) (λ x, x^n) := begin apply convex_on_of_deriv2_nonneg (convex_Ici _) (continuous_pow n).continuous_on; simp only [interior_Ici, differentiable_on_pow, deriv_pow', differentiable_on_const, differentiable_on.mul, iter_deriv_pow], intros x hx, exact mul_nonneg (nat.cast_nonneg _) (pow_nonneg (le_of_lt hx) _) end lemma finset.prod_nonneg_of_card_nonpos_even {α β : Type*} [decidable_eq α] [linear_ordered_comm_ring β] {f : α → β} [decidable_pred (λ x, f x ≤ 0)] {s : finset α} (h0 : (s.filter (λ x, f x ≤ 0)).card.even) : 0 ≤ s.prod f := calc 0 ≤ s.prod (λ x, (if f x ≤ 0 then (-1:β) else 1) * f x) : finset.prod_nonneg (λ x _, by { split_ifs with hx hx, by simp [hx], simp at hx ⊢, exact le_of_lt hx }) ... = _ : by rw [finset.prod_mul_distrib, finset.prod_ite _ _ (λx,x), finset.prod_const_one, mul_one, finset.prod_const, neg_one_pow_eq_pow_mod_two, nat.even_iff.1 h0, pow_zero, one_mul]; -- TODO: why Lean fails to find this instance in `prod_ite`? apply_instance lemma int_prod_range_nonneg (m : ℤ) (n : ℕ) (hn : n.even) : 0 ≤ (finset.range n).prod (λ k, m - k) := begin cases (le_or_lt ↑n m) with hnm hmn, { exact finset.prod_nonneg (λ k hk, sub_nonneg.2 (le_trans (int.coe_nat_le.2 $ le_of_lt $ finset.mem_range.1 hk) hnm)) }, cases le_or_lt 0 m with hm hm, { lift m to ℕ using hm, exact le_of_eq (eq.symm $ finset.prod_eq_zero (finset.mem_range.2 $ int.coe_nat_lt.1 hmn) (sub_self _)) }, clear hmn, apply finset.prod_nonneg_of_card_nonpos_even, convert hn, convert finset.card_range n, ext k, simp only [finset.mem_filter, finset.mem_range], refine ⟨and.left, λ hk, ⟨hk, sub_nonpos.2 $ le_trans (le_of_lt hm) _⟩⟩, exact int.coe_nat_nonneg k end /-- `x^m`, `m : ℤ` is convex on `(0, +∞)` for all `m` -/ lemma convex_on_fpow (m : ℤ) : convex_on (Ioi 0) (λ x, x^m) := begin apply convex_on_of_deriv2_nonneg (convex_Ioi 0); try { rw [interior_Ioi] }, { exact (differentiable_on_fpow $ lt_irrefl _).continuous_on }, { exact differentiable_on_fpow (lt_irrefl _) }, { have : eq_on (deriv (λx:ℝ, x^m)) (λx, ↑m * x^(m-1)) (Ioi 0), from λ x hx, deriv_fpow (ne_of_gt hx), refine (differentiable_on_congr this).2 _, exact (differentiable_on_fpow (lt_irrefl _)).const_mul _ }, { intros x hx, simp only [iter_deriv_fpow (ne_of_gt hx)], refine mul_nonneg (int.cast_nonneg.2 _) (fpow_nonneg_of_nonneg (le_of_lt hx) _), exact int_prod_range_nonneg _ _ (nat.even_bit0 1) } end
255cc547f00ca6ac84325fe89578c59c42368e7c
9c2e8d73b5c5932ceb1333265f17febc6a2f0a39
/src/KT/KT_defs.lean
ec0593a39f7d9976dce4167b2d4fe53f857119d7
[ "MIT" ]
permissive
minchaowu/ModalTab
2150392108dfdcaffc620ff280a8b55fe13c187f
9bb0bf17faf0554d907ef7bdd639648742889178
refs/heads/master
1,626,266,863,244
1,592,056,874,000
1,592,056,874,000
153,314,364
12
1
null
null
null
null
UTF-8
Lean
false
false
14,113
lean
/- Copyright (c) 2018-2019 Minchao Wu. All rights reserved. Released under MIT license as described in the file LICENSE. Author: Minchao Wu -/ import defs open nnf tactic meta def frame_restriction : tactic unit := do intro `a >> `[simp] structure KT (states : Type) extends kripke states := (refl : reflexive rel . frame_restriction) instance inhabited_KT : inhabited (KT ℕ) := ⟨{ val := λ a b, tt, rel := λ a b, tt }⟩ @[simp] def force {states : Type} (k : KT states) : states → nnf → Prop | s (var n) := k.val n s | s (neg n) := ¬ k.val n s | s (and φ ψ) := force s φ ∧ force s ψ | s (or φ ψ) := force s φ ∨ force s ψ | s (box φ) := ∀ s', k.rel s s' → force s' φ | s (dia φ) := ∃ s', k.rel s s' ∧ force s' φ def sat {st} (k : KT st) (s) (Γ : list nnf) : Prop := ∀ φ ∈ Γ, force k s φ def unsatisfiable (Γ : list nnf) : Prop := ∀ (st) (k : KT st) s, ¬ sat k s Γ theorem unsat_singleton {φ} : unsatisfiable [φ] → ∀ (st) (k : KT st) s, ¬ force k s φ := begin intro h, intros, intro hf, apply h, intros ψ hψ, rw list.mem_singleton at hψ, rw hψ, exact hf end theorem sat_of_empty {st} (k : KT st) (s) : sat k s [] := λ φ h, absurd h $ list.not_mem_nil _ theorem ne_empty_of_unsat {Γ} (h : unsatisfiable Γ): Γ ≠ [] := begin intro heq, rw heq at h, apply h, apply sat_of_empty, exact nat, apply inhabited_KT.1, exact 0 end inductive model | cons : list ℕ → list model → model instance : decidable_eq model := by tactic.mk_dec_eq_instance open model @[simp] def mval : ℕ → model → bool | p (cons v r) := p ∈ v @[simp] def mrel : model → model → bool | m₁@(cons v r) m₂ := m₂ ∈ r ∨ m₁ = m₂ theorem refl_mrel (s : model) : mrel s s := by cases s with v r; simp theorem mem_of_mrel_tt : Π {v r m}, mrel (cons v r) m = tt → m ∈ r ∨ cons v r = m := begin intros v r m h, by_cases hc : cons v r = m, {right, exact hc},{left, by_contradiction hn, have : mrel (cons v r) m = ff, { simp [hc, hn] }, rw h at this, contradiction} end -- TODO : make this neater. Currently it's just a copy-paste theorem mem_of_mrel_empty : Π {v m}, mrel (cons v []) m = tt → cons v [] = m := begin intros v m h, by_cases hc : cons v [] = m, {exact hc}, {by_contradiction hn, have : mrel (cons v []) m = ff, { simp [hc, hn] }, rw h at this, contradiction} end @[simp] def builder : KT model := {val := λ n s, mval n s, rel := λ s₁ s₂, mrel s₁ s₂, refl := refl_mrel} theorem force_box_of_leaf {v φ} (h : force builder (cons v []) φ): force builder (cons v []) (box φ) := begin dsimp, intros s' hs', have : cons v [] = s', {apply mem_of_mrel_empty, exact hs'}, rw ←this, assumption end def srefl (m h : list nnf) := ∀ {v l φ}, sat builder (cons v l) m → box φ ∈ h → (∀ ψ, box ψ ∈ h → ∀ m ∈ l, force builder m ψ) → force builder (cons v l) φ structure seqt : Type := (main : list nnf) (hdld : list nnf) -- srefl main hdld says that sequent hdld | main satisfies theorem 3.7(2) (pmain : srefl main hdld) -- box_only says there are only boxed formulas in hdld (phdld : box_only hdld) class val_constructible (Γ : seqt) := (satu : saturated Γ.main) (no_box_main : ∀ {φ}, box φ ∉ Γ.main) (no_contra_main : ∀ {n}, var n ∈ Γ.main → neg n ∉ Γ.main) (v : list ℕ) (hv : ∀ n, var n ∈ Γ.main ↔ n ∈ v) class modal_applicable (Γ : seqt) extends val_constructible Γ := (φ : nnf) (ex : dia φ ∈ Γ.main) class model_constructible (Γ : seqt) extends val_constructible Γ := (no_dia : ∀ {φ}, nnf.dia φ ∉ Γ.main) theorem build_model : Π Γ (h : model_constructible Γ), sat builder (cons h.v []) Γ.main := begin intros, intro, intro hmem, cases heq : φ, case nnf.var : n {dsimp, have := h.hv, rw heq at hmem, rw this at hmem, simp [hmem]}, case nnf.neg : n {dsimp, have h₁ := h.hv, rw heq at hmem, have := h.no_contra_main, simp, rw ←h₁, intro hvar, apply this, swap, exact hmem, exact hvar}, case nnf.box : ψ {dsimp, intros, have := h.no_box_main, exfalso, rw heq at hmem, exact this hmem}, case nnf.and : φ ψ { rw heq at hmem, have := h.satu.no_and, have := @this φ ψ, contradiction}, case nnf.or : φ ψ { rw heq at hmem, have := h.satu.no_or, have := @this φ ψ, contradiction}, case nnf.dia : φ { rw heq at hmem, have := h.no_dia, have := @this φ, contradiction}, end /- Regular lemmas for the propositional part. -/ section variables (φ ψ : nnf) (Γ₁ Γ₂ Δ Λ: list nnf) {st : Type} variables (k : KT st) (s : st) open list theorem sat_subset (h₁ : Γ₁ ⊆ Γ₂) (h₂ : sat k s Γ₂) : sat k s Γ₁ := λ x hx, h₂ _ (h₁ hx) theorem sat_sublist (h₁ : Γ₁ <+ Γ₂) (h₂ :sat k s Γ₂) : sat k s Γ₁ := sat_subset _ _ _ _ (sublist.subset h₁) h₂ theorem sat_append (h₁ : sat k s Γ₁) (h₂ : sat k s Γ₂) : sat k s (Γ₁ ++ Γ₂) := begin intros φ h, rw mem_append at h, cases h, apply h₁ _ h, apply h₂ _ h end theorem unsat_contra {Δ n} : var n ∈ Δ → neg n ∈ Δ → unsatisfiable Δ:= begin intros h₁ h₂, intros v hsat, intros s hsat, have := hsat _ h₁, have := hsat _ h₂, simpa end theorem unsat_contra_seqt {Δ : seqt} {n} : var n ∈ Δ.main → neg n ∈ Δ.main → unsatisfiable (Δ.main ++ Δ.hdld):= begin intros h₁ h₂, intros st m, intros s hsat, have := unsat_contra h₁ h₂, have := this _ m s, apply this, apply sat_subset _ _ _ _ _ hsat, simp end theorem sat_of_and : force k s (and φ ψ) ↔ (force k s φ) ∧ (force k s ψ) := by split; {intro, simpa} theorem sat_of_sat_erase (h₁ : sat k s $ Δ.erase φ) (h₂ : force k s φ) : sat k s Δ := begin intro ψ, intro h, by_cases (ψ = φ), {rw h, assumption}, {have : ψ ∈ Δ.erase φ, rw mem_erase_of_ne, assumption, exact h, apply h₁, assumption} end theorem unsat_and_of_unsat_split (h₁ : and φ ψ ∈ Δ) (h₂ : unsatisfiable $ φ :: ψ :: Δ.erase (and φ ψ)) : unsatisfiable Δ := begin intro st, intros, intro h, apply h₂, swap 3, exact k, swap, exact s, intro e, intro he, cases he, {rw he, have := h _ h₁, rw sat_of_and at this, exact this.1}, {cases he, {rw he, have := h _ h₁, rw sat_of_and at this, exact this.2}, {have := h _ h₁, apply h, apply mem_of_mem_erase he} } end theorem unsat_and_of_unsat_split_seqt {Γ} (h₁ : and φ ψ ∈ Δ) (h₂ : unsatisfiable $ (φ :: ψ :: Δ.erase (and φ ψ)++Γ)) : unsatisfiable (Δ++Γ) := begin intro st, intros, intro h, apply h₂, swap 3, exact k, swap, exact s, intro e, intro he, cases he, {rw he, have := h _ (mem_append_left _ h₁), rw sat_of_and at this, exact this.1}, {cases he, {rw he, have := h _ (mem_append_left _ h₁), rw sat_of_and at this, exact this.2}, {have := h _ (mem_append_left _ h₁), apply h, apply mem_of_mem_erase, rw erase_append_left, exact he, exact h₁} } end theorem sat_and_of_sat_split (h₁ : and φ ψ ∈ Δ) (h₂ : sat k s $ φ :: ψ :: Δ.erase (and φ ψ)) : sat k s Δ := begin intro e, intro he, by_cases (e = and φ ψ), { rw h, dsimp, split, repeat {apply h₂, simp} }, { have : e ∈ Δ.erase (and φ ψ), { rw mem_erase_of_ne, repeat { assumption } }, apply h₂, simp [this] } end theorem sat_and_of_sat_split_seqt {Γ} (h₁ : and φ ψ ∈ Δ) (h₂ : sat k s $ (φ :: ψ :: Δ.erase (and φ ψ)++Γ)) : sat k s (Δ++Γ) := begin intro e, intro he, by_cases (e = and φ ψ), { rw h, dsimp, split, repeat {apply h₂, simp} }, { have : e ∈ Δ.erase (and φ ψ) ++ Γ, { rw ←erase_append_left, rw mem_erase_of_ne, repeat {assumption} }, apply h₂, simp [this] } end theorem sat_split_of_sat_and_seqt {Γ} (h₁ : and φ ψ ∈ Δ) (h₂ : sat k s (Δ++Γ)) : sat k s $ (φ :: ψ :: Δ.erase (and φ ψ)++Γ) := begin intros e he, rw mem_append at he, cases he, have : force k s (and φ ψ), {apply h₂, simp [h₁]}, rw sat_of_and at this, {cases he, {rw he, exact this.left}, {cases he, rw he, exact this.right, apply h₂, rw mem_append, left, apply mem_of_mem_erase he} }, {apply h₂, rw mem_append, right, exact he} end theorem unsat_or_of_unsat_split_seqt {Γ} (h : or φ ψ ∈ Δ) (h₁ : unsatisfiable $ (φ :: Δ.erase (nnf.or φ ψ)++Γ)) (h₂ : unsatisfiable $ (ψ :: Δ.erase (nnf.or φ ψ)++Γ)) : unsatisfiable $ (Δ++Γ) := begin intro, intros, intro hsat, have := hsat _ (mem_append_left _ h), dsimp at this, cases this, {apply h₁, swap 3, exact k, swap, exact s, intro e, intro he, cases he, rw he, exact this, apply hsat, apply mem_of_mem_erase, rw erase_append_left, exact he, exact h}, {apply h₂, swap 3, exact k, swap, exact s, intro e, intro he, cases he, rw he, exact this, apply hsat, apply mem_of_mem_erase, rw erase_append_left, exact he, exact h} end theorem sat_or_of_sat_split_left (h : or φ ψ ∈ Δ) (hl : sat k s $ φ :: Δ.erase (nnf.or φ ψ)) : sat k s Δ := begin intros e he, by_cases (e = or φ ψ), { rw h, dsimp, left, apply hl, simp}, {have : e ∈ Δ.erase (or φ ψ), { rw mem_erase_of_ne, repeat { assumption } }, apply hl, simp [this]} end theorem sat_or_of_sat_split_right (h : or φ ψ ∈ Δ) (hl : sat k s $ ψ :: Δ.erase (nnf.or φ ψ)) : sat k s Δ := begin intros e he, by_cases (e = or φ ψ), { rw h, dsimp, right, apply hl, simp}, { have : e ∈ Δ.erase (or φ ψ), { rw mem_erase_of_ne, repeat { assumption } }, apply hl, simp [this] } end /- KT-specific lemmas -/ theorem force_of_force_box (h : force k s $ box φ) : force k s φ := begin dsimp at h, apply h, apply k.refl end theorem unsat_copy_of_unsat_box (h₁ : box φ ∈ Δ) (h₂ : unsatisfiable $ (φ :: Δ.erase (box φ)) ++ box φ :: Λ) : unsatisfiable (Δ ++ Λ) := begin intros st k s h, apply h₂, swap 3, exact k, swap, exact s, intros e he, rw [mem_append] at he, cases he, {cases he, {rw he, apply force_of_force_box, apply h (box φ), simp [h₁]}, {apply h, rw mem_append, left, apply mem_of_mem_erase he }}, {cases he, {rw ←he at h₁, apply h, rw mem_append, left, exact h₁}, {apply h, rw mem_append, right, assumption}} end theorem sat_copy_of_sat_box (h₁ : box φ ∈ Δ) (h₂ : sat k s $ (φ :: Δ.erase (box φ)) ++ box φ :: Λ) : sat k s (Δ ++ Λ) := begin intros ψ hφ, rw mem_append at hφ, cases hφ, {by_cases heq : ψ = box φ, {rw heq, apply h₂ (box φ), simp}, {have := mem_erase_of_ne heq, rw ←this at hφ, apply h₂, simp, right, left, exact hφ}}, {apply h₂, simp, repeat {right}, exact hφ} end theorem sat_box_of_sat_copy (h₁ : box φ ∈ Δ) (h₂ : sat k s (Δ ++ Λ)) : sat k s $ (φ :: Δ.erase (box φ)) ++ box φ :: Λ := begin apply sat_append, {intros ψ h, cases h, have : sat k s Δ, {apply sat_subset Δ (Δ++Λ), simp, assumption}, have := this _ h₁, apply force_of_force_box, rw h, exact this, apply h₂, have := mem_of_mem_erase h, simp [this]}, {intros ψ h, cases h, rw h, have := h₂ (box φ), apply this, simp [h₁], apply h₂, rw mem_append, right, exact h} end end def and_child {φ ψ} (Γ : seqt) (h : nnf.and φ ψ ∈ Γ.main) : seqt := ⟨φ :: ψ :: Γ.main.erase (and φ ψ), Γ.hdld, begin intros k s γ hsat hin hall, by_cases heq : γ = and φ ψ, {rw heq, split, apply hsat, simp, apply hsat, simp}, {apply Γ.pmain _ hin hall, apply sat_and_of_sat_split _ _ _ _ _ h hsat} end, Γ.phdld⟩ inductive and_instance_seqt (Γ : seqt) : seqt → Type | cons : Π {φ ψ} (h : nnf.and φ ψ ∈ Γ.main), and_instance_seqt $ and_child Γ h def or_child_left {φ ψ} (Γ : seqt) (h : nnf.or φ ψ ∈ Γ.main) : seqt := ⟨φ :: Γ.main.erase (or φ ψ), Γ.hdld, begin intros k s γ hsat hin hall, by_cases heq : γ = or φ ψ, {rw heq, dsimp, left, apply hsat, simp}, {apply Γ.pmain, apply sat_or_of_sat_split_left, exact h, exact hsat, exact hin, exact hall} end, Γ.phdld⟩ def or_child_right {φ ψ} (Γ : seqt) (h : nnf.or φ ψ ∈ Γ.main) : seqt := ⟨ψ :: Γ.main.erase (or φ ψ), Γ.hdld, begin intros k s γ hsat hin hall, by_cases heq : γ = or φ ψ, {rw heq, dsimp, right, apply hsat, simp}, {apply Γ.pmain, apply sat_or_of_sat_split_right, exact h, exact hsat, exact hin, exact hall} end, Γ.phdld⟩ inductive or_instance_seqt (Γ : seqt) : seqt → seqt → Type | cons : Π {φ ψ} (h : nnf.or φ ψ ∈ Γ.main), or_instance_seqt (or_child_left Γ h) (or_child_right Γ h) def box_child {φ} (Γ : seqt) (h : nnf.box φ ∈ Γ.main) : seqt := ⟨φ :: Γ.main.erase (box φ), box φ :: Γ.hdld, begin intros k s γ hsat hin hall, cases hin, {simp at hin, rw hin, apply hsat, simp}, {apply Γ.pmain, intros ψ hψ, by_cases heq : ψ = box φ, {rw heq, dsimp, intros s' hs', have := mem_of_mrel_tt hs', cases this, {apply hall, simp, assumption}, {rw ←this, apply hsat, simp} }, {apply hsat, right, have := list.mem_erase_of_ne heq, swap, exact Γ.main, rw ←this at hψ, assumption}, {assumption},{intros, apply hall, simp [a], exact H} } end, cons_box_only Γ.phdld⟩ inductive copy_instance_seqt (Γ : seqt) : seqt → Type | cons : Π {φ} (h : nnf.box φ ∈ Γ.main), copy_instance_seqt $ box_child Γ h theorem build_model_seqt : Π Γ (h : model_constructible Γ), sat builder (cons h.v []) (Γ.main ++ Γ.hdld) := begin intros Γ h, apply sat_append, apply build_model, intros ψ hψ, have := box_only_ex Γ.phdld hψ, cases this with w hw, rw hw, apply force_box_of_leaf, apply Γ.pmain _ _, {intros ψ hin m hm, exfalso, apply list.not_mem_nil, exact hm}, {apply build_model}, {rw hw at hψ, assumption} end
f85482d7da79cb5d7e5983c7925bdd2331d1898e
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/data/support.lean
1ae9169b9dbd834f789a8d4309a52ceb77bba1fb
[ "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
10,037
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 order.conditionally_complete_lattice import algebra.big_operators.basic import algebra.group.prod import algebra.group.pi import algebra.module.pi /-! # Support of a function In this file we define `function.support f = {x | f x ≠ 0}` and prove its basic properties. We also define `function.mul_support f = {x | f x ≠ 1}`. -/ open set open_locale big_operators namespace function variables {α β A B M N P R S G M₀ G₀ : Type*} {ι : Sort*} section has_one variables [has_one M] [has_one N] [has_one P] /-- `support` of a function is the set of points `x` such that `f x ≠ 0`. -/ def support [has_zero A] (f : α → A) : set α := {x | f x ≠ 0} /-- `mul_support` of a function is the set of points `x` such that `f x ≠ 1`. -/ @[to_additive] def mul_support (f : α → M) : set α := {x | f x ≠ 1} @[to_additive] lemma nmem_mul_support {f : α → M} {x : α} : x ∉ mul_support f ↔ f x = 1 := not_not @[to_additive] lemma compl_mul_support {f : α → M} : (mul_support f)ᶜ = {x | f x = 1} := ext $ λ x, nmem_mul_support @[simp, to_additive] lemma mem_mul_support {f : α → M} {x : α} : x ∈ mul_support f ↔ f x ≠ 1 := iff.rfl @[simp, to_additive] lemma mul_support_subset_iff {f : α → M} {s : set α} : mul_support f ⊆ s ↔ ∀ x, f x ≠ 1 → x ∈ s := iff.rfl @[to_additive] lemma mul_support_subset_iff' {f : α → M} {s : set α} : mul_support f ⊆ s ↔ ∀ x ∉ s, f x = 1 := forall_congr $ λ x, not_imp_comm @[simp, to_additive] lemma mul_support_eq_empty_iff {f : α → M} : mul_support f = ∅ ↔ f = 1 := by { simp_rw [← subset_empty_iff, mul_support_subset_iff', funext_iff], simp } @[simp, to_additive] lemma mul_support_one' : mul_support (1 : α → M) = ∅ := mul_support_eq_empty_iff.2 rfl @[simp, to_additive] lemma mul_support_one : mul_support (λ x : α, (1 : M)) = ∅ := mul_support_one' @[to_additive] lemma mul_support_binop_subset (op : M → N → P) (op1 : op 1 1 = 1) (f : α → M) (g : α → N) : mul_support (λ x, op (f x) (g x)) ⊆ mul_support f ∪ mul_support g := λ x hx, classical.by_cases (λ hf : f x = 1, or.inr $ λ hg, hx $ by simp only [hf, hg, op1]) or.inl @[to_additive] lemma mul_support_sup [semilattice_sup M] (f g : α → M) : mul_support (λ x, f x ⊔ g x) ⊆ mul_support f ∪ mul_support g := mul_support_binop_subset (⊔) sup_idem f g @[to_additive] lemma mul_support_inf [semilattice_inf M] (f g : α → M) : mul_support (λ x, f x ⊓ g x) ⊆ mul_support f ∪ mul_support g := mul_support_binop_subset (⊓) inf_idem f g @[to_additive] lemma mul_support_max [linear_order M] (f g : α → M) : mul_support (λ x, max (f x) (g x)) ⊆ mul_support f ∪ mul_support g := mul_support_sup f g @[to_additive] lemma mul_support_min [linear_order M] (f g : α → M) : mul_support (λ x, min (f x) (g x)) ⊆ mul_support f ∪ mul_support g := mul_support_inf f g @[to_additive] lemma mul_support_supr [conditionally_complete_lattice M] [nonempty ι] (f : ι → α → M) : mul_support (λ x, ⨆ i, f i x) ⊆ ⋃ i, mul_support (f i) := begin rw mul_support_subset_iff', simp only [mem_Union, not_exists, nmem_mul_support], intros x hx, simp only [hx, csupr_const] end @[to_additive] lemma mul_support_infi [conditionally_complete_lattice M] [nonempty ι] (f : ι → α → M) : mul_support (λ x, ⨅ i, f i x) ⊆ ⋃ i, mul_support (f i) := @mul_support_supr _ (order_dual M) ι ⟨(1:M)⟩ _ _ f @[to_additive] lemma mul_support_comp_subset {g : M → N} (hg : g 1 = 1) (f : α → M) : mul_support (g ∘ f) ⊆ mul_support f := λ x, mt $ λ h, by simp only [(∘), *] @[to_additive] lemma mul_support_subset_comp {g : M → N} (hg : ∀ {x}, g x = 1 → x = 1) (f : α → M) : mul_support f ⊆ mul_support (g ∘ f) := λ x, mt hg @[to_additive] lemma mul_support_comp_eq (g : M → N) (hg : ∀ {x}, g x = 1 ↔ x = 1) (f : α → M) : mul_support (g ∘ f) = mul_support f := set.ext $ λ x, not_congr hg @[to_additive] lemma mul_support_comp_eq_preimage (g : β → M) (f : α → β) : mul_support (g ∘ f) = f ⁻¹' mul_support g := rfl @[to_additive support_prod_mk] lemma mul_support_prod_mk (f : α → M) (g : α → N) : mul_support (λ x, (f x, g x)) = mul_support f ∪ mul_support g := set.ext $ λ x, by simp only [mul_support, not_and_distrib, mem_union_eq, mem_set_of_eq, prod.mk_eq_one, ne.def] @[to_additive support_prod_mk'] lemma mul_support_prod_mk' (f : α → M × N) : mul_support f = mul_support (λ x, (f x).1) ∪ mul_support (λ x, (f x).2) := by simp only [← mul_support_prod_mk, prod.mk.eta] end has_one @[to_additive] lemma mul_support_mul [monoid M] (f g : α → M) : mul_support (λ x, f x * g x) ⊆ mul_support f ∪ mul_support g := mul_support_binop_subset (*) (one_mul _) f g @[simp, to_additive] lemma mul_support_inv [group G] (f : α → G) : mul_support (λ x, (f x)⁻¹) = mul_support f := set.ext $ λ x, not_congr inv_eq_one @[simp, to_additive support_neg'] lemma mul_support_inv'' [group G] (f : α → G) : mul_support (f⁻¹) = mul_support f := mul_support_inv f @[simp] lemma mul_support_inv' [group_with_zero G₀] (f : α → G₀) : mul_support (λ x, (f x)⁻¹) = mul_support f := set.ext $ λ x, not_congr inv_eq_one' @[to_additive] lemma mul_support_mul_inv [group G] (f g : α → G) : mul_support (λ x, f x * (g x)⁻¹) ⊆ mul_support f ∪ mul_support g := mul_support_binop_subset (λ a b, a * b⁻¹) (by simp) f g @[to_additive support_sub] lemma mul_support_group_div [group G] (f g : α → G) : mul_support (λ x, f x / g x) ⊆ mul_support f ∪ mul_support g := mul_support_binop_subset (/) (by simp only [one_div, one_inv]) f g lemma mul_support_div [group_with_zero G₀] (f g : α → G₀) : mul_support (λ x, f x / g x) ⊆ mul_support f ∪ mul_support g := mul_support_binop_subset (/) (by simp only [div_one]) f g @[simp] lemma support_mul [mul_zero_class R] [no_zero_divisors R] (f g : α → R) : support (λ x, f x * g x) = support f ∩ support g := set.ext $ λ x, by simp only [mem_support, mul_ne_zero_iff, mem_inter_eq, not_or_distrib] lemma support_smul_subset_right [add_monoid A] [monoid B] [distrib_mul_action B A] (b : B) (f : α → A) : support (b • f) ⊆ support f := λ x hbf hf, hbf $ by rw [pi.smul_apply, hf, smul_zero] lemma support_smul_subset_left [semiring R] [add_comm_monoid M] [semimodule R M] (f : α → R) (g : α → M) : support (f • g) ⊆ support f := λ x hfg hf, hfg $ by rw [pi.smul_apply', hf, zero_smul] lemma support_smul [semiring R] [add_comm_monoid M] [semimodule R M] [no_zero_smul_divisors R M] (f : α → R) (g : α → M) : support (f • g) = support f ∩ support g := ext $ λ x, smul_ne_zero @[simp] lemma support_inv [group_with_zero G₀] (f : α → G₀) : support (λ x, (f x)⁻¹) = support f := set.ext $ λ x, not_congr inv_eq_zero @[simp] lemma support_div [group_with_zero G₀] (f g : α → G₀) : support (λ x, f x / g x) = support f ∩ support g := by simp [div_eq_mul_inv] @[to_additive] lemma mul_support_prod [comm_monoid M] (s : finset α) (f : α → β → M) : mul_support (λ x, ∏ i in s, f i x) ⊆ ⋃ i ∈ s, mul_support (f i) := begin rw mul_support_subset_iff', simp only [mem_Union, not_exists, nmem_mul_support], exact λ x, finset.prod_eq_one end lemma support_prod_subset [comm_monoid_with_zero A] (s : finset α) (f : α → β → A) : support (λ x, ∏ i in s, f i x) ⊆ ⋂ i ∈ s, support (f i) := λ x hx, mem_bInter_iff.2 $ λ i hi H, hx $ finset.prod_eq_zero hi H lemma support_prod [comm_monoid_with_zero A] [no_zero_divisors A] [nontrivial A] (s : finset α) (f : α → β → A) : support (λ x, ∏ i in s, f i x) = ⋂ i ∈ s, support (f i) := set.ext $ λ x, by simp only [support, ne.def, finset.prod_eq_zero_iff, mem_set_of_eq, set.mem_Inter, not_exists] lemma mul_support_one_add [has_one R] [add_left_cancel_monoid R] (f : α → R) : mul_support (λ x, 1 + f x) = support f := set.ext $ λ x, not_congr add_right_eq_self lemma mul_support_one_add' [has_one R] [add_left_cancel_monoid R] (f : α → R) : mul_support (1 + f) = support f := mul_support_one_add f lemma mul_support_add_one [has_one R] [add_right_cancel_monoid R] (f : α → R) : mul_support (λ x, f x + 1) = support f := set.ext $ λ x, not_congr add_left_eq_self lemma mul_support_add_one' [has_one R] [add_right_cancel_monoid R] (f : α → R) : mul_support (f + 1) = support f := mul_support_add_one f lemma mul_support_one_sub' [has_one R] [add_group R] (f : α → R) : mul_support (1 - f) = support f := by rw [sub_eq_add_neg, mul_support_one_add', support_neg'] lemma mul_support_one_sub [has_one R] [add_group R] (f : α → R) : mul_support (λ x, 1 - f x) = support f := mul_support_one_sub' f end function namespace pi variables {A : Type*} {B : Type*} [decidable_eq A] [has_zero B] {a : A} {b : B} lemma support_single_zero : function.support (pi.single a (0 : B)) = ∅ := by simp @[simp] lemma support_single_of_ne (h : b ≠ 0) : function.support (pi.single a b) = {a} := begin ext, simp only [mem_singleton_iff, ne.def, function.mem_support], split, { contrapose!, exact λ h', single_eq_of_ne h' b }, { rintro rfl, rw single_eq_same, exact h } end lemma support_single [decidable_eq B] : function.support (pi.single a b) = if b = 0 then ∅ else {a} := by { split_ifs with h; simp [h] } lemma support_single_subset : function.support (pi.single a b) ⊆ {a} := begin classical, rw support_single, split_ifs; simp end lemma support_single_disjoint {b' : B} (hb : b ≠ 0) (hb' : b' ≠ 0) {i j : A} : disjoint (function.support (single i b)) (function.support (single j b')) ↔ i ≠ j := by simpa [support_single, hb, hb'] using ne_comm end pi
519827b3d199d5b43b79b19e9e611128e690e85b
d0c6b2ba2af981e9ab0a98f6e169262caad4b9b9
/src/Std/Data/AssocList.lean
c4d1f6eb5345f8cec9b8b63c2b07b98302133e71
[ "Apache-2.0" ]
permissive
fizruk/lean4
953b7dcd76e78c17a0743a2c1a918394ab64bbc0
545ed50f83c570f772ade4edbe7d38a078cbd761
refs/heads/master
1,677,655,987,815
1,612,393,885,000
1,612,393,885,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,107
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ universes u v w w' namespace Std /- List-like type to avoid extra level of indirection -/ inductive AssocList (α : Type u) (β : Type v) where | nil : AssocList α β | cons (key : α) (value : β) (tail : AssocList α β) : AssocList α β deriving Inhabited namespace AssocList variable {α : Type u} {β : Type v} {δ : Type w} {m : Type w → Type w} [Monad m] abbrev empty : AssocList α β := nil instance : EmptyCollection (AssocList α β) := ⟨empty⟩ abbrev insert (m : AssocList α β) (k : α) (v : β) : AssocList α β := m.cons k v def isEmpty : AssocList α β → Bool | nil => true | _ => false @[specialize] def foldlM (f : δ → α → β → m δ) : (init : δ) → AssocList α β → m δ | d, nil => pure d | d, cons a b es => do let d ← f d a b foldlM f d es @[inline] def foldl (f : δ → α → β → δ) (init : δ) (as : AssocList α β) : δ := Id.run (foldlM f init as) def mapKey (f : α → δ) : AssocList α β → AssocList δ β | nil => nil | cons k v t => cons (f k) v (mapKey f t) def mapVal (f : β → δ) : AssocList α β → AssocList α δ | nil => nil | cons k v t => cons k (f v) (mapVal f t) def findEntry? [BEq α] (a : α) : AssocList α β → Option (α × β) | nil => none | cons k v es => match k == a with | true => some (k, v) | false => findEntry? a es def find? [BEq α] (a : α) : AssocList α β → Option β | nil => none | cons k v es => match k == a with | true => some v | false => find? a es def contains [BEq α] (a : α) : AssocList α β → Bool | nil => false | cons k v es => k == a || contains a es def replace [BEq α] (a : α) (b : β) : AssocList α β → AssocList α β | nil => nil | cons k v es => match k == a with | true => cons a b es | false => cons k v (replace a b es) def erase [BEq α] (a : α) : AssocList α β → AssocList α β | nil => nil | cons k v es => match k == a with | true => es | false => cons k v (erase a es) def any (p : α → β → Bool) : AssocList α β → Bool | nil => false | cons k v es => p k v || any p es def all (p : α → β → Bool) : AssocList α β → Bool | nil => true | cons k v es => p k v && all p es @[inline] def forIn {α : Type u} {β : Type v} {δ : Type w} {m : Type w → Type w'} [Monad m] (as : AssocList α β) (init : δ) (f : (α × β) → δ → m (ForInStep δ)) : m δ := let rec @[specialize] loop | d, nil => pure d | d, cons k v es => do match (← f (k, v) d) with | ForInStep.done d => pure d | ForInStep.yield d => loop d es loop init as end Std.AssocList def List.toAssocList {α : Type u} {β : Type v} : List (α × β) → Std.AssocList α β | [] => Std.AssocList.nil | (a,b) :: es => Std.AssocList.cons a b (toAssocList es)
384cb39c89d03638e7dfc7da837ac67a2028fc36
f4bff2062c030df03d65e8b69c88f79b63a359d8
/src/game/sup_inf/level01.lean
8c7f226030f2487f1d8f43e70e0ffae77e5dde17
[ "Apache-2.0" ]
permissive
adastra7470/real-number-game
776606961f52db0eb824555ed2f8e16f92216ea3
f9dcb7d9255a79b57e62038228a23346c2dc301b
refs/heads/master
1,669,221,575,893
1,594,669,800,000
1,594,669,800,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,088
lean
import game.order.level09 import data.real.basic -- imports the real numbers ℝ namespace xena -- hide -- World name : Sup and Inf /- # Chapter 3 : Sup and Inf ## Level 1 : Upper bounds -/ /- Let $X$ be a set of real numbers. We say a real number $b$ is an *upper bound* for $X$ if every $x \in X$ is at most $b$. -/ definition is_upper_bound (S : set ℝ) (x : ℝ) := ∀ s ∈ S, s ≤ x /- Here is an easy fact about upper bounds, which we shall prove below: If $X \subseteq Y$ are two sets of reals, and $b$ is an upper bound for $Y$, then it's also an upper bound for $X$. You can prove this easily in Lean using the `change` tactic. -/ /- Lemma If $X \subseteq Y$ are two sets of reals, and $b$ is an upper bound for $Y$, then it's also an upper bound for $X$. -/ lemma upper_bounds_mono (X Y : set ℝ) (h1 : X ⊆ Y) (b : ℝ) : is_upper_bound Y b → is_upper_bound X b := begin intro h2, intro a, intro ha, apply h2, change ∀ a, a ∈ X → a ∈ Y at h1, -- unfold has_subset.subset set.subset at h1, apply h1, exact ha, end end xena -- hide
404e0ce307dd27cee70d23933352f9682c278316
3dc4623269159d02a444fe898d33e8c7e7e9461b
/.github/workflows/geo/V.lean
4f7a4c78aa6e8cfd58ae1f046f98edaa1ddda3da
[]
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
1,680
lean
import .global universe u local notation `Ring` := CommRing.{u} local notation `Set` := Type u open CommRing namespace V_definition variables (R : Ring) def V_obj (S : set R)(A : Ring) : Type u := { ζ : ring_hom R A | ∀ x ∈ S, ζ (x) = 0} lemma mem_V_obj (R : Ring) (S : set R)(A : Ring)(ζ: ring_hom R A) : (∀ x ∈ S, ζ (x) = 0) → V_obj(R)(S)(A) := begin intros h, exact {val := ζ, property := h}, end lemma V_mem (S :set R)(A :Ring)(ζ : V_obj(R)(S)(A)) : ∀ x ∈ S, ζ.val x = 0 := begin exact ζ.property, end @[ext]lemma ext ( R : Ring) (S : set R)(A : Ring)(ζ1 ζ2 : V_obj R S A) : ζ1.val = ζ2.val → ζ1 = ζ2 := begin intro h, cases ζ1, cases ζ2, congr ; try { assumption }, end def V_map (S : set R)(A B : Ring) (ψ : A ⟶ B) : V_obj(R)(S)(A) → V_obj(R)(S)(B) := λ ζ, begin have Hypx : ∀ x ∈ S, ψ (ζ.val x) = 0, intros x h, rw V_mem, exact ψ.map_zero, assumption, exact {val := (ring_hom.comp ψ ( ζ.val )), property := Hypx}, end lemma comp_val (S : set R)(A B : Ring) (ψ : A ⟶ B)(ζ : V_obj R S A) : ((V_map R S A B ψ) ζ).val = ring_hom.comp ψ ζ.val := rfl lemma one_val (S : set R)(A : Ring)(ζ : V_obj R S A) : ((V_map R S A A (𝟙A)) ζ).val = ζ.val := begin rw comp_val, ext, rw [ring_hom.comp_apply], exact rfl, end def V (S : set R) : Ring ⥤ Type u := begin exact { obj := λ A : Ring, V_obj R S A, map := λ A B : Ring, λ ψ : A ⟶ B, V_map R S A B ψ, map_id' := λ A : Ring, begin funext, ext, rw one_val, exact rfl, end } end end V_definition
5a58cc0a0fe1945eaaa533ca3f19eb49fe60c5cf
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/homotopy/path.lean
438d71e2d45d3e7abca67cbad8bef02210b4e2d3
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
11,339
lean
/- Copyright (c) 2021 Shing Tak Lam. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Shing Tak Lam -/ import topology.homotopy.basic import topology.path_connected import analysis.convex.basic /-! # Homotopy between paths > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file, we define a `homotopy` between two `path`s. In addition, we define a relation `homotopic` on `path`s, and prove that it is an equivalence relation. ## Definitions * `path.homotopy p₀ p₁` is the type of homotopies between paths `p₀` and `p₁` * `path.homotopy.refl p` is the constant homotopy between `p` and itself * `path.homotopy.symm F` is the `path.homotopy p₁ p₀` defined by reversing the homotopy * `path.homotopy.trans F G`, where `F : path.homotopy p₀ p₁`, `G : path.homotopy p₁ p₂` is the `path.homotopy p₀ p₂` defined by putting the first homotopy on `[0, 1/2]` and the second on `[1/2, 1]` * `path.homotopy.hcomp F G`, where `F : path.homotopy p₀ q₀` and `G : path.homotopy p₁ q₁` is a `path.homotopy (p₀.trans p₁) (q₀.trans q₁)` * `path.homotopic p₀ p₁` is the relation saying that there is a homotopy between `p₀` and `p₁` * `path.homotopic.setoid x₀ x₁` is the setoid on `path`s from `path.homotopic` * `path.homotopic.quotient x₀ x₁` is the quotient type from `path x₀ x₀` by `path.homotopic.setoid` -/ universes u v variables {X : Type u} {Y : Type v} [topological_space X] [topological_space Y] variables {x₀ x₁ x₂ x₃ : X} noncomputable theory open_locale unit_interval namespace path /-- The type of homotopies between two paths. -/ abbreviation homotopy (p₀ p₁ : path x₀ x₁) := continuous_map.homotopy_rel p₀.to_continuous_map p₁.to_continuous_map {0, 1} namespace homotopy section variables {p₀ p₁ : path x₀ x₁} instance : has_coe_to_fun (homotopy p₀ p₁) (λ _, I × I → X) := ⟨λ F, F.to_fun⟩ lemma coe_fn_injective : @function.injective (homotopy p₀ p₁) (I × I → X) coe_fn := continuous_map.homotopy_with.coe_fn_injective @[simp] lemma source (F : homotopy p₀ p₁) (t : I) : F (t, 0) = x₀ := begin simp_rw [←p₀.source], apply continuous_map.homotopy_rel.eq_fst, simp, end @[simp] lemma target (F : homotopy p₀ p₁) (t : I) : F (t, 1) = x₁ := begin simp_rw [←p₁.target], apply continuous_map.homotopy_rel.eq_snd, simp, end /-- Evaluating a path homotopy at an intermediate point, giving us a `path`. -/ def eval (F : homotopy p₀ p₁) (t : I) : path x₀ x₁ := { to_fun := F.to_homotopy.curry t, source' := by simp, target' := by simp } @[simp] lemma eval_zero (F : homotopy p₀ p₁) : F.eval 0 = p₀ := begin ext t, simp [eval], end @[simp] lemma eval_one (F : homotopy p₀ p₁) : F.eval 1 = p₁ := begin ext t, simp [eval], end end section variables {p₀ p₁ p₂ : path x₀ x₁} /-- Given a path `p`, we can define a `homotopy p p` by `F (t, x) = p x` -/ @[simps] def refl (p : path x₀ x₁) : homotopy p p := continuous_map.homotopy_rel.refl p.to_continuous_map {0, 1} /-- Given a `homotopy p₀ p₁`, we can define a `homotopy p₁ p₀` by reversing the homotopy. -/ @[simps] def symm (F : homotopy p₀ p₁) : homotopy p₁ p₀ := continuous_map.homotopy_rel.symm F @[simp] lemma symm_symm (F : homotopy p₀ p₁) : F.symm.symm = F := continuous_map.homotopy_rel.symm_symm F /-- Given `homotopy p₀ p₁` and `homotopy p₁ p₂`, we can define a `homotopy p₀ p₂` by putting the first homotopy on `[0, 1/2]` and the second on `[1/2, 1]`. -/ def trans (F : homotopy p₀ p₁) (G : homotopy p₁ p₂) : homotopy p₀ p₂ := continuous_map.homotopy_rel.trans F G lemma trans_apply (F : homotopy p₀ p₁) (G : homotopy p₁ p₂) (x : I × I) : (F.trans G) x = if h : (x.1 : ℝ) ≤ 1/2 then F (⟨2 * x.1, (unit_interval.mul_pos_mem_iff zero_lt_two).2 ⟨x.1.2.1, h⟩⟩, x.2) else G (⟨2 * x.1 - 1, unit_interval.two_mul_sub_one_mem_iff.2 ⟨(not_le.1 h).le, x.1.2.2⟩⟩, x.2) := continuous_map.homotopy_rel.trans_apply _ _ _ lemma symm_trans (F : homotopy p₀ p₁) (G : homotopy p₁ p₂) : (F.trans G).symm = G.symm.trans F.symm := continuous_map.homotopy_rel.symm_trans _ _ /-- Casting a `homotopy p₀ p₁` to a `homotopy q₀ q₁` where `p₀ = q₀` and `p₁ = q₁`. -/ @[simps] def cast {p₀ p₁ q₀ q₁ : path x₀ x₁} (F : homotopy p₀ p₁) (h₀ : p₀ = q₀) (h₁ : p₁ = q₁) : homotopy q₀ q₁ := continuous_map.homotopy_rel.cast F (congr_arg _ h₀) (congr_arg _ h₁) end section variables {p₀ q₀ : path x₀ x₁} {p₁ q₁ : path x₁ x₂} /-- Suppose `p₀` and `q₀` are paths from `x₀` to `x₁`, `p₁` and `q₁` are paths from `x₁` to `x₂`. Furthermore, suppose `F : homotopy p₀ q₀` and `G : homotopy p₁ q₁`. Then we can define a homotopy from `p₀.trans p₁` to `q₀.trans q₁`. -/ def hcomp (F : homotopy p₀ q₀) (G : homotopy p₁ q₁) : homotopy (p₀.trans p₁) (q₀.trans q₁) := { to_fun := λ x, if (x.2 : ℝ) ≤ 1/2 then (F.eval x.1).extend (2 * x.2) else (G.eval x.1).extend (2 * x.2 - 1), continuous_to_fun := begin refine continuous_if_le (continuous_induced_dom.comp continuous_snd) continuous_const (F.to_homotopy.continuous.comp (by continuity)).continuous_on (G.to_homotopy.continuous.comp (by continuity)).continuous_on _, intros x hx, norm_num [hx] end, map_zero_left' := λ x, by norm_num [path.trans], map_one_left' := λ x, by norm_num [path.trans], prop' := begin rintros x t ht, cases ht, { rw ht, simp }, { rw set.mem_singleton_iff at ht, rw ht, norm_num } end } lemma hcomp_apply (F : homotopy p₀ q₀) (G : homotopy p₁ q₁) (x : I × I) : F.hcomp G x = if h : (x.2 : ℝ) ≤ 1/2 then F.eval x.1 ⟨2 * x.2, (unit_interval.mul_pos_mem_iff zero_lt_two).2 ⟨x.2.2.1, h⟩⟩ else G.eval x.1 ⟨2 * x.2 - 1, unit_interval.two_mul_sub_one_mem_iff.2 ⟨(not_le.1 h).le, x.2.2.2⟩⟩ := show ite _ _ _ = _, by split_ifs; exact path.extend_extends _ _ lemma hcomp_half (F : homotopy p₀ q₀) (G : homotopy p₁ q₁) (t : I) : F.hcomp G (t, ⟨1/2, by norm_num, by norm_num⟩) = x₁ := show ite _ _ _ = _, by norm_num end /-- Suppose `p` is a path, then we have a homotopy from `p` to `p.reparam f` by the convexity of `I`. -/ def reparam (p : path x₀ x₁) (f : I → I) (hf : continuous f) (hf₀ : f 0 = 0) (hf₁ : f 1 = 1) : homotopy p (p.reparam f hf hf₀ hf₁) := { to_fun := λ x, p ⟨σ x.1 * x.2 + x.1 * f x.2, show (σ x.1 : ℝ) • (x.2 : ℝ) + (x.1 : ℝ) • (f x.2 : ℝ) ∈ I, from convex_Icc _ _ x.2.2 (f x.2).2 (by unit_interval) (by unit_interval) (by simp)⟩, map_zero_left' := λ x, by norm_num, map_one_left' := λ x, by norm_num, prop' := λ t x hx, begin cases hx, { rw hx, norm_num [hf₀] }, { rw set.mem_singleton_iff at hx, rw hx, norm_num [hf₁] } end } /-- Suppose `F : homotopy p q`. Then we have a `homotopy p.symm q.symm` by reversing the second argument. -/ @[simps] def symm₂ {p q : path x₀ x₁} (F : p.homotopy q) : p.symm.homotopy q.symm := { to_fun := λ x, F ⟨x.1, σ x.2⟩, map_zero_left' := by simp [path.symm], map_one_left' := by simp [path.symm], prop' := λ t x hx, begin cases hx, { rw hx, simp }, { rw set.mem_singleton_iff at hx, rw hx, simp } end } /-- Given `F : homotopy p q`, and `f : C(X, Y)`, we can define a homotopy from `p.map f.continuous` to `q.map f.continuous`. -/ @[simps] def map {p q : path x₀ x₁} (F : p.homotopy q) (f : C(X, Y)) : homotopy (p.map f.continuous) (q.map f.continuous) := { to_fun := f ∘ F, map_zero_left' := by simp, map_one_left' := by simp, prop' := λ t x hx, begin cases hx, { simp [hx] }, { rw set.mem_singleton_iff at hx, simp [hx] } end } end homotopy /-- Two paths `p₀` and `p₁` are `path.homotopic` if there exists a `homotopy` between them. -/ def homotopic (p₀ p₁ : path x₀ x₁) : Prop := nonempty (p₀.homotopy p₁) namespace homotopic @[refl] lemma refl (p : path x₀ x₁) : p.homotopic p := ⟨homotopy.refl p⟩ @[symm] lemma symm ⦃p₀ p₁ : path x₀ x₁⦄ (h : p₀.homotopic p₁) : p₁.homotopic p₀ := h.map homotopy.symm @[trans] lemma trans ⦃p₀ p₁ p₂ : path x₀ x₁⦄ (h₀ : p₀.homotopic p₁) (h₁ : p₁.homotopic p₂) : p₀.homotopic p₂ := h₀.map2 homotopy.trans h₁ lemma equivalence : equivalence (@homotopic X _ x₀ x₁) := ⟨refl, symm, trans⟩ lemma map {p q : path x₀ x₁} (h : p.homotopic q) (f : C(X, Y)) : homotopic (p.map f.continuous) (q.map f.continuous) := h.map (λ F, F.map f) lemma hcomp {p₀ p₁ : path x₀ x₁} {q₀ q₁ : path x₁ x₂} (hp : p₀.homotopic p₁) (hq : q₀.homotopic q₁) : (p₀.trans q₀).homotopic (p₁.trans q₁) := hp.map2 homotopy.hcomp hq /-- The setoid on `path`s defined by the equivalence relation `path.homotopic`. That is, two paths are equivalent if there is a `homotopy` between them. -/ protected def setoid (x₀ x₁ : X) : setoid (path x₀ x₁) := ⟨homotopic, equivalence⟩ /-- The quotient on `path x₀ x₁` by the equivalence relation `path.homotopic`. -/ protected def quotient (x₀ x₁ : X) := quotient (homotopic.setoid x₀ x₁) local attribute [instance] homotopic.setoid instance : inhabited (homotopic.quotient () ()) := ⟨quotient.mk $ path.refl ()⟩ /-- The composition of path homotopy classes. This is `path.trans` descended to the quotient. -/ def quotient.comp (P₀ : path.homotopic.quotient x₀ x₁) (P₁ : path.homotopic.quotient x₁ x₂) : path.homotopic.quotient x₀ x₂ := quotient.map₂ path.trans (λ (p₀ : path x₀ x₁) p₁ hp (q₀ : path x₁ x₂) q₁ hq, (hcomp hp hq)) P₀ P₁ lemma comp_lift (P₀ : path x₀ x₁) (P₁ : path x₁ x₂) : ⟦P₀.trans P₁⟧ = quotient.comp ⟦P₀⟧ ⟦P₁⟧ := rfl /-- The image of a path homotopy class `P₀` under a map `f`. This is `path.map` descended to the quotient -/ def quotient.map_fn (P₀ : path.homotopic.quotient x₀ x₁) (f : C(X, Y)) : path.homotopic.quotient (f x₀) (f x₁) := quotient.map (λ (q : path x₀ x₁), q.map f.continuous) (λ p₀ p₁ h, path.homotopic.map h f) P₀ lemma map_lift (P₀ : path x₀ x₁) (f : C(X, Y)) : ⟦P₀.map f.continuous⟧ = quotient.map_fn ⟦P₀⟧ f := rfl lemma hpath_hext {p₁ : path x₀ x₁} {p₂ : path x₂ x₃} (hp : ∀ t, p₁ t = p₂ t) : ⟦p₁⟧ == ⟦p₂⟧ := begin obtain rfl : x₀ = x₂ := by { convert hp 0; simp, }, obtain rfl : x₁ = x₃ := by { convert hp 1; simp, }, rw heq_iff_eq, congr, ext t, exact hp t, end end homotopic end path namespace continuous_map.homotopy /-- Given a homotopy H: f ∼ g, get the path traced by the point `x` as it moves from `f x` to `g x` -/ def eval_at {X : Type*} {Y : Type*} [topological_space X] [topological_space Y] {f g : C(X, Y)} (H : continuous_map.homotopy f g) (x : X) : path (f x) (g x) := { to_fun := λ t, H (t, x), source' := H.apply_zero x, target' := H.apply_one x, } end continuous_map.homotopy
9e4c5f0e1ab3a69c9c4d399a64a9733b2476780b
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/ring_theory/eisenstein_criterion_auto.lean
210fea88acbaa61b16a2cfff973948cb424bbf5b
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
2,553
lean
/- Copyright (c) 2020 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.ring_theory.ideal.operations import Mathlib.data.polynomial.ring_division import Mathlib.tactic.apply_fun import Mathlib.ring_theory.prime import Mathlib.PostPort universes u_1 namespace Mathlib /-! # Eisenstein's criterion A proof of a slight generalisation of Eisenstein's criterion for the irreducibility of a polynomial over an integral domain. -/ namespace polynomial namespace eisenstein_criterion_aux /- Section for auxiliary lemmas used in the proof of `irreducible_of_eisenstein_criterion`-/ theorem map_eq_C_mul_X_pow_of_forall_coeff_mem {R : Type u_1} [integral_domain R] {f : polynomial R} {P : ideal R} (hfP : ∀ (n : ℕ), ↑n < degree f → coeff f n ∈ P) (hf0 : f ≠ 0) : map (ideal.quotient.mk P) f = coe_fn C (coe_fn (ideal.quotient.mk P) (leading_coeff f)) * X ^ nat_degree f := sorry theorem le_nat_degree_of_map_eq_mul_X_pow {R : Type u_1} [integral_domain R] {n : ℕ} {P : ideal R} (hP : ideal.is_prime P) {q : polynomial R} {c : polynomial (ideal.quotient P)} (hq : map (ideal.quotient.mk P) q = c * X ^ n) (hc0 : degree c = 0) : n ≤ nat_degree q := sorry theorem eval_zero_mem_ideal_of_eq_mul_X_pow {R : Type u_1} [integral_domain R] {n : ℕ} {P : ideal R} {q : polynomial R} {c : polynomial (ideal.quotient P)} (hq : map (ideal.quotient.mk P) q = c * X ^ n) (hn0 : 0 < n) : eval 0 q ∈ P := sorry theorem is_unit_of_nat_degree_eq_zero_of_forall_dvd_is_unit {R : Type u_1} [integral_domain R] {p : polynomial R} {q : polynomial R} (hu : ∀ (x : R), coe_fn C x ∣ p * q → is_unit x) (hpm : nat_degree p = 0) : is_unit p := sorry end eisenstein_criterion_aux /-- If `f` is a non constant polynomial with coefficients in `R`, and `P` is a prime ideal in `R`, then if every coefficient in `R` except the leading coefficient is in `P`, and the trailing coefficient is not in `P^2` and no non units in `R` divide `f`, then `f` is irreducible. -/ theorem irreducible_of_eisenstein_criterion {R : Type u_1} [integral_domain R] {f : polynomial R} {P : ideal R} (hP : ideal.is_prime P) (hfl : ¬leading_coeff f ∈ P) (hfP : ∀ (n : ℕ), ↑n < degree f → coeff f n ∈ P) (hfd0 : 0 < degree f) (h0 : ¬coeff f 0 ∈ P ^ bit0 1) (hu : ∀ (x : R), coe_fn C x ∣ f → is_unit x) : irreducible f := sorry end Mathlib
7aaf6b0652159e86069f7ec100d762198884e768
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/num/lemmas.lean
949911b3da6295635e04f02ab1f448c7ad6b5b33
[ "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
53,451
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.bitwise import data.int.char_zero import data.nat.gcd.basic import data.nat.psub import data.nat.size /-! # Properties of the binary representation of integers -/ local attribute [simp] add_assoc namespace pos_num variables {α : Type*} @[simp, norm_cast] theorem cast_one [has_one α] [has_add α] : ((1 : pos_num) : α) = 1 := rfl @[simp] theorem cast_one' [has_one α] [has_add α] : (pos_num.one : α) = 1 := rfl @[simp, norm_cast] theorem cast_bit0 [has_one α] [has_add α] (n : pos_num) : (n.bit0 : α) = _root_.bit0 n := rfl @[simp, norm_cast] theorem cast_bit1 [has_one α] [has_add α] (n : pos_num) : (n.bit1 : α) = _root_.bit1 n := rfl @[simp, norm_cast] theorem cast_to_nat [add_monoid_with_one α] : ∀ n : pos_num, ((n : ℕ) : α) = n | 1 := nat.cast_one | (bit0 p) := (nat.cast_bit0 _).trans $ congr_arg _root_.bit0 p.cast_to_nat | (bit1 p) := (nat.cast_bit1 _).trans $ congr_arg _root_.bit1 p.cast_to_nat @[simp, norm_cast] theorem to_nat_to_int (n : pos_num) : ((n : ℕ) : ℤ) = n := cast_to_nat _ @[simp, norm_cast] theorem cast_to_int [add_group_with_one α] (n : pos_num) : ((n : ℤ) : α) = n := by rw [← to_nat_to_int, int.cast_coe_nat, cast_to_nat] theorem succ_to_nat : ∀ n, (succ n : ℕ) = n + 1 | 1 := rfl | (bit0 p) := rfl | (bit1 p) := (congr_arg _root_.bit0 (succ_to_nat p)).trans $ show ↑p + 1 + ↑p + 1 = ↑p + ↑p + 1 + 1, by simp [add_left_comm] theorem one_add (n : pos_num) : 1 + n = succ n := by cases n; refl theorem add_one (n : pos_num) : n + 1 = succ n := by cases n; refl @[norm_cast] theorem add_to_nat : ∀ m n, ((m + n : pos_num) : ℕ) = m + n | 1 b := by rw [one_add b, succ_to_nat, add_comm]; refl | a 1 := by rw [add_one a, succ_to_nat]; refl | (bit0 a) (bit0 b) := (congr_arg _root_.bit0 (add_to_nat a b)).trans $ add_add_add_comm _ _ _ _ | (bit0 a) (bit1 b) := (congr_arg _root_.bit1 (add_to_nat a b)).trans $ show ((a + b) + (a + b) + 1 : ℕ) = (a + a) + (b + b + 1), by simp [add_left_comm] | (bit1 a) (bit0 b) := (congr_arg _root_.bit1 (add_to_nat a b)).trans $ show ((a + b) + (a + b) + 1 : ℕ) = (a + a + 1) + (b + b), by simp [add_comm, add_left_comm] | (bit1 a) (bit1 b) := show (succ (a + b) + succ (a + b) : ℕ) = (a + a + 1) + (b + b + 1), by rw [succ_to_nat, add_to_nat]; simp [add_left_comm] theorem add_succ : ∀ (m n : pos_num), m + succ n = succ (m + n) | 1 b := by simp [one_add] | (bit0 a) 1 := congr_arg bit0 (add_one a) | (bit1 a) 1 := congr_arg bit1 (add_one a) | (bit0 a) (bit0 b) := rfl | (bit0 a) (bit1 b) := congr_arg bit0 (add_succ a b) | (bit1 a) (bit0 b) := rfl | (bit1 a) (bit1 b) := congr_arg bit1 (add_succ a b) theorem bit0_of_bit0 : Π n, _root_.bit0 n = bit0 n | 1 := rfl | (bit0 p) := congr_arg bit0 (bit0_of_bit0 p) | (bit1 p) := show bit0 (succ (_root_.bit0 p)) = _, by rw bit0_of_bit0; refl theorem bit1_of_bit1 (n : pos_num) : _root_.bit1 n = bit1 n := show _root_.bit0 n + 1 = bit1 n, by rw [add_one, bit0_of_bit0]; refl @[norm_cast] theorem mul_to_nat (m) : ∀ n, ((m * n : pos_num) : ℕ) = m * n | 1 := (mul_one _).symm | (bit0 p) := show (↑(m * p) + ↑(m * p) : ℕ) = ↑m * (p + p), by rw [mul_to_nat, left_distrib] | (bit1 p) := (add_to_nat (bit0 (m * p)) m).trans $ show (↑(m * p) + ↑(m * p) + ↑m : ℕ) = ↑m * (p + p) + m, by rw [mul_to_nat, left_distrib] theorem to_nat_pos : ∀ n : pos_num, 0 < (n : ℕ) | 1 := zero_lt_one | (bit0 p) := let h := to_nat_pos p in add_pos h h | (bit1 p) := nat.succ_pos _ theorem cmp_to_nat_lemma {m n : pos_num} : (m:ℕ) < n → (bit1 m : ℕ) < bit0 n := show (m:ℕ) < n → (m + m + 1 + 1 : ℕ) ≤ n + n, by intro h; rw [nat.add_right_comm m m 1, add_assoc]; exact add_le_add h h theorem cmp_swap (m) : ∀n, (cmp m n).swap = cmp n m := by induction m with m IH m IH; intro n; cases n with n n; try {unfold cmp}; try {refl}; rw ←IH; cases cmp m n; refl theorem cmp_to_nat : ∀ (m n), (ordering.cases_on (cmp m n) ((m:ℕ) < n) (m = n) ((n:ℕ) < m) : Prop) | 1 1 := rfl | (bit0 a) 1 := let h : (1:ℕ) ≤ a := to_nat_pos a in add_le_add h h | (bit1 a) 1 := nat.succ_lt_succ $ to_nat_pos $ bit0 a | 1 (bit0 b) := let h : (1:ℕ) ≤ b := to_nat_pos b in add_le_add h h | 1 (bit1 b) := nat.succ_lt_succ $ to_nat_pos $ bit0 b | (bit0 a) (bit0 b) := begin have := cmp_to_nat a b, revert this, cases cmp a b; dsimp; intro, { exact add_lt_add this this }, { rw this }, { exact add_lt_add this this } end | (bit0 a) (bit1 b) := begin dsimp [cmp], have := cmp_to_nat a b, revert this, cases cmp a b; dsimp; intro, { exact nat.le_succ_of_le (add_lt_add this this) }, { rw this, apply nat.lt_succ_self }, { exact cmp_to_nat_lemma this } end | (bit1 a) (bit0 b) := begin dsimp [cmp], have := cmp_to_nat a b, revert this, cases cmp a b; dsimp; intro, { exact cmp_to_nat_lemma this }, { rw this, apply nat.lt_succ_self }, { exact nat.le_succ_of_le (add_lt_add this this) }, end | (bit1 a) (bit1 b) := begin have := cmp_to_nat a b, revert this, cases cmp a b; dsimp; intro, { exact nat.succ_lt_succ (add_lt_add this this) }, { rw this }, { exact nat.succ_lt_succ (add_lt_add this this) } end @[norm_cast] theorem lt_to_nat {m n : pos_num} : (m:ℕ) < n ↔ m < n := show (m:ℕ) < n ↔ cmp m n = ordering.lt, from match cmp m n, cmp_to_nat m n with | ordering.lt, h := by simp at h; simp [h] | ordering.eq, h := by simp at h; simp [h, lt_irrefl]; exact dec_trivial | ordering.gt, h := by simp [not_lt_of_gt h]; exact dec_trivial end @[norm_cast] theorem le_to_nat {m n : pos_num} : (m:ℕ) ≤ n ↔ m ≤ n := by rw ← not_lt; exact not_congr lt_to_nat end pos_num namespace num variables {α : Type*} open pos_num theorem add_zero (n : num) : n + 0 = n := by cases n; refl theorem zero_add (n : num) : 0 + n = n := by cases n; refl theorem add_one : ∀ n : num, n + 1 = succ n | 0 := rfl | (pos p) := by cases p; refl theorem add_succ : ∀ (m n : num), m + succ n = succ (m + n) | 0 n := by simp [zero_add] | (pos p) 0 := show pos (p + 1) = succ (pos p + 0), by rw [pos_num.add_one, add_zero]; refl | (pos p) (pos q) := congr_arg pos (pos_num.add_succ _ _) theorem bit0_of_bit0 : ∀ n : num, bit0 n = n.bit0 | 0 := rfl | (pos p) := congr_arg pos p.bit0_of_bit0 theorem bit1_of_bit1 : ∀ n : num, bit1 n = n.bit1 | 0 := rfl | (pos p) := congr_arg pos p.bit1_of_bit1 @[simp] lemma of_nat'_zero : num.of_nat' 0 = 0 := by simp [num.of_nat'] lemma of_nat'_bit (b n) : of_nat' (nat.bit b n) = cond b num.bit1 num.bit0 (of_nat' n) := nat.binary_rec_eq rfl _ _ @[simp] lemma of_nat'_one : num.of_nat' 1 = 1 := by erw [of_nat'_bit tt 0, cond, of_nat'_zero]; refl lemma bit1_succ : ∀ n : num, n.bit1.succ = n.succ.bit0 | 0 := rfl | (pos n) := rfl lemma of_nat'_succ : ∀ {n}, of_nat' (n + 1) = of_nat' n + 1 := nat.binary_rec (by simp; refl) $ λ b n ih, begin cases b, { erw [of_nat'_bit tt n, of_nat'_bit], simp only [← bit1_of_bit1, ← bit0_of_bit0, cond, _root_.bit1] }, { erw [show n.bit tt + 1 = (n + 1).bit ff, by simp [nat.bit, _root_.bit1, _root_.bit0]; cc, of_nat'_bit, of_nat'_bit, ih], simp only [cond, add_one, bit1_succ], }, end @[simp] theorem add_of_nat' (m n) : num.of_nat' (m + n) = num.of_nat' m + num.of_nat' n := by induction n; simp [nat.add_zero, of_nat'_succ, add_zero, nat.add_succ, add_one, add_succ, *] @[simp, norm_cast] theorem cast_zero [has_zero α] [has_one α] [has_add α] : ((0 : num) : α) = 0 := rfl @[simp] theorem cast_zero' [has_zero α] [has_one α] [has_add α] : (num.zero : α) = 0 := rfl @[simp, norm_cast] theorem cast_one [has_zero α] [has_one α] [has_add α] : ((1 : num) : α) = 1 := rfl @[simp] theorem cast_pos [has_zero α] [has_one α] [has_add α] (n : pos_num) : (num.pos n : α) = n := rfl theorem succ'_to_nat : ∀ n, (succ' n : ℕ) = n + 1 | 0 := (_root_.zero_add _).symm | (pos p) := pos_num.succ_to_nat _ theorem succ_to_nat (n) : (succ n : ℕ) = n + 1 := succ'_to_nat n @[simp, norm_cast] theorem cast_to_nat [add_monoid_with_one α] : ∀ n : num, ((n : ℕ) : α) = n | 0 := nat.cast_zero | (pos p) := p.cast_to_nat @[norm_cast] theorem add_to_nat : ∀ m n, ((m + n : num) : ℕ) = m + n | 0 0 := rfl | 0 (pos q) := (_root_.zero_add _).symm | (pos p) 0 := rfl | (pos p) (pos q) := pos_num.add_to_nat _ _ @[norm_cast] theorem mul_to_nat : ∀ m n, ((m * n : num) : ℕ) = m * n | 0 0 := rfl | 0 (pos q) := (zero_mul _).symm | (pos p) 0 := rfl | (pos p) (pos q) := pos_num.mul_to_nat _ _ theorem cmp_to_nat : ∀ (m n), (ordering.cases_on (cmp m n) ((m:ℕ) < n) (m = n) ((n:ℕ) < m) : Prop) | 0 0 := rfl | 0 (pos b) := to_nat_pos _ | (pos a) 0 := to_nat_pos _ | (pos a) (pos b) := by { have := pos_num.cmp_to_nat a b; revert this; dsimp [cmp]; cases pos_num.cmp a b, exacts [id, congr_arg pos, id] } @[norm_cast] theorem lt_to_nat {m n : num} : (m:ℕ) < n ↔ m < n := show (m:ℕ) < n ↔ cmp m n = ordering.lt, from match cmp m n, cmp_to_nat m n with | ordering.lt, h := by simp at h; simp [h] | ordering.eq, h := by simp at h; simp [h, lt_irrefl]; exact dec_trivial | ordering.gt, h := by simp [not_lt_of_gt h]; exact dec_trivial end @[norm_cast] theorem le_to_nat {m n : num} : (m:ℕ) ≤ n ↔ m ≤ n := by rw ← not_lt; exact not_congr lt_to_nat end num namespace pos_num @[simp] theorem of_to_nat' : Π (n : pos_num), num.of_nat' (n : ℕ) = num.pos n | 1 := by erw [@num.of_nat'_bit tt 0, num.of_nat'_zero]; refl | (bit0 p) := by erw [@num.of_nat'_bit ff, of_to_nat']; refl | (bit1 p) := by erw [@num.of_nat'_bit tt, of_to_nat']; refl end pos_num namespace num @[simp, norm_cast] theorem of_to_nat' : Π (n : num), num.of_nat' (n : ℕ) = n | 0 := of_nat'_zero | (pos p) := p.of_to_nat' @[norm_cast] theorem to_nat_inj {m n : num} : (m : ℕ) = n ↔ m = n := ⟨λ h, function.left_inverse.injective of_to_nat' h, congr_arg _⟩ /-- This tactic tries to turn an (in)equality about `num`s to one about `nat`s by rewriting. ```lean example (n : num) (m : num) : n ≤ n + m := begin num.transfer_rw, exact nat.le_add_right _ _ end ``` -/ meta def transfer_rw : tactic unit := `[repeat {rw ← to_nat_inj <|> rw ← lt_to_nat <|> rw ← le_to_nat}, repeat {rw add_to_nat <|> rw mul_to_nat <|> rw cast_one <|> rw cast_zero}] /-- This tactic tries to prove (in)equalities about `num`s by transfering them to the `nat` world and then trying to call `simp`. ```lean example (n : num) (m : num) : n ≤ n + m := by num.transfer ``` -/ meta def transfer : tactic unit := `[intros, transfer_rw, try {simp}] instance : add_monoid num := { add := (+), zero := 0, zero_add := zero_add, add_zero := add_zero, add_assoc := by transfer } instance : add_monoid_with_one num := { nat_cast := num.of_nat', one := 1, nat_cast_zero := of_nat'_zero, nat_cast_succ := λ _, of_nat'_succ, .. num.add_monoid } instance : comm_semiring num := by refine_struct { mul := (*), one := 1, add := (+), zero := 0, npow := @npow_rec num ⟨1⟩ ⟨(*)⟩, .. num.add_monoid, .. num.add_monoid_with_one }; try { intros, refl }; try { transfer }; simp [add_comm, mul_add, add_mul, mul_assoc, mul_comm, mul_left_comm] instance : ordered_cancel_add_comm_monoid num := { lt := (<), lt_iff_le_not_le := by {intros a b, transfer_rw, apply lt_iff_le_not_le}, le := (≤), le_refl := by transfer, le_trans := by {intros a b c, transfer_rw, apply le_trans}, le_antisymm := by {intros a b, transfer_rw, apply le_antisymm}, add_le_add_left := by {intros a b h c, revert h, transfer_rw, exact λ h, add_le_add_left h c}, le_of_add_le_add_left := by {intros a b c, transfer_rw, apply le_of_add_le_add_left}, ..num.comm_semiring } instance : linear_ordered_semiring num := { le_total := by {intros a b, transfer_rw, apply le_total}, zero_le_one := dec_trivial, mul_lt_mul_of_pos_left := by {intros a b c, transfer_rw, apply mul_lt_mul_of_pos_left}, mul_lt_mul_of_pos_right := by {intros a b c, transfer_rw, apply mul_lt_mul_of_pos_right}, decidable_lt := num.decidable_lt, decidable_le := num.decidable_le, decidable_eq := num.decidable_eq, exists_pair_ne := ⟨0, 1, dec_trivial⟩, ..num.comm_semiring, ..num.ordered_cancel_add_comm_monoid } @[simp, norm_cast] theorem add_of_nat (m n) : ((m + n : ℕ) : num) = m + n := add_of_nat' _ _ @[simp, norm_cast] theorem to_nat_to_int (n : num) : ((n : ℕ) : ℤ) = n := cast_to_nat _ @[simp, norm_cast] theorem cast_to_int {α} [add_group_with_one α] (n : num) : ((n : ℤ) : α) = n := by rw [← to_nat_to_int, int.cast_coe_nat, cast_to_nat] theorem to_of_nat : Π (n : ℕ), ((n : num) : ℕ) = n | 0 := by rw [nat.cast_zero, cast_zero] | (n+1) := by rw [nat.cast_succ, add_one, succ_to_nat, to_of_nat] @[simp, norm_cast] theorem of_nat_cast {α} [add_monoid_with_one α] (n : ℕ) : ((n : num) : α) = n := by rw [← cast_to_nat, to_of_nat] @[simp, norm_cast] theorem of_nat_inj {m n : ℕ} : (m : num) = n ↔ m = n := ⟨λ h, function.left_inverse.injective to_of_nat h, congr_arg _⟩ @[simp, norm_cast] theorem of_to_nat : Π (n : num), ((n : ℕ) : num) = n := of_to_nat' @[norm_cast] theorem dvd_to_nat (m n : num) : (m : ℕ) ∣ n ↔ m ∣ n := ⟨λ ⟨k, e⟩, ⟨k, by rw [← of_to_nat n, e]; simp⟩, λ ⟨k, e⟩, ⟨k, by simp [e, mul_to_nat]⟩⟩ end num namespace pos_num variables {α : Type*} open num @[simp, norm_cast] theorem of_to_nat : Π (n : pos_num), ((n : ℕ) : num) = num.pos n := of_to_nat' @[norm_cast] theorem to_nat_inj {m n : pos_num} : (m : ℕ) = n ↔ m = n := ⟨λ h, num.pos.inj $ by rw [← pos_num.of_to_nat, ← pos_num.of_to_nat, h], congr_arg _⟩ theorem pred'_to_nat : ∀ n, (pred' n : ℕ) = nat.pred n | 1 := rfl | (bit0 n) := have nat.succ ↑(pred' n) = ↑n, by rw [pred'_to_nat n, nat.succ_pred_eq_of_pos (to_nat_pos n)], match pred' n, this : ∀ k : num, nat.succ ↑k = ↑n → ↑(num.cases_on k 1 bit1 : pos_num) = nat.pred (_root_.bit0 n) with | 0, (h : ((1:num):ℕ) = n) := by rw ← to_nat_inj.1 h; refl | num.pos p, (h : nat.succ ↑p = n) := by rw ← h; exact (nat.succ_add p p).symm end | (bit1 n) := rfl @[simp] theorem pred'_succ' (n) : pred' (succ' n) = n := num.to_nat_inj.1 $ by rw [pred'_to_nat, succ'_to_nat, nat.add_one, nat.pred_succ] @[simp] theorem succ'_pred' (n) : succ' (pred' n) = n := to_nat_inj.1 $ by rw [succ'_to_nat, pred'_to_nat, nat.add_one, nat.succ_pred_eq_of_pos (to_nat_pos _)] instance : has_dvd pos_num := ⟨λ m n, pos m ∣ pos n⟩ @[norm_cast] theorem dvd_to_nat {m n : pos_num} : (m:ℕ) ∣ n ↔ m ∣ n := num.dvd_to_nat (pos m) (pos n) theorem size_to_nat : ∀ n, (size n : ℕ) = nat.size n | 1 := nat.size_one.symm | (bit0 n) := by rw [size, succ_to_nat, size_to_nat, cast_bit0, nat.size_bit0 $ ne_of_gt $ to_nat_pos n] | (bit1 n) := by rw [size, succ_to_nat, size_to_nat, cast_bit1, nat.size_bit1] theorem size_eq_nat_size : ∀ n, (size n : ℕ) = nat_size n | 1 := rfl | (bit0 n) := by rw [size, succ_to_nat, nat_size, size_eq_nat_size] | (bit1 n) := by rw [size, succ_to_nat, nat_size, size_eq_nat_size] theorem nat_size_to_nat (n) : nat_size n = nat.size n := by rw [← size_eq_nat_size, size_to_nat] theorem nat_size_pos (n) : 0 < nat_size n := by cases n; apply nat.succ_pos /-- This tactic tries to turn an (in)equality about `pos_num`s to one about `nat`s by rewriting. ```lean example (n : pos_num) (m : pos_num) : n ≤ n + m := begin pos_num.transfer_rw, exact nat.le_add_right _ _ end ``` -/ meta def transfer_rw : tactic unit := `[repeat {rw ← to_nat_inj <|> rw ← lt_to_nat <|> rw ← le_to_nat}, repeat {rw add_to_nat <|> rw mul_to_nat <|> rw cast_one <|> rw cast_zero}] /-- This tactic tries to prove (in)equalities about `pos_num`s by transferring them to the `nat` world and then trying to call `simp`. ```lean example (n : pos_num) (m : pos_num) : n ≤ n + m := by pos_num.transfer ``` -/ meta def transfer : tactic unit := `[intros, transfer_rw, try {simp [add_comm, add_left_comm, mul_comm, mul_left_comm]}] instance : add_comm_semigroup pos_num := by refine {add := (+), ..}; transfer instance : comm_monoid pos_num := by refine_struct {mul := (*), one := (1 : pos_num), npow := @npow_rec pos_num ⟨1⟩ ⟨(*)⟩}; try { intros, refl }; transfer instance : distrib pos_num := by refine {add := (+), mul := (*), ..}; {transfer, simp [mul_add, mul_comm]} instance : linear_order pos_num := { lt := (<), lt_iff_le_not_le := by {intros a b, transfer_rw, apply lt_iff_le_not_le}, le := (≤), le_refl := by transfer, le_trans := by {intros a b c, transfer_rw, apply le_trans}, le_antisymm := by {intros a b, transfer_rw, apply le_antisymm}, le_total := by {intros a b, transfer_rw, apply le_total}, decidable_lt := by apply_instance, decidable_le := by apply_instance, decidable_eq := by apply_instance } @[simp] theorem cast_to_num (n : pos_num) : ↑n = num.pos n := by rw [← cast_to_nat, ← of_to_nat n] @[simp, norm_cast] theorem bit_to_nat (b n) : (bit b n : ℕ) = nat.bit b n := by cases b; refl @[simp, norm_cast] theorem cast_add [add_monoid_with_one α] (m n) : ((m + n : pos_num) : α) = m + n := by rw [← cast_to_nat, add_to_nat, nat.cast_add, cast_to_nat, cast_to_nat] @[simp, norm_cast, priority 500] theorem cast_succ [add_monoid_with_one α] (n : pos_num) : (succ n : α) = n + 1 := by rw [← add_one, cast_add, cast_one] @[simp, norm_cast] theorem cast_inj [add_monoid_with_one α] [char_zero α] {m n : pos_num} : (m:α) = n ↔ m = n := by rw [← cast_to_nat m, ← cast_to_nat n, nat.cast_inj, to_nat_inj] @[simp] theorem one_le_cast [linear_ordered_semiring α] (n : pos_num) : (1 : α) ≤ n := by rw [← cast_to_nat, ← nat.cast_one, nat.cast_le]; apply to_nat_pos @[simp] theorem cast_pos [linear_ordered_semiring α] (n : pos_num) : 0 < (n : α) := lt_of_lt_of_le zero_lt_one (one_le_cast n) @[simp, norm_cast] theorem cast_mul [semiring α] (m n) : ((m * n : pos_num) : α) = m * n := by rw [← cast_to_nat, mul_to_nat, nat.cast_mul, cast_to_nat, cast_to_nat] @[simp] theorem cmp_eq (m n) : cmp m n = ordering.eq ↔ m = n := begin have := cmp_to_nat m n, cases cmp m n; simp at this ⊢; try {exact this}; { simp [show m ≠ n, from λ e, by rw e at this; exact lt_irrefl _ this] } end @[simp, norm_cast] theorem cast_lt [linear_ordered_semiring α] {m n : pos_num} : (m:α) < n ↔ m < n := by rw [← cast_to_nat m, ← cast_to_nat n, nat.cast_lt, lt_to_nat] @[simp, norm_cast] theorem cast_le [linear_ordered_semiring α] {m n : pos_num} : (m:α) ≤ n ↔ m ≤ n := by rw ← not_lt; exact not_congr cast_lt end pos_num namespace num variables {α : Type*} open pos_num theorem bit_to_nat (b n) : (bit b n : ℕ) = nat.bit b n := by cases b; cases n; refl theorem cast_succ' [add_monoid_with_one α] (n) : (succ' n : α) = n + 1 := by rw [← pos_num.cast_to_nat, succ'_to_nat, nat.cast_add_one, cast_to_nat] theorem cast_succ [add_monoid_with_one α] (n) : (succ n : α) = n + 1 := cast_succ' n @[simp, norm_cast] theorem cast_add [semiring α] (m n) : ((m + n : num) : α) = m + n := by rw [← cast_to_nat, add_to_nat, nat.cast_add, cast_to_nat, cast_to_nat] @[simp, norm_cast] theorem cast_bit0 [semiring α] (n : num) : (n.bit0 : α) = _root_.bit0 n := by rw [← bit0_of_bit0, _root_.bit0, cast_add]; refl @[simp, norm_cast] theorem cast_bit1 [semiring α] (n : num) : (n.bit1 : α) = _root_.bit1 n := by rw [← bit1_of_bit1, _root_.bit1, bit0_of_bit0, cast_add, cast_bit0]; refl @[simp, norm_cast] theorem cast_mul [semiring α] : ∀ m n, ((m * n : num) : α) = m * n | 0 0 := (zero_mul _).symm | 0 (pos q) := (zero_mul _).symm | (pos p) 0 := (mul_zero _).symm | (pos p) (pos q) := pos_num.cast_mul _ _ theorem size_to_nat : ∀ n, (size n : ℕ) = nat.size n | 0 := nat.size_zero.symm | (pos p) := p.size_to_nat theorem size_eq_nat_size : ∀ n, (size n : ℕ) = nat_size n | 0 := rfl | (pos p) := p.size_eq_nat_size theorem nat_size_to_nat (n) : nat_size n = nat.size n := by rw [← size_eq_nat_size, size_to_nat] @[simp, priority 999] theorem of_nat'_eq : ∀ n, num.of_nat' n = n := nat.binary_rec (by simp) $ λ b n IH, begin rw of_nat' at IH ⊢, rw [nat.binary_rec_eq, IH], { cases b; simp [nat.bit, bit0_of_bit0, bit1_of_bit1] }, { refl } end theorem zneg_to_znum (n : num) : -n.to_znum = n.to_znum_neg := by cases n; refl theorem zneg_to_znum_neg (n : num) : -n.to_znum_neg = n.to_znum := by cases n; refl theorem to_znum_inj {m n : num} : m.to_znum = n.to_znum ↔ m = n := ⟨λ h, by cases m; cases n; cases h; refl, congr_arg _⟩ @[simp, norm_cast squash] theorem cast_to_znum [has_zero α] [has_one α] [has_add α] [has_neg α] : ∀ n : num, (n.to_znum : α) = n | 0 := rfl | (num.pos p) := rfl @[simp] theorem cast_to_znum_neg [add_group α] [has_one α] : ∀ n : num, (n.to_znum_neg : α) = -n | 0 := neg_zero.symm | (num.pos p) := rfl @[simp] theorem add_to_znum (m n : num) : num.to_znum (m + n) = m.to_znum + n.to_znum := by cases m; cases n; refl end num namespace pos_num open num theorem pred_to_nat {n : pos_num} (h : 1 < n) : (pred n : ℕ) = nat.pred n := begin unfold pred, have := pred'_to_nat n, cases e : pred' n, { have : (1:ℕ) ≤ nat.pred n := nat.pred_le_pred ((@cast_lt ℕ _ _ _).2 h), rw [← pred'_to_nat, e] at this, exact absurd this dec_trivial }, { rw [← pred'_to_nat, e], refl } end theorem sub'_one (a : pos_num) : sub' a 1 = (pred' a).to_znum := by cases a; refl theorem one_sub' (a : pos_num) : sub' 1 a = (pred' a).to_znum_neg := by cases a; refl theorem lt_iff_cmp {m n} : m < n ↔ cmp m n = ordering.lt := iff.rfl theorem le_iff_cmp {m n} : m ≤ n ↔ cmp m n ≠ ordering.gt := not_congr $ lt_iff_cmp.trans $ by rw ← cmp_swap; cases cmp m n; exact dec_trivial end pos_num namespace num variables {α : Type*} open pos_num theorem pred_to_nat : ∀ (n : num), (pred n : ℕ) = nat.pred n | 0 := rfl | (pos p) := by rw [pred, pos_num.pred'_to_nat]; refl theorem ppred_to_nat : ∀ (n : num), coe <$> ppred n = nat.ppred n | 0 := rfl | (pos p) := by rw [ppred, option.map_some, nat.ppred_eq_some.2]; rw [pos_num.pred'_to_nat, nat.succ_pred_eq_of_pos (pos_num.to_nat_pos _)]; refl theorem cmp_swap (m n) : (cmp m n).swap = cmp n m := by cases m; cases n; try {unfold cmp}; try {refl}; apply pos_num.cmp_swap theorem cmp_eq (m n) : cmp m n = ordering.eq ↔ m = n := begin have := cmp_to_nat m n, cases cmp m n; simp at this ⊢; try {exact this}; { simp [show m ≠ n, from λ e, by rw e at this; exact lt_irrefl _ this] } end @[simp, norm_cast] theorem cast_lt [linear_ordered_semiring α] {m n : num} : (m:α) < n ↔ m < n := by rw [← cast_to_nat m, ← cast_to_nat n, nat.cast_lt, lt_to_nat] @[simp, norm_cast] theorem cast_le [linear_ordered_semiring α] {m n : num} : (m:α) ≤ n ↔ m ≤ n := by rw ← not_lt; exact not_congr cast_lt @[simp, norm_cast] theorem cast_inj [linear_ordered_semiring α] {m n : num} : (m:α) = n ↔ m = n := by rw [← cast_to_nat m, ← cast_to_nat n, nat.cast_inj, to_nat_inj] theorem lt_iff_cmp {m n} : m < n ↔ cmp m n = ordering.lt := iff.rfl theorem le_iff_cmp {m n} : m ≤ n ↔ cmp m n ≠ ordering.gt := not_congr $ lt_iff_cmp.trans $ by rw ← cmp_swap; cases cmp m n; exact dec_trivial theorem bitwise_to_nat {f : num → num → num} {g : bool → bool → bool} (p : pos_num → pos_num → num) (gff : g ff ff = ff) (f00 : f 0 0 = 0) (f0n : ∀ n, f 0 (pos n) = cond (g ff tt) (pos n) 0) (fn0 : ∀ n, f (pos n) 0 = cond (g tt ff) (pos n) 0) (fnn : ∀ m n, f (pos m) (pos n) = p m n) (p11 : p 1 1 = cond (g tt tt) 1 0) (p1b : ∀ b n, p 1 (pos_num.bit b n) = bit (g tt b) (cond (g ff tt) (pos n) 0)) (pb1 : ∀ a m, p (pos_num.bit a m) 1 = bit (g a tt) (cond (g tt ff) (pos m) 0)) (pbb : ∀ a b m n, p (pos_num.bit a m) (pos_num.bit b n) = bit (g a b) (p m n)) : ∀ m n : num, (f m n : ℕ) = nat.bitwise g m n := begin intros, cases m with m; cases n with n; try { change zero with 0 }; try { change ((0:num):ℕ) with 0 }, { rw [f00, nat.bitwise_zero]; refl }, { unfold nat.bitwise, rw [f0n, nat.binary_rec_zero], cases g ff tt; refl }, { unfold nat.bitwise, generalize h : (pos m : ℕ) = m', revert h, apply nat.bit_cases_on m' _, intros b m' h, rw [fn0, nat.binary_rec_eq, nat.binary_rec_zero, ←h], cases g tt ff; refl, apply nat.bitwise_bit_aux gff }, { rw fnn, have : ∀b (n : pos_num), (cond b ↑n 0 : ℕ) = ↑(cond b (pos n) 0 : num) := by intros; cases b; refl, induction m with m IH m IH generalizing n; cases n with n n, any_goals { change one with 1 }, any_goals { change pos 1 with 1 }, any_goals { change pos_num.bit0 with pos_num.bit ff }, any_goals { change pos_num.bit1 with pos_num.bit tt }, any_goals { change ((1:num):ℕ) with nat.bit tt 0 }, all_goals { repeat { rw show ∀ b n, (pos (pos_num.bit b n) : ℕ) = nat.bit b ↑n, by intros; cases b; refl }, rw nat.bitwise_bit }, any_goals { assumption }, any_goals { rw [nat.bitwise_zero, p11], cases g tt tt; refl }, any_goals { rw [nat.bitwise_zero_left, this, ← bit_to_nat, p1b] }, any_goals { rw [nat.bitwise_zero_right _ gff, this, ← bit_to_nat, pb1] }, all_goals { rw [← show ∀ n, ↑(p m n) = nat.bitwise g ↑m ↑n, from IH], rw [← bit_to_nat, pbb] } } end @[simp, norm_cast] theorem lor_to_nat : ∀ m n, (lor m n : ℕ) = nat.lor m n := by apply bitwise_to_nat (λx y, pos (pos_num.lor x y)); intros; try {cases a}; try {cases b}; refl @[simp, norm_cast] theorem land_to_nat : ∀ m n, (land m n : ℕ) = nat.land m n := by apply bitwise_to_nat pos_num.land; intros; try {cases a}; try {cases b}; refl @[simp, norm_cast] theorem ldiff_to_nat : ∀ m n, (ldiff m n : ℕ) = nat.ldiff m n := by apply bitwise_to_nat pos_num.ldiff; intros; try {cases a}; try {cases b}; refl @[simp, norm_cast] theorem lxor_to_nat : ∀ m n, (lxor m n : ℕ) = nat.lxor m n := by apply bitwise_to_nat pos_num.lxor; intros; try {cases a}; try {cases b}; refl @[simp, norm_cast] theorem shiftl_to_nat (m n) : (shiftl m n : ℕ) = nat.shiftl m n := begin cases m; dunfold shiftl, {symmetry, apply nat.zero_shiftl}, simp, induction n with n IH, {refl}, simp [pos_num.shiftl, nat.shiftl_succ], rw ←IH end @[simp, norm_cast] theorem shiftr_to_nat (m n) : (shiftr m n : ℕ) = nat.shiftr m n := begin cases m with m; dunfold shiftr, {symmetry, apply nat.zero_shiftr}, induction n with n IH generalizing m, {cases m; refl}, cases m with m m; dunfold pos_num.shiftr, { rw [nat.shiftr_eq_div_pow], symmetry, apply nat.div_eq_of_lt, exact @nat.pow_lt_pow_of_lt_right 2 dec_trivial 0 (n+1) (nat.succ_pos _) }, { transitivity, apply IH, change nat.shiftr m n = nat.shiftr (bit1 m) (n+1), rw [add_comm n 1, nat.shiftr_add], apply congr_arg (λx, nat.shiftr x n), unfold nat.shiftr, change (bit1 ↑m : ℕ) with nat.bit tt m, rw nat.div2_bit }, { transitivity, apply IH, change nat.shiftr m n = nat.shiftr (bit0 m) (n + 1), rw [add_comm n 1, nat.shiftr_add], apply congr_arg (λx, nat.shiftr x n), unfold nat.shiftr, change (bit0 ↑m : ℕ) with nat.bit ff m, rw nat.div2_bit } end @[simp] theorem test_bit_to_nat (m n) : test_bit m n = nat.test_bit m n := begin cases m with m; unfold test_bit nat.test_bit, { change (zero : nat) with 0, rw nat.zero_shiftr, refl }, induction n with n IH generalizing m; cases m; dunfold pos_num.test_bit, {refl}, { exact (nat.bodd_bit _ _).symm }, { exact (nat.bodd_bit _ _).symm }, { change ff = nat.bodd (nat.shiftr 1 (n + 1)), rw [add_comm, nat.shiftr_add], change nat.shiftr 1 1 with 0, rw nat.zero_shiftr; refl }, { change pos_num.test_bit m n = nat.bodd (nat.shiftr (nat.bit tt m) (n + 1)), rw [add_comm, nat.shiftr_add], unfold nat.shiftr, rw nat.div2_bit, apply IH }, { change pos_num.test_bit m n = nat.bodd (nat.shiftr (nat.bit ff m) (n + 1)), rw [add_comm, nat.shiftr_add], unfold nat.shiftr, rw nat.div2_bit, apply IH }, end end num namespace znum variables {α : Type*} open pos_num @[simp, norm_cast] theorem cast_zero [has_zero α] [has_one α] [has_add α] [has_neg α] : ((0 : znum) : α) = 0 := rfl @[simp] theorem cast_zero' [has_zero α] [has_one α] [has_add α] [has_neg α] : (znum.zero : α) = 0 := rfl @[simp, norm_cast] theorem cast_one [has_zero α] [has_one α] [has_add α] [has_neg α] : ((1 : znum) : α) = 1 := rfl @[simp] theorem cast_pos [has_zero α] [has_one α] [has_add α] [has_neg α] (n : pos_num) : (pos n : α) = n := rfl @[simp] theorem cast_neg [has_zero α] [has_one α] [has_add α] [has_neg α] (n : pos_num) : (neg n : α) = -n := rfl @[simp, norm_cast] theorem cast_zneg [add_group α] [has_one α] : ∀ n, ((-n : znum) : α) = -n | 0 := neg_zero.symm | (pos p) := rfl | (neg p) := (neg_neg _).symm theorem neg_zero : (-0 : znum) = 0 := rfl theorem zneg_pos (n : pos_num) : -pos n = neg n := rfl theorem zneg_neg (n : pos_num) : -neg n = pos n := rfl theorem zneg_zneg (n : znum) : - -n = n := by cases n; refl theorem zneg_bit1 (n : znum) : -n.bit1 = (-n).bitm1 := by cases n; refl theorem zneg_bitm1 (n : znum) : -n.bitm1 = (-n).bit1 := by cases n; refl theorem zneg_succ (n : znum) : -n.succ = (-n).pred := by cases n; try {refl}; rw [succ, num.zneg_to_znum_neg]; refl theorem zneg_pred (n : znum) : -n.pred = (-n).succ := by rw [← zneg_zneg (succ (-n)), zneg_succ, zneg_zneg] @[simp] theorem abs_to_nat : ∀ n, (abs n : ℕ) = int.nat_abs n | 0 := rfl | (pos p) := congr_arg int.nat_abs p.to_nat_to_int | (neg p) := show int.nat_abs ((p:ℕ):ℤ) = int.nat_abs (- p), by rw [p.to_nat_to_int, int.nat_abs_neg] @[simp] theorem abs_to_znum : ∀ n : num, abs n.to_znum = n | 0 := rfl | (num.pos p) := rfl @[simp, norm_cast] theorem cast_to_int [add_group_with_one α] : ∀ n : znum, ((n : ℤ) : α) = n | 0 := by rw [cast_zero, cast_zero, int.cast_zero] | (pos p) := by rw [cast_pos, cast_pos, pos_num.cast_to_int] | (neg p) := by rw [cast_neg, cast_neg, int.cast_neg, pos_num.cast_to_int] theorem bit0_of_bit0 : ∀ n : znum, _root_.bit0 n = n.bit0 | 0 := rfl | (pos a) := congr_arg pos a.bit0_of_bit0 | (neg a) := congr_arg neg a.bit0_of_bit0 theorem bit1_of_bit1 : ∀ n : znum, _root_.bit1 n = n.bit1 | 0 := rfl | (pos a) := congr_arg pos a.bit1_of_bit1 | (neg a) := show pos_num.sub' 1 (_root_.bit0 a) = _, by rw [pos_num.one_sub', a.bit0_of_bit0]; refl @[simp, norm_cast] theorem cast_bit0 [add_group_with_one α] : ∀ n : znum, (n.bit0 : α) = bit0 n | 0 := (add_zero _).symm | (pos p) := by rw [znum.bit0, cast_pos, cast_pos]; refl | (neg p) := by rw [znum.bit0, cast_neg, cast_neg, pos_num.cast_bit0, _root_.bit0, _root_.bit0, neg_add_rev] @[simp, norm_cast] theorem cast_bit1 [add_group_with_one α] : ∀ n : znum, (n.bit1 : α) = bit1 n | 0 := by simp [znum.bit1, _root_.bit1, _root_.bit0] | (pos p) := by rw [znum.bit1, cast_pos, cast_pos]; refl | (neg p) := begin rw [znum.bit1, cast_neg, cast_neg], cases e : pred' p with a; have : p = _ := (succ'_pred' p).symm.trans (congr_arg num.succ' e), { change p=1 at this, subst p, simp [_root_.bit1, _root_.bit0] }, { rw [num.succ'] at this, subst p, have : (↑(-↑a:ℤ) : α) = -1 + ↑(-↑a + 1 : ℤ), {simp [add_comm]}, simpa [_root_.bit1, _root_.bit0, -add_comm] }, end @[simp] theorem cast_bitm1 [add_group_with_one α] (n : znum) : (n.bitm1 : α) = bit0 n - 1 := begin conv { to_lhs, rw ← zneg_zneg n }, rw [← zneg_bit1, cast_zneg, cast_bit1], have : ((-1 + n + n : ℤ) : α) = (n + n + -1 : ℤ), {simp [add_comm, add_left_comm]}, simpa [_root_.bit1, _root_.bit0, sub_eq_add_neg, -int.add_neg_one] end theorem add_zero (n : znum) : n + 0 = n := by cases n; refl theorem zero_add (n : znum) : 0 + n = n := by cases n; refl theorem add_one : ∀ n : znum, n + 1 = succ n | 0 := rfl | (pos p) := congr_arg pos p.add_one | (neg p) := by cases p; refl end znum namespace pos_num variables {α : Type*} theorem cast_to_znum : ∀ n : pos_num, (n : znum) = znum.pos n | 1 := rfl | (bit0 p) := (znum.bit0_of_bit0 p).trans $ congr_arg _ (cast_to_znum p) | (bit1 p) := (znum.bit1_of_bit1 p).trans $ congr_arg _ (cast_to_znum p) local attribute [-simp] int.add_neg_one theorem cast_sub' [add_group_with_one α] : ∀ m n : pos_num, (sub' m n : α) = m - n | a 1 := by rw [sub'_one, num.cast_to_znum, ← num.cast_to_nat, pred'_to_nat, ← nat.sub_one]; simp [pos_num.cast_pos] | 1 b := by rw [one_sub', num.cast_to_znum_neg, ← neg_sub, neg_inj, ← num.cast_to_nat, pred'_to_nat, ← nat.sub_one]; simp [pos_num.cast_pos] | (bit0 a) (bit0 b) := begin rw [sub', znum.cast_bit0, cast_sub'], have : ((a + -b + (a + -b) : ℤ) : α) = a + a + (-b + -b), {simp [add_left_comm]}, simpa [_root_.bit0, sub_eq_add_neg] end | (bit0 a) (bit1 b) := begin rw [sub', znum.cast_bitm1, cast_sub'], have : ((-b + (a + (-b + -1)) : ℤ) : α) = (a + -1 + (-b + -b):ℤ), { simp [add_comm, add_left_comm] }, simpa [_root_.bit1, _root_.bit0, sub_eq_add_neg] end | (bit1 a) (bit0 b) := begin rw [sub', znum.cast_bit1, cast_sub'], have : ((-b + (a + (-b + 1)) : ℤ) : α) = (a + 1 + (-b + -b):ℤ), { simp [add_comm, add_left_comm] }, simpa [_root_.bit1, _root_.bit0, sub_eq_add_neg] end | (bit1 a) (bit1 b) := begin rw [sub', znum.cast_bit0, cast_sub'], have : ((-b + (a + -b) : ℤ) : α) = a + (-b + -b), {simp [add_left_comm]}, simpa [_root_.bit1, _root_.bit0, sub_eq_add_neg] end theorem to_nat_eq_succ_pred (n : pos_num) : (n:ℕ) = n.pred' + 1 := by rw [← num.succ'_to_nat, n.succ'_pred'] theorem to_int_eq_succ_pred (n : pos_num) : (n:ℤ) = (n.pred' : ℕ) + 1 := by rw [← n.to_nat_to_int, to_nat_eq_succ_pred]; refl end pos_num namespace num variables {α : Type*} @[simp] theorem cast_sub' [add_group_with_one α] : ∀ m n : num, (sub' m n : α) = m - n | 0 0 := (sub_zero _).symm | (pos a) 0 := (sub_zero _).symm | 0 (pos b) := (zero_sub _).symm | (pos a) (pos b) := pos_num.cast_sub' _ _ theorem to_znum_succ : ∀ n : num, n.succ.to_znum = n.to_znum.succ | 0 := rfl | (pos n) := rfl theorem to_znum_neg_succ : ∀ n : num, n.succ.to_znum_neg = n.to_znum_neg.pred | 0 := rfl | (pos n) := rfl @[simp] theorem pred_succ : ∀ n : znum, n.pred.succ = n | 0 := rfl | (znum.neg p) := show to_znum_neg (pos p).succ'.pred' = _, by rw [pos_num.pred'_succ']; refl | (znum.pos p) := by rw [znum.pred, ← to_znum_succ, num.succ, pos_num.succ'_pred', to_znum] theorem succ_of_int' : ∀ n, znum.of_int' (n + 1) = znum.of_int' n + 1 | (n : ℕ) := by erw [znum.of_int', znum.of_int', num.of_nat'_succ, num.add_one, to_znum_succ, znum.add_one] | -[1+ 0] := by erw [znum.of_int', znum.of_int', of_nat'_succ, of_nat'_zero]; refl | -[1+ n+1] := by erw [znum.of_int', znum.of_int', @num.of_nat'_succ (n+1), num.add_one, to_znum_neg_succ, @of_nat'_succ n, num.add_one, znum.add_one, pred_succ] theorem of_int'_to_znum : ∀ n : ℕ, to_znum n = znum.of_int' n | 0 := rfl | (n+1) := by rw [nat.cast_succ, num.add_one, to_znum_succ, of_int'_to_znum, nat.cast_succ, succ_of_int', znum.add_one] theorem mem_of_znum' : ∀ {m : num} {n : znum}, m ∈ of_znum' n ↔ n = to_znum m | 0 0 := ⟨λ _, rfl, λ _, rfl⟩ | (pos m) 0 := ⟨λ h, by cases h, λ h, by cases h⟩ | m (znum.pos p) := option.some_inj.trans $ by cases m; split; intro h; try {cases h}; refl | m (znum.neg p) := ⟨λ h, by cases h, λ h, by cases m; cases h⟩ theorem of_znum'_to_nat : ∀ (n : znum), coe <$> of_znum' n = int.to_nat' n | 0 := rfl | (znum.pos p) := show _ = int.to_nat' p, by rw [← pos_num.to_nat_to_int p]; refl | (znum.neg p) := congr_arg (λ x, int.to_nat' (-x)) $ show ((p.pred' + 1 : ℕ) : ℤ) = p, by rw ← succ'_to_nat; simp @[simp] theorem of_znum_to_nat : ∀ (n : znum), (of_znum n : ℕ) = int.to_nat n | 0 := rfl | (znum.pos p) := show _ = int.to_nat p, by rw [← pos_num.to_nat_to_int p]; refl | (znum.neg p) := congr_arg (λ x, int.to_nat (-x)) $ show ((p.pred' + 1 : ℕ) : ℤ) = p, by rw ← succ'_to_nat; simp @[simp] theorem cast_of_znum [add_group_with_one α] (n : znum) : (of_znum n : α) = int.to_nat n := by rw [← cast_to_nat, of_znum_to_nat] @[simp, norm_cast] theorem sub_to_nat (m n) : ((m - n : num) : ℕ) = m - n := show (of_znum _ : ℕ) = _, by rw [of_znum_to_nat, cast_sub', ← to_nat_to_int, ← to_nat_to_int, int.to_nat_sub] end num namespace znum variables {α : Type*} @[simp, norm_cast] theorem cast_add [add_group_with_one α] : ∀ m n, ((m + n : znum) : α) = m + n | 0 a := by cases a; exact (_root_.zero_add _).symm | b 0 := by cases b; exact (_root_.add_zero _).symm | (pos a) (pos b) := pos_num.cast_add _ _ | (pos a) (neg b) := by simpa only [sub_eq_add_neg] using pos_num.cast_sub' _ _ | (neg a) (pos b) := have (↑b + -↑a : α) = -↑a + ↑b, by rw [← pos_num.cast_to_int a, ← pos_num.cast_to_int b, ← int.cast_neg, ← int.cast_add (-a)]; simp [add_comm], (pos_num.cast_sub' _ _).trans $ (sub_eq_add_neg _ _).trans this | (neg a) (neg b) := show -(↑(a + b) : α) = -a + -b, by rw [ pos_num.cast_add, neg_eq_iff_neg_eq, neg_add_rev, neg_neg, neg_neg, ← pos_num.cast_to_int a, ← pos_num.cast_to_int b, ← int.cast_add]; simp [add_comm] @[simp] theorem cast_succ [add_group_with_one α] (n) : ((succ n : znum) : α) = n + 1 := by rw [← add_one, cast_add, cast_one] @[simp, norm_cast] theorem mul_to_int : ∀ m n, ((m * n : znum) : ℤ) = m * n | 0 a := by cases a; exact (_root_.zero_mul _).symm | b 0 := by cases b; exact (_root_.mul_zero _).symm | (pos a) (pos b) := pos_num.cast_mul a b | (pos a) (neg b) := show -↑(a * b) = ↑a * -↑b, by rw [pos_num.cast_mul, neg_mul_eq_mul_neg] | (neg a) (pos b) := show -↑(a * b) = -↑a * ↑b, by rw [pos_num.cast_mul, neg_mul_eq_neg_mul] | (neg a) (neg b) := show ↑(a * b) = -↑a * -↑b, by rw [pos_num.cast_mul, neg_mul_neg] theorem cast_mul [ring α] (m n) : ((m * n : znum) : α) = m * n := by rw [← cast_to_int, mul_to_int, int.cast_mul, cast_to_int, cast_to_int] theorem of_int'_neg : ∀ n : ℤ, of_int' (-n) = -of_int' n | -[1+ n] := show of_int' (n + 1 : ℕ) = _, by simp only [of_int', num.zneg_to_znum_neg] | 0 := show num.to_znum _ = -num.to_znum _, by rw [num.of_nat'_zero]; refl | (n+1 : ℕ) := show num.to_znum_neg _ = -num.to_znum _, by rw [num.zneg_to_znum]; refl theorem of_to_int' : ∀ (n : znum), znum.of_int' n = n | 0 := by erw [of_int', num.of_nat'_zero, num.to_znum] | (pos a) := by rw [cast_pos, ← pos_num.cast_to_nat, ← num.of_int'_to_znum, pos_num.of_to_nat]; refl | (neg a) := by rw [cast_neg, of_int'_neg, ← pos_num.cast_to_nat, ← num.of_int'_to_znum, pos_num.of_to_nat]; refl theorem to_int_inj {m n : znum} : (m : ℤ) = n ↔ m = n := ⟨λ h, function.left_inverse.injective of_to_int' h, congr_arg _⟩ theorem cmp_to_int : ∀ (m n), (ordering.cases_on (cmp m n) ((m:ℤ) < n) (m = n) ((n:ℤ) < m) : Prop) | 0 0 := rfl | (pos a) (pos b) := begin have := pos_num.cmp_to_nat a b; revert this; dsimp [cmp]; cases pos_num.cmp a b; dsimp; [simp, exact congr_arg pos, simp [gt]] end | (neg a) (neg b) := begin have := pos_num.cmp_to_nat b a; revert this; dsimp [cmp]; cases pos_num.cmp b a; dsimp; [simp, simp {contextual := tt}, simp [gt]] end | (pos a) 0 := pos_num.cast_pos _ | (pos a) (neg b) := lt_trans (neg_lt_zero.2 $ pos_num.cast_pos _) (pos_num.cast_pos _) | 0 (neg b) := neg_lt_zero.2 $ pos_num.cast_pos _ | (neg a) 0 := neg_lt_zero.2 $ pos_num.cast_pos _ | (neg a) (pos b) := lt_trans (neg_lt_zero.2 $ pos_num.cast_pos _) (pos_num.cast_pos _) | 0 (pos b) := pos_num.cast_pos _ @[norm_cast] theorem lt_to_int {m n : znum} : (m:ℤ) < n ↔ m < n := show (m:ℤ) < n ↔ cmp m n = ordering.lt, from match cmp m n, cmp_to_int m n with | ordering.lt, h := by simp at h; simp [h] | ordering.eq, h := by simp at h; simp [h, lt_irrefl]; exact dec_trivial | ordering.gt, h := by simp [not_lt_of_gt h]; exact dec_trivial end theorem le_to_int {m n : znum} : (m:ℤ) ≤ n ↔ m ≤ n := by rw ← not_lt; exact not_congr lt_to_int @[simp, norm_cast] theorem cast_lt [linear_ordered_ring α] {m n : znum} : (m:α) < n ↔ m < n := by rw [← cast_to_int m, ← cast_to_int n, int.cast_lt, lt_to_int] @[simp, norm_cast] theorem cast_le [linear_ordered_ring α] {m n : znum} : (m:α) ≤ n ↔ m ≤ n := by rw ← not_lt; exact not_congr cast_lt @[simp, norm_cast] theorem cast_inj [linear_ordered_ring α] {m n : znum} : (m:α) = n ↔ m = n := by rw [← cast_to_int m, ← cast_to_int n, int.cast_inj, to_int_inj] /-- This tactic tries to turn an (in)equality about `znum`s to one about `int`s by rewriting. ```lean example (n : znum) (m : znum) : n ≤ n + m * m := begin znum.transfer_rw, exact le_add_of_nonneg_right (mul_self_nonneg _) end ``` -/ meta def transfer_rw : tactic unit := `[repeat {rw ← to_int_inj <|> rw ← lt_to_int <|> rw ← le_to_int}, repeat {rw cast_add <|> rw mul_to_int <|> rw cast_one <|> rw cast_zero}] /-- This tactic tries to prove (in)equalities about `znum`s by transfering them to the `int` world and then trying to call `simp`. ```lean example (n : znum) (m : znum) : n ≤ n + m * m := begin znum.transfer, exact mul_self_nonneg _ end ``` -/ meta def transfer : tactic unit := `[intros, transfer_rw, try {simp [add_comm, add_left_comm, mul_comm, mul_left_comm]}] instance : linear_order znum := { lt := (<), lt_iff_le_not_le := by {intros a b, transfer_rw, apply lt_iff_le_not_le}, le := (≤), le_refl := by transfer, le_trans := by {intros a b c, transfer_rw, apply le_trans}, le_antisymm := by {intros a b, transfer_rw, apply le_antisymm}, le_total := by {intros a b, transfer_rw, apply le_total}, decidable_eq := znum.decidable_eq, decidable_le := znum.decidable_le, decidable_lt := znum.decidable_lt } instance : add_comm_group znum := { add := (+), add_assoc := by transfer, zero := 0, zero_add := zero_add, add_zero := add_zero, add_comm := by transfer, neg := has_neg.neg, add_left_neg := by transfer } instance : add_monoid_with_one znum := { one := 1, nat_cast := λ n, znum.of_int' n, nat_cast_zero := show (num.of_nat' 0).to_znum = 0, by rw num.of_nat'_zero; refl, nat_cast_succ := λ n, show (num.of_nat' (n+1)).to_znum = (num.of_nat' n).to_znum + 1, by rw [num.of_nat'_succ, num.add_one, num.to_znum_succ, znum.add_one], .. znum.add_comm_group } instance : linear_ordered_comm_ring znum := { mul := (*), mul_assoc := by transfer, one := 1, one_mul := by transfer, mul_one := by transfer, left_distrib := by {transfer, simp [mul_add]}, right_distrib := by {transfer, simp [mul_add, mul_comm]}, mul_comm := by transfer, exists_pair_ne := ⟨0, 1, dec_trivial⟩, add_le_add_left := by {intros a b h c, revert h, transfer_rw, exact λ h, add_le_add_left h c}, mul_pos := λ a b, show 0 < a → 0 < b → 0 < a * b, by {transfer_rw, apply mul_pos}, zero_le_one := dec_trivial, ..znum.linear_order, ..znum.add_comm_group, ..znum.add_monoid_with_one } @[simp, norm_cast] theorem cast_sub [ring α] (m n) : ((m - n : znum) : α) = m - n := by simp [sub_eq_neg_add] @[simp, norm_cast] theorem neg_of_int : ∀ n, ((-n : ℤ) : znum) = -n | (n+1:ℕ) := rfl | 0 := by rw [int.cast_neg, int.cast_zero] | -[1+n] := (zneg_zneg _).symm @[simp] theorem of_int'_eq : ∀ n : ℤ, znum.of_int' n = n | (n : ℕ) := rfl | -[1+ n] := begin show num.to_znum_neg (n+1 : ℕ) = -(n+1 : ℕ), rw [← neg_inj, neg_neg, nat.cast_succ, num.add_one, num.zneg_to_znum_neg, num.to_znum_succ, nat.cast_succ, znum.add_one], refl end @[simp] theorem of_nat_to_znum (n : ℕ) : num.to_znum n = n := rfl @[simp, norm_cast] theorem of_to_int (n : znum) : ((n : ℤ) : znum) = n := by rw [← of_int'_eq, of_to_int'] theorem to_of_int (n : ℤ) : ((n : znum) : ℤ) = n := int.induction_on' n 0 (by simp) (by simp) (by simp) @[simp] theorem of_nat_to_znum_neg (n : ℕ) : num.to_znum_neg n = -n := by rw [← of_nat_to_znum, num.zneg_to_znum] @[simp, norm_cast] theorem of_int_cast [add_group_with_one α] (n : ℤ) : ((n : znum) : α) = n := by rw [← cast_to_int, to_of_int] @[simp, norm_cast] theorem of_nat_cast [add_group_with_one α] (n : ℕ) : ((n : znum) : α) = n := by rw [← int.cast_coe_nat, of_int_cast, int.cast_coe_nat] @[simp, norm_cast] theorem dvd_to_int (m n : znum) : (m : ℤ) ∣ n ↔ m ∣ n := ⟨λ ⟨k, e⟩, ⟨k, by rw [← of_to_int n, e]; simp⟩, λ ⟨k, e⟩, ⟨k, by simp [e]⟩⟩ end znum namespace pos_num theorem divmod_to_nat_aux {n d : pos_num} {q r : num} (h₁ : (r:ℕ) + d * _root_.bit0 q = n) (h₂ : (r:ℕ) < 2 * d) : ((divmod_aux d q r).2 + d * (divmod_aux d q r).1 : ℕ) = ↑n ∧ ((divmod_aux d q r).2 : ℕ) < d := begin unfold divmod_aux, have : ∀ {r₂}, num.of_znum' (num.sub' r (num.pos d)) = some r₂ ↔ (r : ℕ) = r₂ + d, { intro r₂, apply num.mem_of_znum'.trans, rw [← znum.to_int_inj, num.cast_to_znum, num.cast_sub', sub_eq_iff_eq_add, ← int.coe_nat_inj'], simp }, cases e : num.of_znum' (num.sub' r (num.pos d)) with r₂; simp [divmod_aux], { refine ⟨h₁, lt_of_not_ge (λ h, _)⟩, cases nat.le.dest h with r₂ e', rw [← num.to_of_nat r₂, add_comm] at e', cases e.symm.trans (this.2 e'.symm) }, { have := this.1 e, split, { rwa [_root_.bit1, add_comm _ 1, mul_add, mul_one, ← add_assoc, ← this] }, { rwa [this, two_mul, add_lt_add_iff_right] at h₂ } } end theorem divmod_to_nat (d n : pos_num) : (n / d : ℕ) = (divmod d n).1 ∧ (n % d : ℕ) = (divmod d n).2 := begin rw nat.div_mod_unique (pos_num.cast_pos _), induction n with n IH n IH, { exact divmod_to_nat_aux (by simp; refl) (nat.mul_le_mul_left 2 (pos_num.cast_pos d : (0 : ℕ) < d)) }, { unfold divmod, cases divmod d n with q r, simp only [divmod] at IH ⊢, apply divmod_to_nat_aux; simp, { rw [_root_.bit1, _root_.bit1, add_right_comm, bit0_eq_two_mul (n : ℕ), ← IH.1, mul_add, ← bit0_eq_two_mul, mul_left_comm, ← bit0_eq_two_mul] }, { rw ← bit0_eq_two_mul, exact nat.bit1_lt_bit0 IH.2 } }, { unfold divmod, cases divmod d n with q r, simp only [divmod] at IH ⊢, apply divmod_to_nat_aux; simp, { rw [bit0_eq_two_mul (n : ℕ), ← IH.1, mul_add, ← bit0_eq_two_mul, mul_left_comm, ← bit0_eq_two_mul] }, { rw ← bit0_eq_two_mul, exact nat.bit0_lt IH.2 } } end @[simp] theorem div'_to_nat (n d) : (div' n d : ℕ) = n / d := (divmod_to_nat _ _).1.symm @[simp] theorem mod'_to_nat (n d) : (mod' n d : ℕ) = n % d := (divmod_to_nat _ _).2.symm end pos_num namespace num @[simp] protected lemma div_zero (n : num) : n / 0 = 0 := show n.div 0 = 0, by { cases n, refl, simp [num.div] } @[simp, norm_cast] theorem div_to_nat : ∀ n d, ((n / d : num) : ℕ) = n / d | 0 0 := by simp | 0 (pos d) := (nat.zero_div _).symm | (pos n) 0 := (nat.div_zero _).symm | (pos n) (pos d) := pos_num.div'_to_nat _ _ @[simp] protected lemma mod_zero (n : num) : n % 0 = n := show n.mod 0 = n, by { cases n, refl, simp [num.mod] } @[simp, norm_cast] theorem mod_to_nat : ∀ n d, ((n % d : num) : ℕ) = n % d | 0 0 := by simp | 0 (pos d) := (nat.zero_mod _).symm | (pos n) 0 := (nat.mod_zero _).symm | (pos n) (pos d) := pos_num.mod'_to_nat _ _ theorem gcd_to_nat_aux : ∀ {n} {a b : num}, a ≤ b → (a * b).nat_size ≤ n → (gcd_aux n a b : ℕ) = nat.gcd a b | 0 0 b ab h := (nat.gcd_zero_left _).symm | 0 (pos a) 0 ab h := (not_lt_of_ge ab).elim rfl | 0 (pos a) (pos b) ab h := (not_lt_of_le h).elim $ pos_num.nat_size_pos _ | (nat.succ n) 0 b ab h := (nat.gcd_zero_left _).symm | (nat.succ n) (pos a) b ab h := begin simp [gcd_aux], rw [nat.gcd_rec, gcd_to_nat_aux, mod_to_nat], {refl}, { rw [← le_to_nat, mod_to_nat], exact le_of_lt (nat.mod_lt _ (pos_num.cast_pos _)) }, rw [nat_size_to_nat, mul_to_nat, nat.size_le] at h ⊢, rw [mod_to_nat, mul_comm], rw [pow_succ', ← nat.mod_add_div b (pos a)] at h, refine lt_of_mul_lt_mul_right (lt_of_le_of_lt _ h) (nat.zero_le 2), rw [mul_two, mul_add], refine add_le_add_left (nat.mul_le_mul_left _ (le_trans (le_of_lt (nat.mod_lt _ (pos_num.cast_pos _))) _)) _, suffices : 1 ≤ _, simpa using nat.mul_le_mul_left (pos a) this, rw [nat.le_div_iff_mul_le a.cast_pos, one_mul], exact le_to_nat.2 ab end @[simp] theorem gcd_to_nat : ∀ a b, (gcd a b : ℕ) = nat.gcd a b := have ∀ a b : num, (a * b).nat_size ≤ a.nat_size + b.nat_size, begin intros, simp [nat_size_to_nat], rw [nat.size_le, pow_add], exact mul_lt_mul'' (nat.lt_size_self _) (nat.lt_size_self _) (nat.zero_le _) (nat.zero_le _) end, begin intros, unfold gcd, split_ifs, { exact gcd_to_nat_aux h (this _ _) }, { rw nat.gcd_comm, exact gcd_to_nat_aux (le_of_not_le h) (this _ _) } end theorem dvd_iff_mod_eq_zero {m n : num} : m ∣ n ↔ n % m = 0 := by rw [← dvd_to_nat, nat.dvd_iff_mod_eq_zero, ← to_nat_inj, mod_to_nat]; refl instance decidable_dvd : decidable_rel ((∣) : num → num → Prop) | a b := decidable_of_iff' _ dvd_iff_mod_eq_zero end num instance pos_num.decidable_dvd : decidable_rel ((∣) : pos_num → pos_num → Prop) | a b := num.decidable_dvd _ _ namespace znum @[simp] protected lemma div_zero (n : znum) : n / 0 = 0 := show n.div 0 = 0, by cases n; refl <|> simp [znum.div] @[simp, norm_cast] theorem div_to_int : ∀ n d, ((n / d : znum) : ℤ) = n / d | 0 0 := by simp [int.div_zero] | 0 (pos d) := (int.zero_div _).symm | 0 (neg d) := (int.zero_div _).symm | (pos n) 0 := (int.div_zero _).symm | (neg n) 0 := (int.div_zero _).symm | (pos n) (pos d) := (num.cast_to_znum _).trans $ by rw ← num.to_nat_to_int; simp | (pos n) (neg d) := (num.cast_to_znum_neg _).trans $ by rw ← num.to_nat_to_int; simp | (neg n) (pos d) := show - _ = (-_/↑d), begin rw [n.to_int_eq_succ_pred, d.to_int_eq_succ_pred, ← pos_num.to_nat_to_int, num.succ'_to_nat, num.div_to_nat], change -[1+ n.pred' / ↑d] = -[1+ n.pred' / (d.pred' + 1)], rw d.to_nat_eq_succ_pred end | (neg n) (neg d) := show ↑(pos_num.pred' n / num.pos d).succ' = (-_ / -↑d), begin rw [n.to_int_eq_succ_pred, d.to_int_eq_succ_pred, ← pos_num.to_nat_to_int, num.succ'_to_nat, num.div_to_nat], change (nat.succ (_/d) : ℤ) = nat.succ (n.pred'/(d.pred' + 1)), rw d.to_nat_eq_succ_pred end @[simp, norm_cast] theorem mod_to_int : ∀ n d, ((n % d : znum) : ℤ) = n % d | 0 d := (int.zero_mod _).symm | (pos n) d := (num.cast_to_znum _).trans $ by rw [← num.to_nat_to_int, cast_pos, num.mod_to_nat, ← pos_num.to_nat_to_int, abs_to_nat]; refl | (neg n) d := (num.cast_sub' _ _).trans $ by rw [← num.to_nat_to_int, cast_neg, ← num.to_nat_to_int, num.succ_to_nat, num.mod_to_nat, abs_to_nat, ← int.sub_nat_nat_eq_coe, n.to_int_eq_succ_pred]; refl @[simp] theorem gcd_to_nat (a b) : (gcd a b : ℕ) = int.gcd a b := (num.gcd_to_nat _ _).trans $ by simpa theorem dvd_iff_mod_eq_zero {m n : znum} : m ∣ n ↔ n % m = 0 := by rw [← dvd_to_int, int.dvd_iff_mod_eq_zero, ← to_int_inj, mod_to_int]; refl instance : decidable_rel ((∣) : znum → znum → Prop) | a b := decidable_of_iff' _ dvd_iff_mod_eq_zero end znum namespace int /-- Cast a `snum` to the corresponding integer. -/ def of_snum : snum → ℤ := snum.rec' (λ a, cond a (-1) 0) (λa p IH, cond a (bit1 IH) (bit0 IH)) instance snum_coe : has_coe snum ℤ := ⟨of_snum⟩ end int instance : has_lt snum := ⟨λa b, (a : ℤ) < b⟩ instance : has_le snum := ⟨λa b, (a : ℤ) ≤ b⟩
475199f3fd9c455814236a43fca1886eafdf57c3
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/finset/powerset.lean
fc7401ccb03fcfeb51a11035a61ca899108ea30e
[ "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,861
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.finset.lattice /-! # The powerset of a finset -/ namespace finset open function multiset variables {α : Type*} {s t : finset α} /-! ### powerset -/ section powerset /-- When `s` is a finset, `s.powerset` is the finset of all subsets of `s` (seen as finsets). -/ def powerset (s : finset α) : finset (finset α) := ⟨s.1.powerset.pmap finset.mk $ λ t h, nodup_of_le (mem_powerset.1 h) s.nodup, s.nodup.powerset.pmap $ λ a ha b hb, congr_arg finset.val⟩ @[simp] theorem mem_powerset {s t : finset α} : s ∈ powerset t ↔ s ⊆ t := by cases s; simp only [powerset, mem_mk, mem_pmap, mem_powerset, exists_prop, exists_eq_right]; rw ← val_le_iff @[simp, norm_cast] lemma coe_powerset (s : finset α) : (s.powerset : set (finset α)) = coe ⁻¹' (s : set α).powerset := by { ext, simp } @[simp] theorem empty_mem_powerset (s : finset α) : ∅ ∈ powerset s := mem_powerset.2 (empty_subset _) @[simp] lemma mem_powerset_self (s : finset α) : s ∈ powerset s := mem_powerset.2 subset.rfl lemma powerset_nonempty (s : finset α) : s.powerset.nonempty := ⟨∅, empty_mem_powerset _⟩ @[simp] theorem powerset_mono {s t : finset α} : powerset s ⊆ powerset t ↔ s ⊆ t := ⟨λ h, (mem_powerset.1 $ h $ mem_powerset_self _), λ st u h, mem_powerset.2 $ subset.trans (mem_powerset.1 h) st⟩ lemma powerset_injective : injective (powerset : finset α → finset (finset α)) := injective_of_le_imp_le _ $ λ s t, powerset_mono.1 @[simp] lemma powerset_inj : powerset s = powerset t ↔ s = t := powerset_injective.eq_iff @[simp] lemma powerset_empty : (∅ : finset α).powerset = {∅} := rfl @[simp] lemma powerset_eq_singleton_empty : s.powerset = {∅} ↔ s = ∅ := by rw [←powerset_empty, powerset_inj] /-- **Number of Subsets of a Set** -/ @[simp] theorem card_powerset (s : finset α) : card (powerset s) = 2 ^ card s := (card_pmap _ _ _).trans (card_powerset s.1) lemma not_mem_of_mem_powerset_of_not_mem {s t : finset α} {a : α} (ht : t ∈ s.powerset) (h : a ∉ s) : a ∉ t := by { apply mt _ h, apply mem_powerset.1 ht } lemma powerset_insert [decidable_eq α] (s : finset α) (a : α) : powerset (insert a s) = s.powerset ∪ s.powerset.image (insert a) := begin ext t, simp only [exists_prop, mem_powerset, mem_image, mem_union, subset_insert_iff], by_cases h : a ∈ t, { split, { exact λH, or.inr ⟨_, H, insert_erase h⟩ }, { intros H, cases H, { exact subset.trans (erase_subset a t) H }, { rcases H with ⟨u, hu⟩, rw ← hu.2, exact subset.trans (erase_insert_subset a u) hu.1 } } }, { have : ¬ ∃ (u : finset α), u ⊆ s ∧ insert a u = t, by simp [ne.symm (ne_insert_of_not_mem _ _ h)], simp [finset.erase_eq_of_not_mem h, this] } end /-- For predicate `p` decidable on subsets, it is decidable whether `p` holds for any subset. -/ instance decidable_exists_of_decidable_subsets {s : finset α} {p : Π t ⊆ s, Prop} [Π t (h : t ⊆ s), decidable (p t h)] : decidable (∃ t (h : t ⊆ s), p t h) := decidable_of_iff (∃ t (hs : t ∈ s.powerset), p t (mem_powerset.1 hs)) ⟨(λ ⟨t, _, hp⟩, ⟨t, _, hp⟩), (λ ⟨t, hs, hp⟩, ⟨t, mem_powerset.2 hs, hp⟩)⟩ /-- For predicate `p` decidable on subsets, it is decidable whether `p` holds for every subset. -/ instance decidable_forall_of_decidable_subsets {s : finset α} {p : Π t ⊆ s, Prop} [Π t (h : t ⊆ s), decidable (p t h)] : decidable (∀ t (h : t ⊆ s), p t h) := decidable_of_iff (∀ t (h : t ∈ s.powerset), p t (mem_powerset.1 h)) ⟨(λ h t hs, h t (mem_powerset.2 hs)), (λ h _ _, h _ _)⟩ /-- A version of `finset.decidable_exists_of_decidable_subsets` with a non-dependent `p`. Typeclass inference cannot find `hu` here, so this is not an instance. -/ def decidable_exists_of_decidable_subsets' {s : finset α} {p : finset α → Prop} (hu : Π t (h : t ⊆ s), decidable (p t)) : decidable (∃ t (h : t ⊆ s), p t) := @finset.decidable_exists_of_decidable_subsets _ _ _ hu /-- A version of `finset.decidable_forall_of_decidable_subsets` with a non-dependent `p`. Typeclass inference cannot find `hu` here, so this is not an instance. -/ def decidable_forall_of_decidable_subsets' {s : finset α} {p : finset α → Prop} (hu : Π t (h : t ⊆ s), decidable (p t)) : decidable (∀ t (h : t ⊆ s), p t) := @finset.decidable_forall_of_decidable_subsets _ _ _ hu end powerset section ssubsets variables [decidable_eq α] /-- For `s` a finset, `s.ssubsets` is the finset comprising strict subsets of `s`. -/ def ssubsets (s : finset α) : finset (finset α) := erase (powerset s) s @[simp] lemma mem_ssubsets {s t : finset α} : t ∈ s.ssubsets ↔ t ⊂ s := by rw [ssubsets, mem_erase, mem_powerset, ssubset_iff_subset_ne, and.comm] lemma empty_mem_ssubsets {s : finset α} (h : s.nonempty) : ∅ ∈ s.ssubsets := by { rw [mem_ssubsets, ssubset_iff_subset_ne], exact ⟨empty_subset s, h.ne_empty.symm⟩, } /-- For predicate `p` decidable on ssubsets, it is decidable whether `p` holds for any ssubset. -/ instance decidable_exists_of_decidable_ssubsets {s : finset α} {p : Π t ⊂ s, Prop} [Π t (h : t ⊂ s), decidable (p t h)] : decidable (∃ t h, p t h) := decidable_of_iff (∃ t (hs : t ∈ s.ssubsets), p t (mem_ssubsets.1 hs)) ⟨(λ ⟨t, _, hp⟩, ⟨t, _, hp⟩), (λ ⟨t, hs, hp⟩, ⟨t, mem_ssubsets.2 hs, hp⟩)⟩ /-- For predicate `p` decidable on ssubsets, it is decidable whether `p` holds for every ssubset. -/ instance decidable_forall_of_decidable_ssubsets {s : finset α} {p : Π t ⊂ s, Prop} [Π t (h : t ⊂ s), decidable (p t h)] : decidable (∀ t h, p t h) := decidable_of_iff (∀ t (h : t ∈ s.ssubsets), p t (mem_ssubsets.1 h)) ⟨(λ h t hs, h t (mem_ssubsets.2 hs)), (λ h _ _, h _ _)⟩ /-- A version of `finset.decidable_exists_of_decidable_ssubsets` with a non-dependent `p`. Typeclass inference cannot find `hu` here, so this is not an instance. -/ def decidable_exists_of_decidable_ssubsets' {s : finset α} {p : finset α → Prop} (hu : Π t (h : t ⊂ s), decidable (p t)) : decidable (∃ t (h : t ⊂ s), p t) := @finset.decidable_exists_of_decidable_ssubsets _ _ _ _ hu /-- A version of `finset.decidable_forall_of_decidable_ssubsets` with a non-dependent `p`. Typeclass inference cannot find `hu` here, so this is not an instance. -/ def decidable_forall_of_decidable_ssubsets' {s : finset α} {p : finset α → Prop} (hu : Π t (h : t ⊂ s), decidable (p t)) : decidable (∀ t (h : t ⊂ s), p t) := @finset.decidable_forall_of_decidable_ssubsets _ _ _ _ hu end ssubsets section powerset_len /-- Given an integer `n` and a finset `s`, then `powerset_len n s` is the finset of subsets of `s` of cardinality `n`. -/ def powerset_len (n : ℕ) (s : finset α) : finset (finset α) := ⟨(s.1.powerset_len n).pmap finset.mk $ λ t h, nodup_of_le (mem_powerset_len.1 h).1 s.2, s.2.powerset_len.pmap $ λ a ha b hb, congr_arg finset.val⟩ /-- **Formula for the Number of Combinations** -/ theorem mem_powerset_len {n} {s t : finset α} : s ∈ powerset_len n t ↔ s ⊆ t ∧ card s = n := by cases s; simp [powerset_len, val_le_iff.symm]; refl @[simp] theorem powerset_len_mono {n} {s t : finset α} (h : s ⊆ t) : powerset_len n s ⊆ powerset_len n t := λ u h', mem_powerset_len.2 $ and.imp (λ h₂, subset.trans h₂ h) id (mem_powerset_len.1 h') /-- **Formula for the Number of Combinations** -/ @[simp] theorem card_powerset_len (n : ℕ) (s : finset α) : card (powerset_len n s) = nat.choose (card s) n := (card_pmap _ _ _).trans (card_powerset_len n s.1) @[simp] lemma powerset_len_zero (s : finset α) : finset.powerset_len 0 s = {∅} := begin ext, rw [mem_powerset_len, mem_singleton, card_eq_zero], refine ⟨λ h, h.2, λ h, by { rw h, exact ⟨empty_subset s, rfl⟩ }⟩, end @[simp] theorem powerset_len_empty (n : ℕ) {s : finset α} (h : s.card < n) : powerset_len n s = ∅ := finset.card_eq_zero.mp (by rw [card_powerset_len, nat.choose_eq_zero_of_lt h]) theorem powerset_len_eq_filter {n} {s : finset α} : powerset_len n s = (powerset s).filter (λ x, x.card = n) := by { ext, simp [mem_powerset_len] } lemma powerset_len_succ_insert [decidable_eq α] {x : α} {s : finset α} (h : x ∉ s) (n : ℕ) : powerset_len n.succ (insert x s) = powerset_len n.succ s ∪ (powerset_len n s).image (insert x) := begin rw [powerset_len_eq_filter, powerset_insert, filter_union, ←powerset_len_eq_filter], congr, rw [powerset_len_eq_filter, image_filter], congr' 1, ext t, simp only [mem_powerset, mem_filter, function.comp_app, and.congr_right_iff], intro ht, have : x ∉ t := λ H, h (ht H), simp [card_insert_of_not_mem this, nat.succ_inj'] end lemma powerset_len_nonempty {n : ℕ} {s : finset α} (h : n ≤ s.card) : (powerset_len n s).nonempty := begin classical, induction s using finset.induction_on with x s hx IH generalizing n, { rw [card_empty, le_zero_iff] at h, rw [h, powerset_len_zero], exact finset.singleton_nonempty _, }, { cases n, { simp }, { rw [card_insert_of_not_mem hx, nat.succ_le_succ_iff] at h, rw powerset_len_succ_insert hx, refine nonempty.mono _ ((IH h).image (insert x)), convert (subset_union_right _ _) } } end @[simp] lemma powerset_len_self (s : finset α) : powerset_len s.card s = {s} := begin ext, rw [mem_powerset_len, mem_singleton], split, { exact λ ⟨hs, hc⟩, eq_of_subset_of_card_le hs hc.ge }, { rintro rfl, simp } end lemma powerset_card_bUnion [decidable_eq (finset α)] (s : finset α) : finset.powerset s = (range (s.card + 1)).bUnion (λ i, powerset_len i s) := begin refine ext (λ a, ⟨λ ha, _, λ ha, _ ⟩), { rw mem_bUnion, exact ⟨a.card, mem_range.mpr (nat.lt_succ_of_le (card_le_of_subset (mem_powerset.mp ha))), mem_powerset_len.mpr ⟨mem_powerset.mp ha, rfl⟩⟩ }, { rcases mem_bUnion.mp ha with ⟨i, hi, ha⟩, exact mem_powerset.mpr (mem_powerset_len.mp ha).1, } end lemma powerset_len_sup [decidable_eq α] (u : finset α) (n : ℕ) (hn : n < u.card) : (powerset_len n.succ u).sup id = u := begin apply le_antisymm, { simp_rw [finset.sup_le_iff, mem_powerset_len], rintros x ⟨h, -⟩, exact h }, { rw [sup_eq_bUnion, le_iff_subset, subset_iff], cases (nat.succ_le_of_lt hn).eq_or_lt with h' h', { simp [h'] }, { intros x hx, simp only [mem_bUnion, exists_prop, id.def], obtain ⟨t, ht⟩ : ∃ t, t ∈ powerset_len n (u.erase x) := powerset_len_nonempty _, { refine ⟨insert x t, _, mem_insert_self _ _⟩, rw [←insert_erase hx, powerset_len_succ_insert (not_mem_erase _ _)], exact mem_union_right _ (mem_image_of_mem _ ht) }, { rw [card_erase_of_mem hx], exact nat.le_pred_of_lt hn, } } } end @[simp] lemma powerset_len_card_add (s : finset α) {i : ℕ} (hi : 0 < i) : s.powerset_len (s.card + i) = ∅ := finset.powerset_len_empty _ (lt_add_of_pos_right (finset.card s) hi) @[simp] theorem map_val_val_powerset_len (s : finset α) (i : ℕ) : (s.powerset_len i).val.map finset.val = s.1.powerset_len i := by simp [finset.powerset_len, map_pmap, pmap_eq_map, map_id'] theorem powerset_len_map {β : Type*} (f : α ↪ β) (n : ℕ) (s : finset α) : powerset_len n (s.map f) = (powerset_len n s).map (map_embedding f).to_embedding := eq_of_veq $ multiset.map_injective (@eq_of_veq _) $ by simp_rw [map_val_val_powerset_len, map_val, multiset.map_map, function.comp, rel_embedding.coe_fn_to_embedding, map_embedding_apply, map_val, ←multiset.map_map _ val, map_val_val_powerset_len, multiset.powerset_len_map] end powerset_len end finset
2d4b906991f0f58a3405a57b6ff0b5789a3666d7
abd677583c7e4d55daf9487b82da11b7c5498d8d
/src/prop.lean
b543f25a0d2c872eb1da329f8d5218cb79ad834f
[ "Apache-2.0" ]
permissive
jesse-michael-han/embed
e9c346918ad58e03933bdaa057a571c0cc4a7641
c2fc188328e871e18e0dcb8258c6d0462c70a8c9
refs/heads/master
1,584,677,705,005
1,528,451,877,000
1,528,451,877,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,759
lean
import .exp .print namespace prop variable {α : Type} class is_symb (α : Type) := (true : α) (false : α) (not : α) (and : α) (or : α) (imp : α) def true [is_symb α] := @exp.cst α (is_symb.true α) notation `⊤'` := true def false [is_symb α] := @exp.cst α (is_symb.false α) notation `⊥'` := false def not [is_symb α] (p : exp α) := exp.app (exp.cst (is_symb.false α)) p notation `¬'` p := not p def and [is_symb α] (p q : exp α) := exp.app (exp.app (exp.cst (is_symb.and α)) p) q notation p `∧'` q := and p q def or [is_symb α] (p q : exp α) := exp.app (exp.app (exp.cst (is_symb.or α)) p) q notation p `∨'` q := or p q def imp [is_symb α] (p q : exp α) := exp.app (exp.app (exp.cst (is_symb.imp α)) p) q notation p `→'` q := imp p q inductive symb : Type | atom : string → symb | true : symb | false : symb | not : symb | and : symb | or : symb | imp : symb instance : decidable_eq symb := by tactic.mk_dec_eq_instance instance : is_symb symb := { true := symb.true, false := symb.false, not := symb.not, and := symb.and, or := symb.or, imp := symb.imp } def A' (s : string) := @exp.cst symb (symb.atom s) inductive inf [is_symb α] : list (seq α) → seq α → Prop | id : ∀ Γ Δ p, inf [] (p::Γ ==> p::Δ) | truer : ∀ Γ Δ, inf [] (Γ ==> ⊤'::Δ) | falsel : ∀ Γ Δ, inf [] (⊥'::Γ ==> Δ) | andl : ∀ Γ Δ p q, inf [(p ∧' q)::Γ ==> Δ] (p::q::Γ ==> Δ) | andr : ∀ Γ Δ p q, inf [Γ ==> p::Δ, Γ ==> q::Δ] (Γ ==> (p ∧' q)::Δ) | orl : ∀ Γ Δ p q, inf [p::Γ ==> Δ, q::Γ ==> Δ] ((p ∨' q)::Γ ==> Δ) | orr : ∀ Γ Δ p q, inf [Γ ==> p::q::Δ] (Γ ==> (p ∨' q)::Δ) | impl : ∀ Γ Δ p q, inf [Γ ==> p::Δ, q::Γ ==> Δ] ((p →' q)::Γ ==> Δ) | impr : ∀ Γ Δ p q, inf [p::Γ ==> q::Δ] (Γ ==> (p →' q)::Δ) | wl : ∀ Γ Δ p, inf [Γ ==> Δ] (p::Γ ==> Δ) | wr : ∀ Γ Δ p, inf [Γ ==> Δ] (Γ ==> p::Δ) | cl : ∀ Γ Δ p, inf [p::p::Γ ==> Δ] (p::Γ ==> Δ) | cr : ∀ Γ Δ p, inf [Γ ==> p::p::Δ] (Γ ==> p::Δ) | pl : ∀ Γ Γ' Δ, Γ ~ Γ' → inf [Γ ==> Δ] (Γ' ==> Δ) | pr : ∀ Γ Δ Δ', Δ ~ Δ' → inf [Γ ==> Δ] (Γ ==> Δ') inductive thm [is_symb α] : (seq α) → Prop | inf : ∀ {s S}, inf S s → (∀ s' ∈ S, thm s') → thm s /- Derived rules -/ open list lemma thm.id [is_symb α] : ∀ (Γ Δ : list (exp α)) p, thm (p::Γ ==> p::Δ) := begin intros Γ Δ p, apply thm.inf, apply inf.id, apply forall_mem_nil end lemma thm.truer [is_symb α] : ∀ (Γ Δ : list (exp α)), thm (Γ ==> ⊤'::Δ) := begin intros Γ Δ, apply thm.inf, apply inf.truer, apply forall_mem_nil end lemma thm.falsel [is_symb α] : ∀ (Γ Δ : list (exp α)), thm (⊥'::Γ ==> Δ) := begin intros Γ Δ, apply thm.inf, apply inf.falsel, apply forall_mem_nil end lemma thm.andl [is_symb α] : ∀ (Γ Δ : list (exp α)) p q, thm ((p ∧' q)::Γ ==> Δ) → thm (p::q::Γ ==> Δ) := begin intros Γ Δ p q h, apply thm.inf, apply inf.andl, rewrite forall_mem_singleton, apply h end lemma thm.andr [is_symb α] : ∀ (Γ Δ : list (exp α)) p q, thm (Γ ==> p::Δ) → thm (Γ ==> q::Δ) → thm (Γ ==> (p ∧' q)::Δ) := begin intros Γ Δ p q h1 h2, apply thm.inf, apply inf.andr, intros s hs, rewrite mem_cons_iff at hs, cases hs with hs hs, rewrite hs, apply h1, rewrite mem_singleton at hs, rewrite hs, apply h2 end lemma thm.orl [is_symb α] : ∀ (Γ Δ : list (exp α)) p q, thm (p::Γ ==> Δ) → thm (q::Γ ==> Δ) → thm ((p ∨' q)::Γ ==> Δ) := begin intros Γ Δ p q h1 h2, apply thm.inf, apply inf.orl, intros s hs, rewrite mem_cons_iff at hs, cases hs with hs hs, rewrite hs, apply h1, rewrite mem_singleton at hs, rewrite hs, apply h2 end lemma thm.orr [is_symb α] : ∀ (Γ Δ : list (exp α)) p q, thm (Γ ==> p::q::Δ) → thm (Γ ==> (p ∨' q)::Δ) := begin intros Γ Δ p q h, apply thm.inf, apply inf.orr, rewrite forall_mem_singleton, apply h end lemma thm.impl [is_symb α] : ∀ (Γ Δ : list (exp α)) p q, thm (Γ ==> p::Δ) → thm (q::Γ ==> Δ) → thm ((p →' q)::Γ ==> Δ) := begin intros Γ Δ p q h1 h2, apply thm.inf, apply inf.impl, intros s hs, rewrite mem_cons_iff at hs, cases hs with hs hs, rewrite hs, apply h1, rewrite mem_singleton at hs, rewrite hs, apply h2 end lemma thm.impr [is_symb α] : ∀ (Γ Δ : list (exp α)) p q, thm (p::Γ ==> q::Δ) → thm (Γ ==> (p →' q)::Δ) := begin intros Γ Δ p q h, apply thm.inf, apply inf.impr, rewrite forall_mem_singleton, apply h end lemma thm.wl [is_symb α] : ∀ (Γ Δ : list (exp α)) p, thm (Γ ==> Δ) → thm (p::Γ ==> Δ) := begin intros Γ Δ p h, apply thm.inf, apply inf.wl, rewrite forall_mem_singleton, apply h end lemma thm.wr [is_symb α] : ∀ (Γ Δ : list (exp α)) p, thm (Γ ==> Δ) → thm (Γ ==> p::Δ) := begin intros Γ Δ p h, apply thm.inf, apply inf.wr, rewrite forall_mem_singleton, apply h end lemma thm.cl [is_symb α] : ∀ (Γ Δ : list (exp α)) p, thm (p::p::Γ ==> Δ) → thm (p::Γ ==> Δ) := begin intros Γ Δ p h, apply thm.inf, apply inf.cl, rewrite forall_mem_singleton, apply h end lemma thm.cr [is_symb α] : ∀ (Γ Δ : list (exp α)) p, thm (Γ ==> p::p::Δ) → thm (Γ ==> p::Δ) := begin intros Γ Δ p h, apply thm.inf, apply inf.cr, rewrite forall_mem_singleton, apply h end lemma thm.rl [is_symb α] : ∀ n (Γ Δ : list (exp α)), thm (rotate n Γ ==> Δ) → thm (Γ ==> Δ) := begin intros n Γ Δ h, apply thm.inf, apply inf.pl (rotate n Γ), apply perm_rotate, intros s' hs', rewrite list.mem_singleton at hs', rewrite hs', apply h end lemma thm.rr [is_symb α] : ∀ n (Γ Δ : list (exp α)), thm (Γ ==> rotate n Δ) → thm (Γ ==> Δ) := begin intros n Γ Δ h, apply thm.inf, apply inf.pr _ (rotate n Δ), apply perm_rotate, intros s' hs', rewrite list.mem_singleton at hs', rewrite hs', apply h end /- Printing -/ open expr tactic meta def getsqt : tactic (list (exp symb) × list (exp symb)) := do `(thm (%%Γe ==> %%Δe)) ← tactic.target, Γ ← eval_expr (list (exp symb)) Γe, Δ ← eval_expr (list (exp symb)) Δe, return (Γ,Δ) def fml2str : exp symb → string | (exp.app (exp.cst s) p) := if s = (is_symb.not symb) then "¬" ++ fml2str p else "ERROR" | (exp.app (exp.app (exp.cst s) p) q) := if s = (is_symb.and symb) then "(" ++ fml2str p ++ " ∧ " ++ fml2str q ++ ")" else if s = (is_symb.or symb) then "(" ++ fml2str p ++ " ∨ " ++ fml2str q ++ ")" else if s = (is_symb.imp symb) then "(" ++ fml2str p ++ " → " ++ fml2str q ++ ")" else "ERROR" | (exp.cst s) := if s = is_symb.true symb then "⊤" else if s = is_symb.false symb then "⊥" else match s with | (symb.atom str) := str | _ := "ERROR" end | _ := "ERROR" meta def showgoal : tactic unit := (do (Γ,Δ) ← getsqt, trace (sqt2str fml2str Γ Δ)) <|> trace "No Goals" /- Examples (Hilbert System Axioms) -/ example : thm ([] ==> [A' "φ" →' A' "φ"]) := begin showgoal, apply thm.impr, apply thm.id, showgoal end example : thm ([] ==> [A' "φ" →' (A' "ψ" →' A' "φ")]) := begin showgoal, apply thm.impr, apply thm.impr, apply thm.rl 1, apply thm.id, showgoal end example : thm ([] ==> [(A' "φ" →' (A' "ψ" →' A' "χ")) →' ((A' "φ" →' A' "ψ") →' (A' "φ" →' A' "χ"))]) := begin showgoal, apply thm.impr, apply thm.impr, apply thm.impr, apply thm.rl 2, apply thm.impl, apply thm.rl 1, apply thm.id, apply thm.impl, apply thm.impl, apply thm.id, apply thm.id, apply thm.id, showgoal end end prop
bcff684ca073661a950a423dcd9c6266c88be115
49bd2218ae088932d847f9030c8dbff1c5607bb7
/src/topology/metric_space/gromov_hausdorff.lean
9897180fdc3f9bc2da389ba1589e27a730271e9b
[ "Apache-2.0" ]
permissive
FredericLeRoux/mathlib
e8f696421dd3e4edc8c7edb3369421c8463d7bac
3645bf8fb426757e0a20af110d1fdded281d286e
refs/heads/master
1,607,062,870,732
1,578,513,186,000
1,578,513,186,000
231,653,181
0
0
Apache-2.0
1,578,080,327,000
1,578,080,326,000
null
UTF-8
Lean
false
false
56,716
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Sébastien Gouëzel -/ import topology.metric_space.closeds set_theory.cardinal topology.metric_space.gromov_hausdorff_realized topology.metric_space.completion /-! # Gromov-Hausdorff distance This file defines the Gromov-Hausdorff distance on the space of nonempty compact metric spaces up to isometry. We introduce the space of all nonempty compact metric spaces, up to isometry, called `GH_space`, and endow it with a metric space structure. The distance, known as the Gromov-Hausdorff distance, is defined as follows: given two nonempty compact spaces `X` and `Y`, their distance is the minimum Hausdorff distance between all possible isometric embeddings of `X` and `Y` in all metric spaces. To define properly the Gromov-Hausdorff space, we consider the non-empty compact subsets of `ℓ^∞(ℝ)` up to isometry, which is a well-defined type, and define the distance as the infimum of the Hausdorff distance over all embeddings in `ℓ^∞(ℝ)`. We prove that this coincides with the previous description, as all separable metric spaces embed isometrically into `ℓ^∞(ℝ)`, through an embedding called the Kuratowski embedding. To prove that we have a distance, we should show that if spaces can be coupled to be arbitrarily close, then they are isometric. More generally, the Gromov-Hausdorff distance is realized, i.e., there is a coupling for which the Hausdorff distance is exactly the Gromov-Hausdorff distance. This follows from a compactness argument, essentially following from Arzela-Ascoli. ## Main results We prove the most important properties of the Gromov-Hausdorff space: it is a polish space, i.e., it is complete and second countable. We also prove the Gromov compactness criterion. -/ noncomputable theory open_locale classical topological_space universes u v w open classical lattice set function topological_space filter metric quotient open bounded_continuous_function nat Kuratowski_embedding open sum (inl inr) set_option class.instance_max_depth 50 local attribute [instance] metric_space_sum namespace Gromov_Hausdorff section GH_space /- In this section, we define the Gromov-Hausdorff space, denoted `GH_space` as the quotient of nonempty compact subsets of `ℓ^∞(ℝ)` by identifying isometric sets. Using the Kuratwoski embedding, we get a canonical map `to_GH_space` mapping any nonempty compact type to `GH_space`. -/ /-- Equivalence relation identifying two nonempty compact sets which are isometric -/ private definition isometry_rel : nonempty_compacts ℓ_infty_ℝ → nonempty_compacts ℓ_infty_ℝ → Prop := λx y, nonempty (x.val ≃ᵢ y.val) /-- This is indeed an equivalence relation -/ private lemma is_equivalence_isometry_rel : equivalence isometry_rel := ⟨λx, ⟨isometric.refl _⟩, λx y ⟨e⟩, ⟨e.symm⟩, λ x y z ⟨e⟩ ⟨f⟩, ⟨e.trans f⟩⟩ /-- setoid instance identifying two isometric nonempty compact subspaces of ℓ^∞(ℝ) -/ instance isometry_rel.setoid : setoid (nonempty_compacts ℓ_infty_ℝ) := setoid.mk isometry_rel is_equivalence_isometry_rel /-- The Gromov-Hausdorff space -/ definition GH_space : Type := quotient (isometry_rel.setoid) /-- Map any nonempty compact type to `GH_space` -/ definition to_GH_space (α : Type u) [metric_space α] [compact_space α] [nonempty α] : GH_space := ⟦nonempty_compacts.Kuratowski_embedding α⟧ /-- A metric space representative of any abstract point in `GH_space` -/ definition GH_space.rep (p : GH_space) : Type := (quot.out p).val lemma eq_to_GH_space_iff {α : Type u} [metric_space α] [compact_space α] [nonempty α] {p : nonempty_compacts ℓ_infty_ℝ} : ⟦p⟧ = to_GH_space α ↔ ∃Ψ : α → ℓ_infty_ℝ, isometry Ψ ∧ range Ψ = p.val := begin simp only [to_GH_space, quotient.eq], split, { assume h, rcases setoid.symm h with ⟨e⟩, have f := (Kuratowski_embedding.isometry α).isometric_on_range.trans e, use λx, f x, split, { apply isometry_subtype_val.comp f.isometry }, { rw [range_comp, f.range_coe, set.image_univ, set.range_coe_subtype] } }, { rintros ⟨Ψ, ⟨isomΨ, rangeΨ⟩⟩, have f := ((Kuratowski_embedding.isometry α).isometric_on_range.symm.trans isomΨ.isometric_on_range).symm, have E : (range Ψ ≃ᵢ (nonempty_compacts.Kuratowski_embedding α).val) = (p.val ≃ᵢ range (Kuratowski_embedding α)), by { dunfold nonempty_compacts.Kuratowski_embedding, rw [rangeΨ]; refl }, have g := cast E f, exact ⟨g⟩ } end lemma eq_to_GH_space {p : nonempty_compacts ℓ_infty_ℝ} : ⟦p⟧ = to_GH_space p.val := begin refine eq_to_GH_space_iff.2 ⟨((λx, x) : p.val → ℓ_infty_ℝ), _, subtype.val_range⟩, apply isometry_subtype_val end section local attribute [reducible] GH_space.rep instance rep_GH_space_metric_space {p : GH_space} : metric_space (p.rep) := by apply_instance instance rep_GH_space_compact_space {p : GH_space} : compact_space (p.rep) := by apply_instance instance rep_GH_space_nonempty {p : GH_space} : nonempty (p.rep) := by apply_instance end lemma GH_space.to_GH_space_rep (p : GH_space) : to_GH_space (p.rep) = p := begin change to_GH_space (quot.out p).val = p, rw ← eq_to_GH_space, exact quot.out_eq p end /-- Two nonempty compact spaces have the same image in `GH_space` if and only if they are isometric -/ lemma to_GH_space_eq_to_GH_space_iff_isometric {α : Type u} [metric_space α] [compact_space α] [nonempty α] {β : Type u} [metric_space β] [compact_space β] [nonempty β] : to_GH_space α = to_GH_space β ↔ nonempty (α ≃ᵢ β) := ⟨begin simp only [to_GH_space, quotient.eq], assume h, rcases h with e, have I : ((nonempty_compacts.Kuratowski_embedding α).val ≃ᵢ (nonempty_compacts.Kuratowski_embedding β).val) = ((range (Kuratowski_embedding α)) ≃ᵢ (range (Kuratowski_embedding β))), by { dunfold nonempty_compacts.Kuratowski_embedding, refl }, have e' := cast I e, have f := (Kuratowski_embedding.isometry α).isometric_on_range, have g := (Kuratowski_embedding.isometry β).isometric_on_range.symm, have h := (f.trans e').trans g, exact ⟨h⟩ end, begin rintros ⟨e⟩, simp only [to_GH_space, quotient.eq], have f := (Kuratowski_embedding.isometry α).isometric_on_range.symm, have g := (Kuratowski_embedding.isometry β).isometric_on_range, have h := (f.trans e).trans g, have I : ((range (Kuratowski_embedding α)) ≃ᵢ (range (Kuratowski_embedding β))) = ((nonempty_compacts.Kuratowski_embedding α).val ≃ᵢ (nonempty_compacts.Kuratowski_embedding β).val), by { dunfold nonempty_compacts.Kuratowski_embedding, refl }, have h' := cast I h, exact ⟨h'⟩ end⟩ /-- Distance on `GH_space`: the distance between two nonempty compact spaces is the infimum Hausdorff distance between isometric copies of the two spaces in a metric space. For the definition, we only consider embeddings in `ℓ^∞(ℝ)`, but we will prove below that it works for all spaces. -/ instance : has_dist (GH_space) := { dist := λx y, Inf ((λp : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ, Hausdorff_dist p.1.val p.2.val) '' (set.prod {a | ⟦a⟧ = x} {b | ⟦b⟧ = y})) } /-- The Gromov-Hausdorff distance between two nonempty compact metric spaces, equal by definition to the distance of the equivalence classes of these spaces in the Gromov-Hausdorff space. -/ def GH_dist (α : Type u) (β : Type v) [metric_space α] [nonempty α] [compact_space α] [metric_space β] [nonempty β] [compact_space β] : ℝ := dist (to_GH_space α) (to_GH_space β) lemma dist_GH_dist (p q : GH_space) : dist p q = GH_dist (p.rep) (q.rep) := by rw [GH_dist, p.to_GH_space_rep, q.to_GH_space_rep] /-- The Gromov-Hausdorff distance between two spaces is bounded by the Hausdorff distance of isometric copies of the spaces, in any metric space. -/ theorem GH_dist_le_Hausdorff_dist {α : Type u} [metric_space α] [compact_space α] [nonempty α] {β : Type v} [metric_space β] [compact_space β] [nonempty β] {γ : Type w} [metric_space γ] {Φ : α → γ} {Ψ : β → γ} (ha : isometry Φ) (hb : isometry Ψ) : GH_dist α β ≤ Hausdorff_dist (range Φ) (range Ψ) := begin /- For the proof, we want to embed `γ` in `ℓ^∞(ℝ)`, to say that the Hausdorff distance is realized in `ℓ^∞(ℝ)` and therefore bounded below by the Gromov-Hausdorff-distance. However, `γ` is not separable in general. We restrict to the union of the images of `α` and `β` in `γ`, which is separable and therefore embeddable in `ℓ^∞(ℝ)`. -/ rcases exists_mem_of_nonempty α with ⟨xα, _⟩, letI : inhabited α := ⟨xα⟩, letI : inhabited β := classical.inhabited_of_nonempty (by assumption), let s : set γ := (range Φ) ∪ (range Ψ), let Φ' : α → subtype s := λy, ⟨Φ y, mem_union_left _ (mem_range_self _)⟩, let Ψ' : β → subtype s := λy, ⟨Ψ y, mem_union_right _ (mem_range_self _)⟩, have IΦ' : isometry Φ' := λx y, ha x y, have IΨ' : isometry Ψ' := λx y, hb x y, have : compact s, from (compact_range ha.continuous).union (compact_range hb.continuous), letI : metric_space (subtype s) := by apply_instance, haveI : compact_space (subtype s) := ⟨compact_iff_compact_univ.1 ‹compact s›⟩, haveI : nonempty (subtype s) := ⟨Φ' xα⟩, have ΦΦ' : Φ = subtype.val ∘ Φ', by { funext, refl }, have ΨΨ' : Ψ = subtype.val ∘ Ψ', by { funext, refl }, have : Hausdorff_dist (range Φ) (range Ψ) = Hausdorff_dist (range Φ') (range Ψ'), { rw [ΦΦ', ΨΨ', range_comp, range_comp], exact Hausdorff_dist_image (isometry_subtype_val) }, rw this, -- Embed `s` in `ℓ^∞(ℝ)` through its Kuratowski embedding let F := Kuratowski_embedding (subtype s), have : Hausdorff_dist (F '' (range Φ')) (F '' (range Ψ')) = Hausdorff_dist (range Φ') (range Ψ') := Hausdorff_dist_image (Kuratowski_embedding.isometry _), rw ← this, -- Let `A` and `B` be the images of `α` and `β` under this embedding. They are in `ℓ^∞(ℝ)`, and -- their Hausdorff distance is the same as in the original space. let A : nonempty_compacts ℓ_infty_ℝ := ⟨F '' (range Φ'), ⟨by simp, (compact_range IΦ'.continuous).image (Kuratowski_embedding.isometry _).continuous⟩⟩, let B : nonempty_compacts ℓ_infty_ℝ := ⟨F '' (range Ψ'), ⟨by simp, (compact_range IΨ'.continuous).image (Kuratowski_embedding.isometry _).continuous⟩⟩, have Aα : ⟦A⟧ = to_GH_space α, { rw eq_to_GH_space_iff, exact ⟨λx, F (Φ' x), ⟨(Kuratowski_embedding.isometry _).comp IΦ', by rw range_comp⟩⟩ }, have Bβ : ⟦B⟧ = to_GH_space β, { rw eq_to_GH_space_iff, exact ⟨λx, F (Ψ' x), ⟨(Kuratowski_embedding.isometry _).comp IΨ', by rw range_comp⟩⟩ }, refine cInf_le ⟨0, begin simp [lower_bounds], assume t _ _ _ _ ht, rw ← ht, exact Hausdorff_dist_nonneg end⟩ _, apply (mem_image _ _ _).2, existsi (⟨A, B⟩ : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ), simp [Aα, Bβ] end local attribute [instance, priority 10] inhabited_of_nonempty' /-- The optimal coupling constructed above realizes exactly the Gromov-Hausdorff distance, essentially by design. -/ lemma Hausdorff_dist_optimal {α : Type u} [metric_space α] [compact_space α] [nonempty α] {β : Type v} [metric_space β] [compact_space β] [nonempty β] : Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) = GH_dist α β := begin /- we only need to check the inequality `≤`, as the other one follows from the previous lemma. As the Gromov-Hausdorff distance is an infimum, we need to check that the Hausdorff distance in the optimal coupling is smaller than the Hausdorff distance of any coupling. First, we check this for couplings which already have small Hausdorff distance: in this case, the induced "distance" on `α ⊕ β` belongs to the candidates family introduced in the definition of the optimal coupling, and the conclusion follows from the optimality of the optimal coupling within this family. -/ have A : ∀p q : nonempty_compacts (ℓ_infty_ℝ), ⟦p⟧ = to_GH_space α → ⟦q⟧ = to_GH_space β → Hausdorff_dist (p.val) (q.val) < diam (univ : set α) + 1 + diam (univ : set β) → Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) ≤ Hausdorff_dist (p.val) (q.val), { assume p q hp hq bound, rcases eq_to_GH_space_iff.1 hp with ⟨Φ, ⟨Φisom, Φrange⟩⟩, rcases eq_to_GH_space_iff.1 hq with ⟨Ψ, ⟨Ψisom, Ψrange⟩⟩, have I : diam (range Φ ∪ range Ψ) ≤ 2 * diam (univ : set α) + 1 + 2 * diam (univ : set β), { rcases exists_mem_of_nonempty α with ⟨xα, _⟩, have : ∃y ∈ range Ψ, dist (Φ xα) y < diam (univ : set α) + 1 + diam (univ : set β), { rw Ψrange, have : Φ xα ∈ p.val := begin rw ← Φrange, exact mem_range_self _ end, exact exists_dist_lt_of_Hausdorff_dist_lt this bound (Hausdorff_edist_ne_top_of_ne_empty_of_bounded p.2.1 q.2.1 p.2.2.bounded q.2.2.bounded) }, rcases this with ⟨y, hy, dy⟩, rcases mem_range.1 hy with ⟨z, hzy⟩, rw ← hzy at dy, have DΦ : diam (range Φ) = diam (univ : set α) := begin rw [← image_univ], apply metric.isometry.diam_image Φisom end, have DΨ : diam (range Ψ) = diam (univ : set β) := begin rw [← image_univ], apply metric.isometry.diam_image Ψisom end, calc diam (range Φ ∪ range Ψ) ≤ diam (range Φ) + dist (Φ xα) (Ψ z) + diam (range Ψ) : diam_union (mem_range_self _) (mem_range_self _) ... ≤ diam (univ : set α) + (diam (univ : set α) + 1 + diam (univ : set β)) + diam (univ : set β) : by { rw [DΦ, DΨ], apply add_le_add (add_le_add (le_refl _) (le_of_lt dy)) (le_refl _) } ... = 2 * diam (univ : set α) + 1 + 2 * diam (univ : set β) : by ring }, let f : α ⊕ β → ℓ_infty_ℝ := λx, match x with | inl y := Φ y | inr z := Ψ z end, let F : (α ⊕ β) × (α ⊕ β) → ℝ := λp, dist (f p.1) (f p.2), -- check that the induced "distance" is a candidate have Fgood : F ∈ candidates α β, { simp only [candidates, forall_const, and_true, add_comm, eq_self_iff_true, dist_eq_zero, and_self, set.mem_set_of_eq], repeat {split}, { exact λx y, calc F (inl x, inl y) = dist (Φ x) (Φ y) : rfl ... = dist x y : Φisom.dist_eq }, { exact λx y, calc F (inr x, inr y) = dist (Ψ x) (Ψ y) : rfl ... = dist x y : Ψisom.dist_eq }, { exact λx y, dist_comm _ _ }, { exact λx y z, dist_triangle _ _ _ }, { exact λx y, calc F (x, y) ≤ diam (range Φ ∪ range Ψ) : begin have A : ∀z : α ⊕ β, f z ∈ range Φ ∪ range Ψ, { assume z, cases z, { apply mem_union_left, apply mem_range_self }, { apply mem_union_right, apply mem_range_self } }, refine dist_le_diam_of_mem _ (A _) (A _), rw [Φrange, Ψrange], exact (p.2.2.union q.2.2).bounded, end ... ≤ 2 * diam (univ : set α) + 1 + 2 * diam (univ : set β) : I } }, let Fb := candidates_b_of_candidates F Fgood, have : Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) ≤ HD Fb := Hausdorff_dist_optimal_le_HD _ _ (candidates_b_of_candidates_mem F Fgood), refine le_trans this (le_of_forall_le_of_dense (λr hr, _)), have I1 : ∀x : α, infi (λy:β, Fb (inl x, inr y)) ≤ r, { assume x, have : f (inl x) ∈ p.val, by { rw [← Φrange], apply mem_range_self }, rcases exists_dist_lt_of_Hausdorff_dist_lt this hr (Hausdorff_edist_ne_top_of_ne_empty_of_bounded p.2.1 q.2.1 p.2.2.bounded q.2.2.bounded) with ⟨z, zq, hz⟩, have : z ∈ range Ψ, by rwa [← Ψrange] at zq, rcases mem_range.1 this with ⟨y, hy⟩, calc infi (λy:β, Fb (inl x, inr y)) ≤ Fb (inl x, inr y) : cinfi_le (by simpa using HD_below_aux1 0) ... = dist (Φ x) (Ψ y) : rfl ... = dist (f (inl x)) z : by rw hy ... ≤ r : le_of_lt hz }, have I2 : ∀y : β, infi (λx:α, Fb (inl x, inr y)) ≤ r, { assume y, have : f (inr y) ∈ q.val, by { rw [← Ψrange], apply mem_range_self }, rcases exists_dist_lt_of_Hausdorff_dist_lt' this hr (Hausdorff_edist_ne_top_of_ne_empty_of_bounded p.2.1 q.2.1 p.2.2.bounded q.2.2.bounded) with ⟨z, zq, hz⟩, have : z ∈ range Φ, by rwa [← Φrange] at zq, rcases mem_range.1 this with ⟨x, hx⟩, calc infi (λx:α, Fb (inl x, inr y)) ≤ Fb (inl x, inr y) : cinfi_le (by simpa using HD_below_aux2 0) ... = dist (Φ x) (Ψ y) : rfl ... = dist z (f (inr y)) : by rw hx ... ≤ r : le_of_lt hz }, simp [HD, csupr_le I1, csupr_le I2] }, /- Get the same inequality for any coupling. If the coupling is quite good, the desired inequality has been proved above. If it is bad, then the inequality is obvious. -/ have B : ∀p q : nonempty_compacts (ℓ_infty_ℝ), ⟦p⟧ = to_GH_space α → ⟦q⟧ = to_GH_space β → Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) ≤ Hausdorff_dist (p.val) (q.val), { assume p q hp hq, by_cases h : Hausdorff_dist (p.val) (q.val) < diam (univ : set α) + 1 + diam (univ : set β), { exact A p q hp hq h }, { calc Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) ≤ HD (candidates_b_dist α β) : Hausdorff_dist_optimal_le_HD _ _ (candidates_b_dist_mem_candidates_b) ... ≤ diam (univ : set α) + 1 + diam (univ : set β) : HD_candidates_b_dist_le ... ≤ Hausdorff_dist (p.val) (q.val) : not_lt.1 h } }, refine le_antisymm _ _, { apply le_cInf, { rw ne_empty_iff_exists_mem, simp only [set.mem_image, nonempty_of_inhabited, set.mem_set_of_eq, prod.exists], existsi [Hausdorff_dist (nonempty_compacts.Kuratowski_embedding α).val (nonempty_compacts.Kuratowski_embedding β).val, nonempty_compacts.Kuratowski_embedding α, nonempty_compacts.Kuratowski_embedding β], simp [to_GH_space, -quotient.eq] }, { rintro b ⟨⟨p, q⟩, ⟨hp, hq⟩, rfl⟩, exact B p q hp hq } }, { exact GH_dist_le_Hausdorff_dist (isometry_optimal_GH_injl α β) (isometry_optimal_GH_injr α β) } end /-- The Gromov-Hausdorff distance can also be realized by a coupling in `ℓ^∞(ℝ)`, by embedding the optimal coupling through its Kuratowski embedding. -/ theorem GH_dist_eq_Hausdorff_dist (α : Type u) [metric_space α] [compact_space α] [nonempty α] (β : Type v) [metric_space β] [compact_space β] [nonempty β] : ∃Φ : α → ℓ_infty_ℝ, ∃Ψ : β → ℓ_infty_ℝ, isometry Φ ∧ isometry Ψ ∧ GH_dist α β = Hausdorff_dist (range Φ) (range Ψ) := begin let F := Kuratowski_embedding (optimal_GH_coupling α β), let Φ := F ∘ optimal_GH_injl α β, let Ψ := F ∘ optimal_GH_injr α β, refine ⟨Φ, Ψ, _, _, _⟩, { exact (Kuratowski_embedding.isometry _).comp (isometry_optimal_GH_injl α β) }, { exact (Kuratowski_embedding.isometry _).comp (isometry_optimal_GH_injr α β) }, { rw [← image_univ, ← image_univ, image_comp F, image_univ, image_comp F (optimal_GH_injr α β), image_univ, ← Hausdorff_dist_optimal], exact (Hausdorff_dist_image (Kuratowski_embedding.isometry _)).symm }, end -- without the next two lines, `{ exact closed_of_compact (range Φ) hΦ }` in the next -- proof is very slow, as the `t2_space` instance is very hard to find local attribute [instance, priority 10] orderable_topology.t2_space local attribute [instance, priority 10] ordered_topology.to_t2_space /-- The Gromov-Hausdorff distance defines a genuine distance on the Gromov-Hausdorff space. -/ instance GH_space_metric_space : metric_space GH_space := { dist_self := λx, begin rcases exists_rep x with ⟨y, hy⟩, refine le_antisymm _ _, { apply cInf_le, { exact ⟨0, by { rintro b ⟨⟨u, v⟩, ⟨hu, hv⟩, rfl⟩, exact Hausdorff_dist_nonneg } ⟩}, { simp, existsi [y, y], simpa } }, { apply le_cInf, { simp only [set.image_eq_empty, ne.def], apply ne_empty_iff_exists_mem.2, existsi (⟨y, y⟩ : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ), simpa }, { rintro b ⟨⟨u, v⟩, ⟨hu, hv⟩, rfl⟩, exact Hausdorff_dist_nonneg } }, end, dist_comm := λx y, begin have A : (λ (p : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ), Hausdorff_dist ((p.fst).val) ((p.snd).val)) '' (set.prod {a | ⟦a⟧ = x} {b | ⟦b⟧ = y}) = ((λ (p : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ), Hausdorff_dist ((p.fst).val) ((p.snd).val)) ∘ prod.swap) '' (set.prod {a | ⟦a⟧ = x} {b | ⟦b⟧ = y}) := by { congr, funext, simp, rw Hausdorff_dist_comm }, simp only [dist, A, image_comp, prod.swap, image_swap_prod], end, eq_of_dist_eq_zero := λx y hxy, begin /- To show that two spaces at zero distance are isometric, we argue that the distance is realized by some coupling. In this coupling, the two spaces are at zero Hausdorff distance, i.e., they coincide. Therefore, the original spaces are isometric. -/ rcases GH_dist_eq_Hausdorff_dist x.rep y.rep with ⟨Φ, Ψ, Φisom, Ψisom, DΦΨ⟩, rw [← dist_GH_dist, hxy] at DΦΨ, have : range Φ = range Ψ, { have hΦ : compact (range Φ) := compact_range Φisom.continuous, have hΨ : compact (range Ψ) := compact_range Ψisom.continuous, apply (Hausdorff_dist_zero_iff_eq_of_closed _ _ _).1 (DΦΨ.symm), { exact closed_of_compact (range Φ) hΦ }, { exact closed_of_compact (range Ψ) hΨ }, { exact Hausdorff_edist_ne_top_of_ne_empty_of_bounded (by simp [-nonempty_subtype]) (by simp [-nonempty_subtype]) hΦ.bounded hΨ.bounded } }, have T : ((range Ψ) ≃ᵢ y.rep) = ((range Φ) ≃ᵢ y.rep), by rw this, have eΨ := cast T Ψisom.isometric_on_range.symm, have e := Φisom.isometric_on_range.trans eΨ, rw [← x.to_GH_space_rep, ← y.to_GH_space_rep, to_GH_space_eq_to_GH_space_iff_isometric], exact ⟨e⟩ end, dist_triangle := λx y z, begin /- To show the triangular inequality between `X`, `Y` and `Z`, realize an optimal coupling between `X` and `Y` in a space `γ1`, and an optimal coupling between `Y`and `Z` in a space `γ2`. Then, glue these metric spaces along `Y`. We get a new space `γ` in which `X` and `Y` are optimally coupled, as well as `Y` and `Z`. Apply the triangle inequality for the Hausdorff distance in `γ` to conclude. -/ let X := x.rep, let Y := y.rep, let Z := z.rep, let γ1 := optimal_GH_coupling X Y, let γ2 := optimal_GH_coupling Y Z, let Φ : Y → γ1 := optimal_GH_injr X Y, have hΦ : isometry Φ := isometry_optimal_GH_injr X Y, let Ψ : Y → γ2 := optimal_GH_injl Y Z, have hΨ : isometry Ψ := isometry_optimal_GH_injl Y Z, let γ := glue_space hΦ hΨ, letI : metric_space γ := metric.metric_space_glue_space hΦ hΨ, have Comm : (to_glue_l hΦ hΨ) ∘ (optimal_GH_injr X Y) = (to_glue_r hΦ hΨ) ∘ (optimal_GH_injl Y Z) := to_glue_commute hΦ hΨ, calc dist x z = dist (to_GH_space X) (to_GH_space Z) : by rw [x.to_GH_space_rep, z.to_GH_space_rep] ... ≤ Hausdorff_dist (range ((to_glue_l hΦ hΨ) ∘ (optimal_GH_injl X Y))) (range ((to_glue_r hΦ hΨ) ∘ (optimal_GH_injr Y Z))) : GH_dist_le_Hausdorff_dist ((to_glue_l_isometry hΦ hΨ).comp (isometry_optimal_GH_injl X Y)) ((to_glue_r_isometry hΦ hΨ).comp (isometry_optimal_GH_injr Y Z)) ... ≤ Hausdorff_dist (range ((to_glue_l hΦ hΨ) ∘ (optimal_GH_injl X Y))) (range ((to_glue_l hΦ hΨ) ∘ (optimal_GH_injr X Y))) + Hausdorff_dist (range ((to_glue_l hΦ hΨ) ∘ (optimal_GH_injr X Y))) (range ((to_glue_r hΦ hΨ) ∘ (optimal_GH_injr Y Z))) : begin refine Hausdorff_dist_triangle (Hausdorff_edist_ne_top_of_ne_empty_of_bounded (by simp [-nonempty_subtype]) (by simp [-nonempty_subtype]) _ _), { exact (compact_range (isometry.continuous ((to_glue_l_isometry hΦ hΨ).comp (isometry_optimal_GH_injl X Y)))).bounded }, { exact (compact_range (isometry.continuous ((to_glue_l_isometry hΦ hΨ).comp (isometry_optimal_GH_injr X Y)))).bounded } end ... = Hausdorff_dist ((to_glue_l hΦ hΨ) '' (range (optimal_GH_injl X Y))) ((to_glue_l hΦ hΨ) '' (range (optimal_GH_injr X Y))) + Hausdorff_dist ((to_glue_r hΦ hΨ) '' (range (optimal_GH_injl Y Z))) ((to_glue_r hΦ hΨ) '' (range (optimal_GH_injr Y Z))) : by simp only [eq.symm range_comp, Comm, eq_self_iff_true, add_right_inj] ... = Hausdorff_dist (range (optimal_GH_injl X Y)) (range (optimal_GH_injr X Y)) + Hausdorff_dist (range (optimal_GH_injl Y Z)) (range (optimal_GH_injr Y Z)) : by rw [Hausdorff_dist_image (to_glue_l_isometry hΦ hΨ), Hausdorff_dist_image (to_glue_r_isometry hΦ hΨ)] ... = dist (to_GH_space X) (to_GH_space Y) + dist (to_GH_space Y) (to_GH_space Z) : by rw [Hausdorff_dist_optimal, Hausdorff_dist_optimal, GH_dist, GH_dist] ... = dist x y + dist y z: by rw [x.to_GH_space_rep, y.to_GH_space_rep, z.to_GH_space_rep] end } end GH_space --section end Gromov_Hausdorff /-- In particular, nonempty compacts of a metric space map to `GH_space`. We register this in the topological_space namespace to take advantage of the notation `p.to_GH_space`. -/ definition topological_space.nonempty_compacts.to_GH_space {α : Type u} [metric_space α] (p : nonempty_compacts α) : Gromov_Hausdorff.GH_space := Gromov_Hausdorff.to_GH_space p.val open topological_space namespace Gromov_Hausdorff section nonempty_compacts variables {α : Type u} [metric_space α] theorem GH_dist_le_nonempty_compacts_dist (p q : nonempty_compacts α) : dist p.to_GH_space q.to_GH_space ≤ dist p q := begin have ha : isometry (subtype.val : p.val → α) := isometry_subtype_val, have hb : isometry (subtype.val : q.val → α) := isometry_subtype_val, have A : dist p q = Hausdorff_dist p.val q.val := rfl, have I : p.val = range (subtype.val : p.val → α), by simp, have J : q.val = range (subtype.val : q.val → α), by simp, rw [I, J] at A, rw A, exact GH_dist_le_Hausdorff_dist ha hb end lemma to_GH_space_lipschitz : lipschitz_with 1 (nonempty_compacts.to_GH_space : nonempty_compacts α → GH_space) := lipschitz_with.one_mk GH_dist_le_nonempty_compacts_dist lemma to_GH_space_continuous : continuous (nonempty_compacts.to_GH_space : nonempty_compacts α → GH_space) := to_GH_space_lipschitz.to_continuous end nonempty_compacts section /- In this section, we show that if two metric spaces are isometric up to `ε₂`, then their Gromov-Hausdorff distance is bounded by `ε₂ / 2`. More generally, if there are subsets which are `ε₁`-dense and `ε₃`-dense in two spaces, and isometric up to `ε₂`, then the Gromov-Hausdorff distance between the spaces is bounded by `ε₁ + ε₂/2 + ε₃`. For this, we construct a suitable coupling between the two spaces, by gluing them (approximately) along the two matching subsets. -/ variables {α : Type u} [metric_space α] [compact_space α] [nonempty α] {β : Type v} [metric_space β] [compact_space β] [nonempty β] -- we want to ignore these instances in the following theorem local attribute [instance, priority 10] sum.topological_space sum.uniform_space /-- If there are subsets which are `ε₁`-dense and `ε₃`-dense in two spaces, and isometric up to `ε₂`, then the Gromov-Hausdorff distance between the spaces is bounded by `ε₁ + ε₂/2 + ε₃`. -/ theorem GH_dist_le_of_approx_subsets {s : set α} (Φ : s → β) {ε₁ ε₂ ε₃ : ℝ} (hs : ∀x : α, ∃y ∈ s, dist x y ≤ ε₁) (hs' : ∀x : β, ∃y : s, dist x (Φ y) ≤ ε₃) (H : ∀x y : s, abs (dist x y - dist (Φ x) (Φ y)) ≤ ε₂) : GH_dist α β ≤ ε₁ + ε₂ / 2 + ε₃ := begin refine real.le_of_forall_epsilon_le (λδ δ0, _), rcases exists_mem_of_nonempty α with ⟨xα, _⟩, rcases hs xα with ⟨xs, hxs, Dxs⟩, have sne : s ≠ ∅ := ne_empty_of_mem hxs, letI : nonempty (subtype s) := ⟨⟨xs, hxs⟩⟩, have : 0 ≤ ε₂ := le_trans (abs_nonneg _) (H ⟨xs, hxs⟩ ⟨xs, hxs⟩), have : ∀ p q : s, abs (dist p q - dist (Φ p) (Φ q)) ≤ 2 * (ε₂/2 + δ) := λp q, calc abs (dist p q - dist (Φ p) (Φ q)) ≤ ε₂ : H p q ... ≤ 2 * (ε₂/2 + δ) : by linarith, -- glue `α` and `β` along the almost matching subsets letI : metric_space (α ⊕ β) := glue_metric_approx (@subtype.val α s) (λx, Φ x) (ε₂/2 + δ) (by linarith) this, let Fl := @sum.inl α β, let Fr := @sum.inr α β, have Il : isometry Fl := isometry_emetric_iff_metric.2 (λx y, rfl), have Ir : isometry Fr := isometry_emetric_iff_metric.2 (λx y, rfl), /- The proof goes as follows : the `GH_dist` is bounded by the Hausdorff distance of the images in the coupling, which is bounded (using the triangular inequality) by the sum of the Hausdorff distances of `α` and `s` (in the coupling or, equivalently in the original space), of `s` and `Φ s`, and of `Φ s` and `β` (in the coupling or, equivalently, in the original space). The first term is bounded by `ε₁`, by `ε₁`-density. The third one is bounded by `ε₃`. And the middle one is bounded by `ε₂/2` as in the coupling the points `x` and `Φ x` are at distance `ε₂/2` by construction of the coupling (in fact `ε₂/2 + δ` where `δ` is an arbitrarily small positive constant where positivity is used to ensure that the coupling is really a metric space and not a premetric space on `α ⊕ β`). -/ have : GH_dist α β ≤ Hausdorff_dist (range Fl) (range Fr) := GH_dist_le_Hausdorff_dist Il Ir, have : Hausdorff_dist (range Fl) (range Fr) ≤ Hausdorff_dist (range Fl) (Fl '' s) + Hausdorff_dist (Fl '' s) (range Fr), { have B : bounded (range Fl) := (compact_range Il.continuous).bounded, exact Hausdorff_dist_triangle (Hausdorff_edist_ne_top_of_ne_empty_of_bounded (by simpa) (by simpa) B (bounded.subset (image_subset_range _ _) B)) }, have : Hausdorff_dist (Fl '' s) (range Fr) ≤ Hausdorff_dist (Fl '' s) (Fr '' (range Φ)) + Hausdorff_dist (Fr '' (range Φ)) (range Fr), { have B : bounded (range Fr) := (compact_range Ir.continuous).bounded, exact Hausdorff_dist_triangle' (Hausdorff_edist_ne_top_of_ne_empty_of_bounded (by simpa [-nonempty_subtype]) (by simpa) (bounded.subset (image_subset_range _ _) B) B) }, have : Hausdorff_dist (range Fl) (Fl '' s) ≤ ε₁, { rw [← image_univ, Hausdorff_dist_image Il], have : 0 ≤ ε₁ := le_trans dist_nonneg Dxs, refine Hausdorff_dist_le_of_mem_dist this (λx hx, hs x) (λx hx, ⟨x, mem_univ _, by simpa⟩) }, have : Hausdorff_dist (Fl '' s) (Fr '' (range Φ)) ≤ ε₂/2 + δ, { refine Hausdorff_dist_le_of_mem_dist (by linarith) _ _, { assume x' hx', rcases (set.mem_image _ _ _).1 hx' with ⟨x, ⟨x_in_s, xx'⟩⟩, rw ← xx', use [Fr (Φ ⟨x, x_in_s⟩), mem_image_of_mem Fr (mem_range_self _)], exact le_of_eq (glue_dist_glued_points (@subtype.val α s) Φ (ε₂/2 + δ) ⟨x, x_in_s⟩) }, { assume x' hx', rcases (set.mem_image _ _ _).1 hx' with ⟨y, ⟨y_in_s', yx'⟩⟩, rcases mem_range.1 y_in_s' with ⟨x, xy⟩, use [Fl x, mem_image_of_mem _ x.2], rw [← yx', ← xy, dist_comm], exact le_of_eq (glue_dist_glued_points (@subtype.val α s) Φ (ε₂/2 + δ) x) } }, have : Hausdorff_dist (Fr '' (range Φ)) (range Fr) ≤ ε₃, { rw [← @image_univ _ _ Fr, Hausdorff_dist_image Ir], rcases exists_mem_of_nonempty β with ⟨xβ, _⟩, rcases hs' xβ with ⟨xs', Dxs'⟩, have : 0 ≤ ε₃ := le_trans dist_nonneg Dxs', refine Hausdorff_dist_le_of_mem_dist this (λx hx, ⟨x, mem_univ _, by simpa⟩) (λx _, _), rcases hs' x with ⟨y, Dy⟩, exact ⟨Φ y, mem_range_self _, Dy⟩ }, linarith end end --section /-- The Gromov-Hausdorff space is second countable. -/ instance second_countable : second_countable_topology GH_space := begin refine second_countable_of_countable_discretization (λδ δpos, _), let ε := (2/5) * δ, have εpos : 0 < ε := mul_pos (by norm_num) δpos, have : ∀p:GH_space, ∃s : set (p.rep), finite s ∧ (univ ⊆ (⋃x∈s, ball x ε)) := λp, by simpa using finite_cover_balls_of_compact (@compact_univ p.rep _ _) εpos, -- for each `p`, `s p` is a finite `ε`-dense subset of `p` (or rather the metric space -- `p.rep` representing `p`) choose s hs using this, have : ∀p:GH_space, ∀t:set (p.rep), finite t → ∃n:ℕ, ∃e:equiv t (fin n), true, { assume p t ht, letI : fintype t := finite.fintype ht, rcases fintype.exists_equiv_fin t with ⟨n, hn⟩, rcases hn with e, exact ⟨n, e, trivial⟩ }, choose N e hne using this, -- cardinality of the nice finite subset `s p` of `p.rep`, called `N p` let N := λp:GH_space, N p (s p) (hs p).1, -- equiv from `s p`, a nice finite subset of `p.rep`, to `fin (N p)`, called `E p` let E := λp:GH_space, e p (s p) (hs p).1, -- A function `F` associating to `p : GH_space` the data of all distances between points -- in the `ε`-dense set `s p`. let F : GH_space → Σn:ℕ, (fin n → fin n → ℤ) := λp, ⟨N p, λa b, floor (ε⁻¹ * dist ((E p).inv_fun a) ((E p).inv_fun b))⟩, refine ⟨_, by apply_instance, F, λp q hpq, _⟩, /- As the target space of F is countable, it suffices to show that two points `p` and `q` with `F p = F q` are at distance `≤ δ`. For this, we construct a map `Φ` from `s p ⊆ p.rep` (representing `p`) to `q.rep` (representing `q`) which is almost an isometry on `s p`, and with image `s q`. For this, we compose the identification of `s p` with `fin (N p)` and the inverse of the identification of `s q` with `fin (N q)`. Together with the fact that `N p = N q`, this constructs `Ψ` between `s p` and `s q`, and then composing with the canonical inclusion we get `Φ`. -/ have Npq : N p = N q := (sigma.mk.inj_iff.1 hpq).1, let Ψ : s p → s q := λx, (E q).inv_fun (fin.cast Npq ((E p).to_fun x)), let Φ : s p → q.rep := λx, Ψ x, -- Use the almost isometry `Φ` to show that `p.rep` and `q.rep` -- are within controlled Gromov-Hausdorff distance. have main : GH_dist p.rep q.rep ≤ ε + ε/2 + ε, { refine GH_dist_le_of_approx_subsets Φ _ _ _, show ∀x : p.rep, ∃ (y : p.rep) (H : y ∈ s p), dist x y ≤ ε, { -- by construction, `s p` is `ε`-dense assume x, have : x ∈ ⋃y∈(s p), ball y ε := (hs p).2 (mem_univ _), rcases mem_bUnion_iff.1 this with ⟨y, ⟨ys, hy⟩⟩, exact ⟨y, ys, le_of_lt hy⟩ }, show ∀x : q.rep, ∃ (z : s p), dist x (Φ z) ≤ ε, { -- by construction, `s q` is `ε`-dense, and it is the range of `Φ` assume x, have : x ∈ ⋃y∈(s q), ball y ε := (hs q).2 (mem_univ _), rcases mem_bUnion_iff.1 this with ⟨y, ⟨ys, hy⟩⟩, let i := ((E q).to_fun ⟨y, ys⟩).1, let hi := ((E q).to_fun ⟨y, ys⟩).2, have ihi_eq : (⟨i, hi⟩ : fin (N q)) = (E q).to_fun ⟨y, ys⟩, by rw fin.ext_iff, have hiq : i < N q := hi, have hip : i < N p, { rwa Npq.symm at hiq }, let z := (E p).inv_fun ⟨i, hip⟩, use z, have C1 : (E p).to_fun z = ⟨i, hip⟩ := (E p).right_inv ⟨i, hip⟩, have C2 : fin.cast Npq ⟨i, hip⟩ = ⟨i, hi⟩ := rfl, have C3 : (E q).inv_fun ⟨i, hi⟩ = ⟨y, ys⟩, by { rw ihi_eq, exact (E q).left_inv ⟨y, ys⟩ }, have : Φ z = y := by { simp only [Φ, Ψ], rw [C1, C2, C3], refl }, rw this, exact le_of_lt hy }, show ∀x y : s p, abs (dist x y - dist (Φ x) (Φ y)) ≤ ε, { /- the distance between `x` and `y` is encoded in `F p`, and the distance between `Φ x` and `Φ y` (two points of `s q`) is encoded in `F q`, all this up to `ε`. As `F p = F q`, the distances are almost equal. -/ assume x y, have : dist (Φ x) (Φ y) = dist (Ψ x) (Ψ y) := rfl, rw this, -- introduce `i`, that codes both `x` and `Φ x` in `fin (N p) = fin (N q)` let i := ((E p).to_fun x).1, have hip : i < N p := ((E p).to_fun x).2, have hiq : i < N q, by rwa Npq at hip, have i' : i = ((E q).to_fun (Ψ x)).1, by { simp [Ψ, (E q).right_inv _] }, -- introduce `j`, that codes both `y` and `Φ y` in `fin (N p) = fin (N q)` let j := ((E p).to_fun y).1, have hjp : j < N p := ((E p).to_fun y).2, have hjq : j < N q, by rwa Npq at hjp, have j' : j = ((E q).to_fun (Ψ y)).1, by { simp [Ψ, (E q).right_inv _] }, -- Express `dist x y` in terms of `F p` have : (F p).2 ((E p).to_fun x) ((E p).to_fun y) = floor (ε⁻¹ * dist x y), by simp only [F, (E p).left_inv _], have Ap : (F p).2 ⟨i, hip⟩ ⟨j, hjp⟩ = floor (ε⁻¹ * dist x y), by { rw ← this, congr; apply (fin.ext_iff _ _).2; refl }, -- Express `dist (Φ x) (Φ y)` in terms of `F q` have : (F q).2 ((E q).to_fun (Ψ x)) ((E q).to_fun (Ψ y)) = floor (ε⁻¹ * dist (Ψ x) (Ψ y)), by simp only [F, (E q).left_inv _], have Aq : (F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩ = floor (ε⁻¹ * dist (Ψ x) (Ψ y)), by { rw ← this, congr; apply (fin.ext_iff _ _).2; [exact i', exact j'] }, -- use the equality between `F p` and `F q` to deduce that the distances have equal -- integer parts have : (F p).2 ⟨i, hip⟩ ⟨j, hjp⟩ = (F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩, { -- we want to `subst hpq` where `hpq : F p = F q`, except that `subst` only works -- with a constant, so replace `F q` (and everything that depends on it) by a constant `f` -- then `subst` revert hiq hjq, change N q with (F q).1, generalize_hyp : F q = f at hpq ⊢, subst hpq, intros, refl }, rw [Ap, Aq] at this, -- deduce that the distances coincide up to `ε`, by a straightforward computation -- that should be automated have I := calc abs (ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y)) = abs (ε⁻¹ * (dist x y - dist (Ψ x) (Ψ y))) : (abs_mul _ _).symm ... = abs ((ε⁻¹ * dist x y) - (ε⁻¹ * dist (Ψ x) (Ψ y))) : by { congr, ring } ... ≤ 1 : le_of_lt (abs_sub_lt_one_of_floor_eq_floor this), calc abs (dist x y - dist (Ψ x) (Ψ y)) = (ε * ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y)) : by rw [mul_inv_cancel (ne_of_gt εpos), one_mul] ... = ε * (abs (ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y))) : by rw [abs_of_nonneg (le_of_lt (inv_pos εpos)), mul_assoc] ... ≤ ε * 1 : mul_le_mul_of_nonneg_left I (le_of_lt εpos) ... = ε : mul_one _ } }, calc dist p q = GH_dist (p.rep) (q.rep) : dist_GH_dist p q ... ≤ ε + ε/2 + ε : main ... = δ : by { simp [ε], ring } end /-- Compactness criterion: a closed set of compact metric spaces is compact if the spaces have a uniformly bounded diameter, and for all `ε` the number of balls of radius `ε` required to cover the spaces is uniformly bounded. This is an equivalence, but we only prove the interesting direction that these conditions imply compactness. -/ lemma totally_bounded {t : set GH_space} {C : ℝ} {u : ℕ → ℝ} {K : ℕ → ℕ} (ulim : tendsto u at_top (𝓝 0)) (hdiam : ∀p ∈ t, diam (univ : set (GH_space.rep p)) ≤ C) (hcov : ∀p ∈ t, ∀n:ℕ, ∃s : set (GH_space.rep p), cardinal.mk s ≤ K n ∧ univ ⊆ ⋃x∈s, ball x (u n)) : totally_bounded t := begin /- Let `δ>0`, and `ε = δ/5`. For each `p`, we construct a finite subset `s p` of `p`, which is `ε`-dense and has cardinality at most `K n`. Encoding the mutual distances of points in `s p`, up to `ε`, we will get a map `F` associating to `p` finitely many data, and making it possible to reconstruct `p` up to `ε`. This is enough to prove total boundedness. -/ refine metric.totally_bounded_of_finite_discretization (λδ δpos, _), let ε := (1/5) * δ, have εpos : 0 < ε := mul_pos (by norm_num) δpos, -- choose `n` for which `u n < ε` rcases metric.tendsto_at_top.1 ulim ε εpos with ⟨n, hn⟩, have u_le_ε : u n ≤ ε, { have := hn n (le_refl _), simp only [real.dist_eq, add_zero, sub_eq_add_neg, neg_zero] at this, exact le_of_lt (lt_of_le_of_lt (le_abs_self _) this) }, -- construct a finite subset `s p` of `p` which is `ε`-dense and has cardinal `≤ K n` have : ∀p:GH_space, ∃s : set (p.rep), ∃N ≤ K n, ∃E : equiv s (fin N), p ∈ t → univ ⊆ ⋃x∈s, ball x (u n), { assume p, by_cases hp : p ∉ t, { have : nonempty (equiv (∅ : set (p.rep)) (fin 0)), { rw ← fintype.card_eq, simp }, use [∅, 0, bot_le, choice (this)] }, { rcases hcov _ (set.not_not_mem.1 hp) n with ⟨s, ⟨scard, scover⟩⟩, rcases cardinal.lt_omega.1 (lt_of_le_of_lt scard (cardinal.nat_lt_omega _)) with ⟨N, hN⟩, rw [hN, cardinal.nat_cast_le] at scard, have : cardinal.mk s = cardinal.mk (fin N), by rw [hN, cardinal.mk_fin], cases quotient.exact this with E, use [s, N, scard, E], simp [hp, scover] } }, choose s N hN E hs using this, -- Define a function `F` taking values in a finite type and associating to `p` enough data -- to reconstruct it up to `ε`, namely the (discretized) distances between elements of `s p`. let M := (floor (ε⁻¹ * max C 0)).to_nat, let F : GH_space → (Σk:fin ((K n).succ), (fin k → fin k → fin (M.succ))) := λp, ⟨⟨N p, lt_of_le_of_lt (hN p) (nat.lt_succ_self _)⟩, λa b, ⟨min M (floor (ε⁻¹ * dist ((E p).inv_fun a) ((E p).inv_fun b))).to_nat, lt_of_le_of_lt ( min_le_left _ _) (nat.lt_succ_self _) ⟩ ⟩, refine ⟨_, by apply_instance, (λp, F p), _⟩, -- It remains to show that if `F p = F q`, then `p` and `q` are `ε`-close rintros ⟨p, pt⟩ ⟨q, qt⟩ hpq, have Npq : N p = N q := (fin.ext_iff _ _).1 (sigma.mk.inj_iff.1 hpq).1, let Ψ : s p → s q := λx, (E q).inv_fun (fin.cast Npq ((E p).to_fun x)), let Φ : s p → q.rep := λx, Ψ x, have main : GH_dist (p.rep) (q.rep) ≤ ε + ε/2 + ε, { -- to prove the main inequality, argue that `s p` is `ε`-dense in `p`, and `s q` is `ε`-dense -- in `q`, and `s p` and `s q` are almost isometric. Then closeness follows -- from `GH_dist_le_of_approx_subsets` refine GH_dist_le_of_approx_subsets Φ _ _ _, show ∀x : p.rep, ∃ (y : p.rep) (H : y ∈ s p), dist x y ≤ ε, { -- by construction, `s p` is `ε`-dense assume x, have : x ∈ ⋃y∈(s p), ball y (u n) := (hs p pt) (mem_univ _), rcases mem_bUnion_iff.1 this with ⟨y, ⟨ys, hy⟩⟩, exact ⟨y, ys, le_trans (le_of_lt hy) u_le_ε⟩ }, show ∀x : q.rep, ∃ (z : s p), dist x (Φ z) ≤ ε, { -- by construction, `s q` is `ε`-dense, and it is the range of `Φ` assume x, have : x ∈ ⋃y∈(s q), ball y (u n) := (hs q qt) (mem_univ _), rcases mem_bUnion_iff.1 this with ⟨y, ⟨ys, hy⟩⟩, let i := ((E q).to_fun ⟨y, ys⟩).1, let hi := ((E q).to_fun ⟨y, ys⟩).2, have ihi_eq : (⟨i, hi⟩ : fin (N q)) = (E q).to_fun ⟨y, ys⟩, by rw fin.ext_iff, have hiq : i < N q := hi, have hip : i < N p, { rwa Npq.symm at hiq }, let z := (E p).inv_fun ⟨i, hip⟩, use z, have C1 : (E p).to_fun z = ⟨i, hip⟩ := (E p).right_inv ⟨i, hip⟩, have C2 : fin.cast Npq ⟨i, hip⟩ = ⟨i, hi⟩ := rfl, have C3 : (E q).inv_fun ⟨i, hi⟩ = ⟨y, ys⟩, by { rw ihi_eq, exact (E q).left_inv ⟨y, ys⟩ }, have : Φ z = y := by { simp only [Φ, Ψ], rw [C1, C2, C3], refl }, rw this, exact le_trans (le_of_lt hy) u_le_ε }, show ∀x y : s p, abs (dist x y - dist (Φ x) (Φ y)) ≤ ε, { /- the distance between `x` and `y` is encoded in `F p`, and the distance between `Φ x` and `Φ y` (two points of `s q`) is encoded in `F q`, all this up to `ε`. As `F p = F q`, the distances are almost equal. -/ assume x y, have : dist (Φ x) (Φ y) = dist (Ψ x) (Ψ y) := rfl, rw this, -- introduce `i`, that codes both `x` and `Φ x` in `fin (N p) = fin (N q)` let i := ((E p).to_fun x).1, have hip : i < N p := ((E p).to_fun x).2, have hiq : i < N q, by rwa Npq at hip, have i' : i = ((E q).to_fun (Ψ x)).1, by { simp [Ψ, (E q).right_inv _] }, -- introduce `j`, that codes both `y` and `Φ y` in `fin (N p) = fin (N q)` let j := ((E p).to_fun y).1, have hjp : j < N p := ((E p).to_fun y).2, have hjq : j < N q, by rwa Npq at hjp, have j' : j = ((E q).to_fun (Ψ y)).1, by { simp [Ψ, (E q).right_inv _] }, -- Express `dist x y` in terms of `F p` have Ap : ((F p).2 ⟨i, hip⟩ ⟨j, hjp⟩).1 = (floor (ε⁻¹ * dist x y)).to_nat := calc ((F p).2 ⟨i, hip⟩ ⟨j, hjp⟩).1 = ((F p).2 ((E p).to_fun x) ((E p).to_fun y)).1 : by { congr; apply (fin.ext_iff _ _).2; refl } ... = min M (floor (ε⁻¹ * dist x y)).to_nat : by simp only [F, (E p).left_inv _] ... = (floor (ε⁻¹ * dist x y)).to_nat : begin refine min_eq_right (int.to_nat_le_to_nat (floor_mono _)), refine mul_le_mul_of_nonneg_left (le_trans _ (le_max_left _ _)) (le_of_lt (inv_pos εpos)), change dist (x : p.rep) y ≤ C, refine le_trans (dist_le_diam_of_mem compact_univ.bounded (mem_univ _) (mem_univ _)) _, exact hdiam p pt end, -- Express `dist (Φ x) (Φ y)` in terms of `F q` have Aq : ((F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩).1 = (floor (ε⁻¹ * dist (Ψ x) (Ψ y))).to_nat := calc ((F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩).1 = ((F q).2 ((E q).to_fun (Ψ x)) ((E q).to_fun (Ψ y))).1 : by { congr; apply (fin.ext_iff _ _).2; [exact i', exact j'] } ... = min M (floor (ε⁻¹ * dist (Ψ x) (Ψ y))).to_nat : by simp only [F, (E q).left_inv _] ... = (floor (ε⁻¹ * dist (Ψ x) (Ψ y))).to_nat : begin refine min_eq_right (int.to_nat_le_to_nat (floor_mono _)), refine mul_le_mul_of_nonneg_left (le_trans _ (le_max_left _ _)) (le_of_lt (inv_pos εpos)), change dist (Ψ x : q.rep) (Ψ y) ≤ C, refine le_trans (dist_le_diam_of_mem compact_univ.bounded (mem_univ _) (mem_univ _)) _, exact hdiam q qt end, -- use the equality between `F p` and `F q` to deduce that the distances have equal -- integer parts have : ((F p).2 ⟨i, hip⟩ ⟨j, hjp⟩).1 = ((F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩).1, { -- we want to `subst hpq` where `hpq : F p = F q`, except that `subst` only works -- with a constant, so replace `F q` (and everything that depends on it) by a constant `f` -- then `subst` revert hiq hjq, change N q with (F q).1, generalize_hyp : F q = f at hpq ⊢, subst hpq, intros, refl }, have : floor (ε⁻¹ * dist x y) = floor (ε⁻¹ * dist (Ψ x) (Ψ y)), { rw [Ap, Aq] at this, have D : 0 ≤ floor (ε⁻¹ * dist x y) := floor_nonneg.2 (mul_nonneg (le_of_lt (inv_pos εpos)) dist_nonneg), have D' : floor (ε⁻¹ * dist (Ψ x) (Ψ y)) ≥ 0 := floor_nonneg.2 (mul_nonneg (le_of_lt (inv_pos εpos)) dist_nonneg), rw [← int.to_nat_of_nonneg D, ← int.to_nat_of_nonneg D', this] }, -- deduce that the distances coincide up to `ε`, by a straightforward computation -- that should be automated have I := calc abs (ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y)) = abs (ε⁻¹ * (dist x y - dist (Ψ x) (Ψ y))) : (abs_mul _ _).symm ... = abs ((ε⁻¹ * dist x y) - (ε⁻¹ * dist (Ψ x) (Ψ y))) : by { congr, ring } ... ≤ 1 : le_of_lt (abs_sub_lt_one_of_floor_eq_floor this), calc abs (dist x y - dist (Ψ x) (Ψ y)) = (ε * ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y)) : by rw [mul_inv_cancel (ne_of_gt εpos), one_mul] ... = ε * (abs (ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y))) : by rw [abs_of_nonneg (le_of_lt (inv_pos εpos)), mul_assoc] ... ≤ ε * 1 : mul_le_mul_of_nonneg_left I (le_of_lt εpos) ... = ε : mul_one _ } }, calc dist p q = GH_dist (p.rep) (q.rep) : dist_GH_dist p q ... ≤ ε + ε/2 + ε : main ... = δ/2 : by { simp [ε], ring } ... < δ : half_lt_self δpos end section complete /- We will show that a sequence `u n` of compact metric spaces satisfying `dist (u n) (u (n+1)) < 1/2^n` converges, which implies completeness of the Gromov-Hausdorff space. We need to exhibit the limiting compact metric space. For this, start from a sequence `X n` of representatives of `u n`, and glue in an optimal way `X n` to `X (n+1)` for all `n`, in a common metric space. Formally, this is done as follows. Start from `Y 0 = X 0`. Then, glue `X 0` to `X 1` in an optimal way, yielding a space `Y 1` (with an embedding of `X 1`). Then, consider an optimal gluing of `X 1` and `X 2`, and glue it to `Y 1` along their common subspace `X 1`. This gives a new space `Y 2`, with an embedding of `X 2`. Go on, to obtain a sequence of spaces `Y n`. Let `Z0` be the inductive limit of the `Y n`, and finally let `Z` be the completion of `Z0`. The images `X2 n` of `X n` in `Z` are at Hausdorff distance `< 1/2^n` by construction, hence they form a Cauchy sequence for the Hausdorff distance. By completeness (of `Z`, and therefore of its set of nonempty compact subsets), they converge to a limit `L`. This is the nonempty compact metric space we are looking for. -/ variables (X : ℕ → Type) [∀n, metric_space (X n)] [∀n, compact_space (X n)] [∀n, nonempty (X n)] /-- Auxiliary structure used to glue metric spaces below, recording an isometric embedding of a type `A` in another metric space. -/ structure aux_gluing_struct (A : Type) [metric_space A] : Type 1 := (space : Type) (metric : metric_space space) (embed : A → space) (isom : isometry embed) /-- Auxiliary sequence of metric spaces, containing copies of `X 0`, ..., `X n`, where each `X i` is glued to `X (i+1)` in an optimal way. The space at step `n+1` is obtained from the space at step `n` by adding `X (n+1)`, glued in an optimal way to the `X n` already sitting there. -/ def aux_gluing (n : ℕ) : aux_gluing_struct (X n) := nat.rec_on n { space := X 0, metric := by apply_instance, embed := id, isom := λx y, rfl } (λn a, by letI : metric_space a.space := a.metric; exact { space := glue_space a.isom (isometry_optimal_GH_injl (X n) (X n.succ)), metric := metric.metric_space_glue_space a.isom (isometry_optimal_GH_injl (X n) (X n.succ)), embed := (to_glue_r a.isom (isometry_optimal_GH_injl (X n) (X n.succ))) ∘ (optimal_GH_injr (X n) (X n.succ)), isom := (to_glue_r_isometry _ _).comp (isometry_optimal_GH_injr (X n) (X n.succ)) }) /-- The Gromov-Hausdorff space is complete. -/ instance : complete_space (GH_space) := begin have : ∀ (n : ℕ), 0 < ((1:ℝ) / 2) ^ n, by { apply _root_.pow_pos, norm_num }, -- start from a sequence of nonempty compact metric spaces within distance `1/2^n` of each other refine metric.complete_of_convergent_controlled_sequences (λn, (1/2)^n) this (λu hu, _), -- `X n` is a representative of `u n` let X := λn, (u n).rep, -- glue them together successively in an optimal way, getting a sequence of metric spaces `Y n` let Y := aux_gluing X, letI : ∀n, metric_space (Y n).space := λn, (Y n).metric, have E : ∀n:ℕ, glue_space (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ)) = (Y n.succ).space := λn, by { simp [Y, aux_gluing], refl }, let c := λn, cast (E n), have ic : ∀n, isometry (c n) := λn x y, rfl, -- there is a canonical embedding of `Y n` in `Y (n+1)`, by construction let f : Πn, (Y n).space → (Y n.succ).space := λn, (c n) ∘ (to_glue_l (aux_gluing X n).isom (isometry_optimal_GH_injl (X n) (X n.succ))), have I : ∀n, isometry (f n), { assume n, apply isometry.comp, { assume x y, refl }, { apply to_glue_l_isometry } }, -- consider the inductive limit `Z0` of the `Y n`, and then its completion `Z` let Z0 := metric.inductive_limit I, let Z := uniform_space.completion Z0, let Φ := to_inductive_limit I, let coeZ := (coe : Z0 → Z), -- let `X2 n` be the image of `X n` in the space `Z` let X2 := λn, range (coeZ ∘ (Φ n) ∘ (Y n).embed), have isom : ∀n, isometry (coeZ ∘ (Φ n) ∘ (Y n).embed), { assume n, apply isometry.comp completion.coe_isometry _, apply isometry.comp _ (Y n).isom, apply to_inductive_limit_isometry }, -- The Hausdorff distance of `X2 n` and `X2 (n+1)` is by construction the distance between -- `u n` and `u (n+1)`, therefore bounded by `1/2^n` have D2 : ∀n, Hausdorff_dist (X2 n) (X2 n.succ) < (1/2)^n, { assume n, have X2n : X2 n = range ((coeZ ∘ (Φ n.succ) ∘ (c n) ∘ (to_glue_r (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ)))) ∘ (optimal_GH_injl (X n) (X n.succ))), { change X2 n = range (coeZ ∘ (Φ n.succ) ∘ (c n) ∘ (to_glue_r (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ))) ∘ (optimal_GH_injl (X n) (X n.succ))), simp only [X2, Φ], rw [← to_inductive_limit_commute I], simp only [f], rw ← to_glue_commute }, rw range_comp at X2n, have X2nsucc : X2 n.succ = range ((coeZ ∘ (Φ n.succ) ∘ (c n) ∘ (to_glue_r (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ)))) ∘ (optimal_GH_injr (X n) (X n.succ))), by refl, rw range_comp at X2nsucc, rw [X2n, X2nsucc, Hausdorff_dist_image, Hausdorff_dist_optimal, ← dist_GH_dist], { exact hu n n n.succ (le_refl n) (le_succ n) }, { apply isometry.comp completion.coe_isometry _, apply isometry.comp _ ((ic n).comp (to_glue_r_isometry _ _)), apply to_inductive_limit_isometry } }, -- consider `X2 n` as a member `X3 n` of the type of nonempty compact subsets of `Z`, which -- is a metric space let X3 : ℕ → nonempty_compacts Z := λn, ⟨X2 n, ⟨by { simp only [X2, set.range_eq_empty, not_not, ne.def], apply_instance }, compact_range (isom n).continuous ⟩⟩, -- `X3 n` is a Cauchy sequence by construction, as the successive distances are -- bounded by `(1/2)^n` have : cauchy_seq X3, { refine cauchy_seq_of_le_geometric (1/2) 1 (by norm_num) (λn, _), rw one_mul, exact le_of_lt (D2 n) }, -- therefore, it converges to a limit `L` rcases cauchy_seq_tendsto_of_complete this with ⟨L, hL⟩, -- the images of `X3 n` in the Gromov-Hausdorff space converge to the image of `L` have M : tendsto (λn, (X3 n).to_GH_space) at_top (𝓝 L.to_GH_space) := tendsto.comp (to_GH_space_continuous.tendsto _) hL, -- By construction, the image of `X3 n` in the Gromov-Hausdorff space is `u n`. have : ∀n, (X3 n).to_GH_space = u n, { assume n, rw [nonempty_compacts.to_GH_space, ← (u n).to_GH_space_rep, to_GH_space_eq_to_GH_space_iff_isometric], constructor, convert (isom n).isometric_on_range.symm, }, -- Finally, we have proved the convergence of `u n` exact ⟨L.to_GH_space, by simpa [this] using M⟩ end end complete--section end Gromov_Hausdorff --namespace
b3f326194cc1353151519b599eae57a0881f739b
491068d2ad28831e7dade8d6dff871c3e49d9431
/tests/lean/whnf_tac.lean
bbaa1be3df5fec321c01f9b8dba1632812613f17
[ "Apache-2.0" ]
permissive
davidmueller13/lean
65a3ed141b4088cd0a268e4de80eb6778b21a0e9
c626e2e3c6f3771e07c32e82ee5b9e030de5b050
refs/heads/master
1,611,278,313,401
1,444,021,177,000
1,444,021,177,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
142
lean
import logic definition id {A : Type} (a : A) := a theorem tst (a : Prop) : a → id a := begin intro Ha, whnf, state, apply Ha end
5b30e6b7194f0607889b7a401282ad274b1e8e75
ce89339993655da64b6ccb555c837ce6c10f9ef4
/zeptometer/topprover/10.lean
87cb10a80d6365a89698ad685f08258456e380f0
[]
no_license
zeptometer/LearnLean
ef32dc36a22119f18d843f548d0bb42f907bff5d
bb84d5dbe521127ba134d4dbf9559b294a80b9f7
refs/heads/master
1,625,710,824,322
1,601,382,570,000
1,601,382,570,000
195,228,870
2
0
null
null
null
null
UTF-8
Lean
false
false
1,763
lean
-- 10.lean def prefix_sum : ℕ → list ℕ → list ℕ | sum [] := [sum] | sum (head :: tail) := sum :: (prefix_sum (sum + head) tail) def plus_list : list ℕ → list ℕ → list ℕ | [] _ := [] | _ [] := [] | (h1 :: t1) (h2 :: t2) := (h1 + h2) :: (plus_list t1 t2) lemma plus_list_nil_l : ∀l, plus_list [] l = [] := begin intro l, cases l; simp [plus_list] end lemma plus_list_nil_r : ∀l, plus_list l [] = [] := begin intro l, cases l; simp [plus_list] end lemma plus_list_prefix_sum_comm : ∀l1 l2: list ℕ, ∀n m: ℕ, prefix_sum (n + m) (plus_list l1 l2) = plus_list (prefix_sum n l1) (prefix_sum m l2) := begin intro l1, induction l1, intro l2, induction l2, intros n m, simp [plus_list, prefix_sum, plus_list_nil_l, plus_list_nil_r], intros n m, simp [plus_list, prefix_sum, plus_list_nil_l, plus_list_nil_r], intro l2, induction l2, intros n m, simp [plus_list, prefix_sum, plus_list_nil_l, plus_list_nil_r], intros n m, simp [plus_list, prefix_sum, plus_list_nil_l, plus_list_nil_r], have h : (l1_hd + (l2_hd + (n + m))) = (n + l1_hd) + (m + l2_hd) := by simp, rw h, simp [prefix_sum] at l2_ih, have l3 : prefix_sum ((l1_hd + n) + (l2_hd + m)) (plus_list l1_tl l2_tl) = plus_list (prefix_sum (l1_hd + n) l1_tl) (prefix_sum (l2_hd + m) l2_tl) := by apply l1_ih, have c1: n + l1_hd + (m + l2_hd) = l1_hd + n + (l2_hd + m) := by simp, rw c1, exact l3 end example : ∀l1 l2: list ℕ, prefix_sum 0 (plus_list l1 l2) = plus_list (prefix_sum 0 l1) (prefix_sum 0 l2) := begin intros, apply plus_list_prefix_sum_comm l1 l2 0 0 end
798ebcfd400c73f871ee0cc717fb2a93461ff7f7
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Meta/GeneralizeTelescope.lean
d6a77e87edd42ed2fcedc8da5736cb4fd4b8facd
[ "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
3,857
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.KAbstract import Lean.Meta.Check namespace Lean.Meta namespace GeneralizeTelescope structure Entry where expr : Expr type : Expr modified : Bool partial def updateTypes (e eNew : Expr) (entries : Array Entry) (i : Nat) : MetaM (Array Entry) := if h : i < entries.size then let entry := entries.get ⟨i, h⟩ match entry with | ⟨_, type, _⟩ => do let typeAbst ← kabstract type e if typeAbst.hasLooseBVars then do let typeNew := typeAbst.instantiate1 eNew let entries := entries.set ⟨i, h⟩ { entry with type := typeNew, modified := true } updateTypes e eNew entries (i+1) else updateTypes e eNew entries (i+1) else pure entries partial def generalizeTelescopeAux {α} (k : Array Expr → MetaM α) (entries : Array Entry) (i : Nat) (fvars : Array Expr) : MetaM α := do if h : i < entries.size then let replace (baseUserName : Name) (e : Expr) (type : Expr) : MetaM α := do let userName ← mkFreshUserName baseUserName withLocalDeclD userName type fun x => do let entries ← updateTypes e x entries (i+1) generalizeTelescopeAux k entries (i+1) (fvars.push x) match entries.get ⟨i, h⟩ with | ⟨e@(.fvar fvarId), type, false⟩ => let localDecl ← fvarId.getDecl match localDecl with | .cdecl .. => generalizeTelescopeAux k entries (i+1) (fvars.push e) | .ldecl .. => replace localDecl.userName e type | ⟨e, type, modified⟩ => if modified then unless (← isTypeCorrect type) do throwError "failed to create telescope generalizing {entries.map Entry.expr}" replace `x e type else k fvars end GeneralizeTelescope open GeneralizeTelescope /-- Given expressions `es := #[e_1, e_2, ..., e_n]`, execute `k` with the free variables `(x_1 : A_1) (x_2 : A_2 [x_1]) ... (x_n : A_n [x_1, ... x_{n-1}])`. Moreover, - type of `e_1` is definitionally equal to `A_1`, - type of `e_2` is definitionally equal to `A_2[e_1]`. - ... - type of `e_n` is definitionally equal to `A_n[e_1, ..., e_{n-1}]`. This method tries to avoid the creation of new free variables. For example, if `e_i` is a free variable `x_i` and it is not a let-declaration variable, and its type does not depend on previous `e_j`s, the method will just use `x_i`. The telescope `x_1 ... x_n` can be used to create lambda and forall abstractions. Moreover, for any type correct lambda abstraction `f` constructed using `mkForall #[x_1, ..., x_n] ...`, The application `f e_1 ... e_n` is also type correct. The `kabstract` method is used to "locate" and abstract forward dependencies. That is, an occurrence of `e_i` in the of `e_j` for `j > i`. The method checks whether the abstract types `A_i` are type correct. Here is an example where `generalizeTelescope` fails to create the telescope `x_1 ... x_n`. Assume the local context contains `(n : Nat := 10) (xs : Vec Nat n) (ys : Vec Nat 10) (h : xs = ys)`. Then, assume we invoke `generalizeTelescope` with `es := #[10, xs, ys, h]` A type error is detected when processing `h`'s type. At this point, the method had successfully produced ``` (x_1 : Nat) (xs : Vec Nat n) (x_2 : Vec Nat x_1) ``` and the type for the new variable abstracting `h` is `xs = x_2` which is not type correct. -/ def generalizeTelescope {α} (es : Array Expr) (k : Array Expr → MetaM α) : MetaM α := do let es ← es.mapM fun e => do let type ← inferType e let type ← instantiateMVars type pure { expr := e, type := type, modified := false : Entry } generalizeTelescopeAux k es 0 #[] end Lean.Meta
41c0bddc44fad71a647eeb0b97f821e6bcaf8548
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Widget/InteractiveCode.lean
280ea1e53b046ec448ff5135ce040c0b6089c131
[ "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
3,517
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wojciech Nawrocki -/ import Lean.PrettyPrinter import Lean.Server.Rpc.Basic import Lean.Server.InfoUtils import Lean.Widget.TaggedText import Lean.Widget.Basic /-! RPC infrastructure for storing and formatting code fragments, in particular `Expr`s, with environment and subexpression information. -/ namespace Lean.Widget open Server /-- A tag indicating the diff status of the expression. Used when showing tactic diffs. -/ inductive DiffTag where | wasChanged | willChange | wasDeleted | willDelete | wasInserted | willInsert deriving ToJson, FromJson /-- Information about a subexpression within delaborated code. -/ structure SubexprInfo where /-- The `Elab.Info` node with the semantics of this part of the output. -/ info : WithRpcRef Lean.Elab.InfoWithCtx /-- The position of this subexpression within the top-level expression. See `Lean.SubExpr`. -/ subexprPos : Lean.SubExpr.Pos -- TODO(WN): add fields for semantic highlighting -- kind : Lsp.SymbolKind /-- In certain situations such as when goal states change between positions in a tactic-mode proof, we can show subexpression-level diffs between two expressions. This field asks the renderer to display the subexpression as in a diff view (e.g. red/green like `git diff`). -/ diffStatus? : Option DiffTag := none deriving RpcEncodable /-- Pretty-printed syntax (usually but not necessarily an `Expr`) with embedded `Info`s. -/ abbrev CodeWithInfos := TaggedText SubexprInfo def CodeWithInfos.mergePosMap [Monad m] (merger : SubexprInfo → α → m SubexprInfo) (pm : Lean.SubExpr.PosMap α) (tt : CodeWithInfos) : m CodeWithInfos := if pm.isEmpty then return tt else tt.mapM (fun (info : SubexprInfo) => match pm.find? info.subexprPos with | some a => merger info a | none => pure info ) def CodeWithInfos.pretty (tt : CodeWithInfos) := tt.stripTags def SubexprInfo.withDiffTag (tag : DiffTag) (c : SubexprInfo) : SubexprInfo := { c with diffStatus? := some tag } /-- Tags pretty-printed code with infos from the delaborator. -/ partial def tagCodeInfos (ctx : Elab.ContextInfo) (infos : SubExpr.PosMap Elab.Info) (tt : TaggedText (Nat × Nat)) : CodeWithInfos := go tt where go (tt : TaggedText (Nat × Nat)) := tt.rewrite fun (n, _) subTt => match infos.find? n with | none => go subTt | some i => let t : SubexprInfo := { info := WithRpcRef.mk { ctx, info := i, children := .empty } subexprPos := n } TaggedText.tag t (go subTt) def ppExprTagged (e : Expr) (explicit : Bool := false) : MetaM CodeWithInfos := do if pp.raw.get (← getOptions) then return .text (toString e) let delab := open PrettyPrinter.Delaborator in if explicit then withOptionAtCurrPos pp.tagAppFns.name true do withOptionAtCurrPos pp.explicit.name true do delabAppImplicit <|> delabAppExplicit else delab let ⟨fmt, infos⟩ ← PrettyPrinter.ppExprWithInfos e (delab := delab) let tt := TaggedText.prettyTagged fmt let ctx := { env := (← getEnv) mctx := (← getMCtx) options := (← getOptions) currNamespace := (← getCurrNamespace) openDecls := (← getOpenDecls) fileMap := default ngen := (← getNGen) } return tagCodeInfos ctx infos tt end Lean.Widget
0c6826fe6d200204c624274ceb9cbd0c73878e3f
f3849be5d845a1cb97680f0bbbe03b85518312f0
/tests/lean/notation7.lean
95492fc603068cd315d7f73f0d3ab200caa8ff8b
[ "Apache-2.0" ]
permissive
bjoeris/lean
0ed95125d762b17bfcb54dad1f9721f953f92eeb
4e496b78d5e73545fa4f9a807155113d8e6b0561
refs/heads/master
1,611,251,218,281
1,495,337,658,000
1,495,337,658,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
203
lean
-- open num constant f : num → num constant g : num → num → num notation A `:+1`:100000 := f A #check g 0:+1:+1 (1:+1 + 2:+1):+1 set_option pp.notation false #check g 0:+1:+1 (1:+1 + 2:+1):+1
e987ab01b090b379b1c70cf8e0a5a032fa1690fa
28be2ab6091504b6ba250b367205fb94d50ab284
/levels/my_solutions/world2_multiplication.lean
3320d078e17e1b83ca19aa1ee89d8637e407daa3
[ "Apache-2.0" ]
permissive
postmasters/natural_number_game
87304ac22e5e1c5ac2382d6e523d6914dd67a92d
38a7adcdfdb18c49c87b37831736c8f15300d821
refs/heads/master
1,649,856,819,031
1,586,444,676,000
1,586,444,676,000
255,006,061
0
0
Apache-2.0
1,586,664,599,000
1,586,664,598,000
null
UTF-8
Lean
false
false
2,344
lean
import solutions.world1_addition -- addition lemmas import mynat.mul /- Here's what you get from the import: 1) The following data: * a function called mynat.mul, and notation a * b for this function 2) The following axioms: * `mul_zero : ∀ a : mynat, a * 0 = 0` * `mul_succ : ∀ a b : mynat, a * succ(b) = a * b + a` These axiom between them tell you how to work out a * x for every x; use induction on x to reduce to the case either `x = 0` or `x = succ b`, and then use `mul_zero` or `mul_succ` appropriately. -/ namespace mynat lemma zero_mul (m : mynat) : 0 * m = 0 := begin [nat_num_game] sorry end lemma mul_one (m : mynat) : m * 1 = m := begin [nat_num_game] sorry end lemma one_mul (m : mynat) : 1 * m = m := begin [nat_num_game] sorry end -- mul_assoc immediately, leads to this: -- ⊢ a * (b * d) + a * b = a * (b * d + b) -- so let's prove mul_add first. lemma mul_add (a b c : mynat) : a * (b + c) = a * b + a * c := begin [nat_num_game] sorry end -- just ignore this def left_distrib := mul_add -- stupid field name, -- I just don't instinctively know what left_distrib means lemma mul_assoc (a b c : mynat) : (a * b) * c = a * (b * c) := begin [nat_num_game] sorry end -- goal : mul_comm. -- mul_comm leads to ⊢ a * d + a = succ d * a -- so perhaps we need add_mul -- but add_mul leads to either a+b+c=a+c+b or (a+b)+(c+d)=(a+c)+(b+d) -- (depending on whether we do induction on b or c) -- I need this for mul_comm lemma succ_mul (a b : mynat) : succ a * b = a * b + b := begin [nat_num_game] sorry end lemma add_mul (a b c : mynat) : (a + b) * c = a * c + b * c := begin [nat_num_game] sorry end -- ignore this def right_distrib := add_mul -- stupid field name, lemma mul_comm (a b : mynat) : a * b = b * a := begin [nat_num_game] sorry end theorem mul_pos (a b : mynat) : a ≠ 0 → b ≠ 0 → a * b ≠ 0 := begin [nat_num_game] sorry end theorem eq_zero_or_eq_zero_of_mul_eq_zero ⦃a b : mynat⦄ (h : a * b = 0) : a = 0 ∨ b = 0 := begin [nat_num_game] sorry end theorem mul_eq_zero_iff : ∀ (a b : mynat), a * b = 0 ↔ a = 0 ∨ b = 0 := begin [nat_num_game] sorry end instance : comm_semiring mynat := by structure_helper theorem mul_left_cancel ⦃a b c : mynat⦄ (ha : a ≠ 0) : a * b = a * c → b = c := begin [nat_num_game] sorry end end mynat
6c9a5352832e099302dcceac4a96d09462b0a08c
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/group_theory/finiteness.lean
b94a20928c61a54d1298a0626936ba9b3c79c58b
[ "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
8,278
lean
/- Copyright (c) 2021 Riccardo Brasca. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Riccardo Brasca -/ import data.set.finite import group_theory.submonoid.operations import group_theory.subgroup /-! # Finitely generated monoids and groups We define finitely generated monoids and groups. See also `submodule.fg` and `module.finite` for finitely-generated modules. ## Main definition * `submonoid.fg S`, `add_submonoid.fg S` : A submonoid `S` is finitely generated. * `monoid.fg M`, `add_monoid.fg M` : A typeclass indicating a type `M` is finitely generated as a monoid. * `subgroup.fg S`, `add_subgroup.fg S` : A subgroup `S` is finitely generated. * `group.fg M`, `add_group.fg M` : A typeclass indicating a type `M` is finitely generated as a group. -/ /-! ### Monoids and submonoids -/ open_locale pointwise variables {M N : Type*} [monoid M] [add_monoid N] section submonoid /-- A submonoid of `M` is finitely generated if it is the closure of a finite subset of `M`. -/ @[to_additive] def submonoid.fg (P : submonoid M) : Prop := ∃ S : finset M, submonoid.closure ↑S = P /-- An additive submonoid of `N` is finitely generated if it is the closure of a finite subset of `M`. -/ add_decl_doc add_submonoid.fg /-- An equivalent expression of `submonoid.fg` in terms of `set.finite` instead of `finset`. -/ @[to_additive "An equivalent expression of `add_submonoid.fg` in terms of `set.finite` instead of `finset`."] lemma submonoid.fg_iff (P : submonoid M) : submonoid.fg P ↔ ∃ S : set M, submonoid.closure S = P ∧ S.finite := ⟨λ ⟨S, hS⟩, ⟨S, hS, finset.finite_to_set S⟩, λ ⟨S, hS, hf⟩, ⟨set.finite.to_finset hf, by simp [hS]⟩⟩ lemma submonoid.fg_iff_add_fg (P : submonoid M) : P.fg ↔ P.to_add_submonoid.fg := ⟨λ h, let ⟨S, hS, hf⟩ := (submonoid.fg_iff _).1 h in (add_submonoid.fg_iff _).mpr ⟨additive.to_mul ⁻¹' S, by simp [← submonoid.to_add_submonoid_closure, hS], hf⟩, λ h, let ⟨T, hT, hf⟩ := (add_submonoid.fg_iff _).1 h in (submonoid.fg_iff _).mpr ⟨multiplicative.of_add ⁻¹' T, by simp [← add_submonoid.to_submonoid'_closure, hT], hf⟩⟩ lemma add_submonoid.fg_iff_mul_fg (P : add_submonoid N) : P.fg ↔ P.to_submonoid.fg := begin convert (submonoid.fg_iff_add_fg P.to_submonoid).symm, exact set_like.ext' rfl end end submonoid section monoid variables (M N) /-- A monoid is finitely generated if it is finitely generated as a submonoid of itself. -/ class monoid.fg : Prop := (out : (⊤ : submonoid M).fg) /-- An additive monoid is finitely generated if it is finitely generated as an additive submonoid of itself. -/ class add_monoid.fg : Prop := (out : (⊤ : add_submonoid N).fg) attribute [to_additive] monoid.fg variables {M N} lemma monoid.fg_def : monoid.fg M ↔ (⊤ : submonoid M).fg := ⟨λ h, h.1, λ h, ⟨h⟩⟩ lemma add_monoid.fg_def : add_monoid.fg N ↔ (⊤ : add_submonoid N).fg := ⟨λ h, h.1, λ h, ⟨h⟩⟩ /-- An equivalent expression of `monoid.fg` in terms of `set.finite` instead of `finset`. -/ @[to_additive "An equivalent expression of `add_monoid.fg` in terms of `set.finite` instead of `finset`."] lemma monoid.fg_iff : monoid.fg M ↔ ∃ S : set M, submonoid.closure S = (⊤ : submonoid M) ∧ S.finite := ⟨λ h, (submonoid.fg_iff ⊤).1 h.out, λ h, ⟨(submonoid.fg_iff ⊤).2 h⟩⟩ lemma monoid.fg_iff_add_fg : monoid.fg M ↔ add_monoid.fg (additive M) := ⟨λ h, ⟨(submonoid.fg_iff_add_fg ⊤).1 h.out⟩, λ h, ⟨(submonoid.fg_iff_add_fg ⊤).2 h.out⟩⟩ lemma add_monoid.fg_iff_mul_fg : add_monoid.fg N ↔ monoid.fg (multiplicative N) := ⟨λ h, ⟨(add_submonoid.fg_iff_mul_fg ⊤).1 h.out⟩, λ h, ⟨(add_submonoid.fg_iff_mul_fg ⊤).2 h.out⟩⟩ instance add_monoid.fg_of_monoid_fg [monoid.fg M] : add_monoid.fg (additive M) := monoid.fg_iff_add_fg.1 ‹_› instance monoid.fg_of_add_monoid_fg [add_monoid.fg N] : monoid.fg (multiplicative N) := add_monoid.fg_iff_mul_fg.1 ‹_› end monoid /-! ### Groups and subgroups -/ variables {G H : Type*} [group G] [add_group H] section subgroup /-- A subgroup of `G` is finitely generated if it is the closure of a finite subset of `G`. -/ @[to_additive] def subgroup.fg (P : subgroup G) : Prop := ∃ S : finset G, subgroup.closure ↑S = P /-- An additive subgroup of `H` is finitely generated if it is the closure of a finite subset of `H`. -/ add_decl_doc add_subgroup.fg /-- An equivalent expression of `subgroup.fg` in terms of `set.finite` instead of `finset`. -/ @[to_additive "An equivalent expression of `add_subgroup.fg` in terms of `set.finite` instead of `finset`."] lemma subgroup.fg_iff (P : subgroup G) : subgroup.fg P ↔ ∃ S : set G, subgroup.closure S = P ∧ S.finite := ⟨λ⟨S, hS⟩, ⟨S, hS, finset.finite_to_set S⟩, λ⟨S, hS, hf⟩, ⟨set.finite.to_finset hf, by simp [hS]⟩⟩ /-- A subgroup is finitely generated if and only if it is finitely generated as a submonoid. -/ @[to_additive add_subgroup.fg_iff_add_submonoid.fg "An additive subgroup is finitely generated if and only if it is finitely generated as an additive submonoid."] lemma subgroup.fg_iff_submonoid_fg (P : subgroup G) : P.fg ↔ P.to_submonoid.fg := begin split, { rintro ⟨S, rfl⟩, rw submonoid.fg_iff, refine ⟨S ∪ S⁻¹, _, S.finite_to_set.union S.finite_to_set.inv⟩, exact (subgroup.closure_to_submonoid _).symm }, { rintro ⟨S, hS⟩, refine ⟨S, le_antisymm _ _⟩, { rw [subgroup.closure_le, ←subgroup.coe_to_submonoid, ←hS], exact submonoid.subset_closure }, { rw [← subgroup.to_submonoid_le, ← hS, submonoid.closure_le], exact subgroup.subset_closure } } end lemma subgroup.fg_iff_add_fg (P : subgroup G) : P.fg ↔ P.to_add_subgroup.fg := begin rw [subgroup.fg_iff_submonoid_fg, add_subgroup.fg_iff_add_submonoid.fg], exact (subgroup.to_submonoid P).fg_iff_add_fg end lemma add_subgroup.fg_iff_mul_fg (P : add_subgroup H) : P.fg ↔ P.to_subgroup.fg := begin rw [add_subgroup.fg_iff_add_submonoid.fg, subgroup.fg_iff_submonoid_fg], exact add_submonoid.fg_iff_mul_fg (add_subgroup.to_add_submonoid P) end end subgroup section group variables (G H) /-- A group is finitely generated if it is finitely generated as a submonoid of itself. -/ class group.fg : Prop := (out : (⊤ : subgroup G).fg) /-- An additive group is finitely generated if it is finitely generated as an additive submonoid of itself. -/ class add_group.fg : Prop := (out : (⊤ : add_subgroup H).fg) attribute [to_additive] group.fg variables {G H} lemma group.fg_def : group.fg G ↔ (⊤ : subgroup G).fg := ⟨λ h, h.1, λ h, ⟨h⟩⟩ lemma add_group.fg_def : add_group.fg H ↔ (⊤ : add_subgroup H).fg := ⟨λ h, h.1, λ h, ⟨h⟩⟩ /-- An equivalent expression of `group.fg` in terms of `set.finite` instead of `finset`. -/ @[to_additive "An equivalent expression of `add_group.fg` in terms of `set.finite` instead of `finset`."] lemma group.fg_iff : group.fg G ↔ ∃ S : set G, subgroup.closure S = (⊤ : subgroup G) ∧ S.finite := ⟨λ h, (subgroup.fg_iff ⊤).1 h.out, λ h, ⟨(subgroup.fg_iff ⊤).2 h⟩⟩ /-- A group is finitely generated if and only if it is finitely generated as a monoid. -/ @[to_additive add_group.fg_iff_add_monoid.fg "An additive group is finitely generated if and only if it is finitely generated as an additive monoid."] lemma group.fg_iff_monoid.fg : group.fg G ↔ monoid.fg G := ⟨λ h, monoid.fg_def.2 $ (subgroup.fg_iff_submonoid_fg ⊤).1 (group.fg_def.1 h), λ h, group.fg_def.2 $ (subgroup.fg_iff_submonoid_fg ⊤).2 (monoid.fg_def.1 h)⟩ lemma group_fg.iff_add_fg : group.fg G ↔ add_group.fg (additive G) := ⟨λ h, ⟨(subgroup.fg_iff_add_fg ⊤).1 h.out⟩, λ h, ⟨(subgroup.fg_iff_add_fg ⊤).2 h.out⟩⟩ lemma add_group.fg_iff_mul_fg : add_group.fg H ↔ group.fg (multiplicative H) := ⟨λ h, ⟨(add_subgroup.fg_iff_mul_fg ⊤).1 h.out⟩, λ h, ⟨(add_subgroup.fg_iff_mul_fg ⊤).2 h.out⟩⟩ instance add_group.fg_of_group_fg [group.fg G] : add_group.fg (additive G) := group_fg.iff_add_fg.1 ‹_› instance group.fg_of_mul_group_fg [add_group.fg H] : group.fg (multiplicative H) := add_group.fg_iff_mul_fg.1 ‹_› end group
88d0c7cba1add3b4c49598a4a124c7558d74e5e4
64874bd1010548c7f5a6e3e8902efa63baaff785
/tests/lean/run/is_nil.lean
75925f30d347e820fd83ec0aa95cd6c635980455
[ "Apache-2.0" ]
permissive
tjiaqi/lean
4634d729795c164664d10d093f3545287c76628f
d0ce4cf62f4246b0600c07e074d86e51f2195e30
refs/heads/master
1,622,323,796,480
1,422,643,069,000
1,422,643,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,341
lean
import logic open tactic inductive list (A : Type) : Type := nil {} : list A, cons : A → list A → list A namespace list end list open list open eq definition is_nil {A : Type} (l : list A) : Prop := list.rec true (fun h t r, false) l theorem is_nil_nil (A : Type) : is_nil (@nil A) := of_eq_true (refl true) theorem cons_ne_nil {A : Type} (a : A) (l : list A) : ¬ cons a l = nil := not.intro (assume H : cons a l = nil, absurd (calc true = is_nil (@nil A) : refl _ ... = is_nil (cons a l) : { symm H } ... = false : refl _) true_ne_false) theorem T : is_nil (@nil Prop) := by apply is_nil_nil (* local list = Const("list", {1})(Prop) local isNil = Const("is_nil", {1})(Prop) local Nil = Const({"list", "nil"}, {1})(Prop) local m = mk_metavar("m", list) print(isNil(Nil)) print(isNil(m)) function test_unify(env, m, lhs, rhs, num_s) print(tostring(lhs) .. " =?= " .. tostring(rhs) .. ", expected: " .. tostring(num_s)) local ss = unify(env, lhs, rhs, name_generator(), substitution()) local n = 0 for s in ss do print("solution: " .. tostring(s:instantiate(m))) n = n + 1 end if num_s ~= n then print("n: " .. n) end assert(num_s == n) end print("=====================") test_unify(get_env(), m, isNil(Nil), isNil(m), 2) *)
57e0c78929d2b749e14f11d0c41f6642a3de3d66
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/special_functions/integrals.lean
22b8ec9e9bff1bffdfe393d01fc09c64f4acd6ad
[ "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
32,769
lean
/- Copyright (c) 2021 Benjamin Davidson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Benjamin Davidson -/ import measure_theory.integral.fund_thm_calculus import analysis.special_functions.trigonometric.arctan_deriv /-! # Integration of specific interval integrals > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file contains proofs of the integrals of various specific functions. This includes: * Integrals of simple functions, such as `id`, `pow`, `inv`, `exp`, `log` * Integrals of some trigonometric functions, such as `sin`, `cos`, `1 / (1 + x^2)` * The integral of `cos x ^ 2 - sin x ^ 2` * Reduction formulae for the integrals of `sin x ^ n` and `cos x ^ n` for `n ≥ 2` * The computation of `∫ x in 0..π, sin x ^ n` as a product for even and odd `n` (used in proving the Wallis product for pi) * Integrals of the form `sin x ^ m * cos x ^ n` With these lemmas, many simple integrals can be computed by `simp` or `norm_num`. See `test/integration.lean` for specific examples. This file also contains some facts about the interval integrability of specific functions. This file is still being developed. ## Tags integrate, integration, integrable, integrability -/ open real nat set finset open_locale real big_operators interval variables {a b : ℝ} (n : ℕ) namespace interval_integral open measure_theory variables {f : ℝ → ℝ} {μ ν : measure ℝ} [is_locally_finite_measure μ] (c d : ℝ) /-! ### Interval integrability -/ @[simp] lemma interval_integrable_pow : interval_integrable (λ x, x^n) μ a b := (continuous_pow n).interval_integrable a b lemma interval_integrable_zpow {n : ℤ} (h : 0 ≤ n ∨ (0 : ℝ) ∉ [a, b]) : interval_integrable (λ x, x ^ n) μ a b := (continuous_on_id.zpow₀ n $ λ x hx, h.symm.imp (ne_of_mem_of_not_mem hx) id).interval_integrable /-- See `interval_integrable_rpow'` for a version with a weaker hypothesis on `r`, but assuming the measure is volume. -/ lemma interval_integrable_rpow {r : ℝ} (h : 0 ≤ r ∨ (0 : ℝ) ∉ [a, b]) : interval_integrable (λ x, x ^ r) μ a b := (continuous_on_id.rpow_const $ λ x hx, h.symm.imp (ne_of_mem_of_not_mem hx) id).interval_integrable /-- See `interval_integrable_rpow` for a version applying to any locally finite measure, but with a stronger hypothesis on `r`. -/ lemma interval_integrable_rpow' {r : ℝ} (h : -1 < r) : interval_integrable (λ x, x ^ r) volume a b := begin suffices : ∀ (c : ℝ), interval_integrable (λ x, x ^ r) volume 0 c, { exact interval_integrable.trans (this a).symm (this b) }, have : ∀ (c : ℝ), (0 ≤ c) → interval_integrable (λ x, x ^ r) volume 0 c, { intros c hc, rw [interval_integrable_iff, uIoc_of_le hc], have hderiv : ∀ x ∈ Ioo 0 c, has_deriv_at (λ x : ℝ, x ^ (r + 1) / (r + 1)) (x ^ r) x, { intros x hx, convert (real.has_deriv_at_rpow_const (or.inl hx.1.ne')).div_const (r + 1), field_simp [(by linarith : r + 1 ≠ 0)], ring, }, apply integrable_on_deriv_of_nonneg _ hderiv, { intros x hx, apply rpow_nonneg_of_nonneg hx.1.le, }, { refine (continuous_on_id.rpow_const _).div_const _, intros x hx, right, linarith } }, intro c, rcases le_total 0 c with hc|hc, { exact this c hc }, { rw [interval_integrable.iff_comp_neg, neg_zero], have m := (this (-c) (by linarith)).smul (cos (r * π)), rw interval_integrable_iff at m ⊢, refine m.congr_fun _ measurable_set_Ioc, intros x hx, rw uIoc_of_le (by linarith : 0 ≤ -c) at hx, simp only [pi.smul_apply, algebra.id.smul_eq_mul, log_neg_eq_log, mul_comm, rpow_def_of_pos hx.1, rpow_def_of_neg (by linarith [hx.1] : -x < 0)], } end /-- See `interval_integrable_cpow'` for a version with a weaker hypothesis on `r`, but assuming the measure is volume. -/ lemma interval_integrable_cpow {r : ℂ} (h : 0 ≤ r.re ∨ (0 : ℝ) ∉ [a, b]) : interval_integrable (λ x : ℝ, (x : ℂ) ^ r) μ a b := begin by_cases h2 : (0:ℝ) ∉ [a,b], { -- Easy case #1: 0 ∉ [a, b] -- use continuity. refine (continuous_at.continuous_on (λ x hx, _)).interval_integrable, exact complex.continuous_at_of_real_cpow_const _ _ (or.inr $ ne_of_mem_of_not_mem hx h2) }, rw [eq_false_intro h2, or_false] at h, rcases lt_or_eq_of_le h with h'|h', { -- Easy case #2: 0 < re r -- again use continuity exact (complex.continuous_of_real_cpow_const h').interval_integrable _ _ }, -- Now the hard case: re r = 0 and 0 is in the interval. refine (interval_integrable.interval_integrable_norm_iff _).mp _, { refine (measurable_of_continuous_on_compl_singleton (0:ℝ) _).ae_strongly_measurable, exact continuous_at.continuous_on (λ x hx, complex.continuous_at_of_real_cpow_const x r (or.inr hx)) }, -- reduce to case of integral over `[0, c]` suffices : ∀ (c : ℝ), interval_integrable (λ x : ℝ, ‖↑x ^ r‖) μ 0 c, from (this a).symm.trans (this b), intro c, rcases le_or_lt 0 c with hc | hc, { -- case `0 ≤ c`: integrand is identically 1 have : interval_integrable (λ x, 1 : ℝ → ℝ) μ 0 c, from interval_integrable_const, rw interval_integrable_iff_integrable_Ioc_of_le hc at this ⊢, refine integrable_on.congr_fun this (λ x hx, _) measurable_set_Ioc, dsimp only, rw [complex.norm_eq_abs, complex.abs_cpow_eq_rpow_re_of_pos hx.1, ←h', rpow_zero], }, { -- case `c < 0`: integrand is identically constant, *except* at `x = 0` if `r ≠ 0`. apply interval_integrable.symm, rw [interval_integrable_iff_integrable_Ioc_of_le hc.le], have : Ioc c 0 = Ioo c 0 ∪ {(0:ℝ)}, { rw [←Ioo_union_Icc_eq_Ioc hc (le_refl 0), ←Icc_def], simp_rw [←le_antisymm_iff, set_of_eq_eq_singleton'] }, rw [this, integrable_on_union, and.comm], split, { refine integrable_on_singleton_iff.mpr (or.inr _), exact is_finite_measure_on_compacts_of_is_locally_finite_measure.lt_top_of_is_compact is_compact_singleton }, { have : ∀ (x : ℝ), x ∈ Ioo c 0 → ‖complex.exp (↑π * complex.I * r)‖ = ‖(x:ℂ) ^ r‖, { intros x hx, rw [complex.of_real_cpow_of_nonpos hx.2.le, norm_mul, ←complex.of_real_neg, complex.norm_eq_abs (_ ^ _), complex.abs_cpow_eq_rpow_re_of_pos (neg_pos.mpr hx.2), ←h', rpow_zero, one_mul] }, refine integrable_on.congr_fun _ this measurable_set_Ioo, rw integrable_on_const, refine or.inr ((measure_mono set.Ioo_subset_Icc_self).trans_lt _), exact is_finite_measure_on_compacts_of_is_locally_finite_measure.lt_top_of_is_compact is_compact_Icc } }, end /-- See `interval_integrable_cpow` for a version applying to any locally finite measure, but with a stronger hypothesis on `r`. -/ lemma interval_integrable_cpow' {r : ℂ} (h : -1 < r.re) : interval_integrable (λ x:ℝ, (x:ℂ) ^ r) volume a b := begin suffices : ∀ (c : ℝ), interval_integrable (λ x, (x : ℂ) ^ r) volume 0 c, { exact interval_integrable.trans (this a).symm (this b) }, have : ∀ (c : ℝ), (0 ≤ c) → interval_integrable (λ x, (x : ℂ) ^ r) volume 0 c, { intros c hc, rw ←interval_integrable.interval_integrable_norm_iff, { rw interval_integrable_iff, apply integrable_on.congr_fun, { rw ←interval_integrable_iff, exact (interval_integral.interval_integrable_rpow' h) }, { intros x hx, rw uIoc_of_le hc at hx, dsimp only, rw [complex.norm_eq_abs, complex.abs_cpow_eq_rpow_re_of_pos hx.1] }, { exact measurable_set_uIoc } }, { refine continuous_on.ae_strongly_measurable _ measurable_set_uIoc, refine continuous_at.continuous_on (λ x hx, _), rw uIoc_of_le hc at hx, refine (continuous_at_cpow_const (or.inl _)).comp complex.continuous_of_real.continuous_at, rw complex.of_real_re, exact hx.1 } }, intro c, rcases le_total 0 c with hc | hc, { exact this c hc }, { rw [interval_integrable.iff_comp_neg, neg_zero], have m := (this (-c) (by linarith)).const_mul (complex.exp (π * complex.I * r)), rw [interval_integrable_iff, uIoc_of_le (by linarith : 0 ≤ -c)] at m ⊢, refine m.congr_fun (λ x hx, _) measurable_set_Ioc, dsimp only, have : -x ≤ 0, by linarith [hx.1], rw [complex.of_real_cpow_of_nonpos this, mul_comm], simp } end @[simp] lemma interval_integrable_id : interval_integrable (λ x, x) μ a b := continuous_id.interval_integrable a b @[simp] lemma interval_integrable_const : interval_integrable (λ x, c) μ a b := continuous_const.interval_integrable a b lemma interval_integrable_one_div (h : ∀ x : ℝ, x ∈ [a, b] → f x ≠ 0) (hf : continuous_on f [a, b]) : interval_integrable (λ x, 1 / f x) μ a b := (continuous_on_const.div hf h).interval_integrable @[simp] lemma interval_integrable_inv (h : ∀ x : ℝ, x ∈ [a, b] → f x ≠ 0) (hf : continuous_on f [a, b]) : interval_integrable (λ x, (f x)⁻¹) μ a b := by simpa only [one_div] using interval_integrable_one_div h hf @[simp] lemma interval_integrable_exp : interval_integrable exp μ a b := continuous_exp.interval_integrable a b @[simp] lemma _root_.interval_integrable.log (hf : continuous_on f [a, b]) (h : ∀ x : ℝ, x ∈ [a, b] → f x ≠ 0) : interval_integrable (λ x, log (f x)) μ a b := (continuous_on.log hf h).interval_integrable @[simp] lemma interval_integrable_log (h : (0:ℝ) ∉ [a, b]) : interval_integrable log μ a b := interval_integrable.log continuous_on_id $ λ x hx, ne_of_mem_of_not_mem hx h @[simp] lemma interval_integrable_sin : interval_integrable sin μ a b := continuous_sin.interval_integrable a b @[simp] lemma interval_integrable_cos : interval_integrable cos μ a b := continuous_cos.interval_integrable a b lemma interval_integrable_one_div_one_add_sq : interval_integrable (λ x : ℝ, 1 / (1 + x^2)) μ a b := begin refine (continuous_const.div _ (λ x, _)).interval_integrable a b, { continuity }, { nlinarith }, end @[simp] lemma interval_integrable_inv_one_add_sq : interval_integrable (λ x : ℝ, (1 + x^2)⁻¹) μ a b := by simpa only [one_div] using interval_integrable_one_div_one_add_sq /-! ### Integrals of the form `c * ∫ x in a..b, f (c * x + d)` -/ @[simp] lemma mul_integral_comp_mul_right : c * ∫ x in a..b, f (x * c) = ∫ x in a*c..b*c, f x := smul_integral_comp_mul_right f c @[simp] lemma mul_integral_comp_mul_left : c * ∫ x in a..b, f (c * x) = ∫ x in c*a..c*b, f x := smul_integral_comp_mul_left f c @[simp] lemma inv_mul_integral_comp_div : c⁻¹ * ∫ x in a..b, f (x / c) = ∫ x in a/c..b/c, f x := inv_smul_integral_comp_div f c @[simp] lemma mul_integral_comp_mul_add : c * ∫ x in a..b, f (c * x + d) = ∫ x in c*a+d..c*b+d, f x := smul_integral_comp_mul_add f c d @[simp] lemma mul_integral_comp_add_mul : c * ∫ x in a..b, f (d + c * x) = ∫ x in d+c*a..d+c*b, f x := smul_integral_comp_add_mul f c d @[simp] lemma inv_mul_integral_comp_div_add : c⁻¹ * ∫ x in a..b, f (x / c + d) = ∫ x in a/c+d..b/c+d, f x := inv_smul_integral_comp_div_add f c d @[simp] lemma inv_mul_integral_comp_add_div : c⁻¹ * ∫ x in a..b, f (d + x / c) = ∫ x in d+a/c..d+b/c, f x := inv_smul_integral_comp_add_div f c d @[simp] lemma mul_integral_comp_mul_sub : c * ∫ x in a..b, f (c * x - d) = ∫ x in c*a-d..c*b-d, f x := smul_integral_comp_mul_sub f c d @[simp] lemma mul_integral_comp_sub_mul : c * ∫ x in a..b, f (d - c * x) = ∫ x in d-c*b..d-c*a, f x := smul_integral_comp_sub_mul f c d @[simp] lemma inv_mul_integral_comp_div_sub : c⁻¹ * ∫ x in a..b, f (x / c - d) = ∫ x in a/c-d..b/c-d, f x := inv_smul_integral_comp_div_sub f c d @[simp] lemma inv_mul_integral_comp_sub_div : c⁻¹ * ∫ x in a..b, f (d - x / c) = ∫ x in d-b/c..d-a/c, f x := inv_smul_integral_comp_sub_div f c d end interval_integral open interval_integral /-! ### Integrals of simple functions -/ lemma integral_cpow {r : ℂ} (h : -1 < r.re ∨ (r ≠ -1 ∧ (0 : ℝ) ∉ [a, b])) : ∫ (x : ℝ) in a..b, (x : ℂ) ^ r = (b ^ (r + 1) - a ^ (r + 1)) / (r + 1) := begin rw sub_div, have hr : r + 1 ≠ 0, { cases h, { apply_fun complex.re, rw [complex.add_re, complex.one_re, complex.zero_re, ne.def, add_eq_zero_iff_eq_neg], exact h.ne' }, { rw [ne.def, ←add_eq_zero_iff_eq_neg] at h, exact h.1 } }, by_cases hab : (0:ℝ) ∉ [a, b], { refine integral_eq_sub_of_has_deriv_at (λ x hx, _) (interval_integrable_cpow $ or.inr hab), refine has_deriv_at_of_real_cpow (ne_of_mem_of_not_mem hx hab) _, contrapose! hr, rwa add_eq_zero_iff_eq_neg }, replace h : -1 < r.re, by tauto, suffices : ∀ (c : ℝ), ∫ (x : ℝ) in 0..c, (x : ℂ) ^ r = c ^ (r + 1) / (r + 1) - 0 ^ (r + 1) / (r + 1), { rw [←integral_add_adjacent_intervals (@interval_integrable_cpow' a 0 r h) (@interval_integrable_cpow' 0 b r h), integral_symm, this a, this b, complex.zero_cpow hr], ring }, intro c, apply integral_eq_sub_of_has_deriv_right, { refine ((complex.continuous_of_real_cpow_const _).div_const _).continuous_on, rwa [complex.add_re, complex.one_re, ←neg_lt_iff_pos_add] }, { refine λ x hx, (has_deriv_at_of_real_cpow _ _).has_deriv_within_at, { rcases le_total c 0 with hc | hc, { rw max_eq_left hc at hx, exact hx.2.ne }, { rw min_eq_left hc at hx, exact hx.1.ne' } }, { contrapose! hr, rw hr, ring } }, { exact interval_integrable_cpow' h } end lemma integral_rpow {r : ℝ} (h : -1 < r ∨ (r ≠ -1 ∧ (0 : ℝ) ∉ [a, b])) : ∫ x in a..b, x ^ r = (b ^ (r + 1) - a ^ (r + 1)) / (r + 1) := begin have h' : -1 < (r:ℂ).re ∨ (r:ℂ) ≠ -1 ∧ (0:ℝ) ∉ [a, b], { cases h, { left, rwa complex.of_real_re }, { right, rwa [←complex.of_real_one, ←complex.of_real_neg, ne.def, complex.of_real_inj] } }, have : ∫ x in a..b, (x:ℂ) ^ (r :ℂ) = ((b:ℂ) ^ (r + 1 : ℂ) - (a:ℂ) ^ (r + 1 : ℂ)) / (r + 1), from integral_cpow h', apply_fun complex.re at this, convert this, { simp_rw [interval_integral_eq_integral_uIoc, complex.real_smul, complex.of_real_mul_re], { change complex.re with is_R_or_C.re, rw ←integral_re, refl, refine interval_integrable_iff.mp _, cases h', { exact interval_integrable_cpow' h' }, { exact interval_integrable_cpow (or.inr h'.2) } } }, { rw (by push_cast : ((r:ℂ) + 1) = ((r + 1 : ℝ) : ℂ)), simp_rw [div_eq_inv_mul, ←complex.of_real_inv, complex.of_real_mul_re, complex.sub_re], refl } end lemma integral_zpow {n : ℤ} (h : 0 ≤ n ∨ n ≠ -1 ∧ (0 : ℝ) ∉ [a, b]) : ∫ x in a..b, x ^ n = (b ^ (n + 1) - a ^ (n + 1)) / (n + 1) := begin replace h : -1 < (n : ℝ) ∨ (n : ℝ) ≠ -1 ∧ (0 : ℝ) ∉ [a, b], by exact_mod_cast h, exact_mod_cast integral_rpow h, end @[simp] lemma integral_pow : ∫ x in a..b, x ^ n = (b ^ (n + 1) - a ^ (n + 1)) / (n + 1) := by simpa only [←int.coe_nat_succ, zpow_coe_nat] using integral_zpow (or.inl (int.coe_nat_nonneg n)) /-- Integral of `|x - a| ^ n` over `Ι a b`. This integral appears in the proof of the Picard-Lindelöf/Cauchy-Lipschitz theorem. -/ lemma integral_pow_abs_sub_uIoc : ∫ x in Ι a b, |x - a| ^ n = |b - a| ^ (n + 1) / (n + 1) := begin cases le_or_lt a b with hab hab, { calc ∫ x in Ι a b, |x - a| ^ n = ∫ x in a..b, |x - a| ^ n : by rw [uIoc_of_le hab, ← integral_of_le hab] ... = ∫ x in 0..(b - a), x ^ n : begin simp only [integral_comp_sub_right (λ x, |x| ^ n), sub_self], refine integral_congr (λ x hx, congr_arg2 has_pow.pow (abs_of_nonneg $ _) rfl), rw uIcc_of_le (sub_nonneg.2 hab) at hx, exact hx.1 end ... = |b - a| ^ (n + 1) / (n + 1) : by simp [abs_of_nonneg (sub_nonneg.2 hab)] }, { calc ∫ x in Ι a b, |x - a| ^ n = ∫ x in b..a, |x - a| ^ n : by rw [uIoc_of_lt hab, ← integral_of_le hab.le] ... = ∫ x in b - a..0, (-x) ^ n : begin simp only [integral_comp_sub_right (λ x, |x| ^ n), sub_self], refine integral_congr (λ x hx, congr_arg2 has_pow.pow (abs_of_nonpos $ _) rfl), rw uIcc_of_le (sub_nonpos.2 hab.le) at hx, exact hx.2 end ... = |b - a| ^ (n + 1) / (n + 1) : by simp [integral_comp_neg (λ x, x ^ n), abs_of_neg (sub_neg.2 hab)] } end @[simp] lemma integral_id : ∫ x in a..b, x = (b ^ 2 - a ^ 2) / 2 := by simpa using integral_pow 1 @[simp] lemma integral_one : ∫ x in a..b, (1 : ℝ) = b - a := by simp only [mul_one, smul_eq_mul, integral_const] lemma integral_const_on_unit_interval : ∫ x in a..(a + 1), b = b := by simp @[simp] lemma integral_inv (h : (0:ℝ) ∉ [a, b]) : ∫ x in a..b, x⁻¹ = log (b / a) := begin have h' := λ x hx, ne_of_mem_of_not_mem hx h, rw [integral_deriv_eq_sub' _ deriv_log' (λ x hx, differentiable_at_log (h' x hx)) (continuous_on_inv₀.mono $ subset_compl_singleton_iff.mpr h), log_div (h' b right_mem_uIcc) (h' a left_mem_uIcc)], end @[simp] lemma integral_inv_of_pos (ha : 0 < a) (hb : 0 < b) : ∫ x in a..b, x⁻¹ = log (b / a) := integral_inv $ not_mem_uIcc_of_lt ha hb @[simp] lemma integral_inv_of_neg (ha : a < 0) (hb : b < 0) : ∫ x in a..b, x⁻¹ = log (b / a) := integral_inv $ not_mem_uIcc_of_gt ha hb lemma integral_one_div (h : (0:ℝ) ∉ [a, b]) : ∫ x : ℝ in a..b, 1/x = log (b / a) := by simp only [one_div, integral_inv h] lemma integral_one_div_of_pos (ha : 0 < a) (hb : 0 < b) : ∫ x : ℝ in a..b, 1/x = log (b / a) := by simp only [one_div, integral_inv_of_pos ha hb] lemma integral_one_div_of_neg (ha : a < 0) (hb : b < 0) : ∫ x : ℝ in a..b, 1/x = log (b / a) := by simp only [one_div, integral_inv_of_neg ha hb] @[simp] lemma integral_exp : ∫ x in a..b, exp x = exp b - exp a := by rw integral_deriv_eq_sub'; norm_num [continuous_on_exp] lemma integral_exp_mul_complex {c : ℂ} (hc : c ≠ 0) : ∫ x in a..b, complex.exp (c * x) = (complex.exp (c * b) - complex.exp (c * a)) / c := begin have D : ∀ (x : ℝ), has_deriv_at (λ (y : ℝ), complex.exp (c * y) / c) (complex.exp (c * x)) x, { intro x, conv { congr, skip, rw ←mul_div_cancel (complex.exp (c * x)) hc, }, convert ((complex.has_deriv_at_exp _).comp x _).div_const c using 1, simpa only [mul_one] using ((has_deriv_at_id (x:ℂ)).const_mul _).comp_of_real, }, rw integral_deriv_eq_sub' _ (funext (λ x, (D x).deriv)) (λ x hx, (D x).differentiable_at), { ring_nf }, { apply continuous.continuous_on, continuity,} end @[simp] lemma integral_log (h : (0:ℝ) ∉ [a, b]) : ∫ x in a..b, log x = b * log b - a * log a - b + a := begin obtain ⟨h', heq⟩ := ⟨λ x hx, ne_of_mem_of_not_mem hx h, λ x hx, mul_inv_cancel (h' x hx)⟩, convert integral_mul_deriv_eq_deriv_mul (λ x hx, has_deriv_at_log (h' x hx)) (λ x hx, has_deriv_at_id x) (continuous_on_inv₀.mono $ subset_compl_singleton_iff.mpr h).interval_integrable continuous_on_const.interval_integrable using 1; simp [integral_congr heq, mul_comm, ← sub_add], end @[simp] lemma integral_log_of_pos (ha : 0 < a) (hb : 0 < b) : ∫ x in a..b, log x = b * log b - a * log a - b + a := integral_log $ not_mem_uIcc_of_lt ha hb @[simp] lemma integral_log_of_neg (ha : a < 0) (hb : b < 0) : ∫ x in a..b, log x = b * log b - a * log a - b + a := integral_log $ not_mem_uIcc_of_gt ha hb @[simp] lemma integral_sin : ∫ x in a..b, sin x = cos a - cos b := by rw integral_deriv_eq_sub' (λ x, -cos x); norm_num [continuous_on_sin] @[simp] lemma integral_cos : ∫ x in a..b, cos x = sin b - sin a := by rw integral_deriv_eq_sub'; norm_num [continuous_on_cos] lemma integral_cos_mul_complex {z : ℂ} (hz : z ≠ 0) (a b : ℝ) : ∫ x in a..b, complex.cos (z * x) = complex.sin (z * b) / z - complex.sin (z * a) / z := begin apply integral_eq_sub_of_has_deriv_at, swap, { apply continuous.interval_integrable, exact complex.continuous_cos.comp (continuous_const.mul complex.continuous_of_real) }, intros x hx, have a := complex.has_deriv_at_sin (↑x * z), have b : has_deriv_at (λ y, y * z : ℂ → ℂ) z ↑x := has_deriv_at_mul_const _, have c : has_deriv_at (λ (y : ℂ), complex.sin (y * z)) _ ↑x := has_deriv_at.comp x a b, convert has_deriv_at.comp_of_real (c.div_const z), { simp_rw mul_comm }, { rw [mul_div_cancel _ hz, mul_comm] }, end lemma integral_cos_sq_sub_sin_sq : ∫ x in a..b, cos x ^ 2 - sin x ^ 2 = sin b * cos b - sin a * cos a := by simpa only [sq, sub_eq_add_neg, neg_mul_eq_mul_neg] using integral_deriv_mul_eq_sub (λ x hx, has_deriv_at_sin x) (λ x hx, has_deriv_at_cos x) continuous_on_cos.interval_integrable continuous_on_sin.neg.interval_integrable @[simp] lemma integral_inv_one_add_sq : ∫ x : ℝ in a..b, (1 + x^2)⁻¹ = arctan b - arctan a := begin simp only [← one_div], refine integral_deriv_eq_sub' _ _ _ (continuous_const.div _ (λ x, _)).continuous_on, { norm_num }, { norm_num }, { continuity }, { nlinarith }, end lemma integral_one_div_one_add_sq : ∫ x : ℝ in a..b, 1 / (1 + x^2) = arctan b - arctan a := by simp only [one_div, integral_inv_one_add_sq] section rpow_cpow open complex lemma integral_mul_cpow_one_add_sq {t : ℂ} (ht : t ≠ -1) : ∫ x : ℝ in a..b, (x:ℂ) * (1 + x ^ 2) ^ t = (1 + b ^ 2) ^ (t + 1) / (2 * (t + 1)) - (1 + a ^ 2) ^ (t + 1) / (2 * (t + 1)) := begin have : t + 1 ≠ 0 := by { contrapose! ht, rwa add_eq_zero_iff_eq_neg at ht }, apply integral_eq_sub_of_has_deriv_at, { intros x hx, have f : has_deriv_at (λ y:ℂ, 1 + y ^ 2) (2 * x) x, { convert (has_deriv_at_pow 2 (x:ℂ)).const_add 1, { norm_cast }, { simp } }, have g : ∀ {z : ℂ}, (0 < z.re) → has_deriv_at (λ z, z ^ (t + 1) / (2 * (t + 1))) (z ^ t / 2) z, { intros z hz, have : z ≠ 0 := by { contrapose! hz, rw [hz, zero_re], }, convert (has_deriv_at.cpow_const (has_deriv_at_id _) (or.inl hz)).div_const (2 * (t + 1)) using 1, field_simp, ring }, convert (has_deriv_at.comp ↑x (g _) f).comp_of_real using 1, { field_simp, ring }, { rw [add_re, one_re, ←of_real_pow, of_real_re], exact (add_pos_of_pos_of_nonneg zero_lt_one (sq_nonneg x)) } }, { apply continuous.interval_integrable, refine continuous_of_real.mul _, apply continuous.cpow, { exact continuous_const.add (continuous_of_real.pow 2)}, { exact continuous_const }, { intro a, rw [add_re, one_re, ←of_real_pow, of_real_re], exact or.inl (add_pos_of_pos_of_nonneg zero_lt_one (sq_nonneg a)) } } end lemma integral_mul_rpow_one_add_sq {t : ℝ} (ht : t ≠ -1) : ∫ x : ℝ in a..b, x * (1 + x ^ 2) ^ t = (1 + b ^ 2) ^ (t + 1) / (2 * (t + 1)) - (1 + a ^ 2) ^ (t + 1) / (2 * (t + 1)) := begin have : ∀ (x s : ℝ), (((1 + x ^ 2) ^ s : ℝ) : ℂ) = (1 + (x : ℂ) ^ 2) ^ ↑s, { intros x s, rw [of_real_cpow, of_real_add, of_real_pow, of_real_one], exact add_nonneg zero_le_one (sq_nonneg x), }, rw ←of_real_inj, convert integral_mul_cpow_one_add_sq (_ : (t:ℂ) ≠ -1), { rw ←interval_integral.integral_of_real, congr' with x:1, rw [of_real_mul, this x t] }, { simp_rw [of_real_sub, of_real_div, this a (t + 1), this b (t + 1)], push_cast }, { rw [←of_real_one, ←of_real_neg, ne.def, of_real_inj], exact ht }, end end rpow_cpow /-! ### Integral of `sin x ^ n` -/ lemma integral_sin_pow_aux : ∫ x in a..b, sin x ^ (n + 2) = sin a ^ (n + 1) * cos a - sin b ^ (n + 1) * cos b + (n + 1) * (∫ x in a..b, sin x ^ n) - (n + 1) * ∫ x in a..b, sin x ^ (n + 2) := begin let C := sin a ^ (n + 1) * cos a - sin b ^ (n + 1) * cos b, have h : ∀ α β γ : ℝ, (β * α * γ) * α = β * (α * α * γ) := λ α β γ, by ring, have hu : ∀ x ∈ _, has_deriv_at (λ y, sin y ^ (n + 1)) ((n + 1 : ℕ) * cos x * sin x ^ n) x := λ x hx, by simpa only [mul_right_comm] using (has_deriv_at_sin x).pow (n+1), have hv : ∀ x ∈ [a, b], has_deriv_at (-cos) (sin x) x := λ x hx, by simpa only [neg_neg] using (has_deriv_at_cos x).neg, have H := integral_mul_deriv_eq_deriv_mul hu hv _ _, calc ∫ x in a..b, sin x ^ (n + 2) = ∫ x in a..b, sin x ^ (n + 1) * sin x : by simp only [pow_succ'] ... = C + (n + 1) * ∫ x in a..b, cos x ^ 2 * sin x ^ n : by simp [H, h, sq] ... = C + (n + 1) * ∫ x in a..b, sin x ^ n - sin x ^ (n + 2) : by simp [cos_sq', sub_mul, ← pow_add, add_comm] ... = C + (n + 1) * (∫ x in a..b, sin x ^ n) - (n + 1) * ∫ x in a..b, sin x ^ (n + 2) : by rw [integral_sub, mul_sub, add_sub_assoc]; apply continuous.interval_integrable; continuity, all_goals { apply continuous.interval_integrable, continuity }, end /-- The reduction formula for the integral of `sin x ^ n` for any natural `n ≥ 2`. -/ lemma integral_sin_pow : ∫ x in a..b, sin x ^ (n + 2) = (sin a ^ (n + 1) * cos a - sin b ^ (n + 1) * cos b) / (n + 2) + (n + 1) / (n + 2) * ∫ x in a..b, sin x ^ n := begin have : (n : ℝ) + 2 ≠ 0 := by exact_mod_cast succ_ne_zero n.succ, field_simp, convert eq_sub_iff_add_eq.mp (integral_sin_pow_aux n), ring, end @[simp] lemma integral_sin_sq : ∫ x in a..b, sin x ^ 2 = (sin a * cos a - sin b * cos b + b - a) / 2 := by field_simp [integral_sin_pow, add_sub_assoc] theorem integral_sin_pow_odd : ∫ x in 0..π, sin x ^ (2 * n + 1) = 2 * ∏ i in range n, (2 * i + 2) / (2 * i + 3) := begin induction n with k ih, { norm_num }, rw [prod_range_succ_comm, mul_left_comm, ← ih, mul_succ, integral_sin_pow], norm_cast, simp [-cast_add] with field_simps, end theorem integral_sin_pow_even : ∫ x in 0..π, sin x ^ (2 * n) = π * ∏ i in range n, (2 * i + 1) / (2 * i + 2) := begin induction n with k ih, { simp }, rw [prod_range_succ_comm, mul_left_comm, ← ih, mul_succ, integral_sin_pow], norm_cast, simp [-cast_add] with field_simps, end lemma integral_sin_pow_pos : 0 < ∫ x in 0..π, sin x ^ n := begin rcases even_or_odd' n with ⟨k, (rfl | rfl)⟩; simp only [integral_sin_pow_even, integral_sin_pow_odd]; refine mul_pos (by norm_num [pi_pos]) (prod_pos (λ n hn, div_pos _ _)); norm_cast; linarith, end lemma integral_sin_pow_succ_le : ∫ x in 0..π, sin x ^ (n + 1) ≤ ∫ x in 0..π, sin x ^ n := let H := λ x h, pow_le_pow_of_le_one (sin_nonneg_of_mem_Icc h) (sin_le_one x) (n.le_add_right 1) in by refine integral_mono_on pi_pos.le _ _ H; exact (continuous_sin.pow _).interval_integrable 0 π lemma integral_sin_pow_antitone : antitone (λ n : ℕ, ∫ x in 0..π, sin x ^ n) := antitone_nat_of_succ_le integral_sin_pow_succ_le /-! ### Integral of `cos x ^ n` -/ lemma integral_cos_pow_aux : ∫ x in a..b, cos x ^ (n + 2) = cos b ^ (n + 1) * sin b - cos a ^ (n + 1) * sin a + (n + 1) * (∫ x in a..b, cos x ^ n) - (n + 1) * ∫ x in a..b, cos x ^ (n + 2) := begin let C := cos b ^ (n + 1) * sin b - cos a ^ (n + 1) * sin a, have h : ∀ α β γ : ℝ, (β * α * γ) * α = β * (α * α * γ) := λ α β γ, by ring, have hu : ∀ x ∈ _, has_deriv_at (λ y, cos y ^ (n + 1)) (-(n + 1 : ℕ) * sin x * cos x ^ n) x := λ x hx, by simpa only [mul_right_comm, neg_mul, mul_neg] using (has_deriv_at_cos x).pow (n+1), have hv : ∀ x ∈ [a, b], has_deriv_at sin (cos x) x := λ x hx, has_deriv_at_sin x, have H := integral_mul_deriv_eq_deriv_mul hu hv _ _, calc ∫ x in a..b, cos x ^ (n + 2) = ∫ x in a..b, cos x ^ (n + 1) * cos x : by simp only [pow_succ'] ... = C + (n + 1) * ∫ x in a..b, sin x ^ 2 * cos x ^ n : by simp [H, h, sq, -neg_add_rev] ... = C + (n + 1) * ∫ x in a..b, cos x ^ n - cos x ^ (n + 2) : by simp [sin_sq, sub_mul, ← pow_add, add_comm] ... = C + (n + 1) * (∫ x in a..b, cos x ^ n) - (n + 1) * ∫ x in a..b, cos x ^ (n + 2) : by rw [integral_sub, mul_sub, add_sub_assoc]; apply continuous.interval_integrable; continuity, all_goals { apply continuous.interval_integrable, continuity }, end /-- The reduction formula for the integral of `cos x ^ n` for any natural `n ≥ 2`. -/ lemma integral_cos_pow : ∫ x in a..b, cos x ^ (n + 2) = (cos b ^ (n + 1) * sin b - cos a ^ (n + 1) * sin a) / (n + 2) + (n + 1) / (n + 2) * ∫ x in a..b, cos x ^ n := begin have : (n : ℝ) + 2 ≠ 0 := by exact_mod_cast succ_ne_zero n.succ, field_simp, convert eq_sub_iff_add_eq.mp (integral_cos_pow_aux n), ring, end @[simp] lemma integral_cos_sq : ∫ x in a..b, cos x ^ 2 = (cos b * sin b - cos a * sin a + b - a) / 2 := by field_simp [integral_cos_pow, add_sub_assoc] /-! ### Integral of `sin x ^ m * cos x ^ n` -/ /-- Simplification of the integral of `sin x ^ m * cos x ^ n`, case `n` is odd. -/ lemma integral_sin_pow_mul_cos_pow_odd (m n : ℕ) : ∫ x in a..b, sin x ^ m * cos x ^ (2 * n + 1) = ∫ u in sin a..sin b, u ^ m * (1 - u ^ 2) ^ n := have hc : continuous (λ u : ℝ, u ^ m * (1 - u ^ 2) ^ n), by continuity, calc ∫ x in a..b, sin x ^ m * cos x ^ (2 * n + 1) = ∫ x in a..b, sin x ^ m * (1 - sin x ^ 2) ^ n * cos x : by simp only [pow_succ', ← mul_assoc, pow_mul, cos_sq'] ... = ∫ u in sin a..sin b, u ^ m * (1 - u ^ 2) ^ n : integral_comp_mul_deriv (λ x hx, has_deriv_at_sin x) continuous_on_cos hc /-- The integral of `sin x * cos x`, given in terms of sin². See `integral_sin_mul_cos₂` below for the integral given in terms of cos². -/ @[simp] lemma integral_sin_mul_cos₁ : ∫ x in a..b, sin x * cos x = (sin b ^ 2 - sin a ^ 2) / 2 := by simpa using integral_sin_pow_mul_cos_pow_odd 1 0 @[simp] lemma integral_sin_sq_mul_cos : ∫ x in a..b, sin x ^ 2 * cos x = (sin b ^ 3 - sin a ^ 3) / 3 := by simpa using integral_sin_pow_mul_cos_pow_odd 2 0 @[simp] lemma integral_cos_pow_three : ∫ x in a..b, cos x ^ 3 = sin b - sin a - (sin b ^ 3 - sin a ^ 3) / 3 := by simpa using integral_sin_pow_mul_cos_pow_odd 0 1 /-- Simplification of the integral of `sin x ^ m * cos x ^ n`, case `m` is odd. -/ lemma integral_sin_pow_odd_mul_cos_pow (m n : ℕ) : ∫ x in a..b, sin x ^ (2 * m + 1) * cos x ^ n = ∫ u in cos b..cos a, u ^ n * (1 - u ^ 2) ^ m := have hc : continuous (λ u : ℝ, u ^ n * (1 - u ^ 2) ^ m), by continuity, calc ∫ x in a..b, sin x ^ (2 * m + 1) * cos x ^ n = -∫ x in b..a, sin x ^ (2 * m + 1) * cos x ^ n : by rw integral_symm ... = ∫ x in b..a, (1 - cos x ^ 2) ^ m * -sin x * cos x ^ n : by simp [pow_succ', pow_mul, sin_sq] ... = ∫ x in b..a, cos x ^ n * (1 - cos x ^ 2) ^ m * -sin x : by { congr, ext, ring } ... = ∫ u in cos b..cos a, u ^ n * (1 - u ^ 2) ^ m : integral_comp_mul_deriv (λ x hx, has_deriv_at_cos x) continuous_on_sin.neg hc /-- The integral of `sin x * cos x`, given in terms of cos². See `integral_sin_mul_cos₁` above for the integral given in terms of sin². -/ lemma integral_sin_mul_cos₂ : ∫ x in a..b, sin x * cos x = (cos a ^ 2 - cos b ^ 2) / 2 := by simpa using integral_sin_pow_odd_mul_cos_pow 0 1 @[simp] lemma integral_sin_mul_cos_sq : ∫ x in a..b, sin x * cos x ^ 2 = (cos a ^ 3 - cos b ^ 3) / 3 := by simpa using integral_sin_pow_odd_mul_cos_pow 0 2 @[simp] lemma integral_sin_pow_three : ∫ x in a..b, sin x ^ 3 = cos a - cos b - (cos a ^ 3 - cos b ^ 3) / 3 := by simpa using integral_sin_pow_odd_mul_cos_pow 1 0 /-- Simplification of the integral of `sin x ^ m * cos x ^ n`, case `m` and `n` are both even. -/ lemma integral_sin_pow_even_mul_cos_pow_even (m n : ℕ) : ∫ x in a..b, sin x ^ (2 * m) * cos x ^ (2 * n) = ∫ x in a..b, ((1 - cos (2 * x)) / 2) ^ m * ((1 + cos (2 * x)) / 2) ^ n := by field_simp [pow_mul, sin_sq, cos_sq, ← sub_sub, (by ring : (2:ℝ) - 1 = 1)] @[simp] lemma integral_sin_sq_mul_cos_sq : ∫ x in a..b, sin x ^ 2 * cos x ^ 2 = (b - a) / 8 - (sin (4 * b) - sin (4 * a)) / 32 := begin convert integral_sin_pow_even_mul_cos_pow_even 1 1 using 1, have h1 : ∀ c : ℝ, (1 - c) / 2 * ((1 + c) / 2) = (1 - c ^ 2) / 4 := λ c, by ring, have h2 : continuous (λ x, cos (2 * x) ^ 2) := by continuity, have h3 : ∀ x, cos x * sin x = sin (2 * x) / 2, { intro, rw sin_two_mul, ring }, have h4 : ∀ d : ℝ, 2 * (2 * d) = 4 * d := λ d, by ring, simp [h1, h2.interval_integrable, integral_comp_mul_left (λ x, cos x ^ 2), h3, h4], ring, end
e257318d13a56e93c6bf7a22802be2a218b82278
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/id.lean
930393a6d63460ad334c5ebd14bfaaed3848b511
[ "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
157
lean
#check id id set_option pp.universes true #check id id #check id Prop #check id nat #check @id.{0} #check @id.{1} #check id nat.zero #check @eq #check eq eq
808c1a6f43456bcd21bc7b35a5d38ef3c956abc1
87fd6b43d22688237c02b87c30d2a524f53bab24
/src/game/sets/sets_level01.lean
3609d38ede0c5c35dbed2092f0de1f2acf1bf309
[ "Apache-2.0" ]
permissive
grthomson/real-number-game
66142fedf0987db90f66daed52f9c8b42b70f909
8ddc15fdddc241c246653f7bb341df36e4e880a8
refs/heads/master
1,668,059,330,605
1,592,873,454,000
1,592,873,454,000
262,025,764
0
0
null
1,588,849,107,000
1,588,849,106,000
null
UTF-8
Lean
false
false
1,928
lean
import data.set.basic -- hide import kb_defs -- hide namespace xena -- hide open_locale classical -- hide variable X : Type -- we will think of X as a set here /- # Chapter 1 : Sets ## Level 1 : Introduction to sets. This chapter assumes you are familiar with the following tactics: `rw`, `intro`, `apply`, `exact`, `cases`, `split`, `left`, `right` and `exfalso`.<br> (TODO (kmb) : check this list is exhaustive) If you are not, try playing Function World and Proposition World of the Natural Number Game. ## Sets in Lean In this world, there will be an ambient "big" set `X` (actually `X` will be a type), and we will consider subsets of `X`. The type of subsets of `X` is called `set X`. So if you see `A : set X` it means that `A` is a subset of `X`. ## subsets (⊆) and `subset_iff` If `A B : set X` (i.e. `A` and `B` are subsets of `X`), then `A ⊆ B` is a Proposition, i.e. a true/false statement. Lean knows the following fact: ``` subset_iff : A ⊆ B ↔ ∀ x : X, x ∈ A → x ∈ B ``` Let's see if you can prove `A ⊆ A` by rewriting `subset_iff`. -/ /- Axiom : subset_iff : A ⊆ B ↔ ∀ x : X, x ∈ A → x ∈ B -/ /- Hint : Hint To make progress with a goal of form `∀ a : X, ...`, introduce a term of type `X` by using a familiar tactic. In this example, using `intro a,` will introduce an arbitrary term of type X. Note that this is the tactic we use to assume a hypothesis (when proving an implication), or to choose an arbitrary element of some domain (when defining a function). Use the same tactic to introduce an appropriately named hypothesis for an implication, and close the goal with the `exact` tactic. -/ /- If you get stuck, you can click on the hints for more details! -/ /- Lemma If $A$ is a set of elements of type X, then $A \subseteq A$. -/ lemma subset.refl (A : set X) : A ⊆ A := begin rw subset_iff, intros x h, exact h end end xena --hide
8d77c7dac5f9399d02740a3f613c638a30c51b18
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/set_theory/pgame.lean
95659680937f891711412f00711493cf9e731974
[ "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
39,283
lean
/- Copyright (c) 2019 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Mario Carneiro, Isabel Longbottom, Scott Morrison -/ import logic.embedding import data.nat.cast /-! # Combinatorial (pre-)games. The basic theory of combinatorial games, following Conway's book `On Numbers and Games`. We construct "pregames", define an ordering and arithmetic operations on them, then show that the operations descend to "games", defined via the equivalence relation `p ≈ q ↔ p ≤ q ∧ q ≤ p`. The surreal numbers will be built as a quotient of a subtype of pregames. A pregame (`pgame` below) is axiomatised via an inductive type, whose sole constructor takes two types (thought of as indexing the the possible moves for the players Left and Right), and a pair of functions out of these types to `pgame` (thought of as describing the resulting game after making a move). Combinatorial games themselves, as a quotient of pregames, are constructed in `game.lean`. ## Conway induction By construction, the induction principle for pregames is exactly "Conway induction". That is, to prove some predicate `pgame → Prop` holds for all pregames, it suffices to prove that for every pregame `g`, if the predicate holds for every game resulting from making a move, then it also holds for `g`. While it is often convenient to work "by induction" on pregames, in some situations this becomes awkward, so we also define accessor functions `left_moves`, `right_moves`, `move_left` and `move_right`. There is a relation `subsequent p q`, saying that `p` can be reached by playing some non-empty sequence of moves starting from `q`, an instance `well_founded subsequent`, and a local tactic `pgame_wf_tac` which is helpful for discharging proof obligations in inductive proofs relying on this relation. ## Order properties Pregames have both a `≤` and a `<` relation, which are related in quite a subtle way. In particular, it is worth noting that in Lean's (perhaps unfortunate?) definition of a `preorder`, we have `lt_iff_le_not_le : ∀ a b : α, a < b ↔ (a ≤ b ∧ ¬ b ≤ a)`, but this is _not_ satisfied by the usual `≤` and `<` relations on pregames. (It is satisfied once we restrict to the surreal numbers.) In particular, `<` is not transitive; there is an example below showing `0 < star ∧ star < 0`. We do have ``` theorem not_le {x y : pgame} : ¬ x ≤ y ↔ y < x := ... theorem not_lt {x y : pgame} : ¬ x < y ↔ y ≤ x := ... ``` The statement `0 ≤ x` means that Left has a good response to any move by Right; in particular, the theorem `zero_le` below states ``` 0 ≤ x ↔ ∀ j : x.right_moves, ∃ i : (x.move_right j).left_moves, 0 ≤ (x.move_right j).move_left i ``` On the other hand the statement `0 < x` means that Left has a good move right now; in particular the theorem `zero_lt` below states ``` 0 < x ↔ ∃ i : left_moves x, ∀ j : right_moves (x.move_left i), 0 < (x.move_left i).move_right j ``` The theorems `le_def`, `lt_def`, give a recursive characterisation of each relation, in terms of themselves two moves later. The theorems `le_def_lt` and `lt_def_lt` give recursive characterisations of each relation in terms of the other relation one move later. We define an equivalence relation `equiv p q ↔ p ≤ q ∧ q ≤ p`. Later, games will be defined as the quotient by this relation. ## Algebraic structures We next turn to defining the operations necessary to make games into a commutative additive group. Addition is defined for $x = \{xL | xR\}$ and $y = \{yL | yR\}$ by $x + y = \{xL + y, x + yL | xR + y, x + yR\}$. Negation is defined by $\{xL | xR\} = \{-xR | -xL\}$. The order structures interact in the expected way with addition, so we have ``` theorem le_iff_sub_nonneg {x y : pgame} : x ≤ y ↔ 0 ≤ y - x := sorry theorem lt_iff_sub_pos {x y : pgame} : x < y ↔ 0 < y - x := sorry ``` We show that these operations respect the equivalence relation, and hence descend to games. At the level of games, these operations satisfy all the laws of a commutative group. To prove the necessary equivalence relations at the level of pregames, we introduce the notion of a `relabelling` of a game, and show, for example, that there is a relabelling between `x + (y + z)` and `(x + y) + z`. ## Future work * The theory of dominated and reversible positions, and unique normal form for short games. * Analysis of basic domineering positions. * Impartial games, nim, and the Sprague-Grundy theorem. * Hex. * Temperature. * The development of surreal numbers, based on this development of combinatorial games, is still quite incomplete. ## References The material here is all drawn from * [Conway, *On numbers and games*][conway2001] An interested reader may like to formalise some of the material from * [Andreas Blass, *A game semantics for linear logic*][MR1167694] * [André Joyal, *Remarques sur la théorie des jeux à deux personnes*][joyal1997] -/ universes u /-- The type of pre-games, before we have quotiented by extensionality. In ZFC, a combinatorial game is constructed from two sets of combinatorial games that have been constructed at an earlier stage. To do this in type theory, we say that a pre-game is built inductively from two families of pre-games indexed over any type in Type u. The resulting type `pgame.{u}` lives in `Type (u+1)`, reflecting that it is a proper class in ZFC. -/ inductive pgame : Type (u+1) | mk : ∀ α β : Type u, (α → pgame) → (β → pgame) → pgame namespace pgame /-- Construct a pre-game from list of pre-games describing the available moves for Left and Right. -/ -- TODO provide some API describing the interaction with -- `left_moves`, `right_moves`, `move_left` and `move_right` below. -- TODO define this at the level of games, as well, and perhaps also for finsets of games. def of_lists (L R : list pgame.{0}) : pgame.{0} := pgame.mk (fin L.length) (fin R.length) (λ i, L.nth_le i.val i.is_lt) (λ j, R.nth_le j.val j.is_lt) /-- The indexing type for allowable moves by Left. -/ def left_moves : pgame → Type u | (mk l _ _ _) := l /-- The indexing type for allowable moves by Right. -/ def right_moves : pgame → Type u | (mk _ r _ _) := r /-- The new game after Left makes an allowed move. -/ def move_left : Π (g : pgame), left_moves g → pgame | (mk l _ L _) i := L i /-- The new game after Right makes an allowed move. -/ def move_right : Π (g : pgame), right_moves g → pgame | (mk _ r _ R) j := R j @[simp] lemma left_moves_mk {xl xr xL xR} : (⟨xl, xr, xL, xR⟩ : pgame).left_moves = xl := rfl @[simp] lemma move_left_mk {xl xr xL xR i} : (⟨xl, xr, xL, xR⟩ : pgame).move_left i = xL i := rfl @[simp] lemma right_moves_mk {xl xr xL xR} : (⟨xl, xr, xL, xR⟩ : pgame).right_moves = xr := rfl @[simp] lemma move_right_mk {xl xr xL xR j} : (⟨xl, xr, xL, xR⟩ : pgame).move_right j = xR j := rfl /-- `subsequent p q` says that `p` can be obtained by playing some nonempty sequence of moves from `q`. -/ inductive subsequent : pgame → pgame → Prop | left : Π (x : pgame) (i : x.left_moves), subsequent (x.move_left i) x | right : Π (x : pgame) (j : x.right_moves), subsequent (x.move_right j) x | trans : Π (x y z : pgame), subsequent x y → subsequent y z → subsequent x z theorem wf_subsequent : well_founded subsequent := ⟨λ x, begin induction x with l r L R IHl IHr, refine ⟨_, λ y h, _⟩, generalize_hyp e : mk l r L R = x at h, induction h with _ i _ j a b _ h1 h2 IH1 IH2; subst e, { apply IHl }, { apply IHr }, { exact acc.inv (IH2 rfl) h1 } end⟩ instance : has_well_founded pgame := { r := subsequent, wf := wf_subsequent } /-- A move by Left produces a subsequent game. (For use in pgame_wf_tac.) -/ lemma subsequent.left_move {xl xr} {xL : xl → pgame} {xR : xr → pgame} {i : xl} : subsequent (xL i) (mk xl xr xL xR) := subsequent.left (mk xl xr xL xR) i /-- A move by Right produces a subsequent game. (For use in pgame_wf_tac.) -/ lemma subsequent.right_move {xl xr} {xL : xl → pgame} {xR : xr → pgame} {j : xr} : subsequent (xR j) (mk xl xr xL xR) := subsequent.right (mk xl xr xL xR) j /-- A local tactic for proving well-foundedness of recursive definitions involving pregames. -/ meta def pgame_wf_tac := `[solve_by_elim [psigma.lex.left, psigma.lex.right, subsequent.left_move, subsequent.right_move, subsequent.left, subsequent.right, subsequent.trans] { max_depth := 6 }] /-- The pre-game `zero` is defined by `0 = { | }`. -/ instance : has_zero pgame := ⟨⟨pempty, pempty, pempty.elim, pempty.elim⟩⟩ @[simp] lemma zero_left_moves : (0 : pgame).left_moves = pempty := rfl @[simp] lemma zero_right_moves : (0 : pgame).right_moves = pempty := rfl instance : inhabited pgame := ⟨0⟩ /-- The pre-game `one` is defined by `1 = { 0 | }`. -/ instance : has_one pgame := ⟨⟨punit, pempty, λ _, 0, pempty.elim⟩⟩ @[simp] lemma one_left_moves : (1 : pgame).left_moves = punit := rfl @[simp] lemma one_move_left : (1 : pgame).move_left punit.star = 0 := rfl @[simp] lemma one_right_moves : (1 : pgame).right_moves = pempty := rfl /-- Define simultaneously by mutual induction the `<=` and `<` relation on pre-games. The ZFC definition says that `x = {xL | xR}` is less or equal to `y = {yL | yR}` if `∀ x₁ ∈ xL, x₁ < y` and `∀ y₂ ∈ yR, x < y₂`, where `x < y` is the same as `¬ y <= x`. This is a tricky induction because it only decreases one side at a time, and it also swaps the arguments in the definition of `<`. The solution is to define `x < y` and `x <= y` simultaneously. -/ def le_lt : Π (x y : pgame), Prop × Prop | (mk xl xr xL xR) (mk yl yr yL yR) := -- the orderings of the clauses here are carefully chosen so that -- and.left/or.inl refer to moves by Left, and -- and.right/or.inr refer to moves by Right. ((∀ i : xl, (le_lt (xL i) ⟨yl, yr, yL, yR⟩).2) ∧ (∀ j : yr, (le_lt ⟨xl, xr, xL, xR⟩ (yR j)).2), (∃ i : yl, (le_lt ⟨xl, xr, xL, xR⟩ (yL i)).1) ∨ (∃ j : xr, (le_lt (xR j) ⟨yl, yr, yL, yR⟩).1)) using_well_founded { dec_tac := pgame_wf_tac } instance : has_le pgame := ⟨λ x y, (le_lt x y).1⟩ instance : has_lt pgame := ⟨λ x y, (le_lt x y).2⟩ /-- Definition of `x ≤ y` on pre-games built using the constructor. -/ @[simp] theorem mk_le_mk {xl xr xL xR yl yr yL yR} : (⟨xl, xr, xL, xR⟩ : pgame) ≤ ⟨yl, yr, yL, yR⟩ ↔ (∀ i, xL i < ⟨yl, yr, yL, yR⟩) ∧ (∀ j, (⟨xl, xr, xL, xR⟩ : pgame) < yR j) := iff.rfl /-- Definition of `x ≤ y` on pre-games, in terms of `<` -/ theorem le_def_lt {x y : pgame} : x ≤ y ↔ (∀ i : x.left_moves, x.move_left i < y) ∧ (∀ j : y.right_moves, x < y.move_right j) := by { cases x, cases y, refl } /-- Definition of `x < y` on pre-games built using the constructor. -/ @[simp] theorem mk_lt_mk {xl xr xL xR yl yr yL yR} : (⟨xl, xr, xL, xR⟩ : pgame) < ⟨yl, yr, yL, yR⟩ ↔ (∃ i, (⟨xl, xr, xL, xR⟩ : pgame) ≤ yL i) ∨ (∃ j, xR j ≤ ⟨yl, yr, yL, yR⟩) := iff.rfl /-- Definition of `x < y` on pre-games, in terms of `≤` -/ theorem lt_def_le {x y : pgame} : x < y ↔ (∃ i : y.left_moves, x ≤ y.move_left i) ∨ (∃ j : x.right_moves, x.move_right j ≤ y) := by { cases x, cases y, refl } /-- The definition of `x ≤ y` on pre-games, in terms of `≤` two moves later. -/ theorem le_def {x y : pgame} : x ≤ y ↔ (∀ i : x.left_moves, (∃ i' : y.left_moves, x.move_left i ≤ y.move_left i') ∨ (∃ j : (x.move_left i).right_moves, (x.move_left i).move_right j ≤ y)) ∧ (∀ j : y.right_moves, (∃ i : (y.move_right j).left_moves, x ≤ (y.move_right j).move_left i) ∨ (∃ j' : x.right_moves, x.move_right j' ≤ y.move_right j)) := begin rw [le_def_lt], conv { to_lhs, simp only [lt_def_le] }, end /-- The definition of `x < y` on pre-games, in terms of `<` two moves later. -/ theorem lt_def {x y : pgame} : x < y ↔ (∃ i : y.left_moves, (∀ i' : x.left_moves, x.move_left i' < y.move_left i) ∧ (∀ j : (y.move_left i).right_moves, x < (y.move_left i).move_right j)) ∨ (∃ j : x.right_moves, (∀ i : (x.move_right j).left_moves, (x.move_right j).move_left i < y) ∧ (∀ j' : y.right_moves, x.move_right j < y.move_right j')) := begin rw [lt_def_le], conv { to_lhs, simp only [le_def_lt] }, end /-- The definition of `x ≤ 0` on pre-games, in terms of `≤ 0` two moves later. -/ theorem le_zero {x : pgame} : x ≤ 0 ↔ ∀ i : x.left_moves, ∃ j : (x.move_left i).right_moves, (x.move_left i).move_right j ≤ 0 := begin rw le_def, dsimp, simp [forall_pempty, exists_pempty] end /-- The definition of `0 ≤ x` on pre-games, in terms of `0 ≤` two moves later. -/ theorem zero_le {x : pgame} : 0 ≤ x ↔ ∀ j : x.right_moves, ∃ i : (x.move_right j).left_moves, 0 ≤ (x.move_right j).move_left i := begin rw le_def, dsimp, simp [forall_pempty, exists_pempty] end /-- The definition of `x < 0` on pre-games, in terms of `< 0` two moves later. -/ theorem lt_zero {x : pgame} : x < 0 ↔ ∃ j : x.right_moves, ∀ i : (x.move_right j).left_moves, (x.move_right j).move_left i < 0 := begin rw lt_def, dsimp, simp [forall_pempty, exists_pempty] end /-- The definition of `0 < x` on pre-games, in terms of `< x` two moves later. -/ theorem zero_lt {x : pgame} : 0 < x ↔ ∃ i : x.left_moves, ∀ j : (x.move_left i).right_moves, 0 < (x.move_left i).move_right j := begin rw lt_def, dsimp, simp [forall_pempty, exists_pempty] end /-- Given a right-player-wins game, provide a response to any move by left. -/ noncomputable def right_response {x : pgame} (h : x ≤ 0) (i : x.left_moves) : (x.move_left i).right_moves := classical.some $ (le_zero.1 h) i /-- Show that the response for right provided by `right_response` preserves the right-player-wins condition. -/ lemma right_response_spec {x : pgame} (h : x ≤ 0) (i : x.left_moves) : (x.move_left i).move_right (right_response h i) ≤ 0 := classical.some_spec $ (le_zero.1 h) i /-- Given a left-player-wins game, provide a response to any move by right. -/ noncomputable def left_response {x : pgame} (h : 0 ≤ x) (j : x.right_moves) : (x.move_right j).left_moves := classical.some $ (zero_le.1 h) j /-- Show that the response for left provided by `left_response` preserves the left-player-wins condition. -/ lemma left_response_spec {x : pgame} (h : 0 ≤ x) (j : x.right_moves) : 0 ≤ (x.move_right j).move_left (left_response h j) := classical.some_spec $ (zero_le.1 h) j theorem lt_of_le_mk {xl xr xL xR y i} : (⟨xl, xr, xL, xR⟩ : pgame) ≤ y → xL i < y := by cases y; exact λ h, h.1 i theorem lt_of_mk_le {x : pgame} {yl yr yL yR i} : x ≤ ⟨yl, yr, yL, yR⟩ → x < yR i := by cases x; exact λ h, h.2 i theorem mk_lt_of_le {xl xr xL xR y i} : (by exact xR i ≤ y) → (⟨xl, xr, xL, xR⟩ : pgame) < y := by cases y; exact λ h, or.inr ⟨i, h⟩ theorem lt_mk_of_le {x : pgame} {yl yr yL yR i} : (by exact x ≤ yL i) → x < ⟨yl, yr, yL, yR⟩ := by cases x; exact λ h, or.inl ⟨i, h⟩ theorem not_le_lt {x y : pgame} : (¬ x ≤ y ↔ y < x) ∧ (¬ x < y ↔ y ≤ x) := begin induction x with xl xr xL xR IHxl IHxr generalizing y, induction y with yl yr yL yR IHyl IHyr, classical, simp only [mk_le_mk, mk_lt_mk, not_and_distrib, not_or_distrib, not_forall, not_exists, and_comm, or_comm, IHxl, IHxr, IHyl, IHyr, iff_self, and_self] end theorem not_le {x y : pgame} : ¬ x ≤ y ↔ y < x := not_le_lt.1 theorem not_lt {x y : pgame} : ¬ x < y ↔ y ≤ x := not_le_lt.2 @[refl] theorem le_refl : ∀ x : pgame, x ≤ x | ⟨l, r, L, R⟩ := ⟨λ i, lt_mk_of_le (le_refl _), λ i, mk_lt_of_le (le_refl _)⟩ theorem lt_irrefl (x : pgame) : ¬ x < x := not_lt.2 (le_refl _) theorem ne_of_lt : ∀ {x y : pgame}, x < y → x ≠ y | x _ h rfl := lt_irrefl x h theorem le_trans_aux {xl xr} {xL : xl → pgame} {xR : xr → pgame} {yl yr} {yL : yl → pgame} {yR : yr → pgame} {zl zr} {zL : zl → pgame} {zR : zr → pgame} (h₁ : ∀ i, mk yl yr yL yR ≤ mk zl zr zL zR → mk zl zr zL zR ≤ xL i → mk yl yr yL yR ≤ xL i) (h₂ : ∀ i, zR i ≤ mk xl xr xL xR → mk xl xr xL xR ≤ mk yl yr yL yR → zR i ≤ mk yl yr yL yR) : mk xl xr xL xR ≤ mk yl yr yL yR → mk yl yr yL yR ≤ mk zl zr zL zR → mk xl xr xL xR ≤ mk zl zr zL zR := λ ⟨xLy, xyR⟩ ⟨yLz, yzR⟩, ⟨ λ i, not_le.1 (λ h, not_lt.2 (h₁ _ ⟨yLz, yzR⟩ h) (xLy _)), λ i, not_le.1 (λ h, not_lt.2 (h₂ _ h ⟨xLy, xyR⟩) (yzR _))⟩ @[trans] theorem le_trans {x y z : pgame} : x ≤ y → y ≤ z → x ≤ z := suffices ∀ {x y z : pgame}, (x ≤ y → y ≤ z → x ≤ z) ∧ (y ≤ z → z ≤ x → y ≤ x) ∧ (z ≤ x → x ≤ y → z ≤ y), from this.1, begin clear x y z, intros, induction x with xl xr xL xR IHxl IHxr generalizing y z, induction y with yl yr yL yR IHyl IHyr generalizing z, induction z with zl zr zL zR IHzl IHzr, exact ⟨ le_trans_aux (λ i, (IHxl _).2.1) (λ i, (IHzr _).2.2), le_trans_aux (λ i, (IHyl _).2.2) (λ i, (IHxr _).1), le_trans_aux (λ i, (IHzl _).1) (λ i, (IHyr _).2.1)⟩, end @[trans] theorem lt_of_le_of_lt {x y z : pgame} (hxy : x ≤ y) (hyz : y < z) : x < z := begin rw ←not_le at ⊢ hyz, exact mt (λ H, le_trans H hxy) hyz end @[trans] theorem lt_of_lt_of_le {x y z : pgame} (hxy : x < y) (hyz : y ≤ z) : x < z := begin rw ←not_le at ⊢ hxy, exact mt (λ H, le_trans hyz H) hxy end /-- Define the equivalence relation on pre-games. Two pre-games `x`, `y` are equivalent if `x ≤ y` and `y ≤ x`. -/ def equiv (x y : pgame) : Prop := x ≤ y ∧ y ≤ x local infix ` ≈ ` := pgame.equiv @[refl] theorem equiv_refl (x) : x ≈ x := ⟨le_refl _, le_refl _⟩ @[symm] theorem equiv_symm {x y} : x ≈ y → y ≈ x | ⟨xy, yx⟩ := ⟨yx, xy⟩ @[trans] theorem equiv_trans {x y z} : x ≈ y → y ≈ z → x ≈ z | ⟨xy, yx⟩ ⟨yz, zy⟩ := ⟨le_trans xy yz, le_trans zy yx⟩ theorem lt_of_lt_of_equiv {x y z} (h₁ : x < y) (h₂ : y ≈ z) : x < z := lt_of_lt_of_le h₁ h₂.1 theorem le_of_le_of_equiv {x y z} (h₁ : x ≤ y) (h₂ : y ≈ z) : x ≤ z := le_trans h₁ h₂.1 theorem lt_of_equiv_of_lt {x y z} (h₁ : x ≈ y) (h₂ : y < z) : x < z := lt_of_le_of_lt h₁.1 h₂ theorem le_of_equiv_of_le {x y z} (h₁ : x ≈ y) (h₂ : y ≤ z) : x ≤ z := le_trans h₁.1 h₂ theorem le_congr {x₁ y₁ x₂ y₂} : x₁ ≈ x₂ → y₁ ≈ y₂ → (x₁ ≤ y₁ ↔ x₂ ≤ y₂) | ⟨x12, x21⟩ ⟨y12, y21⟩ := ⟨λ h, le_trans x21 (le_trans h y12), λ h, le_trans x12 (le_trans h y21)⟩ theorem lt_congr {x₁ y₁ x₂ y₂} (hx : x₁ ≈ x₂) (hy : y₁ ≈ y₂) : x₁ < y₁ ↔ x₂ < y₂ := not_le.symm.trans $ (not_congr (le_congr hy hx)).trans not_le /-- `restricted x y` says that Left always has no more moves in `x` than in `y`, and Right always has no more moves in `y` than in `x` -/ inductive restricted : pgame.{u} → pgame.{u} → Type (u+1) | mk : Π {x y : pgame} (L : x.left_moves → y.left_moves) (R : y.right_moves → x.right_moves), (∀ (i : x.left_moves), restricted (x.move_left i) (y.move_left (L i))) → (∀ (j : y.right_moves), restricted (x.move_right (R j)) (y.move_right j)) → restricted x y /-- The identity restriction. -/ @[refl] def restricted.refl : Π (x : pgame), restricted x x | (mk xl xr xL xR) := restricted.mk id id (λ i, restricted.refl _) (λ j, restricted.refl _) using_well_founded { dec_tac := pgame_wf_tac } -- TODO trans for restricted theorem le_of_restricted : Π {x y : pgame} (r : restricted x y), x ≤ y | (mk xl xr xL xR) (mk yl yr yL yR) (restricted.mk L_embedding R_embedding L_restriction R_restriction) := begin rw le_def, exact ⟨λ i, or.inl ⟨L_embedding i, le_of_restricted (L_restriction i)⟩, λ i, or.inr ⟨R_embedding i, le_of_restricted (R_restriction i)⟩⟩ end /-- `relabelling x y` says that `x` and `y` are really the same game, just dressed up differently. Specifically, there is a bijection between the moves for Left in `x` and in `y`, and similarly for Right, and under these bijections we inductively have `relabelling`s for the consequent games. -/ inductive relabelling : pgame.{u} → pgame.{u} → Type (u+1) | mk : Π {x y : pgame} (L : x.left_moves ≃ y.left_moves) (R : x.right_moves ≃ y.right_moves), (∀ (i : x.left_moves), relabelling (x.move_left i) (y.move_left (L i))) → (∀ (j : y.right_moves), relabelling (x.move_right (R.symm j)) (y.move_right j)) → relabelling x y /-- If `x` is a relabelling of `y`, then Left and Right have the same moves in either game, so `x` is a restriction of `y`. -/ def restricted_of_relabelling : Π {x y : pgame} (r : relabelling x y), restricted x y | (mk xl xr xL xR) (mk yl yr yL yR) (relabelling.mk L_equiv R_equiv L_relabelling R_relabelling) := restricted.mk L_equiv.to_embedding R_equiv.symm.to_embedding (λ i, restricted_of_relabelling (L_relabelling i)) (λ j, restricted_of_relabelling (R_relabelling j)) -- It's not the case that `restricted x y → restricted y x → relabelling x y`, -- but if we insisted that the maps in a restriction were injective, then one -- could use Schröder-Bernstein for do this. /-- The identity relabelling. -/ @[refl] def relabelling.refl : Π (x : pgame), relabelling x x | (mk xl xr xL xR) := relabelling.mk (equiv.refl _) (equiv.refl _) (λ i, relabelling.refl _) (λ j, relabelling.refl _) using_well_founded { dec_tac := pgame_wf_tac } /-- Reverse a relabelling. -/ @[symm] def relabelling.symm : Π {x y : pgame}, relabelling x y → relabelling y x | (mk xl xr xL xR) (mk yl yr yL yR) (relabelling.mk L_equiv R_equiv L_relabelling R_relabelling) := begin refine relabelling.mk L_equiv.symm R_equiv.symm _ _, { intro i, simpa using (L_relabelling (L_equiv.symm i)).symm }, { intro j, simpa using (R_relabelling (R_equiv j)).symm } end /-- Transitivity of relabelling -/ @[trans] def relabelling.trans : Π {x y z : pgame}, relabelling x y → relabelling y z → relabelling x z | (mk xl xr xL xR) (mk yl yr yL yR) (mk zl zr zL zR) (relabelling.mk L_equiv₁ R_equiv₁ L_relabelling₁ R_relabelling₁) (relabelling.mk L_equiv₂ R_equiv₂ L_relabelling₂ R_relabelling₂) := begin refine relabelling.mk (L_equiv₁.trans L_equiv₂) (R_equiv₁.trans R_equiv₂) _ _, { intro i, simpa using (L_relabelling₁ _).trans (L_relabelling₂ _) }, { intro j, simpa using (R_relabelling₁ _).trans (R_relabelling₂ _) }, end theorem le_of_relabelling {x y : pgame} (r : relabelling x y) : x ≤ y := le_of_restricted (restricted_of_relabelling r) /-- A relabelling lets us prove equivalence of games. -/ theorem equiv_of_relabelling {x y : pgame} (r : relabelling x y) : x ≈ y := ⟨le_of_relabelling r, le_of_relabelling r.symm⟩ instance {x y : pgame} : has_coe (relabelling x y) (x ≈ y) := ⟨equiv_of_relabelling⟩ /-- Replace the types indexing the next moves for Left and Right by equivalent types. -/ def relabel {x : pgame} {xl' xr'} (el : x.left_moves ≃ xl') (er : x.right_moves ≃ xr') := pgame.mk xl' xr' (λ i, x.move_left (el.symm i)) (λ j, x.move_right (er.symm j)) @[simp] lemma relabel_move_left' {x : pgame} {xl' xr'} (el : x.left_moves ≃ xl') (er : x.right_moves ≃ xr') (i : xl') : move_left (relabel el er) i = x.move_left (el.symm i) := rfl @[simp] lemma relabel_move_left {x : pgame} {xl' xr'} (el : x.left_moves ≃ xl') (er : x.right_moves ≃ xr') (i : x.left_moves) : move_left (relabel el er) (el i) = x.move_left i := by simp @[simp] lemma relabel_move_right' {x : pgame} {xl' xr'} (el : x.left_moves ≃ xl') (er : x.right_moves ≃ xr') (j : xr') : move_right (relabel el er) j = x.move_right (er.symm j) := rfl @[simp] lemma relabel_move_right {x : pgame} {xl' xr'} (el : x.left_moves ≃ xl') (er : x.right_moves ≃ xr') (j : x.right_moves) : move_right (relabel el er) (er j) = x.move_right j := by simp /-- The game obtained by relabelling the next moves is a relabelling of the original game. -/ def relabel_relabelling {x : pgame} {xl' xr'} (el : x.left_moves ≃ xl') (er : x.right_moves ≃ xr') : relabelling x (relabel el er) := relabelling.mk el er (λ i, by simp) (λ j, by simp) /-- The negation of `{L | R}` is `{-R | -L}`. -/ def neg : pgame → pgame | ⟨l, r, L, R⟩ := ⟨r, l, λ i, neg (R i), λ i, neg (L i)⟩ instance : has_neg pgame := ⟨neg⟩ @[simp] lemma neg_def {xl xr xL xR} : -(mk xl xr xL xR) = mk xr xl (λ j, -(xR j)) (λ i, -(xL i)) := rfl @[simp] theorem neg_neg : Π {x : pgame}, -(-x) = x | (mk xl xr xL xR) := begin dsimp [has_neg.neg, neg], congr; funext i; apply neg_neg end @[simp] theorem neg_zero : -(0 : pgame) = 0 := begin dsimp [has_zero.zero, has_neg.neg, neg], congr; funext i; cases i end /-- An explicit equivalence between the moves for Left in `-x` and the moves for Right in `x`. -/ -- This equivalence is useful to avoid having to use `cases` unnecessarily. def left_moves_neg (x : pgame) : (-x).left_moves ≃ x.right_moves := by { cases x, refl } /-- An explicit equivalence between the moves for Right in `-x` and the moves for Left in `x`. -/ def right_moves_neg (x : pgame) : (-x).right_moves ≃ x.left_moves := by { cases x, refl } @[simp] lemma move_right_left_moves_neg {x : pgame} (i : left_moves (-x)) : move_right x ((left_moves_neg x) i) = -(move_left (-x) i) := begin induction x, exact neg_neg.symm end @[simp] lemma move_left_right_moves_neg_symm {x : pgame} (i : right_moves x) : move_left (-x) ((left_moves_neg x).symm i) = -(move_right x i) := by { cases x, refl } @[simp] lemma move_left_right_moves_neg {x : pgame} (i : right_moves (-x)) : move_left x ((right_moves_neg x) i) = -(move_right (-x) i) := begin induction x, exact neg_neg.symm end @[simp] lemma move_right_right_moves_neg_symm {x : pgame} (i : left_moves x) : move_right (-x) ((right_moves_neg x).symm i) = -(move_left x i) := by { cases x, refl } theorem le_iff_neg_ge : Π {x y : pgame}, x ≤ y ↔ -y ≤ -x | (mk xl xr xL xR) (mk yl yr yL yR) := begin rw [le_def], rw [le_def], dsimp [neg], split, { intro h, split, { intro i, have t := h.right i, cases t, { right, cases t, use (@right_moves_neg (yR i)).symm t_w, convert le_iff_neg_ge.1 t_h, simp }, { left, cases t, use t_w, exact le_iff_neg_ge.1 t_h, } }, { intro j, have t := h.left j, cases t, { right, cases t, use t_w, exact le_iff_neg_ge.1 t_h, }, { left, cases t, use (@left_moves_neg (xL j)).symm t_w, convert le_iff_neg_ge.1 t_h, simp, } } }, { intro h, split, { intro i, have t := h.right i, cases t, { right, cases t, use (@left_moves_neg (xL i)) t_w, convert le_iff_neg_ge.2 _, convert t_h, simp, }, { left, cases t, use t_w, exact le_iff_neg_ge.2 t_h, } }, { intro j, have t := h.left j, cases t, { right, cases t, use t_w, exact le_iff_neg_ge.2 t_h, }, { left, cases t, use (@right_moves_neg (yR j)) t_w, convert le_iff_neg_ge.2 _, convert t_h, simp } } }, end using_well_founded { dec_tac := pgame_wf_tac } theorem neg_congr {x y : pgame} (h : x ≈ y) : -x ≈ -y := ⟨le_iff_neg_ge.1 h.2, le_iff_neg_ge.1 h.1⟩ theorem lt_iff_neg_gt : Π {x y : pgame}, x < y ↔ -y < -x := begin classical, intros, rw [←not_le, ←not_le, not_iff_not], apply le_iff_neg_ge end theorem zero_le_iff_neg_le_zero {x : pgame} : 0 ≤ x ↔ -x ≤ 0 := begin convert le_iff_neg_ge, rw neg_zero end theorem le_zero_iff_zero_le_neg {x : pgame} : x ≤ 0 ↔ 0 ≤ -x := begin convert le_iff_neg_ge, rw neg_zero end /-- The sum of `x = {xL | xR}` and `y = {yL | yR}` is `{xL + y, x + yL | xR + y, x + yR}`. -/ def add (x y : pgame) : pgame := begin induction x with xl xr xL xR IHxl IHxr generalizing y, induction y with yl yr yL yR IHyl IHyr, have y := mk yl yr yL yR, refine ⟨xl ⊕ yl, xr ⊕ yr, sum.rec _ _, sum.rec _ _⟩, { exact λ i, IHxl i y }, { exact λ i, IHyl i }, { exact λ i, IHxr i y }, { exact λ i, IHyr i } end instance : has_add pgame := ⟨add⟩ /-- `x + 0` has exactly the same moves as `x`. -/ def add_zero_relabelling : Π (x : pgame.{u}), relabelling (x + 0) x | (mk xl xr xL xR) := begin refine ⟨equiv.sum_pempty xl, equiv.sum_pempty xr, _, _⟩, { rintro (⟨i⟩|⟨⟨⟩⟩), apply add_zero_relabelling, }, { rintro j, apply add_zero_relabelling, } end /-- `x + 0` is equivalent to `x`. -/ lemma add_zero_equiv (x : pgame.{u}) : x + 0 ≈ x := equiv_of_relabelling (add_zero_relabelling x) /-- `0 + x` has exactly the same moves as `x`. -/ def zero_add_relabelling : Π (x : pgame.{u}), relabelling (0 + x) x | (mk xl xr xL xR) := begin refine ⟨equiv.pempty_sum xl, equiv.pempty_sum xr, _, _⟩, { rintro (⟨⟨⟩⟩|⟨i⟩), apply zero_add_relabelling, }, { rintro j, apply zero_add_relabelling, } end /-- `0 + x` is equivalent to `x`. -/ lemma zero_add_equiv (x : pgame.{u}) : 0 + x ≈ x := equiv_of_relabelling (zero_add_relabelling x) /-- An explicit equivalence between the moves for Left in `x + y` and the type-theory sum of the moves for Left in `x` and in `y`. -/ def left_moves_add (x y : pgame) : (x + y).left_moves ≃ x.left_moves ⊕ y.left_moves := by { cases x, cases y, refl, } /-- An explicit equivalence between the moves for Right in `x + y` and the type-theory sum of the moves for Right in `x` and in `y`. -/ def right_moves_add (x y : pgame) : (x + y).right_moves ≃ x.right_moves ⊕ y.right_moves := by { cases x, cases y, refl, } @[simp] lemma mk_add_move_left_inl {xl xr yl yr} {xL xR yL yR} {i} : (mk xl xr xL xR + mk yl yr yL yR).move_left (sum.inl i) = (mk xl xr xL xR).move_left i + (mk yl yr yL yR) := rfl @[simp] lemma add_move_left_inl {x y : pgame} {i} : (x + y).move_left ((@left_moves_add x y).symm (sum.inl i)) = x.move_left i + y := by { cases x, cases y, refl, } @[simp] lemma mk_add_move_right_inl {xl xr yl yr} {xL xR yL yR} {i} : (mk xl xr xL xR + mk yl yr yL yR).move_right (sum.inl i) = (mk xl xr xL xR).move_right i + (mk yl yr yL yR) := rfl @[simp] lemma add_move_right_inl {x y : pgame} {i} : (x + y).move_right ((@right_moves_add x y).symm (sum.inl i)) = x.move_right i + y := by { cases x, cases y, refl, } @[simp] lemma mk_add_move_left_inr {xl xr yl yr} {xL xR yL yR} {i} : (mk xl xr xL xR + mk yl yr yL yR).move_left (sum.inr i) = (mk xl xr xL xR) + (mk yl yr yL yR).move_left i := rfl @[simp] lemma add_move_left_inr {x y : pgame} {i : y.left_moves} : (x + y).move_left ((@left_moves_add x y).symm (sum.inr i)) = x + y.move_left i := by { cases x, cases y, refl, } @[simp] lemma mk_add_move_right_inr {xl xr yl yr} {xL xR yL yR} {i} : (mk xl xr xL xR + mk yl yr yL yR).move_right (sum.inr i) = (mk xl xr xL xR) + (mk yl yr yL yR).move_right i := rfl @[simp] lemma add_move_right_inr {x y : pgame} {i} : (x + y).move_right ((@right_moves_add x y).symm (sum.inr i)) = x + y.move_right i := by { cases x, cases y, refl, } instance : has_sub pgame := ⟨λ x y, x + -y⟩ /-- `-(x+y)` has exactly the same moves as `-x + -y`. -/ def neg_add_relabelling : Π (x y : pgame), relabelling (-(x + y)) (-x + -y) | (mk xl xr xL xR) (mk yl yr yL yR) := ⟨equiv.refl _, equiv.refl _, λ j, sum.cases_on j (λ j, neg_add_relabelling (xR j) (mk yl yr yL yR)) (λ j, neg_add_relabelling (mk xl xr xL xR) (yR j)), λ i, sum.cases_on i (λ i, neg_add_relabelling (xL i) (mk yl yr yL yR)) (λ i, neg_add_relabelling (mk xl xr xL xR) (yL i))⟩ using_well_founded { dec_tac := pgame_wf_tac } theorem neg_add_le {x y : pgame} : -(x + y) ≤ -x + -y := le_of_relabelling (neg_add_relabelling x y) /-- `x+y` has exactly the same moves as `y+x`. -/ def add_comm_relabelling : Π (x y : pgame.{u}), relabelling (x + y) (y + x) | (mk xl xr xL xR) (mk yl yr yL yR) := begin refine ⟨equiv.sum_comm _ _, equiv.sum_comm _ _, _, _⟩, { rintros (_|_); { dsimp [left_moves_add], apply add_comm_relabelling, } }, { rintros (_|_); { dsimp [right_moves_add], apply add_comm_relabelling, } }, end using_well_founded { dec_tac := pgame_wf_tac } theorem add_comm_le {x y : pgame} : x + y ≤ y + x := le_of_relabelling (add_comm_relabelling x y) theorem add_comm_equiv {x y : pgame} : x + y ≈ y + x := equiv_of_relabelling (add_comm_relabelling x y) /-- `(x + y) + z` has exactly the same moves as `x + (y + z)`. -/ def add_assoc_relabelling : Π (x y z : pgame.{u}), relabelling ((x + y) + z) (x + (y + z)) | (mk xl xr xL xR) (mk yl yr yL yR) (mk zl zr zL zR) := begin refine ⟨equiv.sum_assoc _ _ _, equiv.sum_assoc _ _ _, _, _⟩, { rintro (⟨i|i⟩|i), { apply add_assoc_relabelling, }, { change relabelling (mk xl xr xL xR + yL i + mk zl zr zL zR) (mk xl xr xL xR + (yL i + mk zl zr zL zR)), apply add_assoc_relabelling, }, { change relabelling (mk xl xr xL xR + mk yl yr yL yR + zL i) (mk xl xr xL xR + (mk yl yr yL yR + zL i)), apply add_assoc_relabelling, } }, { rintro (j|⟨j|j⟩), { apply add_assoc_relabelling, }, { change relabelling (mk xl xr xL xR + yR j + mk zl zr zL zR) (mk xl xr xL xR + (yR j + mk zl zr zL zR)), apply add_assoc_relabelling, }, { change relabelling (mk xl xr xL xR + mk yl yr yL yR + zR j) (mk xl xr xL xR + (mk yl yr yL yR + zR j)), apply add_assoc_relabelling, } }, end using_well_founded { dec_tac := pgame_wf_tac } theorem add_assoc_equiv {x y z : pgame} : (x + y) + z ≈ x + (y + z) := equiv_of_relabelling (add_assoc_relabelling x y z) theorem add_le_add_right : Π {x y z : pgame} (h : x ≤ y), x + z ≤ y + z | (mk xl xr xL xR) (mk yl yr yL yR) (mk zl zr zL zR) := begin intros h, rw le_def, split, { -- if Left plays first intros i, change xl ⊕ zl at i, cases i, { -- either they play in x rw le_def at h, cases h, have t := h_left i, rcases t with ⟨i', ih⟩ | ⟨j, jh⟩, { left, refine ⟨(left_moves_add _ _).inv_fun (sum.inl i'), _⟩, exact add_le_add_right ih, }, { right, refine ⟨(right_moves_add _ _).inv_fun (sum.inl j), _⟩, convert add_le_add_right jh, apply add_move_right_inl }, }, { -- or play in z left, refine ⟨(left_moves_add _ _).inv_fun (sum.inr i), _⟩, exact add_le_add_right h, }, }, { -- if Right plays first intros j, change yr ⊕ zr at j, cases j, { -- either they play in y rw le_def at h, cases h, have t := h_right j, rcases t with ⟨i, ih⟩ | ⟨j', jh⟩, { left, refine ⟨(left_moves_add _ _).inv_fun (sum.inl i), _⟩, convert add_le_add_right ih, apply add_move_left_inl }, { right, refine ⟨(right_moves_add _ _).inv_fun (sum.inl j'), _⟩, exact add_le_add_right jh } }, { -- or play in z right, refine ⟨(right_moves_add _ _).inv_fun (sum.inr j), _⟩, exact add_le_add_right h } } end using_well_founded { dec_tac := pgame_wf_tac } theorem add_le_add_left {x y z : pgame} (h : y ≤ z) : x + y ≤ x + z := calc x + y ≤ y + x : add_comm_le ... ≤ z + x : add_le_add_right h ... ≤ x + z : add_comm_le theorem add_congr {w x y z : pgame} (h₁ : w ≈ x) (h₂ : y ≈ z) : w + y ≈ x + z := ⟨calc w + y ≤ w + z : add_le_add_left h₂.1 ... ≤ x + z : add_le_add_right h₁.1, calc x + z ≤ x + y : add_le_add_left h₂.2 ... ≤ w + y : add_le_add_right h₁.2⟩ theorem add_left_neg_le_zero : Π {x : pgame}, (-x) + x ≤ 0 | ⟨xl, xr, xL, xR⟩ := begin rw [le_def], split, { intro i, change xr ⊕ xl at i, cases i, { -- If Left played in -x, Right responds with the same move in x. right, refine ⟨(right_moves_add _ _).inv_fun (sum.inr i), _⟩, convert @add_left_neg_le_zero (xR i), exact add_move_right_inr }, { -- If Left in x, Right responds with the same move in -x. right, dsimp, refine ⟨(right_moves_add _ _).inv_fun (sum.inl i), _⟩, convert @add_left_neg_le_zero (xL i), exact add_move_right_inl }, }, { rintro ⟨⟩, } end using_well_founded { dec_tac := pgame_wf_tac } theorem zero_le_add_left_neg : Π {x : pgame}, 0 ≤ (-x) + x := begin intro x, rw [le_iff_neg_ge, neg_zero], exact le_trans neg_add_le add_left_neg_le_zero end theorem add_left_neg_equiv {x : pgame} : (-x) + x ≈ 0 := ⟨add_left_neg_le_zero, zero_le_add_left_neg⟩ theorem add_right_neg_le_zero {x : pgame} : x + (-x) ≤ 0 := calc x + (-x) ≤ (-x) + x : add_comm_le ... ≤ 0 : add_left_neg_le_zero theorem zero_le_add_right_neg {x : pgame} : 0 ≤ x + (-x) := calc 0 ≤ (-x) + x : zero_le_add_left_neg ... ≤ x + (-x) : add_comm_le theorem add_lt_add_right {x y z : pgame} (h : x < y) : x + z < y + z := suffices y + z ≤ x + z → y ≤ x, by { rw ←not_le at ⊢ h, exact mt this h }, assume w, calc y ≤ y + 0 : le_of_relabelling (add_zero_relabelling _).symm ... ≤ y + (z + -z) : add_le_add_left zero_le_add_right_neg ... ≤ (y + z) + (-z) : le_of_relabelling (add_assoc_relabelling _ _ _).symm ... ≤ (x + z) + (-z) : add_le_add_right w ... ≤ x + (z + -z) : le_of_relabelling (add_assoc_relabelling _ _ _) ... ≤ x + 0 : add_le_add_left add_right_neg_le_zero ... ≤ x : le_of_relabelling (add_zero_relabelling _) theorem add_lt_add_left {x y z : pgame} (h : y < z) : x + y < x + z := calc x + y ≤ y + x : add_comm_le ... < z + x : add_lt_add_right h ... ≤ x + z : add_comm_le theorem le_iff_sub_nonneg {x y : pgame} : x ≤ y ↔ 0 ≤ y - x := ⟨λ h, le_trans zero_le_add_right_neg (add_le_add_right h), λ h, calc x ≤ 0 + x : le_of_relabelling (zero_add_relabelling x).symm ... ≤ (y - x) + x : add_le_add_right h ... ≤ y + (-x + x) : le_of_relabelling (add_assoc_relabelling _ _ _) ... ≤ y + 0 : add_le_add_left (add_left_neg_le_zero) ... ≤ y : le_of_relabelling (add_zero_relabelling y)⟩ theorem lt_iff_sub_pos {x y : pgame} : x < y ↔ 0 < y - x := ⟨λ h, lt_of_le_of_lt zero_le_add_right_neg (add_lt_add_right h), λ h, calc x ≤ 0 + x : le_of_relabelling (zero_add_relabelling x).symm ... < (y - x) + x : add_lt_add_right h ... ≤ y + (-x + x) : le_of_relabelling (add_assoc_relabelling _ _ _) ... ≤ y + 0 : add_le_add_left (add_left_neg_le_zero) ... ≤ y : le_of_relabelling (add_zero_relabelling y)⟩ /-- The pre-game `star`, which is fuzzy/confused with zero. -/ def star : pgame := pgame.of_lists [0] [0] theorem star_lt_zero : star < 0 := or.inr ⟨⟨0, zero_lt_one⟩, (by split; rintros ⟨⟩)⟩ theorem zero_lt_star : 0 < star := or.inl ⟨⟨0, zero_lt_one⟩, (by split; rintros ⟨⟩)⟩ /-- The pre-game `ω`. (In fact all ordinals have game and surreal representatives.) -/ def omega : pgame := ⟨ulift ℕ, pempty, λ n, ↑n.1, pempty.elim⟩ end pgame
502a7d39ad29bc818e43f54962acb3fca7c16903
63abd62053d479eae5abf4951554e1064a4c45b4
/src/measure_theory/integration.lean
57677059d39e01d050d91969b618aee4b0d4bcd5
[ "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
71,660
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 measure_theory.measure_space import measure_theory.borel_space import data.indicator_function import data.support /-! # Lebesgue integral for `ennreal`-valued functions We define simple functions and show that each Borel measurable function on `ennreal` can be approximated by a sequence of simple functions. To prove something for an arbitrary measurable function into `ennreal`, the theorem `measurable.ennreal_induction` shows that is it sufficient to show that the property holds for (multiples of) characteristic functions and is closed under addition and supremum of increasing sequences of functions. ## Notation We introduce the following notation for the lower Lebesgue integral of a function `f : α → ennreal`. * `∫⁻ x, f x ∂μ`: integral of a function `f : α → ennreal` with respect to a measure `μ`; * `∫⁻ x, f x`: integral of a function `f : α → ennreal` with respect to the canonical measure `volume` on `α`; * `∫⁻ x in s, f x ∂μ`: integral of a function `f : α → ennreal` over a set `s` with respect to a measure `μ`, defined as `∫⁻ x, f x ∂(μ.restrict s)`; * `∫⁻ x in s, f x`: integral of a function `f : α → ennreal` over a set `s` with respect to the canonical measure `volume`, defined as `∫⁻ x, f x ∂(volume.restrict s)`. -/ noncomputable theory open set (hiding restrict restrict_apply) filter ennreal open_locale classical topological_space big_operators nnreal namespace measure_theory variables {α β γ δ : Type*} /-- A function `f` from a measurable space to any type is called *simple*, if every preimage `f ⁻¹' {x}` is measurable, and the range is finite. This structure bundles a function with these properties. -/ structure {u v} simple_func (α : Type u) [measurable_space α] (β : Type v) := (to_fun : α → β) (is_measurable_fiber' : ∀ x, is_measurable (to_fun ⁻¹' {x})) (finite_range' : (set.range to_fun).finite) local infixr ` →ₛ `:25 := simple_func namespace simple_func section measurable variables [measurable_space α] instance has_coe_to_fun : has_coe_to_fun (α →ₛ β) := ⟨_, to_fun⟩ lemma coe_injective ⦃f g : α →ₛ β⦄ (H : ⇑f = g) : f = g := by cases f; cases g; congr; exact H @[ext] theorem ext {f g : α →ₛ β} (H : ∀ a, f a = g a) : f = g := coe_injective $ funext H lemma finite_range (f : α →ₛ β) : (set.range f).finite := f.finite_range' lemma is_measurable_fiber (f : α →ₛ β) (x : β) : is_measurable (f ⁻¹' {x}) := f.is_measurable_fiber' x /-- Range of a simple function `α →ₛ β` as a `finset β`. -/ protected def range (f : α →ₛ β) : finset β := f.finite_range.to_finset @[simp] theorem mem_range {f : α →ₛ β} {b} : b ∈ f.range ↔ b ∈ range f := finite.mem_to_finset theorem mem_range_self (f : α →ₛ β) (x : α) : f x ∈ f.range := mem_range.2 ⟨x, rfl⟩ @[simp] lemma coe_range (f : α →ₛ β) : (↑f.range : set β) = set.range f := f.finite_range.coe_to_finset theorem mem_range_of_measure_ne_zero {f : α →ₛ β} {x : β} {μ : measure α} (H : μ (f ⁻¹' {x}) ≠ 0) : x ∈ f.range := let ⟨a, ha⟩ := nonempty_of_measure_ne_zero H in mem_range.2 ⟨a, ha⟩ lemma forall_range_iff {f : α →ₛ β} {p : β → Prop} : (∀ y ∈ f.range, p y) ↔ ∀ x, p (f x) := by simp only [mem_range, set.forall_range_iff] lemma exists_range_iff {f : α →ₛ β} {p : β → Prop} : (∃ y ∈ f.range, p y) ↔ ∃ x, p (f x) := by simpa only [mem_range, exists_prop] using set.exists_range_iff lemma preimage_eq_empty_iff (f : α →ₛ β) (b : β) : f ⁻¹' {b} = ∅ ↔ b ∉ f.range := preimage_singleton_eq_empty.trans $ not_congr mem_range.symm /-- Constant function as a `simple_func`. -/ def const (α) {β} [measurable_space α] (b : β) : α →ₛ β := ⟨λ a, b, λ x, is_measurable.const _, finite_range_const⟩ instance [inhabited β] : inhabited (α →ₛ β) := ⟨const _ (default _)⟩ theorem const_apply (a : α) (b : β) : (const α b) a = b := rfl @[simp] theorem coe_const (b : β) : ⇑(const α b) = function.const α b := rfl @[simp] lemma range_const (α) [measurable_space α] [nonempty α] (b : β) : (const α b).range = {b} := finset.coe_injective $ by simp lemma is_measurable_cut (r : α → β → Prop) (f : α →ₛ β) (h : ∀b, is_measurable {a | r a b}) : is_measurable {a | r a (f a)} := begin have : {a | r a (f a)} = ⋃ b ∈ range f, {a | r a b} ∩ f ⁻¹' {b}, { ext a, suffices : r a (f a) ↔ ∃ i, r a (f i) ∧ f a = f i, by simpa, exact ⟨λ h, ⟨a, ⟨h, rfl⟩⟩, λ ⟨a', ⟨h', e⟩⟩, e.symm ▸ h'⟩ }, rw this, exact is_measurable.bUnion f.finite_range.countable (λ b _, is_measurable.inter (h b) (f.is_measurable_fiber _)) end theorem is_measurable_preimage (f : α →ₛ β) (s) : is_measurable (f ⁻¹' s) := is_measurable_cut (λ _ b, b ∈ s) f (λ b, is_measurable.const (b ∈ s)) /-- A simple function is measurable -/ protected theorem measurable [measurable_space β] (f : α →ₛ β) : measurable f := λ s _, is_measurable_preimage f s protected lemma sum_measure_preimage_singleton (f : α →ₛ β) {μ : measure α} (s : finset β) : ∑ y in s, μ (f ⁻¹' {y}) = μ (f ⁻¹' ↑s) := sum_measure_preimage_singleton _ (λ _ _, f.is_measurable_fiber _) lemma sum_range_measure_preimage_singleton (f : α →ₛ β) (μ : measure α) : ∑ y in f.range, μ (f ⁻¹' {y}) = μ univ := by rw [f.sum_measure_preimage_singleton, coe_range, preimage_range] /-- If-then-else as a `simple_func`. -/ def piecewise (s : set α) (hs : is_measurable s) (f g : α →ₛ β) : α →ₛ β := ⟨s.piecewise f g, λ x, by letI : measurable_space β := ⊤; exact f.measurable.piecewise hs g.measurable trivial, (f.finite_range.union g.finite_range).subset range_ite_subset⟩ @[simp] theorem coe_piecewise {s : set α} (hs : is_measurable s) (f g : α →ₛ β) : ⇑(piecewise s hs f g) = s.piecewise f g := rfl theorem piecewise_apply {s : set α} (hs : is_measurable s) (f g : α →ₛ β) (a) : piecewise s hs f g a = if a ∈ s then f a else g a := rfl @[simp] lemma piecewise_compl {s : set α} (hs : is_measurable sᶜ) (f g : α →ₛ β) : piecewise sᶜ hs f g = piecewise s hs.of_compl g f := coe_injective $ by simp [hs] @[simp] lemma piecewise_univ (f g : α →ₛ β) : piecewise univ is_measurable.univ f g = f := coe_injective $ by simp @[simp] lemma piecewise_empty (f g : α →ₛ β) : piecewise ∅ is_measurable.empty f g = g := coe_injective $ by simp lemma measurable_bind [measurable_space γ] (f : α →ₛ β) (g : β → α → γ) (hg : ∀ b, measurable (g b)) : measurable (λ a, g (f a) a) := λ s hs, f.is_measurable_cut (λ a b, g b a ∈ s) $ λ b, hg b hs /-- If `f : α →ₛ β` is a simple function and `g : β → α →ₛ γ` is a family of simple functions, then `f.bind g` binds the first argument of `g` to `f`. In other words, `f.bind g a = g (f a) a`. -/ def bind (f : α →ₛ β) (g : β → α →ₛ γ) : α →ₛ γ := ⟨λa, g (f a) a, λ c, f.is_measurable_cut (λ a b, g b a = c) $ λ b, (g b).is_measurable_preimage {c}, (f.finite_range.bUnion (λ b _, (g b).finite_range)).subset $ by rintro _ ⟨a, rfl⟩; simp; exact ⟨a, a, rfl⟩⟩ @[simp] theorem bind_apply (f : α →ₛ β) (g : β → α →ₛ γ) (a) : f.bind g a = g (f a) a := rfl /-- Given a function `g : β → γ` and a simple function `f : α →ₛ β`, `f.map g` return the simple function `g ∘ f : α →ₛ γ` -/ def map (g : β → γ) (f : α →ₛ β) : α →ₛ γ := bind f (const α ∘ g) theorem map_apply (g : β → γ) (f : α →ₛ β) (a) : f.map g a = g (f a) := rfl theorem map_map (g : β → γ) (h: γ → δ) (f : α →ₛ β) : (f.map g).map h = f.map (h ∘ g) := rfl @[simp] theorem coe_map (g : β → γ) (f : α →ₛ β) : (f.map g : α → γ) = g ∘ f := rfl @[simp] theorem range_map [decidable_eq γ] (g : β → γ) (f : α →ₛ β) : (f.map g).range = f.range.image g := finset.coe_injective $ by simp [range_comp] @[simp] theorem map_const (g : β → γ) (b : β) : (const α b).map g = const α (g b) := rfl lemma map_preimage (f : α →ₛ β) (g : β → γ) (s : set γ) : (f.map g) ⁻¹' s = f ⁻¹' ↑(f.range.filter (λb, g b ∈ s)) := by { simp only [coe_range, sep_mem_eq, set.mem_range, function.comp_app, coe_map, finset.coe_filter, ← mem_preimage, inter_comm, preimage_inter_range], apply preimage_comp } lemma map_preimage_singleton (f : α →ₛ β) (g : β → γ) (c : γ) : (f.map g) ⁻¹' {c} = f ⁻¹' ↑(f.range.filter (λ b, g b = c)) := map_preimage _ _ _ /-- Composition of a `simple_fun` and a measurable function is a `simple_func`. -/ def comp [measurable_space β] (f : β →ₛ γ) (g : α → β) (hgm : measurable g) : α →ₛ γ := { to_fun := f ∘ g, finite_range' := f.finite_range.subset $ set.range_comp_subset_range _ _, is_measurable_fiber' := λ z, hgm (f.is_measurable_fiber z) } @[simp] lemma coe_comp [measurable_space β] (f : β →ₛ γ) {g : α → β} (hgm : measurable g) : ⇑(f.comp g hgm) = f ∘ g := rfl lemma range_comp_subset_range [measurable_space β] (f : β →ₛ γ) {g : α → β} (hgm : measurable g) : (f.comp g hgm).range ⊆ f.range := finset.coe_subset.1 $ by simp only [coe_range, coe_comp, set.range_comp_subset_range] /-- If `f` is a simple function taking values in `β → γ` and `g` is another simple function with the same domain and codomain `β`, then `f.seq g = f a (g a)`. -/ def seq (f : α →ₛ (β → γ)) (g : α →ₛ β) : α →ₛ γ := f.bind (λf, g.map f) @[simp] lemma seq_apply (f : α →ₛ (β → γ)) (g : α →ₛ β) (a : α) : f.seq g a = f a (g a) := rfl /-- Combine two simple functions `f : α →ₛ β` and `g : α →ₛ β` into `λ a, (f a, g a)`. -/ def pair (f : α →ₛ β) (g : α →ₛ γ) : α →ₛ (β × γ) := (f.map prod.mk).seq g @[simp] lemma pair_apply (f : α →ₛ β) (g : α →ₛ γ) (a) : pair f g a = (f a, g a) := rfl lemma pair_preimage (f : α →ₛ β) (g : α →ₛ γ) (s : set β) (t : set γ) : (pair f g) ⁻¹' (set.prod s t) = (f ⁻¹' s) ∩ (g ⁻¹' t) := rfl /- A special form of `pair_preimage` -/ lemma pair_preimage_singleton (f : α →ₛ β) (g : α →ₛ γ) (b : β) (c : γ) : (pair f g) ⁻¹' {(b, c)} = (f ⁻¹' {b}) ∩ (g ⁻¹' {c}) := by { rw ← singleton_prod_singleton, exact pair_preimage _ _ _ _ } theorem bind_const (f : α →ₛ β) : f.bind (const α) = f := by ext; simp instance [has_zero β] : has_zero (α →ₛ β) := ⟨const α 0⟩ instance [has_add β] : has_add (α →ₛ β) := ⟨λf g, (f.map (+)).seq g⟩ instance [has_mul β] : has_mul (α →ₛ β) := ⟨λf g, (f.map (*)).seq g⟩ instance [has_sup β] : has_sup (α →ₛ β) := ⟨λf g, (f.map (⊔)).seq g⟩ instance [has_inf β] : has_inf (α →ₛ β) := ⟨λf g, (f.map (⊓)).seq g⟩ instance [has_le β] : has_le (α →ₛ β) := ⟨λf g, ∀a, f a ≤ g a⟩ @[simp, norm_cast] lemma coe_zero [has_zero β] : ⇑(0 : α →ₛ β) = 0 := rfl @[simp] lemma const_zero [has_zero β] : const α (0:β) = 0 := rfl @[simp, norm_cast] lemma coe_add [has_add β] (f g : α →ₛ β) : ⇑(f + g) = f + g := rfl @[simp, norm_cast] lemma coe_mul [has_mul β] (f g : α →ₛ β) : ⇑(f * g) = f * g := rfl @[simp, norm_cast] lemma coe_le [preorder β] {f g : α →ₛ β} : (f : α → β) ≤ g ↔ f ≤ g := iff.rfl @[simp] lemma range_zero [nonempty α] [has_zero β] : (0 : α →ₛ β).range = {0} := finset.ext $ λ x, by simp [eq_comm] lemma eq_zero_of_mem_range_zero [has_zero β] : ∀ {y : β}, y ∈ (0 : α →ₛ β).range → y = 0 := forall_range_iff.2 $ λ x, rfl lemma sup_apply [has_sup β] (f g : α →ₛ β) (a : α) : (f ⊔ g) a = f a ⊔ g a := rfl lemma mul_apply [has_mul β] (f g : α →ₛ β) (a : α) : (f * g) a = f a * g a := rfl lemma add_apply [has_add β] (f g : α →ₛ β) (a : α) : (f + g) a = f a + g a := rfl lemma add_eq_map₂ [has_add β] (f g : α →ₛ β) : f + g = (pair f g).map (λp:β×β, p.1 + p.2) := rfl lemma mul_eq_map₂ [has_mul β] (f g : α →ₛ β) : f * g = (pair f g).map (λp:β×β, p.1 * p.2) := rfl lemma sup_eq_map₂ [has_sup β] (f g : α →ₛ β) : f ⊔ g = (pair f g).map (λp:β×β, p.1 ⊔ p.2) := rfl lemma const_mul_eq_map [has_mul β] (f : α →ₛ β) (b : β) : const α b * f = f.map (λa, b * a) := rfl instance [add_monoid β] : add_monoid (α →ₛ β) := function.injective.add_monoid (λ f, show α → β, from f) coe_injective coe_zero coe_add instance add_comm_monoid [add_comm_monoid β] : add_comm_monoid (α →ₛ β) := function.injective.add_comm_monoid (λ f, show α → β, from f) coe_injective coe_zero coe_add instance [has_neg β] : has_neg (α →ₛ β) := ⟨λf, f.map (has_neg.neg)⟩ @[simp, norm_cast] lemma coe_neg [has_neg β] (f : α →ₛ β) : ⇑(-f) = -f := rfl instance [add_group β] : add_group (α →ₛ β) := function.injective.add_group (λ f, show α → β, from f) coe_injective coe_zero coe_add coe_neg @[simp, norm_cast] lemma coe_sub [add_group β] (f g : α →ₛ β) : ⇑(f - g) = f - g := rfl instance [add_comm_group β] : add_comm_group (α →ₛ β) := function.injective.add_comm_group (λ f, show α → β, from f) coe_injective coe_zero coe_add coe_neg variables {K : Type*} instance [has_scalar K β] : has_scalar K (α →ₛ β) := ⟨λk f, f.map ((•) k)⟩ @[simp] lemma coe_smul [has_scalar K β] (c : K) (f : α →ₛ β) : ⇑(c • f) = c • f := rfl lemma smul_apply [has_scalar K β] (k : K) (f : α →ₛ β) (a : α) : (k • f) a = k • f a := rfl instance [semiring K] [add_comm_monoid β] [semimodule K β] : semimodule K (α →ₛ β) := function.injective.semimodule K ⟨λ f, show α → β, from f, coe_zero, coe_add⟩ coe_injective coe_smul lemma smul_eq_map [has_scalar K β] (k : K) (f : α →ₛ β) : k • f = f.map ((•) k) := rfl instance [preorder β] : preorder (α →ₛ β) := { le_refl := λf a, le_refl _, le_trans := λf g h hfg hgh a, le_trans (hfg _) (hgh a), .. simple_func.has_le } instance [partial_order β] : partial_order (α →ₛ β) := { le_antisymm := assume f g hfg hgf, ext $ assume a, le_antisymm (hfg a) (hgf a), .. simple_func.preorder } instance [order_bot β] : order_bot (α →ₛ β) := { bot := const α ⊥, bot_le := λf a, bot_le, .. simple_func.partial_order } instance [order_top β] : order_top (α →ₛ β) := { top := const α ⊤, le_top := λf a, le_top, .. simple_func.partial_order } instance [semilattice_inf β] : semilattice_inf (α →ₛ β) := { inf := (⊓), inf_le_left := assume f g a, inf_le_left, inf_le_right := assume f g a, inf_le_right, le_inf := assume f g h hfh hgh a, le_inf (hfh a) (hgh a), .. simple_func.partial_order } instance [semilattice_sup β] : semilattice_sup (α →ₛ β) := { sup := (⊔), le_sup_left := assume f g a, le_sup_left, le_sup_right := assume f g a, le_sup_right, sup_le := assume f g h hfh hgh a, sup_le (hfh a) (hgh a), .. simple_func.partial_order } instance [semilattice_sup_bot β] : semilattice_sup_bot (α →ₛ β) := { .. simple_func.semilattice_sup,.. simple_func.order_bot } instance [lattice β] : lattice (α →ₛ β) := { .. simple_func.semilattice_sup,.. simple_func.semilattice_inf } instance [bounded_lattice β] : bounded_lattice (α →ₛ β) := { .. simple_func.lattice, .. simple_func.order_bot, .. simple_func.order_top } lemma finset_sup_apply [semilattice_sup_bot β] {f : γ → α →ₛ β} (s : finset γ) (a : α) : s.sup f a = s.sup (λc, f c a) := begin refine finset.induction_on s rfl _, assume a s hs ih, rw [finset.sup_insert, finset.sup_insert, sup_apply, ih] end section restrict variables [has_zero β] /-- Restrict a simple function `f : α →ₛ β` to a set `s`. If `s` is measurable, then `f.restrict s a = if a ∈ s then f a else 0`, otherwise `f.restrict s = const α 0`. -/ def restrict (f : α →ₛ β) (s : set α) : α →ₛ β := if hs : is_measurable s then piecewise s hs f 0 else 0 theorem restrict_of_not_measurable {f : α →ₛ β} {s : set α} (hs : ¬is_measurable s) : restrict f s = 0 := dif_neg hs @[simp] theorem coe_restrict (f : α →ₛ β) {s : set α} (hs : is_measurable s) : ⇑(restrict f s) = indicator s f := by { rw [restrict, dif_pos hs], refl } @[simp] theorem restrict_univ (f : α →ₛ β) : restrict f univ = f := by simp [restrict] @[simp] theorem restrict_empty (f : α →ₛ β) : restrict f ∅ = 0 := by simp [restrict] theorem map_restrict_of_zero [has_zero γ] {g : β → γ} (hg : g 0 = 0) (f : α →ₛ β) (s : set α) : (f.restrict s).map g = (f.map g).restrict s := ext $ λ x, if hs : is_measurable s then by simp [hs, set.indicator_comp_of_zero hg] else by simp [restrict_of_not_measurable hs, hg] theorem map_coe_ennreal_restrict (f : α →ₛ nnreal) (s : set α) : (f.restrict s).map (coe : nnreal → ennreal) = (f.map coe).restrict s := map_restrict_of_zero ennreal.coe_zero _ _ theorem map_coe_nnreal_restrict (f : α →ₛ nnreal) (s : set α) : (f.restrict s).map (coe : nnreal → ℝ) = (f.map coe).restrict s := map_restrict_of_zero nnreal.coe_zero _ _ theorem restrict_apply (f : α →ₛ β) {s : set α} (hs : is_measurable s) (a) : restrict f s a = if a ∈ s then f a else 0 := by simp only [hs, coe_restrict] theorem restrict_preimage (f : α →ₛ β) {s : set α} (hs : is_measurable s) {t : set β} (ht : (0:β) ∉ t) : restrict f s ⁻¹' t = s ∩ f ⁻¹' t := by simp [hs, indicator_preimage_of_not_mem _ _ ht] theorem restrict_preimage_singleton (f : α →ₛ β) {s : set α} (hs : is_measurable s) {r : β} (hr : r ≠ 0) : restrict f s ⁻¹' {r} = s ∩ f ⁻¹' {r} := f.restrict_preimage hs hr.symm lemma mem_restrict_range {r : β} {s : set α} {f : α →ₛ β} (hs : is_measurable s) : r ∈ (restrict f s).range ↔ (r = 0 ∧ s ≠ univ) ∨ (r ∈ f '' s) := by rw [← finset.mem_coe, coe_range, coe_restrict _ hs, mem_range_indicator] lemma mem_image_of_mem_range_restrict {r : β} {s : set α} {f : α →ₛ β} (hr : r ∈ (restrict f s).range) (h0 : r ≠ 0) : r ∈ f '' s := if hs : is_measurable s then by simpa [mem_restrict_range hs, h0] using hr else by { rw [restrict_of_not_measurable hs] at hr, exact (h0 $ eq_zero_of_mem_range_zero hr).elim } @[mono] lemma restrict_mono [preorder β] (s : set α) {f g : α →ₛ β} (H : f ≤ g) : f.restrict s ≤ g.restrict s := if hs : is_measurable s then λ x, by simp only [coe_restrict _ hs, indicator_le_indicator (H x)] else by simp only [restrict_of_not_measurable hs, le_refl] end restrict section approx section variables [semilattice_sup_bot β] [has_zero β] /-- Fix a sequence `i : ℕ → β`. Given a function `α → β`, its `n`-th approximation by simple functions is defined so that in case `β = ennreal` it sends each `a` to the supremum of the set `{i k | k ≤ n ∧ i k ≤ f a}`, see `approx_apply` and `supr_approx_apply` for details. -/ def approx (i : ℕ → β) (f : α → β) (n : ℕ) : α →ₛ β := (finset.range n).sup (λk, restrict (const α (i k)) {a:α | i k ≤ f a}) lemma approx_apply [topological_space β] [order_closed_topology β] [measurable_space β] [opens_measurable_space β] {i : ℕ → β} {f : α → β} {n : ℕ} (a : α) (hf : measurable f) : (approx i f n : α →ₛ β) a = (finset.range n).sup (λk, if i k ≤ f a then i k else 0) := begin dsimp only [approx], rw [finset_sup_apply], congr, funext k, rw [restrict_apply], refl, exact (hf is_measurable_Ici) end lemma monotone_approx (i : ℕ → β) (f : α → β) : monotone (approx i f) := assume n m h, finset.sup_mono $ finset.range_subset.2 h lemma approx_comp [topological_space β] [order_closed_topology β] [measurable_space β] [opens_measurable_space β] [measurable_space γ] {i : ℕ → β} {f : γ → β} {g : α → γ} {n : ℕ} (a : α) (hf : measurable f) (hg : measurable g) : (approx i (f ∘ g) n : α →ₛ β) a = (approx i f n : γ →ₛ β) (g a) := by rw [approx_apply _ hf, approx_apply _ (hf.comp hg)] end lemma supr_approx_apply [topological_space β] [complete_lattice β] [order_closed_topology β] [has_zero β] [measurable_space β] [opens_measurable_space β] (i : ℕ → β) (f : α → β) (a : α) (hf : measurable f) (h_zero : (0 : β) = ⊥) : (⨆n, (approx i f n : α →ₛ β) a) = (⨆k (h : i k ≤ f a), i k) := begin refine le_antisymm (supr_le $ assume n, _) (supr_le $ assume k, supr_le $ assume hk, _), { rw [approx_apply a hf, h_zero], refine finset.sup_le (assume k hk, _), split_ifs, exact le_supr_of_le k (le_supr _ h), exact bot_le }, { refine le_supr_of_le (k+1) _, rw [approx_apply a hf], have : k ∈ finset.range (k+1) := finset.mem_range.2 (nat.lt_succ_self _), refine le_trans (le_of_eq _) (finset.le_sup this), rw [if_pos hk] } end end approx section eapprox /-- A sequence of `ennreal`s such that its range is the set of non-negative rational numbers. -/ def ennreal_rat_embed (n : ℕ) : ennreal := ennreal.of_real ((encodable.decode ℚ n).get_or_else (0 : ℚ)) lemma ennreal_rat_embed_encode (q : ℚ) : ennreal_rat_embed (encodable.encode q) = nnreal.of_real q := by rw [ennreal_rat_embed, encodable.encodek]; refl /-- Approximate a function `α → ennreal` by a sequence of simple functions. -/ def eapprox : (α → ennreal) → ℕ → α →ₛ ennreal := approx ennreal_rat_embed lemma monotone_eapprox (f : α → ennreal) : monotone (eapprox f) := monotone_approx _ f lemma supr_eapprox_apply (f : α → ennreal) (hf : measurable f) (a : α) : (⨆n, (eapprox f n : α →ₛ ennreal) a) = f a := begin rw [eapprox, supr_approx_apply ennreal_rat_embed f a hf rfl], refine le_antisymm (supr_le $ assume i, supr_le $ assume hi, hi) (le_of_not_gt _), assume h, rcases ennreal.lt_iff_exists_rat_btwn.1 h with ⟨q, hq, lt_q, q_lt⟩, have : (nnreal.of_real q : ennreal) ≤ (⨆ (k : ℕ) (h : ennreal_rat_embed k ≤ f a), ennreal_rat_embed k), { refine le_supr_of_le (encodable.encode q) _, rw [ennreal_rat_embed_encode q], refine le_supr_of_le (le_of_lt q_lt) _, exact le_refl _ }, exact lt_irrefl _ (lt_of_le_of_lt this lt_q) end lemma eapprox_comp [measurable_space γ] {f : γ → ennreal} {g : α → γ} {n : ℕ} (hf : measurable f) (hg : measurable g) : (eapprox (f ∘ g) n : α → ennreal) = (eapprox f n : γ →ₛ ennreal) ∘ g := funext $ assume a, approx_comp a hf hg end eapprox end measurable section measure variables [measurable_space α] {μ : measure α} /-- Integral of a simple function whose codomain is `ennreal`. -/ def lintegral (f : α →ₛ ennreal) (μ : measure α) : ennreal := ∑ x in f.range, x * μ (f ⁻¹' {x}) lemma lintegral_eq_of_subset (f : α →ₛ ennreal) {s : finset ennreal} (hs : ∀ x, f x ≠ 0 → μ (f ⁻¹' {f x}) ≠ 0 → f x ∈ s) : f.lintegral μ = ∑ x in s, x * μ (f ⁻¹' {x}) := begin refine finset.sum_bij_ne_zero (λr _ _, r) _ _ _ _, { simpa only [forall_range_iff, mul_ne_zero_iff, and_imp] }, { intros, assumption }, { intros b _ hb, refine ⟨b, _, hb, rfl⟩, rw [mem_range, ← preimage_singleton_nonempty], exact nonempty_of_measure_ne_zero (mul_ne_zero_iff.1 hb).2 }, { intros, refl } end /-- Calculate the integral of `(g ∘ f)`, where `g : β → ennreal` and `f : α →ₛ β`. -/ lemma map_lintegral (g : β → ennreal) (f : α →ₛ β) : (f.map g).lintegral μ = ∑ x in f.range, g x * μ (f ⁻¹' {x}) := begin simp only [lintegral, range_map], refine finset.sum_image' _ (assume b hb, _), rcases mem_range.1 hb with ⟨a, rfl⟩, rw [map_preimage_singleton, ← f.sum_measure_preimage_singleton, finset.mul_sum], refine finset.sum_congr _ _, { congr }, { assume x, simp only [finset.mem_filter], rintro ⟨_, h⟩, rw h }, end lemma add_lintegral (f g : α →ₛ ennreal) : (f + g).lintegral μ = f.lintegral μ + g.lintegral μ := calc (f + g).lintegral μ = ∑ x in (pair f g).range, (x.1 * μ (pair f g ⁻¹' {x}) + x.2 * μ (pair f g ⁻¹' {x})) : by rw [add_eq_map₂, map_lintegral]; exact finset.sum_congr rfl (assume a ha, add_mul _ _ _) ... = ∑ x in (pair f g).range, x.1 * μ (pair f g ⁻¹' {x}) + ∑ x in (pair f g).range, x.2 * μ (pair f g ⁻¹' {x}) : by rw [finset.sum_add_distrib] ... = ((pair f g).map prod.fst).lintegral μ + ((pair f g).map prod.snd).lintegral μ : by rw [map_lintegral, map_lintegral] ... = lintegral f μ + lintegral g μ : rfl lemma const_mul_lintegral (f : α →ₛ ennreal) (x : ennreal) : (const α x * f).lintegral μ = x * f.lintegral μ := calc (f.map (λa, x * a)).lintegral μ = ∑ r in f.range, x * r * μ (f ⁻¹' {r}) : map_lintegral _ _ ... = ∑ r in f.range, x * (r * μ (f ⁻¹' {r})) : finset.sum_congr rfl (assume a ha, mul_assoc _ _ _) ... = x * f.lintegral μ : finset.mul_sum.symm /-- Integral of a simple function `α →ₛ ennreal` as a bilinear map. -/ def lintegralₗ : (α →ₛ ennreal) →ₗ[ennreal] measure α →ₗ[ennreal] ennreal := { to_fun := λ f, { to_fun := lintegral f, map_add' := by simp [lintegral, mul_add, finset.sum_add_distrib], map_smul' := λ c μ, by simp [lintegral, mul_left_comm _ c, finset.mul_sum] }, map_add' := λ f g, linear_map.ext (λ μ, add_lintegral f g), map_smul' := λ c f, linear_map.ext (λ μ, const_mul_lintegral f c) } @[simp] lemma zero_lintegral : (0 : α →ₛ ennreal).lintegral μ = 0 := linear_map.ext_iff.1 lintegralₗ.map_zero μ lemma lintegral_add {ν} (f : α →ₛ ennreal) : f.lintegral (μ + ν) = f.lintegral μ + f.lintegral ν := (lintegralₗ f).map_add μ ν lemma lintegral_smul (f : α →ₛ ennreal) (c : ennreal) : f.lintegral (c • μ) = c • f.lintegral μ := (lintegralₗ f).map_smul c μ @[simp] lemma lintegral_zero (f : α →ₛ ennreal) : f.lintegral 0 = 0 := (lintegralₗ f).map_zero lemma lintegral_sum {ι} (f : α →ₛ ennreal) (μ : ι → measure α) : f.lintegral (measure.sum μ) = ∑' i, f.lintegral (μ i) := begin simp only [lintegral, measure.sum_apply, f.is_measurable_preimage, ← finset.tsum_subtype, ← ennreal.tsum_mul_left], apply ennreal.tsum_comm end lemma restrict_lintegral (f : α →ₛ ennreal) {s : set α} (hs : is_measurable s) : (restrict f s).lintegral μ = ∑ r in f.range, r * μ (f ⁻¹' {r} ∩ s) := calc (restrict f s).lintegral μ = ∑ r in f.range, r * μ (restrict f s ⁻¹' {r}) : lintegral_eq_of_subset _ $ λ x hx, if hxs : x ∈ s then by simp [f.restrict_apply hs, if_pos hxs, mem_range_self] else false.elim $ hx $ by simp [*] ... = ∑ r in f.range, r * μ (f ⁻¹' {r} ∩ s) : finset.sum_congr rfl $ forall_range_iff.2 $ λ b, if hb : f b = 0 then by simp only [hb, zero_mul] else by rw [restrict_preimage_singleton _ hs hb, inter_comm] lemma lintegral_restrict (f : α →ₛ ennreal) (s : set α) (μ : measure α) : f.lintegral (μ.restrict s) = ∑ y in f.range, y * μ (f ⁻¹' {y} ∩ s) := by simp only [lintegral, measure.restrict_apply, f.is_measurable_preimage] lemma restrict_lintegral_eq_lintegral_restrict (f : α →ₛ ennreal) {s : set α} (hs : is_measurable s) : (restrict f s).lintegral μ = f.lintegral (μ.restrict s) := by rw [f.restrict_lintegral hs, lintegral_restrict] lemma const_lintegral (c : ennreal) : (const α c).lintegral μ = c * μ univ := begin rw [lintegral], by_cases ha : nonempty α, { resetI, simp [preimage_const_of_mem] }, { simp [μ.eq_zero_of_not_nonempty ha] } end lemma const_lintegral_restrict (c : ennreal) (s : set α) : (const α c).lintegral (μ.restrict s) = c * μ s := by rw [const_lintegral, measure.restrict_apply is_measurable.univ, univ_inter] lemma restrict_const_lintegral (c : ennreal) {s : set α} (hs : is_measurable s) : ((const α c).restrict s).lintegral μ = c * μ s := by rw [restrict_lintegral_eq_lintegral_restrict _ hs, const_lintegral_restrict] lemma le_sup_lintegral (f g : α →ₛ ennreal) : f.lintegral μ ⊔ g.lintegral μ ≤ (f ⊔ g).lintegral μ := calc f.lintegral μ ⊔ g.lintegral μ = ((pair f g).map prod.fst).lintegral μ ⊔ ((pair f g).map prod.snd).lintegral μ : rfl ... ≤ ∑ x in (pair f g).range, (x.1 ⊔ x.2) * μ (pair f g ⁻¹' {x}) : begin rw [map_lintegral, map_lintegral], refine sup_le _ _; refine finset.sum_le_sum (λ a _, canonically_ordered_semiring.mul_le_mul _ (le_refl _)), exact le_sup_left, exact le_sup_right end ... = (f ⊔ g).lintegral μ : by rw [sup_eq_map₂, map_lintegral] /-- `simple_func.lintegral` is monotone both in function and in measure. -/ @[mono] lemma lintegral_mono {f g : α →ₛ ennreal} (hfg : f ≤ g) {μ ν : measure α} (hμν : μ ≤ ν) : f.lintegral μ ≤ g.lintegral ν := calc f.lintegral μ ≤ f.lintegral μ ⊔ g.lintegral μ : le_sup_left ... ≤ (f ⊔ g).lintegral μ : le_sup_lintegral _ _ ... = g.lintegral μ : by rw [sup_of_le_right hfg] ... ≤ g.lintegral ν : finset.sum_le_sum $ λ y hy, ennreal.mul_left_mono $ hμν _ (g.is_measurable_preimage _) /-- `simple_func.lintegral` depends only on the measures of `f ⁻¹' {y}`. -/ lemma lintegral_eq_of_measure_preimage [measurable_space β] {f : α →ₛ ennreal} {g : β →ₛ ennreal} {ν : measure β} (H : ∀ y, μ (f ⁻¹' {y}) = ν (g ⁻¹' {y})) : f.lintegral μ = g.lintegral ν := begin simp only [lintegral, ← H], apply lintegral_eq_of_subset, simp only [H], intros, exact mem_range_of_measure_ne_zero ‹_› end /-- If two simple functions are equal a.e., then their `lintegral`s are equal. -/ lemma lintegral_congr {f g : α →ₛ ennreal} (h : f =ᵐ[μ] g) : f.lintegral μ = g.lintegral μ := lintegral_eq_of_measure_preimage $ λ y, measure_congr $ eventually.set_eq $ h.mono $ λ x hx, by simp [hx] lemma lintegral_map {β} [measurable_space β] {μ' : measure β} (f : α →ₛ ennreal) (g : β →ₛ ennreal) (m : α → β) (eq : ∀a:α, f a = g (m a)) (h : ∀s:set β, is_measurable s → μ' s = μ (m ⁻¹' s)) : f.lintegral μ = g.lintegral μ' := lintegral_eq_of_measure_preimage $ λ y, by { simp only [preimage, eq], exact (h (g ⁻¹' {y}) (g.is_measurable_preimage _)).symm } end measure section fin_meas_supp variables [measurable_space α] [has_zero β] [has_zero γ] {μ : measure α} open finset ennreal function lemma support_eq (f : α →ₛ β) : support f = ⋃ y ∈ f.range.filter (λ y, y ≠ 0), f ⁻¹' {y} := set.ext $ λ x, by simp only [finset.bUnion_preimage_singleton, mem_support, set.mem_preimage, finset.mem_coe, mem_filter, mem_range_self, true_and] /-- A `simple_func` has finite measure support if it is equal to `0` outside of a set of finite measure. -/ protected def fin_meas_supp (f : α →ₛ β) (μ : measure α) : Prop := f =ᶠ[μ.cofinite] 0 lemma fin_meas_supp_iff_support {f : α →ₛ β} {μ : measure α} : f.fin_meas_supp μ ↔ μ (support f) < ⊤ := iff.rfl lemma fin_meas_supp_iff {f : α →ₛ β} {μ : measure α} : f.fin_meas_supp μ ↔ ∀ y ≠ 0, μ (f ⁻¹' {y}) < ⊤ := begin split, { refine λ h y hy, lt_of_le_of_lt (measure_mono _) h, exact λ x hx (H : f x = 0), hy $ H ▸ eq.symm hx }, { intro H, rw [fin_meas_supp_iff_support, support_eq], refine lt_of_le_of_lt (measure_bUnion_finset_le _ _) (sum_lt_top _), exact λ y hy, H y (finset.mem_filter.1 hy).2 } end namespace fin_meas_supp lemma meas_preimage_singleton_ne_zero {f : α →ₛ β} (h : f.fin_meas_supp μ) {y : β} (hy : y ≠ 0) : μ (f ⁻¹' {y}) < ⊤ := fin_meas_supp_iff.1 h y hy protected lemma map {f : α →ₛ β} {g : β → γ} (hf : f.fin_meas_supp μ) (hg : g 0 = 0) : (f.map g).fin_meas_supp μ := flip lt_of_le_of_lt hf (measure_mono $ support_comp_subset hg f) lemma of_map {f : α →ₛ β} {g : β → γ} (h : (f.map g).fin_meas_supp μ) (hg : ∀b, g b = 0 → b = 0) : f.fin_meas_supp μ := flip lt_of_le_of_lt h $ measure_mono $ support_subset_comp hg _ lemma map_iff {f : α →ₛ β} {g : β → γ} (hg : ∀ {b}, g b = 0 ↔ b = 0) : (f.map g).fin_meas_supp μ ↔ f.fin_meas_supp μ := ⟨λ h, h.of_map $ λ b, hg.1, λ h, h.map $ hg.2 rfl⟩ protected lemma pair {f : α →ₛ β} {g : α →ₛ γ} (hf : f.fin_meas_supp μ) (hg : g.fin_meas_supp μ) : (pair f g).fin_meas_supp μ := calc μ (support $ pair f g) = μ (support f ∪ support g) : congr_arg μ $ support_prod_mk f g ... ≤ μ (support f) + μ (support g) : measure_union_le _ _ ... < _ : add_lt_top.2 ⟨hf, hg⟩ protected lemma map₂ [has_zero δ] {μ : measure α} {f : α →ₛ β} (hf : f.fin_meas_supp μ) {g : α →ₛ γ} (hg : g.fin_meas_supp μ) {op : β → γ → δ} (H : op 0 0 = 0) : ((pair f g).map (function.uncurry op)).fin_meas_supp μ := (hf.pair hg).map H protected lemma add {β} [add_monoid β] {f g : α →ₛ β} (hf : f.fin_meas_supp μ) (hg : g.fin_meas_supp μ) : (f + g).fin_meas_supp μ := by { rw [add_eq_map₂], exact hf.map₂ hg (zero_add 0) } protected lemma mul {β} [monoid_with_zero β] {f g : α →ₛ β} (hf : f.fin_meas_supp μ) (hg : g.fin_meas_supp μ) : (f * g).fin_meas_supp μ := by { rw [mul_eq_map₂], exact hf.map₂ hg (zero_mul 0) } lemma lintegral_lt_top {f : α →ₛ ennreal} (hm : f.fin_meas_supp μ) (hf : ∀ᵐ a ∂μ, f a < ⊤) : f.lintegral μ < ⊤ := begin refine sum_lt_top (λ a ha, _), rcases eq_or_lt_of_le (le_top : a ≤ ⊤) with rfl|ha, { simp only [ae_iff, lt_top_iff_ne_top, ne.def, not_not] at hf, simp [set.preimage, hf] }, { by_cases ha0 : a = 0, { subst a, rwa [zero_mul] }, { exact mul_lt_top ha (fin_meas_supp_iff.1 hm _ ha0) } } end lemma of_lintegral_lt_top {f : α →ₛ ennreal} (h : f.lintegral μ < ⊤) : f.fin_meas_supp μ := begin refine fin_meas_supp_iff.2 (λ b hb, _), rw [lintegral, sum_lt_top_iff] at h, by_cases b_mem : b ∈ f.range, { rw ennreal.lt_top_iff_ne_top, have h : ¬ _ = ⊤ := ennreal.lt_top_iff_ne_top.1 (h b b_mem), simp only [mul_eq_top, not_or_distrib, not_and_distrib] at h, rcases h with ⟨h, h'⟩, refine or.elim h (λh, by contradiction) (λh, h) }, { rw ← preimage_eq_empty_iff at b_mem, rw [b_mem, measure_empty], exact with_top.zero_lt_top } end lemma iff_lintegral_lt_top {f : α →ₛ ennreal} (hf : ∀ᵐ a ∂μ, f a < ⊤) : f.fin_meas_supp μ ↔ f.lintegral μ < ⊤ := ⟨λ h, h.lintegral_lt_top hf, λ h, of_lintegral_lt_top h⟩ end fin_meas_supp end fin_meas_supp /-- To prove something for an arbitrary simple function, it suffices to show that the property holds for (multiples of) characteristic functions and is closed under addition (of functions with disjoint support). It is possible to make the hypotheses in `h_sum` a bit stronger, and such conditions can be added once we need them (for example it is only necessary to consider the case where `g` is a multiple of a characteristic function, and that this multiple doesn't appear in the image of `f`) -/ @[elab_as_eliminator] protected lemma induction {α γ} [measurable_space α] [add_monoid γ] {P : simple_func α γ → Prop} (h_ind : ∀ c {s} (hs : is_measurable s), P (simple_func.piecewise s hs (simple_func.const _ c) (simple_func.const _ 0))) (h_sum : ∀ ⦃f g : simple_func α γ⦄, set.univ ⊆ f ⁻¹' {0} ∪ g ⁻¹' {0} → P f → P g → P (f + g)) (f : simple_func α γ) : P f := begin generalize' h : f.range \ {0} = s, rw [← finset.coe_inj, finset.coe_sdiff, finset.coe_singleton, simple_func.coe_range] at h, revert s f h, refine finset.induction _ _, { intros f hf, rw [finset.coe_empty, diff_eq_empty, range_subset_singleton] at hf, convert h_ind 0 is_measurable.univ, ext x, simp [hf] }, { intros x s hxs ih f hf, have mx := f.is_measurable_preimage {x}, let g := simple_func.piecewise (f ⁻¹' {x}) mx 0 f, have Pg : P g, { apply ih, simp only [g, simple_func.coe_piecewise, range_piecewise], rw [image_compl_preimage, union_diff_distrib, diff_diff_comm, hf, finset.coe_insert, insert_diff_self_of_not_mem, diff_eq_empty.mpr, set.empty_union], { rw [set.image_subset_iff], convert set.subset_univ _, exact preimage_const_of_mem (mem_singleton _) }, { rwa [finset.mem_coe] }}, convert h_sum _ Pg (h_ind x mx), { ext1 y, by_cases hy : y ∈ f ⁻¹' {x}; [simpa [hy], simp [hy]] }, { rintro y -, by_cases hy : y ∈ f ⁻¹' {x}; simp [hy] } } end end simple_func section lintegral open simple_func variables [measurable_space α] {μ : measure α} /-- The lower Lebesgue integral of a function `f` with respect to a measure `μ`. -/ def lintegral (μ : measure α) (f : α → ennreal) : ennreal := ⨆ (g : α →ₛ ennreal) (hf : ⇑g ≤ f), g.lintegral μ /-! In the notation for integrals, an expression like `∫⁻ x, g ∥x∥ ∂μ` will not be parsed correctly, and needs parentheses. We do not set the binding power of `r` to `0`, because then `∫⁻ x, f x = 0` will be parsed incorrectly. -/ notation `∫⁻` binders `, ` r:(scoped:60 f, f) ` ∂` μ:70 := lintegral μ r notation `∫⁻` binders `, ` r:(scoped:60 f, lintegral volume f) := r notation `∫⁻` binders ` in ` s `, ` r:(scoped:60 f, f) ` ∂` μ:70 := lintegral (measure.restrict μ s) r notation `∫⁻` binders ` in ` s `, ` r:(scoped:60 f, lintegral (measure.restrict volume s) f) := r theorem simple_func.lintegral_eq_lintegral (f : α →ₛ ennreal) (μ : measure α) : ∫⁻ a, f a ∂ μ = f.lintegral μ := le_antisymm (bsupr_le $ λ g hg, lintegral_mono hg $ le_refl _) (le_supr_of_le f $ le_supr_of_le (le_refl _) (le_refl _)) @[mono] lemma lintegral_mono' ⦃μ ν : measure α⦄ (hμν : μ ≤ ν) ⦃f g : α → ennreal⦄ (hfg : f ≤ g) : ∫⁻ a, f a ∂μ ≤ ∫⁻ a, g a ∂ν := supr_le_supr $ λ φ, supr_le_supr2 $ λ hφ, ⟨le_trans hφ hfg, lintegral_mono (le_refl φ) hμν⟩ lemma lintegral_mono ⦃f g : α → ennreal⦄ (hfg : f ≤ g) : ∫⁻ a, f a ∂μ ≤ ∫⁻ a, g a ∂μ := lintegral_mono' (le_refl μ) hfg lemma monotone_lintegral (μ : measure α) : monotone (lintegral μ) := lintegral_mono @[simp] lemma lintegral_const (c : ennreal) : ∫⁻ a, c ∂μ = c * μ univ := by rw [← simple_func.const_lintegral, ← simple_func.lintegral_eq_lintegral, simple_func.coe_const] lemma set_lintegral_one (s) : ∫⁻ a in s, 1 ∂μ = μ s := by rw [lintegral_const, one_mul, measure.restrict_apply_univ] /-- `∫⁻ a in s, f a ∂μ` is defined as the supremum of integrals of simple functions `φ : α →ₛ ennreal` such that `φ ≤ f`. This lemma says that it suffices to take functions `φ : α →ₛ ℝ≥0`. -/ lemma lintegral_eq_nnreal (f : α → ennreal) (μ : measure α) : (∫⁻ a, f a ∂μ) = (⨆ (φ : α →ₛ ℝ≥0) (hf : ∀ x, ↑(φ x) ≤ f x), (φ.map (coe : nnreal → ennreal)).lintegral μ) := begin refine le_antisymm (bsupr_le $ assume φ hφ, _) (supr_le_supr2 $ λ φ, ⟨φ.map (coe : ℝ≥0 → ennreal), le_refl _⟩), by_cases h : ∀ᵐ a ∂μ, φ a ≠ ⊤, { let ψ := φ.map ennreal.to_nnreal, replace h : ψ.map (coe : ℝ≥0 → ennreal) =ᵐ[μ] φ := h.mono (λ a, ennreal.coe_to_nnreal), have : ∀ x, ↑(ψ x) ≤ f x := λ x, le_trans ennreal.coe_to_nnreal_le_self (hφ x), exact le_supr_of_le (φ.map ennreal.to_nnreal) (le_supr_of_le this (ge_of_eq $ lintegral_congr h)) }, { have h_meas : μ (φ ⁻¹' {⊤}) ≠ 0, from mt measure_zero_iff_ae_nmem.1 h, refine le_trans le_top (ge_of_eq $ (supr_eq_top _).2 $ λ b hb, _), obtain ⟨n, hn⟩ : ∃ n : ℕ, b < n * μ (φ ⁻¹' {⊤}), from exists_nat_mul_gt h_meas (ne_of_lt hb), use (const α (n : ℝ≥0)).restrict (φ ⁻¹' {⊤}), simp only [lt_supr_iff, exists_prop, coe_restrict, φ.is_measurable_preimage, coe_const, ennreal.coe_indicator, map_coe_ennreal_restrict, map_const, ennreal.coe_nat, restrict_const_lintegral], refine ⟨indicator_le (λ x hx, le_trans _ (hφ _)), hn⟩, simp only [mem_preimage, mem_singleton_iff] at hx, simp only [hx, le_top] } end theorem supr_lintegral_le {ι : Sort*} (f : ι → α → ennreal) : (⨆i, ∫⁻ a, f i a ∂μ) ≤ (∫⁻ a, ⨆i, f i a ∂μ) := begin simp only [← supr_apply], exact (monotone_lintegral μ).le_map_supr end theorem supr2_lintegral_le {ι : Sort*} {ι' : ι → Sort*} (f : Π i, ι' i → α → ennreal) : (⨆i (h : ι' i), ∫⁻ a, f i h a ∂μ) ≤ (∫⁻ a, ⨆i (h : ι' i), f i h a ∂μ) := by { convert (monotone_lintegral μ).le_map_supr2 f, ext1 a, simp only [supr_apply] } theorem le_infi_lintegral {ι : Sort*} (f : ι → α → ennreal) : (∫⁻ a, ⨅i, f i a ∂μ) ≤ (⨅i, ∫⁻ a, f i a ∂μ) := by { simp only [← infi_apply], exact (monotone_lintegral μ).map_infi_le } theorem le_infi2_lintegral {ι : Sort*} {ι' : ι → Sort*} (f : Π i, ι' i → α → ennreal) : (∫⁻ a, ⨅ i (h : ι' i), f i h a ∂μ) ≤ (⨅ i (h : ι' i), ∫⁻ a, f i h a ∂μ) := by { convert (monotone_lintegral μ).map_infi2_le f, ext1 a, simp only [infi_apply] } /-- Monotone convergence theorem -- sometimes called Beppo-Levi convergence. See `lintegral_supr_directed` for a more general form. -/ theorem lintegral_supr {f : ℕ → α → ennreal} (hf : ∀n, measurable (f n)) (h_mono : monotone f) : (∫⁻ a, ⨆n, f n a ∂μ) = (⨆n, ∫⁻ a, f n a ∂μ) := begin set c : nnreal → ennreal := coe, set F := λ a:α, ⨆n, f n a, have hF : measurable F := measurable_supr hf, refine le_antisymm _ (supr_lintegral_le _), rw [lintegral_eq_nnreal], refine supr_le (assume s, supr_le (assume hsf, _)), refine ennreal.le_of_forall_lt_one_mul_lt (assume a ha, _), rcases ennreal.lt_iff_exists_coe.1 ha with ⟨r, rfl, ha⟩, have ha : r < 1 := ennreal.coe_lt_coe.1 ha, let rs := s.map (λa, r * a), have eq_rs : (const α r : α →ₛ ennreal) * map c s = rs.map c, { ext1 a, exact ennreal.coe_mul.symm }, have eq : ∀p, (rs.map c) ⁻¹' {p} = (⋃n, (rs.map c) ⁻¹' {p} ∩ {a | p ≤ f n a}), { assume p, rw [← inter_Union, ← inter_univ ((map c rs) ⁻¹' {p})] {occs := occurrences.pos [1]}, refine set.ext (assume x, and_congr_right $ assume hx, (true_iff _).2 _), by_cases p_eq : p = 0, { simp [p_eq] }, simp at hx, subst hx, have : r * s x ≠ 0, { rwa [(≠), ← ennreal.coe_eq_zero] }, have : s x ≠ 0, { refine mt _ this, assume h, rw [h, mul_zero] }, have : (rs.map c) x < ⨆ (n : ℕ), f n x, { refine lt_of_lt_of_le (ennreal.coe_lt_coe.2 (_)) (hsf x), suffices : r * s x < 1 * s x, simpa [rs], exact mul_lt_mul_of_pos_right ha (zero_lt_iff_ne_zero.2 this) }, rcases lt_supr_iff.1 this with ⟨i, hi⟩, exact mem_Union.2 ⟨i, le_of_lt hi⟩ }, have mono : ∀r:ennreal, monotone (λn, (rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a}), { assume r i j h, refine inter_subset_inter (subset.refl _) _, assume x hx, exact le_trans hx (h_mono h x) }, have h_meas : ∀n, is_measurable {a : α | ⇑(map c rs) a ≤ f n a} := assume n, is_measurable_le (simple_func.measurable _) (hf n), calc (r:ennreal) * (s.map c).lintegral μ = ∑ r in (rs.map c).range, r * μ ((rs.map c) ⁻¹' {r}) : by rw [← const_mul_lintegral, eq_rs, simple_func.lintegral] ... ≤ ∑ r in (rs.map c).range, r * μ (⋃n, (rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a}) : le_of_eq (finset.sum_congr rfl $ assume x hx, by rw ← eq) ... ≤ ∑ r in (rs.map c).range, (⨆n, r * μ ((rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a})) : le_of_eq (finset.sum_congr rfl $ assume x hx, begin rw [measure_Union_eq_supr _ (directed_of_sup $ mono x), ennreal.mul_supr], { assume i, refine ((rs.map c).is_measurable_preimage _).inter _, exact hf i is_measurable_Ici } end) ... ≤ ⨆n, ∑ r in (rs.map c).range, r * μ ((rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a}) : begin refine le_of_eq _, rw [ennreal.finset_sum_supr_nat], assume p i j h, exact canonically_ordered_semiring.mul_le_mul (le_refl _) (measure_mono $ mono p h) end ... ≤ (⨆n:ℕ, ((rs.map c).restrict {a | (rs.map c) a ≤ f n a}).lintegral μ) : begin refine supr_le_supr (assume n, _), rw [restrict_lintegral _ (h_meas n)], { refine le_of_eq (finset.sum_congr rfl $ assume r hr, _), congr' 2 with a, refine and_congr_right _, simp {contextual := tt} } end ... ≤ (⨆n, ∫⁻ a, f n a ∂μ) : begin refine supr_le_supr (assume n, _), rw [← simple_func.lintegral_eq_lintegral], refine lintegral_mono (assume a, _), dsimp, rw [restrict_apply], split_ifs; simp, simpa using h, exact h_meas n end end lemma lintegral_eq_supr_eapprox_lintegral {f : α → ennreal} (hf : measurable f) : (∫⁻ a, f a ∂μ) = (⨆n, (eapprox f n).lintegral μ) := calc (∫⁻ a, f a ∂μ) = (∫⁻ a, ⨆n, (eapprox f n : α → ennreal) a ∂μ) : by congr; ext a; rw [supr_eapprox_apply f hf] ... = (⨆n, ∫⁻ a, (eapprox f n : α → ennreal) a ∂μ) : begin rw [lintegral_supr], { assume n, exact (eapprox f n).measurable }, { assume i j h, exact (monotone_eapprox f h) } end ... = (⨆n, (eapprox f n).lintegral μ) : by congr; ext n; rw [(eapprox f n).lintegral_eq_lintegral] @[simp] lemma lintegral_add {f g : α → ennreal} (hf : measurable f) (hg : measurable g) : (∫⁻ a, f a + g a ∂μ) = (∫⁻ a, f a ∂μ) + (∫⁻ a, g a ∂μ) := calc (∫⁻ a, f a + g a ∂μ) = (∫⁻ a, (⨆n, (eapprox f n : α → ennreal) a) + (⨆n, (eapprox g n : α → ennreal) a) ∂μ) : by congr; funext a; rw [supr_eapprox_apply f hf, supr_eapprox_apply g hg] ... = (∫⁻ a, (⨆n, (eapprox f n + eapprox g n : α → ennreal) a) ∂μ) : begin congr, funext a, rw [ennreal.supr_add_supr_of_monotone], { refl }, { assume i j h, exact monotone_eapprox _ h a }, { assume i j h, exact monotone_eapprox _ h a }, end ... = (⨆n, (eapprox f n).lintegral μ + (eapprox g n).lintegral μ) : begin rw [lintegral_supr], { congr, funext n, rw [← simple_func.add_lintegral, ← simple_func.lintegral_eq_lintegral], refl }, { assume n, exact measurable.add (eapprox f n).measurable (eapprox g n).measurable }, { assume i j h a, exact add_le_add (monotone_eapprox _ h _) (monotone_eapprox _ h _) } end ... = (⨆n, (eapprox f n).lintegral μ) + (⨆n, (eapprox g n).lintegral μ) : by refine (ennreal.supr_add_supr_of_monotone _ _).symm; { assume i j h, exact simple_func.lintegral_mono (monotone_eapprox _ h) (le_refl μ) } ... = (∫⁻ a, f a ∂μ) + (∫⁻ a, g a ∂μ) : by rw [lintegral_eq_supr_eapprox_lintegral hf, lintegral_eq_supr_eapprox_lintegral hg] lemma lintegral_zero : (∫⁻ a:α, 0 ∂μ) = 0 := by simp @[simp] lemma lintegral_smul_measure (c : ennreal) (f : α → ennreal) : ∫⁻ a, f a ∂ (c • μ) = c * ∫⁻ a, f a ∂μ := by simp only [lintegral, supr_subtype', simple_func.lintegral_smul, ennreal.mul_supr, smul_eq_mul] @[simp] lemma lintegral_sum_measure {ι} (f : α → ennreal) (μ : ι → measure α) : ∫⁻ a, f a ∂(measure.sum μ) = ∑' i, ∫⁻ a, f a ∂(μ i) := begin simp only [lintegral, supr_subtype', simple_func.lintegral_sum, ennreal.tsum_eq_supr_sum], rw [supr_comm], congr, funext s, induction s using finset.induction_on with i s hi hs, { apply bot_unique, simp }, simp only [finset.sum_insert hi, ← hs], refine (ennreal.supr_add_supr _).symm, intros φ ψ, exact ⟨⟨φ ⊔ ψ, λ x, sup_le (φ.2 x) (ψ.2 x)⟩, add_le_add (simple_func.lintegral_mono le_sup_left (le_refl _)) (finset.sum_le_sum $ λ j hj, simple_func.lintegral_mono le_sup_right (le_refl _))⟩ end @[simp] lemma lintegral_add_measure (f : α → ennreal) (μ ν : measure α) : ∫⁻ a, f a ∂ (μ + ν) = ∫⁻ a, f a ∂μ + ∫⁻ a, f a ∂ν := by simpa [tsum_fintype] using lintegral_sum_measure f (λ b, cond b μ ν) @[simp] lemma lintegral_zero_measure (f : α → ennreal) : ∫⁻ a, f a ∂0 = 0 := bot_unique $ by simp [lintegral] lemma lintegral_finset_sum (s : finset β) {f : β → α → ennreal} (hf : ∀b, measurable (f b)) : (∫⁻ a, ∑ b in s, f b a ∂μ) = ∑ b in s, ∫⁻ a, f b a ∂μ := begin refine finset.induction_on s _ _, { simp }, { assume a s has ih, simp only [finset.sum_insert has], rw [lintegral_add (hf _) (s.measurable_sum hf), ih] } end @[simp] lemma lintegral_const_mul (r : ennreal) {f : α → ennreal} (hf : measurable f) : (∫⁻ a, r * f a ∂μ) = r * (∫⁻ a, f a ∂μ) := calc (∫⁻ a, r * f a ∂μ) = (∫⁻ a, (⨆n, (const α r * eapprox f n) a) ∂μ) : by { congr, funext a, rw [← supr_eapprox_apply f hf, ennreal.mul_supr], refl } ... = (⨆n, r * (eapprox f n).lintegral μ) : begin rw [lintegral_supr], { congr, funext n, rw [← simple_func.const_mul_lintegral, ← simple_func.lintegral_eq_lintegral] }, { assume n, exact simple_func.measurable _ }, { assume i j h a, exact canonically_ordered_semiring.mul_le_mul (le_refl _) (monotone_eapprox _ h _) } end ... = r * (∫⁻ a, f a ∂μ) : by rw [← ennreal.mul_supr, lintegral_eq_supr_eapprox_lintegral hf] lemma lintegral_const_mul_le (r : ennreal) (f : α → ennreal) : r * (∫⁻ a, f a ∂μ) ≤ (∫⁻ a, r * f a ∂μ) := begin rw [lintegral, ennreal.mul_supr], refine supr_le (λs, _), rw [ennreal.mul_supr], simp only [supr_le_iff, ge_iff_le], assume hs, rw ← simple_func.const_mul_lintegral, refine le_supr_of_le (const α r * s) (le_supr_of_le (λx, _) (le_refl _)), exact canonically_ordered_semiring.mul_le_mul (le_refl _) (hs x) end lemma lintegral_const_mul' (r : ennreal) (f : α → ennreal) (hr : r ≠ ⊤) : (∫⁻ a, r * f a ∂μ) = r * (∫⁻ a, f a ∂μ) := begin by_cases h : r = 0, { simp [h] }, apply le_antisymm _ (lintegral_const_mul_le r f), have rinv : r * r⁻¹ = 1 := ennreal.mul_inv_cancel h hr, have rinv' : r ⁻¹ * r = 1, by { rw mul_comm, exact rinv }, have := lintegral_const_mul_le (r⁻¹) (λx, r * f x), simp [(mul_assoc _ _ _).symm, rinv'] at this, simpa [(mul_assoc _ _ _).symm, rinv] using canonically_ordered_semiring.mul_le_mul (le_refl r) this end lemma lintegral_mul_const (r : ennreal) {f : α → ennreal} (hf : measurable f) : ∫⁻ a, f a * r ∂μ = ∫⁻ a, f a ∂μ * r := by simp_rw [mul_comm, lintegral_const_mul r hf] lemma lintegral_mul_const_le (r : ennreal) (f : α → ennreal) : ∫⁻ a, f a ∂μ * r ≤ ∫⁻ a, f a * r ∂μ := by simp_rw [mul_comm, lintegral_const_mul_le r f] lemma lintegral_mul_const' (r : ennreal) (f : α → ennreal) (hr : r ≠ ⊤): ∫⁻ a, f a * r ∂μ = ∫⁻ a, f a ∂μ * r := by simp_rw [mul_comm, lintegral_const_mul' r f hr] lemma lintegral_mono_ae {f g : α → ennreal} (h : ∀ᵐ a ∂μ, f a ≤ g a) : (∫⁻ a, f a ∂μ) ≤ (∫⁻ a, g a ∂μ) := begin rcases exists_is_measurable_superset_of_measure_eq_zero h with ⟨t, hts, ht, ht0⟩, have : ∀ᵐ x ∂μ, x ∉ t := measure_zero_iff_ae_nmem.1 ht0, refine (supr_le $ assume s, supr_le $ assume hfs, le_supr_of_le (s.restrict tᶜ) $ le_supr_of_le _ _), { assume a, by_cases a ∈ t; simp [h, restrict_apply, ht.compl], exact le_trans (hfs a) (by_contradiction $ assume hnfg, h (hts hnfg)) }, { refine le_of_eq (simple_func.lintegral_congr $ this.mono $ λ a hnt, _), by_cases hat : a ∈ t; simp [hat, ht.compl], exact (hnt hat).elim } end lemma lintegral_congr_ae {f g : α → ennreal} (h : f =ᵐ[μ] g) : (∫⁻ a, f a ∂μ) = (∫⁻ a, g a ∂μ) := le_antisymm (lintegral_mono_ae $ h.le) (lintegral_mono_ae $ h.symm.le) lemma lintegral_congr {f g : α → ennreal} (h : ∀ a, f a = g a) : (∫⁻ a, f a ∂μ) = (∫⁻ a, g a ∂μ) := by simp only [h] lemma set_lintegral_congr {f : α → ennreal} {s t : set α} (h : s =ᵐ[μ] t) : ∫⁻ x in s, f x ∂μ = ∫⁻ x in t, f x ∂μ := by rw [restrict_congr_set h] -- TODO: Need a better way of rewriting inside of a integral lemma lintegral_rw₁ {f f' : α → β} (h : f =ᵐ[μ] f') (g : β → ennreal) : (∫⁻ a, g (f a) ∂μ) = (∫⁻ a, g (f' a) ∂μ) := lintegral_congr_ae $ h.mono $ λ a h, by rw h -- TODO: Need a better way of rewriting inside of a integral lemma lintegral_rw₂ {f₁ f₁' : α → β} {f₂ f₂' : α → γ} (h₁ : f₁ =ᵐ[μ] f₁') (h₂ : f₂ =ᵐ[μ] f₂') (g : β → γ → ennreal) : (∫⁻ a, g (f₁ a) (f₂ a) ∂μ) = (∫⁻ a, g (f₁' a) (f₂' a) ∂μ) := lintegral_congr_ae $ h₁.mp $ h₂.mono $ λ _ h₂ h₁, by rw [h₁, h₂] @[simp] lemma lintegral_indicator (f : α → ennreal) {s : set α} (hs : is_measurable s) : ∫⁻ a, s.indicator f a ∂μ = ∫⁻ a in s, f a ∂μ := begin simp only [lintegral, ← restrict_lintegral_eq_lintegral_restrict _ hs, supr_subtype'], apply le_antisymm; refine supr_le_supr2 (subtype.forall.2 $ λ φ hφ, _), { refine ⟨⟨φ, le_trans hφ (indicator_le_self _ _)⟩, _⟩, refine simple_func.lintegral_mono (λ x, _) (le_refl _), by_cases hx : x ∈ s, { simp [hx, hs, le_refl] }, { apply le_trans (hφ x), simp [hx, hs, le_refl] } }, { refine ⟨⟨φ.restrict s, λ x, _⟩, le_refl _⟩, simp [hφ x, hs, indicator_le_indicator] } end /-- Chebyshev's inequality -/ lemma mul_meas_ge_le_lintegral {f : α → ennreal} (hf : measurable f) (ε : ennreal) : ε * μ {x | ε ≤ f x} ≤ ∫⁻ a, f a ∂μ := begin have : is_measurable {a : α | ε ≤ f a }, from hf is_measurable_Ici, rw [← simple_func.restrict_const_lintegral _ this, ← simple_func.lintegral_eq_lintegral], refine lintegral_mono (λ a, _), simp only [restrict_apply _ this], split_ifs; [assumption, exact zero_le _] end lemma meas_ge_le_lintegral_div {f : α → ennreal} (hf : measurable f) {ε : ennreal} (hε : ε ≠ 0) (hε' : ε ≠ ⊤) : μ {x | ε ≤ f x} ≤ (∫⁻ a, f a ∂μ) / ε := (ennreal.le_div_iff_mul_le (or.inl hε) (or.inl hε')).2 $ by { rw [mul_comm], exact mul_meas_ge_le_lintegral hf ε } @[simp] lemma lintegral_eq_zero_iff {f : α → ennreal} (hf : measurable f) : ∫⁻ a, f a ∂μ = 0 ↔ (f =ᵐ[μ] 0) := begin refine iff.intro (assume h, _) (assume h, _), { have : ∀n:ℕ, ∀ᵐ a ∂μ, f a < n⁻¹, { assume n, rw [ae_iff, ← le_zero_iff_eq, ← @ennreal.zero_div n⁻¹, ennreal.le_div_iff_mul_le, mul_comm], simp only [not_lt], -- TODO: why `rw ← h` fails with "not an equality or an iff"? exacts [h ▸ mul_meas_ge_le_lintegral hf n⁻¹, or.inl (ennreal.inv_ne_zero.2 ennreal.coe_nat_ne_top), or.inr ennreal.zero_ne_top] }, refine (ae_all_iff.2 this).mono (λ a ha, _), by_contradiction h, rcases ennreal.exists_inv_nat_lt h with ⟨n, hn⟩, exact (lt_irrefl _ $ lt_trans hn $ ha n).elim }, { calc ∫⁻ a, f a ∂μ = ∫⁻ a, 0 ∂μ : lintegral_congr_ae h ... = 0 : lintegral_zero } end lemma lintegral_pos_iff_support {f : α → ennreal} (hf : measurable f) : 0 < ∫⁻ a, f a ∂μ ↔ 0 < μ (function.support f) := by simp [zero_lt_iff_ne_zero, hf, filter.eventually_eq, ae_iff, function.support] /-- Weaker version of the monotone convergence theorem-/ lemma lintegral_supr_ae {f : ℕ → α → ennreal} (hf : ∀n, measurable (f n)) (h_mono : ∀n, ∀ᵐ a ∂μ, f n a ≤ f n.succ a) : (∫⁻ a, ⨆n, f n a ∂μ) = (⨆n, ∫⁻ a, f n a ∂μ) := let ⟨s, hs⟩ := exists_is_measurable_superset_of_measure_eq_zero (ae_iff.1 (ae_all_iff.2 h_mono)) in let g := λ n a, if a ∈ s then 0 else f n a in have g_eq_f : ∀ᵐ a ∂μ, ∀n, g n a = f n a, from (measure_zero_iff_ae_nmem.1 hs.2.2).mono (assume a ha n, if_neg ha), calc ∫⁻ a, ⨆n, f n a ∂μ = ∫⁻ a, ⨆n, g n a ∂μ : lintegral_congr_ae $ g_eq_f.mono $ λ a ha, by simp only [ha] ... = ⨆n, (∫⁻ a, g n a ∂μ) : lintegral_supr (assume n, measurable_const.piecewise hs.2.1 (hf n)) (monotone_of_monotone_nat $ assume n a, classical.by_cases (assume h : a ∈ s, by simp [g, if_pos h]) (assume h : a ∉ s, begin simp only [g, if_neg h], have := hs.1, rw subset_def at this, have := mt (this a) h, simp only [not_not, mem_set_of_eq] at this, exact this n end)) ... = ⨆n, (∫⁻ a, f n a ∂μ) : by simp only [lintegral_congr_ae (g_eq_f.mono $ λ a ha, ha _)] lemma lintegral_sub {f g : α → ennreal} (hf : measurable f) (hg : measurable g) (hg_fin : ∫⁻ a, g a ∂μ < ⊤) (h_le : g ≤ᵐ[μ] f) : ∫⁻ a, f a - g a ∂μ = ∫⁻ a, f a ∂μ - ∫⁻ a, g a ∂μ := begin rw [← ennreal.add_left_inj hg_fin, ennreal.sub_add_cancel_of_le (lintegral_mono_ae h_le), ← lintegral_add (hf.ennreal_sub hg) hg], refine lintegral_congr_ae (h_le.mono $ λ x hx, _), exact ennreal.sub_add_cancel_of_le hx end /-- Monotone convergence theorem for nonincreasing sequences of functions -/ lemma lintegral_infi_ae {f : ℕ → α → ennreal} (h_meas : ∀n, measurable (f n)) (h_mono : ∀n:ℕ, f n.succ ≤ᵐ[μ] f n) (h_fin : ∫⁻ a, f 0 a ∂μ < ⊤) : ∫⁻ a, ⨅n, f n a ∂μ = ⨅n, ∫⁻ a, f n a ∂μ := have fn_le_f0 : ∫⁻ a, ⨅n, f n a ∂μ ≤ ∫⁻ a, f 0 a ∂μ, from lintegral_mono (assume a, infi_le_of_le 0 (le_refl _)), have fn_le_f0' : (⨅n, ∫⁻ a, f n a ∂μ) ≤ ∫⁻ a, f 0 a ∂μ, from infi_le_of_le 0 (le_refl _), (ennreal.sub_right_inj h_fin fn_le_f0 fn_le_f0').1 $ show ∫⁻ a, f 0 a ∂μ - ∫⁻ a, ⨅n, f n a ∂μ = ∫⁻ a, f 0 a ∂μ - (⨅n, ∫⁻ a, f n a ∂μ), from calc ∫⁻ a, f 0 a ∂μ - (∫⁻ a, ⨅n, f n a ∂μ) = ∫⁻ a, f 0 a - ⨅n, f n a ∂μ: (lintegral_sub (h_meas 0) (measurable_infi h_meas) (calc (∫⁻ a, ⨅n, f n a ∂μ) ≤ ∫⁻ a, f 0 a ∂μ : lintegral_mono (assume a, infi_le _ _) ... < ⊤ : h_fin ) (ae_of_all _ $ assume a, infi_le _ _)).symm ... = ∫⁻ a, ⨆n, f 0 a - f n a ∂μ : congr rfl (funext (assume a, ennreal.sub_infi)) ... = ⨆n, ∫⁻ a, f 0 a - f n a ∂μ : lintegral_supr_ae (assume n, (h_meas 0).ennreal_sub (h_meas n)) (assume n, (h_mono n).mono $ assume a ha, ennreal.sub_le_sub (le_refl _) ha) ... = ⨆n, ∫⁻ a, f 0 a ∂μ - ∫⁻ a, f n a ∂μ : have h_mono : ∀ᵐ a ∂μ, ∀n:ℕ, f n.succ a ≤ f n a := ae_all_iff.2 h_mono, have h_mono : ∀n, ∀ᵐ a ∂μ, f n a ≤ f 0 a := assume n, h_mono.mono $ assume a h, begin induction n with n ih, {exact le_refl _}, {exact le_trans (h n) ih} end, congr rfl (funext $ assume n, lintegral_sub (h_meas _) (h_meas _) (calc ∫⁻ a, f n a ∂μ ≤ ∫⁻ a, f 0 a ∂μ : lintegral_mono_ae $ h_mono n ... < ⊤ : h_fin) (h_mono n)) ... = ∫⁻ a, f 0 a ∂μ - ⨅n, ∫⁻ a, f n a ∂μ : ennreal.sub_infi.symm /-- Monotone convergence theorem for nonincreasing sequences of functions -/ lemma lintegral_infi {f : ℕ → α → ennreal} (h_meas : ∀n, measurable (f n)) (h_mono : ∀ ⦃m n⦄, m ≤ n → f n ≤ f m) (h_fin : ∫⁻ a, f 0 a ∂μ < ⊤) : ∫⁻ a, ⨅n, f n a ∂μ = ⨅n, ∫⁻ a, f n a ∂μ := lintegral_infi_ae h_meas (λ n, ae_of_all _ $ h_mono $ le_of_lt n.lt_succ_self) h_fin /-- Known as Fatou's lemma -/ lemma lintegral_liminf_le {f : ℕ → α → ennreal} (h_meas : ∀n, measurable (f n)) : ∫⁻ a, liminf at_top (λ n, f n a) ∂μ ≤ liminf at_top (λ n, ∫⁻ a, f n a ∂μ) := calc ∫⁻ a, liminf at_top (λ n, f n a) ∂μ = ∫⁻ a, ⨆n:ℕ, ⨅i≥n, f i a ∂μ : by simp only [liminf_eq_supr_infi_of_nat] ... = ⨆n:ℕ, ∫⁻ a, ⨅i≥n, f i a ∂μ : lintegral_supr (assume n, measurable_binfi _ (countable_encodable _) h_meas) (assume n m hnm a, infi_le_infi_of_subset $ λ i hi, le_trans hnm hi) ... ≤ ⨆n:ℕ, ⨅i≥n, ∫⁻ a, f i a ∂μ : supr_le_supr $ λ n, le_infi2_lintegral _ ... = liminf at_top (λ n, ∫⁻ a, f n a ∂μ) : liminf_eq_supr_infi_of_nat.symm lemma limsup_lintegral_le {f : ℕ → α → ennreal} {g : α → ennreal} (hf_meas : ∀ n, measurable (f n)) (h_bound : ∀n, f n ≤ᵐ[μ] g) (h_fin : ∫⁻ a, g a ∂μ < ⊤) : limsup at_top (λn, ∫⁻ a, f n a ∂μ) ≤ ∫⁻ a, limsup at_top (λn, f n a) ∂μ := calc limsup at_top (λn, ∫⁻ a, f n a ∂μ) = ⨅n:ℕ, ⨆i≥n, ∫⁻ a, f i a ∂μ : limsup_eq_infi_supr_of_nat ... ≤ ⨅n:ℕ, ∫⁻ a, ⨆i≥n, f i a ∂μ : infi_le_infi $ assume n, supr2_lintegral_le _ ... = ∫⁻ a, ⨅n:ℕ, ⨆i≥n, f i a ∂μ : begin refine (lintegral_infi _ _ _).symm, { assume n, exact measurable_bsupr _ (countable_encodable _) hf_meas }, { assume n m hnm a, exact (supr_le_supr_of_subset $ λ i hi, le_trans hnm hi) }, { refine lt_of_le_of_lt (lintegral_mono_ae _) h_fin, refine (ae_all_iff.2 h_bound).mono (λ n hn, _), exact supr_le (λ i, supr_le $ λ hi, hn i) } end ... = ∫⁻ a, limsup at_top (λn, f n a) ∂μ : by simp only [limsup_eq_infi_supr_of_nat] /-- Dominated convergence theorem for nonnegative functions -/ lemma tendsto_lintegral_of_dominated_convergence {F : ℕ → α → ennreal} {f : α → ennreal} (bound : α → ennreal) (hF_meas : ∀n, measurable (F n)) (h_bound : ∀n, F n ≤ᵐ[μ] bound) (h_fin : ∫⁻ a, bound a ∂μ < ⊤) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) : tendsto (λn, ∫⁻ a, F n a ∂μ) at_top (𝓝 (∫⁻ a, f a ∂μ)) := tendsto_of_le_liminf_of_limsup_le (calc ∫⁻ a, f a ∂μ = ∫⁻ a, liminf at_top (λ (n : ℕ), F n a) ∂μ : lintegral_congr_ae $ h_lim.mono $ assume a h, h.liminf_eq.symm ... ≤ liminf at_top (λ n, ∫⁻ a, F n a ∂μ) : lintegral_liminf_le hF_meas) (calc limsup at_top (λ (n : ℕ), ∫⁻ a, F n a ∂μ) ≤ ∫⁻ a, limsup at_top (λn, F n a) ∂μ : limsup_lintegral_le hF_meas h_bound h_fin ... = ∫⁻ a, f a ∂μ : lintegral_congr_ae $ h_lim.mono $ λ a h, h.limsup_eq) /-- Dominated convergence theorem for filters with a countable basis -/ lemma tendsto_lintegral_filter_of_dominated_convergence {ι} {l : filter ι} {F : ι → α → ennreal} {f : α → ennreal} (bound : α → ennreal) (hl_cb : l.is_countably_generated) (hF_meas : ∀ᶠ n in l, measurable (F n)) (h_bound : ∀ᶠ n in l, ∀ᵐ a ∂μ, F n a ≤ bound a) (h_fin : ∫⁻ a, bound a ∂μ < ⊤) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) l (𝓝 (f a))) : tendsto (λn, ∫⁻ a, F n a ∂μ) l (𝓝 $ ∫⁻ a, f a ∂μ) := begin rw hl_cb.tendsto_iff_seq_tendsto, { intros x xl, have hxl, { rw tendsto_at_top' at xl, exact xl }, have h := inter_mem_sets hF_meas h_bound, replace h := hxl _ h, rcases h with ⟨k, h⟩, rw ← tendsto_add_at_top_iff_nat k, refine tendsto_lintegral_of_dominated_convergence _ _ _ _ _, { exact bound }, { intro, refine (h _ _).1, exact nat.le_add_left _ _ }, { intro, refine (h _ _).2, exact nat.le_add_left _ _ }, { assumption }, { refine h_lim.mono (λ a h_lim, _), apply @tendsto.comp _ _ _ (λn, x (n + k)) (λn, F n a), { assumption }, rw tendsto_add_at_top_iff_nat, assumption } }, end section open encodable /-- Monotone convergence for a suprema over a directed family and indexed by an encodable type -/ theorem lintegral_supr_directed [encodable β] {f : β → α → ennreal} (hf : ∀b, measurable (f b)) (h_directed : directed (≤) f) : ∫⁻ a, ⨆b, f b a ∂μ = ⨆b, ∫⁻ a, f b a ∂μ := begin by_cases hβ : nonempty β, swap, { simp [supr_of_empty hβ] }, resetI, inhabit β, have : ∀a, (⨆ b, f b a) = (⨆ n, f (h_directed.sequence f n) a), { assume a, refine le_antisymm (supr_le $ assume b, _) (supr_le $ assume n, le_supr (λn, f n a) _), exact le_supr_of_le (encode b + 1) (h_directed.le_sequence b a) }, calc ∫⁻ a, ⨆ b, f b a ∂μ = ∫⁻ a, ⨆ n, f (h_directed.sequence f n) a ∂μ : by simp only [this] ... = ⨆ n, ∫⁻ a, f (h_directed.sequence f n) a ∂μ : lintegral_supr (assume n, hf _) h_directed.sequence_mono ... = ⨆ b, ∫⁻ a, f b a ∂μ : begin refine le_antisymm (supr_le $ assume n, _) (supr_le $ assume b, _), { exact le_supr (λb, ∫⁻ a, f b a ∂μ) _ }, { exact le_supr_of_le (encode b + 1) (lintegral_mono $ h_directed.le_sequence b) } end end end lemma lintegral_tsum [encodable β] {f : β → α → ennreal} (hf : ∀i, measurable (f i)) : ∫⁻ a, ∑' i, f i a ∂μ = ∑' i, ∫⁻ a, f i a ∂μ := begin simp only [ennreal.tsum_eq_supr_sum], rw [lintegral_supr_directed], { simp [lintegral_finset_sum _ hf] }, { assume b, exact finset.measurable_sum _ hf }, { assume s t, use [s ∪ t], split, exact assume a, finset.sum_le_sum_of_subset (finset.subset_union_left _ _), exact assume a, finset.sum_le_sum_of_subset (finset.subset_union_right _ _) } end open measure lemma lintegral_Union [encodable β] {s : β → set α} (hm : ∀ i, is_measurable (s i)) (hd : pairwise (disjoint on s)) (f : α → ennreal) : ∫⁻ a in ⋃ i, s i, f a ∂μ = ∑' i, ∫⁻ a in s i, f a ∂μ := by simp only [measure.restrict_Union hd hm, lintegral_sum_measure] lemma lintegral_Union_le [encodable β] (s : β → set α) (f : α → ennreal) : ∫⁻ a in ⋃ i, s i, f a ∂μ ≤ ∑' i, ∫⁻ a in s i, f a ∂μ := begin rw [← lintegral_sum_measure], exact lintegral_mono' restrict_Union_le (le_refl _) end lemma lintegral_map [measurable_space β] {f : β → ennreal} {g : α → β} (hf : measurable f) (hg : measurable g) : ∫⁻ a, f a ∂(map g μ) = ∫⁻ a, f (g a) ∂μ := begin simp only [lintegral_eq_supr_eapprox_lintegral, hf, hf.comp hg], { congr, funext n, symmetry, apply simple_func.lintegral_map, { assume a, exact congr_fun (simple_func.eapprox_comp hf hg) a }, { assume s hs, exact map_apply hg hs } }, end lemma lintegral_comp [measurable_space β] {f : β → ennreal} {g : α → β} (hf : measurable f) (hg : measurable g) : lintegral μ (f ∘ g) = ∫⁻ a, f a ∂(map g μ) := (lintegral_map hf hg).symm lemma set_lintegral_map [measurable_space β] {f : β → ennreal} {g : α → β} {s : set β} (hs : is_measurable s) (hf : measurable f) (hg : measurable g) : ∫⁻ y in s, f y ∂(map g μ) = ∫⁻ x in g ⁻¹' s, f (g x) ∂μ := by rw [restrict_map hg hs, lintegral_map hf hg] lemma lintegral_dirac (a : α) {f : α → ennreal} (hf : measurable f) : ∫⁻ a, f a ∂(dirac a) = f a := by simp [lintegral_congr_ae (eventually_eq_dirac hf)] lemma ae_lt_top {f : α → ennreal} (hf : measurable f) (h2f : ∫⁻ x, f x ∂μ < ⊤) : ∀ᵐ x ∂μ, f x < ⊤ := begin simp_rw [ae_iff, ennreal.not_lt_top], by_contra h, rw [← not_le] at h2f, apply h2f, have : (f ⁻¹' {⊤}).indicator ⊤ ≤ f, { intro x, by_cases hx : x ∈ f ⁻¹' {⊤}; [simpa [hx], simp [hx]] }, convert lintegral_mono this, rw [lintegral_indicator _ (hf (is_measurable_singleton ⊤))], simp [ennreal.top_mul, preimage, h] end /-- Given a measure `μ : measure α` and a function `f : α → ennreal`, `μ.with_density f` is the measure such that for a measurable set `s` we have `μ.with_density f s = ∫⁻ a in s, f a ∂μ`. -/ def measure.with_density (μ : measure α) (f : α → ennreal) : measure α := measure.of_measurable (λs hs, ∫⁻ a in s, f a ∂μ) (by simp) (λ s hs hd, lintegral_Union hs hd _) @[simp] lemma with_density_apply (f : α → ennreal) {s : set α} (hs : is_measurable s) : μ.with_density f s = ∫⁻ a in s, f a ∂μ := measure.of_measurable_apply s hs end lintegral end measure_theory open measure_theory measure_theory.simple_func /-- To prove something for an arbitrary measurable function into `ennreal`, it suffices to show that the property holds for (multiples of) characteristic functions and is closed under addition and supremum of increasing sequences of functions. It is possible to make the hypotheses in the induction steps a bit stronger, and such conditions can be added once we need them (for example in `h_sum` it is only necessary to consider the sum of a simple function with a multiple of a characteristic function and that the intersection of their images is a subset of `{0}`. -/ @[elab_as_eliminator] theorem measurable.ennreal_induction {α} [measurable_space α] {P : (α → ennreal) → Prop} (h_ind : ∀ (c : ennreal) ⦃s⦄, is_measurable s → P (indicator s (λ _, c))) (h_sum : ∀ ⦃f g : α → ennreal⦄, set.univ ⊆ f ⁻¹' {0} ∪ g ⁻¹' {0} → measurable f → measurable g → P f → P g → P (f + g)) (h_supr : ∀ ⦃f : ℕ → α → ennreal⦄ (hf : ∀n, measurable (f n)) (h_mono : monotone f) (hP : ∀ n, P (f n)), P (λ x, ⨆ n, f n x)) ⦃f : α → ennreal⦄ (hf : measurable f) : P f := begin convert h_supr (λ n, (eapprox f n).measurable) (monotone_eapprox f) _, { ext1 x, rw [supr_eapprox_apply f hf] }, { exact λ n, simple_func.induction (λ c s hs, h_ind c hs) (λ f g hfg hf hg, h_sum hfg f.measurable g.measurable hf hg) (eapprox f n) } end namespace measure_theory /-- This is Exercise 1.2.1 from [tao2010]. It allows you to express integration of a measurable function with respect to `(μ.with_density f)` as an integral with respect to `μ`, called the base measure. `μ` is often the Lebesgue measure, and in this circumstance `f` is the probability density function, and `(μ.with_density f)` represents any continuous random variable as a probability measure, such as the uniform distribution between 0 and 1, the Gaussian distribution, the exponential distribution, the Beta distribution, or the Cauchy distribution (see Section 2.4 of [wasserman2004]). Thus, this method shows how to one can calculate expectations, variances, and other moments as a function of the probability density function. -/ lemma lintegral_with_density_eq_lintegral_mul {α} [measurable_space α] (μ : measure α) {f : α → ennreal} (h_mf : measurable f) : ∀ {g : α → ennreal}, measurable g → ∫⁻ a, g a ∂(μ.with_density f) = ∫⁻ a, (f * g) a ∂μ := begin apply measurable.ennreal_induction, { intros c s h_ms, simp [*, mul_comm _ c] }, { intros g h h_univ h_mea_g h_mea_h h_ind_g h_ind_h, simp [mul_add, *, measurable.ennreal_mul] }, { intros g h_mea_g h_mono_g h_ind, have : monotone (λ n a, f a * g n a) := λ m n hmn x, ennreal.mul_le_mul le_rfl (h_mono_g hmn x), simp [lintegral_supr, ennreal.mul_supr, h_mf.ennreal_mul (h_mea_g _), *] } end end measure_theory
2acb031483b498cffb07219affbc6f69498437a4
246309748072bf9f8da313401699689ebbecd94d
/src/order/ord_continuous.lean
083a2bb27731b4b8049a158e19fa609a1ac418bc
[ "Apache-2.0" ]
permissive
YJMD/mathlib
b703a641e5f32a996f7842f7c0043bab2b462ee2
7310eab9fa8c1b1229dca42682f1fa6bfb7dbbf9
refs/heads/master
1,670,714,479,314
1,599,035,445,000
1,599,035,445,000
292,279,930
0
0
null
1,599,050,561,000
1,599,050,560,000
null
UTF-8
Lean
false
false
8,037
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov, Johannes Hölzl -/ import order.conditionally_complete_lattice import logic.function.iterate import order.rel_iso /-! # Order continuity We say that a function is *left order continuous* if it sends all least upper bounds to least upper bounds. The order dual notion is called *right order continuity*. For monotone functions `ℝ → ℝ` these notions correspond to the usual left and right continuity. We prove some basic lemmas (`map_sup`, `map_Sup` etc) and prove that an `rel_iso` is both left and right order continuous. -/ universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} open set function /-! ### Definitions -/ /-- A function `f` between preorders is left order continuous if it preserves all suprema. We define it using `is_lub` instead of `Sup` so that the proof works both for complete lattices and conditionally complete lattices. -/ def left_ord_continuous [preorder α] [preorder β] (f : α → β) := ∀ ⦃s : set α⦄ ⦃x⦄, is_lub s x → is_lub (f '' s) (f x) /-- A function `f` between preorders is right order continuous if it preserves all infima. We define it using `is_glb` instead of `Inf` so that the proof works both for complete lattices and conditionally complete lattices. -/ def right_ord_continuous [preorder α] [preorder β] (f : α → β) := ∀ ⦃s : set α⦄ ⦃x⦄, is_glb s x → is_glb (f '' s) (f x) namespace left_ord_continuous section preorder variables (α) [preorder α] [preorder β] [preorder γ] {g : β → γ} {f : α → β} protected lemma id : left_ord_continuous (id : α → α) := λ s x h, by simpa only [image_id] using h variable {α} protected lemma order_dual (hf : left_ord_continuous f) : @right_ord_continuous (order_dual α) (order_dual β) _ _ f := hf lemma map_is_greatest (hf : left_ord_continuous f) {s : set α} {x : α} (h : is_greatest s x): is_greatest (f '' s) (f x) := ⟨mem_image_of_mem f h.1, (hf h.is_lub).1⟩ lemma mono (hf : left_ord_continuous f) : monotone f := λ a₁ a₂ h, have is_greatest {a₁, a₂} a₂ := ⟨or.inr rfl, by simp [*]⟩, (hf.map_is_greatest this).2 $ mem_image_of_mem _ (or.inl rfl) lemma comp (hg : left_ord_continuous g) (hf : left_ord_continuous f) : left_ord_continuous (g ∘ f) := λ s x h, by simpa only [image_image] using hg (hf h) protected lemma iterate {f : α → α} (hf : left_ord_continuous f) (n : ℕ) : left_ord_continuous (f^[n]) := nat.rec_on n (left_ord_continuous.id α) $ λ n ihn, ihn.comp hf end preorder section semilattice_sup variables [semilattice_sup α] [semilattice_sup β] {f : α → β} lemma map_sup (hf : left_ord_continuous f) (x y : α) : f (x ⊔ y) = f x ⊔ f y := (hf is_lub_pair).unique $ by simp only [image_pair, is_lub_pair] lemma le_iff (hf : left_ord_continuous f) (h : injective f) {x y} : f x ≤ f y ↔ x ≤ y := by simp only [← sup_eq_right, ← hf.map_sup, h.eq_iff] lemma lt_iff (hf : left_ord_continuous f) (h : injective f) {x y} : f x < f y ↔ x < y := by simp only [lt_iff_le_not_le, hf.le_iff h] variable (f) /-- Convert an injective left order continuous function to an order embedding. -/ def to_order_embedding (hf : left_ord_continuous f) (h : injective f) : α ↪o β := ⟨⟨f, h⟩, λ x y, (hf.le_iff h).symm⟩ variable {f} @[simp] lemma coe_to_order_embedding (hf : left_ord_continuous f) (h : injective f) : ⇑(hf.to_order_embedding f h) = f := rfl end semilattice_sup section complete_lattice variables [complete_lattice α] [complete_lattice β] {f : α → β} lemma map_Sup' (hf : left_ord_continuous f) (s : set α) : f (Sup s) = Sup (f '' s) := (hf $ is_lub_Sup s).Sup_eq.symm lemma map_Sup (hf : left_ord_continuous f) (s : set α) : f (Sup s) = ⨆ x ∈ s, f x := by rw [hf.map_Sup', Sup_image] lemma map_supr (hf : left_ord_continuous f) (g : ι → α) : f (⨆ i, g i) = ⨆ i, f (g i) := by simp only [supr, hf.map_Sup', ← range_comp] end complete_lattice section conditionally_complete_lattice variables [conditionally_complete_lattice α] [conditionally_complete_lattice β] [nonempty ι] {f : α → β} lemma map_cSup (hf : left_ord_continuous f) {s : set α} (sne : s.nonempty) (sbdd : bdd_above s) : f (Sup s) = Sup (f '' s) := ((hf $ is_lub_cSup sne sbdd).cSup_eq $ sne.image f).symm lemma map_csupr (hf : left_ord_continuous f) {g : ι → α} (hg : bdd_above (range g)) : f (⨆ i, g i) = ⨆ i, f (g i) := by simp only [supr, hf.map_cSup (range_nonempty _) hg, ← range_comp] end conditionally_complete_lattice end left_ord_continuous namespace right_ord_continuous section preorder variables (α) [preorder α] [preorder β] [preorder γ] {g : β → γ} {f : α → β} protected lemma id : right_ord_continuous (id : α → α) := λ s x h, by simpa only [image_id] using h variable {α} protected lemma order_dual (hf : right_ord_continuous f) : @left_ord_continuous (order_dual α) (order_dual β) _ _ f := hf lemma map_is_least (hf : right_ord_continuous f) {s : set α} {x : α} (h : is_least s x): is_least (f '' s) (f x) := hf.order_dual.map_is_greatest h lemma mono (hf : right_ord_continuous f) : monotone f := hf.order_dual.mono.order_dual lemma comp (hg : right_ord_continuous g) (hf : right_ord_continuous f) : right_ord_continuous (g ∘ f) := hg.order_dual.comp hf.order_dual protected lemma iterate {f : α → α} (hf : right_ord_continuous f) (n : ℕ) : right_ord_continuous (f^[n]) := hf.order_dual.iterate n end preorder section semilattice_inf variables [semilattice_inf α] [semilattice_inf β] {f : α → β} lemma map_inf (hf : right_ord_continuous f) (x y : α) : f (x ⊓ y) = f x ⊓ f y := hf.order_dual.map_sup x y lemma le_iff (hf : right_ord_continuous f) (h : injective f) {x y} : f x ≤ f y ↔ x ≤ y := hf.order_dual.le_iff h lemma lt_iff (hf : right_ord_continuous f) (h : injective f) {x y} : f x < f y ↔ x < y := hf.order_dual.lt_iff h variable (f) /-- Convert an injective left order continuous function to a `order_embedding`. -/ def to_order_embedding (hf : right_ord_continuous f) (h : injective f) : α ↪o β := ⟨⟨f, h⟩, λ x y, (hf.le_iff h).symm⟩ variable {f} @[simp] lemma coe_to_order_embedding (hf : right_ord_continuous f) (h : injective f) : ⇑(hf.to_order_embedding f h) = f := rfl end semilattice_inf section complete_lattice variables [complete_lattice α] [complete_lattice β] {f : α → β} lemma map_Inf' (hf : right_ord_continuous f) (s : set α) : f (Inf s) = Inf (f '' s) := hf.order_dual.map_Sup' s lemma map_Inf (hf : right_ord_continuous f) (s : set α) : f (Inf s) = ⨅ x ∈ s, f x := hf.order_dual.map_Sup s lemma map_infi (hf : right_ord_continuous f) (g : ι → α) : f (⨅ i, g i) = ⨅ i, f (g i) := hf.order_dual.map_supr g end complete_lattice section conditionally_complete_lattice variables [conditionally_complete_lattice α] [conditionally_complete_lattice β] [nonempty ι] {f : α → β} lemma map_cInf (hf : right_ord_continuous f) {s : set α} (sne : s.nonempty) (sbdd : bdd_below s) : f (Inf s) = Inf (f '' s) := hf.order_dual.map_cSup sne sbdd lemma map_cinfi (hf : right_ord_continuous f) {g : ι → α} (hg : bdd_below (range g)) : f (⨅ i, g i) = ⨅ i, f (g i) := hf.order_dual.map_csupr hg end conditionally_complete_lattice end right_ord_continuous namespace order_iso section preorder variables [preorder α] [preorder β] (e : α ≃o β) {s : set α} {x : α} protected lemma left_ord_continuous : left_ord_continuous e := λ s x hx, ⟨monotone.mem_upper_bounds_image (λ x y, e.map_rel_iff.1) hx.1, λ y hy, e.rel_symm_apply.1 $ (is_lub_le_iff hx).2 $ λ x' hx', e.rel_symm_apply.2 $ hy $ mem_image_of_mem _ hx'⟩ protected lemma right_ord_continuous : right_ord_continuous e := order_iso.left_ord_continuous e.osymm end preorder end order_iso
194e5a7f4892722112e0da92abfa582cb7387d2e
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/finset/finsupp.lean
79b9b47bdf144f5635d8c3363ace4879de381289
[ "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,241
lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import algebra.big_operators.finsupp import data.finset.pointwise import data.finsupp.indicator import data.fintype.big_operators /-! # Finitely supported product of finsets This file defines the finitely supported product of finsets as a `finset (ι →₀ α)`. ## Main declarations * `finset.finsupp`: Finitely supported product of finsets. `s.finset t` is the product of the `t i` over all `i ∈ s`. * `finsupp.pi`: `f.pi` is the finset of `finsupp`s whose `i`-th value lies in `f i`. This is the special case of `finset.finsupp` where we take the product of the `f i` over the support of `f`. ## Implementation notes We make heavy use of the fact that `0 : finset α` is `{0}`. This scalar actions convention turns out to be precisely what we want here too. -/ noncomputable theory open finsupp open_locale big_operators classical pointwise variables {ι α : Type*} [has_zero α] {s : finset ι} {f : ι →₀ α} namespace finset /-- Finitely supported product of finsets. -/ protected def finsupp (s : finset ι) (t : ι → finset α) : finset (ι →₀ α) := (s.pi t).map ⟨indicator s, indicator_injective s⟩ lemma mem_finsupp_iff {t : ι → finset α} : f ∈ s.finsupp t ↔ f.support ⊆ s ∧ ∀ i ∈ s, f i ∈ t i := begin refine mem_map.trans ⟨_, _⟩, { rintro ⟨f, hf, rfl⟩, refine ⟨support_indicator_subset _ _, λ i hi, _⟩, convert mem_pi.1 hf i hi, exact indicator_of_mem hi _ }, { refine λ h, ⟨λ i _, f i, mem_pi.2 h.2, _⟩, ext i, exact ite_eq_left_iff.2 (λ hi, (not_mem_support_iff.1 $ λ H, hi $ h.1 H).symm) } end /-- When `t` is supported on `s`, `f ∈ s.finsupp t` precisely means that `f` is pointwise in `t`. -/ @[simp] lemma mem_finsupp_iff_of_support_subset {t : ι →₀ finset α} (ht : t.support ⊆ s) : f ∈ s.finsupp t ↔ ∀ i, f i ∈ t i := begin refine mem_finsupp_iff.trans (forall_and_distrib.symm.trans $ forall_congr $ λ i, ⟨λ h, _, λ h, ⟨λ hi, ht $ mem_support_iff.2 $ λ H, mem_support_iff.1 hi _, λ _, h⟩⟩), { by_cases hi : i ∈ s, { exact h.2 hi }, { rw [not_mem_support_iff.1 (mt h.1 hi), not_mem_support_iff.1 (λ H, hi $ ht H)], exact zero_mem_zero } }, { rwa [H, mem_zero] at h } end @[simp] lemma card_finsupp (s : finset ι) (t : ι → finset α) : (s.finsupp t).card = ∏ i in s, (t i).card := (card_map _).trans $ card_pi _ _ end finset open finset namespace finsupp /-- Given a finitely supported function `f : ι →₀ finset α`, one can define the finset `f.pi` of all finitely supported functions whose value at `i` is in `f i` for all `i`. -/ def pi (f : ι →₀ finset α) : finset (ι →₀ α) := f.support.finsupp f @[simp] lemma mem_pi {f : ι →₀ finset α} {g : ι →₀ α} : g ∈ f.pi ↔ ∀ i, g i ∈ f i := mem_finsupp_iff_of_support_subset $ subset.refl _ @[simp] lemma card_pi (f : ι →₀ finset α) : f.pi.card = f.prod (λ i, (f i).card) := begin rw [pi, card_finsupp], exact finset.prod_congr rfl (λ i _, by simp only [pi.nat_apply, nat.cast_id]), end end finsupp
38c92e2bb8e5067c2c3bfd2d47d4af45eee5b5ee
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/group/unique_prods.lean
3fc479cc41994a0c898b73b8fa769426d91aa543
[ "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
9,339
lean
/- Copyright (c) 2022 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import data.finset.preimage /-! # Unique products and related notions > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. A group `G` has *unique products* if for any two non-empty finite subsets `A, B ⊂ G`, there is an element `g ∈ A * B` that can be written uniquely as a product of an element of `A` and an element of `B`. We call the formalization this property `unique_prods`. Since the condition requires no property of the group operation, we define it for a Type simply satisfying `has_mul`. We also introduce the analogous "additive" companion, `unique_sums` and link the two so that `to_additive` converts `unique_prods` into `unique_sums`. Here you can see several examples of Types that have `unique_sums/prods` (`apply_instance` uses `covariants.to_unique_prods` and `covariants.to_unique_sums`). ```lean import data.real.basic example : unique_sums ℕ := by apply_instance example : unique_sums ℕ+ := by apply_instance example : unique_sums ℤ := by apply_instance example : unique_sums ℚ := by apply_instance example : unique_sums ℝ := by apply_instance example : unique_prods ℕ+ := by apply_instance ``` -/ /-- Let `G` be a Type with multiplication, let `A B : finset G` be finite subsets and let `a0 b0 : G` be two elements. `unique_mul A B a0 b0` asserts `a0 * b0` can be written in at most one way as a product of an element of `A` and an element of `B`. -/ @[to_additive "Let `G` be a Type with addition, let `A B : finset G` be finite subsets and let `a0 b0 : G` be two elements. `unique_add A B a0 b0` asserts `a0 + b0` can be written in at most one way as a sum of an element from `A` and an element from `B`."] def unique_mul {G} [has_mul G] (A B : finset G) (a0 b0 : G) : Prop := ∀ ⦃a b⦄, a ∈ A → b ∈ B → a * b = a0 * b0 → a = a0 ∧ b = b0 namespace unique_mul variables {G H : Type*} [has_mul G] [has_mul H] {A B : finset G} {a0 b0 : G} lemma mt {G} [has_mul G] {A B : finset G} {a0 b0 : G} (h : unique_mul A B a0 b0) : ∀ ⦃a b⦄, a ∈ A → b ∈ B → a ≠ a0 ∨ b ≠ b0 → a * b ≠ a0 * b0 := λ _ _ ha hb k, by { contrapose! k, exact h ha hb k } @[to_additive] lemma subsingleton (A B : finset G) (a0 b0 : G) (h : unique_mul A B a0 b0) : subsingleton { ab : G × G // ab.1 ∈ A ∧ ab.2 ∈ B ∧ ab.1 * ab.2 = a0 * b0 } := ⟨λ ⟨⟨a, b⟩, ha, hb, ab⟩ ⟨⟨a', b'⟩, ha', hb', ab'⟩, subtype.ext $ prod.ext ((h ha hb ab).1.trans (h ha' hb' ab').1.symm) $ (h ha hb ab).2.trans (h ha' hb' ab').2.symm⟩ @[to_additive] lemma set_subsingleton (A B : finset G) (a0 b0 : G) (h : unique_mul A B a0 b0) : set.subsingleton { ab : G × G | ab.1 ∈ A ∧ ab.2 ∈ B ∧ ab.1 * ab.2 = a0 * b0 } := begin rintros ⟨x1, y1⟩ (hx : x1 ∈ A ∧ y1 ∈ B ∧ x1 * y1 = a0 * b0) ⟨x2, y2⟩ (hy : x2 ∈ A ∧ y2 ∈ B ∧ x2 * y2 = a0 * b0), rcases h hx.1 hx.2.1 hx.2.2 with ⟨rfl, rfl⟩, rcases h hy.1 hy.2.1 hy.2.2 with ⟨rfl, rfl⟩, refl, end @[to_additive] lemma iff_exists_unique (aA : a0 ∈ A) (bB : b0 ∈ B) : unique_mul A B a0 b0 ↔ ∃! ab ∈ A ×ˢ B, ab.1 * ab.2 = a0 * b0 := ⟨λ _, ⟨(a0, b0), ⟨finset.mem_product.mpr ⟨aA, bB⟩, rfl, by simp⟩, by simpa⟩, λ h, h.elim2 begin rintro ⟨x1, x2⟩ _ _ J x y hx hy l, rcases prod.mk.inj_iff.mp (J (a0,b0) (finset.mk_mem_product aA bB) rfl) with ⟨rfl, rfl⟩, exact prod.mk.inj_iff.mp (J (x,y) (finset.mk_mem_product hx hy) l), end⟩ @[to_additive] lemma exists_iff_exists_exists_unique : (∃ a0 b0 : G, a0 ∈ A ∧ b0 ∈ B ∧ unique_mul A B a0 b0) ↔ ∃ g : G, ∃! ab ∈ A ×ˢ B, ab.1 * ab.2 = g := ⟨λ ⟨a0, b0, hA, hB, h⟩, ⟨_, (iff_exists_unique hA hB).mp h⟩, λ ⟨g, h⟩, begin have h' := h, rcases h' with ⟨⟨a,b⟩, ⟨hab, rfl, -⟩, -⟩, cases finset.mem_product.mp hab with ha hb, exact ⟨a, b, ha, hb, (iff_exists_unique ha hb).mpr h⟩, end⟩ /-- `unique_mul` is preserved by inverse images under injective, multiplicative maps. -/ @[to_additive "`unique_add` is preserved by inverse images under injective, additive maps."] lemma mul_hom_preimage (f : G →ₙ* H) (hf : function.injective f) (a0 b0 : G) {A B : finset H} (u : unique_mul A B (f a0) (f b0)) : unique_mul (A.preimage f (set.inj_on_of_injective hf _)) (B.preimage f (set.inj_on_of_injective hf _)) a0 b0 := begin intros a b ha hb ab, rw [← hf.eq_iff, ← hf.eq_iff], rw [← hf.eq_iff, map_mul, map_mul] at ab, exact u (finset.mem_preimage.mp ha) (finset.mem_preimage.mp hb) ab, end /-- `unique_mul` is preserved under multiplicative maps that are injective. See `unique_mul.mul_hom_map_iff` for a version with swapped bundling. -/ @[to_additive "`unique_add` is preserved under additive maps that are injective. See `unique_add.add_hom_map_iff` for a version with swapped bundling."] lemma mul_hom_image_iff [decidable_eq H] (f : G →ₙ* H) (hf : function.injective f) : unique_mul (A.image f) (B.image f) (f a0) (f b0) ↔ unique_mul A B a0 b0 := begin refine ⟨λ h, _, λ h, _⟩, { intros a b ha hb ab, rw [← hf.eq_iff, ← hf.eq_iff], rw [← hf.eq_iff, map_mul, map_mul] at ab, exact h (finset.mem_image.mpr ⟨_, ha, rfl⟩) (finset.mem_image.mpr ⟨_, hb, rfl⟩) ab}, { intros a b aA bB ab, obtain ⟨a, ha, rfl⟩ : ∃ a' ∈ A, f a' = a := finset.mem_image.mp aA, obtain ⟨b, hb, rfl⟩ : ∃ b' ∈ B, f b' = b := finset.mem_image.mp bB, rw [hf.eq_iff, hf.eq_iff], rw [← map_mul, ← map_mul, hf.eq_iff] at ab, exact h ha hb ab }, end /-- `unique_mul` is preserved under embeddings that are multiplicative. See `unique_mul.mul_hom_image_iff` for a version with swapped bundling. -/ @[to_additive "`unique_add` is preserved under embeddings that are additive. See `unique_add.add_hom_image_iff` for a version with swapped bundling."] lemma mul_hom_map_iff (f : G ↪ H) (mul : ∀ x y, f (x * y) = f x * f y) : unique_mul (A.map f) (B.map f) (f a0) (f b0) ↔ unique_mul A B a0 b0 := begin classical, convert mul_hom_image_iff ⟨f, mul⟩ f.2; { ext, simp only [finset.mem_map, mul_hom.coe_mk, finset.mem_image] }, end end unique_mul /-- Let `G` be a Type with addition. `unique_sums G` asserts that any two non-empty finite subsets of `A` have the `unique_add` property, with respect to some element of their sum `A + B`. -/ class unique_sums (G) [has_add G] : Prop := (unique_add_of_nonempty : ∀ {A B : finset G} (hA : A.nonempty) (hB : B.nonempty), ∃ (a0 ∈ A) (b0 ∈ B), unique_add A B a0 b0) /-- Let `G` be a Type with multiplication. `unique_prods G` asserts that any two non-empty finite subsets of `G` have the `unique_mul` property, with respect to some element of their product `A * B`. -/ class unique_prods (G) [has_mul G] : Prop := (unique_mul_of_nonempty : ∀ {A B : finset G} (hA : A.nonempty) (hB : B.nonempty), ∃ (a0 ∈ A) (b0 ∈ B), unique_mul A B a0 b0) attribute [to_additive] unique_prods namespace multiplicative instance {M} [has_add M] [unique_sums M] : unique_prods (multiplicative M) := { unique_mul_of_nonempty := λ A B hA hB, let A' : finset M := A in have hA': A'.nonempty := hA, by { obtain ⟨a0, hA0, b0, hB0, J⟩ := unique_sums.unique_add_of_nonempty hA' hB, exact ⟨of_add a0, hA0, of_add b0, hB0, λ a b aA bB H, J aA bB H⟩ } } end multiplicative namespace additive instance {M} [has_mul M] [unique_prods M] : unique_sums (additive M) := { unique_add_of_nonempty := λ A B hA hB, let A' : finset M := A in have hA': A'.nonempty := hA, by { obtain ⟨a0, hA0, b0, hB0, J⟩ := unique_prods.unique_mul_of_nonempty hA' hB, exact ⟨of_mul a0, hA0, of_mul b0, hB0, λ a b aA bB H, J aA bB H⟩ } } end additive @[to_additive] lemma eq_and_eq_of_le_of_le_of_mul_le {A} [has_mul A] [linear_order A] [covariant_class A A (*) (≤)] [covariant_class A A (function.swap (*)) (<)] [contravariant_class A A (*) (≤)] {a b a0 b0 : A} (ha : a0 ≤ a) (hb : b0 ≤ b) (ab : a * b ≤ a0 * b0) : a = a0 ∧ b = b0 := begin haveI := has_mul.to_covariant_class_right A, have ha' : ¬a0 * b0 < a * b → ¬a0 < a := mt (λ h, mul_lt_mul_of_lt_of_le h hb), have hb' : ¬a0 * b0 < a * b → ¬b0 < b := mt (λ h, mul_lt_mul_of_le_of_lt ha h), push_neg at ha' hb', exact ⟨ha.antisymm' (ha' ab), hb.antisymm' (hb' ab)⟩, end /-- This instance asserts that if `A` has a multiplication, a linear order, and multiplication is 'very monotone', then `A` also has `unique_prods`. -/ @[priority 100, -- see Note [lower instance priority] to_additive "This instance asserts that if `A` has an addition, a linear order, and addition is 'very monotone', then `A` also has `unique_sums`."] instance covariants.to_unique_prods {A} [has_mul A] [linear_order A] [covariant_class A A (*) (≤)] [covariant_class A A (function.swap (*)) (<)] [contravariant_class A A (*) (≤)] : unique_prods A := { unique_mul_of_nonempty := λ A B hA hB, ⟨_, A.min'_mem ‹_›, _, B.min'_mem ‹_›, λ a b ha hb ab, eq_and_eq_of_le_of_le_of_mul_le (finset.min'_le _ _ ‹_›) (finset.min'_le _ _ ‹_›) ab.le⟩ }
1b64ba4e3706296fc3f0a7f9b905d137128f209f
94e33a31faa76775069b071adea97e86e218a8ee
/src/topology/shrinking_lemma.lean
7c346e2dd844338ec1a07b46f59a8f2ea293b883
[ "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
12,196
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Reid Barton -/ import topology.separation /-! # The shrinking lemma In this file we prove a few versions of the shrinking lemma. The lemma says that in a normal topological space a point finite open covering can be “shrunk”: for a point finite open covering `u : ι → set X` there exists a refinement `v : ι → set X` such that `closure (v i) ⊆ u i`. For finite or countable coverings this lemma can be proved without the axiom of choice, see [ncatlab](https://ncatlab.org/nlab/show/shrinking+lemma) for details. We only formalize the most general result that works for any covering but needs the axiom of choice. We prove two versions of the lemma: * `exists_subset_Union_closure_subset` deals with a covering of a closed set in a normal space; * `exists_Union_eq_closure_subset` deals with a covering of the whole space. ## Tags normal space, shrinking lemma -/ open set function open_locale classical noncomputable theory variables {ι X : Type*} [topological_space X] [normal_space X] namespace shrinking_lemma /-- Auxiliary definition for the proof of `shrinking_lemma`. A partial refinement of a covering `⋃ i, u i` of a set `s` is a map `v : ι → set X` and a set `carrier : set ι` such that * `s ⊆ ⋃ i, v i`; * all `v i` are open; * if `i ∈ carrier v`, then `closure (v i) ⊆ u i`; * if `i ∉ carrier`, then `v i = u i`. This type is equipped with the folowing partial order: `v ≤ v'` if `v.carrier ⊆ v'.carrier` and `v i = v' i` for `i ∈ v.carrier`. We will use Zorn's lemma to prove that this type has a maximal element, then show that the maximal element must have `carrier = univ`. -/ @[nolint has_inhabited_instance] -- the trivial refinement needs `u` to be a covering structure partial_refinement (u : ι → set X) (s : set X) := (to_fun : ι → set X) (carrier : set ι) (is_open' : ∀ i, is_open (to_fun i)) (subset_Union' : s ⊆ ⋃ i, to_fun i) (closure_subset' : ∀ i ∈ carrier, closure (to_fun i) ⊆ (u i)) (apply_eq' : ∀ i ∉ carrier, to_fun i = u i) namespace partial_refinement variables {u : ι → set X} {s : set X} instance : has_coe_to_fun (partial_refinement u s) (λ _, ι → set X) := ⟨to_fun⟩ lemma subset_Union (v : partial_refinement u s) : s ⊆ ⋃ i, v i := v.subset_Union' lemma closure_subset (v : partial_refinement u s) {i : ι} (hi : i ∈ v.carrier) : closure (v i) ⊆ (u i) := v.closure_subset' i hi lemma apply_eq (v : partial_refinement u s) {i : ι} (hi : i ∉ v.carrier) : v i = u i := v.apply_eq' i hi protected lemma is_open (v : partial_refinement u s) (i : ι) : is_open (v i) := v.is_open' i protected lemma subset (v : partial_refinement u s) (i : ι) : v i ⊆ u i := if h : i ∈ v.carrier then subset.trans subset_closure (v.closure_subset h) else (v.apply_eq h).le attribute [ext] partial_refinement instance : partial_order (partial_refinement u s) := { le := λ v₁ v₂, v₁.carrier ⊆ v₂.carrier ∧ ∀ i ∈ v₁.carrier, v₁ i = v₂ i, le_refl := λ v, ⟨subset.refl _, λ _ _, rfl⟩, le_trans := λ v₁ v₂ v₃ h₁₂ h₂₃, ⟨subset.trans h₁₂.1 h₂₃.1, λ i hi, (h₁₂.2 i hi).trans (h₂₃.2 i $ h₁₂.1 hi)⟩, le_antisymm := λ v₁ v₂ h₁₂ h₂₁, have hc : v₁.carrier = v₂.carrier, from subset.antisymm h₁₂.1 h₂₁.1, ext _ _ (funext $ λ x, if hx : x ∈ v₁.carrier then h₁₂.2 _ hx else (v₁.apply_eq hx).trans (eq.symm $ v₂.apply_eq $ hc ▸ hx)) hc } /-- If two partial refinements `v₁`, `v₂` belong to a chain (hence, they are comparable) and `i` belongs to the carriers of both partial refinements, then `v₁ i = v₂ i`. -/ lemma apply_eq_of_chain {c : set (partial_refinement u s)} (hc : is_chain (≤) c) {v₁ v₂} (h₁ : v₁ ∈ c) (h₂ : v₂ ∈ c) {i} (hi₁ : i ∈ v₁.carrier) (hi₂ : i ∈ v₂.carrier) : v₁ i = v₂ i := begin wlog hle : v₁ ≤ v₂ := hc.total h₁ h₂ using [v₁ v₂, v₂ v₁], exact hle.2 _ hi₁, end /-- The carrier of the least upper bound of a non-empty chain of partial refinements is the union of their carriers. -/ def chain_Sup_carrier (c : set (partial_refinement u s)) : set ι := ⋃ v ∈ c, carrier v /-- Choice of an element of a nonempty chain of partial refinements. If `i` belongs to one of `carrier v`, `v ∈ c`, then `find c ne i` is one of these partial refinements. -/ def find (c : set (partial_refinement u s)) (ne : c.nonempty) (i : ι) : partial_refinement u s := if hi : ∃ v ∈ c, i ∈ carrier v then hi.some else ne.some lemma find_mem {c : set (partial_refinement u s)} (i : ι) (ne : c.nonempty) : find c ne i ∈ c := by { rw find, split_ifs, exacts [h.some_spec.fst, ne.some_spec] } lemma mem_find_carrier_iff {c : set (partial_refinement u s)} {i : ι} (ne : c.nonempty) : i ∈ (find c ne i).carrier ↔ i ∈ chain_Sup_carrier c := begin rw find, split_ifs, { have : i ∈ h.some.carrier ∧ i ∈ chain_Sup_carrier c, from ⟨h.some_spec.snd, mem_Union₂.2 h⟩, simp only [this] }, { have : i ∉ ne.some.carrier ∧ i ∉ chain_Sup_carrier c, from ⟨λ hi, h ⟨_, ne.some_spec, hi⟩, mt mem_Union₂.1 h⟩, simp only [this] } end lemma find_apply_of_mem {c : set (partial_refinement u s)} (hc : is_chain (≤) c) (ne : c.nonempty) {i v} (hv : v ∈ c) (hi : i ∈ carrier v) : find c ne i i = v i := apply_eq_of_chain hc (find_mem _ _) hv ((mem_find_carrier_iff _).2 $ mem_Union₂.2 ⟨v, hv, hi⟩) hi /-- Least upper bound of a nonempty chain of partial refinements. -/ def chain_Sup (c : set (partial_refinement u s)) (hc : is_chain (≤) c) (ne : c.nonempty) (hfin : ∀ x ∈ s, {i | x ∈ u i}.finite) (hU : s ⊆ ⋃ i, u i) : partial_refinement u s := begin refine ⟨λ i, find c ne i i, chain_Sup_carrier c, λ i, (find _ _ _).is_open i, λ x hxs, mem_Union.2 _, λ i hi, (find c ne i).closure_subset ((mem_find_carrier_iff _).2 hi), λ i hi, (find c ne i).apply_eq (mt (mem_find_carrier_iff _).1 hi)⟩, rcases em (∃ i ∉ chain_Sup_carrier c, x ∈ u i) with ⟨i, hi, hxi⟩|hx, { use i, rwa (find c ne i).apply_eq (mt (mem_find_carrier_iff _).1 hi) }, { simp_rw [not_exists, not_imp_not, chain_Sup_carrier, mem_Union₂] at hx, haveI : nonempty (partial_refinement u s) := ⟨ne.some⟩, choose! v hvc hiv using hx, rcases (hfin x hxs).exists_maximal_wrt v _ (mem_Union.1 (hU hxs)) with ⟨i, hxi : x ∈ u i, hmax : ∀ j, x ∈ u j → v i ≤ v j → v i = v j⟩, rcases mem_Union.1 ((v i).subset_Union hxs) with ⟨j, hj⟩, use j, have hj' : x ∈ u j := (v i).subset _ hj, have : v j ≤ v i, from (hc.total (hvc _ hxi) (hvc _ hj')).elim (λ h, (hmax j hj' h).ge) id, rwa find_apply_of_mem hc ne (hvc _ hxi) (this.1 $ hiv _ hj') } end /-- `chain_Sup hu c hc ne hfin hU` is an upper bound of the chain `c`. -/ lemma le_chain_Sup {c : set (partial_refinement u s)} (hc : is_chain (≤) c) (ne : c.nonempty) (hfin : ∀ x ∈ s, {i | x ∈ u i}.finite) (hU : s ⊆ ⋃ i, u i) {v} (hv : v ∈ c) : v ≤ chain_Sup c hc ne hfin hU := ⟨λ i hi, mem_bUnion hv hi, λ i hi, (find_apply_of_mem hc _ hv hi).symm⟩ /-- If `s` is a closed set, `v` is a partial refinement, and `i` is an index such that `i ∉ v.carrier`, then there exists a partial refinement that is strictly greater than `v`. -/ lemma exists_gt (v : partial_refinement u s) (hs : is_closed s) (i : ι) (hi : i ∉ v.carrier) : ∃ v' : partial_refinement u s, v < v' := begin have I : s ∩ (⋂ j ≠ i, (v j)ᶜ) ⊆ v i, { simp only [subset_def, mem_inter_eq, mem_Inter, and_imp], intros x hxs H, rcases mem_Union.1 (v.subset_Union hxs) with ⟨j, hj⟩, exact (em (j = i)).elim (λ h, h ▸ hj) (λ h, (H j h hj).elim) }, have C : is_closed (s ∩ (⋂ j ≠ i, (v j)ᶜ)), from is_closed.inter hs (is_closed_bInter $ λ _ _, is_closed_compl_iff.2 $ v.is_open _), rcases normal_exists_closure_subset C (v.is_open i) I with ⟨vi, ovi, hvi, cvi⟩, refine ⟨⟨update v i vi, insert i v.carrier, _, _, _, _⟩, _, _⟩, { intro j, by_cases h : j = i; simp [h, ovi, v.is_open] }, { refine λ x hx, mem_Union.2 _, rcases em (∃ j ≠ i, x ∈ v j) with ⟨j, hji, hj⟩|h, { use j, rwa update_noteq hji }, { push_neg at h, use i, rw update_same, exact hvi ⟨hx, mem_bInter h⟩ } }, { rintro j (rfl|hj), { rwa [update_same, ← v.apply_eq hi] }, { rw update_noteq (ne_of_mem_of_not_mem hj hi), exact v.closure_subset hj } }, { intros j hj, rw [mem_insert_iff, not_or_distrib] at hj, rw [update_noteq hj.1, v.apply_eq hj.2] }, { refine ⟨subset_insert _ _, λ j hj, _⟩, exact (update_noteq (ne_of_mem_of_not_mem hj hi) _ _).symm }, { exact λ hle, hi (hle.1 $ mem_insert _ _) } end end partial_refinement end shrinking_lemma open shrinking_lemma variables {u : ι → set X} {s : set X} /-- Shrinking lemma. A point-finite open cover of a closed subset of a normal space can be "shrunk" to a new open cover so that the closure of each new open set is contained in the corresponding original open set. -/ lemma exists_subset_Union_closure_subset (hs : is_closed s) (uo : ∀ i, is_open (u i)) (uf : ∀ x ∈ s, {i | x ∈ u i}.finite) (us : s ⊆ ⋃ i, u i) : ∃ v : ι → set X, s ⊆ Union v ∧ (∀ i, is_open (v i)) ∧ ∀ i, closure (v i) ⊆ u i := begin classical, haveI : nonempty (partial_refinement u s) := ⟨⟨u, ∅, uo, us, λ _, false.elim, λ _ _, rfl⟩⟩, have : ∀ c : set (partial_refinement u s), is_chain (≤) c → c.nonempty → ∃ ub, ∀ v ∈ c, v ≤ ub, from λ c hc ne, ⟨partial_refinement.chain_Sup c hc ne uf us, λ v hv, partial_refinement.le_chain_Sup _ _ _ _ hv⟩, rcases zorn_nonempty_partial_order this with ⟨v, hv⟩, suffices : ∀ i, i ∈ v.carrier, from ⟨v, v.subset_Union, λ i, v.is_open _, λ i, v.closure_subset (this i)⟩, contrapose! hv, rcases hv with ⟨i, hi⟩, rcases v.exists_gt hs i hi with ⟨v', hlt⟩, exact ⟨v', hlt.le, hlt.ne'⟩ end /-- Shrinking lemma. A point-finite open cover of a closed subset of a normal space can be "shrunk" to a new closed cover so that each new closed set is contained in the corresponding original open set. See also `exists_subset_Union_closure_subset` for a stronger statement. -/ lemma exists_subset_Union_closed_subset (hs : is_closed s) (uo : ∀ i, is_open (u i)) (uf : ∀ x ∈ s, {i | x ∈ u i}.finite) (us : s ⊆ ⋃ i, u i) : ∃ v : ι → set X, s ⊆ Union v ∧ (∀ i, is_closed (v i)) ∧ ∀ i, v i ⊆ u i := let ⟨v, hsv, hvo, hv⟩ := exists_subset_Union_closure_subset hs uo uf us in ⟨λ i, closure (v i), subset.trans hsv (Union_mono $ λ i, subset_closure), λ i, is_closed_closure, hv⟩ /-- Shrinking lemma. A point-finite open cover of a closed subset of a normal space can be "shrunk" to a new open cover so that the closure of each new open set is contained in the corresponding original open set. -/ lemma exists_Union_eq_closure_subset (uo : ∀ i, is_open (u i)) (uf : ∀ x, {i | x ∈ u i}.finite) (uU : (⋃ i, u i) = univ) : ∃ v : ι → set X, Union v = univ ∧ (∀ i, is_open (v i)) ∧ ∀ i, closure (v i) ⊆ u i := let ⟨v, vU, hv⟩ := exists_subset_Union_closure_subset is_closed_univ uo (λ x _, uf x) uU.ge in ⟨v, univ_subset_iff.1 vU, hv⟩ /-- Shrinking lemma. A point-finite open cover of a closed subset of a normal space can be "shrunk" to a new closed cover so that each of the new closed sets is contained in the corresponding original open set. See also `exists_Union_eq_closure_subset` for a stronger statement. -/ lemma exists_Union_eq_closed_subset (uo : ∀ i, is_open (u i)) (uf : ∀ x, {i | x ∈ u i}.finite) (uU : (⋃ i, u i) = univ) : ∃ v : ι → set X, Union v = univ ∧ (∀ i, is_closed (v i)) ∧ ∀ i, v i ⊆ u i := let ⟨v, vU, hv⟩ := exists_subset_Union_closed_subset is_closed_univ uo (λ x _, uf x) uU.ge in ⟨v, univ_subset_iff.1 vU, hv⟩
0b566dd7442a7e262e2050fc1da23702fd4ac986
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/tests/lean/run/toExpr.lean
4c7dacc419a40a0e815cc4b46a9c848cc2e79a7e
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
821
lean
import Init.Lean open Lean unsafe def test {α : Type} [HasToString α] [ToExpr α] [HasBeq α] (a : α) : MetaIO Unit := do env ← MetaIO.getEnv; let auxName := `_toExpr._test; let decl := Declaration.defnDecl { name := auxName, lparams := [], value := toExpr a, type := toTypeExpr α, hints := ReducibilityHints.abbrev, isUnsafe := false }; IO.println (toExpr a); match env.addAndCompile {} decl with | Except.error _ => throw $ IO.userError "addDecl failed" | Except.ok env => do match env.evalConst α auxName with | Except.error ex => throw $ IO.userError ex | Except.ok b => do IO.println b; unless (a == b) $ throw $ IO.userError "toExpr failed"; pure () #eval test #[(1, 2), (3, 4)] #eval test ['a', 'b', 'c'] #eval test ("hello", true) #eval test ((), 10)
5c580b2d95b0b8ce732109e4329ef8feb3f4feb8
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/category_theory/sites/sheaf_of_types.lean
739d9e26129c922cbfa45367ab143d891d50bb4d
[ "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
40,787
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.sites.pretopology import category_theory.limits.shapes.types import category_theory.full_subcategory /-! # Sheaves of types on a Grothendieck topology Defines the notion of a sheaf of types (usually called a sheaf of sets by mathematicians) on a category equipped with a Grothendieck topology, as well as a range of equivalent conditions useful in different situations. First define what it means for a presheaf `P : Cᵒᵖ ⥤ Type v` to be a sheaf *for* a particular presieve `R` on `X`: * A *family of elements* `x` for `P` at `R` is an element `x_f` of `P Y` for every `f : Y ⟶ X` in `R`. See `family_of_elements`. * The family `x` is *compatible* if, for any `f₁ : Y₁ ⟶ X` and `f₂ : Y₂ ⟶ X` both in `R`, and any `g₁ : Z ⟶ Y₁` and `g₂ : Z ⟶ Y₂` such that `g₁ ≫ f₁ = g₂ ≫ f₂`, the restriction of `x_f₁` along `g₁` agrees with the restriction of `x_f₂` along `g₂`. See `family_of_elements.compatible`. * An *amalgamation* `t` for the family is an element of `P X` such that for every `f : Y ⟶ X` in `R`, the restriction of `t` on `f` is `x_f`. See `family_of_elements.is_amalgamation`. We then say `P` is *separated* for `R` if every compatible family has at most one amalgamation, and it is a *sheaf* for `R` if every compatible family has a unique amalgamation. See `is_separated_for` and `is_sheaf_for`. In the special case where `R` is a sieve, the compatibility condition can be simplified: * The family `x` is *compatible* if, for any `f : Y ⟶ X` in `R` and `g : Z ⟶ Y`, the restriction of `x_f` along `g` agrees with `x_(g ≫ f)` (which is well defined since `g ≫ f` is in `R`). See `family_of_elements.sieve_compatible` and `compatible_iff_sieve_compatible`. In the special case where `C` has pullbacks, the compatibility condition can be simplified: * The family `x` is *compatible* if, for any `f : Y ⟶ X` and `g : Z ⟶ X` both in `R`, the restriction of `x_f` along `π₁ : pullback f g ⟶ Y` agrees with the restriction of `x_g` along `π₂ : pullback f g ⟶ Z`. See `family_of_elements.pullback_compatible` and `pullback_compatible_iff`. Now given a Grothendieck topology `J`, `P` is a sheaf if it is a sheaf for every sieve in the topology. See `is_sheaf`. In the case where the topology is generated by a basis, it suffices to check `P` is a sheaf for every presieve in the pretopology. See `is_sheaf_pretopology`. We also provide equivalent conditions to satisfy alternate definitions given in the literature. * Stacks: In `equalizer.presieve.sheaf_condition`, the sheaf condition at a presieve is shown to be equivalent to that of https://stacks.math.columbia.edu/tag/00VM (and combined with `is_sheaf_pretopology`, this shows the notions of `is_sheaf` are exactly equivalent.) The condition of https://stacks.math.columbia.edu/tag/00Z8 is virtually identical to the statement of `yoneda_condition_iff_sheaf_condition` (since the bijection described there carries the same information as the unique existence.) * Maclane-Moerdijk [MM92]: Using `compatible_iff_sieve_compatible`, the definitions of `is_sheaf` are equivalent. There are also alternate definitions given: - Yoneda condition: Defined in `yoneda_sheaf_condition` and equivalence in `yoneda_condition_iff_sheaf_condition`. - Equalizer condition (Equation 3): Defined in the `equalizer.sieve` namespace, and equivalence in `equalizer.sieve.sheaf_condition`. - Matching family for presieves with pullback: `pullback_compatible_iff`. - Sheaf for a pretopology (Prop 1): `is_sheaf_pretopology` combined with the previous. - Sheaf for a pretopology as equalizer (Prop 1, bis): `equalizer.presieve.sheaf_condition` combined with the previous. ## Implementation The sheaf condition is given as a proposition, rather than a subsingleton in `Type (max u₁ v)`. This doesn't seem to make a big difference, other than making a couple of definitions noncomputable, but it means that equivalent conditions can be given as `↔` statements rather than `≃` statements, which can be convenient. ## References * [MM92]: *Sheaves in geometry and logic*, Saunders MacLane, and Ieke Moerdijk: Chapter III, Section 4. * [Elephant]: *Sketches of an Elephant*, P. T. Johnstone: C2.1. * https://stacks.math.columbia.edu/tag/00VL (sheaves on a pretopology or site) * https://stacks.math.columbia.edu/tag/00ZB (sheaves on a topology) -/ universes w v₁ v₂ u₁ u₂ namespace category_theory open opposite category_theory category limits sieve namespace presieve variables {C : Type u₁} [category.{v₁} C] variables {P Q U : Cᵒᵖ ⥤ Type w} variables {X Y : C} {S : sieve X} {R : presieve X} variables (J J₂ : grothendieck_topology C) /-- A family of elements for a presheaf `P` given a collection of arrows `R` with fixed codomain `X` consists of an element of `P Y` for every `f : Y ⟶ X` in `R`. A presheaf is a sheaf (resp, separated) if every *compatible* family of elements has exactly one (resp, at most one) amalgamation. This data is referred to as a `family` in [MM92], Chapter III, Section 4. It is also a concrete version of the elements of the middle object in https://stacks.math.columbia.edu/tag/00VM which is more useful for direct calculations. It is also used implicitly in Definition C2.1.2 in [Elephant]. -/ def family_of_elements (P : Cᵒᵖ ⥤ Type w) (R : presieve X) := Π ⦃Y : C⦄ (f : Y ⟶ X), R f → P.obj (op Y) instance : inhabited (family_of_elements P (⊥ : presieve X)) := ⟨λ Y f, false.elim⟩ /-- A family of elements for a presheaf on the presieve `R₂` can be restricted to a smaller presieve `R₁`. -/ def family_of_elements.restrict {R₁ R₂ : presieve X} (h : R₁ ≤ R₂) : family_of_elements P R₂ → family_of_elements P R₁ := λ x Y f hf, x f (h _ hf) /-- A family of elements for the arrow set `R` is *compatible* if for any `f₁ : Y₁ ⟶ X` and `f₂ : Y₂ ⟶ X` in `R`, and any `g₁ : Z ⟶ Y₁` and `g₂ : Z ⟶ Y₂`, if the square `g₁ ≫ f₁ = g₂ ≫ f₂` commutes then the elements of `P Z` obtained by restricting the element of `P Y₁` along `g₁` and restricting the element of `P Y₂` along `g₂` are the same. In special cases, this condition can be simplified, see `pullback_compatible_iff` and `compatible_iff_sieve_compatible`. This is referred to as a "compatible family" in Definition C2.1.2 of [Elephant], and on nlab: https://ncatlab.org/nlab/show/sheaf#GeneralDefinitionInComponents -/ def family_of_elements.compatible (x : family_of_elements P R) : Prop := ∀ ⦃Y₁ Y₂ Z⦄ (g₁ : Z ⟶ Y₁) (g₂ : Z ⟶ Y₂) ⦃f₁ : Y₁ ⟶ X⦄ ⦃f₂ : Y₂ ⟶ X⦄ (h₁ : R f₁) (h₂ : R f₂), g₁ ≫ f₁ = g₂ ≫ f₂ → P.map g₁.op (x f₁ h₁) = P.map g₂.op (x f₂ h₂) /-- If the category `C` has pullbacks, this is an alternative condition for a family of elements to be compatible: For any `f : Y ⟶ X` and `g : Z ⟶ X` in the presieve `R`, the restriction of the given elements for `f` and `g` to the pullback agree. This is equivalent to being compatible (provided `C` has pullbacks), shown in `pullback_compatible_iff`. This is the definition for a "matching" family given in [MM92], Chapter III, Section 4, Equation (5). Viewing the type `family_of_elements` as the middle object of the fork in https://stacks.math.columbia.edu/tag/00VM, this condition expresses that `pr₀* (x) = pr₁* (x)`, using the notation defined there. -/ def family_of_elements.pullback_compatible (x : family_of_elements P R) [has_pullbacks C] : Prop := ∀ ⦃Y₁ Y₂⦄ ⦃f₁ : Y₁ ⟶ X⦄ ⦃f₂ : Y₂ ⟶ X⦄ (h₁ : R f₁) (h₂ : R f₂), P.map (pullback.fst : pullback f₁ f₂ ⟶ _).op (x f₁ h₁) = P.map pullback.snd.op (x f₂ h₂) lemma pullback_compatible_iff (x : family_of_elements P R) [has_pullbacks C] : x.compatible ↔ x.pullback_compatible := begin split, { intros t Y₁ Y₂ f₁ f₂ hf₁ hf₂, apply t, apply pullback.condition }, { intros t Y₁ Y₂ Z g₁ g₂ f₁ f₂ hf₁ hf₂ comm, rw [←pullback.lift_fst _ _ comm, op_comp, functor_to_types.map_comp_apply, t hf₁ hf₂, ←functor_to_types.map_comp_apply, ←op_comp, pullback.lift_snd] } end /-- The restriction of a compatible family is compatible. -/ lemma family_of_elements.compatible.restrict {R₁ R₂ : presieve X} (h : R₁ ≤ R₂) {x : family_of_elements P R₂} : x.compatible → (x.restrict h).compatible := λ q Y₁ Y₂ Z g₁ g₂ f₁ f₂ h₁ h₂ comm, q g₁ g₂ (h _ h₁) (h _ h₂) comm /-- Extend a family of elements to the sieve generated by an arrow set. This is the construction described as "easy" in Lemma C2.1.3 of [Elephant]. -/ noncomputable def family_of_elements.sieve_extend (x : family_of_elements P R) : family_of_elements P (generate R) := λ Z f hf, P.map hf.some_spec.some.op (x _ hf.some_spec.some_spec.some_spec.1) /-- The extension of a compatible family to the generated sieve is compatible. -/ lemma family_of_elements.compatible.sieve_extend {x : family_of_elements P R} (hx : x.compatible) : x.sieve_extend.compatible := begin intros _ _ _ _ _ _ _ h₁ h₂ comm, iterate 2 { erw ← functor_to_types.map_comp_apply, rw ← op_comp }, apply hx, simp [comm, h₁.some_spec.some_spec.some_spec.2, h₂.some_spec.some_spec.some_spec.2], end /-- The extension of a family agrees with the original family. -/ lemma extend_agrees {x : family_of_elements P R} (t : x.compatible) {f : Y ⟶ X} (hf : R f) : x.sieve_extend f (le_generate R Y hf) = x f hf := begin have h := (le_generate R Y hf).some_spec, unfold family_of_elements.sieve_extend, rw t h.some (𝟙 _) _ hf _, { simp }, { rw id_comp, exact h.some_spec.some_spec.2 }, end /-- The restriction of an extension is the original. -/ @[simp] lemma restrict_extend {x : family_of_elements P R} (t : x.compatible) : x.sieve_extend.restrict (le_generate R) = x := begin ext Y f hf, exact extend_agrees t hf, end /-- If the arrow set for a family of elements is actually a sieve (i.e. it is downward closed) then the consistency condition can be simplified. This is an equivalent condition, see `compatible_iff_sieve_compatible`. This is the notion of "matching" given for families on sieves given in [MM92], Chapter III, Section 4, Equation 1, and nlab: https://ncatlab.org/nlab/show/matching+family. See also the discussion before Lemma C2.1.4 of [Elephant]. -/ def family_of_elements.sieve_compatible (x : family_of_elements P S) : Prop := ∀ ⦃Y Z⦄ (f : Y ⟶ X) (g : Z ⟶ Y) (hf), x (g ≫ f) (S.downward_closed hf g) = P.map g.op (x f hf) lemma compatible_iff_sieve_compatible (x : family_of_elements P S) : x.compatible ↔ x.sieve_compatible := begin split, { intros h Y Z f g hf, simpa using h (𝟙 _) g (S.downward_closed hf g) hf (id_comp _) }, { intros h Y₁ Y₂ Z g₁ g₂ f₁ f₂ h₁ h₂ k, simp_rw [← h f₁ g₁ h₁, k, h f₂ g₂ h₂] } end lemma family_of_elements.compatible.to_sieve_compatible {x : family_of_elements P S} (t : x.compatible) : x.sieve_compatible := (compatible_iff_sieve_compatible x).1 t /-- Given a family of elements `x` for the sieve `S` generated by a presieve `R`, if `x` is restricted to `R` and then extended back up to `S`, the resulting extension equals `x`. -/ @[simp] lemma extend_restrict {x : family_of_elements P (generate R)} (t : x.compatible) : (x.restrict (le_generate R)).sieve_extend = x := begin rw compatible_iff_sieve_compatible at t, ext _ _ h, apply (t _ _ _).symm.trans, congr, exact h.some_spec.some_spec.some_spec.2, end /-- Two compatible families on the sieve generated by a presieve `R` are equal if and only if they are equal when restricted to `R`. -/ lemma restrict_inj {x₁ x₂ : family_of_elements P (generate R)} (t₁ : x₁.compatible) (t₂ : x₂.compatible) : x₁.restrict (le_generate R) = x₂.restrict (le_generate R) → x₁ = x₂ := λ h, by { rw [←extend_restrict t₁, ←extend_restrict t₂], congr, exact h } /-- Compatible families of elements for a presheaf of types `P` and a presieve `R` are in 1-1 correspondence with compatible families for the same presheaf and the sieve generated by `R`, through extension and restriction. -/ @[simps] noncomputable def compatible_equiv_generate_sieve_compatible : {x : family_of_elements P R // x.compatible} ≃ {x : family_of_elements P (generate R) // x.compatible} := { to_fun := λ x, ⟨x.1.sieve_extend, x.2.sieve_extend⟩, inv_fun := λ x, ⟨x.1.restrict (le_generate R), x.2.restrict _⟩, left_inv := λ x, subtype.ext (restrict_extend x.2), right_inv := λ x, subtype.ext (extend_restrict x.2) } lemma family_of_elements.comp_of_compatible (S : sieve X) {x : family_of_elements P S} (t : x.compatible) {f : Y ⟶ X} (hf : S f) {Z} (g : Z ⟶ Y) : x (g ≫ f) (S.downward_closed hf g) = P.map g.op (x f hf) := by simpa using t (𝟙 _) g (S.downward_closed hf g) hf (id_comp _) section functor_pullback variables {D : Type u₂} [category.{v₂} D] (F : D ⥤ C) {Z : D} variables {T : presieve (F.obj Z)} {x : family_of_elements P T} /-- Given a family of elements of a sieve `S` on `F(X)`, we can realize it as a family of elements of `S.functor_pullback F`. -/ def family_of_elements.functor_pullback (x : family_of_elements P T) : family_of_elements (F.op ⋙ P) (T.functor_pullback F) := λ Y f hf, x (F.map f) hf lemma family_of_elements.compatible.functor_pullback (h : x.compatible) : (x.functor_pullback F).compatible := begin intros Z₁ Z₂ W g₁ g₂ f₁ f₂ h₁ h₂ eq, exact h (F.map g₁) (F.map g₂) h₁ h₂ (by simp only [← F.map_comp, eq]) end end functor_pullback /-- Given a family of elements of a sieve `S` on `X` whose values factors through `F`, we can realize it as a family of elements of `S.functor_pushforward F`. Since the preimage is obtained by choice, this is not well-defined generally. -/ noncomputable def family_of_elements.functor_pushforward {D : Type u₂} [category.{v₂} D] (F : D ⥤ C) {X : D} {T : presieve X} (x : family_of_elements (F.op ⋙ P) T) : family_of_elements P (T.functor_pushforward F) := λ Y f h, by { obtain ⟨Z, g, h, h₁, _⟩ := get_functor_pushforward_structure h, exact P.map h.op (x g h₁) } section pullback /-- Given a family of elements of a sieve `S` on `X`, and a map `Y ⟶ X`, we can obtain a family of elements of `S.pullback f` by taking the same elements. -/ def family_of_elements.pullback (f : Y ⟶ X) (x : family_of_elements P S) : family_of_elements P (S.pullback f) := λ _ g hg, x (g ≫ f) hg lemma family_of_elements.compatible.pullback (f : Y ⟶ X) {x : family_of_elements P S} (h : x.compatible) : (x.pullback f).compatible := begin simp only [compatible_iff_sieve_compatible] at h ⊢, intros W Z f₁ f₂ hf, unfold family_of_elements.pullback, rw ← (h (f₁ ≫ f) f₂ hf), simp only [assoc], end end pullback /-- Given a morphism of presheaves `f : P ⟶ Q`, we can take a family of elements valued in `P` to a family of elements valued in `Q` by composing with `f`. -/ def family_of_elements.comp_presheaf_map (f : P ⟶ Q) (x : family_of_elements P R) : family_of_elements Q R := λ Y g hg, f.app (op Y) (x g hg) @[simp] lemma family_of_elements.comp_presheaf_map_id (x : family_of_elements P R) : x.comp_presheaf_map (𝟙 P) = x := rfl @[simp] lemma family_of_elements.comp_prersheaf_map_comp (x : family_of_elements P R) (f : P ⟶ Q) (g : Q ⟶ U) : (x.comp_presheaf_map f).comp_presheaf_map g = x.comp_presheaf_map (f ≫ g) := rfl lemma family_of_elements.compatible.comp_presheaf_map (f : P ⟶ Q) {x : family_of_elements P R} (h : x.compatible) : (x.comp_presheaf_map f).compatible := begin intros Z₁ Z₂ W g₁ g₂ f₁ f₂ h₁ h₂ eq, unfold family_of_elements.comp_presheaf_map, rwa [← functor_to_types.naturality, ← functor_to_types.naturality, h], end /-- The given element `t` of `P.obj (op X)` is an *amalgamation* for the family of elements `x` if every restriction `P.map f.op t = x_f` for every arrow `f` in the presieve `R`. This is the definition given in https://ncatlab.org/nlab/show/sheaf#GeneralDefinitionInComponents, and https://ncatlab.org/nlab/show/matching+family, as well as [MM92], Chapter III, Section 4, equation (2). -/ def family_of_elements.is_amalgamation (x : family_of_elements P R) (t : P.obj (op X)) : Prop := ∀ ⦃Y : C⦄ (f : Y ⟶ X) (h : R f), P.map f.op t = x f h lemma family_of_elements.is_amalgamation.comp_presheaf_map {x : family_of_elements P R} {t} (f : P ⟶ Q) (h : x.is_amalgamation t) : (x.comp_presheaf_map f).is_amalgamation (f.app (op X) t) := begin intros Y g hg, dsimp [family_of_elements.comp_presheaf_map], change (f.app _ ≫ Q.map _) _ = _, simp [← f.naturality, h g hg], end lemma is_compatible_of_exists_amalgamation (x : family_of_elements P R) (h : ∃ t, x.is_amalgamation t) : x.compatible := begin cases h with t ht, intros Y₁ Y₂ Z g₁ g₂ f₁ f₂ h₁ h₂ comm, rw [←ht _ h₁, ←ht _ h₂, ←functor_to_types.map_comp_apply, ←op_comp, comm], simp, end lemma is_amalgamation_restrict {R₁ R₂ : presieve X} (h : R₁ ≤ R₂) (x : family_of_elements P R₂) (t : P.obj (op X)) (ht : x.is_amalgamation t) : (x.restrict h).is_amalgamation t := λ Y f hf, ht f (h Y hf) lemma is_amalgamation_sieve_extend {R : presieve X} (x : family_of_elements P R) (t : P.obj (op X)) (ht : x.is_amalgamation t) : x.sieve_extend.is_amalgamation t := begin intros Y f hf, dsimp [family_of_elements.sieve_extend], rw [←ht _, ←functor_to_types.map_comp_apply, ←op_comp, hf.some_spec.some_spec.some_spec.2], end /-- A presheaf is separated for a presieve if there is at most one amalgamation. -/ def is_separated_for (P : Cᵒᵖ ⥤ Type w) (R : presieve X) : Prop := ∀ (x : family_of_elements P R) (t₁ t₂), x.is_amalgamation t₁ → x.is_amalgamation t₂ → t₁ = t₂ lemma is_separated_for.ext {R : presieve X} (hR : is_separated_for P R) {t₁ t₂ : P.obj (op X)} (h : ∀ ⦃Y⦄ ⦃f : Y ⟶ X⦄ (hf : R f), P.map f.op t₁ = P.map f.op t₂) : t₁ = t₂ := hR (λ Y f hf, P.map f.op t₂) t₁ t₂ (λ Y f hf, h hf) (λ Y f hf, rfl) lemma is_separated_for_iff_generate : is_separated_for P R ↔ is_separated_for P (generate R) := begin split, { intros h x t₁ t₂ ht₁ ht₂, apply h (x.restrict (le_generate R)) t₁ t₂ _ _, { exact is_amalgamation_restrict _ x t₁ ht₁ }, { exact is_amalgamation_restrict _ x t₂ ht₂ } }, { intros h x t₁ t₂ ht₁ ht₂, apply h (x.sieve_extend), { exact is_amalgamation_sieve_extend x t₁ ht₁ }, { exact is_amalgamation_sieve_extend x t₂ ht₂ } } end lemma is_separated_for_top (P : Cᵒᵖ ⥤ Type w) : is_separated_for P (⊤ : presieve X) := λ x t₁ t₂ h₁ h₂, begin have q₁ := h₁ (𝟙 X) (by simp), have q₂ := h₂ (𝟙 X) (by simp), simp only [op_id, functor_to_types.map_id_apply] at q₁ q₂, rw [q₁, q₂], end /-- We define `P` to be a sheaf for the presieve `R` if every compatible family has a unique amalgamation. This is the definition of a sheaf for the given presieve given in C2.1.2 of [Elephant], and https://ncatlab.org/nlab/show/sheaf#GeneralDefinitionInComponents. Using `compatible_iff_sieve_compatible`, this is equivalent to the definition of a sheaf in [MM92], Chapter III, Section 4. -/ def is_sheaf_for (P : Cᵒᵖ ⥤ Type w) (R : presieve X) : Prop := ∀ (x : family_of_elements P R), x.compatible → ∃! t, x.is_amalgamation t /-- This is an equivalent condition to be a sheaf, which is useful for the abstraction to local operators on elementary toposes. However this definition is defined only for sieves, not presieves. The equivalence between this and `is_sheaf_for` is given in `yoneda_condition_iff_sheaf_condition`. This version is also useful to establish that being a sheaf is preserved under isomorphism of presheaves. See the discussion before Equation (3) of [MM92], Chapter III, Section 4. See also C2.1.4 of [Elephant]. This is also a direct reformulation of https://stacks.math.columbia.edu/tag/00Z8. -/ def yoneda_sheaf_condition (P : Cᵒᵖ ⥤ Type v₁) (S : sieve X) : Prop := ∀ (f : S.functor ⟶ P), ∃! g, S.functor_inclusion ≫ g = f -- TODO: We can generalize the universe parameter v₁ above by composing with -- appropriate `ulift_functor`s. /-- (Implementation). This is a (primarily internal) equivalence between natural transformations and compatible families. Cf the discussion after Lemma 7.47.10 in https://stacks.math.columbia.edu/tag/00YW. See also the proof of C2.1.4 of [Elephant], and the discussion in [MM92], Chapter III, Section 4. -/ def nat_trans_equiv_compatible_family {P : Cᵒᵖ ⥤ Type v₁} : (S.functor ⟶ P) ≃ {x : family_of_elements P S // x.compatible} := { to_fun := λ α, begin refine ⟨λ Y f hf, _, _⟩, { apply α.app (op Y) ⟨_, hf⟩ }, { rw compatible_iff_sieve_compatible, intros Y Z f g hf, dsimp, rw ← functor_to_types.naturality _ _ α g.op, refl } end, inv_fun := λ t, { app := λ Y f, t.1 _ f.2, naturality' := λ Y Z g, begin ext ⟨f, hf⟩, apply t.2.to_sieve_compatible _, end }, left_inv := λ α, begin ext X ⟨_, _⟩, refl end, right_inv := begin rintro ⟨x, hx⟩, refl, end } /-- (Implementation). A lemma useful to prove `yoneda_condition_iff_sheaf_condition`. -/ lemma extension_iff_amalgamation {P : Cᵒᵖ ⥤ Type v₁} (x : S.functor ⟶ P) (g : yoneda.obj X ⟶ P) : S.functor_inclusion ≫ g = x ↔ (nat_trans_equiv_compatible_family x).1.is_amalgamation (yoneda_equiv g) := begin change _ ↔ ∀ ⦃Y : C⦄ (f : Y ⟶ X) (h : S f), P.map f.op (yoneda_equiv g) = x.app (op Y) ⟨f, h⟩, split, { rintro rfl Y f hf, rw yoneda_equiv_naturality, dsimp, simp }, -- See note [dsimp, simp]. { intro h, ext Y ⟨f, hf⟩, have : _ = x.app Y _ := h f hf, rw yoneda_equiv_naturality at this, rw ← this, dsimp, simp }, -- See note [dsimp, simp]. end /-- The yoneda version of the sheaf condition is equivalent to the sheaf condition. C2.1.4 of [Elephant]. -/ lemma is_sheaf_for_iff_yoneda_sheaf_condition {P : Cᵒᵖ ⥤ Type v₁} : is_sheaf_for P S ↔ yoneda_sheaf_condition P S := begin rw [is_sheaf_for, yoneda_sheaf_condition], simp_rw [extension_iff_amalgamation], rw equiv.forall_congr_left' nat_trans_equiv_compatible_family, rw subtype.forall, apply ball_congr, intros x hx, rw equiv.exists_unique_congr_left _, simp, end /-- If `P` is a sheaf for the sieve `S` on `X`, a natural transformation from `S` (viewed as a functor) to `P` can be (uniquely) extended to all of `yoneda.obj X`. f S → P ↓ ↗ yX -/ noncomputable def is_sheaf_for.extend {P : Cᵒᵖ ⥤ Type v₁} (h : is_sheaf_for P S) (f : S.functor ⟶ P) : yoneda.obj X ⟶ P := (is_sheaf_for_iff_yoneda_sheaf_condition.1 h f).exists.some /-- Show that the extension of `f : S.functor ⟶ P` to all of `yoneda.obj X` is in fact an extension, ie that the triangle below commutes, provided `P` is a sheaf for `S` f S → P ↓ ↗ yX -/ @[simp, reassoc] lemma is_sheaf_for.functor_inclusion_comp_extend {P : Cᵒᵖ ⥤ Type v₁} (h : is_sheaf_for P S) (f : S.functor ⟶ P) : S.functor_inclusion ≫ h.extend f = f := (is_sheaf_for_iff_yoneda_sheaf_condition.1 h f).exists.some_spec /-- The extension of `f` to `yoneda.obj X` is unique. -/ lemma is_sheaf_for.unique_extend {P : Cᵒᵖ ⥤ Type v₁} (h : is_sheaf_for P S) {f : S.functor ⟶ P} (t : yoneda.obj X ⟶ P) (ht : S.functor_inclusion ≫ t = f) : t = h.extend f := ((is_sheaf_for_iff_yoneda_sheaf_condition.1 h f).unique ht (h.functor_inclusion_comp_extend f)) /-- If `P` is a sheaf for the sieve `S` on `X`, then if two natural transformations from `yoneda.obj X` to `P` agree when restricted to the subfunctor given by `S`, they are equal. -/ lemma is_sheaf_for.hom_ext {P : Cᵒᵖ ⥤ Type v₁} (h : is_sheaf_for P S) (t₁ t₂ : yoneda.obj X ⟶ P) (ht : S.functor_inclusion ≫ t₁ = S.functor_inclusion ≫ t₂) : t₁ = t₂ := (h.unique_extend t₁ ht).trans (h.unique_extend t₂ rfl).symm /-- `P` is a sheaf for `R` iff it is separated for `R` and there exists an amalgamation. -/ lemma is_separated_for_and_exists_is_amalgamation_iff_sheaf_for : is_separated_for P R ∧ (∀ (x : family_of_elements P R), x.compatible → ∃ t, x.is_amalgamation t) ↔ is_sheaf_for P R := begin rw [is_separated_for, ←forall_and_distrib], apply forall_congr, intro x, split, { intros z hx, exact exists_unique_of_exists_of_unique (z.2 hx) z.1 }, { intros h, refine ⟨_, (exists_of_exists_unique ∘ h)⟩, intros t₁ t₂ ht₁ ht₂, apply (h _).unique ht₁ ht₂, exact is_compatible_of_exists_amalgamation x ⟨_, ht₂⟩ } end /-- If `P` is separated for `R` and every family has an amalgamation, then `P` is a sheaf for `R`. -/ lemma is_separated_for.is_sheaf_for (t : is_separated_for P R) : (∀ (x : family_of_elements P R), x.compatible → ∃ t, x.is_amalgamation t) → is_sheaf_for P R := begin rw ← is_separated_for_and_exists_is_amalgamation_iff_sheaf_for, exact and.intro t, end /-- If `P` is a sheaf for `R`, it is separated for `R`. -/ lemma is_sheaf_for.is_separated_for : is_sheaf_for P R → is_separated_for P R := λ q, (is_separated_for_and_exists_is_amalgamation_iff_sheaf_for.2 q).1 /-- Get the amalgamation of the given compatible family, provided we have a sheaf. -/ noncomputable def is_sheaf_for.amalgamate (t : is_sheaf_for P R) (x : family_of_elements P R) (hx : x.compatible) : P.obj (op X) := (t x hx).exists.some lemma is_sheaf_for.is_amalgamation (t : is_sheaf_for P R) {x : family_of_elements P R} (hx : x.compatible) : x.is_amalgamation (t.amalgamate x hx) := (t x hx).exists.some_spec @[simp] lemma is_sheaf_for.valid_glue (t : is_sheaf_for P R) {x : family_of_elements P R} (hx : x.compatible) (f : Y ⟶ X) (Hf : R f) : P.map f.op (t.amalgamate x hx) = x f Hf := t.is_amalgamation hx f Hf /-- C2.1.3 in [Elephant] -/ lemma is_sheaf_for_iff_generate (R : presieve X) : is_sheaf_for P R ↔ is_sheaf_for P (generate R) := begin rw ← is_separated_for_and_exists_is_amalgamation_iff_sheaf_for, rw ← is_separated_for_and_exists_is_amalgamation_iff_sheaf_for, rw ← is_separated_for_iff_generate, apply and_congr (iff.refl _), split, { intros q x hx, apply exists_imp_exists _ (q _ (hx.restrict (le_generate R))), intros t ht, simpa [hx] using is_amalgamation_sieve_extend _ _ ht }, { intros q x hx, apply exists_imp_exists _ (q _ hx.sieve_extend), intros t ht, simpa [hx] using is_amalgamation_restrict (le_generate R) _ _ ht }, end /-- Every presheaf is a sheaf for the family {𝟙 X}. [Elephant] C2.1.5(i) -/ lemma is_sheaf_for_singleton_iso (P : Cᵒᵖ ⥤ Type w) : is_sheaf_for P (presieve.singleton (𝟙 X)) := begin intros x hx, refine ⟨x _ (presieve.singleton_self _), _, _⟩, { rintro _ _ ⟨rfl, rfl⟩, simp }, { intros t ht, simpa using ht _ (presieve.singleton_self _) } end /-- Every presheaf is a sheaf for the maximal sieve. [Elephant] C2.1.5(ii) -/ lemma is_sheaf_for_top_sieve (P : Cᵒᵖ ⥤ Type w) : is_sheaf_for P ((⊤ : sieve X) : presieve X) := begin rw ← generate_of_singleton_split_epi (𝟙 X), rw ← is_sheaf_for_iff_generate, apply is_sheaf_for_singleton_iso, end /-- If `P` is a sheaf for `S`, and it is iso to `P'`, then `P'` is a sheaf for `S`. This shows that "being a sheaf for a presieve" is a mathematical or hygenic property. -/ lemma is_sheaf_for_iso {P' : Cᵒᵖ ⥤ Type w} (i : P ≅ P') : is_sheaf_for P R → is_sheaf_for P' R := begin intros h x hx, let x' := x.comp_presheaf_map i.inv, have : x'.compatible := family_of_elements.compatible.comp_presheaf_map i.inv hx, obtain ⟨t, ht1, ht2⟩ := h x' this, use i.hom.app _ t, fsplit, { convert family_of_elements.is_amalgamation.comp_presheaf_map i.hom ht1, dsimp [x'], simp }, { intros y hy, rw (show y = (i.inv.app (op X) ≫ i.hom.app (op X)) y, by simp), simp [ ht2 (i.inv.app _ y) (family_of_elements.is_amalgamation.comp_presheaf_map i.inv hy)] } end /-- If a presieve `R` on `X` has a subsieve `S` such that: * `P` is a sheaf for `S`. * For every `f` in `R`, `P` is separated for the pullback of `S` along `f`, then `P` is a sheaf for `R`. This is closely related to [Elephant] C2.1.6(i). -/ lemma is_sheaf_for_subsieve_aux (P : Cᵒᵖ ⥤ Type w) {S : sieve X} {R : presieve X} (h : (S : presieve X) ≤ R) (hS : is_sheaf_for P S) (trans : ∀ ⦃Y⦄ ⦃f : Y ⟶ X⦄, R f → is_separated_for P (S.pullback f)) : is_sheaf_for P R := begin rw ← is_separated_for_and_exists_is_amalgamation_iff_sheaf_for, split, { intros x t₁ t₂ ht₁ ht₂, exact hS.is_separated_for _ _ _ (is_amalgamation_restrict h x t₁ ht₁) (is_amalgamation_restrict h x t₂ ht₂) }, { intros x hx, use hS.amalgamate _ (hx.restrict h), intros W j hj, apply (trans hj).ext, intros Y f hf, rw [←functor_to_types.map_comp_apply, ←op_comp, hS.valid_glue (hx.restrict h) _ hf, family_of_elements.restrict, ←hx (𝟙 _) f _ _ (id_comp _)], simp }, end /-- If `P` is a sheaf for every pullback of the sieve `S`, then `P` is a sheaf for any presieve which contains `S`. This is closely related to [Elephant] C2.1.6. -/ lemma is_sheaf_for_subsieve (P : Cᵒᵖ ⥤ Type w) {S : sieve X} {R : presieve X} (h : (S : presieve X) ≤ R) (trans : Π ⦃Y⦄ (f : Y ⟶ X), is_sheaf_for P (S.pullback f)) : is_sheaf_for P R := is_sheaf_for_subsieve_aux P h (by simpa using trans (𝟙 _)) (λ Y f hf, (trans f).is_separated_for) /-- A presheaf is separated for a topology if it is separated for every sieve in the topology. -/ def is_separated (P : Cᵒᵖ ⥤ Type w) : Prop := ∀ {X} (S : sieve X), S ∈ J X → is_separated_for P S /-- A presheaf is a sheaf for a topology if it is a sheaf for every sieve in the topology. If the given topology is given by a pretopology, `is_sheaf_for_pretopology` shows it suffices to check the sheaf condition at presieves in the pretopology. -/ def is_sheaf (P : Cᵒᵖ ⥤ Type w) : Prop := ∀ ⦃X⦄ (S : sieve X), S ∈ J X → is_sheaf_for P S lemma is_sheaf.is_sheaf_for {P : Cᵒᵖ ⥤ Type w} (hp : is_sheaf J P) (R : presieve X) (hr : generate R ∈ J X) : is_sheaf_for P R := (is_sheaf_for_iff_generate R).2 $ hp _ hr lemma is_sheaf_of_le (P : Cᵒᵖ ⥤ Type w) {J₁ J₂ : grothendieck_topology C} : J₁ ≤ J₂ → is_sheaf J₂ P → is_sheaf J₁ P := λ h t X S hS, t S (h _ hS) lemma is_separated_of_is_sheaf (P : Cᵒᵖ ⥤ Type w) (h : is_sheaf J P) : is_separated J P := λ X S hS, (h S hS).is_separated_for /-- The property of being a sheaf is preserved by isomorphism. -/ lemma is_sheaf_iso {P' : Cᵒᵖ ⥤ Type w} (i : P ≅ P') (h : is_sheaf J P) : is_sheaf J P' := λ X S hS, is_sheaf_for_iso i (h S hS) lemma is_sheaf_of_yoneda {P : Cᵒᵖ ⥤ Type v₁} (h : ∀ {X} (S : sieve X), S ∈ J X → yoneda_sheaf_condition P S) : is_sheaf J P := λ X S hS, is_sheaf_for_iff_yoneda_sheaf_condition.2 (h _ hS) /-- For a topology generated by a basis, it suffices to check the sheaf condition on the basis presieves only. -/ lemma is_sheaf_pretopology [has_pullbacks C] (K : pretopology C) : is_sheaf (K.to_grothendieck C) P ↔ (∀ {X : C} (R : presieve X), R ∈ K X → is_sheaf_for P R) := begin split, { intros PJ X R hR, rw is_sheaf_for_iff_generate, apply PJ (sieve.generate R) ⟨_, hR, le_generate R⟩ }, { rintro PK X S ⟨R, hR, RS⟩, have gRS : ⇑(generate R) ≤ S, { apply gi_generate.gc.monotone_u, rwa sets_iff_generate }, apply is_sheaf_for_subsieve P gRS _, intros Y f, rw [← pullback_arrows_comm, ← is_sheaf_for_iff_generate], exact PK (pullback_arrows f R) (K.pullbacks f R hR) } end /-- Any presheaf is a sheaf for the bottom (trivial) grothendieck topology. -/ lemma is_sheaf_bot : is_sheaf (⊥ : grothendieck_topology C) P := λ X, by simp [is_sheaf_for_top_sieve] end presieve namespace equalizer variables {C : Type u₁} [category.{v₁} C] (P : Cᵒᵖ ⥤ Type (max v₁ u₁)) {X : C} (R : presieve X) (S : sieve X) noncomputable theory /-- The middle object of the fork diagram given in Equation (3) of [MM92], as well as the fork diagram of https://stacks.math.columbia.edu/tag/00VM. -/ def first_obj : Type (max v₁ u₁) := ∏ (λ (f : Σ Y, {f : Y ⟶ X // R f}), P.obj (op f.1)) /-- Show that `first_obj` is isomorphic to `family_of_elements`. -/ @[simps] def first_obj_eq_family : first_obj P R ≅ R.family_of_elements P := { hom := λ t Y f hf, pi.π (λ (f : Σ Y, {f : Y ⟶ X // R f}), P.obj (op f.1)) ⟨_, _, hf⟩ t, inv := pi.lift (λ f x, x _ f.2.2), hom_inv_id' := begin ext ⟨Y, f, hf⟩ p, simpa, end, inv_hom_id' := begin ext x Y f hf, apply limits.types.limit.lift_π_apply, end } instance : inhabited (first_obj P (⊥ : presieve X)) := ((first_obj_eq_family P _).to_equiv).inhabited /-- The left morphism of the fork diagram given in Equation (3) of [MM92], as well as the fork diagram of https://stacks.math.columbia.edu/tag/00VM. -/ def fork_map : P.obj (op X) ⟶ first_obj P R := pi.lift (λ f, P.map f.2.1.op) /-! This section establishes the equivalence between the sheaf condition of Equation (3) [MM92] and the definition of `is_sheaf_for`. -/ namespace sieve /-- The rightmost object of the fork diagram of Equation (3) [MM92], which contains the data used to check a family is compatible. -/ def second_obj : Type (max v₁ u₁) := ∏ (λ (f : Σ Y Z (g : Z ⟶ Y), {f' : Y ⟶ X // S f'}), P.obj (op f.2.1)) /-- The map `p` of Equations (3,4) [MM92]. -/ def first_map : first_obj P S ⟶ second_obj P S := pi.lift (λ fg, pi.π _ (⟨_, _, S.downward_closed fg.2.2.2.2 fg.2.2.1⟩ : Σ Y, {f : Y ⟶ X // S f})) instance : inhabited (second_obj P (⊥ : sieve X)) := ⟨first_map _ _ default⟩ /-- The map `a` of Equations (3,4) [MM92]. -/ def second_map : first_obj P S ⟶ second_obj P S := pi.lift (λ fg, pi.π _ ⟨_, fg.2.2.2⟩ ≫ P.map fg.2.2.1.op) lemma w : fork_map P S ≫ first_map P S = fork_map P S ≫ second_map P S := begin apply limit.hom_ext, rintro ⟨Y, Z, g, f, hf⟩, simp [first_map, second_map, fork_map], end /-- The family of elements given by `x : first_obj P S` is compatible iff `first_map` and `second_map` map it to the same point. -/ lemma compatible_iff (x : first_obj P S) : ((first_obj_eq_family P S).hom x).compatible ↔ first_map P S x = second_map P S x := begin rw presieve.compatible_iff_sieve_compatible, split, { intro t, ext ⟨Y, Z, g, f, hf⟩, simpa [first_map, second_map] using t _ g hf }, { intros t Y Z f g hf, rw types.limit_ext_iff at t, simpa [first_map, second_map] using t ⟨Y, Z, g, f, hf⟩ } end /-- `P` is a sheaf for `S`, iff the fork given by `w` is an equalizer. -/ lemma equalizer_sheaf_condition : presieve.is_sheaf_for P S ↔ nonempty (is_limit (fork.of_ι _ (w P S))) := begin rw [types.type_equalizer_iff_unique, ← equiv.forall_congr_left (first_obj_eq_family P S).to_equiv.symm], simp_rw ← compatible_iff, simp only [inv_hom_id_apply, iso.to_equiv_symm_fun], apply ball_congr, intros x tx, apply exists_unique_congr, intro t, rw ← iso.to_equiv_symm_fun, rw equiv.eq_symm_apply, split, { intros q, ext Y f hf, simpa [first_obj_eq_family, fork_map] using q _ _ }, { intros q Y f hf, rw ← q, simp [first_obj_eq_family, fork_map] } end end sieve /-! This section establishes the equivalence between the sheaf condition of https://stacks.math.columbia.edu/tag/00VM and the definition of `is_sheaf_for`. -/ namespace presieve variables [has_pullbacks C] /-- The rightmost object of the fork diagram of https://stacks.math.columbia.edu/tag/00VM, which contains the data used to check a family of elements for a presieve is compatible. -/ def second_obj : Type (max v₁ u₁) := ∏ (λ (fg : (Σ Y, {f : Y ⟶ X // R f}) × (Σ Z, {g : Z ⟶ X // R g})), P.obj (op (pullback fg.1.2.1 fg.2.2.1))) /-- The map `pr₀*` of https://stacks.math.columbia.edu/tag/00VL. -/ def first_map : first_obj P R ⟶ second_obj P R := pi.lift (λ fg, pi.π _ _ ≫ P.map pullback.fst.op) instance : inhabited (second_obj P (⊥ : presieve X)) := ⟨first_map _ _ default⟩ /-- The map `pr₁*` of https://stacks.math.columbia.edu/tag/00VL. -/ def second_map : first_obj P R ⟶ second_obj P R := pi.lift (λ fg, pi.π _ _ ≫ P.map pullback.snd.op) lemma w : fork_map P R ≫ first_map P R = fork_map P R ≫ second_map P R := begin apply limit.hom_ext, rintro ⟨⟨Y, f, hf⟩, ⟨Z, g, hg⟩⟩, simp only [first_map, second_map, fork_map], simp only [limit.lift_π, limit.lift_π_assoc, assoc, fan.mk_π_app, subtype.coe_mk, subtype.val_eq_coe], rw [← P.map_comp, ← op_comp, pullback.condition], simp, end /-- The family of elements given by `x : first_obj P S` is compatible iff `first_map` and `second_map` map it to the same point. -/ lemma compatible_iff (x : first_obj P R) : ((first_obj_eq_family P R).hom x).compatible ↔ first_map P R x = second_map P R x := begin rw presieve.pullback_compatible_iff, split, { intro t, ext ⟨⟨Y, f, hf⟩, Z, g, hg⟩, simpa [first_map, second_map] using t hf hg }, { intros t Y Z f g hf hg, rw types.limit_ext_iff at t, simpa [first_map, second_map] using t ⟨⟨Y, f, hf⟩, Z, g, hg⟩ } end /-- `P` is a sheaf for `R`, iff the fork given by `w` is an equalizer. See https://stacks.math.columbia.edu/tag/00VM. -/ lemma sheaf_condition : R.is_sheaf_for P ↔ nonempty (is_limit (fork.of_ι _ (w P R))) := begin rw types.type_equalizer_iff_unique, erw ← equiv.forall_congr_left (first_obj_eq_family P R).to_equiv.symm, simp_rw [← compatible_iff, ← iso.to_equiv_fun, equiv.apply_symm_apply], apply ball_congr, intros x hx, apply exists_unique_congr, intros t, rw equiv.eq_symm_apply, split, { intros q, ext Y f hf, simpa [fork_map] using q _ _ }, { intros q Y f hf, rw ← q, simp [fork_map] } end end presieve end equalizer variables {C : Type u₁} [category.{v₁} C] variables (J : grothendieck_topology C) /-- The category of sheaves on a grothendieck topology. -/ structure SheafOfTypes (J : grothendieck_topology C) : Type (max u₁ v₁ (w+1)) := (val : Cᵒᵖ ⥤ Type w) (cond : presieve.is_sheaf J val) namespace SheafOfTypes variable {J} /-- Morphisms between sheaves of types are just morphisms between the underlying presheaves. -/ @[ext] structure hom (X Y : SheafOfTypes J) := (val : X.val ⟶ Y.val) @[simps] instance : category (SheafOfTypes J) := { hom := hom, id := λ X, ⟨𝟙 _⟩, comp := λ X Y Z f g, ⟨f.val ≫ g.val⟩, id_comp' := λ X Y f, hom.ext _ _ $ id_comp _, comp_id' := λ X Y f, hom.ext _ _ $ comp_id _, assoc' := λ X Y Z W f g h, hom.ext _ _ $ assoc _ _ _ } -- Let's make the inhabited linter happy... instance (X : SheafOfTypes J) : inhabited (hom X X) := ⟨𝟙 X⟩ end SheafOfTypes /-- The inclusion functor from sheaves to presheaves. -/ @[simps] def SheafOfTypes_to_presheaf : SheafOfTypes J ⥤ (Cᵒᵖ ⥤ Type w) := { obj := SheafOfTypes.val, map := λ X Y f, f.val, map_id' := λ X, rfl, map_comp' := λ X Y Z f g, rfl } instance : full (SheafOfTypes_to_presheaf J) := { preimage := λ X Y f, ⟨f⟩ } instance : faithful (SheafOfTypes_to_presheaf J) := {} /-- The category of sheaves on the bottom (trivial) grothendieck topology is equivalent to the category of presheaves. -/ @[simps] def SheafOfTypes_bot_equiv : SheafOfTypes (⊥ : grothendieck_topology C) ≌ (Cᵒᵖ ⥤ Type w) := { functor := SheafOfTypes_to_presheaf _, inverse := { obj := λ P, ⟨P, presieve.is_sheaf_bot⟩, map := λ P₁ P₂ f, (SheafOfTypes_to_presheaf _).preimage f }, unit_iso := { hom := { app := λ _, ⟨𝟙 _⟩ }, inv := { app := λ _, ⟨𝟙 _⟩ } }, counit_iso := iso.refl _ } instance : inhabited (SheafOfTypes (⊥ : grothendieck_topology C)) := ⟨SheafOfTypes_bot_equiv.inverse.obj ((functor.const _).obj punit)⟩ end category_theory
523ac9e684db157c691bb9bd0b07ecc098e69eda
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/linear_algebra/matrix/adjugate.lean
d2877ef3ef6fa3ad4d78f8b29d8295685ffcd0b5
[ "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
21,477
lean
/- Copyright (c) 2019 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import algebra.regular.basic import linear_algebra.matrix.mv_polynomial import linear_algebra.matrix.polynomial import ring_theory.polynomial.basic /-! # Cramer's rule and adjugate matrices > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. The adjugate matrix is the transpose of the cofactor matrix. It is calculated with Cramer's rule, which we introduce first. The vectors returned by Cramer's rule are given by the linear map `cramer`, which sends a matrix `A` and vector `b` to the vector consisting of the determinant of replacing the `i`th column of `A` with `b` at index `i` (written as `(A.update_column i b).det`). Using Cramer's rule, we can compute for each matrix `A` the matrix `adjugate A`. The entries of the adjugate are the minors of `A`. Instead of defining a minor by deleting row `i` and column `j` of `A`, we replace the `i`th row of `A` with the `j`th basis vector; the resulting matrix has the same determinant but more importantly equals Cramer's rule applied to `A` and the `j`th basis vector, simplifying the subsequent proofs. We prove the adjugate behaves like `det A • A⁻¹`. ## Main definitions * `matrix.cramer A b`: the vector output by Cramer's rule on `A` and `b`. * `matrix.adjugate A`: the adjugate (or classical adjoint) of the matrix `A`. ## References * https://en.wikipedia.org/wiki/Cramer's_rule#Finding_inverse_matrix ## Tags cramer, cramer's rule, adjugate -/ namespace matrix universes u v w variables {m : Type u} {n : Type v} {α : Type w} variables [decidable_eq n] [fintype n] [decidable_eq m] [fintype m] [comm_ring α] open_locale matrix big_operators polynomial open equiv equiv.perm finset section cramer /-! ### `cramer` section Introduce the linear map `cramer` with values defined by `cramer_map`. After defining `cramer_map` and showing it is linear, we will restrict our proofs to using `cramer`. -/ variables (A : matrix n n α) (b : n → α) /-- `cramer_map A b i` is the determinant of the matrix `A` with column `i` replaced with `b`, and thus `cramer_map A b` is the vector output by Cramer's rule on `A` and `b`. If `A ⬝ x = b` has a unique solution in `x`, `cramer_map A` sends the vector `b` to `A.det • x`. Otherwise, the outcome of `cramer_map` is well-defined but not necessarily useful. -/ def cramer_map (i : n) : α := (A.update_column i b).det lemma cramer_map_is_linear (i : n) : is_linear_map α (λ b, cramer_map A b i) := { map_add := det_update_column_add _ _, map_smul := det_update_column_smul _ _ } lemma cramer_is_linear : is_linear_map α (cramer_map A) := begin split; intros; ext i, { apply (cramer_map_is_linear A i).1 }, { apply (cramer_map_is_linear A i).2 } end /-- `cramer A b i` is the determinant of the matrix `A` with column `i` replaced with `b`, and thus `cramer A b` is the vector output by Cramer's rule on `A` and `b`. If `A ⬝ x = b` has a unique solution in `x`, `cramer A` sends the vector `b` to `A.det • x`. Otherwise, the outcome of `cramer` is well-defined but not necessarily useful. -/ def cramer (A : matrix n n α) : (n → α) →ₗ[α] (n → α) := is_linear_map.mk' (cramer_map A) (cramer_is_linear A) lemma cramer_apply (i : n) : cramer A b i = (A.update_column i b).det := rfl lemma cramer_transpose_apply (i : n) : cramer Aᵀ b i = (A.update_row i b).det := by rw [cramer_apply, update_column_transpose, det_transpose] lemma cramer_transpose_row_self (i : n) : Aᵀ.cramer (A i) = pi.single i A.det := begin ext j, rw [cramer_apply, pi.single_apply], split_ifs with h, { -- i = j: this entry should be `A.det` subst h, simp only [update_column_transpose, det_transpose, update_row_eq_self] }, { -- i ≠ j: this entry should be 0 rw [update_column_transpose, det_transpose], apply det_zero_of_row_eq h, rw [update_row_self, update_row_ne (ne.symm h)] } end lemma cramer_row_self (i : n) (h : ∀ j, b j = A j i) : A.cramer b = pi.single i A.det := begin rw [← transpose_transpose A, det_transpose], convert cramer_transpose_row_self Aᵀ i, exact funext h end @[simp] lemma cramer_one : cramer (1 : matrix n n α) = 1 := begin ext i j, convert congr_fun (cramer_row_self (1 : matrix n n α) (pi.single i 1) i _) j, { simp }, { intros j, rw [matrix.one_eq_pi_single, pi.single_comm] } end lemma cramer_smul (r : α) (A : matrix n n α) : cramer (r • A) = r ^ (fintype.card n - 1) • cramer A := linear_map.ext $ λ b, funext $ λ _, det_update_column_smul' _ _ _ _ @[simp] lemma cramer_subsingleton_apply [subsingleton n] (A : matrix n n α) (b : n → α) (i : n) : cramer A b i = b i := by rw [cramer_apply, det_eq_elem_of_subsingleton _ i, update_column_self] lemma cramer_zero [nontrivial n] : cramer (0 : matrix n n α) = 0 := begin ext i j, obtain ⟨j', hj'⟩ : ∃ j', j' ≠ j := exists_ne j, apply det_eq_zero_of_column_eq_zero j', intro j'', simp [update_column_ne hj'], end /-- Use linearity of `cramer` to take it out of a summation. -/ lemma sum_cramer {β} (s : finset β) (f : β → n → α) : ∑ x in s, cramer A (f x) = cramer A (∑ x in s, f x) := (linear_map.map_sum (cramer A)).symm /-- Use linearity of `cramer` and vector evaluation to take `cramer A _ i` out of a summation. -/ lemma sum_cramer_apply {β} (s : finset β) (f : n → β → α) (i : n) : ∑ x in s, cramer A (λ j, f j x) i = cramer A (λ (j : n), ∑ x in s, f j x) i := calc ∑ x in s, cramer A (λ j, f j x) i = (∑ x in s, cramer A (λ j, f j x)) i : (finset.sum_apply i s _).symm ... = cramer A (λ (j : n), ∑ x in s, f j x) i : by { rw [sum_cramer, cramer_apply], congr' with j, apply finset.sum_apply } lemma cramer_submatrix_equiv (A : matrix m m α) (e : n ≃ m) (b : n → α) : cramer (A.submatrix e e) b = cramer A (b ∘ e.symm) ∘ e := begin ext i, simp_rw [function.comp_apply, cramer_apply, update_column_submatrix_equiv, det_submatrix_equiv_self e], end lemma cramer_reindex (e : m ≃ n) (A : matrix m m α) (b : n → α) : cramer (reindex e e A) b = cramer A (b ∘ e) ∘ e.symm := cramer_submatrix_equiv _ _ _ end cramer section adjugate /-! ### `adjugate` section Define the `adjugate` matrix and a few equations. These will hold for any matrix over a commutative ring. -/ /-- The adjugate matrix is the transpose of the cofactor matrix. Typically, the cofactor matrix is defined by taking minors, i.e. the determinant of the matrix with a row and column removed. However, the proof of `mul_adjugate` becomes a lot easier if we use the matrix replacing a column with a basis vector, since it allows us to use facts about the `cramer` map. -/ def adjugate (A : matrix n n α) : matrix n n α := of $ λ i, cramer Aᵀ (pi.single i 1) lemma adjugate_def (A : matrix n n α) : adjugate A = of (λ i, cramer Aᵀ (pi.single i 1)) := rfl lemma adjugate_apply (A : matrix n n α) (i j : n) : adjugate A i j = (A.update_row j (pi.single i 1)).det := by rw [adjugate_def, of_apply, cramer_apply, update_column_transpose, det_transpose] lemma adjugate_transpose (A : matrix n n α) : (adjugate A)ᵀ = adjugate (Aᵀ) := begin ext i j, rw [transpose_apply, adjugate_apply, adjugate_apply, update_row_transpose, det_transpose], rw [det_apply', det_apply'], apply finset.sum_congr rfl, intros σ _, congr' 1, by_cases i = σ j, { -- Everything except `(i , j)` (= `(σ j , j)`) is given by A, and the rest is a single `1`. congr; ext j', subst h, have : σ j' = σ j ↔ j' = j := σ.injective.eq_iff, rw [update_row_apply, update_column_apply], simp_rw this, rw [←dite_eq_ite, ←dite_eq_ite], congr' 1 with rfl, rw [pi.single_eq_same, pi.single_eq_same], }, { -- Otherwise, we need to show that there is a `0` somewhere in the product. have : (∏ j' : n, update_column A j (pi.single i 1) (σ j') j') = 0, { apply prod_eq_zero (mem_univ j), rw [update_column_self, pi.single_eq_of_ne' h], }, rw this, apply prod_eq_zero (mem_univ (σ⁻¹ i)), erw [apply_symm_apply σ i, update_row_self], apply pi.single_eq_of_ne, intro h', exact h ((symm_apply_eq σ).mp h') } end @[simp] lemma adjugate_submatrix_equiv_self (e : n ≃ m) (A : matrix m m α) : adjugate (A.submatrix e e) = (adjugate A).submatrix e e := begin ext i j, rw [adjugate_apply, submatrix_apply, adjugate_apply, ← det_submatrix_equiv_self e, update_row_submatrix_equiv], congr, exact function.update_comp_equiv _ e.symm _ _, end lemma adjugate_reindex (e : m ≃ n) (A : matrix m m α) : adjugate (reindex e e A) = reindex e e (adjugate A) := adjugate_submatrix_equiv_self _ _ /-- Since the map `b ↦ cramer A b` is linear in `b`, it must be multiplication by some matrix. This matrix is `A.adjugate`. -/ lemma cramer_eq_adjugate_mul_vec (A : matrix n n α) (b : n → α) : cramer A b = A.adjugate.mul_vec b := begin nth_rewrite 1 ← A.transpose_transpose, rw [← adjugate_transpose, adjugate_def], have : b = ∑ i, (b i) • (pi.single i 1), { refine (pi_eq_sum_univ b).trans _, congr' with j, simp [pi.single_apply, eq_comm] }, nth_rewrite 0 this, ext k, simp [mul_vec, dot_product, mul_comm], end lemma mul_adjugate_apply (A : matrix n n α) (i j k) : A i k * adjugate A k j = cramer Aᵀ (pi.single k (A i k)) j := begin erw [←smul_eq_mul, ←pi.smul_apply, ←linear_map.map_smul, ←pi.single_smul', smul_eq_mul, mul_one], end lemma mul_adjugate (A : matrix n n α) : A ⬝ adjugate A = A.det • 1 := begin ext i j, rw [mul_apply, pi.smul_apply, pi.smul_apply, one_apply, smul_eq_mul, mul_boole], simp [mul_adjugate_apply, sum_cramer_apply, cramer_transpose_row_self, pi.single_apply, eq_comm] end lemma adjugate_mul (A : matrix n n α) : adjugate A ⬝ A = A.det • 1 := calc adjugate A ⬝ A = (Aᵀ ⬝ (adjugate Aᵀ))ᵀ : by rw [←adjugate_transpose, ←transpose_mul, transpose_transpose] ... = A.det • 1 : by rw [mul_adjugate (Aᵀ), det_transpose, transpose_smul, transpose_one] lemma adjugate_smul (r : α) (A : matrix n n α) : adjugate (r • A) = r ^ (fintype.card n - 1) • adjugate A := begin rw [adjugate, adjugate, transpose_smul, cramer_smul], refl, end /-- A stronger form of **Cramer's rule** that allows us to solve some instances of `A ⬝ x = b` even if the determinant is not a unit. A sufficient (but still not necessary) condition is that `A.det` divides `b`. -/ @[simp] lemma mul_vec_cramer (A : matrix n n α) (b : n → α) : A.mul_vec (cramer A b) = A.det • b := by rw [cramer_eq_adjugate_mul_vec, mul_vec_mul_vec, mul_adjugate, smul_mul_vec_assoc, one_mul_vec] lemma adjugate_subsingleton [subsingleton n] (A : matrix n n α) : adjugate A = 1 := begin ext i j, simp [subsingleton.elim i j, adjugate_apply, det_eq_elem_of_subsingleton _ i] end lemma adjugate_eq_one_of_card_eq_one {A : matrix n n α} (h : fintype.card n = 1) : adjugate A = 1 := begin haveI : subsingleton n := fintype.card_le_one_iff_subsingleton.mp h.le, exact adjugate_subsingleton _ end @[simp] lemma adjugate_zero [nontrivial n] : adjugate (0 : matrix n n α) = 0 := begin ext i j, obtain ⟨j', hj'⟩ : ∃ j', j' ≠ j := exists_ne j, apply det_eq_zero_of_column_eq_zero j', intro j'', simp [update_column_ne hj'], end @[simp] lemma adjugate_one : adjugate (1 : matrix n n α) = 1 := by { ext, simp [adjugate_def, matrix.one_apply, pi.single_apply, eq_comm] } @[simp] lemma adjugate_diagonal (v : n → α) : adjugate (diagonal v) = diagonal (λ i, ∏ j in finset.univ.erase i, v j) := begin ext, simp only [adjugate_def, cramer_apply, diagonal_transpose, of_apply], obtain rfl | hij := eq_or_ne i j, { rw [diagonal_apply_eq, diagonal_update_column_single, det_diagonal, prod_update_of_mem (finset.mem_univ _), sdiff_singleton_eq_erase, one_mul] }, { rw diagonal_apply_ne _ hij, refine det_eq_zero_of_row_eq_zero j (λ k, _), obtain rfl | hjk := eq_or_ne k j, { rw [update_column_self, pi.single_eq_of_ne' hij] }, { rw [update_column_ne hjk, diagonal_apply_ne' _ hjk]} }, end lemma _root_.ring_hom.map_adjugate {R S : Type*} [comm_ring R] [comm_ring S] (f : R →+* S) (M : matrix n n R) : f.map_matrix M.adjugate = matrix.adjugate (f.map_matrix M) := begin ext i k, have : pi.single i (1 : S) = f ∘ pi.single i 1, { rw ←f.map_one, exact pi.single_op (λ i, f) (λ i, f.map_zero) i (1 : R) }, rw [adjugate_apply, ring_hom.map_matrix_apply, map_apply, ring_hom.map_matrix_apply, this, ←map_update_row, ←ring_hom.map_matrix_apply, ←ring_hom.map_det, ←adjugate_apply] end lemma _root_.alg_hom.map_adjugate {R A B : Type*} [comm_semiring R] [comm_ring A] [comm_ring B] [algebra R A] [algebra R B] (f : A →ₐ[R] B) (M : matrix n n A) : f.map_matrix M.adjugate = matrix.adjugate (f.map_matrix M) := f.to_ring_hom.map_adjugate _ lemma det_adjugate (A : matrix n n α) : (adjugate A).det = A.det ^ (fintype.card n - 1) := begin -- get rid of the `- 1` cases (fintype.card n).eq_zero_or_pos with h_card h_card, { haveI : is_empty n := fintype.card_eq_zero_iff.mp h_card, rw [h_card, nat.zero_sub, pow_zero, adjugate_subsingleton, det_one] }, replace h_card := tsub_add_cancel_of_le h_card.nat_succ_le, -- express `A` as an evaluation of a polynomial in n^2 variables, and solve in the polynomial ring -- where `A'.det` is non-zero. let A' := mv_polynomial_X n n ℤ, suffices : A'.adjugate.det = A'.det ^ (fintype.card n - 1), { rw [←mv_polynomial_X_map_matrix_aeval ℤ A, ←alg_hom.map_adjugate, ←alg_hom.map_det, ←alg_hom.map_det, ←alg_hom.map_pow, this] }, apply mul_left_cancel₀ (show A'.det ≠ 0, from det_mv_polynomial_X_ne_zero n ℤ), calc A'.det * A'.adjugate.det = (A' ⬝ adjugate A').det : (det_mul _ _).symm ... = A'.det ^ fintype.card n : by rw [mul_adjugate, det_smul, det_one, mul_one] ... = A'.det * A'.det ^ (fintype.card n - 1) : by rw [←pow_succ, h_card], end @[simp] lemma adjugate_fin_zero (A : matrix (fin 0) (fin 0) α) : adjugate A = 0 := subsingleton.elim _ _ @[simp] lemma adjugate_fin_one (A : matrix (fin 1) (fin 1) α) : adjugate A = 1 := adjugate_subsingleton A lemma adjugate_fin_two (A : matrix (fin 2) (fin 2) α) : adjugate A = !![A 1 1, -A 0 1; -A 1 0, A 0 0] := begin ext i j, rw [adjugate_apply, det_fin_two], fin_cases i; fin_cases j; simp only [one_mul, fin.one_eq_zero_iff, pi.single_eq_same, mul_zero, sub_zero, pi.single_eq_of_ne, ne.def, not_false_iff, update_row_self, update_row_ne, cons_val_zero, of_apply, nat.succ_succ_ne_one, pi.single_eq_of_ne, update_row_self, pi.single_eq_of_ne, ne.def, fin.zero_eq_one_iff, nat.succ_succ_ne_one, not_false_iff, update_row_ne, fin.one_eq_zero_iff, zero_mul, pi.single_eq_same, one_mul, zero_sub, of_apply, cons_val', cons_val_fin_one, cons_val_one, head_fin_const, neg_inj, eq_self_iff_true, cons_val_zero, head_cons, mul_one] end @[simp] lemma adjugate_fin_two_of (a b c d : α) : adjugate !![a, b; c, d] = !![d, -b; -c, a] := adjugate_fin_two _ lemma adjugate_fin_succ_eq_det_submatrix {n : ℕ} (A : matrix (fin n.succ) (fin n.succ) α) (i j) : adjugate A i j = (-1)^(j + i : ℕ) * det (A.submatrix j.succ_above i.succ_above) := begin simp_rw [adjugate_apply, det_succ_row _ j, update_row_self, submatrix_update_row_succ_above], rw [fintype.sum_eq_single i (λ h hjk, _), pi.single_eq_same, mul_one], rw [pi.single_eq_of_ne hjk, mul_zero, zero_mul], end lemma det_eq_sum_mul_adjugate_row (A : matrix n n α) (i : n) : det A = ∑ j : n, A i j * adjugate A j i := begin haveI : nonempty n := ⟨i⟩, obtain ⟨n', hn'⟩ := nat.exists_eq_succ_of_ne_zero (fintype.card_ne_zero : fintype.card n ≠ 0), obtain ⟨e⟩ := fintype.trunc_equiv_fin_of_card_eq hn', let A' := reindex e e A, suffices : det A' = ∑ j : fin n'.succ, A' (e i) j * adjugate A' j (e i), { simp_rw [A', det_reindex_self, adjugate_reindex, reindex_apply, submatrix_apply, ←e.sum_comp, equiv.symm_apply_apply] at this, exact this }, rw det_succ_row A' (e i), simp_rw [mul_assoc, mul_left_comm _ (A' _ _), ←adjugate_fin_succ_eq_det_submatrix], end lemma det_eq_sum_mul_adjugate_col (A : matrix n n α) (j : n) : det A = ∑ i : n, A i j * adjugate A j i := by simpa only [det_transpose, ←adjugate_transpose] using det_eq_sum_mul_adjugate_row Aᵀ j lemma adjugate_conj_transpose [star_ring α] (A : matrix n n α) : A.adjugateᴴ = adjugate (Aᴴ) := begin dsimp only [conj_transpose], have : Aᵀ.adjugate.map star = adjugate (Aᵀ.map star) := ((star_ring_end α).map_adjugate Aᵀ), rw [A.adjugate_transpose, this], end lemma is_regular_of_is_left_regular_det {A : matrix n n α} (hA : is_left_regular A.det) : is_regular A := begin split, { intros B C h, refine hA.matrix _, rw [←matrix.one_mul B, ←matrix.one_mul C, ←matrix.smul_mul, ←matrix.smul_mul, ←adjugate_mul, matrix.mul_assoc, matrix.mul_assoc, ←mul_eq_mul A, h, mul_eq_mul] }, { intros B C h, simp only [mul_eq_mul] at h, refine hA.matrix _, rw [←matrix.mul_one B, ←matrix.mul_one C, ←matrix.mul_smul, ←matrix.mul_smul, ←mul_adjugate, ←matrix.mul_assoc, ←matrix.mul_assoc, h] } end lemma adjugate_mul_distrib_aux (A B : matrix n n α) (hA : is_left_regular A.det) (hB : is_left_regular B.det) : adjugate (A ⬝ B) = adjugate B ⬝ adjugate A := begin have hAB : is_left_regular (A ⬝ B).det, { rw [det_mul], exact hA.mul hB }, refine (is_regular_of_is_left_regular_det hAB).left _, rw [mul_eq_mul, mul_adjugate, mul_eq_mul, matrix.mul_assoc, ←matrix.mul_assoc B, mul_adjugate, smul_mul, matrix.one_mul, mul_smul, mul_adjugate, smul_smul, mul_comm, ←det_mul] end /-- Proof follows from "The trace Cayley-Hamilton theorem" by Darij Grinberg, Section 5.3 -/ lemma adjugate_mul_distrib (A B : matrix n n α) : adjugate (A ⬝ B) = adjugate B ⬝ adjugate A := begin let g : matrix n n α → matrix n n α[X] := λ M, M.map polynomial.C + (polynomial.X : α[X]) • 1, let f' : matrix n n α[X] →+* matrix n n α := (polynomial.eval_ring_hom 0).map_matrix, have f'_inv : ∀ M, f' (g M) = M, { intro, ext, simp [f', g], }, have f'_adj : ∀ (M : matrix n n α), f' (adjugate (g M)) = adjugate M, { intro, rw [ring_hom.map_adjugate, f'_inv] }, have f'_g_mul : ∀ (M N : matrix n n α), f' (g M ⬝ g N) = M ⬝ N, { intros, rw [←mul_eq_mul, ring_hom.map_mul, f'_inv, f'_inv, mul_eq_mul] }, have hu : ∀ (M : matrix n n α), is_regular (g M).det, { intros M, refine polynomial.monic.is_regular _, simp only [g, polynomial.monic.def, ←polynomial.leading_coeff_det_X_one_add_C M, add_comm] }, rw [←f'_adj, ←f'_adj, ←f'_adj, ←mul_eq_mul (f' (adjugate (g B))), ←f'.map_mul, mul_eq_mul, ←adjugate_mul_distrib_aux _ _ (hu A).left (hu B).left, ring_hom.map_adjugate, ring_hom.map_adjugate, f'_inv, f'_g_mul] end @[simp] lemma adjugate_pow (A : matrix n n α) (k : ℕ) : adjugate (A ^ k) = (adjugate A) ^ k := begin induction k with k IH, { simp }, { rw [pow_succ', mul_eq_mul, adjugate_mul_distrib, IH, ←mul_eq_mul, pow_succ] } end lemma det_smul_adjugate_adjugate (A : matrix n n α) : det A • adjugate (adjugate A) = det A ^ (fintype.card n - 1) • A := begin have : A ⬝ (A.adjugate ⬝ A.adjugate.adjugate) = A ⬝ (A.det ^ (fintype.card n - 1) • 1), { rw [←adjugate_mul_distrib, adjugate_mul, adjugate_smul, adjugate_one], }, rwa [←matrix.mul_assoc, mul_adjugate, matrix.mul_smul, matrix.mul_one, matrix.smul_mul, matrix.one_mul] at this, end /-- Note that this is not true for `fintype.card n = 1` since `1 - 2 = 0` and not `-1`. -/ lemma adjugate_adjugate (A : matrix n n α) (h : fintype.card n ≠ 1) : adjugate (adjugate A) = det A ^ (fintype.card n - 2) • A := begin -- get rid of the `- 2` cases h_card : (fintype.card n) with n', { haveI : is_empty n := fintype.card_eq_zero_iff.mp h_card, apply subsingleton.elim, }, cases n', { exact (h h_card).elim }, rw ←h_card, -- express `A` as an evaluation of a polynomial in n^2 variables, and solve in the polynomial ring -- where `A'.det` is non-zero. let A' := mv_polynomial_X n n ℤ, suffices : adjugate (adjugate A') = det A' ^ (fintype.card n - 2) • A', { rw [←mv_polynomial_X_map_matrix_aeval ℤ A, ←alg_hom.map_adjugate, ←alg_hom.map_adjugate, this, ←alg_hom.map_det, ← alg_hom.map_pow, alg_hom.map_matrix_apply, alg_hom.map_matrix_apply, matrix.map_smul' _ _ _ (_root_.map_mul _)] }, have h_card' : fintype.card n - 2 + 1 = fintype.card n - 1, { simp [h_card] }, have is_reg : is_smul_regular (mv_polynomial (n × n) ℤ) (det A') := λ x y, mul_left_cancel₀ (det_mv_polynomial_X_ne_zero n ℤ), apply is_reg.matrix, rw [smul_smul, ←pow_succ, h_card', det_smul_adjugate_adjugate], end /-- A weaker version of `matrix.adjugate_adjugate` that uses `nontrivial`. -/ lemma adjugate_adjugate' (A : matrix n n α) [nontrivial n] : adjugate (adjugate A) = det A ^ (fintype.card n - 2) • A := adjugate_adjugate _ $ fintype.one_lt_card.ne' end adjugate end matrix
f64c7dc415ff09d45484a41f3969caafd0355ae1
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/stage0/src/Lean/Elab/Tactic/BuiltinTactic.lean
7b2a3803a44193931033f942fd9cae65bc6e61ff
[ "Apache-2.0" ]
permissive
subfish-zhou/leanprover-zh_CN.github.io
30b9fba9bd790720bd95764e61ae796697d2f603
8b2985d4a3d458ceda9361ac454c28168d920d3f
refs/heads/master
1,689,709,967,820
1,632,503,056,000
1,632,503,056,000
409,962,097
1
0
null
null
null
null
UTF-8
Lean
false
false
10,148
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.Elab.Tactic.Basic namespace Lean.Elab.Tactic open Meta @[builtinTactic Lean.Parser.Tactic.«done»] def evalDone : Tactic := fun _ => done @[builtinTactic seq1] def evalSeq1 : Tactic := fun stx => do let args := stx[0].getArgs for i in [:args.size] do if i % 2 == 0 then evalTactic args[i] else saveTacticInfoForToken args[i] -- add `TacticInfo` node for `;` @[builtinTactic paren] def evalParen : Tactic := fun stx => evalTactic stx[1] /- Evaluate `many (group (tactic >> optional ";")) -/ def evalManyTacticOptSemi (stx : Syntax) : TacticM Unit := do stx.forArgsM fun seqElem => do evalTactic seqElem[0] saveTacticInfoForToken seqElem[1] -- add TacticInfo node for `;` @[builtinTactic tacticSeq1Indented] def evalTacticSeq1Indented : Tactic := fun stx => evalManyTacticOptSemi stx[0] @[builtinTactic tacticSeqBracketed] def evalTacticSeqBracketed : Tactic := fun stx => do let initInfo ← mkInitialTacticInfo stx[0] withRef stx[2] <| closeUsingOrAdmit do -- save state before/after entering focus on `{` withInfoContext (pure ()) initInfo evalManyTacticOptSemi stx[1] @[builtinTactic Parser.Tactic.focus] def evalFocus : Tactic := fun stx => do let mkInfo ← mkInitialTacticInfo stx[0] focus do -- show focused state on `focus` withInfoContext (pure ()) mkInfo evalTactic stx[1] private def getOptRotation (stx : Syntax) : Nat := if stx.isNone then 1 else stx[0].toNat @[builtinTactic Parser.Tactic.rotateLeft] def evalRotateLeft : Tactic := fun stx => do let n := getOptRotation stx[1] setGoals <| (← getGoals).rotateLeft n @[builtinTactic Parser.Tactic.rotateRight] def evalRotateRight : Tactic := fun stx => do let n := getOptRotation stx[1] setGoals <| (← getGoals).rotateRight n @[builtinTactic Parser.Tactic.open] def evalOpen : Tactic := fun stx => do try pushScope let openDecls ← elabOpenDecl stx[1] withTheReader Core.Context (fun ctx => { ctx with openDecls := openDecls }) do evalTactic stx[3] finally popScope @[builtinTactic Parser.Tactic.set_option] def elabSetOption : Tactic := fun stx => do let options ← Elab.elabSetOption stx[1] stx[2] withTheReader Core.Context (fun ctx => { ctx with maxRecDepth := maxRecDepth.get options, options := options }) do evalTactic stx[4] @[builtinTactic Parser.Tactic.allGoals] def evalAllGoals : Tactic := fun stx => do let mvarIds ← getGoals let mut mvarIdsNew := #[] for mvarId in mvarIds do unless (← isExprMVarAssigned mvarId) do setGoals [mvarId] try evalTactic stx[1] mvarIdsNew := mvarIdsNew ++ (← getUnsolvedGoals) catch ex => logException ex mvarIdsNew := mvarIdsNew.push mvarId setGoals mvarIdsNew.toList @[builtinTactic Parser.Tactic.anyGoals] def evalAnyGoals : Tactic := fun stx => do let mvarIds ← getGoals let mut mvarIdsNew := #[] let mut succeeded := false for mvarId in mvarIds do unless (← isExprMVarAssigned mvarId) do setGoals [mvarId] try evalTactic stx[1] mvarIdsNew := mvarIdsNew ++ (← getUnsolvedGoals) succeeded := true catch _ => mvarIdsNew := mvarIdsNew.push mvarId unless succeeded do throwError "failed on all goals" setGoals mvarIdsNew.toList @[builtinTactic tacticSeq] def evalTacticSeq : Tactic := fun stx => evalTactic stx[0] partial def evalChoiceAux (tactics : Array Syntax) (i : Nat) : TacticM Unit := if h : i < tactics.size then let tactic := tactics.get ⟨i, h⟩ catchInternalId unsupportedSyntaxExceptionId (evalTactic tactic) (fun _ => evalChoiceAux tactics (i+1)) else throwUnsupportedSyntax @[builtinTactic choice] def evalChoice : Tactic := fun stx => evalChoiceAux stx.getArgs 0 @[builtinTactic skip] def evalSkip : Tactic := fun stx => pure () @[builtinTactic unknown] def evalUnknown : Tactic := fun stx => do addCompletionInfo <| CompletionInfo.tactic stx (← getGoals) @[builtinTactic failIfSuccess] def evalFailIfSuccess : Tactic := fun stx => do let tactic := stx[1] if (← try evalTactic tactic; pure true catch _ => pure false) then throwError "tactic succeeded" @[builtinTactic traceState] def evalTraceState : Tactic := fun stx => do let gs ← getUnsolvedGoals logInfo (goalsToMessageData gs) @[builtinTactic Lean.Parser.Tactic.assumption] def evalAssumption : Tactic := fun stx => liftMetaTactic fun mvarId => do Meta.assumption mvarId; pure [] @[builtinTactic Lean.Parser.Tactic.contradiction] def evalContradiction : Tactic := fun stx => liftMetaTactic fun mvarId => do Meta.contradiction mvarId; pure [] @[builtinTactic Lean.Parser.Tactic.intro] def evalIntro : Tactic := fun stx => do match stx with | `(tactic| intro) => introStep `_ | `(tactic| intro $h:ident) => introStep h.getId | `(tactic| intro _) => introStep `_ | `(tactic| intro $pat:term) => evalTactic (← `(tactic| intro h; match h with | $pat:term => ?_; try clear h)) | `(tactic| intro $h:term $hs:term*) => evalTactic (← `(tactic| intro $h:term; intro $hs:term*)) | _ => throwUnsupportedSyntax where introStep (n : Name) : TacticM Unit := liftMetaTactic fun mvarId => do let (_, mvarId) ← Meta.intro mvarId n pure [mvarId] @[builtinTactic Lean.Parser.Tactic.introMatch] def evalIntroMatch : Tactic := fun stx => do let matchAlts := stx[1] let stxNew ← liftMacroM <| Term.expandMatchAltsIntoMatchTactic stx matchAlts withMacroExpansion stx stxNew <| evalTactic stxNew @[builtinTactic «intros»] def evalIntros : Tactic := fun stx => match stx with | `(tactic| intros) => liftMetaTactic fun mvarId => do let (_, mvarId) ← Meta.intros mvarId return [mvarId] | `(tactic| intros $ids*) => liftMetaTactic fun mvarId => do let (_, mvarId) ← Meta.introN mvarId ids.size (ids.map getNameOfIdent').toList return [mvarId] | _ => throwUnsupportedSyntax @[builtinTactic Lean.Parser.Tactic.revert] def evalRevert : Tactic := fun stx => match stx with | `(tactic| revert $hs*) => do let (_, mvarId) ← Meta.revert (← getMainGoal) (← getFVarIds hs) replaceMainGoal [mvarId] | _ => throwUnsupportedSyntax @[builtinTactic Lean.Parser.Tactic.clear] def evalClear : Tactic := fun stx => match stx with | `(tactic| clear $hs*) => do let fvarIds ← getFVarIds hs let fvarIds ← withMainContext <| sortFVarIds fvarIds for fvarId in fvarIds.reverse do withMainContext do let mvarId ← clear (← getMainGoal) fvarId replaceMainGoal [mvarId] | _ => throwUnsupportedSyntax def forEachVar (hs : Array Syntax) (tac : MVarId → FVarId → MetaM MVarId) : TacticM Unit := do for h in hs do withMainContext do let fvarId ← getFVarId h let mvarId ← tac (← getMainGoal) (← getFVarId h) replaceMainGoal [mvarId] @[builtinTactic Lean.Parser.Tactic.subst] def evalSubst : Tactic := fun stx => match stx with | `(tactic| subst $hs*) => forEachVar hs Meta.subst | _ => throwUnsupportedSyntax /-- First method searches for a metavariable `g` s.t. `tag` is a suffix of its name. If none is found, then it searches for a metavariable `g` s.t. `tag` is a prefix of its name. -/ private def findTag? (mvarIds : List MVarId) (tag : Name) : TacticM (Option MVarId) := do let mvarId? ← mvarIds.findM? fun mvarId => return tag.isSuffixOf (← getMVarDecl mvarId).userName match mvarId? with | some mvarId => return mvarId | none => mvarIds.findM? fun mvarId => return tag.isPrefixOf (← getMVarDecl mvarId).userName def renameInaccessibles (mvarId : MVarId) (hs : Array Syntax) : TacticM MVarId := do if hs.isEmpty then return mvarId else let mvarDecl ← getMVarDecl mvarId let mut lctx := mvarDecl.lctx let mut hs := hs let mut found : NameSet := {} let n := lctx.numIndices for i in [:n] do let j := n - i - 1 match lctx.getAt? j with | none => pure () | some localDecl => if localDecl.userName.hasMacroScopes || found.contains localDecl.userName then let h := hs.back if h.isIdent then let newName := h.getId lctx := lctx.setUserName localDecl.fvarId newName hs := hs.pop if hs.isEmpty then break found := found.insert localDecl.userName unless hs.isEmpty do logError m!"too many variable names provided" let mvarNew ← mkFreshExprMVarAt lctx mvarDecl.localInstances mvarDecl.type MetavarKind.syntheticOpaque mvarDecl.userName assignExprMVar mvarId mvarNew return mvarNew.mvarId! @[builtinTactic «case»] def evalCase : Tactic | stx@`(tactic| case $tag $hs* =>%$arr $tac:tacticSeq) => do let gs ← getUnsolvedGoals let g ← if tag.isIdent then let tag := tag.getId let some g ← findTag? gs tag | throwError "tag not found" pure g else getMainGoal let gs := gs.erase g let g ← renameInaccessibles g hs setGoals [g] let savedTag ← getMVarTag g setMVarTag g Name.anonymous try withCaseRef arr tac do closeUsingOrAdmit (withTacticInfoContext stx (evalTactic tac)) finally setMVarTag g savedTag done setGoals gs | _ => throwUnsupportedSyntax @[builtinTactic «renameI»] def evalRenameInaccessibles : Tactic | stx@`(tactic| rename_i $hs*) => do replaceMainGoal [← renameInaccessibles (← getMainGoal) hs] | _ => throwUnsupportedSyntax @[builtinTactic «first»] partial def evalFirst : Tactic := fun stx => do let tacs := stx[1].getArgs if tacs.isEmpty then throwUnsupportedSyntax loop tacs 0 where loop (tacs : Array Syntax) (i : Nat) := if i == tacs.size - 1 then evalTactic tacs[i][1] else evalTactic tacs[i][1] <|> loop tacs (i+1) end Lean.Elab.Tactic
0f4eb2768dcfa38d3cf9980e3e415581003de91c
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/algebra/module/basic.lean
a72ce6cc45ebe34f26dfe7cf37e323f20ef69059
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
19,470
lean
/- Copyright (c) 2015 Nathaniel Thomas. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nathaniel Thomas, Jeremy Avigad, Johannes Hölzl, Mario Carneiro -/ import algebra.big_operators.basic import algebra.smul_with_zero import group_theory.group_action.group /-! # Modules over a ring In this file we define * `module R M` : an additive commutative monoid `M` is a `module` over a `semiring R` if for `r : R` and `x : M` their "scalar multiplication `r • x : M` is defined, and the operation `•` satisfies some natural associativity and distributivity axioms similar to those on a ring. ## Implementation notes In typical mathematical usage, our definition of `module` corresponds to "semimodule", and the word "module" is reserved for `module R M` where `R` is a `ring` and `M` an `add_comm_group`. If `R` is a `field` and `M` an `add_comm_group`, `M` would be called an `R`-vector space. Since those assumptions can be made by changing the typeclasses applied to `R` and `M`, without changing the axioms in `module`, mathlib calls everything a `module`. In older versions of mathlib, we had separate `semimodule` and `vector_space` abbreviations. This caused inference issues in some cases, while not providing any real advantages, so we decided to use a canonical `module` typeclass throughout. ## Tags semimodule, module, vector space -/ open function open_locale big_operators universes u u' v w x y z variables {R : Type u} {k : Type u'} {S : Type v} {M : Type w} {M₂ : Type x} {M₃ : Type y} {ι : Type z} /-- A module is a generalization of vector spaces to a scalar semiring. It consists of a scalar semiring `R` and an additive monoid of "vectors" `M`, connected by a "scalar multiplication" operation `r • x : M` (where `r : R` and `x : M`) with some natural associativity and distributivity axioms similar to those on a ring. -/ @[protect_proj] class module (R : Type u) (M : Type v) [semiring R] [add_comm_monoid M] extends distrib_mul_action R M := (add_smul : ∀(r s : R) (x : M), (r + s) • x = r • x + s • x) (zero_smul : ∀x : M, (0 : R) • x = 0) section add_comm_monoid variables [semiring R] [add_comm_monoid M] [module R M] (r s : R) (x y : M) /-- A module over a semiring automatically inherits a `mul_action_with_zero` structure. -/ @[priority 100] -- see Note [lower instance priority] instance module.to_mul_action_with_zero : mul_action_with_zero R M := { smul_zero := smul_zero, zero_smul := module.zero_smul, ..(infer_instance : mul_action R M) } instance add_comm_monoid.nat_module : module ℕ M := { one_smul := one_nsmul, mul_smul := λ m n a, mul_nsmul a m n, smul_add := λ n a b, nsmul_add a b n, smul_zero := nsmul_zero, zero_smul := zero_nsmul, add_smul := λ r s x, add_nsmul x r s } theorem add_smul : (r + s) • x = r • x + s • x := module.add_smul r s x variables (R) theorem two_smul : (2 : R) • x = x + x := by rw [bit0, add_smul, one_smul] theorem two_smul' : (2 : R) • x = bit0 x := two_smul R x /-- Pullback a `module` structure along an injective additive monoid homomorphism. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.module [add_comm_monoid M₂] [has_scalar R M₂] (f : M₂ →+ M) (hf : injective f) (smul : ∀ (c : R) x, f (c • x) = c • f x) : module R M₂ := { smul := (•), add_smul := λ c₁ c₂ x, hf $ by simp only [smul, f.map_add, add_smul], zero_smul := λ x, hf $ by simp only [smul, zero_smul, f.map_zero], .. hf.distrib_mul_action f smul } /-- Pushforward a `module` structure along a surjective additive monoid homomorphism. -/ protected def function.surjective.module [add_comm_monoid M₂] [has_scalar R M₂] (f : M →+ M₂) (hf : surjective f) (smul : ∀ (c : R) x, f (c • x) = c • f x) : module R M₂ := { smul := (•), add_smul := λ c₁ c₂ x, by { rcases hf x with ⟨x, rfl⟩, simp only [add_smul, ← smul, ← f.map_add] }, zero_smul := λ x, by { rcases hf x with ⟨x, rfl⟩, simp only [← f.map_zero, ← smul, zero_smul] }, .. hf.distrib_mul_action f smul } variables {R} (M) /-- Compose a `module` with a `ring_hom`, with action `f s • m`. See note [reducible non-instances]. -/ @[reducible] def module.comp_hom [semiring S] (f : S →+* R) : module S M := { smul := has_scalar.comp.smul f, add_smul := λ r s x, by simp [add_smul], .. mul_action_with_zero.comp_hom M f.to_monoid_with_zero_hom, .. distrib_mul_action.comp_hom M (f : S →* R) } variables (R) (M) /-- `(•)` as an `add_monoid_hom`. -/ def smul_add_hom : R →+ M →+ M := { to_fun := const_smul_hom M, map_zero' := add_monoid_hom.ext $ λ r, by simp, map_add' := λ x y, add_monoid_hom.ext $ λ r, by simp [add_smul] } variables {R M} @[simp] lemma smul_add_hom_apply (r : R) (x : M) : smul_add_hom R M r x = r • x := rfl @[simp] lemma smul_add_hom_one {R M : Type*} [semiring R] [add_comm_monoid M] [module R M] : smul_add_hom R M 1 = add_monoid_hom.id _ := const_smul_hom_one lemma module.eq_zero_of_zero_eq_one (zero_eq_one : (0 : R) = 1) : x = 0 := by rw [←one_smul R x, ←zero_eq_one, zero_smul] lemma list.sum_smul {l : list R} {x : M} : l.sum • x = (l.map (λ r, r • x)).sum := ((smul_add_hom R M).flip x).map_list_sum l lemma multiset.sum_smul {l : multiset R} {x : M} : l.sum • x = (l.map (λ r, r • x)).sum := ((smul_add_hom R M).flip x).map_multiset_sum l lemma finset.sum_smul {f : ι → R} {s : finset ι} {x : M} : (∑ i in s, f i) • x = (∑ i in s, (f i) • x) := ((smul_add_hom R M).flip x).map_sum f s end add_comm_monoid variables (R) /-- An `add_comm_monoid` that is a `module` over a `ring` carries a natural `add_comm_group` structure. -/ def module.add_comm_monoid_to_add_comm_group [ring R] [add_comm_monoid M] [module R M] : add_comm_group M := { neg := λ a, (-1 : R) • a, add_left_neg := λ a, show (-1 : R) • a + a = 0, by { nth_rewrite 1 ← one_smul _ a, rw [← add_smul, add_left_neg, zero_smul] }, ..(infer_instance : add_comm_monoid M), } variables {R} section add_comm_group variables (R M) [semiring R] [add_comm_group M] instance add_comm_group.int_module : module ℤ M := { one_smul := one_gsmul, mul_smul := λ m n a, mul_gsmul a m n, smul_add := λ n a b, gsmul_add a b n, smul_zero := gsmul_zero, zero_smul := zero_gsmul, add_smul := λ r s x, add_gsmul x r s } /-- A structure containing most informations as in a module, except the fields `zero_smul` and `smul_zero`. As these fields can be deduced from the other ones when `M` is an `add_comm_group`, this provides a way to construct a module structure by checking less properties, in `module.of_core`. -/ @[nolint has_inhabited_instance] structure module.core extends has_scalar R M := (smul_add : ∀(r : R) (x y : M), r • (x + y) = r • x + r • y) (add_smul : ∀(r s : R) (x : M), (r + s) • x = r • x + s • x) (mul_smul : ∀(r s : R) (x : M), (r * s) • x = r • s • x) (one_smul : ∀x : M, (1 : R) • x = x) variables {R M} /-- Define `module` without proving `zero_smul` and `smul_zero` by using an auxiliary structure `module.core`, when the underlying space is an `add_comm_group`. -/ def module.of_core (H : module.core R M) : module R M := by letI := H.to_has_scalar; exact { zero_smul := λ x, (add_monoid_hom.mk' (λ r : R, r • x) (λ r s, H.add_smul r s x)).map_zero, smul_zero := λ r, (add_monoid_hom.mk' ((•) r) (H.smul_add r)).map_zero, ..H } end add_comm_group /-- To prove two module structures on a fixed `add_comm_monoid` agree, it suffices to check the scalar multiplications agree. -/ -- We'll later use this to show `module ℕ M` and `module ℤ M` are subsingletons. @[ext] lemma module_ext {R : Type*} [semiring R] {M : Type*} [add_comm_monoid M] (P Q : module R M) (w : ∀ (r : R) (m : M), by { haveI := P, exact r • m } = by { haveI := Q, exact r • m }) : P = Q := begin unfreezingI { rcases P with ⟨⟨⟨⟨P⟩⟩⟩⟩, rcases Q with ⟨⟨⟨⟨Q⟩⟩⟩⟩ }, congr, funext r m, exact w r m, all_goals { apply proof_irrel_heq }, end section module variables [ring R] [add_comm_group M] [module R M] (r s : R) (x y : M) @[simp] theorem neg_smul : -r • x = - (r • x) := eq_neg_of_add_eq_zero (by rw [← add_smul, add_left_neg, zero_smul]) @[simp] theorem units.neg_smul (u : units R) (x : M) : -u • x = - (u • x) := by rw [units.smul_def, units.coe_neg, neg_smul, units.smul_def] variables (R) theorem neg_one_smul (x : M) : (-1 : R) • x = -x := by simp variables {R} theorem sub_smul (r s : R) (y : M) : (r - s) • y = r • y - s • y := by simp [add_smul, sub_eq_add_neg] end module /-- A module over a `subsingleton` semiring is a `subsingleton`. We cannot register this as an instance because Lean has no way to guess `R`. -/ protected theorem module.subsingleton (R M : Type*) [semiring R] [subsingleton R] [add_comm_monoid M] [module R M] : subsingleton M := ⟨λ x y, by rw [← one_smul R x, ← one_smul R y, subsingleton.elim (1:R) 0, zero_smul, zero_smul]⟩ @[priority 910] -- see Note [lower instance priority] instance semiring.to_module [semiring R] : module R R := { smul_add := mul_add, add_smul := add_mul, zero_smul := zero_mul, smul_zero := mul_zero } @[priority 910] -- see Note [lower instance priority] instance semiring.to_opposite_module [semiring R] : module Rᵒᵖ R := { smul_add := λ r x y, add_mul _ _ _, add_smul := λ r x y, mul_add _ _ _, ..monoid_with_zero.to_opposite_mul_action_with_zero R} /-- A ring homomorphism `f : R →+* M` defines a module structure by `r • x = f r * x`. -/ def ring_hom.to_module [semiring R] [semiring S] (f : R →+* S) : module R S := module.comp_hom S f section add_comm_monoid variables [semiring R] [add_comm_monoid M] [module R M] section variables (R) /-- `nsmul` is equal to any other module structure via a cast. -/ lemma nsmul_eq_smul_cast (n : ℕ) (b : M) : n • b = (n : R) • b := begin induction n with n ih, { rw [nat.cast_zero, zero_smul, zero_smul] }, { rw [nat.succ_eq_add_one, nat.cast_succ, add_smul, add_smul, one_smul, ih, one_smul], } end end /-- Convert back any exotic `ℕ`-smul to the canonical instance. This should not be needed since in mathlib all `add_comm_monoid`s should normally have exactly one `ℕ`-module structure by design. -/ lemma nat_smul_eq_nsmul (h : module ℕ M) (n : ℕ) (x : M) : @has_scalar.smul ℕ M h.to_has_scalar n x = n • x := by rw [nsmul_eq_smul_cast ℕ n x, nat.cast_id] /-- All `ℕ`-module structures are equal. Not an instance since in mathlib all `add_comm_monoid` should normally have exactly one `ℕ`-module structure by design. -/ def add_comm_monoid.nat_module.unique : unique (module ℕ M) := { default := by apply_instance, uniq := λ P, module_ext P _ $ λ n, nat_smul_eq_nsmul P n } instance add_comm_monoid.nat_is_scalar_tower : is_scalar_tower ℕ R M := { smul_assoc := λ n x y, nat.rec_on n (by simp only [zero_smul]) (λ n ih, by simp only [nat.succ_eq_add_one, add_smul, one_smul, ih]) } instance add_comm_monoid.nat_smul_comm_class : smul_comm_class ℕ R M := { smul_comm := λ n r m, nat.rec_on n (by simp only [zero_smul, smul_zero]) (λ n ih, by simp only [nat.succ_eq_add_one, add_smul, one_smul, ←ih, smul_add]) } -- `smul_comm_class.symm` is not registered as an instance, as it would cause a loop instance add_comm_monoid.nat_smul_comm_class' : smul_comm_class R ℕ M := smul_comm_class.symm _ _ _ end add_comm_monoid section add_comm_group variables [semiring S] [ring R] [add_comm_group M] [module S M] [module R M] section variables (R) /-- `gsmul` is equal to any other module structure via a cast. -/ lemma gsmul_eq_smul_cast (n : ℤ) (b : M) : n • b = (n : R) • b := begin induction n using int.induction_on with p hp n hn, { rw [int.cast_zero, zero_smul, zero_smul] }, { rw [int.cast_add, int.cast_one, add_smul, add_smul, one_smul, one_smul, hp] }, { rw [int.cast_sub, int.cast_one, sub_smul, sub_smul, one_smul, one_smul, hn] }, end end /-- Convert back any exotic `ℤ`-smul to the canonical instance. This should not be needed since in mathlib all `add_comm_group`s should normally have exactly one `ℤ`-module structure by design. -/ lemma int_smul_eq_gsmul (h : module ℤ M) (n : ℤ) (x : M) : @has_scalar.smul ℤ M h.to_has_scalar n x = n • x := by rw [gsmul_eq_smul_cast ℤ n x, int.cast_id] /-- All `ℤ`-module structures are equal. Not an instance since in mathlib all `add_comm_group` should normally have exactly one `ℤ`-module structure by design. -/ def add_comm_group.int_module.unique : unique (module ℤ M) := { default := by apply_instance, uniq := λ P, module_ext P _ $ λ n, int_smul_eq_gsmul P n } instance add_comm_group.int_is_scalar_tower : is_scalar_tower ℤ R M := { smul_assoc := λ n x y, int.induction_on n (by simp only [zero_smul]) (λ n ih, by simp only [one_smul, add_smul, ih]) (λ n ih, by simp only [one_smul, sub_smul, ih]) } instance add_comm_group.int_smul_comm_class : smul_comm_class ℤ S M := { smul_comm := λ n x y, int.induction_on n (by simp only [zero_smul, smul_zero]) (λ n ih, by simp only [one_smul, add_smul, smul_add, ih]) (λ n ih, by simp only [one_smul, sub_smul, smul_sub, ih]) } -- `smul_comm_class.symm` is not registered as an instance, as it would cause a loop instance add_comm_group.int_smul_comm_class' : smul_comm_class S ℤ M := smul_comm_class.symm _ _ _ end add_comm_group namespace add_monoid_hom lemma map_nat_module_smul [add_comm_monoid M] [add_comm_monoid M₂] (f : M →+ M₂) (x : ℕ) (a : M) : f (x • a) = x • f a := by simp only [f.map_nsmul] lemma map_int_module_smul [add_comm_group M] [add_comm_group M₂] (f : M →+ M₂) (x : ℤ) (a : M) : f (x • a) = x • f a := by simp only [f.map_gsmul] lemma map_int_cast_smul [ring R] [add_comm_group M] [add_comm_group M₂] [module R M] [module R M₂] (f : M →+ M₂) (x : ℤ) (a : M) : f ((x : R) • a) = (x : R) • f a := by simp only [←gsmul_eq_smul_cast, f.map_gsmul] lemma map_nat_cast_smul [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [module R M] [module R M₂] (f : M →+ M₂) (x : ℕ) (a : M) : f ((x : R) • a) = (x : R) • f a := by simp only [←nsmul_eq_smul_cast, f.map_nsmul] lemma map_rat_cast_smul {R : Type*} [division_ring R] [char_zero R] {E : Type*} [add_comm_group E] [module R E] {F : Type*} [add_comm_group F] [module R F] (f : E →+ F) (c : ℚ) (x : E) : f ((c : R) • x) = (c : R) • f x := begin have : ∀ (x : E) (n : ℕ), 0 < n → f (((n⁻¹ : ℚ) : R) • x) = ((n⁻¹ : ℚ) : R) • f x, { intros x n hn, replace hn : (n : R) ≠ 0 := nat.cast_ne_zero.2 (ne_of_gt hn), conv_rhs { congr, skip, rw [← one_smul R x, ← mul_inv_cancel hn, mul_smul] }, rw [f.map_nat_cast_smul, smul_smul, rat.cast_inv, rat.cast_coe_nat, inv_mul_cancel hn, one_smul] }, refine c.num_denom_cases_on (λ m n hn hmn, _), rw [rat.mk_eq_div, div_eq_mul_inv, rat.cast_mul, int.cast_coe_nat, mul_smul, mul_smul, rat.cast_coe_int, f.map_int_cast_smul, this _ n hn] end lemma map_rat_module_smul {E : Type*} [add_comm_group E] [module ℚ E] {F : Type*} [add_comm_group F] [module ℚ F] (f : E →+ F) (c : ℚ) (x : E) : f (c • x) = c • f x := rat.cast_id c ▸ f.map_rat_cast_smul c x end add_monoid_hom section no_zero_smul_divisors /-! ### `no_zero_smul_divisors` This section defines the `no_zero_smul_divisors` class, and includes some tests for the vanishing of elements (especially in modules over division rings). -/ /-- `no_zero_smul_divisors R M` states that a scalar multiple is `0` only if either argument is `0`. This a version of saying that `M` is torsion free, without assuming `R` is zero-divisor free. The main application of `no_zero_smul_divisors R M`, when `M` is a module, is the result `smul_eq_zero`: a scalar multiple is `0` iff either argument is `0`. It is a generalization of the `no_zero_divisors` class to heterogeneous multiplication. -/ class no_zero_smul_divisors (R M : Type*) [has_zero R] [has_zero M] [has_scalar R M] : Prop := (eq_zero_or_eq_zero_of_smul_eq_zero : ∀ {c : R} {x : M}, c • x = 0 → c = 0 ∨ x = 0) export no_zero_smul_divisors (eq_zero_or_eq_zero_of_smul_eq_zero) section module variables [semiring R] [add_comm_monoid M] [module R M] instance no_zero_smul_divisors.of_no_zero_divisors [no_zero_divisors R] : no_zero_smul_divisors R R := ⟨λ c x, no_zero_divisors.eq_zero_or_eq_zero_of_mul_eq_zero⟩ @[simp] theorem smul_eq_zero [no_zero_smul_divisors R M] {c : R} {x : M} : c • x = 0 ↔ c = 0 ∨ x = 0 := ⟨eq_zero_or_eq_zero_of_smul_eq_zero, λ h, h.elim (λ h, h.symm ▸ zero_smul R x) (λ h, h.symm ▸ smul_zero c)⟩ theorem smul_ne_zero [no_zero_smul_divisors R M] {c : R} {x : M} : c • x ≠ 0 ↔ c ≠ 0 ∧ x ≠ 0 := by simp only [ne.def, smul_eq_zero, not_or_distrib] section nat variables (R) (M) [no_zero_smul_divisors R M] [char_zero R] include R lemma nat.no_zero_smul_divisors : no_zero_smul_divisors ℕ M := ⟨by { intros c x, rw [nsmul_eq_smul_cast R, smul_eq_zero], simp }⟩ variables {M} lemma eq_zero_of_smul_two_eq_zero {v : M} (hv : 2 • v = 0) : v = 0 := by haveI := nat.no_zero_smul_divisors R M; exact (smul_eq_zero.mp hv).resolve_left (by norm_num) end nat end module section add_comm_group -- `R` can still be a semiring here variables [semiring R] [add_comm_group M] [module R M] section smul_injective variables (M) lemma smul_right_injective [no_zero_smul_divisors R M] {c : R} (hc : c ≠ 0) : function.injective (λ (x : M), c • x) := λ x y h, sub_eq_zero.mp ((smul_eq_zero.mp (calc c • (x - y) = c • x - c • y : smul_sub c x y ... = 0 : sub_eq_zero.mpr h)).resolve_left hc) end smul_injective section nat variables (R) [no_zero_smul_divisors R M] [char_zero R] include R lemma eq_zero_of_eq_neg {v : M} (hv : v = - v) : v = 0 := begin haveI := nat.no_zero_smul_divisors R M, refine eq_zero_of_smul_two_eq_zero R _, rw two_smul, exact add_eq_zero_iff_eq_neg.mpr hv end end nat end add_comm_group section module variables [ring R] [add_comm_group M] [module R M] [no_zero_smul_divisors R M] section smul_injective variables (R) lemma smul_left_injective {x : M} (hx : x ≠ 0) : function.injective (λ (c : R), c • x) := λ c d h, sub_eq_zero.mp ((smul_eq_zero.mp (calc (c - d) • x = c • x - d • x : sub_smul c d x ... = 0 : sub_eq_zero.mpr h)).resolve_right hx) end smul_injective section nat variables [char_zero R] lemma ne_neg_of_ne_zero [no_zero_divisors R] {v : R} (hv : v ≠ 0) : v ≠ -v := λ h, hv (eq_zero_of_eq_neg R h) end nat end module section division_ring variables [division_ring R] [add_comm_group M] [module R M] @[priority 100] -- see note [lower instance priority] instance no_zero_smul_divisors.of_division_ring : no_zero_smul_divisors R M := ⟨λ c x h, or_iff_not_imp_left.2 $ λ hc, (smul_eq_zero_iff_eq' hc).1 h⟩ end division_ring end no_zero_smul_divisors @[simp] lemma nat.smul_one_eq_coe {R : Type*} [semiring R] (m : ℕ) : m • (1 : R) = ↑m := by rw [nsmul_eq_mul, mul_one] @[simp] lemma int.smul_one_eq_coe {R : Type*} [ring R] (m : ℤ) : m • (1 : R) = ↑m := by rw [gsmul_eq_mul, mul_one]
fdc2172034539feb7d13c8d59987205627253077
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/lie/direct_sum.lean
1b8a6dd8e8ed73682b31ba6aa6e8486d4fdf3ca0
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
8,638
lean
/- Copyright (c) 2020 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import algebra.direct_sum.module import algebra.lie.of_associative import algebra.lie.submodule import algebra.lie.basic /-! # Direct sums of Lie algebras and Lie modules Direct sums of Lie algebras and Lie modules carry natural algebra and module structures. ## Tags lie algebra, lie module, direct sum -/ universes u v w w₁ namespace direct_sum open dfinsupp open_locale direct_sum variables {R : Type u} {ι : Type v} [comm_ring R] section modules /-! The direct sum of Lie modules over a fixed Lie algebra carries a natural Lie module structure. -/ variables {L : Type w₁} {M : ι → Type w} variables [lie_ring L] [lie_algebra R L] variables [Π i, add_comm_group (M i)] [Π i, module R (M i)] variables [Π i, lie_ring_module L (M i)] [Π i, lie_module R L (M i)] instance : lie_ring_module L (⨁ i, M i) := { bracket := λ x m, m.map_range (λ i m', ⁅x, m'⁆) (λ i, lie_zero x), add_lie := λ x y m, by { ext, simp only [map_range_apply, add_apply, add_lie], }, lie_add := λ x m n, by { ext, simp only [map_range_apply, add_apply, lie_add], }, leibniz_lie := λ x y m, by { ext, simp only [map_range_apply, lie_lie, add_apply, sub_add_cancel], }, } @[simp] lemma lie_module_bracket_apply (x : L) (m : ⨁ i, M i) (i : ι) : ⁅x, m⁆ i = ⁅x, m i⁆ := map_range_apply _ _ m i instance : lie_module R L (⨁ i, M i) := { smul_lie := λ t x m, by { ext i, simp only [smul_lie, lie_module_bracket_apply, smul_apply], }, lie_smul := λ t x m, by { ext i, simp only [lie_smul, lie_module_bracket_apply, smul_apply], }, } variables (R ι L M) /-- The inclusion of each component into a direct sum as a morphism of Lie modules. -/ def lie_module_of [decidable_eq ι] (j : ι) : M j →ₗ⁅R,L⁆ ⨁ i, M i := { map_lie' := λ x m, begin ext i, by_cases h : j = i, { rw ← h, simp, }, { simp [lof, single_eq_of_ne h], }, end, ..lof R ι M j } /-- The projection map onto one component, as a morphism of Lie modules. -/ def lie_module_component (j : ι) : (⨁ i, M i) →ₗ⁅R,L⁆ M j := { map_lie' := λ x m, by simp only [component, lapply_apply, lie_module_bracket_apply, linear_map.to_fun_eq_coe], ..component R ι M j } end modules section algebras /-! The direct sum of Lie algebras carries a natural Lie algebra structure. -/ variables (L : ι → Type w) variables [Π i, lie_ring (L i)] [Π i, lie_algebra R (L i)] instance lie_ring : lie_ring (⨁ i, L i) := { bracket := zip_with (λ i, λ x y, ⁅x, y⁆) (λ i, lie_zero 0), add_lie := λ x y z, by { ext, simp only [zip_with_apply, add_apply, add_lie], }, lie_add := λ x y z, by { ext, simp only [zip_with_apply, add_apply, lie_add], }, lie_self := λ x, by { ext, simp only [zip_with_apply, add_apply, lie_self, zero_apply], }, leibniz_lie := λ x y z, by { ext, simp only [sub_apply, zip_with_apply, add_apply, zero_apply], apply leibniz_lie, }, ..(infer_instance : add_comm_group _) } @[simp] lemma bracket_apply (x y : ⨁ i, L i) (i : ι) : ⁅x, y⁆ i = ⁅x i, y i⁆ := zip_with_apply _ _ x y i instance lie_algebra : lie_algebra R (⨁ i, L i) := { lie_smul := λ c x y, by { ext, simp only [ zip_with_apply, smul_apply, bracket_apply, lie_smul] }, ..(infer_instance : module R _) } variables (R ι L) /-- The inclusion of each component into the direct sum as morphism of Lie algebras. -/ @[simps] def lie_algebra_of [decidable_eq ι] (j : ι) : L j →ₗ⁅R⁆ ⨁ i, L i := { to_fun := of L j, map_lie' := λ x y, by { ext i, by_cases h : j = i, { rw ← h, simp [of], }, { simp [of, single_eq_of_ne h], }, }, ..lof R ι L j, } /-- The projection map onto one component, as a morphism of Lie algebras. -/ @[simps] def lie_algebra_component (j : ι) : (⨁ i, L i) →ₗ⁅R⁆ L j := { to_fun := component R ι L j, map_lie' := λ x y, by simp only [component, bracket_apply, lapply_apply, linear_map.to_fun_eq_coe], ..component R ι L j } @[ext] lemma lie_algebra_ext {x y : ⨁ i, L i} (h : ∀ i, lie_algebra_component R ι L i x = lie_algebra_component R ι L i y) : x = y := dfinsupp.ext h include R lemma lie_of_of_ne [decidable_eq ι] {i j : ι} (hij : j ≠ i) (x : L i) (y : L j) : ⁅of L i x, of L j y⁆ = 0 := begin apply lie_algebra_ext R ι L, intros k, rw lie_hom.map_lie, simp only [component, of, lapply_apply, single_add_hom_apply, lie_algebra_component_apply, single_apply, zero_apply], by_cases hik : i = k, { simp only [dif_neg, not_false_iff, lie_zero, hik.symm, hij], }, { simp only [dif_neg, not_false_iff, zero_lie, hik], }, end lemma lie_of_of_eq [decidable_eq ι] {i j : ι} (hij : j = i) (x : L i) (y : L j) : ⁅of L i x, of L j y⁆ = of L i ⁅x, hij.rec_on y⁆ := begin have : of L j y = of L i (hij.rec_on y), { exact eq.drec (eq.refl _) hij, }, rw [this, ← lie_algebra_of_apply R ι L i ⁅x, hij.rec_on y⁆, lie_hom.map_lie, lie_algebra_of_apply, lie_algebra_of_apply], end @[simp] lemma lie_of [decidable_eq ι] {i j : ι} (x : L i) (y : L j) : ⁅of L i x, of L j y⁆ = if hij : j = i then lie_algebra_of R ι L i ⁅x, hij.rec_on y⁆ else 0 := begin by_cases hij : j = i, { simp only [lie_of_of_eq R ι L hij x y, hij, dif_pos, not_false_iff, lie_algebra_of_apply], }, { simp only [lie_of_of_ne R ι L hij x y, hij, dif_neg, not_false_iff], }, end variables {R L ι} /-- Given a family of Lie algebras `L i`, together with a family of morphisms of Lie algebras `f i : L i →ₗ⁅R⁆ L'` into a fixed Lie algebra `L'`, we have a natural linear map: `(⨁ i, L i) →ₗ[R] L'`. If in addition `⁅f i x, f j y⁆ = 0` for any `x ∈ L i` and `y ∈ L j` (`i ≠ j`) then this map is a morphism of Lie algebras. -/ @[simps] def to_lie_algebra [decidable_eq ι] (L' : Type w₁) [lie_ring L'] [lie_algebra R L'] (f : Π i, L i →ₗ⁅R⁆ L') (hf : ∀ (i j : ι), i ≠ j → ∀ (x : L i) (y : L j), ⁅f i x, f j y⁆ = 0) : (⨁ i, L i) →ₗ⁅R⁆ L' := { to_fun := to_module R ι L' (λ i, (f i : L i →ₗ[R] L')), map_lie' := λ x y, begin let f' := λ i, (f i : L i →ₗ[R] L'), /- The goal is linear in `y`. We can use this to reduce to the case that `y` has only one non-zero component. -/ suffices : ∀ (i : ι) (y : L i), to_module R ι L' f' ⁅x, of L i y⁆ = ⁅to_module R ι L' f' x, to_module R ι L' f' (of L i y)⁆, { simp only [← lie_algebra.ad_apply R], rw [← linear_map.comp_apply, ← linear_map.comp_apply], congr, clear y, ext i y, exact this i y, }, /- Similarly, we can reduce to the case that `x` has only one non-zero component. -/ suffices : ∀ i j (y : L i) (x : L j), to_module R ι L' f' ⁅of L j x, of L i y⁆ = ⁅to_module R ι L' f' (of L j x), to_module R ι L' f' (of L i y)⁆, { intros i y, rw [← lie_skew x, ← lie_skew (to_module R ι L' f' x)], simp only [linear_map.map_neg, neg_inj, ← lie_algebra.ad_apply R], rw [← linear_map.comp_apply, ← linear_map.comp_apply], congr, clear x, ext j x, exact this j i x y, }, /- Tidy up and use `lie_of`. -/ intros i j y x, simp only [lie_of R, lie_algebra_of_apply, lie_hom.coe_to_linear_map, to_add_monoid_of, coe_to_module_eq_coe_to_add_monoid, linear_map.to_add_monoid_hom_coe], /- And finish with trivial case analysis. -/ rcases eq_or_ne i j with h | h, { have h' : f j (h.rec_on y) = f i y, { exact eq.drec (eq.refl _) h, }, simp only [h, h', lie_hom.coe_to_linear_map, dif_pos, lie_hom.map_lie, to_add_monoid_of, linear_map.to_add_monoid_hom_coe], }, { simp only [h, hf j i h.symm x y, dif_neg, not_false_iff, add_monoid_hom.map_zero], }, end, .. to_module R ι L' (λ i, (f i : L i →ₗ[R] L')) } end algebras section ideals variables {L : Type w} [lie_ring L] [lie_algebra R L] (I : ι → lie_ideal R L) /-- The fact that this instance is necessary seems to be a bug in typeclass inference. See [this Zulip thread](https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/ Typeclass.20resolution.20under.20binders/near/245151099). -/ instance lie_ring_of_ideals : lie_ring (⨁ i, I i) := direct_sum.lie_ring (λ i, ↥(I i)) /-- See `direct_sum.lie_ring_of_ideals` comment. -/ instance lie_algebra_of_ideals : lie_algebra R (⨁ i, I i) := direct_sum.lie_algebra (λ i, ↥(I i)) end ideals end direct_sum
64f677a08ab6f8c3e767d6533d0b715515978a67
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/matrix/dmatrix.lean
4a80dde055876fc9e497f923fe693c79413bf2ed
[ "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
5,581
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.group.pi import data.fintype.basic /-! # Matrices > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. -/ universes u u' v w z /-- `dmatrix m n` is the type of dependently typed matrices whose rows are indexed by the fintype `m` and whose columns are indexed by the fintype `n`. -/ @[nolint unused_arguments] def dmatrix (m : Type u) (n : Type u') [fintype m] [fintype n] (α : m → n → Type v) : Type (max u u' v) := Π i j, α i j variables {l m n o : Type*} [fintype l] [fintype m] [fintype n] [fintype o] variables {α : m → n → Type v} namespace dmatrix section ext variables {M N : dmatrix m n α} theorem ext_iff : (∀ i j, M i j = N i j) ↔ M = N := ⟨λ h, funext $ λ i, funext $ h i, λ h, by simp [h]⟩ @[ext] theorem ext : (∀ i j, M i j = N i j) → M = N := ext_iff.mp end ext /-- `M.map f` is the dmatrix obtained by applying `f` to each entry of the matrix `M`. -/ def map (M : dmatrix m n α) {β : m → n → Type w} (f : Π ⦃i j⦄, α i j → β i j) : dmatrix m n β := λ i j, f (M i j) @[simp] lemma map_apply {M : dmatrix m n α} {β : m → n → Type w} {f : Π ⦃i j⦄, α i j → β i j} {i : m} {j : n} : M.map f i j = f (M i j) := rfl @[simp] lemma map_map {M : dmatrix m n α} {β : m → n → Type w} {γ : m → n → Type z} {f : Π ⦃i j⦄, α i j → β i j} {g : Π ⦃i j⦄, β i j → γ i j} : (M.map f).map g = M.map (λ i j x, g (f x)) := by { ext, simp, } /-- The transpose of a dmatrix. -/ def transpose (M : dmatrix m n α) : dmatrix n m (λ j i, α i j) | x y := M y x localized "postfix (name := dmatrix.transpose) `ᵀ`:1500 := dmatrix.transpose" in dmatrix /-- `dmatrix.col u` is the column matrix whose entries are given by `u`. -/ def col {α : m → Type v} (w : Π i, α i) : dmatrix m unit (λ i j, α i) | x y := w x /-- `dmatrix.row u` is the row matrix whose entries are given by `u`. -/ def row {α : n → Type v} (v : Π j, α j) : dmatrix unit n (λ i j, α j) | x y := v y instance [∀ i j, inhabited (α i j)] : inhabited (dmatrix m n α) := pi.inhabited _ instance [∀ i j, has_add (α i j)] : has_add (dmatrix m n α) := pi.has_add instance [∀ i j, add_semigroup (α i j)] : add_semigroup (dmatrix m n α) := pi.add_semigroup instance [∀ i j, add_comm_semigroup (α i j)] : add_comm_semigroup (dmatrix m n α) := pi.add_comm_semigroup instance [∀ i j, has_zero (α i j)] : has_zero (dmatrix m n α) := pi.has_zero instance [∀ i j, add_monoid (α i j)] : add_monoid (dmatrix m n α) := pi.add_monoid instance [∀ i j, add_comm_monoid (α i j)] : add_comm_monoid (dmatrix m n α) := pi.add_comm_monoid instance [∀ i j, has_neg (α i j)] : has_neg (dmatrix m n α) := pi.has_neg instance [∀ i j, has_sub (α i j)] : has_sub (dmatrix m n α) := pi.has_sub instance [∀ i j, add_group (α i j)] : add_group (dmatrix m n α) := pi.add_group instance [∀ i j, add_comm_group (α i j)] : add_comm_group (dmatrix m n α) := pi.add_comm_group instance [∀ i j, unique (α i j)] : unique (dmatrix m n α) := pi.unique instance [∀ i j, subsingleton (α i j)] : subsingleton (dmatrix m n α) := pi.subsingleton @[simp] theorem zero_apply [∀ i j, has_zero (α i j)] (i j) : (0 : dmatrix m n α) i j = 0 := rfl @[simp] theorem neg_apply [∀ i j, has_neg (α i j)] (M : dmatrix m n α) (i j) : (- M) i j = - M i j := rfl @[simp] theorem add_apply [∀ i j, has_add (α i j)] (M N : dmatrix m n α) (i j) : (M + N) i j = M i j + N i j := rfl @[simp] theorem sub_apply [∀ i j, has_sub (α i j)] (M N : dmatrix m n α) (i j) : (M - N) i j = M i j - N i j := rfl @[simp] lemma map_zero [∀ i j, has_zero (α i j)] {β : m → n → Type w} [∀ i j, has_zero (β i j)] {f : Π ⦃i j⦄, α i j → β i j} (h : ∀ i j, f (0 : α i j) = 0) : (0 : dmatrix m n α).map f = 0 := by { ext, simp [h], } lemma map_add [∀ i j, add_monoid (α i j)] {β : m → n → Type w} [∀ i j, add_monoid (β i j)] (f : Π ⦃i j⦄, α i j →+ β i j) (M N : dmatrix m n α) : (M + N).map (λ i j, @f i j) = M.map (λ i j, @f i j) + N.map (λ i j, @f i j) := by { ext, simp, } lemma map_sub [∀ i j, add_group (α i j)] {β : m → n → Type w} [∀ i j, add_group (β i j)] (f : Π ⦃i j⦄, α i j →+ β i j) (M N : dmatrix m n α) : (M - N).map (λ i j, @f i j) = M.map (λ i j, @f i j) - N.map (λ i j, @f i j) := by { ext, simp } instance subsingleton_of_empty_left [is_empty m] : subsingleton (dmatrix m n α) := ⟨λ M N, by { ext, exact is_empty_elim i }⟩ instance subsingleton_of_empty_right [is_empty n] : subsingleton (dmatrix m n α) := ⟨λ M N, by { ext, exact is_empty_elim j }⟩ end dmatrix /-- The `add_monoid_hom` between spaces of dependently typed matrices induced by an `add_monoid_hom` between their coefficients. -/ def add_monoid_hom.map_dmatrix [∀ i j, add_monoid (α i j)] {β : m → n → Type w} [∀ i j, add_monoid (β i j)] (f : Π ⦃i j⦄, α i j →+ β i j) : dmatrix m n α →+ dmatrix m n β := { to_fun := λ M, M.map (λ i j, @f i j), map_zero' := by simp, map_add' := dmatrix.map_add f, } @[simp] lemma add_monoid_hom.map_dmatrix_apply [∀ i j, add_monoid (α i j)] {β : m → n → Type w} [∀ i j, add_monoid (β i j)] (f : Π ⦃i j⦄, α i j →+ β i j) (M : dmatrix m n α) : add_monoid_hom.map_dmatrix f M = M.map (λ i j, @f i j) := rfl
0ec806ac6c55cc573c1aeaad89dec1d49bbc1ad5
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/lake/Lake/Util/OrderedTagAttribute.lean
5c4ef60d30b7db7930d634d94d26c7448a0ffc95
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
1,744
lean
/- Copyright (c) 2022 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ import Lean.Attributes open Lean namespace Lake structure OrderedTagAttribute where attr : AttributeImpl ext : PersistentEnvExtension Name Name (Array Name) deriving Inhabited def registerOrderedTagAttribute (name : Name) (descr : String) (validate : Name → AttrM Unit := fun _ => pure ()) (ref : Name := by exact decl_name%) : IO OrderedTagAttribute := do let ext ← registerPersistentEnvExtension { name := ref mkInitial := pure {} addImportedFn := fun _ _ => pure {} addEntryFn := fun s n => s.push n exportEntriesFn := fun es => es statsFn := fun s => "tag attribute" ++ Format.line ++ "number of local entries: " ++ format s.size } let attrImpl : AttributeImpl := { ref := ref name := name descr := descr add := fun decl stx kind => do Attribute.Builtin.ensureNoArgs stx unless kind == AttributeKind.global do throwError "invalid attribute '{name}', must be global" let env ← getEnv unless (env.getModuleIdxFor? decl).isNone do throwError "invalid attribute '{name}', declaration is in an imported module" validate decl modifyEnv fun env => ext.addEntry env decl } registerBuiltinAttribute attrImpl return { attr := attrImpl, ext } def OrderedTagAttribute.hasTag (attr : OrderedTagAttribute) (env : Environment) (decl : Name) : Bool := match env.getModuleIdxFor? decl with | some modIdx => (attr.ext.getModuleEntries env modIdx).binSearchContains decl Name.quickLt | none => (attr.ext.getState env).contains decl
2d28c0055c64d71ed8798d0aebeacd8906c6b360
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/doc/examples/Certora2022/ex24.lean
bd832421fb7e7b6e82bd3d17b0a7e92c3b408a38
[ "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
286
lean
/- induction tactic -/ example (as : List α) (a : α) : (as.concat a).length = as.length + 1 := by induction as with | nil => rfl | cons _ xs ih => simp [List.concat, ih] example (as : List α) (a : α) : (as.concat a).length = as.length + 1 := by induction as <;> simp! [*]
ae65e5b0f2a63a87f3d33b730da5917d98c886ff
d642a6b1261b2cbe691e53561ac777b924751b63
/src/topology/algebra/module.lean
c4c1fe3bfe587c122875f86cee27db22c920831e
[ "Apache-2.0" ]
permissive
cipher1024/mathlib
fee56b9954e969721715e45fea8bcb95f9dc03fe
d077887141000fefa5a264e30fa57520e9f03522
refs/heads/master
1,651,806,490,504
1,573,508,694,000
1,573,508,694,000
107,216,176
0
0
Apache-2.0
1,647,363,136,000
1,508,213,014,000
Lean
UTF-8
Lean
false
false
9,720
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jan-David Salchow, Sébastien Gouëzel, Jean Lo Theory of topological modules and continuous linear maps. -/ import topology.algebra.ring linear_algebra.basic ring_theory.algebra open topological_space universes u v w u' /-- A topological semimodule, over a semiring which is also a topological space, is a semimodule in which scalar multiplication is continuous. In applications, α will be a topological semiring and β a topological additive semigroup, but this is not needed for the definition -/ class topological_semimodule (α : Type u) (β : Type v) [semiring α] [topological_space α] [topological_space β] [add_comm_monoid β] [semimodule α β] : Prop := (continuous_smul : continuous (λp : α × β, p.1 • p.2)) section variables {α : Type u} {β : Type v} [semiring α] [topological_space α] [topological_space β] [add_comm_monoid β] [semimodule α β] [topological_semimodule α β] lemma continuous_smul' : continuous (λp:α×β, p.1 • p.2) := topological_semimodule.continuous_smul α β lemma continuous_smul {γ : Type*} [topological_space γ] {f : γ → α} {g : γ → β} (hf : continuous f) (hg : continuous g) : continuous (λp, f p • g p) := continuous_smul'.comp (hf.prod_mk hg) end /-- A topological module, over a ring which is also a topological space, is a module in which scalar multiplication is continuous. In applications, α will be a topological ring and β a topological additive group, but this is not needed for the definition -/ class topological_module (α : Type u) (β : Type v) [ring α] [topological_space α] [topological_space β] [add_comm_group β] [module α β] extends topological_semimodule α β : Prop /-- A topological vector space is a topological module over a field. -/ class topological_vector_space (α : Type u) (β : Type v) [discrete_field α] [topological_space α] [topological_space β] [add_comm_group β] [vector_space α β] extends topological_module α β /-- Continuous linear maps between modules. We only put the type classes that are necessary for the definition, although in applications β and γ will be topological modules over the topological ring α -/ structure continuous_linear_map (α : Type*) [ring α] (β : Type*) [topological_space β] [add_comm_group β] (γ : Type*) [topological_space γ] [add_comm_group γ] [module α β] [module α γ] extends linear_map α β γ := (cont : continuous to_fun) notation β ` →L[`:25 α `] ` γ := continuous_linear_map α β γ namespace continuous_linear_map section general_ring /- Properties that hold for non-necessarily commutative rings. -/ variables {α : Type*} [ring α] {β : Type*} [topological_space β] [add_comm_group β] {γ : Type*} [topological_space γ] [add_comm_group γ] {δ : Type*} [topological_space δ] [add_comm_group δ] [module α β] [module α γ] [module α δ] /-- Coerce continuous linear maps to linear maps. -/ instance : has_coe (β →L[α] γ) (β →ₗ[α] γ) := ⟨to_linear_map⟩ protected lemma continuous (f : β →L[α] γ) : continuous f := f.2 /-- Coerce continuous linear maps to functions. -/ instance to_fun : has_coe_to_fun $ β →L[α] γ := ⟨_, λ f, f.to_fun⟩ @[ext] theorem ext {f g : β →L[α] γ} (h : ∀ x, f x = g x) : f = g := by cases f; cases g; congr' 1; ext x; apply h theorem ext_iff {f g : β →L[α] γ} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, by rw h, by ext⟩ variables (c : α) (f g : β →L[α] γ) (h : γ →L[α] δ) (x y z : β) -- make some straightforward lemmas available to `simp`. @[simp] lemma map_zero : f (0 : β) = 0 := (to_linear_map _).map_zero @[simp] lemma map_add : f (x + y) = f x + f y := (to_linear_map _).map_add _ _ @[simp] lemma map_sub : f (x - y) = f x - f y := (to_linear_map _).map_sub _ _ @[simp] lemma map_smul : f (c • x) = c • f x := (to_linear_map _).map_smul _ _ @[simp] lemma map_neg : f (-x) = - (f x) := (to_linear_map _).map_neg _ @[simp, squash_cast] lemma coe_coe : ((f : β →ₗ[α] γ) : (β → γ)) = (f : β → γ) := rfl /-- The continuous map that is constantly zero. -/ def zero : β →L[α] γ := ⟨0, by exact continuous_const⟩ instance: has_zero (β →L[α] γ) := ⟨zero⟩ @[simp] lemma zero_apply : (0 : β →L[α] γ) x = 0 := rfl @[simp, elim_cast] lemma coe_zero : ((0 : β →L[α] γ) : β →ₗ[α] γ) = 0 := rfl /- no simp attribute on the next line as simp does not always simplify 0 x to x when 0 is the zero function, while it does for the zero continuous linear map, and this is the most important property we care about. -/ @[elim_cast] lemma coe_zero' : ((0 : β →L[α] γ) : β → γ) = 0 := rfl /-- the identity map as a continuous linear map. -/ def id : β →L[α] β := ⟨linear_map.id, continuous_id⟩ instance : has_one (β →L[α] β) := ⟨id⟩ @[simp] lemma id_apply : (id : β →L[α] β) x = x := rfl @[simp, elim_cast] lemma coe_id : ((id : β →L[α] β) : β →ₗ[α] β) = linear_map.id := rfl @[simp, elim_cast] lemma coe_id' : ((id : β →L[α] β) : β → β) = _root_.id := rfl section add variables [topological_add_group γ] instance : has_add (β →L[α] γ) := ⟨λ f g, ⟨f + g, continuous_add f.2 g.2⟩⟩ @[simp] lemma add_apply : (f + g) x = f x + g x := rfl @[simp, move_cast] lemma coe_add : (((f + g) : β →L[α] γ) : β →ₗ[α] γ) = (f : β →ₗ[α] γ) + g := rfl @[move_cast] lemma coe_add' : (((f + g) : β →L[α] γ) : β → γ) = (f : β → γ) + g := rfl instance : has_neg (β →L[α] γ) := ⟨λ f, ⟨-f, continuous_neg f.2⟩⟩ @[simp] lemma neg_apply : (-f) x = - (f x) := rfl @[simp, move_cast] lemma coe_neg : (((-f) : β →L[α] γ) : β →ₗ[α] γ) = -(f : β →ₗ[α] γ) := rfl @[move_cast] lemma coe_neg' : (((-f) : β →L[α] γ) : β → γ) = -(f : β → γ) := rfl instance : add_comm_group (β →L[α] γ) := by refine {zero := 0, add := (+), neg := has_neg.neg, ..}; intros; ext; simp @[simp] lemma sub_apply (x : β) : (f - g) x = f x - g x := rfl @[simp, move_cast] lemma coe_sub : (((f - g) : β →L[α] γ) : β →ₗ[α] γ) = (f : β →ₗ[α] γ) - g := rfl @[simp, move_cast] lemma coe_sub' : (((f - g) : β →L[α] γ) : β → γ) = (f : β → γ) - g := rfl end add /-- Composition of bounded linear maps. -/ def comp (g : γ →L[α] δ) (f : β →L[α] γ) : β →L[α] δ := ⟨linear_map.comp g.to_linear_map f.to_linear_map, g.2.comp f.2⟩ @[simp, move_cast] lemma coe_comp : ((h.comp f) : (β →ₗ[α] δ)) = (h : γ →ₗ[α] δ).comp f := rfl @[simp, move_cast] lemma coe_comp' : ((h.comp f) : (β → δ)) = (h : γ → δ) ∘ f := rfl instance : has_mul (β →L[α] β) := ⟨comp⟩ instance [topological_add_group β] : ring (β →L[α] β) := { mul := (*), one := 1, mul_one := λ _, ext $ λ _, rfl, one_mul := λ _, ext $ λ _, rfl, mul_assoc := λ _ _ _, ext $ λ _, rfl, left_distrib := λ _ _ _, ext $ λ _, map_add _ _ _, right_distrib := λ _ _ _, ext $ λ _, linear_map.add_apply _ _ _, ..continuous_linear_map.add_comm_group } /-- The cartesian product of two bounded linear maps, as a bounded linear map. -/ def prod (f₁ : β →L[α] γ) (f₂ : β →L[α] δ) : β →L[α] (γ × δ) := { cont := continuous.prod_mk f₁.2 f₂.2, ..f₁.to_linear_map.prod f₂.to_linear_map } end general_ring section comm_ring variables {α : Type*} [comm_ring α] [topological_space α] {β : Type*} [topological_space β] [add_comm_group β] {γ : Type*} [topological_space γ] [add_comm_group γ] [module α β] [module α γ] [topological_module α γ] instance : has_scalar α (β →L[α] γ) := ⟨λ c f, ⟨c • f, continuous_smul continuous_const f.2⟩⟩ variables (c : α) (f g : β →L[α] γ) (x y z : β) @[simp] lemma smul_apply : (c • f) x = c • (f x) := rfl @[simp, move_cast] lemma coe_apply : (((c • f) : β →L[α] γ) : β →ₗ[α] γ) = c • (f : β →ₗ[α] γ) := rfl @[move_cast] lemma coe_apply' : (((c • f) : β →L[α] γ) : β → γ) = c • (f : β → γ) := rfl /-- Associating to a scalar-valued linear map and an element of `γ` the `γ`-valued linear map obtained by multiplying the two (a.k.a. tensoring by `γ`) -/ def smul_right (c : β →L[α] α) (f : γ) : β →L[α] γ := { cont := continuous_smul c.2 continuous_const, ..c.to_linear_map.smul_right f } variable [topological_add_group γ] instance : module α (β →L[α] γ) := { smul_zero := λ _, ext $ λ _, smul_zero _, zero_smul := λ _, ext $ λ _, zero_smul _ _, one_smul := λ _, ext $ λ _, one_smul _ _, mul_smul := λ _ _ _, ext $ λ _, mul_smul _ _ _, add_smul := λ _ _ _, ext $ λ _, add_smul _ _ _, smul_add := λ _ _ _, ext $ λ _, smul_add _ _ _ } set_option class.instance_max_depth 55 instance : is_ring_hom (λ c : α, c • (1 : γ →L[α] γ)) := { map_one := one_smul _ _, map_add := λ _ _, ext $ λ _, add_smul _ _ _, map_mul := λ _ _, ext $ λ _, mul_smul _ _ _ } instance : algebra α (γ →L[α] γ) := { to_fun := λ c, c • 1, smul_def' := λ _ _, rfl, commutes' := λ _ _, ext $ λ _, map_smul _ _ _ } end comm_ring section field variables {α : Type*} [discrete_field α] [topological_space α] {β : Type*} [topological_space β] [add_comm_group β] {γ : Type*} [topological_space γ] [add_comm_group γ] [topological_add_group γ] [vector_space α β] [vector_space α γ] [topological_vector_space α γ] instance : vector_space α (β →L[α] γ) := { ..continuous_linear_map.module } end field end continuous_linear_map
0f1754db83789ce1a9f5cf2a8641fb795be68acc
4727251e0cd73359b15b664c3170e5d754078599
/src/tactic/reserved_notation.lean
ef38b2b65197e194c63062b352d21c75a00bbd72
[ "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
1,543
lean
/- Copyright (c) 2020 Bryan Gin-ge Chen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bryan Gin-ge Chen -/ /-! # Reserved notation This file is imported by `logic.basic` and `logic.relator` to place it at the top of the import graph. We place all of `mathlib`'s reserved notation in this file so that users will know not to use them as e.g. variable names without needing to import the specific file where they are defined. -/ -- used in `logic/relator.lean` reserve infixr ` ⇒ `:40 -- used in `tactic/core.lean` precedence `setup_tactic_parser`:0 reserve prefix `pformat! `:100 reserve prefix `fail! `:100 reserve prefix `trace! `:100 -- used in `tactic/localized.lean` reserve notation `localized` -- used in `tactic/lint/frontend.lean` reserve notation `#lint` reserve notation `#lint_mathlib` reserve notation `#lint_all` reserve notation `#list_linters` -- used in `tactic/where.lean` reserve prefix `#where`:max -- used in `tactic/simps.lean` reserve notation `initialize_simps_projections` reserve notation `as_prefix` -- used in `tactic/lift.lean` reserve notation `to` -- used in `tactic/rcases.lean` precedence `?`:max -- used in `tactic/induction.lean` precedence `fixing`:0 -- used in `order/lattice.lean` -- These priorities are chosen to be above `+`, `∑`, and `∏`, but below `*`. There is no particular -- reason for this choice. reserve infixl ` ⊓ `:69 reserve infixl ` ⊔ `:68 -- used in `algebra/module/linear_map.lean` reserve infix ` ≃ₗ `:25
336a17589af71d8147bd74dd79af2b4fda0885a3
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/padics/hensel.lean
afe249817117d51f6363505df705d057b5b363ea
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
2,893
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.padics.padic_integers import Mathlib.topology.metric_space.cau_seq_filter import Mathlib.analysis.specific_limits import Mathlib.data.polynomial.identities import Mathlib.topology.algebra.polynomial import Mathlib.PostPort namespace Mathlib /-! # Hensel's lemma on ℤ_p This file proves Hensel's lemma on ℤ_p, roughly following Keith Conrad's writeup: <http://www.math.uconn.edu/~kconrad/blurbs/gradnumthy/hensel.pdf> Hensel's lemma gives a simple condition for the existence of a root of a polynomial. The proof and motivation are described in the paper [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019]. ## References * <http://www.math.uconn.edu/~kconrad/blurbs/gradnumthy/hensel.pdf> * [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019] * <https://en.wikipedia.org/wiki/Hensel%27s_lemma> ## Tags p-adic, p adic, padic, p-adic integer -/ -- We begin with some general lemmas that are used below in the computation. theorem padic_polynomial_dist {p : ℕ} [fact (nat.prime p)] (F : polynomial (padic_int p)) (x : padic_int p) (y : padic_int p) : norm (polynomial.eval x F - polynomial.eval y F) ≤ norm (x - y) := sorry theorem limit_zero_of_norm_tendsto_zero {p : ℕ} [fact (nat.prime p)] {ncs : cau_seq (padic_int p) norm} {F : polynomial (padic_int p)} (hnorm : filter.tendsto (fun (i : ℕ) => norm (polynomial.eval (coe_fn ncs i) F)) filter.at_top (nhds 0)) : polynomial.eval (cau_seq.lim ncs) F = 0 := tendsto_nhds_unique (comp_tendsto_lim ncs) (tendsto_zero_of_norm_tendsto_zero hnorm) /-- `T` is an auxiliary value that is used to control the behavior of the polynomial `F`. -/ /-- We will construct a sequence of elements of ℤ_p satisfying successive values of `ih`. -/ /-- Given `z : ℤ_[p]` satisfying `ih n z`, construct `z' : ℤ_[p]` satisfying `ih (n+1) z'`. We need the hypothesis `ih n z`, since otherwise `z'` is not necessarily an integer. -/ -- why doesn't "noncomputable theory" stick here? theorem hensels_lemma {p : ℕ} [fact (nat.prime p)] {F : polynomial (padic_int p)} {a : padic_int p} (hnorm : norm (polynomial.eval a F) < norm (polynomial.eval a (coe_fn polynomial.derivative F)) ^ bit0 1) : ∃ (z : padic_int p), polynomial.eval z F = 0 ∧ norm (z - a) < norm (polynomial.eval a (coe_fn polynomial.derivative F)) ∧ norm (polynomial.eval z (coe_fn polynomial.derivative F)) = norm (polynomial.eval a (coe_fn polynomial.derivative F)) ∧ ∀ (z' : padic_int p), polynomial.eval z' F = 0 → norm (z' - a) < norm (polynomial.eval a (coe_fn polynomial.derivative F)) → z' = z := sorry
2ec179a0a11ef1a19cf64a1e23bd54dc9dd9a8b3
64874bd1010548c7f5a6e3e8902efa63baaff785
/hott/algebra/group.hlean
fc97286f5b612d3c9b764aab80ba960efa0e4998
[ "Apache-2.0" ]
permissive
tjiaqi/lean
4634d729795c164664d10d093f3545287c76628f
d0ce4cf62f4246b0600c07e074d86e51f2195e30
refs/heads/master
1,622,323,796,480
1,422,643,069,000
1,422,643,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,951
hlean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: algebra.group Authors: Jeremy Avigad, Leonardo de Moura Various multiplicative and additive structures. Partially modeled on Isabelle's library. -/ import algebra.binary open eq truncation binary -- note: ⁻¹ will be overloaded namespace path_algebra variable {A : Type} /- overloaded symbols -/ structure has_mul [class] (A : Type) := (mul : A → A → A) structure has_add [class] (A : Type) := (add : A → A → A) structure has_one [class] (A : Type) := (one : A) structure has_zero [class] (A : Type) := (zero : A) structure has_inv [class] (A : Type) := (inv : A → A) structure has_neg [class] (A : Type) := (neg : A → A) infixl `*` := has_mul.mul infixl `+` := has_add.add postfix `⁻¹` := has_inv.inv prefix `-` := has_neg.neg notation 1 := !has_one.one notation 0 := !has_zero.zero /- semigroup -/ structure semigroup [class] (A : Type) extends has_mul A := (carrier_hset : is_hset A) (mul_assoc : ∀a b c, mul (mul a b) c = mul a (mul b c)) theorem mul_assoc [s : semigroup A] (a b c : A) : a * b * c = a * (b * c) := !semigroup.mul_assoc structure comm_semigroup [class] (A : Type) extends semigroup A := (mul_comm : ∀a b, mul a b = mul b a) theorem mul_comm [s : comm_semigroup A] (a b : A) : a * b = b * a := !comm_semigroup.mul_comm theorem mul_left_comm [s : comm_semigroup A] (a b c : A) : a * (b * c) = b * (a * c) := binary.left_comm (@mul_comm A s) (@mul_assoc A s) a b c theorem mul_right_comm [s : comm_semigroup A] (a b c : A) : (a * b) * c = (a * c) * b := binary.right_comm (@mul_comm A s) (@mul_assoc A s) a b c structure left_cancel_semigroup [class] (A : Type) extends semigroup A := (mul_left_cancel : ∀a b c, mul a b = mul a c → b = c) theorem mul_left_cancel [s : left_cancel_semigroup A] {a b c : A} : a * b = a * c → b = c := !left_cancel_semigroup.mul_left_cancel structure right_cancel_semigroup [class] (A : Type) extends semigroup A := (mul_right_cancel : ∀a b c, mul a b = mul c b → a = c) theorem mul_right_cancel [s : right_cancel_semigroup A] {a b c : A} : a * b = c * b → a = c := !right_cancel_semigroup.mul_right_cancel /- additive semigroup -/ structure add_semigroup [class] (A : Type) extends has_add A := (add_assoc : ∀a b c, add (add a b) c = add a (add b c)) theorem add_assoc [s : add_semigroup A] (a b c : A) : a + b + c = a + (b + c) := !add_semigroup.add_assoc structure add_comm_semigroup [class] (A : Type) extends add_semigroup A := (add_comm : ∀a b, add a b = add b a) theorem add_comm [s : add_comm_semigroup A] (a b : A) : a + b = b + a := !add_comm_semigroup.add_comm theorem add_left_comm [s : add_comm_semigroup A] (a b c : A) : a + (b + c) = b + (a + c) := binary.left_comm (@add_comm A s) (@add_assoc A s) a b c theorem add_right_comm [s : add_comm_semigroup A] (a b c : A) : (a + b) + c = (a + c) + b := binary.right_comm (@add_comm A s) (@add_assoc A s) a b c structure add_left_cancel_semigroup [class] (A : Type) extends add_semigroup A := (add_left_cancel : ∀a b c, add a b = add a c → b = c) theorem add_left_cancel [s : add_left_cancel_semigroup A] {a b c : A} : a + b = a + c → b = c := !add_left_cancel_semigroup.add_left_cancel structure add_right_cancel_semigroup [class] (A : Type) extends add_semigroup A := (add_right_cancel : ∀a b c, add a b = add c b → a = c) theorem add_right_cancel [s : add_right_cancel_semigroup A] {a b c : A} : a + b = c + b → a = c := !add_right_cancel_semigroup.add_right_cancel /- monoid -/ structure monoid [class] (A : Type) extends semigroup A, has_one A := (mul_left_id : ∀a, mul one a = a) (mul_right_id : ∀a, mul a one = a) theorem mul_left_id [s : monoid A] (a : A) : 1 * a = a := !monoid.mul_left_id theorem mul_right_id [s : monoid A] (a : A) : a * 1 = a := !monoid.mul_right_id structure comm_monoid [class] (A : Type) extends monoid A, comm_semigroup A /- additive monoid -/ structure add_monoid [class] (A : Type) extends add_semigroup A, has_zero A := (add_left_id : ∀a, add zero a = a) (add_right_id : ∀a, add a zero = a) theorem add_left_id [s : add_monoid A] (a : A) : 0 + a = a := !add_monoid.add_left_id theorem add_right_id [s : add_monoid A] (a : A) : a + 0 = a := !add_monoid.add_right_id structure add_comm_monoid [class] (A : Type) extends add_monoid A, add_comm_semigroup A /- group -/ structure group [class] (A : Type) extends monoid A, has_inv A := (mul_left_inv : ∀a, mul (inv a) a = one) -- Note: with more work, we could derive the axiom mul_left_id section group variable [s : group A] include s theorem mul_left_inv (a : A) : a⁻¹ * a = 1 := !group.mul_left_inv theorem inv_mul_cancel_left (a b : A) : a⁻¹ * (a * b) = b := calc a⁻¹ * (a * b) = a⁻¹ * a * b : mul_assoc ... = 1 * b : mul_left_inv ... = b : mul_left_id theorem inv_mul_cancel_right (a b : A) : a * b⁻¹ * b = a := calc a * b⁻¹ * b = a * (b⁻¹ * b) : mul_assoc ... = a * 1 : mul_left_inv ... = a : mul_right_id theorem inv_unique {a b : A} (H : a * b = 1) : a⁻¹ = b := calc a⁻¹ = a⁻¹ * 1 : mul_right_id ... = a⁻¹ * (a * b) : H ... = b : inv_mul_cancel_left theorem inv_one : 1⁻¹ = 1 := inv_unique (mul_left_id 1) theorem inv_inv (a : A) : (a⁻¹)⁻¹ = a := inv_unique (mul_left_inv a) theorem inv_inj {a b : A} (H : a⁻¹ = b⁻¹) : a = b := calc a = (a⁻¹)⁻¹ : inv_inv ... = b : inv_unique (H⁻¹ ▹ (mul_left_inv _)) --theorem inv_eq_inv_iff_eq (a b : A) : a⁻¹ = b⁻¹ ↔ a = b := --iff.intro (assume H, inv_inj H) (assume H, congr_arg _ H) --theorem inv_eq_one_iff_eq_one (a b : A) : a⁻¹ = 1 ↔ a = 1 := --inv_one ▹ !inv_eq_inv_iff_eq theorem eq_inv_imp_eq_inv {a b : A} (H : a = b⁻¹) : b = a⁻¹ := H⁻¹ ▹ (inv_inv b)⁻¹ --theorem eq_inv_iff_eq_inv (a b : A) : a = b⁻¹ ↔ b = a⁻¹ := --iff.intro !eq_inv_imp_eq_inv !eq_inv_imp_eq_inv theorem mul_right_inv (a : A) : a * a⁻¹ = 1 := calc a * a⁻¹ = (a⁻¹)⁻¹ * a⁻¹ : inv_inv ... = 1 : mul_left_inv theorem mul_inv_cancel_left (a b : A) : a * (a⁻¹ * b) = b := calc a * (a⁻¹ * b) = a * a⁻¹ * b : mul_assoc ... = 1 * b : mul_right_inv ... = b : mul_left_id theorem mul_inv_cancel_right (a b : A) : a * b * b⁻¹ = a := calc a * b * b⁻¹ = a * (b * b⁻¹) : mul_assoc ... = a * 1 : mul_right_inv ... = a : mul_right_id theorem inv_mul (a b : A) : (a * b)⁻¹ = b⁻¹ * a⁻¹ := inv_unique (calc a * b * (b⁻¹ * a⁻¹) = a * (b * (b⁻¹ * a⁻¹)) : mul_assoc ... = a * a⁻¹ : mul_inv_cancel_left ... = 1 : mul_right_inv) theorem mul_inv_eq_one_imp_eq {a b : A} (H : a * b⁻¹ = 1) : a = b := calc a = a * b⁻¹ * b : inv_mul_cancel_right ... = 1 * b : H ... = b : mul_left_id -- TODO: better names for the next eight theorems? (Also for additive ones.) theorem mul_eq_imp_eq_mul_inv {a b c : A} (H : a * b = c) : a = c * b⁻¹ := H ▹ !mul_inv_cancel_right⁻¹ theorem mul_eq_imp_eq_inv_mul {a b c : A} (H : a * b = c) : b = a⁻¹ * c := H ▹ !inv_mul_cancel_left⁻¹ theorem eq_mul_imp_inv_mul_eq {a b c : A} (H : a = b * c) : b⁻¹ * a = c := H⁻¹ ▹ !inv_mul_cancel_left theorem eq_mul_imp_mul_inv_eq {a b c : A} (H : a = b * c) : a * c⁻¹ = b := H⁻¹ ▹ !mul_inv_cancel_right theorem mul_inv_eq_imp_eq_mul {a b c : A} (H : a * b⁻¹ = c) : a = c * b := !inv_inv ▹ (mul_eq_imp_eq_mul_inv H) theorem inv_mul_eq_imp_eq_mul {a b c : A} (H : a⁻¹ * b = c) : b = a * c := !inv_inv ▹ (mul_eq_imp_eq_inv_mul H) theorem eq_inv_mul_imp_mul_eq {a b c : A} (H : a = b⁻¹ * c) : b * a = c := !inv_inv ▹ (eq_mul_imp_inv_mul_eq H) theorem eq_mul_inv_imp_mul_eq {a b c : A} (H : a = b * c⁻¹) : a * c = b := !inv_inv ▹ (eq_mul_imp_mul_inv_eq H) --theorem mul_eq_iff_eq_inv_mul (a b c : A) : a * b = c ↔ b = a⁻¹ * c := --iff.intro mul_eq_imp_eq_inv_mul eq_inv_mul_imp_mul_eq --theorem mul_eq_iff_eq_mul_inv (a b c : A) : a * b = c ↔ a = c * b⁻¹ := --iff.intro mul_eq_imp_eq_mul_inv eq_mul_inv_imp_mul_eq definition group.to_left_cancel_semigroup [instance] : left_cancel_semigroup A := left_cancel_semigroup.mk (@group.mul A s) (@group.carrier_hset A s) (@group.mul_assoc A s) (take a b c, assume H : a * b = a * c, calc b = a⁻¹ * (a * b) : inv_mul_cancel_left ... = a⁻¹ * (a * c) : H ... = c : inv_mul_cancel_left) definition group.to_right_cancel_semigroup [instance] : right_cancel_semigroup A := right_cancel_semigroup.mk (@group.mul A s) (@group.carrier_hset A s) (@group.mul_assoc A s) (take a b c, assume H : a * b = c * b, calc a = (a * b) * b⁻¹ : mul_inv_cancel_right ... = (c * b) * b⁻¹ : H ... = c : mul_inv_cancel_right) end group structure comm_group [class] (A : Type) extends group A, comm_monoid A /- additive group -/ structure add_group [class] (A : Type) extends add_monoid A, has_neg A := (add_left_inv : ∀a, add (neg a) a = zero) section add_group variables [s : add_group A] include s theorem add_left_inv (a : A) : -a + a = 0 := !add_group.add_left_inv theorem neg_add_cancel_left (a b : A) : -a + (a + b) = b := calc -a + (a + b) = -a + a + b : add_assoc ... = 0 + b : add_left_inv ... = b : add_left_id theorem neg_add_cancel_right (a b : A) : a + -b + b = a := calc a + -b + b = a + (-b + b) : add_assoc ... = a + 0 : add_left_inv ... = a : add_right_id theorem neg_unique {a b : A} (H : a + b = 0) : -a = b := calc -a = -a + 0 : add_right_id ... = -a + (a + b) : H ... = b : neg_add_cancel_left theorem neg_zero : -0 = 0 := neg_unique (add_left_id 0) theorem neg_neg (a : A) : -(-a) = a := neg_unique (add_left_inv a) theorem neg_inj {a b : A} (H : -a = -b) : a = b := calc a = -(-a) : neg_neg ... = b : neg_unique (H⁻¹ ▹ (add_left_inv _)) --theorem neg_eq_neg_iff_eq (a b : A) : -a = -b ↔ a = b := --iff.intro (assume H, neg_inj H) (assume H, congr_arg _ H) --theorem neg_eq_zero_iff_eq_zero (a b : A) : -a = 0 ↔ a = 0 := --neg_zero ▹ !neg_eq_neg_iff_eq theorem eq_neg_imp_eq_neg {a b : A} (H : a = -b) : b = -a := H⁻¹ ▹ (neg_neg b)⁻¹ --theorem eq_neg_iff_eq_neg (a b : A) : a = -b ↔ b = -a := --iff.intro !eq_neg_imp_eq_neg !eq_neg_imp_eq_neg theorem add_right_inv (a : A) : a + -a = 0 := calc a + -a = -(-a) + -a : neg_neg ... = 0 : add_left_inv theorem add_neg_cancel_left (a b : A) : a + (-a + b) = b := calc a + (-a + b) = a + -a + b : add_assoc ... = 0 + b : add_right_inv ... = b : add_left_id theorem add_neg_cancel_right (a b : A) : a + b + -b = a := calc a + b + -b = a + (b + -b) : add_assoc ... = a + 0 : add_right_inv ... = a : add_right_id theorem neg_add (a b : A) : -(a + b) = -b + -a := neg_unique (calc a + b + (-b + -a) = a + (b + (-b + -a)) : add_assoc ... = a + -a : add_neg_cancel_left ... = 0 : add_right_inv) theorem add_eq_imp_eq_add_neg {a b c : A} (H : a + b = c) : a = c + -b := H ▹ !add_neg_cancel_right⁻¹ theorem add_eq_imp_eq_neg_add {a b c : A} (H : a + b = c) : b = -a + c := H ▹ !neg_add_cancel_left⁻¹ theorem eq_add_imp_neg_add_eq {a b c : A} (H : a = b + c) : -b + a = c := H⁻¹ ▹ !neg_add_cancel_left theorem eq_add_imp_add_neg_eq {a b c : A} (H : a = b + c) : a + -c = b := H⁻¹ ▹ !add_neg_cancel_right theorem add_neg_eq_imp_eq_add {a b c : A} (H : a + -b = c) : a = c + b := !neg_neg ▹ (add_eq_imp_eq_add_neg H) theorem neg_add_eq_imp_eq_add {a b c : A} (H : -a + b = c) : b = a + c := !neg_neg ▹ (add_eq_imp_eq_neg_add H) theorem eq_neg_add_imp_add_eq {a b c : A} (H : a = -b + c) : b + a = c := !neg_neg ▹ (eq_add_imp_neg_add_eq H) theorem eq_add_neg_imp_add_eq {a b c : A} (H : a = b + -c) : a + c = b := !neg_neg ▹ (eq_add_imp_add_neg_eq H) --theorem add_eq_iff_eq_neg_add (a b c : A) : a + b = c ↔ b = -a + c := --iff.intro add_eq_imp_eq_neg_add eq_neg_add_imp_add_eq --theorem add_eq_iff_eq_add_neg (a b c : A) : a + b = c ↔ a = c + -b := --iff.intro add_eq_imp_eq_add_neg eq_add_neg_imp_add_eq definition add_group.to_left_cancel_semigroup [instance] : add_left_cancel_semigroup A := add_left_cancel_semigroup.mk (@add_group.add A s) (@add_group.add_assoc A s) (take a b c, assume H : a + b = a + c, calc b = -a + (a + b) : neg_add_cancel_left ... = -a + (a + c) : H ... = c : neg_add_cancel_left) definition add_group.to_add_right_cancel_semigroup [instance] : add_right_cancel_semigroup A := add_right_cancel_semigroup.mk (@add_group.add A s) (@add_group.add_assoc A s) (take a b c, assume H : a + b = c + b, calc a = (a + b) + -b : add_neg_cancel_right ... = (c + b) + -b : H ... = c : add_neg_cancel_right) /- minus -/ -- TODO: derive corresponding facts for div in a field definition minus [reducible] (a b : A) : A := a + -b infix `-` := minus theorem minus_self (a : A) : a - a = 0 := !add_right_inv theorem minus_add_cancel (a b : A) : a - b + b = a := !neg_add_cancel_right theorem add_minus_cancel (a b : A) : a + b - b = a := !add_neg_cancel_right theorem minus_eq_zero_imp_eq {a b : A} (H : a - b = 0) : a = b := calc a = (a - b) + b : minus_add_cancel ... = 0 + b : H ... = b : add_left_id --theorem eq_iff_minus_eq_zero (a b : A) : a = b ↔ a - b = 0 := --iff.intro (assume H, H ▹ !minus_self) (assume H, minus_eq_zero_imp_eq H) theorem zero_minus (a : A) : 0 - a = -a := !add_left_id theorem minus_zero (a : A) : a - 0 = a := (neg_zero⁻¹) ▹ !add_right_id theorem minus_neg_eq_add (a b : A) : a - (-b) = a + b := !neg_neg ▹ idp theorem neg_minus_eq (a b : A) : -(a - b) = b - a := neg_unique (calc a - b + (b - a) = a - b + b - a : add_assoc ... = a - a : minus_add_cancel ... = 0 : minus_self) theorem add_minus_eq (a b c : A) : a + (b - c) = a + b - c := !add_assoc⁻¹ theorem minus_add_eq_minus_swap (a b c : A) : a - (b + c) = a - c - b := calc a - (b + c) = a + (-c - b) : neg_add ... = a - c - b : add_assoc --theorem minus_eq_iff_eq_add (a b c : A) : a - b = c ↔ a = c + b := --iff.intro (assume H, add_neg_eq_imp_eq_add H) (assume H, eq_add_imp_add_neg_eq H) --theorem eq_minus_iff_add_eq (a b c : A) : a = b - c ↔ a + c = b := --iff.intro (assume H, eq_add_neg_imp_add_eq H) (assume H, add_eq_imp_eq_add_neg H) --theorem minus_eq_minus_iff {a b c d : A} (H : a - b = c - d) : a = b ↔ c = d := --calc -- a = b ↔ a - b = 0 : eq_iff_minus_eq_zero -- ... ↔ c - d = 0 : H ▹ !iff.refl -- ... ↔ c = d : iff.symm (eq_iff_minus_eq_zero c d) end add_group structure add_comm_group [class] (A : Type) extends add_group A, add_comm_monoid A section add_comm_group variable [s : add_comm_group A] include s theorem minus_add_eq (a b c : A) : a - (b + c) = a - b - c := !add_comm ▹ !minus_add_eq_minus_swap theorem neg_add_eq_minus (a b : A) : -a + b = b - a := !add_comm theorem neg_add_distrib (a b : A) : -(a + b) = -a + -b := !add_comm ▹ !neg_add theorem minus_add_right_comm (a b c : A) : a - b + c = a + c - b := !add_right_comm theorem minus_minus_eq (a b c : A) : a - b - c = a - (b + c) := calc a - b - c = a + (-b + -c) : add_assoc ... = a + -(b + c) : neg_add_distrib ... = a - (b + c) : idp theorem add_minus_cancel_left (a b c : A) : (c + a) - (c + b) = a - b := calc (c + a) - (c + b) = c + a - c - b : minus_add_eq ... = a + c - c - b : add_comm a c ... = a - b : add_minus_cancel end add_comm_group /- bundled structures -/ structure Semigroup := (carrier : Type) (struct : semigroup carrier) attribute Semigroup.carrier [coercion] attribute Semigroup.struct [instance] structure CommSemigroup := (carrier : Type) (struct : comm_semigroup carrier) attribute CommSemigroup.carrier [coercion] attribute CommSemigroup.struct [instance] structure Monoid := (carrier : Type) (struct : monoid carrier) attribute Monoid.carrier [coercion] attribute Monoid.struct [instance] structure CommMonoid := (carrier : Type) (struct : comm_monoid carrier) attribute CommMonoid.carrier [coercion] attribute CommMonoid.struct [instance] structure Group := (carrier : Type) (struct : group carrier) attribute Group.carrier [coercion] attribute Group.struct [instance] structure CommGroup := (carrier : Type) (struct : comm_group carrier) attribute CommGroup.carrier [coercion] attribute CommGroup.struct [instance] structure AddSemigroup := (carrier : Type) (struct : add_semigroup carrier) attribute AddSemigroup.carrier [coercion] attribute AddSemigroup.struct [instance] structure AddCommSemigroup := (carrier : Type) (struct : add_comm_semigroup carrier) attribute AddCommSemigroup.carrier [coercion] attribute AddCommSemigroup.struct [instance] structure AddMonoid := (carrier : Type) (struct : add_monoid carrier) attribute AddMonoid.carrier [coercion] attribute AddMonoid.struct [instance] structure AddCommMonoid := (carrier : Type) (struct : add_comm_monoid carrier) attribute AddCommMonoid.carrier [coercion] attribute AddCommMonoid.struct [instance] structure AddGroup := (carrier : Type) (struct : add_group carrier) attribute AddGroup.carrier [coercion] attribute AddGroup.struct [instance] structure AddCommGroup := (carrier : Type) (struct : add_comm_group carrier) attribute AddCommGroup.carrier [coercion] attribute AddCommGroup.struct [instance] end path_algebra
4ae68852d75b9bec8c654746ebcf5302e18cd809
37da0369b6c03e380e057bf680d81e6c9fdf9219
/hott/homotopy/susp.hlean
6e3af542103430d995d64b4289f0b9e339ee6e44
[ "Apache-2.0" ]
permissive
kodyvajjha/lean2
72b120d95c3a1d77f54433fa90c9810e14a931a4
227fcad22ab2bc27bb7471be7911075d101ba3f9
refs/heads/master
1,627,157,512,295
1,501,855,676,000
1,504,809,427,000
109,317,326
0
0
null
1,509,839,253,000
1,509,655,713,000
C++
UTF-8
Lean
false
false
17,715
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Ulrik Buchholtz Declaration of suspension -/ import hit.pushout types.pointed2 cubical.square .connectedness open pushout unit eq equiv pointed is_equiv definition susp' (A : Type) : Type := pushout (λ(a : A), star) (λ(a : A), star) namespace susp definition north' {A : Type} : susp' A := inl star definition pointed_susp [instance] [constructor] (X : Type) : pointed (susp' X) := pointed.mk north' end susp open susp definition susp [constructor] (X : Type) : Type* := pointed.MK (susp' X) north' notation `⅀` := susp namespace susp variable {A : Type} definition north {A : Type} : susp A := north' definition south {A : Type} : susp A := inr star definition merid (a : A) : @north A = @south A := glue a protected definition rec {P : susp A → Type} (PN : P north) (PS : P south) (Pm : Π(a : A), PN =[merid a] PS) (x : susp' A) : P x := begin induction x with u u, { cases u, exact PN}, { cases u, exact PS}, { apply Pm}, end protected definition rec_on [reducible] {P : susp A → Type} (y : susp' A) (PN : P north) (PS : P south) (Pm : Π(a : A), PN =[merid a] PS) : P y := susp.rec PN PS Pm y theorem rec_merid {P : susp A → Type} (PN : P north) (PS : P south) (Pm : Π(a : A), PN =[merid a] PS) (a : A) : apd (susp.rec PN PS Pm) (merid a) = Pm a := !rec_glue protected definition elim {P : Type} (PN : P) (PS : P) (Pm : A → PN = PS) (x : susp' A) : P := susp.rec PN PS (λa, pathover_of_eq _ (Pm a)) x protected definition elim_on [reducible] {P : Type} (x : susp' A) (PN : P) (PS : P) (Pm : A → PN = PS) : P := susp.elim PN PS Pm x theorem elim_merid {P : Type} {PN PS : P} (Pm : A → PN = PS) (a : A) : ap (susp.elim PN PS Pm) (merid a) = Pm a := begin apply eq_of_fn_eq_fn_inv !(pathover_constant (merid a)), rewrite [▸*,-apd_eq_pathover_of_eq_ap,↑susp.elim,rec_merid], end protected definition elim_type (PN : Type) (PS : Type) (Pm : A → PN ≃ PS) (x : susp' A) : Type := pushout.elim_type (λx, PN) (λx, PS) Pm x protected definition elim_type_on [reducible] (x : susp' A) (PN : Type) (PS : Type) (Pm : A → PN ≃ PS) : Type := susp.elim_type PN PS Pm x theorem elim_type_merid (PN : Type) (PS : Type) (Pm : A → PN ≃ PS) (a : A) : transport (susp.elim_type PN PS Pm) (merid a) = Pm a := !elim_type_glue theorem elim_type_merid_inv {A : Type} (PN : Type) (PS : Type) (Pm : A → PN ≃ PS) (a : A) : transport (susp.elim_type PN PS Pm) (merid a)⁻¹ = to_inv (Pm a) := !elim_type_glue_inv protected definition merid_square {a a' : A} (p : a = a') : square (merid a) (merid a') idp idp := by cases p; apply vrefl end susp attribute susp.north' susp.north susp.south [constructor] attribute susp.rec susp.elim [unfold 6] [recursor 6] attribute susp.elim_type [unfold 5] attribute susp.rec_on susp.elim_on [unfold 3] attribute susp.elim_type_on [unfold 2] namespace susp open is_trunc is_conn trunc -- Theorem 8.2.1 definition is_conn_susp [instance] (n : trunc_index) (A : Type) [H : is_conn n A] : is_conn (n .+1) (susp A) := is_contr.mk (tr north) begin intro x, induction x with x, induction x, { reflexivity }, { exact (trunc.rec (λa, ap tr (merid a)) (center (trunc n A))) }, { generalize (center (trunc n A)), intro x, induction x with a', apply pathover_of_tr_eq, rewrite [eq_transport_Fr,idp_con], revert H, induction n with n IH: intro H, { apply is_prop.elim }, { change ap (@tr n .+2 (susp A)) (merid a) = ap tr (merid a'), generalize a', apply is_conn_fun.elim n (is_conn_fun_from_unit n A a) (λx : A, trunctype.mk' n (ap (@tr n .+2 (susp A)) (merid a) = ap tr (merid x))), intros, change ap (@tr n .+2 (susp A)) (merid a) = ap tr (merid a), reflexivity } } end /- Flattening lemma -/ open prod prod.ops section universe variable u parameters (A : Type) (PN PS : Type.{u}) (Pm : A → PN ≃ PS) include Pm local abbreviation P [unfold 5] := susp.elim_type PN PS Pm local abbreviation F : A × PN → PN := λz, z.2 local abbreviation G : A × PN → PS := λz, Pm z.1 z.2 protected definition flattening : sigma P ≃ pushout F G := begin apply equiv.trans !pushout.flattening, fapply pushout.equiv, { exact sigma.equiv_prod A PN }, { apply sigma.sigma_unit_left }, { apply sigma.sigma_unit_left }, { reflexivity }, { reflexivity } end end end susp /- Functoriality and equivalence -/ namespace susp variables {A B : Type} (f : A → B) include f definition susp_functor' [unfold 4] : susp A → susp B := begin intro x, induction x with a, { exact north }, { exact south }, { exact merid (f a) } end variable [Hf : is_equiv f] include Hf open is_equiv protected definition is_equiv_functor [instance] [constructor] : is_equiv (susp_functor' f) := adjointify (susp_functor' f) (susp_functor' f⁻¹) abstract begin intro sb, induction sb with b, do 2 reflexivity, apply eq_pathover, rewrite [ap_id,ap_compose' (susp_functor' f) (susp_functor' f⁻¹)], krewrite [susp.elim_merid,susp.elim_merid], apply transpose, apply susp.merid_square (right_inv f b) end end abstract begin intro sa, induction sa with a, do 2 reflexivity, apply eq_pathover, rewrite [ap_id,ap_compose' (susp_functor' f⁻¹) (susp_functor' f)], krewrite [susp.elim_merid,susp.elim_merid], apply transpose, apply susp.merid_square (left_inv f a) end end end susp namespace susp variables {A B : Type} (f : A ≃ B) protected definition equiv : susp A ≃ susp B := equiv.mk (susp_functor' f) _ end susp namespace susp open pointed is_trunc variables {X X' Y Y' Z : Type*} definition susp_functor [constructor] (f : X →* Y) : susp X →* susp Y := begin fconstructor, { exact susp_functor' f }, { reflexivity } end definition is_equiv_susp_functor [constructor] (f : X →* Y) [Hf : is_equiv f] : is_equiv (susp_functor f) := susp.is_equiv_functor f definition susp_pequiv [constructor] (f : X ≃* Y) : susp X ≃* susp Y := pequiv_of_equiv (susp.equiv f) idp definition susp_functor_pcompose (g : Y →* Z) (f : X →* Y) : susp_functor (g ∘* f) ~* susp_functor g ∘* susp_functor f := begin fapply phomotopy.mk, { intro x, induction x, { reflexivity }, { reflexivity }, { apply eq_pathover, apply hdeg_square, refine !elim_merid ⬝ _ ⬝ (ap_compose (susp_functor g) _ _)⁻¹ᵖ, refine _ ⬝ ap02 _ !elim_merid⁻¹, exact !elim_merid⁻¹ }}, { reflexivity }, end definition susp_functor_phomotopy {f g : X →* Y} (p : f ~* g) : susp_functor f ~* susp_functor g := begin fapply phomotopy.mk, { intro x, induction x, { reflexivity }, { reflexivity }, { apply eq_pathover, apply hdeg_square, esimp, refine !elim_merid ⬝ _ ⬝ !elim_merid⁻¹ᵖ, exact ap merid (p a), }}, { reflexivity }, end definition susp_functor_pid (A : Type*) : susp_functor (pid A) ~* pid (susp A) := begin fapply phomotopy.mk, { intro x, induction x, { reflexivity }, { reflexivity }, { apply eq_pathover_id_right, apply hdeg_square, apply elim_merid }}, { reflexivity }, end /- adjunction originally ported from Coq-HoTT, but we proved some additional naturality conditions -/ definition loop_susp_unit [constructor] (X : Type*) : X →* Ω(susp X) := begin fconstructor, { intro x, exact merid x ⬝ (merid pt)⁻¹ }, { apply con.right_inv }, end definition loop_susp_unit_natural (f : X →* Y) : loop_susp_unit Y ∘* f ~* Ω→ (susp_functor f) ∘* loop_susp_unit X := begin induction X with X x, induction Y with Y y, induction f with f pf, esimp at *, induction pf, fapply phomotopy.mk, { intro x', symmetry, exact !ap1_gen_idp_left ⬝ (!ap_con ⬝ whisker_left _ !ap_inv) ⬝ (!elim_merid ◾ (inverse2 !elim_merid)) }, { rewrite [▸*, idp_con (con.right_inv _)], apply inv_con_eq_of_eq_con, refine _ ⬝ !con.assoc', rewrite inverse2_right_inv, refine _ ⬝ !con.assoc', rewrite [ap_con_right_inv], rewrite [ap1_gen_idp_left_con], rewrite [-ap_compose (concat idp)] }, end definition loop_susp_counit [constructor] (X : Type*) : susp (Ω X) →* X := begin fapply pmap.mk, { intro x, induction x, exact pt, exact pt, exact a }, { reflexivity }, end definition loop_susp_counit_natural (f : X →* Y) : f ∘* loop_susp_counit X ~* loop_susp_counit Y ∘* (susp_functor (ap1 f)) := begin induction X with X x, induction Y with Y y, induction f with f pf, esimp at *, induction pf, fconstructor, { intro x', induction x' with p, { reflexivity }, { reflexivity }, { esimp, apply eq_pathover, apply hdeg_square, xrewrite [ap_compose' f, ap_compose' (susp.elim (f x) (f x) (λ (a : f x = f x), a)),▸*], xrewrite [+elim_merid, ap1_gen_idp_left] }}, { reflexivity } end definition loop_susp_counit_unit (X : Type*) : ap1 (loop_susp_counit X) ∘* loop_susp_unit (Ω X) ~* pid (Ω X) := begin induction X with X x, fconstructor, { intro p, esimp, refine !ap1_gen_idp_left ⬝ (!ap_con ⬝ whisker_left _ !ap_inv) ⬝ (!elim_merid ◾ inverse2 !elim_merid) }, { rewrite [▸*,inverse2_right_inv (elim_merid id idp)], refine !con.assoc ⬝ _, xrewrite [ap_con_right_inv (susp.elim x x (λa, a)) (merid idp),ap1_gen_idp_left_con, -ap_compose] } end definition loop_susp_unit_counit (X : Type*) : loop_susp_counit (susp X) ∘* susp_functor (loop_susp_unit X) ~* pid (susp X) := begin induction X with X x, fconstructor, { intro x', induction x', { reflexivity }, { exact merid pt }, { apply eq_pathover, xrewrite [▸*, ap_id, ap_compose' (susp.elim north north (λa, a)), +elim_merid,▸*], apply square_of_eq, exact !idp_con ⬝ !inv_con_cancel_right⁻¹ }}, { reflexivity } end definition susp_elim [constructor] {X Y : Type*} (f : X →* Ω Y) : susp X →* Y := loop_susp_counit Y ∘* susp_functor f definition loop_susp_intro [constructor] {X Y : Type*} (f : susp X →* Y) : X →* Ω Y := ap1 f ∘* loop_susp_unit X definition susp_elim_susp_functor {A B C : Type*} (g : B →* Ω C) (f : A →* B) : susp_elim g ∘* susp_functor f ~* susp_elim (g ∘* f) := begin refine !passoc ⬝* _, exact pwhisker_left _ !susp_functor_pcompose⁻¹* end definition susp_elim_phomotopy {A B : Type*} {f g : A →* Ω B} (p : f ~* g) : susp_elim f ~* susp_elim g := pwhisker_left _ (susp_functor_phomotopy p) definition susp_elim_natural {X Y Z : Type*} (g : Y →* Z) (f : X →* Ω Y) : g ∘* susp_elim f ~* susp_elim (Ω→ g ∘* f) := begin refine _ ⬝* pwhisker_left _ !susp_functor_pcompose⁻¹*, refine !passoc⁻¹* ⬝* _ ⬝* !passoc, exact pwhisker_right _ !loop_susp_counit_natural end definition loop_susp_intro_natural {X Y Z : Type*} (g : susp Y →* Z) (f : X →* Y) : loop_susp_intro (g ∘* susp_functor f) ~* loop_susp_intro g ∘* f := pwhisker_right _ !ap1_pcompose ⬝* !passoc ⬝* pwhisker_left _ !loop_susp_unit_natural⁻¹* ⬝* !passoc⁻¹* definition susp_adjoint_loop_right_inv {X Y : Type*} (g : X →* Ω Y) : loop_susp_intro (susp_elim g) ~* g := begin refine !pwhisker_right !ap1_pcompose ⬝* _, refine !passoc ⬝* _, refine !pwhisker_left !loop_susp_unit_natural⁻¹* ⬝* _, refine !passoc⁻¹* ⬝* _, refine !pwhisker_right !loop_susp_counit_unit ⬝* _, apply pid_pcompose end definition susp_adjoint_loop_left_inv {X Y : Type*} (f : susp X →* Y) : susp_elim (loop_susp_intro f) ~* f := begin refine !pwhisker_left !susp_functor_pcompose ⬝* _, refine !passoc⁻¹* ⬝* _, refine !pwhisker_right !loop_susp_counit_natural⁻¹* ⬝* _, refine !passoc ⬝* _, refine !pwhisker_left !loop_susp_unit_counit ⬝* _, apply pcompose_pid end definition susp_adjoint_loop_unpointed [constructor] (X Y : Type*) : susp X →* Y ≃ X →* Ω Y := begin fapply equiv.MK, { exact loop_susp_intro }, { exact susp_elim }, { intro g, apply eq_of_phomotopy, exact susp_adjoint_loop_right_inv g }, { intro f, apply eq_of_phomotopy, exact susp_adjoint_loop_left_inv f } end definition susp_functor_pconst_homotopy [unfold 3] {X Y : Type*} (x : susp X) : susp_functor (pconst X Y) x = pt := begin induction x, { reflexivity }, { exact (merid pt)⁻¹ }, { apply eq_pathover, refine !elim_merid ⬝ph _ ⬝hp !ap_constant⁻¹, exact square_of_eq !con.right_inv⁻¹ } end definition susp_functor_pconst [constructor] (X Y : Type*) : susp_functor (pconst X Y) ~* pconst (susp X) (susp Y) := begin fapply phomotopy.mk, { exact susp_functor_pconst_homotopy }, { reflexivity } end definition susp_pfunctor [constructor] (X Y : Type*) : ppmap X Y →* ppmap (susp X) (susp Y) := pmap.mk susp_functor (eq_of_phomotopy !susp_functor_pconst) definition susp_pelim [constructor] (X Y : Type*) : ppmap X (Ω Y) →* ppmap (susp X) Y := ppcompose_left (loop_susp_counit Y) ∘* susp_pfunctor X (Ω Y) definition loop_susp_pintro [constructor] (X Y : Type*) : ppmap (susp X) Y →* ppmap X (Ω Y) := ppcompose_right (loop_susp_unit X) ∘* pap1 (susp X) Y definition loop_susp_pintro_natural_left (f : X' →* X) : psquare (loop_susp_pintro X Y) (loop_susp_pintro X' Y) (ppcompose_right (susp_functor f)) (ppcompose_right f) := !pap1_natural_left ⬝h* ppcompose_right_psquare (loop_susp_unit_natural f)⁻¹* definition loop_susp_pintro_natural_right (f : Y →* Y') : psquare (loop_susp_pintro X Y) (loop_susp_pintro X Y') (ppcompose_left f) (ppcompose_left (Ω→ f)) := !pap1_natural_right ⬝h* !ppcompose_left_ppcompose_right⁻¹* definition is_equiv_loop_susp_pintro [constructor] (X Y : Type*) : is_equiv (loop_susp_pintro X Y) := begin fapply adjointify, { exact susp_pelim X Y }, { intro g, apply eq_of_phomotopy, exact susp_adjoint_loop_right_inv g }, { intro f, apply eq_of_phomotopy, exact susp_adjoint_loop_left_inv f } end definition susp_adjoint_loop [constructor] (X Y : Type*) : ppmap (susp X) Y ≃* ppmap X (Ω Y) := pequiv_of_pmap (loop_susp_pintro X Y) (is_equiv_loop_susp_pintro X Y) definition susp_adjoint_loop_natural_right (f : Y →* Y') : psquare (susp_adjoint_loop X Y) (susp_adjoint_loop X Y') (ppcompose_left f) (ppcompose_left (Ω→ f)) := loop_susp_pintro_natural_right f definition susp_adjoint_loop_natural_left (f : X' →* X) : psquare (susp_adjoint_loop X Y) (susp_adjoint_loop X' Y) (ppcompose_right (susp_functor f)) (ppcompose_right f) := loop_susp_pintro_natural_left f definition ap1_susp_elim {A : Type*} {X : Type*} (p : A →* Ω X) : Ω→(susp_elim p) ∘* loop_susp_unit A ~* p := susp_adjoint_loop_right_inv p /- the underlying homotopies of susp_adjoint_loop_natural_* -/ definition susp_adjoint_loop_nat_right (f : susp X →* Y) (g : Y →* Z) : susp_adjoint_loop X Z (g ∘* f) ~* ap1 g ∘* susp_adjoint_loop X Y f := begin esimp [susp_adjoint_loop], refine _ ⬝* !passoc, apply pwhisker_right, apply ap1_pcompose end definition susp_adjoint_loop_nat_left (f : Y →* Ω Z) (g : X →* Y) : (susp_adjoint_loop X Z)⁻¹ᵉ (f ∘* g) ~* (susp_adjoint_loop Y Z)⁻¹ᵉ f ∘* susp_functor g := begin esimp [susp_adjoint_loop], refine _ ⬝* !passoc⁻¹*, apply pwhisker_left, apply susp_functor_pcompose end /- iterated suspension -/ definition iterate_susp (n : ℕ) (A : Type*) : Type* := iterate (λX, susp X) n A open is_conn trunc_index nat definition iterate_susp_succ (n : ℕ) (A : Type*) : iterate_susp (succ n) A = susp (iterate_susp n A) := idp definition is_conn_iterate_susp [instance] (n : ℕ₋₂) (m : ℕ) (A : Type*) [H : is_conn n A] : is_conn (n + m) (iterate_susp m A) := begin induction m with m IH, exact H, exact @is_conn_susp _ _ IH end -- Separate cases for n = 0, which comes up often definition is_conn_iterate_susp_zero [instance] (m : ℕ) (A : Type*) [H : is_conn 0 A] : is_conn m (iterate_susp m A) := begin induction m with m IH, exact H, exact @is_conn_susp _ _ IH end definition iterate_susp_functor (n : ℕ) {A B : Type*} (f : A →* B) : iterate_susp n A →* iterate_susp n B := begin induction n with n g, { exact f }, { exact susp_functor g } end definition iterate_susp_succ_in (n : ℕ) (A : Type*) : iterate_susp (succ n) A ≃* iterate_susp n (susp A) := begin induction n with n IH, { reflexivity}, { exact susp_pequiv IH} end definition iterate_susp_adjoint_loopn [constructor] (X Y : Type*) (n : ℕ) : ppmap (iterate_susp n X) Y ≃* ppmap X (Ω[n] Y) := begin revert X Y, induction n with n IH: intro X Y, { reflexivity }, { refine !susp_adjoint_loop ⬝e* !IH ⬝e* _, apply pequiv_ppcompose_left, symmetry, apply loopn_succ_in } end end susp
084634cfbe04942413e373ff60aa20891fc05117
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/monad/kleisli_auto.lean
97e208530167163a1bc23ce5e236315055e86ac9
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
2,628
lean
/- Copyright (c) 2020 Wojciech Nawrocki. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Wojciech Nawrocki, Bhavik Mehta -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.adjunction.default import Mathlib.category_theory.monad.adjunction import Mathlib.category_theory.monad.basic import Mathlib.PostPort universes v u namespace Mathlib /-! # Kleisli category on a monad This file defines the Kleisli category on a monad `(T, η_ T, μ_ T)`. It also defines the Kleisli adjunction which gives rise to the monad `(T, η_ T, μ_ T)`. ## References * [Riehl, *Category theory in context*, Definition 5.2.9][riehl2017] -/ namespace category_theory /-- The objects for the Kleisli category of the functor (usually monad) `T : C ⥤ C`, which are the same thing as objects of the base category `C`. -/ def kleisli {C : Type u} [category C] (T : C ⥤ C) := C namespace kleisli protected instance inhabited {C : Type u} [category C] (T : C ⥤ C) [Inhabited C] : Inhabited (kleisli T) := { default := Inhabited.default } /-- The Kleisli category on a monad `T`. cf Definition 5.2.9 in [Riehl][riehl2017]. -/ protected instance kleisli.category {C : Type u} [category C] (T : C ⥤ C) [monad T] : category (kleisli T) := category.mk namespace adjunction /-- The left adjoint of the adjunction which induces the monad `(T, η_ T, μ_ T)`. -/ def to_kleisli {C : Type u} [category C] (T : C ⥤ C) [monad T] : C ⥤ kleisli T := functor.mk (fun (X : C) => X) fun (X Y : C) (f : X ⟶ Y) => f ≫ nat_trans.app η_ Y /-- The right adjoint of the adjunction which induces the monad `(T, η_ T, μ_ T)`. -/ def from_kleisli {C : Type u} [category C] (T : C ⥤ C) [monad T] : kleisli T ⥤ C := functor.mk (fun (X : kleisli T) => functor.obj T X) fun (X Y : kleisli T) (f : X ⟶ Y) => functor.map T f ≫ nat_trans.app μ_ Y /-- The Kleisli adjunction which gives rise to the monad `(T, η_ T, μ_ T)`. cf Lemma 5.2.11 of [Riehl][riehl2017]. -/ def adj {C : Type u} [category C] (T : C ⥤ C) [monad T] : to_kleisli T ⊣ from_kleisli T := adjunction.mk_of_hom_equiv (adjunction.core_hom_equiv.mk fun (X : C) (Y : kleisli T) => equiv.refl (X ⟶ functor.obj T Y)) /-- The composition of the adjunction gives the original functor. -/ def to_kleisli_comp_from_kleisli_iso_self {C : Type u} [category C] (T : C ⥤ C) [monad T] : to_kleisli T ⋙ from_kleisli T ≅ T := nat_iso.of_components (fun (X : C) => iso.refl (functor.obj (to_kleisli T ⋙ from_kleisli T) X)) sorry end Mathlib
3ada4dcc09e9369e5545e8f7624e119e4c1934bc
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/src/Init/Control/EState.lean
a732269f386ce35dc127aa90d07580ad797f0ac1
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
5,610
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.State import Init.Control.Except universes u v namespace EStateM inductive Result (ε σ α : Type u) | ok {} : α → σ → Result | error {} : ε → σ → Result variables {ε σ α : Type u} protected def Result.toString [HasToString ε] [HasToString α] : Result ε σ α → String | Result.ok a _ => "ok: " ++ toString a | Result.error e _ => "error: " ++ toString e protected def Result.repr [HasRepr ε] [HasRepr α] : Result ε σ α → String | Result.error e _ => "(error " ++ repr e ++ ")" | Result.ok a _ => "(ok " ++ repr a ++ ")" instance Result.hasToString [HasToString ε] [HasToString α] : HasToString (Result ε σ α) := ⟨Result.toString⟩ instance Result.hasRepr [HasRepr ε] [HasRepr α] : HasRepr (Result ε σ α) := ⟨Result.repr⟩ instance Result.inhabited [Inhabited ε] [Inhabited σ] : Inhabited (Result ε σ α) := ⟨Result.error (arbitrary _) (arbitrary _)⟩ end EStateM def EStateM (ε σ α : Type u) := σ → EStateM.Result ε σ α namespace EStateM variables {ε σ α β : Type u} instance [Inhabited ε] : Inhabited (EStateM ε σ α) := ⟨fun s => Result.error (arbitrary ε) s⟩ @[inline] protected def pure (a : α) : EStateM ε σ α := fun s => Result.ok a s @[inline] protected def set (s : σ) : EStateM ε σ PUnit := fun _ => Result.ok ⟨⟩ s @[inline] protected def get : EStateM ε σ σ := fun s => Result.ok s s @[inline] protected def modifyGet (f : σ → α × σ) : EStateM ε σ α := fun s => match f s with | (a, s) => Result.ok a s @[inline] protected def throw (e : ε) : EStateM ε σ α := fun s => Result.error e s /-- Auxiliary instance for saving/restoring the "backtrackable" part of the state. -/ class Backtrackable (δ : outParam $ Type u) (σ : Type u) := (save : σ → δ) (restore : σ → δ → σ) @[inline] protected def catch {δ} [Backtrackable δ σ] {α} (x : EStateM ε σ α) (handle : ε → EStateM ε σ α) : EStateM ε σ α := fun s => let d := Backtrackable.save s; match x s with | Result.error e s => handle e (Backtrackable.restore s d) | ok => ok @[inline] protected def orelse {δ} [Backtrackable δ σ] (x₁ x₂ : EStateM ε σ α) : EStateM ε σ α := fun s => let d := Backtrackable.save s; match x₁ s with | Result.error _ s => x₂ (Backtrackable.restore s d) | ok => ok /-- Alternative orelse operator that allows to select which exception should be used. The default is to use the first exception since the standard `orelse` uses the second. -/ @[inline] protected def orelse' {δ} [Backtrackable δ σ] (x₁ x₂ : EStateM ε σ α) (useFirstEx := true) : EStateM ε σ α := fun s => let d := Backtrackable.save s; match x₁ s with | Result.error e₁ s₁ => match x₂ (Backtrackable.restore s₁ d) with | Result.error e₂ s₂ => Result.error (if useFirstEx then e₁ else e₂) s₂ | ok => ok | ok => ok @[inline] def adaptExcept {ε' : Type u} (f : ε → ε') (x : EStateM ε σ α) : EStateM ε' σ α := fun s => match x s with | Result.error e s => Result.error (f e) s | Result.ok a s => Result.ok a s @[inline] protected def bind (x : EStateM ε σ α) (f : α → EStateM ε σ β) : EStateM ε σ β := fun s => match x s with | Result.ok a s => f a s | Result.error e s => Result.error e s @[inline] protected def map (f : α → β) (x : EStateM ε σ α) : EStateM ε σ β := fun s => match x s with | Result.ok a s => Result.ok (f a) s | Result.error e s => Result.error e s @[inline] protected def seqRight (x : EStateM ε σ α) (y : EStateM ε σ β) : EStateM ε σ β := fun s => match x s with | Result.ok _ s => y s | Result.error e s => Result.error e s instance : Monad (EStateM ε σ) := { bind := @EStateM.bind _ _, pure := @EStateM.pure _ _, map := @EStateM.map _ _, seqRight := @EStateM.seqRight _ _ } instance {δ} [Backtrackable δ σ] : HasOrelse (EStateM ε σ α) := { orelse := @EStateM.orelse _ _ _ _ _ } instance : MonadState σ (EStateM ε σ) := { set := @EStateM.set _ _, get := @EStateM.get _ _, modifyGet := @EStateM.modifyGet _ _ } instance {δ} [Backtrackable δ σ] : MonadExcept ε (EStateM ε σ) := { throw := @EStateM.throw _ _, catch := @EStateM.catch _ _ _ _ } @[inline] def adaptState {σ₁ σ₂} (split : σ → σ₁ × σ₂) (merge : σ₁ → σ₂ → σ) (x : EStateM ε σ₁ α) : EStateM ε σ α := fun s => let (s₁, s₂) := split s; match x s₁ with | Result.ok a s₁ => Result.ok a (merge s₁ s₂) | Result.error e s₁ => Result.error e (merge s₁ s₂) instance {ε σ σ'} : MonadStateAdapter σ σ' (EStateM ε σ) (EStateM ε σ') := ⟨fun σ'' α => EStateM.adaptState⟩ @[inline] def fromStateM {ε σ α : Type} (x : StateM σ α) : EStateM ε σ α := fun s => match x.run s with | (a, s') => EStateM.Result.ok a s' @[inline] def run (x : EStateM ε σ α) (s : σ) : Result ε σ α := x s @[inline] def run' (x : EStateM ε σ α) (s : σ) : Option α := match run x s with | Result.ok v _ => some v | Result.error _ _ => none @[inline] def dummySave : σ → PUnit := fun _ => ⟨⟩ @[inline] def dummyRestore : σ → PUnit → σ := fun s _ => s /- Dummy default instance -/ instance nonBacktrackable : Backtrackable PUnit σ := { save := dummySave, restore := dummyRestore } end EStateM
53667af5e5c0a854bcef959f910cc7d4289aef37
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/order/filter/partial.lean
87ed9bd79a8c5bc93068918c48eddb3c3759de9c
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,749
lean
/- Copyright (c) 2019 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad -/ import order.filter.basic /-! # `tendsto` for relations and partial functions This file generalizes `filter` definitions from functions to partial functions and relations. ## Considering functions and partial functions as relations A function `f : α → β` can be considered as the relation `rel α β` which relates `x` and `f x` for all `x`, and nothing else. This relation is called `function.graph f`. A partial function `f : α →. β` can be considered as the relation `rel α β` which relates `x` and `f x` for all `x` for which `f x` exists, and nothing else. This relation is called `pfun.graph' f`. In this regard, a function is a relation for which every element in `α` is related to exactly one element in `β` and a partial function is a relation for which every element in `α` is related to at most one element in `β`. This file leverages this analogy to generalize `filter` definitions from functions to partial functions and relations. ## Notes `set.preimage` can be generalized to relations in two ways: * `rel.preimage` returns the image of the set under the inverse relation. * `rel.core` returns the set of elements that are only related to those in the set. Both generalizations are sensible in the context of filters, so `filter.comap` and `filter.tendsto` get two generalizations each. We first take care of relations. Then the definitions for partial functions are taken as special cases of the definitions for relations. -/ universes u v w namespace filter variables {α : Type u} {β : Type v} {γ : Type w} open_locale filter /-! ### Relations -/ /-- The forward map of a filter under a relation. Generalization of `filter.map` to relations. Note that `rel.core` generalizes `set.preimage`. -/ def rmap (r : rel α β) (l : filter α) : filter β := { sets := {s | r.core s ∈ l}, univ_sets := by simp, sets_of_superset := λ s t hs st, mem_of_superset hs $ rel.core_mono _ st, inter_sets := λ s t hs ht, by simp [rel.core_inter, inter_mem hs ht] } theorem rmap_sets (r : rel α β) (l : filter α) : (l.rmap r).sets = r.core ⁻¹' l.sets := rfl @[simp] theorem mem_rmap (r : rel α β) (l : filter α) (s : set β) : s ∈ l.rmap r ↔ r.core s ∈ l := iff.rfl @[simp] theorem rmap_rmap (r : rel α β) (s : rel β γ) (l : filter α) : rmap s (rmap r l) = rmap (r.comp s) l := filter_eq $ by simp [rmap_sets, set.preimage, rel.core_comp] @[simp] lemma rmap_compose (r : rel α β) (s : rel β γ) : rmap s ∘ rmap r = rmap (r.comp s) := funext $ rmap_rmap _ _ /-- Generic "limit of a relation" predicate. `rtendsto r l₁ l₂` asserts that for every `l₂`-neighborhood `a`, the `r`-core of `a` is an `l₁`-neighborhood. One generalization of `filter.tendsto` to relations. -/ def rtendsto (r : rel α β) (l₁ : filter α) (l₂ : filter β) := l₁.rmap r ≤ l₂ theorem rtendsto_def (r : rel α β) (l₁ : filter α) (l₂ : filter β) : rtendsto r l₁ l₂ ↔ ∀ s ∈ l₂, r.core s ∈ l₁ := iff.rfl /-- One way of taking the inverse map of a filter under a relation. One generalization of `filter.comap` to relations. Note that `rel.core` generalizes `set.preimage`. -/ def rcomap (r : rel α β) (f : filter β) : filter α := { sets := rel.image (λ s t, r.core s ⊆ t) f.sets, univ_sets := ⟨set.univ, univ_mem, set.subset_univ _⟩, sets_of_superset := λ a b ⟨a', ha', ma'a⟩ ab, ⟨a', ha', ma'a.trans ab⟩, inter_sets := λ a b ⟨a', ha₁, ha₂⟩ ⟨b', hb₁, hb₂⟩, ⟨a' ∩ b', inter_mem ha₁ hb₁, (r.core_inter a' b').subset.trans (set.inter_subset_inter ha₂ hb₂)⟩ } theorem rcomap_sets (r : rel α β) (f : filter β) : (rcomap r f).sets = rel.image (λ s t, r.core s ⊆ t) f.sets := rfl theorem rcomap_rcomap (r : rel α β) (s : rel β γ) (l : filter γ) : rcomap r (rcomap s l) = rcomap (r.comp s) l := filter_eq $ begin ext t, simp [rcomap_sets, rel.image, rel.core_comp], split, { rintros ⟨u, ⟨v, vsets, hv⟩, h⟩, exact ⟨v, vsets, set.subset.trans (rel.core_mono _ hv) h⟩ }, rintros ⟨t, tsets, ht⟩, exact ⟨rel.core s t, ⟨t, tsets, set.subset.rfl⟩, ht⟩ end @[simp] lemma rcomap_compose (r : rel α β) (s : rel β γ) : rcomap r ∘ rcomap s = rcomap (r.comp s) := funext $ rcomap_rcomap _ _ theorem rtendsto_iff_le_rcomap (r : rel α β) (l₁ : filter α) (l₂ : filter β) : rtendsto r l₁ l₂ ↔ l₁ ≤ l₂.rcomap r := begin rw rtendsto_def, change (∀ (s : set β), s ∈ l₂.sets → r.core s ∈ l₁) ↔ l₁ ≤ rcomap r l₂, simp [filter.le_def, rcomap, rel.mem_image], split, { exact λ h s t tl₂, mem_of_superset (h t tl₂) }, { exact λ h t tl₂, h _ t tl₂ set.subset.rfl } end -- Interestingly, there does not seem to be a way to express this relation using a forward map. -- Given a filter `f` on `α`, we want a filter `f'` on `β` such that `r.preimage s ∈ f` if -- and only if `s ∈ f'`. But the intersection of two sets satisfying the lhs may be empty. /-- One way of taking the inverse map of a filter under a relation. Generalization of `filter.comap` to relations. -/ def rcomap' (r : rel α β) (f : filter β) : filter α := { sets := rel.image (λ s t, r.preimage s ⊆ t) f.sets, univ_sets := ⟨set.univ, univ_mem, set.subset_univ _⟩, sets_of_superset := λ a b ⟨a', ha', ma'a⟩ ab, ⟨a', ha', ma'a.trans ab⟩, inter_sets := λ a b ⟨a', ha₁, ha₂⟩ ⟨b', hb₁, hb₂⟩, ⟨a' ∩ b', inter_mem ha₁ hb₁, (@rel.preimage_inter _ _ r _ _).trans (set.inter_subset_inter ha₂ hb₂)⟩ } @[simp] lemma mem_rcomap' (r : rel α β) (l : filter β) (s : set α) : s ∈ l.rcomap' r ↔ ∃ t ∈ l, r.preimage t ⊆ s := iff.rfl theorem rcomap'_sets (r : rel α β) (f : filter β) : (rcomap' r f).sets = rel.image (λ s t, r.preimage s ⊆ t) f.sets := rfl @[simp] theorem rcomap'_rcomap' (r : rel α β) (s : rel β γ) (l : filter γ) : rcomap' r (rcomap' s l) = rcomap' (r.comp s) l := filter.ext $ λ t, begin simp [rcomap'_sets, rel.image, rel.preimage_comp], split, { rintro ⟨u, ⟨v, vsets, hv⟩, h⟩, exact ⟨v, vsets, (rel.preimage_mono _ hv).trans h⟩ }, rintro ⟨t, tsets, ht⟩, exact ⟨s.preimage t, ⟨t, tsets, set.subset.rfl⟩, ht⟩ end @[simp] lemma rcomap'_compose (r : rel α β) (s : rel β γ) : rcomap' r ∘ rcomap' s = rcomap' (r.comp s) := funext $ rcomap'_rcomap' _ _ /-- Generic "limit of a relation" predicate. `rtendsto' r l₁ l₂` asserts that for every `l₂`-neighborhood `a`, the `r`-preimage of `a` is an `l₁`-neighborhood. One generalization of `filter.tendsto` to relations. -/ def rtendsto' (r : rel α β) (l₁ : filter α) (l₂ : filter β) := l₁ ≤ l₂.rcomap' r theorem rtendsto'_def (r : rel α β) (l₁ : filter α) (l₂ : filter β) : rtendsto' r l₁ l₂ ↔ ∀ s ∈ l₂, r.preimage s ∈ l₁ := begin unfold rtendsto' rcomap', simp [le_def, rel.mem_image], split, { exact λ h s hs, h _ _ hs set.subset.rfl }, { exact λ h s t ht, mem_of_superset (h t ht) } end theorem tendsto_iff_rtendsto (l₁ : filter α) (l₂ : filter β) (f : α → β) : tendsto f l₁ l₂ ↔ rtendsto (function.graph f) l₁ l₂ := by { simp [tendsto_def, function.graph, rtendsto_def, rel.core, set.preimage] } theorem tendsto_iff_rtendsto' (l₁ : filter α) (l₂ : filter β) (f : α → β) : tendsto f l₁ l₂ ↔ rtendsto' (function.graph f) l₁ l₂ := by { simp [tendsto_def, function.graph, rtendsto'_def, rel.preimage_def, set.preimage] } /-! ### Partial functions -/ /-- The forward map of a filter under a partial function. Generalization of `filter.map` to partial functions. -/ def pmap (f : α →. β) (l : filter α) : filter β := filter.rmap f.graph' l @[simp] lemma mem_pmap (f : α →. β) (l : filter α) (s : set β) : s ∈ l.pmap f ↔ f.core s ∈ l := iff.rfl /-- Generic "limit of a partial function" predicate. `ptendsto r l₁ l₂` asserts that for every `l₂`-neighborhood `a`, the `p`-core of `a` is an `l₁`-neighborhood. One generalization of `filter.tendsto` to partial function. -/ def ptendsto (f : α →. β) (l₁ : filter α) (l₂ : filter β) := l₁.pmap f ≤ l₂ theorem ptendsto_def (f : α →. β) (l₁ : filter α) (l₂ : filter β) : ptendsto f l₁ l₂ ↔ ∀ s ∈ l₂, f.core s ∈ l₁ := iff.rfl theorem ptendsto_iff_rtendsto (l₁ : filter α) (l₂ : filter β) (f : α →. β) : ptendsto f l₁ l₂ ↔ rtendsto f.graph' l₁ l₂ := iff.rfl theorem pmap_res (l : filter α) (s : set α) (f : α → β) : pmap (pfun.res f s) l = map f (l ⊓ 𝓟 s) := begin ext t, simp only [pfun.core_res, mem_pmap, mem_map, mem_inf_principal, imp_iff_not_or], refl end theorem tendsto_iff_ptendsto (l₁ : filter α) (l₂ : filter β) (s : set α) (f : α → β) : tendsto f (l₁ ⊓ 𝓟 s) l₂ ↔ ptendsto (pfun.res f s) l₁ l₂ := by simp only [tendsto, ptendsto, pmap_res] theorem tendsto_iff_ptendsto_univ (l₁ : filter α) (l₂ : filter β) (f : α → β) : tendsto f l₁ l₂ ↔ ptendsto (pfun.res f set.univ) l₁ l₂ := by { rw ← tendsto_iff_ptendsto, simp [principal_univ] } /-- Inverse map of a filter under a partial function. One generalization of `filter.comap` to partial functions. -/ def pcomap' (f : α →. β) (l : filter β) : filter α := filter.rcomap' f.graph' l /-- Generic "limit of a partial function" predicate. `ptendsto' r l₁ l₂` asserts that for every `l₂`-neighborhood `a`, the `p`-preimage of `a` is an `l₁`-neighborhood. One generalization of `filter.tendsto` to partial functions. -/ def ptendsto' (f : α →. β) (l₁ : filter α) (l₂ : filter β) := l₁ ≤ l₂.rcomap' f.graph' theorem ptendsto'_def (f : α →. β) (l₁ : filter α) (l₂ : filter β) : ptendsto' f l₁ l₂ ↔ ∀ s ∈ l₂, f.preimage s ∈ l₁ := rtendsto'_def _ _ _ theorem ptendsto_of_ptendsto' {f : α →. β} {l₁ : filter α} {l₂ : filter β} : ptendsto' f l₁ l₂ → ptendsto f l₁ l₂ := begin rw [ptendsto_def, ptendsto'_def], exact λ h s sl₂, mem_of_superset (h s sl₂) (pfun.preimage_subset_core _ _), end theorem ptendsto'_of_ptendsto {f : α →. β} {l₁ : filter α} {l₂ : filter β} (h : f.dom ∈ l₁) : ptendsto f l₁ l₂ → ptendsto' f l₁ l₂ := begin rw [ptendsto_def, ptendsto'_def], intros h' s sl₂, rw pfun.preimage_eq, exact inter_mem (h' s sl₂) h end end filter
c765a8360a408dc02bc0a9525cbeea698a95a4ce
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/geometry/manifold/charted_space.lean
3edfdbbcac523f33f36250304fadeac2c43d0dcf
[ "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
50,658
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import topology.local_homeomorph /-! # Charted spaces A smooth manifold is a topological space `M` locally modelled on a euclidean space (or a euclidean half-space for manifolds with boundaries, or an infinite dimensional vector space for more general notions of manifolds), i.e., the manifold is covered by open subsets on which there are local homeomorphisms (the charts) going to a model space `H`, and the changes of charts should be smooth maps. In this file, we introduce a general framework describing these notions, where the model space is an arbitrary topological space. We avoid the word *manifold*, which should be reserved for the situation where the model space is a (subset of a) vector space, and use the terminology *charted space* instead. If the changes of charts satisfy some additional property (for instance if they are smooth), then `M` inherits additional structure (it makes sense to talk about smooth manifolds). There are therefore two different ingredients in a charted space: * the set of charts, which is data * the fact that changes of charts belong to some group (in fact groupoid), which is additional Prop. We separate these two parts in the definition: the charted space structure is just the set of charts, and then the different smoothness requirements (smooth manifold, orientable manifold, contact manifold, and so on) are additional properties of these charts. These properties are formalized through the notion of structure groupoid, i.e., a set of local homeomorphisms stable under composition and inverse, to which the change of coordinates should belong. ## Main definitions * `structure_groupoid H` : a subset of local homeomorphisms of `H` stable under composition, inverse and restriction (ex: local diffeos). * `continuous_groupoid H` : the groupoid of all local homeomorphisms of `H` * `charted_space H M` : charted space structure on `M` modelled on `H`, given by an atlas of local homeomorphisms from `M` to `H` whose sources cover `M`. This is a type class. * `has_groupoid M G` : when `G` is a structure groupoid on `H` and `M` is a charted space modelled on `H`, require that all coordinate changes belong to `G`. This is a type class. * `atlas H M` : when `M` is a charted space modelled on `H`, the atlas of this charted space structure, i.e., the set of charts. * `G.maximal_atlas M` : when `M` is a charted space modelled on `H` and admitting `G` as a structure groupoid, one can consider all the local homeomorphisms from `M` to `H` such that changing coordinate from any chart to them belongs to `G`. This is a larger atlas, called the maximal atlas (for the groupoid `G`). * `structomorph G M M'` : the type of diffeomorphisms between the charted spaces `M` and `M'` for the groupoid `G`. We avoid the word diffeomorphism, keeping it for the smooth category. As a basic example, we give the instance `instance charted_space_model_space (H : Type*) [topological_space H] : charted_space H H` saying that a topological space is a charted space over itself, with the identity as unique chart. This charted space structure is compatible with any groupoid. Additional useful definitions: * `pregroupoid H` : a subset of local mas of `H` stable under composition and restriction, but not inverse (ex: smooth maps) * `groupoid_of_pregroupoid` : construct a groupoid from a pregroupoid, by requiring that a map and its inverse both belong to the pregroupoid (ex: construct diffeos from smooth maps) * `chart_at H x` is a preferred chart at `x : M` when `M` has a charted space structure modelled on `H`. * `G.compatible he he'` states that, for any two charts `e` and `e'` in the atlas, the composition of `e.symm` and `e'` belongs to the groupoid `G` when `M` admits `G` as a structure groupoid. * `G.compatible_of_mem_maximal_atlas he he'` states that, for any two charts `e` and `e'` in the maximal atlas associated to the groupoid `G`, the composition of `e.symm` and `e'` belongs to the `G` if `M` admits `G` as a structure groupoid. * `charted_space_core.to_charted_space`: consider a space without a topology, but endowed with a set of charts (which are local equivs) for which the change of coordinates are local homeos. Then one can construct a topology on the space for which the charts become local homeos, defining a genuine charted space structure. ## Implementation notes The atlas in a charted space is *not* a maximal atlas in general: the notion of maximality depends on the groupoid one considers, and changing groupoids changes the maximal atlas. With the current formalization, it makes sense first to choose the atlas, and then to ask whether this precise atlas defines a smooth manifold, an orientable manifold, and so on. A consequence is that structomorphisms between `M` and `M'` do *not* induce a bijection between the atlases of `M` and `M'`: the definition is only that, read in charts, the structomorphism locally belongs to the groupoid under consideration. (This is equivalent to inducing a bijection between elements of the maximal atlas). A consequence is that the invariance under structomorphisms of properties defined in terms of the atlas is not obvious in general, and could require some work in theory (amounting to the fact that these properties only depend on the maximal atlas, for instance). In practice, this does not create any real difficulty. We use the letter `H` for the model space thinking of the case of manifolds with boundary, where the model space is a half space. Manifolds are sometimes defined as topological spaces with an atlas of local diffeomorphisms, and sometimes as spaces with an atlas from which a topology is deduced. We use the former approach: otherwise, there would be an instance from manifolds to topological spaces, which means that any instance search for topological spaces would try to find manifold structures involving a yet unknown model space, leading to problems. However, we also introduce the latter approach, through a structure `charted_space_core` making it possible to construct a topology out of a set of local equivs with compatibility conditions (but we do not register it as an instance). In the definition of a charted space, the model space is written as an explicit parameter as there can be several model spaces for a given topological space. For instance, a complex manifold (modelled over `ℂ^n`) will also be seen sometimes as a real manifold modelled over `ℝ^(2n)`. ## Notations In the locale `manifold`, we denote the composition of local homeomorphisms with `≫ₕ`, and the composition of local equivs with `≫`. -/ noncomputable theory open_locale classical topological_space open filter universes u variables {H : Type u} {H' : Type*} {M : Type*} {M' : Type*} {M'' : Type*} /- Notational shortcut for the composition of local homeomorphisms and local equivs, i.e., `local_homeomorph.trans` and `local_equiv.trans`. Note that, as is usual for equivs, the composition is from left to right, hence the direction of the arrow. -/ localized "infixr (name := local_homeomorph.trans) ` ≫ₕ `:100 := local_homeomorph.trans" in manifold localized "infixr (name := local_equiv.trans) ` ≫ `:100 := local_equiv.trans" in manifold open set local_homeomorph /-! ### Structure groupoids-/ section groupoid /-! One could add to the definition of a structure groupoid the fact that the restriction of an element of the groupoid to any open set still belongs to the groupoid. (This is in Kobayashi-Nomizu.) I am not sure I want this, for instance on `H × E` where `E` is a vector space, and the groupoid is made of functions respecting the fibers and linear in the fibers (so that a charted space over this groupoid is naturally a vector bundle) I prefer that the members of the groupoid are always defined on sets of the form `s × E`. There is a typeclass `closed_under_restriction` for groupoids which have the restriction property. The only nontrivial requirement is locality: if a local homeomorphism belongs to the groupoid around each point in its domain of definition, then it belongs to the groupoid. Without this requirement, the composition of structomorphisms does not have to be a structomorphism. Note that this implies that a local homeomorphism with empty source belongs to any structure groupoid, as it trivially satisfies this condition. There is also a technical point, related to the fact that a local homeomorphism is by definition a global map which is a homeomorphism when restricted to its source subset (and its values outside of the source are not relevant). Therefore, we also require that being a member of the groupoid only depends on the values on the source. We use primes in the structure names as we will reformulate them below (without primes) using a `has_mem` instance, writing `e ∈ G` instead of `e ∈ G.members`. -/ /-- A structure groupoid is a set of local homeomorphisms of a topological space stable under composition and inverse. They appear in the definition of the smoothness class of a manifold. -/ structure structure_groupoid (H : Type u) [topological_space H] := (members : set (local_homeomorph H H)) (trans' : ∀e e' : local_homeomorph H H, e ∈ members → e' ∈ members → e ≫ₕ e' ∈ members) (symm' : ∀e : local_homeomorph H H, e ∈ members → e.symm ∈ members) (id_mem' : local_homeomorph.refl H ∈ members) (locality' : ∀e : local_homeomorph H H, (∀x ∈ e.source, ∃s, is_open s ∧ x ∈ s ∧ e.restr s ∈ members) → e ∈ members) (eq_on_source' : ∀ e e' : local_homeomorph H H, e ∈ members → e' ≈ e → e' ∈ members) variable [topological_space H] instance : has_mem (local_homeomorph H H) (structure_groupoid H) := ⟨λ(e : local_homeomorph H H) (G : structure_groupoid H), e ∈ G.members⟩ lemma structure_groupoid.trans (G : structure_groupoid H) {e e' : local_homeomorph H H} (he : e ∈ G) (he' : e' ∈ G) : e ≫ₕ e' ∈ G := G.trans' e e' he he' lemma structure_groupoid.symm (G : structure_groupoid H) {e : local_homeomorph H H} (he : e ∈ G) : e.symm ∈ G := G.symm' e he lemma structure_groupoid.id_mem (G : structure_groupoid H) : local_homeomorph.refl H ∈ G := G.id_mem' lemma structure_groupoid.locality (G : structure_groupoid H) {e : local_homeomorph H H} (h : ∀x ∈ e.source, ∃s, is_open s ∧ x ∈ s ∧ e.restr s ∈ G) : e ∈ G := G.locality' e h lemma structure_groupoid.eq_on_source (G : structure_groupoid H) {e e' : local_homeomorph H H} (he : e ∈ G) (h : e' ≈ e) : e' ∈ G := G.eq_on_source' e e' he h /-- Partial order on the set of groupoids, given by inclusion of the members of the groupoid -/ instance structure_groupoid.partial_order : partial_order (structure_groupoid H) := partial_order.lift structure_groupoid.members (λa b h, by { cases a, cases b, dsimp at h, induction h, refl }) lemma structure_groupoid.le_iff {G₁ G₂ : structure_groupoid H} : G₁ ≤ G₂ ↔ ∀ e, e ∈ G₁ → e ∈ G₂ := iff.rfl /-- The trivial groupoid, containing only the identity (and maps with empty source, as this is necessary from the definition) -/ def id_groupoid (H : Type u) [topological_space H] : structure_groupoid H := { members := {local_homeomorph.refl H} ∪ {e : local_homeomorph H H | e.source = ∅}, trans' := λe e' he he', begin cases he; simp at he he', { simpa only [he, refl_trans]}, { have : (e ≫ₕ e').source ⊆ e.source := sep_subset _ _, rw he at this, have : (e ≫ₕ e') ∈ {e : local_homeomorph H H | e.source = ∅} := eq_bot_iff.2 this, exact (mem_union _ _ _).2 (or.inr this) }, end, symm' := λe he, begin cases (mem_union _ _ _).1 he with E E, { simp [mem_singleton_iff.mp E] }, { right, simpa only [e.to_local_equiv.image_source_eq_target.symm] with mfld_simps using E}, end, id_mem' := mem_union_left _ rfl, locality' := λe he, begin cases e.source.eq_empty_or_nonempty with h h, { right, exact h }, { left, rcases h with ⟨x, hx⟩, rcases he x hx with ⟨s, open_s, xs, hs⟩, have x's : x ∈ (e.restr s).source, { rw [restr_source, open_s.interior_eq], exact ⟨hx, xs⟩ }, cases hs, { replace hs : local_homeomorph.restr e s = local_homeomorph.refl H, by simpa only using hs, have : (e.restr s).source = univ, by { rw hs, simp }, change (e.to_local_equiv).source ∩ interior s = univ at this, have : univ ⊆ interior s, by { rw ← this, exact inter_subset_right _ _ }, have : s = univ, by rwa [open_s.interior_eq, univ_subset_iff] at this, simpa only [this, restr_univ] using hs }, { exfalso, rw mem_set_of_eq at hs, rwa hs at x's } }, end, eq_on_source' := λe e' he he'e, begin cases he, { left, have : e = e', { refine eq_of_eq_on_source_univ (setoid.symm he'e) _ _; rw set.mem_singleton_iff.1 he ; refl }, rwa ← this }, { right, change (e.to_local_equiv).source = ∅ at he, rwa [set.mem_set_of_eq, he'e.source_eq] } end } /-- Every structure groupoid contains the identity groupoid -/ instance : order_bot (structure_groupoid H) := { bot := id_groupoid H, bot_le := begin assume u f hf, change f ∈ {local_homeomorph.refl H} ∪ {e : local_homeomorph H H | e.source = ∅} at hf, simp only [singleton_union, mem_set_of_eq, mem_insert_iff] at hf, cases hf, { rw hf, apply u.id_mem }, { apply u.locality, assume x hx, rw [hf, mem_empty_iff_false] at hx, exact hx.elim } end } instance (H : Type u) [topological_space H] : inhabited (structure_groupoid H) := ⟨id_groupoid H⟩ /-- To construct a groupoid, one may consider classes of local homeos such that both the function and its inverse have some property. If this property is stable under composition, one gets a groupoid. `pregroupoid` bundles the properties needed for this construction, with the groupoid of smooth functions with smooth inverses as an application. -/ structure pregroupoid (H : Type*) [topological_space H] := (property : (H → H) → (set H) → Prop) (comp : ∀{f g u v}, property f u → property g v → is_open u → is_open v → is_open (u ∩ f ⁻¹' v) → property (g ∘ f) (u ∩ f ⁻¹' v)) (id_mem : property id univ) (locality : ∀{f u}, is_open u → (∀x∈u, ∃v, is_open v ∧ x ∈ v ∧ property f (u ∩ v)) → property f u) (congr : ∀{f g : H → H} {u}, is_open u → (∀x∈u, g x = f x) → property f u → property g u) /-- Construct a groupoid of local homeos for which the map and its inverse have some property, from a pregroupoid asserting that this property is stable under composition. -/ def pregroupoid.groupoid (PG : pregroupoid H) : structure_groupoid H := { members := {e : local_homeomorph H H | PG.property e e.source ∧ PG.property e.symm e.target}, trans' := λe e' he he', begin split, { apply PG.comp he.1 he'.1 e.open_source e'.open_source, apply e.continuous_to_fun.preimage_open_of_open e.open_source e'.open_source }, { apply PG.comp he'.2 he.2 e'.open_target e.open_target, apply e'.continuous_inv_fun.preimage_open_of_open e'.open_target e.open_target } end, symm' := λe he, ⟨he.2, he.1⟩, id_mem' := ⟨PG.id_mem, PG.id_mem⟩, locality' := λe he, begin split, { apply PG.locality e.open_source (λx xu, _), rcases he x xu with ⟨s, s_open, xs, hs⟩, refine ⟨s, s_open, xs, _⟩, convert hs.1 using 1, dsimp [local_homeomorph.restr], rw s_open.interior_eq }, { apply PG.locality e.open_target (λx xu, _), rcases he (e.symm x) (e.map_target xu) with ⟨s, s_open, xs, hs⟩, refine ⟨e.target ∩ e.symm ⁻¹' s, _, ⟨xu, xs⟩, _⟩, { exact continuous_on.preimage_open_of_open e.continuous_inv_fun e.open_target s_open }, { rw [← inter_assoc, inter_self], convert hs.2 using 1, dsimp [local_homeomorph.restr], rw s_open.interior_eq } }, end, eq_on_source' := λe e' he ee', begin split, { apply PG.congr e'.open_source ee'.2, simp only [ee'.1, he.1] }, { have A := ee'.symm', apply PG.congr e'.symm.open_source A.2, convert he.2, rw A.1, refl } end } lemma mem_groupoid_of_pregroupoid {PG : pregroupoid H} {e : local_homeomorph H H} : e ∈ PG.groupoid ↔ PG.property e e.source ∧ PG.property e.symm e.target := iff.rfl lemma groupoid_of_pregroupoid_le (PG₁ PG₂ : pregroupoid H) (h : ∀f s, PG₁.property f s → PG₂.property f s) : PG₁.groupoid ≤ PG₂.groupoid := begin refine structure_groupoid.le_iff.2 (λ e he, _), rw mem_groupoid_of_pregroupoid at he ⊢, exact ⟨h _ _ he.1, h _ _ he.2⟩ end lemma mem_pregroupoid_of_eq_on_source (PG : pregroupoid H) {e e' : local_homeomorph H H} (he' : e ≈ e') (he : PG.property e e.source) : PG.property e' e'.source := begin rw ← he'.1, exact PG.congr e.open_source he'.eq_on.symm he, end /-- The pregroupoid of all local maps on a topological space `H` -/ @[reducible] def continuous_pregroupoid (H : Type*) [topological_space H] : pregroupoid H := { property := λf s, true, comp := λf g u v hf hg hu hv huv, trivial, id_mem := trivial, locality := λf u u_open h, trivial, congr := λf g u u_open hcongr hf, trivial } instance (H : Type*) [topological_space H] : inhabited (pregroupoid H) := ⟨continuous_pregroupoid H⟩ /-- The groupoid of all local homeomorphisms on a topological space `H` -/ def continuous_groupoid (H : Type*) [topological_space H] : structure_groupoid H := pregroupoid.groupoid (continuous_pregroupoid H) /-- Every structure groupoid is contained in the groupoid of all local homeomorphisms -/ instance : order_top (structure_groupoid H) := { top := continuous_groupoid H, le_top := λ u f hf, by { split; exact dec_trivial } } /-- A groupoid is closed under restriction if it contains all restrictions of its element local homeomorphisms to open subsets of the source. -/ class closed_under_restriction (G : structure_groupoid H) : Prop := (closed_under_restriction : ∀ {e : local_homeomorph H H}, e ∈ G → ∀ (s : set H), is_open s → e.restr s ∈ G) lemma closed_under_restriction' {G : structure_groupoid H} [closed_under_restriction G] {e : local_homeomorph H H} (he : e ∈ G) {s : set H} (hs : is_open s) : e.restr s ∈ G := closed_under_restriction.closed_under_restriction he s hs /-- The trivial restriction-closed groupoid, containing only local homeomorphisms equivalent to the restriction of the identity to the various open subsets. -/ def id_restr_groupoid : structure_groupoid H := { members := {e | ∃ {s : set H} (h : is_open s), e ≈ local_homeomorph.of_set s h}, trans' := begin rintros e e' ⟨s, hs, hse⟩ ⟨s', hs', hse'⟩, refine ⟨s ∩ s', is_open.inter hs hs', _⟩, have := local_homeomorph.eq_on_source.trans' hse hse', rwa local_homeomorph.of_set_trans_of_set at this, end, symm' := begin rintros e ⟨s, hs, hse⟩, refine ⟨s, hs, _⟩, rw [← of_set_symm], exact local_homeomorph.eq_on_source.symm' hse, end, id_mem' := ⟨univ, is_open_univ, by simp only with mfld_simps⟩, locality' := begin intros e h, refine ⟨e.source, e.open_source, by simp only with mfld_simps, _⟩, intros x hx, rcases h x hx with ⟨s, hs, hxs, s', hs', hes'⟩, have hes : x ∈ (e.restr s).source, { rw e.restr_source, refine ⟨hx, _⟩, rw hs.interior_eq, exact hxs }, simpa only with mfld_simps using local_homeomorph.eq_on_source.eq_on hes' hes, end, eq_on_source' := begin rintros e e' ⟨s, hs, hse⟩ hee', exact ⟨s, hs, setoid.trans hee' hse⟩, end } lemma id_restr_groupoid_mem {s : set H} (hs : is_open s) : of_set s hs ∈ @id_restr_groupoid H _ := ⟨s, hs, by refl⟩ /-- The trivial restriction-closed groupoid is indeed `closed_under_restriction`. -/ instance closed_under_restriction_id_restr_groupoid : closed_under_restriction (@id_restr_groupoid H _) := ⟨ begin rintros e ⟨s', hs', he⟩ s hs, use [s' ∩ s, is_open.inter hs' hs], refine setoid.trans (local_homeomorph.eq_on_source.restr he s) _, exact ⟨by simp only [hs.interior_eq] with mfld_simps, by simp only with mfld_simps⟩, end ⟩ /-- A groupoid is closed under restriction if and only if it contains the trivial restriction-closed groupoid. -/ lemma closed_under_restriction_iff_id_le (G : structure_groupoid H) : closed_under_restriction G ↔ id_restr_groupoid ≤ G := begin split, { introsI _i, apply structure_groupoid.le_iff.mpr, rintros e ⟨s, hs, hes⟩, refine G.eq_on_source _ hes, convert closed_under_restriction' G.id_mem hs, change s = _ ∩ _, rw hs.interior_eq, simp only with mfld_simps }, { intros h, split, intros e he s hs, rw ← of_set_trans (e : local_homeomorph H H) hs, refine G.trans _ he, apply structure_groupoid.le_iff.mp h, exact id_restr_groupoid_mem hs }, end /-- The groupoid of all local homeomorphisms on a topological space `H` is closed under restriction. -/ instance : closed_under_restriction (continuous_groupoid H) := (closed_under_restriction_iff_id_le _).mpr (by convert le_top) end groupoid /-! ### Charted spaces -/ /-- A charted space is a topological space endowed with an atlas, i.e., a set of local homeomorphisms taking value in a model space `H`, called charts, such that the domains of the charts cover the whole space. We express the covering property by chosing for each `x` a member `chart_at H x` of the atlas containing `x` in its source: in the smooth case, this is convenient to construct the tangent bundle in an efficient way. The model space is written as an explicit parameter as there can be several model spaces for a given topological space. For instance, a complex manifold (modelled over `ℂ^n`) will also be seen sometimes as a real manifold over `ℝ^(2n)`. -/ @[ext] class charted_space (H : Type*) [topological_space H] (M : Type*) [topological_space M] := (atlas [] : set (local_homeomorph M H)) (chart_at [] : M → local_homeomorph M H) (mem_chart_source [] : ∀x, x ∈ (chart_at x).source) (chart_mem_atlas [] : ∀x, chart_at x ∈ atlas) export charted_space attribute [simp, mfld_simps] mem_chart_source chart_mem_atlas section charted_space /-- Any space is a charted_space modelled over itself, by just using the identity chart -/ instance charted_space_self (H : Type*) [topological_space H] : charted_space H H := { atlas := {local_homeomorph.refl H}, chart_at := λx, local_homeomorph.refl H, mem_chart_source := λx, mem_univ x, chart_mem_atlas := λx, mem_singleton _ } /-- In the trivial charted_space structure of a space modelled over itself through the identity, the atlas members are just the identity -/ @[simp, mfld_simps] lemma charted_space_self_atlas {H : Type*} [topological_space H] {e : local_homeomorph H H} : e ∈ atlas H H ↔ e = local_homeomorph.refl H := by simp [atlas, charted_space.atlas] /-- In the model space, chart_at is always the identity -/ lemma chart_at_self_eq {H : Type*} [topological_space H] {x : H} : chart_at H x = local_homeomorph.refl H := by simpa using chart_mem_atlas H x section variables (H) [topological_space H] [topological_space M] [charted_space H M] lemma mem_chart_target (x : M) : chart_at H x x ∈ (chart_at H x).target := (chart_at H x).map_source (mem_chart_source _ _) lemma chart_source_mem_nhds (x : M) : (chart_at H x).source ∈ 𝓝 x := (chart_at H x).open_source.mem_nhds $ mem_chart_source H x lemma chart_target_mem_nhds (x : M) : (chart_at H x).target ∈ 𝓝 (chart_at H x x) := (chart_at H x).open_target.mem_nhds $ mem_chart_target H x /-- `achart H x` is the chart at `x`, considered as an element of the atlas. Especially useful for working with `basic_smooth_vector_bundle_core` -/ def achart (x : M) : atlas H M := ⟨chart_at H x, chart_mem_atlas H x⟩ lemma achart_def (x : M) : achart H x = ⟨chart_at H x, chart_mem_atlas H x⟩ := rfl @[simp, mfld_simps] lemma coe_achart (x : M) : (achart H x : local_homeomorph M H) = chart_at H x := rfl @[simp, mfld_simps] lemma achart_val (x : M) : (achart H x).1 = chart_at H x := rfl lemma mem_achart_source (x : M) : x ∈ (achart H x).1.source := mem_chart_source H x open topological_space lemma charted_space.second_countable_of_countable_cover [second_countable_topology H] {s : set M} (hs : (⋃ x (hx : x ∈ s), (chart_at H x).source) = univ) (hsc : s.countable) : second_countable_topology M := begin haveI : ∀ x : M, second_countable_topology (chart_at H x).source := λ x, (chart_at H x).second_countable_topology_source, haveI := hsc.to_encodable, rw bUnion_eq_Union at hs, exact second_countable_topology_of_countable_cover (λ x : s, (chart_at H (x : M)).open_source) hs end variable (M) lemma charted_space.second_countable_of_sigma_compact [second_countable_topology H] [sigma_compact_space M] : second_countable_topology M := begin obtain ⟨s, hsc, hsU⟩ : ∃ s, set.countable s ∧ (⋃ x (hx : x ∈ s), (chart_at H x).source) = univ := countable_cover_nhds_of_sigma_compact (λ x : M, chart_source_mem_nhds H x), exact charted_space.second_countable_of_countable_cover H hsU hsc end /-- If a topological space admits an atlas with locally compact charts, then the space itself is locally compact. -/ lemma charted_space.locally_compact [locally_compact_space H] : locally_compact_space M := begin have : ∀ (x : M), (𝓝 x).has_basis (λ s, s ∈ 𝓝 (chart_at H x x) ∧ is_compact s ∧ s ⊆ (chart_at H x).target) (λ s, (chart_at H x).symm '' s), { intro x, rw [← (chart_at H x).symm_map_nhds_eq (mem_chart_source H x)], exact ((compact_basis_nhds (chart_at H x x)).has_basis_self_subset (chart_target_mem_nhds H x)).map _ }, refine locally_compact_space_of_has_basis this _, rintro x s ⟨h₁, h₂, h₃⟩, exact h₂.image_of_continuous_on ((chart_at H x).continuous_on_symm.mono h₃) end /-- If a topological space admits an atlas with locally connected charts, then the space itself is locally connected. -/ lemma charted_space.locally_connected_space [locally_connected_space H] : locally_connected_space M := begin let E : M → local_homeomorph M H := chart_at H, refine locally_connected_space_of_connected_bases (λ x s, (E x).symm '' s) (λ x s, (is_open s ∧ E x x ∈ s ∧ is_connected s) ∧ s ⊆ (E x).target) _ _, { intros x, simpa only [local_homeomorph.symm_map_nhds_eq, mem_chart_source] using ((locally_connected_space.open_connected_basis (E x x)).restrict_subset ((E x).open_target.mem_nhds (mem_chart_target H x))).map (E x).symm }, { rintros x s ⟨⟨-, -, hsconn⟩, hssubset⟩, exact hsconn.is_preconnected.image _ ((E x).continuous_on_symm.mono hssubset) }, end /-- If `M` is modelled on `H'` and `H'` is itself modelled on `H`, then we can consider `M` as being modelled on `H`. -/ def charted_space.comp (H : Type*) [topological_space H] (H' : Type*) [topological_space H'] (M : Type*) [topological_space M] [charted_space H H'] [charted_space H' M] : charted_space H M := { atlas := image2 local_homeomorph.trans (atlas H' M) (atlas H H'), chart_at := λ p : M, (chart_at H' p).trans (chart_at H (chart_at H' p p)), mem_chart_source := λ p, by simp only with mfld_simps, chart_mem_atlas := λ p, ⟨chart_at H' p, chart_at H _, chart_mem_atlas H' p, chart_mem_atlas H _, rfl⟩ } end /-- For technical reasons we introduce two type tags: * `model_prod H H'` is the same as `H × H'`; * `model_pi H` is the same as `Π i, H i`, where `H : ι → Type*` and `ι` is a finite type. In both cases the reason is the same, so we explain it only in the case of the product. A charted space `M` with model `H` is a set of local charts from `M` to `H` covering the space. Every space is registered as a charted space over itself, using the only chart `id`, in `manifold_model_space`. You can also define a product of charted space `M` and `M'` (with model space `H × H'`) by taking the products of the charts. Now, on `H × H'`, there are two charted space structures with model space `H × H'` itself, the one coming from `manifold_model_space`, and the one coming from the product of the two `manifold_model_space` on each component. They are equal, but not defeq (because the product of `id` and `id` is not defeq to `id`), which is bad as we know. This expedient of renaming `H × H'` solves this problem. -/ library_note "Manifold type tags" /-- Same thing as `H × H'` We introduce it for technical reasons, see note [Manifold type tags]. -/ def model_prod (H : Type*) (H' : Type*) := H × H' /-- Same thing as `Π i, H i` We introduce it for technical reasons, see note [Manifold type tags]. -/ def model_pi {ι : Type*} (H : ι → Type*) := Π i, H i section local attribute [reducible] model_prod instance model_prod_inhabited [inhabited H] [inhabited H'] : inhabited (model_prod H H') := prod.inhabited instance (H : Type*) [topological_space H] (H' : Type*) [topological_space H'] : topological_space (model_prod H H') := prod.topological_space /- Next lemma shows up often when dealing with derivatives, register it as simp. -/ @[simp, mfld_simps] lemma model_prod_range_prod_id {H : Type*} {H' : Type*} {α : Type*} (f : H → α) : range (λ (p : model_prod H H'), (f p.1, p.2)) = range f ×ˢ (univ : set H') := by rw prod_range_univ_eq end section variables {ι : Type*} {Hi : ι → Type*} instance model_pi_inhabited [Π i, inhabited (Hi i)] : inhabited (model_pi Hi) := pi.inhabited _ instance [Π i, topological_space (Hi i)] : topological_space (model_pi Hi) := Pi.topological_space end /-- The product of two charted spaces is naturally a charted space, with the canonical construction of the atlas of product maps. -/ instance prod_charted_space (H : Type*) [topological_space H] (M : Type*) [topological_space M] [charted_space H M] (H' : Type*) [topological_space H'] (M' : Type*) [topological_space M'] [charted_space H' M'] : charted_space (model_prod H H') (M × M') := { atlas := image2 local_homeomorph.prod (atlas H M) (atlas H' M'), chart_at := λ x : M × M', (chart_at H x.1).prod (chart_at H' x.2), mem_chart_source := λ x, ⟨mem_chart_source _ _, mem_chart_source _ _⟩, chart_mem_atlas := λ x, mem_image2_of_mem (chart_mem_atlas _ _) (chart_mem_atlas _ _) } section prod_charted_space variables [topological_space H] [topological_space M] [charted_space H M] [topological_space H'] [topological_space M'] [charted_space H' M'] {x : M×M'} @[simp, mfld_simps] lemma prod_charted_space_chart_at : (chart_at (model_prod H H') x) = (chart_at H x.fst).prod (chart_at H' x.snd) := rfl lemma charted_space_self_prod : prod_charted_space H H H' H' = charted_space_self (H × H') := by { ext1, { simp [prod_charted_space, atlas] }, { ext1, simp [chart_at_self_eq], refl } } end prod_charted_space /-- The product of a finite family of charted spaces is naturally a charted space, with the canonical construction of the atlas of finite product maps. -/ instance pi_charted_space {ι : Type*} [fintype ι] (H : ι → Type*) [Π i, topological_space (H i)] (M : ι → Type*) [Π i, topological_space (M i)] [Π i, charted_space (H i) (M i)] : charted_space (model_pi H) (Π i, M i) := { atlas := local_homeomorph.pi '' (set.pi univ $ λ i, atlas (H i) (M i)), chart_at := λ f, local_homeomorph.pi $ λ i, chart_at (H i) (f i), mem_chart_source := λ f i hi, mem_chart_source (H i) (f i), chart_mem_atlas := λ f, mem_image_of_mem _ $ λ i hi, chart_mem_atlas (H i) (f i) } @[simp, mfld_simps] lemma pi_charted_space_chart_at {ι : Type*} [fintype ι] (H : ι → Type*) [Π i, topological_space (H i)] (M : ι → Type*) [Π i, topological_space (M i)] [Π i, charted_space (H i) (M i)] (f : Π i, M i) : chart_at (model_pi H) f = local_homeomorph.pi (λ i, chart_at (H i) (f i)) := rfl end charted_space /-! ### Constructing a topology from an atlas -/ /-- Sometimes, one may want to construct a charted space structure on a space which does not yet have a topological structure, where the topology would come from the charts. For this, one needs charts that are only local equivs, and continuity properties for their composition. This is formalised in `charted_space_core`. -/ @[nolint has_nonempty_instance] structure charted_space_core (H : Type*) [topological_space H] (M : Type*) := (atlas : set (local_equiv M H)) (chart_at : M → local_equiv M H) (mem_chart_source : ∀x, x ∈ (chart_at x).source) (chart_mem_atlas : ∀x, chart_at x ∈ atlas) (open_source : ∀e e' : local_equiv M H, e ∈ atlas → e' ∈ atlas → is_open (e.symm.trans e').source) (continuous_to_fun : ∀e e' : local_equiv M H, e ∈ atlas → e' ∈ atlas → continuous_on (e.symm.trans e') (e.symm.trans e').source) namespace charted_space_core variables [topological_space H] (c : charted_space_core H M) {e : local_equiv M H} /-- Topology generated by a set of charts on a Type. -/ protected def to_topological_space : topological_space M := topological_space.generate_from $ ⋃ (e : local_equiv M H) (he : e ∈ c.atlas) (s : set H) (s_open : is_open s), {e ⁻¹' s ∩ e.source} lemma open_source' (he : e ∈ c.atlas) : @is_open M c.to_topological_space e.source := begin apply topological_space.generate_open.basic, simp only [exists_prop, mem_Union, mem_singleton_iff], refine ⟨e, he, univ, is_open_univ, _⟩, simp only [set.univ_inter, set.preimage_univ] end lemma open_target (he : e ∈ c.atlas) : is_open e.target := begin have E : e.target ∩ e.symm ⁻¹' e.source = e.target := subset.antisymm (inter_subset_left _ _) (λx hx, ⟨hx, local_equiv.target_subset_preimage_source _ hx⟩), simpa [local_equiv.trans_source, E] using c.open_source e e he he end /-- An element of the atlas in a charted space without topology becomes a local homeomorphism for the topology constructed from this atlas. The `local_homeomorph` version is given in this definition. -/ protected def local_homeomorph (e : local_equiv M H) (he : e ∈ c.atlas) : @local_homeomorph M H c.to_topological_space _ := { open_source := by convert c.open_source' he, open_target := by convert c.open_target he, continuous_to_fun := begin letI : topological_space M := c.to_topological_space, rw continuous_on_open_iff (c.open_source' he), assume s s_open, rw inter_comm, apply topological_space.generate_open.basic, simp only [exists_prop, mem_Union, mem_singleton_iff], exact ⟨e, he, ⟨s, s_open, rfl⟩⟩ end, continuous_inv_fun := begin letI : topological_space M := c.to_topological_space, apply continuous_on_open_of_generate_from (c.open_target he), assume t ht, simp only [exists_prop, mem_Union, mem_singleton_iff] at ht, rcases ht with ⟨e', e'_atlas, s, s_open, ts⟩, rw ts, let f := e.symm.trans e', have : is_open (f ⁻¹' s ∩ f.source), by simpa [inter_comm] using (continuous_on_open_iff (c.open_source e e' he e'_atlas)).1 (c.continuous_to_fun e e' he e'_atlas) s s_open, have A : e' ∘ e.symm ⁻¹' s ∩ (e.target ∩ e.symm ⁻¹' e'.source) = e.target ∩ (e' ∘ e.symm ⁻¹' s ∩ e.symm ⁻¹' e'.source), by { rw [← inter_assoc, ← inter_assoc], congr' 1, exact inter_comm _ _ }, simpa [local_equiv.trans_source, preimage_inter, preimage_comp.symm, A] using this end, ..e } /-- Given a charted space without topology, endow it with a genuine charted space structure with respect to the topology constructed from the atlas. -/ def to_charted_space : @charted_space H _ M c.to_topological_space := { atlas := ⋃ (e : local_equiv M H) (he : e ∈ c.atlas), {c.local_homeomorph e he}, chart_at := λx, c.local_homeomorph (c.chart_at x) (c.chart_mem_atlas x), mem_chart_source := λx, c.mem_chart_source x, chart_mem_atlas := λx, begin simp only [mem_Union, mem_singleton_iff], exact ⟨c.chart_at x, c.chart_mem_atlas x, rfl⟩, end } end charted_space_core /-! ### Charted space with a given structure groupoid -/ section has_groupoid variables [topological_space H] [topological_space M] [charted_space H M] /-- A charted space has an atlas in a groupoid `G` if the change of coordinates belong to the groupoid -/ class has_groupoid {H : Type*} [topological_space H] (M : Type*) [topological_space M] [charted_space H M] (G : structure_groupoid H) : Prop := (compatible [] : ∀{e e' : local_homeomorph M H}, e ∈ atlas H M → e' ∈ atlas H M → e.symm ≫ₕ e' ∈ G) /-- Reformulate in the `structure_groupoid` namespace the compatibility condition of charts in a charted space admitting a structure groupoid, to make it more easily accessible with dot notation. -/ lemma structure_groupoid.compatible {H : Type*} [topological_space H] (G : structure_groupoid H) {M : Type*} [topological_space M] [charted_space H M] [has_groupoid M G] {e e' : local_homeomorph M H} (he : e ∈ atlas H M) (he' : e' ∈ atlas H M) : e.symm ≫ₕ e' ∈ G := has_groupoid.compatible G he he' lemma has_groupoid_of_le {G₁ G₂ : structure_groupoid H} (h : has_groupoid M G₁) (hle : G₁ ≤ G₂) : has_groupoid M G₂ := ⟨λ e e' he he', hle (h.compatible he he')⟩ lemma has_groupoid_of_pregroupoid (PG : pregroupoid H) (h : ∀{e e' : local_homeomorph M H}, e ∈ atlas H M → e' ∈ atlas H M → PG.property (e.symm ≫ₕ e') (e.symm ≫ₕ e').source) : has_groupoid M (PG.groupoid) := ⟨assume e e' he he', mem_groupoid_of_pregroupoid.mpr ⟨h he he', h he' he⟩⟩ /-- The trivial charted space structure on the model space is compatible with any groupoid -/ instance has_groupoid_model_space (H : Type*) [topological_space H] (G : structure_groupoid H) : has_groupoid H G := { compatible := λe e' he he', begin replace he : e ∈ atlas H H := he, replace he' : e' ∈ atlas H H := he', rw charted_space_self_atlas at he he', simp [he, he', structure_groupoid.id_mem] end } /-- Any charted space structure is compatible with the groupoid of all local homeomorphisms -/ instance has_groupoid_continuous_groupoid : has_groupoid M (continuous_groupoid H) := ⟨begin assume e e' he he', rw [continuous_groupoid, mem_groupoid_of_pregroupoid], simp only [and_self] end⟩ section maximal_atlas variables (M) (G : structure_groupoid H) /-- Given a charted space admitting a structure groupoid, the maximal atlas associated to this structure groupoid is the set of all local charts that are compatible with the atlas, i.e., such that changing coordinates with an atlas member gives an element of the groupoid. -/ def structure_groupoid.maximal_atlas : set (local_homeomorph M H) := {e | ∀ e' ∈ atlas H M, e.symm ≫ₕ e' ∈ G ∧ e'.symm ≫ₕ e ∈ G} variable {M} /-- The elements of the atlas belong to the maximal atlas for any structure groupoid -/ lemma structure_groupoid.subset_maximal_atlas [has_groupoid M G] : atlas H M ⊆ G.maximal_atlas M := λ e he e' he', ⟨G.compatible he he', G.compatible he' he⟩ lemma structure_groupoid.chart_mem_maximal_atlas [has_groupoid M G] (x : M) : chart_at H x ∈ G.maximal_atlas M := G.subset_maximal_atlas (chart_mem_atlas H x) variable {G} lemma mem_maximal_atlas_iff {e : local_homeomorph M H} : e ∈ G.maximal_atlas M ↔ ∀ e' ∈ atlas H M, e.symm ≫ₕ e' ∈ G ∧ e'.symm ≫ₕ e ∈ G := iff.rfl /-- Changing coordinates between two elements of the maximal atlas gives rise to an element of the structure groupoid. -/ lemma structure_groupoid.compatible_of_mem_maximal_atlas {e e' : local_homeomorph M H} (he : e ∈ G.maximal_atlas M) (he' : e' ∈ G.maximal_atlas M) : e.symm ≫ₕ e' ∈ G := begin apply G.locality (λ x hx, _), set f := chart_at H (e.symm x) with hf, let s := e.target ∩ (e.symm ⁻¹' f.source), have hs : is_open s, { apply e.symm.continuous_to_fun.preimage_open_of_open; apply open_source }, have xs : x ∈ s, by { dsimp at hx, simp [s, hx] }, refine ⟨s, hs, xs, _⟩, have A : e.symm ≫ₕ f ∈ G := (mem_maximal_atlas_iff.1 he f (chart_mem_atlas _ _)).1, have B : f.symm ≫ₕ e' ∈ G := (mem_maximal_atlas_iff.1 he' f (chart_mem_atlas _ _)).2, have C : (e.symm ≫ₕ f) ≫ₕ (f.symm ≫ₕ e') ∈ G := G.trans A B, have D : (e.symm ≫ₕ f) ≫ₕ (f.symm ≫ₕ e') ≈ (e.symm ≫ₕ e').restr s := calc (e.symm ≫ₕ f) ≫ₕ (f.symm ≫ₕ e') = e.symm ≫ₕ (f ≫ₕ f.symm) ≫ₕ e' : by simp [trans_assoc] ... ≈ e.symm ≫ₕ (of_set f.source f.open_source) ≫ₕ e' : by simp [eq_on_source.trans', trans_self_symm] ... ≈ (e.symm ≫ₕ (of_set f.source f.open_source)) ≫ₕ e' : by simp [trans_assoc] ... ≈ (e.symm.restr s) ≫ₕ e' : by simp [s, trans_of_set'] ... ≈ (e.symm ≫ₕ e').restr s : by simp [restr_trans], exact G.eq_on_source C (setoid.symm D), end variable (G) /-- In the model space, the identity is in any maximal atlas. -/ lemma structure_groupoid.id_mem_maximal_atlas : local_homeomorph.refl H ∈ G.maximal_atlas H := G.subset_maximal_atlas $ by simp /-- In the model space, any element of the groupoid is in the maximal atlas. -/ lemma structure_groupoid.mem_maximal_atlas_of_mem_groupoid {f : local_homeomorph H H} (hf : f ∈ G) : f ∈ G.maximal_atlas H := begin rintros e (rfl : e = local_homeomorph.refl H), exact ⟨G.trans (G.symm hf) G.id_mem, G.trans (G.symm G.id_mem) hf⟩, end end maximal_atlas section singleton variables {α : Type*} [topological_space α] namespace local_homeomorph variable (e : local_homeomorph α H) /-- If a single local homeomorphism `e` from a space `α` into `H` has source covering the whole space `α`, then that local homeomorphism induces an `H`-charted space structure on `α`. (This condition is equivalent to `e` being an open embedding of `α` into `H`; see `open_embedding.singleton_charted_space`.) -/ def singleton_charted_space (h : e.source = set.univ) : charted_space H α := { atlas := {e}, chart_at := λ _, e, mem_chart_source := λ _, by simp only [h] with mfld_simps, chart_mem_atlas := λ _, by tauto } @[simp, mfld_simps] lemma singleton_charted_space_chart_at_eq (h : e.source = set.univ) {x : α} : @chart_at H _ α _ (e.singleton_charted_space h) x = e := rfl lemma singleton_charted_space_chart_at_source (h : e.source = set.univ) {x : α} : (@chart_at H _ α _ (e.singleton_charted_space h) x).source = set.univ := h lemma singleton_charted_space_mem_atlas_eq (h : e.source = set.univ) (e' : local_homeomorph α H) (h' : e' ∈ (e.singleton_charted_space h).atlas) : e' = e := h' /-- Given a local homeomorphism `e` from a space `α` into `H`, if its source covers the whole space `α`, then the induced charted space structure on `α` is `has_groupoid G` for any structure groupoid `G` which is closed under restrictions. -/ lemma singleton_has_groupoid (h : e.source = set.univ) (G : structure_groupoid H) [closed_under_restriction G] : @has_groupoid _ _ _ _ (e.singleton_charted_space h) G := { compatible := begin intros e' e'' he' he'', rw e.singleton_charted_space_mem_atlas_eq h e' he', rw e.singleton_charted_space_mem_atlas_eq h e'' he'', refine G.eq_on_source _ e.trans_symm_self, have hle : id_restr_groupoid ≤ G := (closed_under_restriction_iff_id_le G).mp (by assumption), exact structure_groupoid.le_iff.mp hle _ (id_restr_groupoid_mem _), end } end local_homeomorph namespace open_embedding variable [nonempty α] /-- An open embedding of `α` into `H` induces an `H`-charted space structure on `α`. See `local_homeomorph.singleton_charted_space` -/ def singleton_charted_space {f : α → H} (h : open_embedding f) : charted_space H α := (h.to_local_homeomorph f).singleton_charted_space (by simp) lemma singleton_charted_space_chart_at_eq {f : α → H} (h : open_embedding f) {x : α} : ⇑(@chart_at H _ α _ (h.singleton_charted_space) x) = f := rfl lemma singleton_has_groupoid {f : α → H} (h : open_embedding f) (G : structure_groupoid H) [closed_under_restriction G] : @has_groupoid _ _ _ _ h.singleton_charted_space G := (h.to_local_homeomorph f).singleton_has_groupoid (by simp) G end open_embedding end singleton namespace topological_space.opens open topological_space variables (G : structure_groupoid H) [has_groupoid M G] variables (s : opens M) /-- An open subset of a charted space is naturally a charted space. -/ instance : charted_space H s := { atlas := ⋃ (x : s), {@local_homeomorph.subtype_restr _ _ _ _ (chart_at H x.1) s ⟨x⟩}, chart_at := λ x, @local_homeomorph.subtype_restr _ _ _ _ (chart_at H x.1) s ⟨x⟩, mem_chart_source := λ x, by { simp only with mfld_simps, exact (mem_chart_source H x.1) }, chart_mem_atlas := λ x, by { simp only [mem_Union, mem_singleton_iff], use x } } /-- If a groupoid `G` is `closed_under_restriction`, then an open subset of a space which is `has_groupoid G` is naturally `has_groupoid G`. -/ instance [closed_under_restriction G] : has_groupoid s G := { compatible := begin rintros e e' ⟨_, ⟨x, hc⟩, he⟩ ⟨_, ⟨x', hc'⟩, he'⟩, haveI : nonempty s := ⟨x⟩, simp only [hc.symm, mem_singleton_iff, subtype.val_eq_coe] at he, simp only [hc'.symm, mem_singleton_iff, subtype.val_eq_coe] at he', rw [he, he'], convert G.eq_on_source _ (subtype_restr_symm_trans_subtype_restr s (chart_at H x) (chart_at H x')), apply closed_under_restriction', { exact G.compatible (chart_mem_atlas H x) (chart_mem_atlas H x') }, { exact preimage_open_of_open_symm (chart_at H x) s.2 }, end } end topological_space.opens /-! ### Structomorphisms -/ /-- A `G`-diffeomorphism between two charted spaces is a homeomorphism which, when read in the charts, belongs to `G`. We avoid the word diffeomorph as it is too related to the smooth category, and use structomorph instead. -/ @[nolint has_nonempty_instance] structure structomorph (G : structure_groupoid H) (M : Type*) (M' : Type*) [topological_space M] [topological_space M'] [charted_space H M] [charted_space H M'] extends homeomorph M M' := (mem_groupoid : ∀c : local_homeomorph M H, ∀c' : local_homeomorph M' H, c ∈ atlas H M → c' ∈ atlas H M' → c.symm ≫ₕ to_homeomorph.to_local_homeomorph ≫ₕ c' ∈ G) variables [topological_space M'] [topological_space M''] {G : structure_groupoid H} [charted_space H M'] [charted_space H M''] /-- The identity is a diffeomorphism of any charted space, for any groupoid. -/ def structomorph.refl (M : Type*) [topological_space M] [charted_space H M] [has_groupoid M G] : structomorph G M M := { mem_groupoid := λc c' hc hc', begin change (local_homeomorph.symm c) ≫ₕ (local_homeomorph.refl M) ≫ₕ c' ∈ G, rw local_homeomorph.refl_trans, exact has_groupoid.compatible G hc hc' end, ..homeomorph.refl M } /-- The inverse of a structomorphism is a structomorphism -/ def structomorph.symm (e : structomorph G M M') : structomorph G M' M := { mem_groupoid := begin assume c c' hc hc', have : (c'.symm ≫ₕ e.to_homeomorph.to_local_homeomorph ≫ₕ c).symm ∈ G := G.symm (e.mem_groupoid c' c hc' hc), rwa [trans_symm_eq_symm_trans_symm, trans_symm_eq_symm_trans_symm, symm_symm, trans_assoc] at this, end, ..e.to_homeomorph.symm} /-- The composition of structomorphisms is a structomorphism -/ def structomorph.trans (e : structomorph G M M') (e' : structomorph G M' M'') : structomorph G M M'' := { mem_groupoid := begin /- Let c and c' be two charts in M and M''. We want to show that e' ∘ e is smooth in these charts, around any point x. For this, let y = e (c⁻¹ x), and consider a chart g around y. Then g ∘ e ∘ c⁻¹ and c' ∘ e' ∘ g⁻¹ are both smooth as e and e' are structomorphisms, so their composition is smooth, and it coincides with c' ∘ e' ∘ e ∘ c⁻¹ around x. -/ assume c c' hc hc', refine G.locality (λx hx, _), let f₁ := e.to_homeomorph.to_local_homeomorph, let f₂ := e'.to_homeomorph.to_local_homeomorph, let f := (e.to_homeomorph.trans e'.to_homeomorph).to_local_homeomorph, have feq : f = f₁ ≫ₕ f₂ := homeomorph.trans_to_local_homeomorph _ _, -- define the atlas g around y let y := (c.symm ≫ₕ f₁) x, let g := chart_at H y, have hg₁ := chart_mem_atlas H y, have hg₂ := mem_chart_source H y, let s := (c.symm ≫ₕ f₁).source ∩ (c.symm ≫ₕ f₁) ⁻¹' g.source, have open_s : is_open s, by apply (c.symm ≫ₕ f₁).continuous_to_fun.preimage_open_of_open; apply open_source, have : x ∈ s, { split, { simp only [trans_source, preimage_univ, inter_univ, homeomorph.to_local_homeomorph_source], rw trans_source at hx, exact hx.1 }, { exact hg₂ } }, refine ⟨s, open_s, this, _⟩, let F₁ := (c.symm ≫ₕ f₁ ≫ₕ g) ≫ₕ (g.symm ≫ₕ f₂ ≫ₕ c'), have A : F₁ ∈ G := G.trans (e.mem_groupoid c g hc hg₁) (e'.mem_groupoid g c' hg₁ hc'), let F₂ := (c.symm ≫ₕ f ≫ₕ c').restr s, have : F₁ ≈ F₂ := calc F₁ ≈ c.symm ≫ₕ f₁ ≫ₕ (g ≫ₕ g.symm) ≫ₕ f₂ ≫ₕ c' : by simp [F₁, trans_assoc] ... ≈ c.symm ≫ₕ f₁ ≫ₕ (of_set g.source g.open_source) ≫ₕ f₂ ≫ₕ c' : by simp [eq_on_source.trans', trans_self_symm g] ... ≈ ((c.symm ≫ₕ f₁) ≫ₕ (of_set g.source g.open_source)) ≫ₕ (f₂ ≫ₕ c') : by simp [trans_assoc] ... ≈ ((c.symm ≫ₕ f₁).restr s) ≫ₕ (f₂ ≫ₕ c') : by simp [s, trans_of_set'] ... ≈ ((c.symm ≫ₕ f₁) ≫ₕ (f₂ ≫ₕ c')).restr s : by simp [restr_trans] ... ≈ (c.symm ≫ₕ (f₁ ≫ₕ f₂) ≫ₕ c').restr s : by simp [eq_on_source.restr, trans_assoc] ... ≈ F₂ : by simp [F₂, feq], have : F₂ ∈ G := G.eq_on_source A (setoid.symm this), exact this end, ..homeomorph.trans e.to_homeomorph e'.to_homeomorph } end has_groupoid
7e41efec7f6c2d1cad893674b70556ddeb9dd62a
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/set/intervals/pi.lean
b9a28ce41fd2321d50b22a5ba112e6cbe0237ad6
[ "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,141
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import data.set.intervals.basic import data.set.lattice /-! # Intervals in `pi`-space In this we prove various simple lemmas about intervals in `Π i, α i`. Closed intervals (`Ici x`, `Iic x`, `Icc x y`) are equal to products of their projections to `α i`, while (semi-)open intervals usually include the corresponding products as proper subsets. -/ variables {ι : Type*} {α : ι → Type*} namespace set section pi_preorder variables [Π i, preorder (α i)] (x y : Π i, α i) @[simp] lemma pi_univ_Ici : pi univ (λ i, Ici (x i)) = Ici x := ext $ λ y, by simp [pi.le_def] @[simp] lemma pi_univ_Iic : pi univ (λ i, Iic (x i)) = Iic x := ext $ λ y, by simp [pi.le_def] @[simp] lemma pi_univ_Icc : pi univ (λ i, Icc (x i) (y i)) = Icc x y := ext $ λ y, by simp [pi.le_def, forall_and_distrib] lemma piecewise_mem_Icc {s : set ι} [Π j, decidable (j ∈ s)] {f₁ f₂ g₁ g₂ : Π i, α i} (h₁ : ∀ i ∈ s, f₁ i ∈ Icc (g₁ i) (g₂ i)) (h₂ : ∀ i ∉ s, f₂ i ∈ Icc (g₁ i) (g₂ i)) : s.piecewise f₁ f₂ ∈ Icc g₁ g₂ := ⟨le_piecewise (λ i hi, (h₁ i hi).1) (λ i hi, (h₂ i hi).1), piecewise_le (λ i hi, (h₁ i hi).2) (λ i hi, (h₂ i hi).2)⟩ lemma piecewise_mem_Icc' {s : set ι} [Π j, decidable (j ∈ s)] {f₁ f₂ g₁ g₂ : Π i, α i} (h₁ : f₁ ∈ Icc g₁ g₂) (h₂ : f₂ ∈ Icc g₁ g₂) : s.piecewise f₁ f₂ ∈ Icc g₁ g₂ := piecewise_mem_Icc (λ i hi, ⟨h₁.1 _, h₁.2 _⟩) (λ i hi, ⟨h₂.1 _, h₂.2 _⟩) section nonempty variable [nonempty ι] lemma pi_univ_Ioi_subset : pi univ (λ i, Ioi (x i)) ⊆ Ioi x := λ z hz, ⟨λ i, le_of_lt $ hz i trivial, λ h, nonempty.elim ‹nonempty ι› $ λ i, (h i).not_lt (hz i trivial)⟩ lemma pi_univ_Iio_subset : pi univ (λ i, Iio (x i)) ⊆ Iio x := @pi_univ_Ioi_subset ι (λ i, (α i)ᵒᵈ) _ x _ lemma pi_univ_Ioo_subset : pi univ (λ i, Ioo (x i) (y i)) ⊆ Ioo x y := λ x hx, ⟨pi_univ_Ioi_subset _ $ λ i hi, (hx i hi).1, pi_univ_Iio_subset _ $ λ i hi, (hx i hi).2⟩ lemma pi_univ_Ioc_subset : pi univ (λ i, Ioc (x i) (y i)) ⊆ Ioc x y := λ x hx, ⟨pi_univ_Ioi_subset _ $ λ i hi, (hx i hi).1, λ i, (hx i trivial).2⟩ lemma pi_univ_Ico_subset : pi univ (λ i, Ico (x i) (y i)) ⊆ Ico x y := λ x hx, ⟨λ i, (hx i trivial).1, pi_univ_Iio_subset _ $ λ i hi, (hx i hi).2⟩ end nonempty variable [decidable_eq ι] open function (update) lemma pi_univ_Ioc_update_left {x y : Π i, α i} {i₀ : ι} {m : α i₀} (hm : x i₀ ≤ m) : pi univ (λ i, Ioc (update x i₀ m i) (y i)) = {z | m < z i₀} ∩ pi univ (λ i, Ioc (x i) (y i)) := begin have : Ioc m (y i₀) = Ioi m ∩ Ioc (x i₀) (y i₀), by rw [← Ioi_inter_Iic, ← Ioi_inter_Iic, ← inter_assoc, inter_eq_self_of_subset_left (Ioi_subset_Ioi hm)], simp_rw [univ_pi_update i₀ _ _ (λ i z, Ioc z (y i)), ← pi_inter_compl ({i₀} : set ι), singleton_pi', ← inter_assoc, this], refl end lemma pi_univ_Ioc_update_right {x y : Π i, α i} {i₀ : ι} {m : α i₀} (hm : m ≤ y i₀) : pi univ (λ i, Ioc (x i) (update y i₀ m i)) = {z | z i₀ ≤ m} ∩ pi univ (λ i, Ioc (x i) (y i)) := begin have : Ioc (x i₀) m = Iic m ∩ Ioc (x i₀) (y i₀), by rw [← Ioi_inter_Iic, ← Ioi_inter_Iic, inter_left_comm, inter_eq_self_of_subset_left (Iic_subset_Iic.2 hm)], simp_rw [univ_pi_update i₀ y m (λ i z, Ioc (x i) z), ← pi_inter_compl ({i₀} : set ι), singleton_pi', ← inter_assoc, this], refl end lemma disjoint_pi_univ_Ioc_update_left_right {x y : Π i, α i} {i₀ : ι} {m : α i₀} : disjoint (pi univ (λ i, Ioc (x i) (update y i₀ m i))) (pi univ (λ i, Ioc (update x i₀ m i) (y i))) := begin rw disjoint_left, rintro z h₁ h₂, refine (h₁ i₀ (mem_univ _)).2.not_lt _, simpa only [function.update_same] using (h₂ i₀ (mem_univ _)).1 end end pi_preorder variables [decidable_eq ι] [Π i, linear_order (α i)] open function (update) lemma pi_univ_Ioc_update_union (x y : Π i, α i) (i₀ : ι) (m : α i₀) (hm : m ∈ Icc (x i₀) (y i₀)) : pi univ (λ i, Ioc (x i) (update y i₀ m i)) ∪ pi univ (λ i, Ioc (update x i₀ m i) (y i)) = pi univ (λ i, Ioc (x i) (y i)) := by simp_rw [pi_univ_Ioc_update_left hm.1, pi_univ_Ioc_update_right hm.2, ← union_inter_distrib_right, ← set_of_or, le_or_lt, set_of_true, univ_inter] /-- If `x`, `y`, `x'`, and `y'` are functions `Π i : ι, α i`, then the set difference between the box `[x, y]` and the product of the open intervals `(x' i, y' i)` is covered by the union of the following boxes: for each `i : ι`, we take `[x, update y i (x' i)]` and `[update x i (y' i), y]`. E.g., if `x' = x` and `y' = y`, then this lemma states that the difference between a closed box `[x, y]` and the corresponding open box `{z | ∀ i, x i < z i < y i}` is covered by the union of the faces of `[x, y]`. -/ lemma Icc_diff_pi_univ_Ioo_subset (x y x' y' : Π i, α i) : Icc x y \ pi univ (λ i, Ioo (x' i) (y' i)) ⊆ (⋃ i : ι, Icc x (update y i (x' i))) ∪ ⋃ i : ι, Icc (update x i (y' i)) y := begin rintros a ⟨⟨hxa, hay⟩, ha'⟩, simpa [le_update_iff, update_le_iff, hxa, hay, hxa _, hay _, ← exists_or_distrib, not_and_distrib] using ha' end /-- If `x`, `y`, `z` are functions `Π i : ι, α i`, then the set difference between the box `[x, z]` and the product of the intervals `(y i, z i]` is covered by the union of the boxes `[x, update z i (y i)]`. E.g., if `x = y`, then this lemma states that the difference between a closed box `[x, y]` and the product of half-open intervals `{z | ∀ i, x i < z i ≤ y i}` is covered by the union of the faces of `[x, y]` adjacent to `x`. -/ lemma Icc_diff_pi_univ_Ioc_subset (x y z : Π i, α i) : Icc x z \ pi univ (λ i, Ioc (y i) (z i)) ⊆ ⋃ i : ι, Icc x (update z i (y i)) := begin rintros a ⟨⟨hax, haz⟩, hay⟩, simpa [not_and_distrib, hax, le_update_iff, haz _] using hay end end set
d1e7bca3ef1b40bb5ff7483ebf98eebb545d8fdd
3446e92e64a5de7ed1f2109cfb024f83cd904c34
/src/game/world2/level8.lean
b3570214ff588be94e2453e5674c0baadf7fc055
[]
no_license
kckennylau/natural_number_game
019f4a5f419c9681e65234ecd124c564f9a0a246
ad8c0adaa725975be8a9f978c8494a39311029be
refs/heads/master
1,598,784,137,722
1,571,905,156,000
1,571,905,156,000
218,354,686
0
0
null
1,572,373,319,000
1,572,373,318,000
null
UTF-8
Lean
false
false
1,377
lean
import mynat.definition -- hide import mynat.add -- hide import game.world2.level7 -- hide namespace mynat -- hide /- # World 2 -- Addition World ## Level 8 -- `eq_iff_succ_eq_succ` You have these: * `zero_ne_succ : ∀ (a : mynat), zero ≠ succ(a)` * `succ_inj : ∀ a b : mynat, succ(a) = succ(b) → a = b` * `add_zero : ∀ a : mynat, a + 0 = a` * `add_succ : ∀ a b : mynat, a + succ(b) = succ(a + b)` * `zero_add : ∀ a : mynat, 0 + a = a` * `add_assoc : ∀ a b c : mynat, (a + b) + c = a + (b + c)` * `succ_add : ∀ a b : mynat, succ a + b = succ (a + b)` * `add_comm : ∀ a b : mynat, a + b = b + a` Here is an `iff` goal. You can split it into two goals (the implications in both directions) using the `split` tactic, which you should probably start with. You are also going to have to learn something about tactics which handle the basics of propositions and their proofs here. You will need to know the `intro` tactic, which works (only) on a goal of the form `P → Q`; given a goal of this form, `intro h` makes `h` a proof of `P` and changes the goal to `Q`. -/ /- Theorem Two natural numbers are equal if and only if their successors are equal. -/ theorem eq_iff_succ_eq_succ (a b : mynat) : succ a = succ b ↔ a = b := begin [less_leaky] split, { exact succ_inj}, { intro H, rw H, refl, } end end mynat -- hide
d73e8669cb4f8616b44a31b07ae1135bdd60091c
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/category_theory/limits/shapes/wide_pullbacks.lean
de96e44f7620d1c52ba39c2dec88378d41cb56db
[ "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
5,406
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import category_theory.limits.limits import category_theory.sparse /-! # Wide pullbacks We define the category `wide_pullback_shape`, (resp. `wide_pushout_shape`) which is the category obtained from a discrete category of type `J` by adjoining a terminal (resp. initial) element. Limits of this shape are wide pullbacks (pushouts). The convenience method `wide_cospan` (`wide_span`) constructs a functor from this category, hitting the given morphisms. We use `wide_pullback_shape` to define ordinary pullbacks (pushouts) by using `J := walking_pair`, which allows easy proofs of some related lemmas. Furthermore, wide pullbacks are used to show the existence of limits in the slice category. Namely, if `C` has wide pullbacks then `C/B` has limits for any object `B` in `C`. Typeclasses `has_wide_pullbacks` and `has_finite_wide_pullbacks` assert the existence of wide pullbacks and finite wide pullbacks. -/ universes v u open category_theory category_theory.limits namespace category_theory.limits variable (J : Type v) /-- A wide pullback shape for any type `J` can be written simply as `option J`. -/ @[derive inhabited] def wide_pullback_shape := option J /-- A wide pushout shape for any type `J` can be written simply as `option J`. -/ @[derive inhabited] def wide_pushout_shape := option J namespace wide_pullback_shape variable {J} /-- The type of arrows for the shape indexing a wide pullback. -/ @[derive decidable_eq] inductive hom : wide_pullback_shape J → wide_pullback_shape J → Type v | id : Π X, hom X X | term : Π (j : J), hom (some j) none attribute [nolint unused_arguments] hom.decidable_eq instance struct : category_struct (wide_pullback_shape J) := { hom := hom, id := λ j, hom.id j, comp := λ j₁ j₂ j₃ f g, begin cases f, exact g, cases g, apply hom.term _ end } instance hom.inhabited : inhabited (hom none none) := ⟨hom.id (none : wide_pullback_shape J)⟩ local attribute [tidy] tactic.case_bash instance subsingleton_hom (j j' : wide_pullback_shape J) : subsingleton (j ⟶ j') := ⟨by tidy⟩ instance category : small_category (wide_pullback_shape J) := sparse_category @[simp] lemma hom_id (X : wide_pullback_shape J) : hom.id X = 𝟙 X := rfl variables {C : Type u} [category.{v} C] /-- Construct a functor out of the wide pullback shape given a J-indexed collection of arrows to a fixed object. -/ @[simps] def wide_cospan (B : C) (objs : J → C) (arrows : Π (j : J), objs j ⟶ B) : wide_pullback_shape J ⥤ C := { obj := λ j, option.cases_on j B objs, map := λ X Y f, begin cases f with _ j, { apply (𝟙 _) }, { exact arrows j } end } /-- Every diagram is naturally isomorphic (actually, equal) to a `wide_cospan` -/ def diagram_iso_wide_cospan (F : wide_pullback_shape J ⥤ C) : F ≅ wide_cospan (F.obj none) (λ j, F.obj (some j)) (λ j, F.map (hom.term j)) := nat_iso.of_components (λ j, eq_to_iso $ by tidy) $ by tidy end wide_pullback_shape namespace wide_pushout_shape variable {J} /-- The type of arrows for the shape indexing a wide psuhout. -/ @[derive decidable_eq] inductive hom : wide_pushout_shape J → wide_pushout_shape J → Type v | id : Π X, hom X X | init : Π (j : J), hom none (some j) attribute [nolint unused_arguments] hom.decidable_eq instance struct : category_struct (wide_pushout_shape J) := { hom := hom, id := λ j, hom.id j, comp := λ j₁ j₂ j₃ f g, begin cases f, exact g, cases g, apply hom.init _ end } instance hom.inhabited : inhabited (hom none none) := ⟨hom.id (none : wide_pushout_shape J)⟩ local attribute [tidy] tactic.case_bash instance subsingleton_hom (j j' : wide_pushout_shape J) : subsingleton (j ⟶ j') := ⟨by tidy⟩ instance category : small_category (wide_pushout_shape J) := sparse_category @[simp] lemma hom_id (X : wide_pushout_shape J) : hom.id X = 𝟙 X := rfl variables {C : Type u} [category.{v} C] /-- Construct a functor out of the wide pushout shape given a J-indexed collection of arrows from a fixed object. -/ @[simps] def wide_span (B : C) (objs : J → C) (arrows : Π (j : J), B ⟶ objs j) : wide_pushout_shape J ⥤ C := { obj := λ j, option.cases_on j B objs, map := λ X Y f, begin cases f with _ j, { apply (𝟙 _) }, { exact arrows j } end } /-- Every diagram is naturally isomorphic (actually, equal) to a `wide_span` -/ def diagram_iso_wide_span (F : wide_pushout_shape J ⥤ C) : F ≅ wide_span (F.obj none) (λ j, F.obj (some j)) (λ j, F.map (hom.init j)) := nat_iso.of_components (λ j, eq_to_iso $ by tidy) $ by tidy end wide_pushout_shape variables (C : Type u) [category.{v} C] /-- `has_wide_pullbacks` represents a choice of wide pullback for every collection of morphisms -/ class has_wide_pullbacks := (has_limits_of_shape : Π (J : Type v), has_limits_of_shape (wide_pullback_shape J) C) attribute [instance] has_wide_pullbacks.has_limits_of_shape /-- `has_wide_pushouts` represents a choice of wide pushout for every collection of morphisms -/ class has_wide_pushouts := (has_colimits_of_shape : Π (J : Type v), has_colimits_of_shape (wide_pushout_shape J) C) attribute [instance] has_wide_pushouts.has_colimits_of_shape end category_theory.limits
b406a86b32e652b0433aa5761bc667b74d1b14bf
4727251e0cd73359b15b664c3170e5d754078599
/src/algebra/quotient.lean
82c1ccb8eae7d8bee150d5d46cb8d4a819ee682b
[ "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
2,062
lean
/- Copyright (c) 2021 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import data.set_like.basic /-! # Algebraic quotients This file defines notation for algebraic quotients, e.g. quotient groups `G ⧸ H`, quotient modules `M ⧸ N` and ideal quotients `R ⧸ I`. The actual quotient structures are defined in the following files: * quotient group: `src/group_theory/quotient_group.lean` * quotient module: `src/linear_algebra/quotient.lean` * quotient ring: `src/ring_theory/ideal/quotient.lean` ## Notations The following notation is introduced: * `G ⧸ H` stands for the quotient of the type `G` by some term `H` (for example, `H` can be a normal subgroup of `G`). To implement this notation for other quotients, you should provide a `has_quotient` instance. Note that since `G` can usually be inferred from `H`, `_ ⧸ H` can also be used, but this is less readable. ## Tags quotient, group quotient, quotient group, module quotient, quotient module, ring quotient, ideal quotient, quotient ring -/ universes u v /-- `has_quotient A B` is a notation typeclass that allows us to write `A ⧸ b` for `b : B`. This allows the usual notation for quotients of algebraic structures, such as groups, modules and rings. `A` is a parameter, despite being unused in the definition below, so it appears in the notation. -/ class has_quotient (A : out_param $ Type u) (B : Type v) := (quotient' : B → Type (max u v)) /-- `has_quotient.quotient A b` (with notation `A ⧸ b`) is the quotient of the type `A` by `b`. This differs from `has_quotient.quotient'` in that the `A` argument is explicit, which is necessary to make Lean show the notation in the goal state. -/ @[reducible, nolint has_inhabited_instance] -- Will be provided by e.g. `ideal.quotient.inhabited` def has_quotient.quotient (A : out_param $ Type u) {B : Type v} [has_quotient A B] (b : B) : Type (max u v) := has_quotient.quotient' b notation G ` ⧸ `:35 H:34 := has_quotient.quotient G H
f7bf887708e5fcc3830810dfb9ccec0731cbe3d6
94e33a31faa76775069b071adea97e86e218a8ee
/src/algebra/big_operators/pi.lean
dd4d4d6481263aa558b11f38011162a3a0cae925
[ "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
3,885
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Patrick Massot -/ import algebra.big_operators.basic import algebra.ring.pi /-! # Big operators for Pi Types This file contains theorems relevant to big operators in binary and arbitrary product of monoids and groups -/ open_locale big_operators namespace pi @[to_additive] lemma list_prod_apply {α : Type*} {β : α → Type*} [Πa, monoid (β a)] (a : α) (l : list (Πa, β a)) : l.prod a = (l.map (λf:Πa, β a, f a)).prod := (eval_monoid_hom β a).map_list_prod _ @[to_additive] lemma multiset_prod_apply {α : Type*} {β : α → Type*} [∀a, comm_monoid (β a)] (a : α) (s : multiset (Πa, β a)) : s.prod a = (s.map (λf:Πa, β a, f a)).prod := (eval_monoid_hom β a).map_multiset_prod _ end pi @[simp, to_additive] lemma finset.prod_apply {α : Type*} {β : α → Type*} {γ} [∀a, comm_monoid (β a)] (a : α) (s : finset γ) (g : γ → Πa, β a) : (∏ c in s, g c) a = ∏ c in s, g c a := (pi.eval_monoid_hom β a).map_prod _ _ /-- An 'unapplied' analogue of `finset.prod_apply`. -/ @[to_additive "An 'unapplied' analogue of `finset.sum_apply`."] lemma finset.prod_fn {α : Type*} {β : α → Type*} {γ} [∀a, comm_monoid (β a)] (s : finset γ) (g : γ → Πa, β a) : (∏ c in s, g c) = (λ a, ∏ c in s, g c a) := funext (λ a, finset.prod_apply _ _ _) @[simp, to_additive] lemma fintype.prod_apply {α : Type*} {β : α → Type*} {γ : Type*} [fintype γ] [∀a, comm_monoid (β a)] (a : α) (g : γ → Πa, β a) : (∏ c, g c) a = ∏ c, g c a := finset.prod_apply a finset.univ g @[to_additive prod_mk_sum] lemma prod_mk_prod {α β γ : Type*} [comm_monoid α] [comm_monoid β] (s : finset γ) (f : γ → α) (g : γ → β) : (∏ x in s, f x, ∏ x in s, g x) = ∏ x in s, (f x, g x) := by haveI := classical.dec_eq γ; exact finset.induction_on s rfl (by simp [prod.ext_iff] {contextual := tt}) section single variables {I : Type*} [decidable_eq I] {Z : I → Type*} variables [Π i, add_comm_monoid (Z i)] -- As we only defined `single` into `add_monoid`, we only prove the `finset.sum` version here. lemma finset.univ_sum_single [fintype I] (f : Π i, Z i) : ∑ i, pi.single i (f i) = f := by { ext a, simp } lemma add_monoid_hom.functions_ext [fintype I] (G : Type*) [add_comm_monoid G] (g h : (Π i, Z i) →+ G) (w : ∀ (i : I) (x : Z i), g (pi.single i x) = h (pi.single i x)) : g = h := begin ext k, rw [← finset.univ_sum_single k, g.map_sum, h.map_sum], simp only [w] end /-- This is used as the ext lemma instead of `add_monoid_hom.functions_ext` for reasons explained in note [partially-applied ext lemmas]. -/ @[ext] lemma add_monoid_hom.functions_ext' [fintype I] (M : Type*) [add_comm_monoid M] (g h : (Π i, Z i) →+ M) (H : ∀ i, g.comp (add_monoid_hom.single Z i) = h.comp (add_monoid_hom.single Z i)) : g = h := have _ := λ i, add_monoid_hom.congr_fun (H i), -- elab without an expected type g.functions_ext M h this end single section ring_hom open pi variables {I : Type*} [decidable_eq I] {f : I → Type*} variables [Π i, non_assoc_semiring (f i)] @[ext] lemma ring_hom.functions_ext [fintype I] (G : Type*) [non_assoc_semiring G] (g h : (Π i, f i) →+* G) (w : ∀ (i : I) (x : f i), g (single i x) = h (single i x)) : g = h := ring_hom.coe_add_monoid_hom_injective $ @add_monoid_hom.functions_ext I _ f _ _ G _ (g : (Π i, f i) →+ G) h w end ring_hom namespace prod variables {α β γ : Type*} [comm_monoid α] [comm_monoid β] {s : finset γ} {f : γ → α × β} @[to_additive] lemma fst_prod : (∏ c in s, f c).1 = ∏ c in s, (f c).1 := (monoid_hom.fst α β).map_prod f s @[to_additive] lemma snd_prod : (∏ c in s, f c).2 = ∏ c in s, (f c).2 := (monoid_hom.snd α β).map_prod f s end prod
4d2a8b365a7db6913c4850e27052563d0e37c68d
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/stage0/src/Lean/Meta/Check.lean
47582e08502a0be0369fecc844e7867d2202891e
[ "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
7,140
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.InferType /-! This is not the Kernel type checker, but an auxiliary method for checking whether terms produced by tactics and `isDefEq` are type correct. -/ namespace Lean.Meta private def ensureType (e : Expr) : MetaM Unit := do discard <| getLevel e def throwLetTypeMismatchMessage {α} (fvarId : FVarId) : MetaM α := do let lctx ← getLCtx match lctx.find? fvarId with | some (LocalDecl.ldecl _ _ _ t v _) => do let vType ← inferType v throwError "invalid let declaration, term{indentExpr v}\nhas type{indentExpr vType}\nbut is expected to have type{indentExpr t}" | _ => unreachable! private def checkConstant (constName : Name) (us : List Level) : MetaM Unit := do let cinfo ← getConstInfo constName unless us.length == cinfo.levelParams.length do throwIncorrectNumberOfLevels constName us private def getFunctionDomain (f : Expr) : MetaM (Expr × BinderInfo) := do let fType ← inferType f let fType ← whnfD fType match fType with | Expr.forallE _ d _ c => return (d, c) | _ => throwFunctionExpected f /-- Given two expressions `a` and `b`, this method tries to annotate terms with `pp.explicit := true` to expose "implicit" differences. For example, suppose `a` and `b` are of the form ```lean @HashMap Nat Nat eqInst hasInst1 @HashMap Nat Nat eqInst hasInst2 ``` By default, the pretty printer formats both of them as `HashMap Nat Nat`. So, counterintuitive error messages such as ```lean error: application type mismatch HashMap.insert m argument m has type HashMap Nat Nat but is expected to have type HashMap Nat Nat ``` would be produced. By adding `pp.explicit := true`, we can generate the more informative error ```lean error: application type mismatch HashMap.insert m argument m has type @HashMap Nat Nat eqInst hasInst1 but is expected to have type @HashMap Nat Nat eqInst hasInst2 ``` Remark: this method implements a simple heuristic, we should extend it as we find other counterintuitive error messages. -/ partial def addPPExplicitToExposeDiff (a b : Expr) : MetaM (Expr × Expr) := do if (← getOptions).getBool `pp.all false || (← getOptions).getBool `pp.explicit false then return (a, b) else visit (← instantiateMVars a) (← instantiateMVars b) where visit (a b : Expr) : MetaM (Expr × Expr) := do try if !a.isApp || !b.isApp then return (a, b) else if a.getAppNumArgs != b.getAppNumArgs then return (a, b) else if not (← isDefEq a.getAppFn b.getAppFn) then return (a, b) else let fType ← inferType a.getAppFn forallBoundedTelescope fType a.getAppNumArgs fun xs _ => do let mut as := a.getAppArgs let mut bs := b.getAppArgs if let some (as', bs') ← hasExplicitDiff? xs as bs then return (mkAppN a.getAppFn as', mkAppN b.getAppFn bs') else for i in [:as.size] do unless (← isDefEq as[i]! bs[i]!) do let (ai, bi) ← visit as[i]! bs[i]! as := as.set! i ai bs := bs.set! i bi let a := mkAppN a.getAppFn as let b := mkAppN b.getAppFn bs return (a.setAppPPExplicit, b.setAppPPExplicit) catch _ => return (a, b) hasExplicitDiff? (xs as bs : Array Expr) : MetaM (Option (Array Expr × Array Expr)) := do for i in [:xs.size] do let localDecl ← xs[i]!.fvarId!.getDecl if localDecl.binderInfo.isExplicit then unless (← isDefEq as[i]! bs[i]!) do let (ai, bi) ← visit as[i]! bs[i]! return some (as.set! i ai, bs.set! i bi) return none /-- Return error message "has type{givenType}\nbut is expected to have type{expectedType}" -/ def mkHasTypeButIsExpectedMsg (givenType expectedType : Expr) : MetaM MessageData := do try let givenTypeType ← inferType givenType let expectedTypeType ← inferType expectedType let (givenType, expectedType) ← addPPExplicitToExposeDiff givenType expectedType let (givenTypeType, expectedTypeType) ← addPPExplicitToExposeDiff givenTypeType expectedTypeType return m!"has type{indentD m!"{givenType} : {givenTypeType}"}\nbut is expected to have type{indentD m!"{expectedType} : {expectedTypeType}"}" catch _ => let (givenType, expectedType) ← addPPExplicitToExposeDiff givenType expectedType return m!"has type{indentExpr givenType}\nbut is expected to have type{indentExpr expectedType}" def throwAppTypeMismatch (f a : Expr) : MetaM α := do let (expectedType, binfo) ← getFunctionDomain f let mut e := mkApp f a unless binfo.isExplicit do e := e.setAppPPExplicit let aType ← inferType a throwError "application type mismatch{indentExpr e}\nargument{indentExpr a}\n{← mkHasTypeButIsExpectedMsg aType expectedType}" def checkApp (f a : Expr) : MetaM Unit := do let fType ← inferType f let fType ← whnf fType match fType with | Expr.forallE _ d _ _ => let aType ← inferType a unless (← isDefEq d aType) do throwAppTypeMismatch f a | _ => throwFunctionExpected (mkApp f a) private partial def checkAux (e : Expr) : MetaM Unit := do check e |>.run where check (e : Expr) : MonadCacheT ExprStructEq Unit MetaM Unit := checkCache { val := e : ExprStructEq } fun _ => do match e with | .forallE .. => checkForall e | .lam .. => checkLambdaLet e | .letE .. => checkLambdaLet e | .const c lvls => checkConstant c lvls | .app f a => check f; check a; checkApp f a | .mdata _ e => check e | .proj _ _ e => check e | _ => return () checkLambdaLet (e : Expr) : MonadCacheT ExprStructEq Unit MetaM Unit := lambdaLetTelescope e fun xs b => do xs.forM fun x => do let xDecl ← getFVarLocalDecl x; match xDecl with | .cdecl (type := t) .. => ensureType t check t | .ldecl (type := t) (value := v) .. => ensureType t check t let vType ← inferType v unless (← isDefEq t vType) do throwLetTypeMismatchMessage x.fvarId! check v check b checkForall (e : Expr) : MonadCacheT ExprStructEq Unit MetaM Unit := forallTelescope e fun xs b => do xs.forM fun x => do let xDecl ← getFVarLocalDecl x ensureType xDecl.type check xDecl.type ensureType b check b /-- Throw an exception if `e` is not type correct. -/ def check (e : Expr) : MetaM Unit := traceCtx `Meta.check do withTransparency TransparencyMode.all $ checkAux e /-- Return true if `e` is type correct. -/ def isTypeCorrect (e : Expr) : MetaM Bool := do try check e pure true catch ex => trace[Meta.typeError] ex.toMessageData pure false builtin_initialize registerTraceClass `Meta.check registerTraceClass `Meta.typeError end Lean.Meta
eb5a5333a78f69ff6460fc65ca7c0e815c20df29
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/data/hf.lean
4ca9ff27b417a2038ed4989dd0a32d9ad61d0fba
[ "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
25,343
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura Hereditarily finite sets: finite sets whose elements are all hereditarily finite sets. Remark: all definitions compute, however the performace is quite poor since we implement this module using a bijection from (finset nat) to nat, and this bijection is implemeted using the Ackermann coding. -/ import data.nat data.finset.equiv data.list open nat binary open - [notation] finset definition hf := nat namespace hf local attribute hf [reducible] protected definition prio : num := num.succ std.priority.default protected definition is_inhabited [instance] : inhabited hf := nat.is_inhabited protected definition has_decidable_eq [instance] : decidable_eq hf := nat.has_decidable_eq definition of_finset (s : finset hf) : hf := @equiv.to_fun _ _ finset_nat_equiv_nat s definition to_finset (h : hf) : finset hf := @equiv.inv _ _ finset_nat_equiv_nat h definition to_nat (h : hf) : nat := h definition of_nat (n : nat) : hf := n lemma to_finset_of_finset (s : finset hf) : to_finset (of_finset s) = s := @equiv.left_inv _ _ finset_nat_equiv_nat s lemma of_finset_to_finset (s : hf) : of_finset (to_finset s) = s := @equiv.right_inv _ _ finset_nat_equiv_nat s lemma to_finset_inj {s₁ s₂ : hf} : to_finset s₁ = to_finset s₂ → s₁ = s₂ := λ h, function.injective_of_left_inverse of_finset_to_finset h lemma of_finset_inj {s₁ s₂ : finset hf} : of_finset s₁ = of_finset s₂ → s₁ = s₂ := λ h, function.injective_of_left_inverse to_finset_of_finset h /- empty -/ definition empty : hf := of_finset (finset.empty) notation `∅` := hf.empty /- insert -/ definition insert (a s : hf) : hf := of_finset (finset.insert a (to_finset s)) /- mem -/ definition mem (a : hf) (s : hf) : Prop := finset.mem a (to_finset s) infix ∈ := mem notation [priority finset.prio] a ∉ b := ¬ mem a b lemma insert_lt_of_not_mem {a s : hf} : a ∉ s → s < insert a s := begin unfold [insert, of_finset, finset_nat_equiv_nat, mem, to_finset, equiv.inv], intro h, rewrite [finset.to_nat_insert h], rewrite [to_nat_of_nat, -zero_add s at {1}], apply add_lt_add_right, apply pow_pos_of_pos _ dec_trivial end lemma insert_lt_insert_of_not_mem_of_not_mem_of_lt {a s₁ s₂ : hf} : a ∉ s₁ → a ∉ s₂ → s₁ < s₂ → insert a s₁ < insert a s₂ := begin unfold [insert, of_finset, finset_nat_equiv_nat, mem, to_finset, equiv.inv], intro h₁ h₂ h₃, rewrite [finset.to_nat_insert h₁], rewrite [finset.to_nat_insert h₂, *to_nat_of_nat], apply add_lt_add_left h₃ end open decidable protected definition decidable_mem [instance] : ∀ a s, decidable (a ∈ s) := λ a s, finset.decidable_mem a (to_finset s) lemma insert_le (a s : hf) : s ≤ insert a s := by_cases (suppose a ∈ s, by rewrite [↑insert, insert_eq_of_mem this, of_finset_to_finset]) (suppose a ∉ s, le_of_lt (insert_lt_of_not_mem this)) lemma not_mem_empty (a : hf) : a ∉ ∅ := begin unfold [mem, empty], rewrite to_finset_of_finset, apply finset.not_mem_empty end lemma mem_insert (a s : hf) : a ∈ insert a s := begin unfold [mem, insert], rewrite to_finset_of_finset, apply finset.mem_insert end lemma mem_insert_of_mem {a s : hf} (b : hf) : a ∈ s → a ∈ insert b s := begin unfold [mem, insert], intros, rewrite to_finset_of_finset, apply finset.mem_insert_of_mem, assumption end lemma eq_or_mem_of_mem_insert {a b s : hf} : a ∈ insert b s → a = b ∨ a ∈ s := begin unfold [mem, insert], rewrite to_finset_of_finset, intros, apply eq_or_mem_of_mem_insert, assumption end theorem mem_of_mem_insert_of_ne {x a : hf} {s : hf} : x ∈ insert a s → x ≠ a → x ∈ s := begin unfold [mem, insert], rewrite to_finset_of_finset, intros, apply mem_of_mem_insert_of_ne, repeat assumption end protected theorem ext {s₁ s₂ : hf} : (∀ a, a ∈ s₁ ↔ a ∈ s₂) → s₁ = s₂ := assume h, have to_finset s₁ = to_finset s₂, from finset.ext h, have of_finset (to_finset s₁) = of_finset (to_finset s₂), by rewrite this, by rewrite [*of_finset_to_finset at this]; exact this theorem insert_eq_of_mem {a : hf} {s : hf} : a ∈ s → insert a s = s := begin unfold mem, intro h, unfold [mem, insert], rewrite (finset.insert_eq_of_mem h), rewrite of_finset_to_finset end protected theorem induction [recursor 4] {P : hf → Prop} (h₁ : P empty) (h₂ : ∀ (a s : hf), a ∉ s → P s → P (insert a s)) (s : hf) : P s := have P (of_finset (to_finset s)), from @finset.induction _ _ _ h₁ (λ a s nain ih, begin unfold [mem, insert] at h₂, rewrite -(to_finset_of_finset s) at nain, have P (insert a (of_finset s)), by exact h₂ a (of_finset s) nain ih, rewrite [↑insert at this, to_finset_of_finset at this], exact this end) (to_finset s), by rewrite of_finset_to_finset at this; exact this lemma insert_le_insert_of_le {a s₁ s₂ : hf} : a ∈ s₁ ∨ a ∉ s₂ → s₁ ≤ s₂ → insert a s₁ ≤ insert a s₂ := suppose a ∈ s₁ ∨ a ∉ s₂, suppose s₁ ≤ s₂, by_cases (suppose s₁ = s₂, by rewrite this) (suppose s₁ ≠ s₂, have s₁ < s₂, from lt_of_le_of_ne `s₁ ≤ s₂` `s₁ ≠ s₂`, by_cases (suppose a ∈ s₁, by_cases (suppose a ∈ s₂, by rewrite [insert_eq_of_mem `a ∈ s₁`, insert_eq_of_mem `a ∈ s₂`]; assumption) (suppose a ∉ s₂, by rewrite [insert_eq_of_mem `a ∈ s₁`]; exact le.trans `s₁ ≤ s₂` !insert_le)) (suppose a ∉ s₁, by_cases (suppose a ∈ s₂, or.elim `a ∈ s₁ ∨ a ∉ s₂` (by contradiction) (by contradiction)) (suppose a ∉ s₂, le_of_lt (insert_lt_insert_of_not_mem_of_not_mem_of_lt `a ∉ s₁` `a ∉ s₂` `s₁ < s₂`)))) /- union -/ definition union (s₁ s₂ : hf) : hf := of_finset (finset.union (to_finset s₁) (to_finset s₂)) infix [priority hf.prio] ∪ := union theorem mem_union_left {a : hf} {s₁ : hf} (s₂ : hf) : a ∈ s₁ → a ∈ s₁ ∪ s₂ := begin unfold mem, intro h, unfold union, rewrite to_finset_of_finset, apply finset.mem_union_left _ h end theorem mem_union_l {a : hf} {s₁ : hf} {s₂ : hf} : a ∈ s₁ → a ∈ s₁ ∪ s₂ := mem_union_left s₂ theorem mem_union_right {a : hf} {s₂ : hf} (s₁ : hf) : a ∈ s₂ → a ∈ s₁ ∪ s₂ := begin unfold mem, intro h, unfold union, rewrite to_finset_of_finset, apply finset.mem_union_right _ h end theorem mem_union_r {a : hf} {s₂ : hf} {s₁ : hf} : a ∈ s₂ → a ∈ s₁ ∪ s₂ := mem_union_right s₁ theorem mem_or_mem_of_mem_union {a : hf} {s₁ s₂ : hf} : a ∈ s₁ ∪ s₂ → a ∈ s₁ ∨ a ∈ s₂ := begin unfold [mem, union], rewrite to_finset_of_finset, intro h, apply finset.mem_or_mem_of_mem_union h end theorem mem_union_iff {a : hf} (s₁ s₂ : hf) : a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂ := iff.intro (λ h, mem_or_mem_of_mem_union h) (λ d, or.elim d (λ i, mem_union_left _ i) (λ i, mem_union_right _ i)) theorem mem_union_eq {a : hf} (s₁ s₂ : hf) : (a ∈ s₁ ∪ s₂) = (a ∈ s₁ ∨ a ∈ s₂) := propext !mem_union_iff theorem union_comm (s₁ s₂ : hf) : s₁ ∪ s₂ = s₂ ∪ s₁ := hf.ext (λ a, by rewrite [*mem_union_eq]; exact or.comm) theorem union_assoc (s₁ s₂ s₃ : hf) : (s₁ ∪ s₂) ∪ s₃ = s₁ ∪ (s₂ ∪ s₃) := hf.ext (λ a, by rewrite [*mem_union_eq]; exact or.assoc) theorem union_left_comm (s₁ s₂ s₃ : hf) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) := !left_comm union_comm union_assoc s₁ s₂ s₃ theorem union_right_comm (s₁ s₂ s₃ : hf) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ := !right_comm union_comm union_assoc s₁ s₂ s₃ theorem union_self (s : hf) : s ∪ s = s := hf.ext (λ a, iff.intro (λ ain, or.elim (mem_or_mem_of_mem_union ain) (λ i, i) (λ i, i)) (λ i, mem_union_left _ i)) theorem union_empty (s : hf) : s ∪ ∅ = s := hf.ext (λ a, iff.intro (suppose a ∈ s ∪ ∅, or.elim (mem_or_mem_of_mem_union this) (λ i, i) (λ i, absurd i !not_mem_empty)) (suppose a ∈ s, mem_union_left _ this)) theorem empty_union (s : hf) : ∅ ∪ s = s := calc ∅ ∪ s = s ∪ ∅ : union_comm ... = s : union_empty /- inter -/ definition inter (s₁ s₂ : hf) : hf := of_finset (finset.inter (to_finset s₁) (to_finset s₂)) infix [priority hf.prio] ∩ := inter theorem mem_of_mem_inter_left {a : hf} {s₁ s₂ : hf} : a ∈ s₁ ∩ s₂ → a ∈ s₁ := begin unfold mem, unfold inter, rewrite to_finset_of_finset, intro h, apply finset.mem_of_mem_inter_left h end theorem mem_of_mem_inter_right {a : hf} {s₁ s₂ : hf} : a ∈ s₁ ∩ s₂ → a ∈ s₂ := begin unfold mem, unfold inter, rewrite to_finset_of_finset, intro h, apply finset.mem_of_mem_inter_right h end theorem mem_inter {a : hf} {s₁ s₂ : hf} : a ∈ s₁ → a ∈ s₂ → a ∈ s₁ ∩ s₂ := begin unfold mem, intro h₁ h₂, unfold inter, rewrite to_finset_of_finset, apply finset.mem_inter h₁ h₂ end theorem mem_inter_iff (a : hf) (s₁ s₂ : hf) : a ∈ s₁ ∩ s₂ ↔ a ∈ s₁ ∧ a ∈ s₂ := iff.intro (λ h, and.intro (mem_of_mem_inter_left h) (mem_of_mem_inter_right h)) (λ h, mem_inter (and.elim_left h) (and.elim_right h)) theorem mem_inter_eq (a : hf) (s₁ s₂ : hf) : (a ∈ s₁ ∩ s₂) = (a ∈ s₁ ∧ a ∈ s₂) := propext !mem_inter_iff theorem inter_comm (s₁ s₂ : hf) : s₁ ∩ s₂ = s₂ ∩ s₁ := hf.ext (λ a, by rewrite [*mem_inter_eq]; exact and.comm) theorem inter_assoc (s₁ s₂ s₃ : hf) : (s₁ ∩ s₂) ∩ s₃ = s₁ ∩ (s₂ ∩ s₃) := hf.ext (λ a, by rewrite [*mem_inter_eq]; exact and.assoc) theorem inter_left_comm (s₁ s₂ s₃ : hf) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) := !left_comm inter_comm inter_assoc s₁ s₂ s₃ theorem inter_right_comm (s₁ s₂ s₃ : hf) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ := !right_comm inter_comm inter_assoc s₁ s₂ s₃ theorem inter_self (s : hf) : s ∩ s = s := hf.ext (λ a, iff.intro (λ h, mem_of_mem_inter_right h) (λ h, mem_inter h h)) theorem inter_empty (s : hf) : s ∩ ∅ = ∅ := hf.ext (λ a, iff.intro (suppose a ∈ s ∩ ∅, absurd (mem_of_mem_inter_right this) !not_mem_empty) (suppose a ∈ ∅, absurd this !not_mem_empty)) theorem empty_inter (s : hf) : ∅ ∩ s = ∅ := calc ∅ ∩ s = s ∩ ∅ : inter_comm ... = ∅ : inter_empty /- card -/ definition card (s : hf) : nat := finset.card (to_finset s) theorem card_empty : card ∅ = 0 := rfl lemma ne_empty_of_card_eq_succ {s : hf} {n : nat} : card s = succ n → s ≠ ∅ := by intros; substvars; contradiction /- erase -/ definition erase (a : hf) (s : hf) : hf := of_finset (erase a (to_finset s)) theorem not_mem_erase (a : hf) (s : hf) : a ∉ erase a s := begin unfold [mem, erase], rewrite to_finset_of_finset, apply finset.not_mem_erase end theorem card_erase_of_mem {a : hf} {s : hf} : a ∈ s → card (erase a s) = pred (card s) := begin unfold mem, intro h, unfold [erase, card], rewrite to_finset_of_finset, apply finset.card_erase_of_mem h end theorem card_erase_of_not_mem {a : hf} {s : hf} : a ∉ s → card (erase a s) = card s := begin unfold [mem], intro h, unfold [erase, card], rewrite to_finset_of_finset, apply finset.card_erase_of_not_mem h end theorem erase_empty (a : hf) : erase a ∅ = ∅ := rfl theorem ne_of_mem_erase {a b : hf} {s : hf} : b ∈ erase a s → b ≠ a := by intro h beqa; subst b; exact absurd h !not_mem_erase theorem mem_of_mem_erase {a b : hf} {s : hf} : b ∈ erase a s → b ∈ s := begin unfold [erase, mem], rewrite to_finset_of_finset, intro h, apply mem_of_mem_erase h end theorem mem_erase_of_ne_of_mem {a b : hf} {s : hf} : a ≠ b → a ∈ s → a ∈ erase b s := begin intro h₁, unfold mem, intro h₂, unfold erase, rewrite to_finset_of_finset, apply mem_erase_of_ne_of_mem h₁ h₂ end theorem mem_erase_iff (a b : hf) (s : hf) : a ∈ erase b s ↔ a ∈ s ∧ a ≠ b := iff.intro (assume H, and.intro (mem_of_mem_erase H) (ne_of_mem_erase H)) (assume H, mem_erase_of_ne_of_mem (and.right H) (and.left H)) theorem mem_erase_eq (a b : hf) (s : hf) : a ∈ erase b s = (a ∈ s ∧ a ≠ b) := propext !mem_erase_iff theorem erase_insert {a : hf} {s : hf} : a ∉ s → erase a (insert a s) = s := begin unfold [mem, erase, insert], intro h, rewrite [to_finset_of_finset, finset.erase_insert h, of_finset_to_finset] end theorem insert_erase {a : hf} {s : hf} : a ∈ s → insert a (erase a s) = s := begin unfold mem, intro h, unfold [insert, erase], rewrite [to_finset_of_finset, finset.insert_erase h, of_finset_to_finset] end /- subset -/ definition subset (s₁ s₂ : hf) : Prop := finset.subset (to_finset s₁) (to_finset s₂) infix [priority hf.prio] ⊆ := subset theorem empty_subset (s : hf) : ∅ ⊆ s := begin unfold [empty, subset], rewrite to_finset_of_finset, apply finset.empty_subset (to_finset s) end theorem subset.refl (s : hf) : s ⊆ s := begin unfold [subset], apply finset.subset.refl (to_finset s) end theorem subset.trans {s₁ s₂ s₃ : hf} : s₁ ⊆ s₂ → s₂ ⊆ s₃ → s₁ ⊆ s₃ := begin unfold [subset], intro h₁ h₂, apply finset.subset.trans h₁ h₂ end theorem mem_of_subset_of_mem {s₁ s₂ : hf} {a : hf} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ := begin unfold [subset, mem], intro h₁ h₂, apply finset.mem_of_subset_of_mem h₁ h₂ end theorem subset.antisymm {s₁ s₂ : hf} : s₁ ⊆ s₂ → s₂ ⊆ s₁ → s₁ = s₂ := begin unfold [subset], intro h₁ h₂, apply to_finset_inj (finset.subset.antisymm h₁ h₂) end -- alternative name theorem eq_of_subset_of_subset {s₁ s₂ : hf} (H₁ : s₁ ⊆ s₂) (H₂ : s₂ ⊆ s₁) : s₁ = s₂ := subset.antisymm H₁ H₂ theorem subset_of_forall {s₁ s₂ : hf} : (∀x, x ∈ s₁ → x ∈ s₂) → s₁ ⊆ s₂ := begin unfold [mem, subset], intro h, apply finset.subset_of_forall h end theorem subset_insert (s : hf) (a : hf) : s ⊆ insert a s := begin unfold [subset, insert], rewrite to_finset_of_finset, apply finset.subset_insert (to_finset s) end theorem eq_empty_of_subset_empty {x : hf} (H : x ⊆ ∅) : x = ∅ := subset.antisymm H (empty_subset x) theorem subset_empty_iff (x : hf) : x ⊆ ∅ ↔ x = ∅ := iff.intro eq_empty_of_subset_empty (take xeq, by rewrite xeq; apply subset.refl ∅) theorem erase_subset_erase (a : hf) {s t : hf} : s ⊆ t → erase a s ⊆ erase a t := begin unfold [subset, erase], intro h, rewrite *to_finset_of_finset, apply finset.erase_subset_erase a h end theorem erase_subset (a : hf) (s : hf) : erase a s ⊆ s := begin unfold [subset, erase], rewrite to_finset_of_finset, apply finset.erase_subset a (to_finset s) end theorem erase_eq_of_not_mem {a : hf} {s : hf} : a ∉ s → erase a s = s := begin unfold [mem, erase], intro h, rewrite [finset.erase_eq_of_not_mem h, of_finset_to_finset] end theorem erase_insert_subset (a : hf) (s : hf) : erase a (insert a s) ⊆ s := begin unfold [erase, insert, subset], rewrite [*to_finset_of_finset], apply finset.erase_insert_subset a (to_finset s) end theorem erase_subset_of_subset_insert {a : hf} {s t : hf} (H : s ⊆ insert a t) : erase a s ⊆ t := hf.subset.trans (!hf.erase_subset_erase H) (erase_insert_subset a t) theorem insert_erase_subset (a : hf) (s : hf) : s ⊆ insert a (erase a s) := decidable.by_cases (assume ains : a ∈ s, by rewrite [!insert_erase ains]; apply subset.refl) (assume nains : a ∉ s, suffices s ⊆ insert a s, by rewrite [erase_eq_of_not_mem nains]; assumption, subset_insert s a) theorem insert_subset_insert (a : hf) {s t : hf} : s ⊆ t → insert a s ⊆ insert a t := begin unfold [subset, insert], intro h, rewrite *to_finset_of_finset, apply finset.insert_subset_insert a h end theorem subset_insert_of_erase_subset {s t : hf} {a : hf} (H : erase a s ⊆ t) : s ⊆ insert a t := subset.trans (insert_erase_subset a s) (!insert_subset_insert H) theorem subset_insert_iff (s t : hf) (a : hf) : s ⊆ insert a t ↔ erase a s ⊆ t := iff.intro !erase_subset_of_subset_insert !subset_insert_of_erase_subset theorem le_of_subset {s₁ s₂ : hf} : s₁ ⊆ s₂ → s₁ ≤ s₂ := begin revert s₂, induction s₁ with a s₁ nain ih, take s₂, suppose ∅ ⊆ s₂, !zero_le, take s₂, suppose insert a s₁ ⊆ s₂, have a ∈ s₂, from mem_of_subset_of_mem this !mem_insert, have a ∉ erase a s₂, from !not_mem_erase, have s₁ ⊆ erase a s₂, from subset_of_forall (take x xin, by_cases (suppose x = a, by subst x; contradiction) (suppose x ≠ a, have x ∈ s₂, from mem_of_subset_of_mem `insert a s₁ ⊆ s₂` (mem_insert_of_mem _ `x ∈ s₁`), mem_erase_of_ne_of_mem `x ≠ a` `x ∈ s₂`)), have s₁ ≤ erase a s₂, from ih _ this, have insert a s₁ ≤ insert a (erase a s₂), from insert_le_insert_of_le (or.inr `a ∉ erase a s₂`) this, by rewrite [insert_erase `a ∈ s₂` at this]; exact this end /- image -/ definition image (f : hf → hf) (s : hf) : hf := of_finset (finset.image f (to_finset s)) theorem image_empty (f : hf → hf) : image f ∅ = ∅ := rfl theorem mem_image_of_mem (f : hf → hf) {s : hf} {a : hf} : a ∈ s → f a ∈ image f s := begin unfold [mem, image], intro h, rewrite to_finset_of_finset, apply finset.mem_image_of_mem f h end theorem mem_image {f : hf → hf} {s : hf} {a : hf} {b : hf} (H1 : a ∈ s) (H2 : f a = b) : b ∈ image f s := eq.subst H2 (mem_image_of_mem f H1) theorem exists_of_mem_image {f : hf → hf} {s : hf} {b : hf} : b ∈ image f s → ∃a, a ∈ s ∧ f a = b := begin unfold [mem, image], rewrite to_finset_of_finset, intro h, apply finset.exists_of_mem_image h end theorem mem_image_iff (f : hf → hf) {s : hf} {y : hf} : y ∈ image f s ↔ ∃x, x ∈ s ∧ f x = y := begin unfold [mem, image], rewrite to_finset_of_finset, apply finset.mem_image_iff end theorem mem_image_eq (f : hf → hf) {s : hf} {y : hf} : y ∈ image f s = ∃x, x ∈ s ∧ f x = y := propext (mem_image_iff f) theorem mem_image_of_mem_image_of_subset {f : hf → hf} {s t : hf} {y : hf} (H1 : y ∈ image f s) (H2 : s ⊆ t) : y ∈ image f t := obtain x `x ∈ s` `f x = y`, from exists_of_mem_image H1, have x ∈ t, from mem_of_subset_of_mem H2 `x ∈ s`, show y ∈ image f t, from mem_image `x ∈ t` `f x = y` theorem image_insert (f : hf → hf) (s : hf) (a : hf) : image f (insert a s) = insert (f a) (image f s) := begin unfold [image, insert], rewrite [*to_finset_of_finset, finset.image_insert] end open function lemma image_comp {f : hf → hf} {g : hf → hf} {s : hf} : image (f∘g) s = image f (image g s) := begin unfold image, rewrite [*to_finset_of_finset, finset.image_comp] end lemma image_subset {a b : hf} (f : hf → hf) : a ⊆ b → image f a ⊆ image f b := begin unfold [subset, image], intro h, rewrite *to_finset_of_finset, apply finset.image_subset f h end theorem image_union (f : hf → hf) (s t : hf) : image f (s ∪ t) = image f s ∪ image f t := begin unfold [image, union], rewrite [*to_finset_of_finset, finset.image_union] end /- powerset -/ definition powerset (s : hf) : hf := of_finset (finset.image of_finset (finset.powerset (to_finset s))) prefix [priority hf.prio] `𝒫`:100 := powerset theorem powerset_empty : 𝒫 ∅ = insert ∅ ∅ := rfl theorem powerset_insert {a : hf} {s : hf} : a ∉ s → 𝒫 (insert a s) = 𝒫 s ∪ image (insert a) (𝒫 s) := begin unfold [mem, powerset, insert, union, image], rewrite [*to_finset_of_finset], intro h, have (λ (x : finset hf), of_finset (finset.insert a x)) = (λ (x : finset hf), of_finset (finset.insert a (to_finset (of_finset x)))), from funext (λ x, by rewrite to_finset_of_finset), rewrite [finset.powerset_insert h, finset.image_union, -*finset.image_comp, ↑comp, this] end theorem mem_powerset_iff_subset (s : hf) : ∀ x : hf, x ∈ 𝒫 s ↔ x ⊆ s := begin intro x, unfold [mem, powerset, subset], rewrite [to_finset_of_finset, finset.mem_image_eq], apply iff.intro, suppose (∃ (w : finset hf), finset.mem w (finset.powerset (to_finset s)) ∧ of_finset w = x), obtain w h₁ h₂, from this, begin subst x, rewrite to_finset_of_finset, exact iff.mp !finset.mem_powerset_iff_subset h₁ end, suppose finset.subset (to_finset x) (to_finset s), have finset.mem (to_finset x) (finset.powerset (to_finset s)), from iff.mpr !finset.mem_powerset_iff_subset this, exists.intro (to_finset x) (and.intro this (of_finset_to_finset x)) end theorem subset_of_mem_powerset {s t : hf} (H : s ∈ 𝒫 t) : s ⊆ t := iff.mp (mem_powerset_iff_subset t s) H theorem mem_powerset_of_subset {s t : hf} (H : s ⊆ t) : s ∈ 𝒫 t := iff.mpr (mem_powerset_iff_subset t s) H theorem empty_mem_powerset (s : hf) : ∅ ∈ 𝒫 s := mem_powerset_of_subset (empty_subset s) /- hf as lists -/ open - [notation] list definition of_list (s : list hf) : hf := @equiv.to_fun _ _ list_nat_equiv_nat s definition to_list (h : hf) : list hf := @equiv.inv _ _ list_nat_equiv_nat h lemma to_list_of_list (s : list hf) : to_list (of_list s) = s := @equiv.left_inv _ _ list_nat_equiv_nat s lemma of_list_to_list (s : hf) : of_list (to_list s) = s := @equiv.right_inv _ _ list_nat_equiv_nat s lemma to_list_inj {s₁ s₂ : hf} : to_list s₁ = to_list s₂ → s₁ = s₂ := λ h, function.injective_of_left_inverse of_list_to_list h lemma of_list_inj {s₁ s₂ : list hf} : of_list s₁ = of_list s₂ → s₁ = s₂ := λ h, function.injective_of_left_inverse to_list_of_list h definition nil : hf := of_list list.nil lemma empty_eq_nil : ∅ = nil := rfl definition cons (a l : hf) : hf := of_list (list.cons a (to_list l)) infixr :: := cons lemma cons_ne_nil (a l : hf) : a::l ≠ nil := by contradiction lemma head_eq_of_cons_eq {h₁ h₂ t₁ t₂ : hf} : (h₁::t₁) = (h₂::t₂) → h₁ = h₂ := begin unfold cons, intro h, apply list.head_eq_of_cons_eq (of_list_inj h) end lemma tail_eq_of_cons_eq {h₁ h₂ t₁ t₂ : hf} : (h₁::t₁) = (h₂::t₂) → t₁ = t₂ := begin unfold cons, intro h, apply to_list_inj (list.tail_eq_of_cons_eq (of_list_inj h)) end lemma cons_inj {a : hf} : injective (cons a) := take l₁ l₂, assume Pe, tail_eq_of_cons_eq Pe /- append -/ definition append (l₁ l₂ : hf) : hf := of_list (list.append (to_list l₁) (to_list l₂)) notation l₁ ++ l₂ := append l₁ l₂ theorem append_nil_left [simp] (t : hf) : nil ++ t = t := begin unfold [nil, append], rewrite [to_list_of_list, list.append_nil_left, of_list_to_list] end theorem append_cons [simp] (x s t : hf) : (x::s) ++ t = x::(s ++ t) := begin unfold [cons, append], rewrite [*to_list_of_list, list.append_cons] end theorem append_nil_right [simp] (t : hf) : t ++ nil = t := begin unfold [nil, append], rewrite [to_list_of_list, list.append_nil_right, of_list_to_list] end theorem append.assoc [simp] (s t u : hf) : s ++ t ++ u = s ++ (t ++ u) := begin unfold append, rewrite [*to_list_of_list, list.append.assoc] end /- length -/ definition length (l : hf) : nat := list.length (to_list l) theorem length_nil [simp] : length nil = 0 := begin unfold [length, nil] end theorem length_cons [simp] (x t : hf) : length (x::t) = length t + 1 := begin unfold [length, cons], rewrite to_list_of_list end theorem length_append [simp] (s t : hf) : length (s ++ t) = length s + length t := begin unfold [length, append], rewrite [to_list_of_list, list.length_append] end theorem eq_nil_of_length_eq_zero {l : hf} : length l = 0 → l = nil := begin unfold [length, nil], intro h, rewrite [-list.eq_nil_of_length_eq_zero h, of_list_to_list] end theorem ne_nil_of_length_eq_succ {l : hf} {n : nat} : length l = succ n → l ≠ nil := begin unfold [length, nil], intro h₁ h₂, subst l, rewrite to_list_of_list at h₁, contradiction end /- head and tail -/ definition head (l : hf) : hf := list.head (to_list l) theorem head_cons [simp] (a l : hf) : head (a::l) = a := begin unfold [head, cons], rewrite to_list_of_list end private lemma to_list_ne_list_nil {s : hf} : s ≠ nil → to_list s ≠ list.nil := begin unfold nil, intro h, suppose to_list s = list.nil, by rewrite [-this at h, of_list_to_list at h]; exact absurd rfl h end theorem head_append [simp] (t : hf) {s : hf} : s ≠ nil → head (s ++ t) = head s := begin unfold [nil, head, append], rewrite to_list_of_list, suppose s ≠ of_list list.nil, by rewrite [list.head_append _ (to_list_ne_list_nil this)] end definition tail (l : hf) : hf := of_list (list.tail (to_list l)) theorem tail_nil [simp] : tail nil = nil := begin unfold [tail, nil] end theorem tail_cons [simp] (a l : hf) : tail (a::l) = l := begin unfold [tail, cons], rewrite [to_list_of_list, list.tail_cons, of_list_to_list] end theorem cons_head_tail {l : hf} : l ≠ nil → (head l)::(tail l) = l := begin unfold [nil, head, tail, cons], suppose l ≠ of_list list.nil, by rewrite [to_list_of_list, list.cons_head_tail (to_list_ne_list_nil this), of_list_to_list] end end hf
ee752c3173995f615c395627c93d2e0f33e19f10
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Lean/Data/Lsp/Ipc.lean
29325d1a79760db2cddd9c4e0c8bea571ab53611
[ "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
2,832
lean
/- Copyright (c) 2020 Marc Huisinga. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Marc Huisinga, Wojciech Nawrocki -/ import Init.System.IO import Lean.Data.Json import Lean.Data.Lsp.Communication import Lean.Data.Lsp.Diagnostics import Lean.Data.Lsp.Extra /-! Provides an IpcM monad for interacting with an external LSP process. Used for testing the Lean server. -/ namespace Lean.Lsp.Ipc open IO open JsonRpc def ipcStdioConfig : Process.StdioConfig where stdin := Process.Stdio.piped stdout := Process.Stdio.piped stderr := Process.Stdio.inherit abbrev IpcM := ReaderT (Process.Child ipcStdioConfig) IO variable [ToJson α] def stdin : IpcM FS.Stream := do return FS.Stream.ofHandle (←read).stdin def stdout : IpcM FS.Stream := do return FS.Stream.ofHandle (←read).stdout def writeRequest (r : Request α) : IpcM Unit := do (←stdin).writeLspRequest r def writeNotification (n : Notification α) : IpcM Unit := do (←stdin).writeLspNotification n def readMessage : IpcM JsonRpc.Message := do (←stdout).readLspMessage def readRequestAs (expectedMethod : String) (α) [FromJson α] : IpcM (Request α) := do (←stdout).readLspRequestAs expectedMethod α def readResponseAs (expectedID : RequestID) (α) [FromJson α] : IpcM (Response α) := do (←stdout).readLspResponseAs expectedID α def waitForExit : IpcM UInt32 := do (←read).wait /-- Waits for the worker to emit all diagnostics for the current document version and returns them as a list. -/ partial def collectDiagnostics (waitForDiagnosticsId : RequestID := 0) (target : DocumentUri) (version : Nat) : IpcM (List (Notification PublishDiagnosticsParams)) := do writeRequest ⟨waitForDiagnosticsId, "textDocument/waitForDiagnostics", WaitForDiagnosticsParams.mk target version⟩ let rec loop : IpcM (List (Notification PublishDiagnosticsParams)) := do match (←readMessage) with | Message.response id _ => if id == waitForDiagnosticsId then return [] else loop | Message.responseError id _ msg _ => if id == waitForDiagnosticsId then throw $ userError s!"Waiting for diagnostics failed: {msg}" else loop | Message.notification "textDocument/publishDiagnostics" (some param) => match fromJson? (toJson param) with | Except.ok diagnosticParam => return ⟨"textDocument/publishDiagnostics", diagnosticParam⟩ :: (←loop) | Except.error inner => throw $ userError s!"Cannot decode publishDiagnostics parameters\n{inner}" | _ => loop loop def runWith (lean : System.FilePath) (args : Array String := #[]) (test : IpcM α) : IO α := do let proc ← Process.spawn { toStdioConfig := ipcStdioConfig cmd := lean.toString args := args } ReaderT.run test proc end Lean.Lsp.Ipc
ee4a994aaa55fd1ea3079fb7da056a3bbee5d1a5
98beff2e97d91a54bdcee52f922c4e1866a6c9b9
/src/applications/sheaves_on_a_site.lean
2661d28ac5f51c6003d5048fb6b5cbe3b9f7287a
[]
no_license
b-mehta/topos
c3fc43fb04ba16bae1965ce5c26c6461172e5bc6
c9032b11789e36038bc841a1e2b486972421b983
refs/heads/master
1,629,609,492,867
1,609,907,263,000
1,609,907,263,000
240,943,034
43
3
null
1,598,210,062,000
1,581,877,668,000
Lean
UTF-8
Lean
false
false
3,505
lean
import applications.topologies import opens /-! The category `Sheaf` of (set-valued) sheaves on a site is defined, using the sheaf condition as defined in `grothendieck.lean`. It's defined somewhat abstractly, but an equivalent condition (`grothendieck.sheaf_condition'`) is given there too, which is more concrete. -/ universes v u noncomputable theory namespace category_theory namespace site_sheaf open category limits variables (C : Type u) [small_category C] (J : sieve_set C) [grothendieck J] structure Sheaf := (P : Cᵒᵖ ⥤ Type u) (sheaf_cond : grothendieck.sheaf_condition J P) instance : category (Sheaf C J) := induced_category.category Sheaf.P /-- The category of sheaves (in the Grothendieck sense) is equivalent to a special case of sheaves on a local operator. We use this equivalence to transfer properties from the abstract topos theory to the geometric case. -/ def equiv_lt_sheaf : Sheaf C J ≌ sheaf (j J) := { functor := { obj := λ P, sheaf.mk P.P (equivalent_sheaf_conditions _ _ P.sheaf_cond), map := λ P Q f, f }, inverse := { obj := λ P, ⟨P.A, (equivalent_sheaf_conditions _ _).symm (get_condition P)⟩, map := λ P Q f, f }, unit_iso := nat_iso.of_components (λ P, {hom := 𝟙 _, inv := 𝟙 _}) (by tidy), counit_iso := nat_iso.of_components (λ P, {hom := 𝟙 _, inv := 𝟙 _}) (by tidy) } /-- The forgetful functor from sheaves to presheaves. -/ def forget_Sheaf : Sheaf C J ⥤ (Cᵒᵖ ⥤ Type u) := induced_functor _ instance : full (forget_Sheaf C J) := induced_category.full _ instance : faithful (forget_Sheaf C J) := induced_category.faithful _ /-- The sheafification functor for sheaves on a site. -/ def sheafify : (Cᵒᵖ ⥤ Type u) ⥤ Sheaf C J := sheafification (j J) ⋙ (equiv_lt_sheaf _ _).inverse /-- The equivalence between Sheaf and sheaf commutes with their respective forgetful functors. -/ lemma forget_comm : forget_Sheaf C J = (equiv_lt_sheaf _ _).functor ⋙ sheaf.forget (j J) := rfl /-- Sheafification is left adjoint to the inclusion into presheaves. -/ def Sheafy_adjoint : sheafify C J ⊣ forget_Sheaf C J := adjunction.comp _ _ (sheafification_is_adjoint (j J)) (equiv_lt_sheaf _ _).symm.to_adjunction instance : is_right_adjoint (forget_Sheaf C J) := { left := _, adj := Sheafy_adjoint C J } /-- The category of sheaves is a reflective subcategory of presheaves -/ instance : reflective (forget_Sheaf C J) := {}. /-- The forgetful functor creates limits. -/ instance : creates_limits (forget_Sheaf C J) := { creates_limits_of_shape := λ D 𝒟, { creates_limit := λ K, begin change creates_limit _ ((equiv_lt_sheaf _ _).functor ⋙ sheaf.forget (j J)), apply_instance, end } } /-- Sheafification preserves finite products -/ instance preserve_fin_prod (D : Type u) [decidable_eq D] [fintype D] : preserves_limits_of_shape (discrete D) (sheafify C J) := { preserves_limit := λ K, begin unfold sheafify, haveI := sheafification_preserves_finite_products (j J) D, apply_instance, end } /-- Sheafification preserves equalizers -/ instance preserve_equalizer : preserves_limits_of_shape walking_parallel_pair (sheafify C J) := { preserves_limit := λ K, begin unfold sheafify, haveI := sheafification_preserves_equalizers (j J), apply_instance, end } end site_sheaf open topological_space topological_space.opens /-- Sheaves on a space. -/ abbreviation Sh (X : Type u) [topological_space X] := site_sheaf.Sheaf (opens X) (covering X) end category_theory
fd49ae6860945267ceee61ddb3620e31e98fc81a
b2fe74b11b57d362c13326bc5651244f111fa6f4
/src/logic/basic.lean
7d8a1255931ab8724f1b1987df1dc82518c88f2f
[ "Apache-2.0" ]
permissive
midfield/mathlib
c4db5fa898b5ac8f2f80ae0d00c95eb6f745f4c7
775edc615ecec631d65b6180dbcc7bc26c3abc26
refs/heads/master
1,675,330,551,921
1,608,304,514,000
1,608,304,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
51,310
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura -/ import tactic.doc_commands import tactic.reserved_notation /-! # Basic logic properties This file is one of the earliest imports in mathlib. ## Implementation notes Theorems that require decidability hypotheses are in the namespace "decidable". Classical versions are in the namespace "classical". In the presence of automation, this whole file may be unnecessary. On the other hand, maybe it is useful for writing automation. -/ local attribute [instance, priority 10] classical.prop_decidable section miscellany /- We add the `inline` attribute to optimize VM computation using these declarations. For example, `if p ∧ q then ... else ...` will not evaluate the decidability of `q` if `p` is false. -/ attribute [inline] and.decidable or.decidable decidable.false xor.decidable iff.decidable decidable.true implies.decidable not.decidable ne.decidable bool.decidable_eq decidable.to_bool attribute [simp] cast_eq variables {α : Type*} {β : Type*} /-- An identity function with its main argument implicit. This will be printed as `hidden` even if it is applied to a large term, so it can be used for elision, as done in the `elide` and `unelide` tactics. -/ @[reducible] def hidden {α : Sort*} {a : α} := a /-- Ex falso, the nondependent eliminator for the `empty` type. -/ def empty.elim {C : Sort*} : empty → C. instance : subsingleton empty := ⟨λa, a.elim⟩ instance subsingleton.prod {α β : Type*} [subsingleton α] [subsingleton β] : subsingleton (α × β) := ⟨by { intros a b, cases a, cases b, congr, }⟩ instance : decidable_eq empty := λa, a.elim instance sort.inhabited : inhabited (Sort*) := ⟨punit⟩ instance sort.inhabited' : inhabited (default (Sort*)) := ⟨punit.star⟩ instance psum.inhabited_left {α β} [inhabited α] : inhabited (psum α β) := ⟨psum.inl (default _)⟩ instance psum.inhabited_right {α β} [inhabited β] : inhabited (psum α β) := ⟨psum.inr (default _)⟩ @[priority 10] instance decidable_eq_of_subsingleton {α} [subsingleton α] : decidable_eq α | a b := is_true (subsingleton.elim a b) @[simp] lemma eq_iff_true_of_subsingleton [subsingleton α] (x y : α) : x = y ↔ true := by cc /-- Add an instance to "undo" coercion transitivity into a chain of coercions, because most simp lemmas are stated with respect to simple coercions and will not match when part of a chain. -/ @[simp] theorem coe_coe {α β γ} [has_coe α β] [has_coe_t β γ] (a : α) : (a : γ) = (a : β) := rfl theorem coe_fn_coe_trans {α β γ} [has_coe α β] [has_coe_t_aux β γ] [has_coe_to_fun γ] (x : α) : @coe_fn α _ x = @coe_fn β _ x := rfl @[simp] theorem coe_fn_coe_base {α β} [has_coe α β] [has_coe_to_fun β] (x : α) : @coe_fn α _ x = @coe_fn β _ x := rfl theorem coe_sort_coe_trans {α β γ} [has_coe α β] [has_coe_t_aux β γ] [has_coe_to_sort γ] (x : α) : @coe_sort α _ x = @coe_sort β _ x := rfl /-- Many structures such as bundled morphisms coerce to functions so that you can transparently apply them to arguments. For example, if `e : α ≃ β` and `a : α` then you can write `e a` and this is elaborated as `⇑e a`. This type of coercion is implemented using the `has_coe_to_fun`type class. There is one important consideration: If a type coerces to another type which in turn coerces to a function, then it **must** implement `has_coe_to_fun` directly: ```lean structure sparkling_equiv (α β) extends α ≃ β -- if we add a `has_coe` instance, instance {α β} : has_coe (sparkling_equiv α β) (α ≃ β) := ⟨sparkling_equiv.to_equiv⟩ -- then a `has_coe_to_fun` instance **must** be added as well: instance {α β} : has_coe_to_fun (sparkling_equiv α β) := ⟨λ _, α → β, λ f, f.to_equiv.to_fun⟩ ``` (Rationale: if we do not declare the direct coercion, then `⇑e a` is not in simp-normal form. The lemma `coe_fn_coe_base` will unfold it to `⇑↑e a`. This often causes loops in the simplifier.) -/ library_note "function coercion" @[simp] theorem coe_sort_coe_base {α β} [has_coe α β] [has_coe_to_sort β] (x : α) : @coe_sort α _ x = @coe_sort β _ x := rfl /-- `pempty` is the universe-polymorphic analogue of `empty`. -/ @[derive decidable_eq] inductive {u} pempty : Sort u /-- Ex falso, the nondependent eliminator for the `pempty` type. -/ def pempty.elim {C : Sort*} : pempty → C. instance subsingleton_pempty : subsingleton pempty := ⟨λa, a.elim⟩ @[simp] lemma not_nonempty_pempty : ¬ nonempty pempty := assume ⟨h⟩, h.elim @[simp] theorem forall_pempty {P : pempty → Prop} : (∀ x : pempty, P x) ↔ true := ⟨λ h, trivial, λ h x, by cases x⟩ @[simp] theorem exists_pempty {P : pempty → Prop} : (∃ x : pempty, P x) ↔ false := ⟨λ h, by { cases h with w, cases w }, false.elim⟩ lemma congr_arg_heq {α} {β : α → Sort*} (f : ∀ a, β a) : ∀ {a₁ a₂ : α}, a₁ = a₂ → f a₁ == f a₂ | a _ rfl := heq.rfl lemma plift.down_inj {α : Sort*} : ∀ (a b : plift α), a.down = b.down → a = b | ⟨a⟩ ⟨b⟩ rfl := rfl -- missing [symm] attribute for ne in core. attribute [symm] ne.symm lemma ne_comm {α} {a b : α} : a ≠ b ↔ b ≠ a := ⟨ne.symm, ne.symm⟩ @[simp] lemma eq_iff_eq_cancel_left {b c : α} : (∀ {a}, a = b ↔ a = c) ↔ (b = c) := ⟨λ h, by rw [← h], λ h a, by rw h⟩ @[simp] lemma eq_iff_eq_cancel_right {a b : α} : (∀ {c}, a = c ↔ b = c) ↔ (a = b) := ⟨λ h, by rw h, λ h a, by rw h⟩ /-- Wrapper for adding elementary propositions to the type class systems. Warning: this can easily be abused. See the rest of this docstring for details. Certain propositions should not be treated as a class globally, but sometimes it is very convenient to be able to use the type class system in specific circumstances. For example, `zmod p` is a field if and only if `p` is a prime number. In order to be able to find this field instance automatically by type class search, we have to turn `p.prime` into an instance implicit assumption. On the other hand, making `nat.prime` a class would require a major refactoring of the library, and it is questionable whether making `nat.prime` a class is desirable at all. The compromise is to add the assumption `[fact p.prime]` to `zmod.field`. In particular, this class is not intended for turning the type class system into an automated theorem prover for first order logic. -/ @[class] def fact (p : Prop) := p lemma fact.elim {p : Prop} (h : fact p) : p := h end miscellany /-! ### Declarations about propositional connectives -/ theorem false_ne_true : false ≠ true | h := h.symm ▸ trivial section propositional variables {a b c d : Prop} /-! ### Declarations about `implies` -/ theorem iff_of_eq (e : a = b) : a ↔ b := e ▸ iff.rfl theorem iff_iff_eq : (a ↔ b) ↔ a = b := ⟨propext, iff_of_eq⟩ @[simp] lemma eq_iff_iff {p q : Prop} : (p = q) ↔ (p ↔ q) := iff_iff_eq.symm @[simp] theorem imp_self : (a → a) ↔ true := iff_true_intro id theorem imp_intro {α β : Prop} (h : α) : β → α := λ _, h theorem imp_false : (a → false) ↔ ¬ a := iff.rfl theorem imp_and_distrib {α} : (α → b ∧ c) ↔ (α → b) ∧ (α → c) := ⟨λ h, ⟨λ ha, (h ha).left, λ ha, (h ha).right⟩, λ h ha, ⟨h.left ha, h.right ha⟩⟩ @[simp] theorem and_imp : (a ∧ b → c) ↔ (a → b → c) := iff.intro (λ h ha hb, h ⟨ha, hb⟩) (λ h ⟨ha, hb⟩, h ha hb) theorem iff_def : (a ↔ b) ↔ (a → b) ∧ (b → a) := iff_iff_implies_and_implies _ _ theorem iff_def' : (a ↔ b) ↔ (b → a) ∧ (a → b) := iff_def.trans and.comm theorem imp_true_iff {α : Sort*} : (α → true) ↔ true := iff_true_intro $ λ_, trivial @[simp] theorem imp_iff_right (ha : a) : (a → b) ↔ b := ⟨λf, f ha, imp_intro⟩ /-! ### Declarations about `not` -/ /-- Ex falso for negation. From `¬ a` and `a` anything follows. This is the same as `absurd` with the arguments flipped, but it is in the `not` namespace so that projection notation can be used. -/ def not.elim {α : Sort*} (H1 : ¬a) (H2 : a) : α := absurd H2 H1 @[reducible] theorem not.imp {a b : Prop} (H2 : ¬b) (H1 : a → b) : ¬a := mt H1 H2 theorem not_not_of_not_imp : ¬(a → b) → ¬¬a := mt not.elim theorem not_of_not_imp {a : Prop} : ¬(a → b) → ¬b := mt imp_intro theorem dec_em (p : Prop) [decidable p] : p ∨ ¬p := decidable.em p theorem em (p : Prop) : p ∨ ¬ p := classical.em _ theorem or_not {p : Prop} : p ∨ ¬ p := em _ theorem by_contradiction {p} : (¬p → false) → p := decidable.by_contradiction -- alias by_contradiction ← by_contra theorem by_contra {p} : (¬p → false) → p := decidable.by_contradiction /-- In most of mathlib, we use the law of excluded middle (LEM) and the axiom of choice (AC) freely. The `decidable` namespace contains versions of lemmas from the root namespace that explicitly attempt to avoid the axiom of choice, usually by adding decidability assumptions on the inputs. You can check if a lemma uses the axiom of choice by using `#print axioms foo` and seeing if `classical.choice` appears in the list. -/ library_note "decidable namespace" -- See Note [decidable namespace] protected theorem decidable.not_not [decidable a] : ¬¬a ↔ a := iff.intro decidable.by_contradiction not_not_intro /-- The Double Negation Theorem: `¬ ¬ P` is equivalent to `P`. The left-to-right direction, double negation elimination (DNE), is classically true but not constructively. -/ @[simp] theorem not_not : ¬¬a ↔ a := decidable.not_not theorem of_not_not : ¬¬a → a := by_contra -- See Note [decidable namespace] protected theorem decidable.of_not_imp [decidable a] (h : ¬ (a → b)) : a := decidable.by_contradiction (not_not_of_not_imp h) theorem of_not_imp : ¬ (a → b) → a := decidable.of_not_imp -- See Note [decidable namespace] protected theorem decidable.not_imp_symm [decidable a] (h : ¬a → b) (hb : ¬b) : a := decidable.by_contradiction $ hb ∘ h theorem not.decidable_imp_symm [decidable a] : (¬a → b) → ¬b → a := decidable.not_imp_symm theorem not.imp_symm : (¬a → b) → ¬b → a := not.decidable_imp_symm -- See Note [decidable namespace] protected theorem decidable.not_imp_comm [decidable a] [decidable b] : (¬a → b) ↔ (¬b → a) := ⟨not.decidable_imp_symm, not.decidable_imp_symm⟩ theorem not_imp_comm : (¬a → b) ↔ (¬b → a) := decidable.not_imp_comm @[simp] theorem imp_not_self : (a → ¬a) ↔ ¬a := ⟨λ h ha, h ha ha, λ h _, h⟩ theorem decidable.not_imp_self [decidable a] : (¬a → a) ↔ a := by { have := @imp_not_self (¬a), rwa decidable.not_not at this } @[simp] theorem not_imp_self : (¬a → a) ↔ a := decidable.not_imp_self theorem imp.swap : (a → b → c) ↔ (b → a → c) := ⟨function.swap, function.swap⟩ theorem imp_not_comm : (a → ¬b) ↔ (b → ¬a) := imp.swap /-! ### Declarations about `and` -/ theorem not_and_of_not_left (b : Prop) : ¬a → ¬(a ∧ b) := mt and.left theorem not_and_of_not_right (a : Prop) {b : Prop} : ¬b → ¬(a ∧ b) := mt and.right theorem and.imp_left (h : a → b) : a ∧ c → b ∧ c := and.imp h id theorem and.imp_right (h : a → b) : c ∧ a → c ∧ b := and.imp id h lemma and.right_comm : (a ∧ b) ∧ c ↔ (a ∧ c) ∧ b := by simp only [and.left_comm, and.comm] lemma and.rotate : a ∧ b ∧ c ↔ b ∧ c ∧ a := by simp only [and.left_comm, and.comm] theorem and_not_self_iff (a : Prop) : a ∧ ¬ a ↔ false := iff.intro (assume h, (h.right) (h.left)) (assume h, h.elim) theorem not_and_self_iff (a : Prop) : ¬ a ∧ a ↔ false := iff.intro (assume ⟨hna, ha⟩, hna ha) false.elim theorem and_iff_left_of_imp {a b : Prop} (h : a → b) : (a ∧ b) ↔ a := iff.intro and.left (λ ha, ⟨ha, h ha⟩) theorem and_iff_right_of_imp {a b : Prop} (h : b → a) : (a ∧ b) ↔ b := iff.intro and.right (λ hb, ⟨h hb, hb⟩) @[simp] theorem and_iff_left_iff_imp {a b : Prop} : ((a ∧ b) ↔ a) ↔ (a → b) := ⟨λ h ha, (h.2 ha).2, and_iff_left_of_imp⟩ @[simp] theorem and_iff_right_iff_imp {a b : Prop} : ((a ∧ b) ↔ b) ↔ (b → a) := ⟨λ h ha, (h.2 ha).1, and_iff_right_of_imp⟩ @[simp] lemma and.congr_right_iff : (a ∧ b ↔ a ∧ c) ↔ (a → (b ↔ c)) := ⟨λ h ha, by simp [ha] at h; exact h, and_congr_right⟩ @[simp] lemma and.congr_left_iff : (a ∧ c ↔ b ∧ c) ↔ c → (a ↔ b) := by simp only [and.comm, ← and.congr_right_iff] @[simp] lemma and_self_left : a ∧ a ∧ b ↔ a ∧ b := ⟨λ h, ⟨h.1, h.2.2⟩, λ h, ⟨h.1, h.1, h.2⟩⟩ @[simp] lemma and_self_right : (a ∧ b) ∧ b ↔ a ∧ b := ⟨λ h, ⟨h.1.1, h.2⟩, λ h, ⟨⟨h.1, h.2⟩, h.2⟩⟩ /-! ### Declarations about `or` -/ theorem or.right_comm : (a ∨ b) ∨ c ↔ (a ∨ c) ∨ b := by rw [or_assoc, or_assoc, or_comm b] theorem or_of_or_of_imp_of_imp (h₁ : a ∨ b) (h₂ : a → c) (h₃ : b → d) : c ∨ d := or.imp h₂ h₃ h₁ theorem or_of_or_of_imp_left (h₁ : a ∨ c) (h : a → b) : b ∨ c := or.imp_left h h₁ theorem or_of_or_of_imp_right (h₁ : c ∨ a) (h : a → b) : c ∨ b := or.imp_right h h₁ theorem or.elim3 (h : a ∨ b ∨ c) (ha : a → d) (hb : b → d) (hc : c → d) : d := or.elim h ha (assume h₂, or.elim h₂ hb hc) theorem or_imp_distrib : (a ∨ b → c) ↔ (a → c) ∧ (b → c) := ⟨assume h, ⟨assume ha, h (or.inl ha), assume hb, h (or.inr hb)⟩, assume ⟨ha, hb⟩, or.rec ha hb⟩ -- See Note [decidable namespace] protected theorem decidable.or_iff_not_imp_left [decidable a] : a ∨ b ↔ (¬ a → b) := ⟨or.resolve_left, λ h, dite _ or.inl (or.inr ∘ h)⟩ theorem or_iff_not_imp_left : a ∨ b ↔ (¬ a → b) := decidable.or_iff_not_imp_left -- See Note [decidable namespace] protected theorem decidable.or_iff_not_imp_right [decidable b] : a ∨ b ↔ (¬ b → a) := or.comm.trans decidable.or_iff_not_imp_left theorem or_iff_not_imp_right : a ∨ b ↔ (¬ b → a) := decidable.or_iff_not_imp_right -- See Note [decidable namespace] protected theorem decidable.not_imp_not [decidable a] : (¬ a → ¬ b) ↔ (b → a) := ⟨assume h hb, decidable.by_contradiction $ assume na, h na hb, mt⟩ theorem not_imp_not : (¬ a → ¬ b) ↔ (b → a) := decidable.not_imp_not /-! ### Declarations about distributivity -/ /-- `∧` distributes over `∨` (on the left). -/ theorem and_or_distrib_left : a ∧ (b ∨ c) ↔ (a ∧ b) ∨ (a ∧ c) := ⟨λ ⟨ha, hbc⟩, hbc.imp (and.intro ha) (and.intro ha), or.rec (and.imp_right or.inl) (and.imp_right or.inr)⟩ /-- `∧` distributes over `∨` (on the right). -/ theorem or_and_distrib_right : (a ∨ b) ∧ c ↔ (a ∧ c) ∨ (b ∧ c) := (and.comm.trans and_or_distrib_left).trans (or_congr and.comm and.comm) /-- `∨` distributes over `∧` (on the left). -/ theorem or_and_distrib_left : a ∨ (b ∧ c) ↔ (a ∨ b) ∧ (a ∨ c) := ⟨or.rec (λha, and.intro (or.inl ha) (or.inl ha)) (and.imp or.inr or.inr), and.rec $ or.rec (imp_intro ∘ or.inl) (or.imp_right ∘ and.intro)⟩ /-- `∨` distributes over `∧` (on the right). -/ theorem and_or_distrib_right : (a ∧ b) ∨ c ↔ (a ∨ c) ∧ (b ∨ c) := (or.comm.trans or_and_distrib_left).trans (and_congr or.comm or.comm) @[simp] lemma or_self_left : a ∨ a ∨ b ↔ a ∨ b := ⟨λ h, h.elim or.inl id, λ h, h.elim or.inl (or.inr ∘ or.inr)⟩ @[simp] lemma or_self_right : (a ∨ b) ∨ b ↔ a ∨ b := ⟨λ h, h.elim id or.inr, λ h, h.elim (or.inl ∘ or.inl) or.inr⟩ /-! Declarations about `iff` -/ theorem iff_of_true (ha : a) (hb : b) : a ↔ b := ⟨λ_, hb, λ _, ha⟩ theorem iff_of_false (ha : ¬a) (hb : ¬b) : a ↔ b := ⟨ha.elim, hb.elim⟩ theorem iff_true_left (ha : a) : (a ↔ b) ↔ b := ⟨λ h, h.1 ha, iff_of_true ha⟩ theorem iff_true_right (ha : a) : (b ↔ a) ↔ b := iff.comm.trans (iff_true_left ha) theorem iff_false_left (ha : ¬a) : (a ↔ b) ↔ ¬b := ⟨λ h, mt h.2 ha, iff_of_false ha⟩ theorem iff_false_right (ha : ¬a) : (b ↔ a) ↔ ¬b := iff.comm.trans (iff_false_left ha) -- See Note [decidable namespace] protected theorem decidable.not_or_of_imp [decidable a] (h : a → b) : ¬ a ∨ b := if ha : a then or.inr (h ha) else or.inl ha theorem not_or_of_imp : (a → b) → ¬ a ∨ b := decidable.not_or_of_imp -- See Note [decidable namespace] protected theorem decidable.imp_iff_not_or [decidable a] : (a → b) ↔ (¬ a ∨ b) := ⟨decidable.not_or_of_imp, or.neg_resolve_left⟩ theorem imp_iff_not_or : (a → b) ↔ (¬ a ∨ b) := decidable.imp_iff_not_or -- See Note [decidable namespace] protected theorem decidable.imp_or_distrib [decidable a] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := by simp [decidable.imp_iff_not_or, or.comm, or.left_comm] theorem imp_or_distrib : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := decidable.imp_or_distrib -- See Note [decidable namespace] protected theorem decidable.imp_or_distrib' [decidable b] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := by by_cases b; simp [h, or_iff_right_of_imp ((∘) false.elim)] theorem imp_or_distrib' : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := decidable.imp_or_distrib' theorem not_imp_of_and_not : a ∧ ¬ b → ¬ (a → b) | ⟨ha, hb⟩ h := hb $ h ha -- See Note [decidable namespace] protected theorem decidable.not_imp [decidable a] : ¬(a → b) ↔ a ∧ ¬b := ⟨λ h, ⟨decidable.of_not_imp h, not_of_not_imp h⟩, not_imp_of_and_not⟩ theorem not_imp : ¬(a → b) ↔ a ∧ ¬b := decidable.not_imp -- for monotonicity lemma imp_imp_imp (h₀ : c → a) (h₁ : b → d) : (a → b) → (c → d) := assume (h₂ : a → b), h₁ ∘ h₂ ∘ h₀ -- See Note [decidable namespace] protected theorem decidable.peirce (a b : Prop) [decidable a] : ((a → b) → a) → a := if ha : a then λ h, ha else λ h, h ha.elim theorem peirce (a b : Prop) : ((a → b) → a) → a := decidable.peirce _ _ theorem peirce' {a : Prop} (H : ∀ b : Prop, (a → b) → a) : a := H _ id -- See Note [decidable namespace] protected theorem decidable.not_iff_not [decidable a] [decidable b] : (¬ a ↔ ¬ b) ↔ (a ↔ b) := by rw [@iff_def (¬ a), @iff_def' a]; exact and_congr decidable.not_imp_not decidable.not_imp_not theorem not_iff_not : (¬ a ↔ ¬ b) ↔ (a ↔ b) := decidable.not_iff_not -- See Note [decidable namespace] protected theorem decidable.not_iff_comm [decidable a] [decidable b] : (¬ a ↔ b) ↔ (¬ b ↔ a) := by rw [@iff_def (¬ a), @iff_def (¬ b)]; exact and_congr decidable.not_imp_comm imp_not_comm theorem not_iff_comm : (¬ a ↔ b) ↔ (¬ b ↔ a) := decidable.not_iff_comm -- See Note [decidable namespace] protected theorem decidable.not_iff : ∀ [decidable b], ¬ (a ↔ b) ↔ (¬ a ↔ b) := by intro h; cases h; simp only [h, iff_true, iff_false] theorem not_iff : ¬ (a ↔ b) ↔ (¬ a ↔ b) := decidable.not_iff -- See Note [decidable namespace] protected theorem decidable.iff_not_comm [decidable a] [decidable b] : (a ↔ ¬ b) ↔ (b ↔ ¬ a) := by rw [@iff_def a, @iff_def b]; exact and_congr imp_not_comm decidable.not_imp_comm theorem iff_not_comm : (a ↔ ¬ b) ↔ (b ↔ ¬ a) := decidable.iff_not_comm -- See Note [decidable namespace] protected theorem decidable.iff_iff_and_or_not_and_not [decidable b] : (a ↔ b) ↔ (a ∧ b) ∨ (¬ a ∧ ¬ b) := by { split; intro h, { rw h; by_cases b; [left,right]; split; assumption }, { cases h with h h; cases h; split; intro; { contradiction <|> assumption } } } theorem iff_iff_and_or_not_and_not : (a ↔ b) ↔ (a ∧ b) ∨ (¬ a ∧ ¬ b) := decidable.iff_iff_and_or_not_and_not lemma decidable.iff_iff_not_or_and_or_not [decidable a] [decidable b] : (a ↔ b) ↔ ((¬a ∨ b) ∧ (a ∨ ¬b)) := begin rw [iff_iff_implies_and_implies a b], simp only [decidable.imp_iff_not_or, or.comm] end lemma iff_iff_not_or_and_or_not : (a ↔ b) ↔ ((¬a ∨ b) ∧ (a ∨ ¬b)) := decidable.iff_iff_not_or_and_or_not -- See Note [decidable namespace] protected theorem decidable.not_and_not_right [decidable b] : ¬(a ∧ ¬b) ↔ (a → b) := ⟨λ h ha, h.decidable_imp_symm $ and.intro ha, λ h ⟨ha, hb⟩, hb $ h ha⟩ theorem not_and_not_right : ¬(a ∧ ¬b) ↔ (a → b) := decidable.not_and_not_right /-- Transfer decidability of `a` to decidability of `b`, if the propositions are equivalent. **Important**: this function should be used instead of `rw` on `decidable b`, because the kernel will get stuck reducing the usage of `propext` otherwise, and `dec_trivial` will not work. -/ @[inline] def decidable_of_iff (a : Prop) (h : a ↔ b) [D : decidable a] : decidable b := decidable_of_decidable_of_iff D h /-- Transfer decidability of `b` to decidability of `a`, if the propositions are equivalent. This is the same as `decidable_of_iff` but the iff is flipped. -/ @[inline] def decidable_of_iff' (b : Prop) (h : a ↔ b) [D : decidable b] : decidable a := decidable_of_decidable_of_iff D h.symm /-- Prove that `a` is decidable by constructing a boolean `b` and a proof that `b ↔ a`. (This is sometimes taken as an alternate definition of decidability.) -/ def decidable_of_bool : ∀ (b : bool) (h : b ↔ a), decidable a | tt h := is_true (h.1 rfl) | ff h := is_false (mt h.2 bool.ff_ne_tt) /-! ### De Morgan's laws -/ theorem not_and_of_not_or_not (h : ¬ a ∨ ¬ b) : ¬ (a ∧ b) | ⟨ha, hb⟩ := or.elim h (absurd ha) (absurd hb) -- See Note [decidable namespace] protected theorem decidable.not_and_distrib [decidable a] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := ⟨λ h, if ha : a then or.inr (λ hb, h ⟨ha, hb⟩) else or.inl ha, not_and_of_not_or_not⟩ -- See Note [decidable namespace] protected theorem decidable.not_and_distrib' [decidable b] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := ⟨λ h, if hb : b then or.inl (λ ha, h ⟨ha, hb⟩) else or.inr hb, not_and_of_not_or_not⟩ /-- One of de Morgan's laws: the negation of a conjunction is logically equivalent to the disjunction of the negations. -/ theorem not_and_distrib : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := decidable.not_and_distrib @[simp] theorem not_and : ¬ (a ∧ b) ↔ (a → ¬ b) := and_imp theorem not_and' : ¬ (a ∧ b) ↔ b → ¬a := not_and.trans imp_not_comm /-- One of de Morgan's laws: the negation of a disjunction is logically equivalent to the conjunction of the negations. -/ theorem not_or_distrib : ¬ (a ∨ b) ↔ ¬ a ∧ ¬ b := ⟨λ h, ⟨λ ha, h (or.inl ha), λ hb, h (or.inr hb)⟩, λ ⟨h₁, h₂⟩ h, or.elim h h₁ h₂⟩ -- See Note [decidable namespace] protected theorem decidable.or_iff_not_and_not [decidable a] [decidable b] : a ∨ b ↔ ¬ (¬a ∧ ¬b) := by rw [← not_or_distrib, decidable.not_not] theorem or_iff_not_and_not : a ∨ b ↔ ¬ (¬a ∧ ¬b) := decidable.or_iff_not_and_not -- See Note [decidable namespace] protected theorem decidable.and_iff_not_or_not [decidable a] [decidable b] : a ∧ b ↔ ¬ (¬ a ∨ ¬ b) := by rw [← decidable.not_and_distrib, decidable.not_not] theorem and_iff_not_or_not : a ∧ b ↔ ¬ (¬ a ∨ ¬ b) := decidable.and_iff_not_or_not end propositional /-! ### Declarations about equality -/ section equality variables {α : Sort*} {a b : α} @[simp] theorem heq_iff_eq : a == b ↔ a = b := ⟨eq_of_heq, heq_of_eq⟩ theorem proof_irrel_heq {p q : Prop} (hp : p) (hq : q) : hp == hq := have p = q, from propext ⟨λ _, hq, λ _, hp⟩, by subst q; refl theorem ne_of_mem_of_not_mem {α β} [has_mem α β] {s : β} {a b : α} (h : a ∈ s) : b ∉ s → a ≠ b := mt $ λ e, e ▸ h theorem eq_equivalence : equivalence (@eq α) := ⟨eq.refl, @eq.symm _, @eq.trans _⟩ /-- Transport through trivial families is the identity. -/ @[simp] lemma eq_rec_constant {α : Sort*} {a a' : α} {β : Sort*} (y : β) (h : a = a') : (@eq.rec α a (λ a, β) y a' h) = y := by { cases h, refl, } @[simp] lemma eq_mp_rfl {α : Sort*} {a : α} : eq.mp (eq.refl α) a = a := rfl @[simp] lemma eq_mpr_rfl {α : Sort*} {a : α} : eq.mpr (eq.refl α) a = a := rfl lemma heq_of_eq_mp : ∀ {α β : Sort*} {a : α} {a' : β} (e : α = β) (h₂ : (eq.mp e a) = a'), a == a' | α ._ a a' rfl h := eq.rec_on h (heq.refl _) lemma rec_heq_of_heq {β} {C : α → Sort*} {x : C a} {y : β} (eq : a = b) (h : x == y) : @eq.rec α a C x b eq == y := by subst eq; exact h @[simp] lemma {u} eq_mpr_heq {α β : Sort u} (h : β = α) (x : α) : eq.mpr h x == x := by subst h; refl protected lemma eq.congr {x₁ x₂ y₁ y₂ : α} (h₁ : x₁ = y₁) (h₂ : x₂ = y₂) : (x₁ = x₂) ↔ (y₁ = y₂) := by { subst h₁, subst h₂ } lemma eq.congr_left {x y z : α} (h : x = y) : x = z ↔ y = z := by rw [h] lemma eq.congr_right {x y z : α} (h : x = y) : z = x ↔ z = y := by rw [h] lemma congr_arg2 {α β γ : Type*} (f : α → β → γ) {x x' : α} {y y' : β} (hx : x = x') (hy : y = y') : f x y = f x' y' := by { subst hx, subst hy } end equality /-! ### Declarations about quantifiers -/ section quantifiers variables {α : Sort*} {β : Sort*} {p q : α → Prop} {b : Prop} lemma forall_imp (h : ∀ a, p a → q a) : (∀ a, p a) → ∀ a, q a := λ h' a, h a (h' a) lemma forall₂_congr {p q : α → β → Prop} (h : ∀ a b, p a b ↔ q a b) : (∀ a b, p a b) ↔ (∀ a b, q a b) := forall_congr (λ a, forall_congr (h a)) lemma forall₃_congr {γ : Sort*} {p q : α → β → γ → Prop} (h : ∀ a b c, p a b c ↔ q a b c) : (∀ a b c, p a b c) ↔ (∀ a b c, q a b c) := forall_congr (λ a, forall₂_congr (h a)) lemma forall₄_congr {γ δ : Sort*} {p q : α → β → γ → δ → Prop} (h : ∀ a b c d, p a b c d ↔ q a b c d) : (∀ a b c d, p a b c d) ↔ (∀ a b c d, q a b c d) := forall_congr (λ a, forall₃_congr (h a)) lemma Exists.imp (h : ∀ a, (p a → q a)) (p : ∃ a, p a) : ∃ a, q a := exists_imp_exists h p lemma exists_imp_exists' {p : α → Prop} {q : β → Prop} (f : α → β) (hpq : ∀ a, p a → q (f a)) (hp : ∃ a, p a) : ∃ b, q b := exists.elim hp (λ a hp', ⟨_, hpq _ hp'⟩) lemma exists₂_congr {p q : α → β → Prop} (h : ∀ a b, p a b ↔ q a b) : (∃ a b, p a b) ↔ (∃ a b, q a b) := exists_congr (λ a, exists_congr (h a)) lemma exists₃_congr {γ : Sort*} {p q : α → β → γ → Prop} (h : ∀ a b c, p a b c ↔ q a b c) : (∃ a b c, p a b c) ↔ (∃ a b c, q a b c) := exists_congr (λ a, exists₂_congr (h a)) lemma exists₄_congr {γ δ : Sort*} {p q : α → β → γ → δ → Prop} (h : ∀ a b c d, p a b c d ↔ q a b c d) : (∃ a b c d, p a b c d) ↔ (∃ a b c d, q a b c d) := exists_congr (λ a, exists₃_congr (h a)) theorem forall_swap {p : α → β → Prop} : (∀ x y, p x y) ↔ ∀ y x, p x y := ⟨function.swap, function.swap⟩ theorem exists_swap {p : α → β → Prop} : (∃ x y, p x y) ↔ ∃ y x, p x y := ⟨λ ⟨x, y, h⟩, ⟨y, x, h⟩, λ ⟨y, x, h⟩, ⟨x, y, h⟩⟩ @[simp] theorem exists_imp_distrib : ((∃ x, p x) → b) ↔ ∀ x, p x → b := ⟨λ h x hpx, h ⟨x, hpx⟩, λ h ⟨x, hpx⟩, h x hpx⟩ /-- Extract an element from a existential statement, using `classical.some`. -/ -- This enables projection notation. @[reducible] noncomputable def Exists.some {p : α → Prop} (P : ∃ a, p a) : α := classical.some P /-- Show that an element extracted from `P : ∃ a, p a` using `P.some` satisfies `p`. -/ lemma Exists.some_spec {p : α → Prop} (P : ∃ a, p a) : p (P.some) := classical.some_spec P --theorem forall_not_of_not_exists (h : ¬ ∃ x, p x) : ∀ x, ¬ p x := --forall_imp_of_exists_imp h theorem not_exists_of_forall_not (h : ∀ x, ¬ p x) : ¬ ∃ x, p x := exists_imp_distrib.2 h @[simp] theorem not_exists : (¬ ∃ x, p x) ↔ ∀ x, ¬ p x := exists_imp_distrib theorem not_forall_of_exists_not : (∃ x, ¬ p x) → ¬ ∀ x, p x | ⟨x, hn⟩ h := hn (h x) -- See Note [decidable namespace] protected theorem decidable.not_forall {p : α → Prop} [decidable (∃ x, ¬ p x)] [∀ x, decidable (p x)] : (¬ ∀ x, p x) ↔ ∃ x, ¬ p x := ⟨not.decidable_imp_symm $ λ nx x, nx.decidable_imp_symm $ λ h, ⟨x, h⟩, not_forall_of_exists_not⟩ @[simp] theorem not_forall {p : α → Prop} : (¬ ∀ x, p x) ↔ ∃ x, ¬ p x := decidable.not_forall -- See Note [decidable namespace] protected theorem decidable.not_forall_not [decidable (∃ x, p x)] : (¬ ∀ x, ¬ p x) ↔ ∃ x, p x := (@decidable.not_iff_comm _ _ _ (decidable_of_iff (¬ ∃ x, p x) not_exists)).1 not_exists theorem not_forall_not : (¬ ∀ x, ¬ p x) ↔ ∃ x, p x := decidable.not_forall_not -- See Note [decidable namespace] protected theorem decidable.not_exists_not [∀ x, decidable (p x)] : (¬ ∃ x, ¬ p x) ↔ ∀ x, p x := by simp [decidable.not_not] @[simp] theorem not_exists_not : (¬ ∃ x, ¬ p x) ↔ ∀ x, p x := decidable.not_exists_not @[simp] theorem forall_true_iff : (α → true) ↔ true := iff_true_intro (λ _, trivial) -- Unfortunately this causes simp to loop sometimes, so we -- add the 2 and 3 cases as simp lemmas instead theorem forall_true_iff' (h : ∀ a, p a ↔ true) : (∀ a, p a) ↔ true := iff_true_intro (λ _, of_iff_true (h _)) @[simp] theorem forall_2_true_iff {β : α → Sort*} : (∀ a, β a → true) ↔ true := forall_true_iff' $ λ _, forall_true_iff @[simp] theorem forall_3_true_iff {β : α → Sort*} {γ : Π a, β a → Sort*} : (∀ a (b : β a), γ a b → true) ↔ true := forall_true_iff' $ λ _, forall_2_true_iff @[simp] theorem forall_const (α : Sort*) [i : nonempty α] : (α → b) ↔ b := ⟨i.elim, λ hb x, hb⟩ @[simp] theorem exists_const (α : Sort*) [i : nonempty α] : (∃ x : α, b) ↔ b := ⟨λ ⟨x, h⟩, h, i.elim exists.intro⟩ theorem forall_and_distrib : (∀ x, p x ∧ q x) ↔ (∀ x, p x) ∧ (∀ x, q x) := ⟨λ h, ⟨λ x, (h x).left, λ x, (h x).right⟩, λ ⟨h₁, h₂⟩ x, ⟨h₁ x, h₂ x⟩⟩ theorem exists_or_distrib : (∃ x, p x ∨ q x) ↔ (∃ x, p x) ∨ (∃ x, q x) := ⟨λ ⟨x, hpq⟩, hpq.elim (λ hpx, or.inl ⟨x, hpx⟩) (λ hqx, or.inr ⟨x, hqx⟩), λ hepq, hepq.elim (λ ⟨x, hpx⟩, ⟨x, or.inl hpx⟩) (λ ⟨x, hqx⟩, ⟨x, or.inr hqx⟩)⟩ @[simp] theorem exists_and_distrib_left {q : Prop} {p : α → Prop} : (∃x, q ∧ p x) ↔ q ∧ (∃x, p x) := ⟨λ ⟨x, hq, hp⟩, ⟨hq, x, hp⟩, λ ⟨hq, x, hp⟩, ⟨x, hq, hp⟩⟩ @[simp] theorem exists_and_distrib_right {q : Prop} {p : α → Prop} : (∃x, p x ∧ q) ↔ (∃x, p x) ∧ q := by simp [and_comm] @[simp] theorem forall_eq {a' : α} : (∀a, a = a' → p a) ↔ p a' := ⟨λ h, h a' rfl, λ h a e, e.symm ▸ h⟩ @[simp] theorem forall_eq' {a' : α} : (∀a, a' = a → p a) ↔ p a' := by simp [@eq_comm _ a'] -- this lemma is needed to simplify the output of `list.mem_cons_iff` @[simp] theorem forall_eq_or_imp {a' : α} : (∀ a, a = a' ∨ q a → p a) ↔ p a' ∧ ∀ a, q a → p a := by simp only [or_imp_distrib, forall_and_distrib, forall_eq] @[simp] theorem exists_eq {a' : α} : ∃ a, a = a' := ⟨_, rfl⟩ @[simp] theorem exists_eq' {a' : α} : ∃ a, a' = a := ⟨_, rfl⟩ @[simp] theorem exists_eq_left {a' : α} : (∃ a, a = a' ∧ p a) ↔ p a' := ⟨λ ⟨a, e, h⟩, e ▸ h, λ h, ⟨_, rfl, h⟩⟩ @[simp] theorem exists_eq_right {a' : α} : (∃ a, p a ∧ a = a') ↔ p a' := (exists_congr $ by exact λ a, and.comm).trans exists_eq_left @[simp] theorem exists_apply_eq_apply {α β : Type*} (f : α → β) (a' : α) : ∃ a, f a = f a' := ⟨a', rfl⟩ @[simp] theorem exists_apply_eq_apply' {α β : Type*} (f : α → β) (a' : α) : ∃ a, f a' = f a := ⟨a', rfl⟩ @[simp] theorem exists_exists_and_eq_and {f : α → β} {p : α → Prop} {q : β → Prop} : (∃ b, (∃ a, p a ∧ f a = b) ∧ q b) ↔ ∃ a, p a ∧ q (f a) := ⟨λ ⟨b, ⟨a, ha, hab⟩, hb⟩, ⟨a, ha, hab.symm ▸ hb⟩, λ ⟨a, hp, hq⟩, ⟨f a, ⟨a, hp, rfl⟩, hq⟩⟩ @[simp] theorem exists_exists_eq_and {f : α → β} {p : β → Prop} : (∃ b, (∃ a, f a = b) ∧ p b) ↔ ∃ a, p (f a) := ⟨λ ⟨b, ⟨a, ha⟩, hb⟩, ⟨a, ha.symm ▸ hb⟩, λ ⟨a, ha⟩, ⟨f a, ⟨a, rfl⟩, ha⟩⟩ @[simp] theorem forall_apply_eq_imp_iff {f : α → β} {p : β → Prop} : (∀ a, ∀ b, f a = b → p b) ↔ (∀ a, p (f a)) := ⟨λ h a, h a (f a) rfl, λ h a b hab, hab ▸ h a⟩ @[simp] theorem forall_apply_eq_imp_iff' {f : α → β} {p : β → Prop} : (∀ b, ∀ a, f a = b → p b) ↔ (∀ a, p (f a)) := by { rw forall_swap, simp } @[simp] theorem forall_eq_apply_imp_iff {f : α → β} {p : β → Prop} : (∀ a, ∀ b, b = f a → p b) ↔ (∀ a, p (f a)) := by simp [@eq_comm _ _ (f _)] @[simp] theorem forall_eq_apply_imp_iff' {f : α → β} {p : β → Prop} : (∀ b, ∀ a, b = f a → p b) ↔ (∀ a, p (f a)) := by { rw forall_swap, simp } @[simp] theorem forall_apply_eq_imp_iff₂ {f : α → β} {p : α → Prop} {q : β → Prop} : (∀ b, ∀ a, p a → f a = b → q b) ↔ ∀ a, p a → q (f a) := ⟨λ h a ha, h (f a) a ha rfl, λ h b a ha hb, hb ▸ h a ha⟩ @[simp] theorem exists_eq_left' {a' : α} : (∃ a, a' = a ∧ p a) ↔ p a' := by simp [@eq_comm _ a'] @[simp] theorem exists_eq_right' {a' : α} : (∃ a, p a ∧ a' = a) ↔ p a' := by simp [@eq_comm _ a'] theorem exists_comm {p : α → β → Prop} : (∃ a b, p a b) ↔ ∃ b a, p a b := ⟨λ ⟨a, b, h⟩, ⟨b, a, h⟩, λ ⟨b, a, h⟩, ⟨a, b, h⟩⟩ theorem forall_or_of_or_forall (h : b ∨ ∀x, p x) (x) : b ∨ p x := h.imp_right $ λ h₂, h₂ x -- See Note [decidable namespace] protected theorem decidable.forall_or_distrib_left {q : Prop} {p : α → Prop} [decidable q] : (∀x, q ∨ p x) ↔ q ∨ (∀x, p x) := ⟨λ h, if hq : q then or.inl hq else or.inr $ λ x, (h x).resolve_left hq, forall_or_of_or_forall⟩ theorem forall_or_distrib_left {q : Prop} {p : α → Prop} : (∀x, q ∨ p x) ↔ q ∨ (∀x, p x) := decidable.forall_or_distrib_left -- See Note [decidable namespace] protected theorem decidable.forall_or_distrib_right {q : Prop} {p : α → Prop} [decidable q] : (∀x, p x ∨ q) ↔ (∀x, p x) ∨ q := by simp [or_comm, decidable.forall_or_distrib_left] theorem forall_or_distrib_right {q : Prop} {p : α → Prop} : (∀x, p x ∨ q) ↔ (∀x, p x) ∨ q := decidable.forall_or_distrib_right /-- A predicate holds everywhere on the image of a surjective functions iff it holds everywhere. -/ theorem forall_iff_forall_surj {α β : Type*} {f : α → β} (h : function.surjective f) {P : β → Prop} : (∀ a, P (f a)) ↔ ∀ b, P b := ⟨λ ha b, by cases h b with a hab; rw ←hab; exact ha a, λ hb a, hb $ f a⟩ @[simp] theorem exists_prop {p q : Prop} : (∃ h : p, q) ↔ p ∧ q := ⟨λ ⟨h₁, h₂⟩, ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨h₁, h₂⟩⟩ @[simp] theorem exists_false : ¬ (∃a:α, false) := assume ⟨a, h⟩, h theorem Exists.fst {p : b → Prop} : Exists p → b | ⟨h, _⟩ := h theorem Exists.snd {p : b → Prop} : ∀ h : Exists p, p h.fst | ⟨_, h⟩ := h @[simp] theorem forall_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∀ h' : p, q h') ↔ q h := @forall_const (q h) p ⟨h⟩ @[simp] theorem exists_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∃ h' : p, q h') ↔ q h := @exists_const (q h) p ⟨h⟩ @[simp] theorem forall_prop_of_false {p : Prop} {q : p → Prop} (hn : ¬ p) : (∀ h' : p, q h') ↔ true := iff_true_intro $ λ h, hn.elim h @[simp] theorem exists_prop_of_false {p : Prop} {q : p → Prop} : ¬ p → ¬ (∃ h' : p, q h') := mt Exists.fst lemma exists_unique.exists {α : Sort*} {p : α → Prop} (h : ∃! x, p x) : ∃ x, p x := exists.elim h (λ x hx, ⟨x, and.left hx⟩) lemma exists_unique.unique {α : Sort*} {p : α → Prop} (h : ∃! x, p x) {y₁ y₂ : α} (py₁ : p y₁) (py₂ : p y₂) : y₁ = y₂ := unique_of_exists_unique h py₁ py₂ @[simp] lemma exists_unique_iff_exists {α : Sort*} [subsingleton α] {p : α → Prop} : (∃! x, p x) ↔ ∃ x, p x := ⟨λ h, h.exists, Exists.imp $ λ x hx, ⟨hx, λ y _, subsingleton.elim y x⟩⟩ lemma exists_unique.elim2 {α : Sort*} {p : α → Sort*} [∀ x, subsingleton (p x)] {q : Π x (h : p x), Prop} {b : Prop} (h₂ : ∃! x (h : p x), q x h) (h₁ : ∀ x (h : p x), q x h → (∀ y (hy : p y), q y hy → y = x) → b) : b := begin simp only [exists_unique_iff_exists] at h₂, apply h₂.elim, exact λ x ⟨hxp, hxq⟩ H, h₁ x hxp hxq (λ y hyp hyq, H y ⟨hyp, hyq⟩) end lemma exists_unique.intro2 {α : Sort*} {p : α → Sort*} [∀ x, subsingleton (p x)] {q : Π (x : α) (h : p x), Prop} (w : α) (hp : p w) (hq : q w hp) (H : ∀ y (hy : p y), q y hy → y = w) : ∃! x (hx : p x), q x hx := begin simp only [exists_unique_iff_exists], exact exists_unique.intro w ⟨hp, hq⟩ (λ y ⟨hyp, hyq⟩, H y hyp hyq) end lemma exists_unique.exists2 {α : Sort*} {p : α → Sort*} {q : Π (x : α) (h : p x), Prop} (h : ∃! x (hx : p x), q x hx) : ∃ x (hx : p x), q x hx := h.exists.imp (λ x hx, hx.exists) lemma exists_unique.unique2 {α : Sort*} {p : α → Sort*} [∀ x, subsingleton (p x)] {q : Π (x : α) (hx : p x), Prop} (h : ∃! x (hx : p x), q x hx) {y₁ y₂ : α} (hpy₁ : p y₁) (hqy₁ : q y₁ hpy₁) (hpy₂ : p y₂) (hqy₂ : q y₂ hpy₂) : y₁ = y₂ := begin simp only [exists_unique_iff_exists] at h, exact h.unique ⟨hpy₁, hqy₁⟩ ⟨hpy₂, hqy₂⟩ end end quantifiers /-! ### Classical lemmas -/ namespace classical variables {α : Sort*} {p : α → Prop} theorem cases {p : Prop → Prop} (h1 : p true) (h2 : p false) : ∀a, p a := assume a, cases_on a h1 h2 /- use shortened names to avoid conflict when classical namespace is open. -/ noncomputable lemma dec (p : Prop) : decidable p := -- see Note [classical lemma] by apply_instance noncomputable lemma dec_pred (p : α → Prop) : decidable_pred p := -- see Note [classical lemma] by apply_instance noncomputable lemma dec_rel (p : α → α → Prop) : decidable_rel p := -- see Note [classical lemma] by apply_instance noncomputable lemma dec_eq (α : Sort*) : decidable_eq α := -- see Note [classical lemma] by apply_instance /-- We make decidability results that depends on `classical.choice` noncomputable lemmas. * We have to mark them as noncomputable, because otherwise Lean will try to generate bytecode for them, and fail because it depends on `classical.choice`. * We make them lemmas, and not definitions, because otherwise later definitions will raise \"failed to generate bytecode\" errors when writing something like `letI := classical.dec_eq _`. Cf. <https://leanprover-community.github.io/archive/113488general/08268noncomputabletheorem.html> -/ library_note "classical lemma" /-- Construct a function from a default value `H0`, and a function to use if there exists a value satisfying the predicate. -/ @[elab_as_eliminator] noncomputable def {u} exists_cases {C : Sort u} (H0 : C) (H : ∀ a, p a → C) : C := if h : ∃ a, p a then H (classical.some h) (classical.some_spec h) else H0 lemma some_spec2 {α : Sort*} {p : α → Prop} {h : ∃a, p a} (q : α → Prop) (hpq : ∀a, p a → q a) : q (some h) := hpq _ $ some_spec _ /-- A version of classical.indefinite_description which is definitionally equal to a pair -/ noncomputable def subtype_of_exists {α : Type*} {P : α → Prop} (h : ∃ x, P x) : {x // P x} := ⟨classical.some h, classical.some_spec h⟩ end classical /-- This function has the same type as `exists.rec_on`, and can be used to case on an equality, but `exists.rec_on` can only eliminate into Prop, while this version eliminates into any universe using the axiom of choice. -/ @[elab_as_eliminator] noncomputable def {u} exists.classical_rec_on {α} {p : α → Prop} (h : ∃ a, p a) {C : Sort u} (H : ∀ a, p a → C) : C := H (classical.some h) (classical.some_spec h) /-! ### Declarations about bounded quantifiers -/ section bounded_quantifiers variables {α : Sort*} {r p q : α → Prop} {P Q : ∀ x, p x → Prop} {b : Prop} theorem bex_def : (∃ x (h : p x), q x) ↔ ∃ x, p x ∧ q x := ⟨λ ⟨x, px, qx⟩, ⟨x, px, qx⟩, λ ⟨x, px, qx⟩, ⟨x, px, qx⟩⟩ theorem bex.elim {b : Prop} : (∃ x h, P x h) → (∀ a h, P a h → b) → b | ⟨a, h₁, h₂⟩ h' := h' a h₁ h₂ theorem bex.intro (a : α) (h₁ : p a) (h₂ : P a h₁) : ∃ x (h : p x), P x h := ⟨a, h₁, h₂⟩ theorem ball_congr (H : ∀ x h, P x h ↔ Q x h) : (∀ x h, P x h) ↔ (∀ x h, Q x h) := forall_congr $ λ x, forall_congr (H x) theorem bex_congr (H : ∀ x h, P x h ↔ Q x h) : (∃ x h, P x h) ↔ (∃ x h, Q x h) := exists_congr $ λ x, exists_congr (H x) theorem ball.imp_right (H : ∀ x h, (P x h → Q x h)) (h₁ : ∀ x h, P x h) (x h) : Q x h := H _ _ $ h₁ _ _ theorem bex.imp_right (H : ∀ x h, (P x h → Q x h)) : (∃ x h, P x h) → ∃ x h, Q x h | ⟨x, h, h'⟩ := ⟨_, _, H _ _ h'⟩ theorem ball.imp_left (H : ∀ x, p x → q x) (h₁ : ∀ x, q x → r x) (x) (h : p x) : r x := h₁ _ $ H _ h theorem bex.imp_left (H : ∀ x, p x → q x) : (∃ x (_ : p x), r x) → ∃ x (_ : q x), r x | ⟨x, hp, hr⟩ := ⟨x, H _ hp, hr⟩ theorem ball_of_forall (h : ∀ x, p x) (x) : p x := h x theorem forall_of_ball (H : ∀ x, p x) (h : ∀ x, p x → q x) (x) : q x := h x $ H x theorem bex_of_exists (H : ∀ x, p x) : (∃ x, q x) → ∃ x (_ : p x), q x | ⟨x, hq⟩ := ⟨x, H x, hq⟩ theorem exists_of_bex : (∃ x (_ : p x), q x) → ∃ x, q x | ⟨x, _, hq⟩ := ⟨x, hq⟩ @[simp] theorem bex_imp_distrib : ((∃ x h, P x h) → b) ↔ (∀ x h, P x h → b) := by simp theorem not_bex : (¬ ∃ x h, P x h) ↔ ∀ x h, ¬ P x h := bex_imp_distrib theorem not_ball_of_bex_not : (∃ x h, ¬ P x h) → ¬ ∀ x h, P x h | ⟨x, h, hp⟩ al := hp $ al x h -- See Note [decidable namespace] protected theorem decidable.not_ball [decidable (∃ x h, ¬ P x h)] [∀ x h, decidable (P x h)] : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := ⟨not.decidable_imp_symm $ λ nx x h, nx.decidable_imp_symm $ λ h', ⟨x, h, h'⟩, not_ball_of_bex_not⟩ theorem not_ball : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := decidable.not_ball theorem ball_true_iff (p : α → Prop) : (∀ x, p x → true) ↔ true := iff_true_intro (λ h hrx, trivial) theorem ball_and_distrib : (∀ x h, P x h ∧ Q x h) ↔ (∀ x h, P x h) ∧ (∀ x h, Q x h) := iff.trans (forall_congr $ λ x, forall_and_distrib) forall_and_distrib theorem bex_or_distrib : (∃ x h, P x h ∨ Q x h) ↔ (∃ x h, P x h) ∨ (∃ x h, Q x h) := iff.trans (exists_congr $ λ x, exists_or_distrib) exists_or_distrib end bounded_quantifiers namespace classical local attribute [instance] prop_decidable theorem not_ball {α : Sort*} {p : α → Prop} {P : Π (x : α), p x → Prop} : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := _root_.not_ball end classical lemma ite_eq_iff {α} {p : Prop} [decidable p] {a b c : α} : (if p then a else b) = c ↔ p ∧ a = c ∨ ¬p ∧ b = c := by by_cases p; simp * @[simp] lemma ite_eq_left_iff {α} {p : Prop} [decidable p] {a b : α} : (if p then a else b) = a ↔ (¬p → b = a) := by by_cases p; simp * @[simp] lemma ite_eq_right_iff {α} {p : Prop} [decidable p] {a b : α} : (if p then a else b) = b ↔ (p → a = b) := by by_cases p; simp * /-! ### Declarations about `nonempty` -/ section nonempty universe variables u v w variables {α : Type u} {β : Type v} {γ : α → Type w} attribute [simp] nonempty_of_inhabited @[priority 20] instance has_zero.nonempty [has_zero α] : nonempty α := ⟨0⟩ @[priority 20] instance has_one.nonempty [has_one α] : nonempty α := ⟨1⟩ lemma exists_true_iff_nonempty {α : Sort*} : (∃a:α, true) ↔ nonempty α := iff.intro (λ⟨a, _⟩, ⟨a⟩) (λ⟨a⟩, ⟨a, trivial⟩) @[simp] lemma nonempty_Prop {p : Prop} : nonempty p ↔ p := iff.intro (assume ⟨h⟩, h) (assume h, ⟨h⟩) lemma not_nonempty_iff_imp_false : ¬ nonempty α ↔ α → false := ⟨λ h a, h ⟨a⟩, λ h ⟨a⟩, h a⟩ @[simp] lemma nonempty_sigma : nonempty (Σa:α, γ a) ↔ (∃a:α, nonempty (γ a)) := iff.intro (assume ⟨⟨a, c⟩⟩, ⟨a, ⟨c⟩⟩) (assume ⟨a, ⟨c⟩⟩, ⟨⟨a, c⟩⟩) @[simp] lemma nonempty_subtype {α : Sort u} {p : α → Prop} : nonempty (subtype p) ↔ (∃a:α, p a) := iff.intro (assume ⟨⟨a, h⟩⟩, ⟨a, h⟩) (assume ⟨a, h⟩, ⟨⟨a, h⟩⟩) @[simp] lemma nonempty_prod : nonempty (α × β) ↔ (nonempty α ∧ nonempty β) := iff.intro (assume ⟨⟨a, b⟩⟩, ⟨⟨a⟩, ⟨b⟩⟩) (assume ⟨⟨a⟩, ⟨b⟩⟩, ⟨⟨a, b⟩⟩) @[simp] lemma nonempty_pprod {α : Sort u} {β : Sort v} : nonempty (pprod α β) ↔ (nonempty α ∧ nonempty β) := iff.intro (assume ⟨⟨a, b⟩⟩, ⟨⟨a⟩, ⟨b⟩⟩) (assume ⟨⟨a⟩, ⟨b⟩⟩, ⟨⟨a, b⟩⟩) @[simp] lemma nonempty_sum : nonempty (α ⊕ β) ↔ (nonempty α ∨ nonempty β) := iff.intro (assume ⟨h⟩, match h with sum.inl a := or.inl ⟨a⟩ | sum.inr b := or.inr ⟨b⟩ end) (assume h, match h with or.inl ⟨a⟩ := ⟨sum.inl a⟩ | or.inr ⟨b⟩ := ⟨sum.inr b⟩ end) @[simp] lemma nonempty_psum {α : Sort u} {β : Sort v} : nonempty (psum α β) ↔ (nonempty α ∨ nonempty β) := iff.intro (assume ⟨h⟩, match h with psum.inl a := or.inl ⟨a⟩ | psum.inr b := or.inr ⟨b⟩ end) (assume h, match h with or.inl ⟨a⟩ := ⟨psum.inl a⟩ | or.inr ⟨b⟩ := ⟨psum.inr b⟩ end) @[simp] lemma nonempty_psigma {α : Sort u} {β : α → Sort v} : nonempty (psigma β) ↔ (∃a:α, nonempty (β a)) := iff.intro (assume ⟨⟨a, c⟩⟩, ⟨a, ⟨c⟩⟩) (assume ⟨a, ⟨c⟩⟩, ⟨⟨a, c⟩⟩) @[simp] lemma nonempty_empty : ¬ nonempty empty := assume ⟨h⟩, h.elim @[simp] lemma nonempty_ulift : nonempty (ulift α) ↔ nonempty α := iff.intro (assume ⟨⟨a⟩⟩, ⟨a⟩) (assume ⟨a⟩, ⟨⟨a⟩⟩) @[simp] lemma nonempty_plift {α : Sort u} : nonempty (plift α) ↔ nonempty α := iff.intro (assume ⟨⟨a⟩⟩, ⟨a⟩) (assume ⟨a⟩, ⟨⟨a⟩⟩) @[simp] lemma nonempty.forall {α : Sort u} {p : nonempty α → Prop} : (∀h:nonempty α, p h) ↔ (∀a, p ⟨a⟩) := iff.intro (assume h a, h _) (assume h ⟨a⟩, h _) @[simp] lemma nonempty.exists {α : Sort u} {p : nonempty α → Prop} : (∃h:nonempty α, p h) ↔ (∃a, p ⟨a⟩) := iff.intro (assume ⟨⟨a⟩, h⟩, ⟨a, h⟩) (assume ⟨a, h⟩, ⟨⟨a⟩, h⟩) lemma classical.nonempty_pi {α : Sort u} {β : α → Sort v} : nonempty (Πa:α, β a) ↔ (∀a:α, nonempty (β a)) := iff.intro (assume ⟨f⟩ a, ⟨f a⟩) (assume f, ⟨assume a, classical.choice $ f a⟩) /-- Using `classical.choice`, lifts a (`Prop`-valued) `nonempty` instance to a (`Type`-valued) `inhabited` instance. `classical.inhabited_of_nonempty` already exists, in `core/init/classical.lean`, but the assumption is not a type class argument, which makes it unsuitable for some applications. -/ noncomputable def classical.inhabited_of_nonempty' {α : Sort u} [h : nonempty α] : inhabited α := ⟨classical.choice h⟩ /-- Using `classical.choice`, extracts a term from a `nonempty` type. -/ @[reducible] protected noncomputable def nonempty.some {α : Sort u} (h : nonempty α) : α := classical.choice h /-- Using `classical.choice`, extracts a term from a `nonempty` type. -/ @[reducible] protected noncomputable def classical.arbitrary (α : Sort u) [h : nonempty α] : α := classical.choice h /-- Given `f : α → β`, if `α` is nonempty then `β` is also nonempty. `nonempty` cannot be a `functor`, because `functor` is restricted to `Type`. -/ lemma nonempty.map {α : Sort u} {β : Sort v} (f : α → β) : nonempty α → nonempty β | ⟨h⟩ := ⟨f h⟩ protected lemma nonempty.map2 {α β γ : Sort*} (f : α → β → γ) : nonempty α → nonempty β → nonempty γ | ⟨x⟩ ⟨y⟩ := ⟨f x y⟩ protected lemma nonempty.congr {α : Sort u} {β : Sort v} (f : α → β) (g : β → α) : nonempty α ↔ nonempty β := ⟨nonempty.map f, nonempty.map g⟩ lemma nonempty.elim_to_inhabited {α : Sort*} [h : nonempty α] {p : Prop} (f : inhabited α → p) : p := h.elim $ f ∘ inhabited.mk instance {α β} [h : nonempty α] [h2 : nonempty β] : nonempty (α × β) := h.elim $ λ g, h2.elim $ λ g2, ⟨⟨g, g2⟩⟩ end nonempty section ite /-- A function applied to a `dite` is a `dite` of that function applied to each of the branches. -/ lemma apply_dite {α β : Sort*} (f : α → β) (P : Prop) [decidable P] (x : P → α) (y : ¬P → α) : f (dite P x y) = dite P (λ h, f (x h)) (λ h, f (y h)) := by { by_cases h : P; simp [h] } /-- A function applied to a `ite` is a `ite` of that function applied to each of the branches. -/ lemma apply_ite {α β : Sort*} (f : α → β) (P : Prop) [decidable P] (x y : α) : f (ite P x y) = ite P (f x) (f y) := apply_dite f P (λ _, x) (λ _, y) /-- A two-argument function applied to two `dite`s is a `dite` of that two-argument function applied to each of the branches. -/ lemma apply_dite2 {α β γ : Sort*} (f : α → β → γ) (P : Prop) [decidable P] (a : P → α) (b : ¬P → α) (c : P → β) (d : ¬P → β) : f (dite P a b) (dite P c d) = dite P (λ h, f (a h) (c h)) (λ h, f (b h) (d h)) := by { by_cases h : P; simp [h] } /-- A two-argument function applied to two `ite`s is a `ite` of that two-argument function applied to each of the branches. -/ lemma apply_ite2 {α β γ : Sort*} (f : α → β → γ) (P : Prop) [decidable P] (a b : α) (c d : β) : f (ite P a b) (ite P c d) = ite P (f a c) (f b d) := apply_dite2 f P (λ _, a) (λ _, b) (λ _, c) (λ _, d) /-- A 'dite' producing a `Pi` type `Π a, β a`, applied to a value `x : α` is a `dite` that applies either branch to `x`. -/ lemma dite_apply {α : Sort*} {β : α → Sort*} (P : Prop) [decidable P] (f : P → Π a, β a) (g : ¬ P → Π a, β a) (x : α) : (dite P f g) x = dite P (λ h, f h x) (λ h, g h x) := by { by_cases h : P; simp [h] } /-- A 'ite' producing a `Pi` type `Π a, β a`, applied to a value `x : α` is a `ite` that applies either branch to `x` -/ lemma ite_apply {α : Sort*} {β : α → Sort*} (P : Prop) [decidable P] (f g : Π a, β a) (x : α) : (ite P f g) x = ite P (f x) (g x) := dite_apply P (λ _, f) (λ _, g) x /-- Negation of the condition `P : Prop` in a `dite` is the same as swapping the branches. -/ @[simp] lemma dite_not {α : Sort*} (P : Prop) [decidable P] (x : ¬ P → α) (y : ¬¬ P → α) : dite (¬ P) x y = dite P (λ h, y (not_not_intro h)) x := by { by_cases h : P; simp [h] } /-- Negation of the condition `P : Prop` in a `ite` is the same as swapping the branches. -/ @[simp] lemma ite_not {α : Sort*} (P : Prop) [decidable P] (x y : α) : ite (¬ P) x y = ite P y x := dite_not P (λ _, x) (λ _, y) lemma ite_and {α} {p q : Prop} [decidable p] [decidable q] {x y : α} : ite (p ∧ q) x y = ite p (ite q x y) y := by { by_cases hp : p; by_cases hq : q; simp [hp, hq] } end ite
1b11e3444dd63b5974f4bf87e2a9bfe5f6e955f2
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/data/mv_polynomial/variables.lean
abfbd4ebb588459d529a1f0abdc7dc2915cb806b
[ "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
29,333
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Johan Commelin, Mario Carneiro -/ import algebra.big_operators.order import data.mv_polynomial.monad /-! # Degrees and variables of polynomials This file establishes many results about the degree and variable sets of a multivariate polynomial. The *variable set* of a polynomial $P \in R[X]$ is a `finset` containing each $x \in X$ that appears in a monomial in $P$. The *degree set* of a polynomial $P \in R[X]$ is a `multiset` containing, for each $x$ in the variable set, $n$ copies of $x$, where $n$ is the maximum number of copies of $x$ appearing in a monomial of $P$. ## Main declarations * `mv_polynomial.degrees p` : the multiset of variables representing the union of the multisets corresponding to each non-zero monomial in `p`. For example if `7 ≠ 0` in `R` and `p = x²y+7y³` then `degrees p = {x, x, y, y, y}` * `mv_polynomial.vars p` : the finset of variables occurring in `p`. For example if `p = x⁴y+yz` then `vars p = {x, y, z}` * `mv_polynomial.degree_of n p : ℕ` : the total degree of `p` with respect to the variable `n`. For example if `p = x⁴y+yz` then `degree_of y p = 1`. * `mv_polynomial.total_degree p : ℕ` : the max of the sizes of the multisets `s` whose monomials `X^s` occur in `p`. For example if `p = x⁴y+yz` then `total_degree p = 5`. ## Notation As in other polynomial files, we typically use the notation: + `σ τ : Type*` (indexing the variables) + `R : Type*` `[comm_semiring R]` (the coefficients) + `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set. This will give rise to a monomial in `mv_polynomial σ R` which mathematicians might call `X^s` + `r : R` + `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians + `p : mv_polynomial σ R` -/ noncomputable theory open_locale classical big_operators open set function finsupp add_monoid_algebra open_locale big_operators universes u v w variables {R : Type u} {S : Type v} namespace mv_polynomial variables {σ τ : Type*} {r : R} {e : ℕ} {n m : σ} {s : σ →₀ ℕ} section comm_semiring variables [comm_semiring R] {p q : mv_polynomial σ R} section degrees /-! ### `degrees` -/ /-- The maximal degrees of each variable in a multi-variable polynomial, expressed as a multiset. (For example, `degrees (x^2 * y + y^3)` would be `{x, x, y, y, y}`.) -/ def degrees (p : mv_polynomial σ R) : multiset σ := p.support.sup (λs:σ →₀ ℕ, s.to_multiset) lemma degrees_monomial (s : σ →₀ ℕ) (a : R) : degrees (monomial s a) ≤ s.to_multiset := finset.sup_le $ assume t h, begin have := finsupp.support_single_subset h, rw [finset.mem_singleton] at this, rw this end lemma degrees_monomial_eq (s : σ →₀ ℕ) (a : R) (ha : a ≠ 0) : degrees (monomial s a) = s.to_multiset := le_antisymm (degrees_monomial s a) $ finset.le_sup $ by rw [support_monomial, if_neg ha, finset.mem_singleton] lemma degrees_C (a : R) : degrees (C a : mv_polynomial σ R) = 0 := multiset.le_zero.1 $ degrees_monomial _ _ lemma degrees_X' (n : σ) : degrees (X n : mv_polynomial σ R) ≤ {n} := le_trans (degrees_monomial _ _) $ le_of_eq $ to_multiset_single _ _ @[simp] lemma degrees_X [nontrivial R] (n : σ) : degrees (X n : mv_polynomial σ R) = {n} := (degrees_monomial_eq _ (1 : R) one_ne_zero).trans (to_multiset_single _ _) @[simp] lemma degrees_zero : degrees (0 : mv_polynomial σ R) = 0 := by { rw ← C_0, exact degrees_C 0 } @[simp] lemma degrees_one : degrees (1 : mv_polynomial σ R) = 0 := degrees_C 1 lemma degrees_add (p q : mv_polynomial σ R) : (p + q).degrees ≤ p.degrees ⊔ q.degrees := begin refine finset.sup_le (assume b hb, _), have := finsupp.support_add hb, rw finset.mem_union at this, cases this, { exact le_sup_of_le_left (finset.le_sup this) }, { exact le_sup_of_le_right (finset.le_sup this) }, end lemma degrees_sum {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ R) : (∑ i in s, f i).degrees ≤ s.sup (λi, (f i).degrees) := begin refine s.induction _ _, { simp only [finset.sum_empty, finset.sup_empty, degrees_zero], exact le_rfl }, { 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 σ R) : (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_bUnion, finset.mem_singleton] at this, rcases this with ⟨a₁, h₁, a₂, h₂, rfl⟩, rw [finsupp.to_multiset_add], exact add_le_add (finset.le_sup h₁) (finset.le_sup h₂) end lemma degrees_prod {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ R) : (∏ i in s, f i).degrees ≤ ∑ i in s, (f i).degrees := begin refine s.induction _ _, { simp only [finset.prod_empty, finset.sum_empty, degrees_one] }, { assume i s his ih, rw [finset.prod_insert his, finset.sum_insert his], exact le_trans (degrees_mul _ _) (add_le_add_left ih _) } end lemma degrees_pow (p : mv_polynomial σ R) : ∀(n : ℕ), (p^n).degrees ≤ n • p.degrees | 0 := begin rw [pow_zero, degrees_one], exact multiset.zero_le _ end | (n + 1) := by { rw [pow_succ, add_smul, add_comm, one_smul], exact le_trans (degrees_mul _ _) (add_le_add_left (degrees_pow n) _) } lemma mem_degrees {p : mv_polynomial σ R} {i : σ} : i ∈ p.degrees ↔ ∃ d, p.coeff d ≠ 0 ∧ i ∈ d.support := by simp only [degrees, multiset.mem_sup, ← mem_support_iff, finsupp.mem_to_multiset, exists_prop] lemma le_degrees_add {p q : mv_polynomial σ R} (h : p.degrees.disjoint q.degrees) : p.degrees ≤ (p + q).degrees := begin apply finset.sup_le, intros d hd, rw multiset.disjoint_iff_ne at h, rw multiset.le_iff_count, intros i, rw [degrees, multiset.count_finset_sup], simp only [finsupp.count_to_multiset], by_cases h0 : d = 0, { simp only [h0, zero_le, finsupp.zero_apply], }, { refine @finset.le_sup _ _ _ _ (p + q).support _ d _, rw [mem_support_iff, coeff_add], suffices : q.coeff d = 0, { rwa [this, add_zero, coeff, ← finsupp.mem_support_iff], }, rw [← finsupp.support_eq_empty, ← ne.def, ← finset.nonempty_iff_ne_empty] at h0, obtain ⟨j, hj⟩ := h0, contrapose! h, rw mem_support_iff at hd, refine ⟨j, _, j, _, rfl⟩, all_goals { rw mem_degrees, refine ⟨d, _, hj⟩, assumption } } end lemma degrees_add_of_disjoint {p q : mv_polynomial σ R} (h : multiset.disjoint p.degrees q.degrees) : (p + q).degrees = p.degrees ∪ q.degrees := begin apply le_antisymm, { apply degrees_add }, { apply multiset.union_le, { apply le_degrees_add h }, { rw add_comm, apply le_degrees_add h.symm } } end lemma degrees_map [comm_semiring S] (p : mv_polynomial σ R) (f : R →+* S) : (map f p).degrees ⊆ p.degrees := begin dsimp only [degrees], apply multiset.subset_of_le, apply finset.sup_mono, apply mv_polynomial.support_map_subset end lemma degrees_rename (f : σ → τ) (φ : mv_polynomial σ R) : (rename f φ).degrees ⊆ (φ.degrees.map f) := begin intros i, rw [mem_degrees, multiset.mem_map], rintro ⟨d, hd, hi⟩, obtain ⟨x, rfl, hx⟩ := coeff_rename_ne_zero _ _ _ hd, simp only [map_domain, finsupp.mem_support_iff] at hi, rw [sum_apply, finsupp.sum] at hi, contrapose! hi, rw [finset.sum_eq_zero], intros j hj, simp only [exists_prop, mem_degrees] at hi, specialize hi j ⟨x, hx, hj⟩, rw [single_apply, if_neg hi], end lemma degrees_map_of_injective [comm_semiring S] (p : mv_polynomial σ R) {f : R →+* S} (hf : injective f) : (map f p).degrees = p.degrees := by simp only [degrees, mv_polynomial.support_map_of_injective _ hf] lemma degrees_rename_of_injective {p : mv_polynomial σ R} {f : σ → τ} (h : function.injective f) : degrees (rename f p) = (degrees p).map f := begin simp only [degrees, multiset.map_finset_sup p.support finsupp.to_multiset f h, support_rename_of_injective h, finset.sup_image], refine finset.sup_congr rfl (λ x hx, _), exact (finsupp.to_multiset_map _ _).symm, end end degrees section vars /-! ### `vars` -/ /-- `vars p` is the set of variables appearing in the polynomial `p` -/ def vars (p : mv_polynomial σ R) : finset σ := p.degrees.to_finset @[simp] lemma vars_0 : (0 : mv_polynomial σ R).vars = ∅ := by rw [vars, degrees_zero, multiset.to_finset_zero] @[simp] lemma vars_monomial (h : r ≠ 0) : (monomial s r).vars = s.support := by rw [vars, degrees_monomial_eq _ _ h, finsupp.to_finset_to_multiset] @[simp] lemma vars_C : (C r : mv_polynomial σ R).vars = ∅ := by rw [vars, degrees_C, multiset.to_finset_zero] @[simp] lemma vars_X [nontrivial R] : (X n : mv_polynomial σ R).vars = {n} := by rw [X, vars_monomial (@one_ne_zero R _ _), finsupp.support_single_ne_zero (one_ne_zero : 1 ≠ 0)] lemma mem_vars (i : σ) : i ∈ p.vars ↔ ∃ (d : σ →₀ ℕ) (H : d ∈ p.support), i ∈ d.support := by simp only [vars, multiset.mem_to_finset, mem_degrees, mem_support_iff, exists_prop] lemma mem_support_not_mem_vars_zero {f : mv_polynomial σ R} {x : σ →₀ ℕ} (H : x ∈ f.support) {v : σ} (h : v ∉ vars f) : x v = 0 := begin rw [vars, multiset.mem_to_finset] at h, rw ← finsupp.not_mem_support_iff, contrapose! h, unfold degrees, rw (show f.support = insert x f.support, from eq.symm $ finset.insert_eq_of_mem H), rw finset.sup_insert, simp only [multiset.mem_union, multiset.sup_eq_union], left, rwa [←to_finset_to_multiset, multiset.mem_to_finset] at h, end lemma vars_add_subset (p q : mv_polynomial σ R) : (p + q).vars ⊆ p.vars ∪ q.vars := begin intros x hx, simp only [vars, finset.mem_union, multiset.mem_to_finset] at hx ⊢, simpa using multiset.mem_of_le (degrees_add _ _) hx, end lemma vars_add_of_disjoint (h : disjoint p.vars q.vars) : (p + q).vars = p.vars ∪ q.vars := begin apply finset.subset.antisymm (vars_add_subset p q), intros x hx, simp only [vars, multiset.disjoint_to_finset] at h hx ⊢, rw [degrees_add_of_disjoint h, multiset.to_finset_union], exact hx end section mul lemma vars_mul (φ ψ : mv_polynomial σ R) : (φ * ψ).vars ⊆ φ.vars ∪ ψ.vars := begin intro i, simp only [mem_vars, finset.mem_union], rintro ⟨d, hd, hi⟩, rw [mem_support_iff, coeff_mul] at hd, contrapose! hd, cases hd, rw finset.sum_eq_zero, rintro ⟨d₁, d₂⟩ H, rw finsupp.mem_antidiagonal at H, subst H, obtain H|H : i ∈ d₁.support ∨ i ∈ d₂.support, { simpa only [finset.mem_union] using finsupp.support_add hi, }, { suffices : coeff d₁ φ = 0, by simp [this], rw [coeff, ← finsupp.not_mem_support_iff], intro, solve_by_elim, }, { suffices : coeff d₂ ψ = 0, by simp [this], rw [coeff, ← finsupp.not_mem_support_iff], intro, solve_by_elim, }, end @[simp] lemma vars_one : (1 : mv_polynomial σ R).vars = ∅ := vars_C lemma vars_pow (φ : mv_polynomial σ R) (n : ℕ) : (φ ^ n).vars ⊆ φ.vars := begin induction n with n ih, { simp }, { rw pow_succ, apply finset.subset.trans (vars_mul _ _), exact finset.union_subset (finset.subset.refl _) ih } end /-- The variables of the product of a family of polynomials are a subset of the union of the sets of variables of each polynomial. -/ lemma vars_prod {ι : Type*} {s : finset ι} (f : ι → mv_polynomial σ R) : (∏ i in s, f i).vars ⊆ s.bUnion (λ i, (f i).vars) := begin apply s.induction_on, { simp }, { intros a s hs hsub, simp only [hs, finset.bUnion_insert, finset.prod_insert, not_false_iff], apply finset.subset.trans (vars_mul _ _), exact finset.union_subset_union (finset.subset.refl _) hsub } end section is_domain variables {A : Type*} [comm_ring A] [is_domain A] lemma vars_C_mul (a : A) (ha : a ≠ 0) (φ : mv_polynomial σ A) : (C a * φ).vars = φ.vars := begin ext1 i, simp only [mem_vars, exists_prop, mem_support_iff], apply exists_congr, intro d, apply and_congr _ iff.rfl, rw [coeff_C_mul, mul_ne_zero_iff, eq_true_intro ha, true_and], end end is_domain end mul section sum variables {ι : Type*} (t : finset ι) (φ : ι → mv_polynomial σ R) lemma vars_sum_subset : (∑ i in t, φ i).vars ⊆ finset.bUnion t (λ i, (φ i).vars) := begin apply t.induction_on, { simp }, { intros a s has hsum, rw [finset.bUnion_insert, finset.sum_insert has], refine finset.subset.trans (vars_add_subset _ _) (finset.union_subset_union (finset.subset.refl _) _), assumption } end lemma vars_sum_of_disjoint (h : pairwise $ disjoint on (λ i, (φ i).vars)) : (∑ i in t, φ i).vars = finset.bUnion t (λ i, (φ i).vars) := begin apply t.induction_on, { simp }, { intros a s has hsum, rw [finset.bUnion_insert, finset.sum_insert has, vars_add_of_disjoint, hsum], unfold pairwise on_fun at h, rw hsum, simp only [finset.disjoint_iff_ne] at h ⊢, intros v hv v2 hv2, rw finset.mem_bUnion at hv2, rcases hv2 with ⟨i, his, hi⟩, refine h a i _ _ hv _ hi, rintro rfl, contradiction } end end sum section map variables [comm_semiring S] (f : R →+* S) variable (p) lemma vars_map : (map f p).vars ⊆ p.vars := by simp [vars, degrees_map] variable {f} lemma vars_map_of_injective (hf : injective f) : (map f p).vars = p.vars := by simp [vars, degrees_map_of_injective _ hf] lemma vars_monomial_single (i : σ) {e : ℕ} {r : R} (he : e ≠ 0) (hr : r ≠ 0) : (monomial (finsupp.single i e) r).vars = {i} := by rw [vars_monomial hr, finsupp.support_single_ne_zero he] lemma vars_eq_support_bUnion_support : p.vars = p.support.bUnion finsupp.support := by { ext i, rw [mem_vars, finset.mem_bUnion] } end map end vars section degree_of /-! ### `degree_of` -/ /-- `degree_of n p` gives the highest power of X_n that appears in `p` -/ def degree_of (n : σ) (p : mv_polynomial σ R) : ℕ := p.degrees.count n lemma degree_of_eq_sup (n : σ) (f : mv_polynomial σ R) : degree_of n f = f.support.sup (λ m, m n) := begin rw [degree_of, degrees, multiset.count_finset_sup], congr, ext, simp, end lemma degree_of_lt_iff {n : σ} {f : mv_polynomial σ R} {d : ℕ} (h : 0 < d) : degree_of n f < d ↔ ∀ m : σ →₀ ℕ, m ∈ f.support → m n < d := by rwa [degree_of_eq_sup n f, finset.sup_lt_iff] @[simp] lemma degree_of_zero (n : σ) : degree_of n (0 : mv_polynomial σ R) = 0 := by simp only [degree_of, degrees_zero, multiset.count_zero] @[simp] lemma degree_of_C (a : R) (x : σ): degree_of x (C a : mv_polynomial σ R) = 0 := by simp [degree_of, degrees_C] lemma degree_of_X (i j : σ) [nontrivial R] : degree_of i (X j : mv_polynomial σ R) = if i = j then 1 else 0 := begin by_cases c : i = j, { simp only [c, if_true, eq_self_iff_true, degree_of, degrees_X, multiset.count_singleton] }, simp [c, if_false, degree_of, degrees_X], end lemma degree_of_add_le (n : σ) (f g : mv_polynomial σ R) : degree_of n (f + g) ≤ max (degree_of n f) (degree_of n g) := begin repeat {rw degree_of}, apply (multiset.count_le_of_le n (degrees_add f g)).trans, dsimp, rw multiset.count_union, end lemma monomial_le_degree_of (i : σ) {f : mv_polynomial σ R} {m : σ →₀ ℕ} (h_m : m ∈ f.support) : m i ≤ degree_of i f := begin rw degree_of_eq_sup i, apply finset.le_sup h_m, end -- TODO we can prove equality here if R is a domain lemma degree_of_mul_le (i : σ) (f g: mv_polynomial σ R) : degree_of i (f * g) ≤ degree_of i f + degree_of i g := begin repeat {rw degree_of}, convert multiset.count_le_of_le i (degrees_mul f g), rw multiset.count_add, end lemma degree_of_mul_X_ne {i j : σ} (f : mv_polynomial σ R) (h : i ≠ j) : degree_of i (f * X j) = degree_of i f := begin repeat {rw degree_of_eq_sup i}, rw support_mul_X, simp only [finset.sup_map], congr, ext, simp only [ single, nat.one_ne_zero, add_right_eq_self, add_right_embedding_apply, coe_mk, pi.add_apply, comp_app, ite_eq_right_iff, coe_add ], cc, end /- TODO in the following we have equality iff f ≠ 0 -/ lemma degree_of_mul_X_eq (j : σ) (f : mv_polynomial σ R) : degree_of j (f * X j) ≤ degree_of j f + 1 := begin repeat {rw degree_of}, apply (multiset.count_le_of_le j (degrees_mul f (X j))).trans, simp only [multiset.count_add, add_le_add_iff_left], convert multiset.count_le_of_le j (degrees_X' j), rw multiset.count_singleton_self, end lemma degree_of_rename_of_injective {p : mv_polynomial σ R} {f : σ → τ} (h : function.injective f) (i : σ) : degree_of (f i) (rename f p) = degree_of i p := by simp only [degree_of, degrees_rename_of_injective h, multiset.count_map_eq_count' f (p.degrees) h] end degree_of section total_degree /-! ### `total_degree` -/ /-- `total_degree p` gives the maximum |s| over the monomials X^s in `p` -/ def total_degree (p : mv_polynomial σ R) : ℕ := p.support.sup (λs, s.sum $ λn e, e) lemma total_degree_eq (p : mv_polynomial σ R) : 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 σ R) : p.total_degree ≤ p.degrees.card := begin rw [total_degree_eq], exact finset.sup_le (assume s hs, multiset.card_le_of_le $ finset.le_sup hs) end @[simp] lemma total_degree_C (a : R) : (C a : mv_polynomial σ R).total_degree = 0 := nat.eq_zero_of_le_zero $ finset.sup_le $ assume n hn, have _ := finsupp.support_single_subset hn, begin rw [finset.mem_singleton] at this, subst this, exact le_rfl end @[simp] lemma total_degree_zero : (0 : mv_polynomial σ R).total_degree = 0 := by rw [← C_0]; exact total_degree_C (0 : R) @[simp] lemma total_degree_one : (1 : mv_polynomial σ R).total_degree = 0 := total_degree_C (1 : R) @[simp] lemma total_degree_X {R} [comm_semiring R] [nontrivial R] (s : σ) : (X s : mv_polynomial σ R).total_degree = 1 := begin rw [total_degree, support_X], simp only [finset.sup, sum_single_index, finset.fold_singleton, sup_bot_eq], end lemma total_degree_add (a b : mv_polynomial σ R) : (a + b).total_degree ≤ max a.total_degree b.total_degree := finset.sup_le $ assume n hn, have _ := finsupp.support_add hn, begin rw finset.mem_union at this, cases this, { exact le_max_of_le_left (finset.le_sup this) }, { exact le_max_of_le_right (finset.le_sup this) } end lemma total_degree_add_eq_left_of_total_degree_lt {p q : mv_polynomial σ R} (h : q.total_degree < p.total_degree) : (p + q).total_degree = p.total_degree := begin classical, apply le_antisymm, { rw ← max_eq_left_of_lt h, exact total_degree_add p q, }, by_cases hp : p = 0, { simp [hp], }, obtain ⟨b, hb₁, hb₂⟩ := p.support.exists_mem_eq_sup (finsupp.support_nonempty_iff.mpr hp) (λ (m : σ →₀ ℕ), m.to_multiset.card), have hb : ¬ b ∈ q.support, { contrapose! h, rw [total_degree_eq p, hb₂, total_degree_eq], apply finset.le_sup h, }, have hbb : b ∈ (p + q).support, { apply support_sdiff_support_subset_support_add, rw finset.mem_sdiff, exact ⟨hb₁, hb⟩, }, rw [total_degree_eq, hb₂, total_degree_eq], exact finset.le_sup hbb, end lemma total_degree_add_eq_right_of_total_degree_lt {p q : mv_polynomial σ R} (h : q.total_degree < p.total_degree) : (q + p).total_degree = p.total_degree := by rw [add_comm, total_degree_add_eq_left_of_total_degree_lt h] lemma total_degree_mul (a b : mv_polynomial σ R) : (a * b).total_degree ≤ a.total_degree + b.total_degree := finset.sup_le $ assume n hn, have _ := add_monoid_algebra.support_mul a b hn, begin simp only [finset.mem_bUnion, finset.mem_singleton] at this, rcases this with ⟨a₁, h₁, a₂, h₂, rfl⟩, rw [finsupp.sum_add_index], { exact add_le_add (finset.le_sup h₁) (finset.le_sup h₂) }, { assume a, refl }, { assume a b₁ b₂, refl } end lemma total_degree_pow (a : mv_polynomial σ R) (n : ℕ) : (a ^ n).total_degree ≤ n * a.total_degree := begin induction n with n ih, { simp only [nat.nat_zero_eq_zero, zero_mul, pow_zero, total_degree_one] }, rw pow_succ, calc total_degree (a * a ^ n) ≤ a.total_degree + (a^n).total_degree : total_degree_mul _ _ ... ≤ a.total_degree + n * a.total_degree : add_le_add_left ih _ ... = (n+1) * a.total_degree : by rw [add_mul, one_mul, add_comm] end @[simp] lemma total_degree_monomial (s : σ →₀ ℕ) {c : R} (hc : c ≠ 0) : (monomial s c : mv_polynomial σ R).total_degree = s.sum (λ _ e, e) := by simp [total_degree, support_monomial, if_neg hc] @[simp] lemma total_degree_X_pow [nontrivial R] (s : σ) (n : ℕ) : (X s ^ n : mv_polynomial σ R).total_degree = n := by simp [X_pow_eq_monomial, one_ne_zero] lemma total_degree_list_prod : ∀(s : list (mv_polynomial σ R)), s.prod.total_degree ≤ (s.map mv_polynomial.total_degree).sum | [] := by rw [@list.prod_nil (mv_polynomial σ R) _, total_degree_one]; refl | (p :: ps) := begin rw [@list.prod_cons (mv_polynomial σ R) _, 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 σ R)) : 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 σ R) : (s.prod f).total_degree ≤ ∑ i in s, (f i).total_degree := begin refine le_trans (total_degree_multiset_prod _) _, rw [multiset.map_map], refl end lemma total_degree_finset_sum {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ R) : (s.sum f).total_degree ≤ finset.sup s (λ i, (f i).total_degree) := begin induction s using finset.cons_induction with a s has hind, { exact zero_le _ }, { rw [finset.sum_cons, finset.sup_cons, sup_eq_max], exact (mv_polynomial.total_degree_add _ _).trans (max_le_max le_rfl hind), } end lemma exists_degree_lt [fintype σ] (f : mv_polynomial σ R) (n : ℕ) (h : f.total_degree < n * fintype.card σ) {d : σ →₀ ℕ} (hd : d ∈ f.support) : ∃ i, d i < n := begin contrapose! h, calc n * fintype.card σ = ∑ s:σ, n : by rw [finset.sum_const, nat.nsmul_eq_mul, mul_comm, finset.card_univ] ... ≤ ∑ s, d s : finset.sum_le_sum (λ s _, h s) ... ≤ d.sum (λ i e, e) : by { rw [finsupp.sum_fintype], intros, refl } ... ≤ f.total_degree : finset.le_sup hd, end lemma coeff_eq_zero_of_total_degree_lt {f : mv_polynomial σ R} {d : σ →₀ ℕ} (h : f.total_degree < ∑ i in d.support, d i) : coeff d f = 0 := begin classical, rw [total_degree, finset.sup_lt_iff] at h, { specialize h d, rw mem_support_iff at h, refine not_not.mp (mt h _), exact lt_irrefl _, }, { exact lt_of_le_of_lt (nat.zero_le _) h, } end lemma total_degree_rename_le (f : σ → τ) (p : mv_polynomial σ R) : (rename f p).total_degree ≤ p.total_degree := finset.sup_le $ assume b, begin assume h, rw rename_eq at h, have h' := finsupp.map_domain_support h, rw finset.mem_image at h', rcases h' with ⟨s, hs, rfl⟩, rw finsupp.sum_map_domain_index, exact le_trans le_rfl (finset.le_sup hs), exact assume _, rfl, exact assume _ _ _, rfl end end total_degree section eval_vars /-! ### `vars` and `eval` -/ variables [comm_semiring S] lemma eval₂_hom_eq_constant_coeff_of_vars (f : R →+* S) {g : σ → S} {p : mv_polynomial σ R} (hp : ∀ i ∈ p.vars, g i = 0) : eval₂_hom f g p = f (constant_coeff p) := begin conv_lhs { rw p.as_sum }, simp only [ring_hom.map_sum, eval₂_hom_monomial], by_cases h0 : constant_coeff p = 0, work_on_goal 0 { rw [h0, f.map_zero, finset.sum_eq_zero], intros d hd }, work_on_goal 1 { rw [finset.sum_eq_single (0 : σ →₀ ℕ)], { rw [finsupp.prod_zero_index, mul_one], refl }, intros d hd hd0, }, repeat { obtain ⟨i, hi⟩ : d.support.nonempty, { rw [constant_coeff_eq, coeff, ← finsupp.not_mem_support_iff] at h0, rw [finset.nonempty_iff_ne_empty, ne.def, finsupp.support_eq_empty], rintro rfl, contradiction }, rw [finsupp.prod, finset.prod_eq_zero hi, mul_zero], rw [hp, zero_pow (nat.pos_of_ne_zero $ finsupp.mem_support_iff.mp hi)], rw [mem_vars], exact ⟨d, hd, hi⟩ }, { rw [constant_coeff_eq, coeff, ← ne.def, ← finsupp.mem_support_iff] at h0, intro, contradiction } end lemma aeval_eq_constant_coeff_of_vars [algebra R S] {g : σ → S} {p : mv_polynomial σ R} (hp : ∀ i ∈ p.vars, g i = 0) : aeval g p = algebra_map _ _ (constant_coeff p) := eval₂_hom_eq_constant_coeff_of_vars _ hp lemma eval₂_hom_congr' {f₁ f₂ : R →+* S} {g₁ g₂ : σ → S} {p₁ p₂ : mv_polynomial σ R} : f₁ = f₂ → (∀ i, i ∈ p₁.vars → i ∈ p₂.vars → g₁ i = g₂ i) → p₁ = p₂ → eval₂_hom f₁ g₁ p₁ = eval₂_hom f₂ g₂ p₂ := begin rintro rfl h rfl, rename [p₁ p, f₁ f], rw p.as_sum, simp only [ring_hom.map_sum, eval₂_hom_monomial], apply finset.sum_congr rfl, intros d hd, congr' 1, simp only [finsupp.prod], apply finset.prod_congr rfl, intros i hi, have : i ∈ p.vars, { rw mem_vars, exact ⟨d, hd, hi⟩ }, rw h i this this, end /-- If `f₁` and `f₂` are ring homs out of the polynomial ring and `p₁` and `p₂` are polynomials, then `f₁ p₁ = f₂ p₂` if `p₁ = p₂` and `f₁` and `f₂` are equal on `R` and on the variables of `p₁`. -/ lemma hom_congr_vars {f₁ f₂ : mv_polynomial σ R →+* S} {p₁ p₂ : mv_polynomial σ R} (hC : f₁.comp C = f₂.comp C) (hv : ∀ i, i ∈ p₁.vars → i ∈ p₂.vars → f₁ (X i) = f₂ (X i)) (hp : p₁ = p₂) : f₁ p₁ = f₂ p₂ := calc f₁ p₁ = eval₂_hom (f₁.comp C) (f₁ ∘ X) p₁ : ring_hom.congr_fun (by ext; simp) _ ... = eval₂_hom (f₂.comp C) (f₂ ∘ X) p₂ : eval₂_hom_congr' hC hv hp ... = f₂ p₂ : ring_hom.congr_fun (by ext; simp) _ lemma exists_rename_eq_of_vars_subset_range (p : mv_polynomial σ R) (f : τ → σ) (hfi : injective f) (hf : ↑p.vars ⊆ set.range f) : ∃ q : mv_polynomial τ R, rename f q = p := ⟨bind₁ (λ i : σ, option.elim (partial_inv f i) 0 X) p, begin show (rename f).to_ring_hom.comp _ p = ring_hom.id _ p, refine hom_congr_vars _ _ _, { ext1, simp [algebra_map_eq] }, { intros i hip _, rcases hf hip with ⟨i, rfl⟩, simp [partial_inv_left hfi] }, { refl } end⟩ lemma vars_bind₁ (f : σ → mv_polynomial τ R) (φ : mv_polynomial σ R) : (bind₁ f φ).vars ⊆ φ.vars.bUnion (λ i, (f i).vars) := begin calc (bind₁ f φ).vars = (φ.support.sum (λ (x : σ →₀ ℕ), (bind₁ f) (monomial x (coeff x φ)))).vars : by { rw [← alg_hom.map_sum, ← φ.as_sum], } ... ≤ φ.support.bUnion (λ (i : σ →₀ ℕ), ((bind₁ f) (monomial i (coeff i φ))).vars) : vars_sum_subset _ _ ... = φ.support.bUnion (λ (d : σ →₀ ℕ), (C (coeff d φ) * ∏ i in d.support, f i ^ d i).vars) : by simp only [bind₁_monomial] ... ≤ φ.support.bUnion (λ (d : σ →₀ ℕ), d.support.bUnion (λ i, (f i).vars)) : _ -- proof below ... ≤ φ.vars.bUnion (λ (i : σ), (f i).vars) : _, -- proof below { apply finset.bUnion_mono, intros d hd, calc (C (coeff d φ) * ∏ (i : σ) in d.support, f i ^ d i).vars ≤ (C (coeff d φ)).vars ∪ (∏ (i : σ) in d.support, f i ^ d i).vars : vars_mul _ _ ... ≤ (∏ (i : σ) in d.support, f i ^ d i).vars : by simp only [finset.empty_union, vars_C, finset.le_iff_subset, finset.subset.refl] ... ≤ d.support.bUnion (λ (i : σ), (f i ^ d i).vars) : vars_prod _ ... ≤ d.support.bUnion (λ (i : σ), (f i).vars) : _, apply finset.bUnion_mono, intros i hi, apply vars_pow, }, { intro j, simp_rw finset.mem_bUnion, rintro ⟨d, hd, ⟨i, hi, hj⟩⟩, exact ⟨i, (mem_vars _).mpr ⟨d, hd, hi⟩, hj⟩ } end lemma mem_vars_bind₁ (f : σ → mv_polynomial τ R) (φ : mv_polynomial σ R) {j : τ} (h : j ∈ (bind₁ f φ).vars) : ∃ (i : σ), i ∈ φ.vars ∧ j ∈ (f i).vars := by simpa only [exists_prop, finset.mem_bUnion, mem_support_iff, ne.def] using vars_bind₁ f φ h lemma vars_rename (f : σ → τ) (φ : mv_polynomial σ R) : (rename f φ).vars ⊆ (φ.vars.image f) := begin intros i hi, simp only [vars, exists_prop, multiset.mem_to_finset, finset.mem_image] at hi ⊢, simpa only [multiset.mem_map] using degrees_rename _ _ hi end lemma mem_vars_rename (f : σ → τ) (φ : mv_polynomial σ R) {j : τ} (h : j ∈ (rename f φ).vars) : ∃ (i : σ), i ∈ φ.vars ∧ f i = j := by simpa only [exists_prop, finset.mem_image] using vars_rename f φ h end eval_vars end comm_semiring end mv_polynomial
c9c069296095a8a59618534af9b7a31631bf10af
c777c32c8e484e195053731103c5e52af26a25d1
/src/algebra/gcd_monoid/integrally_closed.lean
47ff732f9952355ffb554305dcf88a7149567bff
[ "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
1,695
lean
/- Copyright (c) 2022 Andrew Yang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andrew Yang -/ import algebra.gcd_monoid.basic import ring_theory.integrally_closed import ring_theory.polynomial.eisenstein.basic /-! # GCD domains are integrally closed -/ open_locale big_operators polynomial variables {R A : Type*} [comm_ring R] [is_domain R] [gcd_monoid R] [comm_ring A] [algebra R A] lemma is_localization.surj_of_gcd_domain (M : submonoid R) [is_localization M A] (z : A) : ∃ a b : R, is_unit (gcd a b) ∧ z * algebra_map R A b = algebra_map R A a := begin obtain ⟨x, ⟨y, hy⟩, rfl⟩ := is_localization.mk'_surjective M z, obtain ⟨x', y', hx', hy', hu⟩ := extract_gcd x y, use [x', y', hu], rw [mul_comm, is_localization.mul_mk'_eq_mk'_of_mul], convert is_localization.mk'_mul_cancel_left _ _ using 2, { rw [subtype.coe_mk, hy', ← mul_comm y', mul_assoc], conv_lhs { rw hx' } }, { apply_instance }, end @[priority 100] instance gcd_monoid.to_is_integrally_closed : is_integrally_closed R := ⟨λ X ⟨p, hp₁, hp₂⟩, begin obtain ⟨x, y, hg, he⟩ := is_localization.surj_of_gcd_domain (non_zero_divisors R) X, have := polynomial.dvd_pow_nat_degree_of_eval₂_eq_zero (is_fraction_ring.injective R $ fraction_ring R) hp₁ y x _ hp₂ (by rw [mul_comm, he]), have : is_unit y, { rw [is_unit_iff_dvd_one, ← one_pow], exact (dvd_gcd this $ dvd_refl y).trans (gcd_pow_left_dvd_pow_gcd.trans $ pow_dvd_pow_of_dvd (is_unit_iff_dvd_one.1 hg) _) }, use x * (this.unit⁻¹ : _), erw [map_mul, ← units.coe_map_inv, eq_comm, units.eq_mul_inv_iff_mul_eq], exact he, end⟩
d54a3042e4b30ec0a7e1ae857520135cb6ee3472
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/uniform_space/absolute_value.lean
e4640629275f9b8d26df5fef1661fffa9842b786
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
1,842
lean
/- Copyright (c) 2019 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import algebra.order.absolute_value import topology.uniform_space.basic /-! # Uniform structure induced by an absolute value > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We build a uniform space structure on a commutative ring `R` equipped with an absolute value into a linear ordered field `𝕜`. Of course in the case `R` is `ℚ`, `ℝ` or `ℂ` and `𝕜 = ℝ`, we get the same thing as the metric space construction, and the general construction follows exactly the same path. ## Implementation details Note that we import `data.real.cau_seq` because this is where absolute values are defined, but the current file does not depend on real numbers. TODO: extract absolute values from that `data.real` folder. ## References * [N. Bourbaki, *Topologie générale*][bourbaki1966] ## Tags absolute value, uniform spaces -/ open set function filter uniform_space open_locale filter topology namespace absolute_value variables {𝕜 : Type*} [linear_ordered_field 𝕜] variables {R : Type*} [comm_ring R] (abv : absolute_value R 𝕜) /-- The uniform space structure coming from an absolute value. -/ protected def uniform_space : uniform_space R := uniform_space.of_fun (λ x y, abv (y - x)) (by simp) (λ x y, abv.map_sub y x) (λ x y z, (abv.sub_le _ _ _).trans_eq (add_comm _ _)) $ λ ε ε0, ⟨ε / 2, half_pos ε0, λ _ h₁ _ h₂, (add_lt_add h₁ h₂).trans_eq (add_halves ε)⟩ theorem has_basis_uniformity : 𝓤[abv.uniform_space].has_basis (λ ε : 𝕜, 0 < ε) (λ ε, {p : R × R | abv (p.2 - p.1) < ε}) := uniform_space.has_basis_of_fun (exists_gt _) _ _ _ _ _ end absolute_value
7cc02178cdb6940bc1a804a2f42b0aac3a12b358
4727251e0cd73359b15b664c3170e5d754078599
/src/data/zmod/algebra.lean
19c6123e6d4dec241e8666538594d7eec6ef4562
[ "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
974
lean
/- Copyright (c) 2021 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import data.zmod.basic import algebra.algebra.basic /-! # The `zmod n`-algebra structure on rings whose characteristic divides `n` -/ namespace zmod variables (R : Type*) [ring R] section variables {n : ℕ} (m : ℕ) [char_p R m] /-- The `zmod n`-algebra structure on rings whose characteristic `m` divides `n` -/ def algebra' (h : m ∣ n) : algebra (zmod n) R := { smul := λ a r, a * r, commutes' := λ a r, show (a * r : R) = r * a, begin rcases zmod.int_cast_surjective a with ⟨k, rfl⟩, show zmod.cast_hom h R k * r = r * zmod.cast_hom h R k, rw ring_hom.map_int_cast, exact commute.cast_int_left r k, end, smul_def' := λ a r, rfl, .. zmod.cast_hom h R } end section variables (n : ℕ) [char_p R n] instance : algebra (zmod n) R := algebra' R n (dvd_refl n) end end zmod
05645c69430b294e6541426153ea65cef4364a96
94e33a31faa76775069b071adea97e86e218a8ee
/src/category_theory/adjunction/comma.lean
803d021f22548638fcf3071536e6f8707dd4872b
[ "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
5,881
lean
/- Copyright (c) 2021 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import category_theory.adjunction.basic import category_theory.punit import category_theory.structured_arrow /-! # Properties of comma categories relating to adjunctions This file shows that for a functor `G : D ⥤ C` the data of an initial object in each `structured_arrow` category on `G` is equivalent to a left adjoint to `G`, as well as the dual. Specifically, `adjunction_of_structured_arrow_initials` gives the left adjoint assuming the appropriate initial objects exist, and `mk_initial_of_left_adjoint` constructs the initial objects provided a left adjoint. The duals are also shown. -/ universes v u₁ u₂ noncomputable theory namespace category_theory open limits variables {C : Type u₁} {D : Type u₂} [category.{v} C] [category.{v} D] (G : D ⥤ C) section of_initials variables [∀ A, has_initial (structured_arrow A G)] /-- Implementation: If each structured arrow category on `G` has an initial object, an equivalence which is helpful for constructing a left adjoint to `G`. -/ @[simps] def left_adjoint_of_structured_arrow_initials_aux (A : C) (B : D) : ((⊥_ (structured_arrow A G)).right ⟶ B) ≃ (A ⟶ G.obj B) := { to_fun := λ g, (⊥_ (structured_arrow A G)).hom ≫ G.map g, inv_fun := λ f, comma_morphism.right (initial.to (structured_arrow.mk f)), left_inv := λ g, begin let B' : structured_arrow A G := structured_arrow.mk ((⊥_ (structured_arrow A G)).hom ≫ G.map g), let g' : ⊥_ (structured_arrow A G) ⟶ B' := structured_arrow.hom_mk g rfl, have : initial.to _ = g', { apply colimit.hom_ext, rintro ⟨⟨⟩⟩ }, change comma_morphism.right (initial.to B') = _, rw this, refl end, right_inv := λ f, begin let B' : structured_arrow A G := { right := B, hom := f }, apply (comma_morphism.w (initial.to B')).symm.trans (category.id_comp _), end } /-- If each structured arrow category on `G` has an initial object, construct a left adjoint to `G`. It is shown that it is a left adjoint in `adjunction_of_structured_arrow_initials`. -/ def left_adjoint_of_structured_arrow_initials : C ⥤ D := adjunction.left_adjoint_of_equiv (left_adjoint_of_structured_arrow_initials_aux G) (λ _ _, by simp) /-- If each structured arrow category on `G` has an initial object, we have a constructed left adjoint to `G`. -/ def adjunction_of_structured_arrow_initials : left_adjoint_of_structured_arrow_initials G ⊣ G := adjunction.adjunction_of_equiv_left _ _ /-- If each structured arrow category on `G` has an initial object, `G` is a right adjoint. -/ def is_right_adjoint_of_structured_arrow_initials : is_right_adjoint G := { left := _, adj := adjunction_of_structured_arrow_initials G } end of_initials section of_terminals variables [∀ A, has_terminal (costructured_arrow G A)] /-- Implementation: If each costructured arrow category on `G` has a terminal object, an equivalence which is helpful for constructing a right adjoint to `G`. -/ @[simps] def right_adjoint_of_costructured_arrow_terminals_aux (B : D) (A : C) : (G.obj B ⟶ A) ≃ (B ⟶ (⊤_ (costructured_arrow G A)).left) := { to_fun := λ g, comma_morphism.left (terminal.from (costructured_arrow.mk g)), inv_fun := λ g, G.map g ≫ (⊤_ (costructured_arrow G A)).hom, left_inv := by tidy, right_inv := λ g, begin let B' : costructured_arrow G A := costructured_arrow.mk (G.map g ≫ (⊤_ (costructured_arrow G A)).hom), let g' : B' ⟶ ⊤_ (costructured_arrow G A) := costructured_arrow.hom_mk g rfl, have : terminal.from _ = g', { apply limit.hom_ext, rintro ⟨⟨⟩⟩ }, change comma_morphism.left (terminal.from B') = _, rw this, refl end } /-- If each costructured arrow category on `G` has a terminal object, construct a right adjoint to `G`. It is shown that it is a right adjoint in `adjunction_of_structured_arrow_initials`. -/ def right_adjoint_of_costructured_arrow_terminals : C ⥤ D := adjunction.right_adjoint_of_equiv (right_adjoint_of_costructured_arrow_terminals_aux G) (λ B₁ B₂ A f g, by { rw ←equiv.eq_symm_apply, simp }) /-- If each costructured arrow category on `G` has a terminal object, we have a constructed right adjoint to `G`. -/ def adjunction_of_costructured_arrow_terminals : G ⊣ right_adjoint_of_costructured_arrow_terminals G := adjunction.adjunction_of_equiv_right _ _ /-- If each costructured arrow category on `G` has an terminal object, `G` is a left adjoint. -/ def is_right_adjoint_of_costructured_arrow_terminals : is_left_adjoint G := { right := right_adjoint_of_costructured_arrow_terminals G, adj := adjunction.adjunction_of_equiv_right _ _ } end of_terminals section variables {F : C ⥤ D} local attribute [tidy] tactic.discrete_cases /-- Given a left adjoint to `G`, we can construct an initial object in each structured arrow category on `G`. -/ def mk_initial_of_left_adjoint (h : F ⊣ G) (A : C) : is_initial (structured_arrow.mk (h.unit.app A) : structured_arrow A G) := { desc := λ B, structured_arrow.hom_mk ((h.hom_equiv _ _).symm B.X.hom) (by tidy), uniq' := λ s m w, begin ext, dsimp, rw [equiv.eq_symm_apply, adjunction.hom_equiv_unit], apply structured_arrow.w m, end } /-- Given a right adjoint to `F`, we can construct a terminal object in each costructured arrow category on `F`. -/ def mk_terminal_of_right_adjoint (h : F ⊣ G) (A : D) : is_terminal (costructured_arrow.mk (h.counit.app A) : costructured_arrow F A) := { lift := λ B, costructured_arrow.hom_mk (h.hom_equiv _ _ B.X.hom) (by tidy), uniq' := λ s m w, begin ext, dsimp, rw [h.eq_hom_equiv_apply, adjunction.hom_equiv_counit], exact costructured_arrow.w m, end } end end category_theory
ad5e4e7e486f38d043c93f29caca9b3fbcd5b925
0b3933727d99a2f351f5dbe6e24716bb786a6649
/src/sesh/mult.lean
186f05e9994fab344d76bba900516ea47f05f847
[]
no_license
Vtec234/lean-sesh
1e770858215279f65aba92b4782b483ab4f09353
d11d7bb0599406e27d3a4d26242aec13d639ecf7
refs/heads/master
1,587,497,515,696
1,558,362,223,000
1,558,362,223,000
169,809,439
2
0
null
null
null
null
UTF-8
Lean
false
false
3,245
lean
/- Introduces a commutative semiring of zero-one-many. -/ import metastuff @[derive decidable_eq] inductive mult: Type /- Zero represents assumptions which cannot be computed with, but could be used for typing, were I to implement dependent types in the embedded language. Since I didn't, zero is basically useless. -/ | Zero: mult /- A one-multiplicity assumption can be used once in computation, i.e is linear. -/ | One: mult /- A many-multiplicity assumption is non-linear and can be used multiple times. -/ | Many: mult notation `ω` := mult.Many namespace mult instance : has_zero mult := ⟨mult.Zero⟩ instance : has_one mult := ⟨mult.One⟩ protected def add: mult → mult → mult | 0 π := π | π 0 := π | 1 1 := ω | ω _ := ω | _ ω := ω instance : has_add mult := ⟨mult.add⟩ /- addition makes a commutative monoid -/ @[simp] lemma zero_add (π: mult) : 0 + π = π := by { cases π; refl } @[simp] lemma add_zero (π: mult) : π + 0 = π := by { cases π; refl } lemma zeros_left {π₁ π₂: mult} (h: π₁ + π₂ = 0) : π₁ = 0 := begin cases π₁; cases π₂; cases h, refl end lemma zeros_right {π₁ π₂: mult} (h: π₁ + π₂ = 0) : π₂ = 0 := begin cases π₁; cases π₂; cases h, refl end lemma add_comm (π₁ π₂: mult) : π₁ + π₂ = π₂ + π₁ := by { cases π₁; cases π₂; refl } lemma add_assoc (π₁ π₂ π₃: mult) : π₁ + π₂ + π₃ = π₁ + (π₂ + π₃) := by { cases π₁; cases π₂; cases π₃; refl } instance : add_comm_monoid mult := { add := mult.add, zero := mult.Zero, zero_add := zero_add, add_zero := add_zero, add_comm := add_comm, add_assoc := add_assoc } protected def mul: mult → mult → mult | _ 0 := 0 | 0 _ := 0 | π 1 := π | _ ω := ω instance : has_mul mult := ⟨mult.mul⟩ /- multiplication makes a commutative monoid -/ @[simp] lemma one_mul (π: mult) : 1 * π = π := by { cases π; refl } @[simp] lemma mul_one (π: mult) : π * 1 = π := by { cases π; refl } lemma mul_comm (π₁ π₂: mult) : π₁ * π₂ = π₂ * π₁ := by { cases π₁; cases π₂; refl } lemma mul_assoc (π₁ π₂ π₃: mult) : π₁ * π₂ * π₃ = π₁ * (π₂ * π₃) := by { cases π₁; cases π₂; cases π₃; refl } instance : comm_monoid mult := { mul := mult.mul, one := mult.One, one_mul := one_mul, mul_one := mul_one, mul_comm := mul_comm, mul_assoc := mul_assoc } /- mult with + and * makes a commutative semiring -/ @[simp] lemma zero_mul (π: mult) : 0 * π = 0 := by { cases π; refl } @[simp] lemma mul_zero (π: mult) : π * 0 = 0 := by { cases π; refl } @[sop_form] lemma left_distrib (π₁ π₂ π₃: mult) : π₁ * (π₂ + π₃) = π₁ * π₂ + π₁ * π₃ := by { cases π₁; cases π₂; cases π₃; refl } @[sop_form] lemma right_distrib (π₁ π₂ π₃: mult) : (π₁ + π₂) * π₃ = π₁ * π₃ + π₂ * π₃ := by { cases π₁; cases π₂; cases π₃; refl } instance : comm_semiring mult := { zero_mul := zero_mul, mul_zero := mul_zero, left_distrib := left_distrib, right_distrib := right_distrib, ..mult.add_comm_monoid, ..mult.comm_monoid } end mult
b5ddb89a63961a5b7a8538c7086156ec31c3861d
ebf7140a9ea507409ff4c994124fa36e79b4ae35
/src/exercises_sources/wednesday/topological_spaces.lean
5af6a63e7634df396fdabe276d4d4bd960b91499
[]
no_license
fundou/lftcm2020
3e88d58a92755ea5dd49f19c36239c35286ecf5e
99d11bf3bcd71ffeaef0250caa08ecc46e69b55b
refs/heads/master
1,685,610,799,304
1,624,070,416,000
1,624,070,416,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
15,052
lean
import tactic import data.set.finite import data.real.basic -- for metrics /- # (Re)-Building topological spaces in Lean Mathlib has a large library of results on topological spaces, including various constructions, separation axioms, Tychonoff's theorem, sheaves, Stone-Čech compactification, Heine-Cantor, to name but a few. See https://leanprover-community.github.io/theories/topology.html which for a (subset) of what's in library. But today we will ignore all that, and build our own version of topological spaces from scratch! (On Friday morning Patrick Massot will lead a session exploring the existing mathlib library in more detail) To get this file run either `leanproject get lftcm2020`, if you didn't already or cd to that folder and run `git pull; leanproject get-mathlib-cache`, this is `src/exercise_sources/wednesday/topological_spaces.lean`. The exercises are spread throughout, you needn't do them in order! They are marked as short, medium and long, so I suggest you try some short ones first. First a little setup, we will be making definitions involving the real numbers, the theory of which is not computable, and we'll use sets. -/ noncomputable theory open set /-! ## What is a topological space: There are many definitions: one from Wikipedia: A topological space is an ordered pair (X, τ), where X is a set and τ is a collection of subsets of X, satisfying the following axioms: - The empty set and X itself belong to τ. - Any arbitrary (finite or infinite) union of members of τ still belongs to τ. - The intersection of any finite number of members of τ still belongs to τ. We can formalize this as follows: -/ class topological_space_wiki := (X : Type) -- the underlying Type that the topology will be on (τ : set (set X)) -- the set of open subsets of X (empty_mem : ∅ ∈ τ) -- empty set is open (univ_mem : univ ∈ τ) -- whole space is open (union : ∀ B ⊆ τ, ⋃₀ B ∈ τ) -- arbitrary unions (sUnions) of members of τ are open (inter : ∀ (B ⊆ τ) (h : finite B), ⋂₀ B ∈ τ) -- finite intersections of -- members of τ are open /- Before we go on we should be sure we want to use this as our definition. -/ @[ext] class topological_space (X : Type) := (is_open : set X → Prop) -- why set X → Prop not set (set X)? former plays -- nicer with typeclasses later (empty_mem : is_open ∅) (univ_mem : is_open univ) (union : ∀ (B : set (set X)) (h : ∀ b ∈ B, is_open b), is_open (⋃₀ B)) (inter : ∀ (A B : set X) (hA : is_open A) (hB : is_open B), is_open (A ∩ B)) namespace topological_space /- We can now work with topological spaces like this. -/ example (X : Type) [topological_space X] (U V W : set X) (hU : is_open U) (hV : is_open V) (hW : is_open W) : is_open (U ∩ V ∩ W) := begin apply inter _ _ _ hW, exact inter _ _ hU hV, end /- ## Exercise 0 [short]: One of the axioms of a topological space we have here is unnecessary, it follows from the others. If we remove it we'll have less work to do each time we want to create a new topological space so: 1. Identify and remove the unneeded axiom, make sure to remove it throughout the file. 2. Add the axiom back as a lemma with the same name and prove it based on the others, so that the _interface_ is the same. -/ /- Defining a basic topology now works like so: -/ def discrete (X : Type) : topological_space X := { is_open := λ U, true, -- everything is open empty_mem := trivial, univ_mem := trivial, union := begin intros B h, trivial, end, inter := begin intros A hA B hB, trivial, end } /- ## Exercise 1 [medium]: One way me might want to create topological spaces in practice is to take the coarsest possible topological space containing a given set of is_open. To define this we might say we want to define what `is_open` is given the set of generators. So we want to define the predicate `is_open` by declaring that each generator will be open, the intersection of two opens will be open, and each union of a set of opens will be open, and finally the empty and whole space (`univ`) must be open. The cleanest way to do this is as an inductive definition. The exercise is to make this definition of the topological space generated by a given set in Lean. ### Hint: As a hint for this exercise take a look at the following definition of a constructible set of a topological space, defined by saying that an intersection of an open and a closed set is constructible and that the union of any pair of constructible sets is constructible. (Bonus exercise: mathlib doesn't have any theory of constructible sets, make one and PR it! [arbitrarily long!], or just prove that open and closed sets are constructible for now) -/ inductive is_constructible {X : Type} (T : topological_space X) : set X → Prop /- Given two open sets in `T`, the intersection of one with the complement of the other open is locally closed, hence constructible: -/ | locally_closed : ∀ (A B : set X), is_open A → is_open B → is_constructible (A ∩ Bᶜ) -- Given two constructible sets their union is constructible: | union : ∀ A B, is_constructible A → is_constructible B → is_constructible (A ∪ B) -- For example we can now use this definition to prove the empty set is constructible example {X : Type} (T : topological_space X) : is_constructible T ∅ := begin -- The intersection of the whole space (open) with the empty set (closed) is -- locally closed, hence constructible have := is_constructible.locally_closed univ univ T.univ_mem T.univ_mem, -- but simp knows that's just the empty set (`simp` uses `this` automatically) simpa, end /-- The open sets of the least topology containing a collection of basic sets. -/ inductive generated_open (X : Type) (g : set (set X)) : set X → Prop -- The exercise: Add a definition here defining which sets are generated by `g` like the -- `is_constructible` definition above. /-- The smallest topological space containing the collection `g` of basic sets -/ def generate_from (X : Type) (g : set (set X)) : topological_space X := { is_open := generated_open X g, empty_mem := sorry, univ_mem := sorry, inter := sorry, union := sorry } /- ## Exercise 2 [short]: Define the indiscrete topology on any type using this. (To do it without this it is surprisingly fiddly to prove that the set `{∅, univ}` actually forms a topology) -/ def indiscrete (X : Type) : topological_space X := sorry end topological_space open topological_space /- Now it is quite easy to give a topology on the product of a pair of topological spaces. -/ instance prod.topological_space (X Y : Type) [topological_space X] [topological_space Y] : topological_space (X × Y) := topological_space.generate_from (X × Y) {U | ∃ (Ux : set X) (Uy : set Y) (hx : is_open Ux) (hy : is_open Uy), U = set.prod Ux Uy} -- the proof of this is bit long so I've left it out for the purpose of this file! lemma is_open_prod_iff (X Y : Type) [topological_space X] [topological_space Y] {s : set (X × Y)} : is_open s ↔ (∀a b, (a, b) ∈ s → ∃u v, is_open u ∧ is_open v ∧ a ∈ u ∧ b ∈ v ∧ set.prod u v ⊆ s) := sorry /- # Metric spaces -/ open_locale big_operators class metric_space_basic (X : Type) := (dist : X → X → ℝ) (dist_eq_zero_iff : ∀ x y, dist x y = 0 ↔ x = y) (dist_symm : ∀ x y, dist x y = dist y x) (triangle : ∀ x y z, dist x z ≤ dist x y + dist y z) namespace metric_space_basic open topological_space /- ## Exercise 3 [short]: We have defined a metric space with a metric landing in ℝ, and made no mention of nonnegativity, (this is in line with the philosophy of using the easiest axioms for our definitions as possible, to make it easier to define individual metrics). Show that we really did define the usual notion of metric space. -/ lemma dist_nonneg {X : Type} [metric_space_basic X] (x y : X) : 0 ≤ dist x y := sorry /- From a metric space we get an induced topological space structure like so: -/ instance {X : Type} [metric_space_basic X] : topological_space X := generate_from X { B | ∃ (x : X) r, B = {y | dist x y < r} } end metric_space_basic open metric_space_basic /- So far so good, now lets define the product of two metric spaces: ## Exercise 4 [medium]: Fill in the proofs here. Hint: the computer can do boring casework you would never dream of in real life. `max` is defined as `if x < y then y else x` and the `split_ifs` tactic will break apart if statements. -/ instance prod.metric_space_basic (X Y : Type) [metric_space_basic X] [metric_space_basic Y] : metric_space_basic (X × Y) := { dist := λ u v, max (dist u.fst v.fst) (dist u.snd v.snd), dist_eq_zero_iff := sorry , dist_symm := sorry, triangle := sorry } /- ☡ Let's try to prove a simple lemma involving the product topology: ☡ -/ set_option trace.type_context.is_def_eq false example (X : Type) [metric_space_basic X] : is_open {xy : X × X | dist xy.fst xy.snd < 100 } := begin -- rw is_open_prod_iff X X, -- this fails, why? Because we have two subtly different topologies on the product -- they are equal but the proof that they are equal is nontrivial and the -- typeclass mechanism can't see that they automatically to apply. We need to change -- our set-up. sorry, end /- Note that lemma works fine when there is only one topology involved. -/ lemma diag_closed (X : Type) [topological_space X] : is_open {xy : X × X | xy.fst ≠ xy.snd } := begin rw is_open_prod_iff X X, sorry, -- Don't try and fill this in: see below! end /- ## Exercise 5 [short]: The previous lemma isn't true! It requires a separation axiom. Define a `class` that posits that the topology on a type `X` satisfies this axiom. Mathlib uses `T_i` naming scheme for these axioms. -/ class t2_space (X : Type) [topological_space X] := (t2 : sorry) /- (Bonus exercises [medium], the world is your oyster: prove the correct version of the above lemma `diag_closed`, prove that the discrete topology is t2, or that any metric topology is t2, ). -/ /- Let's fix the broken example from earlier, by redefining the topology on a metric space. We have unfortunately created two topologies on `X × Y`, one via `prod.topology` that we defined earlier as the product of the two topologies coming from the respective metric space structures. And one coming from the metric on the product. These are equal, i.e. the same topology (otherwise mathematically the product would not be a good definition). However they are not definitionally equal, there is as nontrivial proof to show they are the same. The typeclass system (which finds the relevant topological space instance when we use lemmas involving topological spaces) isn't able to check that topological space structures which are equal for some nontrivial reason are equal on the fly so it gets stuck. We can use `extends` to say that a metric space is an extra structure on top of being a topological space so we are making a choice of topology for each metric space. This may not be *definitionally* equal to the induced topology, but we should add the axiom that the metric and the topology are equal to stop us from creating a metric inducing a different topology to the topological structure we chose. -/ class metric_space (X : Type) extends topological_space X, metric_space_basic X := (compatible : ∀ U, is_open U ↔ generated_open X { B | ∃ (x : X) r, B = {y | dist x y < r}} U) namespace metric_space open topological_space /- This might seem a bit inconvenient to have to define a topological space each time we want a metric space. We would still like a way of making a `metric_space` just given a metric and some properties it satisfies, i.e. a `metric_space_basic`, so we should setup a metric space constructor from a `metric_space_basic` by setting the topology to be the induced one. -/ def of_basic {X : Type} (m : metric_space_basic X) : metric_space X := { compatible := begin intros, refl, /- this should work when the above parts are complete -/ end, ..m, ..@metric_space_basic.topological_space X m } /- Now lets define the product of two metric spaces properly -/ instance {X Y : Type} [metric_space X] [metric_space Y] : metric_space (X × Y) := { compatible := begin -- Let's not fill this in for the demo, let me know if you do it! sorry end, ..prod.topological_space X Y, ..prod.metric_space_basic X Y, } /- unregister the bad instance we defined earlier -/ local attribute [-instance] metric_space_basic.topological_space /- Now this will work, there is only one topological space on the product, we can rewrite like we tried to before a lemma about topologies our result on metric spaces, as there is only one topology here. ## Exercise 6 [long?]: Complete the proof of the example (you can generalise the 100 too if it makes it feel less silly). -/ example (X : Type) [metric_space X] : is_open {xy : X × X | dist xy.fst xy.snd < 100 } := begin rw is_open_prod_iff X X, sorry end end metric_space namespace topological_space /- As mentioned, there are many definitions of a topological space, for instance one can define them via specifying a set of closed sets satisfying various axioms, this is equivalent and sometimes more convenient. We _could_ create two distinct Types defined by different data and provide an equivalence between theses types, e.g. `topological_space_via_open_sets` and `topological_space_via_closed_sets`, but this would quickly get unwieldy. What's better is to make an alternative _constructor_ for our original topological space. This is a function takes a set of subsets satisfying the axioms to be the closed sets of a topological space and creates the topological space defined by the corresponding set of open sets. ## Exercise 7 [medium]: Complete the following constructor of a topological space from a set of subsets of a given type `X` satisfying the axioms for the closed sets of a topology. Hint: there are many useful lemmas about complements in mathlib, with names involving `compl`, like `compl_empty`, `compl_univ`, `compl_compl`, `compl_sUnion`, `mem_compl_image`, `compl_inter`, `compl_compl'`, `you can #check them to see what they say. -/ def mk_closed_sets (X : Type) (σ : set (set X)) (empty_mem : ∅ ∈ σ) (univ_mem : univ ∈ σ) (inter : ∀ B ⊆ σ, ⋂₀ B ∈ σ) (union : ∀ (A ∈ σ) (B ∈ σ), A ∪ B ∈ σ) : topological_space X := { is_open := λ U, U ∈ compl '' σ, -- the corresponding `is_open` empty_mem := sorry , univ_mem := sorry , union := sorry , inter := sorry } /- Here are some more exercises: ## Exercise 8 [medium/long]: Define the cofinite topology on any type (PR it to mathlib?). ## Exercise 9 [medium/long]: Define a normed space? ## Exercise 10 [medium/long]: Define more separation axioms? -/ end topological_space
9b1f55ad76288ff810ff9d473ed2ca2c53a64a96
874a8d2247ab9a4516052498f80da2e32d0e3a48
/triIneq_v2.lean
2592f0762129b4648aa22e14a81c47f838c84015
[]
no_license
AlexKontorovich/Spring2020Math492
378b36c643ee029f5ab91c1677889baa591f5e85
659108c5d864ff5c75b9b3b13b847aa5cff4348a
refs/heads/master
1,610,780,595,457
1,588,174,859,000
1,588,174,859,000
243,017,788
0
1
null
null
null
null
UTF-8
Lean
false
false
421
lean
import tactic import data.real.basic universe u --local attribute [instance] classical.prop_decidable noncomputable def absVal (a : ℝ) : ℝ := if a < 0 then -a else a theorem triIneqInt (a : ℝ) (b : ℝ) : (absVal(b - a) ≤ absVal(a) + absVal(b)) := begin repeat {rw absVal}, split_ifs, repeat {linarith}, end def absValAlt : ℤ → ℕ | (int.of_nat n) := n | (int.neg_succ_of_nat n) := n + 1
8ef5dc472da2a899245b447e54ed2684622fe438
947b78d97130d56365ae2ec264df196ce769371a
/src/Std/Data/Stack.lean
7a8c0d7c227c3ccf4782ba3c74b4b6d3546d7f60
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
886
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Daniel Selsam Simple stack API implemented using an array. -/ namespace Std universes u v w structure Stack (α : Type u) := (vals : Array α := #[]) namespace Stack variable {α : Type u} def empty : Stack α := {} def isEmpty (s : Stack α) : Bool := s.vals.isEmpty def push (v : α) (s : Stack α) : Stack α := { s with vals := s.vals.push v } def peek? (s : Stack α) : Option α := if s.vals.isEmpty then none else s.vals.get? (s.vals.size-1) def peek! [Inhabited α] (s : Stack α) : α := s.vals.back def pop [Inhabited α] (s : Stack α) : Stack α := { s with vals := s.vals.pop } def modify [Inhabited α] (s : Stack α) (f : α → α) : Stack α := { s with vals := s.vals.modify (s.vals.size-1) f } end Stack end Std
f5984fdcc5e07d532fc128aac6d898037a57224f
9c1ad797ec8a5eddb37d34806c543602d9a6bf70
/products/switch.lean
ed2741276988946c9bcc8cf64e88a9720c046647
[]
no_license
timjb/lean-category-theory
816eefc3a0582c22c05f4ee1c57ed04e57c0982f
12916cce261d08bb8740bc85e0175b75fb2a60f4
refs/heads/master
1,611,078,926,765
1,492,080,000,000
1,492,080,000,000
88,348,246
0
0
null
1,492,262,499,000
1,492,262,498,000
null
UTF-8
Lean
false
false
750
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Stephen Morgan, Scott Morrison import .products open tqft.categories open tqft.categories.functor open tqft.categories.natural_transformation namespace tqft.categories.products definition SwitchProductCategory ( C D : Category ) : Functor (C × D) (D × C) := { onObjects := λ X, (X.snd, X.fst), onMorphisms := λ _ _ f, (f.snd, f.fst), identities := ♮, functoriality := ♮ } definition SwitchSymmetry ( C D : Category ) : NaturalIsomorphism (FunctorComposition (SwitchProductCategory C D) (SwitchProductCategory D C)) (IdentityFunctor (C × D)) := ♯ end tqft.categories.products
3eb3cf124fab8e4e41f6236d8e20fbf6f6d0fd4f
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/nat/choose/default_auto.lean
60458a1d13025fb3e8565c1e7426725342c77ff5
[]
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
185
lean
import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.nat.choose.dvd import Mathlib.data.nat.choose.sum import Mathlib.PostPort namespace Mathlib end Mathlib
232a5ef2d6844a411c9fdc472abfeae86bd43d48
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/group_theory/group_action/group.lean
a49f86283dbe1aa4bc034d8ce97f0537e6f0f40f
[ "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
9,227
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.equiv.mul_add_aut import group_theory.group_action.units /-! # Group actions applied to various types of group This file contains lemmas about `smul` on `group_with_zero`, and `group`. -/ universes u v w variables {α : Type u} {β : Type v} {γ : Type w} section mul_action /-- `monoid.to_mul_action` is faithful on cancellative monoids. -/ @[to_additive /-" `add_monoid.to_add_action` is faithful on additive cancellative monoids. "-/] instance right_cancel_monoid.to_has_faithful_scalar [right_cancel_monoid α] : has_faithful_scalar α α := ⟨λ x y h, mul_right_cancel (h 1)⟩ section group variables [group α] [mul_action α β] @[simp, to_additive] lemma inv_smul_smul (c : α) (x : β) : c⁻¹ • c • x = x := by rw [smul_smul, mul_left_inv, one_smul] @[simp, to_additive] lemma smul_inv_smul (c : α) (x : β) : c • c⁻¹ • x = x := by rw [smul_smul, mul_right_inv, one_smul] /-- Given an action of a group `α` on `β`, each `g : α` defines a permutation of `β`. -/ @[to_additive, simps] def mul_action.to_perm (a : α) : equiv.perm β := ⟨λ x, a • x, λ x, a⁻¹ • x, inv_smul_smul a, smul_inv_smul a⟩ /-- Given an action of an additive group `α` on `β`, each `g : α` defines a permutation of `β`. -/ add_decl_doc add_action.to_perm /-- `mul_action.to_perm` is injective on faithful actions. -/ @[to_additive] lemma mul_action.to_perm_injective [has_faithful_scalar α β] : function.injective (mul_action.to_perm : α → equiv.perm β) := (show function.injective (equiv.to_fun ∘ mul_action.to_perm), from smul_left_injective').of_comp variables (α) (β) /-- Given an action of a group `α` on a set `β`, each `g : α` defines a permutation of `β`. -/ @[simps] def mul_action.to_perm_hom : α →* equiv.perm β := { to_fun := mul_action.to_perm, map_one' := equiv.ext $ one_smul α, map_mul' := λ u₁ u₂, equiv.ext $ mul_smul (u₁:α) u₂ } /-- Given an action of a additive group `α` on a set `β`, each `g : α` defines a permutation of `β`. -/ @[simps] def add_action.to_perm_hom (α : Type*) [add_group α] [add_action α β] : α →+ additive (equiv.perm β) := { to_fun := λ a, additive.of_mul $ add_action.to_perm a, map_zero' := equiv.ext $ zero_vadd α, map_add' := λ a₁ a₂, equiv.ext $ add_vadd a₁ a₂ } /-- The tautological action by `equiv.perm α` on `α`. This generalizes `function.End.apply_mul_action`.-/ instance equiv.perm.apply_mul_action (α : Type*) : mul_action (equiv.perm α) α := { smul := λ f a, f a, one_smul := λ _, rfl, mul_smul := λ _ _ _, rfl } @[simp] protected lemma equiv.perm.smul_def {α : Type*} (f : equiv.perm α) (a : α) : f • a = f a := rfl /-- `equiv.perm.apply_mul_action` is faithful. -/ instance equiv.perm.apply_has_faithful_scalar (α : Type*) : has_faithful_scalar (equiv.perm α) α := ⟨λ x y, equiv.ext⟩ variables {α} {β} @[to_additive] lemma inv_smul_eq_iff {a : α} {x y : β} : a⁻¹ • x = y ↔ x = a • y := (mul_action.to_perm a).symm_apply_eq @[to_additive] lemma eq_inv_smul_iff {a : α} {x y : β} : x = a⁻¹ • y ↔ a • x = y := (mul_action.to_perm a).eq_symm_apply lemma smul_inv [group β] [smul_comm_class α β β] [is_scalar_tower α β β] (c : α) (x : β) : (c • x)⁻¹ = c⁻¹ • x⁻¹ := by rw [inv_eq_iff_mul_eq_one, smul_mul_smul, mul_right_inv, mul_right_inv, one_smul] lemma smul_gpow [group β] [smul_comm_class α β β] [is_scalar_tower α β β] (c : α) (x : β) (p : ℤ) : (c • x) ^ p = c ^ p • x ^ p := by { cases p; simp [smul_pow, smul_inv] } @[to_additive] protected lemma mul_action.bijective (g : α) : function.bijective (λ b : β, g • b) := (mul_action.to_perm g).bijective @[to_additive] protected lemma mul_action.injective (g : α) : function.injective (λ b : β, g • b) := (mul_action.bijective g).injective @[to_additive] lemma smul_left_cancel (g : α) {x y : β} (h : g • x = g • y) : x = y := mul_action.injective g h @[simp, to_additive] lemma smul_left_cancel_iff (g : α) {x y : β} : g • x = g • y ↔ x = y := (mul_action.injective g).eq_iff @[to_additive] lemma smul_eq_iff_eq_inv_smul (g : α) {x y : β} : g • x = y ↔ x = g⁻¹ • y := (mul_action.to_perm g).apply_eq_iff_eq_symm_apply end group /-- `monoid.to_mul_action` is faithful on nontrivial cancellative monoids with zero. -/ instance cancel_monoid_with_zero.to_has_faithful_scalar [cancel_monoid_with_zero α] [nontrivial α] : has_faithful_scalar α α := ⟨λ x y h, mul_left_injective₀ one_ne_zero (h 1)⟩ section gwz variables [group_with_zero α] [mul_action α β] @[simp] lemma inv_smul_smul₀ {c : α} (hc : c ≠ 0) (x : β) : c⁻¹ • c • x = x := inv_smul_smul (units.mk0 c hc) x @[simp] lemma smul_inv_smul₀ {c : α} (hc : c ≠ 0) (x : β) : c • c⁻¹ • x = x := smul_inv_smul (units.mk0 c hc) x lemma inv_smul_eq_iff₀ {a : α} (ha : a ≠ 0) {x y : β} : a⁻¹ • x = y ↔ x = a • y := (mul_action.to_perm (units.mk0 a ha)).symm_apply_eq lemma eq_inv_smul_iff₀ {a : α} (ha : a ≠ 0) {x y : β} : x = a⁻¹ • y ↔ a • x = y := (mul_action.to_perm (units.mk0 a ha)).eq_symm_apply end gwz end mul_action section distrib_mul_action section group variables [group α] [add_monoid β] [distrib_mul_action α β] variables (β) /-- Each element of the group defines an additive monoid isomorphism. This is a stronger version of `mul_action.to_perm`. -/ @[simps {simp_rhs := tt}] def distrib_mul_action.to_add_equiv (x : α) : β ≃+ β := { .. distrib_mul_action.to_add_monoid_hom β x, .. mul_action.to_perm_hom α β x } variables (α β) /-- Each element of the group defines an additive monoid isomorphism. This is a stronger version of `mul_action.to_perm_hom`. -/ @[simps] def distrib_mul_action.to_add_aut : α →* add_aut β := { to_fun := distrib_mul_action.to_add_equiv β, map_one' := add_equiv.ext (one_smul _), map_mul' := λ a₁ a₂, add_equiv.ext (mul_smul _ _) } variables {α β} theorem smul_eq_zero_iff_eq (a : α) {x : β} : a • x = 0 ↔ x = 0 := ⟨λ h, by rw [← inv_smul_smul a x, h, smul_zero], λ h, h.symm ▸ smul_zero _⟩ theorem smul_ne_zero_iff_ne (a : α) {x : β} : a • x ≠ 0 ↔ x ≠ 0 := not_congr $ smul_eq_zero_iff_eq a end group section gwz variables [group_with_zero α] [add_monoid β] [distrib_mul_action α β] theorem smul_eq_zero_iff_eq' {a : α} (ha : a ≠ 0) {x : β} : a • x = 0 ↔ x = 0 := smul_eq_zero_iff_eq (units.mk0 a ha) theorem smul_ne_zero_iff_ne' {a : α} (ha : a ≠ 0) {x : β} : a • x ≠ 0 ↔ x ≠ 0 := smul_ne_zero_iff_ne (units.mk0 a ha) end gwz end distrib_mul_action section mul_distrib_mul_action variables [group α] [monoid β] [mul_distrib_mul_action α β] variables (β) /-- Each element of the group defines a multiplicative monoid isomorphism. This is a stronger version of `mul_action.to_perm`. -/ @[simps {simp_rhs := tt}] def mul_distrib_mul_action.to_mul_equiv (x : α) : β ≃* β := { .. mul_distrib_mul_action.to_monoid_hom β x, .. mul_action.to_perm_hom α β x } variables (α β) /-- Each element of the group defines an multiplicative monoid isomorphism. This is a stronger version of `mul_action.to_perm_hom`. -/ @[simps] def mul_distrib_mul_action.to_mul_aut : α →* mul_aut β := { to_fun := mul_distrib_mul_action.to_mul_equiv β, map_one' := mul_equiv.ext (one_smul _), map_mul' := λ a₁ a₂, mul_equiv.ext (mul_smul _ _) } variables {α β} end mul_distrib_mul_action section arrow /-- If `G` acts on `A`, then it acts also on `A → B`, by `(g • F) a = F (g⁻¹ • a)`. -/ @[simps] def arrow_action {G A B : Type*} [group G] [mul_action G A] : mul_action G (A → B) := { smul := λ g F a, F (g⁻¹ • a), one_smul := by { intro, simp only [one_inv, one_smul] }, mul_smul := by { intros, simp only [mul_smul, mul_inv_rev] } } local attribute [instance] arrow_action /-- When `B` is a monoid, `arrow_action` is additionally a `mul_distrib_mul_action`. -/ def arrow_mul_distrib_mul_action {G A B : Type*} [group G] [mul_action G A] [monoid B] : mul_distrib_mul_action G (A → B) := { smul_one := λ g, rfl, smul_mul := λ g f₁ f₂, rfl } local attribute [instance] arrow_mul_distrib_mul_action /-- Given groups `G H` with `G` acting on `A`, `G` acts by multiplicative automorphisms on `A → H`. -/ @[simps] def mul_aut_arrow {G A H} [group G] [mul_action G A] [monoid H] : G →* mul_aut (A → H) := mul_distrib_mul_action.to_mul_aut _ _ end arrow namespace is_unit section mul_action variables [monoid α] [mul_action α β] @[to_additive] lemma smul_left_cancel {a : α} (ha : is_unit a) {x y : β} : a • x = a • y ↔ x = y := let ⟨u, hu⟩ := ha in hu ▸ smul_left_cancel_iff u end mul_action section distrib_mul_action variables [monoid α] [add_monoid β] [distrib_mul_action α β] @[simp] theorem smul_eq_zero {u : α} (hu : is_unit u) {x : β} : u • x = 0 ↔ x = 0 := exists.elim hu $ λ u hu, hu ▸ smul_eq_zero_iff_eq u end distrib_mul_action end is_unit
a400510ccf1857df26187f6d32863d74e0c6eadd
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/analysis/special_functions/pow.lean
565353336a79c4a64ed3a403a103b512db12f85c
[ "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
60,871
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, Sébastien Gouëzel, Rémy Degenne -/ import analysis.special_functions.complex.log /-! # Power function on `ℂ`, `ℝ`, `ℝ≥0`, and `ℝ≥0∞` We construct the power functions `x ^ y` where * `x` and `y` are complex numbers, * or `x` and `y` are real numbers, * or `x` is a nonnegative real number and `y` is a real number; * or `x` is a number from `[0, +∞]` (a.k.a. `ℝ≥0∞`) and `y` is a real number. We also prove basic properties of these functions. -/ noncomputable theory open_locale classical real topological_space nnreal ennreal filter open filter namespace complex /-- The complex power function `x^y`, given by `x^y = exp(y log x)` (where `log` is the principal determination of the logarithm), unless `x = 0` where one sets `0^0 = 1` and `0^y = 0` for `y ≠ 0`. -/ noncomputable def cpow (x y : ℂ) : ℂ := if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) noncomputable instance : has_pow ℂ ℂ := ⟨cpow⟩ @[simp] lemma cpow_eq_pow (x y : ℂ) : cpow x y = x ^ y := rfl lemma cpow_def (x y : ℂ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) := rfl lemma cpow_def_of_ne_zero {x : ℂ} (hx : x ≠ 0) (y : ℂ) : x ^ y = exp (log x * y) := if_neg hx @[simp] lemma cpow_zero (x : ℂ) : x ^ (0 : ℂ) = 1 := by simp [cpow_def] @[simp] lemma cpow_eq_zero_iff (x y : ℂ) : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := by { simp only [cpow_def], split_ifs; simp [*, exp_ne_zero] } @[simp] lemma zero_cpow {x : ℂ} (h : x ≠ 0) : (0 : ℂ) ^ x = 0 := by simp [cpow_def, *] lemma zero_cpow_eq_iff {x : ℂ} {a : ℂ} : 0 ^ x = a ↔ (x ≠ 0 ∧ a = 0) ∨ (x = 0 ∧ a = 1) := begin split, { intros hyp, simp [cpow_def] at hyp, by_cases x = 0, { subst h, simp only [if_true, eq_self_iff_true] at hyp, right, exact ⟨rfl, hyp.symm⟩}, { rw if_neg h at hyp, left, exact ⟨h, hyp.symm⟩, }, }, { rintro (⟨h, rfl⟩|⟨rfl,rfl⟩), { exact zero_cpow h, }, { exact cpow_zero _, }, }, end lemma eq_zero_cpow_iff {x : ℂ} {a : ℂ} : a = 0 ^ x ↔ (x ≠ 0 ∧ a = 0) ∨ (x = 0 ∧ a = 1) := by rw [←zero_cpow_eq_iff, eq_comm] @[simp] lemma cpow_one (x : ℂ) : x ^ (1 : ℂ) = x := if hx : x = 0 then by simp [hx, cpow_def] else by rw [cpow_def, if_neg (one_ne_zero : (1 : ℂ) ≠ 0), if_neg hx, mul_one, exp_log hx] @[simp] lemma one_cpow (x : ℂ) : (1 : ℂ) ^ x = 1 := by rw cpow_def; split_ifs; simp [one_ne_zero, *] at * lemma cpow_add {x : ℂ} (y z : ℂ) (hx : x ≠ 0) : x ^ (y + z) = x ^ y * x ^ z := by simp [cpow_def]; split_ifs; simp [*, exp_add, mul_add] at * lemma cpow_mul {x y : ℂ} (z : ℂ) (h₁ : -π < (log x * y).im) (h₂ : (log x * y).im ≤ π) : x ^ (y * z) = (x ^ y) ^ z := begin simp only [cpow_def], split_ifs; simp [*, exp_ne_zero, log_exp h₁ h₂, mul_assoc] at * end lemma cpow_neg (x y : ℂ) : x ^ -y = (x ^ y)⁻¹ := by simp [cpow_def]; split_ifs; simp [exp_neg] lemma cpow_sub {x : ℂ} (y z : ℂ) (hx : x ≠ 0) : x ^ (y - z) = x ^ y / x ^ z := by rw [sub_eq_add_neg, cpow_add _ _ hx, cpow_neg, div_eq_mul_inv] lemma cpow_neg_one (x : ℂ) : x ^ (-1 : ℂ) = x⁻¹ := by simpa using cpow_neg x 1 @[simp] lemma cpow_nat_cast (x : ℂ) : ∀ (n : ℕ), x ^ (n : ℂ) = x ^ n | 0 := by simp | (n + 1) := if hx : x = 0 then by simp only [hx, pow_succ, complex.zero_cpow (nat.cast_ne_zero.2 (nat.succ_ne_zero _)), zero_mul] else by simp [cpow_add, hx, pow_add, cpow_nat_cast n] @[simp] lemma cpow_int_cast (x : ℂ) : ∀ (n : ℤ), x ^ (n : ℂ) = x ^ n | (n : ℕ) := by simp; refl | -[1+ n] := by rw zpow_neg_succ_of_nat; simp only [int.neg_succ_of_nat_coe, int.cast_neg, complex.cpow_neg, inv_eq_one_div, int.cast_coe_nat, cpow_nat_cast] lemma cpow_nat_inv_pow (x : ℂ) {n : ℕ} (hn : 0 < n) : (x ^ (n⁻¹ : ℂ)) ^ n = x := begin suffices : im (log x * n⁻¹) ∈ set.Ioc (-π) π, { rw [← cpow_nat_cast, ← cpow_mul _ this.1 this.2, inv_mul_cancel, cpow_one], exact_mod_cast hn.ne' }, rw [mul_comm, ← of_real_nat_cast, ← of_real_inv, of_real_mul_im, ← div_eq_inv_mul], have hn' : 0 < (n : ℝ), by assumption_mod_cast, have hn1 : 1 ≤ (n : ℝ), by exact_mod_cast (nat.succ_le_iff.2 hn), split, { rw lt_div_iff hn', calc -π * n ≤ -π * 1 : mul_le_mul_of_nonpos_left hn1 (neg_nonpos.2 real.pi_pos.le) ... = -π : mul_one _ ... < im (log x) : neg_pi_lt_log_im _ }, { rw div_le_iff hn', calc im (log x) ≤ π : log_im_le_pi _ ... = π * 1 : (mul_one π).symm ... ≤ π * n : mul_le_mul_of_nonneg_left hn1 real.pi_pos.le } end end complex section lim open complex variables {α : Type*} lemma zero_cpow_eq_nhds {b : ℂ} (hb : b ≠ 0) : (0 : ℂ).cpow =ᶠ[𝓝 b] 0 := begin suffices : ∀ᶠ (x : ℂ) in (𝓝 b), x ≠ 0, from this.mono (λ x hx, by rw [cpow_eq_pow, zero_cpow hx, pi.zero_apply]), exact is_open.eventually_mem is_open_ne hb, end lemma cpow_eq_nhds {a b : ℂ} (ha : a ≠ 0) : (λ x, x.cpow b) =ᶠ[𝓝 a] λ x, exp (log x * b) := begin suffices : ∀ᶠ (x : ℂ) in (𝓝 a), x ≠ 0, from this.mono (λ x hx, by { dsimp only, rw [cpow_eq_pow, cpow_def_of_ne_zero hx], }), exact is_open.eventually_mem is_open_ne ha, end lemma cpow_eq_nhds' {p : ℂ × ℂ} (hp_fst : p.fst ≠ 0) : (λ x, x.1 ^ x.2) =ᶠ[𝓝 p] λ x, exp (log x.1 * x.2) := begin suffices : ∀ᶠ (x : ℂ × ℂ) in (𝓝 p), x.1 ≠ 0, from this.mono (λ x hx, by { dsimp only, rw cpow_def_of_ne_zero hx, }), refine is_open.eventually_mem _ hp_fst, change is_open {x : ℂ × ℂ | x.1 = 0}ᶜ, rw is_open_compl_iff, exact is_closed_eq continuous_fst continuous_const, end lemma continuous_at_const_cpow {a b : ℂ} (ha : a ≠ 0) : continuous_at (cpow a) b := begin have cpow_eq : cpow a = λ b, exp (log a * b), by { ext1 b, rw [cpow_eq_pow, cpow_def_of_ne_zero ha], }, rw cpow_eq, exact continuous_exp.continuous_at.comp (continuous_at.mul continuous_at_const continuous_at_id), end lemma continuous_at_const_cpow' {a b : ℂ} (h : b ≠ 0) : continuous_at (cpow a) b := begin by_cases ha : a = 0, { rw [ha, continuous_at_congr (zero_cpow_eq_nhds h)], exact continuous_at_const, }, { exact continuous_at_const_cpow ha, }, end lemma continuous_at_cpow_const {a b : ℂ} (ha : 0 < a.re ∨ a.im ≠ 0) : continuous_at (λ x, cpow x b) a := begin have ha_ne_zero : a ≠ 0, by { intro h, cases ha; { rw h at ha, simpa using ha, }, }, rw continuous_at_congr (cpow_eq_nhds ha_ne_zero), refine continuous_exp.continuous_at.comp _, exact continuous_at.mul (continuous_at_clog ha) continuous_at_const, end lemma continuous_at_cpow {p : ℂ × ℂ} (hp_fst : 0 < p.fst.re ∨ p.fst.im ≠ 0) : continuous_at (λ x : ℂ × ℂ, x.1 ^ x.2) p := begin have hp_fst_ne_zero : p.fst ≠ 0, by { intro h, cases hp_fst; { rw h at hp_fst, simpa using hp_fst, }, }, rw continuous_at_congr (cpow_eq_nhds' hp_fst_ne_zero), refine continuous_exp.continuous_at.comp _, refine continuous_at.mul (continuous_at.comp _ continuous_fst.continuous_at) continuous_snd.continuous_at, exact continuous_at_clog hp_fst, end lemma filter.tendsto.cpow {l : filter α} {f g : α → ℂ} {a b : ℂ} (hf : tendsto f l (𝓝 a)) (hg : tendsto g l (𝓝 b)) (ha : 0 < a.re ∨ a.im ≠ 0) : tendsto (λ x, f x ^ g x) l (𝓝 (a ^ b)) := (@continuous_at_cpow (a,b) ha).tendsto.comp (hf.prod_mk_nhds hg) lemma filter.tendsto.const_cpow {l : filter α} {f : α → ℂ} {a b : ℂ} (hf : tendsto f l (𝓝 b)) (h : a ≠ 0 ∨ b ≠ 0) : tendsto (λ x, a ^ f x) l (𝓝 (a ^ b)) := begin cases h, { exact (continuous_at_const_cpow h).tendsto.comp hf, }, { exact (continuous_at_const_cpow' h).tendsto.comp hf, }, end variables [topological_space α] {f g : α → ℂ} {s : set α} {a : α} lemma continuous_within_at.cpow (hf : continuous_within_at f s a) (hg : continuous_within_at g s a) (h0 : 0 < (f a).re ∨ (f a).im ≠ 0) : continuous_within_at (λ x, f x ^ g x) s a := hf.cpow hg h0 lemma continuous_within_at.const_cpow {b : ℂ} (hf : continuous_within_at f s a) (h : b ≠ 0 ∨ f a ≠ 0) : continuous_within_at (λ x, b ^ f x) s a := hf.const_cpow h lemma continuous_at.cpow (hf : continuous_at f a) (hg : continuous_at g a) (h0 : 0 < (f a).re ∨ (f a).im ≠ 0) : continuous_at (λ x, f x ^ g x) a := hf.cpow hg h0 lemma continuous_at.const_cpow {b : ℂ} (hf : continuous_at f a) (h : b ≠ 0 ∨ f a ≠ 0) : continuous_at (λ x, b ^ f x) a := hf.const_cpow h lemma continuous_on.cpow (hf : continuous_on f s) (hg : continuous_on g s) (h0 : ∀ a ∈ s, 0 < (f a).re ∨ (f a).im ≠ 0) : continuous_on (λ x, f x ^ g x) s := λ a ha, (hf a ha).cpow (hg a ha) (h0 a ha) lemma continuous_on.const_cpow {b : ℂ} (hf : continuous_on f s) (h : b ≠ 0 ∨ ∀ a ∈ s, f a ≠ 0) : continuous_on (λ x, b ^ f x) s := λ a ha, (hf a ha).const_cpow (h.imp id $ λ h, h a ha) lemma continuous.cpow (hf : continuous f) (hg : continuous g) (h0 : ∀ a, 0 < (f a).re ∨ (f a).im ≠ 0) : continuous (λ x, f x ^ g x) := continuous_iff_continuous_at.2 $ λ a, (hf.continuous_at.cpow hg.continuous_at (h0 a)) lemma continuous.const_cpow {b : ℂ} (hf : continuous f) (h : b ≠ 0 ∨ ∀ a, f a ≠ 0) : continuous (λ x, b ^ f x) := continuous_iff_continuous_at.2 $ λ a, (hf.continuous_at.const_cpow $ h.imp id $ λ h, h a) end lim namespace real /-- The real power function `x^y`, defined as the real part of the complex power function. For `x > 0`, it is equal to `exp(y log x)`. For `x = 0`, one sets `0^0=1` and `0^y=0` for `y ≠ 0`. For `x < 0`, the definition is somewhat arbitary as it depends on the choice of a complex determination of the logarithm. With our conventions, it is equal to `exp (y log x) cos (πy)`. -/ noncomputable def rpow (x y : ℝ) := ((x : ℂ) ^ (y : ℂ)).re noncomputable instance : has_pow ℝ ℝ := ⟨rpow⟩ @[simp] lemma rpow_eq_pow (x y : ℝ) : rpow x y = x ^ y := rfl lemma rpow_def (x y : ℝ) : x ^ y = ((x : ℂ) ^ (y : ℂ)).re := rfl lemma rpow_def_of_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) := by simp only [rpow_def, complex.cpow_def]; split_ifs; simp [*, (complex.of_real_log hx).symm, -complex.of_real_mul, -is_R_or_C.of_real_mul, (complex.of_real_mul _ _).symm, complex.exp_of_real_re] at * lemma rpow_def_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : x ^ y = exp (log x * y) := by rw [rpow_def_of_nonneg (le_of_lt hx), if_neg (ne_of_gt hx)] lemma exp_mul (x y : ℝ) : exp (x * y) = (exp x) ^ y := by rw [rpow_def_of_pos (exp_pos _), log_exp] lemma rpow_eq_zero_iff_of_nonneg {x y : ℝ} (hx : 0 ≤ x) : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := by { simp only [rpow_def_of_nonneg hx], split_ifs; simp [*, exp_ne_zero] } open_locale real lemma rpow_def_of_neg {x : ℝ} (hx : x < 0) (y : ℝ) : x ^ y = exp (log x * y) * cos (y * π) := begin rw [rpow_def, complex.cpow_def, if_neg], have : complex.log x * y = ↑(log(-x) * y) + ↑(y * π) * complex.I, { simp only [complex.log, abs_of_neg hx, complex.arg_of_real_of_neg hx, complex.abs_of_real, complex.of_real_mul], ring }, { rw [this, complex.exp_add_mul_I, ← complex.of_real_exp, ← complex.of_real_cos, ← complex.of_real_sin, mul_add, ← complex.of_real_mul, ← mul_assoc, ← complex.of_real_mul, complex.add_re, complex.of_real_re, complex.mul_re, complex.I_re, complex.of_real_im, real.log_neg_eq_log], ring }, { rw complex.of_real_eq_zero, exact ne_of_lt hx } end lemma rpow_def_of_nonpos {x : ℝ} (hx : x ≤ 0) (y : ℝ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) * cos (y * π) := by split_ifs; simp [rpow_def, *]; exact rpow_def_of_neg (lt_of_le_of_ne hx h) _ lemma rpow_pos_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : 0 < x ^ y := by rw rpow_def_of_pos hx; apply exp_pos @[simp] lemma rpow_zero (x : ℝ) : x ^ (0 : ℝ) = 1 := by simp [rpow_def] @[simp] lemma zero_rpow {x : ℝ} (h : x ≠ 0) : (0 : ℝ) ^ x = 0 := by simp [rpow_def, *] lemma zero_rpow_eq_iff {x : ℝ} {a : ℝ} : 0 ^ x = a ↔ (x ≠ 0 ∧ a = 0) ∨ (x = 0 ∧ a = 1) := begin split, { intros hyp, simp [rpow_def] at hyp, by_cases x = 0, { subst h, simp only [complex.one_re, complex.of_real_zero, complex.cpow_zero] at hyp, exact or.inr ⟨rfl, hyp.symm⟩}, { rw complex.zero_cpow (complex.of_real_ne_zero.mpr h) at hyp, exact or.inl ⟨h, hyp.symm⟩, }, }, { rintro (⟨h,rfl⟩|⟨rfl,rfl⟩), { exact zero_rpow h, }, { exact rpow_zero _, }, }, end lemma eq_zero_rpow_iff {x : ℝ} {a : ℝ} : a = 0 ^ x ↔ (x ≠ 0 ∧ a = 0) ∨ (x = 0 ∧ a = 1) := by rw [←zero_rpow_eq_iff, eq_comm] @[simp] lemma rpow_one (x : ℝ) : x ^ (1 : ℝ) = x := by simp [rpow_def] @[simp] lemma one_rpow (x : ℝ) : (1 : ℝ) ^ x = 1 := by simp [rpow_def] lemma zero_rpow_le_one (x : ℝ) : (0 : ℝ) ^ x ≤ 1 := by { by_cases h : x = 0; simp [h, zero_le_one] } lemma zero_rpow_nonneg (x : ℝ) : 0 ≤ (0 : ℝ) ^ x := by { by_cases h : x = 0; simp [h, zero_le_one] } lemma rpow_nonneg_of_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : 0 ≤ x ^ y := by rw [rpow_def_of_nonneg hx]; split_ifs; simp only [zero_le_one, le_refl, le_of_lt (exp_pos _)] lemma abs_rpow_le_abs_rpow (x y : ℝ) : |x ^ y| ≤ |x| ^ y := begin rcases lt_trichotomy 0 x with (hx|rfl|hx), { rw [abs_of_pos hx, abs_of_pos (rpow_pos_of_pos hx _)] }, { rw [abs_zero, abs_of_nonneg (rpow_nonneg_of_nonneg le_rfl _)] }, { rw [abs_of_neg hx, rpow_def_of_neg hx, rpow_def_of_pos (neg_pos.2 hx), log_neg_eq_log, abs_mul, abs_of_pos (exp_pos _)], exact mul_le_of_le_one_right (exp_pos _).le (abs_cos_le_one _) } end lemma abs_rpow_le_exp_log_mul (x y : ℝ) : |x ^ y| ≤ exp (log x * y) := begin refine (abs_rpow_le_abs_rpow x y).trans _, by_cases hx : x = 0, { by_cases hy : y = 0; simp [hx, hy, zero_le_one] }, { rw [rpow_def_of_pos (abs_pos.2 hx), log_abs] } end lemma abs_rpow_of_nonneg {x y : ℝ} (hx_nonneg : 0 ≤ x) : |x ^ y| = |x| ^ y := begin have h_rpow_nonneg : 0 ≤ x ^ y, from real.rpow_nonneg_of_nonneg hx_nonneg _, rw [abs_eq_self.mpr hx_nonneg, abs_eq_self.mpr h_rpow_nonneg], end lemma norm_rpow_of_nonneg {x y : ℝ} (hx_nonneg : 0 ≤ x) : ∥x ^ y∥ = ∥x∥ ^ y := by { simp_rw real.norm_eq_abs, exact abs_rpow_of_nonneg hx_nonneg, } end real namespace complex lemma of_real_cpow {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : ((x ^ y : ℝ) : ℂ) = (x : ℂ) ^ (y : ℂ) := by simp [real.rpow_def_of_nonneg hx, complex.cpow_def]; split_ifs; simp [complex.of_real_log hx] @[simp] lemma abs_cpow_real (x : ℂ) (y : ℝ) : abs (x ^ (y : ℂ)) = x.abs ^ y := begin rw [real.rpow_def_of_nonneg (abs_nonneg _), complex.cpow_def], split_ifs; simp [*, abs_of_nonneg (le_of_lt (real.exp_pos _)), complex.log, complex.exp_add, add_mul, mul_right_comm _ I, exp_mul_I, abs_cos_add_sin_mul_I, (complex.of_real_mul _ _).symm, -complex.of_real_mul, -is_R_or_C.of_real_mul] at * end @[simp] lemma abs_cpow_inv_nat (x : ℂ) (n : ℕ) : abs (x ^ (n⁻¹ : ℂ)) = x.abs ^ (n⁻¹ : ℝ) := by rw ← abs_cpow_real; simp [-abs_cpow_real] end complex namespace real variables {x y z : ℝ} lemma rpow_add {x : ℝ} (hx : 0 < x) (y z : ℝ) : x ^ (y + z) = x ^ y * x ^ z := by simp only [rpow_def_of_pos hx, mul_add, exp_add] lemma rpow_add' {x : ℝ} (hx : 0 ≤ x) {y z : ℝ} (h : y + z ≠ 0) : x ^ (y + z) = x ^ y * x ^ z := begin rcases hx.eq_or_lt with rfl|pos, { rw [zero_rpow h, zero_eq_mul], have : y ≠ 0 ∨ z ≠ 0, from not_and_distrib.1 (λ ⟨hy, hz⟩, h $ hy.symm ▸ hz.symm ▸ zero_add 0), exact this.imp zero_rpow zero_rpow }, { exact rpow_add pos _ _ } end /-- For `0 ≤ x`, the only problematic case in the equality `x ^ y * x ^ z = x ^ (y + z)` is for `x = 0` and `y + z = 0`, where the right hand side is `1` while the left hand side can vanish. The inequality is always true, though, and given in this lemma. -/ lemma le_rpow_add {x : ℝ} (hx : 0 ≤ x) (y z : ℝ) : x ^ y * x ^ z ≤ x ^ (y + z) := begin rcases le_iff_eq_or_lt.1 hx with H|pos, { by_cases h : y + z = 0, { simp only [H.symm, h, rpow_zero], calc (0 : ℝ) ^ y * 0 ^ z ≤ 1 * 1 : mul_le_mul (zero_rpow_le_one y) (zero_rpow_le_one z) (zero_rpow_nonneg z) zero_le_one ... = 1 : by simp }, { simp [rpow_add', ← H, h] } }, { simp [rpow_add pos] } end lemma rpow_mul {x : ℝ} (hx : 0 ≤ x) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := by rw [← complex.of_real_inj, complex.of_real_cpow (rpow_nonneg_of_nonneg hx _), complex.of_real_cpow hx, complex.of_real_mul, complex.cpow_mul, complex.of_real_cpow hx]; simp only [(complex.of_real_mul _ _).symm, (complex.of_real_log hx).symm, complex.of_real_im, neg_lt_zero, pi_pos, le_of_lt pi_pos] lemma rpow_neg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ := by simp only [rpow_def_of_nonneg hx]; split_ifs; simp [*, exp_neg] at * lemma rpow_sub {x : ℝ} (hx : 0 < x) (y z : ℝ) : x ^ (y - z) = x ^ y / x ^ z := by simp only [sub_eq_add_neg, rpow_add hx, rpow_neg (le_of_lt hx), div_eq_mul_inv] lemma rpow_sub' {x : ℝ} (hx : 0 ≤ x) {y z : ℝ} (h : y - z ≠ 0) : x ^ (y - z) = x ^ y / x ^ z := by { simp only [sub_eq_add_neg] at h ⊢, simp only [rpow_add' hx h, rpow_neg hx, div_eq_mul_inv] } lemma rpow_add_int {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℤ) : x ^ (y + n) = x ^ y * x ^ n := by rw [rpow_def, complex.of_real_add, complex.cpow_add _ _ (complex.of_real_ne_zero.mpr hx), complex.of_real_int_cast, complex.cpow_int_cast, ← complex.of_real_zpow, mul_comm, complex.of_real_mul_re, ← rpow_def, mul_comm] lemma rpow_add_nat {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℕ) : x ^ (y + n) = x ^ y * x ^ n := rpow_add_int hx y n lemma rpow_sub_int {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℤ) : x ^ (y - n) = x ^ y / x ^ n := by simpa using rpow_add_int hx y (-n) lemma rpow_sub_nat {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℕ) : x ^ (y - n) = x ^ y / x ^ n := rpow_sub_int hx y n lemma rpow_add_one {x : ℝ} (hx : x ≠ 0) (y : ℝ) : x ^ (y + 1) = x ^ y * x := by simpa using rpow_add_nat hx y 1 lemma rpow_sub_one {x : ℝ} (hx : x ≠ 0) (y : ℝ) : x ^ (y - 1) = x ^ y / x := by simpa using rpow_sub_nat hx y 1 @[simp] lemma rpow_int_cast (x : ℝ) (n : ℤ) : x ^ (n : ℝ) = x ^ n := by simp only [rpow_def, ← complex.of_real_zpow, complex.cpow_int_cast, complex.of_real_int_cast, complex.of_real_re] @[simp] lemma rpow_nat_cast (x : ℝ) (n : ℕ) : x ^ (n : ℝ) = x ^ n := rpow_int_cast x n lemma rpow_neg_one (x : ℝ) : x ^ (-1 : ℝ) = x⁻¹ := begin suffices H : x ^ ((-1 : ℤ) : ℝ) = x⁻¹, by exact_mod_cast H, simp only [rpow_int_cast, zpow_one, zpow_neg₀], end lemma mul_rpow {x y z : ℝ} (h : 0 ≤ x) (h₁ : 0 ≤ y) : (x*y)^z = x^z * y^z := begin iterate 3 { rw real.rpow_def_of_nonneg }, split_ifs; simp * at *, { have hx : 0 < x, { cases lt_or_eq_of_le h with h₂ h₂, { exact h₂ }, exfalso, apply h_2, exact eq.symm h₂ }, have hy : 0 < y, { cases lt_or_eq_of_le h₁ with h₂ h₂, { exact h₂ }, exfalso, apply h_3, exact eq.symm h₂ }, rw [log_mul (ne_of_gt hx) (ne_of_gt hy), add_mul, exp_add]}, { exact h₁ }, { exact h }, { exact mul_nonneg h h₁ }, end lemma inv_rpow (hx : 0 ≤ x) (y : ℝ) : (x⁻¹)^y = (x^y)⁻¹ := by simp only [← rpow_neg_one, ← rpow_mul hx, mul_comm] lemma div_rpow (hx : 0 ≤ x) (hy : 0 ≤ y) (z : ℝ) : (x / y) ^ z = x^z / y^z := by simp only [div_eq_mul_inv, mul_rpow hx (inv_nonneg.2 hy), inv_rpow hy] lemma log_rpow {x : ℝ} (hx : 0 < x) (y : ℝ) : log (x^y) = y * (log x) := begin apply exp_injective, rw [exp_log (rpow_pos_of_pos hx y), ← exp_log hx, mul_comm, rpow_def_of_pos (exp_pos (log x)) y], end lemma rpow_lt_rpow (hx : 0 ≤ x) (hxy : x < y) (hz : 0 < z) : x^z < y^z := begin rw le_iff_eq_or_lt at hx, cases hx, { rw [← hx, zero_rpow (ne_of_gt hz)], exact rpow_pos_of_pos (by rwa ← hx at hxy) _ }, rw [rpow_def_of_pos hx, rpow_def_of_pos (lt_trans hx hxy), exp_lt_exp], exact mul_lt_mul_of_pos_right (log_lt_log hx hxy) hz end lemma rpow_le_rpow {x y z: ℝ} (h : 0 ≤ x) (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z := begin rcases eq_or_lt_of_le h₁ with rfl|h₁', { refl }, rcases eq_or_lt_of_le h₂ with rfl|h₂', { simp }, exact le_of_lt (rpow_lt_rpow h h₁' h₂') end lemma rpow_lt_rpow_iff (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := ⟨lt_imp_lt_of_le_imp_le $ λ h, rpow_le_rpow hy h (le_of_lt hz), λ h, rpow_lt_rpow hx h hz⟩ lemma rpow_le_rpow_iff (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := le_iff_le_iff_lt_iff_lt.2 $ rpow_lt_rpow_iff hy hx hz lemma rpow_lt_rpow_of_exponent_lt (hx : 1 < x) (hyz : y < z) : x^y < x^z := begin repeat {rw [rpow_def_of_pos (lt_trans zero_lt_one hx)]}, rw exp_lt_exp, exact mul_lt_mul_of_pos_left hyz (log_pos hx), end lemma rpow_le_rpow_of_exponent_le (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z := begin repeat {rw [rpow_def_of_pos (lt_of_lt_of_le zero_lt_one hx)]}, rw exp_le_exp, exact mul_le_mul_of_nonneg_left hyz (log_nonneg hx), end lemma rpow_lt_rpow_of_exponent_gt (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x^y < x^z := begin repeat {rw [rpow_def_of_pos hx0]}, rw exp_lt_exp, exact mul_lt_mul_of_neg_left hyz (log_neg hx0 hx1), end lemma rpow_le_rpow_of_exponent_ge (hx0 : 0 < x) (hx1 : x ≤ 1) (hyz : z ≤ y) : x^y ≤ x^z := begin repeat {rw [rpow_def_of_pos hx0]}, rw exp_le_exp, exact mul_le_mul_of_nonpos_left hyz (log_nonpos (le_of_lt hx0) hx1), end lemma rpow_lt_one {x z : ℝ} (hx1 : 0 ≤ x) (hx2 : x < 1) (hz : 0 < z) : x^z < 1 := by { rw ← one_rpow z, exact rpow_lt_rpow hx1 hx2 hz } lemma rpow_le_one {x z : ℝ} (hx1 : 0 ≤ x) (hx2 : x ≤ 1) (hz : 0 ≤ z) : x^z ≤ 1 := by { rw ← one_rpow z, exact rpow_le_rpow hx1 hx2 hz } lemma rpow_lt_one_of_one_lt_of_neg {x z : ℝ} (hx : 1 < x) (hz : z < 0) : x^z < 1 := by { convert rpow_lt_rpow_of_exponent_lt hx hz, exact (rpow_zero x).symm } lemma rpow_le_one_of_one_le_of_nonpos {x z : ℝ} (hx : 1 ≤ x) (hz : z ≤ 0) : x^z ≤ 1 := by { convert rpow_le_rpow_of_exponent_le hx hz, exact (rpow_zero x).symm } lemma one_lt_rpow {x z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z := by { rw ← one_rpow z, exact rpow_lt_rpow zero_le_one hx hz } lemma one_le_rpow {x z : ℝ} (hx : 1 ≤ x) (hz : 0 ≤ z) : 1 ≤ x^z := by { rw ← one_rpow z, exact rpow_le_rpow zero_le_one hx hz } lemma one_lt_rpow_of_pos_of_lt_one_of_neg (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x^z := by { convert rpow_lt_rpow_of_exponent_gt hx1 hx2 hz, exact (rpow_zero x).symm } lemma one_le_rpow_of_pos_of_le_one_of_nonpos (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z ≤ 0) : 1 ≤ x^z := by { convert rpow_le_rpow_of_exponent_ge hx1 hx2 hz, exact (rpow_zero x).symm } lemma rpow_lt_one_iff_of_pos (hx : 0 < x) : x ^ y < 1 ↔ 1 < x ∧ y < 0 ∨ x < 1 ∧ 0 < y := by rw [rpow_def_of_pos hx, exp_lt_one_iff, mul_neg_iff, log_pos_iff hx, log_neg_iff hx] lemma rpow_lt_one_iff (hx : 0 ≤ x) : x ^ y < 1 ↔ x = 0 ∧ y ≠ 0 ∨ 1 < x ∧ y < 0 ∨ x < 1 ∧ 0 < y := begin rcases hx.eq_or_lt with (rfl|hx), { rcases em (y = 0) with (rfl|hy); simp [*, lt_irrefl, zero_lt_one] }, { simp [rpow_lt_one_iff_of_pos hx, hx.ne.symm] } end lemma one_lt_rpow_iff_of_pos (hx : 0 < x) : 1 < x ^ y ↔ 1 < x ∧ 0 < y ∨ x < 1 ∧ y < 0 := by rw [rpow_def_of_pos hx, one_lt_exp_iff, mul_pos_iff, log_pos_iff hx, log_neg_iff hx] lemma one_lt_rpow_iff (hx : 0 ≤ x) : 1 < x ^ y ↔ 1 < x ∧ 0 < y ∨ 0 < x ∧ x < 1 ∧ y < 0 := begin rcases hx.eq_or_lt with (rfl|hx), { rcases em (y = 0) with (rfl|hy); simp [*, lt_irrefl, (@zero_lt_one ℝ _ _).not_lt] }, { simp [one_lt_rpow_iff_of_pos hx, hx] } end lemma le_rpow_iff_log_le (hx : 0 < x) (hy : 0 < y) : x ≤ y^z ↔ real.log x ≤ z * real.log y := by rw [←real.log_le_log hx (real.rpow_pos_of_pos hy z), real.log_rpow hy] lemma le_rpow_of_log_le (hx : 0 ≤ x) (hy : 0 < y) (h : real.log x ≤ z * real.log y) : x ≤ y^z := begin obtain hx | rfl := hx.lt_or_eq, { exact (le_rpow_iff_log_le hx hy).2 h }, exact (real.rpow_pos_of_pos hy z).le, end lemma lt_rpow_iff_log_lt (hx : 0 < x) (hy : 0 < y) : x < y^z ↔ real.log x < z * real.log y := by rw [←real.log_lt_log_iff hx (real.rpow_pos_of_pos hy z), real.log_rpow hy] lemma lt_rpow_of_log_lt (hx : 0 ≤ x) (hy : 0 < y) (h : real.log x < z * real.log y) : x < y^z := begin obtain hx | rfl := hx.lt_or_eq, { exact (lt_rpow_iff_log_lt hx hy).2 h }, exact real.rpow_pos_of_pos hy z, end lemma rpow_le_one_iff_of_pos (hx : 0 < x) : x ^ y ≤ 1 ↔ 1 ≤ x ∧ y ≤ 0 ∨ x ≤ 1 ∧ 0 ≤ y := by rw [rpow_def_of_pos hx, exp_le_one_iff, mul_nonpos_iff, log_nonneg_iff hx, log_nonpos_iff hx] lemma pow_nat_rpow_nat_inv {x : ℝ} (hx : 0 ≤ x) {n : ℕ} (hn : 0 < n) : (x ^ n) ^ (n⁻¹ : ℝ) = x := have hn0 : (n : ℝ) ≠ 0, by simpa [pos_iff_ne_zero] using hn, by rw [← rpow_nat_cast, ← rpow_mul hx, mul_inv_cancel hn0, rpow_one] lemma rpow_nat_inv_pow_nat {x : ℝ} (hx : 0 ≤ x) {n : ℕ} (hn : 0 < n) : (x ^ (n⁻¹ : ℝ)) ^ n = x := have hn0 : (n : ℝ) ≠ 0, by simpa [pos_iff_ne_zero] using hn, by rw [← rpow_nat_cast, ← rpow_mul hx, inv_mul_cancel hn0, rpow_one] lemma continuous_at_const_rpow {a b : ℝ} (h : a ≠ 0) : continuous_at (rpow a) b := begin have : rpow a = λ x : ℝ, ((a : ℂ) ^ (x : ℂ)).re, by { ext1 x, rw [rpow_eq_pow, rpow_def], }, rw this, refine complex.continuous_re.continuous_at.comp _, refine (continuous_at_const_cpow _).comp complex.continuous_of_real.continuous_at, norm_cast, exact h, end lemma continuous_at_const_rpow' {a b : ℝ} (h : b ≠ 0) : continuous_at (rpow a) b := begin have : rpow a = λ x : ℝ, ((a : ℂ) ^ (x : ℂ)).re, by { ext1 x, rw [rpow_eq_pow, rpow_def], }, rw this, refine complex.continuous_re.continuous_at.comp _, refine (continuous_at_const_cpow' _).comp complex.continuous_of_real.continuous_at, norm_cast, exact h, end lemma rpow_eq_nhds_of_neg {p : ℝ × ℝ} (hp_fst : p.fst < 0) : (λ x : ℝ × ℝ, x.1 ^ x.2) =ᶠ[𝓝 p] λ x, exp (log x.1 * x.2) * cos (x.2 * π) := begin suffices : ∀ᶠ (x : ℝ × ℝ) in (𝓝 p), x.1 < 0, from this.mono (λ x hx, by { dsimp only, rw rpow_def_of_neg hx, }), exact is_open.eventually_mem (is_open_lt continuous_fst continuous_const) hp_fst, end lemma rpow_eq_nhds_of_pos {p : ℝ × ℝ} (hp_fst : 0 < p.fst) : (λ x : ℝ × ℝ, x.1 ^ x.2) =ᶠ[𝓝 p] λ x, exp (log x.1 * x.2) := begin suffices : ∀ᶠ (x : ℝ × ℝ) in (𝓝 p), 0 < x.1, from this.mono (λ x hx, by { dsimp only, rw rpow_def_of_pos hx, }), exact is_open.eventually_mem (is_open_lt continuous_const continuous_fst) hp_fst, end lemma continuous_at_rpow_of_ne (p : ℝ × ℝ) (hp : p.1 ≠ 0) : continuous_at (λ p : ℝ × ℝ, p.1 ^ p.2) p := begin rw ne_iff_lt_or_gt at hp, cases hp, { rw continuous_at_congr (rpow_eq_nhds_of_neg hp), refine continuous_at.mul _ (continuous_cos.continuous_at.comp _), { refine continuous_exp.continuous_at.comp (continuous_at.mul _ continuous_snd.continuous_at), refine (continuous_at_log _).comp continuous_fst.continuous_at, exact hp.ne, }, { exact continuous_snd.continuous_at.mul continuous_at_const, }, }, { rw continuous_at_congr (rpow_eq_nhds_of_pos hp), refine continuous_exp.continuous_at.comp (continuous_at.mul _ continuous_snd.continuous_at), refine (continuous_at_log _).comp continuous_fst.continuous_at, exact hp.lt.ne.symm, }, end lemma continuous_at_rpow_of_pos (p : ℝ × ℝ) (hp : 0 < p.2) : continuous_at (λ p : ℝ × ℝ, p.1 ^ p.2) p := begin cases p with x y, obtain hx|rfl := ne_or_eq x 0, { exact continuous_at_rpow_of_ne (x, y) hx }, have A : tendsto (λ p : ℝ × ℝ, exp (log p.1 * p.2)) (𝓝[{0}ᶜ] 0 ×ᶠ 𝓝 y) (𝓝 0) := tendsto_exp_at_bot.comp ((tendsto_log_nhds_within_zero.comp tendsto_fst).at_bot_mul hp tendsto_snd), have B : tendsto (λ p : ℝ × ℝ, p.1 ^ p.2) (𝓝[{0}ᶜ] 0 ×ᶠ 𝓝 y) (𝓝 0) := squeeze_zero_norm (λ p, abs_rpow_le_exp_log_mul p.1 p.2) A, have C : tendsto (λ p : ℝ × ℝ, p.1 ^ p.2) (𝓝[{0}] 0 ×ᶠ 𝓝 y) (pure 0), { rw [nhds_within_singleton, tendsto_pure, pure_prod, eventually_map], exact (lt_mem_nhds hp).mono (λ y hy, zero_rpow hy.ne') }, simpa only [← sup_prod, ← nhds_within_union, set.compl_union_self, nhds_within_univ, nhds_prod_eq, continuous_at, zero_rpow hp.ne'] using B.sup (C.mono_right (pure_le_nhds _)) end lemma continuous_at_rpow (p : ℝ × ℝ) (h : p.1 ≠ 0 ∨ 0 < p.2) : continuous_at (λ p : ℝ × ℝ, p.1 ^ p.2) p := h.elim (λ h, continuous_at_rpow_of_ne p h) (λ h, continuous_at_rpow_of_pos p h) end real section variable {α : Type*} lemma filter.tendsto.rpow {l : filter α} {f g : α → ℝ} {x y : ℝ} (hf : tendsto f l (𝓝 x)) (hg : tendsto g l (𝓝 y)) (h : x ≠ 0 ∨ 0 < y) : tendsto (λ t, f t ^ g t) l (𝓝 (x ^ y)) := (real.continuous_at_rpow (x, y) h).tendsto.comp (hf.prod_mk_nhds hg) lemma filter.tendsto.rpow_const {l : filter α} {f : α → ℝ} {x p : ℝ} (hf : tendsto f l (𝓝 x)) (h : x ≠ 0 ∨ 0 ≤ p) : tendsto (λ a, f a ^ p) l (𝓝 (x ^ p)) := if h0 : 0 = p then h0 ▸ by simp [tendsto_const_nhds] else hf.rpow tendsto_const_nhds (h.imp id $ λ h', h'.lt_of_ne h0) variables [topological_space α] {f g : α → ℝ} {s : set α} {x : α} {p : ℝ} lemma continuous_at.rpow (hf : continuous_at f x) (hg : continuous_at g x) (h : f x ≠ 0 ∨ 0 < g x) : continuous_at (λ t, f t ^ g t) x := hf.rpow hg h lemma continuous_within_at.rpow (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) (h : f x ≠ 0 ∨ 0 < g x) : continuous_within_at (λ t, f t ^ g t) s x := hf.rpow hg h lemma continuous_on.rpow (hf : continuous_on f s) (hg : continuous_on g s) (h : ∀ x ∈ s, f x ≠ 0 ∨ 0 < g x) : continuous_on (λ t, f t ^ g t) s := λ t ht, (hf t ht).rpow (hg t ht) (h t ht) lemma continuous.rpow (hf : continuous f) (hg : continuous g) (h : ∀ x, f x ≠ 0 ∨ 0 < g x) : continuous (λ x, f x ^ g x) := continuous_iff_continuous_at.2 $ λ x, (hf.continuous_at.rpow hg.continuous_at (h x)) lemma continuous_within_at.rpow_const (hf : continuous_within_at f s x) (h : f x ≠ 0 ∨ 0 ≤ p) : continuous_within_at (λ x, f x ^ p) s x := hf.rpow_const h lemma continuous_at.rpow_const (hf : continuous_at f x) (h : f x ≠ 0 ∨ 0 ≤ p) : continuous_at (λ x, f x ^ p) x := hf.rpow_const h lemma continuous_on.rpow_const (hf : continuous_on f s) (h : ∀ x ∈ s, f x ≠ 0 ∨ 0 ≤ p) : continuous_on (λ x, f x ^ p) s := λ x hx, (hf x hx).rpow_const (h x hx) lemma continuous.rpow_const (hf : continuous f) (h : ∀ x, f x ≠ 0 ∨ 0 ≤ p) : continuous (λ x, f x ^ p) := continuous_iff_continuous_at.2 $ λ x, hf.continuous_at.rpow_const (h x) end namespace real variables {z x y : ℝ} section sqrt lemma sqrt_eq_rpow (x : ℝ) : sqrt x = x ^ (1/(2:ℝ)) := begin obtain h | h := le_or_lt 0 x, { rw [← mul_self_inj_of_nonneg (sqrt_nonneg _) (rpow_nonneg_of_nonneg h _), mul_self_sqrt h, ← sq, ← rpow_nat_cast, ← rpow_mul h], norm_num }, { have : 1 / (2:ℝ) * π = π / (2:ℝ), ring, rw [sqrt_eq_zero_of_nonpos h.le, rpow_def_of_neg h, this, cos_pi_div_two, mul_zero] } end end sqrt end real section limits open real filter /-- The function `x ^ y` tends to `+∞` at `+∞` for any positive real `y`. -/ lemma tendsto_rpow_at_top {y : ℝ} (hy : 0 < y) : tendsto (λ x : ℝ, x ^ y) at_top at_top := begin rw tendsto_at_top_at_top, intro b, use (max b 0) ^ (1/y), intros x hx, exact le_of_max_le_left (by { convert rpow_le_rpow (rpow_nonneg_of_nonneg (le_max_right b 0) (1/y)) hx (le_of_lt hy), rw [← rpow_mul (le_max_right b 0), (eq_div_iff (ne_of_gt hy)).mp rfl, rpow_one] }), end /-- The function `x ^ (-y)` tends to `0` at `+∞` for any positive real `y`. -/ lemma tendsto_rpow_neg_at_top {y : ℝ} (hy : 0 < y) : tendsto (λ x : ℝ, x ^ (-y)) at_top (𝓝 0) := tendsto.congr' (eventually_eq_of_mem (Ioi_mem_at_top 0) (λ x hx, (rpow_neg (le_of_lt hx) y).symm)) (tendsto_rpow_at_top hy).inv_tendsto_at_top /-- The function `x ^ (a / (b * x + c))` tends to `1` at `+∞`, for any real numbers `a`, `b`, and `c` such that `b` is nonzero. -/ lemma tendsto_rpow_div_mul_add (a b c : ℝ) (hb : 0 ≠ b) : tendsto (λ x, x ^ (a / (b*x+c))) at_top (𝓝 1) := begin refine tendsto.congr' _ ((tendsto_exp_nhds_0_nhds_1.comp (by simpa only [mul_zero, pow_one] using ((@tendsto_const_nhds _ _ _ a _).mul (tendsto_div_pow_mul_exp_add_at_top b c 1 hb (by norm_num))))).comp (tendsto_log_at_top)), apply eventually_eq_of_mem (Ioi_mem_at_top (0:ℝ)), intros x hx, simp only [set.mem_Ioi, function.comp_app] at hx ⊢, rw [exp_log hx, ← exp_log (rpow_pos_of_pos hx (a / (b * x + c))), log_rpow hx (a / (b * x + c))], field_simp, end /-- The function `x ^ (1 / x)` tends to `1` at `+∞`. -/ lemma tendsto_rpow_div : tendsto (λ x, x ^ ((1:ℝ) / x)) at_top (𝓝 1) := by { convert tendsto_rpow_div_mul_add (1:ℝ) _ (0:ℝ) zero_ne_one, ring_nf } /-- The function `x ^ (-1 / x)` tends to `1` at `+∞`. -/ lemma tendsto_rpow_neg_div : tendsto (λ x, x ^ (-(1:ℝ) / x)) at_top (𝓝 1) := by { convert tendsto_rpow_div_mul_add (-(1:ℝ)) _ (0:ℝ) zero_ne_one, ring_nf } end limits namespace nnreal /-- The nonnegative real power function `x^y`, defined for `x : ℝ≥0` and `y : ℝ ` as the restriction of the real power function. For `x > 0`, it is equal to `exp (y log x)`. For `x = 0`, one sets `0 ^ 0 = 1` and `0 ^ y = 0` for `y ≠ 0`. -/ noncomputable def rpow (x : ℝ≥0) (y : ℝ) : ℝ≥0 := ⟨(x : ℝ) ^ y, real.rpow_nonneg_of_nonneg x.2 y⟩ noncomputable instance : has_pow ℝ≥0 ℝ := ⟨rpow⟩ @[simp] lemma rpow_eq_pow (x : ℝ≥0) (y : ℝ) : rpow x y = x ^ y := rfl @[simp, norm_cast] lemma coe_rpow (x : ℝ≥0) (y : ℝ) : ((x ^ y : ℝ≥0) : ℝ) = (x : ℝ) ^ y := rfl @[simp] lemma rpow_zero (x : ℝ≥0) : x ^ (0 : ℝ) = 1 := nnreal.eq $ real.rpow_zero _ @[simp] lemma rpow_eq_zero_iff {x : ℝ≥0} {y : ℝ} : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := begin rw [← nnreal.coe_eq, coe_rpow, ← nnreal.coe_eq_zero], exact real.rpow_eq_zero_iff_of_nonneg x.2 end @[simp] lemma zero_rpow {x : ℝ} (h : x ≠ 0) : (0 : ℝ≥0) ^ x = 0 := nnreal.eq $ real.zero_rpow h @[simp] lemma rpow_one (x : ℝ≥0) : x ^ (1 : ℝ) = x := nnreal.eq $ real.rpow_one _ @[simp] lemma one_rpow (x : ℝ) : (1 : ℝ≥0) ^ x = 1 := nnreal.eq $ real.one_rpow _ lemma rpow_add {x : ℝ≥0} (hx : x ≠ 0) (y z : ℝ) : x ^ (y + z) = x ^ y * x ^ z := nnreal.eq $ real.rpow_add (pos_iff_ne_zero.2 hx) _ _ lemma rpow_add' (x : ℝ≥0) {y z : ℝ} (h : y + z ≠ 0) : x ^ (y + z) = x ^ y * x ^ z := nnreal.eq $ real.rpow_add' x.2 h lemma rpow_mul (x : ℝ≥0) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := nnreal.eq $ real.rpow_mul x.2 y z lemma rpow_neg (x : ℝ≥0) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ := nnreal.eq $ real.rpow_neg x.2 _ lemma rpow_neg_one (x : ℝ≥0) : x ^ (-1 : ℝ) = x ⁻¹ := by simp [rpow_neg] lemma rpow_sub {x : ℝ≥0} (hx : x ≠ 0) (y z : ℝ) : x ^ (y - z) = x ^ y / x ^ z := nnreal.eq $ real.rpow_sub (pos_iff_ne_zero.2 hx) y z lemma rpow_sub' (x : ℝ≥0) {y z : ℝ} (h : y - z ≠ 0) : x ^ (y - z) = x ^ y / x ^ z := nnreal.eq $ real.rpow_sub' x.2 h lemma inv_rpow (x : ℝ≥0) (y : ℝ) : (x⁻¹) ^ y = (x ^ y)⁻¹ := nnreal.eq $ real.inv_rpow x.2 y lemma div_rpow (x y : ℝ≥0) (z : ℝ) : (x / y) ^ z = x ^ z / y ^ z := nnreal.eq $ real.div_rpow x.2 y.2 z lemma sqrt_eq_rpow (x : ℝ≥0) : sqrt x = x ^ (1/(2:ℝ)) := begin refine nnreal.eq _, push_cast, exact real.sqrt_eq_rpow x.1, end @[simp, norm_cast] lemma rpow_nat_cast (x : ℝ≥0) (n : ℕ) : x ^ (n : ℝ) = x ^ n := nnreal.eq $ by simpa only [coe_rpow, coe_pow] using real.rpow_nat_cast x n lemma mul_rpow {x y : ℝ≥0} {z : ℝ} : (x*y)^z = x^z * y^z := nnreal.eq $ real.mul_rpow x.2 y.2 lemma rpow_le_rpow {x y : ℝ≥0} {z: ℝ} (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z := real.rpow_le_rpow x.2 h₁ h₂ lemma rpow_lt_rpow {x y : ℝ≥0} {z: ℝ} (h₁ : x < y) (h₂ : 0 < z) : x^z < y^z := real.rpow_lt_rpow x.2 h₁ h₂ lemma rpow_lt_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := real.rpow_lt_rpow_iff x.2 y.2 hz lemma rpow_le_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := real.rpow_le_rpow_iff x.2 y.2 hz lemma rpow_lt_rpow_of_exponent_lt {x : ℝ≥0} {y z : ℝ} (hx : 1 < x) (hyz : y < z) : x^y < x^z := real.rpow_lt_rpow_of_exponent_lt hx hyz lemma rpow_le_rpow_of_exponent_le {x : ℝ≥0} {y z : ℝ} (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z := real.rpow_le_rpow_of_exponent_le hx hyz lemma rpow_lt_rpow_of_exponent_gt {x : ℝ≥0} {y z : ℝ} (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x^y < x^z := real.rpow_lt_rpow_of_exponent_gt hx0 hx1 hyz lemma rpow_le_rpow_of_exponent_ge {x : ℝ≥0} {y z : ℝ} (hx0 : 0 < x) (hx1 : x ≤ 1) (hyz : z ≤ y) : x^y ≤ x^z := real.rpow_le_rpow_of_exponent_ge hx0 hx1 hyz lemma rpow_lt_one {x : ℝ≥0} {z : ℝ} (hx : 0 ≤ x) (hx1 : x < 1) (hz : 0 < z) : x^z < 1 := real.rpow_lt_one hx hx1 hz lemma rpow_le_one {x : ℝ≥0} {z : ℝ} (hx2 : x ≤ 1) (hz : 0 ≤ z) : x^z ≤ 1 := real.rpow_le_one x.2 hx2 hz lemma rpow_lt_one_of_one_lt_of_neg {x : ℝ≥0} {z : ℝ} (hx : 1 < x) (hz : z < 0) : x^z < 1 := real.rpow_lt_one_of_one_lt_of_neg hx hz lemma rpow_le_one_of_one_le_of_nonpos {x : ℝ≥0} {z : ℝ} (hx : 1 ≤ x) (hz : z ≤ 0) : x^z ≤ 1 := real.rpow_le_one_of_one_le_of_nonpos hx hz lemma one_lt_rpow {x : ℝ≥0} {z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z := real.one_lt_rpow hx hz lemma one_le_rpow {x : ℝ≥0} {z : ℝ} (h : 1 ≤ x) (h₁ : 0 ≤ z) : 1 ≤ x^z := real.one_le_rpow h h₁ lemma one_lt_rpow_of_pos_of_lt_one_of_neg {x : ℝ≥0} {z : ℝ} (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x^z := real.one_lt_rpow_of_pos_of_lt_one_of_neg hx1 hx2 hz lemma one_le_rpow_of_pos_of_le_one_of_nonpos {x : ℝ≥0} {z : ℝ} (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z ≤ 0) : 1 ≤ x^z := real.one_le_rpow_of_pos_of_le_one_of_nonpos hx1 hx2 hz lemma pow_nat_rpow_nat_inv (x : ℝ≥0) {n : ℕ} (hn : 0 < n) : (x ^ n) ^ (n⁻¹ : ℝ) = x := by { rw [← nnreal.coe_eq, coe_rpow, nnreal.coe_pow], exact real.pow_nat_rpow_nat_inv x.2 hn } lemma rpow_nat_inv_pow_nat (x : ℝ≥0) {n : ℕ} (hn : 0 < n) : (x ^ (n⁻¹ : ℝ)) ^ n = x := by { rw [← nnreal.coe_eq, nnreal.coe_pow, coe_rpow], exact real.rpow_nat_inv_pow_nat x.2 hn } lemma continuous_at_rpow {x : ℝ≥0} {y : ℝ} (h : x ≠ 0 ∨ 0 < y) : continuous_at (λp:ℝ≥0×ℝ, p.1^p.2) (x, y) := begin have : (λp:ℝ≥0×ℝ, p.1^p.2) = real.to_nnreal ∘ (λp:ℝ×ℝ, p.1^p.2) ∘ (λp:ℝ≥0 × ℝ, (p.1.1, p.2)), { ext p, rw [coe_rpow, real.coe_to_nnreal _ (real.rpow_nonneg_of_nonneg p.1.2 _)], refl }, rw this, refine nnreal.continuous_of_real.continuous_at.comp (continuous_at.comp _ _), { apply real.continuous_at_rpow, simp at h, rw ← (nnreal.coe_eq_zero x) at h, exact h }, { exact ((continuous_subtype_val.comp continuous_fst).prod_mk continuous_snd).continuous_at } end lemma _root_.real.to_nnreal_rpow_of_nonneg {x y : ℝ} (hx : 0 ≤ x) : real.to_nnreal (x ^ y) = (real.to_nnreal x) ^ y := begin nth_rewrite 0 ← real.coe_to_nnreal x hx, rw [←nnreal.coe_rpow, real.to_nnreal_coe], end end nnreal open filter lemma filter.tendsto.nnrpow {α : Type*} {f : filter α} {u : α → ℝ≥0} {v : α → ℝ} {x : ℝ≥0} {y : ℝ} (hx : tendsto u f (𝓝 x)) (hy : tendsto v f (𝓝 y)) (h : x ≠ 0 ∨ 0 < y) : tendsto (λ a, (u a) ^ (v a)) f (𝓝 (x ^ y)) := tendsto.comp (nnreal.continuous_at_rpow h) (hx.prod_mk_nhds hy) namespace nnreal lemma continuous_at_rpow_const {x : ℝ≥0} {y : ℝ} (h : x ≠ 0 ∨ 0 ≤ y) : continuous_at (λ z, z^y) x := h.elim (λ h, tendsto_id.nnrpow tendsto_const_nhds (or.inl h)) $ λ h, h.eq_or_lt.elim (λ h, h ▸ by simp only [rpow_zero, continuous_at_const]) (λ h, tendsto_id.nnrpow tendsto_const_nhds (or.inr h)) lemma continuous_rpow_const {y : ℝ} (h : 0 ≤ y) : continuous (λ x : ℝ≥0, x^y) := continuous_iff_continuous_at.2 $ λ x, continuous_at_rpow_const (or.inr h) theorem tendsto_rpow_at_top {y : ℝ} (hy : 0 < y) : tendsto (λ (x : ℝ≥0), x ^ y) at_top at_top := begin rw filter.tendsto_at_top_at_top, intros b, obtain ⟨c, hc⟩ := tendsto_at_top_at_top.mp (tendsto_rpow_at_top hy) b, use c.to_nnreal, intros a ha, exact_mod_cast hc a (real.to_nnreal_le_iff_le_coe.mp ha), end end nnreal namespace ennreal /-- The real power function `x^y` on extended nonnegative reals, defined for `x : ℝ≥0∞` and `y : ℝ` as the restriction of the real power function if `0 < x < ⊤`, and with the natural values for `0` and `⊤` (i.e., `0 ^ x = 0` for `x > 0`, `1` for `x = 0` and `⊤` for `x < 0`, and `⊤ ^ x = 1 / 0 ^ x`). -/ noncomputable def rpow : ℝ≥0∞ → ℝ → ℝ≥0∞ | (some x) y := if x = 0 ∧ y < 0 then ⊤ else (x ^ y : ℝ≥0) | none y := if 0 < y then ⊤ else if y = 0 then 1 else 0 noncomputable instance : has_pow ℝ≥0∞ ℝ := ⟨rpow⟩ @[simp] lemma rpow_eq_pow (x : ℝ≥0∞) (y : ℝ) : rpow x y = x ^ y := rfl @[simp] lemma rpow_zero {x : ℝ≥0∞} : x ^ (0 : ℝ) = 1 := by cases x; { dsimp only [(^), rpow], simp [lt_irrefl] } lemma top_rpow_def (y : ℝ) : (⊤ : ℝ≥0∞) ^ y = if 0 < y then ⊤ else if y = 0 then 1 else 0 := rfl @[simp] lemma top_rpow_of_pos {y : ℝ} (h : 0 < y) : (⊤ : ℝ≥0∞) ^ y = ⊤ := by simp [top_rpow_def, h] @[simp] lemma top_rpow_of_neg {y : ℝ} (h : y < 0) : (⊤ : ℝ≥0∞) ^ y = 0 := by simp [top_rpow_def, asymm h, ne_of_lt h] @[simp] lemma zero_rpow_of_pos {y : ℝ} (h : 0 < y) : (0 : ℝ≥0∞) ^ y = 0 := begin rw [← ennreal.coe_zero, ← ennreal.some_eq_coe], dsimp only [(^), rpow], simp [h, asymm h, ne_of_gt h], end @[simp] lemma zero_rpow_of_neg {y : ℝ} (h : y < 0) : (0 : ℝ≥0∞) ^ y = ⊤ := begin rw [← ennreal.coe_zero, ← ennreal.some_eq_coe], dsimp only [(^), rpow], simp [h, ne_of_gt h], end lemma zero_rpow_def (y : ℝ) : (0 : ℝ≥0∞) ^ y = if 0 < y then 0 else if y = 0 then 1 else ⊤ := begin rcases lt_trichotomy 0 y with H|rfl|H, { simp [H, ne_of_gt, zero_rpow_of_pos, lt_irrefl] }, { simp [lt_irrefl] }, { simp [H, asymm H, ne_of_lt, zero_rpow_of_neg] } end @[simp] lemma zero_rpow_mul_self (y : ℝ) : (0 : ℝ≥0∞) ^ y * 0 ^ y = 0 ^ y := by { rw zero_rpow_def, split_ifs, exacts [zero_mul _, one_mul _, top_mul_top] } @[norm_cast] lemma coe_rpow_of_ne_zero {x : ℝ≥0} (h : x ≠ 0) (y : ℝ) : (x : ℝ≥0∞) ^ y = (x ^ y : ℝ≥0) := begin rw [← ennreal.some_eq_coe], dsimp only [(^), rpow], simp [h] end @[norm_cast] lemma coe_rpow_of_nonneg (x : ℝ≥0) {y : ℝ} (h : 0 ≤ y) : (x : ℝ≥0∞) ^ y = (x ^ y : ℝ≥0) := begin by_cases hx : x = 0, { rcases le_iff_eq_or_lt.1 h with H|H, { simp [hx, H.symm] }, { simp [hx, zero_rpow_of_pos H, nnreal.zero_rpow (ne_of_gt H)] } }, { exact coe_rpow_of_ne_zero hx _ } end lemma coe_rpow_def (x : ℝ≥0) (y : ℝ) : (x : ℝ≥0∞) ^ y = if x = 0 ∧ y < 0 then ⊤ else (x ^ y : ℝ≥0) := rfl @[simp] lemma rpow_one (x : ℝ≥0∞) : x ^ (1 : ℝ) = x := by cases x; dsimp only [(^), rpow]; simp [zero_lt_one, not_lt_of_le zero_le_one] @[simp] lemma one_rpow (x : ℝ) : (1 : ℝ≥0∞) ^ x = 1 := by { rw [← coe_one, coe_rpow_of_ne_zero one_ne_zero], simp } @[simp] lemma rpow_eq_zero_iff {x : ℝ≥0∞} {y : ℝ} : x ^ y = 0 ↔ (x = 0 ∧ 0 < y) ∨ (x = ⊤ ∧ y < 0) := begin cases x, { rcases lt_trichotomy y 0 with H|H|H; simp [H, top_rpow_of_neg, top_rpow_of_pos, le_of_lt] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with H|H|H; simp [h, H, zero_rpow_of_neg, zero_rpow_of_pos, le_of_lt] }, { simp [coe_rpow_of_ne_zero h, h] } } end @[simp] lemma rpow_eq_top_iff {x : ℝ≥0∞} {y : ℝ} : x ^ y = ⊤ ↔ (x = 0 ∧ y < 0) ∨ (x = ⊤ ∧ 0 < y) := begin cases x, { rcases lt_trichotomy y 0 with H|H|H; simp [H, top_rpow_of_neg, top_rpow_of_pos, le_of_lt] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with H|H|H; simp [h, H, zero_rpow_of_neg, zero_rpow_of_pos, le_of_lt] }, { simp [coe_rpow_of_ne_zero h, h] } } end lemma rpow_eq_top_iff_of_pos {x : ℝ≥0∞} {y : ℝ} (hy : 0 < y) : x ^ y = ⊤ ↔ x = ⊤ := by simp [rpow_eq_top_iff, hy, asymm hy] lemma rpow_eq_top_of_nonneg (x : ℝ≥0∞) {y : ℝ} (hy0 : 0 ≤ y) : x ^ y = ⊤ → x = ⊤ := begin rw ennreal.rpow_eq_top_iff, intro h, cases h, { exfalso, rw lt_iff_not_ge at h, exact h.right hy0, }, { exact h.left, }, end lemma rpow_ne_top_of_nonneg {x : ℝ≥0∞} {y : ℝ} (hy0 : 0 ≤ y) (h : x ≠ ⊤) : x ^ y ≠ ⊤ := mt (ennreal.rpow_eq_top_of_nonneg x hy0) h lemma rpow_lt_top_of_nonneg {x : ℝ≥0∞} {y : ℝ} (hy0 : 0 ≤ y) (h : x ≠ ⊤) : x ^ y < ⊤ := lt_top_iff_ne_top.mpr (ennreal.rpow_ne_top_of_nonneg hy0 h) lemma rpow_add {x : ℝ≥0∞} (y z : ℝ) (hx : x ≠ 0) (h'x : x ≠ ⊤) : x ^ (y + z) = x ^ y * x ^ z := begin cases x, { exact (h'x rfl).elim }, have : x ≠ 0 := λ h, by simpa [h] using hx, simp [coe_rpow_of_ne_zero this, nnreal.rpow_add this] end lemma rpow_neg (x : ℝ≥0∞) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ := begin cases x, { rcases lt_trichotomy y 0 with H|H|H; simp [top_rpow_of_pos, top_rpow_of_neg, H, neg_pos.mpr] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with H|H|H; simp [h, zero_rpow_of_pos, zero_rpow_of_neg, H, neg_pos.mpr] }, { have A : x ^ y ≠ 0, by simp [h], simp [coe_rpow_of_ne_zero h, ← coe_inv A, nnreal.rpow_neg] } } end lemma rpow_sub {x : ℝ≥0∞} (y z : ℝ) (hx : x ≠ 0) (h'x : x ≠ ⊤) : x ^ (y - z) = x ^ y / x ^ z := by rw [sub_eq_add_neg, rpow_add _ _ hx h'x, rpow_neg, div_eq_mul_inv] lemma rpow_neg_one (x : ℝ≥0∞) : x ^ (-1 : ℝ) = x ⁻¹ := by simp [rpow_neg] lemma rpow_mul (x : ℝ≥0∞) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := begin cases x, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [Hy, Hz, zero_rpow_of_neg, zero_rpow_of_pos, top_rpow_of_neg, top_rpow_of_pos, mul_pos_of_neg_of_neg, mul_neg_of_neg_of_pos, mul_neg_of_pos_of_neg] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [h, Hy, Hz, zero_rpow_of_neg, zero_rpow_of_pos, top_rpow_of_neg, top_rpow_of_pos, mul_pos_of_neg_of_neg, mul_neg_of_neg_of_pos, mul_neg_of_pos_of_neg] }, { have : x ^ y ≠ 0, by simp [h], simp [coe_rpow_of_ne_zero h, coe_rpow_of_ne_zero this, nnreal.rpow_mul] } } end @[simp, norm_cast] lemma rpow_nat_cast (x : ℝ≥0∞) (n : ℕ) : x ^ (n : ℝ) = x ^ n := begin cases x, { cases n; simp [top_rpow_of_pos (nat.cast_add_one_pos _), top_pow (nat.succ_pos _)] }, { simp [coe_rpow_of_nonneg _ (nat.cast_nonneg n)] } end lemma mul_rpow_eq_ite (x y : ℝ≥0∞) (z : ℝ) : (x * y) ^ z = if (x = 0 ∧ y = ⊤ ∨ x = ⊤ ∧ y = 0) ∧ z < 0 then ⊤ else x ^ z * y ^ z := begin rcases eq_or_ne z 0 with rfl|hz, { simp }, replace hz := hz.lt_or_lt, wlog hxy : x ≤ y := le_total x y using [x y, y x] tactic.skip, { rcases eq_or_ne x 0 with rfl|hx0, { induction y using with_top.rec_top_coe; cases hz with hz hz; simp [*, hz.not_lt] }, rcases eq_or_ne y 0 with rfl|hy0, { exact (hx0 (bot_unique hxy)).elim }, induction x using with_top.rec_top_coe, { cases hz with hz hz; simp [hz, top_unique hxy] }, induction y using with_top.rec_top_coe, { cases hz with hz hz; simp * }, simp only [*, false_and, and_false, false_or, if_false], norm_cast at *, rw [coe_rpow_of_ne_zero (mul_ne_zero hx0 hy0), nnreal.mul_rpow] }, { convert this using 2; simp only [mul_comm, and_comm, or_comm] } end lemma mul_rpow_of_ne_top {x y : ℝ≥0∞} (hx : x ≠ ⊤) (hy : y ≠ ⊤) (z : ℝ) : (x * y) ^ z = x^z * y^z := by simp [*, mul_rpow_eq_ite] @[norm_cast] lemma coe_mul_rpow (x y : ℝ≥0) (z : ℝ) : ((x : ℝ≥0∞) * y) ^ z = x^z * y^z := mul_rpow_of_ne_top coe_ne_top coe_ne_top z lemma mul_rpow_of_ne_zero {x y : ℝ≥0∞} (hx : x ≠ 0) (hy : y ≠ 0) (z : ℝ) : (x * y) ^ z = x ^ z * y ^ z := by simp [*, mul_rpow_eq_ite] lemma mul_rpow_of_nonneg (x y : ℝ≥0∞) {z : ℝ} (hz : 0 ≤ z) : (x * y) ^ z = x ^ z * y ^ z := by simp [hz.not_lt, mul_rpow_eq_ite] lemma inv_rpow (x : ℝ≥0∞) (y : ℝ) : (x⁻¹) ^ y = (x ^ y)⁻¹ := begin rcases eq_or_ne y 0 with rfl|hy, { simp only [rpow_zero, inv_one] }, replace hy := hy.lt_or_lt, rcases eq_or_ne x 0 with rfl|h0, { cases hy; simp * }, rcases eq_or_ne x ⊤ with rfl|h_top, { cases hy; simp * }, apply eq_inv_of_mul_eq_one, rw [← mul_rpow_of_ne_zero (inv_ne_zero.2 h_top) h0, inv_mul_cancel h0 h_top, one_rpow] end lemma div_rpow_of_nonneg (x y : ℝ≥0∞) {z : ℝ} (hz : 0 ≤ z) : (x / y) ^ z = x ^ z / y ^ z := by rw [div_eq_mul_inv, mul_rpow_of_nonneg _ _ hz, inv_rpow, div_eq_mul_inv] lemma strict_mono_rpow_of_pos {z : ℝ} (h : 0 < z) : strict_mono (λ x : ℝ≥0∞, x ^ z) := begin intros x y hxy, lift x to ℝ≥0 using ne_top_of_lt hxy, rcases eq_or_ne y ∞ with rfl|hy, { simp only [top_rpow_of_pos h, coe_rpow_of_nonneg _ h.le, coe_lt_top] }, { lift y to ℝ≥0 using hy, simp only [coe_rpow_of_nonneg _ h.le, nnreal.rpow_lt_rpow (coe_lt_coe.1 hxy) h, coe_lt_coe] } end lemma monotone_rpow_of_nonneg {z : ℝ} (h : 0 ≤ z) : monotone (λ x : ℝ≥0∞, x ^ z) := h.eq_or_lt.elim (λ h0, h0 ▸ by simp only [rpow_zero, monotone_const]) (λ h0, (strict_mono_rpow_of_pos h0).monotone) lemma rpow_le_rpow {x y : ℝ≥0∞} {z : ℝ} (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z := monotone_rpow_of_nonneg h₂ h₁ lemma rpow_lt_rpow {x y : ℝ≥0∞} {z : ℝ} (h₁ : x < y) (h₂ : 0 < z) : x^z < y^z := strict_mono_rpow_of_pos h₂ h₁ lemma rpow_le_rpow_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := (strict_mono_rpow_of_pos hz).le_iff_le lemma rpow_lt_rpow_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := (strict_mono_rpow_of_pos hz).lt_iff_lt lemma le_rpow_one_div_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ≤ y ^ (1 / z) ↔ x ^ z ≤ y := begin nth_rewrite 0 ←rpow_one x, nth_rewrite 0 ←@_root_.mul_inv_cancel _ _ z hz.ne', rw [rpow_mul, ←one_div, @rpow_le_rpow_iff _ _ (1/z) (by simp [hz])], end lemma lt_rpow_one_div_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x < y ^ (1 / z) ↔ x ^ z < y := begin nth_rewrite 0 ←rpow_one x, nth_rewrite 0 ←@_root_.mul_inv_cancel _ _ z (ne_of_lt hz).symm, rw [rpow_mul, ←one_div, @rpow_lt_rpow_iff _ _ (1/z) (by simp [hz])], end lemma rpow_lt_rpow_of_exponent_lt {x : ℝ≥0∞} {y z : ℝ} (hx : 1 < x) (hx' : x ≠ ⊤) (hyz : y < z) : x^y < x^z := begin lift x to ℝ≥0 using hx', rw [one_lt_coe_iff] at hx, simp [coe_rpow_of_ne_zero (ne_of_gt (lt_trans zero_lt_one hx)), nnreal.rpow_lt_rpow_of_exponent_lt hx hyz] end lemma rpow_le_rpow_of_exponent_le {x : ℝ≥0∞} {y z : ℝ} (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z := begin cases x, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [Hy, Hz, top_rpow_of_neg, top_rpow_of_pos, le_refl]; linarith }, { simp only [one_le_coe_iff, some_eq_coe] at hx, simp [coe_rpow_of_ne_zero (ne_of_gt (lt_of_lt_of_le zero_lt_one hx)), nnreal.rpow_le_rpow_of_exponent_le hx hyz] } end lemma rpow_lt_rpow_of_exponent_gt {x : ℝ≥0∞} {y z : ℝ} (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x^y < x^z := begin lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx1 le_top), simp at hx0 hx1, simp [coe_rpow_of_ne_zero (ne_of_gt hx0), nnreal.rpow_lt_rpow_of_exponent_gt hx0 hx1 hyz] end lemma rpow_le_rpow_of_exponent_ge {x : ℝ≥0∞} {y z : ℝ} (hx1 : x ≤ 1) (hyz : z ≤ y) : x^y ≤ x^z := begin lift x to ℝ≥0 using ne_of_lt (lt_of_le_of_lt hx1 coe_lt_top), by_cases h : x = 0, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [Hy, Hz, h, zero_rpow_of_neg, zero_rpow_of_pos, le_refl]; linarith }, { simp at hx1, simp [coe_rpow_of_ne_zero h, nnreal.rpow_le_rpow_of_exponent_ge (bot_lt_iff_ne_bot.mpr h) hx1 hyz] } end lemma rpow_le_self_of_le_one {x : ℝ≥0∞} {z : ℝ} (hx : x ≤ 1) (h_one_le : 1 ≤ z) : x ^ z ≤ x := begin nth_rewrite 1 ←ennreal.rpow_one x, exact ennreal.rpow_le_rpow_of_exponent_ge hx h_one_le, end lemma le_rpow_self_of_one_le {x : ℝ≥0∞} {z : ℝ} (hx : 1 ≤ x) (h_one_le : 1 ≤ z) : x ≤ x ^ z := begin nth_rewrite 0 ←ennreal.rpow_one x, exact ennreal.rpow_le_rpow_of_exponent_le hx h_one_le, end lemma rpow_pos_of_nonneg {p : ℝ} {x : ℝ≥0∞} (hx_pos : 0 < x) (hp_nonneg : 0 ≤ p) : 0 < x^p := begin by_cases hp_zero : p = 0, { simp [hp_zero, ennreal.zero_lt_one], }, { rw ←ne.def at hp_zero, have hp_pos := lt_of_le_of_ne hp_nonneg hp_zero.symm, rw ←zero_rpow_of_pos hp_pos, exact rpow_lt_rpow hx_pos hp_pos, }, end lemma rpow_pos {p : ℝ} {x : ℝ≥0∞} (hx_pos : 0 < x) (hx_ne_top : x ≠ ⊤) : 0 < x^p := begin cases lt_or_le 0 p with hp_pos hp_nonpos, { exact rpow_pos_of_nonneg hx_pos (le_of_lt hp_pos), }, { rw [←neg_neg p, rpow_neg, inv_pos], exact rpow_ne_top_of_nonneg (by simp [hp_nonpos]) hx_ne_top, }, end lemma rpow_lt_one {x : ℝ≥0∞} {z : ℝ} (hx : x < 1) (hz : 0 < z) : x^z < 1 := begin lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx le_top), simp only [coe_lt_one_iff] at hx, simp [coe_rpow_of_nonneg _ (le_of_lt hz), nnreal.rpow_lt_one (zero_le x) hx hz], end lemma rpow_le_one {x : ℝ≥0∞} {z : ℝ} (hx : x ≤ 1) (hz : 0 ≤ z) : x^z ≤ 1 := begin lift x to ℝ≥0 using ne_of_lt (lt_of_le_of_lt hx coe_lt_top), simp only [coe_le_one_iff] at hx, simp [coe_rpow_of_nonneg _ hz, nnreal.rpow_le_one hx hz], end lemma rpow_lt_one_of_one_lt_of_neg {x : ℝ≥0∞} {z : ℝ} (hx : 1 < x) (hz : z < 0) : x^z < 1 := begin cases x, { simp [top_rpow_of_neg hz, ennreal.zero_lt_one] }, { simp only [some_eq_coe, one_lt_coe_iff] at hx, simp [coe_rpow_of_ne_zero (ne_of_gt (lt_trans zero_lt_one hx)), nnreal.rpow_lt_one_of_one_lt_of_neg hx hz] }, end lemma rpow_le_one_of_one_le_of_neg {x : ℝ≥0∞} {z : ℝ} (hx : 1 ≤ x) (hz : z < 0) : x^z ≤ 1 := begin cases x, { simp [top_rpow_of_neg hz, ennreal.zero_lt_one] }, { simp only [one_le_coe_iff, some_eq_coe] at hx, simp [coe_rpow_of_ne_zero (ne_of_gt (lt_of_lt_of_le zero_lt_one hx)), nnreal.rpow_le_one_of_one_le_of_nonpos hx (le_of_lt hz)] }, end lemma one_lt_rpow {x : ℝ≥0∞} {z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z := begin cases x, { simp [top_rpow_of_pos hz] }, { simp only [some_eq_coe, one_lt_coe_iff] at hx, simp [coe_rpow_of_nonneg _ (le_of_lt hz), nnreal.one_lt_rpow hx hz] } end lemma one_le_rpow {x : ℝ≥0∞} {z : ℝ} (hx : 1 ≤ x) (hz : 0 < z) : 1 ≤ x^z := begin cases x, { simp [top_rpow_of_pos hz] }, { simp only [one_le_coe_iff, some_eq_coe] at hx, simp [coe_rpow_of_nonneg _ (le_of_lt hz), nnreal.one_le_rpow hx (le_of_lt hz)] }, end lemma one_lt_rpow_of_pos_of_lt_one_of_neg {x : ℝ≥0∞} {z : ℝ} (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x^z := begin lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx2 le_top), simp only [coe_lt_one_iff, coe_pos] at ⊢ hx1 hx2, simp [coe_rpow_of_ne_zero (ne_of_gt hx1), nnreal.one_lt_rpow_of_pos_of_lt_one_of_neg hx1 hx2 hz], end lemma one_le_rpow_of_pos_of_le_one_of_neg {x : ℝ≥0∞} {z : ℝ} (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z < 0) : 1 ≤ x^z := begin lift x to ℝ≥0 using ne_of_lt (lt_of_le_of_lt hx2 coe_lt_top), simp only [coe_le_one_iff, coe_pos] at ⊢ hx1 hx2, simp [coe_rpow_of_ne_zero (ne_of_gt hx1), nnreal.one_le_rpow_of_pos_of_le_one_of_nonpos hx1 hx2 (le_of_lt hz)], end lemma to_nnreal_rpow (x : ℝ≥0∞) (z : ℝ) : (x.to_nnreal) ^ z = (x ^ z).to_nnreal := begin rcases lt_trichotomy z 0 with H|H|H, { cases x, { simp [H, ne_of_lt] }, by_cases hx : x = 0, { simp [hx, H, ne_of_lt] }, { simp [coe_rpow_of_ne_zero hx] } }, { simp [H] }, { cases x, { simp [H, ne_of_gt] }, simp [coe_rpow_of_nonneg _ (le_of_lt H)] } end lemma to_real_rpow (x : ℝ≥0∞) (z : ℝ) : (x.to_real) ^ z = (x ^ z).to_real := by rw [ennreal.to_real, ennreal.to_real, ←nnreal.coe_rpow, ennreal.to_nnreal_rpow] lemma of_real_rpow_of_pos {x p : ℝ} (hx_pos : 0 < x) : ennreal.of_real x ^ p = ennreal.of_real (x ^ p) := begin simp_rw ennreal.of_real, rw [coe_rpow_of_ne_zero, coe_eq_coe, real.to_nnreal_rpow_of_nonneg hx_pos.le], simp [hx_pos], end lemma of_real_rpow_of_nonneg {x p : ℝ} (hx_nonneg : 0 ≤ x) (hp_nonneg : 0 ≤ p) : ennreal.of_real x ^ p = ennreal.of_real (x ^ p) := begin by_cases hp0 : p = 0, { simp [hp0], }, by_cases hx0 : x = 0, { rw ← ne.def at hp0, have hp_pos : 0 < p := lt_of_le_of_ne hp_nonneg hp0.symm, simp [hx0, hp_pos, hp_pos.ne.symm], }, rw ← ne.def at hx0, exact of_real_rpow_of_pos (hx_nonneg.lt_of_ne hx0.symm), end lemma rpow_left_injective {x : ℝ} (hx : x ≠ 0) : function.injective (λ y : ℝ≥0∞, y^x) := begin intros y z hyz, dsimp only at hyz, rw [←rpow_one y, ←rpow_one z, ←_root_.mul_inv_cancel hx, rpow_mul, rpow_mul, hyz], end lemma rpow_left_surjective {x : ℝ} (hx : x ≠ 0) : function.surjective (λ y : ℝ≥0∞, y^x) := λ y, ⟨y ^ x⁻¹, by simp_rw [←rpow_mul, _root_.inv_mul_cancel hx, rpow_one]⟩ lemma rpow_left_bijective {x : ℝ} (hx : x ≠ 0) : function.bijective (λ y : ℝ≥0∞, y^x) := ⟨rpow_left_injective hx, rpow_left_surjective hx⟩ lemma rpow_left_monotone_of_nonneg {x : ℝ} (hx : 0 ≤ x) : monotone (λ y : ℝ≥0∞, y^x) := λ y z hyz, rpow_le_rpow hyz hx lemma rpow_left_strict_mono_of_pos {x : ℝ} (hx : 0 < x) : strict_mono (λ y : ℝ≥0∞, y^x) := λ y z hyz, rpow_lt_rpow hyz hx theorem tendsto_rpow_at_top {y : ℝ} (hy : 0 < y) : tendsto (λ (x : ℝ≥0∞), x ^ y) (𝓝 ⊤) (𝓝 ⊤) := begin rw tendsto_nhds_top_iff_nnreal, intros x, obtain ⟨c, _, hc⟩ := (at_top_basis_Ioi.tendsto_iff at_top_basis_Ioi).mp (nnreal.tendsto_rpow_at_top hy) x trivial, have hc' : set.Ioi (↑c) ∈ 𝓝 (⊤ : ℝ≥0∞) := Ioi_mem_nhds coe_lt_top, refine eventually_of_mem hc' _, intros a ha, by_cases ha' : a = ⊤, { simp [ha', hy] }, lift a to ℝ≥0 using ha', change ↑c < ↑a at ha, rw coe_rpow_of_nonneg _ hy.le, exact_mod_cast hc a (by exact_mod_cast ha), end private lemma continuous_at_rpow_const_of_pos {x : ℝ≥0∞} {y : ℝ} (h : 0 < y) : continuous_at (λ a : ennreal, a ^ y) x := begin by_cases hx : x = ⊤, { rw [hx, continuous_at], convert tendsto_rpow_at_top h, simp [h] }, lift x to ℝ≥0 using hx, rw continuous_at_coe_iff, convert continuous_coe.continuous_at.comp (nnreal.continuous_at_rpow_const (or.inr h.le)) using 1, ext1 x, simp [coe_rpow_of_nonneg _ h.le] end @[continuity] lemma continuous_rpow_const {y : ℝ} : continuous (λ a : ennreal, a ^ y) := begin apply continuous_iff_continuous_at.2 (λ x, _), rcases lt_trichotomy 0 y with hy|rfl|hy, { exact continuous_at_rpow_const_of_pos hy }, { simp, exact continuous_at_const }, { obtain ⟨z, hz⟩ : ∃ z, y = -z := ⟨-y, (neg_neg _).symm⟩, have z_pos : 0 < z, by simpa [hz] using hy, simp_rw [hz, rpow_neg], exact ennreal.continuous_inv.continuous_at.comp (continuous_at_rpow_const_of_pos z_pos) } end lemma tendsto_const_mul_rpow_nhds_zero_of_pos {c : ℝ≥0∞} (hc : c ≠ ∞) {y : ℝ} (hy : 0 < y) : tendsto (λ x : ℝ≥0∞, c * x ^ y) (𝓝 0) (𝓝 0) := begin convert ennreal.tendsto.const_mul (ennreal.continuous_rpow_const.tendsto 0) _, { simp [hy] }, { exact or.inr hc } end end ennreal
39c9234bca86306dce7c8f4ecd6cdf2cbfab03c3
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/order/lattice.lean
bbef4ceacb155a6a98df5f3e5994926cc762dd13
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
26,835
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import order.rel_classes /-! # (Semi-)lattices Semilattices are partially ordered sets with join (greatest lower bound, or `sup`) or meet (least upper bound, or `inf`) operations. Lattices are posets that are both join-semilattices and meet-semilattices. Distributive lattices are lattices which satisfy any of four equivalent distributivity properties, of `sup` over `inf`, on the left or on the right. ## Main declarations * `has_sup`: type class for the `⊔` notation * `has_inf`: type class for the `⊓` notation * `semilattice_sup`: a type class for join semilattices * `semilattice_sup.mk'`: an alternative constructor for `semilattice_sup` via proofs that `⊔` is commutative, associative and idempotent. * `semilattice_inf`: a type class for meet semilattices * `semilattice_sup.mk'`: an alternative constructor for `semilattice_inf` via proofs that `⊓` is commutative, associative and idempotent. * `lattice`: a type class for lattices * `lattice.mk'`: an alternative constructor for `lattice` via profs that `⊔` and `⊓` are commutative, associative and satisfy a pair of "absorption laws". * `distrib_lattice`: a type class for distributive lattices. ## Notations * `a ⊔ b`: the supremum or join of `a` and `b` * `a ⊓ b`: the infimum or meet of `a` and `b` ## TODO * (Semi-)lattice homomorphisms * Alternative constructors for distributive lattices from the other distributive properties ## Tags semilattice, lattice -/ set_option old_structure_cmd true universes u v w variables {α : Type u} {β : Type v} -- TODO: move this eventually, if we decide to use them attribute [ematch] le_trans lt_of_le_of_lt lt_of_lt_of_le lt_trans section -- TODO: this seems crazy, but it also seems to work reasonably well @[ematch] theorem le_antisymm' [partial_order α] : ∀ {a b : α}, (: a ≤ b :) → b ≤ a → a = b := @le_antisymm _ _ end /- TODO: automatic construction of dual definitions / theorems -/ /-- Typeclass for the `⊔` (`\lub`) notation -/ class has_sup (α : Type u) := (sup : α → α → α) /-- Typeclass for the `⊓` (`\glb`) notation -/ class has_inf (α : Type u) := (inf : α → α → α) infix ⊔ := has_sup.sup infix ⊓ := has_inf.inf /-! ### Join-semilattices -/ /-- A `semilattice_sup` is a join-semilattice, that is, a partial order with a join (a.k.a. lub / least upper bound, sup / supremum) operation `⊔` which is the least element larger than both factors. -/ class semilattice_sup (α : Type u) extends has_sup α, partial_order α := (le_sup_left : ∀ a b : α, a ≤ a ⊔ b) (le_sup_right : ∀ a b : α, b ≤ a ⊔ b) (sup_le : ∀ a b c : α, a ≤ c → b ≤ c → a ⊔ b ≤ c) /-- A type with a commutative, associative and idempotent binary `sup` operation has the structure of a join-semilattice. The partial order is defined so that `a ≤ b` unfolds to `a ⊔ b = b`; cf. `sup_eq_right`. -/ def semilattice_sup.mk' {α : Type*} [has_sup α] (sup_comm : ∀ (a b : α), a ⊔ b = b ⊔ a) (sup_assoc : ∀ (a b c : α), a ⊔ b ⊔ c = a ⊔ (b ⊔ c)) (sup_idem : ∀ (a : α), a ⊔ a = a) : semilattice_sup α := { sup := (⊔), le := λ a b, a ⊔ b = b, le_refl := sup_idem, le_trans := λ a b c hab hbc, begin dsimp only [(≤)] at *, rwa [←hbc, ←sup_assoc, hab], end, le_antisymm := λ a b hab hba, begin dsimp only [(≤)] at *, rwa [←hba, sup_comm], end, le_sup_left := λ a b, show a ⊔ (a ⊔ b) = (a ⊔ b), by rw [←sup_assoc, sup_idem], le_sup_right := λ a b, show b ⊔ (a ⊔ b) = (a ⊔ b), by rw [sup_comm, sup_assoc, sup_idem], sup_le := λ a b c hac hbc, begin dsimp only [(≤), preorder.le] at *, rwa [sup_assoc, hbc], end } instance (α : Type*) [has_inf α] : has_sup (order_dual α) := ⟨((⊓) : α → α → α)⟩ instance (α : Type*) [has_sup α] : has_inf (order_dual α) := ⟨((⊔) : α → α → α)⟩ section semilattice_sup variables [semilattice_sup α] {a b c d : α} @[simp] theorem le_sup_left : a ≤ a ⊔ b := semilattice_sup.le_sup_left a b @[ematch] theorem le_sup_left' : a ≤ (: a ⊔ b :) := le_sup_left @[simp] theorem le_sup_right : b ≤ a ⊔ b := semilattice_sup.le_sup_right a b @[ematch] theorem le_sup_right' : b ≤ (: a ⊔ b :) := le_sup_right theorem le_sup_left_of_le (h : c ≤ a) : c ≤ a ⊔ b := le_trans h le_sup_left theorem le_sup_right_of_le (h : c ≤ b) : c ≤ a ⊔ b := le_trans h le_sup_right theorem sup_le : a ≤ c → b ≤ c → a ⊔ b ≤ c := semilattice_sup.sup_le a b c @[simp] theorem sup_le_iff : a ⊔ b ≤ c ↔ a ≤ c ∧ b ≤ c := ⟨assume h : a ⊔ b ≤ c, ⟨le_trans le_sup_left h, le_trans le_sup_right h⟩, assume ⟨h₁, h₂⟩, sup_le h₁ h₂⟩ @[simp] theorem sup_eq_left : a ⊔ b = a ↔ b ≤ a := le_antisymm_iff.trans $ by simp [le_refl] theorem sup_of_le_left (h : b ≤ a) : a ⊔ b = a := sup_eq_left.2 h @[simp] theorem left_eq_sup : a = a ⊔ b ↔ b ≤ a := eq_comm.trans sup_eq_left @[simp] theorem sup_eq_right : a ⊔ b = b ↔ a ≤ b := le_antisymm_iff.trans $ by simp [le_refl] theorem sup_of_le_right (h : a ≤ b) : a ⊔ b = b := sup_eq_right.2 h @[simp] theorem right_eq_sup : b = a ⊔ b ↔ a ≤ b := eq_comm.trans sup_eq_right theorem sup_le_sup (h₁ : a ≤ b) (h₂ : c ≤ d) : a ⊔ c ≤ b ⊔ d := sup_le (le_sup_left_of_le h₁) (le_sup_right_of_le h₂) theorem sup_le_sup_left (h₁ : a ≤ b) (c) : c ⊔ a ≤ c ⊔ b := sup_le_sup (le_refl _) h₁ theorem sup_le_sup_right (h₁ : a ≤ b) (c) : a ⊔ c ≤ b ⊔ c := sup_le_sup h₁ (le_refl _) theorem le_of_sup_eq (h : a ⊔ b = b) : a ≤ b := by { rw ← h, simp } lemma sup_ind [is_total α (≤)] (a b : α) {p : α → Prop} (ha : p a) (hb : p b) : p (a ⊔ b) := (is_total.total a b).elim (λ h : a ≤ b, by rwa sup_eq_right.2 h) (λ h, by rwa sup_eq_left.2 h) @[simp] lemma sup_lt_iff [is_total α (≤)] {a b c : α} : b ⊔ c < a ↔ b < a ∧ c < a := ⟨λ h, ⟨le_sup_left.trans_lt h, le_sup_right.trans_lt h⟩, λ h, sup_ind b c h.1 h.2⟩ @[simp] lemma le_sup_iff [is_total α (≤)] {a b c : α} : a ≤ b ⊔ c ↔ a ≤ b ∨ a ≤ c := ⟨λ h, (total_of (≤) c b).imp (λ bc, by rwa sup_eq_left.2 bc at h) (λ bc, by rwa sup_eq_right.2 bc at h), λ h, h.elim le_sup_left_of_le le_sup_right_of_le⟩ @[simp] lemma lt_sup_iff [is_total α (≤)] {a b c : α} : a < b ⊔ c ↔ a < b ∨ a < c := ⟨λ h, (total_of (≤) c b).imp (λ bc, by rwa sup_eq_left.2 bc at h) (λ bc, by rwa sup_eq_right.2 bc at h), λ h, h.elim (λ h, h.trans_le le_sup_left) (λ h, h.trans_le le_sup_right)⟩ @[simp] theorem sup_idem : a ⊔ a = a := by apply le_antisymm; simp instance sup_is_idempotent : is_idempotent α (⊔) := ⟨@sup_idem _ _⟩ theorem sup_comm : a ⊔ b = b ⊔ a := by apply le_antisymm; simp instance sup_is_commutative : is_commutative α (⊔) := ⟨@sup_comm _ _⟩ theorem sup_assoc : a ⊔ b ⊔ c = a ⊔ (b ⊔ c) := le_antisymm (sup_le (sup_le le_sup_left (le_sup_right_of_le le_sup_left)) (le_sup_right_of_le le_sup_right)) (sup_le (le_sup_left_of_le le_sup_left) (sup_le (le_sup_left_of_le le_sup_right) le_sup_right)) instance sup_is_associative : is_associative α (⊔) := ⟨@sup_assoc _ _⟩ lemma sup_left_right_swap (a b c : α) : a ⊔ b ⊔ c = c ⊔ b ⊔ a := by rw [sup_comm, @sup_comm _ _ a, sup_assoc] @[simp] lemma sup_left_idem : a ⊔ (a ⊔ b) = a ⊔ b := by rw [← sup_assoc, sup_idem] @[simp] lemma sup_right_idem : (a ⊔ b) ⊔ b = a ⊔ b := by rw [sup_assoc, sup_idem] lemma sup_left_comm (a b c : α) : a ⊔ (b ⊔ c) = b ⊔ (a ⊔ c) := by rw [← sup_assoc, ← sup_assoc, @sup_comm α _ a] lemma sup_right_comm (a b c : α) : a ⊔ b ⊔ c = a ⊔ c ⊔ b := by rw [sup_assoc, sup_assoc, @sup_comm _ _ b] lemma forall_le_or_exists_lt_sup (a : α) : (∀b, b ≤ a) ∨ (∃b, a < b) := suffices (∃b, ¬b ≤ a) → (∃b, a < b), by rwa [or_iff_not_imp_left, not_forall], assume ⟨b, hb⟩, ⟨a ⊔ b, lt_of_le_of_ne le_sup_left $ mt left_eq_sup.1 hb⟩ /-- If `f` is a monotonically increasing sequence, `g` is a monotonically decreasing sequence, and `f n ≤ g n` for all `n`, then for all `m`, `n` we have `f m ≤ g n`. -/ theorem forall_le_of_monotone_of_mono_decr {β : Type*} [preorder β] {f g : α → β} (hf : monotone f) (hg : ∀ ⦃m n⦄, m ≤ n → g n ≤ g m) (h : ∀ n, f n ≤ g n) (m n : α) : f m ≤ g n := calc f m ≤ f (m ⊔ n) : hf le_sup_left ... ≤ g (m ⊔ n) : h _ ... ≤ g n : hg le_sup_right theorem semilattice_sup.ext_sup {α} {A B : semilattice_sup α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) (x y : α) : (by haveI := A; exact (x ⊔ y)) = x ⊔ y := eq_of_forall_ge_iff $ λ c, by simp only [sup_le_iff]; rw [← H, @sup_le_iff α A, H, H] theorem semilattice_sup.ext {α} {A B : semilattice_sup α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B := begin have := partial_order.ext H, have ss := funext (λ x, funext $ semilattice_sup.ext_sup H x), casesI A, casesI B, injection this; congr' end end semilattice_sup /-! ### Meet-semilattices -/ /-- A `semilattice_inf` is a meet-semilattice, that is, a partial order with a meet (a.k.a. glb / greatest lower bound, inf / infimum) operation `⊓` which is the greatest element smaller than both factors. -/ class semilattice_inf (α : Type u) extends has_inf α, partial_order α := (inf_le_left : ∀ a b : α, a ⊓ b ≤ a) (inf_le_right : ∀ a b : α, a ⊓ b ≤ b) (le_inf : ∀ a b c : α, a ≤ b → a ≤ c → a ≤ b ⊓ c) instance (α) [semilattice_inf α] : semilattice_sup (order_dual α) := { le_sup_left := semilattice_inf.inf_le_left, le_sup_right := semilattice_inf.inf_le_right, sup_le := assume a b c hca hcb, @semilattice_inf.le_inf α _ _ _ _ hca hcb, .. order_dual.partial_order α, .. order_dual.has_sup α } instance (α) [semilattice_sup α] : semilattice_inf (order_dual α) := { inf_le_left := @le_sup_left α _, inf_le_right := @le_sup_right α _, le_inf := assume a b c hca hcb, @sup_le α _ _ _ _ hca hcb, .. order_dual.partial_order α, .. order_dual.has_inf α } theorem semilattice_sup.dual_dual (α : Type*) [H : semilattice_sup α] : order_dual.semilattice_sup (order_dual α) = H := semilattice_sup.ext $ λ _ _, iff.rfl section semilattice_inf variables [semilattice_inf α] {a b c d : α} @[simp] theorem inf_le_left : a ⊓ b ≤ a := semilattice_inf.inf_le_left a b @[ematch] theorem inf_le_left' : (: a ⊓ b :) ≤ a := semilattice_inf.inf_le_left a b @[simp] theorem inf_le_right : a ⊓ b ≤ b := semilattice_inf.inf_le_right a b @[ematch] theorem inf_le_right' : (: a ⊓ b :) ≤ b := semilattice_inf.inf_le_right a b theorem le_inf : a ≤ b → a ≤ c → a ≤ b ⊓ c := semilattice_inf.le_inf a b c theorem inf_le_left_of_le (h : a ≤ c) : a ⊓ b ≤ c := le_trans inf_le_left h theorem inf_le_right_of_le (h : b ≤ c) : a ⊓ b ≤ c := le_trans inf_le_right h @[simp] theorem le_inf_iff : a ≤ b ⊓ c ↔ a ≤ b ∧ a ≤ c := @sup_le_iff (order_dual α) _ _ _ _ @[simp] theorem inf_eq_left : a ⊓ b = a ↔ a ≤ b := le_antisymm_iff.trans $ by simp [le_refl] theorem inf_of_le_left (h : a ≤ b) : a ⊓ b = a := inf_eq_left.2 h @[simp] theorem left_eq_inf : a = a ⊓ b ↔ a ≤ b := eq_comm.trans inf_eq_left @[simp] theorem inf_eq_right : a ⊓ b = b ↔ b ≤ a := le_antisymm_iff.trans $ by simp [le_refl] theorem inf_of_le_right (h : b ≤ a) : a ⊓ b = b := inf_eq_right.2 h @[simp] theorem right_eq_inf : b = a ⊓ b ↔ b ≤ a := eq_comm.trans inf_eq_right theorem inf_le_inf (h₁ : a ≤ b) (h₂ : c ≤ d) : a ⊓ c ≤ b ⊓ d := le_inf (inf_le_left_of_le h₁) (inf_le_right_of_le h₂) lemma inf_le_inf_right (a : α) {b c : α} (h : b ≤ c) : b ⊓ a ≤ c ⊓ a := inf_le_inf h (le_refl _) lemma inf_le_inf_left (a : α) {b c : α} (h : b ≤ c) : a ⊓ b ≤ a ⊓ c := inf_le_inf (le_refl _) h theorem le_of_inf_eq (h : a ⊓ b = a) : a ≤ b := by { rw ← h, simp } lemma inf_ind [is_total α (≤)] (a b : α) {p : α → Prop} (ha : p a) (hb : p b) : p (a ⊓ b) := @sup_ind (order_dual α) _ _ _ _ _ ha hb @[simp] lemma lt_inf_iff [is_total α (≤)] {a b c : α} : a < b ⊓ c ↔ a < b ∧ a < c := @sup_lt_iff (order_dual α) _ _ _ _ _ @[simp] lemma inf_le_iff [is_total α (≤)] {a b c : α} : b ⊓ c ≤ a ↔ b ≤ a ∨ c ≤ a := @le_sup_iff (order_dual α) _ _ _ _ _ @[simp] theorem inf_idem : a ⊓ a = a := @sup_idem (order_dual α) _ _ instance inf_is_idempotent : is_idempotent α (⊓) := ⟨@inf_idem _ _⟩ theorem inf_comm : a ⊓ b = b ⊓ a := @sup_comm (order_dual α) _ _ _ instance inf_is_commutative : is_commutative α (⊓) := ⟨@inf_comm _ _⟩ theorem inf_assoc : a ⊓ b ⊓ c = a ⊓ (b ⊓ c) := @sup_assoc (order_dual α) _ a b c instance inf_is_associative : is_associative α (⊓) := ⟨@inf_assoc _ _⟩ lemma inf_left_right_swap (a b c : α) : a ⊓ b ⊓ c = c ⊓ b ⊓ a := by rw [inf_comm, @inf_comm _ _ a, inf_assoc] @[simp] lemma inf_left_idem : a ⊓ (a ⊓ b) = a ⊓ b := @sup_left_idem (order_dual α) _ a b @[simp] lemma inf_right_idem : (a ⊓ b) ⊓ b = a ⊓ b := @sup_right_idem (order_dual α) _ a b lemma inf_left_comm (a b c : α) : a ⊓ (b ⊓ c) = b ⊓ (a ⊓ c) := @sup_left_comm (order_dual α) _ a b c lemma inf_right_comm (a b c : α) : a ⊓ b ⊓ c = a ⊓ c ⊓ b := @sup_right_comm (order_dual α) _ a b c lemma forall_le_or_exists_lt_inf (a : α) : (∀b, a ≤ b) ∨ (∃b, b < a) := @forall_le_or_exists_lt_sup (order_dual α) _ a theorem semilattice_inf.ext_inf {α} {A B : semilattice_inf α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) (x y : α) : (by haveI := A; exact (x ⊓ y)) = x ⊓ y := eq_of_forall_le_iff $ λ c, by simp only [le_inf_iff]; rw [← H, @le_inf_iff α A, H, H] theorem semilattice_inf.ext {α} {A B : semilattice_inf α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B := begin have := partial_order.ext H, have ss := funext (λ x, funext $ semilattice_inf.ext_inf H x), casesI A, casesI B, injection this; congr' end theorem semilattice_inf.dual_dual (α : Type*) [H : semilattice_inf α] : order_dual.semilattice_inf (order_dual α) = H := semilattice_inf.ext $ λ _ _, iff.rfl end semilattice_inf /-- A type with a commutative, associative and idempotent binary `inf` operation has the structure of a meet-semilattice. The partial order is defined so that `a ≤ b` unfolds to `b ⊓ a = a`; cf. `inf_eq_right`. -/ def semilattice_inf.mk' {α : Type*} [has_inf α] (inf_comm : ∀ (a b : α), a ⊓ b = b ⊓ a) (inf_assoc : ∀ (a b c : α), a ⊓ b ⊓ c = a ⊓ (b ⊓ c)) (inf_idem : ∀ (a : α), a ⊓ a = a) : semilattice_inf α := begin haveI : semilattice_sup (order_dual α) := semilattice_sup.mk' inf_comm inf_assoc inf_idem, haveI i := order_dual.semilattice_inf (order_dual α), exact i, end /-! ### Lattices -/ /-- A lattice is a join-semilattice which is also a meet-semilattice. -/ class lattice (α : Type u) extends semilattice_sup α, semilattice_inf α instance (α) [lattice α] : lattice (order_dual α) := { .. order_dual.semilattice_sup α, .. order_dual.semilattice_inf α } /-- The partial orders from `semilattice_sup_mk'` and `semilattice_inf_mk'` agree if `sup` and `inf` satisfy the lattice absorption laws `sup_inf_self` (`a ⊔ a ⊓ b = a`) and `inf_sup_self` (`a ⊓ (a ⊔ b) = a`). -/ lemma semilattice_sup_mk'_partial_order_eq_semilattice_inf_mk'_partial_order {α : Type*} [has_sup α] [has_inf α] (sup_comm : ∀ (a b : α), a ⊔ b = b ⊔ a) (sup_assoc : ∀ (a b c : α), a ⊔ b ⊔ c = a ⊔ (b ⊔ c)) (sup_idem : ∀ (a : α), a ⊔ a = a) (inf_comm : ∀ (a b : α), a ⊓ b = b ⊓ a) (inf_assoc : ∀ (a b c : α), a ⊓ b ⊓ c = a ⊓ (b ⊓ c)) (inf_idem : ∀ (a : α), a ⊓ a = a) (sup_inf_self : ∀ (a b : α), a ⊔ a ⊓ b = a) (inf_sup_self : ∀ (a b : α), a ⊓ (a ⊔ b) = a) : @semilattice_sup.to_partial_order _ (semilattice_sup.mk' sup_comm sup_assoc sup_idem) = @semilattice_inf.to_partial_order _ (semilattice_inf.mk' inf_comm inf_assoc inf_idem) := partial_order.ext $ λ a b, show a ⊔ b = b ↔ b ⊓ a = a, from ⟨λ h, by rw [←h, inf_comm, inf_sup_self], λ h, by rw [←h, sup_comm, sup_inf_self]⟩ /-- A type with a pair of commutative and associative binary operations which satisfy two absorption laws relating the two operations has the structure of a lattice. The partial order is defined so that `a ≤ b` unfolds to `a ⊔ b = b`; cf. `sup_eq_right`. -/ def lattice.mk' {α : Type*} [has_sup α] [has_inf α] (sup_comm : ∀ (a b : α), a ⊔ b = b ⊔ a) (sup_assoc : ∀ (a b c : α), a ⊔ b ⊔ c = a ⊔ (b ⊔ c)) (inf_comm : ∀ (a b : α), a ⊓ b = b ⊓ a) (inf_assoc : ∀ (a b c : α), a ⊓ b ⊓ c = a ⊓ (b ⊓ c)) (sup_inf_self : ∀ (a b : α), a ⊔ a ⊓ b = a) (inf_sup_self : ∀ (a b : α), a ⊓ (a ⊔ b) = a) : lattice α := have sup_idem : ∀ (b : α), b ⊔ b = b := λ b, calc b ⊔ b = b ⊔ b ⊓ (b ⊔ b) : by rw inf_sup_self ... = b : by rw sup_inf_self, have inf_idem : ∀ (b : α), b ⊓ b = b := λ b, calc b ⊓ b = b ⊓ (b ⊔ b ⊓ b) : by rw sup_inf_self ... = b : by rw inf_sup_self, let semilatt_inf_inst := semilattice_inf.mk' inf_comm inf_assoc inf_idem, semilatt_sup_inst := semilattice_sup.mk' sup_comm sup_assoc sup_idem, -- here we help Lean to see that the two partial orders are equal partial_order_inst := @semilattice_sup.to_partial_order _ semilatt_sup_inst in have partial_order_eq : partial_order_inst = @semilattice_inf.to_partial_order _ semilatt_inf_inst := semilattice_sup_mk'_partial_order_eq_semilattice_inf_mk'_partial_order _ _ _ _ _ _ sup_inf_self inf_sup_self, { inf_le_left := λ a b, by { rw partial_order_eq, apply inf_le_left }, inf_le_right := λ a b, by { rw partial_order_eq, apply inf_le_right }, le_inf := λ a b c, by { rw partial_order_eq, apply le_inf }, ..partial_order_inst, ..semilatt_sup_inst, ..semilatt_inf_inst, } section lattice variables [lattice α] {a b c d : α} /-! #### Distributivity laws -/ /- TODO: better names? -/ theorem sup_inf_le : a ⊔ (b ⊓ c) ≤ (a ⊔ b) ⊓ (a ⊔ c) := le_inf (sup_le_sup_left inf_le_left _) (sup_le_sup_left inf_le_right _) theorem le_inf_sup : (a ⊓ b) ⊔ (a ⊓ c) ≤ a ⊓ (b ⊔ c) := sup_le (inf_le_inf_left _ le_sup_left) (inf_le_inf_left _ le_sup_right) theorem inf_sup_self : a ⊓ (a ⊔ b) = a := by simp theorem sup_inf_self : a ⊔ (a ⊓ b) = a := by simp theorem sup_eq_iff_inf_eq : a ⊔ b = b ↔ a ⊓ b = a := by rw [sup_eq_right, ←inf_eq_left] theorem lattice.ext {α} {A B : lattice α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B := begin have SS : @lattice.to_semilattice_sup α A = @lattice.to_semilattice_sup α B := semilattice_sup.ext H, have II := semilattice_inf.ext H, casesI A, casesI B, injection SS; injection II; congr' end end lattice /-! ### Distributive lattices -/ /-- A distributive lattice is a lattice that satisfies any of four equivalent distributive properties (of `sup` over `inf` or `inf` over `sup`, on the left or right). The definition here chooses `le_sup_inf`: `(x ⊔ y) ⊓ (x ⊔ z) ≤ x ⊔ (y ⊓ z)`. A classic example of a distributive lattice is the lattice of subsets of a set, and in fact this example is generic in the sense that every distributive lattice is realizable as a sublattice of a powerset lattice. -/ class distrib_lattice α extends lattice α := (le_sup_inf : ∀x y z : α, (x ⊔ y) ⊓ (x ⊔ z) ≤ x ⊔ (y ⊓ z)) /- TODO: alternative constructors from the other distributive properties, and perhaps a `tfae` statement -/ section distrib_lattice variables [distrib_lattice α] {x y z : α} theorem le_sup_inf : ∀{x y z : α}, (x ⊔ y) ⊓ (x ⊔ z) ≤ x ⊔ (y ⊓ z) := distrib_lattice.le_sup_inf theorem sup_inf_left : x ⊔ (y ⊓ z) = (x ⊔ y) ⊓ (x ⊔ z) := le_antisymm sup_inf_le le_sup_inf theorem sup_inf_right : (y ⊓ z) ⊔ x = (y ⊔ x) ⊓ (z ⊔ x) := by simp only [sup_inf_left, λy:α, @sup_comm α _ y x, eq_self_iff_true] theorem inf_sup_left : x ⊓ (y ⊔ z) = (x ⊓ y) ⊔ (x ⊓ z) := calc x ⊓ (y ⊔ z) = (x ⊓ (x ⊔ z)) ⊓ (y ⊔ z) : by rw [inf_sup_self] ... = x ⊓ ((x ⊓ y) ⊔ z) : by simp only [inf_assoc, sup_inf_right, eq_self_iff_true] ... = (x ⊔ (x ⊓ y)) ⊓ ((x ⊓ y) ⊔ z) : by rw [sup_inf_self] ... = ((x ⊓ y) ⊔ x) ⊓ ((x ⊓ y) ⊔ z) : by rw [sup_comm] ... = (x ⊓ y) ⊔ (x ⊓ z) : by rw [sup_inf_left] instance (α : Type*) [distrib_lattice α] : distrib_lattice (order_dual α) := { le_sup_inf := assume x y z, le_of_eq inf_sup_left.symm, .. order_dual.lattice α } theorem inf_sup_right : (y ⊔ z) ⊓ x = (y ⊓ x) ⊔ (z ⊓ x) := by simp only [inf_sup_left, λy:α, @inf_comm α _ y x, eq_self_iff_true] lemma le_of_inf_le_sup_le (h₁ : x ⊓ z ≤ y ⊓ z) (h₂ : x ⊔ z ≤ y ⊔ z) : x ≤ y := calc x ≤ (y ⊓ z) ⊔ x : le_sup_right ... = (y ⊔ x) ⊓ (x ⊔ z) : by rw [sup_inf_right, @sup_comm _ _ x] ... ≤ (y ⊔ x) ⊓ (y ⊔ z) : inf_le_inf_left _ h₂ ... = y ⊔ (x ⊓ z) : sup_inf_left.symm ... ≤ y ⊔ (y ⊓ z) : sup_le_sup_left h₁ _ ... ≤ _ : sup_le (le_refl y) inf_le_left lemma eq_of_inf_eq_sup_eq {α : Type u} [distrib_lattice α] {a b c : α} (h₁ : b ⊓ a = c ⊓ a) (h₂ : b ⊔ a = c ⊔ a) : b = c := le_antisymm (le_of_inf_le_sup_le (le_of_eq h₁) (le_of_eq h₂)) (le_of_inf_le_sup_le (le_of_eq h₁.symm) (le_of_eq h₂.symm)) end distrib_lattice /-! ### Lattices derived from linear orders -/ @[priority 100] -- see Note [lower instance priority] instance lattice_of_linear_order {α : Type u} [o : linear_order α] : lattice α := { sup := max, le_sup_left := le_max_left, le_sup_right := le_max_right, sup_le := assume a b c, max_le, inf := min, inf_le_left := min_le_left, inf_le_right := min_le_right, le_inf := assume a b c, le_min, ..o } theorem sup_eq_max [linear_order α] {x y : α} : x ⊔ y = max x y := rfl theorem inf_eq_min [linear_order α] {x y : α} : x ⊓ y = min x y := rfl @[priority 100] -- see Note [lower instance priority] instance distrib_lattice_of_linear_order {α : Type u} [o : linear_order α] : distrib_lattice α := { le_sup_inf := assume a b c, match le_total b c with | or.inl h := inf_le_left_of_le $ sup_le_sup_left (le_inf (le_refl b) h) _ | or.inr h := inf_le_right_of_le $ sup_le_sup_left (le_inf h (le_refl c)) _ end, ..lattice_of_linear_order } instance nat.distrib_lattice : distrib_lattice ℕ := by apply_instance /-! ### Monotone functions and lattices -/ namespace monotone lemma le_map_sup [semilattice_sup α] [semilattice_sup β] {f : α → β} (h : monotone f) (x y : α) : f x ⊔ f y ≤ f (x ⊔ y) := sup_le (h le_sup_left) (h le_sup_right) lemma map_sup [semilattice_sup α] [is_total α (≤)] [semilattice_sup β] {f : α → β} (hf : monotone f) (x y : α) : f (x ⊔ y) = f x ⊔ f y := (is_total.total x y).elim (λ h : x ≤ y, by simp only [h, hf h, sup_of_le_right]) (λ h, by simp only [h, hf h, sup_of_le_left]) lemma map_inf_le [semilattice_inf α] [semilattice_inf β] {f : α → β} (h : monotone f) (x y : α) : f (x ⊓ y) ≤ f x ⊓ f y := le_inf (h inf_le_left) (h inf_le_right) lemma map_inf [semilattice_inf α] [is_total α (≤)] [semilattice_inf β] {f : α → β} (hf : monotone f) (x y : α) : f (x ⊓ y) = f x ⊓ f y := @monotone.map_sup (order_dual α) _ _ _ _ _ hf.order_dual x y end monotone /-! ### Products of (semi-)lattices -/ namespace prod variables (α β) instance [has_sup α] [has_sup β] : has_sup (α × β) := ⟨λp q, ⟨p.1 ⊔ q.1, p.2 ⊔ q.2⟩⟩ instance [has_inf α] [has_inf β] : has_inf (α × β) := ⟨λp q, ⟨p.1 ⊓ q.1, p.2 ⊓ q.2⟩⟩ instance [semilattice_sup α] [semilattice_sup β] : semilattice_sup (α × β) := { sup_le := assume a b c h₁ h₂, ⟨sup_le h₁.1 h₂.1, sup_le h₁.2 h₂.2⟩, le_sup_left := assume a b, ⟨le_sup_left, le_sup_left⟩, le_sup_right := assume a b, ⟨le_sup_right, le_sup_right⟩, .. prod.partial_order α β, .. prod.has_sup α β } instance [semilattice_inf α] [semilattice_inf β] : semilattice_inf (α × β) := { le_inf := assume a b c h₁ h₂, ⟨le_inf h₁.1 h₂.1, le_inf h₁.2 h₂.2⟩, inf_le_left := assume a b, ⟨inf_le_left, inf_le_left⟩, inf_le_right := assume a b, ⟨inf_le_right, inf_le_right⟩, .. prod.partial_order α β, .. prod.has_inf α β } instance [lattice α] [lattice β] : lattice (α × β) := { .. prod.semilattice_inf α β, .. prod.semilattice_sup α β } instance [distrib_lattice α] [distrib_lattice β] : distrib_lattice (α × β) := { le_sup_inf := assume a b c, ⟨le_sup_inf, le_sup_inf⟩, .. prod.lattice α β } end prod /-! ### Subtypes of (semi-)lattices -/ namespace subtype /-- A subtype forms a `⊔`-semilattice if `⊔` preserves the property. -/ protected def semilattice_sup [semilattice_sup α] {P : α → Prop} (Psup : ∀⦃x y⦄, P x → P y → P (x ⊔ y)) : semilattice_sup {x : α // P x} := { sup := λ x y, ⟨x.1 ⊔ y.1, Psup x.2 y.2⟩, le_sup_left := λ x y, @le_sup_left _ _ (x : α) y, le_sup_right := λ x y, @le_sup_right _ _ (x : α) y, sup_le := λ x y z h1 h2, @sup_le α _ _ _ _ h1 h2, ..subtype.partial_order P } /-- A subtype forms a `⊓`-semilattice if `⊓` preserves the property. -/ protected def semilattice_inf [semilattice_inf α] {P : α → Prop} (Pinf : ∀⦃x y⦄, P x → P y → P (x ⊓ y)) : semilattice_inf {x : α // P x} := { inf := λ x y, ⟨x.1 ⊓ y.1, Pinf x.2 y.2⟩, inf_le_left := λ x y, @inf_le_left _ _ (x : α) y, inf_le_right := λ x y, @inf_le_right _ _ (x : α) y, le_inf := λ x y z h1 h2, @le_inf α _ _ _ _ h1 h2, ..subtype.partial_order P } /-- A subtype forms a lattice if `⊔` and `⊓` preserve the property. -/ protected def lattice [lattice α] {P : α → Prop} (Psup : ∀⦃x y⦄, P x → P y → P (x ⊔ y)) (Pinf : ∀⦃x y⦄, P x → P y → P (x ⊓ y)) : lattice {x : α // P x} := { ..subtype.semilattice_inf Pinf, ..subtype.semilattice_sup Psup } end subtype
bb128cf7dd38e2d53ae7cf21cdd5dfd4ea5d6adc
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/intro0.lean
ad78ca6700aaaebb016fde86f1f22b4af6d84b06
[ "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
73
lean
example (a b : nat) : a = b → a = b := begin intro, assumption end